1 /*-
2 * Implementation of the Common Access Method Transport (XPT) layer.
3 *
4 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
5 *
6 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
7 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification, immediately at the beginning of the file.
16 * 2. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include "opt_printf.h"
33
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD: stable/12/sys/cam/cam_xpt.c 368104 2020-11-27 13:25:12Z mav $");
36
37 #include <sys/param.h>
38 #include <sys/bio.h>
39 #include <sys/bus.h>
40 #include <sys/systm.h>
41 #include <sys/types.h>
42 #include <sys/malloc.h>
43 #include <sys/kernel.h>
44 #include <sys/time.h>
45 #include <sys/conf.h>
46 #include <sys/fcntl.h>
47 #include <sys/proc.h>
48 #include <sys/sbuf.h>
49 #include <sys/smp.h>
50 #include <sys/taskqueue.h>
51
52 #include <sys/lock.h>
53 #include <sys/mutex.h>
54 #include <sys/sysctl.h>
55 #include <sys/kthread.h>
56
57 #include <cam/cam.h>
58 #include <cam/cam_ccb.h>
59 #include <cam/cam_iosched.h>
60 #include <cam/cam_periph.h>
61 #include <cam/cam_queue.h>
62 #include <cam/cam_sim.h>
63 #include <cam/cam_xpt.h>
64 #include <cam/cam_xpt_sim.h>
65 #include <cam/cam_xpt_periph.h>
66 #include <cam/cam_xpt_internal.h>
67 #include <cam/cam_debug.h>
68 #include <cam/cam_compat.h>
69
70 #include <cam/scsi/scsi_all.h>
71 #include <cam/scsi/scsi_message.h>
72 #include <cam/scsi/scsi_pass.h>
73
74 #include <machine/md_var.h> /* geometry translation */
75 #include <machine/stdarg.h> /* for xpt_print below */
76
77 #include "opt_cam.h"
78
79 /* Wild guess based on not wanting to grow the stack too much */
80 #define XPT_PRINT_MAXLEN 512
81 #ifdef PRINTF_BUFR_SIZE
82 #define XPT_PRINT_LEN PRINTF_BUFR_SIZE
83 #else
84 #define XPT_PRINT_LEN 128
85 #endif
86 _Static_assert(XPT_PRINT_LEN <= XPT_PRINT_MAXLEN, "XPT_PRINT_LEN is too large");
87
88 /*
89 * This is the maximum number of high powered commands (e.g. start unit)
90 * that can be outstanding at a particular time.
91 */
92 #ifndef CAM_MAX_HIGHPOWER
93 #define CAM_MAX_HIGHPOWER 4
94 #endif
95
96 /* Datastructures internal to the xpt layer */
97 MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
98 MALLOC_DEFINE(M_CAMDEV, "CAM DEV", "CAM devices");
99 MALLOC_DEFINE(M_CAMCCB, "CAM CCB", "CAM CCBs");
100 MALLOC_DEFINE(M_CAMPATH, "CAM path", "CAM paths");
101
102 struct xpt_softc {
103 uint32_t xpt_generation;
104
105 /* number of high powered commands that can go through right now */
106 struct mtx xpt_highpower_lock;
107 STAILQ_HEAD(highpowerlist, cam_ed) highpowerq;
108 int num_highpower;
109
110 /* queue for handling async rescan requests. */
111 TAILQ_HEAD(, ccb_hdr) ccb_scanq;
112 int buses_to_config;
113 int buses_config_done;
114 int announce_nosbuf;
115
116 /*
117 * Registered buses
118 *
119 * N.B., "busses" is an archaic spelling of "buses". In new code
120 * "buses" is preferred.
121 */
122 TAILQ_HEAD(,cam_eb) xpt_busses;
123 u_int bus_generation;
124
125 int boot_delay;
126 struct callout boot_callout;
127 struct task boot_task;
128 struct root_hold_token xpt_rootmount;
129
130 struct mtx xpt_topo_lock;
131 struct taskqueue *xpt_taskq;
132 };
133
134 typedef enum {
135 DM_RET_COPY = 0x01,
136 DM_RET_FLAG_MASK = 0x0f,
137 DM_RET_NONE = 0x00,
138 DM_RET_STOP = 0x10,
139 DM_RET_DESCEND = 0x20,
140 DM_RET_ERROR = 0x30,
141 DM_RET_ACTION_MASK = 0xf0
142 } dev_match_ret;
143
144 typedef enum {
145 XPT_DEPTH_BUS,
146 XPT_DEPTH_TARGET,
147 XPT_DEPTH_DEVICE,
148 XPT_DEPTH_PERIPH
149 } xpt_traverse_depth;
150
151 struct xpt_traverse_config {
152 xpt_traverse_depth depth;
153 void *tr_func;
154 void *tr_arg;
155 };
156
157 typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
158 typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
159 typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
160 typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
161 typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
162
163 /* Transport layer configuration information */
164 static struct xpt_softc xsoftc;
165
166 MTX_SYSINIT(xpt_topo_init, &xsoftc.xpt_topo_lock, "XPT topology lock", MTX_DEF);
167
168 SYSCTL_INT(_kern_cam, OID_AUTO, boot_delay, CTLFLAG_RDTUN,
169 &xsoftc.boot_delay, 0, "Bus registration wait time");
170 SYSCTL_UINT(_kern_cam, OID_AUTO, xpt_generation, CTLFLAG_RD,
171 &xsoftc.xpt_generation, 0, "CAM peripheral generation count");
172 SYSCTL_INT(_kern_cam, OID_AUTO, announce_nosbuf, CTLFLAG_RWTUN,
173 &xsoftc.announce_nosbuf, 0, "Don't use sbuf for announcements");
174
175 struct cam_doneq {
176 struct mtx_padalign cam_doneq_mtx;
177 STAILQ_HEAD(, ccb_hdr) cam_doneq;
178 int cam_doneq_sleep;
179 };
180
181 static struct cam_doneq cam_doneqs[MAXCPU];
182 static u_int __read_mostly cam_num_doneqs;
183 static struct proc *cam_proc;
184
185 SYSCTL_INT(_kern_cam, OID_AUTO, num_doneqs, CTLFLAG_RDTUN,
186 &cam_num_doneqs, 0, "Number of completion queues/threads");
187
188 struct cam_periph *xpt_periph;
189
190 static periph_init_t xpt_periph_init;
191
192 static struct periph_driver xpt_driver =
193 {
194 xpt_periph_init, "xpt",
195 TAILQ_HEAD_INITIALIZER(xpt_driver.units), /* generation */ 0,
196 CAM_PERIPH_DRV_EARLY
197 };
198
199 PERIPHDRIVER_DECLARE(xpt, xpt_driver);
200
201 static d_open_t xptopen;
202 static d_close_t xptclose;
203 static d_ioctl_t xptioctl;
204 static d_ioctl_t xptdoioctl;
205
206 static struct cdevsw xpt_cdevsw = {
207 .d_version = D_VERSION,
208 .d_flags = 0,
209 .d_open = xptopen,
210 .d_close = xptclose,
211 .d_ioctl = xptioctl,
212 .d_name = "xpt",
213 };
214
215 /* Storage for debugging datastructures */
216 struct cam_path *cam_dpath;
217 u_int32_t __read_mostly cam_dflags = CAM_DEBUG_FLAGS;
218 SYSCTL_UINT(_kern_cam, OID_AUTO, dflags, CTLFLAG_RWTUN,
219 &cam_dflags, 0, "Enabled debug flags");
220 u_int32_t cam_debug_delay = CAM_DEBUG_DELAY;
221 SYSCTL_UINT(_kern_cam, OID_AUTO, debug_delay, CTLFLAG_RWTUN,
222 &cam_debug_delay, 0, "Delay in us after each debug message");
223
224 /* Our boot-time initialization hook */
225 static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
226
227 static moduledata_t cam_moduledata = {
228 "cam",
229 cam_module_event_handler,
230 NULL
231 };
232
233 static int xpt_init(void *);
234
235 DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
236 MODULE_VERSION(cam, 1);
237
238
239 static void xpt_async_bcast(struct async_list *async_head,
240 u_int32_t async_code,
241 struct cam_path *path,
242 void *async_arg);
243 static path_id_t xptnextfreepathid(void);
244 static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
245 static union ccb *xpt_get_ccb(struct cam_periph *periph);
246 static union ccb *xpt_get_ccb_nowait(struct cam_periph *periph);
247 static void xpt_run_allocq(struct cam_periph *periph, int sleep);
248 static void xpt_run_allocq_task(void *context, int pending);
249 static void xpt_run_devq(struct cam_devq *devq);
250 static timeout_t xpt_release_devq_timeout;
251 static void xpt_release_simq_timeout(void *arg) __unused;
252 static void xpt_acquire_bus(struct cam_eb *bus);
253 static void xpt_release_bus(struct cam_eb *bus);
254 static uint32_t xpt_freeze_devq_device(struct cam_ed *dev, u_int count);
255 static int xpt_release_devq_device(struct cam_ed *dev, u_int count,
256 int run_queue);
257 static struct cam_et*
258 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
259 static void xpt_acquire_target(struct cam_et *target);
260 static void xpt_release_target(struct cam_et *target);
261 static struct cam_eb*
262 xpt_find_bus(path_id_t path_id);
263 static struct cam_et*
264 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
265 static struct cam_ed*
266 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
267 static void xpt_config(void *arg);
268 static void xpt_hold_boot_locked(void);
269 static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
270 u_int32_t new_priority);
271 static xpt_devicefunc_t xptpassannouncefunc;
272 static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
273 static void xptpoll(struct cam_sim *sim);
274 static void camisr_runqueue(void);
275 static void xpt_done_process(struct ccb_hdr *ccb_h);
276 static void xpt_done_td(void *);
277 static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
278 u_int num_patterns, struct cam_eb *bus);
279 static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
280 u_int num_patterns,
281 struct cam_ed *device);
282 static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
283 u_int num_patterns,
284 struct cam_periph *periph);
285 static xpt_busfunc_t xptedtbusfunc;
286 static xpt_targetfunc_t xptedttargetfunc;
287 static xpt_devicefunc_t xptedtdevicefunc;
288 static xpt_periphfunc_t xptedtperiphfunc;
289 static xpt_pdrvfunc_t xptplistpdrvfunc;
290 static xpt_periphfunc_t xptplistperiphfunc;
291 static int xptedtmatch(struct ccb_dev_match *cdm);
292 static int xptperiphlistmatch(struct ccb_dev_match *cdm);
293 static int xptbustraverse(struct cam_eb *start_bus,
294 xpt_busfunc_t *tr_func, void *arg);
295 static int xpttargettraverse(struct cam_eb *bus,
296 struct cam_et *start_target,
297 xpt_targetfunc_t *tr_func, void *arg);
298 static int xptdevicetraverse(struct cam_et *target,
299 struct cam_ed *start_device,
300 xpt_devicefunc_t *tr_func, void *arg);
301 static int xptperiphtraverse(struct cam_ed *device,
302 struct cam_periph *start_periph,
303 xpt_periphfunc_t *tr_func, void *arg);
304 static int xptpdrvtraverse(struct periph_driver **start_pdrv,
305 xpt_pdrvfunc_t *tr_func, void *arg);
306 static int xptpdperiphtraverse(struct periph_driver **pdrv,
307 struct cam_periph *start_periph,
308 xpt_periphfunc_t *tr_func,
309 void *arg);
310 static xpt_busfunc_t xptdefbusfunc;
311 static xpt_targetfunc_t xptdeftargetfunc;
312 static xpt_devicefunc_t xptdefdevicefunc;
313 static xpt_periphfunc_t xptdefperiphfunc;
314 static void xpt_finishconfig_task(void *context, int pending);
315 static void xpt_dev_async_default(u_int32_t async_code,
316 struct cam_eb *bus,
317 struct cam_et *target,
318 struct cam_ed *device,
319 void *async_arg);
320 static struct cam_ed * xpt_alloc_device_default(struct cam_eb *bus,
321 struct cam_et *target,
322 lun_id_t lun_id);
323 static xpt_devicefunc_t xptsetasyncfunc;
324 static xpt_busfunc_t xptsetasyncbusfunc;
325 static cam_status xptregister(struct cam_periph *periph,
326 void *arg);
327 static __inline int device_is_queued(struct cam_ed *device);
328
329 static __inline int
xpt_schedule_devq(struct cam_devq * devq,struct cam_ed * dev)330 xpt_schedule_devq(struct cam_devq *devq, struct cam_ed *dev)
331 {
332 int retval;
333
334 mtx_assert(&devq->send_mtx, MA_OWNED);
335 if ((dev->ccbq.queue.entries > 0) &&
336 (dev->ccbq.dev_openings > 0) &&
337 (dev->ccbq.queue.qfrozen_cnt == 0)) {
338 /*
339 * The priority of a device waiting for controller
340 * resources is that of the highest priority CCB
341 * enqueued.
342 */
343 retval =
344 xpt_schedule_dev(&devq->send_queue,
345 &dev->devq_entry,
346 CAMQ_GET_PRIO(&dev->ccbq.queue));
347 } else {
348 retval = 0;
349 }
350 return (retval);
351 }
352
353 static __inline int
device_is_queued(struct cam_ed * device)354 device_is_queued(struct cam_ed *device)
355 {
356 return (device->devq_entry.index != CAM_UNQUEUED_INDEX);
357 }
358
359 static void
xpt_periph_init()360 xpt_periph_init()
361 {
362 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
363 }
364
365 static int
xptopen(struct cdev * dev,int flags,int fmt,struct thread * td)366 xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
367 {
368
369 /*
370 * Only allow read-write access.
371 */
372 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
373 return(EPERM);
374
375 /*
376 * We don't allow nonblocking access.
377 */
378 if ((flags & O_NONBLOCK) != 0) {
379 printf("%s: can't do nonblocking access\n", devtoname(dev));
380 return(ENODEV);
381 }
382
383 return(0);
384 }
385
386 static int
xptclose(struct cdev * dev,int flag,int fmt,struct thread * td)387 xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
388 {
389
390 return(0);
391 }
392
393 /*
394 * Don't automatically grab the xpt softc lock here even though this is going
395 * through the xpt device. The xpt device is really just a back door for
396 * accessing other devices and SIMs, so the right thing to do is to grab
397 * the appropriate SIM lock once the bus/SIM is located.
398 */
399 static int
xptioctl(struct cdev * dev,u_long cmd,caddr_t addr,int flag,struct thread * td)400 xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
401 {
402 int error;
403
404 if ((error = xptdoioctl(dev, cmd, addr, flag, td)) == ENOTTY) {
405 error = cam_compat_ioctl(dev, cmd, addr, flag, td, xptdoioctl);
406 }
407 return (error);
408 }
409
410 static int
xptdoioctl(struct cdev * dev,u_long cmd,caddr_t addr,int flag,struct thread * td)411 xptdoioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
412 {
413 int error;
414
415 error = 0;
416
417 switch(cmd) {
418 /*
419 * For the transport layer CAMIOCOMMAND ioctl, we really only want
420 * to accept CCB types that don't quite make sense to send through a
421 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
422 * in the CAM spec.
423 */
424 case CAMIOCOMMAND: {
425 union ccb *ccb;
426 union ccb *inccb;
427 struct cam_eb *bus;
428
429 inccb = (union ccb *)addr;
430 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
431 if (inccb->ccb_h.func_code == XPT_SCSI_IO)
432 inccb->csio.bio = NULL;
433 #endif
434
435 if (inccb->ccb_h.flags & CAM_UNLOCKED)
436 return (EINVAL);
437
438 bus = xpt_find_bus(inccb->ccb_h.path_id);
439 if (bus == NULL)
440 return (EINVAL);
441
442 switch (inccb->ccb_h.func_code) {
443 case XPT_SCAN_BUS:
444 case XPT_RESET_BUS:
445 if (inccb->ccb_h.target_id != CAM_TARGET_WILDCARD ||
446 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
447 xpt_release_bus(bus);
448 return (EINVAL);
449 }
450 break;
451 case XPT_SCAN_TGT:
452 if (inccb->ccb_h.target_id == CAM_TARGET_WILDCARD ||
453 inccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
454 xpt_release_bus(bus);
455 return (EINVAL);
456 }
457 break;
458 default:
459 break;
460 }
461
462 switch(inccb->ccb_h.func_code) {
463 case XPT_SCAN_BUS:
464 case XPT_RESET_BUS:
465 case XPT_PATH_INQ:
466 case XPT_ENG_INQ:
467 case XPT_SCAN_LUN:
468 case XPT_SCAN_TGT:
469
470 ccb = xpt_alloc_ccb();
471
472 /*
473 * Create a path using the bus, target, and lun the
474 * user passed in.
475 */
476 if (xpt_create_path(&ccb->ccb_h.path, NULL,
477 inccb->ccb_h.path_id,
478 inccb->ccb_h.target_id,
479 inccb->ccb_h.target_lun) !=
480 CAM_REQ_CMP){
481 error = EINVAL;
482 xpt_free_ccb(ccb);
483 break;
484 }
485 /* Ensure all of our fields are correct */
486 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
487 inccb->ccb_h.pinfo.priority);
488 xpt_merge_ccb(ccb, inccb);
489 xpt_path_lock(ccb->ccb_h.path);
490 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
491 xpt_path_unlock(ccb->ccb_h.path);
492 bcopy(ccb, inccb, sizeof(union ccb));
493 xpt_free_path(ccb->ccb_h.path);
494 xpt_free_ccb(ccb);
495 break;
496
497 case XPT_DEBUG: {
498 union ccb ccb;
499
500 /*
501 * This is an immediate CCB, so it's okay to
502 * allocate it on the stack.
503 */
504
505 /*
506 * Create a path using the bus, target, and lun the
507 * user passed in.
508 */
509 if (xpt_create_path(&ccb.ccb_h.path, NULL,
510 inccb->ccb_h.path_id,
511 inccb->ccb_h.target_id,
512 inccb->ccb_h.target_lun) !=
513 CAM_REQ_CMP){
514 error = EINVAL;
515 break;
516 }
517 /* Ensure all of our fields are correct */
518 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
519 inccb->ccb_h.pinfo.priority);
520 xpt_merge_ccb(&ccb, inccb);
521 xpt_action(&ccb);
522 bcopy(&ccb, inccb, sizeof(union ccb));
523 xpt_free_path(ccb.ccb_h.path);
524 break;
525
526 }
527 case XPT_DEV_MATCH: {
528 struct cam_periph_map_info mapinfo;
529 struct cam_path *old_path;
530
531 /*
532 * We can't deal with physical addresses for this
533 * type of transaction.
534 */
535 if ((inccb->ccb_h.flags & CAM_DATA_MASK) !=
536 CAM_DATA_VADDR) {
537 error = EINVAL;
538 break;
539 }
540
541 /*
542 * Save this in case the caller had it set to
543 * something in particular.
544 */
545 old_path = inccb->ccb_h.path;
546
547 /*
548 * We really don't need a path for the matching
549 * code. The path is needed because of the
550 * debugging statements in xpt_action(). They
551 * assume that the CCB has a valid path.
552 */
553 inccb->ccb_h.path = xpt_periph->path;
554
555 bzero(&mapinfo, sizeof(mapinfo));
556
557 /*
558 * Map the pattern and match buffers into kernel
559 * virtual address space.
560 */
561 error = cam_periph_mapmem(inccb, &mapinfo, MAXPHYS);
562
563 if (error) {
564 inccb->ccb_h.path = old_path;
565 break;
566 }
567
568 /*
569 * This is an immediate CCB, we can send it on directly.
570 */
571 xpt_action(inccb);
572
573 /*
574 * Map the buffers back into user space.
575 */
576 cam_periph_unmapmem(inccb, &mapinfo);
577
578 inccb->ccb_h.path = old_path;
579
580 error = 0;
581 break;
582 }
583 default:
584 error = ENOTSUP;
585 break;
586 }
587 xpt_release_bus(bus);
588 break;
589 }
590 /*
591 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
592 * with the periphal driver name and unit name filled in. The other
593 * fields don't really matter as input. The passthrough driver name
594 * ("pass"), and unit number are passed back in the ccb. The current
595 * device generation number, and the index into the device peripheral
596 * driver list, and the status are also passed back. Note that
597 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
598 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
599 * (or rather should be) impossible for the device peripheral driver
600 * list to change since we look at the whole thing in one pass, and
601 * we do it with lock protection.
602 *
603 */
604 case CAMGETPASSTHRU: {
605 union ccb *ccb;
606 struct cam_periph *periph;
607 struct periph_driver **p_drv;
608 char *name;
609 u_int unit;
610 int base_periph_found;
611
612 ccb = (union ccb *)addr;
613 unit = ccb->cgdl.unit_number;
614 name = ccb->cgdl.periph_name;
615 base_periph_found = 0;
616 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
617 if (ccb->ccb_h.func_code == XPT_SCSI_IO)
618 ccb->csio.bio = NULL;
619 #endif
620
621 /*
622 * Sanity check -- make sure we don't get a null peripheral
623 * driver name.
624 */
625 if (*ccb->cgdl.periph_name == '\0') {
626 error = EINVAL;
627 break;
628 }
629
630 /* Keep the list from changing while we traverse it */
631 xpt_lock_buses();
632
633 /* first find our driver in the list of drivers */
634 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
635 if (strcmp((*p_drv)->driver_name, name) == 0)
636 break;
637
638 if (*p_drv == NULL) {
639 xpt_unlock_buses();
640 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
641 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
642 *ccb->cgdl.periph_name = '\0';
643 ccb->cgdl.unit_number = 0;
644 error = ENOENT;
645 break;
646 }
647
648 /*
649 * Run through every peripheral instance of this driver
650 * and check to see whether it matches the unit passed
651 * in by the user. If it does, get out of the loops and
652 * find the passthrough driver associated with that
653 * peripheral driver.
654 */
655 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
656 periph = TAILQ_NEXT(periph, unit_links)) {
657
658 if (periph->unit_number == unit)
659 break;
660 }
661 /*
662 * If we found the peripheral driver that the user passed
663 * in, go through all of the peripheral drivers for that
664 * particular device and look for a passthrough driver.
665 */
666 if (periph != NULL) {
667 struct cam_ed *device;
668 int i;
669
670 base_periph_found = 1;
671 device = periph->path->device;
672 for (i = 0, periph = SLIST_FIRST(&device->periphs);
673 periph != NULL;
674 periph = SLIST_NEXT(periph, periph_links), i++) {
675 /*
676 * Check to see whether we have a
677 * passthrough device or not.
678 */
679 if (strcmp(periph->periph_name, "pass") == 0) {
680 /*
681 * Fill in the getdevlist fields.
682 */
683 strlcpy(ccb->cgdl.periph_name,
684 periph->periph_name,
685 sizeof(ccb->cgdl.periph_name));
686 ccb->cgdl.unit_number =
687 periph->unit_number;
688 if (SLIST_NEXT(periph, periph_links))
689 ccb->cgdl.status =
690 CAM_GDEVLIST_MORE_DEVS;
691 else
692 ccb->cgdl.status =
693 CAM_GDEVLIST_LAST_DEVICE;
694 ccb->cgdl.generation =
695 device->generation;
696 ccb->cgdl.index = i;
697 /*
698 * Fill in some CCB header fields
699 * that the user may want.
700 */
701 ccb->ccb_h.path_id =
702 periph->path->bus->path_id;
703 ccb->ccb_h.target_id =
704 periph->path->target->target_id;
705 ccb->ccb_h.target_lun =
706 periph->path->device->lun_id;
707 ccb->ccb_h.status = CAM_REQ_CMP;
708 break;
709 }
710 }
711 }
712
713 /*
714 * If the periph is null here, one of two things has
715 * happened. The first possibility is that we couldn't
716 * find the unit number of the particular peripheral driver
717 * that the user is asking about. e.g. the user asks for
718 * the passthrough driver for "da11". We find the list of
719 * "da" peripherals all right, but there is no unit 11.
720 * The other possibility is that we went through the list
721 * of peripheral drivers attached to the device structure,
722 * but didn't find one with the name "pass". Either way,
723 * we return ENOENT, since we couldn't find something.
724 */
725 if (periph == NULL) {
726 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
727 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
728 *ccb->cgdl.periph_name = '\0';
729 ccb->cgdl.unit_number = 0;
730 error = ENOENT;
731 /*
732 * It is unfortunate that this is even necessary,
733 * but there are many, many clueless users out there.
734 * If this is true, the user is looking for the
735 * passthrough driver, but doesn't have one in his
736 * kernel.
737 */
738 if (base_periph_found == 1) {
739 printf("xptioctl: pass driver is not in the "
740 "kernel\n");
741 printf("xptioctl: put \"device pass\" in "
742 "your kernel config file\n");
743 }
744 }
745 xpt_unlock_buses();
746 break;
747 }
748 default:
749 error = ENOTTY;
750 break;
751 }
752
753 return(error);
754 }
755
756 static int
cam_module_event_handler(module_t mod,int what,void * arg)757 cam_module_event_handler(module_t mod, int what, void *arg)
758 {
759 int error;
760
761 switch (what) {
762 case MOD_LOAD:
763 if ((error = xpt_init(NULL)) != 0)
764 return (error);
765 break;
766 case MOD_UNLOAD:
767 return EBUSY;
768 default:
769 return EOPNOTSUPP;
770 }
771
772 return 0;
773 }
774
775 static struct xpt_proto *
xpt_proto_find(cam_proto proto)776 xpt_proto_find(cam_proto proto)
777 {
778 struct xpt_proto **pp;
779
780 SET_FOREACH(pp, cam_xpt_proto_set) {
781 if ((*pp)->proto == proto)
782 return *pp;
783 }
784
785 return NULL;
786 }
787
788 static void
xpt_rescan_done(struct cam_periph * periph,union ccb * done_ccb)789 xpt_rescan_done(struct cam_periph *periph, union ccb *done_ccb)
790 {
791
792 if (done_ccb->ccb_h.ppriv_ptr1 == NULL) {
793 xpt_free_path(done_ccb->ccb_h.path);
794 xpt_free_ccb(done_ccb);
795 } else {
796 done_ccb->ccb_h.cbfcnp = done_ccb->ccb_h.ppriv_ptr1;
797 (*done_ccb->ccb_h.cbfcnp)(periph, done_ccb);
798 }
799 xpt_release_boot();
800 }
801
802 /* thread to handle bus rescans */
803 static void
xpt_scanner_thread(void * dummy)804 xpt_scanner_thread(void *dummy)
805 {
806 union ccb *ccb;
807 struct mtx *mtx;
808 struct cam_ed *device;
809
810 xpt_lock_buses();
811 for (;;) {
812 if (TAILQ_EMPTY(&xsoftc.ccb_scanq))
813 msleep(&xsoftc.ccb_scanq, &xsoftc.xpt_topo_lock, PRIBIO,
814 "-", 0);
815 if ((ccb = (union ccb *)TAILQ_FIRST(&xsoftc.ccb_scanq)) != NULL) {
816 TAILQ_REMOVE(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
817 xpt_unlock_buses();
818
819 /*
820 * We need to lock the device's mutex which we use as
821 * the path mutex. We can't do it directly because the
822 * cam_path in the ccb may wind up going away because
823 * the path lock may be dropped and the path retired in
824 * the completion callback. We do this directly to keep
825 * the reference counts in cam_path sane. We also have
826 * to copy the device pointer because ccb_h.path may
827 * be freed in the callback.
828 */
829 mtx = xpt_path_mtx(ccb->ccb_h.path);
830 device = ccb->ccb_h.path->device;
831 xpt_acquire_device(device);
832 mtx_lock(mtx);
833 xpt_action(ccb);
834 mtx_unlock(mtx);
835 xpt_release_device(device);
836
837 xpt_lock_buses();
838 }
839 }
840 }
841
842 void
xpt_rescan(union ccb * ccb)843 xpt_rescan(union ccb *ccb)
844 {
845 struct ccb_hdr *hdr;
846
847 /* Prepare request */
848 if (ccb->ccb_h.path->target->target_id == CAM_TARGET_WILDCARD &&
849 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
850 ccb->ccb_h.func_code = XPT_SCAN_BUS;
851 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
852 ccb->ccb_h.path->device->lun_id == CAM_LUN_WILDCARD)
853 ccb->ccb_h.func_code = XPT_SCAN_TGT;
854 else if (ccb->ccb_h.path->target->target_id != CAM_TARGET_WILDCARD &&
855 ccb->ccb_h.path->device->lun_id != CAM_LUN_WILDCARD)
856 ccb->ccb_h.func_code = XPT_SCAN_LUN;
857 else {
858 xpt_print(ccb->ccb_h.path, "illegal scan path\n");
859 xpt_free_path(ccb->ccb_h.path);
860 xpt_free_ccb(ccb);
861 return;
862 }
863 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
864 ("xpt_rescan: func %#x %s\n", ccb->ccb_h.func_code,
865 xpt_action_name(ccb->ccb_h.func_code)));
866
867 ccb->ccb_h.ppriv_ptr1 = ccb->ccb_h.cbfcnp;
868 ccb->ccb_h.cbfcnp = xpt_rescan_done;
869 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, CAM_PRIORITY_XPT);
870 /* Don't make duplicate entries for the same paths. */
871 xpt_lock_buses();
872 if (ccb->ccb_h.ppriv_ptr1 == NULL) {
873 TAILQ_FOREACH(hdr, &xsoftc.ccb_scanq, sim_links.tqe) {
874 if (xpt_path_comp(hdr->path, ccb->ccb_h.path) == 0) {
875 wakeup(&xsoftc.ccb_scanq);
876 xpt_unlock_buses();
877 xpt_print(ccb->ccb_h.path, "rescan already queued\n");
878 xpt_free_path(ccb->ccb_h.path);
879 xpt_free_ccb(ccb);
880 return;
881 }
882 }
883 }
884 TAILQ_INSERT_TAIL(&xsoftc.ccb_scanq, &ccb->ccb_h, sim_links.tqe);
885 xpt_hold_boot_locked();
886 wakeup(&xsoftc.ccb_scanq);
887 xpt_unlock_buses();
888 }
889
890 /* Functions accessed by the peripheral drivers */
891 static int
xpt_init(void * dummy)892 xpt_init(void *dummy)
893 {
894 struct cam_sim *xpt_sim;
895 struct cam_path *path;
896 struct cam_devq *devq;
897 cam_status status;
898 int error, i;
899
900 TAILQ_INIT(&xsoftc.xpt_busses);
901 TAILQ_INIT(&xsoftc.ccb_scanq);
902 STAILQ_INIT(&xsoftc.highpowerq);
903 xsoftc.num_highpower = CAM_MAX_HIGHPOWER;
904
905 mtx_init(&xsoftc.xpt_highpower_lock, "XPT highpower lock", NULL, MTX_DEF);
906 xsoftc.xpt_taskq = taskqueue_create("CAM XPT task", M_WAITOK,
907 taskqueue_thread_enqueue, /*context*/&xsoftc.xpt_taskq);
908
909 #ifdef CAM_BOOT_DELAY
910 /*
911 * Override this value at compile time to assist our users
912 * who don't use loader to boot a kernel.
913 */
914 xsoftc.boot_delay = CAM_BOOT_DELAY;
915 #endif
916
917 /*
918 * The xpt layer is, itself, the equivalent of a SIM.
919 * Allow 16 ccbs in the ccb pool for it. This should
920 * give decent parallelism when we probe buses and
921 * perform other XPT functions.
922 */
923 devq = cam_simq_alloc(16);
924 xpt_sim = cam_sim_alloc(xptaction,
925 xptpoll,
926 "xpt",
927 /*softc*/NULL,
928 /*unit*/0,
929 /*mtx*/NULL,
930 /*max_dev_transactions*/0,
931 /*max_tagged_dev_transactions*/0,
932 devq);
933 if (xpt_sim == NULL)
934 return (ENOMEM);
935
936 if ((status = xpt_bus_register(xpt_sim, NULL, 0)) != CAM_SUCCESS) {
937 printf("xpt_init: xpt_bus_register failed with status %#x,"
938 " failing attach\n", status);
939 return (EINVAL);
940 }
941
942 /*
943 * Looking at the XPT from the SIM layer, the XPT is
944 * the equivalent of a peripheral driver. Allocate
945 * a peripheral driver entry for us.
946 */
947 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
948 CAM_TARGET_WILDCARD,
949 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
950 printf("xpt_init: xpt_create_path failed with status %#x,"
951 " failing attach\n", status);
952 return (EINVAL);
953 }
954 xpt_path_lock(path);
955 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
956 path, NULL, 0, xpt_sim);
957 xpt_path_unlock(path);
958 xpt_free_path(path);
959
960 if (cam_num_doneqs < 1)
961 cam_num_doneqs = 1 + mp_ncpus / 6;
962 else if (cam_num_doneqs > MAXCPU)
963 cam_num_doneqs = MAXCPU;
964 for (i = 0; i < cam_num_doneqs; i++) {
965 mtx_init(&cam_doneqs[i].cam_doneq_mtx, "CAM doneq", NULL,
966 MTX_DEF);
967 STAILQ_INIT(&cam_doneqs[i].cam_doneq);
968 error = kproc_kthread_add(xpt_done_td, &cam_doneqs[i],
969 &cam_proc, NULL, 0, 0, "cam", "doneq%d", i);
970 if (error != 0) {
971 cam_num_doneqs = i;
972 break;
973 }
974 }
975 if (cam_num_doneqs < 1) {
976 printf("xpt_init: Cannot init completion queues "
977 "- failing attach\n");
978 return (ENOMEM);
979 }
980
981 /*
982 * Register a callback for when interrupts are enabled.
983 */
984 config_intrhook_oneshot(xpt_config, NULL);
985
986 return (0);
987 }
988
989 static cam_status
xptregister(struct cam_periph * periph,void * arg)990 xptregister(struct cam_periph *periph, void *arg)
991 {
992 struct cam_sim *xpt_sim;
993
994 if (periph == NULL) {
995 printf("xptregister: periph was NULL!!\n");
996 return(CAM_REQ_CMP_ERR);
997 }
998
999 xpt_sim = (struct cam_sim *)arg;
1000 xpt_sim->softc = periph;
1001 xpt_periph = periph;
1002 periph->softc = NULL;
1003
1004 return(CAM_REQ_CMP);
1005 }
1006
1007 int32_t
xpt_add_periph(struct cam_periph * periph)1008 xpt_add_periph(struct cam_periph *periph)
1009 {
1010 struct cam_ed *device;
1011 int32_t status;
1012
1013 TASK_INIT(&periph->periph_run_task, 0, xpt_run_allocq_task, periph);
1014 device = periph->path->device;
1015 status = CAM_REQ_CMP;
1016 if (device != NULL) {
1017 mtx_lock(&device->target->bus->eb_mtx);
1018 device->generation++;
1019 SLIST_INSERT_HEAD(&device->periphs, periph, periph_links);
1020 mtx_unlock(&device->target->bus->eb_mtx);
1021 atomic_add_32(&xsoftc.xpt_generation, 1);
1022 }
1023
1024 return (status);
1025 }
1026
1027 void
xpt_remove_periph(struct cam_periph * periph)1028 xpt_remove_periph(struct cam_periph *periph)
1029 {
1030 struct cam_ed *device;
1031
1032 device = periph->path->device;
1033 if (device != NULL) {
1034 mtx_lock(&device->target->bus->eb_mtx);
1035 device->generation++;
1036 SLIST_REMOVE(&device->periphs, periph, cam_periph, periph_links);
1037 mtx_unlock(&device->target->bus->eb_mtx);
1038 atomic_add_32(&xsoftc.xpt_generation, 1);
1039 }
1040 }
1041
1042
1043 void
xpt_announce_periph(struct cam_periph * periph,char * announce_string)1044 xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1045 {
1046 struct cam_path *path = periph->path;
1047 struct xpt_proto *proto;
1048
1049 cam_periph_assert(periph, MA_OWNED);
1050 periph->flags |= CAM_PERIPH_ANNOUNCED;
1051
1052 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1053 periph->periph_name, periph->unit_number,
1054 path->bus->sim->sim_name,
1055 path->bus->sim->unit_number,
1056 path->bus->sim->bus_id,
1057 path->bus->path_id,
1058 path->target->target_id,
1059 (uintmax_t)path->device->lun_id);
1060 printf("%s%d: ", periph->periph_name, periph->unit_number);
1061 proto = xpt_proto_find(path->device->protocol);
1062 if (proto)
1063 proto->ops->announce(path->device);
1064 else
1065 printf("%s%d: Unknown protocol device %d\n",
1066 periph->periph_name, periph->unit_number,
1067 path->device->protocol);
1068 if (path->device->serial_num_len > 0) {
1069 /* Don't wrap the screen - print only the first 60 chars */
1070 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1071 periph->unit_number, path->device->serial_num);
1072 }
1073 /* Announce transport details. */
1074 path->bus->xport->ops->announce(periph);
1075 /* Announce command queueing. */
1076 if (path->device->inq_flags & SID_CmdQue
1077 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1078 printf("%s%d: Command Queueing enabled\n",
1079 periph->periph_name, periph->unit_number);
1080 }
1081 /* Announce caller's details if they've passed in. */
1082 if (announce_string != NULL)
1083 printf("%s%d: %s\n", periph->periph_name,
1084 periph->unit_number, announce_string);
1085 }
1086
1087 void
xpt_announce_periph_sbuf(struct cam_periph * periph,struct sbuf * sb,char * announce_string)1088 xpt_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb,
1089 char *announce_string)
1090 {
1091 struct cam_path *path = periph->path;
1092 struct xpt_proto *proto;
1093
1094 cam_periph_assert(periph, MA_OWNED);
1095 periph->flags |= CAM_PERIPH_ANNOUNCED;
1096
1097 /* Fall back to the non-sbuf method if necessary */
1098 if (xsoftc.announce_nosbuf != 0) {
1099 xpt_announce_periph(periph, announce_string);
1100 return;
1101 }
1102 proto = xpt_proto_find(path->device->protocol);
1103 if (((proto != NULL) && (proto->ops->announce_sbuf == NULL)) ||
1104 (path->bus->xport->ops->announce_sbuf == NULL)) {
1105 xpt_announce_periph(periph, announce_string);
1106 return;
1107 }
1108
1109 sbuf_printf(sb, "%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1110 periph->periph_name, periph->unit_number,
1111 path->bus->sim->sim_name,
1112 path->bus->sim->unit_number,
1113 path->bus->sim->bus_id,
1114 path->bus->path_id,
1115 path->target->target_id,
1116 (uintmax_t)path->device->lun_id);
1117 sbuf_printf(sb, "%s%d: ", periph->periph_name, periph->unit_number);
1118
1119 if (proto)
1120 proto->ops->announce_sbuf(path->device, sb);
1121 else
1122 sbuf_printf(sb, "%s%d: Unknown protocol device %d\n",
1123 periph->periph_name, periph->unit_number,
1124 path->device->protocol);
1125 if (path->device->serial_num_len > 0) {
1126 /* Don't wrap the screen - print only the first 60 chars */
1127 sbuf_printf(sb, "%s%d: Serial Number %.60s\n",
1128 periph->periph_name, periph->unit_number,
1129 path->device->serial_num);
1130 }
1131 /* Announce transport details. */
1132 path->bus->xport->ops->announce_sbuf(periph, sb);
1133 /* Announce command queueing. */
1134 if (path->device->inq_flags & SID_CmdQue
1135 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1136 sbuf_printf(sb, "%s%d: Command Queueing enabled\n",
1137 periph->periph_name, periph->unit_number);
1138 }
1139 /* Announce caller's details if they've passed in. */
1140 if (announce_string != NULL)
1141 sbuf_printf(sb, "%s%d: %s\n", periph->periph_name,
1142 periph->unit_number, announce_string);
1143 }
1144
1145 void
xpt_announce_quirks(struct cam_periph * periph,int quirks,char * bit_string)1146 xpt_announce_quirks(struct cam_periph *periph, int quirks, char *bit_string)
1147 {
1148 if (quirks != 0) {
1149 printf("%s%d: quirks=0x%b\n", periph->periph_name,
1150 periph->unit_number, quirks, bit_string);
1151 }
1152 }
1153
1154 void
xpt_announce_quirks_sbuf(struct cam_periph * periph,struct sbuf * sb,int quirks,char * bit_string)1155 xpt_announce_quirks_sbuf(struct cam_periph *periph, struct sbuf *sb,
1156 int quirks, char *bit_string)
1157 {
1158 if (xsoftc.announce_nosbuf != 0) {
1159 xpt_announce_quirks(periph, quirks, bit_string);
1160 return;
1161 }
1162
1163 if (quirks != 0) {
1164 sbuf_printf(sb, "%s%d: quirks=0x%b\n", periph->periph_name,
1165 periph->unit_number, quirks, bit_string);
1166 }
1167 }
1168
1169 void
xpt_denounce_periph(struct cam_periph * periph)1170 xpt_denounce_periph(struct cam_periph *periph)
1171 {
1172 struct cam_path *path = periph->path;
1173 struct xpt_proto *proto;
1174
1175 cam_periph_assert(periph, MA_OWNED);
1176 printf("%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1177 periph->periph_name, periph->unit_number,
1178 path->bus->sim->sim_name,
1179 path->bus->sim->unit_number,
1180 path->bus->sim->bus_id,
1181 path->bus->path_id,
1182 path->target->target_id,
1183 (uintmax_t)path->device->lun_id);
1184 printf("%s%d: ", periph->periph_name, periph->unit_number);
1185 proto = xpt_proto_find(path->device->protocol);
1186 if (proto)
1187 proto->ops->denounce(path->device);
1188 else
1189 printf("%s%d: Unknown protocol device %d\n",
1190 periph->periph_name, periph->unit_number,
1191 path->device->protocol);
1192 if (path->device->serial_num_len > 0)
1193 printf(" s/n %.60s", path->device->serial_num);
1194 printf(" detached\n");
1195 }
1196
1197 void
xpt_denounce_periph_sbuf(struct cam_periph * periph,struct sbuf * sb)1198 xpt_denounce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb)
1199 {
1200 struct cam_path *path = periph->path;
1201 struct xpt_proto *proto;
1202
1203 cam_periph_assert(periph, MA_OWNED);
1204
1205 /* Fall back to the non-sbuf method if necessary */
1206 if (xsoftc.announce_nosbuf != 0) {
1207 xpt_denounce_periph(periph);
1208 return;
1209 }
1210 proto = xpt_proto_find(path->device->protocol);
1211 if ((proto != NULL) && (proto->ops->denounce_sbuf == NULL)) {
1212 xpt_denounce_periph(periph);
1213 return;
1214 }
1215
1216 sbuf_printf(sb, "%s%d at %s%d bus %d scbus%d target %d lun %jx\n",
1217 periph->periph_name, periph->unit_number,
1218 path->bus->sim->sim_name,
1219 path->bus->sim->unit_number,
1220 path->bus->sim->bus_id,
1221 path->bus->path_id,
1222 path->target->target_id,
1223 (uintmax_t)path->device->lun_id);
1224 sbuf_printf(sb, "%s%d: ", periph->periph_name, periph->unit_number);
1225
1226 if (proto)
1227 proto->ops->denounce_sbuf(path->device, sb);
1228 else
1229 sbuf_printf(sb, "%s%d: Unknown protocol device %d\n",
1230 periph->periph_name, periph->unit_number,
1231 path->device->protocol);
1232 if (path->device->serial_num_len > 0)
1233 sbuf_printf(sb, " s/n %.60s", path->device->serial_num);
1234 sbuf_printf(sb, " detached\n");
1235 }
1236
1237 int
xpt_getattr(char * buf,size_t len,const char * attr,struct cam_path * path)1238 xpt_getattr(char *buf, size_t len, const char *attr, struct cam_path *path)
1239 {
1240 int ret = -1, l, o;
1241 struct ccb_dev_advinfo cdai;
1242 struct scsi_vpd_device_id *did;
1243 struct scsi_vpd_id_descriptor *idd;
1244
1245 xpt_path_assert(path, MA_OWNED);
1246
1247 memset(&cdai, 0, sizeof(cdai));
1248 xpt_setup_ccb(&cdai.ccb_h, path, CAM_PRIORITY_NORMAL);
1249 cdai.ccb_h.func_code = XPT_DEV_ADVINFO;
1250 cdai.flags = CDAI_FLAG_NONE;
1251 cdai.bufsiz = len;
1252 cdai.buf = buf;
1253
1254 if (!strcmp(attr, "GEOM::ident"))
1255 cdai.buftype = CDAI_TYPE_SERIAL_NUM;
1256 else if (!strcmp(attr, "GEOM::physpath"))
1257 cdai.buftype = CDAI_TYPE_PHYS_PATH;
1258 else if (strcmp(attr, "GEOM::lunid") == 0 ||
1259 strcmp(attr, "GEOM::lunname") == 0) {
1260 cdai.buftype = CDAI_TYPE_SCSI_DEVID;
1261 cdai.bufsiz = CAM_SCSI_DEVID_MAXLEN;
1262 cdai.buf = malloc(cdai.bufsiz, M_CAMXPT, M_NOWAIT);
1263 if (cdai.buf == NULL) {
1264 ret = ENOMEM;
1265 goto out;
1266 }
1267 } else
1268 goto out;
1269
1270 xpt_action((union ccb *)&cdai); /* can only be synchronous */
1271 if ((cdai.ccb_h.status & CAM_DEV_QFRZN) != 0)
1272 cam_release_devq(cdai.ccb_h.path, 0, 0, 0, FALSE);
1273 if (cdai.provsiz == 0)
1274 goto out;
1275 switch(cdai.buftype) {
1276 case CDAI_TYPE_SCSI_DEVID:
1277 did = (struct scsi_vpd_device_id *)cdai.buf;
1278 if (strcmp(attr, "GEOM::lunid") == 0) {
1279 idd = scsi_get_devid(did, cdai.provsiz,
1280 scsi_devid_is_lun_naa);
1281 if (idd == NULL)
1282 idd = scsi_get_devid(did, cdai.provsiz,
1283 scsi_devid_is_lun_eui64);
1284 if (idd == NULL)
1285 idd = scsi_get_devid(did, cdai.provsiz,
1286 scsi_devid_is_lun_uuid);
1287 if (idd == NULL)
1288 idd = scsi_get_devid(did, cdai.provsiz,
1289 scsi_devid_is_lun_md5);
1290 } else
1291 idd = NULL;
1292
1293 if (idd == NULL)
1294 idd = scsi_get_devid(did, cdai.provsiz,
1295 scsi_devid_is_lun_t10);
1296 if (idd == NULL)
1297 idd = scsi_get_devid(did, cdai.provsiz,
1298 scsi_devid_is_lun_name);
1299 if (idd == NULL)
1300 break;
1301
1302 ret = 0;
1303 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) ==
1304 SVPD_ID_CODESET_ASCII) {
1305 if (idd->length < len) {
1306 for (l = 0; l < idd->length; l++)
1307 buf[l] = idd->identifier[l] ?
1308 idd->identifier[l] : ' ';
1309 buf[l] = 0;
1310 } else
1311 ret = EFAULT;
1312 break;
1313 }
1314 if ((idd->proto_codeset & SVPD_ID_CODESET_MASK) ==
1315 SVPD_ID_CODESET_UTF8) {
1316 l = strnlen(idd->identifier, idd->length);
1317 if (l < len) {
1318 bcopy(idd->identifier, buf, l);
1319 buf[l] = 0;
1320 } else
1321 ret = EFAULT;
1322 break;
1323 }
1324 if ((idd->id_type & SVPD_ID_TYPE_MASK) ==
1325 SVPD_ID_TYPE_UUID && idd->identifier[0] == 0x10) {
1326 if ((idd->length - 2) * 2 + 4 >= len) {
1327 ret = EFAULT;
1328 break;
1329 }
1330 for (l = 2, o = 0; l < idd->length; l++) {
1331 if (l == 6 || l == 8 || l == 10 || l == 12)
1332 o += sprintf(buf + o, "-");
1333 o += sprintf(buf + o, "%02x",
1334 idd->identifier[l]);
1335 }
1336 break;
1337 }
1338 if (idd->length * 2 < len) {
1339 for (l = 0; l < idd->length; l++)
1340 sprintf(buf + l * 2, "%02x",
1341 idd->identifier[l]);
1342 } else
1343 ret = EFAULT;
1344 break;
1345 default:
1346 if (cdai.provsiz < len) {
1347 cdai.buf[cdai.provsiz] = 0;
1348 ret = 0;
1349 } else
1350 ret = EFAULT;
1351 break;
1352 }
1353
1354 out:
1355 if ((char *)cdai.buf != buf)
1356 free(cdai.buf, M_CAMXPT);
1357 return ret;
1358 }
1359
1360 static dev_match_ret
xptbusmatch(struct dev_match_pattern * patterns,u_int num_patterns,struct cam_eb * bus)1361 xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1362 struct cam_eb *bus)
1363 {
1364 dev_match_ret retval;
1365 u_int i;
1366
1367 retval = DM_RET_NONE;
1368
1369 /*
1370 * If we aren't given something to match against, that's an error.
1371 */
1372 if (bus == NULL)
1373 return(DM_RET_ERROR);
1374
1375 /*
1376 * If there are no match entries, then this bus matches no
1377 * matter what.
1378 */
1379 if ((patterns == NULL) || (num_patterns == 0))
1380 return(DM_RET_DESCEND | DM_RET_COPY);
1381
1382 for (i = 0; i < num_patterns; i++) {
1383 struct bus_match_pattern *cur_pattern;
1384
1385 /*
1386 * If the pattern in question isn't for a bus node, we
1387 * aren't interested. However, we do indicate to the
1388 * calling routine that we should continue descending the
1389 * tree, since the user wants to match against lower-level
1390 * EDT elements.
1391 */
1392 if (patterns[i].type != DEV_MATCH_BUS) {
1393 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1394 retval |= DM_RET_DESCEND;
1395 continue;
1396 }
1397
1398 cur_pattern = &patterns[i].pattern.bus_pattern;
1399
1400 /*
1401 * If they want to match any bus node, we give them any
1402 * device node.
1403 */
1404 if (cur_pattern->flags == BUS_MATCH_ANY) {
1405 /* set the copy flag */
1406 retval |= DM_RET_COPY;
1407
1408 /*
1409 * If we've already decided on an action, go ahead
1410 * and return.
1411 */
1412 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1413 return(retval);
1414 }
1415
1416 /*
1417 * Not sure why someone would do this...
1418 */
1419 if (cur_pattern->flags == BUS_MATCH_NONE)
1420 continue;
1421
1422 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1423 && (cur_pattern->path_id != bus->path_id))
1424 continue;
1425
1426 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1427 && (cur_pattern->bus_id != bus->sim->bus_id))
1428 continue;
1429
1430 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1431 && (cur_pattern->unit_number != bus->sim->unit_number))
1432 continue;
1433
1434 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1435 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1436 DEV_IDLEN) != 0))
1437 continue;
1438
1439 /*
1440 * If we get to this point, the user definitely wants
1441 * information on this bus. So tell the caller to copy the
1442 * data out.
1443 */
1444 retval |= DM_RET_COPY;
1445
1446 /*
1447 * If the return action has been set to descend, then we
1448 * know that we've already seen a non-bus matching
1449 * expression, therefore we need to further descend the tree.
1450 * This won't change by continuing around the loop, so we
1451 * go ahead and return. If we haven't seen a non-bus
1452 * matching expression, we keep going around the loop until
1453 * we exhaust the matching expressions. We'll set the stop
1454 * flag once we fall out of the loop.
1455 */
1456 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1457 return(retval);
1458 }
1459
1460 /*
1461 * If the return action hasn't been set to descend yet, that means
1462 * we haven't seen anything other than bus matching patterns. So
1463 * tell the caller to stop descending the tree -- the user doesn't
1464 * want to match against lower level tree elements.
1465 */
1466 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1467 retval |= DM_RET_STOP;
1468
1469 return(retval);
1470 }
1471
1472 static dev_match_ret
xptdevicematch(struct dev_match_pattern * patterns,u_int num_patterns,struct cam_ed * device)1473 xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1474 struct cam_ed *device)
1475 {
1476 dev_match_ret retval;
1477 u_int i;
1478
1479 retval = DM_RET_NONE;
1480
1481 /*
1482 * If we aren't given something to match against, that's an error.
1483 */
1484 if (device == NULL)
1485 return(DM_RET_ERROR);
1486
1487 /*
1488 * If there are no match entries, then this device matches no
1489 * matter what.
1490 */
1491 if ((patterns == NULL) || (num_patterns == 0))
1492 return(DM_RET_DESCEND | DM_RET_COPY);
1493
1494 for (i = 0; i < num_patterns; i++) {
1495 struct device_match_pattern *cur_pattern;
1496 struct scsi_vpd_device_id *device_id_page;
1497
1498 /*
1499 * If the pattern in question isn't for a device node, we
1500 * aren't interested.
1501 */
1502 if (patterns[i].type != DEV_MATCH_DEVICE) {
1503 if ((patterns[i].type == DEV_MATCH_PERIPH)
1504 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1505 retval |= DM_RET_DESCEND;
1506 continue;
1507 }
1508
1509 cur_pattern = &patterns[i].pattern.device_pattern;
1510
1511 /* Error out if mutually exclusive options are specified. */
1512 if ((cur_pattern->flags & (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1513 == (DEV_MATCH_INQUIRY|DEV_MATCH_DEVID))
1514 return(DM_RET_ERROR);
1515
1516 /*
1517 * If they want to match any device node, we give them any
1518 * device node.
1519 */
1520 if (cur_pattern->flags == DEV_MATCH_ANY)
1521 goto copy_dev_node;
1522
1523 /*
1524 * Not sure why someone would do this...
1525 */
1526 if (cur_pattern->flags == DEV_MATCH_NONE)
1527 continue;
1528
1529 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1530 && (cur_pattern->path_id != device->target->bus->path_id))
1531 continue;
1532
1533 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1534 && (cur_pattern->target_id != device->target->target_id))
1535 continue;
1536
1537 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1538 && (cur_pattern->target_lun != device->lun_id))
1539 continue;
1540
1541 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1542 && (cam_quirkmatch((caddr_t)&device->inq_data,
1543 (caddr_t)&cur_pattern->data.inq_pat,
1544 1, sizeof(cur_pattern->data.inq_pat),
1545 scsi_static_inquiry_match) == NULL))
1546 continue;
1547
1548 device_id_page = (struct scsi_vpd_device_id *)device->device_id;
1549 if (((cur_pattern->flags & DEV_MATCH_DEVID) != 0)
1550 && (device->device_id_len < SVPD_DEVICE_ID_HDR_LEN
1551 || scsi_devid_match((uint8_t *)device_id_page->desc_list,
1552 device->device_id_len
1553 - SVPD_DEVICE_ID_HDR_LEN,
1554 cur_pattern->data.devid_pat.id,
1555 cur_pattern->data.devid_pat.id_len) != 0))
1556 continue;
1557
1558 copy_dev_node:
1559 /*
1560 * If we get to this point, the user definitely wants
1561 * information on this device. So tell the caller to copy
1562 * the data out.
1563 */
1564 retval |= DM_RET_COPY;
1565
1566 /*
1567 * If the return action has been set to descend, then we
1568 * know that we've already seen a peripheral matching
1569 * expression, therefore we need to further descend the tree.
1570 * This won't change by continuing around the loop, so we
1571 * go ahead and return. If we haven't seen a peripheral
1572 * matching expression, we keep going around the loop until
1573 * we exhaust the matching expressions. We'll set the stop
1574 * flag once we fall out of the loop.
1575 */
1576 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1577 return(retval);
1578 }
1579
1580 /*
1581 * If the return action hasn't been set to descend yet, that means
1582 * we haven't seen any peripheral matching patterns. So tell the
1583 * caller to stop descending the tree -- the user doesn't want to
1584 * match against lower level tree elements.
1585 */
1586 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1587 retval |= DM_RET_STOP;
1588
1589 return(retval);
1590 }
1591
1592 /*
1593 * Match a single peripheral against any number of match patterns.
1594 */
1595 static dev_match_ret
xptperiphmatch(struct dev_match_pattern * patterns,u_int num_patterns,struct cam_periph * periph)1596 xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1597 struct cam_periph *periph)
1598 {
1599 dev_match_ret retval;
1600 u_int i;
1601
1602 /*
1603 * If we aren't given something to match against, that's an error.
1604 */
1605 if (periph == NULL)
1606 return(DM_RET_ERROR);
1607
1608 /*
1609 * If there are no match entries, then this peripheral matches no
1610 * matter what.
1611 */
1612 if ((patterns == NULL) || (num_patterns == 0))
1613 return(DM_RET_STOP | DM_RET_COPY);
1614
1615 /*
1616 * There aren't any nodes below a peripheral node, so there's no
1617 * reason to descend the tree any further.
1618 */
1619 retval = DM_RET_STOP;
1620
1621 for (i = 0; i < num_patterns; i++) {
1622 struct periph_match_pattern *cur_pattern;
1623
1624 /*
1625 * If the pattern in question isn't for a peripheral, we
1626 * aren't interested.
1627 */
1628 if (patterns[i].type != DEV_MATCH_PERIPH)
1629 continue;
1630
1631 cur_pattern = &patterns[i].pattern.periph_pattern;
1632
1633 /*
1634 * If they want to match on anything, then we will do so.
1635 */
1636 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
1637 /* set the copy flag */
1638 retval |= DM_RET_COPY;
1639
1640 /*
1641 * We've already set the return action to stop,
1642 * since there are no nodes below peripherals in
1643 * the tree.
1644 */
1645 return(retval);
1646 }
1647
1648 /*
1649 * Not sure why someone would do this...
1650 */
1651 if (cur_pattern->flags == PERIPH_MATCH_NONE)
1652 continue;
1653
1654 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
1655 && (cur_pattern->path_id != periph->path->bus->path_id))
1656 continue;
1657
1658 /*
1659 * For the target and lun id's, we have to make sure the
1660 * target and lun pointers aren't NULL. The xpt peripheral
1661 * has a wildcard target and device.
1662 */
1663 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
1664 && ((periph->path->target == NULL)
1665 ||(cur_pattern->target_id != periph->path->target->target_id)))
1666 continue;
1667
1668 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
1669 && ((periph->path->device == NULL)
1670 || (cur_pattern->target_lun != periph->path->device->lun_id)))
1671 continue;
1672
1673 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
1674 && (cur_pattern->unit_number != periph->unit_number))
1675 continue;
1676
1677 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
1678 && (strncmp(cur_pattern->periph_name, periph->periph_name,
1679 DEV_IDLEN) != 0))
1680 continue;
1681
1682 /*
1683 * If we get to this point, the user definitely wants
1684 * information on this peripheral. So tell the caller to
1685 * copy the data out.
1686 */
1687 retval |= DM_RET_COPY;
1688
1689 /*
1690 * The return action has already been set to stop, since
1691 * peripherals don't have any nodes below them in the EDT.
1692 */
1693 return(retval);
1694 }
1695
1696 /*
1697 * If we get to this point, the peripheral that was passed in
1698 * doesn't match any of the patterns.
1699 */
1700 return(retval);
1701 }
1702
1703 static int
xptedtbusfunc(struct cam_eb * bus,void * arg)1704 xptedtbusfunc(struct cam_eb *bus, void *arg)
1705 {
1706 struct ccb_dev_match *cdm;
1707 struct cam_et *target;
1708 dev_match_ret retval;
1709
1710 cdm = (struct ccb_dev_match *)arg;
1711
1712 /*
1713 * If our position is for something deeper in the tree, that means
1714 * that we've already seen this node. So, we keep going down.
1715 */
1716 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1717 && (cdm->pos.cookie.bus == bus)
1718 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1719 && (cdm->pos.cookie.target != NULL))
1720 retval = DM_RET_DESCEND;
1721 else
1722 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
1723
1724 /*
1725 * If we got an error, bail out of the search.
1726 */
1727 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1728 cdm->status = CAM_DEV_MATCH_ERROR;
1729 return(0);
1730 }
1731
1732 /*
1733 * If the copy flag is set, copy this bus out.
1734 */
1735 if (retval & DM_RET_COPY) {
1736 int spaceleft, j;
1737
1738 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1739 sizeof(struct dev_match_result));
1740
1741 /*
1742 * If we don't have enough space to put in another
1743 * match result, save our position and tell the
1744 * user there are more devices to check.
1745 */
1746 if (spaceleft < sizeof(struct dev_match_result)) {
1747 bzero(&cdm->pos, sizeof(cdm->pos));
1748 cdm->pos.position_type =
1749 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
1750
1751 cdm->pos.cookie.bus = bus;
1752 cdm->pos.generations[CAM_BUS_GENERATION]=
1753 xsoftc.bus_generation;
1754 cdm->status = CAM_DEV_MATCH_MORE;
1755 return(0);
1756 }
1757 j = cdm->num_matches;
1758 cdm->num_matches++;
1759 cdm->matches[j].type = DEV_MATCH_BUS;
1760 cdm->matches[j].result.bus_result.path_id = bus->path_id;
1761 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
1762 cdm->matches[j].result.bus_result.unit_number =
1763 bus->sim->unit_number;
1764 strlcpy(cdm->matches[j].result.bus_result.dev_name,
1765 bus->sim->sim_name,
1766 sizeof(cdm->matches[j].result.bus_result.dev_name));
1767 }
1768
1769 /*
1770 * If the user is only interested in buses, there's no
1771 * reason to descend to the next level in the tree.
1772 */
1773 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1774 return(1);
1775
1776 /*
1777 * If there is a target generation recorded, check it to
1778 * make sure the target list hasn't changed.
1779 */
1780 mtx_lock(&bus->eb_mtx);
1781 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1782 && (cdm->pos.cookie.bus == bus)
1783 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1784 && (cdm->pos.cookie.target != NULL)) {
1785 if ((cdm->pos.generations[CAM_TARGET_GENERATION] !=
1786 bus->generation)) {
1787 mtx_unlock(&bus->eb_mtx);
1788 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1789 return (0);
1790 }
1791 target = (struct cam_et *)cdm->pos.cookie.target;
1792 target->refcount++;
1793 } else
1794 target = NULL;
1795 mtx_unlock(&bus->eb_mtx);
1796
1797 return (xpttargettraverse(bus, target, xptedttargetfunc, arg));
1798 }
1799
1800 static int
xptedttargetfunc(struct cam_et * target,void * arg)1801 xptedttargetfunc(struct cam_et *target, void *arg)
1802 {
1803 struct ccb_dev_match *cdm;
1804 struct cam_eb *bus;
1805 struct cam_ed *device;
1806
1807 cdm = (struct ccb_dev_match *)arg;
1808 bus = target->bus;
1809
1810 /*
1811 * If there is a device list generation recorded, check it to
1812 * make sure the device list hasn't changed.
1813 */
1814 mtx_lock(&bus->eb_mtx);
1815 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1816 && (cdm->pos.cookie.bus == bus)
1817 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1818 && (cdm->pos.cookie.target == target)
1819 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1820 && (cdm->pos.cookie.device != NULL)) {
1821 if (cdm->pos.generations[CAM_DEV_GENERATION] !=
1822 target->generation) {
1823 mtx_unlock(&bus->eb_mtx);
1824 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1825 return(0);
1826 }
1827 device = (struct cam_ed *)cdm->pos.cookie.device;
1828 device->refcount++;
1829 } else
1830 device = NULL;
1831 mtx_unlock(&bus->eb_mtx);
1832
1833 return (xptdevicetraverse(target, device, xptedtdevicefunc, arg));
1834 }
1835
1836 static int
xptedtdevicefunc(struct cam_ed * device,void * arg)1837 xptedtdevicefunc(struct cam_ed *device, void *arg)
1838 {
1839 struct cam_eb *bus;
1840 struct cam_periph *periph;
1841 struct ccb_dev_match *cdm;
1842 dev_match_ret retval;
1843
1844 cdm = (struct ccb_dev_match *)arg;
1845 bus = device->target->bus;
1846
1847 /*
1848 * If our position is for something deeper in the tree, that means
1849 * that we've already seen this node. So, we keep going down.
1850 */
1851 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1852 && (cdm->pos.cookie.device == device)
1853 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1854 && (cdm->pos.cookie.periph != NULL))
1855 retval = DM_RET_DESCEND;
1856 else
1857 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
1858 device);
1859
1860 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1861 cdm->status = CAM_DEV_MATCH_ERROR;
1862 return(0);
1863 }
1864
1865 /*
1866 * If the copy flag is set, copy this device out.
1867 */
1868 if (retval & DM_RET_COPY) {
1869 int spaceleft, j;
1870
1871 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1872 sizeof(struct dev_match_result));
1873
1874 /*
1875 * If we don't have enough space to put in another
1876 * match result, save our position and tell the
1877 * user there are more devices to check.
1878 */
1879 if (spaceleft < sizeof(struct dev_match_result)) {
1880 bzero(&cdm->pos, sizeof(cdm->pos));
1881 cdm->pos.position_type =
1882 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1883 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
1884
1885 cdm->pos.cookie.bus = device->target->bus;
1886 cdm->pos.generations[CAM_BUS_GENERATION]=
1887 xsoftc.bus_generation;
1888 cdm->pos.cookie.target = device->target;
1889 cdm->pos.generations[CAM_TARGET_GENERATION] =
1890 device->target->bus->generation;
1891 cdm->pos.cookie.device = device;
1892 cdm->pos.generations[CAM_DEV_GENERATION] =
1893 device->target->generation;
1894 cdm->status = CAM_DEV_MATCH_MORE;
1895 return(0);
1896 }
1897 j = cdm->num_matches;
1898 cdm->num_matches++;
1899 cdm->matches[j].type = DEV_MATCH_DEVICE;
1900 cdm->matches[j].result.device_result.path_id =
1901 device->target->bus->path_id;
1902 cdm->matches[j].result.device_result.target_id =
1903 device->target->target_id;
1904 cdm->matches[j].result.device_result.target_lun =
1905 device->lun_id;
1906 cdm->matches[j].result.device_result.protocol =
1907 device->protocol;
1908 bcopy(&device->inq_data,
1909 &cdm->matches[j].result.device_result.inq_data,
1910 sizeof(struct scsi_inquiry_data));
1911 bcopy(&device->ident_data,
1912 &cdm->matches[j].result.device_result.ident_data,
1913 sizeof(struct ata_params));
1914
1915 /* Let the user know whether this device is unconfigured */
1916 if (device->flags & CAM_DEV_UNCONFIGURED)
1917 cdm->matches[j].result.device_result.flags =
1918 DEV_RESULT_UNCONFIGURED;
1919 else
1920 cdm->matches[j].result.device_result.flags =
1921 DEV_RESULT_NOFLAG;
1922 }
1923
1924 /*
1925 * If the user isn't interested in peripherals, don't descend
1926 * the tree any further.
1927 */
1928 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
1929 return(1);
1930
1931 /*
1932 * If there is a peripheral list generation recorded, make sure
1933 * it hasn't changed.
1934 */
1935 xpt_lock_buses();
1936 mtx_lock(&bus->eb_mtx);
1937 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
1938 && (cdm->pos.cookie.bus == bus)
1939 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
1940 && (cdm->pos.cookie.target == device->target)
1941 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
1942 && (cdm->pos.cookie.device == device)
1943 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
1944 && (cdm->pos.cookie.periph != NULL)) {
1945 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
1946 device->generation) {
1947 mtx_unlock(&bus->eb_mtx);
1948 xpt_unlock_buses();
1949 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
1950 return(0);
1951 }
1952 periph = (struct cam_periph *)cdm->pos.cookie.periph;
1953 periph->refcount++;
1954 } else
1955 periph = NULL;
1956 mtx_unlock(&bus->eb_mtx);
1957 xpt_unlock_buses();
1958
1959 return (xptperiphtraverse(device, periph, xptedtperiphfunc, arg));
1960 }
1961
1962 static int
xptedtperiphfunc(struct cam_periph * periph,void * arg)1963 xptedtperiphfunc(struct cam_periph *periph, void *arg)
1964 {
1965 struct ccb_dev_match *cdm;
1966 dev_match_ret retval;
1967
1968 cdm = (struct ccb_dev_match *)arg;
1969
1970 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
1971
1972 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
1973 cdm->status = CAM_DEV_MATCH_ERROR;
1974 return(0);
1975 }
1976
1977 /*
1978 * If the copy flag is set, copy this peripheral out.
1979 */
1980 if (retval & DM_RET_COPY) {
1981 int spaceleft, j;
1982 size_t l;
1983
1984 spaceleft = cdm->match_buf_len - (cdm->num_matches *
1985 sizeof(struct dev_match_result));
1986
1987 /*
1988 * If we don't have enough space to put in another
1989 * match result, save our position and tell the
1990 * user there are more devices to check.
1991 */
1992 if (spaceleft < sizeof(struct dev_match_result)) {
1993 bzero(&cdm->pos, sizeof(cdm->pos));
1994 cdm->pos.position_type =
1995 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
1996 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
1997 CAM_DEV_POS_PERIPH;
1998
1999 cdm->pos.cookie.bus = periph->path->bus;
2000 cdm->pos.generations[CAM_BUS_GENERATION]=
2001 xsoftc.bus_generation;
2002 cdm->pos.cookie.target = periph->path->target;
2003 cdm->pos.generations[CAM_TARGET_GENERATION] =
2004 periph->path->bus->generation;
2005 cdm->pos.cookie.device = periph->path->device;
2006 cdm->pos.generations[CAM_DEV_GENERATION] =
2007 periph->path->target->generation;
2008 cdm->pos.cookie.periph = periph;
2009 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2010 periph->path->device->generation;
2011 cdm->status = CAM_DEV_MATCH_MORE;
2012 return(0);
2013 }
2014
2015 j = cdm->num_matches;
2016 cdm->num_matches++;
2017 cdm->matches[j].type = DEV_MATCH_PERIPH;
2018 cdm->matches[j].result.periph_result.path_id =
2019 periph->path->bus->path_id;
2020 cdm->matches[j].result.periph_result.target_id =
2021 periph->path->target->target_id;
2022 cdm->matches[j].result.periph_result.target_lun =
2023 periph->path->device->lun_id;
2024 cdm->matches[j].result.periph_result.unit_number =
2025 periph->unit_number;
2026 l = sizeof(cdm->matches[j].result.periph_result.periph_name);
2027 strlcpy(cdm->matches[j].result.periph_result.periph_name,
2028 periph->periph_name, l);
2029 }
2030
2031 return(1);
2032 }
2033
2034 static int
xptedtmatch(struct ccb_dev_match * cdm)2035 xptedtmatch(struct ccb_dev_match *cdm)
2036 {
2037 struct cam_eb *bus;
2038 int ret;
2039
2040 cdm->num_matches = 0;
2041
2042 /*
2043 * Check the bus list generation. If it has changed, the user
2044 * needs to reset everything and start over.
2045 */
2046 xpt_lock_buses();
2047 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2048 && (cdm->pos.cookie.bus != NULL)) {
2049 if (cdm->pos.generations[CAM_BUS_GENERATION] !=
2050 xsoftc.bus_generation) {
2051 xpt_unlock_buses();
2052 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2053 return(0);
2054 }
2055 bus = (struct cam_eb *)cdm->pos.cookie.bus;
2056 bus->refcount++;
2057 } else
2058 bus = NULL;
2059 xpt_unlock_buses();
2060
2061 ret = xptbustraverse(bus, xptedtbusfunc, cdm);
2062
2063 /*
2064 * If we get back 0, that means that we had to stop before fully
2065 * traversing the EDT. It also means that one of the subroutines
2066 * has set the status field to the proper value. If we get back 1,
2067 * we've fully traversed the EDT and copied out any matching entries.
2068 */
2069 if (ret == 1)
2070 cdm->status = CAM_DEV_MATCH_LAST;
2071
2072 return(ret);
2073 }
2074
2075 static int
xptplistpdrvfunc(struct periph_driver ** pdrv,void * arg)2076 xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2077 {
2078 struct cam_periph *periph;
2079 struct ccb_dev_match *cdm;
2080
2081 cdm = (struct ccb_dev_match *)arg;
2082
2083 xpt_lock_buses();
2084 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2085 && (cdm->pos.cookie.pdrv == pdrv)
2086 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2087 && (cdm->pos.cookie.periph != NULL)) {
2088 if (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2089 (*pdrv)->generation) {
2090 xpt_unlock_buses();
2091 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2092 return(0);
2093 }
2094 periph = (struct cam_periph *)cdm->pos.cookie.periph;
2095 periph->refcount++;
2096 } else
2097 periph = NULL;
2098 xpt_unlock_buses();
2099
2100 return (xptpdperiphtraverse(pdrv, periph, xptplistperiphfunc, arg));
2101 }
2102
2103 static int
xptplistperiphfunc(struct cam_periph * periph,void * arg)2104 xptplistperiphfunc(struct cam_periph *periph, void *arg)
2105 {
2106 struct ccb_dev_match *cdm;
2107 dev_match_ret retval;
2108
2109 cdm = (struct ccb_dev_match *)arg;
2110
2111 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2112
2113 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2114 cdm->status = CAM_DEV_MATCH_ERROR;
2115 return(0);
2116 }
2117
2118 /*
2119 * If the copy flag is set, copy this peripheral out.
2120 */
2121 if (retval & DM_RET_COPY) {
2122 int spaceleft, j;
2123 size_t l;
2124
2125 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2126 sizeof(struct dev_match_result));
2127
2128 /*
2129 * If we don't have enough space to put in another
2130 * match result, save our position and tell the
2131 * user there are more devices to check.
2132 */
2133 if (spaceleft < sizeof(struct dev_match_result)) {
2134 struct periph_driver **pdrv;
2135
2136 pdrv = NULL;
2137 bzero(&cdm->pos, sizeof(cdm->pos));
2138 cdm->pos.position_type =
2139 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2140 CAM_DEV_POS_PERIPH;
2141
2142 /*
2143 * This may look a bit non-sensical, but it is
2144 * actually quite logical. There are very few
2145 * peripheral drivers, and bloating every peripheral
2146 * structure with a pointer back to its parent
2147 * peripheral driver linker set entry would cost
2148 * more in the long run than doing this quick lookup.
2149 */
2150 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2151 if (strcmp((*pdrv)->driver_name,
2152 periph->periph_name) == 0)
2153 break;
2154 }
2155
2156 if (*pdrv == NULL) {
2157 cdm->status = CAM_DEV_MATCH_ERROR;
2158 return(0);
2159 }
2160
2161 cdm->pos.cookie.pdrv = pdrv;
2162 /*
2163 * The periph generation slot does double duty, as
2164 * does the periph pointer slot. They are used for
2165 * both edt and pdrv lookups and positioning.
2166 */
2167 cdm->pos.cookie.periph = periph;
2168 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2169 (*pdrv)->generation;
2170 cdm->status = CAM_DEV_MATCH_MORE;
2171 return(0);
2172 }
2173
2174 j = cdm->num_matches;
2175 cdm->num_matches++;
2176 cdm->matches[j].type = DEV_MATCH_PERIPH;
2177 cdm->matches[j].result.periph_result.path_id =
2178 periph->path->bus->path_id;
2179
2180 /*
2181 * The transport layer peripheral doesn't have a target or
2182 * lun.
2183 */
2184 if (periph->path->target)
2185 cdm->matches[j].result.periph_result.target_id =
2186 periph->path->target->target_id;
2187 else
2188 cdm->matches[j].result.periph_result.target_id =
2189 CAM_TARGET_WILDCARD;
2190
2191 if (periph->path->device)
2192 cdm->matches[j].result.periph_result.target_lun =
2193 periph->path->device->lun_id;
2194 else
2195 cdm->matches[j].result.periph_result.target_lun =
2196 CAM_LUN_WILDCARD;
2197
2198 cdm->matches[j].result.periph_result.unit_number =
2199 periph->unit_number;
2200 l = sizeof(cdm->matches[j].result.periph_result.periph_name);
2201 strlcpy(cdm->matches[j].result.periph_result.periph_name,
2202 periph->periph_name, l);
2203 }
2204
2205 return(1);
2206 }
2207
2208 static int
xptperiphlistmatch(struct ccb_dev_match * cdm)2209 xptperiphlistmatch(struct ccb_dev_match *cdm)
2210 {
2211 int ret;
2212
2213 cdm->num_matches = 0;
2214
2215 /*
2216 * At this point in the edt traversal function, we check the bus
2217 * list generation to make sure that no buses have been added or
2218 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2219 * For the peripheral driver list traversal function, however, we
2220 * don't have to worry about new peripheral driver types coming or
2221 * going; they're in a linker set, and therefore can't change
2222 * without a recompile.
2223 */
2224
2225 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2226 && (cdm->pos.cookie.pdrv != NULL))
2227 ret = xptpdrvtraverse(
2228 (struct periph_driver **)cdm->pos.cookie.pdrv,
2229 xptplistpdrvfunc, cdm);
2230 else
2231 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2232
2233 /*
2234 * If we get back 0, that means that we had to stop before fully
2235 * traversing the peripheral driver tree. It also means that one of
2236 * the subroutines has set the status field to the proper value. If
2237 * we get back 1, we've fully traversed the EDT and copied out any
2238 * matching entries.
2239 */
2240 if (ret == 1)
2241 cdm->status = CAM_DEV_MATCH_LAST;
2242
2243 return(ret);
2244 }
2245
2246 static int
xptbustraverse(struct cam_eb * start_bus,xpt_busfunc_t * tr_func,void * arg)2247 xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2248 {
2249 struct cam_eb *bus, *next_bus;
2250 int retval;
2251
2252 retval = 1;
2253 if (start_bus)
2254 bus = start_bus;
2255 else {
2256 xpt_lock_buses();
2257 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
2258 if (bus == NULL) {
2259 xpt_unlock_buses();
2260 return (retval);
2261 }
2262 bus->refcount++;
2263 xpt_unlock_buses();
2264 }
2265 for (; bus != NULL; bus = next_bus) {
2266 retval = tr_func(bus, arg);
2267 if (retval == 0) {
2268 xpt_release_bus(bus);
2269 break;
2270 }
2271 xpt_lock_buses();
2272 next_bus = TAILQ_NEXT(bus, links);
2273 if (next_bus)
2274 next_bus->refcount++;
2275 xpt_unlock_buses();
2276 xpt_release_bus(bus);
2277 }
2278 return(retval);
2279 }
2280
2281 static int
xpttargettraverse(struct cam_eb * bus,struct cam_et * start_target,xpt_targetfunc_t * tr_func,void * arg)2282 xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2283 xpt_targetfunc_t *tr_func, void *arg)
2284 {
2285 struct cam_et *target, *next_target;
2286 int retval;
2287
2288 retval = 1;
2289 if (start_target)
2290 target = start_target;
2291 else {
2292 mtx_lock(&bus->eb_mtx);
2293 target = TAILQ_FIRST(&bus->et_entries);
2294 if (target == NULL) {
2295 mtx_unlock(&bus->eb_mtx);
2296 return (retval);
2297 }
2298 target->refcount++;
2299 mtx_unlock(&bus->eb_mtx);
2300 }
2301 for (; target != NULL; target = next_target) {
2302 retval = tr_func(target, arg);
2303 if (retval == 0) {
2304 xpt_release_target(target);
2305 break;
2306 }
2307 mtx_lock(&bus->eb_mtx);
2308 next_target = TAILQ_NEXT(target, links);
2309 if (next_target)
2310 next_target->refcount++;
2311 mtx_unlock(&bus->eb_mtx);
2312 xpt_release_target(target);
2313 }
2314 return(retval);
2315 }
2316
2317 static int
xptdevicetraverse(struct cam_et * target,struct cam_ed * start_device,xpt_devicefunc_t * tr_func,void * arg)2318 xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2319 xpt_devicefunc_t *tr_func, void *arg)
2320 {
2321 struct cam_eb *bus;
2322 struct cam_ed *device, *next_device;
2323 int retval;
2324
2325 retval = 1;
2326 bus = target->bus;
2327 if (start_device)
2328 device = start_device;
2329 else {
2330 mtx_lock(&bus->eb_mtx);
2331 device = TAILQ_FIRST(&target->ed_entries);
2332 if (device == NULL) {
2333 mtx_unlock(&bus->eb_mtx);
2334 return (retval);
2335 }
2336 device->refcount++;
2337 mtx_unlock(&bus->eb_mtx);
2338 }
2339 for (; device != NULL; device = next_device) {
2340 mtx_lock(&device->device_mtx);
2341 retval = tr_func(device, arg);
2342 mtx_unlock(&device->device_mtx);
2343 if (retval == 0) {
2344 xpt_release_device(device);
2345 break;
2346 }
2347 mtx_lock(&bus->eb_mtx);
2348 next_device = TAILQ_NEXT(device, links);
2349 if (next_device)
2350 next_device->refcount++;
2351 mtx_unlock(&bus->eb_mtx);
2352 xpt_release_device(device);
2353 }
2354 return(retval);
2355 }
2356
2357 static int
xptperiphtraverse(struct cam_ed * device,struct cam_periph * start_periph,xpt_periphfunc_t * tr_func,void * arg)2358 xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2359 xpt_periphfunc_t *tr_func, void *arg)
2360 {
2361 struct cam_eb *bus;
2362 struct cam_periph *periph, *next_periph;
2363 int retval;
2364
2365 retval = 1;
2366
2367 bus = device->target->bus;
2368 if (start_periph)
2369 periph = start_periph;
2370 else {
2371 xpt_lock_buses();
2372 mtx_lock(&bus->eb_mtx);
2373 periph = SLIST_FIRST(&device->periphs);
2374 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2375 periph = SLIST_NEXT(periph, periph_links);
2376 if (periph == NULL) {
2377 mtx_unlock(&bus->eb_mtx);
2378 xpt_unlock_buses();
2379 return (retval);
2380 }
2381 periph->refcount++;
2382 mtx_unlock(&bus->eb_mtx);
2383 xpt_unlock_buses();
2384 }
2385 for (; periph != NULL; periph = next_periph) {
2386 retval = tr_func(periph, arg);
2387 if (retval == 0) {
2388 cam_periph_release_locked(periph);
2389 break;
2390 }
2391 xpt_lock_buses();
2392 mtx_lock(&bus->eb_mtx);
2393 next_periph = SLIST_NEXT(periph, periph_links);
2394 while (next_periph != NULL &&
2395 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2396 next_periph = SLIST_NEXT(next_periph, periph_links);
2397 if (next_periph)
2398 next_periph->refcount++;
2399 mtx_unlock(&bus->eb_mtx);
2400 xpt_unlock_buses();
2401 cam_periph_release_locked(periph);
2402 }
2403 return(retval);
2404 }
2405
2406 static int
xptpdrvtraverse(struct periph_driver ** start_pdrv,xpt_pdrvfunc_t * tr_func,void * arg)2407 xptpdrvtraverse(struct periph_driver **start_pdrv,
2408 xpt_pdrvfunc_t *tr_func, void *arg)
2409 {
2410 struct periph_driver **pdrv;
2411 int retval;
2412
2413 retval = 1;
2414
2415 /*
2416 * We don't traverse the peripheral driver list like we do the
2417 * other lists, because it is a linker set, and therefore cannot be
2418 * changed during runtime. If the peripheral driver list is ever
2419 * re-done to be something other than a linker set (i.e. it can
2420 * change while the system is running), the list traversal should
2421 * be modified to work like the other traversal functions.
2422 */
2423 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2424 *pdrv != NULL; pdrv++) {
2425 retval = tr_func(pdrv, arg);
2426
2427 if (retval == 0)
2428 return(retval);
2429 }
2430
2431 return(retval);
2432 }
2433
2434 static int
xptpdperiphtraverse(struct periph_driver ** pdrv,struct cam_periph * start_periph,xpt_periphfunc_t * tr_func,void * arg)2435 xptpdperiphtraverse(struct periph_driver **pdrv,
2436 struct cam_periph *start_periph,
2437 xpt_periphfunc_t *tr_func, void *arg)
2438 {
2439 struct cam_periph *periph, *next_periph;
2440 int retval;
2441
2442 retval = 1;
2443
2444 if (start_periph)
2445 periph = start_periph;
2446 else {
2447 xpt_lock_buses();
2448 periph = TAILQ_FIRST(&(*pdrv)->units);
2449 while (periph != NULL && (periph->flags & CAM_PERIPH_FREE) != 0)
2450 periph = TAILQ_NEXT(periph, unit_links);
2451 if (periph == NULL) {
2452 xpt_unlock_buses();
2453 return (retval);
2454 }
2455 periph->refcount++;
2456 xpt_unlock_buses();
2457 }
2458 for (; periph != NULL; periph = next_periph) {
2459 cam_periph_lock(periph);
2460 retval = tr_func(periph, arg);
2461 cam_periph_unlock(periph);
2462 if (retval == 0) {
2463 cam_periph_release(periph);
2464 break;
2465 }
2466 xpt_lock_buses();
2467 next_periph = TAILQ_NEXT(periph, unit_links);
2468 while (next_periph != NULL &&
2469 (next_periph->flags & CAM_PERIPH_FREE) != 0)
2470 next_periph = TAILQ_NEXT(next_periph, unit_links);
2471 if (next_periph)
2472 next_periph->refcount++;
2473 xpt_unlock_buses();
2474 cam_periph_release(periph);
2475 }
2476 return(retval);
2477 }
2478
2479 static int
xptdefbusfunc(struct cam_eb * bus,void * arg)2480 xptdefbusfunc(struct cam_eb *bus, void *arg)
2481 {
2482 struct xpt_traverse_config *tr_config;
2483
2484 tr_config = (struct xpt_traverse_config *)arg;
2485
2486 if (tr_config->depth == XPT_DEPTH_BUS) {
2487 xpt_busfunc_t *tr_func;
2488
2489 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2490
2491 return(tr_func(bus, tr_config->tr_arg));
2492 } else
2493 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2494 }
2495
2496 static int
xptdeftargetfunc(struct cam_et * target,void * arg)2497 xptdeftargetfunc(struct cam_et *target, void *arg)
2498 {
2499 struct xpt_traverse_config *tr_config;
2500
2501 tr_config = (struct xpt_traverse_config *)arg;
2502
2503 if (tr_config->depth == XPT_DEPTH_TARGET) {
2504 xpt_targetfunc_t *tr_func;
2505
2506 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2507
2508 return(tr_func(target, tr_config->tr_arg));
2509 } else
2510 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2511 }
2512
2513 static int
xptdefdevicefunc(struct cam_ed * device,void * arg)2514 xptdefdevicefunc(struct cam_ed *device, void *arg)
2515 {
2516 struct xpt_traverse_config *tr_config;
2517
2518 tr_config = (struct xpt_traverse_config *)arg;
2519
2520 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2521 xpt_devicefunc_t *tr_func;
2522
2523 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2524
2525 return(tr_func(device, tr_config->tr_arg));
2526 } else
2527 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2528 }
2529
2530 static int
xptdefperiphfunc(struct cam_periph * periph,void * arg)2531 xptdefperiphfunc(struct cam_periph *periph, void *arg)
2532 {
2533 struct xpt_traverse_config *tr_config;
2534 xpt_periphfunc_t *tr_func;
2535
2536 tr_config = (struct xpt_traverse_config *)arg;
2537
2538 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2539
2540 /*
2541 * Unlike the other default functions, we don't check for depth
2542 * here. The peripheral driver level is the last level in the EDT,
2543 * so if we're here, we should execute the function in question.
2544 */
2545 return(tr_func(periph, tr_config->tr_arg));
2546 }
2547
2548 /*
2549 * Execute the given function for every bus in the EDT.
2550 */
2551 static int
xpt_for_all_busses(xpt_busfunc_t * tr_func,void * arg)2552 xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2553 {
2554 struct xpt_traverse_config tr_config;
2555
2556 tr_config.depth = XPT_DEPTH_BUS;
2557 tr_config.tr_func = tr_func;
2558 tr_config.tr_arg = arg;
2559
2560 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2561 }
2562
2563 /*
2564 * Execute the given function for every device in the EDT.
2565 */
2566 static int
xpt_for_all_devices(xpt_devicefunc_t * tr_func,void * arg)2567 xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2568 {
2569 struct xpt_traverse_config tr_config;
2570
2571 tr_config.depth = XPT_DEPTH_DEVICE;
2572 tr_config.tr_func = tr_func;
2573 tr_config.tr_arg = arg;
2574
2575 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2576 }
2577
2578 static int
xptsetasyncfunc(struct cam_ed * device,void * arg)2579 xptsetasyncfunc(struct cam_ed *device, void *arg)
2580 {
2581 struct cam_path path;
2582 struct ccb_getdev cgd;
2583 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2584
2585 /*
2586 * Don't report unconfigured devices (Wildcard devs,
2587 * devices only for target mode, device instances
2588 * that have been invalidated but are waiting for
2589 * their last reference count to be released).
2590 */
2591 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2592 return (1);
2593
2594 xpt_compile_path(&path,
2595 NULL,
2596 device->target->bus->path_id,
2597 device->target->target_id,
2598 device->lun_id);
2599 xpt_setup_ccb(&cgd.ccb_h, &path, CAM_PRIORITY_NORMAL);
2600 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2601 xpt_action((union ccb *)&cgd);
2602 csa->callback(csa->callback_arg,
2603 AC_FOUND_DEVICE,
2604 &path, &cgd);
2605 xpt_release_path(&path);
2606
2607 return(1);
2608 }
2609
2610 static int
xptsetasyncbusfunc(struct cam_eb * bus,void * arg)2611 xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2612 {
2613 struct cam_path path;
2614 struct ccb_pathinq cpi;
2615 struct ccb_setasync *csa = (struct ccb_setasync *)arg;
2616
2617 xpt_compile_path(&path, /*periph*/NULL,
2618 bus->path_id,
2619 CAM_TARGET_WILDCARD,
2620 CAM_LUN_WILDCARD);
2621 xpt_path_lock(&path);
2622 xpt_path_inq(&cpi, &path);
2623 csa->callback(csa->callback_arg,
2624 AC_PATH_REGISTERED,
2625 &path, &cpi);
2626 xpt_path_unlock(&path);
2627 xpt_release_path(&path);
2628
2629 return(1);
2630 }
2631
2632 void
xpt_action(union ccb * start_ccb)2633 xpt_action(union ccb *start_ccb)
2634 {
2635
2636 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE,
2637 ("xpt_action: func %#x %s\n", start_ccb->ccb_h.func_code,
2638 xpt_action_name(start_ccb->ccb_h.func_code)));
2639
2640 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2641 (*(start_ccb->ccb_h.path->bus->xport->ops->action))(start_ccb);
2642 }
2643
2644 void
xpt_action_default(union ccb * start_ccb)2645 xpt_action_default(union ccb *start_ccb)
2646 {
2647 struct cam_path *path;
2648 struct cam_sim *sim;
2649 struct mtx *mtx;
2650
2651 path = start_ccb->ccb_h.path;
2652 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2653 ("xpt_action_default: func %#x %s\n", start_ccb->ccb_h.func_code,
2654 xpt_action_name(start_ccb->ccb_h.func_code)));
2655
2656 switch (start_ccb->ccb_h.func_code) {
2657 case XPT_SCSI_IO:
2658 {
2659 struct cam_ed *device;
2660
2661 /*
2662 * For the sake of compatibility with SCSI-1
2663 * devices that may not understand the identify
2664 * message, we include lun information in the
2665 * second byte of all commands. SCSI-1 specifies
2666 * that luns are a 3 bit value and reserves only 3
2667 * bits for lun information in the CDB. Later
2668 * revisions of the SCSI spec allow for more than 8
2669 * luns, but have deprecated lun information in the
2670 * CDB. So, if the lun won't fit, we must omit.
2671 *
2672 * Also be aware that during initial probing for devices,
2673 * the inquiry information is unknown but initialized to 0.
2674 * This means that this code will be exercised while probing
2675 * devices with an ANSI revision greater than 2.
2676 */
2677 device = path->device;
2678 if (device->protocol_version <= SCSI_REV_2
2679 && start_ccb->ccb_h.target_lun < 8
2680 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2681
2682 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2683 start_ccb->ccb_h.target_lun << 5;
2684 }
2685 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2686 }
2687 /* FALLTHROUGH */
2688 case XPT_TARGET_IO:
2689 case XPT_CONT_TARGET_IO:
2690 start_ccb->csio.sense_resid = 0;
2691 start_ccb->csio.resid = 0;
2692 /* FALLTHROUGH */
2693 case XPT_ATA_IO:
2694 if (start_ccb->ccb_h.func_code == XPT_ATA_IO)
2695 start_ccb->ataio.resid = 0;
2696 /* FALLTHROUGH */
2697 case XPT_NVME_IO:
2698 /* FALLTHROUGH */
2699 case XPT_NVME_ADMIN:
2700 /* FALLTHROUGH */
2701 case XPT_MMC_IO:
2702 /* XXX just like nmve_io? */
2703 case XPT_RESET_DEV:
2704 case XPT_ENG_EXEC:
2705 case XPT_SMP_IO:
2706 {
2707 struct cam_devq *devq;
2708
2709 devq = path->bus->sim->devq;
2710 mtx_lock(&devq->send_mtx);
2711 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
2712 if (xpt_schedule_devq(devq, path->device) != 0)
2713 xpt_run_devq(devq);
2714 mtx_unlock(&devq->send_mtx);
2715 break;
2716 }
2717 case XPT_CALC_GEOMETRY:
2718 /* Filter out garbage */
2719 if (start_ccb->ccg.block_size == 0
2720 || start_ccb->ccg.volume_size == 0) {
2721 start_ccb->ccg.cylinders = 0;
2722 start_ccb->ccg.heads = 0;
2723 start_ccb->ccg.secs_per_track = 0;
2724 start_ccb->ccb_h.status = CAM_REQ_CMP;
2725 break;
2726 }
2727 #if defined(__sparc64__)
2728 /*
2729 * For sparc64, we may need adjust the geometry of large
2730 * disks in order to fit the limitations of the 16-bit
2731 * fields of the VTOC8 disk label.
2732 */
2733 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
2734 start_ccb->ccb_h.status = CAM_REQ_CMP;
2735 break;
2736 }
2737 #endif
2738 goto call_sim;
2739 case XPT_ABORT:
2740 {
2741 union ccb* abort_ccb;
2742
2743 abort_ccb = start_ccb->cab.abort_ccb;
2744 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
2745 struct cam_ed *device;
2746 struct cam_devq *devq;
2747
2748 device = abort_ccb->ccb_h.path->device;
2749 devq = device->sim->devq;
2750
2751 mtx_lock(&devq->send_mtx);
2752 if (abort_ccb->ccb_h.pinfo.index > 0) {
2753 cam_ccbq_remove_ccb(&device->ccbq, abort_ccb);
2754 abort_ccb->ccb_h.status =
2755 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2756 xpt_freeze_devq_device(device, 1);
2757 mtx_unlock(&devq->send_mtx);
2758 xpt_done(abort_ccb);
2759 start_ccb->ccb_h.status = CAM_REQ_CMP;
2760 break;
2761 }
2762 mtx_unlock(&devq->send_mtx);
2763
2764 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
2765 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
2766 /*
2767 * We've caught this ccb en route to
2768 * the SIM. Flag it for abort and the
2769 * SIM will do so just before starting
2770 * real work on the CCB.
2771 */
2772 abort_ccb->ccb_h.status =
2773 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
2774 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
2775 start_ccb->ccb_h.status = CAM_REQ_CMP;
2776 break;
2777 }
2778 }
2779 if (XPT_FC_IS_QUEUED(abort_ccb)
2780 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
2781 /*
2782 * It's already completed but waiting
2783 * for our SWI to get to it.
2784 */
2785 start_ccb->ccb_h.status = CAM_UA_ABORT;
2786 break;
2787 }
2788 /*
2789 * If we weren't able to take care of the abort request
2790 * in the XPT, pass the request down to the SIM for processing.
2791 */
2792 }
2793 /* FALLTHROUGH */
2794 case XPT_ACCEPT_TARGET_IO:
2795 case XPT_EN_LUN:
2796 case XPT_IMMED_NOTIFY:
2797 case XPT_NOTIFY_ACK:
2798 case XPT_RESET_BUS:
2799 case XPT_IMMEDIATE_NOTIFY:
2800 case XPT_NOTIFY_ACKNOWLEDGE:
2801 case XPT_GET_SIM_KNOB_OLD:
2802 case XPT_GET_SIM_KNOB:
2803 case XPT_SET_SIM_KNOB:
2804 case XPT_GET_TRAN_SETTINGS:
2805 case XPT_SET_TRAN_SETTINGS:
2806 case XPT_PATH_INQ:
2807 call_sim:
2808 sim = path->bus->sim;
2809 mtx = sim->mtx;
2810 if (mtx && !mtx_owned(mtx))
2811 mtx_lock(mtx);
2812 else
2813 mtx = NULL;
2814
2815 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2816 ("Calling sim->sim_action(): func=%#x\n", start_ccb->ccb_h.func_code));
2817 (*(sim->sim_action))(sim, start_ccb);
2818 CAM_DEBUG(path, CAM_DEBUG_TRACE,
2819 ("sim->sim_action returned: status=%#x\n", start_ccb->ccb_h.status));
2820 if (mtx)
2821 mtx_unlock(mtx);
2822 break;
2823 case XPT_PATH_STATS:
2824 start_ccb->cpis.last_reset = path->bus->last_reset;
2825 start_ccb->ccb_h.status = CAM_REQ_CMP;
2826 break;
2827 case XPT_GDEV_TYPE:
2828 {
2829 struct cam_ed *dev;
2830
2831 dev = path->device;
2832 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
2833 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
2834 } else {
2835 struct ccb_getdev *cgd;
2836
2837 cgd = &start_ccb->cgd;
2838 cgd->protocol = dev->protocol;
2839 cgd->inq_data = dev->inq_data;
2840 cgd->ident_data = dev->ident_data;
2841 cgd->inq_flags = dev->inq_flags;
2842 cgd->ccb_h.status = CAM_REQ_CMP;
2843 cgd->serial_num_len = dev->serial_num_len;
2844 if ((dev->serial_num_len > 0)
2845 && (dev->serial_num != NULL))
2846 bcopy(dev->serial_num, cgd->serial_num,
2847 dev->serial_num_len);
2848 }
2849 break;
2850 }
2851 case XPT_GDEV_STATS:
2852 {
2853 struct ccb_getdevstats *cgds = &start_ccb->cgds;
2854 struct cam_ed *dev = path->device;
2855 struct cam_eb *bus = path->bus;
2856 struct cam_et *tar = path->target;
2857 struct cam_devq *devq = bus->sim->devq;
2858
2859 mtx_lock(&devq->send_mtx);
2860 cgds->dev_openings = dev->ccbq.dev_openings;
2861 cgds->dev_active = dev->ccbq.dev_active;
2862 cgds->allocated = dev->ccbq.allocated;
2863 cgds->queued = cam_ccbq_pending_ccb_count(&dev->ccbq);
2864 cgds->held = cgds->allocated - cgds->dev_active - cgds->queued;
2865 cgds->last_reset = tar->last_reset;
2866 cgds->maxtags = dev->maxtags;
2867 cgds->mintags = dev->mintags;
2868 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
2869 cgds->last_reset = bus->last_reset;
2870 mtx_unlock(&devq->send_mtx);
2871 cgds->ccb_h.status = CAM_REQ_CMP;
2872 break;
2873 }
2874 case XPT_GDEVLIST:
2875 {
2876 struct cam_periph *nperiph;
2877 struct periph_list *periph_head;
2878 struct ccb_getdevlist *cgdl;
2879 u_int i;
2880 struct cam_ed *device;
2881 int found;
2882
2883
2884 found = 0;
2885
2886 /*
2887 * Don't want anyone mucking with our data.
2888 */
2889 device = path->device;
2890 periph_head = &device->periphs;
2891 cgdl = &start_ccb->cgdl;
2892
2893 /*
2894 * Check and see if the list has changed since the user
2895 * last requested a list member. If so, tell them that the
2896 * list has changed, and therefore they need to start over
2897 * from the beginning.
2898 */
2899 if ((cgdl->index != 0) &&
2900 (cgdl->generation != device->generation)) {
2901 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
2902 break;
2903 }
2904
2905 /*
2906 * Traverse the list of peripherals and attempt to find
2907 * the requested peripheral.
2908 */
2909 for (nperiph = SLIST_FIRST(periph_head), i = 0;
2910 (nperiph != NULL) && (i <= cgdl->index);
2911 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
2912 if (i == cgdl->index) {
2913 strlcpy(cgdl->periph_name,
2914 nperiph->periph_name,
2915 sizeof(cgdl->periph_name));
2916 cgdl->unit_number = nperiph->unit_number;
2917 found = 1;
2918 }
2919 }
2920 if (found == 0) {
2921 cgdl->status = CAM_GDEVLIST_ERROR;
2922 break;
2923 }
2924
2925 if (nperiph == NULL)
2926 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
2927 else
2928 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
2929
2930 cgdl->index++;
2931 cgdl->generation = device->generation;
2932
2933 cgdl->ccb_h.status = CAM_REQ_CMP;
2934 break;
2935 }
2936 case XPT_DEV_MATCH:
2937 {
2938 dev_pos_type position_type;
2939 struct ccb_dev_match *cdm;
2940
2941 cdm = &start_ccb->cdm;
2942
2943 /*
2944 * There are two ways of getting at information in the EDT.
2945 * The first way is via the primary EDT tree. It starts
2946 * with a list of buses, then a list of targets on a bus,
2947 * then devices/luns on a target, and then peripherals on a
2948 * device/lun. The "other" way is by the peripheral driver
2949 * lists. The peripheral driver lists are organized by
2950 * peripheral driver. (obviously) So it makes sense to
2951 * use the peripheral driver list if the user is looking
2952 * for something like "da1", or all "da" devices. If the
2953 * user is looking for something on a particular bus/target
2954 * or lun, it's generally better to go through the EDT tree.
2955 */
2956
2957 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
2958 position_type = cdm->pos.position_type;
2959 else {
2960 u_int i;
2961
2962 position_type = CAM_DEV_POS_NONE;
2963
2964 for (i = 0; i < cdm->num_patterns; i++) {
2965 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
2966 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
2967 position_type = CAM_DEV_POS_EDT;
2968 break;
2969 }
2970 }
2971
2972 if (cdm->num_patterns == 0)
2973 position_type = CAM_DEV_POS_EDT;
2974 else if (position_type == CAM_DEV_POS_NONE)
2975 position_type = CAM_DEV_POS_PDRV;
2976 }
2977
2978 switch(position_type & CAM_DEV_POS_TYPEMASK) {
2979 case CAM_DEV_POS_EDT:
2980 xptedtmatch(cdm);
2981 break;
2982 case CAM_DEV_POS_PDRV:
2983 xptperiphlistmatch(cdm);
2984 break;
2985 default:
2986 cdm->status = CAM_DEV_MATCH_ERROR;
2987 break;
2988 }
2989
2990 if (cdm->status == CAM_DEV_MATCH_ERROR)
2991 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2992 else
2993 start_ccb->ccb_h.status = CAM_REQ_CMP;
2994
2995 break;
2996 }
2997 case XPT_SASYNC_CB:
2998 {
2999 struct ccb_setasync *csa;
3000 struct async_node *cur_entry;
3001 struct async_list *async_head;
3002 u_int32_t added;
3003
3004 csa = &start_ccb->csa;
3005 added = csa->event_enable;
3006 async_head = &path->device->asyncs;
3007
3008 /*
3009 * If there is already an entry for us, simply
3010 * update it.
3011 */
3012 cur_entry = SLIST_FIRST(async_head);
3013 while (cur_entry != NULL) {
3014 if ((cur_entry->callback_arg == csa->callback_arg)
3015 && (cur_entry->callback == csa->callback))
3016 break;
3017 cur_entry = SLIST_NEXT(cur_entry, links);
3018 }
3019
3020 if (cur_entry != NULL) {
3021 /*
3022 * If the request has no flags set,
3023 * remove the entry.
3024 */
3025 added &= ~cur_entry->event_enable;
3026 if (csa->event_enable == 0) {
3027 SLIST_REMOVE(async_head, cur_entry,
3028 async_node, links);
3029 xpt_release_device(path->device);
3030 free(cur_entry, M_CAMXPT);
3031 } else {
3032 cur_entry->event_enable = csa->event_enable;
3033 }
3034 csa->event_enable = added;
3035 } else {
3036 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
3037 M_NOWAIT);
3038 if (cur_entry == NULL) {
3039 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3040 break;
3041 }
3042 cur_entry->event_enable = csa->event_enable;
3043 cur_entry->event_lock = (path->bus->sim->mtx &&
3044 mtx_owned(path->bus->sim->mtx)) ? 1 : 0;
3045 cur_entry->callback_arg = csa->callback_arg;
3046 cur_entry->callback = csa->callback;
3047 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3048 xpt_acquire_device(path->device);
3049 }
3050 start_ccb->ccb_h.status = CAM_REQ_CMP;
3051 break;
3052 }
3053 case XPT_REL_SIMQ:
3054 {
3055 struct ccb_relsim *crs;
3056 struct cam_ed *dev;
3057
3058 crs = &start_ccb->crs;
3059 dev = path->device;
3060 if (dev == NULL) {
3061
3062 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3063 break;
3064 }
3065
3066 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3067
3068 /* Don't ever go below one opening */
3069 if (crs->openings > 0) {
3070 xpt_dev_ccbq_resize(path, crs->openings);
3071 if (bootverbose) {
3072 xpt_print(path,
3073 "number of openings is now %d\n",
3074 crs->openings);
3075 }
3076 }
3077 }
3078
3079 mtx_lock(&dev->sim->devq->send_mtx);
3080 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3081
3082 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3083
3084 /*
3085 * Just extend the old timeout and decrement
3086 * the freeze count so that a single timeout
3087 * is sufficient for releasing the queue.
3088 */
3089 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3090 callout_stop(&dev->callout);
3091 } else {
3092
3093 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3094 }
3095
3096 callout_reset_sbt(&dev->callout,
3097 SBT_1MS * crs->release_timeout, 0,
3098 xpt_release_devq_timeout, dev, 0);
3099
3100 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3101
3102 }
3103
3104 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3105
3106 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3107 /*
3108 * Decrement the freeze count so that a single
3109 * completion is still sufficient to unfreeze
3110 * the queue.
3111 */
3112 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3113 } else {
3114
3115 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3116 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3117 }
3118 }
3119
3120 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3121
3122 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3123 || (dev->ccbq.dev_active == 0)) {
3124
3125 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3126 } else {
3127
3128 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3129 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3130 }
3131 }
3132 mtx_unlock(&dev->sim->devq->send_mtx);
3133
3134 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0)
3135 xpt_release_devq(path, /*count*/1, /*run_queue*/TRUE);
3136 start_ccb->crs.qfrozen_cnt = dev->ccbq.queue.qfrozen_cnt;
3137 start_ccb->ccb_h.status = CAM_REQ_CMP;
3138 break;
3139 }
3140 case XPT_DEBUG: {
3141 struct cam_path *oldpath;
3142
3143 /* Check that all request bits are supported. */
3144 if (start_ccb->cdbg.flags & ~(CAM_DEBUG_COMPILE)) {
3145 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3146 break;
3147 }
3148
3149 cam_dflags = CAM_DEBUG_NONE;
3150 if (cam_dpath != NULL) {
3151 oldpath = cam_dpath;
3152 cam_dpath = NULL;
3153 xpt_free_path(oldpath);
3154 }
3155 if (start_ccb->cdbg.flags != CAM_DEBUG_NONE) {
3156 if (xpt_create_path(&cam_dpath, NULL,
3157 start_ccb->ccb_h.path_id,
3158 start_ccb->ccb_h.target_id,
3159 start_ccb->ccb_h.target_lun) !=
3160 CAM_REQ_CMP) {
3161 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3162 } else {
3163 cam_dflags = start_ccb->cdbg.flags;
3164 start_ccb->ccb_h.status = CAM_REQ_CMP;
3165 xpt_print(cam_dpath, "debugging flags now %x\n",
3166 cam_dflags);
3167 }
3168 } else
3169 start_ccb->ccb_h.status = CAM_REQ_CMP;
3170 break;
3171 }
3172 case XPT_NOOP:
3173 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3174 xpt_freeze_devq(path, 1);
3175 start_ccb->ccb_h.status = CAM_REQ_CMP;
3176 break;
3177 case XPT_REPROBE_LUN:
3178 xpt_async(AC_INQ_CHANGED, path, NULL);
3179 start_ccb->ccb_h.status = CAM_REQ_CMP;
3180 xpt_done(start_ccb);
3181 break;
3182 case XPT_ASYNC:
3183 start_ccb->ccb_h.status = CAM_REQ_CMP;
3184 xpt_done(start_ccb);
3185 break;
3186 default:
3187 case XPT_SDEV_TYPE:
3188 case XPT_TERM_IO:
3189 case XPT_ENG_INQ:
3190 /* XXX Implement */
3191 xpt_print(start_ccb->ccb_h.path,
3192 "%s: CCB type %#x %s not supported\n", __func__,
3193 start_ccb->ccb_h.func_code,
3194 xpt_action_name(start_ccb->ccb_h.func_code));
3195 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3196 if (start_ccb->ccb_h.func_code & XPT_FC_DEV_QUEUED) {
3197 xpt_done(start_ccb);
3198 }
3199 break;
3200 }
3201 CAM_DEBUG(path, CAM_DEBUG_TRACE,
3202 ("xpt_action_default: func= %#x %s status %#x\n",
3203 start_ccb->ccb_h.func_code,
3204 xpt_action_name(start_ccb->ccb_h.func_code),
3205 start_ccb->ccb_h.status));
3206 }
3207
3208 /*
3209 * Call the sim poll routine to allow the sim to complete
3210 * any inflight requests, then call camisr_runqueue to
3211 * complete any CCB that the polling completed.
3212 */
3213 void
xpt_sim_poll(struct cam_sim * sim)3214 xpt_sim_poll(struct cam_sim *sim)
3215 {
3216 struct mtx *mtx;
3217
3218 mtx = sim->mtx;
3219 if (mtx)
3220 mtx_lock(mtx);
3221 (*(sim->sim_poll))(sim);
3222 if (mtx)
3223 mtx_unlock(mtx);
3224 camisr_runqueue();
3225 }
3226
3227 uint32_t
xpt_poll_setup(union ccb * start_ccb)3228 xpt_poll_setup(union ccb *start_ccb)
3229 {
3230 u_int32_t timeout;
3231 struct cam_sim *sim;
3232 struct cam_devq *devq;
3233 struct cam_ed *dev;
3234
3235 timeout = start_ccb->ccb_h.timeout * 10;
3236 sim = start_ccb->ccb_h.path->bus->sim;
3237 devq = sim->devq;
3238 dev = start_ccb->ccb_h.path->device;
3239
3240 /*
3241 * Steal an opening so that no other queued requests
3242 * can get it before us while we simulate interrupts.
3243 */
3244 mtx_lock(&devq->send_mtx);
3245 dev->ccbq.dev_openings--;
3246 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0) &&
3247 (--timeout > 0)) {
3248 mtx_unlock(&devq->send_mtx);
3249 DELAY(100);
3250 xpt_sim_poll(sim);
3251 mtx_lock(&devq->send_mtx);
3252 }
3253 dev->ccbq.dev_openings++;
3254 mtx_unlock(&devq->send_mtx);
3255
3256 return (timeout);
3257 }
3258
3259 void
xpt_pollwait(union ccb * start_ccb,uint32_t timeout)3260 xpt_pollwait(union ccb *start_ccb, uint32_t timeout)
3261 {
3262
3263 while (--timeout > 0) {
3264 xpt_sim_poll(start_ccb->ccb_h.path->bus->sim);
3265 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3266 != CAM_REQ_INPROG)
3267 break;
3268 DELAY(100);
3269 }
3270
3271 if (timeout == 0) {
3272 /*
3273 * XXX Is it worth adding a sim_timeout entry
3274 * point so we can attempt recovery? If
3275 * this is only used for dumps, I don't think
3276 * it is.
3277 */
3278 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3279 }
3280 }
3281
3282 void
xpt_polled_action(union ccb * start_ccb)3283 xpt_polled_action(union ccb *start_ccb)
3284 {
3285 uint32_t timeout;
3286 struct cam_ed *dev;
3287
3288 timeout = start_ccb->ccb_h.timeout * 10;
3289 dev = start_ccb->ccb_h.path->device;
3290
3291 mtx_unlock(&dev->device_mtx);
3292
3293 timeout = xpt_poll_setup(start_ccb);
3294 if (timeout > 0) {
3295 xpt_action(start_ccb);
3296 xpt_pollwait(start_ccb, timeout);
3297 } else {
3298 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3299 }
3300
3301 mtx_lock(&dev->device_mtx);
3302 }
3303
3304 /*
3305 * Schedule a peripheral driver to receive a ccb when its
3306 * target device has space for more transactions.
3307 */
3308 void
xpt_schedule(struct cam_periph * periph,u_int32_t new_priority)3309 xpt_schedule(struct cam_periph *periph, u_int32_t new_priority)
3310 {
3311
3312 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3313 cam_periph_assert(periph, MA_OWNED);
3314 if (new_priority < periph->scheduled_priority) {
3315 periph->scheduled_priority = new_priority;
3316 xpt_run_allocq(periph, 0);
3317 }
3318 }
3319
3320
3321 /*
3322 * Schedule a device to run on a given queue.
3323 * If the device was inserted as a new entry on the queue,
3324 * return 1 meaning the device queue should be run. If we
3325 * were already queued, implying someone else has already
3326 * started the queue, return 0 so the caller doesn't attempt
3327 * to run the queue.
3328 */
3329 static int
xpt_schedule_dev(struct camq * queue,cam_pinfo * pinfo,u_int32_t new_priority)3330 xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3331 u_int32_t new_priority)
3332 {
3333 int retval;
3334 u_int32_t old_priority;
3335
3336 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3337
3338
3339 old_priority = pinfo->priority;
3340
3341 /*
3342 * Are we already queued?
3343 */
3344 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3345 /* Simply reorder based on new priority */
3346 if (new_priority < old_priority) {
3347 camq_change_priority(queue, pinfo->index,
3348 new_priority);
3349 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3350 ("changed priority to %d\n",
3351 new_priority));
3352 retval = 1;
3353 } else
3354 retval = 0;
3355 } else {
3356 /* New entry on the queue */
3357 if (new_priority < old_priority)
3358 pinfo->priority = new_priority;
3359
3360 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3361 ("Inserting onto queue\n"));
3362 pinfo->generation = ++queue->generation;
3363 camq_insert(queue, pinfo);
3364 retval = 1;
3365 }
3366 return (retval);
3367 }
3368
3369 static void
xpt_run_allocq_task(void * context,int pending)3370 xpt_run_allocq_task(void *context, int pending)
3371 {
3372 struct cam_periph *periph = context;
3373
3374 cam_periph_lock(periph);
3375 periph->flags &= ~CAM_PERIPH_RUN_TASK;
3376 xpt_run_allocq(periph, 1);
3377 cam_periph_unlock(periph);
3378 cam_periph_release(periph);
3379 }
3380
3381 static void
xpt_run_allocq(struct cam_periph * periph,int sleep)3382 xpt_run_allocq(struct cam_periph *periph, int sleep)
3383 {
3384 struct cam_ed *device;
3385 union ccb *ccb;
3386 uint32_t prio;
3387
3388 cam_periph_assert(periph, MA_OWNED);
3389 if (periph->periph_allocating)
3390 return;
3391 cam_periph_doacquire(periph);
3392 periph->periph_allocating = 1;
3393 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_allocq(%p)\n", periph));
3394 device = periph->path->device;
3395 ccb = NULL;
3396 restart:
3397 while ((prio = min(periph->scheduled_priority,
3398 periph->immediate_priority)) != CAM_PRIORITY_NONE &&
3399 (periph->periph_allocated - (ccb != NULL ? 1 : 0) <
3400 device->ccbq.total_openings || prio <= CAM_PRIORITY_OOB)) {
3401
3402 if (ccb == NULL &&
3403 (ccb = xpt_get_ccb_nowait(periph)) == NULL) {
3404 if (sleep) {
3405 ccb = xpt_get_ccb(periph);
3406 goto restart;
3407 }
3408 if (periph->flags & CAM_PERIPH_RUN_TASK)
3409 break;
3410 cam_periph_doacquire(periph);
3411 periph->flags |= CAM_PERIPH_RUN_TASK;
3412 taskqueue_enqueue(xsoftc.xpt_taskq,
3413 &periph->periph_run_task);
3414 break;
3415 }
3416 xpt_setup_ccb(&ccb->ccb_h, periph->path, prio);
3417 if (prio == periph->immediate_priority) {
3418 periph->immediate_priority = CAM_PRIORITY_NONE;
3419 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3420 ("waking cam_periph_getccb()\n"));
3421 SLIST_INSERT_HEAD(&periph->ccb_list, &ccb->ccb_h,
3422 periph_links.sle);
3423 wakeup(&periph->ccb_list);
3424 } else {
3425 periph->scheduled_priority = CAM_PRIORITY_NONE;
3426 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3427 ("calling periph_start()\n"));
3428 periph->periph_start(periph, ccb);
3429 }
3430 ccb = NULL;
3431 }
3432 if (ccb != NULL)
3433 xpt_release_ccb(ccb);
3434 periph->periph_allocating = 0;
3435 cam_periph_release_locked(periph);
3436 }
3437
3438 static void
xpt_run_devq(struct cam_devq * devq)3439 xpt_run_devq(struct cam_devq *devq)
3440 {
3441 struct mtx *mtx;
3442
3443 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_devq\n"));
3444
3445 devq->send_queue.qfrozen_cnt++;
3446 while ((devq->send_queue.entries > 0)
3447 && (devq->send_openings > 0)
3448 && (devq->send_queue.qfrozen_cnt <= 1)) {
3449 struct cam_ed *device;
3450 union ccb *work_ccb;
3451 struct cam_sim *sim;
3452 struct xpt_proto *proto;
3453
3454 device = (struct cam_ed *)camq_remove(&devq->send_queue,
3455 CAMQ_HEAD);
3456 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3457 ("running device %p\n", device));
3458
3459 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3460 if (work_ccb == NULL) {
3461 printf("device on run queue with no ccbs???\n");
3462 continue;
3463 }
3464
3465 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3466
3467 mtx_lock(&xsoftc.xpt_highpower_lock);
3468 if (xsoftc.num_highpower <= 0) {
3469 /*
3470 * We got a high power command, but we
3471 * don't have any available slots. Freeze
3472 * the device queue until we have a slot
3473 * available.
3474 */
3475 xpt_freeze_devq_device(device, 1);
3476 STAILQ_INSERT_TAIL(&xsoftc.highpowerq, device,
3477 highpowerq_entry);
3478
3479 mtx_unlock(&xsoftc.xpt_highpower_lock);
3480 continue;
3481 } else {
3482 /*
3483 * Consume a high power slot while
3484 * this ccb runs.
3485 */
3486 xsoftc.num_highpower--;
3487 }
3488 mtx_unlock(&xsoftc.xpt_highpower_lock);
3489 }
3490 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3491 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3492 devq->send_openings--;
3493 devq->send_active++;
3494 xpt_schedule_devq(devq, device);
3495 mtx_unlock(&devq->send_mtx);
3496
3497 if ((work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0) {
3498 /*
3499 * The client wants to freeze the queue
3500 * after this CCB is sent.
3501 */
3502 xpt_freeze_devq(work_ccb->ccb_h.path, 1);
3503 }
3504
3505 /* In Target mode, the peripheral driver knows best... */
3506 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3507 if ((device->inq_flags & SID_CmdQue) != 0
3508 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3509 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3510 else
3511 /*
3512 * Clear this in case of a retried CCB that
3513 * failed due to a rejected tag.
3514 */
3515 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3516 }
3517
3518 KASSERT(device == work_ccb->ccb_h.path->device,
3519 ("device (%p) / path->device (%p) mismatch",
3520 device, work_ccb->ccb_h.path->device));
3521 proto = xpt_proto_find(device->protocol);
3522 if (proto && proto->ops->debug_out)
3523 proto->ops->debug_out(work_ccb);
3524
3525 /*
3526 * Device queues can be shared among multiple SIM instances
3527 * that reside on different buses. Use the SIM from the
3528 * queued device, rather than the one from the calling bus.
3529 */
3530 sim = device->sim;
3531 mtx = sim->mtx;
3532 if (mtx && !mtx_owned(mtx))
3533 mtx_lock(mtx);
3534 else
3535 mtx = NULL;
3536 work_ccb->ccb_h.qos.periph_data = cam_iosched_now();
3537 (*(sim->sim_action))(sim, work_ccb);
3538 if (mtx)
3539 mtx_unlock(mtx);
3540 mtx_lock(&devq->send_mtx);
3541 }
3542 devq->send_queue.qfrozen_cnt--;
3543 }
3544
3545 /*
3546 * This function merges stuff from the slave ccb into the master ccb, while
3547 * keeping important fields in the master ccb constant.
3548 */
3549 void
xpt_merge_ccb(union ccb * master_ccb,union ccb * slave_ccb)3550 xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3551 {
3552
3553 /*
3554 * Pull fields that are valid for peripheral drivers to set
3555 * into the master CCB along with the CCB "payload".
3556 */
3557 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3558 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3559 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3560 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3561 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3562 sizeof(union ccb) - sizeof(struct ccb_hdr));
3563 }
3564
3565 void
xpt_setup_ccb_flags(struct ccb_hdr * ccb_h,struct cam_path * path,u_int32_t priority,u_int32_t flags)3566 xpt_setup_ccb_flags(struct ccb_hdr *ccb_h, struct cam_path *path,
3567 u_int32_t priority, u_int32_t flags)
3568 {
3569
3570 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3571 ccb_h->pinfo.priority = priority;
3572 ccb_h->path = path;
3573 ccb_h->path_id = path->bus->path_id;
3574 if (path->target)
3575 ccb_h->target_id = path->target->target_id;
3576 else
3577 ccb_h->target_id = CAM_TARGET_WILDCARD;
3578 if (path->device) {
3579 ccb_h->target_lun = path->device->lun_id;
3580 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3581 } else {
3582 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3583 }
3584 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3585 ccb_h->flags = flags;
3586 ccb_h->xflags = 0;
3587 }
3588
3589 void
xpt_setup_ccb(struct ccb_hdr * ccb_h,struct cam_path * path,u_int32_t priority)3590 xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3591 {
3592 xpt_setup_ccb_flags(ccb_h, path, priority, /*flags*/ 0);
3593 }
3594
3595 /* Path manipulation functions */
3596 cam_status
xpt_create_path(struct cam_path ** new_path_ptr,struct cam_periph * perph,path_id_t path_id,target_id_t target_id,lun_id_t lun_id)3597 xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3598 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3599 {
3600 struct cam_path *path;
3601 cam_status status;
3602
3603 path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3604
3605 if (path == NULL) {
3606 status = CAM_RESRC_UNAVAIL;
3607 return(status);
3608 }
3609 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3610 if (status != CAM_REQ_CMP) {
3611 free(path, M_CAMPATH);
3612 path = NULL;
3613 }
3614 *new_path_ptr = path;
3615 return (status);
3616 }
3617
3618 cam_status
xpt_create_path_unlocked(struct cam_path ** new_path_ptr,struct cam_periph * periph,path_id_t path_id,target_id_t target_id,lun_id_t lun_id)3619 xpt_create_path_unlocked(struct cam_path **new_path_ptr,
3620 struct cam_periph *periph, path_id_t path_id,
3621 target_id_t target_id, lun_id_t lun_id)
3622 {
3623
3624 return (xpt_create_path(new_path_ptr, periph, path_id, target_id,
3625 lun_id));
3626 }
3627
3628 cam_status
xpt_compile_path(struct cam_path * new_path,struct cam_periph * perph,path_id_t path_id,target_id_t target_id,lun_id_t lun_id)3629 xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3630 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3631 {
3632 struct cam_eb *bus;
3633 struct cam_et *target;
3634 struct cam_ed *device;
3635 cam_status status;
3636
3637 status = CAM_REQ_CMP; /* Completed without error */
3638 target = NULL; /* Wildcarded */
3639 device = NULL; /* Wildcarded */
3640
3641 /*
3642 * We will potentially modify the EDT, so block interrupts
3643 * that may attempt to create cam paths.
3644 */
3645 bus = xpt_find_bus(path_id);
3646 if (bus == NULL) {
3647 status = CAM_PATH_INVALID;
3648 } else {
3649 xpt_lock_buses();
3650 mtx_lock(&bus->eb_mtx);
3651 target = xpt_find_target(bus, target_id);
3652 if (target == NULL) {
3653 /* Create one */
3654 struct cam_et *new_target;
3655
3656 new_target = xpt_alloc_target(bus, target_id);
3657 if (new_target == NULL) {
3658 status = CAM_RESRC_UNAVAIL;
3659 } else {
3660 target = new_target;
3661 }
3662 }
3663 xpt_unlock_buses();
3664 if (target != NULL) {
3665 device = xpt_find_device(target, lun_id);
3666 if (device == NULL) {
3667 /* Create one */
3668 struct cam_ed *new_device;
3669
3670 new_device =
3671 (*(bus->xport->ops->alloc_device))(bus,
3672 target,
3673 lun_id);
3674 if (new_device == NULL) {
3675 status = CAM_RESRC_UNAVAIL;
3676 } else {
3677 device = new_device;
3678 }
3679 }
3680 }
3681 mtx_unlock(&bus->eb_mtx);
3682 }
3683
3684 /*
3685 * Only touch the user's data if we are successful.
3686 */
3687 if (status == CAM_REQ_CMP) {
3688 new_path->periph = perph;
3689 new_path->bus = bus;
3690 new_path->target = target;
3691 new_path->device = device;
3692 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
3693 } else {
3694 if (device != NULL)
3695 xpt_release_device(device);
3696 if (target != NULL)
3697 xpt_release_target(target);
3698 if (bus != NULL)
3699 xpt_release_bus(bus);
3700 }
3701 return (status);
3702 }
3703
3704 cam_status
xpt_clone_path(struct cam_path ** new_path_ptr,struct cam_path * path)3705 xpt_clone_path(struct cam_path **new_path_ptr, struct cam_path *path)
3706 {
3707 struct cam_path *new_path;
3708
3709 new_path = (struct cam_path *)malloc(sizeof(*path), M_CAMPATH, M_NOWAIT);
3710 if (new_path == NULL)
3711 return(CAM_RESRC_UNAVAIL);
3712 *new_path = *path;
3713 if (path->bus != NULL)
3714 xpt_acquire_bus(path->bus);
3715 if (path->target != NULL)
3716 xpt_acquire_target(path->target);
3717 if (path->device != NULL)
3718 xpt_acquire_device(path->device);
3719 *new_path_ptr = new_path;
3720 return (CAM_REQ_CMP);
3721 }
3722
3723 void
xpt_release_path(struct cam_path * path)3724 xpt_release_path(struct cam_path *path)
3725 {
3726 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
3727 if (path->device != NULL) {
3728 xpt_release_device(path->device);
3729 path->device = NULL;
3730 }
3731 if (path->target != NULL) {
3732 xpt_release_target(path->target);
3733 path->target = NULL;
3734 }
3735 if (path->bus != NULL) {
3736 xpt_release_bus(path->bus);
3737 path->bus = NULL;
3738 }
3739 }
3740
3741 void
xpt_free_path(struct cam_path * path)3742 xpt_free_path(struct cam_path *path)
3743 {
3744
3745 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
3746 xpt_release_path(path);
3747 free(path, M_CAMPATH);
3748 }
3749
3750 void
xpt_path_counts(struct cam_path * path,uint32_t * bus_ref,uint32_t * periph_ref,uint32_t * target_ref,uint32_t * device_ref)3751 xpt_path_counts(struct cam_path *path, uint32_t *bus_ref,
3752 uint32_t *periph_ref, uint32_t *target_ref, uint32_t *device_ref)
3753 {
3754
3755 xpt_lock_buses();
3756 if (bus_ref) {
3757 if (path->bus)
3758 *bus_ref = path->bus->refcount;
3759 else
3760 *bus_ref = 0;
3761 }
3762 if (periph_ref) {
3763 if (path->periph)
3764 *periph_ref = path->periph->refcount;
3765 else
3766 *periph_ref = 0;
3767 }
3768 xpt_unlock_buses();
3769 if (target_ref) {
3770 if (path->target)
3771 *target_ref = path->target->refcount;
3772 else
3773 *target_ref = 0;
3774 }
3775 if (device_ref) {
3776 if (path->device)
3777 *device_ref = path->device->refcount;
3778 else
3779 *device_ref = 0;
3780 }
3781 }
3782
3783 /*
3784 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
3785 * in path1, 2 for match with wildcards in path2.
3786 */
3787 int
xpt_path_comp(struct cam_path * path1,struct cam_path * path2)3788 xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
3789 {
3790 int retval = 0;
3791
3792 if (path1->bus != path2->bus) {
3793 if (path1->bus->path_id == CAM_BUS_WILDCARD)
3794 retval = 1;
3795 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
3796 retval = 2;
3797 else
3798 return (-1);
3799 }
3800 if (path1->target != path2->target) {
3801 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
3802 if (retval == 0)
3803 retval = 1;
3804 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
3805 retval = 2;
3806 else
3807 return (-1);
3808 }
3809 if (path1->device != path2->device) {
3810 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
3811 if (retval == 0)
3812 retval = 1;
3813 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
3814 retval = 2;
3815 else
3816 return (-1);
3817 }
3818 return (retval);
3819 }
3820
3821 int
xpt_path_comp_dev(struct cam_path * path,struct cam_ed * dev)3822 xpt_path_comp_dev(struct cam_path *path, struct cam_ed *dev)
3823 {
3824 int retval = 0;
3825
3826 if (path->bus != dev->target->bus) {
3827 if (path->bus->path_id == CAM_BUS_WILDCARD)
3828 retval = 1;
3829 else if (dev->target->bus->path_id == CAM_BUS_WILDCARD)
3830 retval = 2;
3831 else
3832 return (-1);
3833 }
3834 if (path->target != dev->target) {
3835 if (path->target->target_id == CAM_TARGET_WILDCARD) {
3836 if (retval == 0)
3837 retval = 1;
3838 } else if (dev->target->target_id == CAM_TARGET_WILDCARD)
3839 retval = 2;
3840 else
3841 return (-1);
3842 }
3843 if (path->device != dev) {
3844 if (path->device->lun_id == CAM_LUN_WILDCARD) {
3845 if (retval == 0)
3846 retval = 1;
3847 } else if (dev->lun_id == CAM_LUN_WILDCARD)
3848 retval = 2;
3849 else
3850 return (-1);
3851 }
3852 return (retval);
3853 }
3854
3855 void
xpt_print_path(struct cam_path * path)3856 xpt_print_path(struct cam_path *path)
3857 {
3858 struct sbuf sb;
3859 char buffer[XPT_PRINT_LEN];
3860
3861 sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3862 xpt_path_sbuf(path, &sb);
3863 sbuf_finish(&sb);
3864 printf("%s", sbuf_data(&sb));
3865 sbuf_delete(&sb);
3866 }
3867
3868 void
xpt_print_device(struct cam_ed * device)3869 xpt_print_device(struct cam_ed *device)
3870 {
3871
3872 if (device == NULL)
3873 printf("(nopath): ");
3874 else {
3875 printf("(noperiph:%s%d:%d:%d:%jx): ", device->sim->sim_name,
3876 device->sim->unit_number,
3877 device->sim->bus_id,
3878 device->target->target_id,
3879 (uintmax_t)device->lun_id);
3880 }
3881 }
3882
3883 void
xpt_print(struct cam_path * path,const char * fmt,...)3884 xpt_print(struct cam_path *path, const char *fmt, ...)
3885 {
3886 va_list ap;
3887 struct sbuf sb;
3888 char buffer[XPT_PRINT_LEN];
3889
3890 sbuf_new(&sb, buffer, XPT_PRINT_LEN, SBUF_FIXEDLEN);
3891
3892 xpt_path_sbuf(path, &sb);
3893 va_start(ap, fmt);
3894 sbuf_vprintf(&sb, fmt, ap);
3895 va_end(ap);
3896
3897 sbuf_finish(&sb);
3898 printf("%s", sbuf_data(&sb));
3899 sbuf_delete(&sb);
3900 }
3901
3902 int
xpt_path_string(struct cam_path * path,char * str,size_t str_len)3903 xpt_path_string(struct cam_path *path, char *str, size_t str_len)
3904 {
3905 struct sbuf sb;
3906 int len;
3907
3908 sbuf_new(&sb, str, str_len, 0);
3909 len = xpt_path_sbuf(path, &sb);
3910 sbuf_finish(&sb);
3911 return (len);
3912 }
3913
3914 int
xpt_path_sbuf(struct cam_path * path,struct sbuf * sb)3915 xpt_path_sbuf(struct cam_path *path, struct sbuf *sb)
3916 {
3917
3918 if (path == NULL)
3919 sbuf_printf(sb, "(nopath): ");
3920 else {
3921 if (path->periph != NULL)
3922 sbuf_printf(sb, "(%s%d:", path->periph->periph_name,
3923 path->periph->unit_number);
3924 else
3925 sbuf_printf(sb, "(noperiph:");
3926
3927 if (path->bus != NULL)
3928 sbuf_printf(sb, "%s%d:%d:", path->bus->sim->sim_name,
3929 path->bus->sim->unit_number,
3930 path->bus->sim->bus_id);
3931 else
3932 sbuf_printf(sb, "nobus:");
3933
3934 if (path->target != NULL)
3935 sbuf_printf(sb, "%d:", path->target->target_id);
3936 else
3937 sbuf_printf(sb, "X:");
3938
3939 if (path->device != NULL)
3940 sbuf_printf(sb, "%jx): ",
3941 (uintmax_t)path->device->lun_id);
3942 else
3943 sbuf_printf(sb, "X): ");
3944 }
3945
3946 return(sbuf_len(sb));
3947 }
3948
3949 path_id_t
xpt_path_path_id(struct cam_path * path)3950 xpt_path_path_id(struct cam_path *path)
3951 {
3952 return(path->bus->path_id);
3953 }
3954
3955 target_id_t
xpt_path_target_id(struct cam_path * path)3956 xpt_path_target_id(struct cam_path *path)
3957 {
3958 if (path->target != NULL)
3959 return (path->target->target_id);
3960 else
3961 return (CAM_TARGET_WILDCARD);
3962 }
3963
3964 lun_id_t
xpt_path_lun_id(struct cam_path * path)3965 xpt_path_lun_id(struct cam_path *path)
3966 {
3967 if (path->device != NULL)
3968 return (path->device->lun_id);
3969 else
3970 return (CAM_LUN_WILDCARD);
3971 }
3972
3973 struct cam_sim *
xpt_path_sim(struct cam_path * path)3974 xpt_path_sim(struct cam_path *path)
3975 {
3976
3977 return (path->bus->sim);
3978 }
3979
3980 struct cam_periph*
xpt_path_periph(struct cam_path * path)3981 xpt_path_periph(struct cam_path *path)
3982 {
3983
3984 return (path->periph);
3985 }
3986
3987 /*
3988 * Release a CAM control block for the caller. Remit the cost of the structure
3989 * to the device referenced by the path. If the this device had no 'credits'
3990 * and peripheral drivers have registered async callbacks for this notification
3991 * call them now.
3992 */
3993 void
xpt_release_ccb(union ccb * free_ccb)3994 xpt_release_ccb(union ccb *free_ccb)
3995 {
3996 struct cam_ed *device;
3997 struct cam_periph *periph;
3998
3999 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4000 xpt_path_assert(free_ccb->ccb_h.path, MA_OWNED);
4001 device = free_ccb->ccb_h.path->device;
4002 periph = free_ccb->ccb_h.path->periph;
4003
4004 xpt_free_ccb(free_ccb);
4005 periph->periph_allocated--;
4006 cam_ccbq_release_opening(&device->ccbq);
4007 xpt_run_allocq(periph, 0);
4008 }
4009
4010 /* Functions accessed by SIM drivers */
4011
4012 static struct xpt_xport_ops xport_default_ops = {
4013 .alloc_device = xpt_alloc_device_default,
4014 .action = xpt_action_default,
4015 .async = xpt_dev_async_default,
4016 };
4017 static struct xpt_xport xport_default = {
4018 .xport = XPORT_UNKNOWN,
4019 .name = "unknown",
4020 .ops = &xport_default_ops,
4021 };
4022
4023 CAM_XPT_XPORT(xport_default);
4024
4025 /*
4026 * A sim structure, listing the SIM entry points and instance
4027 * identification info is passed to xpt_bus_register to hook the SIM
4028 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4029 * for this new bus and places it in the array of buses and assigns
4030 * it a path_id. The path_id may be influenced by "hard wiring"
4031 * information specified by the user. Once interrupt services are
4032 * available, the bus will be probed.
4033 */
4034 int32_t
xpt_bus_register(struct cam_sim * sim,device_t parent,u_int32_t bus)4035 xpt_bus_register(struct cam_sim *sim, device_t parent, u_int32_t bus)
4036 {
4037 struct cam_eb *new_bus;
4038 struct cam_eb *old_bus;
4039 struct ccb_pathinq cpi;
4040 struct cam_path *path;
4041 cam_status status;
4042
4043 sim->bus_id = bus;
4044 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4045 M_CAMXPT, M_NOWAIT|M_ZERO);
4046 if (new_bus == NULL) {
4047 /* Couldn't satisfy request */
4048 return (CAM_RESRC_UNAVAIL);
4049 }
4050
4051 mtx_init(&new_bus->eb_mtx, "CAM bus lock", NULL, MTX_DEF);
4052 TAILQ_INIT(&new_bus->et_entries);
4053 cam_sim_hold(sim);
4054 new_bus->sim = sim;
4055 timevalclear(&new_bus->last_reset);
4056 new_bus->flags = 0;
4057 new_bus->refcount = 1; /* Held until a bus_deregister event */
4058 new_bus->generation = 0;
4059
4060 xpt_lock_buses();
4061 sim->path_id = new_bus->path_id =
4062 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4063 old_bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4064 while (old_bus != NULL
4065 && old_bus->path_id < new_bus->path_id)
4066 old_bus = TAILQ_NEXT(old_bus, links);
4067 if (old_bus != NULL)
4068 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4069 else
4070 TAILQ_INSERT_TAIL(&xsoftc.xpt_busses, new_bus, links);
4071 xsoftc.bus_generation++;
4072 xpt_unlock_buses();
4073
4074 /*
4075 * Set a default transport so that a PATH_INQ can be issued to
4076 * the SIM. This will then allow for probing and attaching of
4077 * a more appropriate transport.
4078 */
4079 new_bus->xport = &xport_default;
4080
4081 status = xpt_create_path(&path, /*periph*/NULL, sim->path_id,
4082 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4083 if (status != CAM_REQ_CMP) {
4084 xpt_release_bus(new_bus);
4085 return (CAM_RESRC_UNAVAIL);
4086 }
4087
4088 xpt_path_inq(&cpi, path);
4089
4090 if (cpi.ccb_h.status == CAM_REQ_CMP) {
4091 struct xpt_xport **xpt;
4092
4093 SET_FOREACH(xpt, cam_xpt_xport_set) {
4094 if ((*xpt)->xport == cpi.transport) {
4095 new_bus->xport = *xpt;
4096 break;
4097 }
4098 }
4099 if (new_bus->xport == NULL) {
4100 xpt_print(path,
4101 "No transport found for %d\n", cpi.transport);
4102 xpt_release_bus(new_bus);
4103 free(path, M_CAMXPT);
4104 return (CAM_RESRC_UNAVAIL);
4105 }
4106 }
4107
4108 /* Notify interested parties */
4109 if (sim->path_id != CAM_XPT_PATH_ID) {
4110
4111 xpt_async(AC_PATH_REGISTERED, path, &cpi);
4112 if ((cpi.hba_misc & PIM_NOSCAN) == 0) {
4113 union ccb *scan_ccb;
4114
4115 /* Initiate bus rescan. */
4116 scan_ccb = xpt_alloc_ccb_nowait();
4117 if (scan_ccb != NULL) {
4118 scan_ccb->ccb_h.path = path;
4119 scan_ccb->ccb_h.func_code = XPT_SCAN_BUS;
4120 scan_ccb->crcn.flags = 0;
4121 xpt_rescan(scan_ccb);
4122 } else {
4123 xpt_print(path,
4124 "Can't allocate CCB to scan bus\n");
4125 xpt_free_path(path);
4126 }
4127 } else
4128 xpt_free_path(path);
4129 } else
4130 xpt_free_path(path);
4131 return (CAM_SUCCESS);
4132 }
4133
4134 int32_t
xpt_bus_deregister(path_id_t pathid)4135 xpt_bus_deregister(path_id_t pathid)
4136 {
4137 struct cam_path bus_path;
4138 cam_status status;
4139
4140 status = xpt_compile_path(&bus_path, NULL, pathid,
4141 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4142 if (status != CAM_REQ_CMP)
4143 return (status);
4144
4145 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4146 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4147
4148 /* Release the reference count held while registered. */
4149 xpt_release_bus(bus_path.bus);
4150 xpt_release_path(&bus_path);
4151
4152 return (CAM_REQ_CMP);
4153 }
4154
4155 static path_id_t
xptnextfreepathid(void)4156 xptnextfreepathid(void)
4157 {
4158 struct cam_eb *bus;
4159 path_id_t pathid;
4160 const char *strval;
4161
4162 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4163 pathid = 0;
4164 bus = TAILQ_FIRST(&xsoftc.xpt_busses);
4165 retry:
4166 /* Find an unoccupied pathid */
4167 while (bus != NULL && bus->path_id <= pathid) {
4168 if (bus->path_id == pathid)
4169 pathid++;
4170 bus = TAILQ_NEXT(bus, links);
4171 }
4172
4173 /*
4174 * Ensure that this pathid is not reserved for
4175 * a bus that may be registered in the future.
4176 */
4177 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4178 ++pathid;
4179 /* Start the search over */
4180 goto retry;
4181 }
4182 return (pathid);
4183 }
4184
4185 static path_id_t
xptpathid(const char * sim_name,int sim_unit,int sim_bus)4186 xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4187 {
4188 path_id_t pathid;
4189 int i, dunit, val;
4190 char buf[32];
4191 const char *dname;
4192
4193 pathid = CAM_XPT_PATH_ID;
4194 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4195 if (strcmp(buf, "xpt0") == 0 && sim_bus == 0)
4196 return (pathid);
4197 i = 0;
4198 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4199 if (strcmp(dname, "scbus")) {
4200 /* Avoid a bit of foot shooting. */
4201 continue;
4202 }
4203 if (dunit < 0) /* unwired?! */
4204 continue;
4205 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4206 if (sim_bus == val) {
4207 pathid = dunit;
4208 break;
4209 }
4210 } else if (sim_bus == 0) {
4211 /* Unspecified matches bus 0 */
4212 pathid = dunit;
4213 break;
4214 } else {
4215 printf("Ambiguous scbus configuration for %s%d "
4216 "bus %d, cannot wire down. The kernel "
4217 "config entry for scbus%d should "
4218 "specify a controller bus.\n"
4219 "Scbus will be assigned dynamically.\n",
4220 sim_name, sim_unit, sim_bus, dunit);
4221 break;
4222 }
4223 }
4224
4225 if (pathid == CAM_XPT_PATH_ID)
4226 pathid = xptnextfreepathid();
4227 return (pathid);
4228 }
4229
4230 static const char *
xpt_async_string(u_int32_t async_code)4231 xpt_async_string(u_int32_t async_code)
4232 {
4233
4234 switch (async_code) {
4235 case AC_BUS_RESET: return ("AC_BUS_RESET");
4236 case AC_UNSOL_RESEL: return ("AC_UNSOL_RESEL");
4237 case AC_SCSI_AEN: return ("AC_SCSI_AEN");
4238 case AC_SENT_BDR: return ("AC_SENT_BDR");
4239 case AC_PATH_REGISTERED: return ("AC_PATH_REGISTERED");
4240 case AC_PATH_DEREGISTERED: return ("AC_PATH_DEREGISTERED");
4241 case AC_FOUND_DEVICE: return ("AC_FOUND_DEVICE");
4242 case AC_LOST_DEVICE: return ("AC_LOST_DEVICE");
4243 case AC_TRANSFER_NEG: return ("AC_TRANSFER_NEG");
4244 case AC_INQ_CHANGED: return ("AC_INQ_CHANGED");
4245 case AC_GETDEV_CHANGED: return ("AC_GETDEV_CHANGED");
4246 case AC_CONTRACT: return ("AC_CONTRACT");
4247 case AC_ADVINFO_CHANGED: return ("AC_ADVINFO_CHANGED");
4248 case AC_UNIT_ATTENTION: return ("AC_UNIT_ATTENTION");
4249 }
4250 return ("AC_UNKNOWN");
4251 }
4252
4253 static int
xpt_async_size(u_int32_t async_code)4254 xpt_async_size(u_int32_t async_code)
4255 {
4256
4257 switch (async_code) {
4258 case AC_BUS_RESET: return (0);
4259 case AC_UNSOL_RESEL: return (0);
4260 case AC_SCSI_AEN: return (0);
4261 case AC_SENT_BDR: return (0);
4262 case AC_PATH_REGISTERED: return (sizeof(struct ccb_pathinq));
4263 case AC_PATH_DEREGISTERED: return (0);
4264 case AC_FOUND_DEVICE: return (sizeof(struct ccb_getdev));
4265 case AC_LOST_DEVICE: return (0);
4266 case AC_TRANSFER_NEG: return (sizeof(struct ccb_trans_settings));
4267 case AC_INQ_CHANGED: return (0);
4268 case AC_GETDEV_CHANGED: return (0);
4269 case AC_CONTRACT: return (sizeof(struct ac_contract));
4270 case AC_ADVINFO_CHANGED: return (-1);
4271 case AC_UNIT_ATTENTION: return (sizeof(struct ccb_scsiio));
4272 }
4273 return (0);
4274 }
4275
4276 static int
xpt_async_process_dev(struct cam_ed * device,void * arg)4277 xpt_async_process_dev(struct cam_ed *device, void *arg)
4278 {
4279 union ccb *ccb = arg;
4280 struct cam_path *path = ccb->ccb_h.path;
4281 void *async_arg = ccb->casync.async_arg_ptr;
4282 u_int32_t async_code = ccb->casync.async_code;
4283 int relock;
4284
4285 if (path->device != device
4286 && path->device->lun_id != CAM_LUN_WILDCARD
4287 && device->lun_id != CAM_LUN_WILDCARD)
4288 return (1);
4289
4290 /*
4291 * The async callback could free the device.
4292 * If it is a broadcast async, it doesn't hold
4293 * device reference, so take our own reference.
4294 */
4295 xpt_acquire_device(device);
4296
4297 /*
4298 * If async for specific device is to be delivered to
4299 * the wildcard client, take the specific device lock.
4300 * XXX: We may need a way for client to specify it.
4301 */
4302 if ((device->lun_id == CAM_LUN_WILDCARD &&
4303 path->device->lun_id != CAM_LUN_WILDCARD) ||
4304 (device->target->target_id == CAM_TARGET_WILDCARD &&
4305 path->target->target_id != CAM_TARGET_WILDCARD) ||
4306 (device->target->bus->path_id == CAM_BUS_WILDCARD &&
4307 path->target->bus->path_id != CAM_BUS_WILDCARD)) {
4308 mtx_unlock(&device->device_mtx);
4309 xpt_path_lock(path);
4310 relock = 1;
4311 } else
4312 relock = 0;
4313
4314 (*(device->target->bus->xport->ops->async))(async_code,
4315 device->target->bus, device->target, device, async_arg);
4316 xpt_async_bcast(&device->asyncs, async_code, path, async_arg);
4317
4318 if (relock) {
4319 xpt_path_unlock(path);
4320 mtx_lock(&device->device_mtx);
4321 }
4322 xpt_release_device(device);
4323 return (1);
4324 }
4325
4326 static int
xpt_async_process_tgt(struct cam_et * target,void * arg)4327 xpt_async_process_tgt(struct cam_et *target, void *arg)
4328 {
4329 union ccb *ccb = arg;
4330 struct cam_path *path = ccb->ccb_h.path;
4331
4332 if (path->target != target
4333 && path->target->target_id != CAM_TARGET_WILDCARD
4334 && target->target_id != CAM_TARGET_WILDCARD)
4335 return (1);
4336
4337 if (ccb->casync.async_code == AC_SENT_BDR) {
4338 /* Update our notion of when the last reset occurred */
4339 microtime(&target->last_reset);
4340 }
4341
4342 return (xptdevicetraverse(target, NULL, xpt_async_process_dev, ccb));
4343 }
4344
4345 static void
xpt_async_process(struct cam_periph * periph,union ccb * ccb)4346 xpt_async_process(struct cam_periph *periph, union ccb *ccb)
4347 {
4348 struct cam_eb *bus;
4349 struct cam_path *path;
4350 void *async_arg;
4351 u_int32_t async_code;
4352
4353 path = ccb->ccb_h.path;
4354 async_code = ccb->casync.async_code;
4355 async_arg = ccb->casync.async_arg_ptr;
4356 CAM_DEBUG(path, CAM_DEBUG_TRACE | CAM_DEBUG_INFO,
4357 ("xpt_async(%s)\n", xpt_async_string(async_code)));
4358 bus = path->bus;
4359
4360 if (async_code == AC_BUS_RESET) {
4361 /* Update our notion of when the last reset occurred */
4362 microtime(&bus->last_reset);
4363 }
4364
4365 xpttargettraverse(bus, NULL, xpt_async_process_tgt, ccb);
4366
4367 /*
4368 * If this wasn't a fully wildcarded async, tell all
4369 * clients that want all async events.
4370 */
4371 if (bus != xpt_periph->path->bus) {
4372 xpt_path_lock(xpt_periph->path);
4373 xpt_async_process_dev(xpt_periph->path->device, ccb);
4374 xpt_path_unlock(xpt_periph->path);
4375 }
4376
4377 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4378 xpt_release_devq(path, 1, TRUE);
4379 else
4380 xpt_release_simq(path->bus->sim, TRUE);
4381 if (ccb->casync.async_arg_size > 0)
4382 free(async_arg, M_CAMXPT);
4383 xpt_free_path(path);
4384 xpt_free_ccb(ccb);
4385 }
4386
4387 static void
xpt_async_bcast(struct async_list * async_head,u_int32_t async_code,struct cam_path * path,void * async_arg)4388 xpt_async_bcast(struct async_list *async_head,
4389 u_int32_t async_code,
4390 struct cam_path *path, void *async_arg)
4391 {
4392 struct async_node *cur_entry;
4393 struct mtx *mtx;
4394
4395 cur_entry = SLIST_FIRST(async_head);
4396 while (cur_entry != NULL) {
4397 struct async_node *next_entry;
4398 /*
4399 * Grab the next list entry before we call the current
4400 * entry's callback. This is because the callback function
4401 * can delete its async callback entry.
4402 */
4403 next_entry = SLIST_NEXT(cur_entry, links);
4404 if ((cur_entry->event_enable & async_code) != 0) {
4405 mtx = cur_entry->event_lock ?
4406 path->device->sim->mtx : NULL;
4407 if (mtx)
4408 mtx_lock(mtx);
4409 cur_entry->callback(cur_entry->callback_arg,
4410 async_code, path,
4411 async_arg);
4412 if (mtx)
4413 mtx_unlock(mtx);
4414 }
4415 cur_entry = next_entry;
4416 }
4417 }
4418
4419 void
xpt_async(u_int32_t async_code,struct cam_path * path,void * async_arg)4420 xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4421 {
4422 union ccb *ccb;
4423 int size;
4424
4425 ccb = xpt_alloc_ccb_nowait();
4426 if (ccb == NULL) {
4427 xpt_print(path, "Can't allocate CCB to send %s\n",
4428 xpt_async_string(async_code));
4429 return;
4430 }
4431
4432 if (xpt_clone_path(&ccb->ccb_h.path, path) != CAM_REQ_CMP) {
4433 xpt_print(path, "Can't allocate path to send %s\n",
4434 xpt_async_string(async_code));
4435 xpt_free_ccb(ccb);
4436 return;
4437 }
4438 ccb->ccb_h.path->periph = NULL;
4439 ccb->ccb_h.func_code = XPT_ASYNC;
4440 ccb->ccb_h.cbfcnp = xpt_async_process;
4441 ccb->ccb_h.flags |= CAM_UNLOCKED;
4442 ccb->casync.async_code = async_code;
4443 ccb->casync.async_arg_size = 0;
4444 size = xpt_async_size(async_code);
4445 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
4446 ("xpt_async: func %#x %s aync_code %d %s\n",
4447 ccb->ccb_h.func_code,
4448 xpt_action_name(ccb->ccb_h.func_code),
4449 async_code,
4450 xpt_async_string(async_code)));
4451 if (size > 0 && async_arg != NULL) {
4452 ccb->casync.async_arg_ptr = malloc(size, M_CAMXPT, M_NOWAIT);
4453 if (ccb->casync.async_arg_ptr == NULL) {
4454 xpt_print(path, "Can't allocate argument to send %s\n",
4455 xpt_async_string(async_code));
4456 xpt_free_path(ccb->ccb_h.path);
4457 xpt_free_ccb(ccb);
4458 return;
4459 }
4460 memcpy(ccb->casync.async_arg_ptr, async_arg, size);
4461 ccb->casync.async_arg_size = size;
4462 } else if (size < 0) {
4463 ccb->casync.async_arg_ptr = async_arg;
4464 ccb->casync.async_arg_size = size;
4465 }
4466 if (path->device != NULL && path->device->lun_id != CAM_LUN_WILDCARD)
4467 xpt_freeze_devq(path, 1);
4468 else
4469 xpt_freeze_simq(path->bus->sim, 1);
4470 xpt_action(ccb);
4471 }
4472
4473 static void
xpt_dev_async_default(u_int32_t async_code,struct cam_eb * bus,struct cam_et * target,struct cam_ed * device,void * async_arg)4474 xpt_dev_async_default(u_int32_t async_code, struct cam_eb *bus,
4475 struct cam_et *target, struct cam_ed *device,
4476 void *async_arg)
4477 {
4478
4479 /*
4480 * We only need to handle events for real devices.
4481 */
4482 if (target->target_id == CAM_TARGET_WILDCARD
4483 || device->lun_id == CAM_LUN_WILDCARD)
4484 return;
4485
4486 printf("%s called\n", __func__);
4487 }
4488
4489 static uint32_t
xpt_freeze_devq_device(struct cam_ed * dev,u_int count)4490 xpt_freeze_devq_device(struct cam_ed *dev, u_int count)
4491 {
4492 struct cam_devq *devq;
4493 uint32_t freeze;
4494
4495 devq = dev->sim->devq;
4496 mtx_assert(&devq->send_mtx, MA_OWNED);
4497 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4498 ("xpt_freeze_devq_device(%d) %u->%u\n", count,
4499 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt + count));
4500 freeze = (dev->ccbq.queue.qfrozen_cnt += count);
4501 /* Remove frozen device from sendq. */
4502 if (device_is_queued(dev))
4503 camq_remove(&devq->send_queue, dev->devq_entry.index);
4504 return (freeze);
4505 }
4506
4507 u_int32_t
xpt_freeze_devq(struct cam_path * path,u_int count)4508 xpt_freeze_devq(struct cam_path *path, u_int count)
4509 {
4510 struct cam_ed *dev = path->device;
4511 struct cam_devq *devq;
4512 uint32_t freeze;
4513
4514 devq = dev->sim->devq;
4515 mtx_lock(&devq->send_mtx);
4516 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_freeze_devq(%d)\n", count));
4517 freeze = xpt_freeze_devq_device(dev, count);
4518 mtx_unlock(&devq->send_mtx);
4519 return (freeze);
4520 }
4521
4522 u_int32_t
xpt_freeze_simq(struct cam_sim * sim,u_int count)4523 xpt_freeze_simq(struct cam_sim *sim, u_int count)
4524 {
4525 struct cam_devq *devq;
4526 uint32_t freeze;
4527
4528 devq = sim->devq;
4529 mtx_lock(&devq->send_mtx);
4530 freeze = (devq->send_queue.qfrozen_cnt += count);
4531 mtx_unlock(&devq->send_mtx);
4532 return (freeze);
4533 }
4534
4535 static void
xpt_release_devq_timeout(void * arg)4536 xpt_release_devq_timeout(void *arg)
4537 {
4538 struct cam_ed *dev;
4539 struct cam_devq *devq;
4540
4541 dev = (struct cam_ed *)arg;
4542 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE, ("xpt_release_devq_timeout\n"));
4543 devq = dev->sim->devq;
4544 mtx_assert(&devq->send_mtx, MA_OWNED);
4545 if (xpt_release_devq_device(dev, /*count*/1, /*run_queue*/TRUE))
4546 xpt_run_devq(devq);
4547 }
4548
4549 void
xpt_release_devq(struct cam_path * path,u_int count,int run_queue)4550 xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4551 {
4552 struct cam_ed *dev;
4553 struct cam_devq *devq;
4554
4555 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_devq(%d, %d)\n",
4556 count, run_queue));
4557 dev = path->device;
4558 devq = dev->sim->devq;
4559 mtx_lock(&devq->send_mtx);
4560 if (xpt_release_devq_device(dev, count, run_queue))
4561 xpt_run_devq(dev->sim->devq);
4562 mtx_unlock(&devq->send_mtx);
4563 }
4564
4565 static int
xpt_release_devq_device(struct cam_ed * dev,u_int count,int run_queue)4566 xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4567 {
4568
4569 mtx_assert(&dev->sim->devq->send_mtx, MA_OWNED);
4570 CAM_DEBUG_DEV(dev, CAM_DEBUG_TRACE,
4571 ("xpt_release_devq_device(%d, %d) %u->%u\n", count, run_queue,
4572 dev->ccbq.queue.qfrozen_cnt, dev->ccbq.queue.qfrozen_cnt - count));
4573 if (count > dev->ccbq.queue.qfrozen_cnt) {
4574 #ifdef INVARIANTS
4575 printf("xpt_release_devq(): requested %u > present %u\n",
4576 count, dev->ccbq.queue.qfrozen_cnt);
4577 #endif
4578 count = dev->ccbq.queue.qfrozen_cnt;
4579 }
4580 dev->ccbq.queue.qfrozen_cnt -= count;
4581 if (dev->ccbq.queue.qfrozen_cnt == 0) {
4582 /*
4583 * No longer need to wait for a successful
4584 * command completion.
4585 */
4586 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4587 /*
4588 * Remove any timeouts that might be scheduled
4589 * to release this queue.
4590 */
4591 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4592 callout_stop(&dev->callout);
4593 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4594 }
4595 /*
4596 * Now that we are unfrozen schedule the
4597 * device so any pending transactions are
4598 * run.
4599 */
4600 xpt_schedule_devq(dev->sim->devq, dev);
4601 } else
4602 run_queue = 0;
4603 return (run_queue);
4604 }
4605
4606 void
xpt_release_simq(struct cam_sim * sim,int run_queue)4607 xpt_release_simq(struct cam_sim *sim, int run_queue)
4608 {
4609 struct cam_devq *devq;
4610
4611 devq = sim->devq;
4612 mtx_lock(&devq->send_mtx);
4613 if (devq->send_queue.qfrozen_cnt <= 0) {
4614 #ifdef INVARIANTS
4615 printf("xpt_release_simq: requested 1 > present %u\n",
4616 devq->send_queue.qfrozen_cnt);
4617 #endif
4618 } else
4619 devq->send_queue.qfrozen_cnt--;
4620 if (devq->send_queue.qfrozen_cnt == 0) {
4621 /*
4622 * If there is a timeout scheduled to release this
4623 * sim queue, remove it. The queue frozen count is
4624 * already at 0.
4625 */
4626 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4627 callout_stop(&sim->callout);
4628 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4629 }
4630 if (run_queue) {
4631 /*
4632 * Now that we are unfrozen run the send queue.
4633 */
4634 xpt_run_devq(sim->devq);
4635 }
4636 }
4637 mtx_unlock(&devq->send_mtx);
4638 }
4639
4640 /*
4641 * XXX Appears to be unused.
4642 */
4643 static void
xpt_release_simq_timeout(void * arg)4644 xpt_release_simq_timeout(void *arg)
4645 {
4646 struct cam_sim *sim;
4647
4648 sim = (struct cam_sim *)arg;
4649 xpt_release_simq(sim, /* run_queue */ TRUE);
4650 }
4651
4652 void
xpt_done(union ccb * done_ccb)4653 xpt_done(union ccb *done_ccb)
4654 {
4655 struct cam_doneq *queue;
4656 int run, hash;
4657
4658 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
4659 if (done_ccb->ccb_h.func_code == XPT_SCSI_IO &&
4660 done_ccb->csio.bio != NULL)
4661 biotrack(done_ccb->csio.bio, __func__);
4662 #endif
4663
4664 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4665 ("xpt_done: func= %#x %s status %#x\n",
4666 done_ccb->ccb_h.func_code,
4667 xpt_action_name(done_ccb->ccb_h.func_code),
4668 done_ccb->ccb_h.status));
4669 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4670 return;
4671
4672 /* Store the time the ccb was in the sim */
4673 done_ccb->ccb_h.qos.periph_data = cam_iosched_delta_t(done_ccb->ccb_h.qos.periph_data);
4674 hash = (u_int)(done_ccb->ccb_h.path_id + done_ccb->ccb_h.target_id +
4675 done_ccb->ccb_h.target_lun) % cam_num_doneqs;
4676 queue = &cam_doneqs[hash];
4677 mtx_lock(&queue->cam_doneq_mtx);
4678 run = (queue->cam_doneq_sleep && STAILQ_EMPTY(&queue->cam_doneq));
4679 STAILQ_INSERT_TAIL(&queue->cam_doneq, &done_ccb->ccb_h, sim_links.stqe);
4680 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4681 mtx_unlock(&queue->cam_doneq_mtx);
4682 if (run)
4683 wakeup(&queue->cam_doneq);
4684 }
4685
4686 void
xpt_done_direct(union ccb * done_ccb)4687 xpt_done_direct(union ccb *done_ccb)
4688 {
4689
4690 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
4691 ("xpt_done_direct: status %#x\n", done_ccb->ccb_h.status));
4692 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) == 0)
4693 return;
4694
4695 /* Store the time the ccb was in the sim */
4696 done_ccb->ccb_h.qos.periph_data = cam_iosched_delta_t(done_ccb->ccb_h.qos.periph_data);
4697 xpt_done_process(&done_ccb->ccb_h);
4698 }
4699
4700 union ccb *
xpt_alloc_ccb()4701 xpt_alloc_ccb()
4702 {
4703 union ccb *new_ccb;
4704
4705 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4706 return (new_ccb);
4707 }
4708
4709 union ccb *
xpt_alloc_ccb_nowait()4710 xpt_alloc_ccb_nowait()
4711 {
4712 union ccb *new_ccb;
4713
4714 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4715 return (new_ccb);
4716 }
4717
4718 void
xpt_free_ccb(union ccb * free_ccb)4719 xpt_free_ccb(union ccb *free_ccb)
4720 {
4721 free(free_ccb, M_CAMCCB);
4722 }
4723
4724
4725
4726 /* Private XPT functions */
4727
4728 /*
4729 * Get a CAM control block for the caller. Charge the structure to the device
4730 * referenced by the path. If we don't have sufficient resources to allocate
4731 * more ccbs, we return NULL.
4732 */
4733 static union ccb *
xpt_get_ccb_nowait(struct cam_periph * periph)4734 xpt_get_ccb_nowait(struct cam_periph *periph)
4735 {
4736 union ccb *new_ccb;
4737
4738 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_NOWAIT);
4739 if (new_ccb == NULL)
4740 return (NULL);
4741 periph->periph_allocated++;
4742 cam_ccbq_take_opening(&periph->path->device->ccbq);
4743 return (new_ccb);
4744 }
4745
4746 static union ccb *
xpt_get_ccb(struct cam_periph * periph)4747 xpt_get_ccb(struct cam_periph *periph)
4748 {
4749 union ccb *new_ccb;
4750
4751 cam_periph_unlock(periph);
4752 new_ccb = malloc(sizeof(*new_ccb), M_CAMCCB, M_ZERO|M_WAITOK);
4753 cam_periph_lock(periph);
4754 periph->periph_allocated++;
4755 cam_ccbq_take_opening(&periph->path->device->ccbq);
4756 return (new_ccb);
4757 }
4758
4759 union ccb *
cam_periph_getccb(struct cam_periph * periph,u_int32_t priority)4760 cam_periph_getccb(struct cam_periph *periph, u_int32_t priority)
4761 {
4762 struct ccb_hdr *ccb_h;
4763
4764 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE, ("cam_periph_getccb\n"));
4765 cam_periph_assert(periph, MA_OWNED);
4766 while ((ccb_h = SLIST_FIRST(&periph->ccb_list)) == NULL ||
4767 ccb_h->pinfo.priority != priority) {
4768 if (priority < periph->immediate_priority) {
4769 periph->immediate_priority = priority;
4770 xpt_run_allocq(periph, 0);
4771 } else
4772 cam_periph_sleep(periph, &periph->ccb_list, PRIBIO,
4773 "cgticb", 0);
4774 }
4775 SLIST_REMOVE_HEAD(&periph->ccb_list, periph_links.sle);
4776 return ((union ccb *)ccb_h);
4777 }
4778
4779 static void
xpt_acquire_bus(struct cam_eb * bus)4780 xpt_acquire_bus(struct cam_eb *bus)
4781 {
4782
4783 xpt_lock_buses();
4784 bus->refcount++;
4785 xpt_unlock_buses();
4786 }
4787
4788 static void
xpt_release_bus(struct cam_eb * bus)4789 xpt_release_bus(struct cam_eb *bus)
4790 {
4791
4792 xpt_lock_buses();
4793 KASSERT(bus->refcount >= 1, ("bus->refcount >= 1"));
4794 if (--bus->refcount > 0) {
4795 xpt_unlock_buses();
4796 return;
4797 }
4798 TAILQ_REMOVE(&xsoftc.xpt_busses, bus, links);
4799 xsoftc.bus_generation++;
4800 xpt_unlock_buses();
4801 KASSERT(TAILQ_EMPTY(&bus->et_entries),
4802 ("destroying bus, but target list is not empty"));
4803 cam_sim_release(bus->sim);
4804 mtx_destroy(&bus->eb_mtx);
4805 free(bus, M_CAMXPT);
4806 }
4807
4808 static struct cam_et *
xpt_alloc_target(struct cam_eb * bus,target_id_t target_id)4809 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4810 {
4811 struct cam_et *cur_target, *target;
4812
4813 mtx_assert(&xsoftc.xpt_topo_lock, MA_OWNED);
4814 mtx_assert(&bus->eb_mtx, MA_OWNED);
4815 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT,
4816 M_NOWAIT|M_ZERO);
4817 if (target == NULL)
4818 return (NULL);
4819
4820 TAILQ_INIT(&target->ed_entries);
4821 target->bus = bus;
4822 target->target_id = target_id;
4823 target->refcount = 1;
4824 target->generation = 0;
4825 target->luns = NULL;
4826 mtx_init(&target->luns_mtx, "CAM LUNs lock", NULL, MTX_DEF);
4827 timevalclear(&target->last_reset);
4828 /*
4829 * Hold a reference to our parent bus so it
4830 * will not go away before we do.
4831 */
4832 bus->refcount++;
4833
4834 /* Insertion sort into our bus's target list */
4835 cur_target = TAILQ_FIRST(&bus->et_entries);
4836 while (cur_target != NULL && cur_target->target_id < target_id)
4837 cur_target = TAILQ_NEXT(cur_target, links);
4838 if (cur_target != NULL) {
4839 TAILQ_INSERT_BEFORE(cur_target, target, links);
4840 } else {
4841 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4842 }
4843 bus->generation++;
4844 return (target);
4845 }
4846
4847 static void
xpt_acquire_target(struct cam_et * target)4848 xpt_acquire_target(struct cam_et *target)
4849 {
4850 struct cam_eb *bus = target->bus;
4851
4852 mtx_lock(&bus->eb_mtx);
4853 target->refcount++;
4854 mtx_unlock(&bus->eb_mtx);
4855 }
4856
4857 static void
xpt_release_target(struct cam_et * target)4858 xpt_release_target(struct cam_et *target)
4859 {
4860 struct cam_eb *bus = target->bus;
4861
4862 mtx_lock(&bus->eb_mtx);
4863 if (--target->refcount > 0) {
4864 mtx_unlock(&bus->eb_mtx);
4865 return;
4866 }
4867 TAILQ_REMOVE(&bus->et_entries, target, links);
4868 bus->generation++;
4869 mtx_unlock(&bus->eb_mtx);
4870 KASSERT(TAILQ_EMPTY(&target->ed_entries),
4871 ("destroying target, but device list is not empty"));
4872 xpt_release_bus(bus);
4873 mtx_destroy(&target->luns_mtx);
4874 if (target->luns)
4875 free(target->luns, M_CAMXPT);
4876 free(target, M_CAMXPT);
4877 }
4878
4879 static struct cam_ed *
xpt_alloc_device_default(struct cam_eb * bus,struct cam_et * target,lun_id_t lun_id)4880 xpt_alloc_device_default(struct cam_eb *bus, struct cam_et *target,
4881 lun_id_t lun_id)
4882 {
4883 struct cam_ed *device;
4884
4885 device = xpt_alloc_device(bus, target, lun_id);
4886 if (device == NULL)
4887 return (NULL);
4888
4889 device->mintags = 1;
4890 device->maxtags = 1;
4891 return (device);
4892 }
4893
4894 static void
xpt_destroy_device(void * context,int pending)4895 xpt_destroy_device(void *context, int pending)
4896 {
4897 struct cam_ed *device = context;
4898
4899 mtx_lock(&device->device_mtx);
4900 mtx_destroy(&device->device_mtx);
4901 free(device, M_CAMDEV);
4902 }
4903
4904 struct cam_ed *
xpt_alloc_device(struct cam_eb * bus,struct cam_et * target,lun_id_t lun_id)4905 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4906 {
4907 struct cam_ed *cur_device, *device;
4908 struct cam_devq *devq;
4909 cam_status status;
4910
4911 mtx_assert(&bus->eb_mtx, MA_OWNED);
4912 /* Make space for us in the device queue on our bus */
4913 devq = bus->sim->devq;
4914 mtx_lock(&devq->send_mtx);
4915 status = cam_devq_resize(devq, devq->send_queue.array_size + 1);
4916 mtx_unlock(&devq->send_mtx);
4917 if (status != CAM_REQ_CMP)
4918 return (NULL);
4919
4920 device = (struct cam_ed *)malloc(sizeof(*device),
4921 M_CAMDEV, M_NOWAIT|M_ZERO);
4922 if (device == NULL)
4923 return (NULL);
4924
4925 cam_init_pinfo(&device->devq_entry);
4926 device->target = target;
4927 device->lun_id = lun_id;
4928 device->sim = bus->sim;
4929 if (cam_ccbq_init(&device->ccbq,
4930 bus->sim->max_dev_openings) != 0) {
4931 free(device, M_CAMDEV);
4932 return (NULL);
4933 }
4934 SLIST_INIT(&device->asyncs);
4935 SLIST_INIT(&device->periphs);
4936 device->generation = 0;
4937 device->flags = CAM_DEV_UNCONFIGURED;
4938 device->tag_delay_count = 0;
4939 device->tag_saved_openings = 0;
4940 device->refcount = 1;
4941 mtx_init(&device->device_mtx, "CAM device lock", NULL, MTX_DEF);
4942 callout_init_mtx(&device->callout, &devq->send_mtx, 0);
4943 TASK_INIT(&device->device_destroy_task, 0, xpt_destroy_device, device);
4944 /*
4945 * Hold a reference to our parent bus so it
4946 * will not go away before we do.
4947 */
4948 target->refcount++;
4949
4950 cur_device = TAILQ_FIRST(&target->ed_entries);
4951 while (cur_device != NULL && cur_device->lun_id < lun_id)
4952 cur_device = TAILQ_NEXT(cur_device, links);
4953 if (cur_device != NULL)
4954 TAILQ_INSERT_BEFORE(cur_device, device, links);
4955 else
4956 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4957 target->generation++;
4958 return (device);
4959 }
4960
4961 void
xpt_acquire_device(struct cam_ed * device)4962 xpt_acquire_device(struct cam_ed *device)
4963 {
4964 struct cam_eb *bus = device->target->bus;
4965
4966 mtx_lock(&bus->eb_mtx);
4967 device->refcount++;
4968 mtx_unlock(&bus->eb_mtx);
4969 }
4970
4971 void
xpt_release_device(struct cam_ed * device)4972 xpt_release_device(struct cam_ed *device)
4973 {
4974 struct cam_eb *bus = device->target->bus;
4975 struct cam_devq *devq;
4976
4977 mtx_lock(&bus->eb_mtx);
4978 if (--device->refcount > 0) {
4979 mtx_unlock(&bus->eb_mtx);
4980 return;
4981 }
4982
4983 TAILQ_REMOVE(&device->target->ed_entries, device,links);
4984 device->target->generation++;
4985 mtx_unlock(&bus->eb_mtx);
4986
4987 /* Release our slot in the devq */
4988 devq = bus->sim->devq;
4989 mtx_lock(&devq->send_mtx);
4990 cam_devq_resize(devq, devq->send_queue.array_size - 1);
4991 mtx_unlock(&devq->send_mtx);
4992
4993 KASSERT(SLIST_EMPTY(&device->periphs),
4994 ("destroying device, but periphs list is not empty"));
4995 KASSERT(device->devq_entry.index == CAM_UNQUEUED_INDEX,
4996 ("destroying device while still queued for ccbs"));
4997
4998 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
4999 callout_stop(&device->callout);
5000
5001 xpt_release_target(device->target);
5002
5003 cam_ccbq_fini(&device->ccbq);
5004 /*
5005 * Free allocated memory. free(9) does nothing if the
5006 * supplied pointer is NULL, so it is safe to call without
5007 * checking.
5008 */
5009 free(device->supported_vpds, M_CAMXPT);
5010 free(device->device_id, M_CAMXPT);
5011 free(device->ext_inq, M_CAMXPT);
5012 free(device->physpath, M_CAMXPT);
5013 free(device->rcap_buf, M_CAMXPT);
5014 free(device->serial_num, M_CAMXPT);
5015 free(device->nvme_data, M_CAMXPT);
5016 free(device->nvme_cdata, M_CAMXPT);
5017 taskqueue_enqueue(xsoftc.xpt_taskq, &device->device_destroy_task);
5018 }
5019
5020 u_int32_t
xpt_dev_ccbq_resize(struct cam_path * path,int newopenings)5021 xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5022 {
5023 int result;
5024 struct cam_ed *dev;
5025
5026 dev = path->device;
5027 mtx_lock(&dev->sim->devq->send_mtx);
5028 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5029 mtx_unlock(&dev->sim->devq->send_mtx);
5030 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5031 || (dev->inq_flags & SID_CmdQue) != 0)
5032 dev->tag_saved_openings = newopenings;
5033 return (result);
5034 }
5035
5036 static struct cam_eb *
xpt_find_bus(path_id_t path_id)5037 xpt_find_bus(path_id_t path_id)
5038 {
5039 struct cam_eb *bus;
5040
5041 xpt_lock_buses();
5042 for (bus = TAILQ_FIRST(&xsoftc.xpt_busses);
5043 bus != NULL;
5044 bus = TAILQ_NEXT(bus, links)) {
5045 if (bus->path_id == path_id) {
5046 bus->refcount++;
5047 break;
5048 }
5049 }
5050 xpt_unlock_buses();
5051 return (bus);
5052 }
5053
5054 static struct cam_et *
xpt_find_target(struct cam_eb * bus,target_id_t target_id)5055 xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5056 {
5057 struct cam_et *target;
5058
5059 mtx_assert(&bus->eb_mtx, MA_OWNED);
5060 for (target = TAILQ_FIRST(&bus->et_entries);
5061 target != NULL;
5062 target = TAILQ_NEXT(target, links)) {
5063 if (target->target_id == target_id) {
5064 target->refcount++;
5065 break;
5066 }
5067 }
5068 return (target);
5069 }
5070
5071 static struct cam_ed *
xpt_find_device(struct cam_et * target,lun_id_t lun_id)5072 xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5073 {
5074 struct cam_ed *device;
5075
5076 mtx_assert(&target->bus->eb_mtx, MA_OWNED);
5077 for (device = TAILQ_FIRST(&target->ed_entries);
5078 device != NULL;
5079 device = TAILQ_NEXT(device, links)) {
5080 if (device->lun_id == lun_id) {
5081 device->refcount++;
5082 break;
5083 }
5084 }
5085 return (device);
5086 }
5087
5088 void
xpt_start_tags(struct cam_path * path)5089 xpt_start_tags(struct cam_path *path)
5090 {
5091 struct ccb_relsim crs;
5092 struct cam_ed *device;
5093 struct cam_sim *sim;
5094 int newopenings;
5095
5096 device = path->device;
5097 sim = path->bus->sim;
5098 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5099 xpt_freeze_devq(path, /*count*/1);
5100 device->inq_flags |= SID_CmdQue;
5101 if (device->tag_saved_openings != 0)
5102 newopenings = device->tag_saved_openings;
5103 else
5104 newopenings = min(device->maxtags,
5105 sim->max_tagged_dev_openings);
5106 xpt_dev_ccbq_resize(path, newopenings);
5107 xpt_async(AC_GETDEV_CHANGED, path, NULL);
5108 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
5109 crs.ccb_h.func_code = XPT_REL_SIMQ;
5110 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5111 crs.openings
5112 = crs.release_timeout
5113 = crs.qfrozen_cnt
5114 = 0;
5115 xpt_action((union ccb *)&crs);
5116 }
5117
5118 void
xpt_stop_tags(struct cam_path * path)5119 xpt_stop_tags(struct cam_path *path)
5120 {
5121 struct ccb_relsim crs;
5122 struct cam_ed *device;
5123 struct cam_sim *sim;
5124
5125 device = path->device;
5126 sim = path->bus->sim;
5127 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
5128 device->tag_delay_count = 0;
5129 xpt_freeze_devq(path, /*count*/1);
5130 device->inq_flags &= ~SID_CmdQue;
5131 xpt_dev_ccbq_resize(path, sim->max_dev_openings);
5132 xpt_async(AC_GETDEV_CHANGED, path, NULL);
5133 xpt_setup_ccb(&crs.ccb_h, path, CAM_PRIORITY_NORMAL);
5134 crs.ccb_h.func_code = XPT_REL_SIMQ;
5135 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
5136 crs.openings
5137 = crs.release_timeout
5138 = crs.qfrozen_cnt
5139 = 0;
5140 xpt_action((union ccb *)&crs);
5141 }
5142
5143 /*
5144 * Assume all possible buses are detected by this time, so allow boot
5145 * as soon as they all are scanned.
5146 */
5147 static void
xpt_boot_delay(void * arg)5148 xpt_boot_delay(void *arg)
5149 {
5150
5151 xpt_release_boot();
5152 }
5153
5154 /*
5155 * Now that all config hooks have completed, start boot_delay timer,
5156 * waiting for possibly still undetected buses (USB) to appear.
5157 */
5158 static void
xpt_ch_done(void * arg)5159 xpt_ch_done(void *arg)
5160 {
5161
5162 callout_init(&xsoftc.boot_callout, 1);
5163 callout_reset_sbt(&xsoftc.boot_callout, SBT_1MS * xsoftc.boot_delay, 0,
5164 xpt_boot_delay, NULL, 0);
5165 }
5166 SYSINIT(xpt_hw_delay, SI_SUB_INT_CONFIG_HOOKS, SI_ORDER_ANY, xpt_ch_done, NULL);
5167
5168 /*
5169 * Now that interrupts are enabled, go find our devices
5170 */
5171 static void
xpt_config(void * arg)5172 xpt_config(void *arg)
5173 {
5174 if (taskqueue_start_threads(&xsoftc.xpt_taskq, 1, PRIBIO, "CAM taskq"))
5175 printf("xpt_config: failed to create taskqueue thread.\n");
5176
5177 /* Setup debugging path */
5178 if (cam_dflags != CAM_DEBUG_NONE) {
5179 if (xpt_create_path(&cam_dpath, NULL,
5180 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
5181 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
5182 printf("xpt_config: xpt_create_path() failed for debug"
5183 " target %d:%d:%d, debugging disabled\n",
5184 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
5185 cam_dflags = CAM_DEBUG_NONE;
5186 }
5187 } else
5188 cam_dpath = NULL;
5189
5190 periphdriver_init(1);
5191 xpt_hold_boot();
5192
5193 /* Fire up rescan thread. */
5194 if (kproc_kthread_add(xpt_scanner_thread, NULL, &cam_proc, NULL, 0, 0,
5195 "cam", "scanner")) {
5196 printf("xpt_config: failed to create rescan thread.\n");
5197 }
5198 }
5199
5200 void
xpt_hold_boot_locked(void)5201 xpt_hold_boot_locked(void)
5202 {
5203
5204 if (xsoftc.buses_to_config++ == 0)
5205 root_mount_hold_token("CAM", &xsoftc.xpt_rootmount);
5206 }
5207
5208 void
xpt_hold_boot(void)5209 xpt_hold_boot(void)
5210 {
5211
5212 xpt_lock_buses();
5213 xpt_hold_boot_locked();
5214 xpt_unlock_buses();
5215 }
5216
5217 void
xpt_release_boot(void)5218 xpt_release_boot(void)
5219 {
5220
5221 xpt_lock_buses();
5222 if (--xsoftc.buses_to_config == 0) {
5223 if (xsoftc.buses_config_done == 0) {
5224 xsoftc.buses_config_done = 1;
5225 xsoftc.buses_to_config++;
5226 TASK_INIT(&xsoftc.boot_task, 0, xpt_finishconfig_task,
5227 NULL);
5228 taskqueue_enqueue(taskqueue_thread, &xsoftc.boot_task);
5229 } else
5230 root_mount_rel(&xsoftc.xpt_rootmount);
5231 }
5232 xpt_unlock_buses();
5233 }
5234
5235 /*
5236 * If the given device only has one peripheral attached to it, and if that
5237 * peripheral is the passthrough driver, announce it. This insures that the
5238 * user sees some sort of announcement for every peripheral in their system.
5239 */
5240 static int
xptpassannouncefunc(struct cam_ed * device,void * arg)5241 xptpassannouncefunc(struct cam_ed *device, void *arg)
5242 {
5243 struct cam_periph *periph;
5244 int i;
5245
5246 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
5247 periph = SLIST_NEXT(periph, periph_links), i++);
5248
5249 periph = SLIST_FIRST(&device->periphs);
5250 if ((i == 1)
5251 && (strncmp(periph->periph_name, "pass", 4) == 0))
5252 xpt_announce_periph(periph, NULL);
5253
5254 return(1);
5255 }
5256
5257 static void
xpt_finishconfig_task(void * context,int pending)5258 xpt_finishconfig_task(void *context, int pending)
5259 {
5260
5261 periphdriver_init(2);
5262 /*
5263 * Check for devices with no "standard" peripheral driver
5264 * attached. For any devices like that, announce the
5265 * passthrough driver so the user will see something.
5266 */
5267 if (!bootverbose)
5268 xpt_for_all_devices(xptpassannouncefunc, NULL);
5269
5270 xpt_release_boot();
5271 }
5272
5273 cam_status
xpt_register_async(int event,ac_callback_t * cbfunc,void * cbarg,struct cam_path * path)5274 xpt_register_async(int event, ac_callback_t *cbfunc, void *cbarg,
5275 struct cam_path *path)
5276 {
5277 struct ccb_setasync csa;
5278 cam_status status;
5279 int xptpath = 0;
5280
5281 if (path == NULL) {
5282 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
5283 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
5284 if (status != CAM_REQ_CMP)
5285 return (status);
5286 xpt_path_lock(path);
5287 xptpath = 1;
5288 }
5289
5290 xpt_setup_ccb(&csa.ccb_h, path, CAM_PRIORITY_NORMAL);
5291 csa.ccb_h.func_code = XPT_SASYNC_CB;
5292 csa.event_enable = event;
5293 csa.callback = cbfunc;
5294 csa.callback_arg = cbarg;
5295 xpt_action((union ccb *)&csa);
5296 status = csa.ccb_h.status;
5297
5298 CAM_DEBUG(csa.ccb_h.path, CAM_DEBUG_TRACE,
5299 ("xpt_register_async: func %p\n", cbfunc));
5300
5301 if (xptpath) {
5302 xpt_path_unlock(path);
5303 xpt_free_path(path);
5304 }
5305
5306 if ((status == CAM_REQ_CMP) &&
5307 (csa.event_enable & AC_FOUND_DEVICE)) {
5308 /*
5309 * Get this peripheral up to date with all
5310 * the currently existing devices.
5311 */
5312 xpt_for_all_devices(xptsetasyncfunc, &csa);
5313 }
5314 if ((status == CAM_REQ_CMP) &&
5315 (csa.event_enable & AC_PATH_REGISTERED)) {
5316 /*
5317 * Get this peripheral up to date with all
5318 * the currently existing buses.
5319 */
5320 xpt_for_all_busses(xptsetasyncbusfunc, &csa);
5321 }
5322
5323 return (status);
5324 }
5325
5326 static void
xptaction(struct cam_sim * sim,union ccb * work_ccb)5327 xptaction(struct cam_sim *sim, union ccb *work_ccb)
5328 {
5329 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
5330
5331 switch (work_ccb->ccb_h.func_code) {
5332 /* Common cases first */
5333 case XPT_PATH_INQ: /* Path routing inquiry */
5334 {
5335 struct ccb_pathinq *cpi;
5336
5337 cpi = &work_ccb->cpi;
5338 cpi->version_num = 1; /* XXX??? */
5339 cpi->hba_inquiry = 0;
5340 cpi->target_sprt = 0;
5341 cpi->hba_misc = 0;
5342 cpi->hba_eng_cnt = 0;
5343 cpi->max_target = 0;
5344 cpi->max_lun = 0;
5345 cpi->initiator_id = 0;
5346 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
5347 strlcpy(cpi->hba_vid, "", HBA_IDLEN);
5348 strlcpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
5349 cpi->unit_number = sim->unit_number;
5350 cpi->bus_id = sim->bus_id;
5351 cpi->base_transfer_speed = 0;
5352 cpi->protocol = PROTO_UNSPECIFIED;
5353 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
5354 cpi->transport = XPORT_UNSPECIFIED;
5355 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
5356 cpi->ccb_h.status = CAM_REQ_CMP;
5357 xpt_done(work_ccb);
5358 break;
5359 }
5360 default:
5361 work_ccb->ccb_h.status = CAM_REQ_INVALID;
5362 xpt_done(work_ccb);
5363 break;
5364 }
5365 }
5366
5367 /*
5368 * The xpt as a "controller" has no interrupt sources, so polling
5369 * is a no-op.
5370 */
5371 static void
xptpoll(struct cam_sim * sim)5372 xptpoll(struct cam_sim *sim)
5373 {
5374 }
5375
5376 void
xpt_lock_buses(void)5377 xpt_lock_buses(void)
5378 {
5379 mtx_lock(&xsoftc.xpt_topo_lock);
5380 }
5381
5382 void
xpt_unlock_buses(void)5383 xpt_unlock_buses(void)
5384 {
5385 mtx_unlock(&xsoftc.xpt_topo_lock);
5386 }
5387
5388 struct mtx *
xpt_path_mtx(struct cam_path * path)5389 xpt_path_mtx(struct cam_path *path)
5390 {
5391
5392 return (&path->device->device_mtx);
5393 }
5394
5395 static void
xpt_done_process(struct ccb_hdr * ccb_h)5396 xpt_done_process(struct ccb_hdr *ccb_h)
5397 {
5398 struct cam_sim *sim = NULL;
5399 struct cam_devq *devq = NULL;
5400 struct mtx *mtx = NULL;
5401
5402 #if defined(BUF_TRACKING) || defined(FULL_BUF_TRACKING)
5403 struct ccb_scsiio *csio;
5404
5405 if (ccb_h->func_code == XPT_SCSI_IO) {
5406 csio = &((union ccb *)ccb_h)->csio;
5407 if (csio->bio != NULL)
5408 biotrack(csio->bio, __func__);
5409 }
5410 #endif
5411
5412 if (ccb_h->flags & CAM_HIGH_POWER) {
5413 struct highpowerlist *hphead;
5414 struct cam_ed *device;
5415
5416 mtx_lock(&xsoftc.xpt_highpower_lock);
5417 hphead = &xsoftc.highpowerq;
5418
5419 device = STAILQ_FIRST(hphead);
5420
5421 /*
5422 * Increment the count since this command is done.
5423 */
5424 xsoftc.num_highpower++;
5425
5426 /*
5427 * Any high powered commands queued up?
5428 */
5429 if (device != NULL) {
5430
5431 STAILQ_REMOVE_HEAD(hphead, highpowerq_entry);
5432 mtx_unlock(&xsoftc.xpt_highpower_lock);
5433
5434 mtx_lock(&device->sim->devq->send_mtx);
5435 xpt_release_devq_device(device,
5436 /*count*/1, /*runqueue*/TRUE);
5437 mtx_unlock(&device->sim->devq->send_mtx);
5438 } else
5439 mtx_unlock(&xsoftc.xpt_highpower_lock);
5440 }
5441
5442 /*
5443 * Insulate against a race where the periph is destroyed but CCBs are
5444 * still not all processed. This shouldn't happen, but allows us better
5445 * bug diagnostic when it does.
5446 */
5447 if (ccb_h->path->bus)
5448 sim = ccb_h->path->bus->sim;
5449
5450 if (ccb_h->status & CAM_RELEASE_SIMQ) {
5451 KASSERT(sim, ("sim missing for CAM_RELEASE_SIMQ request"));
5452 xpt_release_simq(sim, /*run_queue*/FALSE);
5453 ccb_h->status &= ~CAM_RELEASE_SIMQ;
5454 }
5455
5456 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
5457 && (ccb_h->status & CAM_DEV_QFRZN)) {
5458 xpt_release_devq(ccb_h->path, /*count*/1, /*run_queue*/TRUE);
5459 ccb_h->status &= ~CAM_DEV_QFRZN;
5460 }
5461
5462 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
5463 struct cam_ed *dev = ccb_h->path->device;
5464
5465 if (sim)
5466 devq = sim->devq;
5467 KASSERT(devq, ("Periph disappeared with request pending."));
5468
5469 mtx_lock(&devq->send_mtx);
5470 devq->send_active--;
5471 devq->send_openings++;
5472 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
5473
5474 if (((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
5475 && (dev->ccbq.dev_active == 0))) {
5476 dev->flags &= ~CAM_DEV_REL_ON_QUEUE_EMPTY;
5477 xpt_release_devq_device(dev, /*count*/1,
5478 /*run_queue*/FALSE);
5479 }
5480
5481 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
5482 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)) {
5483 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
5484 xpt_release_devq_device(dev, /*count*/1,
5485 /*run_queue*/FALSE);
5486 }
5487
5488 if (!device_is_queued(dev))
5489 (void)xpt_schedule_devq(devq, dev);
5490 xpt_run_devq(devq);
5491 mtx_unlock(&devq->send_mtx);
5492
5493 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0) {
5494 mtx = xpt_path_mtx(ccb_h->path);
5495 mtx_lock(mtx);
5496
5497 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5498 && (--dev->tag_delay_count == 0))
5499 xpt_start_tags(ccb_h->path);
5500 }
5501 }
5502
5503 if ((ccb_h->flags & CAM_UNLOCKED) == 0) {
5504 if (mtx == NULL) {
5505 mtx = xpt_path_mtx(ccb_h->path);
5506 mtx_lock(mtx);
5507 }
5508 } else {
5509 if (mtx != NULL) {
5510 mtx_unlock(mtx);
5511 mtx = NULL;
5512 }
5513 }
5514
5515 /* Call the peripheral driver's callback */
5516 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
5517 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
5518 if (mtx != NULL)
5519 mtx_unlock(mtx);
5520 }
5521
5522 void
xpt_done_td(void * arg)5523 xpt_done_td(void *arg)
5524 {
5525 struct cam_doneq *queue = arg;
5526 struct ccb_hdr *ccb_h;
5527 STAILQ_HEAD(, ccb_hdr) doneq;
5528
5529 STAILQ_INIT(&doneq);
5530 mtx_lock(&queue->cam_doneq_mtx);
5531 while (1) {
5532 while (STAILQ_EMPTY(&queue->cam_doneq)) {
5533 queue->cam_doneq_sleep = 1;
5534 msleep(&queue->cam_doneq, &queue->cam_doneq_mtx,
5535 PRIBIO, "-", 0);
5536 queue->cam_doneq_sleep = 0;
5537 }
5538 STAILQ_CONCAT(&doneq, &queue->cam_doneq);
5539 mtx_unlock(&queue->cam_doneq_mtx);
5540
5541 THREAD_NO_SLEEPING();
5542 while ((ccb_h = STAILQ_FIRST(&doneq)) != NULL) {
5543 STAILQ_REMOVE_HEAD(&doneq, sim_links.stqe);
5544 xpt_done_process(ccb_h);
5545 }
5546 THREAD_SLEEPING_OK();
5547
5548 mtx_lock(&queue->cam_doneq_mtx);
5549 }
5550 }
5551
5552 static void
camisr_runqueue(void)5553 camisr_runqueue(void)
5554 {
5555 struct ccb_hdr *ccb_h;
5556 struct cam_doneq *queue;
5557 int i;
5558
5559 /* Process global queues. */
5560 for (i = 0; i < cam_num_doneqs; i++) {
5561 queue = &cam_doneqs[i];
5562 mtx_lock(&queue->cam_doneq_mtx);
5563 while ((ccb_h = STAILQ_FIRST(&queue->cam_doneq)) != NULL) {
5564 STAILQ_REMOVE_HEAD(&queue->cam_doneq, sim_links.stqe);
5565 mtx_unlock(&queue->cam_doneq_mtx);
5566 xpt_done_process(ccb_h);
5567 mtx_lock(&queue->cam_doneq_mtx);
5568 }
5569 mtx_unlock(&queue->cam_doneq_mtx);
5570 }
5571 }
5572
5573 struct kv
5574 {
5575 uint32_t v;
5576 const char *name;
5577 };
5578
5579 static struct kv map[] = {
5580 { XPT_NOOP, "XPT_NOOP" },
5581 { XPT_SCSI_IO, "XPT_SCSI_IO" },
5582 { XPT_GDEV_TYPE, "XPT_GDEV_TYPE" },
5583 { XPT_GDEVLIST, "XPT_GDEVLIST" },
5584 { XPT_PATH_INQ, "XPT_PATH_INQ" },
5585 { XPT_REL_SIMQ, "XPT_REL_SIMQ" },
5586 { XPT_SASYNC_CB, "XPT_SASYNC_CB" },
5587 { XPT_SDEV_TYPE, "XPT_SDEV_TYPE" },
5588 { XPT_SCAN_BUS, "XPT_SCAN_BUS" },
5589 { XPT_DEV_MATCH, "XPT_DEV_MATCH" },
5590 { XPT_DEBUG, "XPT_DEBUG" },
5591 { XPT_PATH_STATS, "XPT_PATH_STATS" },
5592 { XPT_GDEV_STATS, "XPT_GDEV_STATS" },
5593 { XPT_DEV_ADVINFO, "XPT_DEV_ADVINFO" },
5594 { XPT_ASYNC, "XPT_ASYNC" },
5595 { XPT_ABORT, "XPT_ABORT" },
5596 { XPT_RESET_BUS, "XPT_RESET_BUS" },
5597 { XPT_RESET_DEV, "XPT_RESET_DEV" },
5598 { XPT_TERM_IO, "XPT_TERM_IO" },
5599 { XPT_SCAN_LUN, "XPT_SCAN_LUN" },
5600 { XPT_GET_TRAN_SETTINGS, "XPT_GET_TRAN_SETTINGS" },
5601 { XPT_SET_TRAN_SETTINGS, "XPT_SET_TRAN_SETTINGS" },
5602 { XPT_CALC_GEOMETRY, "XPT_CALC_GEOMETRY" },
5603 { XPT_ATA_IO, "XPT_ATA_IO" },
5604 { XPT_GET_SIM_KNOB, "XPT_GET_SIM_KNOB" },
5605 { XPT_SET_SIM_KNOB, "XPT_SET_SIM_KNOB" },
5606 { XPT_NVME_IO, "XPT_NVME_IO" },
5607 { XPT_MMC_IO, "XPT_MMC_IO" },
5608 { XPT_SMP_IO, "XPT_SMP_IO" },
5609 { XPT_SCAN_TGT, "XPT_SCAN_TGT" },
5610 { XPT_NVME_ADMIN, "XPT_NVME_ADMIN" },
5611 { XPT_ENG_INQ, "XPT_ENG_INQ" },
5612 { XPT_ENG_EXEC, "XPT_ENG_EXEC" },
5613 { XPT_EN_LUN, "XPT_EN_LUN" },
5614 { XPT_TARGET_IO, "XPT_TARGET_IO" },
5615 { XPT_ACCEPT_TARGET_IO, "XPT_ACCEPT_TARGET_IO" },
5616 { XPT_CONT_TARGET_IO, "XPT_CONT_TARGET_IO" },
5617 { XPT_IMMED_NOTIFY, "XPT_IMMED_NOTIFY" },
5618 { XPT_NOTIFY_ACK, "XPT_NOTIFY_ACK" },
5619 { XPT_IMMEDIATE_NOTIFY, "XPT_IMMEDIATE_NOTIFY" },
5620 { XPT_NOTIFY_ACKNOWLEDGE, "XPT_NOTIFY_ACKNOWLEDGE" },
5621 { 0, 0 }
5622 };
5623
5624 const char *
xpt_action_name(uint32_t action)5625 xpt_action_name(uint32_t action)
5626 {
5627 static char buffer[32]; /* Only for unknown messages -- racy */
5628 struct kv *walker = map;
5629
5630 while (walker->name != NULL) {
5631 if (walker->v == action)
5632 return (walker->name);
5633 walker++;
5634 }
5635
5636 snprintf(buffer, sizeof(buffer), "%#x", action);
5637 return (buffer);
5638 }
5639