Deleted Added
full compact
cam_xpt.c (111119) cam_xpt.c (111815)
1/*
2 * Implementation of the Common Access Method Transport (XPT) layer.
3 *
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
1/*
2 * Implementation of the Common Access Method Transport (XPT) layer.
3 *
4 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
5 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions, and the following disclaimer,
13 * without modification, immediately at the beginning of the file.
14 * 2. The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
21 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/sys/cam/cam_xpt.c 111119 2003-02-19 05:47:46Z imp $
29 * $FreeBSD: head/sys/cam/cam_xpt.c 111815 2003-03-03 12:15:54Z phk $
30 */
31#include <sys/param.h>
32#include <sys/bus.h>
33#include <sys/systm.h>
34#include <sys/types.h>
35#include <sys/malloc.h>
36#include <sys/kernel.h>
37#include <sys/time.h>
38#include <sys/conf.h>
39#include <sys/fcntl.h>
40#include <sys/md5.h>
41#include <sys/devicestat.h>
42#include <sys/interrupt.h>
43#include <sys/sbuf.h>
44
45#ifdef PC98
46#include <pc98/pc98/pc98_machdep.h> /* geometry translation */
47#endif
48
49#include <cam/cam.h>
50#include <cam/cam_ccb.h>
51#include <cam/cam_periph.h>
52#include <cam/cam_sim.h>
53#include <cam/cam_xpt.h>
54#include <cam/cam_xpt_sim.h>
55#include <cam/cam_xpt_periph.h>
56#include <cam/cam_debug.h>
57
58#include <cam/scsi/scsi_all.h>
59#include <cam/scsi/scsi_message.h>
60#include <cam/scsi/scsi_pass.h>
61#include "opt_cam.h"
62
63/* Datastructures internal to the xpt layer */
64
65/*
66 * Definition of an async handler callback block. These are used to add
67 * SIMs and peripherals to the async callback lists.
68 */
69struct async_node {
70 SLIST_ENTRY(async_node) links;
71 u_int32_t event_enable; /* Async Event enables */
72 void (*callback)(void *arg, u_int32_t code,
73 struct cam_path *path, void *args);
74 void *callback_arg;
75};
76
77SLIST_HEAD(async_list, async_node);
78SLIST_HEAD(periph_list, cam_periph);
79static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
80
81/*
82 * This is the maximum number of high powered commands (e.g. start unit)
83 * that can be outstanding at a particular time.
84 */
85#ifndef CAM_MAX_HIGHPOWER
86#define CAM_MAX_HIGHPOWER 4
87#endif
88
89/* number of high powered commands that can go through right now */
90static int num_highpower = CAM_MAX_HIGHPOWER;
91
92/*
93 * Structure for queueing a device in a run queue.
94 * There is one run queue for allocating new ccbs,
95 * and another for sending ccbs to the controller.
96 */
97struct cam_ed_qinfo {
98 cam_pinfo pinfo;
99 struct cam_ed *device;
100};
101
102/*
103 * The CAM EDT (Existing Device Table) contains the device information for
104 * all devices for all busses in the system. The table contains a
105 * cam_ed structure for each device on the bus.
106 */
107struct cam_ed {
108 TAILQ_ENTRY(cam_ed) links;
109 struct cam_ed_qinfo alloc_ccb_entry;
110 struct cam_ed_qinfo send_ccb_entry;
111 struct cam_et *target;
112 lun_id_t lun_id;
113 struct camq drvq; /*
114 * Queue of type drivers wanting to do
115 * work on this device.
116 */
117 struct cam_ccbq ccbq; /* Queue of pending ccbs */
118 struct async_list asyncs; /* Async callback info for this B/T/L */
119 struct periph_list periphs; /* All attached devices */
120 u_int generation; /* Generation number */
121 struct cam_periph *owner; /* Peripheral driver's ownership tag */
122 struct xpt_quirk_entry *quirk; /* Oddities about this device */
123 /* Storage for the inquiry data */
124#ifdef CAM_NEW_TRAN_CODE
125 cam_proto protocol;
126 u_int protocol_version;
127 cam_xport transport;
128 u_int transport_version;
129#endif /* CAM_NEW_TRAN_CODE */
130 struct scsi_inquiry_data inq_data;
131 u_int8_t inq_flags; /*
132 * Current settings for inquiry flags.
133 * This allows us to override settings
134 * like disconnection and tagged
135 * queuing for a device.
136 */
137 u_int8_t queue_flags; /* Queue flags from the control page */
138 u_int8_t serial_num_len;
139 u_int8_t *serial_num;
140 u_int32_t qfrozen_cnt;
141 u_int32_t flags;
142#define CAM_DEV_UNCONFIGURED 0x01
143#define CAM_DEV_REL_TIMEOUT_PENDING 0x02
144#define CAM_DEV_REL_ON_COMPLETE 0x04
145#define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
146#define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
147#define CAM_DEV_TAG_AFTER_COUNT 0x20
148#define CAM_DEV_INQUIRY_DATA_VALID 0x40
149 u_int32_t tag_delay_count;
150#define CAM_TAG_DELAY_COUNT 5
151 u_int32_t refcount;
152 struct callout_handle c_handle;
153};
154
155/*
156 * Each target is represented by an ET (Existing Target). These
157 * entries are created when a target is successfully probed with an
158 * identify, and removed when a device fails to respond after a number
159 * of retries, or a bus rescan finds the device missing.
160 */
161struct cam_et {
162 TAILQ_HEAD(, cam_ed) ed_entries;
163 TAILQ_ENTRY(cam_et) links;
164 struct cam_eb *bus;
165 target_id_t target_id;
166 u_int32_t refcount;
167 u_int generation;
168 struct timeval last_reset;
169};
170
171/*
172 * Each bus is represented by an EB (Existing Bus). These entries
173 * are created by calls to xpt_bus_register and deleted by calls to
174 * xpt_bus_deregister.
175 */
176struct cam_eb {
177 TAILQ_HEAD(, cam_et) et_entries;
178 TAILQ_ENTRY(cam_eb) links;
179 path_id_t path_id;
180 struct cam_sim *sim;
181 struct timeval last_reset;
182 u_int32_t flags;
183#define CAM_EB_RUNQ_SCHEDULED 0x01
184 u_int32_t refcount;
185 u_int generation;
186};
187
188struct cam_path {
189 struct cam_periph *periph;
190 struct cam_eb *bus;
191 struct cam_et *target;
192 struct cam_ed *device;
193};
194
195struct xpt_quirk_entry {
196 struct scsi_inquiry_pattern inq_pat;
197 u_int8_t quirks;
198#define CAM_QUIRK_NOLUNS 0x01
199#define CAM_QUIRK_NOSERIAL 0x02
200#define CAM_QUIRK_HILUNS 0x04
201 u_int mintags;
202 u_int maxtags;
203};
204#define CAM_SCSI2_MAXLUN 8
205
206typedef enum {
207 XPT_FLAG_OPEN = 0x01
208} xpt_flags;
209
210struct xpt_softc {
211 xpt_flags flags;
212 u_int32_t generation;
213};
214
215static const char quantum[] = "QUANTUM";
216static const char sony[] = "SONY";
217static const char west_digital[] = "WDIGTL";
218static const char samsung[] = "SAMSUNG";
219static const char seagate[] = "SEAGATE";
220static const char microp[] = "MICROP";
221
222static struct xpt_quirk_entry xpt_quirk_table[] =
223{
224 {
225 /* Reports QUEUE FULL for temporary resource shortages */
226 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
227 /*quirks*/0, /*mintags*/24, /*maxtags*/32
228 },
229 {
230 /* Reports QUEUE FULL for temporary resource shortages */
231 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
232 /*quirks*/0, /*mintags*/24, /*maxtags*/32
233 },
234 {
235 /* Reports QUEUE FULL for temporary resource shortages */
236 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
237 /*quirks*/0, /*mintags*/24, /*maxtags*/32
238 },
239 {
240 /* Broken tagged queuing drive */
241 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
242 /*quirks*/0, /*mintags*/0, /*maxtags*/0
243 },
244 {
245 /* Broken tagged queuing drive */
246 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
247 /*quirks*/0, /*mintags*/0, /*maxtags*/0
248 },
249 {
250 /* Broken tagged queuing drive */
251 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
252 /*quirks*/0, /*mintags*/0, /*maxtags*/0
253 },
254 {
255 /*
256 * Unfortunately, the Quantum Atlas III has the same
257 * problem as the Atlas II drives above.
258 * Reported by: "Johan Granlund" <johan@granlund.nu>
259 *
260 * For future reference, the drive with the problem was:
261 * QUANTUM QM39100TD-SW N1B0
262 *
263 * It's possible that Quantum will fix the problem in later
264 * firmware revisions. If that happens, the quirk entry
265 * will need to be made specific to the firmware revisions
266 * with the problem.
267 *
268 */
269 /* Reports QUEUE FULL for temporary resource shortages */
270 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
271 /*quirks*/0, /*mintags*/24, /*maxtags*/32
272 },
273 {
274 /*
275 * 18 Gig Atlas III, same problem as the 9G version.
276 * Reported by: Andre Albsmeier
277 * <andre.albsmeier@mchp.siemens.de>
278 *
279 * For future reference, the drive with the problem was:
280 * QUANTUM QM318000TD-S N491
281 */
282 /* Reports QUEUE FULL for temporary resource shortages */
283 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
284 /*quirks*/0, /*mintags*/24, /*maxtags*/32
285 },
286 {
287 /*
288 * Broken tagged queuing drive
289 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
290 * and: Martin Renters <martin@tdc.on.ca>
291 */
292 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
293 /*quirks*/0, /*mintags*/0, /*maxtags*/0
294 },
295 /*
296 * The Seagate Medalist Pro drives have very poor write
297 * performance with anything more than 2 tags.
298 *
299 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
300 * Drive: <SEAGATE ST36530N 1444>
301 *
302 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
303 * Drive: <SEAGATE ST34520W 1281>
304 *
305 * No one has actually reported that the 9G version
306 * (ST39140*) of the Medalist Pro has the same problem, but
307 * we're assuming that it does because the 4G and 6.5G
308 * versions of the drive are broken.
309 */
310 {
311 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
312 /*quirks*/0, /*mintags*/2, /*maxtags*/2
313 },
314 {
315 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
316 /*quirks*/0, /*mintags*/2, /*maxtags*/2
317 },
318 {
319 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
320 /*quirks*/0, /*mintags*/2, /*maxtags*/2
321 },
322 {
323 /*
324 * Slow when tagged queueing is enabled. Write performance
325 * steadily drops off with more and more concurrent
326 * transactions. Best sequential write performance with
327 * tagged queueing turned off and write caching turned on.
328 *
329 * PR: kern/10398
330 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
331 * Drive: DCAS-34330 w/ "S65A" firmware.
332 *
333 * The drive with the problem had the "S65A" firmware
334 * revision, and has also been reported (by Stephen J.
335 * Roznowski <sjr@home.net>) for a drive with the "S61A"
336 * firmware revision.
337 *
338 * Although no one has reported problems with the 2 gig
339 * version of the DCAS drive, the assumption is that it
340 * has the same problems as the 4 gig version. Therefore
341 * this quirk entries disables tagged queueing for all
342 * DCAS drives.
343 */
344 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
345 /*quirks*/0, /*mintags*/0, /*maxtags*/0
346 },
347 {
348 /* Broken tagged queuing drive */
349 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
350 /*quirks*/0, /*mintags*/0, /*maxtags*/0
351 },
352 {
353 /* Broken tagged queuing drive */
354 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
355 /*quirks*/0, /*mintags*/0, /*maxtags*/0
356 },
357 {
358 /*
359 * Broken tagged queuing drive.
360 * Submitted by:
361 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
362 * in PR kern/9535
363 */
364 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
365 /*quirks*/0, /*mintags*/0, /*maxtags*/0
366 },
367 {
368 /*
369 * Slow when tagged queueing is enabled. (1.5MB/sec versus
370 * 8MB/sec.)
371 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
372 * Best performance with these drives is achieved with
373 * tagged queueing turned off, and write caching turned on.
374 */
375 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
376 /*quirks*/0, /*mintags*/0, /*maxtags*/0
377 },
378 {
379 /*
380 * Slow when tagged queueing is enabled. (1.5MB/sec versus
381 * 8MB/sec.)
382 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
383 * Best performance with these drives is achieved with
384 * tagged queueing turned off, and write caching turned on.
385 */
386 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
387 /*quirks*/0, /*mintags*/0, /*maxtags*/0
388 },
389 {
390 /*
391 * Doesn't handle queue full condition correctly,
392 * so we need to limit maxtags to what the device
393 * can handle instead of determining this automatically.
394 */
395 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
396 /*quirks*/0, /*mintags*/2, /*maxtags*/32
397 },
398 {
399 /* Really only one LUN */
400 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
401 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
402 },
403 {
404 /* I can't believe we need a quirk for DPT volumes. */
405 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
406 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
407 /*mintags*/0, /*maxtags*/255
408 },
409 {
410 /*
411 * Many Sony CDROM drives don't like multi-LUN probing.
412 */
413 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
414 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
415 },
416 {
417 /*
418 * This drive doesn't like multiple LUN probing.
419 * Submitted by: Parag Patel <parag@cgt.com>
420 */
421 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
422 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
423 },
424 {
425 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
426 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
427 },
428 {
429 /*
430 * The 8200 doesn't like multi-lun probing, and probably
431 * don't like serial number requests either.
432 */
433 {
434 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
435 "EXB-8200*", "*"
436 },
437 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
438 },
439 {
440 /*
441 * Let's try the same as above, but for a drive that says
442 * it's an IPL-6860 but is actually an EXB 8200.
443 */
444 {
445 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
446 "IPL-6860*", "*"
447 },
448 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
449 },
450 {
451 /*
452 * These Hitachi drives don't like multi-lun probing.
453 * The PR submitter has a DK319H, but says that the Linux
454 * kernel has a similar work-around for the DK312 and DK314,
455 * so all DK31* drives are quirked here.
456 * PR: misc/18793
457 * Submitted by: Paul Haddad <paul@pth.com>
458 */
459 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
460 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
461 },
462 {
463 /*
464 * The Hitachi CJ series with J8A8 firmware apparantly has
465 * problems with tagged commands.
466 * PR: 23536
467 * Reported by: amagai@nue.org
468 */
469 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
470 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
471 },
472 {
473 /*
474 * These are the large storage arrays.
475 * Submitted by: William Carrel <william.carrel@infospace.com>
476 */
477 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
478 CAM_QUIRK_HILUNS, 2, 1024
479 },
480 {
481 /*
482 * This old revision of the TDC3600 is also SCSI-1, and
483 * hangs upon serial number probing.
484 */
485 {
486 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
487 " TDC 3600", "U07:"
488 },
489 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
490 },
491 {
492 /*
493 * Maxtor Personal Storage 3000XT (Firewire)
494 * hangs upon serial number probing.
495 */
496 {
497 T_DIRECT, SIP_MEDIA_FIXED, "Maxtor",
498 "1394 storage", "*"
499 },
500 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
501 },
502 {
503 /*
504 * Would repond to all LUNs if asked for.
505 */
506 {
507 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
508 "CP150", "*"
509 },
510 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
511 },
512 {
513 /*
514 * Would repond to all LUNs if asked for.
515 */
516 {
517 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
518 "96X2*", "*"
519 },
520 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
521 },
522 {
523 /* Submitted by: Matthew Dodd <winter@jurai.net> */
524 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
525 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
526 },
527 {
528 /* Submitted by: Matthew Dodd <winter@jurai.net> */
529 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
530 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
531 },
532 {
533 /* TeraSolutions special settings for TRC-22 RAID */
534 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
535 /*quirks*/0, /*mintags*/55, /*maxtags*/255
536 },
537 {
538 /* Veritas Storage Appliance */
539 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
540 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
541 },
542 {
543 /*
544 * Would respond to all LUNs. Device type and removable
545 * flag are jumper-selectable.
546 */
547 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
548 "Tahiti 1", "*"
549 },
550 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
551 },
552 {
553 /* Default tagged queuing parameters for all devices */
554 {
555 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
556 /*vendor*/"*", /*product*/"*", /*revision*/"*"
557 },
558 /*quirks*/0, /*mintags*/2, /*maxtags*/255
559 },
560};
561
562static const int xpt_quirk_table_size =
563 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
564
565typedef enum {
566 DM_RET_COPY = 0x01,
567 DM_RET_FLAG_MASK = 0x0f,
568 DM_RET_NONE = 0x00,
569 DM_RET_STOP = 0x10,
570 DM_RET_DESCEND = 0x20,
571 DM_RET_ERROR = 0x30,
572 DM_RET_ACTION_MASK = 0xf0
573} dev_match_ret;
574
575typedef enum {
576 XPT_DEPTH_BUS,
577 XPT_DEPTH_TARGET,
578 XPT_DEPTH_DEVICE,
579 XPT_DEPTH_PERIPH
580} xpt_traverse_depth;
581
582struct xpt_traverse_config {
583 xpt_traverse_depth depth;
584 void *tr_func;
585 void *tr_arg;
586};
587
588typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
589typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
590typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
591typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
592typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
593
594/* Transport layer configuration information */
595static struct xpt_softc xsoftc;
596
597/* Queues for our software interrupt handler */
598typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
599static cam_isrq_t cam_bioq;
600static cam_isrq_t cam_netq;
601
602/* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
603static SLIST_HEAD(,ccb_hdr) ccb_freeq;
604static u_int xpt_max_ccbs; /*
605 * Maximum size of ccb pool. Modified as
606 * devices are added/removed or have their
607 * opening counts changed.
608 */
609static u_int xpt_ccb_count; /* Current count of allocated ccbs */
610
611struct cam_periph *xpt_periph;
612
613static periph_init_t xpt_periph_init;
614
615static periph_init_t probe_periph_init;
616
617static struct periph_driver xpt_driver =
618{
619 xpt_periph_init, "xpt",
620 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
621};
622
623static struct periph_driver probe_driver =
624{
625 probe_periph_init, "probe",
626 TAILQ_HEAD_INITIALIZER(probe_driver.units)
627};
628
629PERIPHDRIVER_DECLARE(xpt, xpt_driver);
630PERIPHDRIVER_DECLARE(probe, probe_driver);
631
632#define XPT_CDEV_MAJOR 104
633
634static d_open_t xptopen;
635static d_close_t xptclose;
636static d_ioctl_t xptioctl;
637
638static struct cdevsw xpt_cdevsw = {
30 */
31#include <sys/param.h>
32#include <sys/bus.h>
33#include <sys/systm.h>
34#include <sys/types.h>
35#include <sys/malloc.h>
36#include <sys/kernel.h>
37#include <sys/time.h>
38#include <sys/conf.h>
39#include <sys/fcntl.h>
40#include <sys/md5.h>
41#include <sys/devicestat.h>
42#include <sys/interrupt.h>
43#include <sys/sbuf.h>
44
45#ifdef PC98
46#include <pc98/pc98/pc98_machdep.h> /* geometry translation */
47#endif
48
49#include <cam/cam.h>
50#include <cam/cam_ccb.h>
51#include <cam/cam_periph.h>
52#include <cam/cam_sim.h>
53#include <cam/cam_xpt.h>
54#include <cam/cam_xpt_sim.h>
55#include <cam/cam_xpt_periph.h>
56#include <cam/cam_debug.h>
57
58#include <cam/scsi/scsi_all.h>
59#include <cam/scsi/scsi_message.h>
60#include <cam/scsi/scsi_pass.h>
61#include "opt_cam.h"
62
63/* Datastructures internal to the xpt layer */
64
65/*
66 * Definition of an async handler callback block. These are used to add
67 * SIMs and peripherals to the async callback lists.
68 */
69struct async_node {
70 SLIST_ENTRY(async_node) links;
71 u_int32_t event_enable; /* Async Event enables */
72 void (*callback)(void *arg, u_int32_t code,
73 struct cam_path *path, void *args);
74 void *callback_arg;
75};
76
77SLIST_HEAD(async_list, async_node);
78SLIST_HEAD(periph_list, cam_periph);
79static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
80
81/*
82 * This is the maximum number of high powered commands (e.g. start unit)
83 * that can be outstanding at a particular time.
84 */
85#ifndef CAM_MAX_HIGHPOWER
86#define CAM_MAX_HIGHPOWER 4
87#endif
88
89/* number of high powered commands that can go through right now */
90static int num_highpower = CAM_MAX_HIGHPOWER;
91
92/*
93 * Structure for queueing a device in a run queue.
94 * There is one run queue for allocating new ccbs,
95 * and another for sending ccbs to the controller.
96 */
97struct cam_ed_qinfo {
98 cam_pinfo pinfo;
99 struct cam_ed *device;
100};
101
102/*
103 * The CAM EDT (Existing Device Table) contains the device information for
104 * all devices for all busses in the system. The table contains a
105 * cam_ed structure for each device on the bus.
106 */
107struct cam_ed {
108 TAILQ_ENTRY(cam_ed) links;
109 struct cam_ed_qinfo alloc_ccb_entry;
110 struct cam_ed_qinfo send_ccb_entry;
111 struct cam_et *target;
112 lun_id_t lun_id;
113 struct camq drvq; /*
114 * Queue of type drivers wanting to do
115 * work on this device.
116 */
117 struct cam_ccbq ccbq; /* Queue of pending ccbs */
118 struct async_list asyncs; /* Async callback info for this B/T/L */
119 struct periph_list periphs; /* All attached devices */
120 u_int generation; /* Generation number */
121 struct cam_periph *owner; /* Peripheral driver's ownership tag */
122 struct xpt_quirk_entry *quirk; /* Oddities about this device */
123 /* Storage for the inquiry data */
124#ifdef CAM_NEW_TRAN_CODE
125 cam_proto protocol;
126 u_int protocol_version;
127 cam_xport transport;
128 u_int transport_version;
129#endif /* CAM_NEW_TRAN_CODE */
130 struct scsi_inquiry_data inq_data;
131 u_int8_t inq_flags; /*
132 * Current settings for inquiry flags.
133 * This allows us to override settings
134 * like disconnection and tagged
135 * queuing for a device.
136 */
137 u_int8_t queue_flags; /* Queue flags from the control page */
138 u_int8_t serial_num_len;
139 u_int8_t *serial_num;
140 u_int32_t qfrozen_cnt;
141 u_int32_t flags;
142#define CAM_DEV_UNCONFIGURED 0x01
143#define CAM_DEV_REL_TIMEOUT_PENDING 0x02
144#define CAM_DEV_REL_ON_COMPLETE 0x04
145#define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
146#define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
147#define CAM_DEV_TAG_AFTER_COUNT 0x20
148#define CAM_DEV_INQUIRY_DATA_VALID 0x40
149 u_int32_t tag_delay_count;
150#define CAM_TAG_DELAY_COUNT 5
151 u_int32_t refcount;
152 struct callout_handle c_handle;
153};
154
155/*
156 * Each target is represented by an ET (Existing Target). These
157 * entries are created when a target is successfully probed with an
158 * identify, and removed when a device fails to respond after a number
159 * of retries, or a bus rescan finds the device missing.
160 */
161struct cam_et {
162 TAILQ_HEAD(, cam_ed) ed_entries;
163 TAILQ_ENTRY(cam_et) links;
164 struct cam_eb *bus;
165 target_id_t target_id;
166 u_int32_t refcount;
167 u_int generation;
168 struct timeval last_reset;
169};
170
171/*
172 * Each bus is represented by an EB (Existing Bus). These entries
173 * are created by calls to xpt_bus_register and deleted by calls to
174 * xpt_bus_deregister.
175 */
176struct cam_eb {
177 TAILQ_HEAD(, cam_et) et_entries;
178 TAILQ_ENTRY(cam_eb) links;
179 path_id_t path_id;
180 struct cam_sim *sim;
181 struct timeval last_reset;
182 u_int32_t flags;
183#define CAM_EB_RUNQ_SCHEDULED 0x01
184 u_int32_t refcount;
185 u_int generation;
186};
187
188struct cam_path {
189 struct cam_periph *periph;
190 struct cam_eb *bus;
191 struct cam_et *target;
192 struct cam_ed *device;
193};
194
195struct xpt_quirk_entry {
196 struct scsi_inquiry_pattern inq_pat;
197 u_int8_t quirks;
198#define CAM_QUIRK_NOLUNS 0x01
199#define CAM_QUIRK_NOSERIAL 0x02
200#define CAM_QUIRK_HILUNS 0x04
201 u_int mintags;
202 u_int maxtags;
203};
204#define CAM_SCSI2_MAXLUN 8
205
206typedef enum {
207 XPT_FLAG_OPEN = 0x01
208} xpt_flags;
209
210struct xpt_softc {
211 xpt_flags flags;
212 u_int32_t generation;
213};
214
215static const char quantum[] = "QUANTUM";
216static const char sony[] = "SONY";
217static const char west_digital[] = "WDIGTL";
218static const char samsung[] = "SAMSUNG";
219static const char seagate[] = "SEAGATE";
220static const char microp[] = "MICROP";
221
222static struct xpt_quirk_entry xpt_quirk_table[] =
223{
224 {
225 /* Reports QUEUE FULL for temporary resource shortages */
226 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
227 /*quirks*/0, /*mintags*/24, /*maxtags*/32
228 },
229 {
230 /* Reports QUEUE FULL for temporary resource shortages */
231 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
232 /*quirks*/0, /*mintags*/24, /*maxtags*/32
233 },
234 {
235 /* Reports QUEUE FULL for temporary resource shortages */
236 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
237 /*quirks*/0, /*mintags*/24, /*maxtags*/32
238 },
239 {
240 /* Broken tagged queuing drive */
241 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
242 /*quirks*/0, /*mintags*/0, /*maxtags*/0
243 },
244 {
245 /* Broken tagged queuing drive */
246 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
247 /*quirks*/0, /*mintags*/0, /*maxtags*/0
248 },
249 {
250 /* Broken tagged queuing drive */
251 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
252 /*quirks*/0, /*mintags*/0, /*maxtags*/0
253 },
254 {
255 /*
256 * Unfortunately, the Quantum Atlas III has the same
257 * problem as the Atlas II drives above.
258 * Reported by: "Johan Granlund" <johan@granlund.nu>
259 *
260 * For future reference, the drive with the problem was:
261 * QUANTUM QM39100TD-SW N1B0
262 *
263 * It's possible that Quantum will fix the problem in later
264 * firmware revisions. If that happens, the quirk entry
265 * will need to be made specific to the firmware revisions
266 * with the problem.
267 *
268 */
269 /* Reports QUEUE FULL for temporary resource shortages */
270 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
271 /*quirks*/0, /*mintags*/24, /*maxtags*/32
272 },
273 {
274 /*
275 * 18 Gig Atlas III, same problem as the 9G version.
276 * Reported by: Andre Albsmeier
277 * <andre.albsmeier@mchp.siemens.de>
278 *
279 * For future reference, the drive with the problem was:
280 * QUANTUM QM318000TD-S N491
281 */
282 /* Reports QUEUE FULL for temporary resource shortages */
283 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
284 /*quirks*/0, /*mintags*/24, /*maxtags*/32
285 },
286 {
287 /*
288 * Broken tagged queuing drive
289 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
290 * and: Martin Renters <martin@tdc.on.ca>
291 */
292 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
293 /*quirks*/0, /*mintags*/0, /*maxtags*/0
294 },
295 /*
296 * The Seagate Medalist Pro drives have very poor write
297 * performance with anything more than 2 tags.
298 *
299 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
300 * Drive: <SEAGATE ST36530N 1444>
301 *
302 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
303 * Drive: <SEAGATE ST34520W 1281>
304 *
305 * No one has actually reported that the 9G version
306 * (ST39140*) of the Medalist Pro has the same problem, but
307 * we're assuming that it does because the 4G and 6.5G
308 * versions of the drive are broken.
309 */
310 {
311 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
312 /*quirks*/0, /*mintags*/2, /*maxtags*/2
313 },
314 {
315 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
316 /*quirks*/0, /*mintags*/2, /*maxtags*/2
317 },
318 {
319 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
320 /*quirks*/0, /*mintags*/2, /*maxtags*/2
321 },
322 {
323 /*
324 * Slow when tagged queueing is enabled. Write performance
325 * steadily drops off with more and more concurrent
326 * transactions. Best sequential write performance with
327 * tagged queueing turned off and write caching turned on.
328 *
329 * PR: kern/10398
330 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
331 * Drive: DCAS-34330 w/ "S65A" firmware.
332 *
333 * The drive with the problem had the "S65A" firmware
334 * revision, and has also been reported (by Stephen J.
335 * Roznowski <sjr@home.net>) for a drive with the "S61A"
336 * firmware revision.
337 *
338 * Although no one has reported problems with the 2 gig
339 * version of the DCAS drive, the assumption is that it
340 * has the same problems as the 4 gig version. Therefore
341 * this quirk entries disables tagged queueing for all
342 * DCAS drives.
343 */
344 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
345 /*quirks*/0, /*mintags*/0, /*maxtags*/0
346 },
347 {
348 /* Broken tagged queuing drive */
349 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
350 /*quirks*/0, /*mintags*/0, /*maxtags*/0
351 },
352 {
353 /* Broken tagged queuing drive */
354 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
355 /*quirks*/0, /*mintags*/0, /*maxtags*/0
356 },
357 {
358 /*
359 * Broken tagged queuing drive.
360 * Submitted by:
361 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
362 * in PR kern/9535
363 */
364 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
365 /*quirks*/0, /*mintags*/0, /*maxtags*/0
366 },
367 {
368 /*
369 * Slow when tagged queueing is enabled. (1.5MB/sec versus
370 * 8MB/sec.)
371 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
372 * Best performance with these drives is achieved with
373 * tagged queueing turned off, and write caching turned on.
374 */
375 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
376 /*quirks*/0, /*mintags*/0, /*maxtags*/0
377 },
378 {
379 /*
380 * Slow when tagged queueing is enabled. (1.5MB/sec versus
381 * 8MB/sec.)
382 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
383 * Best performance with these drives is achieved with
384 * tagged queueing turned off, and write caching turned on.
385 */
386 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
387 /*quirks*/0, /*mintags*/0, /*maxtags*/0
388 },
389 {
390 /*
391 * Doesn't handle queue full condition correctly,
392 * so we need to limit maxtags to what the device
393 * can handle instead of determining this automatically.
394 */
395 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
396 /*quirks*/0, /*mintags*/2, /*maxtags*/32
397 },
398 {
399 /* Really only one LUN */
400 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
401 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
402 },
403 {
404 /* I can't believe we need a quirk for DPT volumes. */
405 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
406 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
407 /*mintags*/0, /*maxtags*/255
408 },
409 {
410 /*
411 * Many Sony CDROM drives don't like multi-LUN probing.
412 */
413 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
414 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
415 },
416 {
417 /*
418 * This drive doesn't like multiple LUN probing.
419 * Submitted by: Parag Patel <parag@cgt.com>
420 */
421 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
422 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
423 },
424 {
425 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
426 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
427 },
428 {
429 /*
430 * The 8200 doesn't like multi-lun probing, and probably
431 * don't like serial number requests either.
432 */
433 {
434 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
435 "EXB-8200*", "*"
436 },
437 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
438 },
439 {
440 /*
441 * Let's try the same as above, but for a drive that says
442 * it's an IPL-6860 but is actually an EXB 8200.
443 */
444 {
445 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
446 "IPL-6860*", "*"
447 },
448 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
449 },
450 {
451 /*
452 * These Hitachi drives don't like multi-lun probing.
453 * The PR submitter has a DK319H, but says that the Linux
454 * kernel has a similar work-around for the DK312 and DK314,
455 * so all DK31* drives are quirked here.
456 * PR: misc/18793
457 * Submitted by: Paul Haddad <paul@pth.com>
458 */
459 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
460 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
461 },
462 {
463 /*
464 * The Hitachi CJ series with J8A8 firmware apparantly has
465 * problems with tagged commands.
466 * PR: 23536
467 * Reported by: amagai@nue.org
468 */
469 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
470 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
471 },
472 {
473 /*
474 * These are the large storage arrays.
475 * Submitted by: William Carrel <william.carrel@infospace.com>
476 */
477 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
478 CAM_QUIRK_HILUNS, 2, 1024
479 },
480 {
481 /*
482 * This old revision of the TDC3600 is also SCSI-1, and
483 * hangs upon serial number probing.
484 */
485 {
486 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
487 " TDC 3600", "U07:"
488 },
489 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
490 },
491 {
492 /*
493 * Maxtor Personal Storage 3000XT (Firewire)
494 * hangs upon serial number probing.
495 */
496 {
497 T_DIRECT, SIP_MEDIA_FIXED, "Maxtor",
498 "1394 storage", "*"
499 },
500 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
501 },
502 {
503 /*
504 * Would repond to all LUNs if asked for.
505 */
506 {
507 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
508 "CP150", "*"
509 },
510 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
511 },
512 {
513 /*
514 * Would repond to all LUNs if asked for.
515 */
516 {
517 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
518 "96X2*", "*"
519 },
520 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
521 },
522 {
523 /* Submitted by: Matthew Dodd <winter@jurai.net> */
524 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
525 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
526 },
527 {
528 /* Submitted by: Matthew Dodd <winter@jurai.net> */
529 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
530 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
531 },
532 {
533 /* TeraSolutions special settings for TRC-22 RAID */
534 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
535 /*quirks*/0, /*mintags*/55, /*maxtags*/255
536 },
537 {
538 /* Veritas Storage Appliance */
539 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
540 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
541 },
542 {
543 /*
544 * Would respond to all LUNs. Device type and removable
545 * flag are jumper-selectable.
546 */
547 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
548 "Tahiti 1", "*"
549 },
550 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
551 },
552 {
553 /* Default tagged queuing parameters for all devices */
554 {
555 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
556 /*vendor*/"*", /*product*/"*", /*revision*/"*"
557 },
558 /*quirks*/0, /*mintags*/2, /*maxtags*/255
559 },
560};
561
562static const int xpt_quirk_table_size =
563 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
564
565typedef enum {
566 DM_RET_COPY = 0x01,
567 DM_RET_FLAG_MASK = 0x0f,
568 DM_RET_NONE = 0x00,
569 DM_RET_STOP = 0x10,
570 DM_RET_DESCEND = 0x20,
571 DM_RET_ERROR = 0x30,
572 DM_RET_ACTION_MASK = 0xf0
573} dev_match_ret;
574
575typedef enum {
576 XPT_DEPTH_BUS,
577 XPT_DEPTH_TARGET,
578 XPT_DEPTH_DEVICE,
579 XPT_DEPTH_PERIPH
580} xpt_traverse_depth;
581
582struct xpt_traverse_config {
583 xpt_traverse_depth depth;
584 void *tr_func;
585 void *tr_arg;
586};
587
588typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
589typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
590typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
591typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
592typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
593
594/* Transport layer configuration information */
595static struct xpt_softc xsoftc;
596
597/* Queues for our software interrupt handler */
598typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
599static cam_isrq_t cam_bioq;
600static cam_isrq_t cam_netq;
601
602/* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
603static SLIST_HEAD(,ccb_hdr) ccb_freeq;
604static u_int xpt_max_ccbs; /*
605 * Maximum size of ccb pool. Modified as
606 * devices are added/removed or have their
607 * opening counts changed.
608 */
609static u_int xpt_ccb_count; /* Current count of allocated ccbs */
610
611struct cam_periph *xpt_periph;
612
613static periph_init_t xpt_periph_init;
614
615static periph_init_t probe_periph_init;
616
617static struct periph_driver xpt_driver =
618{
619 xpt_periph_init, "xpt",
620 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
621};
622
623static struct periph_driver probe_driver =
624{
625 probe_periph_init, "probe",
626 TAILQ_HEAD_INITIALIZER(probe_driver.units)
627};
628
629PERIPHDRIVER_DECLARE(xpt, xpt_driver);
630PERIPHDRIVER_DECLARE(probe, probe_driver);
631
632#define XPT_CDEV_MAJOR 104
633
634static d_open_t xptopen;
635static d_close_t xptclose;
636static d_ioctl_t xptioctl;
637
638static struct cdevsw xpt_cdevsw = {
639 /* open */ xptopen,
640 /* close */ xptclose,
641 /* read */ noread,
642 /* write */ nowrite,
643 /* ioctl */ xptioctl,
644 /* poll */ nopoll,
645 /* mmap */ nommap,
646 /* strategy */ nostrategy,
647 /* name */ "xpt",
648 /* maj */ XPT_CDEV_MAJOR,
649 /* dump */ nodump,
650 /* psize */ nopsize,
651 /* flags */ 0,
639 .d_open = xptopen,
640 .d_close = xptclose,
641 .d_ioctl = xptioctl,
642 .d_name = "xpt",
643 .d_maj = XPT_CDEV_MAJOR,
652};
653
654static struct intr_config_hook *xpt_config_hook;
655
656/* Registered busses */
657static TAILQ_HEAD(,cam_eb) xpt_busses;
658static u_int bus_generation;
659
660/* Storage for debugging datastructures */
661#ifdef CAMDEBUG
662struct cam_path *cam_dpath;
663u_int32_t cam_dflags;
664u_int32_t cam_debug_delay;
665#endif
666
667/* Pointers to software interrupt handlers */
668static void *camnet_ih;
669static void *cambio_ih;
670
671#if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
672#error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
673#endif
674
675/*
676 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
677 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
678 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
679 */
680#if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
681 || defined(CAM_DEBUG_LUN)
682#ifdef CAMDEBUG
683#if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
684 || !defined(CAM_DEBUG_LUN)
685#error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
686 and CAM_DEBUG_LUN"
687#endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
688#else /* !CAMDEBUG */
689#error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
690#endif /* CAMDEBUG */
691#endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
692
693/* Our boot-time initialization hook */
694static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
695
696static moduledata_t cam_moduledata = {
697 "cam",
698 cam_module_event_handler,
699 NULL
700};
701
702static void xpt_init(void *);
703
704DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
705MODULE_VERSION(cam, 1);
706
707
708static cam_status xpt_compile_path(struct cam_path *new_path,
709 struct cam_periph *perph,
710 path_id_t path_id,
711 target_id_t target_id,
712 lun_id_t lun_id);
713
714static void xpt_release_path(struct cam_path *path);
715
716static void xpt_async_bcast(struct async_list *async_head,
717 u_int32_t async_code,
718 struct cam_path *path,
719 void *async_arg);
720static void xpt_dev_async(u_int32_t async_code,
721 struct cam_eb *bus,
722 struct cam_et *target,
723 struct cam_ed *device,
724 void *async_arg);
725static path_id_t xptnextfreepathid(void);
726static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
727static union ccb *xpt_get_ccb(struct cam_ed *device);
728static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
729 u_int32_t new_priority);
730static void xpt_run_dev_allocq(struct cam_eb *bus);
731static void xpt_run_dev_sendq(struct cam_eb *bus);
732static timeout_t xpt_release_devq_timeout;
733static timeout_t xpt_release_simq_timeout;
734static void xpt_release_bus(struct cam_eb *bus);
735static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
736 int run_queue);
737static struct cam_et*
738 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
739static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
740static struct cam_ed*
741 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
742 lun_id_t lun_id);
743static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
744 struct cam_ed *device);
745static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
746static struct cam_eb*
747 xpt_find_bus(path_id_t path_id);
748static struct cam_et*
749 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
750static struct cam_ed*
751 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
752static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
753static void xpt_scan_lun(struct cam_periph *periph,
754 struct cam_path *path, cam_flags flags,
755 union ccb *ccb);
756static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
757static xpt_busfunc_t xptconfigbuscountfunc;
758static xpt_busfunc_t xptconfigfunc;
759static void xpt_config(void *arg);
760static xpt_devicefunc_t xptpassannouncefunc;
761static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
762static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
763static void xptpoll(struct cam_sim *sim);
764static void camisr(void *);
765#if 0
766static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
767static void xptasync(struct cam_periph *periph,
768 u_int32_t code, cam_path *path);
769#endif
770static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
771 u_int num_patterns, struct cam_eb *bus);
772static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
773 u_int num_patterns,
774 struct cam_ed *device);
775static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
776 u_int num_patterns,
777 struct cam_periph *periph);
778static xpt_busfunc_t xptedtbusfunc;
779static xpt_targetfunc_t xptedttargetfunc;
780static xpt_devicefunc_t xptedtdevicefunc;
781static xpt_periphfunc_t xptedtperiphfunc;
782static xpt_pdrvfunc_t xptplistpdrvfunc;
783static xpt_periphfunc_t xptplistperiphfunc;
784static int xptedtmatch(struct ccb_dev_match *cdm);
785static int xptperiphlistmatch(struct ccb_dev_match *cdm);
786static int xptbustraverse(struct cam_eb *start_bus,
787 xpt_busfunc_t *tr_func, void *arg);
788static int xpttargettraverse(struct cam_eb *bus,
789 struct cam_et *start_target,
790 xpt_targetfunc_t *tr_func, void *arg);
791static int xptdevicetraverse(struct cam_et *target,
792 struct cam_ed *start_device,
793 xpt_devicefunc_t *tr_func, void *arg);
794static int xptperiphtraverse(struct cam_ed *device,
795 struct cam_periph *start_periph,
796 xpt_periphfunc_t *tr_func, void *arg);
797static int xptpdrvtraverse(struct periph_driver **start_pdrv,
798 xpt_pdrvfunc_t *tr_func, void *arg);
799static int xptpdperiphtraverse(struct periph_driver **pdrv,
800 struct cam_periph *start_periph,
801 xpt_periphfunc_t *tr_func,
802 void *arg);
803static xpt_busfunc_t xptdefbusfunc;
804static xpt_targetfunc_t xptdeftargetfunc;
805static xpt_devicefunc_t xptdefdevicefunc;
806static xpt_periphfunc_t xptdefperiphfunc;
807static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
808#ifdef notusedyet
809static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
810 void *arg);
811#endif
812static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
813 void *arg);
814#ifdef notusedyet
815static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
816 void *arg);
817#endif
818static xpt_devicefunc_t xptsetasyncfunc;
819static xpt_busfunc_t xptsetasyncbusfunc;
820static cam_status xptregister(struct cam_periph *periph,
821 void *arg);
822static cam_status proberegister(struct cam_periph *periph,
823 void *arg);
824static void probeschedule(struct cam_periph *probe_periph);
825static void probestart(struct cam_periph *periph, union ccb *start_ccb);
826static void proberequestdefaultnegotiation(struct cam_periph *periph);
827static void probedone(struct cam_periph *periph, union ccb *done_ccb);
828static void probecleanup(struct cam_periph *periph);
829static void xpt_find_quirk(struct cam_ed *device);
830#ifdef CAM_NEW_TRAN_CODE
831static void xpt_devise_transport(struct cam_path *path);
832#endif /* CAM_NEW_TRAN_CODE */
833static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
834 struct cam_ed *device,
835 int async_update);
836static void xpt_toggle_tags(struct cam_path *path);
837static void xpt_start_tags(struct cam_path *path);
838static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
839 struct cam_ed *dev);
840static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
841 struct cam_ed *dev);
842static __inline int periph_is_queued(struct cam_periph *periph);
843static __inline int device_is_alloc_queued(struct cam_ed *device);
844static __inline int device_is_send_queued(struct cam_ed *device);
845static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
846
847static __inline int
848xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
849{
850 int retval;
851
852 if (dev->ccbq.devq_openings > 0) {
853 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
854 cam_ccbq_resize(&dev->ccbq,
855 dev->ccbq.dev_openings
856 + dev->ccbq.dev_active);
857 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
858 }
859 /*
860 * The priority of a device waiting for CCB resources
861 * is that of the the highest priority peripheral driver
862 * enqueued.
863 */
864 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
865 &dev->alloc_ccb_entry.pinfo,
866 CAMQ_GET_HEAD(&dev->drvq)->priority);
867 } else {
868 retval = 0;
869 }
870
871 return (retval);
872}
873
874static __inline int
875xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
876{
877 int retval;
878
879 if (dev->ccbq.dev_openings > 0) {
880 /*
881 * The priority of a device waiting for controller
882 * resources is that of the the highest priority CCB
883 * enqueued.
884 */
885 retval =
886 xpt_schedule_dev(&bus->sim->devq->send_queue,
887 &dev->send_ccb_entry.pinfo,
888 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
889 } else {
890 retval = 0;
891 }
892 return (retval);
893}
894
895static __inline int
896periph_is_queued(struct cam_periph *periph)
897{
898 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
899}
900
901static __inline int
902device_is_alloc_queued(struct cam_ed *device)
903{
904 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
905}
906
907static __inline int
908device_is_send_queued(struct cam_ed *device)
909{
910 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
911}
912
913static __inline int
914dev_allocq_is_runnable(struct cam_devq *devq)
915{
916 /*
917 * Have work to do.
918 * Have space to do more work.
919 * Allowed to do work.
920 */
921 return ((devq->alloc_queue.qfrozen_cnt == 0)
922 && (devq->alloc_queue.entries > 0)
923 && (devq->alloc_openings > 0));
924}
925
926static void
927xpt_periph_init()
928{
929 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
930}
931
932static void
933probe_periph_init()
934{
935}
936
937
938static void
939xptdone(struct cam_periph *periph, union ccb *done_ccb)
940{
941 /* Caller will release the CCB */
942 wakeup(&done_ccb->ccb_h.cbfcnp);
943}
944
945static int
946xptopen(dev_t dev, int flags, int fmt, struct thread *td)
947{
948 int unit;
949
950 unit = minor(dev) & 0xff;
951
952 /*
953 * Only allow read-write access.
954 */
955 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
956 return(EPERM);
957
958 /*
959 * We don't allow nonblocking access.
960 */
961 if ((flags & O_NONBLOCK) != 0) {
962 printf("xpt%d: can't do nonblocking access\n", unit);
963 return(ENODEV);
964 }
965
966 /*
967 * We only have one transport layer right now. If someone accesses
968 * us via something other than minor number 1, point out their
969 * mistake.
970 */
971 if (unit != 0) {
972 printf("xptopen: got invalid xpt unit %d\n", unit);
973 return(ENXIO);
974 }
975
976 /* Mark ourselves open */
977 xsoftc.flags |= XPT_FLAG_OPEN;
978
979 return(0);
980}
981
982static int
983xptclose(dev_t dev, int flag, int fmt, struct thread *td)
984{
985 int unit;
986
987 unit = minor(dev) & 0xff;
988
989 /*
990 * We only have one transport layer right now. If someone accesses
991 * us via something other than minor number 1, point out their
992 * mistake.
993 */
994 if (unit != 0) {
995 printf("xptclose: got invalid xpt unit %d\n", unit);
996 return(ENXIO);
997 }
998
999 /* Mark ourselves closed */
1000 xsoftc.flags &= ~XPT_FLAG_OPEN;
1001
1002 return(0);
1003}
1004
1005static int
1006xptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1007{
1008 int unit, error;
1009
1010 error = 0;
1011 unit = minor(dev) & 0xff;
1012
1013 /*
1014 * We only have one transport layer right now. If someone accesses
1015 * us via something other than minor number 1, point out their
1016 * mistake.
1017 */
1018 if (unit != 0) {
1019 printf("xptioctl: got invalid xpt unit %d\n", unit);
1020 return(ENXIO);
1021 }
1022
1023 switch(cmd) {
1024 /*
1025 * For the transport layer CAMIOCOMMAND ioctl, we really only want
1026 * to accept CCB types that don't quite make sense to send through a
1027 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
1028 * in the CAM spec.
1029 */
1030 case CAMIOCOMMAND: {
1031 union ccb *ccb;
1032 union ccb *inccb;
1033
1034 inccb = (union ccb *)addr;
1035
1036 switch(inccb->ccb_h.func_code) {
1037 case XPT_SCAN_BUS:
1038 case XPT_RESET_BUS:
1039 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
1040 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1041 error = EINVAL;
1042 break;
1043 }
1044 /* FALLTHROUGH */
1045 case XPT_PATH_INQ:
1046 case XPT_ENG_INQ:
1047 case XPT_SCAN_LUN:
1048
1049 ccb = xpt_alloc_ccb();
1050
1051 /*
1052 * Create a path using the bus, target, and lun the
1053 * user passed in.
1054 */
1055 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1056 inccb->ccb_h.path_id,
1057 inccb->ccb_h.target_id,
1058 inccb->ccb_h.target_lun) !=
1059 CAM_REQ_CMP){
1060 error = EINVAL;
1061 xpt_free_ccb(ccb);
1062 break;
1063 }
1064 /* Ensure all of our fields are correct */
1065 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1066 inccb->ccb_h.pinfo.priority);
1067 xpt_merge_ccb(ccb, inccb);
1068 ccb->ccb_h.cbfcnp = xptdone;
1069 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1070 bcopy(ccb, inccb, sizeof(union ccb));
1071 xpt_free_path(ccb->ccb_h.path);
1072 xpt_free_ccb(ccb);
1073 break;
1074
1075 case XPT_DEBUG: {
1076 union ccb ccb;
1077
1078 /*
1079 * This is an immediate CCB, so it's okay to
1080 * allocate it on the stack.
1081 */
1082
1083 /*
1084 * Create a path using the bus, target, and lun the
1085 * user passed in.
1086 */
1087 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1088 inccb->ccb_h.path_id,
1089 inccb->ccb_h.target_id,
1090 inccb->ccb_h.target_lun) !=
1091 CAM_REQ_CMP){
1092 error = EINVAL;
1093 break;
1094 }
1095 /* Ensure all of our fields are correct */
1096 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1097 inccb->ccb_h.pinfo.priority);
1098 xpt_merge_ccb(&ccb, inccb);
1099 ccb.ccb_h.cbfcnp = xptdone;
1100 xpt_action(&ccb);
1101 bcopy(&ccb, inccb, sizeof(union ccb));
1102 xpt_free_path(ccb.ccb_h.path);
1103 break;
1104
1105 }
1106 case XPT_DEV_MATCH: {
1107 struct cam_periph_map_info mapinfo;
1108 struct cam_path *old_path;
1109
1110 /*
1111 * We can't deal with physical addresses for this
1112 * type of transaction.
1113 */
1114 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1115 error = EINVAL;
1116 break;
1117 }
1118
1119 /*
1120 * Save this in case the caller had it set to
1121 * something in particular.
1122 */
1123 old_path = inccb->ccb_h.path;
1124
1125 /*
1126 * We really don't need a path for the matching
1127 * code. The path is needed because of the
1128 * debugging statements in xpt_action(). They
1129 * assume that the CCB has a valid path.
1130 */
1131 inccb->ccb_h.path = xpt_periph->path;
1132
1133 bzero(&mapinfo, sizeof(mapinfo));
1134
1135 /*
1136 * Map the pattern and match buffers into kernel
1137 * virtual address space.
1138 */
1139 error = cam_periph_mapmem(inccb, &mapinfo);
1140
1141 if (error) {
1142 inccb->ccb_h.path = old_path;
1143 break;
1144 }
1145
1146 /*
1147 * This is an immediate CCB, we can send it on directly.
1148 */
1149 xpt_action(inccb);
1150
1151 /*
1152 * Map the buffers back into user space.
1153 */
1154 cam_periph_unmapmem(inccb, &mapinfo);
1155
1156 inccb->ccb_h.path = old_path;
1157
1158 error = 0;
1159 break;
1160 }
1161 default:
1162 error = ENOTSUP;
1163 break;
1164 }
1165 break;
1166 }
1167 /*
1168 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1169 * with the periphal driver name and unit name filled in. The other
1170 * fields don't really matter as input. The passthrough driver name
1171 * ("pass"), and unit number are passed back in the ccb. The current
1172 * device generation number, and the index into the device peripheral
1173 * driver list, and the status are also passed back. Note that
1174 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1175 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1176 * (or rather should be) impossible for the device peripheral driver
1177 * list to change since we look at the whole thing in one pass, and
1178 * we do it with splcam protection.
1179 *
1180 */
1181 case CAMGETPASSTHRU: {
1182 union ccb *ccb;
1183 struct cam_periph *periph;
1184 struct periph_driver **p_drv;
1185 char *name;
1186 u_int unit;
1187 u_int cur_generation;
1188 int base_periph_found;
1189 int splbreaknum;
1190 int s;
1191
1192 ccb = (union ccb *)addr;
1193 unit = ccb->cgdl.unit_number;
1194 name = ccb->cgdl.periph_name;
1195 /*
1196 * Every 100 devices, we want to drop our spl protection to
1197 * give the software interrupt handler a chance to run.
1198 * Most systems won't run into this check, but this should
1199 * avoid starvation in the software interrupt handler in
1200 * large systems.
1201 */
1202 splbreaknum = 100;
1203
1204 ccb = (union ccb *)addr;
1205
1206 base_periph_found = 0;
1207
1208 /*
1209 * Sanity check -- make sure we don't get a null peripheral
1210 * driver name.
1211 */
1212 if (*ccb->cgdl.periph_name == '\0') {
1213 error = EINVAL;
1214 break;
1215 }
1216
1217 /* Keep the list from changing while we traverse it */
1218 s = splcam();
1219ptstartover:
1220 cur_generation = xsoftc.generation;
1221
1222 /* first find our driver in the list of drivers */
1223 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
1224 if (strcmp((*p_drv)->driver_name, name) == 0)
1225 break;
1226
1227 if (*p_drv == NULL) {
1228 splx(s);
1229 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1230 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1231 *ccb->cgdl.periph_name = '\0';
1232 ccb->cgdl.unit_number = 0;
1233 error = ENOENT;
1234 break;
1235 }
1236
1237 /*
1238 * Run through every peripheral instance of this driver
1239 * and check to see whether it matches the unit passed
1240 * in by the user. If it does, get out of the loops and
1241 * find the passthrough driver associated with that
1242 * peripheral driver.
1243 */
1244 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1245 periph = TAILQ_NEXT(periph, unit_links)) {
1246
1247 if (periph->unit_number == unit) {
1248 break;
1249 } else if (--splbreaknum == 0) {
1250 splx(s);
1251 s = splcam();
1252 splbreaknum = 100;
1253 if (cur_generation != xsoftc.generation)
1254 goto ptstartover;
1255 }
1256 }
1257 /*
1258 * If we found the peripheral driver that the user passed
1259 * in, go through all of the peripheral drivers for that
1260 * particular device and look for a passthrough driver.
1261 */
1262 if (periph != NULL) {
1263 struct cam_ed *device;
1264 int i;
1265
1266 base_periph_found = 1;
1267 device = periph->path->device;
1268 for (i = 0, periph = SLIST_FIRST(&device->periphs);
1269 periph != NULL;
1270 periph = SLIST_NEXT(periph, periph_links), i++) {
1271 /*
1272 * Check to see whether we have a
1273 * passthrough device or not.
1274 */
1275 if (strcmp(periph->periph_name, "pass") == 0) {
1276 /*
1277 * Fill in the getdevlist fields.
1278 */
1279 strcpy(ccb->cgdl.periph_name,
1280 periph->periph_name);
1281 ccb->cgdl.unit_number =
1282 periph->unit_number;
1283 if (SLIST_NEXT(periph, periph_links))
1284 ccb->cgdl.status =
1285 CAM_GDEVLIST_MORE_DEVS;
1286 else
1287 ccb->cgdl.status =
1288 CAM_GDEVLIST_LAST_DEVICE;
1289 ccb->cgdl.generation =
1290 device->generation;
1291 ccb->cgdl.index = i;
1292 /*
1293 * Fill in some CCB header fields
1294 * that the user may want.
1295 */
1296 ccb->ccb_h.path_id =
1297 periph->path->bus->path_id;
1298 ccb->ccb_h.target_id =
1299 periph->path->target->target_id;
1300 ccb->ccb_h.target_lun =
1301 periph->path->device->lun_id;
1302 ccb->ccb_h.status = CAM_REQ_CMP;
1303 break;
1304 }
1305 }
1306 }
1307
1308 /*
1309 * If the periph is null here, one of two things has
1310 * happened. The first possibility is that we couldn't
1311 * find the unit number of the particular peripheral driver
1312 * that the user is asking about. e.g. the user asks for
1313 * the passthrough driver for "da11". We find the list of
1314 * "da" peripherals all right, but there is no unit 11.
1315 * The other possibility is that we went through the list
1316 * of peripheral drivers attached to the device structure,
1317 * but didn't find one with the name "pass". Either way,
1318 * we return ENOENT, since we couldn't find something.
1319 */
1320 if (periph == NULL) {
1321 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1322 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1323 *ccb->cgdl.periph_name = '\0';
1324 ccb->cgdl.unit_number = 0;
1325 error = ENOENT;
1326 /*
1327 * It is unfortunate that this is even necessary,
1328 * but there are many, many clueless users out there.
1329 * If this is true, the user is looking for the
1330 * passthrough driver, but doesn't have one in his
1331 * kernel.
1332 */
1333 if (base_periph_found == 1) {
1334 printf("xptioctl: pass driver is not in the "
1335 "kernel\n");
1336 printf("xptioctl: put \"device pass0\" in "
1337 "your kernel config file\n");
1338 }
1339 }
1340 splx(s);
1341 break;
1342 }
1343 default:
1344 error = ENOTTY;
1345 break;
1346 }
1347
1348 return(error);
1349}
1350
1351static int
1352cam_module_event_handler(module_t mod, int what, void *arg)
1353{
1354 if (what == MOD_LOAD) {
1355 xpt_init(NULL);
1356 } else if (what == MOD_UNLOAD) {
1357 return EBUSY;
1358 }
1359
1360 return 0;
1361}
1362
1363/* Functions accessed by the peripheral drivers */
1364static void
1365xpt_init(dummy)
1366 void *dummy;
1367{
1368 struct cam_sim *xpt_sim;
1369 struct cam_path *path;
1370 struct cam_devq *devq;
1371 cam_status status;
1372
1373 TAILQ_INIT(&xpt_busses);
1374 TAILQ_INIT(&cam_bioq);
1375 TAILQ_INIT(&cam_netq);
1376 SLIST_INIT(&ccb_freeq);
1377 STAILQ_INIT(&highpowerq);
1378
1379 /*
1380 * The xpt layer is, itself, the equivelent of a SIM.
1381 * Allow 16 ccbs in the ccb pool for it. This should
1382 * give decent parallelism when we probe busses and
1383 * perform other XPT functions.
1384 */
1385 devq = cam_simq_alloc(16);
1386 xpt_sim = cam_sim_alloc(xptaction,
1387 xptpoll,
1388 "xpt",
1389 /*softc*/NULL,
1390 /*unit*/0,
1391 /*max_dev_transactions*/0,
1392 /*max_tagged_dev_transactions*/0,
1393 devq);
1394 xpt_max_ccbs = 16;
1395
1396 xpt_bus_register(xpt_sim, /*bus #*/0);
1397
1398 /*
1399 * Looking at the XPT from the SIM layer, the XPT is
1400 * the equivelent of a peripheral driver. Allocate
1401 * a peripheral driver entry for us.
1402 */
1403 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1404 CAM_TARGET_WILDCARD,
1405 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1406 printf("xpt_init: xpt_create_path failed with status %#x,"
1407 " failing attach\n", status);
1408 return;
1409 }
1410
1411 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1412 path, NULL, 0, NULL);
1413 xpt_free_path(path);
1414
1415 xpt_sim->softc = xpt_periph;
1416
1417 /*
1418 * Register a callback for when interrupts are enabled.
1419 */
1420 xpt_config_hook =
1421 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1422 M_TEMP, M_NOWAIT | M_ZERO);
1423 if (xpt_config_hook == NULL) {
1424 printf("xpt_init: Cannot malloc config hook "
1425 "- failing attach\n");
1426 return;
1427 }
1428
1429 xpt_config_hook->ich_func = xpt_config;
1430 if (config_intrhook_establish(xpt_config_hook) != 0) {
1431 free (xpt_config_hook, M_TEMP);
1432 printf("xpt_init: config_intrhook_establish failed "
1433 "- failing attach\n");
1434 }
1435
1436 /* Install our software interrupt handlers */
1437 swi_add(NULL, "camnet", camisr, &cam_netq, SWI_CAMNET, 0, &camnet_ih);
1438 swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, 0, &cambio_ih);
1439}
1440
1441static cam_status
1442xptregister(struct cam_periph *periph, void *arg)
1443{
1444 if (periph == NULL) {
1445 printf("xptregister: periph was NULL!!\n");
1446 return(CAM_REQ_CMP_ERR);
1447 }
1448
1449 periph->softc = NULL;
1450
1451 xpt_periph = periph;
1452
1453 return(CAM_REQ_CMP);
1454}
1455
1456int32_t
1457xpt_add_periph(struct cam_periph *periph)
1458{
1459 struct cam_ed *device;
1460 int32_t status;
1461 struct periph_list *periph_head;
1462
1463 device = periph->path->device;
1464
1465 periph_head = &device->periphs;
1466
1467 status = CAM_REQ_CMP;
1468
1469 if (device != NULL) {
1470 int s;
1471
1472 /*
1473 * Make room for this peripheral
1474 * so it will fit in the queue
1475 * when it's scheduled to run
1476 */
1477 s = splsoftcam();
1478 status = camq_resize(&device->drvq,
1479 device->drvq.array_size + 1);
1480
1481 device->generation++;
1482
1483 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1484
1485 splx(s);
1486 }
1487
1488 xsoftc.generation++;
1489
1490 return (status);
1491}
1492
1493void
1494xpt_remove_periph(struct cam_periph *periph)
1495{
1496 struct cam_ed *device;
1497
1498 device = periph->path->device;
1499
1500 if (device != NULL) {
1501 int s;
1502 struct periph_list *periph_head;
1503
1504 periph_head = &device->periphs;
1505
1506 /* Release the slot for this peripheral */
1507 s = splsoftcam();
1508 camq_resize(&device->drvq, device->drvq.array_size - 1);
1509
1510 device->generation++;
1511
1512 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1513
1514 splx(s);
1515 }
1516
1517 xsoftc.generation++;
1518
1519}
1520
1521#ifdef CAM_NEW_TRAN_CODE
1522
1523void
1524xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1525{
1526 struct ccb_pathinq cpi;
1527 struct ccb_trans_settings cts;
1528 struct cam_path *path;
1529 u_int speed;
1530 u_int freq;
1531 u_int mb;
1532 int s;
1533
1534 path = periph->path;
1535 /*
1536 * To ensure that this is printed in one piece,
1537 * mask out CAM interrupts.
1538 */
1539 s = splsoftcam();
1540 printf("%s%d at %s%d bus %d target %d lun %d\n",
1541 periph->periph_name, periph->unit_number,
1542 path->bus->sim->sim_name,
1543 path->bus->sim->unit_number,
1544 path->bus->sim->bus_id,
1545 path->target->target_id,
1546 path->device->lun_id);
1547 printf("%s%d: ", periph->periph_name, periph->unit_number);
1548 scsi_print_inquiry(&path->device->inq_data);
1549 if (bootverbose && path->device->serial_num_len > 0) {
1550 /* Don't wrap the screen - print only the first 60 chars */
1551 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1552 periph->unit_number, path->device->serial_num);
1553 }
1554 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1555 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1556 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1557 xpt_action((union ccb*)&cts);
1558
1559 /* Ask the SIM for its base transfer speed */
1560 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1561 cpi.ccb_h.func_code = XPT_PATH_INQ;
1562 xpt_action((union ccb *)&cpi);
1563
1564 speed = cpi.base_transfer_speed;
1565 freq = 0;
1566 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1567 struct ccb_trans_settings_spi *spi;
1568
1569 spi = &cts.xport_specific.spi;
1570 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1571 && spi->sync_offset != 0) {
1572 freq = scsi_calc_syncsrate(spi->sync_period);
1573 speed = freq;
1574 }
1575
1576 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1577 speed *= (0x01 << spi->bus_width);
1578 }
1579
1580 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1581 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1582 if (fc->valid & CTS_FC_VALID_SPEED) {
1583 speed = fc->bitrate;
1584 }
1585 }
1586
1587 mb = speed / 1000;
1588 if (mb > 0)
1589 printf("%s%d: %d.%03dMB/s transfers",
1590 periph->periph_name, periph->unit_number,
1591 mb, speed % 1000);
1592 else
1593 printf("%s%d: %dKB/s transfers", periph->periph_name,
1594 periph->unit_number, speed);
1595 /* Report additional information about SPI connections */
1596 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1597 struct ccb_trans_settings_spi *spi;
1598
1599 spi = &cts.xport_specific.spi;
1600 if (freq != 0) {
1601 printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1602 freq % 1000,
1603 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1604 ? " DT" : "",
1605 spi->sync_offset);
1606 }
1607 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1608 && spi->bus_width > 0) {
1609 if (freq != 0) {
1610 printf(", ");
1611 } else {
1612 printf(" (");
1613 }
1614 printf("%dbit)", 8 * (0x01 << spi->bus_width));
1615 } else if (freq != 0) {
1616 printf(")");
1617 }
1618 }
1619 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1620 struct ccb_trans_settings_fc *fc;
1621
1622 fc = &cts.xport_specific.fc;
1623 if (fc->valid & CTS_FC_VALID_WWNN)
1624 printf(" WWNN 0x%llx", (long long) fc->wwnn);
1625 if (fc->valid & CTS_FC_VALID_WWPN)
1626 printf(" WWPN 0x%llx", (long long) fc->wwpn);
1627 if (fc->valid & CTS_FC_VALID_PORT)
1628 printf(" PortID 0x%x", fc->port);
1629 }
1630
1631 if (path->device->inq_flags & SID_CmdQue
1632 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1633 printf("\n%s%d: Tagged Queueing Enabled",
1634 periph->periph_name, periph->unit_number);
1635 }
1636 printf("\n");
1637
1638 /*
1639 * We only want to print the caller's announce string if they've
1640 * passed one in..
1641 */
1642 if (announce_string != NULL)
1643 printf("%s%d: %s\n", periph->periph_name,
1644 periph->unit_number, announce_string);
1645 splx(s);
1646}
1647#else /* CAM_NEW_TRAN_CODE */
1648void
1649xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1650{
1651 int s;
1652 u_int mb;
1653 struct cam_path *path;
1654 struct ccb_trans_settings cts;
1655
1656 path = periph->path;
1657 /*
1658 * To ensure that this is printed in one piece,
1659 * mask out CAM interrupts.
1660 */
1661 s = splsoftcam();
1662 printf("%s%d at %s%d bus %d target %d lun %d\n",
1663 periph->periph_name, periph->unit_number,
1664 path->bus->sim->sim_name,
1665 path->bus->sim->unit_number,
1666 path->bus->sim->bus_id,
1667 path->target->target_id,
1668 path->device->lun_id);
1669 printf("%s%d: ", periph->periph_name, periph->unit_number);
1670 scsi_print_inquiry(&path->device->inq_data);
1671 if ((bootverbose)
1672 && (path->device->serial_num_len > 0)) {
1673 /* Don't wrap the screen - print only the first 60 chars */
1674 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1675 periph->unit_number, path->device->serial_num);
1676 }
1677 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1678 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1679 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1680 xpt_action((union ccb*)&cts);
1681 if (cts.ccb_h.status == CAM_REQ_CMP) {
1682 u_int speed;
1683 u_int freq;
1684
1685 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1686 && cts.sync_offset != 0) {
1687 freq = scsi_calc_syncsrate(cts.sync_period);
1688 speed = freq;
1689 } else {
1690 struct ccb_pathinq cpi;
1691
1692 /* Ask the SIM for its base transfer speed */
1693 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1694 cpi.ccb_h.func_code = XPT_PATH_INQ;
1695 xpt_action((union ccb *)&cpi);
1696
1697 speed = cpi.base_transfer_speed;
1698 freq = 0;
1699 }
1700 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1701 speed *= (0x01 << cts.bus_width);
1702 mb = speed / 1000;
1703 if (mb > 0)
1704 printf("%s%d: %d.%03dMB/s transfers",
1705 periph->periph_name, periph->unit_number,
1706 mb, speed % 1000);
1707 else
1708 printf("%s%d: %dKB/s transfers", periph->periph_name,
1709 periph->unit_number, speed);
1710 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1711 && cts.sync_offset != 0) {
1712 printf(" (%d.%03dMHz, offset %d", freq / 1000,
1713 freq % 1000, cts.sync_offset);
1714 }
1715 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1716 && cts.bus_width > 0) {
1717 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1718 && cts.sync_offset != 0) {
1719 printf(", ");
1720 } else {
1721 printf(" (");
1722 }
1723 printf("%dbit)", 8 * (0x01 << cts.bus_width));
1724 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1725 && cts.sync_offset != 0) {
1726 printf(")");
1727 }
1728
1729 if (path->device->inq_flags & SID_CmdQue
1730 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1731 printf(", Tagged Queueing Enabled");
1732 }
1733
1734 printf("\n");
1735 } else if (path->device->inq_flags & SID_CmdQue
1736 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1737 printf("%s%d: Tagged Queueing Enabled\n",
1738 periph->periph_name, periph->unit_number);
1739 }
1740
1741 /*
1742 * We only want to print the caller's announce string if they've
1743 * passed one in..
1744 */
1745 if (announce_string != NULL)
1746 printf("%s%d: %s\n", periph->periph_name,
1747 periph->unit_number, announce_string);
1748 splx(s);
1749}
1750
1751#endif /* CAM_NEW_TRAN_CODE */
1752
1753static dev_match_ret
1754xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1755 struct cam_eb *bus)
1756{
1757 dev_match_ret retval;
1758 int i;
1759
1760 retval = DM_RET_NONE;
1761
1762 /*
1763 * If we aren't given something to match against, that's an error.
1764 */
1765 if (bus == NULL)
1766 return(DM_RET_ERROR);
1767
1768 /*
1769 * If there are no match entries, then this bus matches no
1770 * matter what.
1771 */
1772 if ((patterns == NULL) || (num_patterns == 0))
1773 return(DM_RET_DESCEND | DM_RET_COPY);
1774
1775 for (i = 0; i < num_patterns; i++) {
1776 struct bus_match_pattern *cur_pattern;
1777
1778 /*
1779 * If the pattern in question isn't for a bus node, we
1780 * aren't interested. However, we do indicate to the
1781 * calling routine that we should continue descending the
1782 * tree, since the user wants to match against lower-level
1783 * EDT elements.
1784 */
1785 if (patterns[i].type != DEV_MATCH_BUS) {
1786 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1787 retval |= DM_RET_DESCEND;
1788 continue;
1789 }
1790
1791 cur_pattern = &patterns[i].pattern.bus_pattern;
1792
1793 /*
1794 * If they want to match any bus node, we give them any
1795 * device node.
1796 */
1797 if (cur_pattern->flags == BUS_MATCH_ANY) {
1798 /* set the copy flag */
1799 retval |= DM_RET_COPY;
1800
1801 /*
1802 * If we've already decided on an action, go ahead
1803 * and return.
1804 */
1805 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1806 return(retval);
1807 }
1808
1809 /*
1810 * Not sure why someone would do this...
1811 */
1812 if (cur_pattern->flags == BUS_MATCH_NONE)
1813 continue;
1814
1815 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1816 && (cur_pattern->path_id != bus->path_id))
1817 continue;
1818
1819 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1820 && (cur_pattern->bus_id != bus->sim->bus_id))
1821 continue;
1822
1823 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1824 && (cur_pattern->unit_number != bus->sim->unit_number))
1825 continue;
1826
1827 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1828 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1829 DEV_IDLEN) != 0))
1830 continue;
1831
1832 /*
1833 * If we get to this point, the user definitely wants
1834 * information on this bus. So tell the caller to copy the
1835 * data out.
1836 */
1837 retval |= DM_RET_COPY;
1838
1839 /*
1840 * If the return action has been set to descend, then we
1841 * know that we've already seen a non-bus matching
1842 * expression, therefore we need to further descend the tree.
1843 * This won't change by continuing around the loop, so we
1844 * go ahead and return. If we haven't seen a non-bus
1845 * matching expression, we keep going around the loop until
1846 * we exhaust the matching expressions. We'll set the stop
1847 * flag once we fall out of the loop.
1848 */
1849 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1850 return(retval);
1851 }
1852
1853 /*
1854 * If the return action hasn't been set to descend yet, that means
1855 * we haven't seen anything other than bus matching patterns. So
1856 * tell the caller to stop descending the tree -- the user doesn't
1857 * want to match against lower level tree elements.
1858 */
1859 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1860 retval |= DM_RET_STOP;
1861
1862 return(retval);
1863}
1864
1865static dev_match_ret
1866xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1867 struct cam_ed *device)
1868{
1869 dev_match_ret retval;
1870 int i;
1871
1872 retval = DM_RET_NONE;
1873
1874 /*
1875 * If we aren't given something to match against, that's an error.
1876 */
1877 if (device == NULL)
1878 return(DM_RET_ERROR);
1879
1880 /*
1881 * If there are no match entries, then this device matches no
1882 * matter what.
1883 */
1884 if ((patterns == NULL) || (patterns == 0))
1885 return(DM_RET_DESCEND | DM_RET_COPY);
1886
1887 for (i = 0; i < num_patterns; i++) {
1888 struct device_match_pattern *cur_pattern;
1889
1890 /*
1891 * If the pattern in question isn't for a device node, we
1892 * aren't interested.
1893 */
1894 if (patterns[i].type != DEV_MATCH_DEVICE) {
1895 if ((patterns[i].type == DEV_MATCH_PERIPH)
1896 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1897 retval |= DM_RET_DESCEND;
1898 continue;
1899 }
1900
1901 cur_pattern = &patterns[i].pattern.device_pattern;
1902
1903 /*
1904 * If they want to match any device node, we give them any
1905 * device node.
1906 */
1907 if (cur_pattern->flags == DEV_MATCH_ANY) {
1908 /* set the copy flag */
1909 retval |= DM_RET_COPY;
1910
1911
1912 /*
1913 * If we've already decided on an action, go ahead
1914 * and return.
1915 */
1916 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1917 return(retval);
1918 }
1919
1920 /*
1921 * Not sure why someone would do this...
1922 */
1923 if (cur_pattern->flags == DEV_MATCH_NONE)
1924 continue;
1925
1926 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1927 && (cur_pattern->path_id != device->target->bus->path_id))
1928 continue;
1929
1930 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1931 && (cur_pattern->target_id != device->target->target_id))
1932 continue;
1933
1934 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1935 && (cur_pattern->target_lun != device->lun_id))
1936 continue;
1937
1938 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1939 && (cam_quirkmatch((caddr_t)&device->inq_data,
1940 (caddr_t)&cur_pattern->inq_pat,
1941 1, sizeof(cur_pattern->inq_pat),
1942 scsi_static_inquiry_match) == NULL))
1943 continue;
1944
1945 /*
1946 * If we get to this point, the user definitely wants
1947 * information on this device. So tell the caller to copy
1948 * the data out.
1949 */
1950 retval |= DM_RET_COPY;
1951
1952 /*
1953 * If the return action has been set to descend, then we
1954 * know that we've already seen a peripheral matching
1955 * expression, therefore we need to further descend the tree.
1956 * This won't change by continuing around the loop, so we
1957 * go ahead and return. If we haven't seen a peripheral
1958 * matching expression, we keep going around the loop until
1959 * we exhaust the matching expressions. We'll set the stop
1960 * flag once we fall out of the loop.
1961 */
1962 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1963 return(retval);
1964 }
1965
1966 /*
1967 * If the return action hasn't been set to descend yet, that means
1968 * we haven't seen any peripheral matching patterns. So tell the
1969 * caller to stop descending the tree -- the user doesn't want to
1970 * match against lower level tree elements.
1971 */
1972 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1973 retval |= DM_RET_STOP;
1974
1975 return(retval);
1976}
1977
1978/*
1979 * Match a single peripheral against any number of match patterns.
1980 */
1981static dev_match_ret
1982xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1983 struct cam_periph *periph)
1984{
1985 dev_match_ret retval;
1986 int i;
1987
1988 /*
1989 * If we aren't given something to match against, that's an error.
1990 */
1991 if (periph == NULL)
1992 return(DM_RET_ERROR);
1993
1994 /*
1995 * If there are no match entries, then this peripheral matches no
1996 * matter what.
1997 */
1998 if ((patterns == NULL) || (num_patterns == 0))
1999 return(DM_RET_STOP | DM_RET_COPY);
2000
2001 /*
2002 * There aren't any nodes below a peripheral node, so there's no
2003 * reason to descend the tree any further.
2004 */
2005 retval = DM_RET_STOP;
2006
2007 for (i = 0; i < num_patterns; i++) {
2008 struct periph_match_pattern *cur_pattern;
2009
2010 /*
2011 * If the pattern in question isn't for a peripheral, we
2012 * aren't interested.
2013 */
2014 if (patterns[i].type != DEV_MATCH_PERIPH)
2015 continue;
2016
2017 cur_pattern = &patterns[i].pattern.periph_pattern;
2018
2019 /*
2020 * If they want to match on anything, then we will do so.
2021 */
2022 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
2023 /* set the copy flag */
2024 retval |= DM_RET_COPY;
2025
2026 /*
2027 * We've already set the return action to stop,
2028 * since there are no nodes below peripherals in
2029 * the tree.
2030 */
2031 return(retval);
2032 }
2033
2034 /*
2035 * Not sure why someone would do this...
2036 */
2037 if (cur_pattern->flags == PERIPH_MATCH_NONE)
2038 continue;
2039
2040 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
2041 && (cur_pattern->path_id != periph->path->bus->path_id))
2042 continue;
2043
2044 /*
2045 * For the target and lun id's, we have to make sure the
2046 * target and lun pointers aren't NULL. The xpt peripheral
2047 * has a wildcard target and device.
2048 */
2049 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
2050 && ((periph->path->target == NULL)
2051 ||(cur_pattern->target_id != periph->path->target->target_id)))
2052 continue;
2053
2054 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
2055 && ((periph->path->device == NULL)
2056 || (cur_pattern->target_lun != periph->path->device->lun_id)))
2057 continue;
2058
2059 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
2060 && (cur_pattern->unit_number != periph->unit_number))
2061 continue;
2062
2063 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
2064 && (strncmp(cur_pattern->periph_name, periph->periph_name,
2065 DEV_IDLEN) != 0))
2066 continue;
2067
2068 /*
2069 * If we get to this point, the user definitely wants
2070 * information on this peripheral. So tell the caller to
2071 * copy the data out.
2072 */
2073 retval |= DM_RET_COPY;
2074
2075 /*
2076 * The return action has already been set to stop, since
2077 * peripherals don't have any nodes below them in the EDT.
2078 */
2079 return(retval);
2080 }
2081
2082 /*
2083 * If we get to this point, the peripheral that was passed in
2084 * doesn't match any of the patterns.
2085 */
2086 return(retval);
2087}
2088
2089static int
2090xptedtbusfunc(struct cam_eb *bus, void *arg)
2091{
2092 struct ccb_dev_match *cdm;
2093 dev_match_ret retval;
2094
2095 cdm = (struct ccb_dev_match *)arg;
2096
2097 /*
2098 * If our position is for something deeper in the tree, that means
2099 * that we've already seen this node. So, we keep going down.
2100 */
2101 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2102 && (cdm->pos.cookie.bus == bus)
2103 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2104 && (cdm->pos.cookie.target != NULL))
2105 retval = DM_RET_DESCEND;
2106 else
2107 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
2108
2109 /*
2110 * If we got an error, bail out of the search.
2111 */
2112 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2113 cdm->status = CAM_DEV_MATCH_ERROR;
2114 return(0);
2115 }
2116
2117 /*
2118 * If the copy flag is set, copy this bus out.
2119 */
2120 if (retval & DM_RET_COPY) {
2121 int spaceleft, j;
2122
2123 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2124 sizeof(struct dev_match_result));
2125
2126 /*
2127 * If we don't have enough space to put in another
2128 * match result, save our position and tell the
2129 * user there are more devices to check.
2130 */
2131 if (spaceleft < sizeof(struct dev_match_result)) {
2132 bzero(&cdm->pos, sizeof(cdm->pos));
2133 cdm->pos.position_type =
2134 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
2135
2136 cdm->pos.cookie.bus = bus;
2137 cdm->pos.generations[CAM_BUS_GENERATION]=
2138 bus_generation;
2139 cdm->status = CAM_DEV_MATCH_MORE;
2140 return(0);
2141 }
2142 j = cdm->num_matches;
2143 cdm->num_matches++;
2144 cdm->matches[j].type = DEV_MATCH_BUS;
2145 cdm->matches[j].result.bus_result.path_id = bus->path_id;
2146 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
2147 cdm->matches[j].result.bus_result.unit_number =
2148 bus->sim->unit_number;
2149 strncpy(cdm->matches[j].result.bus_result.dev_name,
2150 bus->sim->sim_name, DEV_IDLEN);
2151 }
2152
2153 /*
2154 * If the user is only interested in busses, there's no
2155 * reason to descend to the next level in the tree.
2156 */
2157 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2158 return(1);
2159
2160 /*
2161 * If there is a target generation recorded, check it to
2162 * make sure the target list hasn't changed.
2163 */
2164 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2165 && (bus == cdm->pos.cookie.bus)
2166 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2167 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
2168 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
2169 bus->generation)) {
2170 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2171 return(0);
2172 }
2173
2174 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2175 && (cdm->pos.cookie.bus == bus)
2176 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2177 && (cdm->pos.cookie.target != NULL))
2178 return(xpttargettraverse(bus,
2179 (struct cam_et *)cdm->pos.cookie.target,
2180 xptedttargetfunc, arg));
2181 else
2182 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2183}
2184
2185static int
2186xptedttargetfunc(struct cam_et *target, void *arg)
2187{
2188 struct ccb_dev_match *cdm;
2189
2190 cdm = (struct ccb_dev_match *)arg;
2191
2192 /*
2193 * If there is a device list generation recorded, check it to
2194 * make sure the device list hasn't changed.
2195 */
2196 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2197 && (cdm->pos.cookie.bus == target->bus)
2198 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2199 && (cdm->pos.cookie.target == target)
2200 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2201 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2202 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2203 target->generation)) {
2204 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2205 return(0);
2206 }
2207
2208 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2209 && (cdm->pos.cookie.bus == target->bus)
2210 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2211 && (cdm->pos.cookie.target == target)
2212 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2213 && (cdm->pos.cookie.device != NULL))
2214 return(xptdevicetraverse(target,
2215 (struct cam_ed *)cdm->pos.cookie.device,
2216 xptedtdevicefunc, arg));
2217 else
2218 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2219}
2220
2221static int
2222xptedtdevicefunc(struct cam_ed *device, void *arg)
2223{
2224
2225 struct ccb_dev_match *cdm;
2226 dev_match_ret retval;
2227
2228 cdm = (struct ccb_dev_match *)arg;
2229
2230 /*
2231 * If our position is for something deeper in the tree, that means
2232 * that we've already seen this node. So, we keep going down.
2233 */
2234 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2235 && (cdm->pos.cookie.device == device)
2236 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2237 && (cdm->pos.cookie.periph != NULL))
2238 retval = DM_RET_DESCEND;
2239 else
2240 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2241 device);
2242
2243 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2244 cdm->status = CAM_DEV_MATCH_ERROR;
2245 return(0);
2246 }
2247
2248 /*
2249 * If the copy flag is set, copy this device out.
2250 */
2251 if (retval & DM_RET_COPY) {
2252 int spaceleft, j;
2253
2254 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2255 sizeof(struct dev_match_result));
2256
2257 /*
2258 * If we don't have enough space to put in another
2259 * match result, save our position and tell the
2260 * user there are more devices to check.
2261 */
2262 if (spaceleft < sizeof(struct dev_match_result)) {
2263 bzero(&cdm->pos, sizeof(cdm->pos));
2264 cdm->pos.position_type =
2265 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2266 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2267
2268 cdm->pos.cookie.bus = device->target->bus;
2269 cdm->pos.generations[CAM_BUS_GENERATION]=
2270 bus_generation;
2271 cdm->pos.cookie.target = device->target;
2272 cdm->pos.generations[CAM_TARGET_GENERATION] =
2273 device->target->bus->generation;
2274 cdm->pos.cookie.device = device;
2275 cdm->pos.generations[CAM_DEV_GENERATION] =
2276 device->target->generation;
2277 cdm->status = CAM_DEV_MATCH_MORE;
2278 return(0);
2279 }
2280 j = cdm->num_matches;
2281 cdm->num_matches++;
2282 cdm->matches[j].type = DEV_MATCH_DEVICE;
2283 cdm->matches[j].result.device_result.path_id =
2284 device->target->bus->path_id;
2285 cdm->matches[j].result.device_result.target_id =
2286 device->target->target_id;
2287 cdm->matches[j].result.device_result.target_lun =
2288 device->lun_id;
2289 bcopy(&device->inq_data,
2290 &cdm->matches[j].result.device_result.inq_data,
2291 sizeof(struct scsi_inquiry_data));
2292
2293 /* Let the user know whether this device is unconfigured */
2294 if (device->flags & CAM_DEV_UNCONFIGURED)
2295 cdm->matches[j].result.device_result.flags =
2296 DEV_RESULT_UNCONFIGURED;
2297 else
2298 cdm->matches[j].result.device_result.flags =
2299 DEV_RESULT_NOFLAG;
2300 }
2301
2302 /*
2303 * If the user isn't interested in peripherals, don't descend
2304 * the tree any further.
2305 */
2306 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2307 return(1);
2308
2309 /*
2310 * If there is a peripheral list generation recorded, make sure
2311 * it hasn't changed.
2312 */
2313 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2314 && (device->target->bus == cdm->pos.cookie.bus)
2315 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2316 && (device->target == cdm->pos.cookie.target)
2317 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2318 && (device == cdm->pos.cookie.device)
2319 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2320 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2321 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2322 device->generation)){
2323 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2324 return(0);
2325 }
2326
2327 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2328 && (cdm->pos.cookie.bus == device->target->bus)
2329 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2330 && (cdm->pos.cookie.target == device->target)
2331 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2332 && (cdm->pos.cookie.device == device)
2333 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2334 && (cdm->pos.cookie.periph != NULL))
2335 return(xptperiphtraverse(device,
2336 (struct cam_periph *)cdm->pos.cookie.periph,
2337 xptedtperiphfunc, arg));
2338 else
2339 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2340}
2341
2342static int
2343xptedtperiphfunc(struct cam_periph *periph, void *arg)
2344{
2345 struct ccb_dev_match *cdm;
2346 dev_match_ret retval;
2347
2348 cdm = (struct ccb_dev_match *)arg;
2349
2350 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2351
2352 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2353 cdm->status = CAM_DEV_MATCH_ERROR;
2354 return(0);
2355 }
2356
2357 /*
2358 * If the copy flag is set, copy this peripheral out.
2359 */
2360 if (retval & DM_RET_COPY) {
2361 int spaceleft, j;
2362
2363 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2364 sizeof(struct dev_match_result));
2365
2366 /*
2367 * If we don't have enough space to put in another
2368 * match result, save our position and tell the
2369 * user there are more devices to check.
2370 */
2371 if (spaceleft < sizeof(struct dev_match_result)) {
2372 bzero(&cdm->pos, sizeof(cdm->pos));
2373 cdm->pos.position_type =
2374 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2375 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2376 CAM_DEV_POS_PERIPH;
2377
2378 cdm->pos.cookie.bus = periph->path->bus;
2379 cdm->pos.generations[CAM_BUS_GENERATION]=
2380 bus_generation;
2381 cdm->pos.cookie.target = periph->path->target;
2382 cdm->pos.generations[CAM_TARGET_GENERATION] =
2383 periph->path->bus->generation;
2384 cdm->pos.cookie.device = periph->path->device;
2385 cdm->pos.generations[CAM_DEV_GENERATION] =
2386 periph->path->target->generation;
2387 cdm->pos.cookie.periph = periph;
2388 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2389 periph->path->device->generation;
2390 cdm->status = CAM_DEV_MATCH_MORE;
2391 return(0);
2392 }
2393
2394 j = cdm->num_matches;
2395 cdm->num_matches++;
2396 cdm->matches[j].type = DEV_MATCH_PERIPH;
2397 cdm->matches[j].result.periph_result.path_id =
2398 periph->path->bus->path_id;
2399 cdm->matches[j].result.periph_result.target_id =
2400 periph->path->target->target_id;
2401 cdm->matches[j].result.periph_result.target_lun =
2402 periph->path->device->lun_id;
2403 cdm->matches[j].result.periph_result.unit_number =
2404 periph->unit_number;
2405 strncpy(cdm->matches[j].result.periph_result.periph_name,
2406 periph->periph_name, DEV_IDLEN);
2407 }
2408
2409 return(1);
2410}
2411
2412static int
2413xptedtmatch(struct ccb_dev_match *cdm)
2414{
2415 int ret;
2416
2417 cdm->num_matches = 0;
2418
2419 /*
2420 * Check the bus list generation. If it has changed, the user
2421 * needs to reset everything and start over.
2422 */
2423 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2424 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2425 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2426 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2427 return(0);
2428 }
2429
2430 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2431 && (cdm->pos.cookie.bus != NULL))
2432 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2433 xptedtbusfunc, cdm);
2434 else
2435 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2436
2437 /*
2438 * If we get back 0, that means that we had to stop before fully
2439 * traversing the EDT. It also means that one of the subroutines
2440 * has set the status field to the proper value. If we get back 1,
2441 * we've fully traversed the EDT and copied out any matching entries.
2442 */
2443 if (ret == 1)
2444 cdm->status = CAM_DEV_MATCH_LAST;
2445
2446 return(ret);
2447}
2448
2449static int
2450xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2451{
2452 struct ccb_dev_match *cdm;
2453
2454 cdm = (struct ccb_dev_match *)arg;
2455
2456 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2457 && (cdm->pos.cookie.pdrv == pdrv)
2458 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2459 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2460 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2461 (*pdrv)->generation)) {
2462 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2463 return(0);
2464 }
2465
2466 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2467 && (cdm->pos.cookie.pdrv == pdrv)
2468 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2469 && (cdm->pos.cookie.periph != NULL))
2470 return(xptpdperiphtraverse(pdrv,
2471 (struct cam_periph *)cdm->pos.cookie.periph,
2472 xptplistperiphfunc, arg));
2473 else
2474 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2475}
2476
2477static int
2478xptplistperiphfunc(struct cam_periph *periph, void *arg)
2479{
2480 struct ccb_dev_match *cdm;
2481 dev_match_ret retval;
2482
2483 cdm = (struct ccb_dev_match *)arg;
2484
2485 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2486
2487 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2488 cdm->status = CAM_DEV_MATCH_ERROR;
2489 return(0);
2490 }
2491
2492 /*
2493 * If the copy flag is set, copy this peripheral out.
2494 */
2495 if (retval & DM_RET_COPY) {
2496 int spaceleft, j;
2497
2498 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2499 sizeof(struct dev_match_result));
2500
2501 /*
2502 * If we don't have enough space to put in another
2503 * match result, save our position and tell the
2504 * user there are more devices to check.
2505 */
2506 if (spaceleft < sizeof(struct dev_match_result)) {
2507 struct periph_driver **pdrv;
2508
2509 pdrv = NULL;
2510 bzero(&cdm->pos, sizeof(cdm->pos));
2511 cdm->pos.position_type =
2512 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2513 CAM_DEV_POS_PERIPH;
2514
2515 /*
2516 * This may look a bit non-sensical, but it is
2517 * actually quite logical. There are very few
2518 * peripheral drivers, and bloating every peripheral
2519 * structure with a pointer back to its parent
2520 * peripheral driver linker set entry would cost
2521 * more in the long run than doing this quick lookup.
2522 */
2523 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2524 if (strcmp((*pdrv)->driver_name,
2525 periph->periph_name) == 0)
2526 break;
2527 }
2528
2529 if (pdrv == NULL) {
2530 cdm->status = CAM_DEV_MATCH_ERROR;
2531 return(0);
2532 }
2533
2534 cdm->pos.cookie.pdrv = pdrv;
2535 /*
2536 * The periph generation slot does double duty, as
2537 * does the periph pointer slot. They are used for
2538 * both edt and pdrv lookups and positioning.
2539 */
2540 cdm->pos.cookie.periph = periph;
2541 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2542 (*pdrv)->generation;
2543 cdm->status = CAM_DEV_MATCH_MORE;
2544 return(0);
2545 }
2546
2547 j = cdm->num_matches;
2548 cdm->num_matches++;
2549 cdm->matches[j].type = DEV_MATCH_PERIPH;
2550 cdm->matches[j].result.periph_result.path_id =
2551 periph->path->bus->path_id;
2552
2553 /*
2554 * The transport layer peripheral doesn't have a target or
2555 * lun.
2556 */
2557 if (periph->path->target)
2558 cdm->matches[j].result.periph_result.target_id =
2559 periph->path->target->target_id;
2560 else
2561 cdm->matches[j].result.periph_result.target_id = -1;
2562
2563 if (periph->path->device)
2564 cdm->matches[j].result.periph_result.target_lun =
2565 periph->path->device->lun_id;
2566 else
2567 cdm->matches[j].result.periph_result.target_lun = -1;
2568
2569 cdm->matches[j].result.periph_result.unit_number =
2570 periph->unit_number;
2571 strncpy(cdm->matches[j].result.periph_result.periph_name,
2572 periph->periph_name, DEV_IDLEN);
2573 }
2574
2575 return(1);
2576}
2577
2578static int
2579xptperiphlistmatch(struct ccb_dev_match *cdm)
2580{
2581 int ret;
2582
2583 cdm->num_matches = 0;
2584
2585 /*
2586 * At this point in the edt traversal function, we check the bus
2587 * list generation to make sure that no busses have been added or
2588 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2589 * For the peripheral driver list traversal function, however, we
2590 * don't have to worry about new peripheral driver types coming or
2591 * going; they're in a linker set, and therefore can't change
2592 * without a recompile.
2593 */
2594
2595 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2596 && (cdm->pos.cookie.pdrv != NULL))
2597 ret = xptpdrvtraverse(
2598 (struct periph_driver **)cdm->pos.cookie.pdrv,
2599 xptplistpdrvfunc, cdm);
2600 else
2601 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2602
2603 /*
2604 * If we get back 0, that means that we had to stop before fully
2605 * traversing the peripheral driver tree. It also means that one of
2606 * the subroutines has set the status field to the proper value. If
2607 * we get back 1, we've fully traversed the EDT and copied out any
2608 * matching entries.
2609 */
2610 if (ret == 1)
2611 cdm->status = CAM_DEV_MATCH_LAST;
2612
2613 return(ret);
2614}
2615
2616static int
2617xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2618{
2619 struct cam_eb *bus, *next_bus;
2620 int retval;
2621
2622 retval = 1;
2623
2624 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2625 bus != NULL;
2626 bus = next_bus) {
2627 next_bus = TAILQ_NEXT(bus, links);
2628
2629 retval = tr_func(bus, arg);
2630 if (retval == 0)
2631 return(retval);
2632 }
2633
2634 return(retval);
2635}
2636
2637static int
2638xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2639 xpt_targetfunc_t *tr_func, void *arg)
2640{
2641 struct cam_et *target, *next_target;
2642 int retval;
2643
2644 retval = 1;
2645 for (target = (start_target ? start_target :
2646 TAILQ_FIRST(&bus->et_entries));
2647 target != NULL; target = next_target) {
2648
2649 next_target = TAILQ_NEXT(target, links);
2650
2651 retval = tr_func(target, arg);
2652
2653 if (retval == 0)
2654 return(retval);
2655 }
2656
2657 return(retval);
2658}
2659
2660static int
2661xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2662 xpt_devicefunc_t *tr_func, void *arg)
2663{
2664 struct cam_ed *device, *next_device;
2665 int retval;
2666
2667 retval = 1;
2668 for (device = (start_device ? start_device :
2669 TAILQ_FIRST(&target->ed_entries));
2670 device != NULL;
2671 device = next_device) {
2672
2673 next_device = TAILQ_NEXT(device, links);
2674
2675 retval = tr_func(device, arg);
2676
2677 if (retval == 0)
2678 return(retval);
2679 }
2680
2681 return(retval);
2682}
2683
2684static int
2685xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2686 xpt_periphfunc_t *tr_func, void *arg)
2687{
2688 struct cam_periph *periph, *next_periph;
2689 int retval;
2690
2691 retval = 1;
2692
2693 for (periph = (start_periph ? start_periph :
2694 SLIST_FIRST(&device->periphs));
2695 periph != NULL;
2696 periph = next_periph) {
2697
2698 next_periph = SLIST_NEXT(periph, periph_links);
2699
2700 retval = tr_func(periph, arg);
2701 if (retval == 0)
2702 return(retval);
2703 }
2704
2705 return(retval);
2706}
2707
2708static int
2709xptpdrvtraverse(struct periph_driver **start_pdrv,
2710 xpt_pdrvfunc_t *tr_func, void *arg)
2711{
2712 struct periph_driver **pdrv;
2713 int retval;
2714
2715 retval = 1;
2716
2717 /*
2718 * We don't traverse the peripheral driver list like we do the
2719 * other lists, because it is a linker set, and therefore cannot be
2720 * changed during runtime. If the peripheral driver list is ever
2721 * re-done to be something other than a linker set (i.e. it can
2722 * change while the system is running), the list traversal should
2723 * be modified to work like the other traversal functions.
2724 */
2725 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2726 *pdrv != NULL; pdrv++) {
2727 retval = tr_func(pdrv, arg);
2728
2729 if (retval == 0)
2730 return(retval);
2731 }
2732
2733 return(retval);
2734}
2735
2736static int
2737xptpdperiphtraverse(struct periph_driver **pdrv,
2738 struct cam_periph *start_periph,
2739 xpt_periphfunc_t *tr_func, void *arg)
2740{
2741 struct cam_periph *periph, *next_periph;
2742 int retval;
2743
2744 retval = 1;
2745
2746 for (periph = (start_periph ? start_periph :
2747 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2748 periph = next_periph) {
2749
2750 next_periph = TAILQ_NEXT(periph, unit_links);
2751
2752 retval = tr_func(periph, arg);
2753 if (retval == 0)
2754 return(retval);
2755 }
2756 return(retval);
2757}
2758
2759static int
2760xptdefbusfunc(struct cam_eb *bus, void *arg)
2761{
2762 struct xpt_traverse_config *tr_config;
2763
2764 tr_config = (struct xpt_traverse_config *)arg;
2765
2766 if (tr_config->depth == XPT_DEPTH_BUS) {
2767 xpt_busfunc_t *tr_func;
2768
2769 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2770
2771 return(tr_func(bus, tr_config->tr_arg));
2772 } else
2773 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2774}
2775
2776static int
2777xptdeftargetfunc(struct cam_et *target, void *arg)
2778{
2779 struct xpt_traverse_config *tr_config;
2780
2781 tr_config = (struct xpt_traverse_config *)arg;
2782
2783 if (tr_config->depth == XPT_DEPTH_TARGET) {
2784 xpt_targetfunc_t *tr_func;
2785
2786 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2787
2788 return(tr_func(target, tr_config->tr_arg));
2789 } else
2790 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2791}
2792
2793static int
2794xptdefdevicefunc(struct cam_ed *device, void *arg)
2795{
2796 struct xpt_traverse_config *tr_config;
2797
2798 tr_config = (struct xpt_traverse_config *)arg;
2799
2800 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2801 xpt_devicefunc_t *tr_func;
2802
2803 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2804
2805 return(tr_func(device, tr_config->tr_arg));
2806 } else
2807 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2808}
2809
2810static int
2811xptdefperiphfunc(struct cam_periph *periph, void *arg)
2812{
2813 struct xpt_traverse_config *tr_config;
2814 xpt_periphfunc_t *tr_func;
2815
2816 tr_config = (struct xpt_traverse_config *)arg;
2817
2818 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2819
2820 /*
2821 * Unlike the other default functions, we don't check for depth
2822 * here. The peripheral driver level is the last level in the EDT,
2823 * so if we're here, we should execute the function in question.
2824 */
2825 return(tr_func(periph, tr_config->tr_arg));
2826}
2827
2828/*
2829 * Execute the given function for every bus in the EDT.
2830 */
2831static int
2832xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2833{
2834 struct xpt_traverse_config tr_config;
2835
2836 tr_config.depth = XPT_DEPTH_BUS;
2837 tr_config.tr_func = tr_func;
2838 tr_config.tr_arg = arg;
2839
2840 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2841}
2842
2843#ifdef notusedyet
2844/*
2845 * Execute the given function for every target in the EDT.
2846 */
2847static int
2848xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2849{
2850 struct xpt_traverse_config tr_config;
2851
2852 tr_config.depth = XPT_DEPTH_TARGET;
2853 tr_config.tr_func = tr_func;
2854 tr_config.tr_arg = arg;
2855
2856 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2857}
2858#endif /* notusedyet */
2859
2860/*
2861 * Execute the given function for every device in the EDT.
2862 */
2863static int
2864xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2865{
2866 struct xpt_traverse_config tr_config;
2867
2868 tr_config.depth = XPT_DEPTH_DEVICE;
2869 tr_config.tr_func = tr_func;
2870 tr_config.tr_arg = arg;
2871
2872 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2873}
2874
2875#ifdef notusedyet
2876/*
2877 * Execute the given function for every peripheral in the EDT.
2878 */
2879static int
2880xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2881{
2882 struct xpt_traverse_config tr_config;
2883
2884 tr_config.depth = XPT_DEPTH_PERIPH;
2885 tr_config.tr_func = tr_func;
2886 tr_config.tr_arg = arg;
2887
2888 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2889}
2890#endif /* notusedyet */
2891
2892static int
2893xptsetasyncfunc(struct cam_ed *device, void *arg)
2894{
2895 struct cam_path path;
2896 struct ccb_getdev cgd;
2897 struct async_node *cur_entry;
2898
2899 cur_entry = (struct async_node *)arg;
2900
2901 /*
2902 * Don't report unconfigured devices (Wildcard devs,
2903 * devices only for target mode, device instances
2904 * that have been invalidated but are waiting for
2905 * their last reference count to be released).
2906 */
2907 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2908 return (1);
2909
2910 xpt_compile_path(&path,
2911 NULL,
2912 device->target->bus->path_id,
2913 device->target->target_id,
2914 device->lun_id);
2915 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2916 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2917 xpt_action((union ccb *)&cgd);
2918 cur_entry->callback(cur_entry->callback_arg,
2919 AC_FOUND_DEVICE,
2920 &path, &cgd);
2921 xpt_release_path(&path);
2922
2923 return(1);
2924}
2925
2926static int
2927xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2928{
2929 struct cam_path path;
2930 struct ccb_pathinq cpi;
2931 struct async_node *cur_entry;
2932
2933 cur_entry = (struct async_node *)arg;
2934
2935 xpt_compile_path(&path, /*periph*/NULL,
2936 bus->sim->path_id,
2937 CAM_TARGET_WILDCARD,
2938 CAM_LUN_WILDCARD);
2939 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2940 cpi.ccb_h.func_code = XPT_PATH_INQ;
2941 xpt_action((union ccb *)&cpi);
2942 cur_entry->callback(cur_entry->callback_arg,
2943 AC_PATH_REGISTERED,
2944 &path, &cpi);
2945 xpt_release_path(&path);
2946
2947 return(1);
2948}
2949
2950void
2951xpt_action(union ccb *start_ccb)
2952{
2953 int iopl;
2954
2955 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2956
2957 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2958
2959 iopl = splsoftcam();
2960 switch (start_ccb->ccb_h.func_code) {
2961 case XPT_SCSI_IO:
2962 {
2963#ifdef CAM_NEW_TRAN_CODE
2964 struct cam_ed *device;
2965#endif /* CAM_NEW_TRAN_CODE */
2966#ifdef CAMDEBUG
2967 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2968 struct cam_path *path;
2969
2970 path = start_ccb->ccb_h.path;
2971#endif
2972
2973 /*
2974 * For the sake of compatibility with SCSI-1
2975 * devices that may not understand the identify
2976 * message, we include lun information in the
2977 * second byte of all commands. SCSI-1 specifies
2978 * that luns are a 3 bit value and reserves only 3
2979 * bits for lun information in the CDB. Later
2980 * revisions of the SCSI spec allow for more than 8
2981 * luns, but have deprecated lun information in the
2982 * CDB. So, if the lun won't fit, we must omit.
2983 *
2984 * Also be aware that during initial probing for devices,
2985 * the inquiry information is unknown but initialized to 0.
2986 * This means that this code will be exercised while probing
2987 * devices with an ANSI revision greater than 2.
2988 */
2989#ifdef CAM_NEW_TRAN_CODE
2990 device = start_ccb->ccb_h.path->device;
2991 if (device->protocol_version <= SCSI_REV_2
2992#else /* CAM_NEW_TRAN_CODE */
2993 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
2994#endif /* CAM_NEW_TRAN_CODE */
2995 && start_ccb->ccb_h.target_lun < 8
2996 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2997
2998 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2999 start_ccb->ccb_h.target_lun << 5;
3000 }
3001 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
3002 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
3003 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
3004 &path->device->inq_data),
3005 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
3006 cdb_str, sizeof(cdb_str))));
3007 /* FALLTHROUGH */
3008 }
3009 case XPT_TARGET_IO:
3010 case XPT_CONT_TARGET_IO:
3011 start_ccb->csio.sense_resid = 0;
3012 start_ccb->csio.resid = 0;
3013 /* FALLTHROUGH */
3014 case XPT_RESET_DEV:
3015 case XPT_ENG_EXEC:
3016 {
3017 struct cam_path *path;
3018 int s;
3019 int runq;
3020
3021 path = start_ccb->ccb_h.path;
3022 s = splsoftcam();
3023
3024 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
3025 if (path->device->qfrozen_cnt == 0)
3026 runq = xpt_schedule_dev_sendq(path->bus, path->device);
3027 else
3028 runq = 0;
3029 splx(s);
3030 if (runq != 0)
3031 xpt_run_dev_sendq(path->bus);
3032 break;
3033 }
3034 case XPT_SET_TRAN_SETTINGS:
3035 {
3036 xpt_set_transfer_settings(&start_ccb->cts,
3037 start_ccb->ccb_h.path->device,
3038 /*async_update*/FALSE);
3039 break;
3040 }
3041 case XPT_CALC_GEOMETRY:
3042 {
3043 struct cam_sim *sim;
3044
3045 /* Filter out garbage */
3046 if (start_ccb->ccg.block_size == 0
3047 || start_ccb->ccg.volume_size == 0) {
3048 start_ccb->ccg.cylinders = 0;
3049 start_ccb->ccg.heads = 0;
3050 start_ccb->ccg.secs_per_track = 0;
3051 start_ccb->ccb_h.status = CAM_REQ_CMP;
3052 break;
3053 }
3054#ifdef PC98
3055 /*
3056 * In a PC-98 system, geometry translation depens on
3057 * the "real" device geometry obtained from mode page 4.
3058 * SCSI geometry translation is performed in the
3059 * initialization routine of the SCSI BIOS and the result
3060 * stored in host memory. If the translation is available
3061 * in host memory, use it. If not, rely on the default
3062 * translation the device driver performs.
3063 */
3064 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
3065 start_ccb->ccb_h.status = CAM_REQ_CMP;
3066 break;
3067 }
3068#endif
3069 sim = start_ccb->ccb_h.path->bus->sim;
3070 (*(sim->sim_action))(sim, start_ccb);
3071 break;
3072 }
3073 case XPT_ABORT:
3074 {
3075 union ccb* abort_ccb;
3076 int s;
3077
3078 abort_ccb = start_ccb->cab.abort_ccb;
3079 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
3080
3081 if (abort_ccb->ccb_h.pinfo.index >= 0) {
3082 struct cam_ccbq *ccbq;
3083
3084 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
3085 cam_ccbq_remove_ccb(ccbq, abort_ccb);
3086 abort_ccb->ccb_h.status =
3087 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3088 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3089 s = splcam();
3090 xpt_done(abort_ccb);
3091 splx(s);
3092 start_ccb->ccb_h.status = CAM_REQ_CMP;
3093 break;
3094 }
3095 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
3096 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
3097 /*
3098 * We've caught this ccb en route to
3099 * the SIM. Flag it for abort and the
3100 * SIM will do so just before starting
3101 * real work on the CCB.
3102 */
3103 abort_ccb->ccb_h.status =
3104 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3105 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3106 start_ccb->ccb_h.status = CAM_REQ_CMP;
3107 break;
3108 }
3109 }
3110 if (XPT_FC_IS_QUEUED(abort_ccb)
3111 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
3112 /*
3113 * It's already completed but waiting
3114 * for our SWI to get to it.
3115 */
3116 start_ccb->ccb_h.status = CAM_UA_ABORT;
3117 break;
3118 }
3119 /*
3120 * If we weren't able to take care of the abort request
3121 * in the XPT, pass the request down to the SIM for processing.
3122 */
3123 /* FALLTHROUGH */
3124 }
3125 case XPT_ACCEPT_TARGET_IO:
3126 case XPT_EN_LUN:
3127 case XPT_IMMED_NOTIFY:
3128 case XPT_NOTIFY_ACK:
3129 case XPT_GET_TRAN_SETTINGS:
3130 case XPT_RESET_BUS:
3131 {
3132 struct cam_sim *sim;
3133
3134 sim = start_ccb->ccb_h.path->bus->sim;
3135 (*(sim->sim_action))(sim, start_ccb);
3136 break;
3137 }
3138 case XPT_PATH_INQ:
3139 {
3140 struct cam_sim *sim;
3141
3142 sim = start_ccb->ccb_h.path->bus->sim;
3143 (*(sim->sim_action))(sim, start_ccb);
3144 break;
3145 }
3146 case XPT_PATH_STATS:
3147 start_ccb->cpis.last_reset =
3148 start_ccb->ccb_h.path->bus->last_reset;
3149 start_ccb->ccb_h.status = CAM_REQ_CMP;
3150 break;
3151 case XPT_GDEV_TYPE:
3152 {
3153 struct cam_ed *dev;
3154 int s;
3155
3156 dev = start_ccb->ccb_h.path->device;
3157 s = splcam();
3158 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3159 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3160 } else {
3161 struct ccb_getdev *cgd;
3162 struct cam_eb *bus;
3163 struct cam_et *tar;
3164
3165 cgd = &start_ccb->cgd;
3166 bus = cgd->ccb_h.path->bus;
3167 tar = cgd->ccb_h.path->target;
3168 cgd->inq_data = dev->inq_data;
3169 cgd->ccb_h.status = CAM_REQ_CMP;
3170 cgd->serial_num_len = dev->serial_num_len;
3171 if ((dev->serial_num_len > 0)
3172 && (dev->serial_num != NULL))
3173 bcopy(dev->serial_num, cgd->serial_num,
3174 dev->serial_num_len);
3175 }
3176 splx(s);
3177 break;
3178 }
3179 case XPT_GDEV_STATS:
3180 {
3181 struct cam_ed *dev;
3182 int s;
3183
3184 dev = start_ccb->ccb_h.path->device;
3185 s = splcam();
3186 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3187 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3188 } else {
3189 struct ccb_getdevstats *cgds;
3190 struct cam_eb *bus;
3191 struct cam_et *tar;
3192
3193 cgds = &start_ccb->cgds;
3194 bus = cgds->ccb_h.path->bus;
3195 tar = cgds->ccb_h.path->target;
3196 cgds->dev_openings = dev->ccbq.dev_openings;
3197 cgds->dev_active = dev->ccbq.dev_active;
3198 cgds->devq_openings = dev->ccbq.devq_openings;
3199 cgds->devq_queued = dev->ccbq.queue.entries;
3200 cgds->held = dev->ccbq.held;
3201 cgds->last_reset = tar->last_reset;
3202 cgds->maxtags = dev->quirk->maxtags;
3203 cgds->mintags = dev->quirk->mintags;
3204 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3205 cgds->last_reset = bus->last_reset;
3206 cgds->ccb_h.status = CAM_REQ_CMP;
3207 }
3208 splx(s);
3209 break;
3210 }
3211 case XPT_GDEVLIST:
3212 {
3213 struct cam_periph *nperiph;
3214 struct periph_list *periph_head;
3215 struct ccb_getdevlist *cgdl;
3216 u_int i;
3217 int s;
3218 struct cam_ed *device;
3219 int found;
3220
3221
3222 found = 0;
3223
3224 /*
3225 * Don't want anyone mucking with our data.
3226 */
3227 s = splcam();
3228 device = start_ccb->ccb_h.path->device;
3229 periph_head = &device->periphs;
3230 cgdl = &start_ccb->cgdl;
3231
3232 /*
3233 * Check and see if the list has changed since the user
3234 * last requested a list member. If so, tell them that the
3235 * list has changed, and therefore they need to start over
3236 * from the beginning.
3237 */
3238 if ((cgdl->index != 0) &&
3239 (cgdl->generation != device->generation)) {
3240 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3241 splx(s);
3242 break;
3243 }
3244
3245 /*
3246 * Traverse the list of peripherals and attempt to find
3247 * the requested peripheral.
3248 */
3249 for (nperiph = SLIST_FIRST(periph_head), i = 0;
3250 (nperiph != NULL) && (i <= cgdl->index);
3251 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3252 if (i == cgdl->index) {
3253 strncpy(cgdl->periph_name,
3254 nperiph->periph_name,
3255 DEV_IDLEN);
3256 cgdl->unit_number = nperiph->unit_number;
3257 found = 1;
3258 }
3259 }
3260 if (found == 0) {
3261 cgdl->status = CAM_GDEVLIST_ERROR;
3262 splx(s);
3263 break;
3264 }
3265
3266 if (nperiph == NULL)
3267 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3268 else
3269 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3270
3271 cgdl->index++;
3272 cgdl->generation = device->generation;
3273
3274 splx(s);
3275 cgdl->ccb_h.status = CAM_REQ_CMP;
3276 break;
3277 }
3278 case XPT_DEV_MATCH:
3279 {
3280 int s;
3281 dev_pos_type position_type;
3282 struct ccb_dev_match *cdm;
3283 int ret;
3284
3285 cdm = &start_ccb->cdm;
3286
3287 /*
3288 * Prevent EDT changes while we traverse it.
3289 */
3290 s = splcam();
3291 /*
3292 * There are two ways of getting at information in the EDT.
3293 * The first way is via the primary EDT tree. It starts
3294 * with a list of busses, then a list of targets on a bus,
3295 * then devices/luns on a target, and then peripherals on a
3296 * device/lun. The "other" way is by the peripheral driver
3297 * lists. The peripheral driver lists are organized by
3298 * peripheral driver. (obviously) So it makes sense to
3299 * use the peripheral driver list if the user is looking
3300 * for something like "da1", or all "da" devices. If the
3301 * user is looking for something on a particular bus/target
3302 * or lun, it's generally better to go through the EDT tree.
3303 */
3304
3305 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3306 position_type = cdm->pos.position_type;
3307 else {
3308 u_int i;
3309
3310 position_type = CAM_DEV_POS_NONE;
3311
3312 for (i = 0; i < cdm->num_patterns; i++) {
3313 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3314 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3315 position_type = CAM_DEV_POS_EDT;
3316 break;
3317 }
3318 }
3319
3320 if (cdm->num_patterns == 0)
3321 position_type = CAM_DEV_POS_EDT;
3322 else if (position_type == CAM_DEV_POS_NONE)
3323 position_type = CAM_DEV_POS_PDRV;
3324 }
3325
3326 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3327 case CAM_DEV_POS_EDT:
3328 ret = xptedtmatch(cdm);
3329 break;
3330 case CAM_DEV_POS_PDRV:
3331 ret = xptperiphlistmatch(cdm);
3332 break;
3333 default:
3334 cdm->status = CAM_DEV_MATCH_ERROR;
3335 break;
3336 }
3337
3338 splx(s);
3339
3340 if (cdm->status == CAM_DEV_MATCH_ERROR)
3341 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3342 else
3343 start_ccb->ccb_h.status = CAM_REQ_CMP;
3344
3345 break;
3346 }
3347 case XPT_SASYNC_CB:
3348 {
3349 struct ccb_setasync *csa;
3350 struct async_node *cur_entry;
3351 struct async_list *async_head;
3352 u_int32_t added;
3353 int s;
3354
3355 csa = &start_ccb->csa;
3356 added = csa->event_enable;
3357 async_head = &csa->ccb_h.path->device->asyncs;
3358
3359 /*
3360 * If there is already an entry for us, simply
3361 * update it.
3362 */
3363 s = splcam();
3364 cur_entry = SLIST_FIRST(async_head);
3365 while (cur_entry != NULL) {
3366 if ((cur_entry->callback_arg == csa->callback_arg)
3367 && (cur_entry->callback == csa->callback))
3368 break;
3369 cur_entry = SLIST_NEXT(cur_entry, links);
3370 }
3371
3372 if (cur_entry != NULL) {
3373 /*
3374 * If the request has no flags set,
3375 * remove the entry.
3376 */
3377 added &= ~cur_entry->event_enable;
3378 if (csa->event_enable == 0) {
3379 SLIST_REMOVE(async_head, cur_entry,
3380 async_node, links);
3381 csa->ccb_h.path->device->refcount--;
3382 free(cur_entry, M_DEVBUF);
3383 } else {
3384 cur_entry->event_enable = csa->event_enable;
3385 }
3386 } else {
3387 cur_entry = malloc(sizeof(*cur_entry), M_DEVBUF,
3388 M_NOWAIT);
3389 if (cur_entry == NULL) {
3390 splx(s);
3391 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3392 break;
3393 }
3394 cur_entry->event_enable = csa->event_enable;
3395 cur_entry->callback_arg = csa->callback_arg;
3396 cur_entry->callback = csa->callback;
3397 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3398 csa->ccb_h.path->device->refcount++;
3399 }
3400
3401 if ((added & AC_FOUND_DEVICE) != 0) {
3402 /*
3403 * Get this peripheral up to date with all
3404 * the currently existing devices.
3405 */
3406 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3407 }
3408 if ((added & AC_PATH_REGISTERED) != 0) {
3409 /*
3410 * Get this peripheral up to date with all
3411 * the currently existing busses.
3412 */
3413 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3414 }
3415 splx(s);
3416 start_ccb->ccb_h.status = CAM_REQ_CMP;
3417 break;
3418 }
3419 case XPT_REL_SIMQ:
3420 {
3421 struct ccb_relsim *crs;
3422 struct cam_ed *dev;
3423 int s;
3424
3425 crs = &start_ccb->crs;
3426 dev = crs->ccb_h.path->device;
3427 if (dev == NULL) {
3428
3429 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3430 break;
3431 }
3432
3433 s = splcam();
3434
3435 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3436
3437 if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3438
3439 /* Don't ever go below one opening */
3440 if (crs->openings > 0) {
3441 xpt_dev_ccbq_resize(crs->ccb_h.path,
3442 crs->openings);
3443
3444 if (bootverbose) {
3445 xpt_print_path(crs->ccb_h.path);
3446 printf("tagged openings "
3447 "now %d\n",
3448 crs->openings);
3449 }
3450 }
3451 }
3452 }
3453
3454 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3455
3456 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3457
3458 /*
3459 * Just extend the old timeout and decrement
3460 * the freeze count so that a single timeout
3461 * is sufficient for releasing the queue.
3462 */
3463 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3464 untimeout(xpt_release_devq_timeout,
3465 dev, dev->c_handle);
3466 } else {
3467
3468 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3469 }
3470
3471 dev->c_handle =
3472 timeout(xpt_release_devq_timeout,
3473 dev,
3474 (crs->release_timeout * hz) / 1000);
3475
3476 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3477
3478 }
3479
3480 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3481
3482 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3483 /*
3484 * Decrement the freeze count so that a single
3485 * completion is still sufficient to unfreeze
3486 * the queue.
3487 */
3488 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3489 } else {
3490
3491 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3492 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3493 }
3494 }
3495
3496 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3497
3498 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3499 || (dev->ccbq.dev_active == 0)) {
3500
3501 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3502 } else {
3503
3504 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3505 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3506 }
3507 }
3508 splx(s);
3509
3510 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3511
3512 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3513 /*run_queue*/TRUE);
3514 }
3515 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3516 start_ccb->ccb_h.status = CAM_REQ_CMP;
3517 break;
3518 }
3519 case XPT_SCAN_BUS:
3520 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3521 break;
3522 case XPT_SCAN_LUN:
3523 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3524 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3525 start_ccb);
3526 break;
3527 case XPT_DEBUG: {
3528#ifdef CAMDEBUG
3529 int s;
3530
3531 s = splcam();
3532#ifdef CAM_DEBUG_DELAY
3533 cam_debug_delay = CAM_DEBUG_DELAY;
3534#endif
3535 cam_dflags = start_ccb->cdbg.flags;
3536 if (cam_dpath != NULL) {
3537 xpt_free_path(cam_dpath);
3538 cam_dpath = NULL;
3539 }
3540
3541 if (cam_dflags != CAM_DEBUG_NONE) {
3542 if (xpt_create_path(&cam_dpath, xpt_periph,
3543 start_ccb->ccb_h.path_id,
3544 start_ccb->ccb_h.target_id,
3545 start_ccb->ccb_h.target_lun) !=
3546 CAM_REQ_CMP) {
3547 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3548 cam_dflags = CAM_DEBUG_NONE;
3549 } else {
3550 start_ccb->ccb_h.status = CAM_REQ_CMP;
3551 xpt_print_path(cam_dpath);
3552 printf("debugging flags now %x\n", cam_dflags);
3553 }
3554 } else {
3555 cam_dpath = NULL;
3556 start_ccb->ccb_h.status = CAM_REQ_CMP;
3557 }
3558 splx(s);
3559#else /* !CAMDEBUG */
3560 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3561#endif /* CAMDEBUG */
3562 break;
3563 }
3564 case XPT_NOOP:
3565 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3566 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3567 start_ccb->ccb_h.status = CAM_REQ_CMP;
3568 break;
3569 default:
3570 case XPT_SDEV_TYPE:
3571 case XPT_TERM_IO:
3572 case XPT_ENG_INQ:
3573 /* XXX Implement */
3574 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3575 break;
3576 }
3577 splx(iopl);
3578}
3579
3580void
3581xpt_polled_action(union ccb *start_ccb)
3582{
3583 int s;
3584 u_int32_t timeout;
3585 struct cam_sim *sim;
3586 struct cam_devq *devq;
3587 struct cam_ed *dev;
3588
3589 timeout = start_ccb->ccb_h.timeout;
3590 sim = start_ccb->ccb_h.path->bus->sim;
3591 devq = sim->devq;
3592 dev = start_ccb->ccb_h.path->device;
3593
3594 s = splcam();
3595
3596 /*
3597 * Steal an opening so that no other queued requests
3598 * can get it before us while we simulate interrupts.
3599 */
3600 dev->ccbq.devq_openings--;
3601 dev->ccbq.dev_openings--;
3602
3603 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3604 && (--timeout > 0)) {
3605 DELAY(1000);
3606 (*(sim->sim_poll))(sim);
3607 camisr(&cam_netq);
3608 camisr(&cam_bioq);
3609 }
3610
3611 dev->ccbq.devq_openings++;
3612 dev->ccbq.dev_openings++;
3613
3614 if (timeout != 0) {
3615 xpt_action(start_ccb);
3616 while(--timeout > 0) {
3617 (*(sim->sim_poll))(sim);
3618 camisr(&cam_netq);
3619 camisr(&cam_bioq);
3620 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3621 != CAM_REQ_INPROG)
3622 break;
3623 DELAY(1000);
3624 }
3625 if (timeout == 0) {
3626 /*
3627 * XXX Is it worth adding a sim_timeout entry
3628 * point so we can attempt recovery? If
3629 * this is only used for dumps, I don't think
3630 * it is.
3631 */
3632 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3633 }
3634 } else {
3635 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3636 }
3637 splx(s);
3638}
3639
3640/*
3641 * Schedule a peripheral driver to receive a ccb when it's
3642 * target device has space for more transactions.
3643 */
3644void
3645xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3646{
3647 struct cam_ed *device;
3648 int s;
3649 int runq;
3650
3651 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3652 device = perph->path->device;
3653 s = splsoftcam();
3654 if (periph_is_queued(perph)) {
3655 /* Simply reorder based on new priority */
3656 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3657 (" change priority to %d\n", new_priority));
3658 if (new_priority < perph->pinfo.priority) {
3659 camq_change_priority(&device->drvq,
3660 perph->pinfo.index,
3661 new_priority);
3662 }
3663 runq = 0;
3664 } else {
3665 /* New entry on the queue */
3666 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3667 (" added periph to queue\n"));
3668 perph->pinfo.priority = new_priority;
3669 perph->pinfo.generation = ++device->drvq.generation;
3670 camq_insert(&device->drvq, &perph->pinfo);
3671 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3672 }
3673 splx(s);
3674 if (runq != 0) {
3675 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3676 (" calling xpt_run_devq\n"));
3677 xpt_run_dev_allocq(perph->path->bus);
3678 }
3679}
3680
3681
3682/*
3683 * Schedule a device to run on a given queue.
3684 * If the device was inserted as a new entry on the queue,
3685 * return 1 meaning the device queue should be run. If we
3686 * were already queued, implying someone else has already
3687 * started the queue, return 0 so the caller doesn't attempt
3688 * to run the queue. Must be run at either splsoftcam
3689 * (or splcam since that encompases splsoftcam).
3690 */
3691static int
3692xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3693 u_int32_t new_priority)
3694{
3695 int retval;
3696 u_int32_t old_priority;
3697
3698 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3699
3700 old_priority = pinfo->priority;
3701
3702 /*
3703 * Are we already queued?
3704 */
3705 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3706 /* Simply reorder based on new priority */
3707 if (new_priority < old_priority) {
3708 camq_change_priority(queue, pinfo->index,
3709 new_priority);
3710 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3711 ("changed priority to %d\n",
3712 new_priority));
3713 }
3714 retval = 0;
3715 } else {
3716 /* New entry on the queue */
3717 if (new_priority < old_priority)
3718 pinfo->priority = new_priority;
3719
3720 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3721 ("Inserting onto queue\n"));
3722 pinfo->generation = ++queue->generation;
3723 camq_insert(queue, pinfo);
3724 retval = 1;
3725 }
3726 return (retval);
3727}
3728
3729static void
3730xpt_run_dev_allocq(struct cam_eb *bus)
3731{
3732 struct cam_devq *devq;
3733 int s;
3734
3735 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3736 devq = bus->sim->devq;
3737
3738 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3739 (" qfrozen_cnt == 0x%x, entries == %d, "
3740 "openings == %d, active == %d\n",
3741 devq->alloc_queue.qfrozen_cnt,
3742 devq->alloc_queue.entries,
3743 devq->alloc_openings,
3744 devq->alloc_active));
3745
3746 s = splsoftcam();
3747 devq->alloc_queue.qfrozen_cnt++;
3748 while ((devq->alloc_queue.entries > 0)
3749 && (devq->alloc_openings > 0)
3750 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3751 struct cam_ed_qinfo *qinfo;
3752 struct cam_ed *device;
3753 union ccb *work_ccb;
3754 struct cam_periph *drv;
3755 struct camq *drvq;
3756
3757 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3758 CAMQ_HEAD);
3759 device = qinfo->device;
3760
3761 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3762 ("running device %p\n", device));
3763
3764 drvq = &device->drvq;
3765
3766#ifdef CAMDEBUG
3767 if (drvq->entries <= 0) {
3768 panic("xpt_run_dev_allocq: "
3769 "Device on queue without any work to do");
3770 }
3771#endif
3772 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3773 devq->alloc_openings--;
3774 devq->alloc_active++;
3775 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3776 splx(s);
3777 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3778 drv->pinfo.priority);
3779 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3780 ("calling periph start\n"));
3781 drv->periph_start(drv, work_ccb);
3782 } else {
3783 /*
3784 * Malloc failure in alloc_ccb
3785 */
3786 /*
3787 * XXX add us to a list to be run from free_ccb
3788 * if we don't have any ccbs active on this
3789 * device queue otherwise we may never get run
3790 * again.
3791 */
3792 break;
3793 }
3794
3795 /* Raise IPL for possible insertion and test at top of loop */
3796 s = splsoftcam();
3797
3798 if (drvq->entries > 0) {
3799 /* We have more work. Attempt to reschedule */
3800 xpt_schedule_dev_allocq(bus, device);
3801 }
3802 }
3803 devq->alloc_queue.qfrozen_cnt--;
3804 splx(s);
3805}
3806
3807static void
3808xpt_run_dev_sendq(struct cam_eb *bus)
3809{
3810 struct cam_devq *devq;
3811 int s;
3812
3813 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3814
3815 devq = bus->sim->devq;
3816
3817 s = splcam();
3818 devq->send_queue.qfrozen_cnt++;
3819 splx(s);
3820 s = splsoftcam();
3821 while ((devq->send_queue.entries > 0)
3822 && (devq->send_openings > 0)) {
3823 struct cam_ed_qinfo *qinfo;
3824 struct cam_ed *device;
3825 union ccb *work_ccb;
3826 struct cam_sim *sim;
3827 int ospl;
3828
3829 ospl = splcam();
3830 if (devq->send_queue.qfrozen_cnt > 1) {
3831 splx(ospl);
3832 break;
3833 }
3834
3835 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3836 CAMQ_HEAD);
3837 device = qinfo->device;
3838
3839 /*
3840 * If the device has been "frozen", don't attempt
3841 * to run it.
3842 */
3843 if (device->qfrozen_cnt > 0) {
3844 splx(ospl);
3845 continue;
3846 }
3847
3848 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3849 ("running device %p\n", device));
3850
3851 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3852 if (work_ccb == NULL) {
3853 printf("device on run queue with no ccbs???\n");
3854 splx(ospl);
3855 continue;
3856 }
3857
3858 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3859
3860 if (num_highpower <= 0) {
3861 /*
3862 * We got a high power command, but we
3863 * don't have any available slots. Freeze
3864 * the device queue until we have a slot
3865 * available.
3866 */
3867 device->qfrozen_cnt++;
3868 STAILQ_INSERT_TAIL(&highpowerq,
3869 &work_ccb->ccb_h,
3870 xpt_links.stqe);
3871
3872 splx(ospl);
3873 continue;
3874 } else {
3875 /*
3876 * Consume a high power slot while
3877 * this ccb runs.
3878 */
3879 num_highpower--;
3880 }
3881 }
3882 devq->active_dev = device;
3883 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3884
3885 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3886 splx(ospl);
3887
3888 devq->send_openings--;
3889 devq->send_active++;
3890
3891 if (device->ccbq.queue.entries > 0)
3892 xpt_schedule_dev_sendq(bus, device);
3893
3894 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3895 /*
3896 * The client wants to freeze the queue
3897 * after this CCB is sent.
3898 */
3899 ospl = splcam();
3900 device->qfrozen_cnt++;
3901 splx(ospl);
3902 }
3903
3904 splx(s);
3905
3906 /* In Target mode, the peripheral driver knows best... */
3907 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3908 if ((device->inq_flags & SID_CmdQue) != 0
3909 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3910 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3911 else
3912 /*
3913 * Clear this in case of a retried CCB that
3914 * failed due to a rejected tag.
3915 */
3916 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3917 }
3918
3919 /*
3920 * Device queues can be shared among multiple sim instances
3921 * that reside on different busses. Use the SIM in the queue
3922 * CCB's path, rather than the one in the bus that was passed
3923 * into this function.
3924 */
3925 sim = work_ccb->ccb_h.path->bus->sim;
3926 (*(sim->sim_action))(sim, work_ccb);
3927
3928 ospl = splcam();
3929 devq->active_dev = NULL;
3930 splx(ospl);
3931 /* Raise IPL for possible insertion and test at top of loop */
3932 s = splsoftcam();
3933 }
3934 splx(s);
3935 s = splcam();
3936 devq->send_queue.qfrozen_cnt--;
3937 splx(s);
3938}
3939
3940/*
3941 * This function merges stuff from the slave ccb into the master ccb, while
3942 * keeping important fields in the master ccb constant.
3943 */
3944void
3945xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3946{
3947 /*
3948 * Pull fields that are valid for peripheral drivers to set
3949 * into the master CCB along with the CCB "payload".
3950 */
3951 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3952 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3953 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3954 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3955 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3956 sizeof(union ccb) - sizeof(struct ccb_hdr));
3957}
3958
3959void
3960xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3961{
3962 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3963 ccb_h->pinfo.priority = priority;
3964 ccb_h->path = path;
3965 ccb_h->path_id = path->bus->path_id;
3966 if (path->target)
3967 ccb_h->target_id = path->target->target_id;
3968 else
3969 ccb_h->target_id = CAM_TARGET_WILDCARD;
3970 if (path->device) {
3971 ccb_h->target_lun = path->device->lun_id;
3972 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3973 } else {
3974 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3975 }
3976 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3977 ccb_h->flags = 0;
3978}
3979
3980/* Path manipulation functions */
3981cam_status
3982xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3983 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3984{
3985 struct cam_path *path;
3986 cam_status status;
3987
3988 path = (struct cam_path *)malloc(sizeof(*path), M_DEVBUF, M_NOWAIT);
3989
3990 if (path == NULL) {
3991 status = CAM_RESRC_UNAVAIL;
3992 return(status);
3993 }
3994 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3995 if (status != CAM_REQ_CMP) {
3996 free(path, M_DEVBUF);
3997 path = NULL;
3998 }
3999 *new_path_ptr = path;
4000 return (status);
4001}
4002
4003static cam_status
4004xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
4005 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4006{
4007 struct cam_eb *bus;
4008 struct cam_et *target;
4009 struct cam_ed *device;
4010 cam_status status;
4011 int s;
4012
4013 status = CAM_REQ_CMP; /* Completed without error */
4014 target = NULL; /* Wildcarded */
4015 device = NULL; /* Wildcarded */
4016
4017 /*
4018 * We will potentially modify the EDT, so block interrupts
4019 * that may attempt to create cam paths.
4020 */
4021 s = splcam();
4022 bus = xpt_find_bus(path_id);
4023 if (bus == NULL) {
4024 status = CAM_PATH_INVALID;
4025 } else {
4026 target = xpt_find_target(bus, target_id);
4027 if (target == NULL) {
4028 /* Create one */
4029 struct cam_et *new_target;
4030
4031 new_target = xpt_alloc_target(bus, target_id);
4032 if (new_target == NULL) {
4033 status = CAM_RESRC_UNAVAIL;
4034 } else {
4035 target = new_target;
4036 }
4037 }
4038 if (target != NULL) {
4039 device = xpt_find_device(target, lun_id);
4040 if (device == NULL) {
4041 /* Create one */
4042 struct cam_ed *new_device;
4043
4044 new_device = xpt_alloc_device(bus,
4045 target,
4046 lun_id);
4047 if (new_device == NULL) {
4048 status = CAM_RESRC_UNAVAIL;
4049 } else {
4050 device = new_device;
4051 }
4052 }
4053 }
4054 }
4055 splx(s);
4056
4057 /*
4058 * Only touch the user's data if we are successful.
4059 */
4060 if (status == CAM_REQ_CMP) {
4061 new_path->periph = perph;
4062 new_path->bus = bus;
4063 new_path->target = target;
4064 new_path->device = device;
4065 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
4066 } else {
4067 if (device != NULL)
4068 xpt_release_device(bus, target, device);
4069 if (target != NULL)
4070 xpt_release_target(bus, target);
4071 if (bus != NULL)
4072 xpt_release_bus(bus);
4073 }
4074 return (status);
4075}
4076
4077static void
4078xpt_release_path(struct cam_path *path)
4079{
4080 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
4081 if (path->device != NULL) {
4082 xpt_release_device(path->bus, path->target, path->device);
4083 path->device = NULL;
4084 }
4085 if (path->target != NULL) {
4086 xpt_release_target(path->bus, path->target);
4087 path->target = NULL;
4088 }
4089 if (path->bus != NULL) {
4090 xpt_release_bus(path->bus);
4091 path->bus = NULL;
4092 }
4093}
4094
4095void
4096xpt_free_path(struct cam_path *path)
4097{
4098 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
4099 xpt_release_path(path);
4100 free(path, M_DEVBUF);
4101}
4102
4103
4104/*
4105 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4106 * in path1, 2 for match with wildcards in path2.
4107 */
4108int
4109xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
4110{
4111 int retval = 0;
4112
4113 if (path1->bus != path2->bus) {
4114 if (path1->bus->path_id == CAM_BUS_WILDCARD)
4115 retval = 1;
4116 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4117 retval = 2;
4118 else
4119 return (-1);
4120 }
4121 if (path1->target != path2->target) {
4122 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4123 if (retval == 0)
4124 retval = 1;
4125 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4126 retval = 2;
4127 else
4128 return (-1);
4129 }
4130 if (path1->device != path2->device) {
4131 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4132 if (retval == 0)
4133 retval = 1;
4134 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4135 retval = 2;
4136 else
4137 return (-1);
4138 }
4139 return (retval);
4140}
4141
4142void
4143xpt_print_path(struct cam_path *path)
4144{
4145 if (path == NULL)
4146 printf("(nopath): ");
4147 else {
4148 if (path->periph != NULL)
4149 printf("(%s%d:", path->periph->periph_name,
4150 path->periph->unit_number);
4151 else
4152 printf("(noperiph:");
4153
4154 if (path->bus != NULL)
4155 printf("%s%d:%d:", path->bus->sim->sim_name,
4156 path->bus->sim->unit_number,
4157 path->bus->sim->bus_id);
4158 else
4159 printf("nobus:");
4160
4161 if (path->target != NULL)
4162 printf("%d:", path->target->target_id);
4163 else
4164 printf("X:");
4165
4166 if (path->device != NULL)
4167 printf("%d): ", path->device->lun_id);
4168 else
4169 printf("X): ");
4170 }
4171}
4172
4173int
4174xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4175{
4176 struct sbuf sb;
4177
4178 sbuf_new(&sb, str, str_len, 0);
4179
4180 if (path == NULL)
4181 sbuf_printf(&sb, "(nopath): ");
4182 else {
4183 if (path->periph != NULL)
4184 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4185 path->periph->unit_number);
4186 else
4187 sbuf_printf(&sb, "(noperiph:");
4188
4189 if (path->bus != NULL)
4190 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
4191 path->bus->sim->unit_number,
4192 path->bus->sim->bus_id);
4193 else
4194 sbuf_printf(&sb, "nobus:");
4195
4196 if (path->target != NULL)
4197 sbuf_printf(&sb, "%d:", path->target->target_id);
4198 else
4199 sbuf_printf(&sb, "X:");
4200
4201 if (path->device != NULL)
4202 sbuf_printf(&sb, "%d): ", path->device->lun_id);
4203 else
4204 sbuf_printf(&sb, "X): ");
4205 }
4206 sbuf_finish(&sb);
4207
4208 return(sbuf_len(&sb));
4209}
4210
4211path_id_t
4212xpt_path_path_id(struct cam_path *path)
4213{
4214 return(path->bus->path_id);
4215}
4216
4217target_id_t
4218xpt_path_target_id(struct cam_path *path)
4219{
4220 if (path->target != NULL)
4221 return (path->target->target_id);
4222 else
4223 return (CAM_TARGET_WILDCARD);
4224}
4225
4226lun_id_t
4227xpt_path_lun_id(struct cam_path *path)
4228{
4229 if (path->device != NULL)
4230 return (path->device->lun_id);
4231 else
4232 return (CAM_LUN_WILDCARD);
4233}
4234
4235struct cam_sim *
4236xpt_path_sim(struct cam_path *path)
4237{
4238 return (path->bus->sim);
4239}
4240
4241struct cam_periph*
4242xpt_path_periph(struct cam_path *path)
4243{
4244 return (path->periph);
4245}
4246
4247/*
4248 * Release a CAM control block for the caller. Remit the cost of the structure
4249 * to the device referenced by the path. If the this device had no 'credits'
4250 * and peripheral drivers have registered async callbacks for this notification
4251 * call them now.
4252 */
4253void
4254xpt_release_ccb(union ccb *free_ccb)
4255{
4256 int s;
4257 struct cam_path *path;
4258 struct cam_ed *device;
4259 struct cam_eb *bus;
4260
4261 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4262 path = free_ccb->ccb_h.path;
4263 device = path->device;
4264 bus = path->bus;
4265 s = splsoftcam();
4266 cam_ccbq_release_opening(&device->ccbq);
4267 if (xpt_ccb_count > xpt_max_ccbs) {
4268 xpt_free_ccb(free_ccb);
4269 xpt_ccb_count--;
4270 } else {
4271 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4272 }
4273 bus->sim->devq->alloc_openings++;
4274 bus->sim->devq->alloc_active--;
4275 /* XXX Turn this into an inline function - xpt_run_device?? */
4276 if ((device_is_alloc_queued(device) == 0)
4277 && (device->drvq.entries > 0)) {
4278 xpt_schedule_dev_allocq(bus, device);
4279 }
4280 splx(s);
4281 if (dev_allocq_is_runnable(bus->sim->devq))
4282 xpt_run_dev_allocq(bus);
4283}
4284
4285/* Functions accessed by SIM drivers */
4286
4287/*
4288 * A sim structure, listing the SIM entry points and instance
4289 * identification info is passed to xpt_bus_register to hook the SIM
4290 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4291 * for this new bus and places it in the array of busses and assigns
4292 * it a path_id. The path_id may be influenced by "hard wiring"
4293 * information specified by the user. Once interrupt services are
4294 * availible, the bus will be probed.
4295 */
4296int32_t
4297xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4298{
4299 struct cam_eb *new_bus;
4300 struct cam_eb *old_bus;
4301 struct ccb_pathinq cpi;
4302 int s;
4303
4304 sim->bus_id = bus;
4305 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4306 M_DEVBUF, M_NOWAIT);
4307 if (new_bus == NULL) {
4308 /* Couldn't satisfy request */
4309 return (CAM_RESRC_UNAVAIL);
4310 }
4311
4312 if (strcmp(sim->sim_name, "xpt") != 0) {
4313
4314 sim->path_id =
4315 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4316 }
4317
4318 TAILQ_INIT(&new_bus->et_entries);
4319 new_bus->path_id = sim->path_id;
4320 new_bus->sim = sim;
4321 timevalclear(&new_bus->last_reset);
4322 new_bus->flags = 0;
4323 new_bus->refcount = 1; /* Held until a bus_deregister event */
4324 new_bus->generation = 0;
4325 s = splcam();
4326 old_bus = TAILQ_FIRST(&xpt_busses);
4327 while (old_bus != NULL
4328 && old_bus->path_id < new_bus->path_id)
4329 old_bus = TAILQ_NEXT(old_bus, links);
4330 if (old_bus != NULL)
4331 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4332 else
4333 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4334 bus_generation++;
4335 splx(s);
4336
4337 /* Notify interested parties */
4338 if (sim->path_id != CAM_XPT_PATH_ID) {
4339 struct cam_path path;
4340
4341 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4342 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4343 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4344 cpi.ccb_h.func_code = XPT_PATH_INQ;
4345 xpt_action((union ccb *)&cpi);
4346 xpt_async(AC_PATH_REGISTERED, &path, &cpi);
4347 xpt_release_path(&path);
4348 }
4349 return (CAM_SUCCESS);
4350}
4351
4352int32_t
4353xpt_bus_deregister(path_id_t pathid)
4354{
4355 struct cam_path bus_path;
4356 cam_status status;
4357
4358 status = xpt_compile_path(&bus_path, NULL, pathid,
4359 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4360 if (status != CAM_REQ_CMP)
4361 return (status);
4362
4363 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4364 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4365
4366 /* Release the reference count held while registered. */
4367 xpt_release_bus(bus_path.bus);
4368 xpt_release_path(&bus_path);
4369
4370 return (CAM_REQ_CMP);
4371}
4372
4373static path_id_t
4374xptnextfreepathid(void)
4375{
4376 struct cam_eb *bus;
4377 path_id_t pathid;
4378 const char *strval;
4379
4380 pathid = 0;
4381 bus = TAILQ_FIRST(&xpt_busses);
4382retry:
4383 /* Find an unoccupied pathid */
4384 while (bus != NULL
4385 && bus->path_id <= pathid) {
4386 if (bus->path_id == pathid)
4387 pathid++;
4388 bus = TAILQ_NEXT(bus, links);
4389 }
4390
4391 /*
4392 * Ensure that this pathid is not reserved for
4393 * a bus that may be registered in the future.
4394 */
4395 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4396 ++pathid;
4397 /* Start the search over */
4398 goto retry;
4399 }
4400 return (pathid);
4401}
4402
4403static path_id_t
4404xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4405{
4406 path_id_t pathid;
4407 int i, dunit, val;
4408 char buf[32];
4409 const char *dname;
4410
4411 pathid = CAM_XPT_PATH_ID;
4412 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4413 i = 0;
4414 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4415 if (strcmp(dname, "scbus")) {
4416 /* Avoid a bit of foot shooting. */
4417 continue;
4418 }
4419 if (dunit < 0) /* unwired?! */
4420 continue;
4421 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4422 if (sim_bus == val) {
4423 pathid = dunit;
4424 break;
4425 }
4426 } else if (sim_bus == 0) {
4427 /* Unspecified matches bus 0 */
4428 pathid = dunit;
4429 break;
4430 } else {
4431 printf("Ambiguous scbus configuration for %s%d "
4432 "bus %d, cannot wire down. The kernel "
4433 "config entry for scbus%d should "
4434 "specify a controller bus.\n"
4435 "Scbus will be assigned dynamically.\n",
4436 sim_name, sim_unit, sim_bus, dunit);
4437 break;
4438 }
4439 }
4440
4441 if (pathid == CAM_XPT_PATH_ID)
4442 pathid = xptnextfreepathid();
4443 return (pathid);
4444}
4445
4446void
4447xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4448{
4449 struct cam_eb *bus;
4450 struct cam_et *target, *next_target;
4451 struct cam_ed *device, *next_device;
4452 int s;
4453
4454 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4455
4456 /*
4457 * Most async events come from a CAM interrupt context. In
4458 * a few cases, the error recovery code at the peripheral layer,
4459 * which may run from our SWI or a process context, may signal
4460 * deferred events with a call to xpt_async. Ensure async
4461 * notifications are serialized by blocking cam interrupts.
4462 */
4463 s = splcam();
4464
4465 bus = path->bus;
4466
4467 if (async_code == AC_BUS_RESET) {
4468 int s;
4469
4470 s = splclock();
4471 /* Update our notion of when the last reset occurred */
4472 microtime(&bus->last_reset);
4473 splx(s);
4474 }
4475
4476 for (target = TAILQ_FIRST(&bus->et_entries);
4477 target != NULL;
4478 target = next_target) {
4479
4480 next_target = TAILQ_NEXT(target, links);
4481
4482 if (path->target != target
4483 && path->target->target_id != CAM_TARGET_WILDCARD
4484 && target->target_id != CAM_TARGET_WILDCARD)
4485 continue;
4486
4487 if (async_code == AC_SENT_BDR) {
4488 int s;
4489
4490 /* Update our notion of when the last reset occurred */
4491 s = splclock();
4492 microtime(&path->target->last_reset);
4493 splx(s);
4494 }
4495
4496 for (device = TAILQ_FIRST(&target->ed_entries);
4497 device != NULL;
4498 device = next_device) {
4499
4500 next_device = TAILQ_NEXT(device, links);
4501
4502 if (path->device != device
4503 && path->device->lun_id != CAM_LUN_WILDCARD
4504 && device->lun_id != CAM_LUN_WILDCARD)
4505 continue;
4506
4507 xpt_dev_async(async_code, bus, target,
4508 device, async_arg);
4509
4510 xpt_async_bcast(&device->asyncs, async_code,
4511 path, async_arg);
4512 }
4513 }
4514
4515 /*
4516 * If this wasn't a fully wildcarded async, tell all
4517 * clients that want all async events.
4518 */
4519 if (bus != xpt_periph->path->bus)
4520 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4521 path, async_arg);
4522 splx(s);
4523}
4524
4525static void
4526xpt_async_bcast(struct async_list *async_head,
4527 u_int32_t async_code,
4528 struct cam_path *path, void *async_arg)
4529{
4530 struct async_node *cur_entry;
4531
4532 cur_entry = SLIST_FIRST(async_head);
4533 while (cur_entry != NULL) {
4534 struct async_node *next_entry;
4535 /*
4536 * Grab the next list entry before we call the current
4537 * entry's callback. This is because the callback function
4538 * can delete its async callback entry.
4539 */
4540 next_entry = SLIST_NEXT(cur_entry, links);
4541 if ((cur_entry->event_enable & async_code) != 0)
4542 cur_entry->callback(cur_entry->callback_arg,
4543 async_code, path,
4544 async_arg);
4545 cur_entry = next_entry;
4546 }
4547}
4548
4549/*
4550 * Handle any per-device event notifications that require action by the XPT.
4551 */
4552static void
4553xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4554 struct cam_ed *device, void *async_arg)
4555{
4556 cam_status status;
4557 struct cam_path newpath;
4558
4559 /*
4560 * We only need to handle events for real devices.
4561 */
4562 if (target->target_id == CAM_TARGET_WILDCARD
4563 || device->lun_id == CAM_LUN_WILDCARD)
4564 return;
4565
4566 /*
4567 * We need our own path with wildcards expanded to
4568 * handle certain types of events.
4569 */
4570 if ((async_code == AC_SENT_BDR)
4571 || (async_code == AC_BUS_RESET)
4572 || (async_code == AC_INQ_CHANGED))
4573 status = xpt_compile_path(&newpath, NULL,
4574 bus->path_id,
4575 target->target_id,
4576 device->lun_id);
4577 else
4578 status = CAM_REQ_CMP_ERR;
4579
4580 if (status == CAM_REQ_CMP) {
4581
4582 /*
4583 * Allow transfer negotiation to occur in a
4584 * tag free environment.
4585 */
4586 if (async_code == AC_SENT_BDR
4587 || async_code == AC_BUS_RESET)
4588 xpt_toggle_tags(&newpath);
4589
4590 if (async_code == AC_INQ_CHANGED) {
4591 /*
4592 * We've sent a start unit command, or
4593 * something similar to a device that
4594 * may have caused its inquiry data to
4595 * change. So we re-scan the device to
4596 * refresh the inquiry data for it.
4597 */
4598 xpt_scan_lun(newpath.periph, &newpath,
4599 CAM_EXPECT_INQ_CHANGE, NULL);
4600 }
4601 xpt_release_path(&newpath);
4602 } else if (async_code == AC_LOST_DEVICE) {
4603 device->flags |= CAM_DEV_UNCONFIGURED;
4604 } else if (async_code == AC_TRANSFER_NEG) {
4605 struct ccb_trans_settings *settings;
4606
4607 settings = (struct ccb_trans_settings *)async_arg;
4608 xpt_set_transfer_settings(settings, device,
4609 /*async_update*/TRUE);
4610 }
4611}
4612
4613u_int32_t
4614xpt_freeze_devq(struct cam_path *path, u_int count)
4615{
4616 int s;
4617 struct ccb_hdr *ccbh;
4618
4619 s = splcam();
4620 path->device->qfrozen_cnt += count;
4621
4622 /*
4623 * Mark the last CCB in the queue as needing
4624 * to be requeued if the driver hasn't
4625 * changed it's state yet. This fixes a race
4626 * where a ccb is just about to be queued to
4627 * a controller driver when it's interrupt routine
4628 * freezes the queue. To completly close the
4629 * hole, controller drives must check to see
4630 * if a ccb's status is still CAM_REQ_INPROG
4631 * under spl protection just before they queue
4632 * the CCB. See ahc_action/ahc_freeze_devq for
4633 * an example.
4634 */
4635 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4636 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4637 ccbh->status = CAM_REQUEUE_REQ;
4638 splx(s);
4639 return (path->device->qfrozen_cnt);
4640}
4641
4642u_int32_t
4643xpt_freeze_simq(struct cam_sim *sim, u_int count)
4644{
4645 sim->devq->send_queue.qfrozen_cnt += count;
4646 if (sim->devq->active_dev != NULL) {
4647 struct ccb_hdr *ccbh;
4648
4649 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4650 ccb_hdr_tailq);
4651 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4652 ccbh->status = CAM_REQUEUE_REQ;
4653 }
4654 return (sim->devq->send_queue.qfrozen_cnt);
4655}
4656
4657static void
4658xpt_release_devq_timeout(void *arg)
4659{
4660 struct cam_ed *device;
4661
4662 device = (struct cam_ed *)arg;
4663
4664 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4665}
4666
4667void
4668xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4669{
4670 xpt_release_devq_device(path->device, count, run_queue);
4671}
4672
4673static void
4674xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4675{
4676 int rundevq;
4677 int s0, s1;
4678
4679 rundevq = 0;
4680 s0 = splsoftcam();
4681 s1 = splcam();
4682 if (dev->qfrozen_cnt > 0) {
4683
4684 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4685 dev->qfrozen_cnt -= count;
4686 if (dev->qfrozen_cnt == 0) {
4687
4688 /*
4689 * No longer need to wait for a successful
4690 * command completion.
4691 */
4692 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4693
4694 /*
4695 * Remove any timeouts that might be scheduled
4696 * to release this queue.
4697 */
4698 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4699 untimeout(xpt_release_devq_timeout, dev,
4700 dev->c_handle);
4701 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4702 }
4703
4704 /*
4705 * Now that we are unfrozen schedule the
4706 * device so any pending transactions are
4707 * run.
4708 */
4709 if ((dev->ccbq.queue.entries > 0)
4710 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4711 && (run_queue != 0)) {
4712 rundevq = 1;
4713 }
4714 }
4715 }
4716 splx(s1);
4717 if (rundevq != 0)
4718 xpt_run_dev_sendq(dev->target->bus);
4719 splx(s0);
4720}
4721
4722void
4723xpt_release_simq(struct cam_sim *sim, int run_queue)
4724{
4725 int s;
4726 struct camq *sendq;
4727
4728 sendq = &(sim->devq->send_queue);
4729 s = splcam();
4730 if (sendq->qfrozen_cnt > 0) {
4731
4732 sendq->qfrozen_cnt--;
4733 if (sendq->qfrozen_cnt == 0) {
4734 struct cam_eb *bus;
4735
4736 /*
4737 * If there is a timeout scheduled to release this
4738 * sim queue, remove it. The queue frozen count is
4739 * already at 0.
4740 */
4741 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4742 untimeout(xpt_release_simq_timeout, sim,
4743 sim->c_handle);
4744 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4745 }
4746 bus = xpt_find_bus(sim->path_id);
4747 splx(s);
4748
4749 if (run_queue) {
4750 /*
4751 * Now that we are unfrozen run the send queue.
4752 */
4753 xpt_run_dev_sendq(bus);
4754 }
4755 xpt_release_bus(bus);
4756 } else
4757 splx(s);
4758 } else
4759 splx(s);
4760}
4761
4762static void
4763xpt_release_simq_timeout(void *arg)
4764{
4765 struct cam_sim *sim;
4766
4767 sim = (struct cam_sim *)arg;
4768 xpt_release_simq(sim, /* run_queue */ TRUE);
4769}
4770
4771void
4772xpt_done(union ccb *done_ccb)
4773{
4774 int s;
4775
4776 s = splcam();
4777
4778 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4779 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4780 /*
4781 * Queue up the request for handling by our SWI handler
4782 * any of the "non-immediate" type of ccbs.
4783 */
4784 switch (done_ccb->ccb_h.path->periph->type) {
4785 case CAM_PERIPH_BIO:
4786 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4787 sim_links.tqe);
4788 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4789 swi_sched(cambio_ih, 0);
4790 break;
4791 case CAM_PERIPH_NET:
4792 TAILQ_INSERT_TAIL(&cam_netq, &done_ccb->ccb_h,
4793 sim_links.tqe);
4794 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4795 swi_sched(camnet_ih, 0);
4796 break;
4797 }
4798 }
4799 splx(s);
4800}
4801
4802union ccb *
4803xpt_alloc_ccb()
4804{
4805 union ccb *new_ccb;
4806
4807 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_WAITOK);
4808 return (new_ccb);
4809}
4810
4811void
4812xpt_free_ccb(union ccb *free_ccb)
4813{
4814 free(free_ccb, M_DEVBUF);
4815}
4816
4817
4818
4819/* Private XPT functions */
4820
4821/*
4822 * Get a CAM control block for the caller. Charge the structure to the device
4823 * referenced by the path. If the this device has no 'credits' then the
4824 * device already has the maximum number of outstanding operations under way
4825 * and we return NULL. If we don't have sufficient resources to allocate more
4826 * ccbs, we also return NULL.
4827 */
4828static union ccb *
4829xpt_get_ccb(struct cam_ed *device)
4830{
4831 union ccb *new_ccb;
4832 int s;
4833
4834 s = splsoftcam();
4835 if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
4836 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_NOWAIT);
4837 if (new_ccb == NULL) {
4838 splx(s);
4839 return (NULL);
4840 }
4841 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4842 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4843 xpt_links.sle);
4844 xpt_ccb_count++;
4845 }
4846 cam_ccbq_take_opening(&device->ccbq);
4847 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4848 splx(s);
4849 return (new_ccb);
4850}
4851
4852static void
4853xpt_release_bus(struct cam_eb *bus)
4854{
4855 int s;
4856
4857 s = splcam();
4858 if ((--bus->refcount == 0)
4859 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4860 TAILQ_REMOVE(&xpt_busses, bus, links);
4861 bus_generation++;
4862 splx(s);
4863 free(bus, M_DEVBUF);
4864 } else
4865 splx(s);
4866}
4867
4868static struct cam_et *
4869xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4870{
4871 struct cam_et *target;
4872
4873 target = (struct cam_et *)malloc(sizeof(*target), M_DEVBUF, M_NOWAIT);
4874 if (target != NULL) {
4875 struct cam_et *cur_target;
4876
4877 TAILQ_INIT(&target->ed_entries);
4878 target->bus = bus;
4879 target->target_id = target_id;
4880 target->refcount = 1;
4881 target->generation = 0;
4882 timevalclear(&target->last_reset);
4883 /*
4884 * Hold a reference to our parent bus so it
4885 * will not go away before we do.
4886 */
4887 bus->refcount++;
4888
4889 /* Insertion sort into our bus's target list */
4890 cur_target = TAILQ_FIRST(&bus->et_entries);
4891 while (cur_target != NULL && cur_target->target_id < target_id)
4892 cur_target = TAILQ_NEXT(cur_target, links);
4893
4894 if (cur_target != NULL) {
4895 TAILQ_INSERT_BEFORE(cur_target, target, links);
4896 } else {
4897 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4898 }
4899 bus->generation++;
4900 }
4901 return (target);
4902}
4903
4904static void
4905xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4906{
4907 int s;
4908
4909 s = splcam();
4910 if ((--target->refcount == 0)
4911 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4912 TAILQ_REMOVE(&bus->et_entries, target, links);
4913 bus->generation++;
4914 splx(s);
4915 free(target, M_DEVBUF);
4916 xpt_release_bus(bus);
4917 } else
4918 splx(s);
4919}
4920
4921static struct cam_ed *
4922xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4923{
4924#ifdef CAM_NEW_TRAN_CODE
4925 struct cam_path path;
4926#endif /* CAM_NEW_TRAN_CODE */
4927 struct cam_ed *device;
4928 struct cam_devq *devq;
4929 cam_status status;
4930
4931 /* Make space for us in the device queue on our bus */
4932 devq = bus->sim->devq;
4933 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4934
4935 if (status != CAM_REQ_CMP) {
4936 device = NULL;
4937 } else {
4938 device = (struct cam_ed *)malloc(sizeof(*device),
4939 M_DEVBUF, M_NOWAIT);
4940 }
4941
4942 if (device != NULL) {
4943 struct cam_ed *cur_device;
4944
4945 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4946 device->alloc_ccb_entry.device = device;
4947 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4948 device->send_ccb_entry.device = device;
4949 device->target = target;
4950 device->lun_id = lun_id;
4951 /* Initialize our queues */
4952 if (camq_init(&device->drvq, 0) != 0) {
4953 free(device, M_DEVBUF);
4954 return (NULL);
4955 }
4956 if (cam_ccbq_init(&device->ccbq,
4957 bus->sim->max_dev_openings) != 0) {
4958 camq_fini(&device->drvq);
4959 free(device, M_DEVBUF);
4960 return (NULL);
4961 }
4962 SLIST_INIT(&device->asyncs);
4963 SLIST_INIT(&device->periphs);
4964 device->generation = 0;
4965 device->owner = NULL;
4966 /*
4967 * Take the default quirk entry until we have inquiry
4968 * data and can determine a better quirk to use.
4969 */
4970 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
4971 bzero(&device->inq_data, sizeof(device->inq_data));
4972 device->inq_flags = 0;
4973 device->queue_flags = 0;
4974 device->serial_num = NULL;
4975 device->serial_num_len = 0;
4976 device->qfrozen_cnt = 0;
4977 device->flags = CAM_DEV_UNCONFIGURED;
4978 device->tag_delay_count = 0;
4979 device->refcount = 1;
4980 callout_handle_init(&device->c_handle);
4981
4982 /*
4983 * Hold a reference to our parent target so it
4984 * will not go away before we do.
4985 */
4986 target->refcount++;
4987
4988 /*
4989 * XXX should be limited by number of CCBs this bus can
4990 * do.
4991 */
4992 xpt_max_ccbs += device->ccbq.devq_openings;
4993 /* Insertion sort into our target's device list */
4994 cur_device = TAILQ_FIRST(&target->ed_entries);
4995 while (cur_device != NULL && cur_device->lun_id < lun_id)
4996 cur_device = TAILQ_NEXT(cur_device, links);
4997 if (cur_device != NULL) {
4998 TAILQ_INSERT_BEFORE(cur_device, device, links);
4999 } else {
5000 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
5001 }
5002 target->generation++;
5003#ifdef CAM_NEW_TRAN_CODE
5004 if (lun_id != CAM_LUN_WILDCARD) {
5005 xpt_compile_path(&path,
5006 NULL,
5007 bus->path_id,
5008 target->target_id,
5009 lun_id);
5010 xpt_devise_transport(&path);
5011 xpt_release_path(&path);
5012 }
5013#endif /* CAM_NEW_TRAN_CODE */
5014 }
5015 return (device);
5016}
5017
5018static void
5019xpt_release_device(struct cam_eb *bus, struct cam_et *target,
5020 struct cam_ed *device)
5021{
5022 int s;
5023
5024 s = splcam();
5025 if ((--device->refcount == 0)
5026 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
5027 struct cam_devq *devq;
5028
5029 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
5030 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
5031 panic("Removing device while still queued for ccbs");
5032
5033 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
5034 untimeout(xpt_release_devq_timeout, device,
5035 device->c_handle);
5036
5037 TAILQ_REMOVE(&target->ed_entries, device,links);
5038 target->generation++;
5039 xpt_max_ccbs -= device->ccbq.devq_openings;
5040 /* Release our slot in the devq */
5041 devq = bus->sim->devq;
5042 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
5043 splx(s);
5044 free(device, M_DEVBUF);
5045 xpt_release_target(bus, target);
5046 } else
5047 splx(s);
5048}
5049
5050static u_int32_t
5051xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5052{
5053 int s;
5054 int diff;
5055 int result;
5056 struct cam_ed *dev;
5057
5058 dev = path->device;
5059 s = splsoftcam();
5060
5061 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
5062 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5063 if (result == CAM_REQ_CMP && (diff < 0)) {
5064 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
5065 }
5066 /* Adjust the global limit */
5067 xpt_max_ccbs += diff;
5068 splx(s);
5069 return (result);
5070}
5071
5072static struct cam_eb *
5073xpt_find_bus(path_id_t path_id)
5074{
5075 struct cam_eb *bus;
5076
5077 for (bus = TAILQ_FIRST(&xpt_busses);
5078 bus != NULL;
5079 bus = TAILQ_NEXT(bus, links)) {
5080 if (bus->path_id == path_id) {
5081 bus->refcount++;
5082 break;
5083 }
5084 }
5085 return (bus);
5086}
5087
5088static struct cam_et *
5089xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5090{
5091 struct cam_et *target;
5092
5093 for (target = TAILQ_FIRST(&bus->et_entries);
5094 target != NULL;
5095 target = TAILQ_NEXT(target, links)) {
5096 if (target->target_id == target_id) {
5097 target->refcount++;
5098 break;
5099 }
5100 }
5101 return (target);
5102}
5103
5104static struct cam_ed *
5105xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5106{
5107 struct cam_ed *device;
5108
5109 for (device = TAILQ_FIRST(&target->ed_entries);
5110 device != NULL;
5111 device = TAILQ_NEXT(device, links)) {
5112 if (device->lun_id == lun_id) {
5113 device->refcount++;
5114 break;
5115 }
5116 }
5117 return (device);
5118}
5119
5120typedef struct {
5121 union ccb *request_ccb;
5122 struct ccb_pathinq *cpi;
5123 int pending_count;
5124} xpt_scan_bus_info;
5125
5126/*
5127 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5128 * As the scan progresses, xpt_scan_bus is used as the
5129 * callback on completion function.
5130 */
5131static void
5132xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
5133{
5134 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5135 ("xpt_scan_bus\n"));
5136 switch (request_ccb->ccb_h.func_code) {
5137 case XPT_SCAN_BUS:
5138 {
5139 xpt_scan_bus_info *scan_info;
5140 union ccb *work_ccb;
5141 struct cam_path *path;
5142 u_int i;
5143 u_int max_target;
5144 u_int initiator_id;
5145
5146 /* Find out the characteristics of the bus */
5147 work_ccb = xpt_alloc_ccb();
5148 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
5149 request_ccb->ccb_h.pinfo.priority);
5150 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5151 xpt_action(work_ccb);
5152 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5153 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
5154 xpt_free_ccb(work_ccb);
5155 xpt_done(request_ccb);
5156 return;
5157 }
5158
5159 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5160 /*
5161 * Can't scan the bus on an adapter that
5162 * cannot perform the initiator role.
5163 */
5164 request_ccb->ccb_h.status = CAM_REQ_CMP;
5165 xpt_free_ccb(work_ccb);
5166 xpt_done(request_ccb);
5167 return;
5168 }
5169
5170 /* Save some state for use while we probe for devices */
5171 scan_info = (xpt_scan_bus_info *)
5172 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
5173 scan_info->request_ccb = request_ccb;
5174 scan_info->cpi = &work_ccb->cpi;
5175
5176 /* Cache on our stack so we can work asynchronously */
5177 max_target = scan_info->cpi->max_target;
5178 initiator_id = scan_info->cpi->initiator_id;
5179
5180 /*
5181 * Don't count the initiator if the
5182 * initiator is addressable.
5183 */
5184 scan_info->pending_count = max_target + 1;
5185 if (initiator_id <= max_target)
5186 scan_info->pending_count--;
5187
5188 for (i = 0; i <= max_target; i++) {
5189 cam_status status;
5190 if (i == initiator_id)
5191 continue;
5192
5193 status = xpt_create_path(&path, xpt_periph,
5194 request_ccb->ccb_h.path_id,
5195 i, 0);
5196 if (status != CAM_REQ_CMP) {
5197 printf("xpt_scan_bus: xpt_create_path failed"
5198 " with status %#x, bus scan halted\n",
5199 status);
5200 break;
5201 }
5202 work_ccb = xpt_alloc_ccb();
5203 xpt_setup_ccb(&work_ccb->ccb_h, path,
5204 request_ccb->ccb_h.pinfo.priority);
5205 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5206 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5207 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
5208 work_ccb->crcn.flags = request_ccb->crcn.flags;
5209 xpt_action(work_ccb);
5210 }
5211 break;
5212 }
5213 case XPT_SCAN_LUN:
5214 {
5215 xpt_scan_bus_info *scan_info;
5216 path_id_t path_id;
5217 target_id_t target_id;
5218 lun_id_t lun_id;
5219
5220 /* Reuse the same CCB to query if a device was really found */
5221 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
5222 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
5223 request_ccb->ccb_h.pinfo.priority);
5224 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5225
5226 path_id = request_ccb->ccb_h.path_id;
5227 target_id = request_ccb->ccb_h.target_id;
5228 lun_id = request_ccb->ccb_h.target_lun;
5229 xpt_action(request_ccb);
5230
5231 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5232 struct cam_ed *device;
5233 struct cam_et *target;
5234 int s, phl;
5235
5236 /*
5237 * If we already probed lun 0 successfully, or
5238 * we have additional configured luns on this
5239 * target that might have "gone away", go onto
5240 * the next lun.
5241 */
5242 target = request_ccb->ccb_h.path->target;
5243 /*
5244 * We may touch devices that we don't
5245 * hold references too, so ensure they
5246 * don't disappear out from under us.
5247 * The target above is referenced by the
5248 * path in the request ccb.
5249 */
5250 phl = 0;
5251 s = splcam();
5252 device = TAILQ_FIRST(&target->ed_entries);
5253 if (device != NULL) {
5254 phl = device->quirk->quirks & CAM_QUIRK_HILUNS;
5255 if (device->lun_id == 0)
5256 device = TAILQ_NEXT(device, links);
5257 }
5258 splx(s);
5259 if ((lun_id != 0) || (device != NULL)) {
5260 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5261 lun_id++;
5262 }
5263 } else {
5264 struct cam_ed *device;
5265
5266 device = request_ccb->ccb_h.path->device;
5267
5268 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5269 /* Try the next lun */
5270 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
5271 (device->quirk->quirks & CAM_QUIRK_HILUNS))
5272 lun_id++;
5273 }
5274 }
5275
5276 xpt_free_path(request_ccb->ccb_h.path);
5277
5278 /* Check Bounds */
5279 if ((lun_id == request_ccb->ccb_h.target_lun)
5280 || lun_id > scan_info->cpi->max_lun) {
5281 /* We're done */
5282
5283 xpt_free_ccb(request_ccb);
5284 scan_info->pending_count--;
5285 if (scan_info->pending_count == 0) {
5286 xpt_free_ccb((union ccb *)scan_info->cpi);
5287 request_ccb = scan_info->request_ccb;
5288 free(scan_info, M_TEMP);
5289 request_ccb->ccb_h.status = CAM_REQ_CMP;
5290 xpt_done(request_ccb);
5291 }
5292 } else {
5293 /* Try the next device */
5294 struct cam_path *path;
5295 cam_status status;
5296
5297 path = request_ccb->ccb_h.path;
5298 status = xpt_create_path(&path, xpt_periph,
5299 path_id, target_id, lun_id);
5300 if (status != CAM_REQ_CMP) {
5301 printf("xpt_scan_bus: xpt_create_path failed "
5302 "with status %#x, halting LUN scan\n",
5303 status);
5304 xpt_free_ccb(request_ccb);
5305 scan_info->pending_count--;
5306 if (scan_info->pending_count == 0) {
5307 xpt_free_ccb(
5308 (union ccb *)scan_info->cpi);
5309 request_ccb = scan_info->request_ccb;
5310 free(scan_info, M_TEMP);
5311 request_ccb->ccb_h.status = CAM_REQ_CMP;
5312 xpt_done(request_ccb);
5313 break;
5314 }
5315 }
5316 xpt_setup_ccb(&request_ccb->ccb_h, path,
5317 request_ccb->ccb_h.pinfo.priority);
5318 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5319 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5320 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5321 request_ccb->crcn.flags =
5322 scan_info->request_ccb->crcn.flags;
5323 xpt_action(request_ccb);
5324 }
5325 break;
5326 }
5327 default:
5328 break;
5329 }
5330}
5331
5332typedef enum {
5333 PROBE_TUR,
5334 PROBE_INQUIRY,
5335 PROBE_FULL_INQUIRY,
5336 PROBE_MODE_SENSE,
5337 PROBE_SERIAL_NUM,
5338 PROBE_TUR_FOR_NEGOTIATION
5339} probe_action;
5340
5341typedef enum {
5342 PROBE_INQUIRY_CKSUM = 0x01,
5343 PROBE_SERIAL_CKSUM = 0x02,
5344 PROBE_NO_ANNOUNCE = 0x04
5345} probe_flags;
5346
5347typedef struct {
5348 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5349 probe_action action;
5350 union ccb saved_ccb;
5351 probe_flags flags;
5352 MD5_CTX context;
5353 u_int8_t digest[16];
5354} probe_softc;
5355
5356static void
5357xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5358 cam_flags flags, union ccb *request_ccb)
5359{
5360 struct ccb_pathinq cpi;
5361 cam_status status;
5362 struct cam_path *new_path;
5363 struct cam_periph *old_periph;
5364 int s;
5365
5366 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5367 ("xpt_scan_lun\n"));
5368
5369 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5370 cpi.ccb_h.func_code = XPT_PATH_INQ;
5371 xpt_action((union ccb *)&cpi);
5372
5373 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5374 if (request_ccb != NULL) {
5375 request_ccb->ccb_h.status = cpi.ccb_h.status;
5376 xpt_done(request_ccb);
5377 }
5378 return;
5379 }
5380
5381 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5382 /*
5383 * Can't scan the bus on an adapter that
5384 * cannot perform the initiator role.
5385 */
5386 if (request_ccb != NULL) {
5387 request_ccb->ccb_h.status = CAM_REQ_CMP;
5388 xpt_done(request_ccb);
5389 }
5390 return;
5391 }
5392
5393 if (request_ccb == NULL) {
5394 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5395 if (request_ccb == NULL) {
5396 xpt_print_path(path);
5397 printf("xpt_scan_lun: can't allocate CCB, can't "
5398 "continue\n");
5399 return;
5400 }
5401 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5402 if (new_path == NULL) {
5403 xpt_print_path(path);
5404 printf("xpt_scan_lun: can't allocate path, can't "
5405 "continue\n");
5406 free(request_ccb, M_TEMP);
5407 return;
5408 }
5409 status = xpt_compile_path(new_path, xpt_periph,
5410 path->bus->path_id,
5411 path->target->target_id,
5412 path->device->lun_id);
5413
5414 if (status != CAM_REQ_CMP) {
5415 xpt_print_path(path);
5416 printf("xpt_scan_lun: can't compile path, can't "
5417 "continue\n");
5418 free(request_ccb, M_TEMP);
5419 free(new_path, M_TEMP);
5420 return;
5421 }
5422 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5423 request_ccb->ccb_h.cbfcnp = xptscandone;
5424 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5425 request_ccb->crcn.flags = flags;
5426 }
5427
5428 s = splsoftcam();
5429 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5430 probe_softc *softc;
5431
5432 softc = (probe_softc *)old_periph->softc;
5433 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5434 periph_links.tqe);
5435 } else {
5436 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5437 probestart, "probe",
5438 CAM_PERIPH_BIO,
5439 request_ccb->ccb_h.path, NULL, 0,
5440 request_ccb);
5441
5442 if (status != CAM_REQ_CMP) {
5443 xpt_print_path(path);
5444 printf("xpt_scan_lun: cam_alloc_periph returned an "
5445 "error, can't continue probe\n");
5446 request_ccb->ccb_h.status = status;
5447 xpt_done(request_ccb);
5448 }
5449 }
5450 splx(s);
5451}
5452
5453static void
5454xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5455{
5456 xpt_release_path(done_ccb->ccb_h.path);
5457 free(done_ccb->ccb_h.path, M_TEMP);
5458 free(done_ccb, M_TEMP);
5459}
5460
5461static cam_status
5462proberegister(struct cam_periph *periph, void *arg)
5463{
5464 union ccb *request_ccb; /* CCB representing the probe request */
5465 probe_softc *softc;
5466
5467 request_ccb = (union ccb *)arg;
5468 if (periph == NULL) {
5469 printf("proberegister: periph was NULL!!\n");
5470 return(CAM_REQ_CMP_ERR);
5471 }
5472
5473 if (request_ccb == NULL) {
5474 printf("proberegister: no probe CCB, "
5475 "can't register device\n");
5476 return(CAM_REQ_CMP_ERR);
5477 }
5478
5479 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5480
5481 if (softc == NULL) {
5482 printf("proberegister: Unable to probe new device. "
5483 "Unable to allocate softc\n");
5484 return(CAM_REQ_CMP_ERR);
5485 }
5486 TAILQ_INIT(&softc->request_ccbs);
5487 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5488 periph_links.tqe);
5489 softc->flags = 0;
5490 periph->softc = softc;
5491 cam_periph_acquire(periph);
5492 /*
5493 * Ensure we've waited at least a bus settle
5494 * delay before attempting to probe the device.
5495 * For HBAs that don't do bus resets, this won't make a difference.
5496 */
5497 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5498 scsi_delay);
5499 probeschedule(periph);
5500 return(CAM_REQ_CMP);
5501}
5502
5503static void
5504probeschedule(struct cam_periph *periph)
5505{
5506 struct ccb_pathinq cpi;
5507 union ccb *ccb;
5508 probe_softc *softc;
5509
5510 softc = (probe_softc *)periph->softc;
5511 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5512
5513 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5514 cpi.ccb_h.func_code = XPT_PATH_INQ;
5515 xpt_action((union ccb *)&cpi);
5516
5517 /*
5518 * If a device has gone away and another device, or the same one,
5519 * is back in the same place, it should have a unit attention
5520 * condition pending. It will not report the unit attention in
5521 * response to an inquiry, which may leave invalid transfer
5522 * negotiations in effect. The TUR will reveal the unit attention
5523 * condition. Only send the TUR for lun 0, since some devices
5524 * will get confused by commands other than inquiry to non-existent
5525 * luns. If you think a device has gone away start your scan from
5526 * lun 0. This will insure that any bogus transfer settings are
5527 * invalidated.
5528 *
5529 * If we haven't seen the device before and the controller supports
5530 * some kind of transfer negotiation, negotiate with the first
5531 * sent command if no bus reset was performed at startup. This
5532 * ensures that the device is not confused by transfer negotiation
5533 * settings left over by loader or BIOS action.
5534 */
5535 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5536 && (ccb->ccb_h.target_lun == 0)) {
5537 softc->action = PROBE_TUR;
5538 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5539 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5540 proberequestdefaultnegotiation(periph);
5541 softc->action = PROBE_INQUIRY;
5542 } else {
5543 softc->action = PROBE_INQUIRY;
5544 }
5545
5546 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5547 softc->flags |= PROBE_NO_ANNOUNCE;
5548 else
5549 softc->flags &= ~PROBE_NO_ANNOUNCE;
5550
5551 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5552}
5553
5554static void
5555probestart(struct cam_periph *periph, union ccb *start_ccb)
5556{
5557 /* Probe the device that our peripheral driver points to */
5558 struct ccb_scsiio *csio;
5559 probe_softc *softc;
5560
5561 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5562
5563 softc = (probe_softc *)periph->softc;
5564 csio = &start_ccb->csio;
5565
5566 switch (softc->action) {
5567 case PROBE_TUR:
5568 case PROBE_TUR_FOR_NEGOTIATION:
5569 {
5570 scsi_test_unit_ready(csio,
5571 /*retries*/4,
5572 probedone,
5573 MSG_SIMPLE_Q_TAG,
5574 SSD_FULL_SIZE,
5575 /*timeout*/60000);
5576 break;
5577 }
5578 case PROBE_INQUIRY:
5579 case PROBE_FULL_INQUIRY:
5580 {
5581 u_int inquiry_len;
5582 struct scsi_inquiry_data *inq_buf;
5583
5584 inq_buf = &periph->path->device->inq_data;
5585 /*
5586 * If the device is currently configured, we calculate an
5587 * MD5 checksum of the inquiry data, and if the serial number
5588 * length is greater than 0, add the serial number data
5589 * into the checksum as well. Once the inquiry and the
5590 * serial number check finish, we attempt to figure out
5591 * whether we still have the same device.
5592 */
5593 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5594
5595 MD5Init(&softc->context);
5596 MD5Update(&softc->context, (unsigned char *)inq_buf,
5597 sizeof(struct scsi_inquiry_data));
5598 softc->flags |= PROBE_INQUIRY_CKSUM;
5599 if (periph->path->device->serial_num_len > 0) {
5600 MD5Update(&softc->context,
5601 periph->path->device->serial_num,
5602 periph->path->device->serial_num_len);
5603 softc->flags |= PROBE_SERIAL_CKSUM;
5604 }
5605 MD5Final(softc->digest, &softc->context);
5606 }
5607
5608 if (softc->action == PROBE_INQUIRY)
5609 inquiry_len = SHORT_INQUIRY_LENGTH;
5610 else
5611 inquiry_len = inq_buf->additional_length + 4;
5612
5613 scsi_inquiry(csio,
5614 /*retries*/4,
5615 probedone,
5616 MSG_SIMPLE_Q_TAG,
5617 (u_int8_t *)inq_buf,
5618 inquiry_len,
5619 /*evpd*/FALSE,
5620 /*page_code*/0,
5621 SSD_MIN_SIZE,
5622 /*timeout*/60 * 1000);
5623 break;
5624 }
5625 case PROBE_MODE_SENSE:
5626 {
5627 void *mode_buf;
5628 int mode_buf_len;
5629
5630 mode_buf_len = sizeof(struct scsi_mode_header_6)
5631 + sizeof(struct scsi_mode_blk_desc)
5632 + sizeof(struct scsi_control_page);
5633 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5634 if (mode_buf != NULL) {
5635 scsi_mode_sense(csio,
5636 /*retries*/4,
5637 probedone,
5638 MSG_SIMPLE_Q_TAG,
5639 /*dbd*/FALSE,
5640 SMS_PAGE_CTRL_CURRENT,
5641 SMS_CONTROL_MODE_PAGE,
5642 mode_buf,
5643 mode_buf_len,
5644 SSD_FULL_SIZE,
5645 /*timeout*/60000);
5646 break;
5647 }
5648 xpt_print_path(periph->path);
5649 printf("Unable to mode sense control page - malloc failure\n");
5650 softc->action = PROBE_SERIAL_NUM;
5651 /* FALLTHROUGH */
5652 }
5653 case PROBE_SERIAL_NUM:
5654 {
5655 struct scsi_vpd_unit_serial_number *serial_buf;
5656 struct cam_ed* device;
5657
5658 serial_buf = NULL;
5659 device = periph->path->device;
5660 device->serial_num = NULL;
5661 device->serial_num_len = 0;
5662
5663 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5664 serial_buf = (struct scsi_vpd_unit_serial_number *)
5665 malloc(sizeof(*serial_buf), M_TEMP,
5666 M_NOWAIT | M_ZERO);
5667
5668 if (serial_buf != NULL) {
5669 scsi_inquiry(csio,
5670 /*retries*/4,
5671 probedone,
5672 MSG_SIMPLE_Q_TAG,
5673 (u_int8_t *)serial_buf,
5674 sizeof(*serial_buf),
5675 /*evpd*/TRUE,
5676 SVPD_UNIT_SERIAL_NUMBER,
5677 SSD_MIN_SIZE,
5678 /*timeout*/60 * 1000);
5679 break;
5680 }
5681 /*
5682 * We'll have to do without, let our probedone
5683 * routine finish up for us.
5684 */
5685 start_ccb->csio.data_ptr = NULL;
5686 probedone(periph, start_ccb);
5687 return;
5688 }
5689 }
5690 xpt_action(start_ccb);
5691}
5692
5693static void
5694proberequestdefaultnegotiation(struct cam_periph *periph)
5695{
5696 struct ccb_trans_settings cts;
5697
5698 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5699 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5700#ifdef CAM_NEW_TRAN_CODE
5701 cts.type = CTS_TYPE_USER_SETTINGS;
5702#else /* CAM_NEW_TRAN_CODE */
5703 cts.flags = CCB_TRANS_USER_SETTINGS;
5704#endif /* CAM_NEW_TRAN_CODE */
5705 xpt_action((union ccb *)&cts);
5706 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5707#ifdef CAM_NEW_TRAN_CODE
5708 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5709#else /* CAM_NEW_TRAN_CODE */
5710 cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5711 cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5712#endif /* CAM_NEW_TRAN_CODE */
5713 xpt_action((union ccb *)&cts);
5714}
5715
5716static void
5717probedone(struct cam_periph *periph, union ccb *done_ccb)
5718{
5719 probe_softc *softc;
5720 struct cam_path *path;
5721 u_int32_t priority;
5722
5723 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5724
5725 softc = (probe_softc *)periph->softc;
5726 path = done_ccb->ccb_h.path;
5727 priority = done_ccb->ccb_h.pinfo.priority;
5728
5729 switch (softc->action) {
5730 case PROBE_TUR:
5731 {
5732 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5733
5734 if (cam_periph_error(done_ccb, 0,
5735 SF_NO_PRINT, NULL) == ERESTART)
5736 return;
5737 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5738 /* Don't wedge the queue */
5739 xpt_release_devq(done_ccb->ccb_h.path,
5740 /*count*/1,
5741 /*run_queue*/TRUE);
5742 }
5743 softc->action = PROBE_INQUIRY;
5744 xpt_release_ccb(done_ccb);
5745 xpt_schedule(periph, priority);
5746 return;
5747 }
5748 case PROBE_INQUIRY:
5749 case PROBE_FULL_INQUIRY:
5750 {
5751 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5752 struct scsi_inquiry_data *inq_buf;
5753 u_int8_t periph_qual;
5754
5755 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5756 inq_buf = &path->device->inq_data;
5757
5758 periph_qual = SID_QUAL(inq_buf);
5759
5760 switch(periph_qual) {
5761 case SID_QUAL_LU_CONNECTED:
5762 {
5763 u_int8_t alen;
5764
5765 /*
5766 * We conservatively request only
5767 * SHORT_INQUIRY_LEN bytes of inquiry
5768 * information during our first try
5769 * at sending an INQUIRY. If the device
5770 * has more information to give,
5771 * perform a second request specifying
5772 * the amount of information the device
5773 * is willing to give.
5774 */
5775 alen = inq_buf->additional_length;
5776 if (softc->action == PROBE_INQUIRY
5777 && alen > (SHORT_INQUIRY_LENGTH - 4)) {
5778 softc->action = PROBE_FULL_INQUIRY;
5779 xpt_release_ccb(done_ccb);
5780 xpt_schedule(periph, priority);
5781 return;
5782 }
5783
5784 xpt_find_quirk(path->device);
5785
5786#ifdef CAM_NEW_TRAN_CODE
5787 xpt_devise_transport(path);
5788#endif /* CAM_NEW_TRAN_CODE */
5789 if ((inq_buf->flags & SID_CmdQue) != 0)
5790 softc->action = PROBE_MODE_SENSE;
5791 else
5792 softc->action = PROBE_SERIAL_NUM;
5793
5794 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5795
5796 xpt_release_ccb(done_ccb);
5797 xpt_schedule(periph, priority);
5798 return;
5799 }
5800 default:
5801 break;
5802 }
5803 } else if (cam_periph_error(done_ccb, 0,
5804 done_ccb->ccb_h.target_lun > 0
5805 ? SF_RETRY_UA|SF_QUIET_IR
5806 : SF_RETRY_UA,
5807 &softc->saved_ccb) == ERESTART) {
5808 return;
5809 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5810 /* Don't wedge the queue */
5811 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5812 /*run_queue*/TRUE);
5813 }
5814 /*
5815 * If we get to this point, we got an error status back
5816 * from the inquiry and the error status doesn't require
5817 * automatically retrying the command. Therefore, the
5818 * inquiry failed. If we had inquiry information before
5819 * for this device, but this latest inquiry command failed,
5820 * the device has probably gone away. If this device isn't
5821 * already marked unconfigured, notify the peripheral
5822 * drivers that this device is no more.
5823 */
5824 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5825 /* Send the async notification. */
5826 xpt_async(AC_LOST_DEVICE, path, NULL);
5827
5828 xpt_release_ccb(done_ccb);
5829 break;
5830 }
5831 case PROBE_MODE_SENSE:
5832 {
5833 struct ccb_scsiio *csio;
5834 struct scsi_mode_header_6 *mode_hdr;
5835
5836 csio = &done_ccb->csio;
5837 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5838 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5839 struct scsi_control_page *page;
5840 u_int8_t *offset;
5841
5842 offset = ((u_int8_t *)&mode_hdr[1])
5843 + mode_hdr->blk_desc_len;
5844 page = (struct scsi_control_page *)offset;
5845 path->device->queue_flags = page->queue_flags;
5846 } else if (cam_periph_error(done_ccb, 0,
5847 SF_RETRY_UA|SF_NO_PRINT,
5848 &softc->saved_ccb) == ERESTART) {
5849 return;
5850 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5851 /* Don't wedge the queue */
5852 xpt_release_devq(done_ccb->ccb_h.path,
5853 /*count*/1, /*run_queue*/TRUE);
5854 }
5855 xpt_release_ccb(done_ccb);
5856 free(mode_hdr, M_TEMP);
5857 softc->action = PROBE_SERIAL_NUM;
5858 xpt_schedule(periph, priority);
5859 return;
5860 }
5861 case PROBE_SERIAL_NUM:
5862 {
5863 struct ccb_scsiio *csio;
5864 struct scsi_vpd_unit_serial_number *serial_buf;
5865 u_int32_t priority;
5866 int changed;
5867 int have_serialnum;
5868
5869 changed = 1;
5870 have_serialnum = 0;
5871 csio = &done_ccb->csio;
5872 priority = done_ccb->ccb_h.pinfo.priority;
5873 serial_buf =
5874 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5875
5876 /* Clean up from previous instance of this device */
5877 if (path->device->serial_num != NULL) {
5878 free(path->device->serial_num, M_DEVBUF);
5879 path->device->serial_num = NULL;
5880 path->device->serial_num_len = 0;
5881 }
5882
5883 if (serial_buf == NULL) {
5884 /*
5885 * Don't process the command as it was never sent
5886 */
5887 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5888 && (serial_buf->length > 0)) {
5889
5890 have_serialnum = 1;
5891 path->device->serial_num =
5892 (u_int8_t *)malloc((serial_buf->length + 1),
5893 M_DEVBUF, M_NOWAIT);
5894 if (path->device->serial_num != NULL) {
5895 bcopy(serial_buf->serial_num,
5896 path->device->serial_num,
5897 serial_buf->length);
5898 path->device->serial_num_len =
5899 serial_buf->length;
5900 path->device->serial_num[serial_buf->length]
5901 = '\0';
5902 }
5903 } else if (cam_periph_error(done_ccb, 0,
5904 SF_RETRY_UA|SF_NO_PRINT,
5905 &softc->saved_ccb) == ERESTART) {
5906 return;
5907 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5908 /* Don't wedge the queue */
5909 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5910 /*run_queue*/TRUE);
5911 }
5912
5913 /*
5914 * Let's see if we have seen this device before.
5915 */
5916 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
5917 MD5_CTX context;
5918 u_int8_t digest[16];
5919
5920 MD5Init(&context);
5921
5922 MD5Update(&context,
5923 (unsigned char *)&path->device->inq_data,
5924 sizeof(struct scsi_inquiry_data));
5925
5926 if (have_serialnum)
5927 MD5Update(&context, serial_buf->serial_num,
5928 serial_buf->length);
5929
5930 MD5Final(digest, &context);
5931 if (bcmp(softc->digest, digest, 16) == 0)
5932 changed = 0;
5933
5934 /*
5935 * XXX Do we need to do a TUR in order to ensure
5936 * that the device really hasn't changed???
5937 */
5938 if ((changed != 0)
5939 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
5940 xpt_async(AC_LOST_DEVICE, path, NULL);
5941 }
5942 if (serial_buf != NULL)
5943 free(serial_buf, M_TEMP);
5944
5945 if (changed != 0) {
5946 /*
5947 * Now that we have all the necessary
5948 * information to safely perform transfer
5949 * negotiations... Controllers don't perform
5950 * any negotiation or tagged queuing until
5951 * after the first XPT_SET_TRAN_SETTINGS ccb is
5952 * received. So, on a new device, just retreive
5953 * the user settings, and set them as the current
5954 * settings to set the device up.
5955 */
5956 proberequestdefaultnegotiation(periph);
5957 xpt_release_ccb(done_ccb);
5958
5959 /*
5960 * Perform a TUR to allow the controller to
5961 * perform any necessary transfer negotiation.
5962 */
5963 softc->action = PROBE_TUR_FOR_NEGOTIATION;
5964 xpt_schedule(periph, priority);
5965 return;
5966 }
5967 xpt_release_ccb(done_ccb);
5968 break;
5969 }
5970 case PROBE_TUR_FOR_NEGOTIATION:
5971 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5972 /* Don't wedge the queue */
5973 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5974 /*run_queue*/TRUE);
5975 }
5976
5977 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5978
5979 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
5980 /* Inform the XPT that a new device has been found */
5981 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5982 xpt_action(done_ccb);
5983
5984 xpt_async(AC_FOUND_DEVICE, xpt_periph->path, done_ccb);
5985 }
5986 xpt_release_ccb(done_ccb);
5987 break;
5988 }
5989 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5990 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
5991 done_ccb->ccb_h.status = CAM_REQ_CMP;
5992 xpt_done(done_ccb);
5993 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
5994 cam_periph_invalidate(periph);
5995 cam_periph_release(periph);
5996 } else {
5997 probeschedule(periph);
5998 }
5999}
6000
6001static void
6002probecleanup(struct cam_periph *periph)
6003{
6004 free(periph->softc, M_TEMP);
6005}
6006
6007static void
6008xpt_find_quirk(struct cam_ed *device)
6009{
6010 caddr_t match;
6011
6012 match = cam_quirkmatch((caddr_t)&device->inq_data,
6013 (caddr_t)xpt_quirk_table,
6014 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
6015 sizeof(*xpt_quirk_table), scsi_inquiry_match);
6016
6017 if (match == NULL)
6018 panic("xpt_find_quirk: device didn't match wildcard entry!!");
6019
6020 device->quirk = (struct xpt_quirk_entry *)match;
6021}
6022
6023#ifdef CAM_NEW_TRAN_CODE
6024
6025static void
6026xpt_devise_transport(struct cam_path *path)
6027{
6028 struct ccb_pathinq cpi;
6029 struct ccb_trans_settings cts;
6030 struct scsi_inquiry_data *inq_buf;
6031
6032 /* Get transport information from the SIM */
6033 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
6034 cpi.ccb_h.func_code = XPT_PATH_INQ;
6035 xpt_action((union ccb *)&cpi);
6036
6037 inq_buf = NULL;
6038 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
6039 inq_buf = &path->device->inq_data;
6040 path->device->protocol = PROTO_SCSI;
6041 path->device->protocol_version =
6042 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
6043 path->device->transport = cpi.transport;
6044 path->device->transport_version = cpi.transport_version;
6045
6046 /*
6047 * Any device not using SPI3 features should
6048 * be considered SPI2 or lower.
6049 */
6050 if (inq_buf != NULL) {
6051 if (path->device->transport == XPORT_SPI
6052 && (inq_buf->spi3data & SID_SPI_MASK) == 0
6053 && path->device->transport_version > 2)
6054 path->device->transport_version = 2;
6055 } else {
6056 struct cam_ed* otherdev;
6057
6058 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6059 otherdev != NULL;
6060 otherdev = TAILQ_NEXT(otherdev, links)) {
6061 if (otherdev != path->device)
6062 break;
6063 }
6064
6065 if (otherdev != NULL) {
6066 /*
6067 * Initially assume the same versioning as
6068 * prior luns for this target.
6069 */
6070 path->device->protocol_version =
6071 otherdev->protocol_version;
6072 path->device->transport_version =
6073 otherdev->transport_version;
6074 } else {
6075 /* Until we know better, opt for safty */
6076 path->device->protocol_version = 2;
6077 if (path->device->transport == XPORT_SPI)
6078 path->device->transport_version = 2;
6079 else
6080 path->device->transport_version = 0;
6081 }
6082 }
6083
6084 /*
6085 * XXX
6086 * For a device compliant with SPC-2 we should be able
6087 * to determine the transport version supported by
6088 * scrutinizing the version descriptors in the
6089 * inquiry buffer.
6090 */
6091
6092 /* Tell the controller what we think */
6093 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
6094 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6095 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6096 cts.transport = path->device->transport;
6097 cts.transport_version = path->device->transport_version;
6098 cts.protocol = path->device->protocol;
6099 cts.protocol_version = path->device->protocol_version;
6100 cts.proto_specific.valid = 0;
6101 cts.xport_specific.valid = 0;
6102 xpt_action((union ccb *)&cts);
6103}
6104
6105static void
6106xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6107 int async_update)
6108{
6109 struct ccb_pathinq cpi;
6110 struct ccb_trans_settings cur_cts;
6111 struct ccb_trans_settings_scsi *scsi;
6112 struct ccb_trans_settings_scsi *cur_scsi;
6113 struct cam_sim *sim;
6114 struct scsi_inquiry_data *inq_data;
6115
6116 if (device == NULL) {
6117 cts->ccb_h.status = CAM_PATH_INVALID;
6118 xpt_done((union ccb *)cts);
6119 return;
6120 }
6121
6122 if (cts->protocol == PROTO_UNKNOWN
6123 || cts->protocol == PROTO_UNSPECIFIED) {
6124 cts->protocol = device->protocol;
6125 cts->protocol_version = device->protocol_version;
6126 }
6127
6128 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
6129 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
6130 cts->protocol_version = device->protocol_version;
6131
6132 if (cts->protocol != device->protocol) {
6133 xpt_print_path(cts->ccb_h.path);
6134 printf("Uninitialized Protocol %x:%x?\n",
6135 cts->protocol, device->protocol);
6136 cts->protocol = device->protocol;
6137 }
6138
6139 if (cts->protocol_version > device->protocol_version) {
6140 if (bootverbose) {
6141 xpt_print_path(cts->ccb_h.path);
6142 printf("Down reving Protocol Version from %d to %d?\n",
6143 cts->protocol_version, device->protocol_version);
6144 }
6145 cts->protocol_version = device->protocol_version;
6146 }
6147
6148 if (cts->transport == XPORT_UNKNOWN
6149 || cts->transport == XPORT_UNSPECIFIED) {
6150 cts->transport = device->transport;
6151 cts->transport_version = device->transport_version;
6152 }
6153
6154 if (cts->transport_version == XPORT_VERSION_UNKNOWN
6155 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
6156 cts->transport_version = device->transport_version;
6157
6158 if (cts->transport != device->transport) {
6159 xpt_print_path(cts->ccb_h.path);
6160 printf("Uninitialized Transport %x:%x?\n",
6161 cts->transport, device->transport);
6162 cts->transport = device->transport;
6163 }
6164
6165 if (cts->transport_version > device->transport_version) {
6166 if (bootverbose) {
6167 xpt_print_path(cts->ccb_h.path);
6168 printf("Down reving Transport Version from %d to %d?\n",
6169 cts->transport_version,
6170 device->transport_version);
6171 }
6172 cts->transport_version = device->transport_version;
6173 }
6174
6175 sim = cts->ccb_h.path->bus->sim;
6176
6177 /*
6178 * Nothing more of interest to do unless
6179 * this is a device connected via the
6180 * SCSI protocol.
6181 */
6182 if (cts->protocol != PROTO_SCSI) {
6183 if (async_update == FALSE)
6184 (*(sim->sim_action))(sim, (union ccb *)cts);
6185 return;
6186 }
6187
6188 inq_data = &device->inq_data;
6189 scsi = &cts->proto_specific.scsi;
6190 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6191 cpi.ccb_h.func_code = XPT_PATH_INQ;
6192 xpt_action((union ccb *)&cpi);
6193
6194 /* SCSI specific sanity checking */
6195 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6196 || (inq_data->flags & SID_CmdQue) == 0
6197 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6198 || (device->quirk->mintags == 0)) {
6199 /*
6200 * Can't tag on hardware that doesn't support tags,
6201 * doesn't have it enabled, or has broken tag support.
6202 */
6203 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6204 }
6205
6206 if (async_update == FALSE) {
6207 /*
6208 * Perform sanity checking against what the
6209 * controller and device can do.
6210 */
6211 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6212 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6213 cur_cts.type = cts->type;
6214 xpt_action((union ccb *)&cur_cts);
6215
6216 cur_scsi = &cur_cts.proto_specific.scsi;
6217 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
6218 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6219 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
6220 }
6221 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
6222 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6223 }
6224
6225 /* SPI specific sanity checking */
6226 if (cts->transport == XPORT_SPI && async_update == FALSE) {
6227 u_int spi3caps;
6228 struct ccb_trans_settings_spi *spi;
6229 struct ccb_trans_settings_spi *cur_spi;
6230
6231 spi = &cts->xport_specific.spi;
6232
6233 cur_spi = &cur_cts.xport_specific.spi;
6234
6235 /* Fill in any gaps in what the user gave us */
6236 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6237 spi->sync_period = cur_spi->sync_period;
6238 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6239 spi->sync_period = 0;
6240 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6241 spi->sync_offset = cur_spi->sync_offset;
6242 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6243 spi->sync_offset = 0;
6244 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6245 spi->ppr_options = cur_spi->ppr_options;
6246 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6247 spi->ppr_options = 0;
6248 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6249 spi->bus_width = cur_spi->bus_width;
6250 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6251 spi->bus_width = 0;
6252 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
6253 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6254 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
6255 }
6256 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
6257 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6258 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6259 && (inq_data->flags & SID_Sync) == 0
6260 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6261 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6262 || (cur_spi->sync_offset == 0)
6263 || (cur_spi->sync_period == 0)) {
6264 /* Force async */
6265 spi->sync_period = 0;
6266 spi->sync_offset = 0;
6267 }
6268
6269 switch (spi->bus_width) {
6270 case MSG_EXT_WDTR_BUS_32_BIT:
6271 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6272 || (inq_data->flags & SID_WBus32) != 0
6273 || cts->type == CTS_TYPE_USER_SETTINGS)
6274 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6275 break;
6276 /* Fall Through to 16-bit */
6277 case MSG_EXT_WDTR_BUS_16_BIT:
6278 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6279 || (inq_data->flags & SID_WBus16) != 0
6280 || cts->type == CTS_TYPE_USER_SETTINGS)
6281 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6282 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6283 break;
6284 }
6285 /* Fall Through to 8-bit */
6286 default: /* New bus width?? */
6287 case MSG_EXT_WDTR_BUS_8_BIT:
6288 /* All targets can do this */
6289 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6290 break;
6291 }
6292
6293 spi3caps = cpi.xport_specific.spi.ppr_options;
6294 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6295 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6296 spi3caps &= inq_data->spi3data;
6297
6298 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
6299 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
6300
6301 if ((spi3caps & SID_SPI_IUS) == 0)
6302 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
6303
6304 if ((spi3caps & SID_SPI_QAS) == 0)
6305 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
6306
6307 /* No SPI Transfer settings are allowed unless we are wide */
6308 if (spi->bus_width == 0)
6309 spi->ppr_options = 0;
6310
6311 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
6312 /*
6313 * Can't tag queue without disconnection.
6314 */
6315 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6316 scsi->valid |= CTS_SCSI_VALID_TQ;
6317 }
6318
6319 /*
6320 * If we are currently performing tagged transactions to
6321 * this device and want to change its negotiation parameters,
6322 * go non-tagged for a bit to give the controller a chance to
6323 * negotiate unhampered by tag messages.
6324 */
6325 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6326 && (device->inq_flags & SID_CmdQue) != 0
6327 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6328 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
6329 CTS_SPI_VALID_SYNC_OFFSET|
6330 CTS_SPI_VALID_BUS_WIDTH)) != 0)
6331 xpt_toggle_tags(cts->ccb_h.path);
6332 }
6333
6334 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6335 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
6336 int device_tagenb;
6337
6338 /*
6339 * If we are transitioning from tags to no-tags or
6340 * vice-versa, we need to carefully freeze and restart
6341 * the queue so that we don't overlap tagged and non-tagged
6342 * commands. We also temporarily stop tags if there is
6343 * a change in transfer negotiation settings to allow
6344 * "tag-less" negotiation.
6345 */
6346 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6347 || (device->inq_flags & SID_CmdQue) != 0)
6348 device_tagenb = TRUE;
6349 else
6350 device_tagenb = FALSE;
6351
6352 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6353 && device_tagenb == FALSE)
6354 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
6355 && device_tagenb == TRUE)) {
6356
6357 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
6358 /*
6359 * Delay change to use tags until after a
6360 * few commands have gone to this device so
6361 * the controller has time to perform transfer
6362 * negotiations without tagged messages getting
6363 * in the way.
6364 */
6365 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6366 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6367 } else {
6368 struct ccb_relsim crs;
6369
6370 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6371 device->inq_flags &= ~SID_CmdQue;
6372 xpt_dev_ccbq_resize(cts->ccb_h.path,
6373 sim->max_dev_openings);
6374 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6375 device->tag_delay_count = 0;
6376
6377 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6378 /*priority*/1);
6379 crs.ccb_h.func_code = XPT_REL_SIMQ;
6380 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6381 crs.openings
6382 = crs.release_timeout
6383 = crs.qfrozen_cnt
6384 = 0;
6385 xpt_action((union ccb *)&crs);
6386 }
6387 }
6388 }
6389 if (async_update == FALSE)
6390 (*(sim->sim_action))(sim, (union ccb *)cts);
6391}
6392
6393#else /* CAM_NEW_TRAN_CODE */
6394
6395static void
6396xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6397 int async_update)
6398{
6399 struct cam_sim *sim;
6400 int qfrozen;
6401
6402 sim = cts->ccb_h.path->bus->sim;
6403 if (async_update == FALSE) {
6404 struct scsi_inquiry_data *inq_data;
6405 struct ccb_pathinq cpi;
6406 struct ccb_trans_settings cur_cts;
6407
6408 if (device == NULL) {
6409 cts->ccb_h.status = CAM_PATH_INVALID;
6410 xpt_done((union ccb *)cts);
6411 return;
6412 }
6413
6414 /*
6415 * Perform sanity checking against what the
6416 * controller and device can do.
6417 */
6418 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6419 cpi.ccb_h.func_code = XPT_PATH_INQ;
6420 xpt_action((union ccb *)&cpi);
6421 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6422 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6423 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
6424 xpt_action((union ccb *)&cur_cts);
6425 inq_data = &device->inq_data;
6426
6427 /* Fill in any gaps in what the user gave us */
6428 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
6429 cts->sync_period = cur_cts.sync_period;
6430 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
6431 cts->sync_offset = cur_cts.sync_offset;
6432 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
6433 cts->bus_width = cur_cts.bus_width;
6434 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
6435 cts->flags &= ~CCB_TRANS_DISC_ENB;
6436 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
6437 }
6438 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
6439 cts->flags &= ~CCB_TRANS_TAG_ENB;
6440 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
6441 }
6442
6443 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6444 && (inq_data->flags & SID_Sync) == 0)
6445 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6446 || (cts->sync_offset == 0)
6447 || (cts->sync_period == 0)) {
6448 /* Force async */
6449 cts->sync_period = 0;
6450 cts->sync_offset = 0;
6451 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6452 && (inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
6453 && cts->sync_period <= 0x9) {
6454 /*
6455 * Don't allow DT transmission rates if the
6456 * device does not support it.
6457 */
6458 cts->sync_period = 0xa;
6459 }
6460
6461 switch (cts->bus_width) {
6462 case MSG_EXT_WDTR_BUS_32_BIT:
6463 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6464 || (inq_data->flags & SID_WBus32) != 0)
6465 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6466 break;
6467 /* FALLTHROUGH to 16-bit */
6468 case MSG_EXT_WDTR_BUS_16_BIT:
6469 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6470 || (inq_data->flags & SID_WBus16) != 0)
6471 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6472 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6473 break;
6474 }
6475 /* FALLTHROUGH to 8-bit */
6476 default: /* New bus width?? */
6477 case MSG_EXT_WDTR_BUS_8_BIT:
6478 /* All targets can do this */
6479 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6480 break;
6481 }
6482
6483 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
6484 /*
6485 * Can't tag queue without disconnection.
6486 */
6487 cts->flags &= ~CCB_TRANS_TAG_ENB;
6488 cts->valid |= CCB_TRANS_TQ_VALID;
6489 }
6490
6491 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6492 || (inq_data->flags & SID_CmdQue) == 0
6493 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6494 || (device->quirk->mintags == 0)) {
6495 /*
6496 * Can't tag on hardware that doesn't support,
6497 * doesn't have it enabled, or has broken tag support.
6498 */
6499 cts->flags &= ~CCB_TRANS_TAG_ENB;
6500 }
6501 }
6502
6503 qfrozen = FALSE;
6504 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
6505 int device_tagenb;
6506
6507 /*
6508 * If we are transitioning from tags to no-tags or
6509 * vice-versa, we need to carefully freeze and restart
6510 * the queue so that we don't overlap tagged and non-tagged
6511 * commands. We also temporarily stop tags if there is
6512 * a change in transfer negotiation settings to allow
6513 * "tag-less" negotiation.
6514 */
6515 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6516 || (device->inq_flags & SID_CmdQue) != 0)
6517 device_tagenb = TRUE;
6518 else
6519 device_tagenb = FALSE;
6520
6521 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
6522 && device_tagenb == FALSE)
6523 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
6524 && device_tagenb == TRUE)) {
6525
6526 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
6527 /*
6528 * Delay change to use tags until after a
6529 * few commands have gone to this device so
6530 * the controller has time to perform transfer
6531 * negotiations without tagged messages getting
6532 * in the way.
6533 */
6534 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6535 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6536 } else {
6537 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6538 qfrozen = TRUE;
6539 device->inq_flags &= ~SID_CmdQue;
6540 xpt_dev_ccbq_resize(cts->ccb_h.path,
6541 sim->max_dev_openings);
6542 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6543 device->tag_delay_count = 0;
6544 }
6545 }
6546 }
6547
6548 if (async_update == FALSE) {
6549 /*
6550 * If we are currently performing tagged transactions to
6551 * this device and want to change its negotiation parameters,
6552 * go non-tagged for a bit to give the controller a chance to
6553 * negotiate unhampered by tag messages.
6554 */
6555 if ((device->inq_flags & SID_CmdQue) != 0
6556 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
6557 CCB_TRANS_SYNC_OFFSET_VALID|
6558 CCB_TRANS_BUS_WIDTH_VALID)) != 0)
6559 xpt_toggle_tags(cts->ccb_h.path);
6560
6561 (*(sim->sim_action))(sim, (union ccb *)cts);
6562 }
6563
6564 if (qfrozen) {
6565 struct ccb_relsim crs;
6566
6567 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6568 /*priority*/1);
6569 crs.ccb_h.func_code = XPT_REL_SIMQ;
6570 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6571 crs.openings
6572 = crs.release_timeout
6573 = crs.qfrozen_cnt
6574 = 0;
6575 xpt_action((union ccb *)&crs);
6576 }
6577}
6578
6579
6580#endif /* CAM_NEW_TRAN_CODE */
6581
6582static void
6583xpt_toggle_tags(struct cam_path *path)
6584{
6585 struct cam_ed *dev;
6586
6587 /*
6588 * Give controllers a chance to renegotiate
6589 * before starting tag operations. We
6590 * "toggle" tagged queuing off then on
6591 * which causes the tag enable command delay
6592 * counter to come into effect.
6593 */
6594 dev = path->device;
6595 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6596 || ((dev->inq_flags & SID_CmdQue) != 0
6597 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6598 struct ccb_trans_settings cts;
6599
6600 xpt_setup_ccb(&cts.ccb_h, path, 1);
6601#ifdef CAM_NEW_TRAN_CODE
6602 cts.protocol = PROTO_SCSI;
6603 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
6604 cts.transport = XPORT_UNSPECIFIED;
6605 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
6606 cts.proto_specific.scsi.flags = 0;
6607 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
6608#else /* CAM_NEW_TRAN_CODE */
6609 cts.flags = 0;
6610 cts.valid = CCB_TRANS_TQ_VALID;
6611#endif /* CAM_NEW_TRAN_CODE */
6612 xpt_set_transfer_settings(&cts, path->device,
6613 /*async_update*/TRUE);
6614#ifdef CAM_NEW_TRAN_CODE
6615 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
6616#else /* CAM_NEW_TRAN_CODE */
6617 cts.flags = CCB_TRANS_TAG_ENB;
6618#endif /* CAM_NEW_TRAN_CODE */
6619 xpt_set_transfer_settings(&cts, path->device,
6620 /*async_update*/TRUE);
6621 }
6622}
6623
6624static void
6625xpt_start_tags(struct cam_path *path)
6626{
6627 struct ccb_relsim crs;
6628 struct cam_ed *device;
6629 struct cam_sim *sim;
6630 int newopenings;
6631
6632 device = path->device;
6633 sim = path->bus->sim;
6634 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6635 xpt_freeze_devq(path, /*count*/1);
6636 device->inq_flags |= SID_CmdQue;
6637 newopenings = min(device->quirk->maxtags, sim->max_tagged_dev_openings);
6638 xpt_dev_ccbq_resize(path, newopenings);
6639 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6640 crs.ccb_h.func_code = XPT_REL_SIMQ;
6641 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6642 crs.openings
6643 = crs.release_timeout
6644 = crs.qfrozen_cnt
6645 = 0;
6646 xpt_action((union ccb *)&crs);
6647}
6648
6649static int busses_to_config;
6650static int busses_to_reset;
6651
6652static int
6653xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6654{
6655 if (bus->path_id != CAM_XPT_PATH_ID) {
6656 struct cam_path path;
6657 struct ccb_pathinq cpi;
6658 int can_negotiate;
6659
6660 busses_to_config++;
6661 xpt_compile_path(&path, NULL, bus->path_id,
6662 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6663 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6664 cpi.ccb_h.func_code = XPT_PATH_INQ;
6665 xpt_action((union ccb *)&cpi);
6666 can_negotiate = cpi.hba_inquiry;
6667 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6668 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6669 && can_negotiate)
6670 busses_to_reset++;
6671 xpt_release_path(&path);
6672 }
6673
6674 return(1);
6675}
6676
6677static int
6678xptconfigfunc(struct cam_eb *bus, void *arg)
6679{
6680 struct cam_path *path;
6681 union ccb *work_ccb;
6682
6683 if (bus->path_id != CAM_XPT_PATH_ID) {
6684 cam_status status;
6685 int can_negotiate;
6686
6687 work_ccb = xpt_alloc_ccb();
6688 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6689 CAM_TARGET_WILDCARD,
6690 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6691 printf("xptconfigfunc: xpt_create_path failed with "
6692 "status %#x for bus %d\n", status, bus->path_id);
6693 printf("xptconfigfunc: halting bus configuration\n");
6694 xpt_free_ccb(work_ccb);
6695 busses_to_config--;
6696 xpt_finishconfig(xpt_periph, NULL);
6697 return(0);
6698 }
6699 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6700 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6701 xpt_action(work_ccb);
6702 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6703 printf("xptconfigfunc: CPI failed on bus %d "
6704 "with status %d\n", bus->path_id,
6705 work_ccb->ccb_h.status);
6706 xpt_finishconfig(xpt_periph, work_ccb);
6707 return(1);
6708 }
6709
6710 can_negotiate = work_ccb->cpi.hba_inquiry;
6711 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6712 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6713 && (can_negotiate != 0)) {
6714 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6715 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6716 work_ccb->ccb_h.cbfcnp = NULL;
6717 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6718 ("Resetting Bus\n"));
6719 xpt_action(work_ccb);
6720 xpt_finishconfig(xpt_periph, work_ccb);
6721 } else {
6722 /* Act as though we performed a successful BUS RESET */
6723 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6724 xpt_finishconfig(xpt_periph, work_ccb);
6725 }
6726 }
6727
6728 return(1);
6729}
6730
6731static void
6732xpt_config(void *arg)
6733{
6734 /*
6735 * Now that interrupts are enabled, go find our devices
6736 */
6737
6738#ifdef CAMDEBUG
6739 /* Setup debugging flags and path */
6740#ifdef CAM_DEBUG_FLAGS
6741 cam_dflags = CAM_DEBUG_FLAGS;
6742#else /* !CAM_DEBUG_FLAGS */
6743 cam_dflags = CAM_DEBUG_NONE;
6744#endif /* CAM_DEBUG_FLAGS */
6745#ifdef CAM_DEBUG_BUS
6746 if (cam_dflags != CAM_DEBUG_NONE) {
6747 if (xpt_create_path(&cam_dpath, xpt_periph,
6748 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6749 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6750 printf("xpt_config: xpt_create_path() failed for debug"
6751 " target %d:%d:%d, debugging disabled\n",
6752 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6753 cam_dflags = CAM_DEBUG_NONE;
6754 }
6755 } else
6756 cam_dpath = NULL;
6757#else /* !CAM_DEBUG_BUS */
6758 cam_dpath = NULL;
6759#endif /* CAM_DEBUG_BUS */
6760#endif /* CAMDEBUG */
6761
6762 /*
6763 * Scan all installed busses.
6764 */
6765 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6766
6767 if (busses_to_config == 0) {
6768 /* Call manually because we don't have any busses */
6769 xpt_finishconfig(xpt_periph, NULL);
6770 } else {
6771 if (busses_to_reset > 0 && scsi_delay >= 2000) {
6772 printf("Waiting %d seconds for SCSI "
6773 "devices to settle\n", scsi_delay/1000);
6774 }
6775 xpt_for_all_busses(xptconfigfunc, NULL);
6776 }
6777}
6778
6779/*
6780 * If the given device only has one peripheral attached to it, and if that
6781 * peripheral is the passthrough driver, announce it. This insures that the
6782 * user sees some sort of announcement for every peripheral in their system.
6783 */
6784static int
6785xptpassannouncefunc(struct cam_ed *device, void *arg)
6786{
6787 struct cam_periph *periph;
6788 int i;
6789
6790 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6791 periph = SLIST_NEXT(periph, periph_links), i++);
6792
6793 periph = SLIST_FIRST(&device->periphs);
6794 if ((i == 1)
6795 && (strncmp(periph->periph_name, "pass", 4) == 0))
6796 xpt_announce_periph(periph, NULL);
6797
6798 return(1);
6799}
6800
6801static void
6802xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6803{
6804 struct periph_driver **p_drv;
6805 int i;
6806
6807 if (done_ccb != NULL) {
6808 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6809 ("xpt_finishconfig\n"));
6810 switch(done_ccb->ccb_h.func_code) {
6811 case XPT_RESET_BUS:
6812 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6813 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6814 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6815 xpt_action(done_ccb);
6816 return;
6817 }
6818 /* FALLTHROUGH */
6819 case XPT_SCAN_BUS:
6820 default:
6821 xpt_free_path(done_ccb->ccb_h.path);
6822 busses_to_config--;
6823 break;
6824 }
6825 }
6826
6827 if (busses_to_config == 0) {
6828 /* Register all the peripheral drivers */
6829 /* XXX This will have to change when we have loadable modules */
6830 p_drv = periph_drivers;
6831 for (i = 0; p_drv[i] != NULL; i++) {
6832 (*p_drv[i]->init)();
6833 }
6834
6835 /*
6836 * Check for devices with no "standard" peripheral driver
6837 * attached. For any devices like that, announce the
6838 * passthrough driver so the user will see something.
6839 */
6840 xpt_for_all_devices(xptpassannouncefunc, NULL);
6841
6842 /* Release our hook so that the boot can continue. */
6843 config_intrhook_disestablish(xpt_config_hook);
6844 free(xpt_config_hook, M_TEMP);
6845 xpt_config_hook = NULL;
6846 }
6847 if (done_ccb != NULL)
6848 xpt_free_ccb(done_ccb);
6849}
6850
6851static void
6852xptaction(struct cam_sim *sim, union ccb *work_ccb)
6853{
6854 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6855
6856 switch (work_ccb->ccb_h.func_code) {
6857 /* Common cases first */
6858 case XPT_PATH_INQ: /* Path routing inquiry */
6859 {
6860 struct ccb_pathinq *cpi;
6861
6862 cpi = &work_ccb->cpi;
6863 cpi->version_num = 1; /* XXX??? */
6864 cpi->hba_inquiry = 0;
6865 cpi->target_sprt = 0;
6866 cpi->hba_misc = 0;
6867 cpi->hba_eng_cnt = 0;
6868 cpi->max_target = 0;
6869 cpi->max_lun = 0;
6870 cpi->initiator_id = 0;
6871 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
6872 strncpy(cpi->hba_vid, "", HBA_IDLEN);
6873 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
6874 cpi->unit_number = sim->unit_number;
6875 cpi->bus_id = sim->bus_id;
6876 cpi->base_transfer_speed = 0;
6877#ifdef CAM_NEW_TRAN_CODE
6878 cpi->protocol = PROTO_UNSPECIFIED;
6879 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
6880 cpi->transport = XPORT_UNSPECIFIED;
6881 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
6882#endif /* CAM_NEW_TRAN_CODE */
6883 cpi->ccb_h.status = CAM_REQ_CMP;
6884 xpt_done(work_ccb);
6885 break;
6886 }
6887 default:
6888 work_ccb->ccb_h.status = CAM_REQ_INVALID;
6889 xpt_done(work_ccb);
6890 break;
6891 }
6892}
6893
6894/*
6895 * The xpt as a "controller" has no interrupt sources, so polling
6896 * is a no-op.
6897 */
6898static void
6899xptpoll(struct cam_sim *sim)
6900{
6901}
6902
6903static void
6904camisr(void *V_queue)
6905{
6906 cam_isrq_t *queue = V_queue;
6907 int s;
6908 struct ccb_hdr *ccb_h;
6909
6910 s = splcam();
6911 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
6912 int runq;
6913
6914 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
6915 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
6916 splx(s);
6917
6918 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
6919 ("camisr\n"));
6920
6921 runq = FALSE;
6922
6923 if (ccb_h->flags & CAM_HIGH_POWER) {
6924 struct highpowerlist *hphead;
6925 struct cam_ed *device;
6926 union ccb *send_ccb;
6927
6928 hphead = &highpowerq;
6929
6930 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
6931
6932 /*
6933 * Increment the count since this command is done.
6934 */
6935 num_highpower++;
6936
6937 /*
6938 * Any high powered commands queued up?
6939 */
6940 if (send_ccb != NULL) {
6941 device = send_ccb->ccb_h.path->device;
6942
6943 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
6944
6945 xpt_release_devq(send_ccb->ccb_h.path,
6946 /*count*/1, /*runqueue*/TRUE);
6947 }
6948 }
6949 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
6950 struct cam_ed *dev;
6951
6952 dev = ccb_h->path->device;
6953
6954 s = splcam();
6955 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
6956
6957 ccb_h->path->bus->sim->devq->send_active--;
6958 ccb_h->path->bus->sim->devq->send_openings++;
6959 splx(s);
6960
6961 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
6962 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
6963 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
6964 && (dev->ccbq.dev_active == 0))) {
6965
6966 xpt_release_devq(ccb_h->path, /*count*/1,
6967 /*run_queue*/TRUE);
6968 }
6969
6970 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6971 && (--dev->tag_delay_count == 0))
6972 xpt_start_tags(ccb_h->path);
6973
6974 if ((dev->ccbq.queue.entries > 0)
6975 && (dev->qfrozen_cnt == 0)
6976 && (device_is_send_queued(dev) == 0)) {
6977 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
6978 dev);
6979 }
6980 }
6981
6982 if (ccb_h->status & CAM_RELEASE_SIMQ) {
6983 xpt_release_simq(ccb_h->path->bus->sim,
6984 /*run_queue*/TRUE);
6985 ccb_h->status &= ~CAM_RELEASE_SIMQ;
6986 runq = FALSE;
6987 }
6988
6989 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
6990 && (ccb_h->status & CAM_DEV_QFRZN)) {
6991 xpt_release_devq(ccb_h->path, /*count*/1,
6992 /*run_queue*/TRUE);
6993 ccb_h->status &= ~CAM_DEV_QFRZN;
6994 } else if (runq) {
6995 xpt_run_dev_sendq(ccb_h->path->bus);
6996 }
6997
6998 /* Call the peripheral driver's callback */
6999 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
7000
7001 /* Raise IPL for while test */
7002 s = splcam();
7003 }
7004 splx(s);
7005}
644};
645
646static struct intr_config_hook *xpt_config_hook;
647
648/* Registered busses */
649static TAILQ_HEAD(,cam_eb) xpt_busses;
650static u_int bus_generation;
651
652/* Storage for debugging datastructures */
653#ifdef CAMDEBUG
654struct cam_path *cam_dpath;
655u_int32_t cam_dflags;
656u_int32_t cam_debug_delay;
657#endif
658
659/* Pointers to software interrupt handlers */
660static void *camnet_ih;
661static void *cambio_ih;
662
663#if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
664#error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
665#endif
666
667/*
668 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
669 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
670 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
671 */
672#if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
673 || defined(CAM_DEBUG_LUN)
674#ifdef CAMDEBUG
675#if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
676 || !defined(CAM_DEBUG_LUN)
677#error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
678 and CAM_DEBUG_LUN"
679#endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
680#else /* !CAMDEBUG */
681#error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
682#endif /* CAMDEBUG */
683#endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
684
685/* Our boot-time initialization hook */
686static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
687
688static moduledata_t cam_moduledata = {
689 "cam",
690 cam_module_event_handler,
691 NULL
692};
693
694static void xpt_init(void *);
695
696DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
697MODULE_VERSION(cam, 1);
698
699
700static cam_status xpt_compile_path(struct cam_path *new_path,
701 struct cam_periph *perph,
702 path_id_t path_id,
703 target_id_t target_id,
704 lun_id_t lun_id);
705
706static void xpt_release_path(struct cam_path *path);
707
708static void xpt_async_bcast(struct async_list *async_head,
709 u_int32_t async_code,
710 struct cam_path *path,
711 void *async_arg);
712static void xpt_dev_async(u_int32_t async_code,
713 struct cam_eb *bus,
714 struct cam_et *target,
715 struct cam_ed *device,
716 void *async_arg);
717static path_id_t xptnextfreepathid(void);
718static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
719static union ccb *xpt_get_ccb(struct cam_ed *device);
720static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
721 u_int32_t new_priority);
722static void xpt_run_dev_allocq(struct cam_eb *bus);
723static void xpt_run_dev_sendq(struct cam_eb *bus);
724static timeout_t xpt_release_devq_timeout;
725static timeout_t xpt_release_simq_timeout;
726static void xpt_release_bus(struct cam_eb *bus);
727static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
728 int run_queue);
729static struct cam_et*
730 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
731static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
732static struct cam_ed*
733 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
734 lun_id_t lun_id);
735static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
736 struct cam_ed *device);
737static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
738static struct cam_eb*
739 xpt_find_bus(path_id_t path_id);
740static struct cam_et*
741 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
742static struct cam_ed*
743 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
744static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
745static void xpt_scan_lun(struct cam_periph *periph,
746 struct cam_path *path, cam_flags flags,
747 union ccb *ccb);
748static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
749static xpt_busfunc_t xptconfigbuscountfunc;
750static xpt_busfunc_t xptconfigfunc;
751static void xpt_config(void *arg);
752static xpt_devicefunc_t xptpassannouncefunc;
753static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
754static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
755static void xptpoll(struct cam_sim *sim);
756static void camisr(void *);
757#if 0
758static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
759static void xptasync(struct cam_periph *periph,
760 u_int32_t code, cam_path *path);
761#endif
762static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
763 u_int num_patterns, struct cam_eb *bus);
764static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
765 u_int num_patterns,
766 struct cam_ed *device);
767static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
768 u_int num_patterns,
769 struct cam_periph *periph);
770static xpt_busfunc_t xptedtbusfunc;
771static xpt_targetfunc_t xptedttargetfunc;
772static xpt_devicefunc_t xptedtdevicefunc;
773static xpt_periphfunc_t xptedtperiphfunc;
774static xpt_pdrvfunc_t xptplistpdrvfunc;
775static xpt_periphfunc_t xptplistperiphfunc;
776static int xptedtmatch(struct ccb_dev_match *cdm);
777static int xptperiphlistmatch(struct ccb_dev_match *cdm);
778static int xptbustraverse(struct cam_eb *start_bus,
779 xpt_busfunc_t *tr_func, void *arg);
780static int xpttargettraverse(struct cam_eb *bus,
781 struct cam_et *start_target,
782 xpt_targetfunc_t *tr_func, void *arg);
783static int xptdevicetraverse(struct cam_et *target,
784 struct cam_ed *start_device,
785 xpt_devicefunc_t *tr_func, void *arg);
786static int xptperiphtraverse(struct cam_ed *device,
787 struct cam_periph *start_periph,
788 xpt_periphfunc_t *tr_func, void *arg);
789static int xptpdrvtraverse(struct periph_driver **start_pdrv,
790 xpt_pdrvfunc_t *tr_func, void *arg);
791static int xptpdperiphtraverse(struct periph_driver **pdrv,
792 struct cam_periph *start_periph,
793 xpt_periphfunc_t *tr_func,
794 void *arg);
795static xpt_busfunc_t xptdefbusfunc;
796static xpt_targetfunc_t xptdeftargetfunc;
797static xpt_devicefunc_t xptdefdevicefunc;
798static xpt_periphfunc_t xptdefperiphfunc;
799static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
800#ifdef notusedyet
801static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
802 void *arg);
803#endif
804static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
805 void *arg);
806#ifdef notusedyet
807static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
808 void *arg);
809#endif
810static xpt_devicefunc_t xptsetasyncfunc;
811static xpt_busfunc_t xptsetasyncbusfunc;
812static cam_status xptregister(struct cam_periph *periph,
813 void *arg);
814static cam_status proberegister(struct cam_periph *periph,
815 void *arg);
816static void probeschedule(struct cam_periph *probe_periph);
817static void probestart(struct cam_periph *periph, union ccb *start_ccb);
818static void proberequestdefaultnegotiation(struct cam_periph *periph);
819static void probedone(struct cam_periph *periph, union ccb *done_ccb);
820static void probecleanup(struct cam_periph *periph);
821static void xpt_find_quirk(struct cam_ed *device);
822#ifdef CAM_NEW_TRAN_CODE
823static void xpt_devise_transport(struct cam_path *path);
824#endif /* CAM_NEW_TRAN_CODE */
825static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
826 struct cam_ed *device,
827 int async_update);
828static void xpt_toggle_tags(struct cam_path *path);
829static void xpt_start_tags(struct cam_path *path);
830static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
831 struct cam_ed *dev);
832static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
833 struct cam_ed *dev);
834static __inline int periph_is_queued(struct cam_periph *periph);
835static __inline int device_is_alloc_queued(struct cam_ed *device);
836static __inline int device_is_send_queued(struct cam_ed *device);
837static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
838
839static __inline int
840xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
841{
842 int retval;
843
844 if (dev->ccbq.devq_openings > 0) {
845 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
846 cam_ccbq_resize(&dev->ccbq,
847 dev->ccbq.dev_openings
848 + dev->ccbq.dev_active);
849 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
850 }
851 /*
852 * The priority of a device waiting for CCB resources
853 * is that of the the highest priority peripheral driver
854 * enqueued.
855 */
856 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
857 &dev->alloc_ccb_entry.pinfo,
858 CAMQ_GET_HEAD(&dev->drvq)->priority);
859 } else {
860 retval = 0;
861 }
862
863 return (retval);
864}
865
866static __inline int
867xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
868{
869 int retval;
870
871 if (dev->ccbq.dev_openings > 0) {
872 /*
873 * The priority of a device waiting for controller
874 * resources is that of the the highest priority CCB
875 * enqueued.
876 */
877 retval =
878 xpt_schedule_dev(&bus->sim->devq->send_queue,
879 &dev->send_ccb_entry.pinfo,
880 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
881 } else {
882 retval = 0;
883 }
884 return (retval);
885}
886
887static __inline int
888periph_is_queued(struct cam_periph *periph)
889{
890 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
891}
892
893static __inline int
894device_is_alloc_queued(struct cam_ed *device)
895{
896 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
897}
898
899static __inline int
900device_is_send_queued(struct cam_ed *device)
901{
902 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
903}
904
905static __inline int
906dev_allocq_is_runnable(struct cam_devq *devq)
907{
908 /*
909 * Have work to do.
910 * Have space to do more work.
911 * Allowed to do work.
912 */
913 return ((devq->alloc_queue.qfrozen_cnt == 0)
914 && (devq->alloc_queue.entries > 0)
915 && (devq->alloc_openings > 0));
916}
917
918static void
919xpt_periph_init()
920{
921 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
922}
923
924static void
925probe_periph_init()
926{
927}
928
929
930static void
931xptdone(struct cam_periph *periph, union ccb *done_ccb)
932{
933 /* Caller will release the CCB */
934 wakeup(&done_ccb->ccb_h.cbfcnp);
935}
936
937static int
938xptopen(dev_t dev, int flags, int fmt, struct thread *td)
939{
940 int unit;
941
942 unit = minor(dev) & 0xff;
943
944 /*
945 * Only allow read-write access.
946 */
947 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
948 return(EPERM);
949
950 /*
951 * We don't allow nonblocking access.
952 */
953 if ((flags & O_NONBLOCK) != 0) {
954 printf("xpt%d: can't do nonblocking access\n", unit);
955 return(ENODEV);
956 }
957
958 /*
959 * We only have one transport layer right now. If someone accesses
960 * us via something other than minor number 1, point out their
961 * mistake.
962 */
963 if (unit != 0) {
964 printf("xptopen: got invalid xpt unit %d\n", unit);
965 return(ENXIO);
966 }
967
968 /* Mark ourselves open */
969 xsoftc.flags |= XPT_FLAG_OPEN;
970
971 return(0);
972}
973
974static int
975xptclose(dev_t dev, int flag, int fmt, struct thread *td)
976{
977 int unit;
978
979 unit = minor(dev) & 0xff;
980
981 /*
982 * We only have one transport layer right now. If someone accesses
983 * us via something other than minor number 1, point out their
984 * mistake.
985 */
986 if (unit != 0) {
987 printf("xptclose: got invalid xpt unit %d\n", unit);
988 return(ENXIO);
989 }
990
991 /* Mark ourselves closed */
992 xsoftc.flags &= ~XPT_FLAG_OPEN;
993
994 return(0);
995}
996
997static int
998xptioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
999{
1000 int unit, error;
1001
1002 error = 0;
1003 unit = minor(dev) & 0xff;
1004
1005 /*
1006 * We only have one transport layer right now. If someone accesses
1007 * us via something other than minor number 1, point out their
1008 * mistake.
1009 */
1010 if (unit != 0) {
1011 printf("xptioctl: got invalid xpt unit %d\n", unit);
1012 return(ENXIO);
1013 }
1014
1015 switch(cmd) {
1016 /*
1017 * For the transport layer CAMIOCOMMAND ioctl, we really only want
1018 * to accept CCB types that don't quite make sense to send through a
1019 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
1020 * in the CAM spec.
1021 */
1022 case CAMIOCOMMAND: {
1023 union ccb *ccb;
1024 union ccb *inccb;
1025
1026 inccb = (union ccb *)addr;
1027
1028 switch(inccb->ccb_h.func_code) {
1029 case XPT_SCAN_BUS:
1030 case XPT_RESET_BUS:
1031 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
1032 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1033 error = EINVAL;
1034 break;
1035 }
1036 /* FALLTHROUGH */
1037 case XPT_PATH_INQ:
1038 case XPT_ENG_INQ:
1039 case XPT_SCAN_LUN:
1040
1041 ccb = xpt_alloc_ccb();
1042
1043 /*
1044 * Create a path using the bus, target, and lun the
1045 * user passed in.
1046 */
1047 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1048 inccb->ccb_h.path_id,
1049 inccb->ccb_h.target_id,
1050 inccb->ccb_h.target_lun) !=
1051 CAM_REQ_CMP){
1052 error = EINVAL;
1053 xpt_free_ccb(ccb);
1054 break;
1055 }
1056 /* Ensure all of our fields are correct */
1057 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1058 inccb->ccb_h.pinfo.priority);
1059 xpt_merge_ccb(ccb, inccb);
1060 ccb->ccb_h.cbfcnp = xptdone;
1061 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1062 bcopy(ccb, inccb, sizeof(union ccb));
1063 xpt_free_path(ccb->ccb_h.path);
1064 xpt_free_ccb(ccb);
1065 break;
1066
1067 case XPT_DEBUG: {
1068 union ccb ccb;
1069
1070 /*
1071 * This is an immediate CCB, so it's okay to
1072 * allocate it on the stack.
1073 */
1074
1075 /*
1076 * Create a path using the bus, target, and lun the
1077 * user passed in.
1078 */
1079 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1080 inccb->ccb_h.path_id,
1081 inccb->ccb_h.target_id,
1082 inccb->ccb_h.target_lun) !=
1083 CAM_REQ_CMP){
1084 error = EINVAL;
1085 break;
1086 }
1087 /* Ensure all of our fields are correct */
1088 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1089 inccb->ccb_h.pinfo.priority);
1090 xpt_merge_ccb(&ccb, inccb);
1091 ccb.ccb_h.cbfcnp = xptdone;
1092 xpt_action(&ccb);
1093 bcopy(&ccb, inccb, sizeof(union ccb));
1094 xpt_free_path(ccb.ccb_h.path);
1095 break;
1096
1097 }
1098 case XPT_DEV_MATCH: {
1099 struct cam_periph_map_info mapinfo;
1100 struct cam_path *old_path;
1101
1102 /*
1103 * We can't deal with physical addresses for this
1104 * type of transaction.
1105 */
1106 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1107 error = EINVAL;
1108 break;
1109 }
1110
1111 /*
1112 * Save this in case the caller had it set to
1113 * something in particular.
1114 */
1115 old_path = inccb->ccb_h.path;
1116
1117 /*
1118 * We really don't need a path for the matching
1119 * code. The path is needed because of the
1120 * debugging statements in xpt_action(). They
1121 * assume that the CCB has a valid path.
1122 */
1123 inccb->ccb_h.path = xpt_periph->path;
1124
1125 bzero(&mapinfo, sizeof(mapinfo));
1126
1127 /*
1128 * Map the pattern and match buffers into kernel
1129 * virtual address space.
1130 */
1131 error = cam_periph_mapmem(inccb, &mapinfo);
1132
1133 if (error) {
1134 inccb->ccb_h.path = old_path;
1135 break;
1136 }
1137
1138 /*
1139 * This is an immediate CCB, we can send it on directly.
1140 */
1141 xpt_action(inccb);
1142
1143 /*
1144 * Map the buffers back into user space.
1145 */
1146 cam_periph_unmapmem(inccb, &mapinfo);
1147
1148 inccb->ccb_h.path = old_path;
1149
1150 error = 0;
1151 break;
1152 }
1153 default:
1154 error = ENOTSUP;
1155 break;
1156 }
1157 break;
1158 }
1159 /*
1160 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1161 * with the periphal driver name and unit name filled in. The other
1162 * fields don't really matter as input. The passthrough driver name
1163 * ("pass"), and unit number are passed back in the ccb. The current
1164 * device generation number, and the index into the device peripheral
1165 * driver list, and the status are also passed back. Note that
1166 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1167 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1168 * (or rather should be) impossible for the device peripheral driver
1169 * list to change since we look at the whole thing in one pass, and
1170 * we do it with splcam protection.
1171 *
1172 */
1173 case CAMGETPASSTHRU: {
1174 union ccb *ccb;
1175 struct cam_periph *periph;
1176 struct periph_driver **p_drv;
1177 char *name;
1178 u_int unit;
1179 u_int cur_generation;
1180 int base_periph_found;
1181 int splbreaknum;
1182 int s;
1183
1184 ccb = (union ccb *)addr;
1185 unit = ccb->cgdl.unit_number;
1186 name = ccb->cgdl.periph_name;
1187 /*
1188 * Every 100 devices, we want to drop our spl protection to
1189 * give the software interrupt handler a chance to run.
1190 * Most systems won't run into this check, but this should
1191 * avoid starvation in the software interrupt handler in
1192 * large systems.
1193 */
1194 splbreaknum = 100;
1195
1196 ccb = (union ccb *)addr;
1197
1198 base_periph_found = 0;
1199
1200 /*
1201 * Sanity check -- make sure we don't get a null peripheral
1202 * driver name.
1203 */
1204 if (*ccb->cgdl.periph_name == '\0') {
1205 error = EINVAL;
1206 break;
1207 }
1208
1209 /* Keep the list from changing while we traverse it */
1210 s = splcam();
1211ptstartover:
1212 cur_generation = xsoftc.generation;
1213
1214 /* first find our driver in the list of drivers */
1215 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
1216 if (strcmp((*p_drv)->driver_name, name) == 0)
1217 break;
1218
1219 if (*p_drv == NULL) {
1220 splx(s);
1221 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1222 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1223 *ccb->cgdl.periph_name = '\0';
1224 ccb->cgdl.unit_number = 0;
1225 error = ENOENT;
1226 break;
1227 }
1228
1229 /*
1230 * Run through every peripheral instance of this driver
1231 * and check to see whether it matches the unit passed
1232 * in by the user. If it does, get out of the loops and
1233 * find the passthrough driver associated with that
1234 * peripheral driver.
1235 */
1236 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1237 periph = TAILQ_NEXT(periph, unit_links)) {
1238
1239 if (periph->unit_number == unit) {
1240 break;
1241 } else if (--splbreaknum == 0) {
1242 splx(s);
1243 s = splcam();
1244 splbreaknum = 100;
1245 if (cur_generation != xsoftc.generation)
1246 goto ptstartover;
1247 }
1248 }
1249 /*
1250 * If we found the peripheral driver that the user passed
1251 * in, go through all of the peripheral drivers for that
1252 * particular device and look for a passthrough driver.
1253 */
1254 if (periph != NULL) {
1255 struct cam_ed *device;
1256 int i;
1257
1258 base_periph_found = 1;
1259 device = periph->path->device;
1260 for (i = 0, periph = SLIST_FIRST(&device->periphs);
1261 periph != NULL;
1262 periph = SLIST_NEXT(periph, periph_links), i++) {
1263 /*
1264 * Check to see whether we have a
1265 * passthrough device or not.
1266 */
1267 if (strcmp(periph->periph_name, "pass") == 0) {
1268 /*
1269 * Fill in the getdevlist fields.
1270 */
1271 strcpy(ccb->cgdl.periph_name,
1272 periph->periph_name);
1273 ccb->cgdl.unit_number =
1274 periph->unit_number;
1275 if (SLIST_NEXT(periph, periph_links))
1276 ccb->cgdl.status =
1277 CAM_GDEVLIST_MORE_DEVS;
1278 else
1279 ccb->cgdl.status =
1280 CAM_GDEVLIST_LAST_DEVICE;
1281 ccb->cgdl.generation =
1282 device->generation;
1283 ccb->cgdl.index = i;
1284 /*
1285 * Fill in some CCB header fields
1286 * that the user may want.
1287 */
1288 ccb->ccb_h.path_id =
1289 periph->path->bus->path_id;
1290 ccb->ccb_h.target_id =
1291 periph->path->target->target_id;
1292 ccb->ccb_h.target_lun =
1293 periph->path->device->lun_id;
1294 ccb->ccb_h.status = CAM_REQ_CMP;
1295 break;
1296 }
1297 }
1298 }
1299
1300 /*
1301 * If the periph is null here, one of two things has
1302 * happened. The first possibility is that we couldn't
1303 * find the unit number of the particular peripheral driver
1304 * that the user is asking about. e.g. the user asks for
1305 * the passthrough driver for "da11". We find the list of
1306 * "da" peripherals all right, but there is no unit 11.
1307 * The other possibility is that we went through the list
1308 * of peripheral drivers attached to the device structure,
1309 * but didn't find one with the name "pass". Either way,
1310 * we return ENOENT, since we couldn't find something.
1311 */
1312 if (periph == NULL) {
1313 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1314 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1315 *ccb->cgdl.periph_name = '\0';
1316 ccb->cgdl.unit_number = 0;
1317 error = ENOENT;
1318 /*
1319 * It is unfortunate that this is even necessary,
1320 * but there are many, many clueless users out there.
1321 * If this is true, the user is looking for the
1322 * passthrough driver, but doesn't have one in his
1323 * kernel.
1324 */
1325 if (base_periph_found == 1) {
1326 printf("xptioctl: pass driver is not in the "
1327 "kernel\n");
1328 printf("xptioctl: put \"device pass0\" in "
1329 "your kernel config file\n");
1330 }
1331 }
1332 splx(s);
1333 break;
1334 }
1335 default:
1336 error = ENOTTY;
1337 break;
1338 }
1339
1340 return(error);
1341}
1342
1343static int
1344cam_module_event_handler(module_t mod, int what, void *arg)
1345{
1346 if (what == MOD_LOAD) {
1347 xpt_init(NULL);
1348 } else if (what == MOD_UNLOAD) {
1349 return EBUSY;
1350 }
1351
1352 return 0;
1353}
1354
1355/* Functions accessed by the peripheral drivers */
1356static void
1357xpt_init(dummy)
1358 void *dummy;
1359{
1360 struct cam_sim *xpt_sim;
1361 struct cam_path *path;
1362 struct cam_devq *devq;
1363 cam_status status;
1364
1365 TAILQ_INIT(&xpt_busses);
1366 TAILQ_INIT(&cam_bioq);
1367 TAILQ_INIT(&cam_netq);
1368 SLIST_INIT(&ccb_freeq);
1369 STAILQ_INIT(&highpowerq);
1370
1371 /*
1372 * The xpt layer is, itself, the equivelent of a SIM.
1373 * Allow 16 ccbs in the ccb pool for it. This should
1374 * give decent parallelism when we probe busses and
1375 * perform other XPT functions.
1376 */
1377 devq = cam_simq_alloc(16);
1378 xpt_sim = cam_sim_alloc(xptaction,
1379 xptpoll,
1380 "xpt",
1381 /*softc*/NULL,
1382 /*unit*/0,
1383 /*max_dev_transactions*/0,
1384 /*max_tagged_dev_transactions*/0,
1385 devq);
1386 xpt_max_ccbs = 16;
1387
1388 xpt_bus_register(xpt_sim, /*bus #*/0);
1389
1390 /*
1391 * Looking at the XPT from the SIM layer, the XPT is
1392 * the equivelent of a peripheral driver. Allocate
1393 * a peripheral driver entry for us.
1394 */
1395 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1396 CAM_TARGET_WILDCARD,
1397 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1398 printf("xpt_init: xpt_create_path failed with status %#x,"
1399 " failing attach\n", status);
1400 return;
1401 }
1402
1403 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1404 path, NULL, 0, NULL);
1405 xpt_free_path(path);
1406
1407 xpt_sim->softc = xpt_periph;
1408
1409 /*
1410 * Register a callback for when interrupts are enabled.
1411 */
1412 xpt_config_hook =
1413 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1414 M_TEMP, M_NOWAIT | M_ZERO);
1415 if (xpt_config_hook == NULL) {
1416 printf("xpt_init: Cannot malloc config hook "
1417 "- failing attach\n");
1418 return;
1419 }
1420
1421 xpt_config_hook->ich_func = xpt_config;
1422 if (config_intrhook_establish(xpt_config_hook) != 0) {
1423 free (xpt_config_hook, M_TEMP);
1424 printf("xpt_init: config_intrhook_establish failed "
1425 "- failing attach\n");
1426 }
1427
1428 /* Install our software interrupt handlers */
1429 swi_add(NULL, "camnet", camisr, &cam_netq, SWI_CAMNET, 0, &camnet_ih);
1430 swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, 0, &cambio_ih);
1431}
1432
1433static cam_status
1434xptregister(struct cam_periph *periph, void *arg)
1435{
1436 if (periph == NULL) {
1437 printf("xptregister: periph was NULL!!\n");
1438 return(CAM_REQ_CMP_ERR);
1439 }
1440
1441 periph->softc = NULL;
1442
1443 xpt_periph = periph;
1444
1445 return(CAM_REQ_CMP);
1446}
1447
1448int32_t
1449xpt_add_periph(struct cam_periph *periph)
1450{
1451 struct cam_ed *device;
1452 int32_t status;
1453 struct periph_list *periph_head;
1454
1455 device = periph->path->device;
1456
1457 periph_head = &device->periphs;
1458
1459 status = CAM_REQ_CMP;
1460
1461 if (device != NULL) {
1462 int s;
1463
1464 /*
1465 * Make room for this peripheral
1466 * so it will fit in the queue
1467 * when it's scheduled to run
1468 */
1469 s = splsoftcam();
1470 status = camq_resize(&device->drvq,
1471 device->drvq.array_size + 1);
1472
1473 device->generation++;
1474
1475 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1476
1477 splx(s);
1478 }
1479
1480 xsoftc.generation++;
1481
1482 return (status);
1483}
1484
1485void
1486xpt_remove_periph(struct cam_periph *periph)
1487{
1488 struct cam_ed *device;
1489
1490 device = periph->path->device;
1491
1492 if (device != NULL) {
1493 int s;
1494 struct periph_list *periph_head;
1495
1496 periph_head = &device->periphs;
1497
1498 /* Release the slot for this peripheral */
1499 s = splsoftcam();
1500 camq_resize(&device->drvq, device->drvq.array_size - 1);
1501
1502 device->generation++;
1503
1504 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1505
1506 splx(s);
1507 }
1508
1509 xsoftc.generation++;
1510
1511}
1512
1513#ifdef CAM_NEW_TRAN_CODE
1514
1515void
1516xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1517{
1518 struct ccb_pathinq cpi;
1519 struct ccb_trans_settings cts;
1520 struct cam_path *path;
1521 u_int speed;
1522 u_int freq;
1523 u_int mb;
1524 int s;
1525
1526 path = periph->path;
1527 /*
1528 * To ensure that this is printed in one piece,
1529 * mask out CAM interrupts.
1530 */
1531 s = splsoftcam();
1532 printf("%s%d at %s%d bus %d target %d lun %d\n",
1533 periph->periph_name, periph->unit_number,
1534 path->bus->sim->sim_name,
1535 path->bus->sim->unit_number,
1536 path->bus->sim->bus_id,
1537 path->target->target_id,
1538 path->device->lun_id);
1539 printf("%s%d: ", periph->periph_name, periph->unit_number);
1540 scsi_print_inquiry(&path->device->inq_data);
1541 if (bootverbose && path->device->serial_num_len > 0) {
1542 /* Don't wrap the screen - print only the first 60 chars */
1543 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1544 periph->unit_number, path->device->serial_num);
1545 }
1546 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1547 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1548 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1549 xpt_action((union ccb*)&cts);
1550
1551 /* Ask the SIM for its base transfer speed */
1552 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1553 cpi.ccb_h.func_code = XPT_PATH_INQ;
1554 xpt_action((union ccb *)&cpi);
1555
1556 speed = cpi.base_transfer_speed;
1557 freq = 0;
1558 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1559 struct ccb_trans_settings_spi *spi;
1560
1561 spi = &cts.xport_specific.spi;
1562 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1563 && spi->sync_offset != 0) {
1564 freq = scsi_calc_syncsrate(spi->sync_period);
1565 speed = freq;
1566 }
1567
1568 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1569 speed *= (0x01 << spi->bus_width);
1570 }
1571
1572 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1573 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1574 if (fc->valid & CTS_FC_VALID_SPEED) {
1575 speed = fc->bitrate;
1576 }
1577 }
1578
1579 mb = speed / 1000;
1580 if (mb > 0)
1581 printf("%s%d: %d.%03dMB/s transfers",
1582 periph->periph_name, periph->unit_number,
1583 mb, speed % 1000);
1584 else
1585 printf("%s%d: %dKB/s transfers", periph->periph_name,
1586 periph->unit_number, speed);
1587 /* Report additional information about SPI connections */
1588 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1589 struct ccb_trans_settings_spi *spi;
1590
1591 spi = &cts.xport_specific.spi;
1592 if (freq != 0) {
1593 printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1594 freq % 1000,
1595 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1596 ? " DT" : "",
1597 spi->sync_offset);
1598 }
1599 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1600 && spi->bus_width > 0) {
1601 if (freq != 0) {
1602 printf(", ");
1603 } else {
1604 printf(" (");
1605 }
1606 printf("%dbit)", 8 * (0x01 << spi->bus_width));
1607 } else if (freq != 0) {
1608 printf(")");
1609 }
1610 }
1611 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1612 struct ccb_trans_settings_fc *fc;
1613
1614 fc = &cts.xport_specific.fc;
1615 if (fc->valid & CTS_FC_VALID_WWNN)
1616 printf(" WWNN 0x%llx", (long long) fc->wwnn);
1617 if (fc->valid & CTS_FC_VALID_WWPN)
1618 printf(" WWPN 0x%llx", (long long) fc->wwpn);
1619 if (fc->valid & CTS_FC_VALID_PORT)
1620 printf(" PortID 0x%x", fc->port);
1621 }
1622
1623 if (path->device->inq_flags & SID_CmdQue
1624 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1625 printf("\n%s%d: Tagged Queueing Enabled",
1626 periph->periph_name, periph->unit_number);
1627 }
1628 printf("\n");
1629
1630 /*
1631 * We only want to print the caller's announce string if they've
1632 * passed one in..
1633 */
1634 if (announce_string != NULL)
1635 printf("%s%d: %s\n", periph->periph_name,
1636 periph->unit_number, announce_string);
1637 splx(s);
1638}
1639#else /* CAM_NEW_TRAN_CODE */
1640void
1641xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1642{
1643 int s;
1644 u_int mb;
1645 struct cam_path *path;
1646 struct ccb_trans_settings cts;
1647
1648 path = periph->path;
1649 /*
1650 * To ensure that this is printed in one piece,
1651 * mask out CAM interrupts.
1652 */
1653 s = splsoftcam();
1654 printf("%s%d at %s%d bus %d target %d lun %d\n",
1655 periph->periph_name, periph->unit_number,
1656 path->bus->sim->sim_name,
1657 path->bus->sim->unit_number,
1658 path->bus->sim->bus_id,
1659 path->target->target_id,
1660 path->device->lun_id);
1661 printf("%s%d: ", periph->periph_name, periph->unit_number);
1662 scsi_print_inquiry(&path->device->inq_data);
1663 if ((bootverbose)
1664 && (path->device->serial_num_len > 0)) {
1665 /* Don't wrap the screen - print only the first 60 chars */
1666 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1667 periph->unit_number, path->device->serial_num);
1668 }
1669 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1670 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1671 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1672 xpt_action((union ccb*)&cts);
1673 if (cts.ccb_h.status == CAM_REQ_CMP) {
1674 u_int speed;
1675 u_int freq;
1676
1677 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1678 && cts.sync_offset != 0) {
1679 freq = scsi_calc_syncsrate(cts.sync_period);
1680 speed = freq;
1681 } else {
1682 struct ccb_pathinq cpi;
1683
1684 /* Ask the SIM for its base transfer speed */
1685 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1686 cpi.ccb_h.func_code = XPT_PATH_INQ;
1687 xpt_action((union ccb *)&cpi);
1688
1689 speed = cpi.base_transfer_speed;
1690 freq = 0;
1691 }
1692 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1693 speed *= (0x01 << cts.bus_width);
1694 mb = speed / 1000;
1695 if (mb > 0)
1696 printf("%s%d: %d.%03dMB/s transfers",
1697 periph->periph_name, periph->unit_number,
1698 mb, speed % 1000);
1699 else
1700 printf("%s%d: %dKB/s transfers", periph->periph_name,
1701 periph->unit_number, speed);
1702 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1703 && cts.sync_offset != 0) {
1704 printf(" (%d.%03dMHz, offset %d", freq / 1000,
1705 freq % 1000, cts.sync_offset);
1706 }
1707 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1708 && cts.bus_width > 0) {
1709 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1710 && cts.sync_offset != 0) {
1711 printf(", ");
1712 } else {
1713 printf(" (");
1714 }
1715 printf("%dbit)", 8 * (0x01 << cts.bus_width));
1716 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1717 && cts.sync_offset != 0) {
1718 printf(")");
1719 }
1720
1721 if (path->device->inq_flags & SID_CmdQue
1722 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1723 printf(", Tagged Queueing Enabled");
1724 }
1725
1726 printf("\n");
1727 } else if (path->device->inq_flags & SID_CmdQue
1728 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1729 printf("%s%d: Tagged Queueing Enabled\n",
1730 periph->periph_name, periph->unit_number);
1731 }
1732
1733 /*
1734 * We only want to print the caller's announce string if they've
1735 * passed one in..
1736 */
1737 if (announce_string != NULL)
1738 printf("%s%d: %s\n", periph->periph_name,
1739 periph->unit_number, announce_string);
1740 splx(s);
1741}
1742
1743#endif /* CAM_NEW_TRAN_CODE */
1744
1745static dev_match_ret
1746xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1747 struct cam_eb *bus)
1748{
1749 dev_match_ret retval;
1750 int i;
1751
1752 retval = DM_RET_NONE;
1753
1754 /*
1755 * If we aren't given something to match against, that's an error.
1756 */
1757 if (bus == NULL)
1758 return(DM_RET_ERROR);
1759
1760 /*
1761 * If there are no match entries, then this bus matches no
1762 * matter what.
1763 */
1764 if ((patterns == NULL) || (num_patterns == 0))
1765 return(DM_RET_DESCEND | DM_RET_COPY);
1766
1767 for (i = 0; i < num_patterns; i++) {
1768 struct bus_match_pattern *cur_pattern;
1769
1770 /*
1771 * If the pattern in question isn't for a bus node, we
1772 * aren't interested. However, we do indicate to the
1773 * calling routine that we should continue descending the
1774 * tree, since the user wants to match against lower-level
1775 * EDT elements.
1776 */
1777 if (patterns[i].type != DEV_MATCH_BUS) {
1778 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1779 retval |= DM_RET_DESCEND;
1780 continue;
1781 }
1782
1783 cur_pattern = &patterns[i].pattern.bus_pattern;
1784
1785 /*
1786 * If they want to match any bus node, we give them any
1787 * device node.
1788 */
1789 if (cur_pattern->flags == BUS_MATCH_ANY) {
1790 /* set the copy flag */
1791 retval |= DM_RET_COPY;
1792
1793 /*
1794 * If we've already decided on an action, go ahead
1795 * and return.
1796 */
1797 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1798 return(retval);
1799 }
1800
1801 /*
1802 * Not sure why someone would do this...
1803 */
1804 if (cur_pattern->flags == BUS_MATCH_NONE)
1805 continue;
1806
1807 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1808 && (cur_pattern->path_id != bus->path_id))
1809 continue;
1810
1811 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1812 && (cur_pattern->bus_id != bus->sim->bus_id))
1813 continue;
1814
1815 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1816 && (cur_pattern->unit_number != bus->sim->unit_number))
1817 continue;
1818
1819 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1820 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1821 DEV_IDLEN) != 0))
1822 continue;
1823
1824 /*
1825 * If we get to this point, the user definitely wants
1826 * information on this bus. So tell the caller to copy the
1827 * data out.
1828 */
1829 retval |= DM_RET_COPY;
1830
1831 /*
1832 * If the return action has been set to descend, then we
1833 * know that we've already seen a non-bus matching
1834 * expression, therefore we need to further descend the tree.
1835 * This won't change by continuing around the loop, so we
1836 * go ahead and return. If we haven't seen a non-bus
1837 * matching expression, we keep going around the loop until
1838 * we exhaust the matching expressions. We'll set the stop
1839 * flag once we fall out of the loop.
1840 */
1841 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1842 return(retval);
1843 }
1844
1845 /*
1846 * If the return action hasn't been set to descend yet, that means
1847 * we haven't seen anything other than bus matching patterns. So
1848 * tell the caller to stop descending the tree -- the user doesn't
1849 * want to match against lower level tree elements.
1850 */
1851 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1852 retval |= DM_RET_STOP;
1853
1854 return(retval);
1855}
1856
1857static dev_match_ret
1858xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1859 struct cam_ed *device)
1860{
1861 dev_match_ret retval;
1862 int i;
1863
1864 retval = DM_RET_NONE;
1865
1866 /*
1867 * If we aren't given something to match against, that's an error.
1868 */
1869 if (device == NULL)
1870 return(DM_RET_ERROR);
1871
1872 /*
1873 * If there are no match entries, then this device matches no
1874 * matter what.
1875 */
1876 if ((patterns == NULL) || (patterns == 0))
1877 return(DM_RET_DESCEND | DM_RET_COPY);
1878
1879 for (i = 0; i < num_patterns; i++) {
1880 struct device_match_pattern *cur_pattern;
1881
1882 /*
1883 * If the pattern in question isn't for a device node, we
1884 * aren't interested.
1885 */
1886 if (patterns[i].type != DEV_MATCH_DEVICE) {
1887 if ((patterns[i].type == DEV_MATCH_PERIPH)
1888 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1889 retval |= DM_RET_DESCEND;
1890 continue;
1891 }
1892
1893 cur_pattern = &patterns[i].pattern.device_pattern;
1894
1895 /*
1896 * If they want to match any device node, we give them any
1897 * device node.
1898 */
1899 if (cur_pattern->flags == DEV_MATCH_ANY) {
1900 /* set the copy flag */
1901 retval |= DM_RET_COPY;
1902
1903
1904 /*
1905 * If we've already decided on an action, go ahead
1906 * and return.
1907 */
1908 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1909 return(retval);
1910 }
1911
1912 /*
1913 * Not sure why someone would do this...
1914 */
1915 if (cur_pattern->flags == DEV_MATCH_NONE)
1916 continue;
1917
1918 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1919 && (cur_pattern->path_id != device->target->bus->path_id))
1920 continue;
1921
1922 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1923 && (cur_pattern->target_id != device->target->target_id))
1924 continue;
1925
1926 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1927 && (cur_pattern->target_lun != device->lun_id))
1928 continue;
1929
1930 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1931 && (cam_quirkmatch((caddr_t)&device->inq_data,
1932 (caddr_t)&cur_pattern->inq_pat,
1933 1, sizeof(cur_pattern->inq_pat),
1934 scsi_static_inquiry_match) == NULL))
1935 continue;
1936
1937 /*
1938 * If we get to this point, the user definitely wants
1939 * information on this device. So tell the caller to copy
1940 * the data out.
1941 */
1942 retval |= DM_RET_COPY;
1943
1944 /*
1945 * If the return action has been set to descend, then we
1946 * know that we've already seen a peripheral matching
1947 * expression, therefore we need to further descend the tree.
1948 * This won't change by continuing around the loop, so we
1949 * go ahead and return. If we haven't seen a peripheral
1950 * matching expression, we keep going around the loop until
1951 * we exhaust the matching expressions. We'll set the stop
1952 * flag once we fall out of the loop.
1953 */
1954 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1955 return(retval);
1956 }
1957
1958 /*
1959 * If the return action hasn't been set to descend yet, that means
1960 * we haven't seen any peripheral matching patterns. So tell the
1961 * caller to stop descending the tree -- the user doesn't want to
1962 * match against lower level tree elements.
1963 */
1964 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1965 retval |= DM_RET_STOP;
1966
1967 return(retval);
1968}
1969
1970/*
1971 * Match a single peripheral against any number of match patterns.
1972 */
1973static dev_match_ret
1974xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1975 struct cam_periph *periph)
1976{
1977 dev_match_ret retval;
1978 int i;
1979
1980 /*
1981 * If we aren't given something to match against, that's an error.
1982 */
1983 if (periph == NULL)
1984 return(DM_RET_ERROR);
1985
1986 /*
1987 * If there are no match entries, then this peripheral matches no
1988 * matter what.
1989 */
1990 if ((patterns == NULL) || (num_patterns == 0))
1991 return(DM_RET_STOP | DM_RET_COPY);
1992
1993 /*
1994 * There aren't any nodes below a peripheral node, so there's no
1995 * reason to descend the tree any further.
1996 */
1997 retval = DM_RET_STOP;
1998
1999 for (i = 0; i < num_patterns; i++) {
2000 struct periph_match_pattern *cur_pattern;
2001
2002 /*
2003 * If the pattern in question isn't for a peripheral, we
2004 * aren't interested.
2005 */
2006 if (patterns[i].type != DEV_MATCH_PERIPH)
2007 continue;
2008
2009 cur_pattern = &patterns[i].pattern.periph_pattern;
2010
2011 /*
2012 * If they want to match on anything, then we will do so.
2013 */
2014 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
2015 /* set the copy flag */
2016 retval |= DM_RET_COPY;
2017
2018 /*
2019 * We've already set the return action to stop,
2020 * since there are no nodes below peripherals in
2021 * the tree.
2022 */
2023 return(retval);
2024 }
2025
2026 /*
2027 * Not sure why someone would do this...
2028 */
2029 if (cur_pattern->flags == PERIPH_MATCH_NONE)
2030 continue;
2031
2032 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
2033 && (cur_pattern->path_id != periph->path->bus->path_id))
2034 continue;
2035
2036 /*
2037 * For the target and lun id's, we have to make sure the
2038 * target and lun pointers aren't NULL. The xpt peripheral
2039 * has a wildcard target and device.
2040 */
2041 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
2042 && ((periph->path->target == NULL)
2043 ||(cur_pattern->target_id != periph->path->target->target_id)))
2044 continue;
2045
2046 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
2047 && ((periph->path->device == NULL)
2048 || (cur_pattern->target_lun != periph->path->device->lun_id)))
2049 continue;
2050
2051 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
2052 && (cur_pattern->unit_number != periph->unit_number))
2053 continue;
2054
2055 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
2056 && (strncmp(cur_pattern->periph_name, periph->periph_name,
2057 DEV_IDLEN) != 0))
2058 continue;
2059
2060 /*
2061 * If we get to this point, the user definitely wants
2062 * information on this peripheral. So tell the caller to
2063 * copy the data out.
2064 */
2065 retval |= DM_RET_COPY;
2066
2067 /*
2068 * The return action has already been set to stop, since
2069 * peripherals don't have any nodes below them in the EDT.
2070 */
2071 return(retval);
2072 }
2073
2074 /*
2075 * If we get to this point, the peripheral that was passed in
2076 * doesn't match any of the patterns.
2077 */
2078 return(retval);
2079}
2080
2081static int
2082xptedtbusfunc(struct cam_eb *bus, void *arg)
2083{
2084 struct ccb_dev_match *cdm;
2085 dev_match_ret retval;
2086
2087 cdm = (struct ccb_dev_match *)arg;
2088
2089 /*
2090 * If our position is for something deeper in the tree, that means
2091 * that we've already seen this node. So, we keep going down.
2092 */
2093 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2094 && (cdm->pos.cookie.bus == bus)
2095 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2096 && (cdm->pos.cookie.target != NULL))
2097 retval = DM_RET_DESCEND;
2098 else
2099 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
2100
2101 /*
2102 * If we got an error, bail out of the search.
2103 */
2104 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2105 cdm->status = CAM_DEV_MATCH_ERROR;
2106 return(0);
2107 }
2108
2109 /*
2110 * If the copy flag is set, copy this bus out.
2111 */
2112 if (retval & DM_RET_COPY) {
2113 int spaceleft, j;
2114
2115 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2116 sizeof(struct dev_match_result));
2117
2118 /*
2119 * If we don't have enough space to put in another
2120 * match result, save our position and tell the
2121 * user there are more devices to check.
2122 */
2123 if (spaceleft < sizeof(struct dev_match_result)) {
2124 bzero(&cdm->pos, sizeof(cdm->pos));
2125 cdm->pos.position_type =
2126 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
2127
2128 cdm->pos.cookie.bus = bus;
2129 cdm->pos.generations[CAM_BUS_GENERATION]=
2130 bus_generation;
2131 cdm->status = CAM_DEV_MATCH_MORE;
2132 return(0);
2133 }
2134 j = cdm->num_matches;
2135 cdm->num_matches++;
2136 cdm->matches[j].type = DEV_MATCH_BUS;
2137 cdm->matches[j].result.bus_result.path_id = bus->path_id;
2138 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
2139 cdm->matches[j].result.bus_result.unit_number =
2140 bus->sim->unit_number;
2141 strncpy(cdm->matches[j].result.bus_result.dev_name,
2142 bus->sim->sim_name, DEV_IDLEN);
2143 }
2144
2145 /*
2146 * If the user is only interested in busses, there's no
2147 * reason to descend to the next level in the tree.
2148 */
2149 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2150 return(1);
2151
2152 /*
2153 * If there is a target generation recorded, check it to
2154 * make sure the target list hasn't changed.
2155 */
2156 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2157 && (bus == cdm->pos.cookie.bus)
2158 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2159 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
2160 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
2161 bus->generation)) {
2162 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2163 return(0);
2164 }
2165
2166 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2167 && (cdm->pos.cookie.bus == bus)
2168 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2169 && (cdm->pos.cookie.target != NULL))
2170 return(xpttargettraverse(bus,
2171 (struct cam_et *)cdm->pos.cookie.target,
2172 xptedttargetfunc, arg));
2173 else
2174 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2175}
2176
2177static int
2178xptedttargetfunc(struct cam_et *target, void *arg)
2179{
2180 struct ccb_dev_match *cdm;
2181
2182 cdm = (struct ccb_dev_match *)arg;
2183
2184 /*
2185 * If there is a device list generation recorded, check it to
2186 * make sure the device list hasn't changed.
2187 */
2188 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2189 && (cdm->pos.cookie.bus == target->bus)
2190 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2191 && (cdm->pos.cookie.target == target)
2192 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2193 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2194 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2195 target->generation)) {
2196 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2197 return(0);
2198 }
2199
2200 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2201 && (cdm->pos.cookie.bus == target->bus)
2202 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2203 && (cdm->pos.cookie.target == target)
2204 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2205 && (cdm->pos.cookie.device != NULL))
2206 return(xptdevicetraverse(target,
2207 (struct cam_ed *)cdm->pos.cookie.device,
2208 xptedtdevicefunc, arg));
2209 else
2210 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2211}
2212
2213static int
2214xptedtdevicefunc(struct cam_ed *device, void *arg)
2215{
2216
2217 struct ccb_dev_match *cdm;
2218 dev_match_ret retval;
2219
2220 cdm = (struct ccb_dev_match *)arg;
2221
2222 /*
2223 * If our position is for something deeper in the tree, that means
2224 * that we've already seen this node. So, we keep going down.
2225 */
2226 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2227 && (cdm->pos.cookie.device == device)
2228 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2229 && (cdm->pos.cookie.periph != NULL))
2230 retval = DM_RET_DESCEND;
2231 else
2232 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2233 device);
2234
2235 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2236 cdm->status = CAM_DEV_MATCH_ERROR;
2237 return(0);
2238 }
2239
2240 /*
2241 * If the copy flag is set, copy this device out.
2242 */
2243 if (retval & DM_RET_COPY) {
2244 int spaceleft, j;
2245
2246 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2247 sizeof(struct dev_match_result));
2248
2249 /*
2250 * If we don't have enough space to put in another
2251 * match result, save our position and tell the
2252 * user there are more devices to check.
2253 */
2254 if (spaceleft < sizeof(struct dev_match_result)) {
2255 bzero(&cdm->pos, sizeof(cdm->pos));
2256 cdm->pos.position_type =
2257 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2258 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2259
2260 cdm->pos.cookie.bus = device->target->bus;
2261 cdm->pos.generations[CAM_BUS_GENERATION]=
2262 bus_generation;
2263 cdm->pos.cookie.target = device->target;
2264 cdm->pos.generations[CAM_TARGET_GENERATION] =
2265 device->target->bus->generation;
2266 cdm->pos.cookie.device = device;
2267 cdm->pos.generations[CAM_DEV_GENERATION] =
2268 device->target->generation;
2269 cdm->status = CAM_DEV_MATCH_MORE;
2270 return(0);
2271 }
2272 j = cdm->num_matches;
2273 cdm->num_matches++;
2274 cdm->matches[j].type = DEV_MATCH_DEVICE;
2275 cdm->matches[j].result.device_result.path_id =
2276 device->target->bus->path_id;
2277 cdm->matches[j].result.device_result.target_id =
2278 device->target->target_id;
2279 cdm->matches[j].result.device_result.target_lun =
2280 device->lun_id;
2281 bcopy(&device->inq_data,
2282 &cdm->matches[j].result.device_result.inq_data,
2283 sizeof(struct scsi_inquiry_data));
2284
2285 /* Let the user know whether this device is unconfigured */
2286 if (device->flags & CAM_DEV_UNCONFIGURED)
2287 cdm->matches[j].result.device_result.flags =
2288 DEV_RESULT_UNCONFIGURED;
2289 else
2290 cdm->matches[j].result.device_result.flags =
2291 DEV_RESULT_NOFLAG;
2292 }
2293
2294 /*
2295 * If the user isn't interested in peripherals, don't descend
2296 * the tree any further.
2297 */
2298 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2299 return(1);
2300
2301 /*
2302 * If there is a peripheral list generation recorded, make sure
2303 * it hasn't changed.
2304 */
2305 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2306 && (device->target->bus == cdm->pos.cookie.bus)
2307 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2308 && (device->target == cdm->pos.cookie.target)
2309 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2310 && (device == cdm->pos.cookie.device)
2311 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2312 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2313 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2314 device->generation)){
2315 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2316 return(0);
2317 }
2318
2319 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2320 && (cdm->pos.cookie.bus == device->target->bus)
2321 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2322 && (cdm->pos.cookie.target == device->target)
2323 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2324 && (cdm->pos.cookie.device == device)
2325 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2326 && (cdm->pos.cookie.periph != NULL))
2327 return(xptperiphtraverse(device,
2328 (struct cam_periph *)cdm->pos.cookie.periph,
2329 xptedtperiphfunc, arg));
2330 else
2331 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2332}
2333
2334static int
2335xptedtperiphfunc(struct cam_periph *periph, void *arg)
2336{
2337 struct ccb_dev_match *cdm;
2338 dev_match_ret retval;
2339
2340 cdm = (struct ccb_dev_match *)arg;
2341
2342 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2343
2344 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2345 cdm->status = CAM_DEV_MATCH_ERROR;
2346 return(0);
2347 }
2348
2349 /*
2350 * If the copy flag is set, copy this peripheral out.
2351 */
2352 if (retval & DM_RET_COPY) {
2353 int spaceleft, j;
2354
2355 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2356 sizeof(struct dev_match_result));
2357
2358 /*
2359 * If we don't have enough space to put in another
2360 * match result, save our position and tell the
2361 * user there are more devices to check.
2362 */
2363 if (spaceleft < sizeof(struct dev_match_result)) {
2364 bzero(&cdm->pos, sizeof(cdm->pos));
2365 cdm->pos.position_type =
2366 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2367 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2368 CAM_DEV_POS_PERIPH;
2369
2370 cdm->pos.cookie.bus = periph->path->bus;
2371 cdm->pos.generations[CAM_BUS_GENERATION]=
2372 bus_generation;
2373 cdm->pos.cookie.target = periph->path->target;
2374 cdm->pos.generations[CAM_TARGET_GENERATION] =
2375 periph->path->bus->generation;
2376 cdm->pos.cookie.device = periph->path->device;
2377 cdm->pos.generations[CAM_DEV_GENERATION] =
2378 periph->path->target->generation;
2379 cdm->pos.cookie.periph = periph;
2380 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2381 periph->path->device->generation;
2382 cdm->status = CAM_DEV_MATCH_MORE;
2383 return(0);
2384 }
2385
2386 j = cdm->num_matches;
2387 cdm->num_matches++;
2388 cdm->matches[j].type = DEV_MATCH_PERIPH;
2389 cdm->matches[j].result.periph_result.path_id =
2390 periph->path->bus->path_id;
2391 cdm->matches[j].result.periph_result.target_id =
2392 periph->path->target->target_id;
2393 cdm->matches[j].result.periph_result.target_lun =
2394 periph->path->device->lun_id;
2395 cdm->matches[j].result.periph_result.unit_number =
2396 periph->unit_number;
2397 strncpy(cdm->matches[j].result.periph_result.periph_name,
2398 periph->periph_name, DEV_IDLEN);
2399 }
2400
2401 return(1);
2402}
2403
2404static int
2405xptedtmatch(struct ccb_dev_match *cdm)
2406{
2407 int ret;
2408
2409 cdm->num_matches = 0;
2410
2411 /*
2412 * Check the bus list generation. If it has changed, the user
2413 * needs to reset everything and start over.
2414 */
2415 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2416 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2417 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2418 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2419 return(0);
2420 }
2421
2422 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2423 && (cdm->pos.cookie.bus != NULL))
2424 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2425 xptedtbusfunc, cdm);
2426 else
2427 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2428
2429 /*
2430 * If we get back 0, that means that we had to stop before fully
2431 * traversing the EDT. It also means that one of the subroutines
2432 * has set the status field to the proper value. If we get back 1,
2433 * we've fully traversed the EDT and copied out any matching entries.
2434 */
2435 if (ret == 1)
2436 cdm->status = CAM_DEV_MATCH_LAST;
2437
2438 return(ret);
2439}
2440
2441static int
2442xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2443{
2444 struct ccb_dev_match *cdm;
2445
2446 cdm = (struct ccb_dev_match *)arg;
2447
2448 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2449 && (cdm->pos.cookie.pdrv == pdrv)
2450 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2451 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2452 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2453 (*pdrv)->generation)) {
2454 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2455 return(0);
2456 }
2457
2458 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2459 && (cdm->pos.cookie.pdrv == pdrv)
2460 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2461 && (cdm->pos.cookie.periph != NULL))
2462 return(xptpdperiphtraverse(pdrv,
2463 (struct cam_periph *)cdm->pos.cookie.periph,
2464 xptplistperiphfunc, arg));
2465 else
2466 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2467}
2468
2469static int
2470xptplistperiphfunc(struct cam_periph *periph, void *arg)
2471{
2472 struct ccb_dev_match *cdm;
2473 dev_match_ret retval;
2474
2475 cdm = (struct ccb_dev_match *)arg;
2476
2477 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2478
2479 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2480 cdm->status = CAM_DEV_MATCH_ERROR;
2481 return(0);
2482 }
2483
2484 /*
2485 * If the copy flag is set, copy this peripheral out.
2486 */
2487 if (retval & DM_RET_COPY) {
2488 int spaceleft, j;
2489
2490 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2491 sizeof(struct dev_match_result));
2492
2493 /*
2494 * If we don't have enough space to put in another
2495 * match result, save our position and tell the
2496 * user there are more devices to check.
2497 */
2498 if (spaceleft < sizeof(struct dev_match_result)) {
2499 struct periph_driver **pdrv;
2500
2501 pdrv = NULL;
2502 bzero(&cdm->pos, sizeof(cdm->pos));
2503 cdm->pos.position_type =
2504 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2505 CAM_DEV_POS_PERIPH;
2506
2507 /*
2508 * This may look a bit non-sensical, but it is
2509 * actually quite logical. There are very few
2510 * peripheral drivers, and bloating every peripheral
2511 * structure with a pointer back to its parent
2512 * peripheral driver linker set entry would cost
2513 * more in the long run than doing this quick lookup.
2514 */
2515 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2516 if (strcmp((*pdrv)->driver_name,
2517 periph->periph_name) == 0)
2518 break;
2519 }
2520
2521 if (pdrv == NULL) {
2522 cdm->status = CAM_DEV_MATCH_ERROR;
2523 return(0);
2524 }
2525
2526 cdm->pos.cookie.pdrv = pdrv;
2527 /*
2528 * The periph generation slot does double duty, as
2529 * does the periph pointer slot. They are used for
2530 * both edt and pdrv lookups and positioning.
2531 */
2532 cdm->pos.cookie.periph = periph;
2533 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2534 (*pdrv)->generation;
2535 cdm->status = CAM_DEV_MATCH_MORE;
2536 return(0);
2537 }
2538
2539 j = cdm->num_matches;
2540 cdm->num_matches++;
2541 cdm->matches[j].type = DEV_MATCH_PERIPH;
2542 cdm->matches[j].result.periph_result.path_id =
2543 periph->path->bus->path_id;
2544
2545 /*
2546 * The transport layer peripheral doesn't have a target or
2547 * lun.
2548 */
2549 if (periph->path->target)
2550 cdm->matches[j].result.periph_result.target_id =
2551 periph->path->target->target_id;
2552 else
2553 cdm->matches[j].result.periph_result.target_id = -1;
2554
2555 if (periph->path->device)
2556 cdm->matches[j].result.periph_result.target_lun =
2557 periph->path->device->lun_id;
2558 else
2559 cdm->matches[j].result.periph_result.target_lun = -1;
2560
2561 cdm->matches[j].result.periph_result.unit_number =
2562 periph->unit_number;
2563 strncpy(cdm->matches[j].result.periph_result.periph_name,
2564 periph->periph_name, DEV_IDLEN);
2565 }
2566
2567 return(1);
2568}
2569
2570static int
2571xptperiphlistmatch(struct ccb_dev_match *cdm)
2572{
2573 int ret;
2574
2575 cdm->num_matches = 0;
2576
2577 /*
2578 * At this point in the edt traversal function, we check the bus
2579 * list generation to make sure that no busses have been added or
2580 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2581 * For the peripheral driver list traversal function, however, we
2582 * don't have to worry about new peripheral driver types coming or
2583 * going; they're in a linker set, and therefore can't change
2584 * without a recompile.
2585 */
2586
2587 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2588 && (cdm->pos.cookie.pdrv != NULL))
2589 ret = xptpdrvtraverse(
2590 (struct periph_driver **)cdm->pos.cookie.pdrv,
2591 xptplistpdrvfunc, cdm);
2592 else
2593 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2594
2595 /*
2596 * If we get back 0, that means that we had to stop before fully
2597 * traversing the peripheral driver tree. It also means that one of
2598 * the subroutines has set the status field to the proper value. If
2599 * we get back 1, we've fully traversed the EDT and copied out any
2600 * matching entries.
2601 */
2602 if (ret == 1)
2603 cdm->status = CAM_DEV_MATCH_LAST;
2604
2605 return(ret);
2606}
2607
2608static int
2609xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2610{
2611 struct cam_eb *bus, *next_bus;
2612 int retval;
2613
2614 retval = 1;
2615
2616 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2617 bus != NULL;
2618 bus = next_bus) {
2619 next_bus = TAILQ_NEXT(bus, links);
2620
2621 retval = tr_func(bus, arg);
2622 if (retval == 0)
2623 return(retval);
2624 }
2625
2626 return(retval);
2627}
2628
2629static int
2630xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2631 xpt_targetfunc_t *tr_func, void *arg)
2632{
2633 struct cam_et *target, *next_target;
2634 int retval;
2635
2636 retval = 1;
2637 for (target = (start_target ? start_target :
2638 TAILQ_FIRST(&bus->et_entries));
2639 target != NULL; target = next_target) {
2640
2641 next_target = TAILQ_NEXT(target, links);
2642
2643 retval = tr_func(target, arg);
2644
2645 if (retval == 0)
2646 return(retval);
2647 }
2648
2649 return(retval);
2650}
2651
2652static int
2653xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2654 xpt_devicefunc_t *tr_func, void *arg)
2655{
2656 struct cam_ed *device, *next_device;
2657 int retval;
2658
2659 retval = 1;
2660 for (device = (start_device ? start_device :
2661 TAILQ_FIRST(&target->ed_entries));
2662 device != NULL;
2663 device = next_device) {
2664
2665 next_device = TAILQ_NEXT(device, links);
2666
2667 retval = tr_func(device, arg);
2668
2669 if (retval == 0)
2670 return(retval);
2671 }
2672
2673 return(retval);
2674}
2675
2676static int
2677xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2678 xpt_periphfunc_t *tr_func, void *arg)
2679{
2680 struct cam_periph *periph, *next_periph;
2681 int retval;
2682
2683 retval = 1;
2684
2685 for (periph = (start_periph ? start_periph :
2686 SLIST_FIRST(&device->periphs));
2687 periph != NULL;
2688 periph = next_periph) {
2689
2690 next_periph = SLIST_NEXT(periph, periph_links);
2691
2692 retval = tr_func(periph, arg);
2693 if (retval == 0)
2694 return(retval);
2695 }
2696
2697 return(retval);
2698}
2699
2700static int
2701xptpdrvtraverse(struct periph_driver **start_pdrv,
2702 xpt_pdrvfunc_t *tr_func, void *arg)
2703{
2704 struct periph_driver **pdrv;
2705 int retval;
2706
2707 retval = 1;
2708
2709 /*
2710 * We don't traverse the peripheral driver list like we do the
2711 * other lists, because it is a linker set, and therefore cannot be
2712 * changed during runtime. If the peripheral driver list is ever
2713 * re-done to be something other than a linker set (i.e. it can
2714 * change while the system is running), the list traversal should
2715 * be modified to work like the other traversal functions.
2716 */
2717 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2718 *pdrv != NULL; pdrv++) {
2719 retval = tr_func(pdrv, arg);
2720
2721 if (retval == 0)
2722 return(retval);
2723 }
2724
2725 return(retval);
2726}
2727
2728static int
2729xptpdperiphtraverse(struct periph_driver **pdrv,
2730 struct cam_periph *start_periph,
2731 xpt_periphfunc_t *tr_func, void *arg)
2732{
2733 struct cam_periph *periph, *next_periph;
2734 int retval;
2735
2736 retval = 1;
2737
2738 for (periph = (start_periph ? start_periph :
2739 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2740 periph = next_periph) {
2741
2742 next_periph = TAILQ_NEXT(periph, unit_links);
2743
2744 retval = tr_func(periph, arg);
2745 if (retval == 0)
2746 return(retval);
2747 }
2748 return(retval);
2749}
2750
2751static int
2752xptdefbusfunc(struct cam_eb *bus, void *arg)
2753{
2754 struct xpt_traverse_config *tr_config;
2755
2756 tr_config = (struct xpt_traverse_config *)arg;
2757
2758 if (tr_config->depth == XPT_DEPTH_BUS) {
2759 xpt_busfunc_t *tr_func;
2760
2761 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2762
2763 return(tr_func(bus, tr_config->tr_arg));
2764 } else
2765 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2766}
2767
2768static int
2769xptdeftargetfunc(struct cam_et *target, void *arg)
2770{
2771 struct xpt_traverse_config *tr_config;
2772
2773 tr_config = (struct xpt_traverse_config *)arg;
2774
2775 if (tr_config->depth == XPT_DEPTH_TARGET) {
2776 xpt_targetfunc_t *tr_func;
2777
2778 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2779
2780 return(tr_func(target, tr_config->tr_arg));
2781 } else
2782 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2783}
2784
2785static int
2786xptdefdevicefunc(struct cam_ed *device, void *arg)
2787{
2788 struct xpt_traverse_config *tr_config;
2789
2790 tr_config = (struct xpt_traverse_config *)arg;
2791
2792 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2793 xpt_devicefunc_t *tr_func;
2794
2795 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2796
2797 return(tr_func(device, tr_config->tr_arg));
2798 } else
2799 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2800}
2801
2802static int
2803xptdefperiphfunc(struct cam_periph *periph, void *arg)
2804{
2805 struct xpt_traverse_config *tr_config;
2806 xpt_periphfunc_t *tr_func;
2807
2808 tr_config = (struct xpt_traverse_config *)arg;
2809
2810 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2811
2812 /*
2813 * Unlike the other default functions, we don't check for depth
2814 * here. The peripheral driver level is the last level in the EDT,
2815 * so if we're here, we should execute the function in question.
2816 */
2817 return(tr_func(periph, tr_config->tr_arg));
2818}
2819
2820/*
2821 * Execute the given function for every bus in the EDT.
2822 */
2823static int
2824xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2825{
2826 struct xpt_traverse_config tr_config;
2827
2828 tr_config.depth = XPT_DEPTH_BUS;
2829 tr_config.tr_func = tr_func;
2830 tr_config.tr_arg = arg;
2831
2832 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2833}
2834
2835#ifdef notusedyet
2836/*
2837 * Execute the given function for every target in the EDT.
2838 */
2839static int
2840xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2841{
2842 struct xpt_traverse_config tr_config;
2843
2844 tr_config.depth = XPT_DEPTH_TARGET;
2845 tr_config.tr_func = tr_func;
2846 tr_config.tr_arg = arg;
2847
2848 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2849}
2850#endif /* notusedyet */
2851
2852/*
2853 * Execute the given function for every device in the EDT.
2854 */
2855static int
2856xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2857{
2858 struct xpt_traverse_config tr_config;
2859
2860 tr_config.depth = XPT_DEPTH_DEVICE;
2861 tr_config.tr_func = tr_func;
2862 tr_config.tr_arg = arg;
2863
2864 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2865}
2866
2867#ifdef notusedyet
2868/*
2869 * Execute the given function for every peripheral in the EDT.
2870 */
2871static int
2872xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2873{
2874 struct xpt_traverse_config tr_config;
2875
2876 tr_config.depth = XPT_DEPTH_PERIPH;
2877 tr_config.tr_func = tr_func;
2878 tr_config.tr_arg = arg;
2879
2880 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2881}
2882#endif /* notusedyet */
2883
2884static int
2885xptsetasyncfunc(struct cam_ed *device, void *arg)
2886{
2887 struct cam_path path;
2888 struct ccb_getdev cgd;
2889 struct async_node *cur_entry;
2890
2891 cur_entry = (struct async_node *)arg;
2892
2893 /*
2894 * Don't report unconfigured devices (Wildcard devs,
2895 * devices only for target mode, device instances
2896 * that have been invalidated but are waiting for
2897 * their last reference count to be released).
2898 */
2899 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2900 return (1);
2901
2902 xpt_compile_path(&path,
2903 NULL,
2904 device->target->bus->path_id,
2905 device->target->target_id,
2906 device->lun_id);
2907 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2908 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2909 xpt_action((union ccb *)&cgd);
2910 cur_entry->callback(cur_entry->callback_arg,
2911 AC_FOUND_DEVICE,
2912 &path, &cgd);
2913 xpt_release_path(&path);
2914
2915 return(1);
2916}
2917
2918static int
2919xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2920{
2921 struct cam_path path;
2922 struct ccb_pathinq cpi;
2923 struct async_node *cur_entry;
2924
2925 cur_entry = (struct async_node *)arg;
2926
2927 xpt_compile_path(&path, /*periph*/NULL,
2928 bus->sim->path_id,
2929 CAM_TARGET_WILDCARD,
2930 CAM_LUN_WILDCARD);
2931 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2932 cpi.ccb_h.func_code = XPT_PATH_INQ;
2933 xpt_action((union ccb *)&cpi);
2934 cur_entry->callback(cur_entry->callback_arg,
2935 AC_PATH_REGISTERED,
2936 &path, &cpi);
2937 xpt_release_path(&path);
2938
2939 return(1);
2940}
2941
2942void
2943xpt_action(union ccb *start_ccb)
2944{
2945 int iopl;
2946
2947 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2948
2949 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2950
2951 iopl = splsoftcam();
2952 switch (start_ccb->ccb_h.func_code) {
2953 case XPT_SCSI_IO:
2954 {
2955#ifdef CAM_NEW_TRAN_CODE
2956 struct cam_ed *device;
2957#endif /* CAM_NEW_TRAN_CODE */
2958#ifdef CAMDEBUG
2959 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2960 struct cam_path *path;
2961
2962 path = start_ccb->ccb_h.path;
2963#endif
2964
2965 /*
2966 * For the sake of compatibility with SCSI-1
2967 * devices that may not understand the identify
2968 * message, we include lun information in the
2969 * second byte of all commands. SCSI-1 specifies
2970 * that luns are a 3 bit value and reserves only 3
2971 * bits for lun information in the CDB. Later
2972 * revisions of the SCSI spec allow for more than 8
2973 * luns, but have deprecated lun information in the
2974 * CDB. So, if the lun won't fit, we must omit.
2975 *
2976 * Also be aware that during initial probing for devices,
2977 * the inquiry information is unknown but initialized to 0.
2978 * This means that this code will be exercised while probing
2979 * devices with an ANSI revision greater than 2.
2980 */
2981#ifdef CAM_NEW_TRAN_CODE
2982 device = start_ccb->ccb_h.path->device;
2983 if (device->protocol_version <= SCSI_REV_2
2984#else /* CAM_NEW_TRAN_CODE */
2985 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
2986#endif /* CAM_NEW_TRAN_CODE */
2987 && start_ccb->ccb_h.target_lun < 8
2988 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
2989
2990 start_ccb->csio.cdb_io.cdb_bytes[1] |=
2991 start_ccb->ccb_h.target_lun << 5;
2992 }
2993 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
2994 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
2995 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
2996 &path->device->inq_data),
2997 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
2998 cdb_str, sizeof(cdb_str))));
2999 /* FALLTHROUGH */
3000 }
3001 case XPT_TARGET_IO:
3002 case XPT_CONT_TARGET_IO:
3003 start_ccb->csio.sense_resid = 0;
3004 start_ccb->csio.resid = 0;
3005 /* FALLTHROUGH */
3006 case XPT_RESET_DEV:
3007 case XPT_ENG_EXEC:
3008 {
3009 struct cam_path *path;
3010 int s;
3011 int runq;
3012
3013 path = start_ccb->ccb_h.path;
3014 s = splsoftcam();
3015
3016 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
3017 if (path->device->qfrozen_cnt == 0)
3018 runq = xpt_schedule_dev_sendq(path->bus, path->device);
3019 else
3020 runq = 0;
3021 splx(s);
3022 if (runq != 0)
3023 xpt_run_dev_sendq(path->bus);
3024 break;
3025 }
3026 case XPT_SET_TRAN_SETTINGS:
3027 {
3028 xpt_set_transfer_settings(&start_ccb->cts,
3029 start_ccb->ccb_h.path->device,
3030 /*async_update*/FALSE);
3031 break;
3032 }
3033 case XPT_CALC_GEOMETRY:
3034 {
3035 struct cam_sim *sim;
3036
3037 /* Filter out garbage */
3038 if (start_ccb->ccg.block_size == 0
3039 || start_ccb->ccg.volume_size == 0) {
3040 start_ccb->ccg.cylinders = 0;
3041 start_ccb->ccg.heads = 0;
3042 start_ccb->ccg.secs_per_track = 0;
3043 start_ccb->ccb_h.status = CAM_REQ_CMP;
3044 break;
3045 }
3046#ifdef PC98
3047 /*
3048 * In a PC-98 system, geometry translation depens on
3049 * the "real" device geometry obtained from mode page 4.
3050 * SCSI geometry translation is performed in the
3051 * initialization routine of the SCSI BIOS and the result
3052 * stored in host memory. If the translation is available
3053 * in host memory, use it. If not, rely on the default
3054 * translation the device driver performs.
3055 */
3056 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
3057 start_ccb->ccb_h.status = CAM_REQ_CMP;
3058 break;
3059 }
3060#endif
3061 sim = start_ccb->ccb_h.path->bus->sim;
3062 (*(sim->sim_action))(sim, start_ccb);
3063 break;
3064 }
3065 case XPT_ABORT:
3066 {
3067 union ccb* abort_ccb;
3068 int s;
3069
3070 abort_ccb = start_ccb->cab.abort_ccb;
3071 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
3072
3073 if (abort_ccb->ccb_h.pinfo.index >= 0) {
3074 struct cam_ccbq *ccbq;
3075
3076 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
3077 cam_ccbq_remove_ccb(ccbq, abort_ccb);
3078 abort_ccb->ccb_h.status =
3079 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3080 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3081 s = splcam();
3082 xpt_done(abort_ccb);
3083 splx(s);
3084 start_ccb->ccb_h.status = CAM_REQ_CMP;
3085 break;
3086 }
3087 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
3088 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
3089 /*
3090 * We've caught this ccb en route to
3091 * the SIM. Flag it for abort and the
3092 * SIM will do so just before starting
3093 * real work on the CCB.
3094 */
3095 abort_ccb->ccb_h.status =
3096 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3097 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3098 start_ccb->ccb_h.status = CAM_REQ_CMP;
3099 break;
3100 }
3101 }
3102 if (XPT_FC_IS_QUEUED(abort_ccb)
3103 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
3104 /*
3105 * It's already completed but waiting
3106 * for our SWI to get to it.
3107 */
3108 start_ccb->ccb_h.status = CAM_UA_ABORT;
3109 break;
3110 }
3111 /*
3112 * If we weren't able to take care of the abort request
3113 * in the XPT, pass the request down to the SIM for processing.
3114 */
3115 /* FALLTHROUGH */
3116 }
3117 case XPT_ACCEPT_TARGET_IO:
3118 case XPT_EN_LUN:
3119 case XPT_IMMED_NOTIFY:
3120 case XPT_NOTIFY_ACK:
3121 case XPT_GET_TRAN_SETTINGS:
3122 case XPT_RESET_BUS:
3123 {
3124 struct cam_sim *sim;
3125
3126 sim = start_ccb->ccb_h.path->bus->sim;
3127 (*(sim->sim_action))(sim, start_ccb);
3128 break;
3129 }
3130 case XPT_PATH_INQ:
3131 {
3132 struct cam_sim *sim;
3133
3134 sim = start_ccb->ccb_h.path->bus->sim;
3135 (*(sim->sim_action))(sim, start_ccb);
3136 break;
3137 }
3138 case XPT_PATH_STATS:
3139 start_ccb->cpis.last_reset =
3140 start_ccb->ccb_h.path->bus->last_reset;
3141 start_ccb->ccb_h.status = CAM_REQ_CMP;
3142 break;
3143 case XPT_GDEV_TYPE:
3144 {
3145 struct cam_ed *dev;
3146 int s;
3147
3148 dev = start_ccb->ccb_h.path->device;
3149 s = splcam();
3150 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3151 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3152 } else {
3153 struct ccb_getdev *cgd;
3154 struct cam_eb *bus;
3155 struct cam_et *tar;
3156
3157 cgd = &start_ccb->cgd;
3158 bus = cgd->ccb_h.path->bus;
3159 tar = cgd->ccb_h.path->target;
3160 cgd->inq_data = dev->inq_data;
3161 cgd->ccb_h.status = CAM_REQ_CMP;
3162 cgd->serial_num_len = dev->serial_num_len;
3163 if ((dev->serial_num_len > 0)
3164 && (dev->serial_num != NULL))
3165 bcopy(dev->serial_num, cgd->serial_num,
3166 dev->serial_num_len);
3167 }
3168 splx(s);
3169 break;
3170 }
3171 case XPT_GDEV_STATS:
3172 {
3173 struct cam_ed *dev;
3174 int s;
3175
3176 dev = start_ccb->ccb_h.path->device;
3177 s = splcam();
3178 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3179 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3180 } else {
3181 struct ccb_getdevstats *cgds;
3182 struct cam_eb *bus;
3183 struct cam_et *tar;
3184
3185 cgds = &start_ccb->cgds;
3186 bus = cgds->ccb_h.path->bus;
3187 tar = cgds->ccb_h.path->target;
3188 cgds->dev_openings = dev->ccbq.dev_openings;
3189 cgds->dev_active = dev->ccbq.dev_active;
3190 cgds->devq_openings = dev->ccbq.devq_openings;
3191 cgds->devq_queued = dev->ccbq.queue.entries;
3192 cgds->held = dev->ccbq.held;
3193 cgds->last_reset = tar->last_reset;
3194 cgds->maxtags = dev->quirk->maxtags;
3195 cgds->mintags = dev->quirk->mintags;
3196 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3197 cgds->last_reset = bus->last_reset;
3198 cgds->ccb_h.status = CAM_REQ_CMP;
3199 }
3200 splx(s);
3201 break;
3202 }
3203 case XPT_GDEVLIST:
3204 {
3205 struct cam_periph *nperiph;
3206 struct periph_list *periph_head;
3207 struct ccb_getdevlist *cgdl;
3208 u_int i;
3209 int s;
3210 struct cam_ed *device;
3211 int found;
3212
3213
3214 found = 0;
3215
3216 /*
3217 * Don't want anyone mucking with our data.
3218 */
3219 s = splcam();
3220 device = start_ccb->ccb_h.path->device;
3221 periph_head = &device->periphs;
3222 cgdl = &start_ccb->cgdl;
3223
3224 /*
3225 * Check and see if the list has changed since the user
3226 * last requested a list member. If so, tell them that the
3227 * list has changed, and therefore they need to start over
3228 * from the beginning.
3229 */
3230 if ((cgdl->index != 0) &&
3231 (cgdl->generation != device->generation)) {
3232 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3233 splx(s);
3234 break;
3235 }
3236
3237 /*
3238 * Traverse the list of peripherals and attempt to find
3239 * the requested peripheral.
3240 */
3241 for (nperiph = SLIST_FIRST(periph_head), i = 0;
3242 (nperiph != NULL) && (i <= cgdl->index);
3243 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3244 if (i == cgdl->index) {
3245 strncpy(cgdl->periph_name,
3246 nperiph->periph_name,
3247 DEV_IDLEN);
3248 cgdl->unit_number = nperiph->unit_number;
3249 found = 1;
3250 }
3251 }
3252 if (found == 0) {
3253 cgdl->status = CAM_GDEVLIST_ERROR;
3254 splx(s);
3255 break;
3256 }
3257
3258 if (nperiph == NULL)
3259 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3260 else
3261 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3262
3263 cgdl->index++;
3264 cgdl->generation = device->generation;
3265
3266 splx(s);
3267 cgdl->ccb_h.status = CAM_REQ_CMP;
3268 break;
3269 }
3270 case XPT_DEV_MATCH:
3271 {
3272 int s;
3273 dev_pos_type position_type;
3274 struct ccb_dev_match *cdm;
3275 int ret;
3276
3277 cdm = &start_ccb->cdm;
3278
3279 /*
3280 * Prevent EDT changes while we traverse it.
3281 */
3282 s = splcam();
3283 /*
3284 * There are two ways of getting at information in the EDT.
3285 * The first way is via the primary EDT tree. It starts
3286 * with a list of busses, then a list of targets on a bus,
3287 * then devices/luns on a target, and then peripherals on a
3288 * device/lun. The "other" way is by the peripheral driver
3289 * lists. The peripheral driver lists are organized by
3290 * peripheral driver. (obviously) So it makes sense to
3291 * use the peripheral driver list if the user is looking
3292 * for something like "da1", or all "da" devices. If the
3293 * user is looking for something on a particular bus/target
3294 * or lun, it's generally better to go through the EDT tree.
3295 */
3296
3297 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3298 position_type = cdm->pos.position_type;
3299 else {
3300 u_int i;
3301
3302 position_type = CAM_DEV_POS_NONE;
3303
3304 for (i = 0; i < cdm->num_patterns; i++) {
3305 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3306 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3307 position_type = CAM_DEV_POS_EDT;
3308 break;
3309 }
3310 }
3311
3312 if (cdm->num_patterns == 0)
3313 position_type = CAM_DEV_POS_EDT;
3314 else if (position_type == CAM_DEV_POS_NONE)
3315 position_type = CAM_DEV_POS_PDRV;
3316 }
3317
3318 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3319 case CAM_DEV_POS_EDT:
3320 ret = xptedtmatch(cdm);
3321 break;
3322 case CAM_DEV_POS_PDRV:
3323 ret = xptperiphlistmatch(cdm);
3324 break;
3325 default:
3326 cdm->status = CAM_DEV_MATCH_ERROR;
3327 break;
3328 }
3329
3330 splx(s);
3331
3332 if (cdm->status == CAM_DEV_MATCH_ERROR)
3333 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3334 else
3335 start_ccb->ccb_h.status = CAM_REQ_CMP;
3336
3337 break;
3338 }
3339 case XPT_SASYNC_CB:
3340 {
3341 struct ccb_setasync *csa;
3342 struct async_node *cur_entry;
3343 struct async_list *async_head;
3344 u_int32_t added;
3345 int s;
3346
3347 csa = &start_ccb->csa;
3348 added = csa->event_enable;
3349 async_head = &csa->ccb_h.path->device->asyncs;
3350
3351 /*
3352 * If there is already an entry for us, simply
3353 * update it.
3354 */
3355 s = splcam();
3356 cur_entry = SLIST_FIRST(async_head);
3357 while (cur_entry != NULL) {
3358 if ((cur_entry->callback_arg == csa->callback_arg)
3359 && (cur_entry->callback == csa->callback))
3360 break;
3361 cur_entry = SLIST_NEXT(cur_entry, links);
3362 }
3363
3364 if (cur_entry != NULL) {
3365 /*
3366 * If the request has no flags set,
3367 * remove the entry.
3368 */
3369 added &= ~cur_entry->event_enable;
3370 if (csa->event_enable == 0) {
3371 SLIST_REMOVE(async_head, cur_entry,
3372 async_node, links);
3373 csa->ccb_h.path->device->refcount--;
3374 free(cur_entry, M_DEVBUF);
3375 } else {
3376 cur_entry->event_enable = csa->event_enable;
3377 }
3378 } else {
3379 cur_entry = malloc(sizeof(*cur_entry), M_DEVBUF,
3380 M_NOWAIT);
3381 if (cur_entry == NULL) {
3382 splx(s);
3383 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3384 break;
3385 }
3386 cur_entry->event_enable = csa->event_enable;
3387 cur_entry->callback_arg = csa->callback_arg;
3388 cur_entry->callback = csa->callback;
3389 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3390 csa->ccb_h.path->device->refcount++;
3391 }
3392
3393 if ((added & AC_FOUND_DEVICE) != 0) {
3394 /*
3395 * Get this peripheral up to date with all
3396 * the currently existing devices.
3397 */
3398 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3399 }
3400 if ((added & AC_PATH_REGISTERED) != 0) {
3401 /*
3402 * Get this peripheral up to date with all
3403 * the currently existing busses.
3404 */
3405 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3406 }
3407 splx(s);
3408 start_ccb->ccb_h.status = CAM_REQ_CMP;
3409 break;
3410 }
3411 case XPT_REL_SIMQ:
3412 {
3413 struct ccb_relsim *crs;
3414 struct cam_ed *dev;
3415 int s;
3416
3417 crs = &start_ccb->crs;
3418 dev = crs->ccb_h.path->device;
3419 if (dev == NULL) {
3420
3421 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3422 break;
3423 }
3424
3425 s = splcam();
3426
3427 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3428
3429 if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3430
3431 /* Don't ever go below one opening */
3432 if (crs->openings > 0) {
3433 xpt_dev_ccbq_resize(crs->ccb_h.path,
3434 crs->openings);
3435
3436 if (bootverbose) {
3437 xpt_print_path(crs->ccb_h.path);
3438 printf("tagged openings "
3439 "now %d\n",
3440 crs->openings);
3441 }
3442 }
3443 }
3444 }
3445
3446 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3447
3448 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3449
3450 /*
3451 * Just extend the old timeout and decrement
3452 * the freeze count so that a single timeout
3453 * is sufficient for releasing the queue.
3454 */
3455 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3456 untimeout(xpt_release_devq_timeout,
3457 dev, dev->c_handle);
3458 } else {
3459
3460 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3461 }
3462
3463 dev->c_handle =
3464 timeout(xpt_release_devq_timeout,
3465 dev,
3466 (crs->release_timeout * hz) / 1000);
3467
3468 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3469
3470 }
3471
3472 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3473
3474 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3475 /*
3476 * Decrement the freeze count so that a single
3477 * completion is still sufficient to unfreeze
3478 * the queue.
3479 */
3480 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3481 } else {
3482
3483 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3484 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3485 }
3486 }
3487
3488 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3489
3490 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3491 || (dev->ccbq.dev_active == 0)) {
3492
3493 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3494 } else {
3495
3496 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3497 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3498 }
3499 }
3500 splx(s);
3501
3502 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3503
3504 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3505 /*run_queue*/TRUE);
3506 }
3507 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3508 start_ccb->ccb_h.status = CAM_REQ_CMP;
3509 break;
3510 }
3511 case XPT_SCAN_BUS:
3512 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3513 break;
3514 case XPT_SCAN_LUN:
3515 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3516 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3517 start_ccb);
3518 break;
3519 case XPT_DEBUG: {
3520#ifdef CAMDEBUG
3521 int s;
3522
3523 s = splcam();
3524#ifdef CAM_DEBUG_DELAY
3525 cam_debug_delay = CAM_DEBUG_DELAY;
3526#endif
3527 cam_dflags = start_ccb->cdbg.flags;
3528 if (cam_dpath != NULL) {
3529 xpt_free_path(cam_dpath);
3530 cam_dpath = NULL;
3531 }
3532
3533 if (cam_dflags != CAM_DEBUG_NONE) {
3534 if (xpt_create_path(&cam_dpath, xpt_periph,
3535 start_ccb->ccb_h.path_id,
3536 start_ccb->ccb_h.target_id,
3537 start_ccb->ccb_h.target_lun) !=
3538 CAM_REQ_CMP) {
3539 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3540 cam_dflags = CAM_DEBUG_NONE;
3541 } else {
3542 start_ccb->ccb_h.status = CAM_REQ_CMP;
3543 xpt_print_path(cam_dpath);
3544 printf("debugging flags now %x\n", cam_dflags);
3545 }
3546 } else {
3547 cam_dpath = NULL;
3548 start_ccb->ccb_h.status = CAM_REQ_CMP;
3549 }
3550 splx(s);
3551#else /* !CAMDEBUG */
3552 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3553#endif /* CAMDEBUG */
3554 break;
3555 }
3556 case XPT_NOOP:
3557 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3558 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3559 start_ccb->ccb_h.status = CAM_REQ_CMP;
3560 break;
3561 default:
3562 case XPT_SDEV_TYPE:
3563 case XPT_TERM_IO:
3564 case XPT_ENG_INQ:
3565 /* XXX Implement */
3566 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3567 break;
3568 }
3569 splx(iopl);
3570}
3571
3572void
3573xpt_polled_action(union ccb *start_ccb)
3574{
3575 int s;
3576 u_int32_t timeout;
3577 struct cam_sim *sim;
3578 struct cam_devq *devq;
3579 struct cam_ed *dev;
3580
3581 timeout = start_ccb->ccb_h.timeout;
3582 sim = start_ccb->ccb_h.path->bus->sim;
3583 devq = sim->devq;
3584 dev = start_ccb->ccb_h.path->device;
3585
3586 s = splcam();
3587
3588 /*
3589 * Steal an opening so that no other queued requests
3590 * can get it before us while we simulate interrupts.
3591 */
3592 dev->ccbq.devq_openings--;
3593 dev->ccbq.dev_openings--;
3594
3595 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3596 && (--timeout > 0)) {
3597 DELAY(1000);
3598 (*(sim->sim_poll))(sim);
3599 camisr(&cam_netq);
3600 camisr(&cam_bioq);
3601 }
3602
3603 dev->ccbq.devq_openings++;
3604 dev->ccbq.dev_openings++;
3605
3606 if (timeout != 0) {
3607 xpt_action(start_ccb);
3608 while(--timeout > 0) {
3609 (*(sim->sim_poll))(sim);
3610 camisr(&cam_netq);
3611 camisr(&cam_bioq);
3612 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3613 != CAM_REQ_INPROG)
3614 break;
3615 DELAY(1000);
3616 }
3617 if (timeout == 0) {
3618 /*
3619 * XXX Is it worth adding a sim_timeout entry
3620 * point so we can attempt recovery? If
3621 * this is only used for dumps, I don't think
3622 * it is.
3623 */
3624 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3625 }
3626 } else {
3627 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3628 }
3629 splx(s);
3630}
3631
3632/*
3633 * Schedule a peripheral driver to receive a ccb when it's
3634 * target device has space for more transactions.
3635 */
3636void
3637xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3638{
3639 struct cam_ed *device;
3640 int s;
3641 int runq;
3642
3643 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3644 device = perph->path->device;
3645 s = splsoftcam();
3646 if (periph_is_queued(perph)) {
3647 /* Simply reorder based on new priority */
3648 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3649 (" change priority to %d\n", new_priority));
3650 if (new_priority < perph->pinfo.priority) {
3651 camq_change_priority(&device->drvq,
3652 perph->pinfo.index,
3653 new_priority);
3654 }
3655 runq = 0;
3656 } else {
3657 /* New entry on the queue */
3658 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3659 (" added periph to queue\n"));
3660 perph->pinfo.priority = new_priority;
3661 perph->pinfo.generation = ++device->drvq.generation;
3662 camq_insert(&device->drvq, &perph->pinfo);
3663 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3664 }
3665 splx(s);
3666 if (runq != 0) {
3667 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3668 (" calling xpt_run_devq\n"));
3669 xpt_run_dev_allocq(perph->path->bus);
3670 }
3671}
3672
3673
3674/*
3675 * Schedule a device to run on a given queue.
3676 * If the device was inserted as a new entry on the queue,
3677 * return 1 meaning the device queue should be run. If we
3678 * were already queued, implying someone else has already
3679 * started the queue, return 0 so the caller doesn't attempt
3680 * to run the queue. Must be run at either splsoftcam
3681 * (or splcam since that encompases splsoftcam).
3682 */
3683static int
3684xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3685 u_int32_t new_priority)
3686{
3687 int retval;
3688 u_int32_t old_priority;
3689
3690 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3691
3692 old_priority = pinfo->priority;
3693
3694 /*
3695 * Are we already queued?
3696 */
3697 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3698 /* Simply reorder based on new priority */
3699 if (new_priority < old_priority) {
3700 camq_change_priority(queue, pinfo->index,
3701 new_priority);
3702 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3703 ("changed priority to %d\n",
3704 new_priority));
3705 }
3706 retval = 0;
3707 } else {
3708 /* New entry on the queue */
3709 if (new_priority < old_priority)
3710 pinfo->priority = new_priority;
3711
3712 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3713 ("Inserting onto queue\n"));
3714 pinfo->generation = ++queue->generation;
3715 camq_insert(queue, pinfo);
3716 retval = 1;
3717 }
3718 return (retval);
3719}
3720
3721static void
3722xpt_run_dev_allocq(struct cam_eb *bus)
3723{
3724 struct cam_devq *devq;
3725 int s;
3726
3727 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3728 devq = bus->sim->devq;
3729
3730 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3731 (" qfrozen_cnt == 0x%x, entries == %d, "
3732 "openings == %d, active == %d\n",
3733 devq->alloc_queue.qfrozen_cnt,
3734 devq->alloc_queue.entries,
3735 devq->alloc_openings,
3736 devq->alloc_active));
3737
3738 s = splsoftcam();
3739 devq->alloc_queue.qfrozen_cnt++;
3740 while ((devq->alloc_queue.entries > 0)
3741 && (devq->alloc_openings > 0)
3742 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3743 struct cam_ed_qinfo *qinfo;
3744 struct cam_ed *device;
3745 union ccb *work_ccb;
3746 struct cam_periph *drv;
3747 struct camq *drvq;
3748
3749 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3750 CAMQ_HEAD);
3751 device = qinfo->device;
3752
3753 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3754 ("running device %p\n", device));
3755
3756 drvq = &device->drvq;
3757
3758#ifdef CAMDEBUG
3759 if (drvq->entries <= 0) {
3760 panic("xpt_run_dev_allocq: "
3761 "Device on queue without any work to do");
3762 }
3763#endif
3764 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3765 devq->alloc_openings--;
3766 devq->alloc_active++;
3767 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3768 splx(s);
3769 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3770 drv->pinfo.priority);
3771 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3772 ("calling periph start\n"));
3773 drv->periph_start(drv, work_ccb);
3774 } else {
3775 /*
3776 * Malloc failure in alloc_ccb
3777 */
3778 /*
3779 * XXX add us to a list to be run from free_ccb
3780 * if we don't have any ccbs active on this
3781 * device queue otherwise we may never get run
3782 * again.
3783 */
3784 break;
3785 }
3786
3787 /* Raise IPL for possible insertion and test at top of loop */
3788 s = splsoftcam();
3789
3790 if (drvq->entries > 0) {
3791 /* We have more work. Attempt to reschedule */
3792 xpt_schedule_dev_allocq(bus, device);
3793 }
3794 }
3795 devq->alloc_queue.qfrozen_cnt--;
3796 splx(s);
3797}
3798
3799static void
3800xpt_run_dev_sendq(struct cam_eb *bus)
3801{
3802 struct cam_devq *devq;
3803 int s;
3804
3805 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3806
3807 devq = bus->sim->devq;
3808
3809 s = splcam();
3810 devq->send_queue.qfrozen_cnt++;
3811 splx(s);
3812 s = splsoftcam();
3813 while ((devq->send_queue.entries > 0)
3814 && (devq->send_openings > 0)) {
3815 struct cam_ed_qinfo *qinfo;
3816 struct cam_ed *device;
3817 union ccb *work_ccb;
3818 struct cam_sim *sim;
3819 int ospl;
3820
3821 ospl = splcam();
3822 if (devq->send_queue.qfrozen_cnt > 1) {
3823 splx(ospl);
3824 break;
3825 }
3826
3827 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3828 CAMQ_HEAD);
3829 device = qinfo->device;
3830
3831 /*
3832 * If the device has been "frozen", don't attempt
3833 * to run it.
3834 */
3835 if (device->qfrozen_cnt > 0) {
3836 splx(ospl);
3837 continue;
3838 }
3839
3840 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3841 ("running device %p\n", device));
3842
3843 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3844 if (work_ccb == NULL) {
3845 printf("device on run queue with no ccbs???\n");
3846 splx(ospl);
3847 continue;
3848 }
3849
3850 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3851
3852 if (num_highpower <= 0) {
3853 /*
3854 * We got a high power command, but we
3855 * don't have any available slots. Freeze
3856 * the device queue until we have a slot
3857 * available.
3858 */
3859 device->qfrozen_cnt++;
3860 STAILQ_INSERT_TAIL(&highpowerq,
3861 &work_ccb->ccb_h,
3862 xpt_links.stqe);
3863
3864 splx(ospl);
3865 continue;
3866 } else {
3867 /*
3868 * Consume a high power slot while
3869 * this ccb runs.
3870 */
3871 num_highpower--;
3872 }
3873 }
3874 devq->active_dev = device;
3875 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3876
3877 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3878 splx(ospl);
3879
3880 devq->send_openings--;
3881 devq->send_active++;
3882
3883 if (device->ccbq.queue.entries > 0)
3884 xpt_schedule_dev_sendq(bus, device);
3885
3886 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3887 /*
3888 * The client wants to freeze the queue
3889 * after this CCB is sent.
3890 */
3891 ospl = splcam();
3892 device->qfrozen_cnt++;
3893 splx(ospl);
3894 }
3895
3896 splx(s);
3897
3898 /* In Target mode, the peripheral driver knows best... */
3899 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3900 if ((device->inq_flags & SID_CmdQue) != 0
3901 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3902 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3903 else
3904 /*
3905 * Clear this in case of a retried CCB that
3906 * failed due to a rejected tag.
3907 */
3908 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3909 }
3910
3911 /*
3912 * Device queues can be shared among multiple sim instances
3913 * that reside on different busses. Use the SIM in the queue
3914 * CCB's path, rather than the one in the bus that was passed
3915 * into this function.
3916 */
3917 sim = work_ccb->ccb_h.path->bus->sim;
3918 (*(sim->sim_action))(sim, work_ccb);
3919
3920 ospl = splcam();
3921 devq->active_dev = NULL;
3922 splx(ospl);
3923 /* Raise IPL for possible insertion and test at top of loop */
3924 s = splsoftcam();
3925 }
3926 splx(s);
3927 s = splcam();
3928 devq->send_queue.qfrozen_cnt--;
3929 splx(s);
3930}
3931
3932/*
3933 * This function merges stuff from the slave ccb into the master ccb, while
3934 * keeping important fields in the master ccb constant.
3935 */
3936void
3937xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3938{
3939 /*
3940 * Pull fields that are valid for peripheral drivers to set
3941 * into the master CCB along with the CCB "payload".
3942 */
3943 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3944 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3945 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3946 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3947 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3948 sizeof(union ccb) - sizeof(struct ccb_hdr));
3949}
3950
3951void
3952xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3953{
3954 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3955 ccb_h->pinfo.priority = priority;
3956 ccb_h->path = path;
3957 ccb_h->path_id = path->bus->path_id;
3958 if (path->target)
3959 ccb_h->target_id = path->target->target_id;
3960 else
3961 ccb_h->target_id = CAM_TARGET_WILDCARD;
3962 if (path->device) {
3963 ccb_h->target_lun = path->device->lun_id;
3964 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
3965 } else {
3966 ccb_h->target_lun = CAM_TARGET_WILDCARD;
3967 }
3968 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
3969 ccb_h->flags = 0;
3970}
3971
3972/* Path manipulation functions */
3973cam_status
3974xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
3975 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3976{
3977 struct cam_path *path;
3978 cam_status status;
3979
3980 path = (struct cam_path *)malloc(sizeof(*path), M_DEVBUF, M_NOWAIT);
3981
3982 if (path == NULL) {
3983 status = CAM_RESRC_UNAVAIL;
3984 return(status);
3985 }
3986 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
3987 if (status != CAM_REQ_CMP) {
3988 free(path, M_DEVBUF);
3989 path = NULL;
3990 }
3991 *new_path_ptr = path;
3992 return (status);
3993}
3994
3995static cam_status
3996xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
3997 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
3998{
3999 struct cam_eb *bus;
4000 struct cam_et *target;
4001 struct cam_ed *device;
4002 cam_status status;
4003 int s;
4004
4005 status = CAM_REQ_CMP; /* Completed without error */
4006 target = NULL; /* Wildcarded */
4007 device = NULL; /* Wildcarded */
4008
4009 /*
4010 * We will potentially modify the EDT, so block interrupts
4011 * that may attempt to create cam paths.
4012 */
4013 s = splcam();
4014 bus = xpt_find_bus(path_id);
4015 if (bus == NULL) {
4016 status = CAM_PATH_INVALID;
4017 } else {
4018 target = xpt_find_target(bus, target_id);
4019 if (target == NULL) {
4020 /* Create one */
4021 struct cam_et *new_target;
4022
4023 new_target = xpt_alloc_target(bus, target_id);
4024 if (new_target == NULL) {
4025 status = CAM_RESRC_UNAVAIL;
4026 } else {
4027 target = new_target;
4028 }
4029 }
4030 if (target != NULL) {
4031 device = xpt_find_device(target, lun_id);
4032 if (device == NULL) {
4033 /* Create one */
4034 struct cam_ed *new_device;
4035
4036 new_device = xpt_alloc_device(bus,
4037 target,
4038 lun_id);
4039 if (new_device == NULL) {
4040 status = CAM_RESRC_UNAVAIL;
4041 } else {
4042 device = new_device;
4043 }
4044 }
4045 }
4046 }
4047 splx(s);
4048
4049 /*
4050 * Only touch the user's data if we are successful.
4051 */
4052 if (status == CAM_REQ_CMP) {
4053 new_path->periph = perph;
4054 new_path->bus = bus;
4055 new_path->target = target;
4056 new_path->device = device;
4057 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
4058 } else {
4059 if (device != NULL)
4060 xpt_release_device(bus, target, device);
4061 if (target != NULL)
4062 xpt_release_target(bus, target);
4063 if (bus != NULL)
4064 xpt_release_bus(bus);
4065 }
4066 return (status);
4067}
4068
4069static void
4070xpt_release_path(struct cam_path *path)
4071{
4072 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
4073 if (path->device != NULL) {
4074 xpt_release_device(path->bus, path->target, path->device);
4075 path->device = NULL;
4076 }
4077 if (path->target != NULL) {
4078 xpt_release_target(path->bus, path->target);
4079 path->target = NULL;
4080 }
4081 if (path->bus != NULL) {
4082 xpt_release_bus(path->bus);
4083 path->bus = NULL;
4084 }
4085}
4086
4087void
4088xpt_free_path(struct cam_path *path)
4089{
4090 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
4091 xpt_release_path(path);
4092 free(path, M_DEVBUF);
4093}
4094
4095
4096/*
4097 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4098 * in path1, 2 for match with wildcards in path2.
4099 */
4100int
4101xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
4102{
4103 int retval = 0;
4104
4105 if (path1->bus != path2->bus) {
4106 if (path1->bus->path_id == CAM_BUS_WILDCARD)
4107 retval = 1;
4108 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4109 retval = 2;
4110 else
4111 return (-1);
4112 }
4113 if (path1->target != path2->target) {
4114 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4115 if (retval == 0)
4116 retval = 1;
4117 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4118 retval = 2;
4119 else
4120 return (-1);
4121 }
4122 if (path1->device != path2->device) {
4123 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4124 if (retval == 0)
4125 retval = 1;
4126 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4127 retval = 2;
4128 else
4129 return (-1);
4130 }
4131 return (retval);
4132}
4133
4134void
4135xpt_print_path(struct cam_path *path)
4136{
4137 if (path == NULL)
4138 printf("(nopath): ");
4139 else {
4140 if (path->periph != NULL)
4141 printf("(%s%d:", path->periph->periph_name,
4142 path->periph->unit_number);
4143 else
4144 printf("(noperiph:");
4145
4146 if (path->bus != NULL)
4147 printf("%s%d:%d:", path->bus->sim->sim_name,
4148 path->bus->sim->unit_number,
4149 path->bus->sim->bus_id);
4150 else
4151 printf("nobus:");
4152
4153 if (path->target != NULL)
4154 printf("%d:", path->target->target_id);
4155 else
4156 printf("X:");
4157
4158 if (path->device != NULL)
4159 printf("%d): ", path->device->lun_id);
4160 else
4161 printf("X): ");
4162 }
4163}
4164
4165int
4166xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4167{
4168 struct sbuf sb;
4169
4170 sbuf_new(&sb, str, str_len, 0);
4171
4172 if (path == NULL)
4173 sbuf_printf(&sb, "(nopath): ");
4174 else {
4175 if (path->periph != NULL)
4176 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4177 path->periph->unit_number);
4178 else
4179 sbuf_printf(&sb, "(noperiph:");
4180
4181 if (path->bus != NULL)
4182 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
4183 path->bus->sim->unit_number,
4184 path->bus->sim->bus_id);
4185 else
4186 sbuf_printf(&sb, "nobus:");
4187
4188 if (path->target != NULL)
4189 sbuf_printf(&sb, "%d:", path->target->target_id);
4190 else
4191 sbuf_printf(&sb, "X:");
4192
4193 if (path->device != NULL)
4194 sbuf_printf(&sb, "%d): ", path->device->lun_id);
4195 else
4196 sbuf_printf(&sb, "X): ");
4197 }
4198 sbuf_finish(&sb);
4199
4200 return(sbuf_len(&sb));
4201}
4202
4203path_id_t
4204xpt_path_path_id(struct cam_path *path)
4205{
4206 return(path->bus->path_id);
4207}
4208
4209target_id_t
4210xpt_path_target_id(struct cam_path *path)
4211{
4212 if (path->target != NULL)
4213 return (path->target->target_id);
4214 else
4215 return (CAM_TARGET_WILDCARD);
4216}
4217
4218lun_id_t
4219xpt_path_lun_id(struct cam_path *path)
4220{
4221 if (path->device != NULL)
4222 return (path->device->lun_id);
4223 else
4224 return (CAM_LUN_WILDCARD);
4225}
4226
4227struct cam_sim *
4228xpt_path_sim(struct cam_path *path)
4229{
4230 return (path->bus->sim);
4231}
4232
4233struct cam_periph*
4234xpt_path_periph(struct cam_path *path)
4235{
4236 return (path->periph);
4237}
4238
4239/*
4240 * Release a CAM control block for the caller. Remit the cost of the structure
4241 * to the device referenced by the path. If the this device had no 'credits'
4242 * and peripheral drivers have registered async callbacks for this notification
4243 * call them now.
4244 */
4245void
4246xpt_release_ccb(union ccb *free_ccb)
4247{
4248 int s;
4249 struct cam_path *path;
4250 struct cam_ed *device;
4251 struct cam_eb *bus;
4252
4253 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4254 path = free_ccb->ccb_h.path;
4255 device = path->device;
4256 bus = path->bus;
4257 s = splsoftcam();
4258 cam_ccbq_release_opening(&device->ccbq);
4259 if (xpt_ccb_count > xpt_max_ccbs) {
4260 xpt_free_ccb(free_ccb);
4261 xpt_ccb_count--;
4262 } else {
4263 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4264 }
4265 bus->sim->devq->alloc_openings++;
4266 bus->sim->devq->alloc_active--;
4267 /* XXX Turn this into an inline function - xpt_run_device?? */
4268 if ((device_is_alloc_queued(device) == 0)
4269 && (device->drvq.entries > 0)) {
4270 xpt_schedule_dev_allocq(bus, device);
4271 }
4272 splx(s);
4273 if (dev_allocq_is_runnable(bus->sim->devq))
4274 xpt_run_dev_allocq(bus);
4275}
4276
4277/* Functions accessed by SIM drivers */
4278
4279/*
4280 * A sim structure, listing the SIM entry points and instance
4281 * identification info is passed to xpt_bus_register to hook the SIM
4282 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4283 * for this new bus and places it in the array of busses and assigns
4284 * it a path_id. The path_id may be influenced by "hard wiring"
4285 * information specified by the user. Once interrupt services are
4286 * availible, the bus will be probed.
4287 */
4288int32_t
4289xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4290{
4291 struct cam_eb *new_bus;
4292 struct cam_eb *old_bus;
4293 struct ccb_pathinq cpi;
4294 int s;
4295
4296 sim->bus_id = bus;
4297 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4298 M_DEVBUF, M_NOWAIT);
4299 if (new_bus == NULL) {
4300 /* Couldn't satisfy request */
4301 return (CAM_RESRC_UNAVAIL);
4302 }
4303
4304 if (strcmp(sim->sim_name, "xpt") != 0) {
4305
4306 sim->path_id =
4307 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4308 }
4309
4310 TAILQ_INIT(&new_bus->et_entries);
4311 new_bus->path_id = sim->path_id;
4312 new_bus->sim = sim;
4313 timevalclear(&new_bus->last_reset);
4314 new_bus->flags = 0;
4315 new_bus->refcount = 1; /* Held until a bus_deregister event */
4316 new_bus->generation = 0;
4317 s = splcam();
4318 old_bus = TAILQ_FIRST(&xpt_busses);
4319 while (old_bus != NULL
4320 && old_bus->path_id < new_bus->path_id)
4321 old_bus = TAILQ_NEXT(old_bus, links);
4322 if (old_bus != NULL)
4323 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4324 else
4325 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4326 bus_generation++;
4327 splx(s);
4328
4329 /* Notify interested parties */
4330 if (sim->path_id != CAM_XPT_PATH_ID) {
4331 struct cam_path path;
4332
4333 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4334 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4335 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4336 cpi.ccb_h.func_code = XPT_PATH_INQ;
4337 xpt_action((union ccb *)&cpi);
4338 xpt_async(AC_PATH_REGISTERED, &path, &cpi);
4339 xpt_release_path(&path);
4340 }
4341 return (CAM_SUCCESS);
4342}
4343
4344int32_t
4345xpt_bus_deregister(path_id_t pathid)
4346{
4347 struct cam_path bus_path;
4348 cam_status status;
4349
4350 status = xpt_compile_path(&bus_path, NULL, pathid,
4351 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4352 if (status != CAM_REQ_CMP)
4353 return (status);
4354
4355 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4356 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4357
4358 /* Release the reference count held while registered. */
4359 xpt_release_bus(bus_path.bus);
4360 xpt_release_path(&bus_path);
4361
4362 return (CAM_REQ_CMP);
4363}
4364
4365static path_id_t
4366xptnextfreepathid(void)
4367{
4368 struct cam_eb *bus;
4369 path_id_t pathid;
4370 const char *strval;
4371
4372 pathid = 0;
4373 bus = TAILQ_FIRST(&xpt_busses);
4374retry:
4375 /* Find an unoccupied pathid */
4376 while (bus != NULL
4377 && bus->path_id <= pathid) {
4378 if (bus->path_id == pathid)
4379 pathid++;
4380 bus = TAILQ_NEXT(bus, links);
4381 }
4382
4383 /*
4384 * Ensure that this pathid is not reserved for
4385 * a bus that may be registered in the future.
4386 */
4387 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4388 ++pathid;
4389 /* Start the search over */
4390 goto retry;
4391 }
4392 return (pathid);
4393}
4394
4395static path_id_t
4396xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4397{
4398 path_id_t pathid;
4399 int i, dunit, val;
4400 char buf[32];
4401 const char *dname;
4402
4403 pathid = CAM_XPT_PATH_ID;
4404 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4405 i = 0;
4406 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4407 if (strcmp(dname, "scbus")) {
4408 /* Avoid a bit of foot shooting. */
4409 continue;
4410 }
4411 if (dunit < 0) /* unwired?! */
4412 continue;
4413 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4414 if (sim_bus == val) {
4415 pathid = dunit;
4416 break;
4417 }
4418 } else if (sim_bus == 0) {
4419 /* Unspecified matches bus 0 */
4420 pathid = dunit;
4421 break;
4422 } else {
4423 printf("Ambiguous scbus configuration for %s%d "
4424 "bus %d, cannot wire down. The kernel "
4425 "config entry for scbus%d should "
4426 "specify a controller bus.\n"
4427 "Scbus will be assigned dynamically.\n",
4428 sim_name, sim_unit, sim_bus, dunit);
4429 break;
4430 }
4431 }
4432
4433 if (pathid == CAM_XPT_PATH_ID)
4434 pathid = xptnextfreepathid();
4435 return (pathid);
4436}
4437
4438void
4439xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4440{
4441 struct cam_eb *bus;
4442 struct cam_et *target, *next_target;
4443 struct cam_ed *device, *next_device;
4444 int s;
4445
4446 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4447
4448 /*
4449 * Most async events come from a CAM interrupt context. In
4450 * a few cases, the error recovery code at the peripheral layer,
4451 * which may run from our SWI or a process context, may signal
4452 * deferred events with a call to xpt_async. Ensure async
4453 * notifications are serialized by blocking cam interrupts.
4454 */
4455 s = splcam();
4456
4457 bus = path->bus;
4458
4459 if (async_code == AC_BUS_RESET) {
4460 int s;
4461
4462 s = splclock();
4463 /* Update our notion of when the last reset occurred */
4464 microtime(&bus->last_reset);
4465 splx(s);
4466 }
4467
4468 for (target = TAILQ_FIRST(&bus->et_entries);
4469 target != NULL;
4470 target = next_target) {
4471
4472 next_target = TAILQ_NEXT(target, links);
4473
4474 if (path->target != target
4475 && path->target->target_id != CAM_TARGET_WILDCARD
4476 && target->target_id != CAM_TARGET_WILDCARD)
4477 continue;
4478
4479 if (async_code == AC_SENT_BDR) {
4480 int s;
4481
4482 /* Update our notion of when the last reset occurred */
4483 s = splclock();
4484 microtime(&path->target->last_reset);
4485 splx(s);
4486 }
4487
4488 for (device = TAILQ_FIRST(&target->ed_entries);
4489 device != NULL;
4490 device = next_device) {
4491
4492 next_device = TAILQ_NEXT(device, links);
4493
4494 if (path->device != device
4495 && path->device->lun_id != CAM_LUN_WILDCARD
4496 && device->lun_id != CAM_LUN_WILDCARD)
4497 continue;
4498
4499 xpt_dev_async(async_code, bus, target,
4500 device, async_arg);
4501
4502 xpt_async_bcast(&device->asyncs, async_code,
4503 path, async_arg);
4504 }
4505 }
4506
4507 /*
4508 * If this wasn't a fully wildcarded async, tell all
4509 * clients that want all async events.
4510 */
4511 if (bus != xpt_periph->path->bus)
4512 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4513 path, async_arg);
4514 splx(s);
4515}
4516
4517static void
4518xpt_async_bcast(struct async_list *async_head,
4519 u_int32_t async_code,
4520 struct cam_path *path, void *async_arg)
4521{
4522 struct async_node *cur_entry;
4523
4524 cur_entry = SLIST_FIRST(async_head);
4525 while (cur_entry != NULL) {
4526 struct async_node *next_entry;
4527 /*
4528 * Grab the next list entry before we call the current
4529 * entry's callback. This is because the callback function
4530 * can delete its async callback entry.
4531 */
4532 next_entry = SLIST_NEXT(cur_entry, links);
4533 if ((cur_entry->event_enable & async_code) != 0)
4534 cur_entry->callback(cur_entry->callback_arg,
4535 async_code, path,
4536 async_arg);
4537 cur_entry = next_entry;
4538 }
4539}
4540
4541/*
4542 * Handle any per-device event notifications that require action by the XPT.
4543 */
4544static void
4545xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4546 struct cam_ed *device, void *async_arg)
4547{
4548 cam_status status;
4549 struct cam_path newpath;
4550
4551 /*
4552 * We only need to handle events for real devices.
4553 */
4554 if (target->target_id == CAM_TARGET_WILDCARD
4555 || device->lun_id == CAM_LUN_WILDCARD)
4556 return;
4557
4558 /*
4559 * We need our own path with wildcards expanded to
4560 * handle certain types of events.
4561 */
4562 if ((async_code == AC_SENT_BDR)
4563 || (async_code == AC_BUS_RESET)
4564 || (async_code == AC_INQ_CHANGED))
4565 status = xpt_compile_path(&newpath, NULL,
4566 bus->path_id,
4567 target->target_id,
4568 device->lun_id);
4569 else
4570 status = CAM_REQ_CMP_ERR;
4571
4572 if (status == CAM_REQ_CMP) {
4573
4574 /*
4575 * Allow transfer negotiation to occur in a
4576 * tag free environment.
4577 */
4578 if (async_code == AC_SENT_BDR
4579 || async_code == AC_BUS_RESET)
4580 xpt_toggle_tags(&newpath);
4581
4582 if (async_code == AC_INQ_CHANGED) {
4583 /*
4584 * We've sent a start unit command, or
4585 * something similar to a device that
4586 * may have caused its inquiry data to
4587 * change. So we re-scan the device to
4588 * refresh the inquiry data for it.
4589 */
4590 xpt_scan_lun(newpath.periph, &newpath,
4591 CAM_EXPECT_INQ_CHANGE, NULL);
4592 }
4593 xpt_release_path(&newpath);
4594 } else if (async_code == AC_LOST_DEVICE) {
4595 device->flags |= CAM_DEV_UNCONFIGURED;
4596 } else if (async_code == AC_TRANSFER_NEG) {
4597 struct ccb_trans_settings *settings;
4598
4599 settings = (struct ccb_trans_settings *)async_arg;
4600 xpt_set_transfer_settings(settings, device,
4601 /*async_update*/TRUE);
4602 }
4603}
4604
4605u_int32_t
4606xpt_freeze_devq(struct cam_path *path, u_int count)
4607{
4608 int s;
4609 struct ccb_hdr *ccbh;
4610
4611 s = splcam();
4612 path->device->qfrozen_cnt += count;
4613
4614 /*
4615 * Mark the last CCB in the queue as needing
4616 * to be requeued if the driver hasn't
4617 * changed it's state yet. This fixes a race
4618 * where a ccb is just about to be queued to
4619 * a controller driver when it's interrupt routine
4620 * freezes the queue. To completly close the
4621 * hole, controller drives must check to see
4622 * if a ccb's status is still CAM_REQ_INPROG
4623 * under spl protection just before they queue
4624 * the CCB. See ahc_action/ahc_freeze_devq for
4625 * an example.
4626 */
4627 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4628 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4629 ccbh->status = CAM_REQUEUE_REQ;
4630 splx(s);
4631 return (path->device->qfrozen_cnt);
4632}
4633
4634u_int32_t
4635xpt_freeze_simq(struct cam_sim *sim, u_int count)
4636{
4637 sim->devq->send_queue.qfrozen_cnt += count;
4638 if (sim->devq->active_dev != NULL) {
4639 struct ccb_hdr *ccbh;
4640
4641 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4642 ccb_hdr_tailq);
4643 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4644 ccbh->status = CAM_REQUEUE_REQ;
4645 }
4646 return (sim->devq->send_queue.qfrozen_cnt);
4647}
4648
4649static void
4650xpt_release_devq_timeout(void *arg)
4651{
4652 struct cam_ed *device;
4653
4654 device = (struct cam_ed *)arg;
4655
4656 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4657}
4658
4659void
4660xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4661{
4662 xpt_release_devq_device(path->device, count, run_queue);
4663}
4664
4665static void
4666xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4667{
4668 int rundevq;
4669 int s0, s1;
4670
4671 rundevq = 0;
4672 s0 = splsoftcam();
4673 s1 = splcam();
4674 if (dev->qfrozen_cnt > 0) {
4675
4676 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4677 dev->qfrozen_cnt -= count;
4678 if (dev->qfrozen_cnt == 0) {
4679
4680 /*
4681 * No longer need to wait for a successful
4682 * command completion.
4683 */
4684 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4685
4686 /*
4687 * Remove any timeouts that might be scheduled
4688 * to release this queue.
4689 */
4690 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4691 untimeout(xpt_release_devq_timeout, dev,
4692 dev->c_handle);
4693 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4694 }
4695
4696 /*
4697 * Now that we are unfrozen schedule the
4698 * device so any pending transactions are
4699 * run.
4700 */
4701 if ((dev->ccbq.queue.entries > 0)
4702 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4703 && (run_queue != 0)) {
4704 rundevq = 1;
4705 }
4706 }
4707 }
4708 splx(s1);
4709 if (rundevq != 0)
4710 xpt_run_dev_sendq(dev->target->bus);
4711 splx(s0);
4712}
4713
4714void
4715xpt_release_simq(struct cam_sim *sim, int run_queue)
4716{
4717 int s;
4718 struct camq *sendq;
4719
4720 sendq = &(sim->devq->send_queue);
4721 s = splcam();
4722 if (sendq->qfrozen_cnt > 0) {
4723
4724 sendq->qfrozen_cnt--;
4725 if (sendq->qfrozen_cnt == 0) {
4726 struct cam_eb *bus;
4727
4728 /*
4729 * If there is a timeout scheduled to release this
4730 * sim queue, remove it. The queue frozen count is
4731 * already at 0.
4732 */
4733 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4734 untimeout(xpt_release_simq_timeout, sim,
4735 sim->c_handle);
4736 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4737 }
4738 bus = xpt_find_bus(sim->path_id);
4739 splx(s);
4740
4741 if (run_queue) {
4742 /*
4743 * Now that we are unfrozen run the send queue.
4744 */
4745 xpt_run_dev_sendq(bus);
4746 }
4747 xpt_release_bus(bus);
4748 } else
4749 splx(s);
4750 } else
4751 splx(s);
4752}
4753
4754static void
4755xpt_release_simq_timeout(void *arg)
4756{
4757 struct cam_sim *sim;
4758
4759 sim = (struct cam_sim *)arg;
4760 xpt_release_simq(sim, /* run_queue */ TRUE);
4761}
4762
4763void
4764xpt_done(union ccb *done_ccb)
4765{
4766 int s;
4767
4768 s = splcam();
4769
4770 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4771 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4772 /*
4773 * Queue up the request for handling by our SWI handler
4774 * any of the "non-immediate" type of ccbs.
4775 */
4776 switch (done_ccb->ccb_h.path->periph->type) {
4777 case CAM_PERIPH_BIO:
4778 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4779 sim_links.tqe);
4780 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4781 swi_sched(cambio_ih, 0);
4782 break;
4783 case CAM_PERIPH_NET:
4784 TAILQ_INSERT_TAIL(&cam_netq, &done_ccb->ccb_h,
4785 sim_links.tqe);
4786 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4787 swi_sched(camnet_ih, 0);
4788 break;
4789 }
4790 }
4791 splx(s);
4792}
4793
4794union ccb *
4795xpt_alloc_ccb()
4796{
4797 union ccb *new_ccb;
4798
4799 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_WAITOK);
4800 return (new_ccb);
4801}
4802
4803void
4804xpt_free_ccb(union ccb *free_ccb)
4805{
4806 free(free_ccb, M_DEVBUF);
4807}
4808
4809
4810
4811/* Private XPT functions */
4812
4813/*
4814 * Get a CAM control block for the caller. Charge the structure to the device
4815 * referenced by the path. If the this device has no 'credits' then the
4816 * device already has the maximum number of outstanding operations under way
4817 * and we return NULL. If we don't have sufficient resources to allocate more
4818 * ccbs, we also return NULL.
4819 */
4820static union ccb *
4821xpt_get_ccb(struct cam_ed *device)
4822{
4823 union ccb *new_ccb;
4824 int s;
4825
4826 s = splsoftcam();
4827 if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
4828 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_NOWAIT);
4829 if (new_ccb == NULL) {
4830 splx(s);
4831 return (NULL);
4832 }
4833 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4834 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4835 xpt_links.sle);
4836 xpt_ccb_count++;
4837 }
4838 cam_ccbq_take_opening(&device->ccbq);
4839 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4840 splx(s);
4841 return (new_ccb);
4842}
4843
4844static void
4845xpt_release_bus(struct cam_eb *bus)
4846{
4847 int s;
4848
4849 s = splcam();
4850 if ((--bus->refcount == 0)
4851 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4852 TAILQ_REMOVE(&xpt_busses, bus, links);
4853 bus_generation++;
4854 splx(s);
4855 free(bus, M_DEVBUF);
4856 } else
4857 splx(s);
4858}
4859
4860static struct cam_et *
4861xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4862{
4863 struct cam_et *target;
4864
4865 target = (struct cam_et *)malloc(sizeof(*target), M_DEVBUF, M_NOWAIT);
4866 if (target != NULL) {
4867 struct cam_et *cur_target;
4868
4869 TAILQ_INIT(&target->ed_entries);
4870 target->bus = bus;
4871 target->target_id = target_id;
4872 target->refcount = 1;
4873 target->generation = 0;
4874 timevalclear(&target->last_reset);
4875 /*
4876 * Hold a reference to our parent bus so it
4877 * will not go away before we do.
4878 */
4879 bus->refcount++;
4880
4881 /* Insertion sort into our bus's target list */
4882 cur_target = TAILQ_FIRST(&bus->et_entries);
4883 while (cur_target != NULL && cur_target->target_id < target_id)
4884 cur_target = TAILQ_NEXT(cur_target, links);
4885
4886 if (cur_target != NULL) {
4887 TAILQ_INSERT_BEFORE(cur_target, target, links);
4888 } else {
4889 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4890 }
4891 bus->generation++;
4892 }
4893 return (target);
4894}
4895
4896static void
4897xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4898{
4899 int s;
4900
4901 s = splcam();
4902 if ((--target->refcount == 0)
4903 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4904 TAILQ_REMOVE(&bus->et_entries, target, links);
4905 bus->generation++;
4906 splx(s);
4907 free(target, M_DEVBUF);
4908 xpt_release_bus(bus);
4909 } else
4910 splx(s);
4911}
4912
4913static struct cam_ed *
4914xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4915{
4916#ifdef CAM_NEW_TRAN_CODE
4917 struct cam_path path;
4918#endif /* CAM_NEW_TRAN_CODE */
4919 struct cam_ed *device;
4920 struct cam_devq *devq;
4921 cam_status status;
4922
4923 /* Make space for us in the device queue on our bus */
4924 devq = bus->sim->devq;
4925 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4926
4927 if (status != CAM_REQ_CMP) {
4928 device = NULL;
4929 } else {
4930 device = (struct cam_ed *)malloc(sizeof(*device),
4931 M_DEVBUF, M_NOWAIT);
4932 }
4933
4934 if (device != NULL) {
4935 struct cam_ed *cur_device;
4936
4937 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
4938 device->alloc_ccb_entry.device = device;
4939 cam_init_pinfo(&device->send_ccb_entry.pinfo);
4940 device->send_ccb_entry.device = device;
4941 device->target = target;
4942 device->lun_id = lun_id;
4943 /* Initialize our queues */
4944 if (camq_init(&device->drvq, 0) != 0) {
4945 free(device, M_DEVBUF);
4946 return (NULL);
4947 }
4948 if (cam_ccbq_init(&device->ccbq,
4949 bus->sim->max_dev_openings) != 0) {
4950 camq_fini(&device->drvq);
4951 free(device, M_DEVBUF);
4952 return (NULL);
4953 }
4954 SLIST_INIT(&device->asyncs);
4955 SLIST_INIT(&device->periphs);
4956 device->generation = 0;
4957 device->owner = NULL;
4958 /*
4959 * Take the default quirk entry until we have inquiry
4960 * data and can determine a better quirk to use.
4961 */
4962 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
4963 bzero(&device->inq_data, sizeof(device->inq_data));
4964 device->inq_flags = 0;
4965 device->queue_flags = 0;
4966 device->serial_num = NULL;
4967 device->serial_num_len = 0;
4968 device->qfrozen_cnt = 0;
4969 device->flags = CAM_DEV_UNCONFIGURED;
4970 device->tag_delay_count = 0;
4971 device->refcount = 1;
4972 callout_handle_init(&device->c_handle);
4973
4974 /*
4975 * Hold a reference to our parent target so it
4976 * will not go away before we do.
4977 */
4978 target->refcount++;
4979
4980 /*
4981 * XXX should be limited by number of CCBs this bus can
4982 * do.
4983 */
4984 xpt_max_ccbs += device->ccbq.devq_openings;
4985 /* Insertion sort into our target's device list */
4986 cur_device = TAILQ_FIRST(&target->ed_entries);
4987 while (cur_device != NULL && cur_device->lun_id < lun_id)
4988 cur_device = TAILQ_NEXT(cur_device, links);
4989 if (cur_device != NULL) {
4990 TAILQ_INSERT_BEFORE(cur_device, device, links);
4991 } else {
4992 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
4993 }
4994 target->generation++;
4995#ifdef CAM_NEW_TRAN_CODE
4996 if (lun_id != CAM_LUN_WILDCARD) {
4997 xpt_compile_path(&path,
4998 NULL,
4999 bus->path_id,
5000 target->target_id,
5001 lun_id);
5002 xpt_devise_transport(&path);
5003 xpt_release_path(&path);
5004 }
5005#endif /* CAM_NEW_TRAN_CODE */
5006 }
5007 return (device);
5008}
5009
5010static void
5011xpt_release_device(struct cam_eb *bus, struct cam_et *target,
5012 struct cam_ed *device)
5013{
5014 int s;
5015
5016 s = splcam();
5017 if ((--device->refcount == 0)
5018 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
5019 struct cam_devq *devq;
5020
5021 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
5022 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
5023 panic("Removing device while still queued for ccbs");
5024
5025 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
5026 untimeout(xpt_release_devq_timeout, device,
5027 device->c_handle);
5028
5029 TAILQ_REMOVE(&target->ed_entries, device,links);
5030 target->generation++;
5031 xpt_max_ccbs -= device->ccbq.devq_openings;
5032 /* Release our slot in the devq */
5033 devq = bus->sim->devq;
5034 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
5035 splx(s);
5036 free(device, M_DEVBUF);
5037 xpt_release_target(bus, target);
5038 } else
5039 splx(s);
5040}
5041
5042static u_int32_t
5043xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5044{
5045 int s;
5046 int diff;
5047 int result;
5048 struct cam_ed *dev;
5049
5050 dev = path->device;
5051 s = splsoftcam();
5052
5053 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
5054 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5055 if (result == CAM_REQ_CMP && (diff < 0)) {
5056 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
5057 }
5058 /* Adjust the global limit */
5059 xpt_max_ccbs += diff;
5060 splx(s);
5061 return (result);
5062}
5063
5064static struct cam_eb *
5065xpt_find_bus(path_id_t path_id)
5066{
5067 struct cam_eb *bus;
5068
5069 for (bus = TAILQ_FIRST(&xpt_busses);
5070 bus != NULL;
5071 bus = TAILQ_NEXT(bus, links)) {
5072 if (bus->path_id == path_id) {
5073 bus->refcount++;
5074 break;
5075 }
5076 }
5077 return (bus);
5078}
5079
5080static struct cam_et *
5081xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5082{
5083 struct cam_et *target;
5084
5085 for (target = TAILQ_FIRST(&bus->et_entries);
5086 target != NULL;
5087 target = TAILQ_NEXT(target, links)) {
5088 if (target->target_id == target_id) {
5089 target->refcount++;
5090 break;
5091 }
5092 }
5093 return (target);
5094}
5095
5096static struct cam_ed *
5097xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5098{
5099 struct cam_ed *device;
5100
5101 for (device = TAILQ_FIRST(&target->ed_entries);
5102 device != NULL;
5103 device = TAILQ_NEXT(device, links)) {
5104 if (device->lun_id == lun_id) {
5105 device->refcount++;
5106 break;
5107 }
5108 }
5109 return (device);
5110}
5111
5112typedef struct {
5113 union ccb *request_ccb;
5114 struct ccb_pathinq *cpi;
5115 int pending_count;
5116} xpt_scan_bus_info;
5117
5118/*
5119 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5120 * As the scan progresses, xpt_scan_bus is used as the
5121 * callback on completion function.
5122 */
5123static void
5124xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
5125{
5126 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5127 ("xpt_scan_bus\n"));
5128 switch (request_ccb->ccb_h.func_code) {
5129 case XPT_SCAN_BUS:
5130 {
5131 xpt_scan_bus_info *scan_info;
5132 union ccb *work_ccb;
5133 struct cam_path *path;
5134 u_int i;
5135 u_int max_target;
5136 u_int initiator_id;
5137
5138 /* Find out the characteristics of the bus */
5139 work_ccb = xpt_alloc_ccb();
5140 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
5141 request_ccb->ccb_h.pinfo.priority);
5142 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5143 xpt_action(work_ccb);
5144 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5145 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
5146 xpt_free_ccb(work_ccb);
5147 xpt_done(request_ccb);
5148 return;
5149 }
5150
5151 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5152 /*
5153 * Can't scan the bus on an adapter that
5154 * cannot perform the initiator role.
5155 */
5156 request_ccb->ccb_h.status = CAM_REQ_CMP;
5157 xpt_free_ccb(work_ccb);
5158 xpt_done(request_ccb);
5159 return;
5160 }
5161
5162 /* Save some state for use while we probe for devices */
5163 scan_info = (xpt_scan_bus_info *)
5164 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
5165 scan_info->request_ccb = request_ccb;
5166 scan_info->cpi = &work_ccb->cpi;
5167
5168 /* Cache on our stack so we can work asynchronously */
5169 max_target = scan_info->cpi->max_target;
5170 initiator_id = scan_info->cpi->initiator_id;
5171
5172 /*
5173 * Don't count the initiator if the
5174 * initiator is addressable.
5175 */
5176 scan_info->pending_count = max_target + 1;
5177 if (initiator_id <= max_target)
5178 scan_info->pending_count--;
5179
5180 for (i = 0; i <= max_target; i++) {
5181 cam_status status;
5182 if (i == initiator_id)
5183 continue;
5184
5185 status = xpt_create_path(&path, xpt_periph,
5186 request_ccb->ccb_h.path_id,
5187 i, 0);
5188 if (status != CAM_REQ_CMP) {
5189 printf("xpt_scan_bus: xpt_create_path failed"
5190 " with status %#x, bus scan halted\n",
5191 status);
5192 break;
5193 }
5194 work_ccb = xpt_alloc_ccb();
5195 xpt_setup_ccb(&work_ccb->ccb_h, path,
5196 request_ccb->ccb_h.pinfo.priority);
5197 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5198 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5199 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
5200 work_ccb->crcn.flags = request_ccb->crcn.flags;
5201 xpt_action(work_ccb);
5202 }
5203 break;
5204 }
5205 case XPT_SCAN_LUN:
5206 {
5207 xpt_scan_bus_info *scan_info;
5208 path_id_t path_id;
5209 target_id_t target_id;
5210 lun_id_t lun_id;
5211
5212 /* Reuse the same CCB to query if a device was really found */
5213 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
5214 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
5215 request_ccb->ccb_h.pinfo.priority);
5216 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5217
5218 path_id = request_ccb->ccb_h.path_id;
5219 target_id = request_ccb->ccb_h.target_id;
5220 lun_id = request_ccb->ccb_h.target_lun;
5221 xpt_action(request_ccb);
5222
5223 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5224 struct cam_ed *device;
5225 struct cam_et *target;
5226 int s, phl;
5227
5228 /*
5229 * If we already probed lun 0 successfully, or
5230 * we have additional configured luns on this
5231 * target that might have "gone away", go onto
5232 * the next lun.
5233 */
5234 target = request_ccb->ccb_h.path->target;
5235 /*
5236 * We may touch devices that we don't
5237 * hold references too, so ensure they
5238 * don't disappear out from under us.
5239 * The target above is referenced by the
5240 * path in the request ccb.
5241 */
5242 phl = 0;
5243 s = splcam();
5244 device = TAILQ_FIRST(&target->ed_entries);
5245 if (device != NULL) {
5246 phl = device->quirk->quirks & CAM_QUIRK_HILUNS;
5247 if (device->lun_id == 0)
5248 device = TAILQ_NEXT(device, links);
5249 }
5250 splx(s);
5251 if ((lun_id != 0) || (device != NULL)) {
5252 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5253 lun_id++;
5254 }
5255 } else {
5256 struct cam_ed *device;
5257
5258 device = request_ccb->ccb_h.path->device;
5259
5260 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5261 /* Try the next lun */
5262 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
5263 (device->quirk->quirks & CAM_QUIRK_HILUNS))
5264 lun_id++;
5265 }
5266 }
5267
5268 xpt_free_path(request_ccb->ccb_h.path);
5269
5270 /* Check Bounds */
5271 if ((lun_id == request_ccb->ccb_h.target_lun)
5272 || lun_id > scan_info->cpi->max_lun) {
5273 /* We're done */
5274
5275 xpt_free_ccb(request_ccb);
5276 scan_info->pending_count--;
5277 if (scan_info->pending_count == 0) {
5278 xpt_free_ccb((union ccb *)scan_info->cpi);
5279 request_ccb = scan_info->request_ccb;
5280 free(scan_info, M_TEMP);
5281 request_ccb->ccb_h.status = CAM_REQ_CMP;
5282 xpt_done(request_ccb);
5283 }
5284 } else {
5285 /* Try the next device */
5286 struct cam_path *path;
5287 cam_status status;
5288
5289 path = request_ccb->ccb_h.path;
5290 status = xpt_create_path(&path, xpt_periph,
5291 path_id, target_id, lun_id);
5292 if (status != CAM_REQ_CMP) {
5293 printf("xpt_scan_bus: xpt_create_path failed "
5294 "with status %#x, halting LUN scan\n",
5295 status);
5296 xpt_free_ccb(request_ccb);
5297 scan_info->pending_count--;
5298 if (scan_info->pending_count == 0) {
5299 xpt_free_ccb(
5300 (union ccb *)scan_info->cpi);
5301 request_ccb = scan_info->request_ccb;
5302 free(scan_info, M_TEMP);
5303 request_ccb->ccb_h.status = CAM_REQ_CMP;
5304 xpt_done(request_ccb);
5305 break;
5306 }
5307 }
5308 xpt_setup_ccb(&request_ccb->ccb_h, path,
5309 request_ccb->ccb_h.pinfo.priority);
5310 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5311 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5312 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5313 request_ccb->crcn.flags =
5314 scan_info->request_ccb->crcn.flags;
5315 xpt_action(request_ccb);
5316 }
5317 break;
5318 }
5319 default:
5320 break;
5321 }
5322}
5323
5324typedef enum {
5325 PROBE_TUR,
5326 PROBE_INQUIRY,
5327 PROBE_FULL_INQUIRY,
5328 PROBE_MODE_SENSE,
5329 PROBE_SERIAL_NUM,
5330 PROBE_TUR_FOR_NEGOTIATION
5331} probe_action;
5332
5333typedef enum {
5334 PROBE_INQUIRY_CKSUM = 0x01,
5335 PROBE_SERIAL_CKSUM = 0x02,
5336 PROBE_NO_ANNOUNCE = 0x04
5337} probe_flags;
5338
5339typedef struct {
5340 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5341 probe_action action;
5342 union ccb saved_ccb;
5343 probe_flags flags;
5344 MD5_CTX context;
5345 u_int8_t digest[16];
5346} probe_softc;
5347
5348static void
5349xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5350 cam_flags flags, union ccb *request_ccb)
5351{
5352 struct ccb_pathinq cpi;
5353 cam_status status;
5354 struct cam_path *new_path;
5355 struct cam_periph *old_periph;
5356 int s;
5357
5358 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5359 ("xpt_scan_lun\n"));
5360
5361 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5362 cpi.ccb_h.func_code = XPT_PATH_INQ;
5363 xpt_action((union ccb *)&cpi);
5364
5365 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5366 if (request_ccb != NULL) {
5367 request_ccb->ccb_h.status = cpi.ccb_h.status;
5368 xpt_done(request_ccb);
5369 }
5370 return;
5371 }
5372
5373 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5374 /*
5375 * Can't scan the bus on an adapter that
5376 * cannot perform the initiator role.
5377 */
5378 if (request_ccb != NULL) {
5379 request_ccb->ccb_h.status = CAM_REQ_CMP;
5380 xpt_done(request_ccb);
5381 }
5382 return;
5383 }
5384
5385 if (request_ccb == NULL) {
5386 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5387 if (request_ccb == NULL) {
5388 xpt_print_path(path);
5389 printf("xpt_scan_lun: can't allocate CCB, can't "
5390 "continue\n");
5391 return;
5392 }
5393 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5394 if (new_path == NULL) {
5395 xpt_print_path(path);
5396 printf("xpt_scan_lun: can't allocate path, can't "
5397 "continue\n");
5398 free(request_ccb, M_TEMP);
5399 return;
5400 }
5401 status = xpt_compile_path(new_path, xpt_periph,
5402 path->bus->path_id,
5403 path->target->target_id,
5404 path->device->lun_id);
5405
5406 if (status != CAM_REQ_CMP) {
5407 xpt_print_path(path);
5408 printf("xpt_scan_lun: can't compile path, can't "
5409 "continue\n");
5410 free(request_ccb, M_TEMP);
5411 free(new_path, M_TEMP);
5412 return;
5413 }
5414 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5415 request_ccb->ccb_h.cbfcnp = xptscandone;
5416 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5417 request_ccb->crcn.flags = flags;
5418 }
5419
5420 s = splsoftcam();
5421 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5422 probe_softc *softc;
5423
5424 softc = (probe_softc *)old_periph->softc;
5425 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5426 periph_links.tqe);
5427 } else {
5428 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5429 probestart, "probe",
5430 CAM_PERIPH_BIO,
5431 request_ccb->ccb_h.path, NULL, 0,
5432 request_ccb);
5433
5434 if (status != CAM_REQ_CMP) {
5435 xpt_print_path(path);
5436 printf("xpt_scan_lun: cam_alloc_periph returned an "
5437 "error, can't continue probe\n");
5438 request_ccb->ccb_h.status = status;
5439 xpt_done(request_ccb);
5440 }
5441 }
5442 splx(s);
5443}
5444
5445static void
5446xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5447{
5448 xpt_release_path(done_ccb->ccb_h.path);
5449 free(done_ccb->ccb_h.path, M_TEMP);
5450 free(done_ccb, M_TEMP);
5451}
5452
5453static cam_status
5454proberegister(struct cam_periph *periph, void *arg)
5455{
5456 union ccb *request_ccb; /* CCB representing the probe request */
5457 probe_softc *softc;
5458
5459 request_ccb = (union ccb *)arg;
5460 if (periph == NULL) {
5461 printf("proberegister: periph was NULL!!\n");
5462 return(CAM_REQ_CMP_ERR);
5463 }
5464
5465 if (request_ccb == NULL) {
5466 printf("proberegister: no probe CCB, "
5467 "can't register device\n");
5468 return(CAM_REQ_CMP_ERR);
5469 }
5470
5471 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5472
5473 if (softc == NULL) {
5474 printf("proberegister: Unable to probe new device. "
5475 "Unable to allocate softc\n");
5476 return(CAM_REQ_CMP_ERR);
5477 }
5478 TAILQ_INIT(&softc->request_ccbs);
5479 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5480 periph_links.tqe);
5481 softc->flags = 0;
5482 periph->softc = softc;
5483 cam_periph_acquire(periph);
5484 /*
5485 * Ensure we've waited at least a bus settle
5486 * delay before attempting to probe the device.
5487 * For HBAs that don't do bus resets, this won't make a difference.
5488 */
5489 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5490 scsi_delay);
5491 probeschedule(periph);
5492 return(CAM_REQ_CMP);
5493}
5494
5495static void
5496probeschedule(struct cam_periph *periph)
5497{
5498 struct ccb_pathinq cpi;
5499 union ccb *ccb;
5500 probe_softc *softc;
5501
5502 softc = (probe_softc *)periph->softc;
5503 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5504
5505 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5506 cpi.ccb_h.func_code = XPT_PATH_INQ;
5507 xpt_action((union ccb *)&cpi);
5508
5509 /*
5510 * If a device has gone away and another device, or the same one,
5511 * is back in the same place, it should have a unit attention
5512 * condition pending. It will not report the unit attention in
5513 * response to an inquiry, which may leave invalid transfer
5514 * negotiations in effect. The TUR will reveal the unit attention
5515 * condition. Only send the TUR for lun 0, since some devices
5516 * will get confused by commands other than inquiry to non-existent
5517 * luns. If you think a device has gone away start your scan from
5518 * lun 0. This will insure that any bogus transfer settings are
5519 * invalidated.
5520 *
5521 * If we haven't seen the device before and the controller supports
5522 * some kind of transfer negotiation, negotiate with the first
5523 * sent command if no bus reset was performed at startup. This
5524 * ensures that the device is not confused by transfer negotiation
5525 * settings left over by loader or BIOS action.
5526 */
5527 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5528 && (ccb->ccb_h.target_lun == 0)) {
5529 softc->action = PROBE_TUR;
5530 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5531 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5532 proberequestdefaultnegotiation(periph);
5533 softc->action = PROBE_INQUIRY;
5534 } else {
5535 softc->action = PROBE_INQUIRY;
5536 }
5537
5538 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5539 softc->flags |= PROBE_NO_ANNOUNCE;
5540 else
5541 softc->flags &= ~PROBE_NO_ANNOUNCE;
5542
5543 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5544}
5545
5546static void
5547probestart(struct cam_periph *periph, union ccb *start_ccb)
5548{
5549 /* Probe the device that our peripheral driver points to */
5550 struct ccb_scsiio *csio;
5551 probe_softc *softc;
5552
5553 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5554
5555 softc = (probe_softc *)periph->softc;
5556 csio = &start_ccb->csio;
5557
5558 switch (softc->action) {
5559 case PROBE_TUR:
5560 case PROBE_TUR_FOR_NEGOTIATION:
5561 {
5562 scsi_test_unit_ready(csio,
5563 /*retries*/4,
5564 probedone,
5565 MSG_SIMPLE_Q_TAG,
5566 SSD_FULL_SIZE,
5567 /*timeout*/60000);
5568 break;
5569 }
5570 case PROBE_INQUIRY:
5571 case PROBE_FULL_INQUIRY:
5572 {
5573 u_int inquiry_len;
5574 struct scsi_inquiry_data *inq_buf;
5575
5576 inq_buf = &periph->path->device->inq_data;
5577 /*
5578 * If the device is currently configured, we calculate an
5579 * MD5 checksum of the inquiry data, and if the serial number
5580 * length is greater than 0, add the serial number data
5581 * into the checksum as well. Once the inquiry and the
5582 * serial number check finish, we attempt to figure out
5583 * whether we still have the same device.
5584 */
5585 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5586
5587 MD5Init(&softc->context);
5588 MD5Update(&softc->context, (unsigned char *)inq_buf,
5589 sizeof(struct scsi_inquiry_data));
5590 softc->flags |= PROBE_INQUIRY_CKSUM;
5591 if (periph->path->device->serial_num_len > 0) {
5592 MD5Update(&softc->context,
5593 periph->path->device->serial_num,
5594 periph->path->device->serial_num_len);
5595 softc->flags |= PROBE_SERIAL_CKSUM;
5596 }
5597 MD5Final(softc->digest, &softc->context);
5598 }
5599
5600 if (softc->action == PROBE_INQUIRY)
5601 inquiry_len = SHORT_INQUIRY_LENGTH;
5602 else
5603 inquiry_len = inq_buf->additional_length + 4;
5604
5605 scsi_inquiry(csio,
5606 /*retries*/4,
5607 probedone,
5608 MSG_SIMPLE_Q_TAG,
5609 (u_int8_t *)inq_buf,
5610 inquiry_len,
5611 /*evpd*/FALSE,
5612 /*page_code*/0,
5613 SSD_MIN_SIZE,
5614 /*timeout*/60 * 1000);
5615 break;
5616 }
5617 case PROBE_MODE_SENSE:
5618 {
5619 void *mode_buf;
5620 int mode_buf_len;
5621
5622 mode_buf_len = sizeof(struct scsi_mode_header_6)
5623 + sizeof(struct scsi_mode_blk_desc)
5624 + sizeof(struct scsi_control_page);
5625 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5626 if (mode_buf != NULL) {
5627 scsi_mode_sense(csio,
5628 /*retries*/4,
5629 probedone,
5630 MSG_SIMPLE_Q_TAG,
5631 /*dbd*/FALSE,
5632 SMS_PAGE_CTRL_CURRENT,
5633 SMS_CONTROL_MODE_PAGE,
5634 mode_buf,
5635 mode_buf_len,
5636 SSD_FULL_SIZE,
5637 /*timeout*/60000);
5638 break;
5639 }
5640 xpt_print_path(periph->path);
5641 printf("Unable to mode sense control page - malloc failure\n");
5642 softc->action = PROBE_SERIAL_NUM;
5643 /* FALLTHROUGH */
5644 }
5645 case PROBE_SERIAL_NUM:
5646 {
5647 struct scsi_vpd_unit_serial_number *serial_buf;
5648 struct cam_ed* device;
5649
5650 serial_buf = NULL;
5651 device = periph->path->device;
5652 device->serial_num = NULL;
5653 device->serial_num_len = 0;
5654
5655 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5656 serial_buf = (struct scsi_vpd_unit_serial_number *)
5657 malloc(sizeof(*serial_buf), M_TEMP,
5658 M_NOWAIT | M_ZERO);
5659
5660 if (serial_buf != NULL) {
5661 scsi_inquiry(csio,
5662 /*retries*/4,
5663 probedone,
5664 MSG_SIMPLE_Q_TAG,
5665 (u_int8_t *)serial_buf,
5666 sizeof(*serial_buf),
5667 /*evpd*/TRUE,
5668 SVPD_UNIT_SERIAL_NUMBER,
5669 SSD_MIN_SIZE,
5670 /*timeout*/60 * 1000);
5671 break;
5672 }
5673 /*
5674 * We'll have to do without, let our probedone
5675 * routine finish up for us.
5676 */
5677 start_ccb->csio.data_ptr = NULL;
5678 probedone(periph, start_ccb);
5679 return;
5680 }
5681 }
5682 xpt_action(start_ccb);
5683}
5684
5685static void
5686proberequestdefaultnegotiation(struct cam_periph *periph)
5687{
5688 struct ccb_trans_settings cts;
5689
5690 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5691 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5692#ifdef CAM_NEW_TRAN_CODE
5693 cts.type = CTS_TYPE_USER_SETTINGS;
5694#else /* CAM_NEW_TRAN_CODE */
5695 cts.flags = CCB_TRANS_USER_SETTINGS;
5696#endif /* CAM_NEW_TRAN_CODE */
5697 xpt_action((union ccb *)&cts);
5698 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5699#ifdef CAM_NEW_TRAN_CODE
5700 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5701#else /* CAM_NEW_TRAN_CODE */
5702 cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5703 cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5704#endif /* CAM_NEW_TRAN_CODE */
5705 xpt_action((union ccb *)&cts);
5706}
5707
5708static void
5709probedone(struct cam_periph *periph, union ccb *done_ccb)
5710{
5711 probe_softc *softc;
5712 struct cam_path *path;
5713 u_int32_t priority;
5714
5715 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5716
5717 softc = (probe_softc *)periph->softc;
5718 path = done_ccb->ccb_h.path;
5719 priority = done_ccb->ccb_h.pinfo.priority;
5720
5721 switch (softc->action) {
5722 case PROBE_TUR:
5723 {
5724 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5725
5726 if (cam_periph_error(done_ccb, 0,
5727 SF_NO_PRINT, NULL) == ERESTART)
5728 return;
5729 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5730 /* Don't wedge the queue */
5731 xpt_release_devq(done_ccb->ccb_h.path,
5732 /*count*/1,
5733 /*run_queue*/TRUE);
5734 }
5735 softc->action = PROBE_INQUIRY;
5736 xpt_release_ccb(done_ccb);
5737 xpt_schedule(periph, priority);
5738 return;
5739 }
5740 case PROBE_INQUIRY:
5741 case PROBE_FULL_INQUIRY:
5742 {
5743 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5744 struct scsi_inquiry_data *inq_buf;
5745 u_int8_t periph_qual;
5746
5747 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5748 inq_buf = &path->device->inq_data;
5749
5750 periph_qual = SID_QUAL(inq_buf);
5751
5752 switch(periph_qual) {
5753 case SID_QUAL_LU_CONNECTED:
5754 {
5755 u_int8_t alen;
5756
5757 /*
5758 * We conservatively request only
5759 * SHORT_INQUIRY_LEN bytes of inquiry
5760 * information during our first try
5761 * at sending an INQUIRY. If the device
5762 * has more information to give,
5763 * perform a second request specifying
5764 * the amount of information the device
5765 * is willing to give.
5766 */
5767 alen = inq_buf->additional_length;
5768 if (softc->action == PROBE_INQUIRY
5769 && alen > (SHORT_INQUIRY_LENGTH - 4)) {
5770 softc->action = PROBE_FULL_INQUIRY;
5771 xpt_release_ccb(done_ccb);
5772 xpt_schedule(periph, priority);
5773 return;
5774 }
5775
5776 xpt_find_quirk(path->device);
5777
5778#ifdef CAM_NEW_TRAN_CODE
5779 xpt_devise_transport(path);
5780#endif /* CAM_NEW_TRAN_CODE */
5781 if ((inq_buf->flags & SID_CmdQue) != 0)
5782 softc->action = PROBE_MODE_SENSE;
5783 else
5784 softc->action = PROBE_SERIAL_NUM;
5785
5786 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5787
5788 xpt_release_ccb(done_ccb);
5789 xpt_schedule(periph, priority);
5790 return;
5791 }
5792 default:
5793 break;
5794 }
5795 } else if (cam_periph_error(done_ccb, 0,
5796 done_ccb->ccb_h.target_lun > 0
5797 ? SF_RETRY_UA|SF_QUIET_IR
5798 : SF_RETRY_UA,
5799 &softc->saved_ccb) == ERESTART) {
5800 return;
5801 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5802 /* Don't wedge the queue */
5803 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5804 /*run_queue*/TRUE);
5805 }
5806 /*
5807 * If we get to this point, we got an error status back
5808 * from the inquiry and the error status doesn't require
5809 * automatically retrying the command. Therefore, the
5810 * inquiry failed. If we had inquiry information before
5811 * for this device, but this latest inquiry command failed,
5812 * the device has probably gone away. If this device isn't
5813 * already marked unconfigured, notify the peripheral
5814 * drivers that this device is no more.
5815 */
5816 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5817 /* Send the async notification. */
5818 xpt_async(AC_LOST_DEVICE, path, NULL);
5819
5820 xpt_release_ccb(done_ccb);
5821 break;
5822 }
5823 case PROBE_MODE_SENSE:
5824 {
5825 struct ccb_scsiio *csio;
5826 struct scsi_mode_header_6 *mode_hdr;
5827
5828 csio = &done_ccb->csio;
5829 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5830 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5831 struct scsi_control_page *page;
5832 u_int8_t *offset;
5833
5834 offset = ((u_int8_t *)&mode_hdr[1])
5835 + mode_hdr->blk_desc_len;
5836 page = (struct scsi_control_page *)offset;
5837 path->device->queue_flags = page->queue_flags;
5838 } else if (cam_periph_error(done_ccb, 0,
5839 SF_RETRY_UA|SF_NO_PRINT,
5840 &softc->saved_ccb) == ERESTART) {
5841 return;
5842 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5843 /* Don't wedge the queue */
5844 xpt_release_devq(done_ccb->ccb_h.path,
5845 /*count*/1, /*run_queue*/TRUE);
5846 }
5847 xpt_release_ccb(done_ccb);
5848 free(mode_hdr, M_TEMP);
5849 softc->action = PROBE_SERIAL_NUM;
5850 xpt_schedule(periph, priority);
5851 return;
5852 }
5853 case PROBE_SERIAL_NUM:
5854 {
5855 struct ccb_scsiio *csio;
5856 struct scsi_vpd_unit_serial_number *serial_buf;
5857 u_int32_t priority;
5858 int changed;
5859 int have_serialnum;
5860
5861 changed = 1;
5862 have_serialnum = 0;
5863 csio = &done_ccb->csio;
5864 priority = done_ccb->ccb_h.pinfo.priority;
5865 serial_buf =
5866 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5867
5868 /* Clean up from previous instance of this device */
5869 if (path->device->serial_num != NULL) {
5870 free(path->device->serial_num, M_DEVBUF);
5871 path->device->serial_num = NULL;
5872 path->device->serial_num_len = 0;
5873 }
5874
5875 if (serial_buf == NULL) {
5876 /*
5877 * Don't process the command as it was never sent
5878 */
5879 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5880 && (serial_buf->length > 0)) {
5881
5882 have_serialnum = 1;
5883 path->device->serial_num =
5884 (u_int8_t *)malloc((serial_buf->length + 1),
5885 M_DEVBUF, M_NOWAIT);
5886 if (path->device->serial_num != NULL) {
5887 bcopy(serial_buf->serial_num,
5888 path->device->serial_num,
5889 serial_buf->length);
5890 path->device->serial_num_len =
5891 serial_buf->length;
5892 path->device->serial_num[serial_buf->length]
5893 = '\0';
5894 }
5895 } else if (cam_periph_error(done_ccb, 0,
5896 SF_RETRY_UA|SF_NO_PRINT,
5897 &softc->saved_ccb) == ERESTART) {
5898 return;
5899 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5900 /* Don't wedge the queue */
5901 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5902 /*run_queue*/TRUE);
5903 }
5904
5905 /*
5906 * Let's see if we have seen this device before.
5907 */
5908 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
5909 MD5_CTX context;
5910 u_int8_t digest[16];
5911
5912 MD5Init(&context);
5913
5914 MD5Update(&context,
5915 (unsigned char *)&path->device->inq_data,
5916 sizeof(struct scsi_inquiry_data));
5917
5918 if (have_serialnum)
5919 MD5Update(&context, serial_buf->serial_num,
5920 serial_buf->length);
5921
5922 MD5Final(digest, &context);
5923 if (bcmp(softc->digest, digest, 16) == 0)
5924 changed = 0;
5925
5926 /*
5927 * XXX Do we need to do a TUR in order to ensure
5928 * that the device really hasn't changed???
5929 */
5930 if ((changed != 0)
5931 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
5932 xpt_async(AC_LOST_DEVICE, path, NULL);
5933 }
5934 if (serial_buf != NULL)
5935 free(serial_buf, M_TEMP);
5936
5937 if (changed != 0) {
5938 /*
5939 * Now that we have all the necessary
5940 * information to safely perform transfer
5941 * negotiations... Controllers don't perform
5942 * any negotiation or tagged queuing until
5943 * after the first XPT_SET_TRAN_SETTINGS ccb is
5944 * received. So, on a new device, just retreive
5945 * the user settings, and set them as the current
5946 * settings to set the device up.
5947 */
5948 proberequestdefaultnegotiation(periph);
5949 xpt_release_ccb(done_ccb);
5950
5951 /*
5952 * Perform a TUR to allow the controller to
5953 * perform any necessary transfer negotiation.
5954 */
5955 softc->action = PROBE_TUR_FOR_NEGOTIATION;
5956 xpt_schedule(periph, priority);
5957 return;
5958 }
5959 xpt_release_ccb(done_ccb);
5960 break;
5961 }
5962 case PROBE_TUR_FOR_NEGOTIATION:
5963 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5964 /* Don't wedge the queue */
5965 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5966 /*run_queue*/TRUE);
5967 }
5968
5969 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5970
5971 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
5972 /* Inform the XPT that a new device has been found */
5973 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5974 xpt_action(done_ccb);
5975
5976 xpt_async(AC_FOUND_DEVICE, xpt_periph->path, done_ccb);
5977 }
5978 xpt_release_ccb(done_ccb);
5979 break;
5980 }
5981 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5982 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
5983 done_ccb->ccb_h.status = CAM_REQ_CMP;
5984 xpt_done(done_ccb);
5985 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
5986 cam_periph_invalidate(periph);
5987 cam_periph_release(periph);
5988 } else {
5989 probeschedule(periph);
5990 }
5991}
5992
5993static void
5994probecleanup(struct cam_periph *periph)
5995{
5996 free(periph->softc, M_TEMP);
5997}
5998
5999static void
6000xpt_find_quirk(struct cam_ed *device)
6001{
6002 caddr_t match;
6003
6004 match = cam_quirkmatch((caddr_t)&device->inq_data,
6005 (caddr_t)xpt_quirk_table,
6006 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
6007 sizeof(*xpt_quirk_table), scsi_inquiry_match);
6008
6009 if (match == NULL)
6010 panic("xpt_find_quirk: device didn't match wildcard entry!!");
6011
6012 device->quirk = (struct xpt_quirk_entry *)match;
6013}
6014
6015#ifdef CAM_NEW_TRAN_CODE
6016
6017static void
6018xpt_devise_transport(struct cam_path *path)
6019{
6020 struct ccb_pathinq cpi;
6021 struct ccb_trans_settings cts;
6022 struct scsi_inquiry_data *inq_buf;
6023
6024 /* Get transport information from the SIM */
6025 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
6026 cpi.ccb_h.func_code = XPT_PATH_INQ;
6027 xpt_action((union ccb *)&cpi);
6028
6029 inq_buf = NULL;
6030 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
6031 inq_buf = &path->device->inq_data;
6032 path->device->protocol = PROTO_SCSI;
6033 path->device->protocol_version =
6034 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
6035 path->device->transport = cpi.transport;
6036 path->device->transport_version = cpi.transport_version;
6037
6038 /*
6039 * Any device not using SPI3 features should
6040 * be considered SPI2 or lower.
6041 */
6042 if (inq_buf != NULL) {
6043 if (path->device->transport == XPORT_SPI
6044 && (inq_buf->spi3data & SID_SPI_MASK) == 0
6045 && path->device->transport_version > 2)
6046 path->device->transport_version = 2;
6047 } else {
6048 struct cam_ed* otherdev;
6049
6050 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6051 otherdev != NULL;
6052 otherdev = TAILQ_NEXT(otherdev, links)) {
6053 if (otherdev != path->device)
6054 break;
6055 }
6056
6057 if (otherdev != NULL) {
6058 /*
6059 * Initially assume the same versioning as
6060 * prior luns for this target.
6061 */
6062 path->device->protocol_version =
6063 otherdev->protocol_version;
6064 path->device->transport_version =
6065 otherdev->transport_version;
6066 } else {
6067 /* Until we know better, opt for safty */
6068 path->device->protocol_version = 2;
6069 if (path->device->transport == XPORT_SPI)
6070 path->device->transport_version = 2;
6071 else
6072 path->device->transport_version = 0;
6073 }
6074 }
6075
6076 /*
6077 * XXX
6078 * For a device compliant with SPC-2 we should be able
6079 * to determine the transport version supported by
6080 * scrutinizing the version descriptors in the
6081 * inquiry buffer.
6082 */
6083
6084 /* Tell the controller what we think */
6085 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
6086 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6087 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6088 cts.transport = path->device->transport;
6089 cts.transport_version = path->device->transport_version;
6090 cts.protocol = path->device->protocol;
6091 cts.protocol_version = path->device->protocol_version;
6092 cts.proto_specific.valid = 0;
6093 cts.xport_specific.valid = 0;
6094 xpt_action((union ccb *)&cts);
6095}
6096
6097static void
6098xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6099 int async_update)
6100{
6101 struct ccb_pathinq cpi;
6102 struct ccb_trans_settings cur_cts;
6103 struct ccb_trans_settings_scsi *scsi;
6104 struct ccb_trans_settings_scsi *cur_scsi;
6105 struct cam_sim *sim;
6106 struct scsi_inquiry_data *inq_data;
6107
6108 if (device == NULL) {
6109 cts->ccb_h.status = CAM_PATH_INVALID;
6110 xpt_done((union ccb *)cts);
6111 return;
6112 }
6113
6114 if (cts->protocol == PROTO_UNKNOWN
6115 || cts->protocol == PROTO_UNSPECIFIED) {
6116 cts->protocol = device->protocol;
6117 cts->protocol_version = device->protocol_version;
6118 }
6119
6120 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
6121 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
6122 cts->protocol_version = device->protocol_version;
6123
6124 if (cts->protocol != device->protocol) {
6125 xpt_print_path(cts->ccb_h.path);
6126 printf("Uninitialized Protocol %x:%x?\n",
6127 cts->protocol, device->protocol);
6128 cts->protocol = device->protocol;
6129 }
6130
6131 if (cts->protocol_version > device->protocol_version) {
6132 if (bootverbose) {
6133 xpt_print_path(cts->ccb_h.path);
6134 printf("Down reving Protocol Version from %d to %d?\n",
6135 cts->protocol_version, device->protocol_version);
6136 }
6137 cts->protocol_version = device->protocol_version;
6138 }
6139
6140 if (cts->transport == XPORT_UNKNOWN
6141 || cts->transport == XPORT_UNSPECIFIED) {
6142 cts->transport = device->transport;
6143 cts->transport_version = device->transport_version;
6144 }
6145
6146 if (cts->transport_version == XPORT_VERSION_UNKNOWN
6147 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
6148 cts->transport_version = device->transport_version;
6149
6150 if (cts->transport != device->transport) {
6151 xpt_print_path(cts->ccb_h.path);
6152 printf("Uninitialized Transport %x:%x?\n",
6153 cts->transport, device->transport);
6154 cts->transport = device->transport;
6155 }
6156
6157 if (cts->transport_version > device->transport_version) {
6158 if (bootverbose) {
6159 xpt_print_path(cts->ccb_h.path);
6160 printf("Down reving Transport Version from %d to %d?\n",
6161 cts->transport_version,
6162 device->transport_version);
6163 }
6164 cts->transport_version = device->transport_version;
6165 }
6166
6167 sim = cts->ccb_h.path->bus->sim;
6168
6169 /*
6170 * Nothing more of interest to do unless
6171 * this is a device connected via the
6172 * SCSI protocol.
6173 */
6174 if (cts->protocol != PROTO_SCSI) {
6175 if (async_update == FALSE)
6176 (*(sim->sim_action))(sim, (union ccb *)cts);
6177 return;
6178 }
6179
6180 inq_data = &device->inq_data;
6181 scsi = &cts->proto_specific.scsi;
6182 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6183 cpi.ccb_h.func_code = XPT_PATH_INQ;
6184 xpt_action((union ccb *)&cpi);
6185
6186 /* SCSI specific sanity checking */
6187 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6188 || (inq_data->flags & SID_CmdQue) == 0
6189 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6190 || (device->quirk->mintags == 0)) {
6191 /*
6192 * Can't tag on hardware that doesn't support tags,
6193 * doesn't have it enabled, or has broken tag support.
6194 */
6195 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6196 }
6197
6198 if (async_update == FALSE) {
6199 /*
6200 * Perform sanity checking against what the
6201 * controller and device can do.
6202 */
6203 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6204 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6205 cur_cts.type = cts->type;
6206 xpt_action((union ccb *)&cur_cts);
6207
6208 cur_scsi = &cur_cts.proto_specific.scsi;
6209 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
6210 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6211 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
6212 }
6213 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
6214 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6215 }
6216
6217 /* SPI specific sanity checking */
6218 if (cts->transport == XPORT_SPI && async_update == FALSE) {
6219 u_int spi3caps;
6220 struct ccb_trans_settings_spi *spi;
6221 struct ccb_trans_settings_spi *cur_spi;
6222
6223 spi = &cts->xport_specific.spi;
6224
6225 cur_spi = &cur_cts.xport_specific.spi;
6226
6227 /* Fill in any gaps in what the user gave us */
6228 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6229 spi->sync_period = cur_spi->sync_period;
6230 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6231 spi->sync_period = 0;
6232 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6233 spi->sync_offset = cur_spi->sync_offset;
6234 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6235 spi->sync_offset = 0;
6236 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6237 spi->ppr_options = cur_spi->ppr_options;
6238 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6239 spi->ppr_options = 0;
6240 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6241 spi->bus_width = cur_spi->bus_width;
6242 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6243 spi->bus_width = 0;
6244 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
6245 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6246 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
6247 }
6248 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
6249 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6250 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6251 && (inq_data->flags & SID_Sync) == 0
6252 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6253 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6254 || (cur_spi->sync_offset == 0)
6255 || (cur_spi->sync_period == 0)) {
6256 /* Force async */
6257 spi->sync_period = 0;
6258 spi->sync_offset = 0;
6259 }
6260
6261 switch (spi->bus_width) {
6262 case MSG_EXT_WDTR_BUS_32_BIT:
6263 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6264 || (inq_data->flags & SID_WBus32) != 0
6265 || cts->type == CTS_TYPE_USER_SETTINGS)
6266 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6267 break;
6268 /* Fall Through to 16-bit */
6269 case MSG_EXT_WDTR_BUS_16_BIT:
6270 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6271 || (inq_data->flags & SID_WBus16) != 0
6272 || cts->type == CTS_TYPE_USER_SETTINGS)
6273 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6274 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6275 break;
6276 }
6277 /* Fall Through to 8-bit */
6278 default: /* New bus width?? */
6279 case MSG_EXT_WDTR_BUS_8_BIT:
6280 /* All targets can do this */
6281 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6282 break;
6283 }
6284
6285 spi3caps = cpi.xport_specific.spi.ppr_options;
6286 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6287 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6288 spi3caps &= inq_data->spi3data;
6289
6290 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
6291 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
6292
6293 if ((spi3caps & SID_SPI_IUS) == 0)
6294 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
6295
6296 if ((spi3caps & SID_SPI_QAS) == 0)
6297 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
6298
6299 /* No SPI Transfer settings are allowed unless we are wide */
6300 if (spi->bus_width == 0)
6301 spi->ppr_options = 0;
6302
6303 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
6304 /*
6305 * Can't tag queue without disconnection.
6306 */
6307 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6308 scsi->valid |= CTS_SCSI_VALID_TQ;
6309 }
6310
6311 /*
6312 * If we are currently performing tagged transactions to
6313 * this device and want to change its negotiation parameters,
6314 * go non-tagged for a bit to give the controller a chance to
6315 * negotiate unhampered by tag messages.
6316 */
6317 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6318 && (device->inq_flags & SID_CmdQue) != 0
6319 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6320 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
6321 CTS_SPI_VALID_SYNC_OFFSET|
6322 CTS_SPI_VALID_BUS_WIDTH)) != 0)
6323 xpt_toggle_tags(cts->ccb_h.path);
6324 }
6325
6326 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6327 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
6328 int device_tagenb;
6329
6330 /*
6331 * If we are transitioning from tags to no-tags or
6332 * vice-versa, we need to carefully freeze and restart
6333 * the queue so that we don't overlap tagged and non-tagged
6334 * commands. We also temporarily stop tags if there is
6335 * a change in transfer negotiation settings to allow
6336 * "tag-less" negotiation.
6337 */
6338 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6339 || (device->inq_flags & SID_CmdQue) != 0)
6340 device_tagenb = TRUE;
6341 else
6342 device_tagenb = FALSE;
6343
6344 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6345 && device_tagenb == FALSE)
6346 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
6347 && device_tagenb == TRUE)) {
6348
6349 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
6350 /*
6351 * Delay change to use tags until after a
6352 * few commands have gone to this device so
6353 * the controller has time to perform transfer
6354 * negotiations without tagged messages getting
6355 * in the way.
6356 */
6357 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6358 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6359 } else {
6360 struct ccb_relsim crs;
6361
6362 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6363 device->inq_flags &= ~SID_CmdQue;
6364 xpt_dev_ccbq_resize(cts->ccb_h.path,
6365 sim->max_dev_openings);
6366 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6367 device->tag_delay_count = 0;
6368
6369 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6370 /*priority*/1);
6371 crs.ccb_h.func_code = XPT_REL_SIMQ;
6372 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6373 crs.openings
6374 = crs.release_timeout
6375 = crs.qfrozen_cnt
6376 = 0;
6377 xpt_action((union ccb *)&crs);
6378 }
6379 }
6380 }
6381 if (async_update == FALSE)
6382 (*(sim->sim_action))(sim, (union ccb *)cts);
6383}
6384
6385#else /* CAM_NEW_TRAN_CODE */
6386
6387static void
6388xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6389 int async_update)
6390{
6391 struct cam_sim *sim;
6392 int qfrozen;
6393
6394 sim = cts->ccb_h.path->bus->sim;
6395 if (async_update == FALSE) {
6396 struct scsi_inquiry_data *inq_data;
6397 struct ccb_pathinq cpi;
6398 struct ccb_trans_settings cur_cts;
6399
6400 if (device == NULL) {
6401 cts->ccb_h.status = CAM_PATH_INVALID;
6402 xpt_done((union ccb *)cts);
6403 return;
6404 }
6405
6406 /*
6407 * Perform sanity checking against what the
6408 * controller and device can do.
6409 */
6410 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6411 cpi.ccb_h.func_code = XPT_PATH_INQ;
6412 xpt_action((union ccb *)&cpi);
6413 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6414 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6415 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
6416 xpt_action((union ccb *)&cur_cts);
6417 inq_data = &device->inq_data;
6418
6419 /* Fill in any gaps in what the user gave us */
6420 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
6421 cts->sync_period = cur_cts.sync_period;
6422 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
6423 cts->sync_offset = cur_cts.sync_offset;
6424 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
6425 cts->bus_width = cur_cts.bus_width;
6426 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
6427 cts->flags &= ~CCB_TRANS_DISC_ENB;
6428 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
6429 }
6430 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
6431 cts->flags &= ~CCB_TRANS_TAG_ENB;
6432 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
6433 }
6434
6435 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6436 && (inq_data->flags & SID_Sync) == 0)
6437 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6438 || (cts->sync_offset == 0)
6439 || (cts->sync_period == 0)) {
6440 /* Force async */
6441 cts->sync_period = 0;
6442 cts->sync_offset = 0;
6443 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6444 && (inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
6445 && cts->sync_period <= 0x9) {
6446 /*
6447 * Don't allow DT transmission rates if the
6448 * device does not support it.
6449 */
6450 cts->sync_period = 0xa;
6451 }
6452
6453 switch (cts->bus_width) {
6454 case MSG_EXT_WDTR_BUS_32_BIT:
6455 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6456 || (inq_data->flags & SID_WBus32) != 0)
6457 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6458 break;
6459 /* FALLTHROUGH to 16-bit */
6460 case MSG_EXT_WDTR_BUS_16_BIT:
6461 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6462 || (inq_data->flags & SID_WBus16) != 0)
6463 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6464 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6465 break;
6466 }
6467 /* FALLTHROUGH to 8-bit */
6468 default: /* New bus width?? */
6469 case MSG_EXT_WDTR_BUS_8_BIT:
6470 /* All targets can do this */
6471 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6472 break;
6473 }
6474
6475 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
6476 /*
6477 * Can't tag queue without disconnection.
6478 */
6479 cts->flags &= ~CCB_TRANS_TAG_ENB;
6480 cts->valid |= CCB_TRANS_TQ_VALID;
6481 }
6482
6483 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6484 || (inq_data->flags & SID_CmdQue) == 0
6485 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6486 || (device->quirk->mintags == 0)) {
6487 /*
6488 * Can't tag on hardware that doesn't support,
6489 * doesn't have it enabled, or has broken tag support.
6490 */
6491 cts->flags &= ~CCB_TRANS_TAG_ENB;
6492 }
6493 }
6494
6495 qfrozen = FALSE;
6496 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
6497 int device_tagenb;
6498
6499 /*
6500 * If we are transitioning from tags to no-tags or
6501 * vice-versa, we need to carefully freeze and restart
6502 * the queue so that we don't overlap tagged and non-tagged
6503 * commands. We also temporarily stop tags if there is
6504 * a change in transfer negotiation settings to allow
6505 * "tag-less" negotiation.
6506 */
6507 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6508 || (device->inq_flags & SID_CmdQue) != 0)
6509 device_tagenb = TRUE;
6510 else
6511 device_tagenb = FALSE;
6512
6513 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
6514 && device_tagenb == FALSE)
6515 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
6516 && device_tagenb == TRUE)) {
6517
6518 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
6519 /*
6520 * Delay change to use tags until after a
6521 * few commands have gone to this device so
6522 * the controller has time to perform transfer
6523 * negotiations without tagged messages getting
6524 * in the way.
6525 */
6526 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6527 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6528 } else {
6529 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6530 qfrozen = TRUE;
6531 device->inq_flags &= ~SID_CmdQue;
6532 xpt_dev_ccbq_resize(cts->ccb_h.path,
6533 sim->max_dev_openings);
6534 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6535 device->tag_delay_count = 0;
6536 }
6537 }
6538 }
6539
6540 if (async_update == FALSE) {
6541 /*
6542 * If we are currently performing tagged transactions to
6543 * this device and want to change its negotiation parameters,
6544 * go non-tagged for a bit to give the controller a chance to
6545 * negotiate unhampered by tag messages.
6546 */
6547 if ((device->inq_flags & SID_CmdQue) != 0
6548 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
6549 CCB_TRANS_SYNC_OFFSET_VALID|
6550 CCB_TRANS_BUS_WIDTH_VALID)) != 0)
6551 xpt_toggle_tags(cts->ccb_h.path);
6552
6553 (*(sim->sim_action))(sim, (union ccb *)cts);
6554 }
6555
6556 if (qfrozen) {
6557 struct ccb_relsim crs;
6558
6559 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6560 /*priority*/1);
6561 crs.ccb_h.func_code = XPT_REL_SIMQ;
6562 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6563 crs.openings
6564 = crs.release_timeout
6565 = crs.qfrozen_cnt
6566 = 0;
6567 xpt_action((union ccb *)&crs);
6568 }
6569}
6570
6571
6572#endif /* CAM_NEW_TRAN_CODE */
6573
6574static void
6575xpt_toggle_tags(struct cam_path *path)
6576{
6577 struct cam_ed *dev;
6578
6579 /*
6580 * Give controllers a chance to renegotiate
6581 * before starting tag operations. We
6582 * "toggle" tagged queuing off then on
6583 * which causes the tag enable command delay
6584 * counter to come into effect.
6585 */
6586 dev = path->device;
6587 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6588 || ((dev->inq_flags & SID_CmdQue) != 0
6589 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6590 struct ccb_trans_settings cts;
6591
6592 xpt_setup_ccb(&cts.ccb_h, path, 1);
6593#ifdef CAM_NEW_TRAN_CODE
6594 cts.protocol = PROTO_SCSI;
6595 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
6596 cts.transport = XPORT_UNSPECIFIED;
6597 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
6598 cts.proto_specific.scsi.flags = 0;
6599 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
6600#else /* CAM_NEW_TRAN_CODE */
6601 cts.flags = 0;
6602 cts.valid = CCB_TRANS_TQ_VALID;
6603#endif /* CAM_NEW_TRAN_CODE */
6604 xpt_set_transfer_settings(&cts, path->device,
6605 /*async_update*/TRUE);
6606#ifdef CAM_NEW_TRAN_CODE
6607 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
6608#else /* CAM_NEW_TRAN_CODE */
6609 cts.flags = CCB_TRANS_TAG_ENB;
6610#endif /* CAM_NEW_TRAN_CODE */
6611 xpt_set_transfer_settings(&cts, path->device,
6612 /*async_update*/TRUE);
6613 }
6614}
6615
6616static void
6617xpt_start_tags(struct cam_path *path)
6618{
6619 struct ccb_relsim crs;
6620 struct cam_ed *device;
6621 struct cam_sim *sim;
6622 int newopenings;
6623
6624 device = path->device;
6625 sim = path->bus->sim;
6626 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6627 xpt_freeze_devq(path, /*count*/1);
6628 device->inq_flags |= SID_CmdQue;
6629 newopenings = min(device->quirk->maxtags, sim->max_tagged_dev_openings);
6630 xpt_dev_ccbq_resize(path, newopenings);
6631 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6632 crs.ccb_h.func_code = XPT_REL_SIMQ;
6633 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6634 crs.openings
6635 = crs.release_timeout
6636 = crs.qfrozen_cnt
6637 = 0;
6638 xpt_action((union ccb *)&crs);
6639}
6640
6641static int busses_to_config;
6642static int busses_to_reset;
6643
6644static int
6645xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6646{
6647 if (bus->path_id != CAM_XPT_PATH_ID) {
6648 struct cam_path path;
6649 struct ccb_pathinq cpi;
6650 int can_negotiate;
6651
6652 busses_to_config++;
6653 xpt_compile_path(&path, NULL, bus->path_id,
6654 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6655 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6656 cpi.ccb_h.func_code = XPT_PATH_INQ;
6657 xpt_action((union ccb *)&cpi);
6658 can_negotiate = cpi.hba_inquiry;
6659 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6660 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6661 && can_negotiate)
6662 busses_to_reset++;
6663 xpt_release_path(&path);
6664 }
6665
6666 return(1);
6667}
6668
6669static int
6670xptconfigfunc(struct cam_eb *bus, void *arg)
6671{
6672 struct cam_path *path;
6673 union ccb *work_ccb;
6674
6675 if (bus->path_id != CAM_XPT_PATH_ID) {
6676 cam_status status;
6677 int can_negotiate;
6678
6679 work_ccb = xpt_alloc_ccb();
6680 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6681 CAM_TARGET_WILDCARD,
6682 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6683 printf("xptconfigfunc: xpt_create_path failed with "
6684 "status %#x for bus %d\n", status, bus->path_id);
6685 printf("xptconfigfunc: halting bus configuration\n");
6686 xpt_free_ccb(work_ccb);
6687 busses_to_config--;
6688 xpt_finishconfig(xpt_periph, NULL);
6689 return(0);
6690 }
6691 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6692 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6693 xpt_action(work_ccb);
6694 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6695 printf("xptconfigfunc: CPI failed on bus %d "
6696 "with status %d\n", bus->path_id,
6697 work_ccb->ccb_h.status);
6698 xpt_finishconfig(xpt_periph, work_ccb);
6699 return(1);
6700 }
6701
6702 can_negotiate = work_ccb->cpi.hba_inquiry;
6703 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6704 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6705 && (can_negotiate != 0)) {
6706 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6707 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6708 work_ccb->ccb_h.cbfcnp = NULL;
6709 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6710 ("Resetting Bus\n"));
6711 xpt_action(work_ccb);
6712 xpt_finishconfig(xpt_periph, work_ccb);
6713 } else {
6714 /* Act as though we performed a successful BUS RESET */
6715 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6716 xpt_finishconfig(xpt_periph, work_ccb);
6717 }
6718 }
6719
6720 return(1);
6721}
6722
6723static void
6724xpt_config(void *arg)
6725{
6726 /*
6727 * Now that interrupts are enabled, go find our devices
6728 */
6729
6730#ifdef CAMDEBUG
6731 /* Setup debugging flags and path */
6732#ifdef CAM_DEBUG_FLAGS
6733 cam_dflags = CAM_DEBUG_FLAGS;
6734#else /* !CAM_DEBUG_FLAGS */
6735 cam_dflags = CAM_DEBUG_NONE;
6736#endif /* CAM_DEBUG_FLAGS */
6737#ifdef CAM_DEBUG_BUS
6738 if (cam_dflags != CAM_DEBUG_NONE) {
6739 if (xpt_create_path(&cam_dpath, xpt_periph,
6740 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6741 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6742 printf("xpt_config: xpt_create_path() failed for debug"
6743 " target %d:%d:%d, debugging disabled\n",
6744 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6745 cam_dflags = CAM_DEBUG_NONE;
6746 }
6747 } else
6748 cam_dpath = NULL;
6749#else /* !CAM_DEBUG_BUS */
6750 cam_dpath = NULL;
6751#endif /* CAM_DEBUG_BUS */
6752#endif /* CAMDEBUG */
6753
6754 /*
6755 * Scan all installed busses.
6756 */
6757 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6758
6759 if (busses_to_config == 0) {
6760 /* Call manually because we don't have any busses */
6761 xpt_finishconfig(xpt_periph, NULL);
6762 } else {
6763 if (busses_to_reset > 0 && scsi_delay >= 2000) {
6764 printf("Waiting %d seconds for SCSI "
6765 "devices to settle\n", scsi_delay/1000);
6766 }
6767 xpt_for_all_busses(xptconfigfunc, NULL);
6768 }
6769}
6770
6771/*
6772 * If the given device only has one peripheral attached to it, and if that
6773 * peripheral is the passthrough driver, announce it. This insures that the
6774 * user sees some sort of announcement for every peripheral in their system.
6775 */
6776static int
6777xptpassannouncefunc(struct cam_ed *device, void *arg)
6778{
6779 struct cam_periph *periph;
6780 int i;
6781
6782 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6783 periph = SLIST_NEXT(periph, periph_links), i++);
6784
6785 periph = SLIST_FIRST(&device->periphs);
6786 if ((i == 1)
6787 && (strncmp(periph->periph_name, "pass", 4) == 0))
6788 xpt_announce_periph(periph, NULL);
6789
6790 return(1);
6791}
6792
6793static void
6794xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6795{
6796 struct periph_driver **p_drv;
6797 int i;
6798
6799 if (done_ccb != NULL) {
6800 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6801 ("xpt_finishconfig\n"));
6802 switch(done_ccb->ccb_h.func_code) {
6803 case XPT_RESET_BUS:
6804 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6805 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6806 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6807 xpt_action(done_ccb);
6808 return;
6809 }
6810 /* FALLTHROUGH */
6811 case XPT_SCAN_BUS:
6812 default:
6813 xpt_free_path(done_ccb->ccb_h.path);
6814 busses_to_config--;
6815 break;
6816 }
6817 }
6818
6819 if (busses_to_config == 0) {
6820 /* Register all the peripheral drivers */
6821 /* XXX This will have to change when we have loadable modules */
6822 p_drv = periph_drivers;
6823 for (i = 0; p_drv[i] != NULL; i++) {
6824 (*p_drv[i]->init)();
6825 }
6826
6827 /*
6828 * Check for devices with no "standard" peripheral driver
6829 * attached. For any devices like that, announce the
6830 * passthrough driver so the user will see something.
6831 */
6832 xpt_for_all_devices(xptpassannouncefunc, NULL);
6833
6834 /* Release our hook so that the boot can continue. */
6835 config_intrhook_disestablish(xpt_config_hook);
6836 free(xpt_config_hook, M_TEMP);
6837 xpt_config_hook = NULL;
6838 }
6839 if (done_ccb != NULL)
6840 xpt_free_ccb(done_ccb);
6841}
6842
6843static void
6844xptaction(struct cam_sim *sim, union ccb *work_ccb)
6845{
6846 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6847
6848 switch (work_ccb->ccb_h.func_code) {
6849 /* Common cases first */
6850 case XPT_PATH_INQ: /* Path routing inquiry */
6851 {
6852 struct ccb_pathinq *cpi;
6853
6854 cpi = &work_ccb->cpi;
6855 cpi->version_num = 1; /* XXX??? */
6856 cpi->hba_inquiry = 0;
6857 cpi->target_sprt = 0;
6858 cpi->hba_misc = 0;
6859 cpi->hba_eng_cnt = 0;
6860 cpi->max_target = 0;
6861 cpi->max_lun = 0;
6862 cpi->initiator_id = 0;
6863 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
6864 strncpy(cpi->hba_vid, "", HBA_IDLEN);
6865 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
6866 cpi->unit_number = sim->unit_number;
6867 cpi->bus_id = sim->bus_id;
6868 cpi->base_transfer_speed = 0;
6869#ifdef CAM_NEW_TRAN_CODE
6870 cpi->protocol = PROTO_UNSPECIFIED;
6871 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
6872 cpi->transport = XPORT_UNSPECIFIED;
6873 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
6874#endif /* CAM_NEW_TRAN_CODE */
6875 cpi->ccb_h.status = CAM_REQ_CMP;
6876 xpt_done(work_ccb);
6877 break;
6878 }
6879 default:
6880 work_ccb->ccb_h.status = CAM_REQ_INVALID;
6881 xpt_done(work_ccb);
6882 break;
6883 }
6884}
6885
6886/*
6887 * The xpt as a "controller" has no interrupt sources, so polling
6888 * is a no-op.
6889 */
6890static void
6891xptpoll(struct cam_sim *sim)
6892{
6893}
6894
6895static void
6896camisr(void *V_queue)
6897{
6898 cam_isrq_t *queue = V_queue;
6899 int s;
6900 struct ccb_hdr *ccb_h;
6901
6902 s = splcam();
6903 while ((ccb_h = TAILQ_FIRST(queue)) != NULL) {
6904 int runq;
6905
6906 TAILQ_REMOVE(queue, ccb_h, sim_links.tqe);
6907 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
6908 splx(s);
6909
6910 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
6911 ("camisr\n"));
6912
6913 runq = FALSE;
6914
6915 if (ccb_h->flags & CAM_HIGH_POWER) {
6916 struct highpowerlist *hphead;
6917 struct cam_ed *device;
6918 union ccb *send_ccb;
6919
6920 hphead = &highpowerq;
6921
6922 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
6923
6924 /*
6925 * Increment the count since this command is done.
6926 */
6927 num_highpower++;
6928
6929 /*
6930 * Any high powered commands queued up?
6931 */
6932 if (send_ccb != NULL) {
6933 device = send_ccb->ccb_h.path->device;
6934
6935 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
6936
6937 xpt_release_devq(send_ccb->ccb_h.path,
6938 /*count*/1, /*runqueue*/TRUE);
6939 }
6940 }
6941 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
6942 struct cam_ed *dev;
6943
6944 dev = ccb_h->path->device;
6945
6946 s = splcam();
6947 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
6948
6949 ccb_h->path->bus->sim->devq->send_active--;
6950 ccb_h->path->bus->sim->devq->send_openings++;
6951 splx(s);
6952
6953 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
6954 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
6955 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
6956 && (dev->ccbq.dev_active == 0))) {
6957
6958 xpt_release_devq(ccb_h->path, /*count*/1,
6959 /*run_queue*/TRUE);
6960 }
6961
6962 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6963 && (--dev->tag_delay_count == 0))
6964 xpt_start_tags(ccb_h->path);
6965
6966 if ((dev->ccbq.queue.entries > 0)
6967 && (dev->qfrozen_cnt == 0)
6968 && (device_is_send_queued(dev) == 0)) {
6969 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
6970 dev);
6971 }
6972 }
6973
6974 if (ccb_h->status & CAM_RELEASE_SIMQ) {
6975 xpt_release_simq(ccb_h->path->bus->sim,
6976 /*run_queue*/TRUE);
6977 ccb_h->status &= ~CAM_RELEASE_SIMQ;
6978 runq = FALSE;
6979 }
6980
6981 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
6982 && (ccb_h->status & CAM_DEV_QFRZN)) {
6983 xpt_release_devq(ccb_h->path, /*count*/1,
6984 /*run_queue*/TRUE);
6985 ccb_h->status &= ~CAM_DEV_QFRZN;
6986 } else if (runq) {
6987 xpt_run_dev_sendq(ccb_h->path->bus);
6988 }
6989
6990 /* Call the peripheral driver's callback */
6991 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
6992
6993 /* Raise IPL for while test */
6994 s = splcam();
6995 }
6996 splx(s);
6997}