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
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sys/cam/cam_xpt.c 147723 2005-07-01 15:21:30Z avatar $");
32
33#include <sys/param.h>
34#include <sys/bus.h>
35#include <sys/systm.h>
36#include <sys/types.h>
37#include <sys/malloc.h>
38#include <sys/kernel.h>
39#include <sys/time.h>
40#include <sys/conf.h>
41#include <sys/fcntl.h>
42#include <sys/md5.h>
43#include <sys/interrupt.h>
44#include <sys/sbuf.h>
45
46#include <sys/lock.h>
47#include <sys/mutex.h>
48
49#ifdef PC98
50#include <pc98/pc98/pc98_machdep.h> /* geometry translation */
51#endif
52
53#include <cam/cam.h>
54#include <cam/cam_ccb.h>
55#include <cam/cam_periph.h>
56#include <cam/cam_sim.h>
57#include <cam/cam_xpt.h>
58#include <cam/cam_xpt_sim.h>
59#include <cam/cam_xpt_periph.h>
60#include <cam/cam_debug.h>
61
62#include <cam/scsi/scsi_all.h>
63#include <cam/scsi/scsi_message.h>
64#include <cam/scsi/scsi_pass.h>
65#include "opt_cam.h"
66
67/* Datastructures internal to the xpt layer */
68MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
69
70/*
71 * Definition of an async handler callback block. These are used to add
72 * SIMs and peripherals to the async callback lists.
73 */
74struct async_node {
75 SLIST_ENTRY(async_node) links;
76 u_int32_t event_enable; /* Async Event enables */
77 void (*callback)(void *arg, u_int32_t code,
78 struct cam_path *path, void *args);
79 void *callback_arg;
80};
81
82SLIST_HEAD(async_list, async_node);
83SLIST_HEAD(periph_list, cam_periph);
84static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
85
86/*
87 * This is the maximum number of high powered commands (e.g. start unit)
88 * that can be outstanding at a particular time.
89 */
90#ifndef CAM_MAX_HIGHPOWER
91#define CAM_MAX_HIGHPOWER 4
92#endif
93
94/* number of high powered commands that can go through right now */
95static int num_highpower = CAM_MAX_HIGHPOWER;
96
97/*
98 * Structure for queueing a device in a run queue.
99 * There is one run queue for allocating new ccbs,
100 * and another for sending ccbs to the controller.
101 */
102struct cam_ed_qinfo {
103 cam_pinfo pinfo;
104 struct cam_ed *device;
105};
106
107/*
108 * The CAM EDT (Existing Device Table) contains the device information for
109 * all devices for all busses in the system. The table contains a
110 * cam_ed structure for each device on the bus.
111 */
112struct cam_ed {
113 TAILQ_ENTRY(cam_ed) links;
114 struct cam_ed_qinfo alloc_ccb_entry;
115 struct cam_ed_qinfo send_ccb_entry;
116 struct cam_et *target;
117 lun_id_t lun_id;
118 struct camq drvq; /*
119 * Queue of type drivers wanting to do
120 * work on this device.
121 */
122 struct cam_ccbq ccbq; /* Queue of pending ccbs */
123 struct async_list asyncs; /* Async callback info for this B/T/L */
124 struct periph_list periphs; /* All attached devices */
125 u_int generation; /* Generation number */
126 struct cam_periph *owner; /* Peripheral driver's ownership tag */
127 struct xpt_quirk_entry *quirk; /* Oddities about this device */
128 /* Storage for the inquiry data */
129#ifdef CAM_NEW_TRAN_CODE
130 cam_proto protocol;
131 u_int protocol_version;
132 cam_xport transport;
133 u_int transport_version;
134#endif /* CAM_NEW_TRAN_CODE */
135 struct scsi_inquiry_data inq_data;
136 u_int8_t inq_flags; /*
137 * Current settings for inquiry flags.
138 * This allows us to override settings
139 * like disconnection and tagged
140 * queuing for a device.
141 */
142 u_int8_t queue_flags; /* Queue flags from the control page */
143 u_int8_t serial_num_len;
144 u_int8_t *serial_num;
145 u_int32_t qfrozen_cnt;
146 u_int32_t flags;
147#define CAM_DEV_UNCONFIGURED 0x01
148#define CAM_DEV_REL_TIMEOUT_PENDING 0x02
149#define CAM_DEV_REL_ON_COMPLETE 0x04
150#define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
151#define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
152#define CAM_DEV_TAG_AFTER_COUNT 0x20
153#define CAM_DEV_INQUIRY_DATA_VALID 0x40
154 u_int32_t tag_delay_count;
155#define CAM_TAG_DELAY_COUNT 5
156 u_int32_t tag_saved_openings;
157 u_int32_t refcount;
158 struct callout_handle c_handle;
159};
160
161/*
162 * Each target is represented by an ET (Existing Target). These
163 * entries are created when a target is successfully probed with an
164 * identify, and removed when a device fails to respond after a number
165 * of retries, or a bus rescan finds the device missing.
166 */
167struct cam_et {
168 TAILQ_HEAD(, cam_ed) ed_entries;
169 TAILQ_ENTRY(cam_et) links;
170 struct cam_eb *bus;
171 target_id_t target_id;
172 u_int32_t refcount;
173 u_int generation;
174 struct timeval last_reset;
175};
176
177/*
178 * Each bus is represented by an EB (Existing Bus). These entries
179 * are created by calls to xpt_bus_register and deleted by calls to
180 * xpt_bus_deregister.
181 */
182struct cam_eb {
183 TAILQ_HEAD(, cam_et) et_entries;
184 TAILQ_ENTRY(cam_eb) links;
185 path_id_t path_id;
186 struct cam_sim *sim;
187 struct timeval last_reset;
188 u_int32_t flags;
189#define CAM_EB_RUNQ_SCHEDULED 0x01
190 u_int32_t refcount;
191 u_int generation;
192};
193
194struct cam_path {
195 struct cam_periph *periph;
196 struct cam_eb *bus;
197 struct cam_et *target;
198 struct cam_ed *device;
199};
200
201struct xpt_quirk_entry {
202 struct scsi_inquiry_pattern inq_pat;
203 u_int8_t quirks;
204#define CAM_QUIRK_NOLUNS 0x01
205#define CAM_QUIRK_NOSERIAL 0x02
206#define CAM_QUIRK_HILUNS 0x04
207#define CAM_QUIRK_NOHILUNS 0x08
208 u_int mintags;
209 u_int maxtags;
210};
211#define CAM_SCSI2_MAXLUN 8
212/*
213 * If we're not quirked to search <= the first 8 luns
214 * and we are either quirked to search above lun 8,
215 * or we're > SCSI-2, we can look for luns above lun 8.
216 */
217#define CAN_SRCH_HI(dv) \
218 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \
219 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \
220 || SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2))
221
222typedef enum {
223 XPT_FLAG_OPEN = 0x01
224} xpt_flags;
225
226struct xpt_softc {
227 xpt_flags flags;
228 u_int32_t generation;
229};
230
231static const char quantum[] = "QUANTUM";
232static const char sony[] = "SONY";
233static const char west_digital[] = "WDIGTL";
234static const char samsung[] = "SAMSUNG";
235static const char seagate[] = "SEAGATE";
236static const char microp[] = "MICROP";
237
238static struct xpt_quirk_entry xpt_quirk_table[] =
239{
240 {
241 /* Reports QUEUE FULL for temporary resource shortages */
242 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
243 /*quirks*/0, /*mintags*/24, /*maxtags*/32
244 },
245 {
246 /* Reports QUEUE FULL for temporary resource shortages */
247 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
248 /*quirks*/0, /*mintags*/24, /*maxtags*/32
249 },
250 {
251 /* Reports QUEUE FULL for temporary resource shortages */
252 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
253 /*quirks*/0, /*mintags*/24, /*maxtags*/32
254 },
255 {
256 /* Broken tagged queuing drive */
257 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
258 /*quirks*/0, /*mintags*/0, /*maxtags*/0
259 },
260 {
261 /* Broken tagged queuing drive */
262 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
263 /*quirks*/0, /*mintags*/0, /*maxtags*/0
264 },
265 {
266 /* Broken tagged queuing drive */
267 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
268 /*quirks*/0, /*mintags*/0, /*maxtags*/0
269 },
270 {
271 /*
272 * Unfortunately, the Quantum Atlas III has the same
273 * problem as the Atlas II drives above.
274 * Reported by: "Johan Granlund" <johan@granlund.nu>
275 *
276 * For future reference, the drive with the problem was:
277 * QUANTUM QM39100TD-SW N1B0
278 *
279 * It's possible that Quantum will fix the problem in later
280 * firmware revisions. If that happens, the quirk entry
281 * will need to be made specific to the firmware revisions
282 * with the problem.
283 *
284 */
285 /* Reports QUEUE FULL for temporary resource shortages */
286 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
287 /*quirks*/0, /*mintags*/24, /*maxtags*/32
288 },
289 {
290 /*
291 * 18 Gig Atlas III, same problem as the 9G version.
292 * Reported by: Andre Albsmeier
293 * <andre.albsmeier@mchp.siemens.de>
294 *
295 * For future reference, the drive with the problem was:
296 * QUANTUM QM318000TD-S N491
297 */
298 /* Reports QUEUE FULL for temporary resource shortages */
299 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
300 /*quirks*/0, /*mintags*/24, /*maxtags*/32
301 },
302 {
303 /*
304 * Broken tagged queuing drive
305 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
306 * and: Martin Renters <martin@tdc.on.ca>
307 */
308 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
309 /*quirks*/0, /*mintags*/0, /*maxtags*/0
310 },
311 /*
312 * The Seagate Medalist Pro drives have very poor write
313 * performance with anything more than 2 tags.
314 *
315 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
316 * Drive: <SEAGATE ST36530N 1444>
317 *
318 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
319 * Drive: <SEAGATE ST34520W 1281>
320 *
321 * No one has actually reported that the 9G version
322 * (ST39140*) of the Medalist Pro has the same problem, but
323 * we're assuming that it does because the 4G and 6.5G
324 * versions of the drive are broken.
325 */
326 {
327 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
328 /*quirks*/0, /*mintags*/2, /*maxtags*/2
329 },
330 {
331 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
332 /*quirks*/0, /*mintags*/2, /*maxtags*/2
333 },
334 {
335 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
336 /*quirks*/0, /*mintags*/2, /*maxtags*/2
337 },
338 {
339 /*
340 * Slow when tagged queueing is enabled. Write performance
341 * steadily drops off with more and more concurrent
342 * transactions. Best sequential write performance with
343 * tagged queueing turned off and write caching turned on.
344 *
345 * PR: kern/10398
346 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
347 * Drive: DCAS-34330 w/ "S65A" firmware.
348 *
349 * The drive with the problem had the "S65A" firmware
350 * revision, and has also been reported (by Stephen J.
351 * Roznowski <sjr@home.net>) for a drive with the "S61A"
352 * firmware revision.
353 *
354 * Although no one has reported problems with the 2 gig
355 * version of the DCAS drive, the assumption is that it
356 * has the same problems as the 4 gig version. Therefore
357 * this quirk entries disables tagged queueing for all
358 * DCAS drives.
359 */
360 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
361 /*quirks*/0, /*mintags*/0, /*maxtags*/0
362 },
363 {
364 /* Broken tagged queuing drive */
365 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
366 /*quirks*/0, /*mintags*/0, /*maxtags*/0
367 },
368 {
369 /* Broken tagged queuing drive */
370 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
371 /*quirks*/0, /*mintags*/0, /*maxtags*/0
372 },
373 {
374 /*
375 * Broken tagged queuing drive.
376 * Submitted by:
377 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
378 * in PR kern/9535
379 */
380 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
381 /*quirks*/0, /*mintags*/0, /*maxtags*/0
382 },
383 {
384 /*
385 * Slow when tagged queueing is enabled. (1.5MB/sec versus
386 * 8MB/sec.)
387 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
388 * Best performance with these drives is achieved with
389 * tagged queueing turned off, and write caching turned on.
390 */
391 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
392 /*quirks*/0, /*mintags*/0, /*maxtags*/0
393 },
394 {
395 /*
396 * Slow when tagged queueing is enabled. (1.5MB/sec versus
397 * 8MB/sec.)
398 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
399 * Best performance with these drives is achieved with
400 * tagged queueing turned off, and write caching turned on.
401 */
402 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
403 /*quirks*/0, /*mintags*/0, /*maxtags*/0
404 },
405 {
406 /*
407 * Doesn't handle queue full condition correctly,
408 * so we need to limit maxtags to what the device
409 * can handle instead of determining this automatically.
410 */
411 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
412 /*quirks*/0, /*mintags*/2, /*maxtags*/32
413 },
414 {
415 /* Really only one LUN */
416 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
417 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
418 },
419 {
420 /* I can't believe we need a quirk for DPT volumes. */
421 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
422 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
423 /*mintags*/0, /*maxtags*/255
424 },
425 {
426 /*
427 * Many Sony CDROM drives don't like multi-LUN probing.
428 */
429 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
430 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
431 },
432 {
433 /*
434 * This drive doesn't like multiple LUN probing.
435 * Submitted by: Parag Patel <parag@cgt.com>
436 */
437 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
438 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
439 },
440 {
441 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
442 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
443 },
444 {
445 /*
446 * The 8200 doesn't like multi-lun probing, and probably
447 * don't like serial number requests either.
448 */
449 {
450 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
451 "EXB-8200*", "*"
452 },
453 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
454 },
455 {
456 /*
457 * Let's try the same as above, but for a drive that says
458 * it's an IPL-6860 but is actually an EXB 8200.
459 */
460 {
461 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
462 "IPL-6860*", "*"
463 },
464 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
465 },
466 {
467 /*
468 * These Hitachi drives don't like multi-lun probing.
469 * The PR submitter has a DK319H, but says that the Linux
470 * kernel has a similar work-around for the DK312 and DK314,
471 * so all DK31* drives are quirked here.
472 * PR: misc/18793
473 * Submitted by: Paul Haddad <paul@pth.com>
474 */
475 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
476 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
477 },
478 {
479 /*
480 * The Hitachi CJ series with J8A8 firmware apparantly has
481 * problems with tagged commands.
482 * PR: 23536
483 * Reported by: amagai@nue.org
484 */
485 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
486 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
487 },
488 {
489 /*
490 * These are the large storage arrays.
491 * Submitted by: William Carrel <william.carrel@infospace.com>
492 */
493 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
494 CAM_QUIRK_HILUNS, 2, 1024
495 },
496 {
497 /*
498 * This old revision of the TDC3600 is also SCSI-1, and
499 * hangs upon serial number probing.
500 */
501 {
502 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
503 " TDC 3600", "U07:"
504 },
505 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
506 },
507 {
508 /*
509 * Maxtor Personal Storage 3000XT (Firewire)
510 * hangs upon serial number probing.
511 */
512 {
513 T_DIRECT, SIP_MEDIA_FIXED, "Maxtor",
514 "1394 storage", "*"
515 },
516 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
517 },
518 {
519 /*
520 * Would repond to all LUNs if asked for.
521 */
522 {
523 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
524 "CP150", "*"
525 },
526 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
527 },
528 {
529 /*
530 * Would repond to all LUNs if asked for.
531 */
532 {
533 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
534 "96X2*", "*"
535 },
536 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
537 },
538 {
539 /* Submitted by: Matthew Dodd <winter@jurai.net> */
540 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
541 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
542 },
543 {
544 /* Submitted by: Matthew Dodd <winter@jurai.net> */
545 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
546 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
547 },
548 {
549 /* TeraSolutions special settings for TRC-22 RAID */
550 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
551 /*quirks*/0, /*mintags*/55, /*maxtags*/255
552 },
553 {
554 /* Veritas Storage Appliance */
555 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
556 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
557 },
558 {
559 /*
560 * Would respond to all LUNs. Device type and removable
561 * flag are jumper-selectable.
562 */
563 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
564 "Tahiti 1", "*"
565 },
566 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
567 },
568 {
569 /* EasyRAID E5A aka. areca ARC-6010 */
570 { T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
571 CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
572 },
573 {
574 /* Default tagged queuing parameters for all devices */
575 {
576 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
577 /*vendor*/"*", /*product*/"*", /*revision*/"*"
578 },
579 /*quirks*/0, /*mintags*/2, /*maxtags*/255
580 },
581};
582
583static const int xpt_quirk_table_size =
584 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
585
586typedef enum {
587 DM_RET_COPY = 0x01,
588 DM_RET_FLAG_MASK = 0x0f,
589 DM_RET_NONE = 0x00,
590 DM_RET_STOP = 0x10,
591 DM_RET_DESCEND = 0x20,
592 DM_RET_ERROR = 0x30,
593 DM_RET_ACTION_MASK = 0xf0
594} dev_match_ret;
595
596typedef enum {
597 XPT_DEPTH_BUS,
598 XPT_DEPTH_TARGET,
599 XPT_DEPTH_DEVICE,
600 XPT_DEPTH_PERIPH
601} xpt_traverse_depth;
602
603struct xpt_traverse_config {
604 xpt_traverse_depth depth;
605 void *tr_func;
606 void *tr_arg;
607};
608
609typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
610typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
611typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
612typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
613typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
614
615/* Transport layer configuration information */
616static struct xpt_softc xsoftc;
617
618/* Queues for our software interrupt handler */
619typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
620static cam_isrq_t cam_bioq;
621static struct mtx cam_bioq_lock;
622
623/* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
624static SLIST_HEAD(,ccb_hdr) ccb_freeq;
625static u_int xpt_max_ccbs; /*
626 * Maximum size of ccb pool. Modified as
627 * devices are added/removed or have their
628 * opening counts changed.
629 */
630static u_int xpt_ccb_count; /* Current count of allocated ccbs */
631
632struct cam_periph *xpt_periph;
633
634static periph_init_t xpt_periph_init;
635
636static periph_init_t probe_periph_init;
637
638static struct periph_driver xpt_driver =
639{
640 xpt_periph_init, "xpt",
641 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
642};
643
644static struct periph_driver probe_driver =
645{
646 probe_periph_init, "probe",
647 TAILQ_HEAD_INITIALIZER(probe_driver.units)
648};
649
650PERIPHDRIVER_DECLARE(xpt, xpt_driver);
651PERIPHDRIVER_DECLARE(probe, probe_driver);
652
653
654static d_open_t xptopen;
655static d_close_t xptclose;
656static d_ioctl_t xptioctl;
657
658static struct cdevsw xpt_cdevsw = {
659 .d_version = D_VERSION,
660 .d_flags = D_NEEDGIANT,
661 .d_open = xptopen,
662 .d_close = xptclose,
663 .d_ioctl = xptioctl,
664 .d_name = "xpt",
665};
666
667static struct intr_config_hook *xpt_config_hook;
668
669/* Registered busses */
670static TAILQ_HEAD(,cam_eb) xpt_busses;
671static u_int bus_generation;
672
673/* Storage for debugging datastructures */
674#ifdef CAMDEBUG
675struct cam_path *cam_dpath;
676u_int32_t cam_dflags;
677u_int32_t cam_debug_delay;
678#endif
679
680/* Pointers to software interrupt handlers */
681static void *cambio_ih;
682
683#if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
684#error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
685#endif
686
687/*
688 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
689 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
690 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
691 */
692#if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
693 || defined(CAM_DEBUG_LUN)
694#ifdef CAMDEBUG
695#if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
696 || !defined(CAM_DEBUG_LUN)
697#error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
698 and CAM_DEBUG_LUN"
699#endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
700#else /* !CAMDEBUG */
701#error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
702#endif /* CAMDEBUG */
703#endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
704
705/* Our boot-time initialization hook */
706static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
707
708static moduledata_t cam_moduledata = {
709 "cam",
710 cam_module_event_handler,
711 NULL
712};
713
714static void xpt_init(void *);
715
716DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
717MODULE_VERSION(cam, 1);
718
719
720static cam_status xpt_compile_path(struct cam_path *new_path,
721 struct cam_periph *perph,
722 path_id_t path_id,
723 target_id_t target_id,
724 lun_id_t lun_id);
725
726static void xpt_release_path(struct cam_path *path);
727
728static void xpt_async_bcast(struct async_list *async_head,
729 u_int32_t async_code,
730 struct cam_path *path,
731 void *async_arg);
732static void xpt_dev_async(u_int32_t async_code,
733 struct cam_eb *bus,
734 struct cam_et *target,
735 struct cam_ed *device,
736 void *async_arg);
737static path_id_t xptnextfreepathid(void);
738static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
739static union ccb *xpt_get_ccb(struct cam_ed *device);
740static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
741 u_int32_t new_priority);
742static void xpt_run_dev_allocq(struct cam_eb *bus);
743static void xpt_run_dev_sendq(struct cam_eb *bus);
744static timeout_t xpt_release_devq_timeout;
745static timeout_t xpt_release_simq_timeout;
746static void xpt_release_bus(struct cam_eb *bus);
747static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
748 int run_queue);
749static struct cam_et*
750 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
751static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
752static struct cam_ed*
753 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
754 lun_id_t lun_id);
755static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
756 struct cam_ed *device);
757static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
758static struct cam_eb*
759 xpt_find_bus(path_id_t path_id);
760static struct cam_et*
761 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
762static struct cam_ed*
763 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
764static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
765static void xpt_scan_lun(struct cam_periph *periph,
766 struct cam_path *path, cam_flags flags,
767 union ccb *ccb);
768static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
769static xpt_busfunc_t xptconfigbuscountfunc;
770static xpt_busfunc_t xptconfigfunc;
771static void xpt_config(void *arg);
772static xpt_devicefunc_t xptpassannouncefunc;
773static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
774static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
775static void xptpoll(struct cam_sim *sim);
776static void camisr(void *);
777#if 0
778static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
779static void xptasync(struct cam_periph *periph,
780 u_int32_t code, cam_path *path);
781#endif
782static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
783 u_int num_patterns, struct cam_eb *bus);
784static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
785 u_int num_patterns,
786 struct cam_ed *device);
787static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
788 u_int num_patterns,
789 struct cam_periph *periph);
790static xpt_busfunc_t xptedtbusfunc;
791static xpt_targetfunc_t xptedttargetfunc;
792static xpt_devicefunc_t xptedtdevicefunc;
793static xpt_periphfunc_t xptedtperiphfunc;
794static xpt_pdrvfunc_t xptplistpdrvfunc;
795static xpt_periphfunc_t xptplistperiphfunc;
796static int xptedtmatch(struct ccb_dev_match *cdm);
797static int xptperiphlistmatch(struct ccb_dev_match *cdm);
798static int xptbustraverse(struct cam_eb *start_bus,
799 xpt_busfunc_t *tr_func, void *arg);
800static int xpttargettraverse(struct cam_eb *bus,
801 struct cam_et *start_target,
802 xpt_targetfunc_t *tr_func, void *arg);
803static int xptdevicetraverse(struct cam_et *target,
804 struct cam_ed *start_device,
805 xpt_devicefunc_t *tr_func, void *arg);
806static int xptperiphtraverse(struct cam_ed *device,
807 struct cam_periph *start_periph,
808 xpt_periphfunc_t *tr_func, void *arg);
809static int xptpdrvtraverse(struct periph_driver **start_pdrv,
810 xpt_pdrvfunc_t *tr_func, void *arg);
811static int xptpdperiphtraverse(struct periph_driver **pdrv,
812 struct cam_periph *start_periph,
813 xpt_periphfunc_t *tr_func,
814 void *arg);
815static xpt_busfunc_t xptdefbusfunc;
816static xpt_targetfunc_t xptdeftargetfunc;
817static xpt_devicefunc_t xptdefdevicefunc;
818static xpt_periphfunc_t xptdefperiphfunc;
819static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
820#ifdef notusedyet
821static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
822 void *arg);
823#endif
824static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
825 void *arg);
826#ifdef notusedyet
827static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
828 void *arg);
829#endif
830static xpt_devicefunc_t xptsetasyncfunc;
831static xpt_busfunc_t xptsetasyncbusfunc;
832static cam_status xptregister(struct cam_periph *periph,
833 void *arg);
834static cam_status proberegister(struct cam_periph *periph,
835 void *arg);
836static void probeschedule(struct cam_periph *probe_periph);
837static void probestart(struct cam_periph *periph, union ccb *start_ccb);
838static void proberequestdefaultnegotiation(struct cam_periph *periph);
839static void probedone(struct cam_periph *periph, union ccb *done_ccb);
840static void probecleanup(struct cam_periph *periph);
841static void xpt_find_quirk(struct cam_ed *device);
842#ifdef CAM_NEW_TRAN_CODE
843static void xpt_devise_transport(struct cam_path *path);
844#endif /* CAM_NEW_TRAN_CODE */
845static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
846 struct cam_ed *device,
847 int async_update);
848static void xpt_toggle_tags(struct cam_path *path);
849static void xpt_start_tags(struct cam_path *path);
850static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
851 struct cam_ed *dev);
852static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
853 struct cam_ed *dev);
854static __inline int periph_is_queued(struct cam_periph *periph);
855static __inline int device_is_alloc_queued(struct cam_ed *device);
856static __inline int device_is_send_queued(struct cam_ed *device);
857static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
858
859static __inline int
860xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
861{
862 int retval;
863
864 if (dev->ccbq.devq_openings > 0) {
865 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
866 cam_ccbq_resize(&dev->ccbq,
867 dev->ccbq.dev_openings
868 + dev->ccbq.dev_active);
869 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
870 }
871 /*
872 * The priority of a device waiting for CCB resources
873 * is that of the the highest priority peripheral driver
874 * enqueued.
875 */
876 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
877 &dev->alloc_ccb_entry.pinfo,
878 CAMQ_GET_HEAD(&dev->drvq)->priority);
879 } else {
880 retval = 0;
881 }
882
883 return (retval);
884}
885
886static __inline int
887xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
888{
889 int retval;
890
891 if (dev->ccbq.dev_openings > 0) {
892 /*
893 * The priority of a device waiting for controller
894 * resources is that of the the highest priority CCB
895 * enqueued.
896 */
897 retval =
898 xpt_schedule_dev(&bus->sim->devq->send_queue,
899 &dev->send_ccb_entry.pinfo,
900 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
901 } else {
902 retval = 0;
903 }
904 return (retval);
905}
906
907static __inline int
908periph_is_queued(struct cam_periph *periph)
909{
910 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
911}
912
913static __inline int
914device_is_alloc_queued(struct cam_ed *device)
915{
916 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
917}
918
919static __inline int
920device_is_send_queued(struct cam_ed *device)
921{
922 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
923}
924
925static __inline int
926dev_allocq_is_runnable(struct cam_devq *devq)
927{
928 /*
929 * Have work to do.
930 * Have space to do more work.
931 * Allowed to do work.
932 */
933 return ((devq->alloc_queue.qfrozen_cnt == 0)
934 && (devq->alloc_queue.entries > 0)
935 && (devq->alloc_openings > 0));
936}
937
938static void
939xpt_periph_init()
940{
941 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
942}
943
944static void
945probe_periph_init()
946{
947}
948
949
950static void
951xptdone(struct cam_periph *periph, union ccb *done_ccb)
952{
953 /* Caller will release the CCB */
954 wakeup(&done_ccb->ccb_h.cbfcnp);
955}
956
957static int
958xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
959{
960 int unit;
961
962 unit = minor(dev) & 0xff;
963
964 /*
965 * Only allow read-write access.
966 */
967 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
968 return(EPERM);
969
970 /*
971 * We don't allow nonblocking access.
972 */
973 if ((flags & O_NONBLOCK) != 0) {
974 printf("xpt%d: can't do nonblocking access\n", unit);
975 return(ENODEV);
976 }
977
978 /*
979 * We only have one transport layer right now. If someone accesses
980 * us via something other than minor number 1, point out their
981 * mistake.
982 */
983 if (unit != 0) {
984 printf("xptopen: got invalid xpt unit %d\n", unit);
985 return(ENXIO);
986 }
987
988 /* Mark ourselves open */
989 xsoftc.flags |= XPT_FLAG_OPEN;
990
991 return(0);
992}
993
994static int
995xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
996{
997 int unit;
998
999 unit = minor(dev) & 0xff;
1000
1001 /*
1002 * We only have one transport layer right now. If someone accesses
1003 * us via something other than minor number 1, point out their
1004 * mistake.
1005 */
1006 if (unit != 0) {
1007 printf("xptclose: got invalid xpt unit %d\n", unit);
1008 return(ENXIO);
1009 }
1010
1011 /* Mark ourselves closed */
1012 xsoftc.flags &= ~XPT_FLAG_OPEN;
1013
1014 return(0);
1015}
1016
1017static int
1018xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1019{
1020 int unit, error;
1021
1022 error = 0;
1023 unit = minor(dev) & 0xff;
1024
1025 /*
1026 * We only have one transport layer right now. If someone accesses
1027 * us via something other than minor number 1, point out their
1028 * mistake.
1029 */
1030 if (unit != 0) {
1031 printf("xptioctl: got invalid xpt unit %d\n", unit);
1032 return(ENXIO);
1033 }
1034
1035 switch(cmd) {
1036 /*
1037 * For the transport layer CAMIOCOMMAND ioctl, we really only want
1038 * to accept CCB types that don't quite make sense to send through a
1039 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
1040 * in the CAM spec.
1041 */
1042 case CAMIOCOMMAND: {
1043 union ccb *ccb;
1044 union ccb *inccb;
1045
1046 inccb = (union ccb *)addr;
1047
1048 switch(inccb->ccb_h.func_code) {
1049 case XPT_SCAN_BUS:
1050 case XPT_RESET_BUS:
1051 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
1052 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1053 error = EINVAL;
1054 break;
1055 }
1056 /* FALLTHROUGH */
1057 case XPT_PATH_INQ:
1058 case XPT_ENG_INQ:
1059 case XPT_SCAN_LUN:
1060
1061 ccb = xpt_alloc_ccb();
1062
1063 /*
1064 * Create a path using the bus, target, and lun the
1065 * user passed in.
1066 */
1067 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1068 inccb->ccb_h.path_id,
1069 inccb->ccb_h.target_id,
1070 inccb->ccb_h.target_lun) !=
1071 CAM_REQ_CMP){
1072 error = EINVAL;
1073 xpt_free_ccb(ccb);
1074 break;
1075 }
1076 /* Ensure all of our fields are correct */
1077 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1078 inccb->ccb_h.pinfo.priority);
1079 xpt_merge_ccb(ccb, inccb);
1080 ccb->ccb_h.cbfcnp = xptdone;
1081 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1082 bcopy(ccb, inccb, sizeof(union ccb));
1083 xpt_free_path(ccb->ccb_h.path);
1084 xpt_free_ccb(ccb);
1085 break;
1086
1087 case XPT_DEBUG: {
1088 union ccb ccb;
1089
1090 /*
1091 * This is an immediate CCB, so it's okay to
1092 * allocate it on the stack.
1093 */
1094
1095 /*
1096 * Create a path using the bus, target, and lun the
1097 * user passed in.
1098 */
1099 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1100 inccb->ccb_h.path_id,
1101 inccb->ccb_h.target_id,
1102 inccb->ccb_h.target_lun) !=
1103 CAM_REQ_CMP){
1104 error = EINVAL;
1105 break;
1106 }
1107 /* Ensure all of our fields are correct */
1108 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1109 inccb->ccb_h.pinfo.priority);
1110 xpt_merge_ccb(&ccb, inccb);
1111 ccb.ccb_h.cbfcnp = xptdone;
1112 xpt_action(&ccb);
1113 bcopy(&ccb, inccb, sizeof(union ccb));
1114 xpt_free_path(ccb.ccb_h.path);
1115 break;
1116
1117 }
1118 case XPT_DEV_MATCH: {
1119 struct cam_periph_map_info mapinfo;
1120 struct cam_path *old_path;
1121
1122 /*
1123 * We can't deal with physical addresses for this
1124 * type of transaction.
1125 */
1126 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1127 error = EINVAL;
1128 break;
1129 }
1130
1131 /*
1132 * Save this in case the caller had it set to
1133 * something in particular.
1134 */
1135 old_path = inccb->ccb_h.path;
1136
1137 /*
1138 * We really don't need a path for the matching
1139 * code. The path is needed because of the
1140 * debugging statements in xpt_action(). They
1141 * assume that the CCB has a valid path.
1142 */
1143 inccb->ccb_h.path = xpt_periph->path;
1144
1145 bzero(&mapinfo, sizeof(mapinfo));
1146
1147 /*
1148 * Map the pattern and match buffers into kernel
1149 * virtual address space.
1150 */
1151 error = cam_periph_mapmem(inccb, &mapinfo);
1152
1153 if (error) {
1154 inccb->ccb_h.path = old_path;
1155 break;
1156 }
1157
1158 /*
1159 * This is an immediate CCB, we can send it on directly.
1160 */
1161 xpt_action(inccb);
1162
1163 /*
1164 * Map the buffers back into user space.
1165 */
1166 cam_periph_unmapmem(inccb, &mapinfo);
1167
1168 inccb->ccb_h.path = old_path;
1169
1170 error = 0;
1171 break;
1172 }
1173 default:
1174 error = ENOTSUP;
1175 break;
1176 }
1177 break;
1178 }
1179 /*
1180 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1181 * with the periphal driver name and unit name filled in. The other
1182 * fields don't really matter as input. The passthrough driver name
1183 * ("pass"), and unit number are passed back in the ccb. The current
1184 * device generation number, and the index into the device peripheral
1185 * driver list, and the status are also passed back. Note that
1186 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1187 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1188 * (or rather should be) impossible for the device peripheral driver
1189 * list to change since we look at the whole thing in one pass, and
1190 * we do it with splcam protection.
1191 *
1192 */
1193 case CAMGETPASSTHRU: {
1194 union ccb *ccb;
1195 struct cam_periph *periph;
1196 struct periph_driver **p_drv;
1197 char *name;
1198 u_int unit;
1199 u_int cur_generation;
1200 int base_periph_found;
1201 int splbreaknum;
1202 int s;
1203
1204 ccb = (union ccb *)addr;
1205 unit = ccb->cgdl.unit_number;
1206 name = ccb->cgdl.periph_name;
1207 /*
1208 * Every 100 devices, we want to drop our spl protection to
1209 * give the software interrupt handler a chance to run.
1210 * Most systems won't run into this check, but this should
1211 * avoid starvation in the software interrupt handler in
1212 * large systems.
1213 */
1214 splbreaknum = 100;
1215
1216 ccb = (union ccb *)addr;
1217
1218 base_periph_found = 0;
1219
1220 /*
1221 * Sanity check -- make sure we don't get a null peripheral
1222 * driver name.
1223 */
1224 if (*ccb->cgdl.periph_name == '\0') {
1225 error = EINVAL;
1226 break;
1227 }
1228
1229 /* Keep the list from changing while we traverse it */
1230 s = splcam();
1231ptstartover:
1232 cur_generation = xsoftc.generation;
1233
1234 /* first find our driver in the list of drivers */
1235 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
1236 if (strcmp((*p_drv)->driver_name, name) == 0)
1237 break;
1238
1239 if (*p_drv == NULL) {
1240 splx(s);
1241 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1242 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1243 *ccb->cgdl.periph_name = '\0';
1244 ccb->cgdl.unit_number = 0;
1245 error = ENOENT;
1246 break;
1247 }
1248
1249 /*
1250 * Run through every peripheral instance of this driver
1251 * and check to see whether it matches the unit passed
1252 * in by the user. If it does, get out of the loops and
1253 * find the passthrough driver associated with that
1254 * peripheral driver.
1255 */
1256 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1257 periph = TAILQ_NEXT(periph, unit_links)) {
1258
1259 if (periph->unit_number == unit) {
1260 break;
1261 } else if (--splbreaknum == 0) {
1262 splx(s);
1263 s = splcam();
1264 splbreaknum = 100;
1265 if (cur_generation != xsoftc.generation)
1266 goto ptstartover;
1267 }
1268 }
1269 /*
1270 * If we found the peripheral driver that the user passed
1271 * in, go through all of the peripheral drivers for that
1272 * particular device and look for a passthrough driver.
1273 */
1274 if (periph != NULL) {
1275 struct cam_ed *device;
1276 int i;
1277
1278 base_periph_found = 1;
1279 device = periph->path->device;
1280 for (i = 0, periph = SLIST_FIRST(&device->periphs);
1281 periph != NULL;
1282 periph = SLIST_NEXT(periph, periph_links), i++) {
1283 /*
1284 * Check to see whether we have a
1285 * passthrough device or not.
1286 */
1287 if (strcmp(periph->periph_name, "pass") == 0) {
1288 /*
1289 * Fill in the getdevlist fields.
1290 */
1291 strcpy(ccb->cgdl.periph_name,
1292 periph->periph_name);
1293 ccb->cgdl.unit_number =
1294 periph->unit_number;
1295 if (SLIST_NEXT(periph, periph_links))
1296 ccb->cgdl.status =
1297 CAM_GDEVLIST_MORE_DEVS;
1298 else
1299 ccb->cgdl.status =
1300 CAM_GDEVLIST_LAST_DEVICE;
1301 ccb->cgdl.generation =
1302 device->generation;
1303 ccb->cgdl.index = i;
1304 /*
1305 * Fill in some CCB header fields
1306 * that the user may want.
1307 */
1308 ccb->ccb_h.path_id =
1309 periph->path->bus->path_id;
1310 ccb->ccb_h.target_id =
1311 periph->path->target->target_id;
1312 ccb->ccb_h.target_lun =
1313 periph->path->device->lun_id;
1314 ccb->ccb_h.status = CAM_REQ_CMP;
1315 break;
1316 }
1317 }
1318 }
1319
1320 /*
1321 * If the periph is null here, one of two things has
1322 * happened. The first possibility is that we couldn't
1323 * find the unit number of the particular peripheral driver
1324 * that the user is asking about. e.g. the user asks for
1325 * the passthrough driver for "da11". We find the list of
1326 * "da" peripherals all right, but there is no unit 11.
1327 * The other possibility is that we went through the list
1328 * of peripheral drivers attached to the device structure,
1329 * but didn't find one with the name "pass". Either way,
1330 * we return ENOENT, since we couldn't find something.
1331 */
1332 if (periph == NULL) {
1333 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1334 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1335 *ccb->cgdl.periph_name = '\0';
1336 ccb->cgdl.unit_number = 0;
1337 error = ENOENT;
1338 /*
1339 * It is unfortunate that this is even necessary,
1340 * but there are many, many clueless users out there.
1341 * If this is true, the user is looking for the
1342 * passthrough driver, but doesn't have one in his
1343 * kernel.
1344 */
1345 if (base_periph_found == 1) {
1346 printf("xptioctl: pass driver is not in the "
1347 "kernel\n");
1348 printf("xptioctl: put \"device pass0\" in "
1349 "your kernel config file\n");
1350 }
1351 }
1352 splx(s);
1353 break;
1354 }
1355 default:
1356 error = ENOTTY;
1357 break;
1358 }
1359
1360 return(error);
1361}
1362
1363static int
1364cam_module_event_handler(module_t mod, int what, void *arg)
1365{
1366 if (what == MOD_LOAD) {
1367 xpt_init(NULL);
1368 } else if (what == MOD_UNLOAD) {
1369 return EBUSY;
1370 } else {
1371 return EOPNOTSUPP;
1372 }
1373
1374 return 0;
1375}
1376
1377/* Functions accessed by the peripheral drivers */
1378static void
1379xpt_init(dummy)
1380 void *dummy;
1381{
1382 struct cam_sim *xpt_sim;
1383 struct cam_path *path;
1384 struct cam_devq *devq;
1385 cam_status status;
1386
1387 TAILQ_INIT(&xpt_busses);
1388 TAILQ_INIT(&cam_bioq);
1389 SLIST_INIT(&ccb_freeq);
1390 STAILQ_INIT(&highpowerq);
1391
1392 mtx_init(&cam_bioq_lock, "CAM BIOQ lock", NULL, MTX_DEF);
1393
1394 /*
1395 * The xpt layer is, itself, the equivelent of a SIM.
1396 * Allow 16 ccbs in the ccb pool for it. This should
1397 * give decent parallelism when we probe busses and
1398 * perform other XPT functions.
1399 */
1400 devq = cam_simq_alloc(16);
1401 xpt_sim = cam_sim_alloc(xptaction,
1402 xptpoll,
1403 "xpt",
1404 /*softc*/NULL,
1405 /*unit*/0,
1406 /*max_dev_transactions*/0,
1407 /*max_tagged_dev_transactions*/0,
1408 devq);
1409 xpt_max_ccbs = 16;
1410
1411 xpt_bus_register(xpt_sim, /*bus #*/0);
1412
1413 /*
1414 * Looking at the XPT from the SIM layer, the XPT is
1415 * the equivelent of a peripheral driver. Allocate
1416 * a peripheral driver entry for us.
1417 */
1418 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1419 CAM_TARGET_WILDCARD,
1420 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1421 printf("xpt_init: xpt_create_path failed with status %#x,"
1422 " failing attach\n", status);
1423 return;
1424 }
1425
1426 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1427 path, NULL, 0, NULL);
1428 xpt_free_path(path);
1429
1430 xpt_sim->softc = xpt_periph;
1431
1432 /*
1433 * Register a callback for when interrupts are enabled.
1434 */
1435 xpt_config_hook =
1436 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1437 M_TEMP, M_NOWAIT | M_ZERO);
1438 if (xpt_config_hook == NULL) {
1439 printf("xpt_init: Cannot malloc config hook "
1440 "- failing attach\n");
1441 return;
1442 }
1443
1444 xpt_config_hook->ich_func = xpt_config;
1445 if (config_intrhook_establish(xpt_config_hook) != 0) {
1446 free (xpt_config_hook, M_TEMP);
1447 printf("xpt_init: config_intrhook_establish failed "
1448 "- failing attach\n");
1449 }
1450
1451 /* Install our software interrupt handlers */
1452 swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, 0, &cambio_ih);
1453}
1454
1455static cam_status
1456xptregister(struct cam_periph *periph, void *arg)
1457{
1458 if (periph == NULL) {
1459 printf("xptregister: periph was NULL!!\n");
1460 return(CAM_REQ_CMP_ERR);
1461 }
1462
1463 periph->softc = NULL;
1464
1465 xpt_periph = periph;
1466
1467 return(CAM_REQ_CMP);
1468}
1469
1470int32_t
1471xpt_add_periph(struct cam_periph *periph)
1472{
1473 struct cam_ed *device;
1474 int32_t status;
1475 struct periph_list *periph_head;
1476
1477 GIANT_REQUIRED;
1478
1479 device = periph->path->device;
1480
1481 periph_head = &device->periphs;
1482
1483 status = CAM_REQ_CMP;
1484
1485 if (device != NULL) {
1486 int s;
1487
1488 /*
1489 * Make room for this peripheral
1490 * so it will fit in the queue
1491 * when it's scheduled to run
1492 */
1493 s = splsoftcam();
1494 status = camq_resize(&device->drvq,
1495 device->drvq.array_size + 1);
1496
1497 device->generation++;
1498
1499 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1500
1501 splx(s);
1502 }
1503
1504 xsoftc.generation++;
1505
1506 return (status);
1507}
1508
1509void
1510xpt_remove_periph(struct cam_periph *periph)
1511{
1512 struct cam_ed *device;
1513
1514 GIANT_REQUIRED;
1515
1516 device = periph->path->device;
1517
1518 if (device != NULL) {
1519 int s;
1520 struct periph_list *periph_head;
1521
1522 periph_head = &device->periphs;
1523
1524 /* Release the slot for this peripheral */
1525 s = splsoftcam();
1526 camq_resize(&device->drvq, device->drvq.array_size - 1);
1527
1528 device->generation++;
1529
1530 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1531
1532 splx(s);
1533 }
1534
1535 xsoftc.generation++;
1536
1537}
1538
1539#ifdef CAM_NEW_TRAN_CODE
1540
1541void
1542xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1543{
1544 struct ccb_pathinq cpi;
1545 struct ccb_trans_settings cts;
1546 struct cam_path *path;
1547 u_int speed;
1548 u_int freq;
1549 u_int mb;
1550 int s;
1551
1552 GIANT_REQUIRED;
1553
1554 path = periph->path;
1555 /*
1556 * To ensure that this is printed in one piece,
1557 * mask out CAM interrupts.
1558 */
1559 s = splsoftcam();
1560 printf("%s%d at %s%d bus %d target %d lun %d\n",
1561 periph->periph_name, periph->unit_number,
1562 path->bus->sim->sim_name,
1563 path->bus->sim->unit_number,
1564 path->bus->sim->bus_id,
1565 path->target->target_id,
1566 path->device->lun_id);
1567 printf("%s%d: ", periph->periph_name, periph->unit_number);
1568 scsi_print_inquiry(&path->device->inq_data);
1569 if (bootverbose && path->device->serial_num_len > 0) {
1570 /* Don't wrap the screen - print only the first 60 chars */
1571 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1572 periph->unit_number, path->device->serial_num);
1573 }
1574 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1575 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1576 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1577 xpt_action((union ccb*)&cts);
1578
1579 /* Ask the SIM for its base transfer speed */
1580 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1581 cpi.ccb_h.func_code = XPT_PATH_INQ;
1582 xpt_action((union ccb *)&cpi);
1583
1584 speed = cpi.base_transfer_speed;
1585 freq = 0;
1586 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1587 struct ccb_trans_settings_spi *spi;
1588
1589 spi = &cts.xport_specific.spi;
1590 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1591 && spi->sync_offset != 0) {
1592 freq = scsi_calc_syncsrate(spi->sync_period);
1593 speed = freq;
1594 }
1595
1596 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1597 speed *= (0x01 << spi->bus_width);
1598 }
1599
1600 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1601 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1602 if (fc->valid & CTS_FC_VALID_SPEED) {
1603 speed = fc->bitrate;
1604 }
1605 }
1606
1607 mb = speed / 1000;
1608 if (mb > 0)
1609 printf("%s%d: %d.%03dMB/s transfers",
1610 periph->periph_name, periph->unit_number,
1611 mb, speed % 1000);
1612 else
1613 printf("%s%d: %dKB/s transfers", periph->periph_name,
1614 periph->unit_number, speed);
1615 /* Report additional information about SPI connections */
1616 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1617 struct ccb_trans_settings_spi *spi;
1618
1619 spi = &cts.xport_specific.spi;
1620 if (freq != 0) {
1621 printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1622 freq % 1000,
1623 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1624 ? " DT" : "",
1625 spi->sync_offset);
1626 }
1627 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1628 && spi->bus_width > 0) {
1629 if (freq != 0) {
1630 printf(", ");
1631 } else {
1632 printf(" (");
1633 }
1634 printf("%dbit)", 8 * (0x01 << spi->bus_width));
1635 } else if (freq != 0) {
1636 printf(")");
1637 }
1638 }
1639 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1640 struct ccb_trans_settings_fc *fc;
1641
1642 fc = &cts.xport_specific.fc;
1643 if (fc->valid & CTS_FC_VALID_WWNN)
1644 printf(" WWNN 0x%llx", (long long) fc->wwnn);
1645 if (fc->valid & CTS_FC_VALID_WWPN)
1646 printf(" WWPN 0x%llx", (long long) fc->wwpn);
1647 if (fc->valid & CTS_FC_VALID_PORT)
1648 printf(" PortID 0x%x", fc->port);
1649 }
1650
1651 if (path->device->inq_flags & SID_CmdQue
1652 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1653 printf("\n%s%d: Tagged Queueing Enabled",
1654 periph->periph_name, periph->unit_number);
1655 }
1656 printf("\n");
1657
1658 /*
1659 * We only want to print the caller's announce string if they've
1660 * passed one in..
1661 */
1662 if (announce_string != NULL)
1663 printf("%s%d: %s\n", periph->periph_name,
1664 periph->unit_number, announce_string);
1665 splx(s);
1666}
1667#else /* CAM_NEW_TRAN_CODE */
1668void
1669xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1670{
1671 int s;
1672 u_int mb;
1673 struct cam_path *path;
1674 struct ccb_trans_settings cts;
1675
1676 GIANT_REQUIRED;
1677
1678 path = periph->path;
1679 /*
1680 * To ensure that this is printed in one piece,
1681 * mask out CAM interrupts.
1682 */
1683 s = splsoftcam();
1684 printf("%s%d at %s%d bus %d target %d lun %d\n",
1685 periph->periph_name, periph->unit_number,
1686 path->bus->sim->sim_name,
1687 path->bus->sim->unit_number,
1688 path->bus->sim->bus_id,
1689 path->target->target_id,
1690 path->device->lun_id);
1691 printf("%s%d: ", periph->periph_name, periph->unit_number);
1692 scsi_print_inquiry(&path->device->inq_data);
1693 if ((bootverbose)
1694 && (path->device->serial_num_len > 0)) {
1695 /* Don't wrap the screen - print only the first 60 chars */
1696 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1697 periph->unit_number, path->device->serial_num);
1698 }
1699 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1700 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1701 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1702 xpt_action((union ccb*)&cts);
1703 if (cts.ccb_h.status == CAM_REQ_CMP) {
1704 u_int speed;
1705 u_int freq;
1706
1707 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1708 && cts.sync_offset != 0) {
1709 freq = scsi_calc_syncsrate(cts.sync_period);
1710 speed = freq;
1711 } else {
1712 struct ccb_pathinq cpi;
1713
1714 /* Ask the SIM for its base transfer speed */
1715 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1716 cpi.ccb_h.func_code = XPT_PATH_INQ;
1717 xpt_action((union ccb *)&cpi);
1718
1719 speed = cpi.base_transfer_speed;
1720 freq = 0;
1721 }
1722 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1723 speed *= (0x01 << cts.bus_width);
1724 mb = speed / 1000;
1725 if (mb > 0)
1726 printf("%s%d: %d.%03dMB/s transfers",
1727 periph->periph_name, periph->unit_number,
1728 mb, speed % 1000);
1729 else
1730 printf("%s%d: %dKB/s transfers", periph->periph_name,
1731 periph->unit_number, speed);
1732 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1733 && cts.sync_offset != 0) {
1734 printf(" (%d.%03dMHz, offset %d", freq / 1000,
1735 freq % 1000, cts.sync_offset);
1736 }
1737 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1738 && cts.bus_width > 0) {
1739 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1740 && cts.sync_offset != 0) {
1741 printf(", ");
1742 } else {
1743 printf(" (");
1744 }
1745 printf("%dbit)", 8 * (0x01 << cts.bus_width));
1746 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1747 && cts.sync_offset != 0) {
1748 printf(")");
1749 }
1750
1751 if (path->device->inq_flags & SID_CmdQue
1752 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1753 printf(", Tagged Queueing Enabled");
1754 }
1755
1756 printf("\n");
1757 } else if (path->device->inq_flags & SID_CmdQue
1758 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1759 printf("%s%d: Tagged Queueing Enabled\n",
1760 periph->periph_name, periph->unit_number);
1761 }
1762
1763 /*
1764 * We only want to print the caller's announce string if they've
1765 * passed one in..
1766 */
1767 if (announce_string != NULL)
1768 printf("%s%d: %s\n", periph->periph_name,
1769 periph->unit_number, announce_string);
1770 splx(s);
1771}
1772
1773#endif /* CAM_NEW_TRAN_CODE */
1774
1775static dev_match_ret
1776xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1777 struct cam_eb *bus)
1778{
1779 dev_match_ret retval;
1780 int i;
1781
1782 retval = DM_RET_NONE;
1783
1784 /*
1785 * If we aren't given something to match against, that's an error.
1786 */
1787 if (bus == NULL)
1788 return(DM_RET_ERROR);
1789
1790 /*
1791 * If there are no match entries, then this bus matches no
1792 * matter what.
1793 */
1794 if ((patterns == NULL) || (num_patterns == 0))
1795 return(DM_RET_DESCEND | DM_RET_COPY);
1796
1797 for (i = 0; i < num_patterns; i++) {
1798 struct bus_match_pattern *cur_pattern;
1799
1800 /*
1801 * If the pattern in question isn't for a bus node, we
1802 * aren't interested. However, we do indicate to the
1803 * calling routine that we should continue descending the
1804 * tree, since the user wants to match against lower-level
1805 * EDT elements.
1806 */
1807 if (patterns[i].type != DEV_MATCH_BUS) {
1808 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1809 retval |= DM_RET_DESCEND;
1810 continue;
1811 }
1812
1813 cur_pattern = &patterns[i].pattern.bus_pattern;
1814
1815 /*
1816 * If they want to match any bus node, we give them any
1817 * device node.
1818 */
1819 if (cur_pattern->flags == BUS_MATCH_ANY) {
1820 /* set the copy flag */
1821 retval |= DM_RET_COPY;
1822
1823 /*
1824 * If we've already decided on an action, go ahead
1825 * and return.
1826 */
1827 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1828 return(retval);
1829 }
1830
1831 /*
1832 * Not sure why someone would do this...
1833 */
1834 if (cur_pattern->flags == BUS_MATCH_NONE)
1835 continue;
1836
1837 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1838 && (cur_pattern->path_id != bus->path_id))
1839 continue;
1840
1841 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1842 && (cur_pattern->bus_id != bus->sim->bus_id))
1843 continue;
1844
1845 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1846 && (cur_pattern->unit_number != bus->sim->unit_number))
1847 continue;
1848
1849 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1850 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1851 DEV_IDLEN) != 0))
1852 continue;
1853
1854 /*
1855 * If we get to this point, the user definitely wants
1856 * information on this bus. So tell the caller to copy the
1857 * data out.
1858 */
1859 retval |= DM_RET_COPY;
1860
1861 /*
1862 * If the return action has been set to descend, then we
1863 * know that we've already seen a non-bus matching
1864 * expression, therefore we need to further descend the tree.
1865 * This won't change by continuing around the loop, so we
1866 * go ahead and return. If we haven't seen a non-bus
1867 * matching expression, we keep going around the loop until
1868 * we exhaust the matching expressions. We'll set the stop
1869 * flag once we fall out of the loop.
1870 */
1871 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1872 return(retval);
1873 }
1874
1875 /*
1876 * If the return action hasn't been set to descend yet, that means
1877 * we haven't seen anything other than bus matching patterns. So
1878 * tell the caller to stop descending the tree -- the user doesn't
1879 * want to match against lower level tree elements.
1880 */
1881 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1882 retval |= DM_RET_STOP;
1883
1884 return(retval);
1885}
1886
1887static dev_match_ret
1888xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1889 struct cam_ed *device)
1890{
1891 dev_match_ret retval;
1892 int i;
1893
1894 retval = DM_RET_NONE;
1895
1896 /*
1897 * If we aren't given something to match against, that's an error.
1898 */
1899 if (device == NULL)
1900 return(DM_RET_ERROR);
1901
1902 /*
1903 * If there are no match entries, then this device matches no
1904 * matter what.
1905 */
1906 if ((patterns == NULL) || (num_patterns == 0))
1907 return(DM_RET_DESCEND | DM_RET_COPY);
1908
1909 for (i = 0; i < num_patterns; i++) {
1910 struct device_match_pattern *cur_pattern;
1911
1912 /*
1913 * If the pattern in question isn't for a device node, we
1914 * aren't interested.
1915 */
1916 if (patterns[i].type != DEV_MATCH_DEVICE) {
1917 if ((patterns[i].type == DEV_MATCH_PERIPH)
1918 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1919 retval |= DM_RET_DESCEND;
1920 continue;
1921 }
1922
1923 cur_pattern = &patterns[i].pattern.device_pattern;
1924
1925 /*
1926 * If they want to match any device node, we give them any
1927 * device node.
1928 */
1929 if (cur_pattern->flags == DEV_MATCH_ANY) {
1930 /* set the copy flag */
1931 retval |= DM_RET_COPY;
1932
1933
1934 /*
1935 * If we've already decided on an action, go ahead
1936 * and return.
1937 */
1938 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1939 return(retval);
1940 }
1941
1942 /*
1943 * Not sure why someone would do this...
1944 */
1945 if (cur_pattern->flags == DEV_MATCH_NONE)
1946 continue;
1947
1948 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1949 && (cur_pattern->path_id != device->target->bus->path_id))
1950 continue;
1951
1952 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1953 && (cur_pattern->target_id != device->target->target_id))
1954 continue;
1955
1956 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1957 && (cur_pattern->target_lun != device->lun_id))
1958 continue;
1959
1960 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1961 && (cam_quirkmatch((caddr_t)&device->inq_data,
1962 (caddr_t)&cur_pattern->inq_pat,
1963 1, sizeof(cur_pattern->inq_pat),
1964 scsi_static_inquiry_match) == NULL))
1965 continue;
1966
1967 /*
1968 * If we get to this point, the user definitely wants
1969 * information on this device. So tell the caller to copy
1970 * the data out.
1971 */
1972 retval |= DM_RET_COPY;
1973
1974 /*
1975 * If the return action has been set to descend, then we
1976 * know that we've already seen a peripheral matching
1977 * expression, therefore we need to further descend the tree.
1978 * This won't change by continuing around the loop, so we
1979 * go ahead and return. If we haven't seen a peripheral
1980 * matching expression, we keep going around the loop until
1981 * we exhaust the matching expressions. We'll set the stop
1982 * flag once we fall out of the loop.
1983 */
1984 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1985 return(retval);
1986 }
1987
1988 /*
1989 * If the return action hasn't been set to descend yet, that means
1990 * we haven't seen any peripheral matching patterns. So tell the
1991 * caller to stop descending the tree -- the user doesn't want to
1992 * match against lower level tree elements.
1993 */
1994 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1995 retval |= DM_RET_STOP;
1996
1997 return(retval);
1998}
1999
2000/*
2001 * Match a single peripheral against any number of match patterns.
2002 */
2003static dev_match_ret
2004xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
2005 struct cam_periph *periph)
2006{
2007 dev_match_ret retval;
2008 int i;
2009
2010 /*
2011 * If we aren't given something to match against, that's an error.
2012 */
2013 if (periph == NULL)
2014 return(DM_RET_ERROR);
2015
2016 /*
2017 * If there are no match entries, then this peripheral matches no
2018 * matter what.
2019 */
2020 if ((patterns == NULL) || (num_patterns == 0))
2021 return(DM_RET_STOP | DM_RET_COPY);
2022
2023 /*
2024 * There aren't any nodes below a peripheral node, so there's no
2025 * reason to descend the tree any further.
2026 */
2027 retval = DM_RET_STOP;
2028
2029 for (i = 0; i < num_patterns; i++) {
2030 struct periph_match_pattern *cur_pattern;
2031
2032 /*
2033 * If the pattern in question isn't for a peripheral, we
2034 * aren't interested.
2035 */
2036 if (patterns[i].type != DEV_MATCH_PERIPH)
2037 continue;
2038
2039 cur_pattern = &patterns[i].pattern.periph_pattern;
2040
2041 /*
2042 * If they want to match on anything, then we will do so.
2043 */
2044 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
2045 /* set the copy flag */
2046 retval |= DM_RET_COPY;
2047
2048 /*
2049 * We've already set the return action to stop,
2050 * since there are no nodes below peripherals in
2051 * the tree.
2052 */
2053 return(retval);
2054 }
2055
2056 /*
2057 * Not sure why someone would do this...
2058 */
2059 if (cur_pattern->flags == PERIPH_MATCH_NONE)
2060 continue;
2061
2062 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
2063 && (cur_pattern->path_id != periph->path->bus->path_id))
2064 continue;
2065
2066 /*
2067 * For the target and lun id's, we have to make sure the
2068 * target and lun pointers aren't NULL. The xpt peripheral
2069 * has a wildcard target and device.
2070 */
2071 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
2072 && ((periph->path->target == NULL)
2073 ||(cur_pattern->target_id != periph->path->target->target_id)))
2074 continue;
2075
2076 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
2077 && ((periph->path->device == NULL)
2078 || (cur_pattern->target_lun != periph->path->device->lun_id)))
2079 continue;
2080
2081 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
2082 && (cur_pattern->unit_number != periph->unit_number))
2083 continue;
2084
2085 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
2086 && (strncmp(cur_pattern->periph_name, periph->periph_name,
2087 DEV_IDLEN) != 0))
2088 continue;
2089
2090 /*
2091 * If we get to this point, the user definitely wants
2092 * information on this peripheral. So tell the caller to
2093 * copy the data out.
2094 */
2095 retval |= DM_RET_COPY;
2096
2097 /*
2098 * The return action has already been set to stop, since
2099 * peripherals don't have any nodes below them in the EDT.
2100 */
2101 return(retval);
2102 }
2103
2104 /*
2105 * If we get to this point, the peripheral that was passed in
2106 * doesn't match any of the patterns.
2107 */
2108 return(retval);
2109}
2110
2111static int
2112xptedtbusfunc(struct cam_eb *bus, void *arg)
2113{
2114 struct ccb_dev_match *cdm;
2115 dev_match_ret retval;
2116
2117 cdm = (struct ccb_dev_match *)arg;
2118
2119 /*
2120 * If our position is for something deeper in the tree, that means
2121 * that we've already seen this node. So, we keep going down.
2122 */
2123 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2124 && (cdm->pos.cookie.bus == bus)
2125 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2126 && (cdm->pos.cookie.target != NULL))
2127 retval = DM_RET_DESCEND;
2128 else
2129 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
2130
2131 /*
2132 * If we got an error, bail out of the search.
2133 */
2134 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2135 cdm->status = CAM_DEV_MATCH_ERROR;
2136 return(0);
2137 }
2138
2139 /*
2140 * If the copy flag is set, copy this bus out.
2141 */
2142 if (retval & DM_RET_COPY) {
2143 int spaceleft, j;
2144
2145 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2146 sizeof(struct dev_match_result));
2147
2148 /*
2149 * If we don't have enough space to put in another
2150 * match result, save our position and tell the
2151 * user there are more devices to check.
2152 */
2153 if (spaceleft < sizeof(struct dev_match_result)) {
2154 bzero(&cdm->pos, sizeof(cdm->pos));
2155 cdm->pos.position_type =
2156 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
2157
2158 cdm->pos.cookie.bus = bus;
2159 cdm->pos.generations[CAM_BUS_GENERATION]=
2160 bus_generation;
2161 cdm->status = CAM_DEV_MATCH_MORE;
2162 return(0);
2163 }
2164 j = cdm->num_matches;
2165 cdm->num_matches++;
2166 cdm->matches[j].type = DEV_MATCH_BUS;
2167 cdm->matches[j].result.bus_result.path_id = bus->path_id;
2168 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
2169 cdm->matches[j].result.bus_result.unit_number =
2170 bus->sim->unit_number;
2171 strncpy(cdm->matches[j].result.bus_result.dev_name,
2172 bus->sim->sim_name, DEV_IDLEN);
2173 }
2174
2175 /*
2176 * If the user is only interested in busses, there's no
2177 * reason to descend to the next level in the tree.
2178 */
2179 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2180 return(1);
2181
2182 /*
2183 * If there is a target generation recorded, check it to
2184 * make sure the target list hasn't changed.
2185 */
2186 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2187 && (bus == cdm->pos.cookie.bus)
2188 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2189 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
2190 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
2191 bus->generation)) {
2192 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2193 return(0);
2194 }
2195
2196 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2197 && (cdm->pos.cookie.bus == bus)
2198 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2199 && (cdm->pos.cookie.target != NULL))
2200 return(xpttargettraverse(bus,
2201 (struct cam_et *)cdm->pos.cookie.target,
2202 xptedttargetfunc, arg));
2203 else
2204 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2205}
2206
2207static int
2208xptedttargetfunc(struct cam_et *target, void *arg)
2209{
2210 struct ccb_dev_match *cdm;
2211
2212 cdm = (struct ccb_dev_match *)arg;
2213
2214 /*
2215 * If there is a device list generation recorded, check it to
2216 * make sure the device list hasn't changed.
2217 */
2218 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2219 && (cdm->pos.cookie.bus == target->bus)
2220 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2221 && (cdm->pos.cookie.target == target)
2222 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2223 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2224 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2225 target->generation)) {
2226 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2227 return(0);
2228 }
2229
2230 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2231 && (cdm->pos.cookie.bus == target->bus)
2232 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2233 && (cdm->pos.cookie.target == target)
2234 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2235 && (cdm->pos.cookie.device != NULL))
2236 return(xptdevicetraverse(target,
2237 (struct cam_ed *)cdm->pos.cookie.device,
2238 xptedtdevicefunc, arg));
2239 else
2240 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2241}
2242
2243static int
2244xptedtdevicefunc(struct cam_ed *device, void *arg)
2245{
2246
2247 struct ccb_dev_match *cdm;
2248 dev_match_ret retval;
2249
2250 cdm = (struct ccb_dev_match *)arg;
2251
2252 /*
2253 * If our position is for something deeper in the tree, that means
2254 * that we've already seen this node. So, we keep going down.
2255 */
2256 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2257 && (cdm->pos.cookie.device == device)
2258 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2259 && (cdm->pos.cookie.periph != NULL))
2260 retval = DM_RET_DESCEND;
2261 else
2262 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2263 device);
2264
2265 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2266 cdm->status = CAM_DEV_MATCH_ERROR;
2267 return(0);
2268 }
2269
2270 /*
2271 * If the copy flag is set, copy this device out.
2272 */
2273 if (retval & DM_RET_COPY) {
2274 int spaceleft, j;
2275
2276 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2277 sizeof(struct dev_match_result));
2278
2279 /*
2280 * If we don't have enough space to put in another
2281 * match result, save our position and tell the
2282 * user there are more devices to check.
2283 */
2284 if (spaceleft < sizeof(struct dev_match_result)) {
2285 bzero(&cdm->pos, sizeof(cdm->pos));
2286 cdm->pos.position_type =
2287 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2288 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2289
2290 cdm->pos.cookie.bus = device->target->bus;
2291 cdm->pos.generations[CAM_BUS_GENERATION]=
2292 bus_generation;
2293 cdm->pos.cookie.target = device->target;
2294 cdm->pos.generations[CAM_TARGET_GENERATION] =
2295 device->target->bus->generation;
2296 cdm->pos.cookie.device = device;
2297 cdm->pos.generations[CAM_DEV_GENERATION] =
2298 device->target->generation;
2299 cdm->status = CAM_DEV_MATCH_MORE;
2300 return(0);
2301 }
2302 j = cdm->num_matches;
2303 cdm->num_matches++;
2304 cdm->matches[j].type = DEV_MATCH_DEVICE;
2305 cdm->matches[j].result.device_result.path_id =
2306 device->target->bus->path_id;
2307 cdm->matches[j].result.device_result.target_id =
2308 device->target->target_id;
2309 cdm->matches[j].result.device_result.target_lun =
2310 device->lun_id;
2311 bcopy(&device->inq_data,
2312 &cdm->matches[j].result.device_result.inq_data,
2313 sizeof(struct scsi_inquiry_data));
2314
2315 /* Let the user know whether this device is unconfigured */
2316 if (device->flags & CAM_DEV_UNCONFIGURED)
2317 cdm->matches[j].result.device_result.flags =
2318 DEV_RESULT_UNCONFIGURED;
2319 else
2320 cdm->matches[j].result.device_result.flags =
2321 DEV_RESULT_NOFLAG;
2322 }
2323
2324 /*
2325 * If the user isn't interested in peripherals, don't descend
2326 * the tree any further.
2327 */
2328 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2329 return(1);
2330
2331 /*
2332 * If there is a peripheral list generation recorded, make sure
2333 * it hasn't changed.
2334 */
2335 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2336 && (device->target->bus == cdm->pos.cookie.bus)
2337 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2338 && (device->target == cdm->pos.cookie.target)
2339 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2340 && (device == cdm->pos.cookie.device)
2341 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2342 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2343 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2344 device->generation)){
2345 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2346 return(0);
2347 }
2348
2349 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2350 && (cdm->pos.cookie.bus == device->target->bus)
2351 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2352 && (cdm->pos.cookie.target == device->target)
2353 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2354 && (cdm->pos.cookie.device == device)
2355 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2356 && (cdm->pos.cookie.periph != NULL))
2357 return(xptperiphtraverse(device,
2358 (struct cam_periph *)cdm->pos.cookie.periph,
2359 xptedtperiphfunc, arg));
2360 else
2361 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2362}
2363
2364static int
2365xptedtperiphfunc(struct cam_periph *periph, void *arg)
2366{
2367 struct ccb_dev_match *cdm;
2368 dev_match_ret retval;
2369
2370 cdm = (struct ccb_dev_match *)arg;
2371
2372 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2373
2374 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2375 cdm->status = CAM_DEV_MATCH_ERROR;
2376 return(0);
2377 }
2378
2379 /*
2380 * If the copy flag is set, copy this peripheral out.
2381 */
2382 if (retval & DM_RET_COPY) {
2383 int spaceleft, j;
2384
2385 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2386 sizeof(struct dev_match_result));
2387
2388 /*
2389 * If we don't have enough space to put in another
2390 * match result, save our position and tell the
2391 * user there are more devices to check.
2392 */
2393 if (spaceleft < sizeof(struct dev_match_result)) {
2394 bzero(&cdm->pos, sizeof(cdm->pos));
2395 cdm->pos.position_type =
2396 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2397 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2398 CAM_DEV_POS_PERIPH;
2399
2400 cdm->pos.cookie.bus = periph->path->bus;
2401 cdm->pos.generations[CAM_BUS_GENERATION]=
2402 bus_generation;
2403 cdm->pos.cookie.target = periph->path->target;
2404 cdm->pos.generations[CAM_TARGET_GENERATION] =
2405 periph->path->bus->generation;
2406 cdm->pos.cookie.device = periph->path->device;
2407 cdm->pos.generations[CAM_DEV_GENERATION] =
2408 periph->path->target->generation;
2409 cdm->pos.cookie.periph = periph;
2410 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2411 periph->path->device->generation;
2412 cdm->status = CAM_DEV_MATCH_MORE;
2413 return(0);
2414 }
2415
2416 j = cdm->num_matches;
2417 cdm->num_matches++;
2418 cdm->matches[j].type = DEV_MATCH_PERIPH;
2419 cdm->matches[j].result.periph_result.path_id =
2420 periph->path->bus->path_id;
2421 cdm->matches[j].result.periph_result.target_id =
2422 periph->path->target->target_id;
2423 cdm->matches[j].result.periph_result.target_lun =
2424 periph->path->device->lun_id;
2425 cdm->matches[j].result.periph_result.unit_number =
2426 periph->unit_number;
2427 strncpy(cdm->matches[j].result.periph_result.periph_name,
2428 periph->periph_name, DEV_IDLEN);
2429 }
2430
2431 return(1);
2432}
2433
2434static int
2435xptedtmatch(struct ccb_dev_match *cdm)
2436{
2437 int ret;
2438
2439 cdm->num_matches = 0;
2440
2441 /*
2442 * Check the bus list generation. If it has changed, the user
2443 * needs to reset everything and start over.
2444 */
2445 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2446 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2447 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2448 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2449 return(0);
2450 }
2451
2452 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2453 && (cdm->pos.cookie.bus != NULL))
2454 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2455 xptedtbusfunc, cdm);
2456 else
2457 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2458
2459 /*
2460 * If we get back 0, that means that we had to stop before fully
2461 * traversing the EDT. It also means that one of the subroutines
2462 * has set the status field to the proper value. If we get back 1,
2463 * we've fully traversed the EDT and copied out any matching entries.
2464 */
2465 if (ret == 1)
2466 cdm->status = CAM_DEV_MATCH_LAST;
2467
2468 return(ret);
2469}
2470
2471static int
2472xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2473{
2474 struct ccb_dev_match *cdm;
2475
2476 cdm = (struct ccb_dev_match *)arg;
2477
2478 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2479 && (cdm->pos.cookie.pdrv == pdrv)
2480 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2481 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2482 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2483 (*pdrv)->generation)) {
2484 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2485 return(0);
2486 }
2487
2488 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2489 && (cdm->pos.cookie.pdrv == pdrv)
2490 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2491 && (cdm->pos.cookie.periph != NULL))
2492 return(xptpdperiphtraverse(pdrv,
2493 (struct cam_periph *)cdm->pos.cookie.periph,
2494 xptplistperiphfunc, arg));
2495 else
2496 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2497}
2498
2499static int
2500xptplistperiphfunc(struct cam_periph *periph, void *arg)
2501{
2502 struct ccb_dev_match *cdm;
2503 dev_match_ret retval;
2504
2505 cdm = (struct ccb_dev_match *)arg;
2506
2507 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2508
2509 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2510 cdm->status = CAM_DEV_MATCH_ERROR;
2511 return(0);
2512 }
2513
2514 /*
2515 * If the copy flag is set, copy this peripheral out.
2516 */
2517 if (retval & DM_RET_COPY) {
2518 int spaceleft, j;
2519
2520 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2521 sizeof(struct dev_match_result));
2522
2523 /*
2524 * If we don't have enough space to put in another
2525 * match result, save our position and tell the
2526 * user there are more devices to check.
2527 */
2528 if (spaceleft < sizeof(struct dev_match_result)) {
2529 struct periph_driver **pdrv;
2530
2531 pdrv = NULL;
2532 bzero(&cdm->pos, sizeof(cdm->pos));
2533 cdm->pos.position_type =
2534 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2535 CAM_DEV_POS_PERIPH;
2536
2537 /*
2538 * This may look a bit non-sensical, but it is
2539 * actually quite logical. There are very few
2540 * peripheral drivers, and bloating every peripheral
2541 * structure with a pointer back to its parent
2542 * peripheral driver linker set entry would cost
2543 * more in the long run than doing this quick lookup.
2544 */
2545 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2546 if (strcmp((*pdrv)->driver_name,
2547 periph->periph_name) == 0)
2548 break;
2549 }
2550
2551 if (*pdrv == NULL) {
2552 cdm->status = CAM_DEV_MATCH_ERROR;
2553 return(0);
2554 }
2555
2556 cdm->pos.cookie.pdrv = pdrv;
2557 /*
2558 * The periph generation slot does double duty, as
2559 * does the periph pointer slot. They are used for
2560 * both edt and pdrv lookups and positioning.
2561 */
2562 cdm->pos.cookie.periph = periph;
2563 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2564 (*pdrv)->generation;
2565 cdm->status = CAM_DEV_MATCH_MORE;
2566 return(0);
2567 }
2568
2569 j = cdm->num_matches;
2570 cdm->num_matches++;
2571 cdm->matches[j].type = DEV_MATCH_PERIPH;
2572 cdm->matches[j].result.periph_result.path_id =
2573 periph->path->bus->path_id;
2574
2575 /*
2576 * The transport layer peripheral doesn't have a target or
2577 * lun.
2578 */
2579 if (periph->path->target)
2580 cdm->matches[j].result.periph_result.target_id =
2581 periph->path->target->target_id;
2582 else
2583 cdm->matches[j].result.periph_result.target_id = -1;
2584
2585 if (periph->path->device)
2586 cdm->matches[j].result.periph_result.target_lun =
2587 periph->path->device->lun_id;
2588 else
2589 cdm->matches[j].result.periph_result.target_lun = -1;
2590
2591 cdm->matches[j].result.periph_result.unit_number =
2592 periph->unit_number;
2593 strncpy(cdm->matches[j].result.periph_result.periph_name,
2594 periph->periph_name, DEV_IDLEN);
2595 }
2596
2597 return(1);
2598}
2599
2600static int
2601xptperiphlistmatch(struct ccb_dev_match *cdm)
2602{
2603 int ret;
2604
2605 cdm->num_matches = 0;
2606
2607 /*
2608 * At this point in the edt traversal function, we check the bus
2609 * list generation to make sure that no busses have been added or
2610 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2611 * For the peripheral driver list traversal function, however, we
2612 * don't have to worry about new peripheral driver types coming or
2613 * going; they're in a linker set, and therefore can't change
2614 * without a recompile.
2615 */
2616
2617 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2618 && (cdm->pos.cookie.pdrv != NULL))
2619 ret = xptpdrvtraverse(
2620 (struct periph_driver **)cdm->pos.cookie.pdrv,
2621 xptplistpdrvfunc, cdm);
2622 else
2623 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2624
2625 /*
2626 * If we get back 0, that means that we had to stop before fully
2627 * traversing the peripheral driver tree. It also means that one of
2628 * the subroutines has set the status field to the proper value. If
2629 * we get back 1, we've fully traversed the EDT and copied out any
2630 * matching entries.
2631 */
2632 if (ret == 1)
2633 cdm->status = CAM_DEV_MATCH_LAST;
2634
2635 return(ret);
2636}
2637
2638static int
2639xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2640{
2641 struct cam_eb *bus, *next_bus;
2642 int retval;
2643
2644 retval = 1;
2645
2646 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2647 bus != NULL;
2648 bus = next_bus) {
2649 next_bus = TAILQ_NEXT(bus, links);
2650
2651 retval = tr_func(bus, arg);
2652 if (retval == 0)
2653 return(retval);
2654 }
2655
2656 return(retval);
2657}
2658
2659static int
2660xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2661 xpt_targetfunc_t *tr_func, void *arg)
2662{
2663 struct cam_et *target, *next_target;
2664 int retval;
2665
2666 retval = 1;
2667 for (target = (start_target ? start_target :
2668 TAILQ_FIRST(&bus->et_entries));
2669 target != NULL; target = next_target) {
2670
2671 next_target = TAILQ_NEXT(target, links);
2672
2673 retval = tr_func(target, arg);
2674
2675 if (retval == 0)
2676 return(retval);
2677 }
2678
2679 return(retval);
2680}
2681
2682static int
2683xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2684 xpt_devicefunc_t *tr_func, void *arg)
2685{
2686 struct cam_ed *device, *next_device;
2687 int retval;
2688
2689 retval = 1;
2690 for (device = (start_device ? start_device :
2691 TAILQ_FIRST(&target->ed_entries));
2692 device != NULL;
2693 device = next_device) {
2694
2695 next_device = TAILQ_NEXT(device, links);
2696
2697 retval = tr_func(device, arg);
2698
2699 if (retval == 0)
2700 return(retval);
2701 }
2702
2703 return(retval);
2704}
2705
2706static int
2707xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2708 xpt_periphfunc_t *tr_func, void *arg)
2709{
2710 struct cam_periph *periph, *next_periph;
2711 int retval;
2712
2713 retval = 1;
2714
2715 for (periph = (start_periph ? start_periph :
2716 SLIST_FIRST(&device->periphs));
2717 periph != NULL;
2718 periph = next_periph) {
2719
2720 next_periph = SLIST_NEXT(periph, periph_links);
2721
2722 retval = tr_func(periph, arg);
2723 if (retval == 0)
2724 return(retval);
2725 }
2726
2727 return(retval);
2728}
2729
2730static int
2731xptpdrvtraverse(struct periph_driver **start_pdrv,
2732 xpt_pdrvfunc_t *tr_func, void *arg)
2733{
2734 struct periph_driver **pdrv;
2735 int retval;
2736
2737 retval = 1;
2738
2739 /*
2740 * We don't traverse the peripheral driver list like we do the
2741 * other lists, because it is a linker set, and therefore cannot be
2742 * changed during runtime. If the peripheral driver list is ever
2743 * re-done to be something other than a linker set (i.e. it can
2744 * change while the system is running), the list traversal should
2745 * be modified to work like the other traversal functions.
2746 */
2747 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2748 *pdrv != NULL; pdrv++) {
2749 retval = tr_func(pdrv, arg);
2750
2751 if (retval == 0)
2752 return(retval);
2753 }
2754
2755 return(retval);
2756}
2757
2758static int
2759xptpdperiphtraverse(struct periph_driver **pdrv,
2760 struct cam_periph *start_periph,
2761 xpt_periphfunc_t *tr_func, void *arg)
2762{
2763 struct cam_periph *periph, *next_periph;
2764 int retval;
2765
2766 retval = 1;
2767
2768 for (periph = (start_periph ? start_periph :
2769 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2770 periph = next_periph) {
2771
2772 next_periph = TAILQ_NEXT(periph, unit_links);
2773
2774 retval = tr_func(periph, arg);
2775 if (retval == 0)
2776 return(retval);
2777 }
2778 return(retval);
2779}
2780
2781static int
2782xptdefbusfunc(struct cam_eb *bus, void *arg)
2783{
2784 struct xpt_traverse_config *tr_config;
2785
2786 tr_config = (struct xpt_traverse_config *)arg;
2787
2788 if (tr_config->depth == XPT_DEPTH_BUS) {
2789 xpt_busfunc_t *tr_func;
2790
2791 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2792
2793 return(tr_func(bus, tr_config->tr_arg));
2794 } else
2795 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2796}
2797
2798static int
2799xptdeftargetfunc(struct cam_et *target, void *arg)
2800{
2801 struct xpt_traverse_config *tr_config;
2802
2803 tr_config = (struct xpt_traverse_config *)arg;
2804
2805 if (tr_config->depth == XPT_DEPTH_TARGET) {
2806 xpt_targetfunc_t *tr_func;
2807
2808 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2809
2810 return(tr_func(target, tr_config->tr_arg));
2811 } else
2812 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2813}
2814
2815static int
2816xptdefdevicefunc(struct cam_ed *device, void *arg)
2817{
2818 struct xpt_traverse_config *tr_config;
2819
2820 tr_config = (struct xpt_traverse_config *)arg;
2821
2822 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2823 xpt_devicefunc_t *tr_func;
2824
2825 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2826
2827 return(tr_func(device, tr_config->tr_arg));
2828 } else
2829 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2830}
2831
2832static int
2833xptdefperiphfunc(struct cam_periph *periph, void *arg)
2834{
2835 struct xpt_traverse_config *tr_config;
2836 xpt_periphfunc_t *tr_func;
2837
2838 tr_config = (struct xpt_traverse_config *)arg;
2839
2840 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2841
2842 /*
2843 * Unlike the other default functions, we don't check for depth
2844 * here. The peripheral driver level is the last level in the EDT,
2845 * so if we're here, we should execute the function in question.
2846 */
2847 return(tr_func(periph, tr_config->tr_arg));
2848}
2849
2850/*
2851 * Execute the given function for every bus in the EDT.
2852 */
2853static int
2854xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2855{
2856 struct xpt_traverse_config tr_config;
2857
2858 tr_config.depth = XPT_DEPTH_BUS;
2859 tr_config.tr_func = tr_func;
2860 tr_config.tr_arg = arg;
2861
2862 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2863}
2864
2865#ifdef notusedyet
2866/*
2867 * Execute the given function for every target in the EDT.
2868 */
2869static int
2870xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2871{
2872 struct xpt_traverse_config tr_config;
2873
2874 tr_config.depth = XPT_DEPTH_TARGET;
2875 tr_config.tr_func = tr_func;
2876 tr_config.tr_arg = arg;
2877
2878 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2879}
2880#endif /* notusedyet */
2881
2882/*
2883 * Execute the given function for every device in the EDT.
2884 */
2885static int
2886xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2887{
2888 struct xpt_traverse_config tr_config;
2889
2890 tr_config.depth = XPT_DEPTH_DEVICE;
2891 tr_config.tr_func = tr_func;
2892 tr_config.tr_arg = arg;
2893
2894 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2895}
2896
2897#ifdef notusedyet
2898/*
2899 * Execute the given function for every peripheral in the EDT.
2900 */
2901static int
2902xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2903{
2904 struct xpt_traverse_config tr_config;
2905
2906 tr_config.depth = XPT_DEPTH_PERIPH;
2907 tr_config.tr_func = tr_func;
2908 tr_config.tr_arg = arg;
2909
2910 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2911}
2912#endif /* notusedyet */
2913
2914static int
2915xptsetasyncfunc(struct cam_ed *device, void *arg)
2916{
2917 struct cam_path path;
2918 struct ccb_getdev cgd;
2919 struct async_node *cur_entry;
2920
2921 cur_entry = (struct async_node *)arg;
2922
2923 /*
2924 * Don't report unconfigured devices (Wildcard devs,
2925 * devices only for target mode, device instances
2926 * that have been invalidated but are waiting for
2927 * their last reference count to be released).
2928 */
2929 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2930 return (1);
2931
2932 xpt_compile_path(&path,
2933 NULL,
2934 device->target->bus->path_id,
2935 device->target->target_id,
2936 device->lun_id);
2937 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2938 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2939 xpt_action((union ccb *)&cgd);
2940 cur_entry->callback(cur_entry->callback_arg,
2941 AC_FOUND_DEVICE,
2942 &path, &cgd);
2943 xpt_release_path(&path);
2944
2945 return(1);
2946}
2947
2948static int
2949xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2950{
2951 struct cam_path path;
2952 struct ccb_pathinq cpi;
2953 struct async_node *cur_entry;
2954
2955 cur_entry = (struct async_node *)arg;
2956
2957 xpt_compile_path(&path, /*periph*/NULL,
2958 bus->sim->path_id,
2959 CAM_TARGET_WILDCARD,
2960 CAM_LUN_WILDCARD);
2961 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2962 cpi.ccb_h.func_code = XPT_PATH_INQ;
2963 xpt_action((union ccb *)&cpi);
2964 cur_entry->callback(cur_entry->callback_arg,
2965 AC_PATH_REGISTERED,
2966 &path, &cpi);
2967 xpt_release_path(&path);
2968
2969 return(1);
2970}
2971
2972void
2973xpt_action(union ccb *start_ccb)
2974{
2975 int iopl;
2976
2977 GIANT_REQUIRED;
2978
2979 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2980
2981 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2982
2983 iopl = splsoftcam();
2984 switch (start_ccb->ccb_h.func_code) {
2985 case XPT_SCSI_IO:
2986 {
2987#ifdef CAM_NEW_TRAN_CODE
2988 struct cam_ed *device;
2989#endif /* CAM_NEW_TRAN_CODE */
2990#ifdef CAMDEBUG
2991 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2992 struct cam_path *path;
2993
2994 path = start_ccb->ccb_h.path;
2995#endif
2996
2997 /*
2998 * For the sake of compatibility with SCSI-1
2999 * devices that may not understand the identify
3000 * message, we include lun information in the
3001 * second byte of all commands. SCSI-1 specifies
3002 * that luns are a 3 bit value and reserves only 3
3003 * bits for lun information in the CDB. Later
3004 * revisions of the SCSI spec allow for more than 8
3005 * luns, but have deprecated lun information in the
3006 * CDB. So, if the lun won't fit, we must omit.
3007 *
3008 * Also be aware that during initial probing for devices,
3009 * the inquiry information is unknown but initialized to 0.
3010 * This means that this code will be exercised while probing
3011 * devices with an ANSI revision greater than 2.
3012 */
3013#ifdef CAM_NEW_TRAN_CODE
3014 device = start_ccb->ccb_h.path->device;
3015 if (device->protocol_version <= SCSI_REV_2
3016#else /* CAM_NEW_TRAN_CODE */
3017 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
3018#endif /* CAM_NEW_TRAN_CODE */
3019 && start_ccb->ccb_h.target_lun < 8
3020 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
3021
3022 start_ccb->csio.cdb_io.cdb_bytes[1] |=
3023 start_ccb->ccb_h.target_lun << 5;
3024 }
3025 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
3026 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
3027 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
3028 &path->device->inq_data),
3029 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
3030 cdb_str, sizeof(cdb_str))));
3031 }
3032 /* FALLTHROUGH */
3033 case XPT_TARGET_IO:
3034 case XPT_CONT_TARGET_IO:
3035 start_ccb->csio.sense_resid = 0;
3036 start_ccb->csio.resid = 0;
3037 /* FALLTHROUGH */
3038 case XPT_RESET_DEV:
3039 case XPT_ENG_EXEC:
3040 {
3041 struct cam_path *path;
3042 int s;
3043 int runq;
3044
3045 path = start_ccb->ccb_h.path;
3046 s = splsoftcam();
3047
3048 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
3049 if (path->device->qfrozen_cnt == 0)
3050 runq = xpt_schedule_dev_sendq(path->bus, path->device);
3051 else
3052 runq = 0;
3053 splx(s);
3054 if (runq != 0)
3055 xpt_run_dev_sendq(path->bus);
3056 break;
3057 }
3058 case XPT_SET_TRAN_SETTINGS:
3059 {
3060 xpt_set_transfer_settings(&start_ccb->cts,
3061 start_ccb->ccb_h.path->device,
3062 /*async_update*/FALSE);
3063 break;
3064 }
3065 case XPT_CALC_GEOMETRY:
3066 {
3067 struct cam_sim *sim;
3068
3069 /* Filter out garbage */
3070 if (start_ccb->ccg.block_size == 0
3071 || start_ccb->ccg.volume_size == 0) {
3072 start_ccb->ccg.cylinders = 0;
3073 start_ccb->ccg.heads = 0;
3074 start_ccb->ccg.secs_per_track = 0;
3075 start_ccb->ccb_h.status = CAM_REQ_CMP;
3076 break;
3077 }
3078#ifdef PC98
3079 /*
3080 * In a PC-98 system, geometry translation depens on
3081 * the "real" device geometry obtained from mode page 4.
3082 * SCSI geometry translation is performed in the
3083 * initialization routine of the SCSI BIOS and the result
3084 * stored in host memory. If the translation is available
3085 * in host memory, use it. If not, rely on the default
3086 * translation the device driver performs.
3087 */
3088 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
3089 start_ccb->ccb_h.status = CAM_REQ_CMP;
3090 break;
3091 }
3092#endif
3093 sim = start_ccb->ccb_h.path->bus->sim;
3094 (*(sim->sim_action))(sim, start_ccb);
3095 break;
3096 }
3097 case XPT_ABORT:
3098 {
3099 union ccb* abort_ccb;
3100 int s;
3101
3102 abort_ccb = start_ccb->cab.abort_ccb;
3103 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
3104
3105 if (abort_ccb->ccb_h.pinfo.index >= 0) {
3106 struct cam_ccbq *ccbq;
3107
3108 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
3109 cam_ccbq_remove_ccb(ccbq, abort_ccb);
3110 abort_ccb->ccb_h.status =
3111 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3112 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3113 s = splcam();
3114 xpt_done(abort_ccb);
3115 splx(s);
3116 start_ccb->ccb_h.status = CAM_REQ_CMP;
3117 break;
3118 }
3119 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
3120 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
3121 /*
3122 * We've caught this ccb en route to
3123 * the SIM. Flag it for abort and the
3124 * SIM will do so just before starting
3125 * real work on the CCB.
3126 */
3127 abort_ccb->ccb_h.status =
3128 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3129 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3130 start_ccb->ccb_h.status = CAM_REQ_CMP;
3131 break;
3132 }
3133 }
3134 if (XPT_FC_IS_QUEUED(abort_ccb)
3135 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
3136 /*
3137 * It's already completed but waiting
3138 * for our SWI to get to it.
3139 */
3140 start_ccb->ccb_h.status = CAM_UA_ABORT;
3141 break;
3142 }
3143 /*
3144 * If we weren't able to take care of the abort request
3145 * in the XPT, pass the request down to the SIM for processing.
3146 */
3147 }
3148 /* FALLTHROUGH */
3149 case XPT_ACCEPT_TARGET_IO:
3150 case XPT_EN_LUN:
3151 case XPT_IMMED_NOTIFY:
3152 case XPT_NOTIFY_ACK:
3153 case XPT_GET_TRAN_SETTINGS:
3154 case XPT_RESET_BUS:
3155 {
3156 struct cam_sim *sim;
3157
3158 sim = start_ccb->ccb_h.path->bus->sim;
3159 (*(sim->sim_action))(sim, start_ccb);
3160 break;
3161 }
3162 case XPT_PATH_INQ:
3163 {
3164 struct cam_sim *sim;
3165
3166 sim = start_ccb->ccb_h.path->bus->sim;
3167 (*(sim->sim_action))(sim, start_ccb);
3168 break;
3169 }
3170 case XPT_PATH_STATS:
3171 start_ccb->cpis.last_reset =
3172 start_ccb->ccb_h.path->bus->last_reset;
3173 start_ccb->ccb_h.status = CAM_REQ_CMP;
3174 break;
3175 case XPT_GDEV_TYPE:
3176 {
3177 struct cam_ed *dev;
3178 int s;
3179
3180 dev = start_ccb->ccb_h.path->device;
3181 s = splcam();
3182 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3183 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3184 } else {
3185 struct ccb_getdev *cgd;
3186 struct cam_eb *bus;
3187 struct cam_et *tar;
3188
3189 cgd = &start_ccb->cgd;
3190 bus = cgd->ccb_h.path->bus;
3191 tar = cgd->ccb_h.path->target;
3192 cgd->inq_data = dev->inq_data;
3193 cgd->ccb_h.status = CAM_REQ_CMP;
3194 cgd->serial_num_len = dev->serial_num_len;
3195 if ((dev->serial_num_len > 0)
3196 && (dev->serial_num != NULL))
3197 bcopy(dev->serial_num, cgd->serial_num,
3198 dev->serial_num_len);
3199 }
3200 splx(s);
3201 break;
3202 }
3203 case XPT_GDEV_STATS:
3204 {
3205 struct cam_ed *dev;
3206 int s;
3207
3208 dev = start_ccb->ccb_h.path->device;
3209 s = splcam();
3210 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3211 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3212 } else {
3213 struct ccb_getdevstats *cgds;
3214 struct cam_eb *bus;
3215 struct cam_et *tar;
3216
3217 cgds = &start_ccb->cgds;
3218 bus = cgds->ccb_h.path->bus;
3219 tar = cgds->ccb_h.path->target;
3220 cgds->dev_openings = dev->ccbq.dev_openings;
3221 cgds->dev_active = dev->ccbq.dev_active;
3222 cgds->devq_openings = dev->ccbq.devq_openings;
3223 cgds->devq_queued = dev->ccbq.queue.entries;
3224 cgds->held = dev->ccbq.held;
3225 cgds->last_reset = tar->last_reset;
3226 cgds->maxtags = dev->quirk->maxtags;
3227 cgds->mintags = dev->quirk->mintags;
3228 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3229 cgds->last_reset = bus->last_reset;
3230 cgds->ccb_h.status = CAM_REQ_CMP;
3231 }
3232 splx(s);
3233 break;
3234 }
3235 case XPT_GDEVLIST:
3236 {
3237 struct cam_periph *nperiph;
3238 struct periph_list *periph_head;
3239 struct ccb_getdevlist *cgdl;
3240 u_int i;
3241 int s;
3242 struct cam_ed *device;
3243 int found;
3244
3245
3246 found = 0;
3247
3248 /*
3249 * Don't want anyone mucking with our data.
3250 */
3251 s = splcam();
3252 device = start_ccb->ccb_h.path->device;
3253 periph_head = &device->periphs;
3254 cgdl = &start_ccb->cgdl;
3255
3256 /*
3257 * Check and see if the list has changed since the user
3258 * last requested a list member. If so, tell them that the
3259 * list has changed, and therefore they need to start over
3260 * from the beginning.
3261 */
3262 if ((cgdl->index != 0) &&
3263 (cgdl->generation != device->generation)) {
3264 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3265 splx(s);
3266 break;
3267 }
3268
3269 /*
3270 * Traverse the list of peripherals and attempt to find
3271 * the requested peripheral.
3272 */
3273 for (nperiph = SLIST_FIRST(periph_head), i = 0;
3274 (nperiph != NULL) && (i <= cgdl->index);
3275 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3276 if (i == cgdl->index) {
3277 strncpy(cgdl->periph_name,
3278 nperiph->periph_name,
3279 DEV_IDLEN);
3280 cgdl->unit_number = nperiph->unit_number;
3281 found = 1;
3282 }
3283 }
3284 if (found == 0) {
3285 cgdl->status = CAM_GDEVLIST_ERROR;
3286 splx(s);
3287 break;
3288 }
3289
3290 if (nperiph == NULL)
3291 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3292 else
3293 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3294
3295 cgdl->index++;
3296 cgdl->generation = device->generation;
3297
3298 splx(s);
3299 cgdl->ccb_h.status = CAM_REQ_CMP;
3300 break;
3301 }
3302 case XPT_DEV_MATCH:
3303 {
3304 int s;
3305 dev_pos_type position_type;
3306 struct ccb_dev_match *cdm;
3307
3308 cdm = &start_ccb->cdm;
3309
3310 /*
3311 * Prevent EDT changes while we traverse it.
3312 */
3313 s = splcam();
3314 /*
3315 * There are two ways of getting at information in the EDT.
3316 * The first way is via the primary EDT tree. It starts
3317 * with a list of busses, then a list of targets on a bus,
3318 * then devices/luns on a target, and then peripherals on a
3319 * device/lun. The "other" way is by the peripheral driver
3320 * lists. The peripheral driver lists are organized by
3321 * peripheral driver. (obviously) So it makes sense to
3322 * use the peripheral driver list if the user is looking
3323 * for something like "da1", or all "da" devices. If the
3324 * user is looking for something on a particular bus/target
3325 * or lun, it's generally better to go through the EDT tree.
3326 */
3327
3328 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3329 position_type = cdm->pos.position_type;
3330 else {
3331 u_int i;
3332
3333 position_type = CAM_DEV_POS_NONE;
3334
3335 for (i = 0; i < cdm->num_patterns; i++) {
3336 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3337 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3338 position_type = CAM_DEV_POS_EDT;
3339 break;
3340 }
3341 }
3342
3343 if (cdm->num_patterns == 0)
3344 position_type = CAM_DEV_POS_EDT;
3345 else if (position_type == CAM_DEV_POS_NONE)
3346 position_type = CAM_DEV_POS_PDRV;
3347 }
3348
3349 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3350 case CAM_DEV_POS_EDT:
3351 xptedtmatch(cdm);
3352 break;
3353 case CAM_DEV_POS_PDRV:
3354 xptperiphlistmatch(cdm);
3355 break;
3356 default:
3357 cdm->status = CAM_DEV_MATCH_ERROR;
3358 break;
3359 }
3360
3361 splx(s);
3362
3363 if (cdm->status == CAM_DEV_MATCH_ERROR)
3364 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3365 else
3366 start_ccb->ccb_h.status = CAM_REQ_CMP;
3367
3368 break;
3369 }
3370 case XPT_SASYNC_CB:
3371 {
3372 struct ccb_setasync *csa;
3373 struct async_node *cur_entry;
3374 struct async_list *async_head;
3375 u_int32_t added;
3376 int s;
3377
3378 csa = &start_ccb->csa;
3379 added = csa->event_enable;
3380 async_head = &csa->ccb_h.path->device->asyncs;
3381
3382 /*
3383 * If there is already an entry for us, simply
3384 * update it.
3385 */
3386 s = splcam();
3387 cur_entry = SLIST_FIRST(async_head);
3388 while (cur_entry != NULL) {
3389 if ((cur_entry->callback_arg == csa->callback_arg)
3390 && (cur_entry->callback == csa->callback))
3391 break;
3392 cur_entry = SLIST_NEXT(cur_entry, links);
3393 }
3394
3395 if (cur_entry != NULL) {
3396 /*
3397 * If the request has no flags set,
3398 * remove the entry.
3399 */
3400 added &= ~cur_entry->event_enable;
3401 if (csa->event_enable == 0) {
3402 SLIST_REMOVE(async_head, cur_entry,
3403 async_node, links);
3404 csa->ccb_h.path->device->refcount--;
3405 free(cur_entry, M_CAMXPT);
3406 } else {
3407 cur_entry->event_enable = csa->event_enable;
3408 }
3409 } else {
3410 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
3411 M_NOWAIT);
3412 if (cur_entry == NULL) {
3413 splx(s);
3414 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3415 break;
3416 }
3417 cur_entry->event_enable = csa->event_enable;
3418 cur_entry->callback_arg = csa->callback_arg;
3419 cur_entry->callback = csa->callback;
3420 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3421 csa->ccb_h.path->device->refcount++;
3422 }
3423
3424 if ((added & AC_FOUND_DEVICE) != 0) {
3425 /*
3426 * Get this peripheral up to date with all
3427 * the currently existing devices.
3428 */
3429 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3430 }
3431 if ((added & AC_PATH_REGISTERED) != 0) {
3432 /*
3433 * Get this peripheral up to date with all
3434 * the currently existing busses.
3435 */
3436 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3437 }
3438 splx(s);
3439 start_ccb->ccb_h.status = CAM_REQ_CMP;
3440 break;
3441 }
3442 case XPT_REL_SIMQ:
3443 {
3444 struct ccb_relsim *crs;
3445 struct cam_ed *dev;
3446 int s;
3447
3448 crs = &start_ccb->crs;
3449 dev = crs->ccb_h.path->device;
3450 if (dev == NULL) {
3451
3452 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3453 break;
3454 }
3455
3456 s = splcam();
3457
3458 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3459
3460 if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3461
3462 /* Don't ever go below one opening */
3463 if (crs->openings > 0) {
3464 xpt_dev_ccbq_resize(crs->ccb_h.path,
3465 crs->openings);
3466
3467 if (bootverbose) {
3468 xpt_print_path(crs->ccb_h.path);
3469 printf("tagged openings "
3470 "now %d\n",
3471 crs->openings);
3472 }
3473 }
3474 }
3475 }
3476
3477 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3478
3479 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3480
3481 /*
3482 * Just extend the old timeout and decrement
3483 * the freeze count so that a single timeout
3484 * is sufficient for releasing the queue.
3485 */
3486 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3487 untimeout(xpt_release_devq_timeout,
3488 dev, dev->c_handle);
3489 } else {
3490
3491 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3492 }
3493
3494 dev->c_handle =
3495 timeout(xpt_release_devq_timeout,
3496 dev,
3497 (crs->release_timeout * hz) / 1000);
3498
3499 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3500
3501 }
3502
3503 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3504
3505 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3506 /*
3507 * Decrement the freeze count so that a single
3508 * completion is still sufficient to unfreeze
3509 * the queue.
3510 */
3511 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3512 } else {
3513
3514 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3515 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3516 }
3517 }
3518
3519 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3520
3521 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3522 || (dev->ccbq.dev_active == 0)) {
3523
3524 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3525 } else {
3526
3527 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3528 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3529 }
3530 }
3531 splx(s);
3532
3533 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3534
3535 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3536 /*run_queue*/TRUE);
3537 }
3538 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3539 start_ccb->ccb_h.status = CAM_REQ_CMP;
3540 break;
3541 }
3542 case XPT_SCAN_BUS:
3543 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3544 break;
3545 case XPT_SCAN_LUN:
3546 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3547 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3548 start_ccb);
3549 break;
3550 case XPT_DEBUG: {
3551#ifdef CAMDEBUG
3552 int s;
3553
3554 s = splcam();
3555#ifdef CAM_DEBUG_DELAY
3556 cam_debug_delay = CAM_DEBUG_DELAY;
3557#endif
3558 cam_dflags = start_ccb->cdbg.flags;
3559 if (cam_dpath != NULL) {
3560 xpt_free_path(cam_dpath);
3561 cam_dpath = NULL;
3562 }
3563
3564 if (cam_dflags != CAM_DEBUG_NONE) {
3565 if (xpt_create_path(&cam_dpath, xpt_periph,
3566 start_ccb->ccb_h.path_id,
3567 start_ccb->ccb_h.target_id,
3568 start_ccb->ccb_h.target_lun) !=
3569 CAM_REQ_CMP) {
3570 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3571 cam_dflags = CAM_DEBUG_NONE;
3572 } else {
3573 start_ccb->ccb_h.status = CAM_REQ_CMP;
3574 xpt_print_path(cam_dpath);
3575 printf("debugging flags now %x\n", cam_dflags);
3576 }
3577 } else {
3578 cam_dpath = NULL;
3579 start_ccb->ccb_h.status = CAM_REQ_CMP;
3580 }
3581 splx(s);
3582#else /* !CAMDEBUG */
3583 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3584#endif /* CAMDEBUG */
3585 break;
3586 }
3587 case XPT_NOOP:
3588 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3589 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3590 start_ccb->ccb_h.status = CAM_REQ_CMP;
3591 break;
3592 default:
3593 case XPT_SDEV_TYPE:
3594 case XPT_TERM_IO:
3595 case XPT_ENG_INQ:
3596 /* XXX Implement */
3597 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3598 break;
3599 }
3600 splx(iopl);
3601}
3602
3603void
3604xpt_polled_action(union ccb *start_ccb)
3605{
3606 int s;
3607 u_int32_t timeout;
3608 struct cam_sim *sim;
3609 struct cam_devq *devq;
3610 struct cam_ed *dev;
3611
3612 GIANT_REQUIRED;
3613
3614 timeout = start_ccb->ccb_h.timeout;
3615 sim = start_ccb->ccb_h.path->bus->sim;
3616 devq = sim->devq;
3617 dev = start_ccb->ccb_h.path->device;
3618
3619 s = splcam();
3620
3621 /*
3622 * Steal an opening so that no other queued requests
3623 * can get it before us while we simulate interrupts.
3624 */
3625 dev->ccbq.devq_openings--;
3626 dev->ccbq.dev_openings--;
3627
3628 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3629 && (--timeout > 0)) {
3630 DELAY(1000);
3631 (*(sim->sim_poll))(sim);
3632 camisr(&cam_bioq);
3633 }
3634
3635 dev->ccbq.devq_openings++;
3636 dev->ccbq.dev_openings++;
3637
3638 if (timeout != 0) {
3639 xpt_action(start_ccb);
3640 while(--timeout > 0) {
3641 (*(sim->sim_poll))(sim);
3642 camisr(&cam_bioq);
3643 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3644 != CAM_REQ_INPROG)
3645 break;
3646 DELAY(1000);
3647 }
3648 if (timeout == 0) {
3649 /*
3650 * XXX Is it worth adding a sim_timeout entry
3651 * point so we can attempt recovery? If
3652 * this is only used for dumps, I don't think
3653 * it is.
3654 */
3655 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3656 }
3657 } else {
3658 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3659 }
3660 splx(s);
3661}
3662
3663/*
3664 * Schedule a peripheral driver to receive a ccb when it's
3665 * target device has space for more transactions.
3666 */
3667void
3668xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3669{
3670 struct cam_ed *device;
3671 int s;
3672 int runq;
3673
3674 GIANT_REQUIRED;
3675
3676 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3677 device = perph->path->device;
3678 s = splsoftcam();
3679 if (periph_is_queued(perph)) {
3680 /* Simply reorder based on new priority */
3681 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3682 (" change priority to %d\n", new_priority));
3683 if (new_priority < perph->pinfo.priority) {
3684 camq_change_priority(&device->drvq,
3685 perph->pinfo.index,
3686 new_priority);
3687 }
3688 runq = 0;
3689 } else {
3690 /* New entry on the queue */
3691 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3692 (" added periph to queue\n"));
3693 perph->pinfo.priority = new_priority;
3694 perph->pinfo.generation = ++device->drvq.generation;
3695 camq_insert(&device->drvq, &perph->pinfo);
3696 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3697 }
3698 splx(s);
3699 if (runq != 0) {
3700 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3701 (" calling xpt_run_devq\n"));
3702 xpt_run_dev_allocq(perph->path->bus);
3703 }
3704}
3705
3706
3707/*
3708 * Schedule a device to run on a given queue.
3709 * If the device was inserted as a new entry on the queue,
3710 * return 1 meaning the device queue should be run. If we
3711 * were already queued, implying someone else has already
3712 * started the queue, return 0 so the caller doesn't attempt
3713 * to run the queue. Must be run at either splsoftcam
3714 * (or splcam since that encompases splsoftcam).
3715 */
3716static int
3717xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3718 u_int32_t new_priority)
3719{
3720 int retval;
3721 u_int32_t old_priority;
3722
3723 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3724
3725 old_priority = pinfo->priority;
3726
3727 /*
3728 * Are we already queued?
3729 */
3730 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3731 /* Simply reorder based on new priority */
3732 if (new_priority < old_priority) {
3733 camq_change_priority(queue, pinfo->index,
3734 new_priority);
3735 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3736 ("changed priority to %d\n",
3737 new_priority));
3738 }
3739 retval = 0;
3740 } else {
3741 /* New entry on the queue */
3742 if (new_priority < old_priority)
3743 pinfo->priority = new_priority;
3744
3745 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3746 ("Inserting onto queue\n"));
3747 pinfo->generation = ++queue->generation;
3748 camq_insert(queue, pinfo);
3749 retval = 1;
3750 }
3751 return (retval);
3752}
3753
3754static void
3755xpt_run_dev_allocq(struct cam_eb *bus)
3756{
3757 struct cam_devq *devq;
3758 int s;
3759
3760 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3761 devq = bus->sim->devq;
3762
3763 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3764 (" qfrozen_cnt == 0x%x, entries == %d, "
3765 "openings == %d, active == %d\n",
3766 devq->alloc_queue.qfrozen_cnt,
3767 devq->alloc_queue.entries,
3768 devq->alloc_openings,
3769 devq->alloc_active));
3770
3771 s = splsoftcam();
3772 devq->alloc_queue.qfrozen_cnt++;
3773 while ((devq->alloc_queue.entries > 0)
3774 && (devq->alloc_openings > 0)
3775 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3776 struct cam_ed_qinfo *qinfo;
3777 struct cam_ed *device;
3778 union ccb *work_ccb;
3779 struct cam_periph *drv;
3780 struct camq *drvq;
3781
3782 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3783 CAMQ_HEAD);
3784 device = qinfo->device;
3785
3786 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3787 ("running device %p\n", device));
3788
3789 drvq = &device->drvq;
3790
3791#ifdef CAMDEBUG
3792 if (drvq->entries <= 0) {
3793 panic("xpt_run_dev_allocq: "
3794 "Device on queue without any work to do");
3795 }
3796#endif
3797 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3798 devq->alloc_openings--;
3799 devq->alloc_active++;
3800 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3801 splx(s);
3802 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3803 drv->pinfo.priority);
3804 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3805 ("calling periph start\n"));
3806 drv->periph_start(drv, work_ccb);
3807 } else {
3808 /*
3809 * Malloc failure in alloc_ccb
3810 */
3811 /*
3812 * XXX add us to a list to be run from free_ccb
3813 * if we don't have any ccbs active on this
3814 * device queue otherwise we may never get run
3815 * again.
3816 */
3817 break;
3818 }
3819
3820 /* Raise IPL for possible insertion and test at top of loop */
3821 s = splsoftcam();
3822
3823 if (drvq->entries > 0) {
3824 /* We have more work. Attempt to reschedule */
3825 xpt_schedule_dev_allocq(bus, device);
3826 }
3827 }
3828 devq->alloc_queue.qfrozen_cnt--;
3829 splx(s);
3830}
3831
3832static void
3833xpt_run_dev_sendq(struct cam_eb *bus)
3834{
3835 struct cam_devq *devq;
3836 int s;
3837
3838 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3839
3840 devq = bus->sim->devq;
3841
3842 s = splcam();
3843 devq->send_queue.qfrozen_cnt++;
3844 splx(s);
3845 s = splsoftcam();
3846 while ((devq->send_queue.entries > 0)
3847 && (devq->send_openings > 0)) {
3848 struct cam_ed_qinfo *qinfo;
3849 struct cam_ed *device;
3850 union ccb *work_ccb;
3851 struct cam_sim *sim;
3852 int ospl;
3853
3854 ospl = splcam();
3855 if (devq->send_queue.qfrozen_cnt > 1) {
3856 splx(ospl);
3857 break;
3858 }
3859
3860 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3861 CAMQ_HEAD);
3862 device = qinfo->device;
3863
3864 /*
3865 * If the device has been "frozen", don't attempt
3866 * to run it.
3867 */
3868 if (device->qfrozen_cnt > 0) {
3869 splx(ospl);
3870 continue;
3871 }
3872
3873 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3874 ("running device %p\n", device));
3875
3876 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3877 if (work_ccb == NULL) {
3878 printf("device on run queue with no ccbs???\n");
3879 splx(ospl);
3880 continue;
3881 }
3882
3883 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3884
3885 if (num_highpower <= 0) {
3886 /*
3887 * We got a high power command, but we
3888 * don't have any available slots. Freeze
3889 * the device queue until we have a slot
3890 * available.
3891 */
3892 device->qfrozen_cnt++;
3893 STAILQ_INSERT_TAIL(&highpowerq,
3894 &work_ccb->ccb_h,
3895 xpt_links.stqe);
3896
3897 splx(ospl);
3898 continue;
3899 } else {
3900 /*
3901 * Consume a high power slot while
3902 * this ccb runs.
3903 */
3904 num_highpower--;
3905 }
3906 }
3907 devq->active_dev = device;
3908 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3909
3910 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3911 splx(ospl);
3912
3913 devq->send_openings--;
3914 devq->send_active++;
3915
3916 if (device->ccbq.queue.entries > 0)
3917 xpt_schedule_dev_sendq(bus, device);
3918
3919 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3920 /*
3921 * The client wants to freeze the queue
3922 * after this CCB is sent.
3923 */
3924 ospl = splcam();
3925 device->qfrozen_cnt++;
3926 splx(ospl);
3927 }
3928
3929 splx(s);
3930
3931 /* In Target mode, the peripheral driver knows best... */
3932 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3933 if ((device->inq_flags & SID_CmdQue) != 0
3934 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3935 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3936 else
3937 /*
3938 * Clear this in case of a retried CCB that
3939 * failed due to a rejected tag.
3940 */
3941 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3942 }
3943
3944 /*
3945 * Device queues can be shared among multiple sim instances
3946 * that reside on different busses. Use the SIM in the queue
3947 * CCB's path, rather than the one in the bus that was passed
3948 * into this function.
3949 */
3950 sim = work_ccb->ccb_h.path->bus->sim;
3951 (*(sim->sim_action))(sim, work_ccb);
3952
3953 ospl = splcam();
3954 devq->active_dev = NULL;
3955 splx(ospl);
3956 /* Raise IPL for possible insertion and test at top of loop */
3957 s = splsoftcam();
3958 }
3959 splx(s);
3960 s = splcam();
3961 devq->send_queue.qfrozen_cnt--;
3962 splx(s);
3963}
3964
3965/*
3966 * This function merges stuff from the slave ccb into the master ccb, while
3967 * keeping important fields in the master ccb constant.
3968 */
3969void
3970xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3971{
3972 GIANT_REQUIRED;
3973
3974 /*
3975 * Pull fields that are valid for peripheral drivers to set
3976 * into the master CCB along with the CCB "payload".
3977 */
3978 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3979 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3980 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3981 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3982 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3983 sizeof(union ccb) - sizeof(struct ccb_hdr));
3984}
3985
3986void
3987xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3988{
3989 GIANT_REQUIRED;
3990
3991 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3992 ccb_h->pinfo.priority = priority;
3993 ccb_h->path = path;
3994 ccb_h->path_id = path->bus->path_id;
3995 if (path->target)
3996 ccb_h->target_id = path->target->target_id;
3997 else
3998 ccb_h->target_id = CAM_TARGET_WILDCARD;
3999 if (path->device) {
4000 ccb_h->target_lun = path->device->lun_id;
4001 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
4002 } else {
4003 ccb_h->target_lun = CAM_TARGET_WILDCARD;
4004 }
4005 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4006 ccb_h->flags = 0;
4007}
4008
4009/* Path manipulation functions */
4010cam_status
4011xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
4012 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4013{
4014 struct cam_path *path;
4015 cam_status status;
4016
4017 GIANT_REQUIRED;
4018
4019 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
4020
4021 if (path == NULL) {
4022 status = CAM_RESRC_UNAVAIL;
4023 return(status);
4024 }
4025 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
4026 if (status != CAM_REQ_CMP) {
4027 free(path, M_CAMXPT);
4028 path = NULL;
4029 }
4030 *new_path_ptr = path;
4031 return (status);
4032}
4033
4034static cam_status
4035xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
4036 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4037{
4038 struct cam_eb *bus;
4039 struct cam_et *target;
4040 struct cam_ed *device;
4041 cam_status status;
4042 int s;
4043
4044 status = CAM_REQ_CMP; /* Completed without error */
4045 target = NULL; /* Wildcarded */
4046 device = NULL; /* Wildcarded */
4047
4048 /*
4049 * We will potentially modify the EDT, so block interrupts
4050 * that may attempt to create cam paths.
4051 */
4052 s = splcam();
4053 bus = xpt_find_bus(path_id);
4054 if (bus == NULL) {
4055 status = CAM_PATH_INVALID;
4056 } else {
4057 target = xpt_find_target(bus, target_id);
4058 if (target == NULL) {
4059 /* Create one */
4060 struct cam_et *new_target;
4061
4062 new_target = xpt_alloc_target(bus, target_id);
4063 if (new_target == NULL) {
4064 status = CAM_RESRC_UNAVAIL;
4065 } else {
4066 target = new_target;
4067 }
4068 }
4069 if (target != NULL) {
4070 device = xpt_find_device(target, lun_id);
4071 if (device == NULL) {
4072 /* Create one */
4073 struct cam_ed *new_device;
4074
4075 new_device = xpt_alloc_device(bus,
4076 target,
4077 lun_id);
4078 if (new_device == NULL) {
4079 status = CAM_RESRC_UNAVAIL;
4080 } else {
4081 device = new_device;
4082 }
4083 }
4084 }
4085 }
4086 splx(s);
4087
4088 /*
4089 * Only touch the user's data if we are successful.
4090 */
4091 if (status == CAM_REQ_CMP) {
4092 new_path->periph = perph;
4093 new_path->bus = bus;
4094 new_path->target = target;
4095 new_path->device = device;
4096 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
4097 } else {
4098 if (device != NULL)
4099 xpt_release_device(bus, target, device);
4100 if (target != NULL)
4101 xpt_release_target(bus, target);
4102 if (bus != NULL)
4103 xpt_release_bus(bus);
4104 }
4105 return (status);
4106}
4107
4108static void
4109xpt_release_path(struct cam_path *path)
4110{
4111 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
4112 if (path->device != NULL) {
4113 xpt_release_device(path->bus, path->target, path->device);
4114 path->device = NULL;
4115 }
4116 if (path->target != NULL) {
4117 xpt_release_target(path->bus, path->target);
4118 path->target = NULL;
4119 }
4120 if (path->bus != NULL) {
4121 xpt_release_bus(path->bus);
4122 path->bus = NULL;
4123 }
4124}
4125
4126void
4127xpt_free_path(struct cam_path *path)
4128{
4129 GIANT_REQUIRED;
4130
4131 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
4132 xpt_release_path(path);
4133 free(path, M_CAMXPT);
4134}
4135
4136
4137/*
4138 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4139 * in path1, 2 for match with wildcards in path2.
4140 */
4141int
4142xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
4143{
4144 GIANT_REQUIRED;
4145
4146 int retval = 0;
4147
4148 if (path1->bus != path2->bus) {
4149 if (path1->bus->path_id == CAM_BUS_WILDCARD)
4150 retval = 1;
4151 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4152 retval = 2;
4153 else
4154 return (-1);
4155 }
4156 if (path1->target != path2->target) {
4157 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4158 if (retval == 0)
4159 retval = 1;
4160 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4161 retval = 2;
4162 else
4163 return (-1);
4164 }
4165 if (path1->device != path2->device) {
4166 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4167 if (retval == 0)
4168 retval = 1;
4169 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4170 retval = 2;
4171 else
4172 return (-1);
4173 }
4174 return (retval);
4175}
4176
4177void
4178xpt_print_path(struct cam_path *path)
4179{
4180 GIANT_REQUIRED;
4181
4182 if (path == NULL)
4183 printf("(nopath): ");
4184 else {
4185 if (path->periph != NULL)
4186 printf("(%s%d:", path->periph->periph_name,
4187 path->periph->unit_number);
4188 else
4189 printf("(noperiph:");
4190
4191 if (path->bus != NULL)
4192 printf("%s%d:%d:", path->bus->sim->sim_name,
4193 path->bus->sim->unit_number,
4194 path->bus->sim->bus_id);
4195 else
4196 printf("nobus:");
4197
4198 if (path->target != NULL)
4199 printf("%d:", path->target->target_id);
4200 else
4201 printf("X:");
4202
4203 if (path->device != NULL)
4204 printf("%d): ", path->device->lun_id);
4205 else
4206 printf("X): ");
4207 }
4208}
4209
4210int
4211xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4212{
4213 struct sbuf sb;
4214
4215 GIANT_REQUIRED;
4216
4217 sbuf_new(&sb, str, str_len, 0);
4218
4219 if (path == NULL)
4220 sbuf_printf(&sb, "(nopath): ");
4221 else {
4222 if (path->periph != NULL)
4223 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4224 path->periph->unit_number);
4225 else
4226 sbuf_printf(&sb, "(noperiph:");
4227
4228 if (path->bus != NULL)
4229 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
4230 path->bus->sim->unit_number,
4231 path->bus->sim->bus_id);
4232 else
4233 sbuf_printf(&sb, "nobus:");
4234
4235 if (path->target != NULL)
4236 sbuf_printf(&sb, "%d:", path->target->target_id);
4237 else
4238 sbuf_printf(&sb, "X:");
4239
4240 if (path->device != NULL)
4241 sbuf_printf(&sb, "%d): ", path->device->lun_id);
4242 else
4243 sbuf_printf(&sb, "X): ");
4244 }
4245 sbuf_finish(&sb);
4246
4247 return(sbuf_len(&sb));
4248}
4249
4250path_id_t
4251xpt_path_path_id(struct cam_path *path)
4252{
4253 GIANT_REQUIRED;
4254
4255 return(path->bus->path_id);
4256}
4257
4258target_id_t
4259xpt_path_target_id(struct cam_path *path)
4260{
4261 GIANT_REQUIRED;
4262
4263 if (path->target != NULL)
4264 return (path->target->target_id);
4265 else
4266 return (CAM_TARGET_WILDCARD);
4267}
4268
4269lun_id_t
4270xpt_path_lun_id(struct cam_path *path)
4271{
4272 GIANT_REQUIRED;
4273
4274 if (path->device != NULL)
4275 return (path->device->lun_id);
4276 else
4277 return (CAM_LUN_WILDCARD);
4278}
4279
4280struct cam_sim *
4281xpt_path_sim(struct cam_path *path)
4282{
4283 GIANT_REQUIRED;
4284
4285 return (path->bus->sim);
4286}
4287
4288struct cam_periph*
4289xpt_path_periph(struct cam_path *path)
4290{
4291 GIANT_REQUIRED;
4292
4293 return (path->periph);
4294}
4295
4296/*
4297 * Release a CAM control block for the caller. Remit the cost of the structure
4298 * to the device referenced by the path. If the this device had no 'credits'
4299 * and peripheral drivers have registered async callbacks for this notification
4300 * call them now.
4301 */
4302void
4303xpt_release_ccb(union ccb *free_ccb)
4304{
4305 int s;
4306 struct cam_path *path;
4307 struct cam_ed *device;
4308 struct cam_eb *bus;
4309
4310 GIANT_REQUIRED;
4311
4312 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4313 path = free_ccb->ccb_h.path;
4314 device = path->device;
4315 bus = path->bus;
4316 s = splsoftcam();
4317 cam_ccbq_release_opening(&device->ccbq);
4318 if (xpt_ccb_count > xpt_max_ccbs) {
4319 xpt_free_ccb(free_ccb);
4320 xpt_ccb_count--;
4321 } else {
4322 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4323 }
4324 bus->sim->devq->alloc_openings++;
4325 bus->sim->devq->alloc_active--;
4326 /* XXX Turn this into an inline function - xpt_run_device?? */
4327 if ((device_is_alloc_queued(device) == 0)
4328 && (device->drvq.entries > 0)) {
4329 xpt_schedule_dev_allocq(bus, device);
4330 }
4331 splx(s);
4332 if (dev_allocq_is_runnable(bus->sim->devq))
4333 xpt_run_dev_allocq(bus);
4334}
4335
4336/* Functions accessed by SIM drivers */
4337
4338/*
4339 * A sim structure, listing the SIM entry points and instance
4340 * identification info is passed to xpt_bus_register to hook the SIM
4341 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4342 * for this new bus and places it in the array of busses and assigns
4343 * it a path_id. The path_id may be influenced by "hard wiring"
4344 * information specified by the user. Once interrupt services are
4345 * availible, the bus will be probed.
4346 */
4347int32_t
4348xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4349{
4350 struct cam_eb *new_bus;
4351 struct cam_eb *old_bus;
4352 struct ccb_pathinq cpi;
4353 int s;
4354
4355 GIANT_REQUIRED;
4356
4357 sim->bus_id = bus;
4358 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4359 M_CAMXPT, M_NOWAIT);
4360 if (new_bus == NULL) {
4361 /* Couldn't satisfy request */
4362 return (CAM_RESRC_UNAVAIL);
4363 }
4364
4365 if (strcmp(sim->sim_name, "xpt") != 0) {
4366
4367 sim->path_id =
4368 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4369 }
4370
4371 TAILQ_INIT(&new_bus->et_entries);
4372 new_bus->path_id = sim->path_id;
4373 new_bus->sim = sim;
4374 timevalclear(&new_bus->last_reset);
4375 new_bus->flags = 0;
4376 new_bus->refcount = 1; /* Held until a bus_deregister event */
4377 new_bus->generation = 0;
4378 s = splcam();
4379 old_bus = TAILQ_FIRST(&xpt_busses);
4380 while (old_bus != NULL
4381 && old_bus->path_id < new_bus->path_id)
4382 old_bus = TAILQ_NEXT(old_bus, links);
4383 if (old_bus != NULL)
4384 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4385 else
4386 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4387 bus_generation++;
4388 splx(s);
4389
4390 /* Notify interested parties */
4391 if (sim->path_id != CAM_XPT_PATH_ID) {
4392 struct cam_path path;
4393
4394 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4395 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4396 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4397 cpi.ccb_h.func_code = XPT_PATH_INQ;
4398 xpt_action((union ccb *)&cpi);
4399 xpt_async(AC_PATH_REGISTERED, &path, &cpi);
4400 xpt_release_path(&path);
4401 }
4402 return (CAM_SUCCESS);
4403}
4404
4405int32_t
4406xpt_bus_deregister(path_id_t pathid)
4407{
4408 struct cam_path bus_path;
4409 cam_status status;
4410
4411 GIANT_REQUIRED;
4412
4413 status = xpt_compile_path(&bus_path, NULL, pathid,
4414 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4415 if (status != CAM_REQ_CMP)
4416 return (status);
4417
4418 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4419 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4420
4421 /* Release the reference count held while registered. */
4422 xpt_release_bus(bus_path.bus);
4423 xpt_release_path(&bus_path);
4424
4425 return (CAM_REQ_CMP);
4426}
4427
4428static path_id_t
4429xptnextfreepathid(void)
4430{
4431 struct cam_eb *bus;
4432 path_id_t pathid;
4433 const char *strval;
4434
4435 pathid = 0;
4436 bus = TAILQ_FIRST(&xpt_busses);
4437retry:
4438 /* Find an unoccupied pathid */
4439 while (bus != NULL
4440 && bus->path_id <= pathid) {
4441 if (bus->path_id == pathid)
4442 pathid++;
4443 bus = TAILQ_NEXT(bus, links);
4444 }
4445
4446 /*
4447 * Ensure that this pathid is not reserved for
4448 * a bus that may be registered in the future.
4449 */
4450 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4451 ++pathid;
4452 /* Start the search over */
4453 goto retry;
4454 }
4455 return (pathid);
4456}
4457
4458static path_id_t
4459xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4460{
4461 path_id_t pathid;
4462 int i, dunit, val;
4463 char buf[32];
4464 const char *dname;
4465
4466 pathid = CAM_XPT_PATH_ID;
4467 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4468 i = 0;
4469 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4470 if (strcmp(dname, "scbus")) {
4471 /* Avoid a bit of foot shooting. */
4472 continue;
4473 }
4474 if (dunit < 0) /* unwired?! */
4475 continue;
4476 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4477 if (sim_bus == val) {
4478 pathid = dunit;
4479 break;
4480 }
4481 } else if (sim_bus == 0) {
4482 /* Unspecified matches bus 0 */
4483 pathid = dunit;
4484 break;
4485 } else {
4486 printf("Ambiguous scbus configuration for %s%d "
4487 "bus %d, cannot wire down. The kernel "
4488 "config entry for scbus%d should "
4489 "specify a controller bus.\n"
4490 "Scbus will be assigned dynamically.\n",
4491 sim_name, sim_unit, sim_bus, dunit);
4492 break;
4493 }
4494 }
4495
4496 if (pathid == CAM_XPT_PATH_ID)
4497 pathid = xptnextfreepathid();
4498 return (pathid);
4499}
4500
4501void
4502xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4503{
4504 struct cam_eb *bus;
4505 struct cam_et *target, *next_target;
4506 struct cam_ed *device, *next_device;
4507 int s;
4508
4509 GIANT_REQUIRED;
4510
4511 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4512
4513 /*
4514 * Most async events come from a CAM interrupt context. In
4515 * a few cases, the error recovery code at the peripheral layer,
4516 * which may run from our SWI or a process context, may signal
4517 * deferred events with a call to xpt_async. Ensure async
4518 * notifications are serialized by blocking cam interrupts.
4519 */
4520 s = splcam();
4521
4522 bus = path->bus;
4523
4524 if (async_code == AC_BUS_RESET) {
4525 int s;
4526
4527 s = splclock();
4528 /* Update our notion of when the last reset occurred */
4529 microtime(&bus->last_reset);
4530 splx(s);
4531 }
4532
4533 for (target = TAILQ_FIRST(&bus->et_entries);
4534 target != NULL;
4535 target = next_target) {
4536
4537 next_target = TAILQ_NEXT(target, links);
4538
4539 if (path->target != target
4540 && path->target->target_id != CAM_TARGET_WILDCARD
4541 && target->target_id != CAM_TARGET_WILDCARD)
4542 continue;
4543
4544 if (async_code == AC_SENT_BDR) {
4545 int s;
4546
4547 /* Update our notion of when the last reset occurred */
4548 s = splclock();
4549 microtime(&path->target->last_reset);
4550 splx(s);
4551 }
4552
4553 for (device = TAILQ_FIRST(&target->ed_entries);
4554 device != NULL;
4555 device = next_device) {
4556
4557 next_device = TAILQ_NEXT(device, links);
4558
4559 if (path->device != device
4560 && path->device->lun_id != CAM_LUN_WILDCARD
4561 && device->lun_id != CAM_LUN_WILDCARD)
4562 continue;
4563
4564 xpt_dev_async(async_code, bus, target,
4565 device, async_arg);
4566
4567 xpt_async_bcast(&device->asyncs, async_code,
4568 path, async_arg);
4569 }
4570 }
4571
4572 /*
4573 * If this wasn't a fully wildcarded async, tell all
4574 * clients that want all async events.
4575 */
4576 if (bus != xpt_periph->path->bus)
4577 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4578 path, async_arg);
4579 splx(s);
4580}
4581
4582static void
4583xpt_async_bcast(struct async_list *async_head,
4584 u_int32_t async_code,
4585 struct cam_path *path, void *async_arg)
4586{
4587 struct async_node *cur_entry;
4588
4589 cur_entry = SLIST_FIRST(async_head);
4590 while (cur_entry != NULL) {
4591 struct async_node *next_entry;
4592 /*
4593 * Grab the next list entry before we call the current
4594 * entry's callback. This is because the callback function
4595 * can delete its async callback entry.
4596 */
4597 next_entry = SLIST_NEXT(cur_entry, links);
4598 if ((cur_entry->event_enable & async_code) != 0)
4599 cur_entry->callback(cur_entry->callback_arg,
4600 async_code, path,
4601 async_arg);
4602 cur_entry = next_entry;
4603 }
4604}
4605
4606/*
4607 * Handle any per-device event notifications that require action by the XPT.
4608 */
4609static void
4610xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4611 struct cam_ed *device, void *async_arg)
4612{
4613 cam_status status;
4614 struct cam_path newpath;
4615
4616 /*
4617 * We only need to handle events for real devices.
4618 */
4619 if (target->target_id == CAM_TARGET_WILDCARD
4620 || device->lun_id == CAM_LUN_WILDCARD)
4621 return;
4622
4623 /*
4624 * We need our own path with wildcards expanded to
4625 * handle certain types of events.
4626 */
4627 if ((async_code == AC_SENT_BDR)
4628 || (async_code == AC_BUS_RESET)
4629 || (async_code == AC_INQ_CHANGED))
4630 status = xpt_compile_path(&newpath, NULL,
4631 bus->path_id,
4632 target->target_id,
4633 device->lun_id);
4634 else
4635 status = CAM_REQ_CMP_ERR;
4636
4637 if (status == CAM_REQ_CMP) {
4638
4639 /*
4640 * Allow transfer negotiation to occur in a
4641 * tag free environment.
4642 */
4643 if (async_code == AC_SENT_BDR
4644 || async_code == AC_BUS_RESET)
4645 xpt_toggle_tags(&newpath);
4646
4647 if (async_code == AC_INQ_CHANGED) {
4648 /*
4649 * We've sent a start unit command, or
4650 * something similar to a device that
4651 * may have caused its inquiry data to
4652 * change. So we re-scan the device to
4653 * refresh the inquiry data for it.
4654 */
4655 xpt_scan_lun(newpath.periph, &newpath,
4656 CAM_EXPECT_INQ_CHANGE, NULL);
4657 }
4658 xpt_release_path(&newpath);
4659 } else if (async_code == AC_LOST_DEVICE) {
4660 device->flags |= CAM_DEV_UNCONFIGURED;
4661 } else if (async_code == AC_TRANSFER_NEG) {
4662 struct ccb_trans_settings *settings;
4663
4664 settings = (struct ccb_trans_settings *)async_arg;
4665 xpt_set_transfer_settings(settings, device,
4666 /*async_update*/TRUE);
4667 }
4668}
4669
4670u_int32_t
4671xpt_freeze_devq(struct cam_path *path, u_int count)
4672{
4673 int s;
4674 struct ccb_hdr *ccbh;
4675
4676 GIANT_REQUIRED;
4677
4678 s = splcam();
4679 path->device->qfrozen_cnt += count;
4680
4681 /*
4682 * Mark the last CCB in the queue as needing
4683 * to be requeued if the driver hasn't
4684 * changed it's state yet. This fixes a race
4685 * where a ccb is just about to be queued to
4686 * a controller driver when it's interrupt routine
4687 * freezes the queue. To completly close the
4688 * hole, controller drives must check to see
4689 * if a ccb's status is still CAM_REQ_INPROG
4690 * under spl protection just before they queue
4691 * the CCB. See ahc_action/ahc_freeze_devq for
4692 * an example.
4693 */
4694 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4695 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4696 ccbh->status = CAM_REQUEUE_REQ;
4697 splx(s);
4698 return (path->device->qfrozen_cnt);
4699}
4700
4701u_int32_t
4702xpt_freeze_simq(struct cam_sim *sim, u_int count)
4703{
4704 GIANT_REQUIRED;
4705
4706 sim->devq->send_queue.qfrozen_cnt += count;
4707 if (sim->devq->active_dev != NULL) {
4708 struct ccb_hdr *ccbh;
4709
4710 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4711 ccb_hdr_tailq);
4712 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4713 ccbh->status = CAM_REQUEUE_REQ;
4714 }
4715 return (sim->devq->send_queue.qfrozen_cnt);
4716}
4717
4718static void
4719xpt_release_devq_timeout(void *arg)
4720{
4721 struct cam_ed *device;
4722
4723 device = (struct cam_ed *)arg;
4724
4725 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4726}
4727
4728void
4729xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4730{
4731 GIANT_REQUIRED;
4732
4733 xpt_release_devq_device(path->device, count, run_queue);
4734}
4735
4736static void
4737xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4738{
4739 int rundevq;
4740 int s0, s1;
4741
4742 rundevq = 0;
4743 s0 = splsoftcam();
4744 s1 = splcam();
4745 if (dev->qfrozen_cnt > 0) {
4746
4747 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4748 dev->qfrozen_cnt -= count;
4749 if (dev->qfrozen_cnt == 0) {
4750
4751 /*
4752 * No longer need to wait for a successful
4753 * command completion.
4754 */
4755 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4756
4757 /*
4758 * Remove any timeouts that might be scheduled
4759 * to release this queue.
4760 */
4761 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4762 untimeout(xpt_release_devq_timeout, dev,
4763 dev->c_handle);
4764 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4765 }
4766
4767 /*
4768 * Now that we are unfrozen schedule the
4769 * device so any pending transactions are
4770 * run.
4771 */
4772 if ((dev->ccbq.queue.entries > 0)
4773 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4774 && (run_queue != 0)) {
4775 rundevq = 1;
4776 }
4777 }
4778 }
4779 splx(s1);
4780 if (rundevq != 0)
4781 xpt_run_dev_sendq(dev->target->bus);
4782 splx(s0);
4783}
4784
4785void
4786xpt_release_simq(struct cam_sim *sim, int run_queue)
4787{
4788 int s;
4789 struct camq *sendq;
4790
4791 GIANT_REQUIRED;
4792
4793 sendq = &(sim->devq->send_queue);
4794 s = splcam();
4795 if (sendq->qfrozen_cnt > 0) {
4796
4797 sendq->qfrozen_cnt--;
4798 if (sendq->qfrozen_cnt == 0) {
4799 struct cam_eb *bus;
4800
4801 /*
4802 * If there is a timeout scheduled to release this
4803 * sim queue, remove it. The queue frozen count is
4804 * already at 0.
4805 */
4806 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4807 untimeout(xpt_release_simq_timeout, sim,
4808 sim->c_handle);
4809 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4810 }
4811 bus = xpt_find_bus(sim->path_id);
4812 splx(s);
4813
4814 if (run_queue) {
4815 /*
4816 * Now that we are unfrozen run the send queue.
4817 */
4818 xpt_run_dev_sendq(bus);
4819 }
4820 xpt_release_bus(bus);
4821 } else
4822 splx(s);
4823 } else
4824 splx(s);
4825}
4826
4827static void
4828xpt_release_simq_timeout(void *arg)
4829{
4830 struct cam_sim *sim;
4831
4832 sim = (struct cam_sim *)arg;
4833 xpt_release_simq(sim, /* run_queue */ TRUE);
4834}
4835
4836void
4837xpt_done(union ccb *done_ccb)
4838{
4839 int s;
4840
4841 s = splcam();
4842
4843 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4844 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4845 /*
4846 * Queue up the request for handling by our SWI handler
4847 * any of the "non-immediate" type of ccbs.
4848 */
4849 switch (done_ccb->ccb_h.path->periph->type) {
4850 case CAM_PERIPH_BIO:
4851 mtx_lock(&cam_bioq_lock);
4852 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4853 sim_links.tqe);
4854 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4855 mtx_unlock(&cam_bioq_lock);
4856 swi_sched(cambio_ih, 0);
4857 break;
4858 default:
4859 panic("unknown periph type %d",
4860 done_ccb->ccb_h.path->periph->type);
4861 }
4862 }
4863 splx(s);
4864}
4865
4866union ccb *
4867xpt_alloc_ccb()
4868{
4869 union ccb *new_ccb;
4870
4871 GIANT_REQUIRED;
4872
4873 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_WAITOK);
4874 return (new_ccb);
4875}
4876
4877union ccb *
4878xpt_alloc_ccb_nowait()
4879{
4880 union ccb *new_ccb;
4881
4882 GIANT_REQUIRED;
4883
4884 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_NOWAIT);
4885 return (new_ccb);
4886}
4887
4888void
4889xpt_free_ccb(union ccb *free_ccb)
4890{
4891 free(free_ccb, M_CAMXPT);
4892}
4893
4894
4895
4896/* Private XPT functions */
4897
4898/*
4899 * Get a CAM control block for the caller. Charge the structure to the device
4900 * referenced by the path. If the this device has no 'credits' then the
4901 * device already has the maximum number of outstanding operations under way
4902 * and we return NULL. If we don't have sufficient resources to allocate more
4903 * ccbs, we also return NULL.
4904 */
4905static union ccb *
4906xpt_get_ccb(struct cam_ed *device)
4907{
4908 union ccb *new_ccb;
4909 int s;
4910
4911 s = splsoftcam();
4912 if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
4913 new_ccb = xpt_alloc_ccb_nowait();
4914 if (new_ccb == NULL) {
4915 splx(s);
4916 return (NULL);
4917 }
4918 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4919 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4920 xpt_links.sle);
4921 xpt_ccb_count++;
4922 }
4923 cam_ccbq_take_opening(&device->ccbq);
4924 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4925 splx(s);
4926 return (new_ccb);
4927}
4928
4929static void
4930xpt_release_bus(struct cam_eb *bus)
4931{
4932 int s;
4933
4934 s = splcam();
4935 if ((--bus->refcount == 0)
4936 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4937 TAILQ_REMOVE(&xpt_busses, bus, links);
4938 bus_generation++;
4939 splx(s);
4940 free(bus, M_CAMXPT);
4941 } else
4942 splx(s);
4943}
4944
4945static struct cam_et *
4946xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4947{
4948 struct cam_et *target;
4949
4950 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
4951 if (target != NULL) {
4952 struct cam_et *cur_target;
4953
4954 TAILQ_INIT(&target->ed_entries);
4955 target->bus = bus;
4956 target->target_id = target_id;
4957 target->refcount = 1;
4958 target->generation = 0;
4959 timevalclear(&target->last_reset);
4960 /*
4961 * Hold a reference to our parent bus so it
4962 * will not go away before we do.
4963 */
4964 bus->refcount++;
4965
4966 /* Insertion sort into our bus's target list */
4967 cur_target = TAILQ_FIRST(&bus->et_entries);
4968 while (cur_target != NULL && cur_target->target_id < target_id)
4969 cur_target = TAILQ_NEXT(cur_target, links);
4970
4971 if (cur_target != NULL) {
4972 TAILQ_INSERT_BEFORE(cur_target, target, links);
4973 } else {
4974 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4975 }
4976 bus->generation++;
4977 }
4978 return (target);
4979}
4980
4981static void
4982xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4983{
4984 int s;
4985
4986 s = splcam();
4987 if ((--target->refcount == 0)
4988 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4989 TAILQ_REMOVE(&bus->et_entries, target, links);
4990 bus->generation++;
4991 splx(s);
4992 free(target, M_CAMXPT);
4993 xpt_release_bus(bus);
4994 } else
4995 splx(s);
4996}
4997
4998static struct cam_ed *
4999xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
5000{
5001#ifdef CAM_NEW_TRAN_CODE
5002 struct cam_path path;
5003#endif /* CAM_NEW_TRAN_CODE */
5004 struct cam_ed *device;
5005 struct cam_devq *devq;
5006 cam_status status;
5007
5008 /* Make space for us in the device queue on our bus */
5009 devq = bus->sim->devq;
5010 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
5011
5012 if (status != CAM_REQ_CMP) {
5013 device = NULL;
5014 } else {
5015 device = (struct cam_ed *)malloc(sizeof(*device),
5016 M_CAMXPT, M_NOWAIT);
5017 }
5018
5019 if (device != NULL) {
5020 struct cam_ed *cur_device;
5021
5022 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
5023 device->alloc_ccb_entry.device = device;
5024 cam_init_pinfo(&device->send_ccb_entry.pinfo);
5025 device->send_ccb_entry.device = device;
5026 device->target = target;
5027 device->lun_id = lun_id;
5028 /* Initialize our queues */
5029 if (camq_init(&device->drvq, 0) != 0) {
5030 free(device, M_CAMXPT);
5031 return (NULL);
5032 }
5033 if (cam_ccbq_init(&device->ccbq,
5034 bus->sim->max_dev_openings) != 0) {
5035 camq_fini(&device->drvq);
5036 free(device, M_CAMXPT);
5037 return (NULL);
5038 }
5039 SLIST_INIT(&device->asyncs);
5040 SLIST_INIT(&device->periphs);
5041 device->generation = 0;
5042 device->owner = NULL;
5043 /*
5044 * Take the default quirk entry until we have inquiry
5045 * data and can determine a better quirk to use.
5046 */
5047 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
5048 bzero(&device->inq_data, sizeof(device->inq_data));
5049 device->inq_flags = 0;
5050 device->queue_flags = 0;
5051 device->serial_num = NULL;
5052 device->serial_num_len = 0;
5053 device->qfrozen_cnt = 0;
5054 device->flags = CAM_DEV_UNCONFIGURED;
5055 device->tag_delay_count = 0;
5056 device->tag_saved_openings = 0;
5057 device->refcount = 1;
5058 callout_handle_init(&device->c_handle);
5059
5060 /*
5061 * Hold a reference to our parent target so it
5062 * will not go away before we do.
5063 */
5064 target->refcount++;
5065
5066 /*
5067 * XXX should be limited by number of CCBs this bus can
5068 * do.
5069 */
5070 xpt_max_ccbs += device->ccbq.devq_openings;
5071 /* Insertion sort into our target's device list */
5072 cur_device = TAILQ_FIRST(&target->ed_entries);
5073 while (cur_device != NULL && cur_device->lun_id < lun_id)
5074 cur_device = TAILQ_NEXT(cur_device, links);
5075 if (cur_device != NULL) {
5076 TAILQ_INSERT_BEFORE(cur_device, device, links);
5077 } else {
5078 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
5079 }
5080 target->generation++;
5081#ifdef CAM_NEW_TRAN_CODE
5082 if (lun_id != CAM_LUN_WILDCARD) {
5083 xpt_compile_path(&path,
5084 NULL,
5085 bus->path_id,
5086 target->target_id,
5087 lun_id);
5088 xpt_devise_transport(&path);
5089 xpt_release_path(&path);
5090 }
5091#endif /* CAM_NEW_TRAN_CODE */
5092 }
5093 return (device);
5094}
5095
5096static void
5097xpt_release_device(struct cam_eb *bus, struct cam_et *target,
5098 struct cam_ed *device)
5099{
5100 int s;
5101
5102 s = splcam();
5103 if ((--device->refcount == 0)
5104 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
5105 struct cam_devq *devq;
5106
5107 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
5108 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
5109 panic("Removing device while still queued for ccbs");
5110
5111 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
5112 untimeout(xpt_release_devq_timeout, device,
5113 device->c_handle);
5114
5115 TAILQ_REMOVE(&target->ed_entries, device,links);
5116 target->generation++;
5117 xpt_max_ccbs -= device->ccbq.devq_openings;
5118 /* Release our slot in the devq */
5119 devq = bus->sim->devq;
5120 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
5121 splx(s);
5122 camq_fini(&device->drvq);
5123 camq_fini(&device->ccbq.queue);
5124 free(device, M_CAMXPT);
5125 xpt_release_target(bus, target);
5126 } else
5127 splx(s);
5128}
5129
5130static u_int32_t
5131xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5132{
5133 int s;
5134 int diff;
5135 int result;
5136 struct cam_ed *dev;
5137
5138 dev = path->device;
5139 s = splsoftcam();
5140
5141 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
5142 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5143 if (result == CAM_REQ_CMP && (diff < 0)) {
5144 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
5145 }
5146 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5147 || (dev->inq_flags & SID_CmdQue) != 0)
5148 dev->tag_saved_openings = newopenings;
5149 /* Adjust the global limit */
5150 xpt_max_ccbs += diff;
5151 splx(s);
5152 return (result);
5153}
5154
5155static struct cam_eb *
5156xpt_find_bus(path_id_t path_id)
5157{
5158 struct cam_eb *bus;
5159
5160 for (bus = TAILQ_FIRST(&xpt_busses);
5161 bus != NULL;
5162 bus = TAILQ_NEXT(bus, links)) {
5163 if (bus->path_id == path_id) {
5164 bus->refcount++;
5165 break;
5166 }
5167 }
5168 return (bus);
5169}
5170
5171static struct cam_et *
5172xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5173{
5174 struct cam_et *target;
5175
5176 for (target = TAILQ_FIRST(&bus->et_entries);
5177 target != NULL;
5178 target = TAILQ_NEXT(target, links)) {
5179 if (target->target_id == target_id) {
5180 target->refcount++;
5181 break;
5182 }
5183 }
5184 return (target);
5185}
5186
5187static struct cam_ed *
5188xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5189{
5190 struct cam_ed *device;
5191
5192 for (device = TAILQ_FIRST(&target->ed_entries);
5193 device != NULL;
5194 device = TAILQ_NEXT(device, links)) {
5195 if (device->lun_id == lun_id) {
5196 device->refcount++;
5197 break;
5198 }
5199 }
5200 return (device);
5201}
5202
5203typedef struct {
5204 union ccb *request_ccb;
5205 struct ccb_pathinq *cpi;
5206 int pending_count;
5207} xpt_scan_bus_info;
5208
5209/*
5210 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5211 * As the scan progresses, xpt_scan_bus is used as the
5212 * callback on completion function.
5213 */
5214static void
5215xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
5216{
5217 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5218 ("xpt_scan_bus\n"));
5219 switch (request_ccb->ccb_h.func_code) {
5220 case XPT_SCAN_BUS:
5221 {
5222 xpt_scan_bus_info *scan_info;
5223 union ccb *work_ccb;
5224 struct cam_path *path;
5225 u_int i;
5226 u_int max_target;
5227 u_int initiator_id;
5228
5229 /* Find out the characteristics of the bus */
5230 work_ccb = xpt_alloc_ccb();
5231 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
5232 request_ccb->ccb_h.pinfo.priority);
5233 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5234 xpt_action(work_ccb);
5235 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5236 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
5237 xpt_free_ccb(work_ccb);
5238 xpt_done(request_ccb);
5239 return;
5240 }
5241
5242 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5243 /*
5244 * Can't scan the bus on an adapter that
5245 * cannot perform the initiator role.
5246 */
5247 request_ccb->ccb_h.status = CAM_REQ_CMP;
5248 xpt_free_ccb(work_ccb);
5249 xpt_done(request_ccb);
5250 return;
5251 }
5252
5253 /* Save some state for use while we probe for devices */
5254 scan_info = (xpt_scan_bus_info *)
5255 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
5256 scan_info->request_ccb = request_ccb;
5257 scan_info->cpi = &work_ccb->cpi;
5258
5259 /* Cache on our stack so we can work asynchronously */
5260 max_target = scan_info->cpi->max_target;
5261 initiator_id = scan_info->cpi->initiator_id;
5262
5263 /*
5264 * Don't count the initiator if the
5265 * initiator is addressable.
5266 */
5267 scan_info->pending_count = max_target + 1;
5268 if (initiator_id <= max_target)
5269 scan_info->pending_count--;
5270
5271 for (i = 0; i <= max_target; i++) {
5272 cam_status status;
5273 if (i == initiator_id)
5274 continue;
5275
5276 status = xpt_create_path(&path, xpt_periph,
5277 request_ccb->ccb_h.path_id,
5278 i, 0);
5279 if (status != CAM_REQ_CMP) {
5280 printf("xpt_scan_bus: xpt_create_path failed"
5281 " with status %#x, bus scan halted\n",
5282 status);
5283 break;
5284 }
5285 work_ccb = xpt_alloc_ccb();
5286 xpt_setup_ccb(&work_ccb->ccb_h, path,
5287 request_ccb->ccb_h.pinfo.priority);
5288 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5289 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5290 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
5291 work_ccb->crcn.flags = request_ccb->crcn.flags;
5292 xpt_action(work_ccb);
5293 }
5294 break;
5295 }
5296 case XPT_SCAN_LUN:
5297 {
5298 xpt_scan_bus_info *scan_info;
5299 path_id_t path_id;
5300 target_id_t target_id;
5301 lun_id_t lun_id;
5302
5303 /* Reuse the same CCB to query if a device was really found */
5304 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
5305 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
5306 request_ccb->ccb_h.pinfo.priority);
5307 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5308
5309 path_id = request_ccb->ccb_h.path_id;
5310 target_id = request_ccb->ccb_h.target_id;
5311 lun_id = request_ccb->ccb_h.target_lun;
5312 xpt_action(request_ccb);
5313
5314 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5315 struct cam_ed *device;
5316 struct cam_et *target;
5317 int s, phl;
5318
5319 /*
5320 * If we already probed lun 0 successfully, or
5321 * we have additional configured luns on this
5322 * target that might have "gone away", go onto
5323 * the next lun.
5324 */
5325 target = request_ccb->ccb_h.path->target;
5326 /*
5327 * We may touch devices that we don't
5328 * hold references too, so ensure they
5329 * don't disappear out from under us.
5330 * The target above is referenced by the
5331 * path in the request ccb.
5332 */
5333 phl = 0;
5334 s = splcam();
5335 device = TAILQ_FIRST(&target->ed_entries);
5336 if (device != NULL) {
5337 phl = CAN_SRCH_HI(device);
5338 if (device->lun_id == 0)
5339 device = TAILQ_NEXT(device, links);
5340 }
5341 splx(s);
5342 if ((lun_id != 0) || (device != NULL)) {
5343 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5344 lun_id++;
5345 }
5346 } else {
5347 struct cam_ed *device;
5348
5349 device = request_ccb->ccb_h.path->device;
5350
5351 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5352 /* Try the next lun */
5353 if (lun_id < (CAM_SCSI2_MAXLUN-1)
5354 || CAN_SRCH_HI(device))
5355 lun_id++;
5356 }
5357 }
5358
5359 xpt_free_path(request_ccb->ccb_h.path);
5360
5361 /* Check Bounds */
5362 if ((lun_id == request_ccb->ccb_h.target_lun)
5363 || lun_id > scan_info->cpi->max_lun) {
5364 /* We're done */
5365
5366 xpt_free_ccb(request_ccb);
5367 scan_info->pending_count--;
5368 if (scan_info->pending_count == 0) {
5369 xpt_free_ccb((union ccb *)scan_info->cpi);
5370 request_ccb = scan_info->request_ccb;
5371 free(scan_info, M_TEMP);
5372 request_ccb->ccb_h.status = CAM_REQ_CMP;
5373 xpt_done(request_ccb);
5374 }
5375 } else {
5376 /* Try the next device */
5377 struct cam_path *path;
5378 cam_status status;
5379
5380 status = xpt_create_path(&path, xpt_periph,
5381 path_id, target_id, lun_id);
5382 if (status != CAM_REQ_CMP) {
5383 printf("xpt_scan_bus: xpt_create_path failed "
5384 "with status %#x, halting LUN scan\n",
5385 status);
5386 xpt_free_ccb(request_ccb);
5387 scan_info->pending_count--;
5388 if (scan_info->pending_count == 0) {
5389 xpt_free_ccb(
5390 (union ccb *)scan_info->cpi);
5391 request_ccb = scan_info->request_ccb;
5392 free(scan_info, M_TEMP);
5393 request_ccb->ccb_h.status = CAM_REQ_CMP;
5394 xpt_done(request_ccb);
5395 }
5396 break;
5397 }
5398 xpt_setup_ccb(&request_ccb->ccb_h, path,
5399 request_ccb->ccb_h.pinfo.priority);
5400 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5401 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5402 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5403 request_ccb->crcn.flags =
5404 scan_info->request_ccb->crcn.flags;
5405 xpt_action(request_ccb);
5406 }
5407 break;
5408 }
5409 default:
5410 break;
5411 }
5412}
5413
5414typedef enum {
5415 PROBE_TUR,
5416 PROBE_INQUIRY,
5417 PROBE_FULL_INQUIRY,
5418 PROBE_MODE_SENSE,
5419 PROBE_SERIAL_NUM,
5420 PROBE_TUR_FOR_NEGOTIATION
5421} probe_action;
5422
5423typedef enum {
5424 PROBE_INQUIRY_CKSUM = 0x01,
5425 PROBE_SERIAL_CKSUM = 0x02,
5426 PROBE_NO_ANNOUNCE = 0x04
5427} probe_flags;
5428
5429typedef struct {
5430 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5431 probe_action action;
5432 union ccb saved_ccb;
5433 probe_flags flags;
5434 MD5_CTX context;
5435 u_int8_t digest[16];
5436} probe_softc;
5437
5438static void
5439xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5440 cam_flags flags, union ccb *request_ccb)
5441{
5442 struct ccb_pathinq cpi;
5443 cam_status status;
5444 struct cam_path *new_path;
5445 struct cam_periph *old_periph;
5446 int s;
5447
5448 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5449 ("xpt_scan_lun\n"));
5450
5451 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5452 cpi.ccb_h.func_code = XPT_PATH_INQ;
5453 xpt_action((union ccb *)&cpi);
5454
5455 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5456 if (request_ccb != NULL) {
5457 request_ccb->ccb_h.status = cpi.ccb_h.status;
5458 xpt_done(request_ccb);
5459 }
5460 return;
5461 }
5462
5463 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5464 /*
5465 * Can't scan the bus on an adapter that
5466 * cannot perform the initiator role.
5467 */
5468 if (request_ccb != NULL) {
5469 request_ccb->ccb_h.status = CAM_REQ_CMP;
5470 xpt_done(request_ccb);
5471 }
5472 return;
5473 }
5474
5475 if (request_ccb == NULL) {
5476 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5477 if (request_ccb == NULL) {
5478 xpt_print_path(path);
5479 printf("xpt_scan_lun: can't allocate CCB, can't "
5480 "continue\n");
5481 return;
5482 }
5483 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5484 if (new_path == NULL) {
5485 xpt_print_path(path);
5486 printf("xpt_scan_lun: can't allocate path, can't "
5487 "continue\n");
5488 free(request_ccb, M_TEMP);
5489 return;
5490 }
5491 status = xpt_compile_path(new_path, xpt_periph,
5492 path->bus->path_id,
5493 path->target->target_id,
5494 path->device->lun_id);
5495
5496 if (status != CAM_REQ_CMP) {
5497 xpt_print_path(path);
5498 printf("xpt_scan_lun: can't compile path, can't "
5499 "continue\n");
5500 free(request_ccb, M_TEMP);
5501 free(new_path, M_TEMP);
5502 return;
5503 }
5504 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5505 request_ccb->ccb_h.cbfcnp = xptscandone;
5506 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5507 request_ccb->crcn.flags = flags;
5508 }
5509
5510 s = splsoftcam();
5511 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5512 probe_softc *softc;
5513
5514 softc = (probe_softc *)old_periph->softc;
5515 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5516 periph_links.tqe);
5517 } else {
5518 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5519 probestart, "probe",
5520 CAM_PERIPH_BIO,
5521 request_ccb->ccb_h.path, NULL, 0,
5522 request_ccb);
5523
5524 if (status != CAM_REQ_CMP) {
5525 xpt_print_path(path);
5526 printf("xpt_scan_lun: cam_alloc_periph returned an "
5527 "error, can't continue probe\n");
5528 request_ccb->ccb_h.status = status;
5529 xpt_done(request_ccb);
5530 }
5531 }
5532 splx(s);
5533}
5534
5535static void
5536xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5537{
5538 xpt_release_path(done_ccb->ccb_h.path);
5539 free(done_ccb->ccb_h.path, M_TEMP);
5540 free(done_ccb, M_TEMP);
5541}
5542
5543static cam_status
5544proberegister(struct cam_periph *periph, void *arg)
5545{
5546 union ccb *request_ccb; /* CCB representing the probe request */
5547 probe_softc *softc;
5548
5549 request_ccb = (union ccb *)arg;
5550 if (periph == NULL) {
5551 printf("proberegister: periph was NULL!!\n");
5552 return(CAM_REQ_CMP_ERR);
5553 }
5554
5555 if (request_ccb == NULL) {
5556 printf("proberegister: no probe CCB, "
5557 "can't register device\n");
5558 return(CAM_REQ_CMP_ERR);
5559 }
5560
5561 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5562
5563 if (softc == NULL) {
5564 printf("proberegister: Unable to probe new device. "
5565 "Unable to allocate softc\n");
5566 return(CAM_REQ_CMP_ERR);
5567 }
5568 TAILQ_INIT(&softc->request_ccbs);
5569 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5570 periph_links.tqe);
5571 softc->flags = 0;
5572 periph->softc = softc;
5573 cam_periph_acquire(periph);
5574 /*
5575 * Ensure we've waited at least a bus settle
5576 * delay before attempting to probe the device.
5577 * For HBAs that don't do bus resets, this won't make a difference.
5578 */
5579 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5580 scsi_delay);
5581 probeschedule(periph);
5582 return(CAM_REQ_CMP);
5583}
5584
5585static void
5586probeschedule(struct cam_periph *periph)
5587{
5588 struct ccb_pathinq cpi;
5589 union ccb *ccb;
5590 probe_softc *softc;
5591
5592 softc = (probe_softc *)periph->softc;
5593 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5594
5595 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5596 cpi.ccb_h.func_code = XPT_PATH_INQ;
5597 xpt_action((union ccb *)&cpi);
5598
5599 /*
5600 * If a device has gone away and another device, or the same one,
5601 * is back in the same place, it should have a unit attention
5602 * condition pending. It will not report the unit attention in
5603 * response to an inquiry, which may leave invalid transfer
5604 * negotiations in effect. The TUR will reveal the unit attention
5605 * condition. Only send the TUR for lun 0, since some devices
5606 * will get confused by commands other than inquiry to non-existent
5607 * luns. If you think a device has gone away start your scan from
5608 * lun 0. This will insure that any bogus transfer settings are
5609 * invalidated.
5610 *
5611 * If we haven't seen the device before and the controller supports
5612 * some kind of transfer negotiation, negotiate with the first
5613 * sent command if no bus reset was performed at startup. This
5614 * ensures that the device is not confused by transfer negotiation
5615 * settings left over by loader or BIOS action.
5616 */
5617 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5618 && (ccb->ccb_h.target_lun == 0)) {
5619 softc->action = PROBE_TUR;
5620 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5621 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5622 proberequestdefaultnegotiation(periph);
5623 softc->action = PROBE_INQUIRY;
5624 } else {
5625 softc->action = PROBE_INQUIRY;
5626 }
5627
5628 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5629 softc->flags |= PROBE_NO_ANNOUNCE;
5630 else
5631 softc->flags &= ~PROBE_NO_ANNOUNCE;
5632
5633 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5634}
5635
5636static void
5637probestart(struct cam_periph *periph, union ccb *start_ccb)
5638{
5639 /* Probe the device that our peripheral driver points to */
5640 struct ccb_scsiio *csio;
5641 probe_softc *softc;
5642
5643 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5644
5645 softc = (probe_softc *)periph->softc;
5646 csio = &start_ccb->csio;
5647
5648 switch (softc->action) {
5649 case PROBE_TUR:
5650 case PROBE_TUR_FOR_NEGOTIATION:
5651 {
5652 scsi_test_unit_ready(csio,
5653 /*retries*/4,
5654 probedone,
5655 MSG_SIMPLE_Q_TAG,
5656 SSD_FULL_SIZE,
5657 /*timeout*/60000);
5658 break;
5659 }
5660 case PROBE_INQUIRY:
5661 case PROBE_FULL_INQUIRY:
5662 {
5663 u_int inquiry_len;
5664 struct scsi_inquiry_data *inq_buf;
5665
5666 inq_buf = &periph->path->device->inq_data;
5667 /*
5668 * If the device is currently configured, we calculate an
5669 * MD5 checksum of the inquiry data, and if the serial number
5670 * length is greater than 0, add the serial number data
5671 * into the checksum as well. Once the inquiry and the
5672 * serial number check finish, we attempt to figure out
5673 * whether we still have the same device.
5674 */
5675 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5676
5677 MD5Init(&softc->context);
5678 MD5Update(&softc->context, (unsigned char *)inq_buf,
5679 sizeof(struct scsi_inquiry_data));
5680 softc->flags |= PROBE_INQUIRY_CKSUM;
5681 if (periph->path->device->serial_num_len > 0) {
5682 MD5Update(&softc->context,
5683 periph->path->device->serial_num,
5684 periph->path->device->serial_num_len);
5685 softc->flags |= PROBE_SERIAL_CKSUM;
5686 }
5687 MD5Final(softc->digest, &softc->context);
5688 }
5689
5690 if (softc->action == PROBE_INQUIRY)
5691 inquiry_len = SHORT_INQUIRY_LENGTH;
5692 else
5693 inquiry_len = inq_buf->additional_length
5694 + offsetof(struct scsi_inquiry_data,
5695 additional_length) + 1;
5696
5697 /*
5698 * Some parallel SCSI devices fail to send an
5699 * ignore wide residue message when dealing with
5700 * odd length inquiry requests. Round up to be
5701 * safe.
5702 */
5703 inquiry_len = roundup2(inquiry_len, 2);
5704
5705 scsi_inquiry(csio,
5706 /*retries*/4,
5707 probedone,
5708 MSG_SIMPLE_Q_TAG,
5709 (u_int8_t *)inq_buf,
5710 inquiry_len,
5711 /*evpd*/FALSE,
5712 /*page_code*/0,
5713 SSD_MIN_SIZE,
5714 /*timeout*/60 * 1000);
5715 break;
5716 }
5717 case PROBE_MODE_SENSE:
5718 {
5719 void *mode_buf;
5720 int mode_buf_len;
5721
5722 mode_buf_len = sizeof(struct scsi_mode_header_6)
5723 + sizeof(struct scsi_mode_blk_desc)
5724 + sizeof(struct scsi_control_page);
5725 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5726 if (mode_buf != NULL) {
5727 scsi_mode_sense(csio,
5728 /*retries*/4,
5729 probedone,
5730 MSG_SIMPLE_Q_TAG,
5731 /*dbd*/FALSE,
5732 SMS_PAGE_CTRL_CURRENT,
5733 SMS_CONTROL_MODE_PAGE,
5734 mode_buf,
5735 mode_buf_len,
5736 SSD_FULL_SIZE,
5737 /*timeout*/60000);
5738 break;
5739 }
5740 xpt_print_path(periph->path);
5741 printf("Unable to mode sense control page - malloc failure\n");
5742 softc->action = PROBE_SERIAL_NUM;
5743 }
5744 /* FALLTHROUGH */
5745 case PROBE_SERIAL_NUM:
5746 {
5747 struct scsi_vpd_unit_serial_number *serial_buf;
5748 struct cam_ed* device;
5749
5750 serial_buf = NULL;
5751 device = periph->path->device;
5752 device->serial_num = NULL;
5753 device->serial_num_len = 0;
5754
5755 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5756 serial_buf = (struct scsi_vpd_unit_serial_number *)
5757 malloc(sizeof(*serial_buf), M_TEMP,
5758 M_NOWAIT | M_ZERO);
5759
5760 if (serial_buf != NULL) {
5761 scsi_inquiry(csio,
5762 /*retries*/4,
5763 probedone,
5764 MSG_SIMPLE_Q_TAG,
5765 (u_int8_t *)serial_buf,
5766 sizeof(*serial_buf),
5767 /*evpd*/TRUE,
5768 SVPD_UNIT_SERIAL_NUMBER,
5769 SSD_MIN_SIZE,
5770 /*timeout*/60 * 1000);
5771 break;
5772 }
5773 /*
5774 * We'll have to do without, let our probedone
5775 * routine finish up for us.
5776 */
5777 start_ccb->csio.data_ptr = NULL;
5778 probedone(periph, start_ccb);
5779 return;
5780 }
5781 }
5782 xpt_action(start_ccb);
5783}
5784
5785static void
5786proberequestdefaultnegotiation(struct cam_periph *periph)
5787{
5788 struct ccb_trans_settings cts;
5789
5790 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5791 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5792#ifdef CAM_NEW_TRAN_CODE
5793 cts.type = CTS_TYPE_USER_SETTINGS;
5794#else /* CAM_NEW_TRAN_CODE */
5795 cts.flags = CCB_TRANS_USER_SETTINGS;
5796#endif /* CAM_NEW_TRAN_CODE */
5797 xpt_action((union ccb *)&cts);
5798 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5799#ifdef CAM_NEW_TRAN_CODE
5800 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5801#else /* CAM_NEW_TRAN_CODE */
5802 cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5803 cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5804#endif /* CAM_NEW_TRAN_CODE */
5805 xpt_action((union ccb *)&cts);
5806}
5807
5808static void
5809probedone(struct cam_periph *periph, union ccb *done_ccb)
5810{
5811 probe_softc *softc;
5812 struct cam_path *path;
5813 u_int32_t priority;
5814
5815 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5816
5817 softc = (probe_softc *)periph->softc;
5818 path = done_ccb->ccb_h.path;
5819 priority = done_ccb->ccb_h.pinfo.priority;
5820
5821 switch (softc->action) {
5822 case PROBE_TUR:
5823 {
5824 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5825
5826 if (cam_periph_error(done_ccb, 0,
5827 SF_NO_PRINT, NULL) == ERESTART)
5828 return;
5829 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5830 /* Don't wedge the queue */
5831 xpt_release_devq(done_ccb->ccb_h.path,
5832 /*count*/1,
5833 /*run_queue*/TRUE);
5834 }
5835 softc->action = PROBE_INQUIRY;
5836 xpt_release_ccb(done_ccb);
5837 xpt_schedule(periph, priority);
5838 return;
5839 }
5840 case PROBE_INQUIRY:
5841 case PROBE_FULL_INQUIRY:
5842 {
5843 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5844 struct scsi_inquiry_data *inq_buf;
5845 u_int8_t periph_qual;
5846
5847 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5848 inq_buf = &path->device->inq_data;
5849
5850 periph_qual = SID_QUAL(inq_buf);
5851
5852 switch(periph_qual) {
5853 case SID_QUAL_LU_CONNECTED:
5854 {
5855 u_int8_t len;
5856
5857 /*
5858 * We conservatively request only
5859 * SHORT_INQUIRY_LEN bytes of inquiry
5860 * information during our first try
5861 * at sending an INQUIRY. If the device
5862 * has more information to give,
5863 * perform a second request specifying
5864 * the amount of information the device
5865 * is willing to give.
5866 */
5867 len = inq_buf->additional_length
5868 + offsetof(struct scsi_inquiry_data,
5869 additional_length) + 1;
5870 if (softc->action == PROBE_INQUIRY
5871 && len > SHORT_INQUIRY_LENGTH) {
5872 softc->action = PROBE_FULL_INQUIRY;
5873 xpt_release_ccb(done_ccb);
5874 xpt_schedule(periph, priority);
5875 return;
5876 }
5877
5878 xpt_find_quirk(path->device);
5879
5880#ifdef CAM_NEW_TRAN_CODE
5881 xpt_devise_transport(path);
5882#endif /* CAM_NEW_TRAN_CODE */
5883 if ((inq_buf->flags & SID_CmdQue) != 0)
5884 softc->action = PROBE_MODE_SENSE;
5885 else
5886 softc->action = PROBE_SERIAL_NUM;
5887
5888 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5889
5890 xpt_release_ccb(done_ccb);
5891 xpt_schedule(periph, priority);
5892 return;
5893 }
5894 default:
5895 break;
5896 }
5897 } else if (cam_periph_error(done_ccb, 0,
5898 done_ccb->ccb_h.target_lun > 0
5899 ? SF_RETRY_UA|SF_QUIET_IR
5900 : SF_RETRY_UA,
5901 &softc->saved_ccb) == ERESTART) {
5902 return;
5903 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5904 /* Don't wedge the queue */
5905 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5906 /*run_queue*/TRUE);
5907 }
5908 /*
5909 * If we get to this point, we got an error status back
5910 * from the inquiry and the error status doesn't require
5911 * automatically retrying the command. Therefore, the
5912 * inquiry failed. If we had inquiry information before
5913 * for this device, but this latest inquiry command failed,
5914 * the device has probably gone away. If this device isn't
5915 * already marked unconfigured, notify the peripheral
5916 * drivers that this device is no more.
5917 */
5918 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5919 /* Send the async notification. */
5920 xpt_async(AC_LOST_DEVICE, path, NULL);
5921
5922 xpt_release_ccb(done_ccb);
5923 break;
5924 }
5925 case PROBE_MODE_SENSE:
5926 {
5927 struct ccb_scsiio *csio;
5928 struct scsi_mode_header_6 *mode_hdr;
5929
5930 csio = &done_ccb->csio;
5931 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5932 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5933 struct scsi_control_page *page;
5934 u_int8_t *offset;
5935
5936 offset = ((u_int8_t *)&mode_hdr[1])
5937 + mode_hdr->blk_desc_len;
5938 page = (struct scsi_control_page *)offset;
5939 path->device->queue_flags = page->queue_flags;
5940 } else if (cam_periph_error(done_ccb, 0,
5941 SF_RETRY_UA|SF_NO_PRINT,
5942 &softc->saved_ccb) == ERESTART) {
5943 return;
5944 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5945 /* Don't wedge the queue */
5946 xpt_release_devq(done_ccb->ccb_h.path,
5947 /*count*/1, /*run_queue*/TRUE);
5948 }
5949 xpt_release_ccb(done_ccb);
5950 free(mode_hdr, M_TEMP);
5951 softc->action = PROBE_SERIAL_NUM;
5952 xpt_schedule(periph, priority);
5953 return;
5954 }
5955 case PROBE_SERIAL_NUM:
5956 {
5957 struct ccb_scsiio *csio;
5958 struct scsi_vpd_unit_serial_number *serial_buf;
5959 u_int32_t priority;
5960 int changed;
5961 int have_serialnum;
5962
5963 changed = 1;
5964 have_serialnum = 0;
5965 csio = &done_ccb->csio;
5966 priority = done_ccb->ccb_h.pinfo.priority;
5967 serial_buf =
5968 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5969
5970 /* Clean up from previous instance of this device */
5971 if (path->device->serial_num != NULL) {
5972 free(path->device->serial_num, M_CAMXPT);
5973 path->device->serial_num = NULL;
5974 path->device->serial_num_len = 0;
5975 }
5976
5977 if (serial_buf == NULL) {
5978 /*
5979 * Don't process the command as it was never sent
5980 */
5981 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5982 && (serial_buf->length > 0)) {
5983
5984 have_serialnum = 1;
5985 path->device->serial_num =
5986 (u_int8_t *)malloc((serial_buf->length + 1),
5987 M_CAMXPT, M_NOWAIT);
5988 if (path->device->serial_num != NULL) {
5989 bcopy(serial_buf->serial_num,
5990 path->device->serial_num,
5991 serial_buf->length);
5992 path->device->serial_num_len =
5993 serial_buf->length;
5994 path->device->serial_num[serial_buf->length]
5995 = '\0';
5996 }
5997 } else if (cam_periph_error(done_ccb, 0,
5998 SF_RETRY_UA|SF_NO_PRINT,
5999 &softc->saved_ccb) == ERESTART) {
6000 return;
6001 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6002 /* Don't wedge the queue */
6003 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6004 /*run_queue*/TRUE);
6005 }
6006
6007 /*
6008 * Let's see if we have seen this device before.
6009 */
6010 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
6011 MD5_CTX context;
6012 u_int8_t digest[16];
6013
6014 MD5Init(&context);
6015
6016 MD5Update(&context,
6017 (unsigned char *)&path->device->inq_data,
6018 sizeof(struct scsi_inquiry_data));
6019
6020 if (have_serialnum)
6021 MD5Update(&context, serial_buf->serial_num,
6022 serial_buf->length);
6023
6024 MD5Final(digest, &context);
6025 if (bcmp(softc->digest, digest, 16) == 0)
6026 changed = 0;
6027
6028 /*
6029 * XXX Do we need to do a TUR in order to ensure
6030 * that the device really hasn't changed???
6031 */
6032 if ((changed != 0)
6033 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
6034 xpt_async(AC_LOST_DEVICE, path, NULL);
6035 }
6036 if (serial_buf != NULL)
6037 free(serial_buf, M_TEMP);
6038
6039 if (changed != 0) {
6040 /*
6041 * Now that we have all the necessary
6042 * information to safely perform transfer
6043 * negotiations... Controllers don't perform
6044 * any negotiation or tagged queuing until
6045 * after the first XPT_SET_TRAN_SETTINGS ccb is
6046 * received. So, on a new device, just retreive
6047 * the user settings, and set them as the current
6048 * settings to set the device up.
6049 */
6050 proberequestdefaultnegotiation(periph);
6051 xpt_release_ccb(done_ccb);
6052
6053 /*
6054 * Perform a TUR to allow the controller to
6055 * perform any necessary transfer negotiation.
6056 */
6057 softc->action = PROBE_TUR_FOR_NEGOTIATION;
6058 xpt_schedule(periph, priority);
6059 return;
6060 }
6061 xpt_release_ccb(done_ccb);
6062 break;
6063 }
6064 case PROBE_TUR_FOR_NEGOTIATION:
6065 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6066 /* Don't wedge the queue */
6067 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6068 /*run_queue*/TRUE);
6069 }
6070
6071 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
6072
6073 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
6074 /* Inform the XPT that a new device has been found */
6075 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
6076 xpt_action(done_ccb);
6077
6078 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6079 done_ccb);
6080 }
6081 xpt_release_ccb(done_ccb);
6082 break;
6083 }
6084 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
6085 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
6086 done_ccb->ccb_h.status = CAM_REQ_CMP;
6087 xpt_done(done_ccb);
6088 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
6089 cam_periph_invalidate(periph);
6090 cam_periph_release(periph);
6091 } else {
6092 probeschedule(periph);
6093 }
6094}
6095
6096static void
6097probecleanup(struct cam_periph *periph)
6098{
6099 free(periph->softc, M_TEMP);
6100}
6101
6102static void
6103xpt_find_quirk(struct cam_ed *device)
6104{
6105 caddr_t match;
6106
6107 match = cam_quirkmatch((caddr_t)&device->inq_data,
6108 (caddr_t)xpt_quirk_table,
6109 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
6110 sizeof(*xpt_quirk_table), scsi_inquiry_match);
6111
6112 if (match == NULL)
6113 panic("xpt_find_quirk: device didn't match wildcard entry!!");
6114
6115 device->quirk = (struct xpt_quirk_entry *)match;
6116}
6117
6118#ifdef CAM_NEW_TRAN_CODE
6119
6120static void
6121xpt_devise_transport(struct cam_path *path)
6122{
6123 struct ccb_pathinq cpi;
6124 struct ccb_trans_settings cts;
6125 struct scsi_inquiry_data *inq_buf;
6126
6127 /* Get transport information from the SIM */
6128 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
6129 cpi.ccb_h.func_code = XPT_PATH_INQ;
6130 xpt_action((union ccb *)&cpi);
6131
6132 inq_buf = NULL;
6133 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
6134 inq_buf = &path->device->inq_data;
6135 path->device->protocol = PROTO_SCSI;
6136 path->device->protocol_version =
6137 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
6138 path->device->transport = cpi.transport;
6139 path->device->transport_version = cpi.transport_version;
6140
6141 /*
6142 * Any device not using SPI3 features should
6143 * be considered SPI2 or lower.
6144 */
6145 if (inq_buf != NULL) {
6146 if (path->device->transport == XPORT_SPI
6147 && (inq_buf->spi3data & SID_SPI_MASK) == 0
6148 && path->device->transport_version > 2)
6149 path->device->transport_version = 2;
6150 } else {
6151 struct cam_ed* otherdev;
6152
6153 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6154 otherdev != NULL;
6155 otherdev = TAILQ_NEXT(otherdev, links)) {
6156 if (otherdev != path->device)
6157 break;
6158 }
6159
6160 if (otherdev != NULL) {
6161 /*
6162 * Initially assume the same versioning as
6163 * prior luns for this target.
6164 */
6165 path->device->protocol_version =
6166 otherdev->protocol_version;
6167 path->device->transport_version =
6168 otherdev->transport_version;
6169 } else {
6170 /* Until we know better, opt for safty */
6171 path->device->protocol_version = 2;
6172 if (path->device->transport == XPORT_SPI)
6173 path->device->transport_version = 2;
6174 else
6175 path->device->transport_version = 0;
6176 }
6177 }
6178
6179 /*
6180 * XXX
6181 * For a device compliant with SPC-2 we should be able
6182 * to determine the transport version supported by
6183 * scrutinizing the version descriptors in the
6184 * inquiry buffer.
6185 */
6186
6187 /* Tell the controller what we think */
6188 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
6189 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6190 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6191 cts.transport = path->device->transport;
6192 cts.transport_version = path->device->transport_version;
6193 cts.protocol = path->device->protocol;
6194 cts.protocol_version = path->device->protocol_version;
6195 cts.proto_specific.valid = 0;
6196 cts.xport_specific.valid = 0;
6197 xpt_action((union ccb *)&cts);
6198}
6199
6200static void
6201xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6202 int async_update)
6203{
6204 struct ccb_pathinq cpi;
6205 struct ccb_trans_settings cur_cts;
6206 struct ccb_trans_settings_scsi *scsi;
6207 struct ccb_trans_settings_scsi *cur_scsi;
6208 struct cam_sim *sim;
6209 struct scsi_inquiry_data *inq_data;
6210
6211 if (device == NULL) {
6212 cts->ccb_h.status = CAM_PATH_INVALID;
6213 xpt_done((union ccb *)cts);
6214 return;
6215 }
6216
6217 if (cts->protocol == PROTO_UNKNOWN
6218 || cts->protocol == PROTO_UNSPECIFIED) {
6219 cts->protocol = device->protocol;
6220 cts->protocol_version = device->protocol_version;
6221 }
6222
6223 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
6224 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
6225 cts->protocol_version = device->protocol_version;
6226
6227 if (cts->protocol != device->protocol) {
6228 xpt_print_path(cts->ccb_h.path);
6229 printf("Uninitialized Protocol %x:%x?\n",
6230 cts->protocol, device->protocol);
6231 cts->protocol = device->protocol;
6232 }
6233
6234 if (cts->protocol_version > device->protocol_version) {
6235 if (bootverbose) {
6236 xpt_print_path(cts->ccb_h.path);
6237 printf("Down reving Protocol Version from %d to %d?\n",
6238 cts->protocol_version, device->protocol_version);
6239 }
6240 cts->protocol_version = device->protocol_version;
6241 }
6242
6243 if (cts->transport == XPORT_UNKNOWN
6244 || cts->transport == XPORT_UNSPECIFIED) {
6245 cts->transport = device->transport;
6246 cts->transport_version = device->transport_version;
6247 }
6248
6249 if (cts->transport_version == XPORT_VERSION_UNKNOWN
6250 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
6251 cts->transport_version = device->transport_version;
6252
6253 if (cts->transport != device->transport) {
6254 xpt_print_path(cts->ccb_h.path);
6255 printf("Uninitialized Transport %x:%x?\n",
6256 cts->transport, device->transport);
6257 cts->transport = device->transport;
6258 }
6259
6260 if (cts->transport_version > device->transport_version) {
6261 if (bootverbose) {
6262 xpt_print_path(cts->ccb_h.path);
6263 printf("Down reving Transport Version from %d to %d?\n",
6264 cts->transport_version,
6265 device->transport_version);
6266 }
6267 cts->transport_version = device->transport_version;
6268 }
6269
6270 sim = cts->ccb_h.path->bus->sim;
6271
6272 /*
6273 * Nothing more of interest to do unless
6274 * this is a device connected via the
6275 * SCSI protocol.
6276 */
6277 if (cts->protocol != PROTO_SCSI) {
6278 if (async_update == FALSE)
6279 (*(sim->sim_action))(sim, (union ccb *)cts);
6280 return;
6281 }
6282
6283 inq_data = &device->inq_data;
6284 scsi = &cts->proto_specific.scsi;
6285 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6286 cpi.ccb_h.func_code = XPT_PATH_INQ;
6287 xpt_action((union ccb *)&cpi);
6288
6289 /* SCSI specific sanity checking */
6290 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6291 || (inq_data->flags & SID_CmdQue) == 0
6292 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6293 || (device->quirk->mintags == 0)) {
6294 /*
6295 * Can't tag on hardware that doesn't support tags,
6296 * doesn't have it enabled, or has broken tag support.
6297 */
6298 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6299 }
6300
6301 if (async_update == FALSE) {
6302 /*
6303 * Perform sanity checking against what the
6304 * controller and device can do.
6305 */
6306 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6307 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6308 cur_cts.type = cts->type;
6309 xpt_action((union ccb *)&cur_cts);
6310
6311 cur_scsi = &cur_cts.proto_specific.scsi;
6312 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
6313 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6314 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
6315 }
6316 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
6317 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6318 }
6319
6320 /* SPI specific sanity checking */
6321 if (cts->transport == XPORT_SPI && async_update == FALSE) {
6322 u_int spi3caps;
6323 struct ccb_trans_settings_spi *spi;
6324 struct ccb_trans_settings_spi *cur_spi;
6325
6326 spi = &cts->xport_specific.spi;
6327
6328 cur_spi = &cur_cts.xport_specific.spi;
6329
6330 /* Fill in any gaps in what the user gave us */
6331 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6332 spi->sync_period = cur_spi->sync_period;
6333 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6334 spi->sync_period = 0;
6335 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6336 spi->sync_offset = cur_spi->sync_offset;
6337 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6338 spi->sync_offset = 0;
6339 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6340 spi->ppr_options = cur_spi->ppr_options;
6341 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6342 spi->ppr_options = 0;
6343 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6344 spi->bus_width = cur_spi->bus_width;
6345 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6346 spi->bus_width = 0;
6347 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
6348 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6349 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
6350 }
6351 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
6352 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6353 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6354 && (inq_data->flags & SID_Sync) == 0
6355 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6356 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6357 || (cur_spi->sync_offset == 0)
6358 || (cur_spi->sync_period == 0)) {
6359 /* Force async */
6360 spi->sync_period = 0;
6361 spi->sync_offset = 0;
6362 }
6363
6364 switch (spi->bus_width) {
6365 case MSG_EXT_WDTR_BUS_32_BIT:
6366 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6367 || (inq_data->flags & SID_WBus32) != 0
6368 || cts->type == CTS_TYPE_USER_SETTINGS)
6369 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6370 break;
6371 /* Fall Through to 16-bit */
6372 case MSG_EXT_WDTR_BUS_16_BIT:
6373 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6374 || (inq_data->flags & SID_WBus16) != 0
6375 || cts->type == CTS_TYPE_USER_SETTINGS)
6376 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6377 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6378 break;
6379 }
6380 /* Fall Through to 8-bit */
6381 default: /* New bus width?? */
6382 case MSG_EXT_WDTR_BUS_8_BIT:
6383 /* All targets can do this */
6384 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6385 break;
6386 }
6387
6388 spi3caps = cpi.xport_specific.spi.ppr_options;
6389 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6390 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6391 spi3caps &= inq_data->spi3data;
6392
6393 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
6394 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
6395
6396 if ((spi3caps & SID_SPI_IUS) == 0)
6397 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
6398
6399 if ((spi3caps & SID_SPI_QAS) == 0)
6400 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
6401
6402 /* No SPI Transfer settings are allowed unless we are wide */
6403 if (spi->bus_width == 0)
6404 spi->ppr_options = 0;
6405
6406 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
6407 /*
6408 * Can't tag queue without disconnection.
6409 */
6410 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6411 scsi->valid |= CTS_SCSI_VALID_TQ;
6412 }
6413
6414 /*
6415 * If we are currently performing tagged transactions to
6416 * this device and want to change its negotiation parameters,
6417 * go non-tagged for a bit to give the controller a chance to
6418 * negotiate unhampered by tag messages.
6419 */
6420 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6421 && (device->inq_flags & SID_CmdQue) != 0
6422 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6423 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
6424 CTS_SPI_VALID_SYNC_OFFSET|
6425 CTS_SPI_VALID_BUS_WIDTH)) != 0)
6426 xpt_toggle_tags(cts->ccb_h.path);
6427 }
6428
6429 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6430 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
6431 int device_tagenb;
6432
6433 /*
6434 * If we are transitioning from tags to no-tags or
6435 * vice-versa, we need to carefully freeze and restart
6436 * the queue so that we don't overlap tagged and non-tagged
6437 * commands. We also temporarily stop tags if there is
6438 * a change in transfer negotiation settings to allow
6439 * "tag-less" negotiation.
6440 */
6441 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6442 || (device->inq_flags & SID_CmdQue) != 0)
6443 device_tagenb = TRUE;
6444 else
6445 device_tagenb = FALSE;
6446
6447 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6448 && device_tagenb == FALSE)
6449 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
6450 && device_tagenb == TRUE)) {
6451
6452 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
6453 /*
6454 * Delay change to use tags until after a
6455 * few commands have gone to this device so
6456 * the controller has time to perform transfer
6457 * negotiations without tagged messages getting
6458 * in the way.
6459 */
6460 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6461 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6462 } else {
6463 struct ccb_relsim crs;
6464
6465 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6466 device->inq_flags &= ~SID_CmdQue;
6467 xpt_dev_ccbq_resize(cts->ccb_h.path,
6468 sim->max_dev_openings);
6469 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6470 device->tag_delay_count = 0;
6471
6472 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6473 /*priority*/1);
6474 crs.ccb_h.func_code = XPT_REL_SIMQ;
6475 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6476 crs.openings
6477 = crs.release_timeout
6478 = crs.qfrozen_cnt
6479 = 0;
6480 xpt_action((union ccb *)&crs);
6481 }
6482 }
6483 }
6484 if (async_update == FALSE)
6485 (*(sim->sim_action))(sim, (union ccb *)cts);
6486}
6487
6488#else /* CAM_NEW_TRAN_CODE */
6489
6490static void
6491xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6492 int async_update)
6493{
6494 struct cam_sim *sim;
6495 int qfrozen;
6496
6497 sim = cts->ccb_h.path->bus->sim;
6498 if (async_update == FALSE) {
6499 struct scsi_inquiry_data *inq_data;
6500 struct ccb_pathinq cpi;
6501 struct ccb_trans_settings cur_cts;
6502
6503 if (device == NULL) {
6504 cts->ccb_h.status = CAM_PATH_INVALID;
6505 xpt_done((union ccb *)cts);
6506 return;
6507 }
6508
6509 /*
6510 * Perform sanity checking against what the
6511 * controller and device can do.
6512 */
6513 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6514 cpi.ccb_h.func_code = XPT_PATH_INQ;
6515 xpt_action((union ccb *)&cpi);
6516 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6517 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6518 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
6519 xpt_action((union ccb *)&cur_cts);
6520 inq_data = &device->inq_data;
6521
6522 /* Fill in any gaps in what the user gave us */
6523 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
6524 cts->sync_period = cur_cts.sync_period;
6525 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
6526 cts->sync_offset = cur_cts.sync_offset;
6527 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
6528 cts->bus_width = cur_cts.bus_width;
6529 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
6530 cts->flags &= ~CCB_TRANS_DISC_ENB;
6531 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
6532 }
6533 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
6534 cts->flags &= ~CCB_TRANS_TAG_ENB;
6535 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
6536 }
6537
6538 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6539 && (inq_data->flags & SID_Sync) == 0)
6540 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6541 || (cts->sync_offset == 0)
6542 || (cts->sync_period == 0)) {
6543 /* Force async */
6544 cts->sync_period = 0;
6545 cts->sync_offset = 0;
6546 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6547 && (inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
6548 && cts->sync_period <= 0x9) {
6549 /*
6550 * Don't allow DT transmission rates if the
6551 * device does not support it.
6552 */
6553 cts->sync_period = 0xa;
6554 }
6555
6556 switch (cts->bus_width) {
6557 case MSG_EXT_WDTR_BUS_32_BIT:
6558 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6559 || (inq_data->flags & SID_WBus32) != 0)
6560 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6561 break;
6562 /* FALLTHROUGH to 16-bit */
6563 case MSG_EXT_WDTR_BUS_16_BIT:
6564 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6565 || (inq_data->flags & SID_WBus16) != 0)
6566 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6567 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6568 break;
6569 }
6570 /* FALLTHROUGH to 8-bit */
6571 default: /* New bus width?? */
6572 case MSG_EXT_WDTR_BUS_8_BIT:
6573 /* All targets can do this */
6574 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6575 break;
6576 }
6577
6578 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
6579 /*
6580 * Can't tag queue without disconnection.
6581 */
6582 cts->flags &= ~CCB_TRANS_TAG_ENB;
6583 cts->valid |= CCB_TRANS_TQ_VALID;
6584 }
6585
6586 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6587 || (inq_data->flags & SID_CmdQue) == 0
6588 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6589 || (device->quirk->mintags == 0)) {
6590 /*
6591 * Can't tag on hardware that doesn't support,
6592 * doesn't have it enabled, or has broken tag support.
6593 */
6594 cts->flags &= ~CCB_TRANS_TAG_ENB;
6595 }
6596 }
6597
6598 qfrozen = FALSE;
6599 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
6600 int device_tagenb;
6601
6602 /*
6603 * If we are transitioning from tags to no-tags or
6604 * vice-versa, we need to carefully freeze and restart
6605 * the queue so that we don't overlap tagged and non-tagged
6606 * commands. We also temporarily stop tags if there is
6607 * a change in transfer negotiation settings to allow
6608 * "tag-less" negotiation.
6609 */
6610 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6611 || (device->inq_flags & SID_CmdQue) != 0)
6612 device_tagenb = TRUE;
6613 else
6614 device_tagenb = FALSE;
6615
6616 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
6617 && device_tagenb == FALSE)
6618 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
6619 && device_tagenb == TRUE)) {
6620
6621 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
6622 /*
6623 * Delay change to use tags until after a
6624 * few commands have gone to this device so
6625 * the controller has time to perform transfer
6626 * negotiations without tagged messages getting
6627 * in the way.
6628 */
6629 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6630 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6631 } else {
6632 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6633 qfrozen = TRUE;
6634 device->inq_flags &= ~SID_CmdQue;
6635 xpt_dev_ccbq_resize(cts->ccb_h.path,
6636 sim->max_dev_openings);
6637 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6638 device->tag_delay_count = 0;
6639 }
6640 }
6641 }
6642
6643 if (async_update == FALSE) {
6644 /*
6645 * If we are currently performing tagged transactions to
6646 * this device and want to change its negotiation parameters,
6647 * go non-tagged for a bit to give the controller a chance to
6648 * negotiate unhampered by tag messages.
6649 */
6650 if ((device->inq_flags & SID_CmdQue) != 0
6651 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
6652 CCB_TRANS_SYNC_OFFSET_VALID|
6653 CCB_TRANS_BUS_WIDTH_VALID)) != 0)
6654 xpt_toggle_tags(cts->ccb_h.path);
6655
6656 (*(sim->sim_action))(sim, (union ccb *)cts);
6657 }
6658
6659 if (qfrozen) {
6660 struct ccb_relsim crs;
6661
6662 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6663 /*priority*/1);
6664 crs.ccb_h.func_code = XPT_REL_SIMQ;
6665 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6666 crs.openings
6667 = crs.release_timeout
6668 = crs.qfrozen_cnt
6669 = 0;
6670 xpt_action((union ccb *)&crs);
6671 }
6672}
6673
6674
6675#endif /* CAM_NEW_TRAN_CODE */
6676
6677static void
6678xpt_toggle_tags(struct cam_path *path)
6679{
6680 struct cam_ed *dev;
6681
6682 /*
6683 * Give controllers a chance to renegotiate
6684 * before starting tag operations. We
6685 * "toggle" tagged queuing off then on
6686 * which causes the tag enable command delay
6687 * counter to come into effect.
6688 */
6689 dev = path->device;
6690 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6691 || ((dev->inq_flags & SID_CmdQue) != 0
6692 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6693 struct ccb_trans_settings cts;
6694
6695 xpt_setup_ccb(&cts.ccb_h, path, 1);
6696#ifdef CAM_NEW_TRAN_CODE
6697 cts.protocol = PROTO_SCSI;
6698 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
6699 cts.transport = XPORT_UNSPECIFIED;
6700 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
6701 cts.proto_specific.scsi.flags = 0;
6702 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
6703#else /* CAM_NEW_TRAN_CODE */
6704 cts.flags = 0;
6705 cts.valid = CCB_TRANS_TQ_VALID;
6706#endif /* CAM_NEW_TRAN_CODE */
6707 xpt_set_transfer_settings(&cts, path->device,
6708 /*async_update*/TRUE);
6709#ifdef CAM_NEW_TRAN_CODE
6710 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
6711#else /* CAM_NEW_TRAN_CODE */
6712 cts.flags = CCB_TRANS_TAG_ENB;
6713#endif /* CAM_NEW_TRAN_CODE */
6714 xpt_set_transfer_settings(&cts, path->device,
6715 /*async_update*/TRUE);
6716 }
6717}
6718
6719static void
6720xpt_start_tags(struct cam_path *path)
6721{
6722 struct ccb_relsim crs;
6723 struct cam_ed *device;
6724 struct cam_sim *sim;
6725 int newopenings;
6726
6727 device = path->device;
6728 sim = path->bus->sim;
6729 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6730 xpt_freeze_devq(path, /*count*/1);
6731 device->inq_flags |= SID_CmdQue;
6732 if (device->tag_saved_openings != 0)
6733 newopenings = device->tag_saved_openings;
6734 else
6735 newopenings = min(device->quirk->maxtags,
6736 sim->max_tagged_dev_openings);
6737 xpt_dev_ccbq_resize(path, newopenings);
6738 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6739 crs.ccb_h.func_code = XPT_REL_SIMQ;
6740 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6741 crs.openings
6742 = crs.release_timeout
6743 = crs.qfrozen_cnt
6744 = 0;
6745 xpt_action((union ccb *)&crs);
6746}
6747
6748static int busses_to_config;
6749static int busses_to_reset;
6750
6751static int
6752xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6753{
6754 if (bus->path_id != CAM_XPT_PATH_ID) {
6755 struct cam_path path;
6756 struct ccb_pathinq cpi;
6757 int can_negotiate;
6758
6759 busses_to_config++;
6760 xpt_compile_path(&path, NULL, bus->path_id,
6761 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6762 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6763 cpi.ccb_h.func_code = XPT_PATH_INQ;
6764 xpt_action((union ccb *)&cpi);
6765 can_negotiate = cpi.hba_inquiry;
6766 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6767 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6768 && can_negotiate)
6769 busses_to_reset++;
6770 xpt_release_path(&path);
6771 }
6772
6773 return(1);
6774}
6775
6776static int
6777xptconfigfunc(struct cam_eb *bus, void *arg)
6778{
6779 struct cam_path *path;
6780 union ccb *work_ccb;
6781
6782 if (bus->path_id != CAM_XPT_PATH_ID) {
6783 cam_status status;
6784 int can_negotiate;
6785
6786 work_ccb = xpt_alloc_ccb();
6787 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6788 CAM_TARGET_WILDCARD,
6789 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6790 printf("xptconfigfunc: xpt_create_path failed with "
6791 "status %#x for bus %d\n", status, bus->path_id);
6792 printf("xptconfigfunc: halting bus configuration\n");
6793 xpt_free_ccb(work_ccb);
6794 busses_to_config--;
6795 xpt_finishconfig(xpt_periph, NULL);
6796 return(0);
6797 }
6798 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6799 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6800 xpt_action(work_ccb);
6801 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6802 printf("xptconfigfunc: CPI failed on bus %d "
6803 "with status %d\n", bus->path_id,
6804 work_ccb->ccb_h.status);
6805 xpt_finishconfig(xpt_periph, work_ccb);
6806 return(1);
6807 }
6808
6809 can_negotiate = work_ccb->cpi.hba_inquiry;
6810 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6811 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6812 && (can_negotiate != 0)) {
6813 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6814 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6815 work_ccb->ccb_h.cbfcnp = NULL;
6816 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6817 ("Resetting Bus\n"));
6818 xpt_action(work_ccb);
6819 xpt_finishconfig(xpt_periph, work_ccb);
6820 } else {
6821 /* Act as though we performed a successful BUS RESET */
6822 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6823 xpt_finishconfig(xpt_periph, work_ccb);
6824 }
6825 }
6826
6827 return(1);
6828}
6829
6830static void
6831xpt_config(void *arg)
6832{
6833 /*
6834 * Now that interrupts are enabled, go find our devices
6835 */
6836
6837#ifdef CAMDEBUG
6838 /* Setup debugging flags and path */
6839#ifdef CAM_DEBUG_FLAGS
6840 cam_dflags = CAM_DEBUG_FLAGS;
6841#else /* !CAM_DEBUG_FLAGS */
6842 cam_dflags = CAM_DEBUG_NONE;
6843#endif /* CAM_DEBUG_FLAGS */
6844#ifdef CAM_DEBUG_BUS
6845 if (cam_dflags != CAM_DEBUG_NONE) {
6846 if (xpt_create_path(&cam_dpath, xpt_periph,
6847 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6848 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6849 printf("xpt_config: xpt_create_path() failed for debug"
6850 " target %d:%d:%d, debugging disabled\n",
6851 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6852 cam_dflags = CAM_DEBUG_NONE;
6853 }
6854 } else
6855 cam_dpath = NULL;
6856#else /* !CAM_DEBUG_BUS */
6857 cam_dpath = NULL;
6858#endif /* CAM_DEBUG_BUS */
6859#endif /* CAMDEBUG */
6860
6861 /*
6862 * Scan all installed busses.
6863 */
6864 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6865
6866 if (busses_to_config == 0) {
6867 /* Call manually because we don't have any busses */
6868 xpt_finishconfig(xpt_periph, NULL);
6869 } else {
6870 if (busses_to_reset > 0 && scsi_delay >= 2000) {
6871 printf("Waiting %d seconds for SCSI "
6872 "devices to settle\n", scsi_delay/1000);
6873 }
6874 xpt_for_all_busses(xptconfigfunc, NULL);
6875 }
6876}
6877
6878/*
6879 * If the given device only has one peripheral attached to it, and if that
6880 * peripheral is the passthrough driver, announce it. This insures that the
6881 * user sees some sort of announcement for every peripheral in their system.
6882 */
6883static int
6884xptpassannouncefunc(struct cam_ed *device, void *arg)
6885{
6886 struct cam_periph *periph;
6887 int i;
6888
6889 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6890 periph = SLIST_NEXT(periph, periph_links), i++);
6891
6892 periph = SLIST_FIRST(&device->periphs);
6893 if ((i == 1)
6894 && (strncmp(periph->periph_name, "pass", 4) == 0))
6895 xpt_announce_periph(periph, NULL);
6896
6897 return(1);
6898}
6899
6900static void
6901xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6902{
6903 struct periph_driver **p_drv;
6904 int i;
6905
6906 if (done_ccb != NULL) {
6907 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6908 ("xpt_finishconfig\n"));
6909 switch(done_ccb->ccb_h.func_code) {
6910 case XPT_RESET_BUS:
6911 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6912 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6913 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6914 done_ccb->crcn.flags = 0;
6915 xpt_action(done_ccb);
6916 return;
6917 }
6918 /* FALLTHROUGH */
6919 case XPT_SCAN_BUS:
6920 default:
6921 xpt_free_path(done_ccb->ccb_h.path);
6922 busses_to_config--;
6923 break;
6924 }
6925 }
6926
6927 if (busses_to_config == 0) {
6928 /* Register all the peripheral drivers */
6929 /* XXX This will have to change when we have loadable modules */
6930 p_drv = periph_drivers;
6931 for (i = 0; p_drv[i] != NULL; i++) {
6932 (*p_drv[i]->init)();
6933 }
6934
6935 /*
6936 * Check for devices with no "standard" peripheral driver
6937 * attached. For any devices like that, announce the
6938 * passthrough driver so the user will see something.
6939 */
6940 xpt_for_all_devices(xptpassannouncefunc, NULL);
6941
6942 /* Release our hook so that the boot can continue. */
6943 config_intrhook_disestablish(xpt_config_hook);
6944 free(xpt_config_hook, M_TEMP);
6945 xpt_config_hook = NULL;
6946 }
6947 if (done_ccb != NULL)
6948 xpt_free_ccb(done_ccb);
6949}
6950
6951static void
6952xptaction(struct cam_sim *sim, union ccb *work_ccb)
6953{
6954 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6955
6956 switch (work_ccb->ccb_h.func_code) {
6957 /* Common cases first */
6958 case XPT_PATH_INQ: /* Path routing inquiry */
6959 {
6960 struct ccb_pathinq *cpi;
6961
6962 cpi = &work_ccb->cpi;
6963 cpi->version_num = 1; /* XXX??? */
6964 cpi->hba_inquiry = 0;
6965 cpi->target_sprt = 0;
6966 cpi->hba_misc = 0;
6967 cpi->hba_eng_cnt = 0;
6968 cpi->max_target = 0;
6969 cpi->max_lun = 0;
6970 cpi->initiator_id = 0;
6971 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
6972 strncpy(cpi->hba_vid, "", HBA_IDLEN);
6973 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
6974 cpi->unit_number = sim->unit_number;
6975 cpi->bus_id = sim->bus_id;
6976 cpi->base_transfer_speed = 0;
6977#ifdef CAM_NEW_TRAN_CODE
6978 cpi->protocol = PROTO_UNSPECIFIED;
6979 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
6980 cpi->transport = XPORT_UNSPECIFIED;
6981 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
6982#endif /* CAM_NEW_TRAN_CODE */
6983 cpi->ccb_h.status = CAM_REQ_CMP;
6984 xpt_done(work_ccb);
6985 break;
6986 }
6987 default:
6988 work_ccb->ccb_h.status = CAM_REQ_INVALID;
6989 xpt_done(work_ccb);
6990 break;
6991 }
6992}
6993
6994/*
6995 * The xpt as a "controller" has no interrupt sources, so polling
6996 * is a no-op.
6997 */
6998static void
6999xptpoll(struct cam_sim *sim)
7000{
7001}
7002
7003static void
7004camisr(void *V_queue)
7005{
7006 cam_isrq_t *oqueue = V_queue;
7007 cam_isrq_t queue;
7008 int s;
7009 struct ccb_hdr *ccb_h;
7010
7011 /*
7012 * Transfer the ccb_bioq list to a temporary list so we can operate
7013 * on it without needing to lock/unlock on every loop. The concat
7014 * function with re-init the real list for us.
7015 */
7016 s = splcam();
7017 mtx_lock(&cam_bioq_lock);
7018 TAILQ_INIT(&queue);
7019 TAILQ_CONCAT(&queue, oqueue, sim_links.tqe);
7020 mtx_unlock(&cam_bioq_lock);
7021
7022 while ((ccb_h = TAILQ_FIRST(&queue)) != NULL) {
7023 int runq;
7024
7025 TAILQ_REMOVE(&queue, ccb_h, sim_links.tqe);
7026 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
7027 splx(s);
7028
7029 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
7030 ("camisr\n"));
7031
7032 runq = FALSE;
7033
7034 if (ccb_h->flags & CAM_HIGH_POWER) {
7035 struct highpowerlist *hphead;
7036 union ccb *send_ccb;
7037
7038 hphead = &highpowerq;
7039
7040 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
7041
7042 /*
7043 * Increment the count since this command is done.
7044 */
7045 num_highpower++;
7046
7047 /*
7048 * Any high powered commands queued up?
7049 */
7050 if (send_ccb != NULL) {
7051
7052 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
7053
7054 xpt_release_devq(send_ccb->ccb_h.path,
7055 /*count*/1, /*runqueue*/TRUE);
7056 }
7057 }
7058 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
7059 struct cam_ed *dev;
7060
7061 dev = ccb_h->path->device;
7062
7063 s = splcam();
7064 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
7065
7066 ccb_h->path->bus->sim->devq->send_active--;
7067 ccb_h->path->bus->sim->devq->send_openings++;
7068 splx(s);
7069
7070 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
7071 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
7072 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
7073 && (dev->ccbq.dev_active == 0))) {
7074
7075 xpt_release_devq(ccb_h->path, /*count*/1,
7076 /*run_queue*/TRUE);
7077 }
7078
7079 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
7080 && (--dev->tag_delay_count == 0))
7081 xpt_start_tags(ccb_h->path);
7082
7083 if ((dev->ccbq.queue.entries > 0)
7084 && (dev->qfrozen_cnt == 0)
7085 && (device_is_send_queued(dev) == 0)) {
7086 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
7087 dev);
7088 }
7089 }
7090
7091 if (ccb_h->status & CAM_RELEASE_SIMQ) {
7092 xpt_release_simq(ccb_h->path->bus->sim,
7093 /*run_queue*/TRUE);
7094 ccb_h->status &= ~CAM_RELEASE_SIMQ;
7095 runq = FALSE;
7096 }
7097
7098 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
7099 && (ccb_h->status & CAM_DEV_QFRZN)) {
7100 xpt_release_devq(ccb_h->path, /*count*/1,
7101 /*run_queue*/TRUE);
7102 ccb_h->status &= ~CAM_DEV_QFRZN;
7103 } else if (runq) {
7104 xpt_run_dev_sendq(ccb_h->path->bus);
7105 }
7106
7107 /* Call the peripheral driver's callback */
7108 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
7109
7110 /* Raise IPL for while test */
7111 s = splcam();
7112 }
7113 splx(s);
7114}
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
30#include <sys/cdefs.h>
31__FBSDID("$FreeBSD: head/sys/cam/cam_xpt.c 147723 2005-07-01 15:21:30Z avatar $");
32
33#include <sys/param.h>
34#include <sys/bus.h>
35#include <sys/systm.h>
36#include <sys/types.h>
37#include <sys/malloc.h>
38#include <sys/kernel.h>
39#include <sys/time.h>
40#include <sys/conf.h>
41#include <sys/fcntl.h>
42#include <sys/md5.h>
43#include <sys/interrupt.h>
44#include <sys/sbuf.h>
45
46#include <sys/lock.h>
47#include <sys/mutex.h>
48
49#ifdef PC98
50#include <pc98/pc98/pc98_machdep.h> /* geometry translation */
51#endif
52
53#include <cam/cam.h>
54#include <cam/cam_ccb.h>
55#include <cam/cam_periph.h>
56#include <cam/cam_sim.h>
57#include <cam/cam_xpt.h>
58#include <cam/cam_xpt_sim.h>
59#include <cam/cam_xpt_periph.h>
60#include <cam/cam_debug.h>
61
62#include <cam/scsi/scsi_all.h>
63#include <cam/scsi/scsi_message.h>
64#include <cam/scsi/scsi_pass.h>
65#include "opt_cam.h"
66
67/* Datastructures internal to the xpt layer */
68MALLOC_DEFINE(M_CAMXPT, "CAM XPT", "CAM XPT buffers");
69
70/*
71 * Definition of an async handler callback block. These are used to add
72 * SIMs and peripherals to the async callback lists.
73 */
74struct async_node {
75 SLIST_ENTRY(async_node) links;
76 u_int32_t event_enable; /* Async Event enables */
77 void (*callback)(void *arg, u_int32_t code,
78 struct cam_path *path, void *args);
79 void *callback_arg;
80};
81
82SLIST_HEAD(async_list, async_node);
83SLIST_HEAD(periph_list, cam_periph);
84static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
85
86/*
87 * This is the maximum number of high powered commands (e.g. start unit)
88 * that can be outstanding at a particular time.
89 */
90#ifndef CAM_MAX_HIGHPOWER
91#define CAM_MAX_HIGHPOWER 4
92#endif
93
94/* number of high powered commands that can go through right now */
95static int num_highpower = CAM_MAX_HIGHPOWER;
96
97/*
98 * Structure for queueing a device in a run queue.
99 * There is one run queue for allocating new ccbs,
100 * and another for sending ccbs to the controller.
101 */
102struct cam_ed_qinfo {
103 cam_pinfo pinfo;
104 struct cam_ed *device;
105};
106
107/*
108 * The CAM EDT (Existing Device Table) contains the device information for
109 * all devices for all busses in the system. The table contains a
110 * cam_ed structure for each device on the bus.
111 */
112struct cam_ed {
113 TAILQ_ENTRY(cam_ed) links;
114 struct cam_ed_qinfo alloc_ccb_entry;
115 struct cam_ed_qinfo send_ccb_entry;
116 struct cam_et *target;
117 lun_id_t lun_id;
118 struct camq drvq; /*
119 * Queue of type drivers wanting to do
120 * work on this device.
121 */
122 struct cam_ccbq ccbq; /* Queue of pending ccbs */
123 struct async_list asyncs; /* Async callback info for this B/T/L */
124 struct periph_list periphs; /* All attached devices */
125 u_int generation; /* Generation number */
126 struct cam_periph *owner; /* Peripheral driver's ownership tag */
127 struct xpt_quirk_entry *quirk; /* Oddities about this device */
128 /* Storage for the inquiry data */
129#ifdef CAM_NEW_TRAN_CODE
130 cam_proto protocol;
131 u_int protocol_version;
132 cam_xport transport;
133 u_int transport_version;
134#endif /* CAM_NEW_TRAN_CODE */
135 struct scsi_inquiry_data inq_data;
136 u_int8_t inq_flags; /*
137 * Current settings for inquiry flags.
138 * This allows us to override settings
139 * like disconnection and tagged
140 * queuing for a device.
141 */
142 u_int8_t queue_flags; /* Queue flags from the control page */
143 u_int8_t serial_num_len;
144 u_int8_t *serial_num;
145 u_int32_t qfrozen_cnt;
146 u_int32_t flags;
147#define CAM_DEV_UNCONFIGURED 0x01
148#define CAM_DEV_REL_TIMEOUT_PENDING 0x02
149#define CAM_DEV_REL_ON_COMPLETE 0x04
150#define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
151#define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
152#define CAM_DEV_TAG_AFTER_COUNT 0x20
153#define CAM_DEV_INQUIRY_DATA_VALID 0x40
154 u_int32_t tag_delay_count;
155#define CAM_TAG_DELAY_COUNT 5
156 u_int32_t tag_saved_openings;
157 u_int32_t refcount;
158 struct callout_handle c_handle;
159};
160
161/*
162 * Each target is represented by an ET (Existing Target). These
163 * entries are created when a target is successfully probed with an
164 * identify, and removed when a device fails to respond after a number
165 * of retries, or a bus rescan finds the device missing.
166 */
167struct cam_et {
168 TAILQ_HEAD(, cam_ed) ed_entries;
169 TAILQ_ENTRY(cam_et) links;
170 struct cam_eb *bus;
171 target_id_t target_id;
172 u_int32_t refcount;
173 u_int generation;
174 struct timeval last_reset;
175};
176
177/*
178 * Each bus is represented by an EB (Existing Bus). These entries
179 * are created by calls to xpt_bus_register and deleted by calls to
180 * xpt_bus_deregister.
181 */
182struct cam_eb {
183 TAILQ_HEAD(, cam_et) et_entries;
184 TAILQ_ENTRY(cam_eb) links;
185 path_id_t path_id;
186 struct cam_sim *sim;
187 struct timeval last_reset;
188 u_int32_t flags;
189#define CAM_EB_RUNQ_SCHEDULED 0x01
190 u_int32_t refcount;
191 u_int generation;
192};
193
194struct cam_path {
195 struct cam_periph *periph;
196 struct cam_eb *bus;
197 struct cam_et *target;
198 struct cam_ed *device;
199};
200
201struct xpt_quirk_entry {
202 struct scsi_inquiry_pattern inq_pat;
203 u_int8_t quirks;
204#define CAM_QUIRK_NOLUNS 0x01
205#define CAM_QUIRK_NOSERIAL 0x02
206#define CAM_QUIRK_HILUNS 0x04
207#define CAM_QUIRK_NOHILUNS 0x08
208 u_int mintags;
209 u_int maxtags;
210};
211#define CAM_SCSI2_MAXLUN 8
212/*
213 * If we're not quirked to search <= the first 8 luns
214 * and we are either quirked to search above lun 8,
215 * or we're > SCSI-2, we can look for luns above lun 8.
216 */
217#define CAN_SRCH_HI(dv) \
218 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \
219 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \
220 || SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2))
221
222typedef enum {
223 XPT_FLAG_OPEN = 0x01
224} xpt_flags;
225
226struct xpt_softc {
227 xpt_flags flags;
228 u_int32_t generation;
229};
230
231static const char quantum[] = "QUANTUM";
232static const char sony[] = "SONY";
233static const char west_digital[] = "WDIGTL";
234static const char samsung[] = "SAMSUNG";
235static const char seagate[] = "SEAGATE";
236static const char microp[] = "MICROP";
237
238static struct xpt_quirk_entry xpt_quirk_table[] =
239{
240 {
241 /* Reports QUEUE FULL for temporary resource shortages */
242 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
243 /*quirks*/0, /*mintags*/24, /*maxtags*/32
244 },
245 {
246 /* Reports QUEUE FULL for temporary resource shortages */
247 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
248 /*quirks*/0, /*mintags*/24, /*maxtags*/32
249 },
250 {
251 /* Reports QUEUE FULL for temporary resource shortages */
252 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
253 /*quirks*/0, /*mintags*/24, /*maxtags*/32
254 },
255 {
256 /* Broken tagged queuing drive */
257 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
258 /*quirks*/0, /*mintags*/0, /*maxtags*/0
259 },
260 {
261 /* Broken tagged queuing drive */
262 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
263 /*quirks*/0, /*mintags*/0, /*maxtags*/0
264 },
265 {
266 /* Broken tagged queuing drive */
267 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
268 /*quirks*/0, /*mintags*/0, /*maxtags*/0
269 },
270 {
271 /*
272 * Unfortunately, the Quantum Atlas III has the same
273 * problem as the Atlas II drives above.
274 * Reported by: "Johan Granlund" <johan@granlund.nu>
275 *
276 * For future reference, the drive with the problem was:
277 * QUANTUM QM39100TD-SW N1B0
278 *
279 * It's possible that Quantum will fix the problem in later
280 * firmware revisions. If that happens, the quirk entry
281 * will need to be made specific to the firmware revisions
282 * with the problem.
283 *
284 */
285 /* Reports QUEUE FULL for temporary resource shortages */
286 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
287 /*quirks*/0, /*mintags*/24, /*maxtags*/32
288 },
289 {
290 /*
291 * 18 Gig Atlas III, same problem as the 9G version.
292 * Reported by: Andre Albsmeier
293 * <andre.albsmeier@mchp.siemens.de>
294 *
295 * For future reference, the drive with the problem was:
296 * QUANTUM QM318000TD-S N491
297 */
298 /* Reports QUEUE FULL for temporary resource shortages */
299 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
300 /*quirks*/0, /*mintags*/24, /*maxtags*/32
301 },
302 {
303 /*
304 * Broken tagged queuing drive
305 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
306 * and: Martin Renters <martin@tdc.on.ca>
307 */
308 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
309 /*quirks*/0, /*mintags*/0, /*maxtags*/0
310 },
311 /*
312 * The Seagate Medalist Pro drives have very poor write
313 * performance with anything more than 2 tags.
314 *
315 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
316 * Drive: <SEAGATE ST36530N 1444>
317 *
318 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
319 * Drive: <SEAGATE ST34520W 1281>
320 *
321 * No one has actually reported that the 9G version
322 * (ST39140*) of the Medalist Pro has the same problem, but
323 * we're assuming that it does because the 4G and 6.5G
324 * versions of the drive are broken.
325 */
326 {
327 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
328 /*quirks*/0, /*mintags*/2, /*maxtags*/2
329 },
330 {
331 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
332 /*quirks*/0, /*mintags*/2, /*maxtags*/2
333 },
334 {
335 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
336 /*quirks*/0, /*mintags*/2, /*maxtags*/2
337 },
338 {
339 /*
340 * Slow when tagged queueing is enabled. Write performance
341 * steadily drops off with more and more concurrent
342 * transactions. Best sequential write performance with
343 * tagged queueing turned off and write caching turned on.
344 *
345 * PR: kern/10398
346 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
347 * Drive: DCAS-34330 w/ "S65A" firmware.
348 *
349 * The drive with the problem had the "S65A" firmware
350 * revision, and has also been reported (by Stephen J.
351 * Roznowski <sjr@home.net>) for a drive with the "S61A"
352 * firmware revision.
353 *
354 * Although no one has reported problems with the 2 gig
355 * version of the DCAS drive, the assumption is that it
356 * has the same problems as the 4 gig version. Therefore
357 * this quirk entries disables tagged queueing for all
358 * DCAS drives.
359 */
360 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
361 /*quirks*/0, /*mintags*/0, /*maxtags*/0
362 },
363 {
364 /* Broken tagged queuing drive */
365 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
366 /*quirks*/0, /*mintags*/0, /*maxtags*/0
367 },
368 {
369 /* Broken tagged queuing drive */
370 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
371 /*quirks*/0, /*mintags*/0, /*maxtags*/0
372 },
373 {
374 /*
375 * Broken tagged queuing drive.
376 * Submitted by:
377 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
378 * in PR kern/9535
379 */
380 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
381 /*quirks*/0, /*mintags*/0, /*maxtags*/0
382 },
383 {
384 /*
385 * Slow when tagged queueing is enabled. (1.5MB/sec versus
386 * 8MB/sec.)
387 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
388 * Best performance with these drives is achieved with
389 * tagged queueing turned off, and write caching turned on.
390 */
391 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
392 /*quirks*/0, /*mintags*/0, /*maxtags*/0
393 },
394 {
395 /*
396 * Slow when tagged queueing is enabled. (1.5MB/sec versus
397 * 8MB/sec.)
398 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
399 * Best performance with these drives is achieved with
400 * tagged queueing turned off, and write caching turned on.
401 */
402 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
403 /*quirks*/0, /*mintags*/0, /*maxtags*/0
404 },
405 {
406 /*
407 * Doesn't handle queue full condition correctly,
408 * so we need to limit maxtags to what the device
409 * can handle instead of determining this automatically.
410 */
411 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
412 /*quirks*/0, /*mintags*/2, /*maxtags*/32
413 },
414 {
415 /* Really only one LUN */
416 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
417 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
418 },
419 {
420 /* I can't believe we need a quirk for DPT volumes. */
421 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
422 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
423 /*mintags*/0, /*maxtags*/255
424 },
425 {
426 /*
427 * Many Sony CDROM drives don't like multi-LUN probing.
428 */
429 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
430 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
431 },
432 {
433 /*
434 * This drive doesn't like multiple LUN probing.
435 * Submitted by: Parag Patel <parag@cgt.com>
436 */
437 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
438 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
439 },
440 {
441 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
442 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
443 },
444 {
445 /*
446 * The 8200 doesn't like multi-lun probing, and probably
447 * don't like serial number requests either.
448 */
449 {
450 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
451 "EXB-8200*", "*"
452 },
453 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
454 },
455 {
456 /*
457 * Let's try the same as above, but for a drive that says
458 * it's an IPL-6860 but is actually an EXB 8200.
459 */
460 {
461 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
462 "IPL-6860*", "*"
463 },
464 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
465 },
466 {
467 /*
468 * These Hitachi drives don't like multi-lun probing.
469 * The PR submitter has a DK319H, but says that the Linux
470 * kernel has a similar work-around for the DK312 and DK314,
471 * so all DK31* drives are quirked here.
472 * PR: misc/18793
473 * Submitted by: Paul Haddad <paul@pth.com>
474 */
475 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
476 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
477 },
478 {
479 /*
480 * The Hitachi CJ series with J8A8 firmware apparantly has
481 * problems with tagged commands.
482 * PR: 23536
483 * Reported by: amagai@nue.org
484 */
485 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
486 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
487 },
488 {
489 /*
490 * These are the large storage arrays.
491 * Submitted by: William Carrel <william.carrel@infospace.com>
492 */
493 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
494 CAM_QUIRK_HILUNS, 2, 1024
495 },
496 {
497 /*
498 * This old revision of the TDC3600 is also SCSI-1, and
499 * hangs upon serial number probing.
500 */
501 {
502 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
503 " TDC 3600", "U07:"
504 },
505 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
506 },
507 {
508 /*
509 * Maxtor Personal Storage 3000XT (Firewire)
510 * hangs upon serial number probing.
511 */
512 {
513 T_DIRECT, SIP_MEDIA_FIXED, "Maxtor",
514 "1394 storage", "*"
515 },
516 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
517 },
518 {
519 /*
520 * Would repond to all LUNs if asked for.
521 */
522 {
523 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
524 "CP150", "*"
525 },
526 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
527 },
528 {
529 /*
530 * Would repond to all LUNs if asked for.
531 */
532 {
533 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
534 "96X2*", "*"
535 },
536 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
537 },
538 {
539 /* Submitted by: Matthew Dodd <winter@jurai.net> */
540 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
541 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
542 },
543 {
544 /* Submitted by: Matthew Dodd <winter@jurai.net> */
545 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
546 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
547 },
548 {
549 /* TeraSolutions special settings for TRC-22 RAID */
550 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
551 /*quirks*/0, /*mintags*/55, /*maxtags*/255
552 },
553 {
554 /* Veritas Storage Appliance */
555 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
556 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
557 },
558 {
559 /*
560 * Would respond to all LUNs. Device type and removable
561 * flag are jumper-selectable.
562 */
563 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
564 "Tahiti 1", "*"
565 },
566 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
567 },
568 {
569 /* EasyRAID E5A aka. areca ARC-6010 */
570 { T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
571 CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
572 },
573 {
574 /* Default tagged queuing parameters for all devices */
575 {
576 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
577 /*vendor*/"*", /*product*/"*", /*revision*/"*"
578 },
579 /*quirks*/0, /*mintags*/2, /*maxtags*/255
580 },
581};
582
583static const int xpt_quirk_table_size =
584 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
585
586typedef enum {
587 DM_RET_COPY = 0x01,
588 DM_RET_FLAG_MASK = 0x0f,
589 DM_RET_NONE = 0x00,
590 DM_RET_STOP = 0x10,
591 DM_RET_DESCEND = 0x20,
592 DM_RET_ERROR = 0x30,
593 DM_RET_ACTION_MASK = 0xf0
594} dev_match_ret;
595
596typedef enum {
597 XPT_DEPTH_BUS,
598 XPT_DEPTH_TARGET,
599 XPT_DEPTH_DEVICE,
600 XPT_DEPTH_PERIPH
601} xpt_traverse_depth;
602
603struct xpt_traverse_config {
604 xpt_traverse_depth depth;
605 void *tr_func;
606 void *tr_arg;
607};
608
609typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
610typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
611typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
612typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
613typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
614
615/* Transport layer configuration information */
616static struct xpt_softc xsoftc;
617
618/* Queues for our software interrupt handler */
619typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
620static cam_isrq_t cam_bioq;
621static struct mtx cam_bioq_lock;
622
623/* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
624static SLIST_HEAD(,ccb_hdr) ccb_freeq;
625static u_int xpt_max_ccbs; /*
626 * Maximum size of ccb pool. Modified as
627 * devices are added/removed or have their
628 * opening counts changed.
629 */
630static u_int xpt_ccb_count; /* Current count of allocated ccbs */
631
632struct cam_periph *xpt_periph;
633
634static periph_init_t xpt_periph_init;
635
636static periph_init_t probe_periph_init;
637
638static struct periph_driver xpt_driver =
639{
640 xpt_periph_init, "xpt",
641 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
642};
643
644static struct periph_driver probe_driver =
645{
646 probe_periph_init, "probe",
647 TAILQ_HEAD_INITIALIZER(probe_driver.units)
648};
649
650PERIPHDRIVER_DECLARE(xpt, xpt_driver);
651PERIPHDRIVER_DECLARE(probe, probe_driver);
652
653
654static d_open_t xptopen;
655static d_close_t xptclose;
656static d_ioctl_t xptioctl;
657
658static struct cdevsw xpt_cdevsw = {
659 .d_version = D_VERSION,
660 .d_flags = D_NEEDGIANT,
661 .d_open = xptopen,
662 .d_close = xptclose,
663 .d_ioctl = xptioctl,
664 .d_name = "xpt",
665};
666
667static struct intr_config_hook *xpt_config_hook;
668
669/* Registered busses */
670static TAILQ_HEAD(,cam_eb) xpt_busses;
671static u_int bus_generation;
672
673/* Storage for debugging datastructures */
674#ifdef CAMDEBUG
675struct cam_path *cam_dpath;
676u_int32_t cam_dflags;
677u_int32_t cam_debug_delay;
678#endif
679
680/* Pointers to software interrupt handlers */
681static void *cambio_ih;
682
683#if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
684#error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
685#endif
686
687/*
688 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
689 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
690 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
691 */
692#if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
693 || defined(CAM_DEBUG_LUN)
694#ifdef CAMDEBUG
695#if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
696 || !defined(CAM_DEBUG_LUN)
697#error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
698 and CAM_DEBUG_LUN"
699#endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
700#else /* !CAMDEBUG */
701#error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
702#endif /* CAMDEBUG */
703#endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
704
705/* Our boot-time initialization hook */
706static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
707
708static moduledata_t cam_moduledata = {
709 "cam",
710 cam_module_event_handler,
711 NULL
712};
713
714static void xpt_init(void *);
715
716DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
717MODULE_VERSION(cam, 1);
718
719
720static cam_status xpt_compile_path(struct cam_path *new_path,
721 struct cam_periph *perph,
722 path_id_t path_id,
723 target_id_t target_id,
724 lun_id_t lun_id);
725
726static void xpt_release_path(struct cam_path *path);
727
728static void xpt_async_bcast(struct async_list *async_head,
729 u_int32_t async_code,
730 struct cam_path *path,
731 void *async_arg);
732static void xpt_dev_async(u_int32_t async_code,
733 struct cam_eb *bus,
734 struct cam_et *target,
735 struct cam_ed *device,
736 void *async_arg);
737static path_id_t xptnextfreepathid(void);
738static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
739static union ccb *xpt_get_ccb(struct cam_ed *device);
740static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
741 u_int32_t new_priority);
742static void xpt_run_dev_allocq(struct cam_eb *bus);
743static void xpt_run_dev_sendq(struct cam_eb *bus);
744static timeout_t xpt_release_devq_timeout;
745static timeout_t xpt_release_simq_timeout;
746static void xpt_release_bus(struct cam_eb *bus);
747static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
748 int run_queue);
749static struct cam_et*
750 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
751static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
752static struct cam_ed*
753 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
754 lun_id_t lun_id);
755static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
756 struct cam_ed *device);
757static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
758static struct cam_eb*
759 xpt_find_bus(path_id_t path_id);
760static struct cam_et*
761 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
762static struct cam_ed*
763 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
764static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
765static void xpt_scan_lun(struct cam_periph *periph,
766 struct cam_path *path, cam_flags flags,
767 union ccb *ccb);
768static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
769static xpt_busfunc_t xptconfigbuscountfunc;
770static xpt_busfunc_t xptconfigfunc;
771static void xpt_config(void *arg);
772static xpt_devicefunc_t xptpassannouncefunc;
773static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
774static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
775static void xptpoll(struct cam_sim *sim);
776static void camisr(void *);
777#if 0
778static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
779static void xptasync(struct cam_periph *periph,
780 u_int32_t code, cam_path *path);
781#endif
782static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
783 u_int num_patterns, struct cam_eb *bus);
784static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
785 u_int num_patterns,
786 struct cam_ed *device);
787static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
788 u_int num_patterns,
789 struct cam_periph *periph);
790static xpt_busfunc_t xptedtbusfunc;
791static xpt_targetfunc_t xptedttargetfunc;
792static xpt_devicefunc_t xptedtdevicefunc;
793static xpt_periphfunc_t xptedtperiphfunc;
794static xpt_pdrvfunc_t xptplistpdrvfunc;
795static xpt_periphfunc_t xptplistperiphfunc;
796static int xptedtmatch(struct ccb_dev_match *cdm);
797static int xptperiphlistmatch(struct ccb_dev_match *cdm);
798static int xptbustraverse(struct cam_eb *start_bus,
799 xpt_busfunc_t *tr_func, void *arg);
800static int xpttargettraverse(struct cam_eb *bus,
801 struct cam_et *start_target,
802 xpt_targetfunc_t *tr_func, void *arg);
803static int xptdevicetraverse(struct cam_et *target,
804 struct cam_ed *start_device,
805 xpt_devicefunc_t *tr_func, void *arg);
806static int xptperiphtraverse(struct cam_ed *device,
807 struct cam_periph *start_periph,
808 xpt_periphfunc_t *tr_func, void *arg);
809static int xptpdrvtraverse(struct periph_driver **start_pdrv,
810 xpt_pdrvfunc_t *tr_func, void *arg);
811static int xptpdperiphtraverse(struct periph_driver **pdrv,
812 struct cam_periph *start_periph,
813 xpt_periphfunc_t *tr_func,
814 void *arg);
815static xpt_busfunc_t xptdefbusfunc;
816static xpt_targetfunc_t xptdeftargetfunc;
817static xpt_devicefunc_t xptdefdevicefunc;
818static xpt_periphfunc_t xptdefperiphfunc;
819static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
820#ifdef notusedyet
821static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
822 void *arg);
823#endif
824static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
825 void *arg);
826#ifdef notusedyet
827static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
828 void *arg);
829#endif
830static xpt_devicefunc_t xptsetasyncfunc;
831static xpt_busfunc_t xptsetasyncbusfunc;
832static cam_status xptregister(struct cam_periph *periph,
833 void *arg);
834static cam_status proberegister(struct cam_periph *periph,
835 void *arg);
836static void probeschedule(struct cam_periph *probe_periph);
837static void probestart(struct cam_periph *periph, union ccb *start_ccb);
838static void proberequestdefaultnegotiation(struct cam_periph *periph);
839static void probedone(struct cam_periph *periph, union ccb *done_ccb);
840static void probecleanup(struct cam_periph *periph);
841static void xpt_find_quirk(struct cam_ed *device);
842#ifdef CAM_NEW_TRAN_CODE
843static void xpt_devise_transport(struct cam_path *path);
844#endif /* CAM_NEW_TRAN_CODE */
845static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
846 struct cam_ed *device,
847 int async_update);
848static void xpt_toggle_tags(struct cam_path *path);
849static void xpt_start_tags(struct cam_path *path);
850static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
851 struct cam_ed *dev);
852static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
853 struct cam_ed *dev);
854static __inline int periph_is_queued(struct cam_periph *periph);
855static __inline int device_is_alloc_queued(struct cam_ed *device);
856static __inline int device_is_send_queued(struct cam_ed *device);
857static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
858
859static __inline int
860xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
861{
862 int retval;
863
864 if (dev->ccbq.devq_openings > 0) {
865 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
866 cam_ccbq_resize(&dev->ccbq,
867 dev->ccbq.dev_openings
868 + dev->ccbq.dev_active);
869 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
870 }
871 /*
872 * The priority of a device waiting for CCB resources
873 * is that of the the highest priority peripheral driver
874 * enqueued.
875 */
876 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
877 &dev->alloc_ccb_entry.pinfo,
878 CAMQ_GET_HEAD(&dev->drvq)->priority);
879 } else {
880 retval = 0;
881 }
882
883 return (retval);
884}
885
886static __inline int
887xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
888{
889 int retval;
890
891 if (dev->ccbq.dev_openings > 0) {
892 /*
893 * The priority of a device waiting for controller
894 * resources is that of the the highest priority CCB
895 * enqueued.
896 */
897 retval =
898 xpt_schedule_dev(&bus->sim->devq->send_queue,
899 &dev->send_ccb_entry.pinfo,
900 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
901 } else {
902 retval = 0;
903 }
904 return (retval);
905}
906
907static __inline int
908periph_is_queued(struct cam_periph *periph)
909{
910 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
911}
912
913static __inline int
914device_is_alloc_queued(struct cam_ed *device)
915{
916 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
917}
918
919static __inline int
920device_is_send_queued(struct cam_ed *device)
921{
922 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
923}
924
925static __inline int
926dev_allocq_is_runnable(struct cam_devq *devq)
927{
928 /*
929 * Have work to do.
930 * Have space to do more work.
931 * Allowed to do work.
932 */
933 return ((devq->alloc_queue.qfrozen_cnt == 0)
934 && (devq->alloc_queue.entries > 0)
935 && (devq->alloc_openings > 0));
936}
937
938static void
939xpt_periph_init()
940{
941 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
942}
943
944static void
945probe_periph_init()
946{
947}
948
949
950static void
951xptdone(struct cam_periph *periph, union ccb *done_ccb)
952{
953 /* Caller will release the CCB */
954 wakeup(&done_ccb->ccb_h.cbfcnp);
955}
956
957static int
958xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
959{
960 int unit;
961
962 unit = minor(dev) & 0xff;
963
964 /*
965 * Only allow read-write access.
966 */
967 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
968 return(EPERM);
969
970 /*
971 * We don't allow nonblocking access.
972 */
973 if ((flags & O_NONBLOCK) != 0) {
974 printf("xpt%d: can't do nonblocking access\n", unit);
975 return(ENODEV);
976 }
977
978 /*
979 * We only have one transport layer right now. If someone accesses
980 * us via something other than minor number 1, point out their
981 * mistake.
982 */
983 if (unit != 0) {
984 printf("xptopen: got invalid xpt unit %d\n", unit);
985 return(ENXIO);
986 }
987
988 /* Mark ourselves open */
989 xsoftc.flags |= XPT_FLAG_OPEN;
990
991 return(0);
992}
993
994static int
995xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
996{
997 int unit;
998
999 unit = minor(dev) & 0xff;
1000
1001 /*
1002 * We only have one transport layer right now. If someone accesses
1003 * us via something other than minor number 1, point out their
1004 * mistake.
1005 */
1006 if (unit != 0) {
1007 printf("xptclose: got invalid xpt unit %d\n", unit);
1008 return(ENXIO);
1009 }
1010
1011 /* Mark ourselves closed */
1012 xsoftc.flags &= ~XPT_FLAG_OPEN;
1013
1014 return(0);
1015}
1016
1017static int
1018xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1019{
1020 int unit, error;
1021
1022 error = 0;
1023 unit = minor(dev) & 0xff;
1024
1025 /*
1026 * We only have one transport layer right now. If someone accesses
1027 * us via something other than minor number 1, point out their
1028 * mistake.
1029 */
1030 if (unit != 0) {
1031 printf("xptioctl: got invalid xpt unit %d\n", unit);
1032 return(ENXIO);
1033 }
1034
1035 switch(cmd) {
1036 /*
1037 * For the transport layer CAMIOCOMMAND ioctl, we really only want
1038 * to accept CCB types that don't quite make sense to send through a
1039 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
1040 * in the CAM spec.
1041 */
1042 case CAMIOCOMMAND: {
1043 union ccb *ccb;
1044 union ccb *inccb;
1045
1046 inccb = (union ccb *)addr;
1047
1048 switch(inccb->ccb_h.func_code) {
1049 case XPT_SCAN_BUS:
1050 case XPT_RESET_BUS:
1051 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
1052 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1053 error = EINVAL;
1054 break;
1055 }
1056 /* FALLTHROUGH */
1057 case XPT_PATH_INQ:
1058 case XPT_ENG_INQ:
1059 case XPT_SCAN_LUN:
1060
1061 ccb = xpt_alloc_ccb();
1062
1063 /*
1064 * Create a path using the bus, target, and lun the
1065 * user passed in.
1066 */
1067 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1068 inccb->ccb_h.path_id,
1069 inccb->ccb_h.target_id,
1070 inccb->ccb_h.target_lun) !=
1071 CAM_REQ_CMP){
1072 error = EINVAL;
1073 xpt_free_ccb(ccb);
1074 break;
1075 }
1076 /* Ensure all of our fields are correct */
1077 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1078 inccb->ccb_h.pinfo.priority);
1079 xpt_merge_ccb(ccb, inccb);
1080 ccb->ccb_h.cbfcnp = xptdone;
1081 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1082 bcopy(ccb, inccb, sizeof(union ccb));
1083 xpt_free_path(ccb->ccb_h.path);
1084 xpt_free_ccb(ccb);
1085 break;
1086
1087 case XPT_DEBUG: {
1088 union ccb ccb;
1089
1090 /*
1091 * This is an immediate CCB, so it's okay to
1092 * allocate it on the stack.
1093 */
1094
1095 /*
1096 * Create a path using the bus, target, and lun the
1097 * user passed in.
1098 */
1099 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1100 inccb->ccb_h.path_id,
1101 inccb->ccb_h.target_id,
1102 inccb->ccb_h.target_lun) !=
1103 CAM_REQ_CMP){
1104 error = EINVAL;
1105 break;
1106 }
1107 /* Ensure all of our fields are correct */
1108 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1109 inccb->ccb_h.pinfo.priority);
1110 xpt_merge_ccb(&ccb, inccb);
1111 ccb.ccb_h.cbfcnp = xptdone;
1112 xpt_action(&ccb);
1113 bcopy(&ccb, inccb, sizeof(union ccb));
1114 xpt_free_path(ccb.ccb_h.path);
1115 break;
1116
1117 }
1118 case XPT_DEV_MATCH: {
1119 struct cam_periph_map_info mapinfo;
1120 struct cam_path *old_path;
1121
1122 /*
1123 * We can't deal with physical addresses for this
1124 * type of transaction.
1125 */
1126 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1127 error = EINVAL;
1128 break;
1129 }
1130
1131 /*
1132 * Save this in case the caller had it set to
1133 * something in particular.
1134 */
1135 old_path = inccb->ccb_h.path;
1136
1137 /*
1138 * We really don't need a path for the matching
1139 * code. The path is needed because of the
1140 * debugging statements in xpt_action(). They
1141 * assume that the CCB has a valid path.
1142 */
1143 inccb->ccb_h.path = xpt_periph->path;
1144
1145 bzero(&mapinfo, sizeof(mapinfo));
1146
1147 /*
1148 * Map the pattern and match buffers into kernel
1149 * virtual address space.
1150 */
1151 error = cam_periph_mapmem(inccb, &mapinfo);
1152
1153 if (error) {
1154 inccb->ccb_h.path = old_path;
1155 break;
1156 }
1157
1158 /*
1159 * This is an immediate CCB, we can send it on directly.
1160 */
1161 xpt_action(inccb);
1162
1163 /*
1164 * Map the buffers back into user space.
1165 */
1166 cam_periph_unmapmem(inccb, &mapinfo);
1167
1168 inccb->ccb_h.path = old_path;
1169
1170 error = 0;
1171 break;
1172 }
1173 default:
1174 error = ENOTSUP;
1175 break;
1176 }
1177 break;
1178 }
1179 /*
1180 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1181 * with the periphal driver name and unit name filled in. The other
1182 * fields don't really matter as input. The passthrough driver name
1183 * ("pass"), and unit number are passed back in the ccb. The current
1184 * device generation number, and the index into the device peripheral
1185 * driver list, and the status are also passed back. Note that
1186 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1187 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1188 * (or rather should be) impossible for the device peripheral driver
1189 * list to change since we look at the whole thing in one pass, and
1190 * we do it with splcam protection.
1191 *
1192 */
1193 case CAMGETPASSTHRU: {
1194 union ccb *ccb;
1195 struct cam_periph *periph;
1196 struct periph_driver **p_drv;
1197 char *name;
1198 u_int unit;
1199 u_int cur_generation;
1200 int base_periph_found;
1201 int splbreaknum;
1202 int s;
1203
1204 ccb = (union ccb *)addr;
1205 unit = ccb->cgdl.unit_number;
1206 name = ccb->cgdl.periph_name;
1207 /*
1208 * Every 100 devices, we want to drop our spl protection to
1209 * give the software interrupt handler a chance to run.
1210 * Most systems won't run into this check, but this should
1211 * avoid starvation in the software interrupt handler in
1212 * large systems.
1213 */
1214 splbreaknum = 100;
1215
1216 ccb = (union ccb *)addr;
1217
1218 base_periph_found = 0;
1219
1220 /*
1221 * Sanity check -- make sure we don't get a null peripheral
1222 * driver name.
1223 */
1224 if (*ccb->cgdl.periph_name == '\0') {
1225 error = EINVAL;
1226 break;
1227 }
1228
1229 /* Keep the list from changing while we traverse it */
1230 s = splcam();
1231ptstartover:
1232 cur_generation = xsoftc.generation;
1233
1234 /* first find our driver in the list of drivers */
1235 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
1236 if (strcmp((*p_drv)->driver_name, name) == 0)
1237 break;
1238
1239 if (*p_drv == NULL) {
1240 splx(s);
1241 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1242 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1243 *ccb->cgdl.periph_name = '\0';
1244 ccb->cgdl.unit_number = 0;
1245 error = ENOENT;
1246 break;
1247 }
1248
1249 /*
1250 * Run through every peripheral instance of this driver
1251 * and check to see whether it matches the unit passed
1252 * in by the user. If it does, get out of the loops and
1253 * find the passthrough driver associated with that
1254 * peripheral driver.
1255 */
1256 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1257 periph = TAILQ_NEXT(periph, unit_links)) {
1258
1259 if (periph->unit_number == unit) {
1260 break;
1261 } else if (--splbreaknum == 0) {
1262 splx(s);
1263 s = splcam();
1264 splbreaknum = 100;
1265 if (cur_generation != xsoftc.generation)
1266 goto ptstartover;
1267 }
1268 }
1269 /*
1270 * If we found the peripheral driver that the user passed
1271 * in, go through all of the peripheral drivers for that
1272 * particular device and look for a passthrough driver.
1273 */
1274 if (periph != NULL) {
1275 struct cam_ed *device;
1276 int i;
1277
1278 base_periph_found = 1;
1279 device = periph->path->device;
1280 for (i = 0, periph = SLIST_FIRST(&device->periphs);
1281 periph != NULL;
1282 periph = SLIST_NEXT(periph, periph_links), i++) {
1283 /*
1284 * Check to see whether we have a
1285 * passthrough device or not.
1286 */
1287 if (strcmp(periph->periph_name, "pass") == 0) {
1288 /*
1289 * Fill in the getdevlist fields.
1290 */
1291 strcpy(ccb->cgdl.periph_name,
1292 periph->periph_name);
1293 ccb->cgdl.unit_number =
1294 periph->unit_number;
1295 if (SLIST_NEXT(periph, periph_links))
1296 ccb->cgdl.status =
1297 CAM_GDEVLIST_MORE_DEVS;
1298 else
1299 ccb->cgdl.status =
1300 CAM_GDEVLIST_LAST_DEVICE;
1301 ccb->cgdl.generation =
1302 device->generation;
1303 ccb->cgdl.index = i;
1304 /*
1305 * Fill in some CCB header fields
1306 * that the user may want.
1307 */
1308 ccb->ccb_h.path_id =
1309 periph->path->bus->path_id;
1310 ccb->ccb_h.target_id =
1311 periph->path->target->target_id;
1312 ccb->ccb_h.target_lun =
1313 periph->path->device->lun_id;
1314 ccb->ccb_h.status = CAM_REQ_CMP;
1315 break;
1316 }
1317 }
1318 }
1319
1320 /*
1321 * If the periph is null here, one of two things has
1322 * happened. The first possibility is that we couldn't
1323 * find the unit number of the particular peripheral driver
1324 * that the user is asking about. e.g. the user asks for
1325 * the passthrough driver for "da11". We find the list of
1326 * "da" peripherals all right, but there is no unit 11.
1327 * The other possibility is that we went through the list
1328 * of peripheral drivers attached to the device structure,
1329 * but didn't find one with the name "pass". Either way,
1330 * we return ENOENT, since we couldn't find something.
1331 */
1332 if (periph == NULL) {
1333 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1334 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1335 *ccb->cgdl.periph_name = '\0';
1336 ccb->cgdl.unit_number = 0;
1337 error = ENOENT;
1338 /*
1339 * It is unfortunate that this is even necessary,
1340 * but there are many, many clueless users out there.
1341 * If this is true, the user is looking for the
1342 * passthrough driver, but doesn't have one in his
1343 * kernel.
1344 */
1345 if (base_periph_found == 1) {
1346 printf("xptioctl: pass driver is not in the "
1347 "kernel\n");
1348 printf("xptioctl: put \"device pass0\" in "
1349 "your kernel config file\n");
1350 }
1351 }
1352 splx(s);
1353 break;
1354 }
1355 default:
1356 error = ENOTTY;
1357 break;
1358 }
1359
1360 return(error);
1361}
1362
1363static int
1364cam_module_event_handler(module_t mod, int what, void *arg)
1365{
1366 if (what == MOD_LOAD) {
1367 xpt_init(NULL);
1368 } else if (what == MOD_UNLOAD) {
1369 return EBUSY;
1370 } else {
1371 return EOPNOTSUPP;
1372 }
1373
1374 return 0;
1375}
1376
1377/* Functions accessed by the peripheral drivers */
1378static void
1379xpt_init(dummy)
1380 void *dummy;
1381{
1382 struct cam_sim *xpt_sim;
1383 struct cam_path *path;
1384 struct cam_devq *devq;
1385 cam_status status;
1386
1387 TAILQ_INIT(&xpt_busses);
1388 TAILQ_INIT(&cam_bioq);
1389 SLIST_INIT(&ccb_freeq);
1390 STAILQ_INIT(&highpowerq);
1391
1392 mtx_init(&cam_bioq_lock, "CAM BIOQ lock", NULL, MTX_DEF);
1393
1394 /*
1395 * The xpt layer is, itself, the equivelent of a SIM.
1396 * Allow 16 ccbs in the ccb pool for it. This should
1397 * give decent parallelism when we probe busses and
1398 * perform other XPT functions.
1399 */
1400 devq = cam_simq_alloc(16);
1401 xpt_sim = cam_sim_alloc(xptaction,
1402 xptpoll,
1403 "xpt",
1404 /*softc*/NULL,
1405 /*unit*/0,
1406 /*max_dev_transactions*/0,
1407 /*max_tagged_dev_transactions*/0,
1408 devq);
1409 xpt_max_ccbs = 16;
1410
1411 xpt_bus_register(xpt_sim, /*bus #*/0);
1412
1413 /*
1414 * Looking at the XPT from the SIM layer, the XPT is
1415 * the equivelent of a peripheral driver. Allocate
1416 * a peripheral driver entry for us.
1417 */
1418 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1419 CAM_TARGET_WILDCARD,
1420 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1421 printf("xpt_init: xpt_create_path failed with status %#x,"
1422 " failing attach\n", status);
1423 return;
1424 }
1425
1426 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1427 path, NULL, 0, NULL);
1428 xpt_free_path(path);
1429
1430 xpt_sim->softc = xpt_periph;
1431
1432 /*
1433 * Register a callback for when interrupts are enabled.
1434 */
1435 xpt_config_hook =
1436 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1437 M_TEMP, M_NOWAIT | M_ZERO);
1438 if (xpt_config_hook == NULL) {
1439 printf("xpt_init: Cannot malloc config hook "
1440 "- failing attach\n");
1441 return;
1442 }
1443
1444 xpt_config_hook->ich_func = xpt_config;
1445 if (config_intrhook_establish(xpt_config_hook) != 0) {
1446 free (xpt_config_hook, M_TEMP);
1447 printf("xpt_init: config_intrhook_establish failed "
1448 "- failing attach\n");
1449 }
1450
1451 /* Install our software interrupt handlers */
1452 swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, 0, &cambio_ih);
1453}
1454
1455static cam_status
1456xptregister(struct cam_periph *periph, void *arg)
1457{
1458 if (periph == NULL) {
1459 printf("xptregister: periph was NULL!!\n");
1460 return(CAM_REQ_CMP_ERR);
1461 }
1462
1463 periph->softc = NULL;
1464
1465 xpt_periph = periph;
1466
1467 return(CAM_REQ_CMP);
1468}
1469
1470int32_t
1471xpt_add_periph(struct cam_periph *periph)
1472{
1473 struct cam_ed *device;
1474 int32_t status;
1475 struct periph_list *periph_head;
1476
1477 GIANT_REQUIRED;
1478
1479 device = periph->path->device;
1480
1481 periph_head = &device->periphs;
1482
1483 status = CAM_REQ_CMP;
1484
1485 if (device != NULL) {
1486 int s;
1487
1488 /*
1489 * Make room for this peripheral
1490 * so it will fit in the queue
1491 * when it's scheduled to run
1492 */
1493 s = splsoftcam();
1494 status = camq_resize(&device->drvq,
1495 device->drvq.array_size + 1);
1496
1497 device->generation++;
1498
1499 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1500
1501 splx(s);
1502 }
1503
1504 xsoftc.generation++;
1505
1506 return (status);
1507}
1508
1509void
1510xpt_remove_periph(struct cam_periph *periph)
1511{
1512 struct cam_ed *device;
1513
1514 GIANT_REQUIRED;
1515
1516 device = periph->path->device;
1517
1518 if (device != NULL) {
1519 int s;
1520 struct periph_list *periph_head;
1521
1522 periph_head = &device->periphs;
1523
1524 /* Release the slot for this peripheral */
1525 s = splsoftcam();
1526 camq_resize(&device->drvq, device->drvq.array_size - 1);
1527
1528 device->generation++;
1529
1530 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1531
1532 splx(s);
1533 }
1534
1535 xsoftc.generation++;
1536
1537}
1538
1539#ifdef CAM_NEW_TRAN_CODE
1540
1541void
1542xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1543{
1544 struct ccb_pathinq cpi;
1545 struct ccb_trans_settings cts;
1546 struct cam_path *path;
1547 u_int speed;
1548 u_int freq;
1549 u_int mb;
1550 int s;
1551
1552 GIANT_REQUIRED;
1553
1554 path = periph->path;
1555 /*
1556 * To ensure that this is printed in one piece,
1557 * mask out CAM interrupts.
1558 */
1559 s = splsoftcam();
1560 printf("%s%d at %s%d bus %d target %d lun %d\n",
1561 periph->periph_name, periph->unit_number,
1562 path->bus->sim->sim_name,
1563 path->bus->sim->unit_number,
1564 path->bus->sim->bus_id,
1565 path->target->target_id,
1566 path->device->lun_id);
1567 printf("%s%d: ", periph->periph_name, periph->unit_number);
1568 scsi_print_inquiry(&path->device->inq_data);
1569 if (bootverbose && path->device->serial_num_len > 0) {
1570 /* Don't wrap the screen - print only the first 60 chars */
1571 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1572 periph->unit_number, path->device->serial_num);
1573 }
1574 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1575 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1576 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1577 xpt_action((union ccb*)&cts);
1578
1579 /* Ask the SIM for its base transfer speed */
1580 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1581 cpi.ccb_h.func_code = XPT_PATH_INQ;
1582 xpt_action((union ccb *)&cpi);
1583
1584 speed = cpi.base_transfer_speed;
1585 freq = 0;
1586 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1587 struct ccb_trans_settings_spi *spi;
1588
1589 spi = &cts.xport_specific.spi;
1590 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1591 && spi->sync_offset != 0) {
1592 freq = scsi_calc_syncsrate(spi->sync_period);
1593 speed = freq;
1594 }
1595
1596 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1597 speed *= (0x01 << spi->bus_width);
1598 }
1599
1600 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1601 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1602 if (fc->valid & CTS_FC_VALID_SPEED) {
1603 speed = fc->bitrate;
1604 }
1605 }
1606
1607 mb = speed / 1000;
1608 if (mb > 0)
1609 printf("%s%d: %d.%03dMB/s transfers",
1610 periph->periph_name, periph->unit_number,
1611 mb, speed % 1000);
1612 else
1613 printf("%s%d: %dKB/s transfers", periph->periph_name,
1614 periph->unit_number, speed);
1615 /* Report additional information about SPI connections */
1616 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1617 struct ccb_trans_settings_spi *spi;
1618
1619 spi = &cts.xport_specific.spi;
1620 if (freq != 0) {
1621 printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1622 freq % 1000,
1623 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1624 ? " DT" : "",
1625 spi->sync_offset);
1626 }
1627 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1628 && spi->bus_width > 0) {
1629 if (freq != 0) {
1630 printf(", ");
1631 } else {
1632 printf(" (");
1633 }
1634 printf("%dbit)", 8 * (0x01 << spi->bus_width));
1635 } else if (freq != 0) {
1636 printf(")");
1637 }
1638 }
1639 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1640 struct ccb_trans_settings_fc *fc;
1641
1642 fc = &cts.xport_specific.fc;
1643 if (fc->valid & CTS_FC_VALID_WWNN)
1644 printf(" WWNN 0x%llx", (long long) fc->wwnn);
1645 if (fc->valid & CTS_FC_VALID_WWPN)
1646 printf(" WWPN 0x%llx", (long long) fc->wwpn);
1647 if (fc->valid & CTS_FC_VALID_PORT)
1648 printf(" PortID 0x%x", fc->port);
1649 }
1650
1651 if (path->device->inq_flags & SID_CmdQue
1652 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1653 printf("\n%s%d: Tagged Queueing Enabled",
1654 periph->periph_name, periph->unit_number);
1655 }
1656 printf("\n");
1657
1658 /*
1659 * We only want to print the caller's announce string if they've
1660 * passed one in..
1661 */
1662 if (announce_string != NULL)
1663 printf("%s%d: %s\n", periph->periph_name,
1664 periph->unit_number, announce_string);
1665 splx(s);
1666}
1667#else /* CAM_NEW_TRAN_CODE */
1668void
1669xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1670{
1671 int s;
1672 u_int mb;
1673 struct cam_path *path;
1674 struct ccb_trans_settings cts;
1675
1676 GIANT_REQUIRED;
1677
1678 path = periph->path;
1679 /*
1680 * To ensure that this is printed in one piece,
1681 * mask out CAM interrupts.
1682 */
1683 s = splsoftcam();
1684 printf("%s%d at %s%d bus %d target %d lun %d\n",
1685 periph->periph_name, periph->unit_number,
1686 path->bus->sim->sim_name,
1687 path->bus->sim->unit_number,
1688 path->bus->sim->bus_id,
1689 path->target->target_id,
1690 path->device->lun_id);
1691 printf("%s%d: ", periph->periph_name, periph->unit_number);
1692 scsi_print_inquiry(&path->device->inq_data);
1693 if ((bootverbose)
1694 && (path->device->serial_num_len > 0)) {
1695 /* Don't wrap the screen - print only the first 60 chars */
1696 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1697 periph->unit_number, path->device->serial_num);
1698 }
1699 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1700 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1701 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1702 xpt_action((union ccb*)&cts);
1703 if (cts.ccb_h.status == CAM_REQ_CMP) {
1704 u_int speed;
1705 u_int freq;
1706
1707 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1708 && cts.sync_offset != 0) {
1709 freq = scsi_calc_syncsrate(cts.sync_period);
1710 speed = freq;
1711 } else {
1712 struct ccb_pathinq cpi;
1713
1714 /* Ask the SIM for its base transfer speed */
1715 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1716 cpi.ccb_h.func_code = XPT_PATH_INQ;
1717 xpt_action((union ccb *)&cpi);
1718
1719 speed = cpi.base_transfer_speed;
1720 freq = 0;
1721 }
1722 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1723 speed *= (0x01 << cts.bus_width);
1724 mb = speed / 1000;
1725 if (mb > 0)
1726 printf("%s%d: %d.%03dMB/s transfers",
1727 periph->periph_name, periph->unit_number,
1728 mb, speed % 1000);
1729 else
1730 printf("%s%d: %dKB/s transfers", periph->periph_name,
1731 periph->unit_number, speed);
1732 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1733 && cts.sync_offset != 0) {
1734 printf(" (%d.%03dMHz, offset %d", freq / 1000,
1735 freq % 1000, cts.sync_offset);
1736 }
1737 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1738 && cts.bus_width > 0) {
1739 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1740 && cts.sync_offset != 0) {
1741 printf(", ");
1742 } else {
1743 printf(" (");
1744 }
1745 printf("%dbit)", 8 * (0x01 << cts.bus_width));
1746 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1747 && cts.sync_offset != 0) {
1748 printf(")");
1749 }
1750
1751 if (path->device->inq_flags & SID_CmdQue
1752 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1753 printf(", Tagged Queueing Enabled");
1754 }
1755
1756 printf("\n");
1757 } else if (path->device->inq_flags & SID_CmdQue
1758 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1759 printf("%s%d: Tagged Queueing Enabled\n",
1760 periph->periph_name, periph->unit_number);
1761 }
1762
1763 /*
1764 * We only want to print the caller's announce string if they've
1765 * passed one in..
1766 */
1767 if (announce_string != NULL)
1768 printf("%s%d: %s\n", periph->periph_name,
1769 periph->unit_number, announce_string);
1770 splx(s);
1771}
1772
1773#endif /* CAM_NEW_TRAN_CODE */
1774
1775static dev_match_ret
1776xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1777 struct cam_eb *bus)
1778{
1779 dev_match_ret retval;
1780 int i;
1781
1782 retval = DM_RET_NONE;
1783
1784 /*
1785 * If we aren't given something to match against, that's an error.
1786 */
1787 if (bus == NULL)
1788 return(DM_RET_ERROR);
1789
1790 /*
1791 * If there are no match entries, then this bus matches no
1792 * matter what.
1793 */
1794 if ((patterns == NULL) || (num_patterns == 0))
1795 return(DM_RET_DESCEND | DM_RET_COPY);
1796
1797 for (i = 0; i < num_patterns; i++) {
1798 struct bus_match_pattern *cur_pattern;
1799
1800 /*
1801 * If the pattern in question isn't for a bus node, we
1802 * aren't interested. However, we do indicate to the
1803 * calling routine that we should continue descending the
1804 * tree, since the user wants to match against lower-level
1805 * EDT elements.
1806 */
1807 if (patterns[i].type != DEV_MATCH_BUS) {
1808 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1809 retval |= DM_RET_DESCEND;
1810 continue;
1811 }
1812
1813 cur_pattern = &patterns[i].pattern.bus_pattern;
1814
1815 /*
1816 * If they want to match any bus node, we give them any
1817 * device node.
1818 */
1819 if (cur_pattern->flags == BUS_MATCH_ANY) {
1820 /* set the copy flag */
1821 retval |= DM_RET_COPY;
1822
1823 /*
1824 * If we've already decided on an action, go ahead
1825 * and return.
1826 */
1827 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1828 return(retval);
1829 }
1830
1831 /*
1832 * Not sure why someone would do this...
1833 */
1834 if (cur_pattern->flags == BUS_MATCH_NONE)
1835 continue;
1836
1837 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1838 && (cur_pattern->path_id != bus->path_id))
1839 continue;
1840
1841 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1842 && (cur_pattern->bus_id != bus->sim->bus_id))
1843 continue;
1844
1845 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1846 && (cur_pattern->unit_number != bus->sim->unit_number))
1847 continue;
1848
1849 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1850 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1851 DEV_IDLEN) != 0))
1852 continue;
1853
1854 /*
1855 * If we get to this point, the user definitely wants
1856 * information on this bus. So tell the caller to copy the
1857 * data out.
1858 */
1859 retval |= DM_RET_COPY;
1860
1861 /*
1862 * If the return action has been set to descend, then we
1863 * know that we've already seen a non-bus matching
1864 * expression, therefore we need to further descend the tree.
1865 * This won't change by continuing around the loop, so we
1866 * go ahead and return. If we haven't seen a non-bus
1867 * matching expression, we keep going around the loop until
1868 * we exhaust the matching expressions. We'll set the stop
1869 * flag once we fall out of the loop.
1870 */
1871 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1872 return(retval);
1873 }
1874
1875 /*
1876 * If the return action hasn't been set to descend yet, that means
1877 * we haven't seen anything other than bus matching patterns. So
1878 * tell the caller to stop descending the tree -- the user doesn't
1879 * want to match against lower level tree elements.
1880 */
1881 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1882 retval |= DM_RET_STOP;
1883
1884 return(retval);
1885}
1886
1887static dev_match_ret
1888xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1889 struct cam_ed *device)
1890{
1891 dev_match_ret retval;
1892 int i;
1893
1894 retval = DM_RET_NONE;
1895
1896 /*
1897 * If we aren't given something to match against, that's an error.
1898 */
1899 if (device == NULL)
1900 return(DM_RET_ERROR);
1901
1902 /*
1903 * If there are no match entries, then this device matches no
1904 * matter what.
1905 */
1906 if ((patterns == NULL) || (num_patterns == 0))
1907 return(DM_RET_DESCEND | DM_RET_COPY);
1908
1909 for (i = 0; i < num_patterns; i++) {
1910 struct device_match_pattern *cur_pattern;
1911
1912 /*
1913 * If the pattern in question isn't for a device node, we
1914 * aren't interested.
1915 */
1916 if (patterns[i].type != DEV_MATCH_DEVICE) {
1917 if ((patterns[i].type == DEV_MATCH_PERIPH)
1918 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1919 retval |= DM_RET_DESCEND;
1920 continue;
1921 }
1922
1923 cur_pattern = &patterns[i].pattern.device_pattern;
1924
1925 /*
1926 * If they want to match any device node, we give them any
1927 * device node.
1928 */
1929 if (cur_pattern->flags == DEV_MATCH_ANY) {
1930 /* set the copy flag */
1931 retval |= DM_RET_COPY;
1932
1933
1934 /*
1935 * If we've already decided on an action, go ahead
1936 * and return.
1937 */
1938 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1939 return(retval);
1940 }
1941
1942 /*
1943 * Not sure why someone would do this...
1944 */
1945 if (cur_pattern->flags == DEV_MATCH_NONE)
1946 continue;
1947
1948 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1949 && (cur_pattern->path_id != device->target->bus->path_id))
1950 continue;
1951
1952 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1953 && (cur_pattern->target_id != device->target->target_id))
1954 continue;
1955
1956 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1957 && (cur_pattern->target_lun != device->lun_id))
1958 continue;
1959
1960 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1961 && (cam_quirkmatch((caddr_t)&device->inq_data,
1962 (caddr_t)&cur_pattern->inq_pat,
1963 1, sizeof(cur_pattern->inq_pat),
1964 scsi_static_inquiry_match) == NULL))
1965 continue;
1966
1967 /*
1968 * If we get to this point, the user definitely wants
1969 * information on this device. So tell the caller to copy
1970 * the data out.
1971 */
1972 retval |= DM_RET_COPY;
1973
1974 /*
1975 * If the return action has been set to descend, then we
1976 * know that we've already seen a peripheral matching
1977 * expression, therefore we need to further descend the tree.
1978 * This won't change by continuing around the loop, so we
1979 * go ahead and return. If we haven't seen a peripheral
1980 * matching expression, we keep going around the loop until
1981 * we exhaust the matching expressions. We'll set the stop
1982 * flag once we fall out of the loop.
1983 */
1984 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1985 return(retval);
1986 }
1987
1988 /*
1989 * If the return action hasn't been set to descend yet, that means
1990 * we haven't seen any peripheral matching patterns. So tell the
1991 * caller to stop descending the tree -- the user doesn't want to
1992 * match against lower level tree elements.
1993 */
1994 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1995 retval |= DM_RET_STOP;
1996
1997 return(retval);
1998}
1999
2000/*
2001 * Match a single peripheral against any number of match patterns.
2002 */
2003static dev_match_ret
2004xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
2005 struct cam_periph *periph)
2006{
2007 dev_match_ret retval;
2008 int i;
2009
2010 /*
2011 * If we aren't given something to match against, that's an error.
2012 */
2013 if (periph == NULL)
2014 return(DM_RET_ERROR);
2015
2016 /*
2017 * If there are no match entries, then this peripheral matches no
2018 * matter what.
2019 */
2020 if ((patterns == NULL) || (num_patterns == 0))
2021 return(DM_RET_STOP | DM_RET_COPY);
2022
2023 /*
2024 * There aren't any nodes below a peripheral node, so there's no
2025 * reason to descend the tree any further.
2026 */
2027 retval = DM_RET_STOP;
2028
2029 for (i = 0; i < num_patterns; i++) {
2030 struct periph_match_pattern *cur_pattern;
2031
2032 /*
2033 * If the pattern in question isn't for a peripheral, we
2034 * aren't interested.
2035 */
2036 if (patterns[i].type != DEV_MATCH_PERIPH)
2037 continue;
2038
2039 cur_pattern = &patterns[i].pattern.periph_pattern;
2040
2041 /*
2042 * If they want to match on anything, then we will do so.
2043 */
2044 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
2045 /* set the copy flag */
2046 retval |= DM_RET_COPY;
2047
2048 /*
2049 * We've already set the return action to stop,
2050 * since there are no nodes below peripherals in
2051 * the tree.
2052 */
2053 return(retval);
2054 }
2055
2056 /*
2057 * Not sure why someone would do this...
2058 */
2059 if (cur_pattern->flags == PERIPH_MATCH_NONE)
2060 continue;
2061
2062 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
2063 && (cur_pattern->path_id != periph->path->bus->path_id))
2064 continue;
2065
2066 /*
2067 * For the target and lun id's, we have to make sure the
2068 * target and lun pointers aren't NULL. The xpt peripheral
2069 * has a wildcard target and device.
2070 */
2071 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
2072 && ((periph->path->target == NULL)
2073 ||(cur_pattern->target_id != periph->path->target->target_id)))
2074 continue;
2075
2076 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
2077 && ((periph->path->device == NULL)
2078 || (cur_pattern->target_lun != periph->path->device->lun_id)))
2079 continue;
2080
2081 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
2082 && (cur_pattern->unit_number != periph->unit_number))
2083 continue;
2084
2085 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
2086 && (strncmp(cur_pattern->periph_name, periph->periph_name,
2087 DEV_IDLEN) != 0))
2088 continue;
2089
2090 /*
2091 * If we get to this point, the user definitely wants
2092 * information on this peripheral. So tell the caller to
2093 * copy the data out.
2094 */
2095 retval |= DM_RET_COPY;
2096
2097 /*
2098 * The return action has already been set to stop, since
2099 * peripherals don't have any nodes below them in the EDT.
2100 */
2101 return(retval);
2102 }
2103
2104 /*
2105 * If we get to this point, the peripheral that was passed in
2106 * doesn't match any of the patterns.
2107 */
2108 return(retval);
2109}
2110
2111static int
2112xptedtbusfunc(struct cam_eb *bus, void *arg)
2113{
2114 struct ccb_dev_match *cdm;
2115 dev_match_ret retval;
2116
2117 cdm = (struct ccb_dev_match *)arg;
2118
2119 /*
2120 * If our position is for something deeper in the tree, that means
2121 * that we've already seen this node. So, we keep going down.
2122 */
2123 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2124 && (cdm->pos.cookie.bus == bus)
2125 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2126 && (cdm->pos.cookie.target != NULL))
2127 retval = DM_RET_DESCEND;
2128 else
2129 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
2130
2131 /*
2132 * If we got an error, bail out of the search.
2133 */
2134 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2135 cdm->status = CAM_DEV_MATCH_ERROR;
2136 return(0);
2137 }
2138
2139 /*
2140 * If the copy flag is set, copy this bus out.
2141 */
2142 if (retval & DM_RET_COPY) {
2143 int spaceleft, j;
2144
2145 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2146 sizeof(struct dev_match_result));
2147
2148 /*
2149 * If we don't have enough space to put in another
2150 * match result, save our position and tell the
2151 * user there are more devices to check.
2152 */
2153 if (spaceleft < sizeof(struct dev_match_result)) {
2154 bzero(&cdm->pos, sizeof(cdm->pos));
2155 cdm->pos.position_type =
2156 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
2157
2158 cdm->pos.cookie.bus = bus;
2159 cdm->pos.generations[CAM_BUS_GENERATION]=
2160 bus_generation;
2161 cdm->status = CAM_DEV_MATCH_MORE;
2162 return(0);
2163 }
2164 j = cdm->num_matches;
2165 cdm->num_matches++;
2166 cdm->matches[j].type = DEV_MATCH_BUS;
2167 cdm->matches[j].result.bus_result.path_id = bus->path_id;
2168 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
2169 cdm->matches[j].result.bus_result.unit_number =
2170 bus->sim->unit_number;
2171 strncpy(cdm->matches[j].result.bus_result.dev_name,
2172 bus->sim->sim_name, DEV_IDLEN);
2173 }
2174
2175 /*
2176 * If the user is only interested in busses, there's no
2177 * reason to descend to the next level in the tree.
2178 */
2179 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2180 return(1);
2181
2182 /*
2183 * If there is a target generation recorded, check it to
2184 * make sure the target list hasn't changed.
2185 */
2186 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2187 && (bus == cdm->pos.cookie.bus)
2188 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2189 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
2190 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
2191 bus->generation)) {
2192 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2193 return(0);
2194 }
2195
2196 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2197 && (cdm->pos.cookie.bus == bus)
2198 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2199 && (cdm->pos.cookie.target != NULL))
2200 return(xpttargettraverse(bus,
2201 (struct cam_et *)cdm->pos.cookie.target,
2202 xptedttargetfunc, arg));
2203 else
2204 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2205}
2206
2207static int
2208xptedttargetfunc(struct cam_et *target, void *arg)
2209{
2210 struct ccb_dev_match *cdm;
2211
2212 cdm = (struct ccb_dev_match *)arg;
2213
2214 /*
2215 * If there is a device list generation recorded, check it to
2216 * make sure the device list hasn't changed.
2217 */
2218 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2219 && (cdm->pos.cookie.bus == target->bus)
2220 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2221 && (cdm->pos.cookie.target == target)
2222 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2223 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2224 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2225 target->generation)) {
2226 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2227 return(0);
2228 }
2229
2230 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2231 && (cdm->pos.cookie.bus == target->bus)
2232 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2233 && (cdm->pos.cookie.target == target)
2234 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2235 && (cdm->pos.cookie.device != NULL))
2236 return(xptdevicetraverse(target,
2237 (struct cam_ed *)cdm->pos.cookie.device,
2238 xptedtdevicefunc, arg));
2239 else
2240 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2241}
2242
2243static int
2244xptedtdevicefunc(struct cam_ed *device, void *arg)
2245{
2246
2247 struct ccb_dev_match *cdm;
2248 dev_match_ret retval;
2249
2250 cdm = (struct ccb_dev_match *)arg;
2251
2252 /*
2253 * If our position is for something deeper in the tree, that means
2254 * that we've already seen this node. So, we keep going down.
2255 */
2256 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2257 && (cdm->pos.cookie.device == device)
2258 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2259 && (cdm->pos.cookie.periph != NULL))
2260 retval = DM_RET_DESCEND;
2261 else
2262 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2263 device);
2264
2265 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2266 cdm->status = CAM_DEV_MATCH_ERROR;
2267 return(0);
2268 }
2269
2270 /*
2271 * If the copy flag is set, copy this device out.
2272 */
2273 if (retval & DM_RET_COPY) {
2274 int spaceleft, j;
2275
2276 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2277 sizeof(struct dev_match_result));
2278
2279 /*
2280 * If we don't have enough space to put in another
2281 * match result, save our position and tell the
2282 * user there are more devices to check.
2283 */
2284 if (spaceleft < sizeof(struct dev_match_result)) {
2285 bzero(&cdm->pos, sizeof(cdm->pos));
2286 cdm->pos.position_type =
2287 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2288 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2289
2290 cdm->pos.cookie.bus = device->target->bus;
2291 cdm->pos.generations[CAM_BUS_GENERATION]=
2292 bus_generation;
2293 cdm->pos.cookie.target = device->target;
2294 cdm->pos.generations[CAM_TARGET_GENERATION] =
2295 device->target->bus->generation;
2296 cdm->pos.cookie.device = device;
2297 cdm->pos.generations[CAM_DEV_GENERATION] =
2298 device->target->generation;
2299 cdm->status = CAM_DEV_MATCH_MORE;
2300 return(0);
2301 }
2302 j = cdm->num_matches;
2303 cdm->num_matches++;
2304 cdm->matches[j].type = DEV_MATCH_DEVICE;
2305 cdm->matches[j].result.device_result.path_id =
2306 device->target->bus->path_id;
2307 cdm->matches[j].result.device_result.target_id =
2308 device->target->target_id;
2309 cdm->matches[j].result.device_result.target_lun =
2310 device->lun_id;
2311 bcopy(&device->inq_data,
2312 &cdm->matches[j].result.device_result.inq_data,
2313 sizeof(struct scsi_inquiry_data));
2314
2315 /* Let the user know whether this device is unconfigured */
2316 if (device->flags & CAM_DEV_UNCONFIGURED)
2317 cdm->matches[j].result.device_result.flags =
2318 DEV_RESULT_UNCONFIGURED;
2319 else
2320 cdm->matches[j].result.device_result.flags =
2321 DEV_RESULT_NOFLAG;
2322 }
2323
2324 /*
2325 * If the user isn't interested in peripherals, don't descend
2326 * the tree any further.
2327 */
2328 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2329 return(1);
2330
2331 /*
2332 * If there is a peripheral list generation recorded, make sure
2333 * it hasn't changed.
2334 */
2335 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2336 && (device->target->bus == cdm->pos.cookie.bus)
2337 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2338 && (device->target == cdm->pos.cookie.target)
2339 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2340 && (device == cdm->pos.cookie.device)
2341 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2342 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2343 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2344 device->generation)){
2345 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2346 return(0);
2347 }
2348
2349 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2350 && (cdm->pos.cookie.bus == device->target->bus)
2351 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2352 && (cdm->pos.cookie.target == device->target)
2353 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2354 && (cdm->pos.cookie.device == device)
2355 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2356 && (cdm->pos.cookie.periph != NULL))
2357 return(xptperiphtraverse(device,
2358 (struct cam_periph *)cdm->pos.cookie.periph,
2359 xptedtperiphfunc, arg));
2360 else
2361 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2362}
2363
2364static int
2365xptedtperiphfunc(struct cam_periph *periph, void *arg)
2366{
2367 struct ccb_dev_match *cdm;
2368 dev_match_ret retval;
2369
2370 cdm = (struct ccb_dev_match *)arg;
2371
2372 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2373
2374 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2375 cdm->status = CAM_DEV_MATCH_ERROR;
2376 return(0);
2377 }
2378
2379 /*
2380 * If the copy flag is set, copy this peripheral out.
2381 */
2382 if (retval & DM_RET_COPY) {
2383 int spaceleft, j;
2384
2385 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2386 sizeof(struct dev_match_result));
2387
2388 /*
2389 * If we don't have enough space to put in another
2390 * match result, save our position and tell the
2391 * user there are more devices to check.
2392 */
2393 if (spaceleft < sizeof(struct dev_match_result)) {
2394 bzero(&cdm->pos, sizeof(cdm->pos));
2395 cdm->pos.position_type =
2396 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2397 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2398 CAM_DEV_POS_PERIPH;
2399
2400 cdm->pos.cookie.bus = periph->path->bus;
2401 cdm->pos.generations[CAM_BUS_GENERATION]=
2402 bus_generation;
2403 cdm->pos.cookie.target = periph->path->target;
2404 cdm->pos.generations[CAM_TARGET_GENERATION] =
2405 periph->path->bus->generation;
2406 cdm->pos.cookie.device = periph->path->device;
2407 cdm->pos.generations[CAM_DEV_GENERATION] =
2408 periph->path->target->generation;
2409 cdm->pos.cookie.periph = periph;
2410 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2411 periph->path->device->generation;
2412 cdm->status = CAM_DEV_MATCH_MORE;
2413 return(0);
2414 }
2415
2416 j = cdm->num_matches;
2417 cdm->num_matches++;
2418 cdm->matches[j].type = DEV_MATCH_PERIPH;
2419 cdm->matches[j].result.periph_result.path_id =
2420 periph->path->bus->path_id;
2421 cdm->matches[j].result.periph_result.target_id =
2422 periph->path->target->target_id;
2423 cdm->matches[j].result.periph_result.target_lun =
2424 periph->path->device->lun_id;
2425 cdm->matches[j].result.periph_result.unit_number =
2426 periph->unit_number;
2427 strncpy(cdm->matches[j].result.periph_result.periph_name,
2428 periph->periph_name, DEV_IDLEN);
2429 }
2430
2431 return(1);
2432}
2433
2434static int
2435xptedtmatch(struct ccb_dev_match *cdm)
2436{
2437 int ret;
2438
2439 cdm->num_matches = 0;
2440
2441 /*
2442 * Check the bus list generation. If it has changed, the user
2443 * needs to reset everything and start over.
2444 */
2445 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2446 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2447 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2448 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2449 return(0);
2450 }
2451
2452 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2453 && (cdm->pos.cookie.bus != NULL))
2454 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2455 xptedtbusfunc, cdm);
2456 else
2457 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2458
2459 /*
2460 * If we get back 0, that means that we had to stop before fully
2461 * traversing the EDT. It also means that one of the subroutines
2462 * has set the status field to the proper value. If we get back 1,
2463 * we've fully traversed the EDT and copied out any matching entries.
2464 */
2465 if (ret == 1)
2466 cdm->status = CAM_DEV_MATCH_LAST;
2467
2468 return(ret);
2469}
2470
2471static int
2472xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2473{
2474 struct ccb_dev_match *cdm;
2475
2476 cdm = (struct ccb_dev_match *)arg;
2477
2478 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2479 && (cdm->pos.cookie.pdrv == pdrv)
2480 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2481 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2482 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2483 (*pdrv)->generation)) {
2484 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2485 return(0);
2486 }
2487
2488 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2489 && (cdm->pos.cookie.pdrv == pdrv)
2490 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2491 && (cdm->pos.cookie.periph != NULL))
2492 return(xptpdperiphtraverse(pdrv,
2493 (struct cam_periph *)cdm->pos.cookie.periph,
2494 xptplistperiphfunc, arg));
2495 else
2496 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2497}
2498
2499static int
2500xptplistperiphfunc(struct cam_periph *periph, void *arg)
2501{
2502 struct ccb_dev_match *cdm;
2503 dev_match_ret retval;
2504
2505 cdm = (struct ccb_dev_match *)arg;
2506
2507 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2508
2509 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2510 cdm->status = CAM_DEV_MATCH_ERROR;
2511 return(0);
2512 }
2513
2514 /*
2515 * If the copy flag is set, copy this peripheral out.
2516 */
2517 if (retval & DM_RET_COPY) {
2518 int spaceleft, j;
2519
2520 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2521 sizeof(struct dev_match_result));
2522
2523 /*
2524 * If we don't have enough space to put in another
2525 * match result, save our position and tell the
2526 * user there are more devices to check.
2527 */
2528 if (spaceleft < sizeof(struct dev_match_result)) {
2529 struct periph_driver **pdrv;
2530
2531 pdrv = NULL;
2532 bzero(&cdm->pos, sizeof(cdm->pos));
2533 cdm->pos.position_type =
2534 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2535 CAM_DEV_POS_PERIPH;
2536
2537 /*
2538 * This may look a bit non-sensical, but it is
2539 * actually quite logical. There are very few
2540 * peripheral drivers, and bloating every peripheral
2541 * structure with a pointer back to its parent
2542 * peripheral driver linker set entry would cost
2543 * more in the long run than doing this quick lookup.
2544 */
2545 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2546 if (strcmp((*pdrv)->driver_name,
2547 periph->periph_name) == 0)
2548 break;
2549 }
2550
2551 if (*pdrv == NULL) {
2552 cdm->status = CAM_DEV_MATCH_ERROR;
2553 return(0);
2554 }
2555
2556 cdm->pos.cookie.pdrv = pdrv;
2557 /*
2558 * The periph generation slot does double duty, as
2559 * does the periph pointer slot. They are used for
2560 * both edt and pdrv lookups and positioning.
2561 */
2562 cdm->pos.cookie.periph = periph;
2563 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2564 (*pdrv)->generation;
2565 cdm->status = CAM_DEV_MATCH_MORE;
2566 return(0);
2567 }
2568
2569 j = cdm->num_matches;
2570 cdm->num_matches++;
2571 cdm->matches[j].type = DEV_MATCH_PERIPH;
2572 cdm->matches[j].result.periph_result.path_id =
2573 periph->path->bus->path_id;
2574
2575 /*
2576 * The transport layer peripheral doesn't have a target or
2577 * lun.
2578 */
2579 if (periph->path->target)
2580 cdm->matches[j].result.periph_result.target_id =
2581 periph->path->target->target_id;
2582 else
2583 cdm->matches[j].result.periph_result.target_id = -1;
2584
2585 if (periph->path->device)
2586 cdm->matches[j].result.periph_result.target_lun =
2587 periph->path->device->lun_id;
2588 else
2589 cdm->matches[j].result.periph_result.target_lun = -1;
2590
2591 cdm->matches[j].result.periph_result.unit_number =
2592 periph->unit_number;
2593 strncpy(cdm->matches[j].result.periph_result.periph_name,
2594 periph->periph_name, DEV_IDLEN);
2595 }
2596
2597 return(1);
2598}
2599
2600static int
2601xptperiphlistmatch(struct ccb_dev_match *cdm)
2602{
2603 int ret;
2604
2605 cdm->num_matches = 0;
2606
2607 /*
2608 * At this point in the edt traversal function, we check the bus
2609 * list generation to make sure that no busses have been added or
2610 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2611 * For the peripheral driver list traversal function, however, we
2612 * don't have to worry about new peripheral driver types coming or
2613 * going; they're in a linker set, and therefore can't change
2614 * without a recompile.
2615 */
2616
2617 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2618 && (cdm->pos.cookie.pdrv != NULL))
2619 ret = xptpdrvtraverse(
2620 (struct periph_driver **)cdm->pos.cookie.pdrv,
2621 xptplistpdrvfunc, cdm);
2622 else
2623 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2624
2625 /*
2626 * If we get back 0, that means that we had to stop before fully
2627 * traversing the peripheral driver tree. It also means that one of
2628 * the subroutines has set the status field to the proper value. If
2629 * we get back 1, we've fully traversed the EDT and copied out any
2630 * matching entries.
2631 */
2632 if (ret == 1)
2633 cdm->status = CAM_DEV_MATCH_LAST;
2634
2635 return(ret);
2636}
2637
2638static int
2639xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2640{
2641 struct cam_eb *bus, *next_bus;
2642 int retval;
2643
2644 retval = 1;
2645
2646 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2647 bus != NULL;
2648 bus = next_bus) {
2649 next_bus = TAILQ_NEXT(bus, links);
2650
2651 retval = tr_func(bus, arg);
2652 if (retval == 0)
2653 return(retval);
2654 }
2655
2656 return(retval);
2657}
2658
2659static int
2660xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2661 xpt_targetfunc_t *tr_func, void *arg)
2662{
2663 struct cam_et *target, *next_target;
2664 int retval;
2665
2666 retval = 1;
2667 for (target = (start_target ? start_target :
2668 TAILQ_FIRST(&bus->et_entries));
2669 target != NULL; target = next_target) {
2670
2671 next_target = TAILQ_NEXT(target, links);
2672
2673 retval = tr_func(target, arg);
2674
2675 if (retval == 0)
2676 return(retval);
2677 }
2678
2679 return(retval);
2680}
2681
2682static int
2683xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2684 xpt_devicefunc_t *tr_func, void *arg)
2685{
2686 struct cam_ed *device, *next_device;
2687 int retval;
2688
2689 retval = 1;
2690 for (device = (start_device ? start_device :
2691 TAILQ_FIRST(&target->ed_entries));
2692 device != NULL;
2693 device = next_device) {
2694
2695 next_device = TAILQ_NEXT(device, links);
2696
2697 retval = tr_func(device, arg);
2698
2699 if (retval == 0)
2700 return(retval);
2701 }
2702
2703 return(retval);
2704}
2705
2706static int
2707xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2708 xpt_periphfunc_t *tr_func, void *arg)
2709{
2710 struct cam_periph *periph, *next_periph;
2711 int retval;
2712
2713 retval = 1;
2714
2715 for (periph = (start_periph ? start_periph :
2716 SLIST_FIRST(&device->periphs));
2717 periph != NULL;
2718 periph = next_periph) {
2719
2720 next_periph = SLIST_NEXT(periph, periph_links);
2721
2722 retval = tr_func(periph, arg);
2723 if (retval == 0)
2724 return(retval);
2725 }
2726
2727 return(retval);
2728}
2729
2730static int
2731xptpdrvtraverse(struct periph_driver **start_pdrv,
2732 xpt_pdrvfunc_t *tr_func, void *arg)
2733{
2734 struct periph_driver **pdrv;
2735 int retval;
2736
2737 retval = 1;
2738
2739 /*
2740 * We don't traverse the peripheral driver list like we do the
2741 * other lists, because it is a linker set, and therefore cannot be
2742 * changed during runtime. If the peripheral driver list is ever
2743 * re-done to be something other than a linker set (i.e. it can
2744 * change while the system is running), the list traversal should
2745 * be modified to work like the other traversal functions.
2746 */
2747 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2748 *pdrv != NULL; pdrv++) {
2749 retval = tr_func(pdrv, arg);
2750
2751 if (retval == 0)
2752 return(retval);
2753 }
2754
2755 return(retval);
2756}
2757
2758static int
2759xptpdperiphtraverse(struct periph_driver **pdrv,
2760 struct cam_periph *start_periph,
2761 xpt_periphfunc_t *tr_func, void *arg)
2762{
2763 struct cam_periph *periph, *next_periph;
2764 int retval;
2765
2766 retval = 1;
2767
2768 for (periph = (start_periph ? start_periph :
2769 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2770 periph = next_periph) {
2771
2772 next_periph = TAILQ_NEXT(periph, unit_links);
2773
2774 retval = tr_func(periph, arg);
2775 if (retval == 0)
2776 return(retval);
2777 }
2778 return(retval);
2779}
2780
2781static int
2782xptdefbusfunc(struct cam_eb *bus, void *arg)
2783{
2784 struct xpt_traverse_config *tr_config;
2785
2786 tr_config = (struct xpt_traverse_config *)arg;
2787
2788 if (tr_config->depth == XPT_DEPTH_BUS) {
2789 xpt_busfunc_t *tr_func;
2790
2791 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2792
2793 return(tr_func(bus, tr_config->tr_arg));
2794 } else
2795 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2796}
2797
2798static int
2799xptdeftargetfunc(struct cam_et *target, void *arg)
2800{
2801 struct xpt_traverse_config *tr_config;
2802
2803 tr_config = (struct xpt_traverse_config *)arg;
2804
2805 if (tr_config->depth == XPT_DEPTH_TARGET) {
2806 xpt_targetfunc_t *tr_func;
2807
2808 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2809
2810 return(tr_func(target, tr_config->tr_arg));
2811 } else
2812 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2813}
2814
2815static int
2816xptdefdevicefunc(struct cam_ed *device, void *arg)
2817{
2818 struct xpt_traverse_config *tr_config;
2819
2820 tr_config = (struct xpt_traverse_config *)arg;
2821
2822 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2823 xpt_devicefunc_t *tr_func;
2824
2825 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2826
2827 return(tr_func(device, tr_config->tr_arg));
2828 } else
2829 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2830}
2831
2832static int
2833xptdefperiphfunc(struct cam_periph *periph, void *arg)
2834{
2835 struct xpt_traverse_config *tr_config;
2836 xpt_periphfunc_t *tr_func;
2837
2838 tr_config = (struct xpt_traverse_config *)arg;
2839
2840 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2841
2842 /*
2843 * Unlike the other default functions, we don't check for depth
2844 * here. The peripheral driver level is the last level in the EDT,
2845 * so if we're here, we should execute the function in question.
2846 */
2847 return(tr_func(periph, tr_config->tr_arg));
2848}
2849
2850/*
2851 * Execute the given function for every bus in the EDT.
2852 */
2853static int
2854xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2855{
2856 struct xpt_traverse_config tr_config;
2857
2858 tr_config.depth = XPT_DEPTH_BUS;
2859 tr_config.tr_func = tr_func;
2860 tr_config.tr_arg = arg;
2861
2862 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2863}
2864
2865#ifdef notusedyet
2866/*
2867 * Execute the given function for every target in the EDT.
2868 */
2869static int
2870xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2871{
2872 struct xpt_traverse_config tr_config;
2873
2874 tr_config.depth = XPT_DEPTH_TARGET;
2875 tr_config.tr_func = tr_func;
2876 tr_config.tr_arg = arg;
2877
2878 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2879}
2880#endif /* notusedyet */
2881
2882/*
2883 * Execute the given function for every device in the EDT.
2884 */
2885static int
2886xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2887{
2888 struct xpt_traverse_config tr_config;
2889
2890 tr_config.depth = XPT_DEPTH_DEVICE;
2891 tr_config.tr_func = tr_func;
2892 tr_config.tr_arg = arg;
2893
2894 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2895}
2896
2897#ifdef notusedyet
2898/*
2899 * Execute the given function for every peripheral in the EDT.
2900 */
2901static int
2902xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2903{
2904 struct xpt_traverse_config tr_config;
2905
2906 tr_config.depth = XPT_DEPTH_PERIPH;
2907 tr_config.tr_func = tr_func;
2908 tr_config.tr_arg = arg;
2909
2910 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2911}
2912#endif /* notusedyet */
2913
2914static int
2915xptsetasyncfunc(struct cam_ed *device, void *arg)
2916{
2917 struct cam_path path;
2918 struct ccb_getdev cgd;
2919 struct async_node *cur_entry;
2920
2921 cur_entry = (struct async_node *)arg;
2922
2923 /*
2924 * Don't report unconfigured devices (Wildcard devs,
2925 * devices only for target mode, device instances
2926 * that have been invalidated but are waiting for
2927 * their last reference count to be released).
2928 */
2929 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2930 return (1);
2931
2932 xpt_compile_path(&path,
2933 NULL,
2934 device->target->bus->path_id,
2935 device->target->target_id,
2936 device->lun_id);
2937 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2938 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2939 xpt_action((union ccb *)&cgd);
2940 cur_entry->callback(cur_entry->callback_arg,
2941 AC_FOUND_DEVICE,
2942 &path, &cgd);
2943 xpt_release_path(&path);
2944
2945 return(1);
2946}
2947
2948static int
2949xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2950{
2951 struct cam_path path;
2952 struct ccb_pathinq cpi;
2953 struct async_node *cur_entry;
2954
2955 cur_entry = (struct async_node *)arg;
2956
2957 xpt_compile_path(&path, /*periph*/NULL,
2958 bus->sim->path_id,
2959 CAM_TARGET_WILDCARD,
2960 CAM_LUN_WILDCARD);
2961 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2962 cpi.ccb_h.func_code = XPT_PATH_INQ;
2963 xpt_action((union ccb *)&cpi);
2964 cur_entry->callback(cur_entry->callback_arg,
2965 AC_PATH_REGISTERED,
2966 &path, &cpi);
2967 xpt_release_path(&path);
2968
2969 return(1);
2970}
2971
2972void
2973xpt_action(union ccb *start_ccb)
2974{
2975 int iopl;
2976
2977 GIANT_REQUIRED;
2978
2979 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2980
2981 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2982
2983 iopl = splsoftcam();
2984 switch (start_ccb->ccb_h.func_code) {
2985 case XPT_SCSI_IO:
2986 {
2987#ifdef CAM_NEW_TRAN_CODE
2988 struct cam_ed *device;
2989#endif /* CAM_NEW_TRAN_CODE */
2990#ifdef CAMDEBUG
2991 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2992 struct cam_path *path;
2993
2994 path = start_ccb->ccb_h.path;
2995#endif
2996
2997 /*
2998 * For the sake of compatibility with SCSI-1
2999 * devices that may not understand the identify
3000 * message, we include lun information in the
3001 * second byte of all commands. SCSI-1 specifies
3002 * that luns are a 3 bit value and reserves only 3
3003 * bits for lun information in the CDB. Later
3004 * revisions of the SCSI spec allow for more than 8
3005 * luns, but have deprecated lun information in the
3006 * CDB. So, if the lun won't fit, we must omit.
3007 *
3008 * Also be aware that during initial probing for devices,
3009 * the inquiry information is unknown but initialized to 0.
3010 * This means that this code will be exercised while probing
3011 * devices with an ANSI revision greater than 2.
3012 */
3013#ifdef CAM_NEW_TRAN_CODE
3014 device = start_ccb->ccb_h.path->device;
3015 if (device->protocol_version <= SCSI_REV_2
3016#else /* CAM_NEW_TRAN_CODE */
3017 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
3018#endif /* CAM_NEW_TRAN_CODE */
3019 && start_ccb->ccb_h.target_lun < 8
3020 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
3021
3022 start_ccb->csio.cdb_io.cdb_bytes[1] |=
3023 start_ccb->ccb_h.target_lun << 5;
3024 }
3025 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
3026 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
3027 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
3028 &path->device->inq_data),
3029 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
3030 cdb_str, sizeof(cdb_str))));
3031 }
3032 /* FALLTHROUGH */
3033 case XPT_TARGET_IO:
3034 case XPT_CONT_TARGET_IO:
3035 start_ccb->csio.sense_resid = 0;
3036 start_ccb->csio.resid = 0;
3037 /* FALLTHROUGH */
3038 case XPT_RESET_DEV:
3039 case XPT_ENG_EXEC:
3040 {
3041 struct cam_path *path;
3042 int s;
3043 int runq;
3044
3045 path = start_ccb->ccb_h.path;
3046 s = splsoftcam();
3047
3048 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
3049 if (path->device->qfrozen_cnt == 0)
3050 runq = xpt_schedule_dev_sendq(path->bus, path->device);
3051 else
3052 runq = 0;
3053 splx(s);
3054 if (runq != 0)
3055 xpt_run_dev_sendq(path->bus);
3056 break;
3057 }
3058 case XPT_SET_TRAN_SETTINGS:
3059 {
3060 xpt_set_transfer_settings(&start_ccb->cts,
3061 start_ccb->ccb_h.path->device,
3062 /*async_update*/FALSE);
3063 break;
3064 }
3065 case XPT_CALC_GEOMETRY:
3066 {
3067 struct cam_sim *sim;
3068
3069 /* Filter out garbage */
3070 if (start_ccb->ccg.block_size == 0
3071 || start_ccb->ccg.volume_size == 0) {
3072 start_ccb->ccg.cylinders = 0;
3073 start_ccb->ccg.heads = 0;
3074 start_ccb->ccg.secs_per_track = 0;
3075 start_ccb->ccb_h.status = CAM_REQ_CMP;
3076 break;
3077 }
3078#ifdef PC98
3079 /*
3080 * In a PC-98 system, geometry translation depens on
3081 * the "real" device geometry obtained from mode page 4.
3082 * SCSI geometry translation is performed in the
3083 * initialization routine of the SCSI BIOS and the result
3084 * stored in host memory. If the translation is available
3085 * in host memory, use it. If not, rely on the default
3086 * translation the device driver performs.
3087 */
3088 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
3089 start_ccb->ccb_h.status = CAM_REQ_CMP;
3090 break;
3091 }
3092#endif
3093 sim = start_ccb->ccb_h.path->bus->sim;
3094 (*(sim->sim_action))(sim, start_ccb);
3095 break;
3096 }
3097 case XPT_ABORT:
3098 {
3099 union ccb* abort_ccb;
3100 int s;
3101
3102 abort_ccb = start_ccb->cab.abort_ccb;
3103 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
3104
3105 if (abort_ccb->ccb_h.pinfo.index >= 0) {
3106 struct cam_ccbq *ccbq;
3107
3108 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
3109 cam_ccbq_remove_ccb(ccbq, abort_ccb);
3110 abort_ccb->ccb_h.status =
3111 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3112 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3113 s = splcam();
3114 xpt_done(abort_ccb);
3115 splx(s);
3116 start_ccb->ccb_h.status = CAM_REQ_CMP;
3117 break;
3118 }
3119 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
3120 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
3121 /*
3122 * We've caught this ccb en route to
3123 * the SIM. Flag it for abort and the
3124 * SIM will do so just before starting
3125 * real work on the CCB.
3126 */
3127 abort_ccb->ccb_h.status =
3128 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3129 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3130 start_ccb->ccb_h.status = CAM_REQ_CMP;
3131 break;
3132 }
3133 }
3134 if (XPT_FC_IS_QUEUED(abort_ccb)
3135 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
3136 /*
3137 * It's already completed but waiting
3138 * for our SWI to get to it.
3139 */
3140 start_ccb->ccb_h.status = CAM_UA_ABORT;
3141 break;
3142 }
3143 /*
3144 * If we weren't able to take care of the abort request
3145 * in the XPT, pass the request down to the SIM for processing.
3146 */
3147 }
3148 /* FALLTHROUGH */
3149 case XPT_ACCEPT_TARGET_IO:
3150 case XPT_EN_LUN:
3151 case XPT_IMMED_NOTIFY:
3152 case XPT_NOTIFY_ACK:
3153 case XPT_GET_TRAN_SETTINGS:
3154 case XPT_RESET_BUS:
3155 {
3156 struct cam_sim *sim;
3157
3158 sim = start_ccb->ccb_h.path->bus->sim;
3159 (*(sim->sim_action))(sim, start_ccb);
3160 break;
3161 }
3162 case XPT_PATH_INQ:
3163 {
3164 struct cam_sim *sim;
3165
3166 sim = start_ccb->ccb_h.path->bus->sim;
3167 (*(sim->sim_action))(sim, start_ccb);
3168 break;
3169 }
3170 case XPT_PATH_STATS:
3171 start_ccb->cpis.last_reset =
3172 start_ccb->ccb_h.path->bus->last_reset;
3173 start_ccb->ccb_h.status = CAM_REQ_CMP;
3174 break;
3175 case XPT_GDEV_TYPE:
3176 {
3177 struct cam_ed *dev;
3178 int s;
3179
3180 dev = start_ccb->ccb_h.path->device;
3181 s = splcam();
3182 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3183 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3184 } else {
3185 struct ccb_getdev *cgd;
3186 struct cam_eb *bus;
3187 struct cam_et *tar;
3188
3189 cgd = &start_ccb->cgd;
3190 bus = cgd->ccb_h.path->bus;
3191 tar = cgd->ccb_h.path->target;
3192 cgd->inq_data = dev->inq_data;
3193 cgd->ccb_h.status = CAM_REQ_CMP;
3194 cgd->serial_num_len = dev->serial_num_len;
3195 if ((dev->serial_num_len > 0)
3196 && (dev->serial_num != NULL))
3197 bcopy(dev->serial_num, cgd->serial_num,
3198 dev->serial_num_len);
3199 }
3200 splx(s);
3201 break;
3202 }
3203 case XPT_GDEV_STATS:
3204 {
3205 struct cam_ed *dev;
3206 int s;
3207
3208 dev = start_ccb->ccb_h.path->device;
3209 s = splcam();
3210 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3211 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3212 } else {
3213 struct ccb_getdevstats *cgds;
3214 struct cam_eb *bus;
3215 struct cam_et *tar;
3216
3217 cgds = &start_ccb->cgds;
3218 bus = cgds->ccb_h.path->bus;
3219 tar = cgds->ccb_h.path->target;
3220 cgds->dev_openings = dev->ccbq.dev_openings;
3221 cgds->dev_active = dev->ccbq.dev_active;
3222 cgds->devq_openings = dev->ccbq.devq_openings;
3223 cgds->devq_queued = dev->ccbq.queue.entries;
3224 cgds->held = dev->ccbq.held;
3225 cgds->last_reset = tar->last_reset;
3226 cgds->maxtags = dev->quirk->maxtags;
3227 cgds->mintags = dev->quirk->mintags;
3228 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3229 cgds->last_reset = bus->last_reset;
3230 cgds->ccb_h.status = CAM_REQ_CMP;
3231 }
3232 splx(s);
3233 break;
3234 }
3235 case XPT_GDEVLIST:
3236 {
3237 struct cam_periph *nperiph;
3238 struct periph_list *periph_head;
3239 struct ccb_getdevlist *cgdl;
3240 u_int i;
3241 int s;
3242 struct cam_ed *device;
3243 int found;
3244
3245
3246 found = 0;
3247
3248 /*
3249 * Don't want anyone mucking with our data.
3250 */
3251 s = splcam();
3252 device = start_ccb->ccb_h.path->device;
3253 periph_head = &device->periphs;
3254 cgdl = &start_ccb->cgdl;
3255
3256 /*
3257 * Check and see if the list has changed since the user
3258 * last requested a list member. If so, tell them that the
3259 * list has changed, and therefore they need to start over
3260 * from the beginning.
3261 */
3262 if ((cgdl->index != 0) &&
3263 (cgdl->generation != device->generation)) {
3264 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3265 splx(s);
3266 break;
3267 }
3268
3269 /*
3270 * Traverse the list of peripherals and attempt to find
3271 * the requested peripheral.
3272 */
3273 for (nperiph = SLIST_FIRST(periph_head), i = 0;
3274 (nperiph != NULL) && (i <= cgdl->index);
3275 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3276 if (i == cgdl->index) {
3277 strncpy(cgdl->periph_name,
3278 nperiph->periph_name,
3279 DEV_IDLEN);
3280 cgdl->unit_number = nperiph->unit_number;
3281 found = 1;
3282 }
3283 }
3284 if (found == 0) {
3285 cgdl->status = CAM_GDEVLIST_ERROR;
3286 splx(s);
3287 break;
3288 }
3289
3290 if (nperiph == NULL)
3291 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3292 else
3293 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3294
3295 cgdl->index++;
3296 cgdl->generation = device->generation;
3297
3298 splx(s);
3299 cgdl->ccb_h.status = CAM_REQ_CMP;
3300 break;
3301 }
3302 case XPT_DEV_MATCH:
3303 {
3304 int s;
3305 dev_pos_type position_type;
3306 struct ccb_dev_match *cdm;
3307
3308 cdm = &start_ccb->cdm;
3309
3310 /*
3311 * Prevent EDT changes while we traverse it.
3312 */
3313 s = splcam();
3314 /*
3315 * There are two ways of getting at information in the EDT.
3316 * The first way is via the primary EDT tree. It starts
3317 * with a list of busses, then a list of targets on a bus,
3318 * then devices/luns on a target, and then peripherals on a
3319 * device/lun. The "other" way is by the peripheral driver
3320 * lists. The peripheral driver lists are organized by
3321 * peripheral driver. (obviously) So it makes sense to
3322 * use the peripheral driver list if the user is looking
3323 * for something like "da1", or all "da" devices. If the
3324 * user is looking for something on a particular bus/target
3325 * or lun, it's generally better to go through the EDT tree.
3326 */
3327
3328 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3329 position_type = cdm->pos.position_type;
3330 else {
3331 u_int i;
3332
3333 position_type = CAM_DEV_POS_NONE;
3334
3335 for (i = 0; i < cdm->num_patterns; i++) {
3336 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3337 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3338 position_type = CAM_DEV_POS_EDT;
3339 break;
3340 }
3341 }
3342
3343 if (cdm->num_patterns == 0)
3344 position_type = CAM_DEV_POS_EDT;
3345 else if (position_type == CAM_DEV_POS_NONE)
3346 position_type = CAM_DEV_POS_PDRV;
3347 }
3348
3349 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3350 case CAM_DEV_POS_EDT:
3351 xptedtmatch(cdm);
3352 break;
3353 case CAM_DEV_POS_PDRV:
3354 xptperiphlistmatch(cdm);
3355 break;
3356 default:
3357 cdm->status = CAM_DEV_MATCH_ERROR;
3358 break;
3359 }
3360
3361 splx(s);
3362
3363 if (cdm->status == CAM_DEV_MATCH_ERROR)
3364 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3365 else
3366 start_ccb->ccb_h.status = CAM_REQ_CMP;
3367
3368 break;
3369 }
3370 case XPT_SASYNC_CB:
3371 {
3372 struct ccb_setasync *csa;
3373 struct async_node *cur_entry;
3374 struct async_list *async_head;
3375 u_int32_t added;
3376 int s;
3377
3378 csa = &start_ccb->csa;
3379 added = csa->event_enable;
3380 async_head = &csa->ccb_h.path->device->asyncs;
3381
3382 /*
3383 * If there is already an entry for us, simply
3384 * update it.
3385 */
3386 s = splcam();
3387 cur_entry = SLIST_FIRST(async_head);
3388 while (cur_entry != NULL) {
3389 if ((cur_entry->callback_arg == csa->callback_arg)
3390 && (cur_entry->callback == csa->callback))
3391 break;
3392 cur_entry = SLIST_NEXT(cur_entry, links);
3393 }
3394
3395 if (cur_entry != NULL) {
3396 /*
3397 * If the request has no flags set,
3398 * remove the entry.
3399 */
3400 added &= ~cur_entry->event_enable;
3401 if (csa->event_enable == 0) {
3402 SLIST_REMOVE(async_head, cur_entry,
3403 async_node, links);
3404 csa->ccb_h.path->device->refcount--;
3405 free(cur_entry, M_CAMXPT);
3406 } else {
3407 cur_entry->event_enable = csa->event_enable;
3408 }
3409 } else {
3410 cur_entry = malloc(sizeof(*cur_entry), M_CAMXPT,
3411 M_NOWAIT);
3412 if (cur_entry == NULL) {
3413 splx(s);
3414 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3415 break;
3416 }
3417 cur_entry->event_enable = csa->event_enable;
3418 cur_entry->callback_arg = csa->callback_arg;
3419 cur_entry->callback = csa->callback;
3420 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3421 csa->ccb_h.path->device->refcount++;
3422 }
3423
3424 if ((added & AC_FOUND_DEVICE) != 0) {
3425 /*
3426 * Get this peripheral up to date with all
3427 * the currently existing devices.
3428 */
3429 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3430 }
3431 if ((added & AC_PATH_REGISTERED) != 0) {
3432 /*
3433 * Get this peripheral up to date with all
3434 * the currently existing busses.
3435 */
3436 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3437 }
3438 splx(s);
3439 start_ccb->ccb_h.status = CAM_REQ_CMP;
3440 break;
3441 }
3442 case XPT_REL_SIMQ:
3443 {
3444 struct ccb_relsim *crs;
3445 struct cam_ed *dev;
3446 int s;
3447
3448 crs = &start_ccb->crs;
3449 dev = crs->ccb_h.path->device;
3450 if (dev == NULL) {
3451
3452 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3453 break;
3454 }
3455
3456 s = splcam();
3457
3458 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3459
3460 if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3461
3462 /* Don't ever go below one opening */
3463 if (crs->openings > 0) {
3464 xpt_dev_ccbq_resize(crs->ccb_h.path,
3465 crs->openings);
3466
3467 if (bootverbose) {
3468 xpt_print_path(crs->ccb_h.path);
3469 printf("tagged openings "
3470 "now %d\n",
3471 crs->openings);
3472 }
3473 }
3474 }
3475 }
3476
3477 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3478
3479 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3480
3481 /*
3482 * Just extend the old timeout and decrement
3483 * the freeze count so that a single timeout
3484 * is sufficient for releasing the queue.
3485 */
3486 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3487 untimeout(xpt_release_devq_timeout,
3488 dev, dev->c_handle);
3489 } else {
3490
3491 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3492 }
3493
3494 dev->c_handle =
3495 timeout(xpt_release_devq_timeout,
3496 dev,
3497 (crs->release_timeout * hz) / 1000);
3498
3499 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3500
3501 }
3502
3503 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3504
3505 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3506 /*
3507 * Decrement the freeze count so that a single
3508 * completion is still sufficient to unfreeze
3509 * the queue.
3510 */
3511 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3512 } else {
3513
3514 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3515 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3516 }
3517 }
3518
3519 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3520
3521 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3522 || (dev->ccbq.dev_active == 0)) {
3523
3524 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3525 } else {
3526
3527 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3528 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3529 }
3530 }
3531 splx(s);
3532
3533 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3534
3535 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3536 /*run_queue*/TRUE);
3537 }
3538 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3539 start_ccb->ccb_h.status = CAM_REQ_CMP;
3540 break;
3541 }
3542 case XPT_SCAN_BUS:
3543 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3544 break;
3545 case XPT_SCAN_LUN:
3546 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3547 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3548 start_ccb);
3549 break;
3550 case XPT_DEBUG: {
3551#ifdef CAMDEBUG
3552 int s;
3553
3554 s = splcam();
3555#ifdef CAM_DEBUG_DELAY
3556 cam_debug_delay = CAM_DEBUG_DELAY;
3557#endif
3558 cam_dflags = start_ccb->cdbg.flags;
3559 if (cam_dpath != NULL) {
3560 xpt_free_path(cam_dpath);
3561 cam_dpath = NULL;
3562 }
3563
3564 if (cam_dflags != CAM_DEBUG_NONE) {
3565 if (xpt_create_path(&cam_dpath, xpt_periph,
3566 start_ccb->ccb_h.path_id,
3567 start_ccb->ccb_h.target_id,
3568 start_ccb->ccb_h.target_lun) !=
3569 CAM_REQ_CMP) {
3570 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3571 cam_dflags = CAM_DEBUG_NONE;
3572 } else {
3573 start_ccb->ccb_h.status = CAM_REQ_CMP;
3574 xpt_print_path(cam_dpath);
3575 printf("debugging flags now %x\n", cam_dflags);
3576 }
3577 } else {
3578 cam_dpath = NULL;
3579 start_ccb->ccb_h.status = CAM_REQ_CMP;
3580 }
3581 splx(s);
3582#else /* !CAMDEBUG */
3583 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3584#endif /* CAMDEBUG */
3585 break;
3586 }
3587 case XPT_NOOP:
3588 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3589 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3590 start_ccb->ccb_h.status = CAM_REQ_CMP;
3591 break;
3592 default:
3593 case XPT_SDEV_TYPE:
3594 case XPT_TERM_IO:
3595 case XPT_ENG_INQ:
3596 /* XXX Implement */
3597 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3598 break;
3599 }
3600 splx(iopl);
3601}
3602
3603void
3604xpt_polled_action(union ccb *start_ccb)
3605{
3606 int s;
3607 u_int32_t timeout;
3608 struct cam_sim *sim;
3609 struct cam_devq *devq;
3610 struct cam_ed *dev;
3611
3612 GIANT_REQUIRED;
3613
3614 timeout = start_ccb->ccb_h.timeout;
3615 sim = start_ccb->ccb_h.path->bus->sim;
3616 devq = sim->devq;
3617 dev = start_ccb->ccb_h.path->device;
3618
3619 s = splcam();
3620
3621 /*
3622 * Steal an opening so that no other queued requests
3623 * can get it before us while we simulate interrupts.
3624 */
3625 dev->ccbq.devq_openings--;
3626 dev->ccbq.dev_openings--;
3627
3628 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3629 && (--timeout > 0)) {
3630 DELAY(1000);
3631 (*(sim->sim_poll))(sim);
3632 camisr(&cam_bioq);
3633 }
3634
3635 dev->ccbq.devq_openings++;
3636 dev->ccbq.dev_openings++;
3637
3638 if (timeout != 0) {
3639 xpt_action(start_ccb);
3640 while(--timeout > 0) {
3641 (*(sim->sim_poll))(sim);
3642 camisr(&cam_bioq);
3643 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3644 != CAM_REQ_INPROG)
3645 break;
3646 DELAY(1000);
3647 }
3648 if (timeout == 0) {
3649 /*
3650 * XXX Is it worth adding a sim_timeout entry
3651 * point so we can attempt recovery? If
3652 * this is only used for dumps, I don't think
3653 * it is.
3654 */
3655 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3656 }
3657 } else {
3658 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3659 }
3660 splx(s);
3661}
3662
3663/*
3664 * Schedule a peripheral driver to receive a ccb when it's
3665 * target device has space for more transactions.
3666 */
3667void
3668xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3669{
3670 struct cam_ed *device;
3671 int s;
3672 int runq;
3673
3674 GIANT_REQUIRED;
3675
3676 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3677 device = perph->path->device;
3678 s = splsoftcam();
3679 if (periph_is_queued(perph)) {
3680 /* Simply reorder based on new priority */
3681 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3682 (" change priority to %d\n", new_priority));
3683 if (new_priority < perph->pinfo.priority) {
3684 camq_change_priority(&device->drvq,
3685 perph->pinfo.index,
3686 new_priority);
3687 }
3688 runq = 0;
3689 } else {
3690 /* New entry on the queue */
3691 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3692 (" added periph to queue\n"));
3693 perph->pinfo.priority = new_priority;
3694 perph->pinfo.generation = ++device->drvq.generation;
3695 camq_insert(&device->drvq, &perph->pinfo);
3696 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3697 }
3698 splx(s);
3699 if (runq != 0) {
3700 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3701 (" calling xpt_run_devq\n"));
3702 xpt_run_dev_allocq(perph->path->bus);
3703 }
3704}
3705
3706
3707/*
3708 * Schedule a device to run on a given queue.
3709 * If the device was inserted as a new entry on the queue,
3710 * return 1 meaning the device queue should be run. If we
3711 * were already queued, implying someone else has already
3712 * started the queue, return 0 so the caller doesn't attempt
3713 * to run the queue. Must be run at either splsoftcam
3714 * (or splcam since that encompases splsoftcam).
3715 */
3716static int
3717xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3718 u_int32_t new_priority)
3719{
3720 int retval;
3721 u_int32_t old_priority;
3722
3723 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3724
3725 old_priority = pinfo->priority;
3726
3727 /*
3728 * Are we already queued?
3729 */
3730 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3731 /* Simply reorder based on new priority */
3732 if (new_priority < old_priority) {
3733 camq_change_priority(queue, pinfo->index,
3734 new_priority);
3735 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3736 ("changed priority to %d\n",
3737 new_priority));
3738 }
3739 retval = 0;
3740 } else {
3741 /* New entry on the queue */
3742 if (new_priority < old_priority)
3743 pinfo->priority = new_priority;
3744
3745 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3746 ("Inserting onto queue\n"));
3747 pinfo->generation = ++queue->generation;
3748 camq_insert(queue, pinfo);
3749 retval = 1;
3750 }
3751 return (retval);
3752}
3753
3754static void
3755xpt_run_dev_allocq(struct cam_eb *bus)
3756{
3757 struct cam_devq *devq;
3758 int s;
3759
3760 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3761 devq = bus->sim->devq;
3762
3763 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3764 (" qfrozen_cnt == 0x%x, entries == %d, "
3765 "openings == %d, active == %d\n",
3766 devq->alloc_queue.qfrozen_cnt,
3767 devq->alloc_queue.entries,
3768 devq->alloc_openings,
3769 devq->alloc_active));
3770
3771 s = splsoftcam();
3772 devq->alloc_queue.qfrozen_cnt++;
3773 while ((devq->alloc_queue.entries > 0)
3774 && (devq->alloc_openings > 0)
3775 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3776 struct cam_ed_qinfo *qinfo;
3777 struct cam_ed *device;
3778 union ccb *work_ccb;
3779 struct cam_periph *drv;
3780 struct camq *drvq;
3781
3782 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3783 CAMQ_HEAD);
3784 device = qinfo->device;
3785
3786 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3787 ("running device %p\n", device));
3788
3789 drvq = &device->drvq;
3790
3791#ifdef CAMDEBUG
3792 if (drvq->entries <= 0) {
3793 panic("xpt_run_dev_allocq: "
3794 "Device on queue without any work to do");
3795 }
3796#endif
3797 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3798 devq->alloc_openings--;
3799 devq->alloc_active++;
3800 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3801 splx(s);
3802 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3803 drv->pinfo.priority);
3804 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3805 ("calling periph start\n"));
3806 drv->periph_start(drv, work_ccb);
3807 } else {
3808 /*
3809 * Malloc failure in alloc_ccb
3810 */
3811 /*
3812 * XXX add us to a list to be run from free_ccb
3813 * if we don't have any ccbs active on this
3814 * device queue otherwise we may never get run
3815 * again.
3816 */
3817 break;
3818 }
3819
3820 /* Raise IPL for possible insertion and test at top of loop */
3821 s = splsoftcam();
3822
3823 if (drvq->entries > 0) {
3824 /* We have more work. Attempt to reschedule */
3825 xpt_schedule_dev_allocq(bus, device);
3826 }
3827 }
3828 devq->alloc_queue.qfrozen_cnt--;
3829 splx(s);
3830}
3831
3832static void
3833xpt_run_dev_sendq(struct cam_eb *bus)
3834{
3835 struct cam_devq *devq;
3836 int s;
3837
3838 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3839
3840 devq = bus->sim->devq;
3841
3842 s = splcam();
3843 devq->send_queue.qfrozen_cnt++;
3844 splx(s);
3845 s = splsoftcam();
3846 while ((devq->send_queue.entries > 0)
3847 && (devq->send_openings > 0)) {
3848 struct cam_ed_qinfo *qinfo;
3849 struct cam_ed *device;
3850 union ccb *work_ccb;
3851 struct cam_sim *sim;
3852 int ospl;
3853
3854 ospl = splcam();
3855 if (devq->send_queue.qfrozen_cnt > 1) {
3856 splx(ospl);
3857 break;
3858 }
3859
3860 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3861 CAMQ_HEAD);
3862 device = qinfo->device;
3863
3864 /*
3865 * If the device has been "frozen", don't attempt
3866 * to run it.
3867 */
3868 if (device->qfrozen_cnt > 0) {
3869 splx(ospl);
3870 continue;
3871 }
3872
3873 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3874 ("running device %p\n", device));
3875
3876 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3877 if (work_ccb == NULL) {
3878 printf("device on run queue with no ccbs???\n");
3879 splx(ospl);
3880 continue;
3881 }
3882
3883 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3884
3885 if (num_highpower <= 0) {
3886 /*
3887 * We got a high power command, but we
3888 * don't have any available slots. Freeze
3889 * the device queue until we have a slot
3890 * available.
3891 */
3892 device->qfrozen_cnt++;
3893 STAILQ_INSERT_TAIL(&highpowerq,
3894 &work_ccb->ccb_h,
3895 xpt_links.stqe);
3896
3897 splx(ospl);
3898 continue;
3899 } else {
3900 /*
3901 * Consume a high power slot while
3902 * this ccb runs.
3903 */
3904 num_highpower--;
3905 }
3906 }
3907 devq->active_dev = device;
3908 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3909
3910 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3911 splx(ospl);
3912
3913 devq->send_openings--;
3914 devq->send_active++;
3915
3916 if (device->ccbq.queue.entries > 0)
3917 xpt_schedule_dev_sendq(bus, device);
3918
3919 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3920 /*
3921 * The client wants to freeze the queue
3922 * after this CCB is sent.
3923 */
3924 ospl = splcam();
3925 device->qfrozen_cnt++;
3926 splx(ospl);
3927 }
3928
3929 splx(s);
3930
3931 /* In Target mode, the peripheral driver knows best... */
3932 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3933 if ((device->inq_flags & SID_CmdQue) != 0
3934 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3935 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3936 else
3937 /*
3938 * Clear this in case of a retried CCB that
3939 * failed due to a rejected tag.
3940 */
3941 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3942 }
3943
3944 /*
3945 * Device queues can be shared among multiple sim instances
3946 * that reside on different busses. Use the SIM in the queue
3947 * CCB's path, rather than the one in the bus that was passed
3948 * into this function.
3949 */
3950 sim = work_ccb->ccb_h.path->bus->sim;
3951 (*(sim->sim_action))(sim, work_ccb);
3952
3953 ospl = splcam();
3954 devq->active_dev = NULL;
3955 splx(ospl);
3956 /* Raise IPL for possible insertion and test at top of loop */
3957 s = splsoftcam();
3958 }
3959 splx(s);
3960 s = splcam();
3961 devq->send_queue.qfrozen_cnt--;
3962 splx(s);
3963}
3964
3965/*
3966 * This function merges stuff from the slave ccb into the master ccb, while
3967 * keeping important fields in the master ccb constant.
3968 */
3969void
3970xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3971{
3972 GIANT_REQUIRED;
3973
3974 /*
3975 * Pull fields that are valid for peripheral drivers to set
3976 * into the master CCB along with the CCB "payload".
3977 */
3978 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3979 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3980 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3981 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3982 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3983 sizeof(union ccb) - sizeof(struct ccb_hdr));
3984}
3985
3986void
3987xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3988{
3989 GIANT_REQUIRED;
3990
3991 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3992 ccb_h->pinfo.priority = priority;
3993 ccb_h->path = path;
3994 ccb_h->path_id = path->bus->path_id;
3995 if (path->target)
3996 ccb_h->target_id = path->target->target_id;
3997 else
3998 ccb_h->target_id = CAM_TARGET_WILDCARD;
3999 if (path->device) {
4000 ccb_h->target_lun = path->device->lun_id;
4001 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
4002 } else {
4003 ccb_h->target_lun = CAM_TARGET_WILDCARD;
4004 }
4005 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4006 ccb_h->flags = 0;
4007}
4008
4009/* Path manipulation functions */
4010cam_status
4011xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
4012 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4013{
4014 struct cam_path *path;
4015 cam_status status;
4016
4017 GIANT_REQUIRED;
4018
4019 path = (struct cam_path *)malloc(sizeof(*path), M_CAMXPT, M_NOWAIT);
4020
4021 if (path == NULL) {
4022 status = CAM_RESRC_UNAVAIL;
4023 return(status);
4024 }
4025 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
4026 if (status != CAM_REQ_CMP) {
4027 free(path, M_CAMXPT);
4028 path = NULL;
4029 }
4030 *new_path_ptr = path;
4031 return (status);
4032}
4033
4034static cam_status
4035xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
4036 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4037{
4038 struct cam_eb *bus;
4039 struct cam_et *target;
4040 struct cam_ed *device;
4041 cam_status status;
4042 int s;
4043
4044 status = CAM_REQ_CMP; /* Completed without error */
4045 target = NULL; /* Wildcarded */
4046 device = NULL; /* Wildcarded */
4047
4048 /*
4049 * We will potentially modify the EDT, so block interrupts
4050 * that may attempt to create cam paths.
4051 */
4052 s = splcam();
4053 bus = xpt_find_bus(path_id);
4054 if (bus == NULL) {
4055 status = CAM_PATH_INVALID;
4056 } else {
4057 target = xpt_find_target(bus, target_id);
4058 if (target == NULL) {
4059 /* Create one */
4060 struct cam_et *new_target;
4061
4062 new_target = xpt_alloc_target(bus, target_id);
4063 if (new_target == NULL) {
4064 status = CAM_RESRC_UNAVAIL;
4065 } else {
4066 target = new_target;
4067 }
4068 }
4069 if (target != NULL) {
4070 device = xpt_find_device(target, lun_id);
4071 if (device == NULL) {
4072 /* Create one */
4073 struct cam_ed *new_device;
4074
4075 new_device = xpt_alloc_device(bus,
4076 target,
4077 lun_id);
4078 if (new_device == NULL) {
4079 status = CAM_RESRC_UNAVAIL;
4080 } else {
4081 device = new_device;
4082 }
4083 }
4084 }
4085 }
4086 splx(s);
4087
4088 /*
4089 * Only touch the user's data if we are successful.
4090 */
4091 if (status == CAM_REQ_CMP) {
4092 new_path->periph = perph;
4093 new_path->bus = bus;
4094 new_path->target = target;
4095 new_path->device = device;
4096 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
4097 } else {
4098 if (device != NULL)
4099 xpt_release_device(bus, target, device);
4100 if (target != NULL)
4101 xpt_release_target(bus, target);
4102 if (bus != NULL)
4103 xpt_release_bus(bus);
4104 }
4105 return (status);
4106}
4107
4108static void
4109xpt_release_path(struct cam_path *path)
4110{
4111 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
4112 if (path->device != NULL) {
4113 xpt_release_device(path->bus, path->target, path->device);
4114 path->device = NULL;
4115 }
4116 if (path->target != NULL) {
4117 xpt_release_target(path->bus, path->target);
4118 path->target = NULL;
4119 }
4120 if (path->bus != NULL) {
4121 xpt_release_bus(path->bus);
4122 path->bus = NULL;
4123 }
4124}
4125
4126void
4127xpt_free_path(struct cam_path *path)
4128{
4129 GIANT_REQUIRED;
4130
4131 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
4132 xpt_release_path(path);
4133 free(path, M_CAMXPT);
4134}
4135
4136
4137/*
4138 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4139 * in path1, 2 for match with wildcards in path2.
4140 */
4141int
4142xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
4143{
4144 GIANT_REQUIRED;
4145
4146 int retval = 0;
4147
4148 if (path1->bus != path2->bus) {
4149 if (path1->bus->path_id == CAM_BUS_WILDCARD)
4150 retval = 1;
4151 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4152 retval = 2;
4153 else
4154 return (-1);
4155 }
4156 if (path1->target != path2->target) {
4157 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4158 if (retval == 0)
4159 retval = 1;
4160 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4161 retval = 2;
4162 else
4163 return (-1);
4164 }
4165 if (path1->device != path2->device) {
4166 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4167 if (retval == 0)
4168 retval = 1;
4169 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4170 retval = 2;
4171 else
4172 return (-1);
4173 }
4174 return (retval);
4175}
4176
4177void
4178xpt_print_path(struct cam_path *path)
4179{
4180 GIANT_REQUIRED;
4181
4182 if (path == NULL)
4183 printf("(nopath): ");
4184 else {
4185 if (path->periph != NULL)
4186 printf("(%s%d:", path->periph->periph_name,
4187 path->periph->unit_number);
4188 else
4189 printf("(noperiph:");
4190
4191 if (path->bus != NULL)
4192 printf("%s%d:%d:", path->bus->sim->sim_name,
4193 path->bus->sim->unit_number,
4194 path->bus->sim->bus_id);
4195 else
4196 printf("nobus:");
4197
4198 if (path->target != NULL)
4199 printf("%d:", path->target->target_id);
4200 else
4201 printf("X:");
4202
4203 if (path->device != NULL)
4204 printf("%d): ", path->device->lun_id);
4205 else
4206 printf("X): ");
4207 }
4208}
4209
4210int
4211xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4212{
4213 struct sbuf sb;
4214
4215 GIANT_REQUIRED;
4216
4217 sbuf_new(&sb, str, str_len, 0);
4218
4219 if (path == NULL)
4220 sbuf_printf(&sb, "(nopath): ");
4221 else {
4222 if (path->periph != NULL)
4223 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4224 path->periph->unit_number);
4225 else
4226 sbuf_printf(&sb, "(noperiph:");
4227
4228 if (path->bus != NULL)
4229 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
4230 path->bus->sim->unit_number,
4231 path->bus->sim->bus_id);
4232 else
4233 sbuf_printf(&sb, "nobus:");
4234
4235 if (path->target != NULL)
4236 sbuf_printf(&sb, "%d:", path->target->target_id);
4237 else
4238 sbuf_printf(&sb, "X:");
4239
4240 if (path->device != NULL)
4241 sbuf_printf(&sb, "%d): ", path->device->lun_id);
4242 else
4243 sbuf_printf(&sb, "X): ");
4244 }
4245 sbuf_finish(&sb);
4246
4247 return(sbuf_len(&sb));
4248}
4249
4250path_id_t
4251xpt_path_path_id(struct cam_path *path)
4252{
4253 GIANT_REQUIRED;
4254
4255 return(path->bus->path_id);
4256}
4257
4258target_id_t
4259xpt_path_target_id(struct cam_path *path)
4260{
4261 GIANT_REQUIRED;
4262
4263 if (path->target != NULL)
4264 return (path->target->target_id);
4265 else
4266 return (CAM_TARGET_WILDCARD);
4267}
4268
4269lun_id_t
4270xpt_path_lun_id(struct cam_path *path)
4271{
4272 GIANT_REQUIRED;
4273
4274 if (path->device != NULL)
4275 return (path->device->lun_id);
4276 else
4277 return (CAM_LUN_WILDCARD);
4278}
4279
4280struct cam_sim *
4281xpt_path_sim(struct cam_path *path)
4282{
4283 GIANT_REQUIRED;
4284
4285 return (path->bus->sim);
4286}
4287
4288struct cam_periph*
4289xpt_path_periph(struct cam_path *path)
4290{
4291 GIANT_REQUIRED;
4292
4293 return (path->periph);
4294}
4295
4296/*
4297 * Release a CAM control block for the caller. Remit the cost of the structure
4298 * to the device referenced by the path. If the this device had no 'credits'
4299 * and peripheral drivers have registered async callbacks for this notification
4300 * call them now.
4301 */
4302void
4303xpt_release_ccb(union ccb *free_ccb)
4304{
4305 int s;
4306 struct cam_path *path;
4307 struct cam_ed *device;
4308 struct cam_eb *bus;
4309
4310 GIANT_REQUIRED;
4311
4312 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4313 path = free_ccb->ccb_h.path;
4314 device = path->device;
4315 bus = path->bus;
4316 s = splsoftcam();
4317 cam_ccbq_release_opening(&device->ccbq);
4318 if (xpt_ccb_count > xpt_max_ccbs) {
4319 xpt_free_ccb(free_ccb);
4320 xpt_ccb_count--;
4321 } else {
4322 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4323 }
4324 bus->sim->devq->alloc_openings++;
4325 bus->sim->devq->alloc_active--;
4326 /* XXX Turn this into an inline function - xpt_run_device?? */
4327 if ((device_is_alloc_queued(device) == 0)
4328 && (device->drvq.entries > 0)) {
4329 xpt_schedule_dev_allocq(bus, device);
4330 }
4331 splx(s);
4332 if (dev_allocq_is_runnable(bus->sim->devq))
4333 xpt_run_dev_allocq(bus);
4334}
4335
4336/* Functions accessed by SIM drivers */
4337
4338/*
4339 * A sim structure, listing the SIM entry points and instance
4340 * identification info is passed to xpt_bus_register to hook the SIM
4341 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4342 * for this new bus and places it in the array of busses and assigns
4343 * it a path_id. The path_id may be influenced by "hard wiring"
4344 * information specified by the user. Once interrupt services are
4345 * availible, the bus will be probed.
4346 */
4347int32_t
4348xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4349{
4350 struct cam_eb *new_bus;
4351 struct cam_eb *old_bus;
4352 struct ccb_pathinq cpi;
4353 int s;
4354
4355 GIANT_REQUIRED;
4356
4357 sim->bus_id = bus;
4358 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4359 M_CAMXPT, M_NOWAIT);
4360 if (new_bus == NULL) {
4361 /* Couldn't satisfy request */
4362 return (CAM_RESRC_UNAVAIL);
4363 }
4364
4365 if (strcmp(sim->sim_name, "xpt") != 0) {
4366
4367 sim->path_id =
4368 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4369 }
4370
4371 TAILQ_INIT(&new_bus->et_entries);
4372 new_bus->path_id = sim->path_id;
4373 new_bus->sim = sim;
4374 timevalclear(&new_bus->last_reset);
4375 new_bus->flags = 0;
4376 new_bus->refcount = 1; /* Held until a bus_deregister event */
4377 new_bus->generation = 0;
4378 s = splcam();
4379 old_bus = TAILQ_FIRST(&xpt_busses);
4380 while (old_bus != NULL
4381 && old_bus->path_id < new_bus->path_id)
4382 old_bus = TAILQ_NEXT(old_bus, links);
4383 if (old_bus != NULL)
4384 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4385 else
4386 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4387 bus_generation++;
4388 splx(s);
4389
4390 /* Notify interested parties */
4391 if (sim->path_id != CAM_XPT_PATH_ID) {
4392 struct cam_path path;
4393
4394 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4395 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4396 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4397 cpi.ccb_h.func_code = XPT_PATH_INQ;
4398 xpt_action((union ccb *)&cpi);
4399 xpt_async(AC_PATH_REGISTERED, &path, &cpi);
4400 xpt_release_path(&path);
4401 }
4402 return (CAM_SUCCESS);
4403}
4404
4405int32_t
4406xpt_bus_deregister(path_id_t pathid)
4407{
4408 struct cam_path bus_path;
4409 cam_status status;
4410
4411 GIANT_REQUIRED;
4412
4413 status = xpt_compile_path(&bus_path, NULL, pathid,
4414 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4415 if (status != CAM_REQ_CMP)
4416 return (status);
4417
4418 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4419 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4420
4421 /* Release the reference count held while registered. */
4422 xpt_release_bus(bus_path.bus);
4423 xpt_release_path(&bus_path);
4424
4425 return (CAM_REQ_CMP);
4426}
4427
4428static path_id_t
4429xptnextfreepathid(void)
4430{
4431 struct cam_eb *bus;
4432 path_id_t pathid;
4433 const char *strval;
4434
4435 pathid = 0;
4436 bus = TAILQ_FIRST(&xpt_busses);
4437retry:
4438 /* Find an unoccupied pathid */
4439 while (bus != NULL
4440 && bus->path_id <= pathid) {
4441 if (bus->path_id == pathid)
4442 pathid++;
4443 bus = TAILQ_NEXT(bus, links);
4444 }
4445
4446 /*
4447 * Ensure that this pathid is not reserved for
4448 * a bus that may be registered in the future.
4449 */
4450 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4451 ++pathid;
4452 /* Start the search over */
4453 goto retry;
4454 }
4455 return (pathid);
4456}
4457
4458static path_id_t
4459xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4460{
4461 path_id_t pathid;
4462 int i, dunit, val;
4463 char buf[32];
4464 const char *dname;
4465
4466 pathid = CAM_XPT_PATH_ID;
4467 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4468 i = 0;
4469 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4470 if (strcmp(dname, "scbus")) {
4471 /* Avoid a bit of foot shooting. */
4472 continue;
4473 }
4474 if (dunit < 0) /* unwired?! */
4475 continue;
4476 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4477 if (sim_bus == val) {
4478 pathid = dunit;
4479 break;
4480 }
4481 } else if (sim_bus == 0) {
4482 /* Unspecified matches bus 0 */
4483 pathid = dunit;
4484 break;
4485 } else {
4486 printf("Ambiguous scbus configuration for %s%d "
4487 "bus %d, cannot wire down. The kernel "
4488 "config entry for scbus%d should "
4489 "specify a controller bus.\n"
4490 "Scbus will be assigned dynamically.\n",
4491 sim_name, sim_unit, sim_bus, dunit);
4492 break;
4493 }
4494 }
4495
4496 if (pathid == CAM_XPT_PATH_ID)
4497 pathid = xptnextfreepathid();
4498 return (pathid);
4499}
4500
4501void
4502xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4503{
4504 struct cam_eb *bus;
4505 struct cam_et *target, *next_target;
4506 struct cam_ed *device, *next_device;
4507 int s;
4508
4509 GIANT_REQUIRED;
4510
4511 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4512
4513 /*
4514 * Most async events come from a CAM interrupt context. In
4515 * a few cases, the error recovery code at the peripheral layer,
4516 * which may run from our SWI or a process context, may signal
4517 * deferred events with a call to xpt_async. Ensure async
4518 * notifications are serialized by blocking cam interrupts.
4519 */
4520 s = splcam();
4521
4522 bus = path->bus;
4523
4524 if (async_code == AC_BUS_RESET) {
4525 int s;
4526
4527 s = splclock();
4528 /* Update our notion of when the last reset occurred */
4529 microtime(&bus->last_reset);
4530 splx(s);
4531 }
4532
4533 for (target = TAILQ_FIRST(&bus->et_entries);
4534 target != NULL;
4535 target = next_target) {
4536
4537 next_target = TAILQ_NEXT(target, links);
4538
4539 if (path->target != target
4540 && path->target->target_id != CAM_TARGET_WILDCARD
4541 && target->target_id != CAM_TARGET_WILDCARD)
4542 continue;
4543
4544 if (async_code == AC_SENT_BDR) {
4545 int s;
4546
4547 /* Update our notion of when the last reset occurred */
4548 s = splclock();
4549 microtime(&path->target->last_reset);
4550 splx(s);
4551 }
4552
4553 for (device = TAILQ_FIRST(&target->ed_entries);
4554 device != NULL;
4555 device = next_device) {
4556
4557 next_device = TAILQ_NEXT(device, links);
4558
4559 if (path->device != device
4560 && path->device->lun_id != CAM_LUN_WILDCARD
4561 && device->lun_id != CAM_LUN_WILDCARD)
4562 continue;
4563
4564 xpt_dev_async(async_code, bus, target,
4565 device, async_arg);
4566
4567 xpt_async_bcast(&device->asyncs, async_code,
4568 path, async_arg);
4569 }
4570 }
4571
4572 /*
4573 * If this wasn't a fully wildcarded async, tell all
4574 * clients that want all async events.
4575 */
4576 if (bus != xpt_periph->path->bus)
4577 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4578 path, async_arg);
4579 splx(s);
4580}
4581
4582static void
4583xpt_async_bcast(struct async_list *async_head,
4584 u_int32_t async_code,
4585 struct cam_path *path, void *async_arg)
4586{
4587 struct async_node *cur_entry;
4588
4589 cur_entry = SLIST_FIRST(async_head);
4590 while (cur_entry != NULL) {
4591 struct async_node *next_entry;
4592 /*
4593 * Grab the next list entry before we call the current
4594 * entry's callback. This is because the callback function
4595 * can delete its async callback entry.
4596 */
4597 next_entry = SLIST_NEXT(cur_entry, links);
4598 if ((cur_entry->event_enable & async_code) != 0)
4599 cur_entry->callback(cur_entry->callback_arg,
4600 async_code, path,
4601 async_arg);
4602 cur_entry = next_entry;
4603 }
4604}
4605
4606/*
4607 * Handle any per-device event notifications that require action by the XPT.
4608 */
4609static void
4610xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4611 struct cam_ed *device, void *async_arg)
4612{
4613 cam_status status;
4614 struct cam_path newpath;
4615
4616 /*
4617 * We only need to handle events for real devices.
4618 */
4619 if (target->target_id == CAM_TARGET_WILDCARD
4620 || device->lun_id == CAM_LUN_WILDCARD)
4621 return;
4622
4623 /*
4624 * We need our own path with wildcards expanded to
4625 * handle certain types of events.
4626 */
4627 if ((async_code == AC_SENT_BDR)
4628 || (async_code == AC_BUS_RESET)
4629 || (async_code == AC_INQ_CHANGED))
4630 status = xpt_compile_path(&newpath, NULL,
4631 bus->path_id,
4632 target->target_id,
4633 device->lun_id);
4634 else
4635 status = CAM_REQ_CMP_ERR;
4636
4637 if (status == CAM_REQ_CMP) {
4638
4639 /*
4640 * Allow transfer negotiation to occur in a
4641 * tag free environment.
4642 */
4643 if (async_code == AC_SENT_BDR
4644 || async_code == AC_BUS_RESET)
4645 xpt_toggle_tags(&newpath);
4646
4647 if (async_code == AC_INQ_CHANGED) {
4648 /*
4649 * We've sent a start unit command, or
4650 * something similar to a device that
4651 * may have caused its inquiry data to
4652 * change. So we re-scan the device to
4653 * refresh the inquiry data for it.
4654 */
4655 xpt_scan_lun(newpath.periph, &newpath,
4656 CAM_EXPECT_INQ_CHANGE, NULL);
4657 }
4658 xpt_release_path(&newpath);
4659 } else if (async_code == AC_LOST_DEVICE) {
4660 device->flags |= CAM_DEV_UNCONFIGURED;
4661 } else if (async_code == AC_TRANSFER_NEG) {
4662 struct ccb_trans_settings *settings;
4663
4664 settings = (struct ccb_trans_settings *)async_arg;
4665 xpt_set_transfer_settings(settings, device,
4666 /*async_update*/TRUE);
4667 }
4668}
4669
4670u_int32_t
4671xpt_freeze_devq(struct cam_path *path, u_int count)
4672{
4673 int s;
4674 struct ccb_hdr *ccbh;
4675
4676 GIANT_REQUIRED;
4677
4678 s = splcam();
4679 path->device->qfrozen_cnt += count;
4680
4681 /*
4682 * Mark the last CCB in the queue as needing
4683 * to be requeued if the driver hasn't
4684 * changed it's state yet. This fixes a race
4685 * where a ccb is just about to be queued to
4686 * a controller driver when it's interrupt routine
4687 * freezes the queue. To completly close the
4688 * hole, controller drives must check to see
4689 * if a ccb's status is still CAM_REQ_INPROG
4690 * under spl protection just before they queue
4691 * the CCB. See ahc_action/ahc_freeze_devq for
4692 * an example.
4693 */
4694 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4695 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4696 ccbh->status = CAM_REQUEUE_REQ;
4697 splx(s);
4698 return (path->device->qfrozen_cnt);
4699}
4700
4701u_int32_t
4702xpt_freeze_simq(struct cam_sim *sim, u_int count)
4703{
4704 GIANT_REQUIRED;
4705
4706 sim->devq->send_queue.qfrozen_cnt += count;
4707 if (sim->devq->active_dev != NULL) {
4708 struct ccb_hdr *ccbh;
4709
4710 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4711 ccb_hdr_tailq);
4712 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4713 ccbh->status = CAM_REQUEUE_REQ;
4714 }
4715 return (sim->devq->send_queue.qfrozen_cnt);
4716}
4717
4718static void
4719xpt_release_devq_timeout(void *arg)
4720{
4721 struct cam_ed *device;
4722
4723 device = (struct cam_ed *)arg;
4724
4725 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4726}
4727
4728void
4729xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4730{
4731 GIANT_REQUIRED;
4732
4733 xpt_release_devq_device(path->device, count, run_queue);
4734}
4735
4736static void
4737xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4738{
4739 int rundevq;
4740 int s0, s1;
4741
4742 rundevq = 0;
4743 s0 = splsoftcam();
4744 s1 = splcam();
4745 if (dev->qfrozen_cnt > 0) {
4746
4747 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4748 dev->qfrozen_cnt -= count;
4749 if (dev->qfrozen_cnt == 0) {
4750
4751 /*
4752 * No longer need to wait for a successful
4753 * command completion.
4754 */
4755 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4756
4757 /*
4758 * Remove any timeouts that might be scheduled
4759 * to release this queue.
4760 */
4761 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4762 untimeout(xpt_release_devq_timeout, dev,
4763 dev->c_handle);
4764 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4765 }
4766
4767 /*
4768 * Now that we are unfrozen schedule the
4769 * device so any pending transactions are
4770 * run.
4771 */
4772 if ((dev->ccbq.queue.entries > 0)
4773 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4774 && (run_queue != 0)) {
4775 rundevq = 1;
4776 }
4777 }
4778 }
4779 splx(s1);
4780 if (rundevq != 0)
4781 xpt_run_dev_sendq(dev->target->bus);
4782 splx(s0);
4783}
4784
4785void
4786xpt_release_simq(struct cam_sim *sim, int run_queue)
4787{
4788 int s;
4789 struct camq *sendq;
4790
4791 GIANT_REQUIRED;
4792
4793 sendq = &(sim->devq->send_queue);
4794 s = splcam();
4795 if (sendq->qfrozen_cnt > 0) {
4796
4797 sendq->qfrozen_cnt--;
4798 if (sendq->qfrozen_cnt == 0) {
4799 struct cam_eb *bus;
4800
4801 /*
4802 * If there is a timeout scheduled to release this
4803 * sim queue, remove it. The queue frozen count is
4804 * already at 0.
4805 */
4806 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4807 untimeout(xpt_release_simq_timeout, sim,
4808 sim->c_handle);
4809 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4810 }
4811 bus = xpt_find_bus(sim->path_id);
4812 splx(s);
4813
4814 if (run_queue) {
4815 /*
4816 * Now that we are unfrozen run the send queue.
4817 */
4818 xpt_run_dev_sendq(bus);
4819 }
4820 xpt_release_bus(bus);
4821 } else
4822 splx(s);
4823 } else
4824 splx(s);
4825}
4826
4827static void
4828xpt_release_simq_timeout(void *arg)
4829{
4830 struct cam_sim *sim;
4831
4832 sim = (struct cam_sim *)arg;
4833 xpt_release_simq(sim, /* run_queue */ TRUE);
4834}
4835
4836void
4837xpt_done(union ccb *done_ccb)
4838{
4839 int s;
4840
4841 s = splcam();
4842
4843 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4844 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4845 /*
4846 * Queue up the request for handling by our SWI handler
4847 * any of the "non-immediate" type of ccbs.
4848 */
4849 switch (done_ccb->ccb_h.path->periph->type) {
4850 case CAM_PERIPH_BIO:
4851 mtx_lock(&cam_bioq_lock);
4852 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4853 sim_links.tqe);
4854 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4855 mtx_unlock(&cam_bioq_lock);
4856 swi_sched(cambio_ih, 0);
4857 break;
4858 default:
4859 panic("unknown periph type %d",
4860 done_ccb->ccb_h.path->periph->type);
4861 }
4862 }
4863 splx(s);
4864}
4865
4866union ccb *
4867xpt_alloc_ccb()
4868{
4869 union ccb *new_ccb;
4870
4871 GIANT_REQUIRED;
4872
4873 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_WAITOK);
4874 return (new_ccb);
4875}
4876
4877union ccb *
4878xpt_alloc_ccb_nowait()
4879{
4880 union ccb *new_ccb;
4881
4882 GIANT_REQUIRED;
4883
4884 new_ccb = malloc(sizeof(*new_ccb), M_CAMXPT, M_NOWAIT);
4885 return (new_ccb);
4886}
4887
4888void
4889xpt_free_ccb(union ccb *free_ccb)
4890{
4891 free(free_ccb, M_CAMXPT);
4892}
4893
4894
4895
4896/* Private XPT functions */
4897
4898/*
4899 * Get a CAM control block for the caller. Charge the structure to the device
4900 * referenced by the path. If the this device has no 'credits' then the
4901 * device already has the maximum number of outstanding operations under way
4902 * and we return NULL. If we don't have sufficient resources to allocate more
4903 * ccbs, we also return NULL.
4904 */
4905static union ccb *
4906xpt_get_ccb(struct cam_ed *device)
4907{
4908 union ccb *new_ccb;
4909 int s;
4910
4911 s = splsoftcam();
4912 if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
4913 new_ccb = xpt_alloc_ccb_nowait();
4914 if (new_ccb == NULL) {
4915 splx(s);
4916 return (NULL);
4917 }
4918 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4919 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4920 xpt_links.sle);
4921 xpt_ccb_count++;
4922 }
4923 cam_ccbq_take_opening(&device->ccbq);
4924 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4925 splx(s);
4926 return (new_ccb);
4927}
4928
4929static void
4930xpt_release_bus(struct cam_eb *bus)
4931{
4932 int s;
4933
4934 s = splcam();
4935 if ((--bus->refcount == 0)
4936 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4937 TAILQ_REMOVE(&xpt_busses, bus, links);
4938 bus_generation++;
4939 splx(s);
4940 free(bus, M_CAMXPT);
4941 } else
4942 splx(s);
4943}
4944
4945static struct cam_et *
4946xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4947{
4948 struct cam_et *target;
4949
4950 target = (struct cam_et *)malloc(sizeof(*target), M_CAMXPT, M_NOWAIT);
4951 if (target != NULL) {
4952 struct cam_et *cur_target;
4953
4954 TAILQ_INIT(&target->ed_entries);
4955 target->bus = bus;
4956 target->target_id = target_id;
4957 target->refcount = 1;
4958 target->generation = 0;
4959 timevalclear(&target->last_reset);
4960 /*
4961 * Hold a reference to our parent bus so it
4962 * will not go away before we do.
4963 */
4964 bus->refcount++;
4965
4966 /* Insertion sort into our bus's target list */
4967 cur_target = TAILQ_FIRST(&bus->et_entries);
4968 while (cur_target != NULL && cur_target->target_id < target_id)
4969 cur_target = TAILQ_NEXT(cur_target, links);
4970
4971 if (cur_target != NULL) {
4972 TAILQ_INSERT_BEFORE(cur_target, target, links);
4973 } else {
4974 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4975 }
4976 bus->generation++;
4977 }
4978 return (target);
4979}
4980
4981static void
4982xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4983{
4984 int s;
4985
4986 s = splcam();
4987 if ((--target->refcount == 0)
4988 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4989 TAILQ_REMOVE(&bus->et_entries, target, links);
4990 bus->generation++;
4991 splx(s);
4992 free(target, M_CAMXPT);
4993 xpt_release_bus(bus);
4994 } else
4995 splx(s);
4996}
4997
4998static struct cam_ed *
4999xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
5000{
5001#ifdef CAM_NEW_TRAN_CODE
5002 struct cam_path path;
5003#endif /* CAM_NEW_TRAN_CODE */
5004 struct cam_ed *device;
5005 struct cam_devq *devq;
5006 cam_status status;
5007
5008 /* Make space for us in the device queue on our bus */
5009 devq = bus->sim->devq;
5010 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
5011
5012 if (status != CAM_REQ_CMP) {
5013 device = NULL;
5014 } else {
5015 device = (struct cam_ed *)malloc(sizeof(*device),
5016 M_CAMXPT, M_NOWAIT);
5017 }
5018
5019 if (device != NULL) {
5020 struct cam_ed *cur_device;
5021
5022 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
5023 device->alloc_ccb_entry.device = device;
5024 cam_init_pinfo(&device->send_ccb_entry.pinfo);
5025 device->send_ccb_entry.device = device;
5026 device->target = target;
5027 device->lun_id = lun_id;
5028 /* Initialize our queues */
5029 if (camq_init(&device->drvq, 0) != 0) {
5030 free(device, M_CAMXPT);
5031 return (NULL);
5032 }
5033 if (cam_ccbq_init(&device->ccbq,
5034 bus->sim->max_dev_openings) != 0) {
5035 camq_fini(&device->drvq);
5036 free(device, M_CAMXPT);
5037 return (NULL);
5038 }
5039 SLIST_INIT(&device->asyncs);
5040 SLIST_INIT(&device->periphs);
5041 device->generation = 0;
5042 device->owner = NULL;
5043 /*
5044 * Take the default quirk entry until we have inquiry
5045 * data and can determine a better quirk to use.
5046 */
5047 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
5048 bzero(&device->inq_data, sizeof(device->inq_data));
5049 device->inq_flags = 0;
5050 device->queue_flags = 0;
5051 device->serial_num = NULL;
5052 device->serial_num_len = 0;
5053 device->qfrozen_cnt = 0;
5054 device->flags = CAM_DEV_UNCONFIGURED;
5055 device->tag_delay_count = 0;
5056 device->tag_saved_openings = 0;
5057 device->refcount = 1;
5058 callout_handle_init(&device->c_handle);
5059
5060 /*
5061 * Hold a reference to our parent target so it
5062 * will not go away before we do.
5063 */
5064 target->refcount++;
5065
5066 /*
5067 * XXX should be limited by number of CCBs this bus can
5068 * do.
5069 */
5070 xpt_max_ccbs += device->ccbq.devq_openings;
5071 /* Insertion sort into our target's device list */
5072 cur_device = TAILQ_FIRST(&target->ed_entries);
5073 while (cur_device != NULL && cur_device->lun_id < lun_id)
5074 cur_device = TAILQ_NEXT(cur_device, links);
5075 if (cur_device != NULL) {
5076 TAILQ_INSERT_BEFORE(cur_device, device, links);
5077 } else {
5078 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
5079 }
5080 target->generation++;
5081#ifdef CAM_NEW_TRAN_CODE
5082 if (lun_id != CAM_LUN_WILDCARD) {
5083 xpt_compile_path(&path,
5084 NULL,
5085 bus->path_id,
5086 target->target_id,
5087 lun_id);
5088 xpt_devise_transport(&path);
5089 xpt_release_path(&path);
5090 }
5091#endif /* CAM_NEW_TRAN_CODE */
5092 }
5093 return (device);
5094}
5095
5096static void
5097xpt_release_device(struct cam_eb *bus, struct cam_et *target,
5098 struct cam_ed *device)
5099{
5100 int s;
5101
5102 s = splcam();
5103 if ((--device->refcount == 0)
5104 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
5105 struct cam_devq *devq;
5106
5107 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
5108 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
5109 panic("Removing device while still queued for ccbs");
5110
5111 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
5112 untimeout(xpt_release_devq_timeout, device,
5113 device->c_handle);
5114
5115 TAILQ_REMOVE(&target->ed_entries, device,links);
5116 target->generation++;
5117 xpt_max_ccbs -= device->ccbq.devq_openings;
5118 /* Release our slot in the devq */
5119 devq = bus->sim->devq;
5120 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
5121 splx(s);
5122 camq_fini(&device->drvq);
5123 camq_fini(&device->ccbq.queue);
5124 free(device, M_CAMXPT);
5125 xpt_release_target(bus, target);
5126 } else
5127 splx(s);
5128}
5129
5130static u_int32_t
5131xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5132{
5133 int s;
5134 int diff;
5135 int result;
5136 struct cam_ed *dev;
5137
5138 dev = path->device;
5139 s = splsoftcam();
5140
5141 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
5142 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5143 if (result == CAM_REQ_CMP && (diff < 0)) {
5144 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
5145 }
5146 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5147 || (dev->inq_flags & SID_CmdQue) != 0)
5148 dev->tag_saved_openings = newopenings;
5149 /* Adjust the global limit */
5150 xpt_max_ccbs += diff;
5151 splx(s);
5152 return (result);
5153}
5154
5155static struct cam_eb *
5156xpt_find_bus(path_id_t path_id)
5157{
5158 struct cam_eb *bus;
5159
5160 for (bus = TAILQ_FIRST(&xpt_busses);
5161 bus != NULL;
5162 bus = TAILQ_NEXT(bus, links)) {
5163 if (bus->path_id == path_id) {
5164 bus->refcount++;
5165 break;
5166 }
5167 }
5168 return (bus);
5169}
5170
5171static struct cam_et *
5172xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5173{
5174 struct cam_et *target;
5175
5176 for (target = TAILQ_FIRST(&bus->et_entries);
5177 target != NULL;
5178 target = TAILQ_NEXT(target, links)) {
5179 if (target->target_id == target_id) {
5180 target->refcount++;
5181 break;
5182 }
5183 }
5184 return (target);
5185}
5186
5187static struct cam_ed *
5188xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5189{
5190 struct cam_ed *device;
5191
5192 for (device = TAILQ_FIRST(&target->ed_entries);
5193 device != NULL;
5194 device = TAILQ_NEXT(device, links)) {
5195 if (device->lun_id == lun_id) {
5196 device->refcount++;
5197 break;
5198 }
5199 }
5200 return (device);
5201}
5202
5203typedef struct {
5204 union ccb *request_ccb;
5205 struct ccb_pathinq *cpi;
5206 int pending_count;
5207} xpt_scan_bus_info;
5208
5209/*
5210 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5211 * As the scan progresses, xpt_scan_bus is used as the
5212 * callback on completion function.
5213 */
5214static void
5215xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
5216{
5217 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5218 ("xpt_scan_bus\n"));
5219 switch (request_ccb->ccb_h.func_code) {
5220 case XPT_SCAN_BUS:
5221 {
5222 xpt_scan_bus_info *scan_info;
5223 union ccb *work_ccb;
5224 struct cam_path *path;
5225 u_int i;
5226 u_int max_target;
5227 u_int initiator_id;
5228
5229 /* Find out the characteristics of the bus */
5230 work_ccb = xpt_alloc_ccb();
5231 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
5232 request_ccb->ccb_h.pinfo.priority);
5233 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5234 xpt_action(work_ccb);
5235 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5236 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
5237 xpt_free_ccb(work_ccb);
5238 xpt_done(request_ccb);
5239 return;
5240 }
5241
5242 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5243 /*
5244 * Can't scan the bus on an adapter that
5245 * cannot perform the initiator role.
5246 */
5247 request_ccb->ccb_h.status = CAM_REQ_CMP;
5248 xpt_free_ccb(work_ccb);
5249 xpt_done(request_ccb);
5250 return;
5251 }
5252
5253 /* Save some state for use while we probe for devices */
5254 scan_info = (xpt_scan_bus_info *)
5255 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
5256 scan_info->request_ccb = request_ccb;
5257 scan_info->cpi = &work_ccb->cpi;
5258
5259 /* Cache on our stack so we can work asynchronously */
5260 max_target = scan_info->cpi->max_target;
5261 initiator_id = scan_info->cpi->initiator_id;
5262
5263 /*
5264 * Don't count the initiator if the
5265 * initiator is addressable.
5266 */
5267 scan_info->pending_count = max_target + 1;
5268 if (initiator_id <= max_target)
5269 scan_info->pending_count--;
5270
5271 for (i = 0; i <= max_target; i++) {
5272 cam_status status;
5273 if (i == initiator_id)
5274 continue;
5275
5276 status = xpt_create_path(&path, xpt_periph,
5277 request_ccb->ccb_h.path_id,
5278 i, 0);
5279 if (status != CAM_REQ_CMP) {
5280 printf("xpt_scan_bus: xpt_create_path failed"
5281 " with status %#x, bus scan halted\n",
5282 status);
5283 break;
5284 }
5285 work_ccb = xpt_alloc_ccb();
5286 xpt_setup_ccb(&work_ccb->ccb_h, path,
5287 request_ccb->ccb_h.pinfo.priority);
5288 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5289 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5290 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
5291 work_ccb->crcn.flags = request_ccb->crcn.flags;
5292 xpt_action(work_ccb);
5293 }
5294 break;
5295 }
5296 case XPT_SCAN_LUN:
5297 {
5298 xpt_scan_bus_info *scan_info;
5299 path_id_t path_id;
5300 target_id_t target_id;
5301 lun_id_t lun_id;
5302
5303 /* Reuse the same CCB to query if a device was really found */
5304 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
5305 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
5306 request_ccb->ccb_h.pinfo.priority);
5307 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5308
5309 path_id = request_ccb->ccb_h.path_id;
5310 target_id = request_ccb->ccb_h.target_id;
5311 lun_id = request_ccb->ccb_h.target_lun;
5312 xpt_action(request_ccb);
5313
5314 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5315 struct cam_ed *device;
5316 struct cam_et *target;
5317 int s, phl;
5318
5319 /*
5320 * If we already probed lun 0 successfully, or
5321 * we have additional configured luns on this
5322 * target that might have "gone away", go onto
5323 * the next lun.
5324 */
5325 target = request_ccb->ccb_h.path->target;
5326 /*
5327 * We may touch devices that we don't
5328 * hold references too, so ensure they
5329 * don't disappear out from under us.
5330 * The target above is referenced by the
5331 * path in the request ccb.
5332 */
5333 phl = 0;
5334 s = splcam();
5335 device = TAILQ_FIRST(&target->ed_entries);
5336 if (device != NULL) {
5337 phl = CAN_SRCH_HI(device);
5338 if (device->lun_id == 0)
5339 device = TAILQ_NEXT(device, links);
5340 }
5341 splx(s);
5342 if ((lun_id != 0) || (device != NULL)) {
5343 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5344 lun_id++;
5345 }
5346 } else {
5347 struct cam_ed *device;
5348
5349 device = request_ccb->ccb_h.path->device;
5350
5351 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5352 /* Try the next lun */
5353 if (lun_id < (CAM_SCSI2_MAXLUN-1)
5354 || CAN_SRCH_HI(device))
5355 lun_id++;
5356 }
5357 }
5358
5359 xpt_free_path(request_ccb->ccb_h.path);
5360
5361 /* Check Bounds */
5362 if ((lun_id == request_ccb->ccb_h.target_lun)
5363 || lun_id > scan_info->cpi->max_lun) {
5364 /* We're done */
5365
5366 xpt_free_ccb(request_ccb);
5367 scan_info->pending_count--;
5368 if (scan_info->pending_count == 0) {
5369 xpt_free_ccb((union ccb *)scan_info->cpi);
5370 request_ccb = scan_info->request_ccb;
5371 free(scan_info, M_TEMP);
5372 request_ccb->ccb_h.status = CAM_REQ_CMP;
5373 xpt_done(request_ccb);
5374 }
5375 } else {
5376 /* Try the next device */
5377 struct cam_path *path;
5378 cam_status status;
5379
5380 status = xpt_create_path(&path, xpt_periph,
5381 path_id, target_id, lun_id);
5382 if (status != CAM_REQ_CMP) {
5383 printf("xpt_scan_bus: xpt_create_path failed "
5384 "with status %#x, halting LUN scan\n",
5385 status);
5386 xpt_free_ccb(request_ccb);
5387 scan_info->pending_count--;
5388 if (scan_info->pending_count == 0) {
5389 xpt_free_ccb(
5390 (union ccb *)scan_info->cpi);
5391 request_ccb = scan_info->request_ccb;
5392 free(scan_info, M_TEMP);
5393 request_ccb->ccb_h.status = CAM_REQ_CMP;
5394 xpt_done(request_ccb);
5395 }
5396 break;
5397 }
5398 xpt_setup_ccb(&request_ccb->ccb_h, path,
5399 request_ccb->ccb_h.pinfo.priority);
5400 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5401 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5402 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5403 request_ccb->crcn.flags =
5404 scan_info->request_ccb->crcn.flags;
5405 xpt_action(request_ccb);
5406 }
5407 break;
5408 }
5409 default:
5410 break;
5411 }
5412}
5413
5414typedef enum {
5415 PROBE_TUR,
5416 PROBE_INQUIRY,
5417 PROBE_FULL_INQUIRY,
5418 PROBE_MODE_SENSE,
5419 PROBE_SERIAL_NUM,
5420 PROBE_TUR_FOR_NEGOTIATION
5421} probe_action;
5422
5423typedef enum {
5424 PROBE_INQUIRY_CKSUM = 0x01,
5425 PROBE_SERIAL_CKSUM = 0x02,
5426 PROBE_NO_ANNOUNCE = 0x04
5427} probe_flags;
5428
5429typedef struct {
5430 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5431 probe_action action;
5432 union ccb saved_ccb;
5433 probe_flags flags;
5434 MD5_CTX context;
5435 u_int8_t digest[16];
5436} probe_softc;
5437
5438static void
5439xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5440 cam_flags flags, union ccb *request_ccb)
5441{
5442 struct ccb_pathinq cpi;
5443 cam_status status;
5444 struct cam_path *new_path;
5445 struct cam_periph *old_periph;
5446 int s;
5447
5448 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5449 ("xpt_scan_lun\n"));
5450
5451 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5452 cpi.ccb_h.func_code = XPT_PATH_INQ;
5453 xpt_action((union ccb *)&cpi);
5454
5455 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5456 if (request_ccb != NULL) {
5457 request_ccb->ccb_h.status = cpi.ccb_h.status;
5458 xpt_done(request_ccb);
5459 }
5460 return;
5461 }
5462
5463 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5464 /*
5465 * Can't scan the bus on an adapter that
5466 * cannot perform the initiator role.
5467 */
5468 if (request_ccb != NULL) {
5469 request_ccb->ccb_h.status = CAM_REQ_CMP;
5470 xpt_done(request_ccb);
5471 }
5472 return;
5473 }
5474
5475 if (request_ccb == NULL) {
5476 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5477 if (request_ccb == NULL) {
5478 xpt_print_path(path);
5479 printf("xpt_scan_lun: can't allocate CCB, can't "
5480 "continue\n");
5481 return;
5482 }
5483 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5484 if (new_path == NULL) {
5485 xpt_print_path(path);
5486 printf("xpt_scan_lun: can't allocate path, can't "
5487 "continue\n");
5488 free(request_ccb, M_TEMP);
5489 return;
5490 }
5491 status = xpt_compile_path(new_path, xpt_periph,
5492 path->bus->path_id,
5493 path->target->target_id,
5494 path->device->lun_id);
5495
5496 if (status != CAM_REQ_CMP) {
5497 xpt_print_path(path);
5498 printf("xpt_scan_lun: can't compile path, can't "
5499 "continue\n");
5500 free(request_ccb, M_TEMP);
5501 free(new_path, M_TEMP);
5502 return;
5503 }
5504 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5505 request_ccb->ccb_h.cbfcnp = xptscandone;
5506 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5507 request_ccb->crcn.flags = flags;
5508 }
5509
5510 s = splsoftcam();
5511 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5512 probe_softc *softc;
5513
5514 softc = (probe_softc *)old_periph->softc;
5515 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5516 periph_links.tqe);
5517 } else {
5518 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5519 probestart, "probe",
5520 CAM_PERIPH_BIO,
5521 request_ccb->ccb_h.path, NULL, 0,
5522 request_ccb);
5523
5524 if (status != CAM_REQ_CMP) {
5525 xpt_print_path(path);
5526 printf("xpt_scan_lun: cam_alloc_periph returned an "
5527 "error, can't continue probe\n");
5528 request_ccb->ccb_h.status = status;
5529 xpt_done(request_ccb);
5530 }
5531 }
5532 splx(s);
5533}
5534
5535static void
5536xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5537{
5538 xpt_release_path(done_ccb->ccb_h.path);
5539 free(done_ccb->ccb_h.path, M_TEMP);
5540 free(done_ccb, M_TEMP);
5541}
5542
5543static cam_status
5544proberegister(struct cam_periph *periph, void *arg)
5545{
5546 union ccb *request_ccb; /* CCB representing the probe request */
5547 probe_softc *softc;
5548
5549 request_ccb = (union ccb *)arg;
5550 if (periph == NULL) {
5551 printf("proberegister: periph was NULL!!\n");
5552 return(CAM_REQ_CMP_ERR);
5553 }
5554
5555 if (request_ccb == NULL) {
5556 printf("proberegister: no probe CCB, "
5557 "can't register device\n");
5558 return(CAM_REQ_CMP_ERR);
5559 }
5560
5561 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5562
5563 if (softc == NULL) {
5564 printf("proberegister: Unable to probe new device. "
5565 "Unable to allocate softc\n");
5566 return(CAM_REQ_CMP_ERR);
5567 }
5568 TAILQ_INIT(&softc->request_ccbs);
5569 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5570 periph_links.tqe);
5571 softc->flags = 0;
5572 periph->softc = softc;
5573 cam_periph_acquire(periph);
5574 /*
5575 * Ensure we've waited at least a bus settle
5576 * delay before attempting to probe the device.
5577 * For HBAs that don't do bus resets, this won't make a difference.
5578 */
5579 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5580 scsi_delay);
5581 probeschedule(periph);
5582 return(CAM_REQ_CMP);
5583}
5584
5585static void
5586probeschedule(struct cam_periph *periph)
5587{
5588 struct ccb_pathinq cpi;
5589 union ccb *ccb;
5590 probe_softc *softc;
5591
5592 softc = (probe_softc *)periph->softc;
5593 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5594
5595 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5596 cpi.ccb_h.func_code = XPT_PATH_INQ;
5597 xpt_action((union ccb *)&cpi);
5598
5599 /*
5600 * If a device has gone away and another device, or the same one,
5601 * is back in the same place, it should have a unit attention
5602 * condition pending. It will not report the unit attention in
5603 * response to an inquiry, which may leave invalid transfer
5604 * negotiations in effect. The TUR will reveal the unit attention
5605 * condition. Only send the TUR for lun 0, since some devices
5606 * will get confused by commands other than inquiry to non-existent
5607 * luns. If you think a device has gone away start your scan from
5608 * lun 0. This will insure that any bogus transfer settings are
5609 * invalidated.
5610 *
5611 * If we haven't seen the device before and the controller supports
5612 * some kind of transfer negotiation, negotiate with the first
5613 * sent command if no bus reset was performed at startup. This
5614 * ensures that the device is not confused by transfer negotiation
5615 * settings left over by loader or BIOS action.
5616 */
5617 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5618 && (ccb->ccb_h.target_lun == 0)) {
5619 softc->action = PROBE_TUR;
5620 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5621 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5622 proberequestdefaultnegotiation(periph);
5623 softc->action = PROBE_INQUIRY;
5624 } else {
5625 softc->action = PROBE_INQUIRY;
5626 }
5627
5628 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5629 softc->flags |= PROBE_NO_ANNOUNCE;
5630 else
5631 softc->flags &= ~PROBE_NO_ANNOUNCE;
5632
5633 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5634}
5635
5636static void
5637probestart(struct cam_periph *periph, union ccb *start_ccb)
5638{
5639 /* Probe the device that our peripheral driver points to */
5640 struct ccb_scsiio *csio;
5641 probe_softc *softc;
5642
5643 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5644
5645 softc = (probe_softc *)periph->softc;
5646 csio = &start_ccb->csio;
5647
5648 switch (softc->action) {
5649 case PROBE_TUR:
5650 case PROBE_TUR_FOR_NEGOTIATION:
5651 {
5652 scsi_test_unit_ready(csio,
5653 /*retries*/4,
5654 probedone,
5655 MSG_SIMPLE_Q_TAG,
5656 SSD_FULL_SIZE,
5657 /*timeout*/60000);
5658 break;
5659 }
5660 case PROBE_INQUIRY:
5661 case PROBE_FULL_INQUIRY:
5662 {
5663 u_int inquiry_len;
5664 struct scsi_inquiry_data *inq_buf;
5665
5666 inq_buf = &periph->path->device->inq_data;
5667 /*
5668 * If the device is currently configured, we calculate an
5669 * MD5 checksum of the inquiry data, and if the serial number
5670 * length is greater than 0, add the serial number data
5671 * into the checksum as well. Once the inquiry and the
5672 * serial number check finish, we attempt to figure out
5673 * whether we still have the same device.
5674 */
5675 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5676
5677 MD5Init(&softc->context);
5678 MD5Update(&softc->context, (unsigned char *)inq_buf,
5679 sizeof(struct scsi_inquiry_data));
5680 softc->flags |= PROBE_INQUIRY_CKSUM;
5681 if (periph->path->device->serial_num_len > 0) {
5682 MD5Update(&softc->context,
5683 periph->path->device->serial_num,
5684 periph->path->device->serial_num_len);
5685 softc->flags |= PROBE_SERIAL_CKSUM;
5686 }
5687 MD5Final(softc->digest, &softc->context);
5688 }
5689
5690 if (softc->action == PROBE_INQUIRY)
5691 inquiry_len = SHORT_INQUIRY_LENGTH;
5692 else
5693 inquiry_len = inq_buf->additional_length
5694 + offsetof(struct scsi_inquiry_data,
5695 additional_length) + 1;
5696
5697 /*
5698 * Some parallel SCSI devices fail to send an
5699 * ignore wide residue message when dealing with
5700 * odd length inquiry requests. Round up to be
5701 * safe.
5702 */
5703 inquiry_len = roundup2(inquiry_len, 2);
5704
5705 scsi_inquiry(csio,
5706 /*retries*/4,
5707 probedone,
5708 MSG_SIMPLE_Q_TAG,
5709 (u_int8_t *)inq_buf,
5710 inquiry_len,
5711 /*evpd*/FALSE,
5712 /*page_code*/0,
5713 SSD_MIN_SIZE,
5714 /*timeout*/60 * 1000);
5715 break;
5716 }
5717 case PROBE_MODE_SENSE:
5718 {
5719 void *mode_buf;
5720 int mode_buf_len;
5721
5722 mode_buf_len = sizeof(struct scsi_mode_header_6)
5723 + sizeof(struct scsi_mode_blk_desc)
5724 + sizeof(struct scsi_control_page);
5725 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5726 if (mode_buf != NULL) {
5727 scsi_mode_sense(csio,
5728 /*retries*/4,
5729 probedone,
5730 MSG_SIMPLE_Q_TAG,
5731 /*dbd*/FALSE,
5732 SMS_PAGE_CTRL_CURRENT,
5733 SMS_CONTROL_MODE_PAGE,
5734 mode_buf,
5735 mode_buf_len,
5736 SSD_FULL_SIZE,
5737 /*timeout*/60000);
5738 break;
5739 }
5740 xpt_print_path(periph->path);
5741 printf("Unable to mode sense control page - malloc failure\n");
5742 softc->action = PROBE_SERIAL_NUM;
5743 }
5744 /* FALLTHROUGH */
5745 case PROBE_SERIAL_NUM:
5746 {
5747 struct scsi_vpd_unit_serial_number *serial_buf;
5748 struct cam_ed* device;
5749
5750 serial_buf = NULL;
5751 device = periph->path->device;
5752 device->serial_num = NULL;
5753 device->serial_num_len = 0;
5754
5755 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5756 serial_buf = (struct scsi_vpd_unit_serial_number *)
5757 malloc(sizeof(*serial_buf), M_TEMP,
5758 M_NOWAIT | M_ZERO);
5759
5760 if (serial_buf != NULL) {
5761 scsi_inquiry(csio,
5762 /*retries*/4,
5763 probedone,
5764 MSG_SIMPLE_Q_TAG,
5765 (u_int8_t *)serial_buf,
5766 sizeof(*serial_buf),
5767 /*evpd*/TRUE,
5768 SVPD_UNIT_SERIAL_NUMBER,
5769 SSD_MIN_SIZE,
5770 /*timeout*/60 * 1000);
5771 break;
5772 }
5773 /*
5774 * We'll have to do without, let our probedone
5775 * routine finish up for us.
5776 */
5777 start_ccb->csio.data_ptr = NULL;
5778 probedone(periph, start_ccb);
5779 return;
5780 }
5781 }
5782 xpt_action(start_ccb);
5783}
5784
5785static void
5786proberequestdefaultnegotiation(struct cam_periph *periph)
5787{
5788 struct ccb_trans_settings cts;
5789
5790 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5791 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5792#ifdef CAM_NEW_TRAN_CODE
5793 cts.type = CTS_TYPE_USER_SETTINGS;
5794#else /* CAM_NEW_TRAN_CODE */
5795 cts.flags = CCB_TRANS_USER_SETTINGS;
5796#endif /* CAM_NEW_TRAN_CODE */
5797 xpt_action((union ccb *)&cts);
5798 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5799#ifdef CAM_NEW_TRAN_CODE
5800 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5801#else /* CAM_NEW_TRAN_CODE */
5802 cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5803 cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5804#endif /* CAM_NEW_TRAN_CODE */
5805 xpt_action((union ccb *)&cts);
5806}
5807
5808static void
5809probedone(struct cam_periph *periph, union ccb *done_ccb)
5810{
5811 probe_softc *softc;
5812 struct cam_path *path;
5813 u_int32_t priority;
5814
5815 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5816
5817 softc = (probe_softc *)periph->softc;
5818 path = done_ccb->ccb_h.path;
5819 priority = done_ccb->ccb_h.pinfo.priority;
5820
5821 switch (softc->action) {
5822 case PROBE_TUR:
5823 {
5824 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5825
5826 if (cam_periph_error(done_ccb, 0,
5827 SF_NO_PRINT, NULL) == ERESTART)
5828 return;
5829 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5830 /* Don't wedge the queue */
5831 xpt_release_devq(done_ccb->ccb_h.path,
5832 /*count*/1,
5833 /*run_queue*/TRUE);
5834 }
5835 softc->action = PROBE_INQUIRY;
5836 xpt_release_ccb(done_ccb);
5837 xpt_schedule(periph, priority);
5838 return;
5839 }
5840 case PROBE_INQUIRY:
5841 case PROBE_FULL_INQUIRY:
5842 {
5843 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5844 struct scsi_inquiry_data *inq_buf;
5845 u_int8_t periph_qual;
5846
5847 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5848 inq_buf = &path->device->inq_data;
5849
5850 periph_qual = SID_QUAL(inq_buf);
5851
5852 switch(periph_qual) {
5853 case SID_QUAL_LU_CONNECTED:
5854 {
5855 u_int8_t len;
5856
5857 /*
5858 * We conservatively request only
5859 * SHORT_INQUIRY_LEN bytes of inquiry
5860 * information during our first try
5861 * at sending an INQUIRY. If the device
5862 * has more information to give,
5863 * perform a second request specifying
5864 * the amount of information the device
5865 * is willing to give.
5866 */
5867 len = inq_buf->additional_length
5868 + offsetof(struct scsi_inquiry_data,
5869 additional_length) + 1;
5870 if (softc->action == PROBE_INQUIRY
5871 && len > SHORT_INQUIRY_LENGTH) {
5872 softc->action = PROBE_FULL_INQUIRY;
5873 xpt_release_ccb(done_ccb);
5874 xpt_schedule(periph, priority);
5875 return;
5876 }
5877
5878 xpt_find_quirk(path->device);
5879
5880#ifdef CAM_NEW_TRAN_CODE
5881 xpt_devise_transport(path);
5882#endif /* CAM_NEW_TRAN_CODE */
5883 if ((inq_buf->flags & SID_CmdQue) != 0)
5884 softc->action = PROBE_MODE_SENSE;
5885 else
5886 softc->action = PROBE_SERIAL_NUM;
5887
5888 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5889
5890 xpt_release_ccb(done_ccb);
5891 xpt_schedule(periph, priority);
5892 return;
5893 }
5894 default:
5895 break;
5896 }
5897 } else if (cam_periph_error(done_ccb, 0,
5898 done_ccb->ccb_h.target_lun > 0
5899 ? SF_RETRY_UA|SF_QUIET_IR
5900 : SF_RETRY_UA,
5901 &softc->saved_ccb) == ERESTART) {
5902 return;
5903 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5904 /* Don't wedge the queue */
5905 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5906 /*run_queue*/TRUE);
5907 }
5908 /*
5909 * If we get to this point, we got an error status back
5910 * from the inquiry and the error status doesn't require
5911 * automatically retrying the command. Therefore, the
5912 * inquiry failed. If we had inquiry information before
5913 * for this device, but this latest inquiry command failed,
5914 * the device has probably gone away. If this device isn't
5915 * already marked unconfigured, notify the peripheral
5916 * drivers that this device is no more.
5917 */
5918 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5919 /* Send the async notification. */
5920 xpt_async(AC_LOST_DEVICE, path, NULL);
5921
5922 xpt_release_ccb(done_ccb);
5923 break;
5924 }
5925 case PROBE_MODE_SENSE:
5926 {
5927 struct ccb_scsiio *csio;
5928 struct scsi_mode_header_6 *mode_hdr;
5929
5930 csio = &done_ccb->csio;
5931 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5932 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5933 struct scsi_control_page *page;
5934 u_int8_t *offset;
5935
5936 offset = ((u_int8_t *)&mode_hdr[1])
5937 + mode_hdr->blk_desc_len;
5938 page = (struct scsi_control_page *)offset;
5939 path->device->queue_flags = page->queue_flags;
5940 } else if (cam_periph_error(done_ccb, 0,
5941 SF_RETRY_UA|SF_NO_PRINT,
5942 &softc->saved_ccb) == ERESTART) {
5943 return;
5944 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5945 /* Don't wedge the queue */
5946 xpt_release_devq(done_ccb->ccb_h.path,
5947 /*count*/1, /*run_queue*/TRUE);
5948 }
5949 xpt_release_ccb(done_ccb);
5950 free(mode_hdr, M_TEMP);
5951 softc->action = PROBE_SERIAL_NUM;
5952 xpt_schedule(periph, priority);
5953 return;
5954 }
5955 case PROBE_SERIAL_NUM:
5956 {
5957 struct ccb_scsiio *csio;
5958 struct scsi_vpd_unit_serial_number *serial_buf;
5959 u_int32_t priority;
5960 int changed;
5961 int have_serialnum;
5962
5963 changed = 1;
5964 have_serialnum = 0;
5965 csio = &done_ccb->csio;
5966 priority = done_ccb->ccb_h.pinfo.priority;
5967 serial_buf =
5968 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5969
5970 /* Clean up from previous instance of this device */
5971 if (path->device->serial_num != NULL) {
5972 free(path->device->serial_num, M_CAMXPT);
5973 path->device->serial_num = NULL;
5974 path->device->serial_num_len = 0;
5975 }
5976
5977 if (serial_buf == NULL) {
5978 /*
5979 * Don't process the command as it was never sent
5980 */
5981 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5982 && (serial_buf->length > 0)) {
5983
5984 have_serialnum = 1;
5985 path->device->serial_num =
5986 (u_int8_t *)malloc((serial_buf->length + 1),
5987 M_CAMXPT, M_NOWAIT);
5988 if (path->device->serial_num != NULL) {
5989 bcopy(serial_buf->serial_num,
5990 path->device->serial_num,
5991 serial_buf->length);
5992 path->device->serial_num_len =
5993 serial_buf->length;
5994 path->device->serial_num[serial_buf->length]
5995 = '\0';
5996 }
5997 } else if (cam_periph_error(done_ccb, 0,
5998 SF_RETRY_UA|SF_NO_PRINT,
5999 &softc->saved_ccb) == ERESTART) {
6000 return;
6001 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6002 /* Don't wedge the queue */
6003 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6004 /*run_queue*/TRUE);
6005 }
6006
6007 /*
6008 * Let's see if we have seen this device before.
6009 */
6010 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
6011 MD5_CTX context;
6012 u_int8_t digest[16];
6013
6014 MD5Init(&context);
6015
6016 MD5Update(&context,
6017 (unsigned char *)&path->device->inq_data,
6018 sizeof(struct scsi_inquiry_data));
6019
6020 if (have_serialnum)
6021 MD5Update(&context, serial_buf->serial_num,
6022 serial_buf->length);
6023
6024 MD5Final(digest, &context);
6025 if (bcmp(softc->digest, digest, 16) == 0)
6026 changed = 0;
6027
6028 /*
6029 * XXX Do we need to do a TUR in order to ensure
6030 * that the device really hasn't changed???
6031 */
6032 if ((changed != 0)
6033 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
6034 xpt_async(AC_LOST_DEVICE, path, NULL);
6035 }
6036 if (serial_buf != NULL)
6037 free(serial_buf, M_TEMP);
6038
6039 if (changed != 0) {
6040 /*
6041 * Now that we have all the necessary
6042 * information to safely perform transfer
6043 * negotiations... Controllers don't perform
6044 * any negotiation or tagged queuing until
6045 * after the first XPT_SET_TRAN_SETTINGS ccb is
6046 * received. So, on a new device, just retreive
6047 * the user settings, and set them as the current
6048 * settings to set the device up.
6049 */
6050 proberequestdefaultnegotiation(periph);
6051 xpt_release_ccb(done_ccb);
6052
6053 /*
6054 * Perform a TUR to allow the controller to
6055 * perform any necessary transfer negotiation.
6056 */
6057 softc->action = PROBE_TUR_FOR_NEGOTIATION;
6058 xpt_schedule(periph, priority);
6059 return;
6060 }
6061 xpt_release_ccb(done_ccb);
6062 break;
6063 }
6064 case PROBE_TUR_FOR_NEGOTIATION:
6065 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6066 /* Don't wedge the queue */
6067 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6068 /*run_queue*/TRUE);
6069 }
6070
6071 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
6072
6073 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
6074 /* Inform the XPT that a new device has been found */
6075 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
6076 xpt_action(done_ccb);
6077
6078 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6079 done_ccb);
6080 }
6081 xpt_release_ccb(done_ccb);
6082 break;
6083 }
6084 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
6085 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
6086 done_ccb->ccb_h.status = CAM_REQ_CMP;
6087 xpt_done(done_ccb);
6088 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
6089 cam_periph_invalidate(periph);
6090 cam_periph_release(periph);
6091 } else {
6092 probeschedule(periph);
6093 }
6094}
6095
6096static void
6097probecleanup(struct cam_periph *periph)
6098{
6099 free(periph->softc, M_TEMP);
6100}
6101
6102static void
6103xpt_find_quirk(struct cam_ed *device)
6104{
6105 caddr_t match;
6106
6107 match = cam_quirkmatch((caddr_t)&device->inq_data,
6108 (caddr_t)xpt_quirk_table,
6109 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
6110 sizeof(*xpt_quirk_table), scsi_inquiry_match);
6111
6112 if (match == NULL)
6113 panic("xpt_find_quirk: device didn't match wildcard entry!!");
6114
6115 device->quirk = (struct xpt_quirk_entry *)match;
6116}
6117
6118#ifdef CAM_NEW_TRAN_CODE
6119
6120static void
6121xpt_devise_transport(struct cam_path *path)
6122{
6123 struct ccb_pathinq cpi;
6124 struct ccb_trans_settings cts;
6125 struct scsi_inquiry_data *inq_buf;
6126
6127 /* Get transport information from the SIM */
6128 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
6129 cpi.ccb_h.func_code = XPT_PATH_INQ;
6130 xpt_action((union ccb *)&cpi);
6131
6132 inq_buf = NULL;
6133 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
6134 inq_buf = &path->device->inq_data;
6135 path->device->protocol = PROTO_SCSI;
6136 path->device->protocol_version =
6137 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
6138 path->device->transport = cpi.transport;
6139 path->device->transport_version = cpi.transport_version;
6140
6141 /*
6142 * Any device not using SPI3 features should
6143 * be considered SPI2 or lower.
6144 */
6145 if (inq_buf != NULL) {
6146 if (path->device->transport == XPORT_SPI
6147 && (inq_buf->spi3data & SID_SPI_MASK) == 0
6148 && path->device->transport_version > 2)
6149 path->device->transport_version = 2;
6150 } else {
6151 struct cam_ed* otherdev;
6152
6153 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6154 otherdev != NULL;
6155 otherdev = TAILQ_NEXT(otherdev, links)) {
6156 if (otherdev != path->device)
6157 break;
6158 }
6159
6160 if (otherdev != NULL) {
6161 /*
6162 * Initially assume the same versioning as
6163 * prior luns for this target.
6164 */
6165 path->device->protocol_version =
6166 otherdev->protocol_version;
6167 path->device->transport_version =
6168 otherdev->transport_version;
6169 } else {
6170 /* Until we know better, opt for safty */
6171 path->device->protocol_version = 2;
6172 if (path->device->transport == XPORT_SPI)
6173 path->device->transport_version = 2;
6174 else
6175 path->device->transport_version = 0;
6176 }
6177 }
6178
6179 /*
6180 * XXX
6181 * For a device compliant with SPC-2 we should be able
6182 * to determine the transport version supported by
6183 * scrutinizing the version descriptors in the
6184 * inquiry buffer.
6185 */
6186
6187 /* Tell the controller what we think */
6188 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
6189 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6190 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6191 cts.transport = path->device->transport;
6192 cts.transport_version = path->device->transport_version;
6193 cts.protocol = path->device->protocol;
6194 cts.protocol_version = path->device->protocol_version;
6195 cts.proto_specific.valid = 0;
6196 cts.xport_specific.valid = 0;
6197 xpt_action((union ccb *)&cts);
6198}
6199
6200static void
6201xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6202 int async_update)
6203{
6204 struct ccb_pathinq cpi;
6205 struct ccb_trans_settings cur_cts;
6206 struct ccb_trans_settings_scsi *scsi;
6207 struct ccb_trans_settings_scsi *cur_scsi;
6208 struct cam_sim *sim;
6209 struct scsi_inquiry_data *inq_data;
6210
6211 if (device == NULL) {
6212 cts->ccb_h.status = CAM_PATH_INVALID;
6213 xpt_done((union ccb *)cts);
6214 return;
6215 }
6216
6217 if (cts->protocol == PROTO_UNKNOWN
6218 || cts->protocol == PROTO_UNSPECIFIED) {
6219 cts->protocol = device->protocol;
6220 cts->protocol_version = device->protocol_version;
6221 }
6222
6223 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
6224 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
6225 cts->protocol_version = device->protocol_version;
6226
6227 if (cts->protocol != device->protocol) {
6228 xpt_print_path(cts->ccb_h.path);
6229 printf("Uninitialized Protocol %x:%x?\n",
6230 cts->protocol, device->protocol);
6231 cts->protocol = device->protocol;
6232 }
6233
6234 if (cts->protocol_version > device->protocol_version) {
6235 if (bootverbose) {
6236 xpt_print_path(cts->ccb_h.path);
6237 printf("Down reving Protocol Version from %d to %d?\n",
6238 cts->protocol_version, device->protocol_version);
6239 }
6240 cts->protocol_version = device->protocol_version;
6241 }
6242
6243 if (cts->transport == XPORT_UNKNOWN
6244 || cts->transport == XPORT_UNSPECIFIED) {
6245 cts->transport = device->transport;
6246 cts->transport_version = device->transport_version;
6247 }
6248
6249 if (cts->transport_version == XPORT_VERSION_UNKNOWN
6250 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
6251 cts->transport_version = device->transport_version;
6252
6253 if (cts->transport != device->transport) {
6254 xpt_print_path(cts->ccb_h.path);
6255 printf("Uninitialized Transport %x:%x?\n",
6256 cts->transport, device->transport);
6257 cts->transport = device->transport;
6258 }
6259
6260 if (cts->transport_version > device->transport_version) {
6261 if (bootverbose) {
6262 xpt_print_path(cts->ccb_h.path);
6263 printf("Down reving Transport Version from %d to %d?\n",
6264 cts->transport_version,
6265 device->transport_version);
6266 }
6267 cts->transport_version = device->transport_version;
6268 }
6269
6270 sim = cts->ccb_h.path->bus->sim;
6271
6272 /*
6273 * Nothing more of interest to do unless
6274 * this is a device connected via the
6275 * SCSI protocol.
6276 */
6277 if (cts->protocol != PROTO_SCSI) {
6278 if (async_update == FALSE)
6279 (*(sim->sim_action))(sim, (union ccb *)cts);
6280 return;
6281 }
6282
6283 inq_data = &device->inq_data;
6284 scsi = &cts->proto_specific.scsi;
6285 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6286 cpi.ccb_h.func_code = XPT_PATH_INQ;
6287 xpt_action((union ccb *)&cpi);
6288
6289 /* SCSI specific sanity checking */
6290 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6291 || (inq_data->flags & SID_CmdQue) == 0
6292 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6293 || (device->quirk->mintags == 0)) {
6294 /*
6295 * Can't tag on hardware that doesn't support tags,
6296 * doesn't have it enabled, or has broken tag support.
6297 */
6298 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6299 }
6300
6301 if (async_update == FALSE) {
6302 /*
6303 * Perform sanity checking against what the
6304 * controller and device can do.
6305 */
6306 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6307 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6308 cur_cts.type = cts->type;
6309 xpt_action((union ccb *)&cur_cts);
6310
6311 cur_scsi = &cur_cts.proto_specific.scsi;
6312 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
6313 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6314 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
6315 }
6316 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
6317 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6318 }
6319
6320 /* SPI specific sanity checking */
6321 if (cts->transport == XPORT_SPI && async_update == FALSE) {
6322 u_int spi3caps;
6323 struct ccb_trans_settings_spi *spi;
6324 struct ccb_trans_settings_spi *cur_spi;
6325
6326 spi = &cts->xport_specific.spi;
6327
6328 cur_spi = &cur_cts.xport_specific.spi;
6329
6330 /* Fill in any gaps in what the user gave us */
6331 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6332 spi->sync_period = cur_spi->sync_period;
6333 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6334 spi->sync_period = 0;
6335 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6336 spi->sync_offset = cur_spi->sync_offset;
6337 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6338 spi->sync_offset = 0;
6339 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6340 spi->ppr_options = cur_spi->ppr_options;
6341 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6342 spi->ppr_options = 0;
6343 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6344 spi->bus_width = cur_spi->bus_width;
6345 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6346 spi->bus_width = 0;
6347 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
6348 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6349 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
6350 }
6351 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
6352 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6353 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6354 && (inq_data->flags & SID_Sync) == 0
6355 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6356 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6357 || (cur_spi->sync_offset == 0)
6358 || (cur_spi->sync_period == 0)) {
6359 /* Force async */
6360 spi->sync_period = 0;
6361 spi->sync_offset = 0;
6362 }
6363
6364 switch (spi->bus_width) {
6365 case MSG_EXT_WDTR_BUS_32_BIT:
6366 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6367 || (inq_data->flags & SID_WBus32) != 0
6368 || cts->type == CTS_TYPE_USER_SETTINGS)
6369 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6370 break;
6371 /* Fall Through to 16-bit */
6372 case MSG_EXT_WDTR_BUS_16_BIT:
6373 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6374 || (inq_data->flags & SID_WBus16) != 0
6375 || cts->type == CTS_TYPE_USER_SETTINGS)
6376 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6377 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6378 break;
6379 }
6380 /* Fall Through to 8-bit */
6381 default: /* New bus width?? */
6382 case MSG_EXT_WDTR_BUS_8_BIT:
6383 /* All targets can do this */
6384 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6385 break;
6386 }
6387
6388 spi3caps = cpi.xport_specific.spi.ppr_options;
6389 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6390 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6391 spi3caps &= inq_data->spi3data;
6392
6393 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
6394 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
6395
6396 if ((spi3caps & SID_SPI_IUS) == 0)
6397 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
6398
6399 if ((spi3caps & SID_SPI_QAS) == 0)
6400 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
6401
6402 /* No SPI Transfer settings are allowed unless we are wide */
6403 if (spi->bus_width == 0)
6404 spi->ppr_options = 0;
6405
6406 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
6407 /*
6408 * Can't tag queue without disconnection.
6409 */
6410 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6411 scsi->valid |= CTS_SCSI_VALID_TQ;
6412 }
6413
6414 /*
6415 * If we are currently performing tagged transactions to
6416 * this device and want to change its negotiation parameters,
6417 * go non-tagged for a bit to give the controller a chance to
6418 * negotiate unhampered by tag messages.
6419 */
6420 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6421 && (device->inq_flags & SID_CmdQue) != 0
6422 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6423 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
6424 CTS_SPI_VALID_SYNC_OFFSET|
6425 CTS_SPI_VALID_BUS_WIDTH)) != 0)
6426 xpt_toggle_tags(cts->ccb_h.path);
6427 }
6428
6429 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6430 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
6431 int device_tagenb;
6432
6433 /*
6434 * If we are transitioning from tags to no-tags or
6435 * vice-versa, we need to carefully freeze and restart
6436 * the queue so that we don't overlap tagged and non-tagged
6437 * commands. We also temporarily stop tags if there is
6438 * a change in transfer negotiation settings to allow
6439 * "tag-less" negotiation.
6440 */
6441 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6442 || (device->inq_flags & SID_CmdQue) != 0)
6443 device_tagenb = TRUE;
6444 else
6445 device_tagenb = FALSE;
6446
6447 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6448 && device_tagenb == FALSE)
6449 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
6450 && device_tagenb == TRUE)) {
6451
6452 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
6453 /*
6454 * Delay change to use tags until after a
6455 * few commands have gone to this device so
6456 * the controller has time to perform transfer
6457 * negotiations without tagged messages getting
6458 * in the way.
6459 */
6460 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6461 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6462 } else {
6463 struct ccb_relsim crs;
6464
6465 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6466 device->inq_flags &= ~SID_CmdQue;
6467 xpt_dev_ccbq_resize(cts->ccb_h.path,
6468 sim->max_dev_openings);
6469 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6470 device->tag_delay_count = 0;
6471
6472 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6473 /*priority*/1);
6474 crs.ccb_h.func_code = XPT_REL_SIMQ;
6475 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6476 crs.openings
6477 = crs.release_timeout
6478 = crs.qfrozen_cnt
6479 = 0;
6480 xpt_action((union ccb *)&crs);
6481 }
6482 }
6483 }
6484 if (async_update == FALSE)
6485 (*(sim->sim_action))(sim, (union ccb *)cts);
6486}
6487
6488#else /* CAM_NEW_TRAN_CODE */
6489
6490static void
6491xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6492 int async_update)
6493{
6494 struct cam_sim *sim;
6495 int qfrozen;
6496
6497 sim = cts->ccb_h.path->bus->sim;
6498 if (async_update == FALSE) {
6499 struct scsi_inquiry_data *inq_data;
6500 struct ccb_pathinq cpi;
6501 struct ccb_trans_settings cur_cts;
6502
6503 if (device == NULL) {
6504 cts->ccb_h.status = CAM_PATH_INVALID;
6505 xpt_done((union ccb *)cts);
6506 return;
6507 }
6508
6509 /*
6510 * Perform sanity checking against what the
6511 * controller and device can do.
6512 */
6513 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6514 cpi.ccb_h.func_code = XPT_PATH_INQ;
6515 xpt_action((union ccb *)&cpi);
6516 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6517 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6518 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
6519 xpt_action((union ccb *)&cur_cts);
6520 inq_data = &device->inq_data;
6521
6522 /* Fill in any gaps in what the user gave us */
6523 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
6524 cts->sync_period = cur_cts.sync_period;
6525 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
6526 cts->sync_offset = cur_cts.sync_offset;
6527 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
6528 cts->bus_width = cur_cts.bus_width;
6529 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
6530 cts->flags &= ~CCB_TRANS_DISC_ENB;
6531 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
6532 }
6533 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
6534 cts->flags &= ~CCB_TRANS_TAG_ENB;
6535 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
6536 }
6537
6538 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6539 && (inq_data->flags & SID_Sync) == 0)
6540 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6541 || (cts->sync_offset == 0)
6542 || (cts->sync_period == 0)) {
6543 /* Force async */
6544 cts->sync_period = 0;
6545 cts->sync_offset = 0;
6546 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6547 && (inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
6548 && cts->sync_period <= 0x9) {
6549 /*
6550 * Don't allow DT transmission rates if the
6551 * device does not support it.
6552 */
6553 cts->sync_period = 0xa;
6554 }
6555
6556 switch (cts->bus_width) {
6557 case MSG_EXT_WDTR_BUS_32_BIT:
6558 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6559 || (inq_data->flags & SID_WBus32) != 0)
6560 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6561 break;
6562 /* FALLTHROUGH to 16-bit */
6563 case MSG_EXT_WDTR_BUS_16_BIT:
6564 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6565 || (inq_data->flags & SID_WBus16) != 0)
6566 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6567 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6568 break;
6569 }
6570 /* FALLTHROUGH to 8-bit */
6571 default: /* New bus width?? */
6572 case MSG_EXT_WDTR_BUS_8_BIT:
6573 /* All targets can do this */
6574 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6575 break;
6576 }
6577
6578 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
6579 /*
6580 * Can't tag queue without disconnection.
6581 */
6582 cts->flags &= ~CCB_TRANS_TAG_ENB;
6583 cts->valid |= CCB_TRANS_TQ_VALID;
6584 }
6585
6586 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6587 || (inq_data->flags & SID_CmdQue) == 0
6588 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6589 || (device->quirk->mintags == 0)) {
6590 /*
6591 * Can't tag on hardware that doesn't support,
6592 * doesn't have it enabled, or has broken tag support.
6593 */
6594 cts->flags &= ~CCB_TRANS_TAG_ENB;
6595 }
6596 }
6597
6598 qfrozen = FALSE;
6599 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
6600 int device_tagenb;
6601
6602 /*
6603 * If we are transitioning from tags to no-tags or
6604 * vice-versa, we need to carefully freeze and restart
6605 * the queue so that we don't overlap tagged and non-tagged
6606 * commands. We also temporarily stop tags if there is
6607 * a change in transfer negotiation settings to allow
6608 * "tag-less" negotiation.
6609 */
6610 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6611 || (device->inq_flags & SID_CmdQue) != 0)
6612 device_tagenb = TRUE;
6613 else
6614 device_tagenb = FALSE;
6615
6616 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
6617 && device_tagenb == FALSE)
6618 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
6619 && device_tagenb == TRUE)) {
6620
6621 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
6622 /*
6623 * Delay change to use tags until after a
6624 * few commands have gone to this device so
6625 * the controller has time to perform transfer
6626 * negotiations without tagged messages getting
6627 * in the way.
6628 */
6629 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6630 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6631 } else {
6632 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6633 qfrozen = TRUE;
6634 device->inq_flags &= ~SID_CmdQue;
6635 xpt_dev_ccbq_resize(cts->ccb_h.path,
6636 sim->max_dev_openings);
6637 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6638 device->tag_delay_count = 0;
6639 }
6640 }
6641 }
6642
6643 if (async_update == FALSE) {
6644 /*
6645 * If we are currently performing tagged transactions to
6646 * this device and want to change its negotiation parameters,
6647 * go non-tagged for a bit to give the controller a chance to
6648 * negotiate unhampered by tag messages.
6649 */
6650 if ((device->inq_flags & SID_CmdQue) != 0
6651 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
6652 CCB_TRANS_SYNC_OFFSET_VALID|
6653 CCB_TRANS_BUS_WIDTH_VALID)) != 0)
6654 xpt_toggle_tags(cts->ccb_h.path);
6655
6656 (*(sim->sim_action))(sim, (union ccb *)cts);
6657 }
6658
6659 if (qfrozen) {
6660 struct ccb_relsim crs;
6661
6662 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6663 /*priority*/1);
6664 crs.ccb_h.func_code = XPT_REL_SIMQ;
6665 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6666 crs.openings
6667 = crs.release_timeout
6668 = crs.qfrozen_cnt
6669 = 0;
6670 xpt_action((union ccb *)&crs);
6671 }
6672}
6673
6674
6675#endif /* CAM_NEW_TRAN_CODE */
6676
6677static void
6678xpt_toggle_tags(struct cam_path *path)
6679{
6680 struct cam_ed *dev;
6681
6682 /*
6683 * Give controllers a chance to renegotiate
6684 * before starting tag operations. We
6685 * "toggle" tagged queuing off then on
6686 * which causes the tag enable command delay
6687 * counter to come into effect.
6688 */
6689 dev = path->device;
6690 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6691 || ((dev->inq_flags & SID_CmdQue) != 0
6692 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6693 struct ccb_trans_settings cts;
6694
6695 xpt_setup_ccb(&cts.ccb_h, path, 1);
6696#ifdef CAM_NEW_TRAN_CODE
6697 cts.protocol = PROTO_SCSI;
6698 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
6699 cts.transport = XPORT_UNSPECIFIED;
6700 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
6701 cts.proto_specific.scsi.flags = 0;
6702 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
6703#else /* CAM_NEW_TRAN_CODE */
6704 cts.flags = 0;
6705 cts.valid = CCB_TRANS_TQ_VALID;
6706#endif /* CAM_NEW_TRAN_CODE */
6707 xpt_set_transfer_settings(&cts, path->device,
6708 /*async_update*/TRUE);
6709#ifdef CAM_NEW_TRAN_CODE
6710 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
6711#else /* CAM_NEW_TRAN_CODE */
6712 cts.flags = CCB_TRANS_TAG_ENB;
6713#endif /* CAM_NEW_TRAN_CODE */
6714 xpt_set_transfer_settings(&cts, path->device,
6715 /*async_update*/TRUE);
6716 }
6717}
6718
6719static void
6720xpt_start_tags(struct cam_path *path)
6721{
6722 struct ccb_relsim crs;
6723 struct cam_ed *device;
6724 struct cam_sim *sim;
6725 int newopenings;
6726
6727 device = path->device;
6728 sim = path->bus->sim;
6729 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6730 xpt_freeze_devq(path, /*count*/1);
6731 device->inq_flags |= SID_CmdQue;
6732 if (device->tag_saved_openings != 0)
6733 newopenings = device->tag_saved_openings;
6734 else
6735 newopenings = min(device->quirk->maxtags,
6736 sim->max_tagged_dev_openings);
6737 xpt_dev_ccbq_resize(path, newopenings);
6738 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6739 crs.ccb_h.func_code = XPT_REL_SIMQ;
6740 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6741 crs.openings
6742 = crs.release_timeout
6743 = crs.qfrozen_cnt
6744 = 0;
6745 xpt_action((union ccb *)&crs);
6746}
6747
6748static int busses_to_config;
6749static int busses_to_reset;
6750
6751static int
6752xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6753{
6754 if (bus->path_id != CAM_XPT_PATH_ID) {
6755 struct cam_path path;
6756 struct ccb_pathinq cpi;
6757 int can_negotiate;
6758
6759 busses_to_config++;
6760 xpt_compile_path(&path, NULL, bus->path_id,
6761 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6762 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6763 cpi.ccb_h.func_code = XPT_PATH_INQ;
6764 xpt_action((union ccb *)&cpi);
6765 can_negotiate = cpi.hba_inquiry;
6766 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6767 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6768 && can_negotiate)
6769 busses_to_reset++;
6770 xpt_release_path(&path);
6771 }
6772
6773 return(1);
6774}
6775
6776static int
6777xptconfigfunc(struct cam_eb *bus, void *arg)
6778{
6779 struct cam_path *path;
6780 union ccb *work_ccb;
6781
6782 if (bus->path_id != CAM_XPT_PATH_ID) {
6783 cam_status status;
6784 int can_negotiate;
6785
6786 work_ccb = xpt_alloc_ccb();
6787 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6788 CAM_TARGET_WILDCARD,
6789 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6790 printf("xptconfigfunc: xpt_create_path failed with "
6791 "status %#x for bus %d\n", status, bus->path_id);
6792 printf("xptconfigfunc: halting bus configuration\n");
6793 xpt_free_ccb(work_ccb);
6794 busses_to_config--;
6795 xpt_finishconfig(xpt_periph, NULL);
6796 return(0);
6797 }
6798 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6799 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6800 xpt_action(work_ccb);
6801 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6802 printf("xptconfigfunc: CPI failed on bus %d "
6803 "with status %d\n", bus->path_id,
6804 work_ccb->ccb_h.status);
6805 xpt_finishconfig(xpt_periph, work_ccb);
6806 return(1);
6807 }
6808
6809 can_negotiate = work_ccb->cpi.hba_inquiry;
6810 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6811 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6812 && (can_negotiate != 0)) {
6813 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6814 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6815 work_ccb->ccb_h.cbfcnp = NULL;
6816 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6817 ("Resetting Bus\n"));
6818 xpt_action(work_ccb);
6819 xpt_finishconfig(xpt_periph, work_ccb);
6820 } else {
6821 /* Act as though we performed a successful BUS RESET */
6822 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6823 xpt_finishconfig(xpt_periph, work_ccb);
6824 }
6825 }
6826
6827 return(1);
6828}
6829
6830static void
6831xpt_config(void *arg)
6832{
6833 /*
6834 * Now that interrupts are enabled, go find our devices
6835 */
6836
6837#ifdef CAMDEBUG
6838 /* Setup debugging flags and path */
6839#ifdef CAM_DEBUG_FLAGS
6840 cam_dflags = CAM_DEBUG_FLAGS;
6841#else /* !CAM_DEBUG_FLAGS */
6842 cam_dflags = CAM_DEBUG_NONE;
6843#endif /* CAM_DEBUG_FLAGS */
6844#ifdef CAM_DEBUG_BUS
6845 if (cam_dflags != CAM_DEBUG_NONE) {
6846 if (xpt_create_path(&cam_dpath, xpt_periph,
6847 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6848 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6849 printf("xpt_config: xpt_create_path() failed for debug"
6850 " target %d:%d:%d, debugging disabled\n",
6851 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6852 cam_dflags = CAM_DEBUG_NONE;
6853 }
6854 } else
6855 cam_dpath = NULL;
6856#else /* !CAM_DEBUG_BUS */
6857 cam_dpath = NULL;
6858#endif /* CAM_DEBUG_BUS */
6859#endif /* CAMDEBUG */
6860
6861 /*
6862 * Scan all installed busses.
6863 */
6864 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6865
6866 if (busses_to_config == 0) {
6867 /* Call manually because we don't have any busses */
6868 xpt_finishconfig(xpt_periph, NULL);
6869 } else {
6870 if (busses_to_reset > 0 && scsi_delay >= 2000) {
6871 printf("Waiting %d seconds for SCSI "
6872 "devices to settle\n", scsi_delay/1000);
6873 }
6874 xpt_for_all_busses(xptconfigfunc, NULL);
6875 }
6876}
6877
6878/*
6879 * If the given device only has one peripheral attached to it, and if that
6880 * peripheral is the passthrough driver, announce it. This insures that the
6881 * user sees some sort of announcement for every peripheral in their system.
6882 */
6883static int
6884xptpassannouncefunc(struct cam_ed *device, void *arg)
6885{
6886 struct cam_periph *periph;
6887 int i;
6888
6889 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6890 periph = SLIST_NEXT(periph, periph_links), i++);
6891
6892 periph = SLIST_FIRST(&device->periphs);
6893 if ((i == 1)
6894 && (strncmp(periph->periph_name, "pass", 4) == 0))
6895 xpt_announce_periph(periph, NULL);
6896
6897 return(1);
6898}
6899
6900static void
6901xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6902{
6903 struct periph_driver **p_drv;
6904 int i;
6905
6906 if (done_ccb != NULL) {
6907 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6908 ("xpt_finishconfig\n"));
6909 switch(done_ccb->ccb_h.func_code) {
6910 case XPT_RESET_BUS:
6911 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6912 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6913 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6914 done_ccb->crcn.flags = 0;
6915 xpt_action(done_ccb);
6916 return;
6917 }
6918 /* FALLTHROUGH */
6919 case XPT_SCAN_BUS:
6920 default:
6921 xpt_free_path(done_ccb->ccb_h.path);
6922 busses_to_config--;
6923 break;
6924 }
6925 }
6926
6927 if (busses_to_config == 0) {
6928 /* Register all the peripheral drivers */
6929 /* XXX This will have to change when we have loadable modules */
6930 p_drv = periph_drivers;
6931 for (i = 0; p_drv[i] != NULL; i++) {
6932 (*p_drv[i]->init)();
6933 }
6934
6935 /*
6936 * Check for devices with no "standard" peripheral driver
6937 * attached. For any devices like that, announce the
6938 * passthrough driver so the user will see something.
6939 */
6940 xpt_for_all_devices(xptpassannouncefunc, NULL);
6941
6942 /* Release our hook so that the boot can continue. */
6943 config_intrhook_disestablish(xpt_config_hook);
6944 free(xpt_config_hook, M_TEMP);
6945 xpt_config_hook = NULL;
6946 }
6947 if (done_ccb != NULL)
6948 xpt_free_ccb(done_ccb);
6949}
6950
6951static void
6952xptaction(struct cam_sim *sim, union ccb *work_ccb)
6953{
6954 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6955
6956 switch (work_ccb->ccb_h.func_code) {
6957 /* Common cases first */
6958 case XPT_PATH_INQ: /* Path routing inquiry */
6959 {
6960 struct ccb_pathinq *cpi;
6961
6962 cpi = &work_ccb->cpi;
6963 cpi->version_num = 1; /* XXX??? */
6964 cpi->hba_inquiry = 0;
6965 cpi->target_sprt = 0;
6966 cpi->hba_misc = 0;
6967 cpi->hba_eng_cnt = 0;
6968 cpi->max_target = 0;
6969 cpi->max_lun = 0;
6970 cpi->initiator_id = 0;
6971 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
6972 strncpy(cpi->hba_vid, "", HBA_IDLEN);
6973 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
6974 cpi->unit_number = sim->unit_number;
6975 cpi->bus_id = sim->bus_id;
6976 cpi->base_transfer_speed = 0;
6977#ifdef CAM_NEW_TRAN_CODE
6978 cpi->protocol = PROTO_UNSPECIFIED;
6979 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
6980 cpi->transport = XPORT_UNSPECIFIED;
6981 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
6982#endif /* CAM_NEW_TRAN_CODE */
6983 cpi->ccb_h.status = CAM_REQ_CMP;
6984 xpt_done(work_ccb);
6985 break;
6986 }
6987 default:
6988 work_ccb->ccb_h.status = CAM_REQ_INVALID;
6989 xpt_done(work_ccb);
6990 break;
6991 }
6992}
6993
6994/*
6995 * The xpt as a "controller" has no interrupt sources, so polling
6996 * is a no-op.
6997 */
6998static void
6999xptpoll(struct cam_sim *sim)
7000{
7001}
7002
7003static void
7004camisr(void *V_queue)
7005{
7006 cam_isrq_t *oqueue = V_queue;
7007 cam_isrq_t queue;
7008 int s;
7009 struct ccb_hdr *ccb_h;
7010
7011 /*
7012 * Transfer the ccb_bioq list to a temporary list so we can operate
7013 * on it without needing to lock/unlock on every loop. The concat
7014 * function with re-init the real list for us.
7015 */
7016 s = splcam();
7017 mtx_lock(&cam_bioq_lock);
7018 TAILQ_INIT(&queue);
7019 TAILQ_CONCAT(&queue, oqueue, sim_links.tqe);
7020 mtx_unlock(&cam_bioq_lock);
7021
7022 while ((ccb_h = TAILQ_FIRST(&queue)) != NULL) {
7023 int runq;
7024
7025 TAILQ_REMOVE(&queue, ccb_h, sim_links.tqe);
7026 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
7027 splx(s);
7028
7029 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
7030 ("camisr\n"));
7031
7032 runq = FALSE;
7033
7034 if (ccb_h->flags & CAM_HIGH_POWER) {
7035 struct highpowerlist *hphead;
7036 union ccb *send_ccb;
7037
7038 hphead = &highpowerq;
7039
7040 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
7041
7042 /*
7043 * Increment the count since this command is done.
7044 */
7045 num_highpower++;
7046
7047 /*
7048 * Any high powered commands queued up?
7049 */
7050 if (send_ccb != NULL) {
7051
7052 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
7053
7054 xpt_release_devq(send_ccb->ccb_h.path,
7055 /*count*/1, /*runqueue*/TRUE);
7056 }
7057 }
7058 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
7059 struct cam_ed *dev;
7060
7061 dev = ccb_h->path->device;
7062
7063 s = splcam();
7064 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
7065
7066 ccb_h->path->bus->sim->devq->send_active--;
7067 ccb_h->path->bus->sim->devq->send_openings++;
7068 splx(s);
7069
7070 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
7071 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
7072 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
7073 && (dev->ccbq.dev_active == 0))) {
7074
7075 xpt_release_devq(ccb_h->path, /*count*/1,
7076 /*run_queue*/TRUE);
7077 }
7078
7079 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
7080 && (--dev->tag_delay_count == 0))
7081 xpt_start_tags(ccb_h->path);
7082
7083 if ((dev->ccbq.queue.entries > 0)
7084 && (dev->qfrozen_cnt == 0)
7085 && (device_is_send_queued(dev) == 0)) {
7086 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
7087 dev);
7088 }
7089 }
7090
7091 if (ccb_h->status & CAM_RELEASE_SIMQ) {
7092 xpt_release_simq(ccb_h->path->bus->sim,
7093 /*run_queue*/TRUE);
7094 ccb_h->status &= ~CAM_RELEASE_SIMQ;
7095 runq = FALSE;
7096 }
7097
7098 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
7099 && (ccb_h->status & CAM_DEV_QFRZN)) {
7100 xpt_release_devq(ccb_h->path, /*count*/1,
7101 /*run_queue*/TRUE);
7102 ccb_h->status &= ~CAM_DEV_QFRZN;
7103 } else if (runq) {
7104 xpt_run_dev_sendq(ccb_h->path->bus);
7105 }
7106
7107 /* Call the peripheral driver's callback */
7108 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
7109
7110 /* Raise IPL for while test */
7111 s = splcam();
7112 }
7113 splx(s);
7114}