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