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