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 147571 2005-06-24 08:09:05Z 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 */
68
69/*
70 * Definition of an async handler callback block. These are used to add
71 * SIMs and peripherals to the async callback lists.
72 */
73struct async_node {
74 SLIST_ENTRY(async_node) links;
75 u_int32_t event_enable; /* Async Event enables */
76 void (*callback)(void *arg, u_int32_t code,
77 struct cam_path *path, void *args);
78 void *callback_arg;
79};
80
81SLIST_HEAD(async_list, async_node);
82SLIST_HEAD(periph_list, cam_periph);
83static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
84
85/*
86 * This is the maximum number of high powered commands (e.g. start unit)
87 * that can be outstanding at a particular time.
88 */
89#ifndef CAM_MAX_HIGHPOWER
90#define CAM_MAX_HIGHPOWER 4
91#endif
92
93/* number of high powered commands that can go through right now */
94static int num_highpower = CAM_MAX_HIGHPOWER;
95
96/*
97 * Structure for queueing a device in a run queue.
98 * There is one run queue for allocating new ccbs,
99 * and another for sending ccbs to the controller.
100 */
101struct cam_ed_qinfo {
102 cam_pinfo pinfo;
103 struct cam_ed *device;
104};
105
106/*
107 * The CAM EDT (Existing Device Table) contains the device information for
108 * all devices for all busses in the system. The table contains a
109 * cam_ed structure for each device on the bus.
110 */
111struct cam_ed {
112 TAILQ_ENTRY(cam_ed) links;
113 struct cam_ed_qinfo alloc_ccb_entry;
114 struct cam_ed_qinfo send_ccb_entry;
115 struct cam_et *target;
116 lun_id_t lun_id;
117 struct camq drvq; /*
118 * Queue of type drivers wanting to do
119 * work on this device.
120 */
121 struct cam_ccbq ccbq; /* Queue of pending ccbs */
122 struct async_list asyncs; /* Async callback info for this B/T/L */
123 struct periph_list periphs; /* All attached devices */
124 u_int generation; /* Generation number */
125 struct cam_periph *owner; /* Peripheral driver's ownership tag */
126 struct xpt_quirk_entry *quirk; /* Oddities about this device */
127 /* Storage for the inquiry data */
128#ifdef CAM_NEW_TRAN_CODE
129 cam_proto protocol;
130 u_int protocol_version;
131 cam_xport transport;
132 u_int transport_version;
133#endif /* CAM_NEW_TRAN_CODE */
134 struct scsi_inquiry_data inq_data;
135 u_int8_t inq_flags; /*
136 * Current settings for inquiry flags.
137 * This allows us to override settings
138 * like disconnection and tagged
139 * queuing for a device.
140 */
141 u_int8_t queue_flags; /* Queue flags from the control page */
142 u_int8_t serial_num_len;
143 u_int8_t *serial_num;
144 u_int32_t qfrozen_cnt;
145 u_int32_t flags;
146#define CAM_DEV_UNCONFIGURED 0x01
147#define CAM_DEV_REL_TIMEOUT_PENDING 0x02
148#define CAM_DEV_REL_ON_COMPLETE 0x04
149#define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
150#define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
151#define CAM_DEV_TAG_AFTER_COUNT 0x20
152#define CAM_DEV_INQUIRY_DATA_VALID 0x40
153 u_int32_t tag_delay_count;
154#define CAM_TAG_DELAY_COUNT 5
155 u_int32_t tag_saved_openings;
156 u_int32_t refcount;
157 struct callout_handle c_handle;
158};
159
160/*
161 * Each target is represented by an ET (Existing Target). These
162 * entries are created when a target is successfully probed with an
163 * identify, and removed when a device fails to respond after a number
164 * of retries, or a bus rescan finds the device missing.
165 */
166struct cam_et {
167 TAILQ_HEAD(, cam_ed) ed_entries;
168 TAILQ_ENTRY(cam_et) links;
169 struct cam_eb *bus;
170 target_id_t target_id;
171 u_int32_t refcount;
172 u_int generation;
173 struct timeval last_reset;
174};
175
176/*
177 * Each bus is represented by an EB (Existing Bus). These entries
178 * are created by calls to xpt_bus_register and deleted by calls to
179 * xpt_bus_deregister.
180 */
181struct cam_eb {
182 TAILQ_HEAD(, cam_et) et_entries;
183 TAILQ_ENTRY(cam_eb) links;
184 path_id_t path_id;
185 struct cam_sim *sim;
186 struct timeval last_reset;
187 u_int32_t flags;
188#define CAM_EB_RUNQ_SCHEDULED 0x01
189 u_int32_t refcount;
190 u_int generation;
191};
192
193struct cam_path {
194 struct cam_periph *periph;
195 struct cam_eb *bus;
196 struct cam_et *target;
197 struct cam_ed *device;
198};
199
200struct xpt_quirk_entry {
201 struct scsi_inquiry_pattern inq_pat;
202 u_int8_t quirks;
203#define CAM_QUIRK_NOLUNS 0x01
204#define CAM_QUIRK_NOSERIAL 0x02
205#define CAM_QUIRK_HILUNS 0x04
206#define CAM_QUIRK_NOHILUNS 0x08
207 u_int mintags;
208 u_int maxtags;
209};
210#define CAM_SCSI2_MAXLUN 8
211/*
212 * If we're not quirked to search <= the first 8 luns
213 * and we are either quirked to search above lun 8,
214 * or we're > SCSI-2, we can look for luns above lun 8.
215 */
216#define CAN_SRCH_HI(dv) \
217 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \
218 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \
219 || SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2))
220
221typedef enum {
222 XPT_FLAG_OPEN = 0x01
223} xpt_flags;
224
225struct xpt_softc {
226 xpt_flags flags;
227 u_int32_t generation;
228};
229
230static const char quantum[] = "QUANTUM";
231static const char sony[] = "SONY";
232static const char west_digital[] = "WDIGTL";
233static const char samsung[] = "SAMSUNG";
234static const char seagate[] = "SEAGATE";
235static const char microp[] = "MICROP";
236
237static struct xpt_quirk_entry xpt_quirk_table[] =
238{
239 {
240 /* Reports QUEUE FULL for temporary resource shortages */
241 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
242 /*quirks*/0, /*mintags*/24, /*maxtags*/32
243 },
244 {
245 /* Reports QUEUE FULL for temporary resource shortages */
246 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
247 /*quirks*/0, /*mintags*/24, /*maxtags*/32
248 },
249 {
250 /* Reports QUEUE FULL for temporary resource shortages */
251 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
252 /*quirks*/0, /*mintags*/24, /*maxtags*/32
253 },
254 {
255 /* Broken tagged queuing drive */
256 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
257 /*quirks*/0, /*mintags*/0, /*maxtags*/0
258 },
259 {
260 /* Broken tagged queuing drive */
261 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
262 /*quirks*/0, /*mintags*/0, /*maxtags*/0
263 },
264 {
265 /* Broken tagged queuing drive */
266 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
267 /*quirks*/0, /*mintags*/0, /*maxtags*/0
268 },
269 {
270 /*
271 * Unfortunately, the Quantum Atlas III has the same
272 * problem as the Atlas II drives above.
273 * Reported by: "Johan Granlund" <johan@granlund.nu>
274 *
275 * For future reference, the drive with the problem was:
276 * QUANTUM QM39100TD-SW N1B0
277 *
278 * It's possible that Quantum will fix the problem in later
279 * firmware revisions. If that happens, the quirk entry
280 * will need to be made specific to the firmware revisions
281 * with the problem.
282 *
283 */
284 /* Reports QUEUE FULL for temporary resource shortages */
285 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
286 /*quirks*/0, /*mintags*/24, /*maxtags*/32
287 },
288 {
289 /*
290 * 18 Gig Atlas III, same problem as the 9G version.
291 * Reported by: Andre Albsmeier
292 * <andre.albsmeier@mchp.siemens.de>
293 *
294 * For future reference, the drive with the problem was:
295 * QUANTUM QM318000TD-S N491
296 */
297 /* Reports QUEUE FULL for temporary resource shortages */
298 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
299 /*quirks*/0, /*mintags*/24, /*maxtags*/32
300 },
301 {
302 /*
303 * Broken tagged queuing drive
304 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
305 * and: Martin Renters <martin@tdc.on.ca>
306 */
307 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
308 /*quirks*/0, /*mintags*/0, /*maxtags*/0
309 },
310 /*
311 * The Seagate Medalist Pro drives have very poor write
312 * performance with anything more than 2 tags.
313 *
314 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
315 * Drive: <SEAGATE ST36530N 1444>
316 *
317 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
318 * Drive: <SEAGATE ST34520W 1281>
319 *
320 * No one has actually reported that the 9G version
321 * (ST39140*) of the Medalist Pro has the same problem, but
322 * we're assuming that it does because the 4G and 6.5G
323 * versions of the drive are broken.
324 */
325 {
326 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
327 /*quirks*/0, /*mintags*/2, /*maxtags*/2
328 },
329 {
330 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
331 /*quirks*/0, /*mintags*/2, /*maxtags*/2
332 },
333 {
334 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
335 /*quirks*/0, /*mintags*/2, /*maxtags*/2
336 },
337 {
338 /*
339 * Slow when tagged queueing is enabled. Write performance
340 * steadily drops off with more and more concurrent
341 * transactions. Best sequential write performance with
342 * tagged queueing turned off and write caching turned on.
343 *
344 * PR: kern/10398
345 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
346 * Drive: DCAS-34330 w/ "S65A" firmware.
347 *
348 * The drive with the problem had the "S65A" firmware
349 * revision, and has also been reported (by Stephen J.
350 * Roznowski <sjr@home.net>) for a drive with the "S61A"
351 * firmware revision.
352 *
353 * Although no one has reported problems with the 2 gig
354 * version of the DCAS drive, the assumption is that it
355 * has the same problems as the 4 gig version. Therefore
356 * this quirk entries disables tagged queueing for all
357 * DCAS drives.
358 */
359 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
360 /*quirks*/0, /*mintags*/0, /*maxtags*/0
361 },
362 {
363 /* Broken tagged queuing drive */
364 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
365 /*quirks*/0, /*mintags*/0, /*maxtags*/0
366 },
367 {
368 /* Broken tagged queuing drive */
369 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
370 /*quirks*/0, /*mintags*/0, /*maxtags*/0
371 },
372 {
373 /*
374 * Broken tagged queuing drive.
375 * Submitted by:
376 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
377 * in PR kern/9535
378 */
379 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
380 /*quirks*/0, /*mintags*/0, /*maxtags*/0
381 },
382 {
383 /*
384 * Slow when tagged queueing is enabled. (1.5MB/sec versus
385 * 8MB/sec.)
386 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
387 * Best performance with these drives is achieved with
388 * tagged queueing turned off, and write caching turned on.
389 */
390 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
391 /*quirks*/0, /*mintags*/0, /*maxtags*/0
392 },
393 {
394 /*
395 * Slow when tagged queueing is enabled. (1.5MB/sec versus
396 * 8MB/sec.)
397 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
398 * Best performance with these drives is achieved with
399 * tagged queueing turned off, and write caching turned on.
400 */
401 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
402 /*quirks*/0, /*mintags*/0, /*maxtags*/0
403 },
404 {
405 /*
406 * Doesn't handle queue full condition correctly,
407 * so we need to limit maxtags to what the device
408 * can handle instead of determining this automatically.
409 */
410 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
411 /*quirks*/0, /*mintags*/2, /*maxtags*/32
412 },
413 {
414 /* Really only one LUN */
415 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
416 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
417 },
418 {
419 /* I can't believe we need a quirk for DPT volumes. */
420 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
421 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
422 /*mintags*/0, /*maxtags*/255
423 },
424 {
425 /*
426 * Many Sony CDROM drives don't like multi-LUN probing.
427 */
428 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
429 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
430 },
431 {
432 /*
433 * This drive doesn't like multiple LUN probing.
434 * Submitted by: Parag Patel <parag@cgt.com>
435 */
436 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
437 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
438 },
439 {
440 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
441 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
442 },
443 {
444 /*
445 * The 8200 doesn't like multi-lun probing, and probably
446 * don't like serial number requests either.
447 */
448 {
449 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
450 "EXB-8200*", "*"
451 },
452 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
453 },
454 {
455 /*
456 * Let's try the same as above, but for a drive that says
457 * it's an IPL-6860 but is actually an EXB 8200.
458 */
459 {
460 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
461 "IPL-6860*", "*"
462 },
463 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
464 },
465 {
466 /*
467 * These Hitachi drives don't like multi-lun probing.
468 * The PR submitter has a DK319H, but says that the Linux
469 * kernel has a similar work-around for the DK312 and DK314,
470 * so all DK31* drives are quirked here.
471 * PR: misc/18793
472 * Submitted by: Paul Haddad <paul@pth.com>
473 */
474 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
475 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
476 },
477 {
478 /*
479 * The Hitachi CJ series with J8A8 firmware apparantly has
480 * problems with tagged commands.
481 * PR: 23536
482 * Reported by: amagai@nue.org
483 */
484 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
485 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
486 },
487 {
488 /*
489 * These are the large storage arrays.
490 * Submitted by: William Carrel <william.carrel@infospace.com>
491 */
492 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
493 CAM_QUIRK_HILUNS, 2, 1024
494 },
495 {
496 /*
497 * This old revision of the TDC3600 is also SCSI-1, and
498 * hangs upon serial number probing.
499 */
500 {
501 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
502 " TDC 3600", "U07:"
503 },
504 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
505 },
506 {
507 /*
508 * Maxtor Personal Storage 3000XT (Firewire)
509 * hangs upon serial number probing.
510 */
511 {
512 T_DIRECT, SIP_MEDIA_FIXED, "Maxtor",
513 "1394 storage", "*"
514 },
515 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
516 },
517 {
518 /*
519 * Would repond to all LUNs if asked for.
520 */
521 {
522 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
523 "CP150", "*"
524 },
525 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
526 },
527 {
528 /*
529 * Would repond to all LUNs if asked for.
530 */
531 {
532 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
533 "96X2*", "*"
534 },
535 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
536 },
537 {
538 /* Submitted by: Matthew Dodd <winter@jurai.net> */
539 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
540 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
541 },
542 {
543 /* Submitted by: Matthew Dodd <winter@jurai.net> */
544 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
545 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
546 },
547 {
548 /* TeraSolutions special settings for TRC-22 RAID */
549 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
550 /*quirks*/0, /*mintags*/55, /*maxtags*/255
551 },
552 {
553 /* Veritas Storage Appliance */
554 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
555 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
556 },
557 {
558 /*
559 * Would respond to all LUNs. Device type and removable
560 * flag are jumper-selectable.
561 */
562 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
563 "Tahiti 1", "*"
564 },
565 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
566 },
567 {
568 /* EasyRAID E5A aka. areca ARC-6010 */
569 { T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
570 CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
571 },
572 {
573 /* Default tagged queuing parameters for all devices */
574 {
575 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
576 /*vendor*/"*", /*product*/"*", /*revision*/"*"
577 },
578 /*quirks*/0, /*mintags*/2, /*maxtags*/255
579 },
580};
581
582static const int xpt_quirk_table_size =
583 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
584
585typedef enum {
586 DM_RET_COPY = 0x01,
587 DM_RET_FLAG_MASK = 0x0f,
588 DM_RET_NONE = 0x00,
589 DM_RET_STOP = 0x10,
590 DM_RET_DESCEND = 0x20,
591 DM_RET_ERROR = 0x30,
592 DM_RET_ACTION_MASK = 0xf0
593} dev_match_ret;
594
595typedef enum {
596 XPT_DEPTH_BUS,
597 XPT_DEPTH_TARGET,
598 XPT_DEPTH_DEVICE,
599 XPT_DEPTH_PERIPH
600} xpt_traverse_depth;
601
602struct xpt_traverse_config {
603 xpt_traverse_depth depth;
604 void *tr_func;
605 void *tr_arg;
606};
607
608typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
609typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
610typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
611typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
612typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
613
614/* Transport layer configuration information */
615static struct xpt_softc xsoftc;
616
617/* Queues for our software interrupt handler */
618typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
619static cam_isrq_t cam_bioq;
620static struct mtx cam_bioq_lock;
621
622/* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
623static SLIST_HEAD(,ccb_hdr) ccb_freeq;
624static u_int xpt_max_ccbs; /*
625 * Maximum size of ccb pool. Modified as
626 * devices are added/removed or have their
627 * opening counts changed.
628 */
629static u_int xpt_ccb_count; /* Current count of allocated ccbs */
630
631struct cam_periph *xpt_periph;
632
633static periph_init_t xpt_periph_init;
634
635static periph_init_t probe_periph_init;
636
637static struct periph_driver xpt_driver =
638{
639 xpt_periph_init, "xpt",
640 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
641};
642
643static struct periph_driver probe_driver =
644{
645 probe_periph_init, "probe",
646 TAILQ_HEAD_INITIALIZER(probe_driver.units)
647};
648
649PERIPHDRIVER_DECLARE(xpt, xpt_driver);
650PERIPHDRIVER_DECLARE(probe, probe_driver);
651
652
653static d_open_t xptopen;
654static d_close_t xptclose;
655static d_ioctl_t xptioctl;
656
657static struct cdevsw xpt_cdevsw = {
658 .d_version = D_VERSION,
659 .d_flags = D_NEEDGIANT,
660 .d_open = xptopen,
661 .d_close = xptclose,
662 .d_ioctl = xptioctl,
663 .d_name = "xpt",
664};
665
666static struct intr_config_hook *xpt_config_hook;
667
668/* Registered busses */
669static TAILQ_HEAD(,cam_eb) xpt_busses;
670static u_int bus_generation;
671
672/* Storage for debugging datastructures */
673#ifdef CAMDEBUG
674struct cam_path *cam_dpath;
675u_int32_t cam_dflags;
676u_int32_t cam_debug_delay;
677#endif
678
679/* Pointers to software interrupt handlers */
680static void *cambio_ih;
681
682#if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
683#error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
684#endif
685
686/*
687 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
688 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
689 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
690 */
691#if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
692 || defined(CAM_DEBUG_LUN)
693#ifdef CAMDEBUG
694#if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
695 || !defined(CAM_DEBUG_LUN)
696#error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
697 and CAM_DEBUG_LUN"
698#endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
699#else /* !CAMDEBUG */
700#error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
701#endif /* CAMDEBUG */
702#endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
703
704/* Our boot-time initialization hook */
705static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
706
707static moduledata_t cam_moduledata = {
708 "cam",
709 cam_module_event_handler,
710 NULL
711};
712
713static void xpt_init(void *);
714
715DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
716MODULE_VERSION(cam, 1);
717
718
719static cam_status xpt_compile_path(struct cam_path *new_path,
720 struct cam_periph *perph,
721 path_id_t path_id,
722 target_id_t target_id,
723 lun_id_t lun_id);
724
725static void xpt_release_path(struct cam_path *path);
726
727static void xpt_async_bcast(struct async_list *async_head,
728 u_int32_t async_code,
729 struct cam_path *path,
730 void *async_arg);
731static void xpt_dev_async(u_int32_t async_code,
732 struct cam_eb *bus,
733 struct cam_et *target,
734 struct cam_ed *device,
735 void *async_arg);
736static path_id_t xptnextfreepathid(void);
737static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
738static union ccb *xpt_get_ccb(struct cam_ed *device);
739static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
740 u_int32_t new_priority);
741static void xpt_run_dev_allocq(struct cam_eb *bus);
742static void xpt_run_dev_sendq(struct cam_eb *bus);
743static timeout_t xpt_release_devq_timeout;
744static timeout_t xpt_release_simq_timeout;
745static void xpt_release_bus(struct cam_eb *bus);
746static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
747 int run_queue);
748static struct cam_et*
749 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
750static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
751static struct cam_ed*
752 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
753 lun_id_t lun_id);
754static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
755 struct cam_ed *device);
756static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
757static struct cam_eb*
758 xpt_find_bus(path_id_t path_id);
759static struct cam_et*
760 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
761static struct cam_ed*
762 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
763static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
764static void xpt_scan_lun(struct cam_periph *periph,
765 struct cam_path *path, cam_flags flags,
766 union ccb *ccb);
767static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
768static xpt_busfunc_t xptconfigbuscountfunc;
769static xpt_busfunc_t xptconfigfunc;
770static void xpt_config(void *arg);
771static xpt_devicefunc_t xptpassannouncefunc;
772static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
773static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
774static void xptpoll(struct cam_sim *sim);
775static void camisr(void *);
776#if 0
777static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
778static void xptasync(struct cam_periph *periph,
779 u_int32_t code, cam_path *path);
780#endif
781static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
782 u_int num_patterns, struct cam_eb *bus);
783static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
784 u_int num_patterns,
785 struct cam_ed *device);
786static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
787 u_int num_patterns,
788 struct cam_periph *periph);
789static xpt_busfunc_t xptedtbusfunc;
790static xpt_targetfunc_t xptedttargetfunc;
791static xpt_devicefunc_t xptedtdevicefunc;
792static xpt_periphfunc_t xptedtperiphfunc;
793static xpt_pdrvfunc_t xptplistpdrvfunc;
794static xpt_periphfunc_t xptplistperiphfunc;
795static int xptedtmatch(struct ccb_dev_match *cdm);
796static int xptperiphlistmatch(struct ccb_dev_match *cdm);
797static int xptbustraverse(struct cam_eb *start_bus,
798 xpt_busfunc_t *tr_func, void *arg);
799static int xpttargettraverse(struct cam_eb *bus,
800 struct cam_et *start_target,
801 xpt_targetfunc_t *tr_func, void *arg);
802static int xptdevicetraverse(struct cam_et *target,
803 struct cam_ed *start_device,
804 xpt_devicefunc_t *tr_func, void *arg);
805static int xptperiphtraverse(struct cam_ed *device,
806 struct cam_periph *start_periph,
807 xpt_periphfunc_t *tr_func, void *arg);
808static int xptpdrvtraverse(struct periph_driver **start_pdrv,
809 xpt_pdrvfunc_t *tr_func, void *arg);
810static int xptpdperiphtraverse(struct periph_driver **pdrv,
811 struct cam_periph *start_periph,
812 xpt_periphfunc_t *tr_func,
813 void *arg);
814static xpt_busfunc_t xptdefbusfunc;
815static xpt_targetfunc_t xptdeftargetfunc;
816static xpt_devicefunc_t xptdefdevicefunc;
817static xpt_periphfunc_t xptdefperiphfunc;
818static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
819#ifdef notusedyet
820static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
821 void *arg);
822#endif
823static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
824 void *arg);
825#ifdef notusedyet
826static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
827 void *arg);
828#endif
829static xpt_devicefunc_t xptsetasyncfunc;
830static xpt_busfunc_t xptsetasyncbusfunc;
831static cam_status xptregister(struct cam_periph *periph,
832 void *arg);
833static cam_status proberegister(struct cam_periph *periph,
834 void *arg);
835static void probeschedule(struct cam_periph *probe_periph);
836static void probestart(struct cam_periph *periph, union ccb *start_ccb);
837static void proberequestdefaultnegotiation(struct cam_periph *periph);
838static void probedone(struct cam_periph *periph, union ccb *done_ccb);
839static void probecleanup(struct cam_periph *periph);
840static void xpt_find_quirk(struct cam_ed *device);
841#ifdef CAM_NEW_TRAN_CODE
842static void xpt_devise_transport(struct cam_path *path);
843#endif /* CAM_NEW_TRAN_CODE */
844static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
845 struct cam_ed *device,
846 int async_update);
847static void xpt_toggle_tags(struct cam_path *path);
848static void xpt_start_tags(struct cam_path *path);
849static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
850 struct cam_ed *dev);
851static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
852 struct cam_ed *dev);
853static __inline int periph_is_queued(struct cam_periph *periph);
854static __inline int device_is_alloc_queued(struct cam_ed *device);
855static __inline int device_is_send_queued(struct cam_ed *device);
856static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
857
858static __inline int
859xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
860{
861 int retval;
862
863 if (dev->ccbq.devq_openings > 0) {
864 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
865 cam_ccbq_resize(&dev->ccbq,
866 dev->ccbq.dev_openings
867 + dev->ccbq.dev_active);
868 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
869 }
870 /*
871 * The priority of a device waiting for CCB resources
872 * is that of the the highest priority peripheral driver
873 * enqueued.
874 */
875 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
876 &dev->alloc_ccb_entry.pinfo,
877 CAMQ_GET_HEAD(&dev->drvq)->priority);
878 } else {
879 retval = 0;
880 }
881
882 return (retval);
883}
884
885static __inline int
886xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
887{
888 int retval;
889
890 if (dev->ccbq.dev_openings > 0) {
891 /*
892 * The priority of a device waiting for controller
893 * resources is that of the the highest priority CCB
894 * enqueued.
895 */
896 retval =
897 xpt_schedule_dev(&bus->sim->devq->send_queue,
898 &dev->send_ccb_entry.pinfo,
899 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
900 } else {
901 retval = 0;
902 }
903 return (retval);
904}
905
906static __inline int
907periph_is_queued(struct cam_periph *periph)
908{
909 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
910}
911
912static __inline int
913device_is_alloc_queued(struct cam_ed *device)
914{
915 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
916}
917
918static __inline int
919device_is_send_queued(struct cam_ed *device)
920{
921 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
922}
923
924static __inline int
925dev_allocq_is_runnable(struct cam_devq *devq)
926{
927 /*
928 * Have work to do.
929 * Have space to do more work.
930 * Allowed to do work.
931 */
932 return ((devq->alloc_queue.qfrozen_cnt == 0)
933 && (devq->alloc_queue.entries > 0)
934 && (devq->alloc_openings > 0));
935}
936
937static void
938xpt_periph_init()
939{
940 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
941}
942
943static void
944probe_periph_init()
945{
946}
947
948
949static void
950xptdone(struct cam_periph *periph, union ccb *done_ccb)
951{
952 /* Caller will release the CCB */
953 wakeup(&done_ccb->ccb_h.cbfcnp);
954}
955
956static int
957xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
958{
959 int unit;
960
961 unit = minor(dev) & 0xff;
962
963 /*
964 * Only allow read-write access.
965 */
966 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
967 return(EPERM);
968
969 /*
970 * We don't allow nonblocking access.
971 */
972 if ((flags & O_NONBLOCK) != 0) {
973 printf("xpt%d: can't do nonblocking access\n", unit);
974 return(ENODEV);
975 }
976
977 /*
978 * We only have one transport layer right now. If someone accesses
979 * us via something other than minor number 1, point out their
980 * mistake.
981 */
982 if (unit != 0) {
983 printf("xptopen: got invalid xpt unit %d\n", unit);
984 return(ENXIO);
985 }
986
987 /* Mark ourselves open */
988 xsoftc.flags |= XPT_FLAG_OPEN;
989
990 return(0);
991}
992
993static int
994xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
995{
996 int unit;
997
998 unit = minor(dev) & 0xff;
999
1000 /*
1001 * We only have one transport layer right now. If someone accesses
1002 * us via something other than minor number 1, point out their
1003 * mistake.
1004 */
1005 if (unit != 0) {
1006 printf("xptclose: got invalid xpt unit %d\n", unit);
1007 return(ENXIO);
1008 }
1009
1010 /* Mark ourselves closed */
1011 xsoftc.flags &= ~XPT_FLAG_OPEN;
1012
1013 return(0);
1014}
1015
1016static int
1017xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1018{
1019 int unit, error;
1020
1021 error = 0;
1022 unit = minor(dev) & 0xff;
1023
1024 /*
1025 * We only have one transport layer right now. If someone accesses
1026 * us via something other than minor number 1, point out their
1027 * mistake.
1028 */
1029 if (unit != 0) {
1030 printf("xptioctl: got invalid xpt unit %d\n", unit);
1031 return(ENXIO);
1032 }
1033
1034 switch(cmd) {
1035 /*
1036 * For the transport layer CAMIOCOMMAND ioctl, we really only want
1037 * to accept CCB types that don't quite make sense to send through a
1038 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
1039 * in the CAM spec.
1040 */
1041 case CAMIOCOMMAND: {
1042 union ccb *ccb;
1043 union ccb *inccb;
1044
1045 inccb = (union ccb *)addr;
1046
1047 switch(inccb->ccb_h.func_code) {
1048 case XPT_SCAN_BUS:
1049 case XPT_RESET_BUS:
1050 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
1051 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1052 error = EINVAL;
1053 break;
1054 }
1055 /* FALLTHROUGH */
1056 case XPT_PATH_INQ:
1057 case XPT_ENG_INQ:
1058 case XPT_SCAN_LUN:
1059
1060 ccb = xpt_alloc_ccb();
1061
1062 /*
1063 * Create a path using the bus, target, and lun the
1064 * user passed in.
1065 */
1066 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1067 inccb->ccb_h.path_id,
1068 inccb->ccb_h.target_id,
1069 inccb->ccb_h.target_lun) !=
1070 CAM_REQ_CMP){
1071 error = EINVAL;
1072 xpt_free_ccb(ccb);
1073 break;
1074 }
1075 /* Ensure all of our fields are correct */
1076 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1077 inccb->ccb_h.pinfo.priority);
1078 xpt_merge_ccb(ccb, inccb);
1079 ccb->ccb_h.cbfcnp = xptdone;
1080 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1081 bcopy(ccb, inccb, sizeof(union ccb));
1082 xpt_free_path(ccb->ccb_h.path);
1083 xpt_free_ccb(ccb);
1084 break;
1085
1086 case XPT_DEBUG: {
1087 union ccb ccb;
1088
1089 /*
1090 * This is an immediate CCB, so it's okay to
1091 * allocate it on the stack.
1092 */
1093
1094 /*
1095 * Create a path using the bus, target, and lun the
1096 * user passed in.
1097 */
1098 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1099 inccb->ccb_h.path_id,
1100 inccb->ccb_h.target_id,
1101 inccb->ccb_h.target_lun) !=
1102 CAM_REQ_CMP){
1103 error = EINVAL;
1104 break;
1105 }
1106 /* Ensure all of our fields are correct */
1107 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1108 inccb->ccb_h.pinfo.priority);
1109 xpt_merge_ccb(&ccb, inccb);
1110 ccb.ccb_h.cbfcnp = xptdone;
1111 xpt_action(&ccb);
1112 bcopy(&ccb, inccb, sizeof(union ccb));
1113 xpt_free_path(ccb.ccb_h.path);
1114 break;
1115
1116 }
1117 case XPT_DEV_MATCH: {
1118 struct cam_periph_map_info mapinfo;
1119 struct cam_path *old_path;
1120
1121 /*
1122 * We can't deal with physical addresses for this
1123 * type of transaction.
1124 */
1125 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1126 error = EINVAL;
1127 break;
1128 }
1129
1130 /*
1131 * Save this in case the caller had it set to
1132 * something in particular.
1133 */
1134 old_path = inccb->ccb_h.path;
1135
1136 /*
1137 * We really don't need a path for the matching
1138 * code. The path is needed because of the
1139 * debugging statements in xpt_action(). They
1140 * assume that the CCB has a valid path.
1141 */
1142 inccb->ccb_h.path = xpt_periph->path;
1143
1144 bzero(&mapinfo, sizeof(mapinfo));
1145
1146 /*
1147 * Map the pattern and match buffers into kernel
1148 * virtual address space.
1149 */
1150 error = cam_periph_mapmem(inccb, &mapinfo);
1151
1152 if (error) {
1153 inccb->ccb_h.path = old_path;
1154 break;
1155 }
1156
1157 /*
1158 * This is an immediate CCB, we can send it on directly.
1159 */
1160 xpt_action(inccb);
1161
1162 /*
1163 * Map the buffers back into user space.
1164 */
1165 cam_periph_unmapmem(inccb, &mapinfo);
1166
1167 inccb->ccb_h.path = old_path;
1168
1169 error = 0;
1170 break;
1171 }
1172 default:
1173 error = ENOTSUP;
1174 break;
1175 }
1176 break;
1177 }
1178 /*
1179 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1180 * with the periphal driver name and unit name filled in. The other
1181 * fields don't really matter as input. The passthrough driver name
1182 * ("pass"), and unit number are passed back in the ccb. The current
1183 * device generation number, and the index into the device peripheral
1184 * driver list, and the status are also passed back. Note that
1185 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1186 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1187 * (or rather should be) impossible for the device peripheral driver
1188 * list to change since we look at the whole thing in one pass, and
1189 * we do it with splcam protection.
1190 *
1191 */
1192 case CAMGETPASSTHRU: {
1193 union ccb *ccb;
1194 struct cam_periph *periph;
1195 struct periph_driver **p_drv;
1196 char *name;
1197 u_int unit;
1198 u_int cur_generation;
1199 int base_periph_found;
1200 int splbreaknum;
1201 int s;
1202
1203 ccb = (union ccb *)addr;
1204 unit = ccb->cgdl.unit_number;
1205 name = ccb->cgdl.periph_name;
1206 /*
1207 * Every 100 devices, we want to drop our spl protection to
1208 * give the software interrupt handler a chance to run.
1209 * Most systems won't run into this check, but this should
1210 * avoid starvation in the software interrupt handler in
1211 * large systems.
1212 */
1213 splbreaknum = 100;
1214
1215 ccb = (union ccb *)addr;
1216
1217 base_periph_found = 0;
1218
1219 /*
1220 * Sanity check -- make sure we don't get a null peripheral
1221 * driver name.
1222 */
1223 if (*ccb->cgdl.periph_name == '\0') {
1224 error = EINVAL;
1225 break;
1226 }
1227
1228 /* Keep the list from changing while we traverse it */
1229 s = splcam();
1230ptstartover:
1231 cur_generation = xsoftc.generation;
1232
1233 /* first find our driver in the list of drivers */
1234 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
1235 if (strcmp((*p_drv)->driver_name, name) == 0)
1236 break;
1237
1238 if (*p_drv == NULL) {
1239 splx(s);
1240 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1241 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1242 *ccb->cgdl.periph_name = '\0';
1243 ccb->cgdl.unit_number = 0;
1244 error = ENOENT;
1245 break;
1246 }
1247
1248 /*
1249 * Run through every peripheral instance of this driver
1250 * and check to see whether it matches the unit passed
1251 * in by the user. If it does, get out of the loops and
1252 * find the passthrough driver associated with that
1253 * peripheral driver.
1254 */
1255 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1256 periph = TAILQ_NEXT(periph, unit_links)) {
1257
1258 if (periph->unit_number == unit) {
1259 break;
1260 } else if (--splbreaknum == 0) {
1261 splx(s);
1262 s = splcam();
1263 splbreaknum = 100;
1264 if (cur_generation != xsoftc.generation)
1265 goto ptstartover;
1266 }
1267 }
1268 /*
1269 * If we found the peripheral driver that the user passed
1270 * in, go through all of the peripheral drivers for that
1271 * particular device and look for a passthrough driver.
1272 */
1273 if (periph != NULL) {
1274 struct cam_ed *device;
1275 int i;
1276
1277 base_periph_found = 1;
1278 device = periph->path->device;
1279 for (i = 0, periph = SLIST_FIRST(&device->periphs);
1280 periph != NULL;
1281 periph = SLIST_NEXT(periph, periph_links), i++) {
1282 /*
1283 * Check to see whether we have a
1284 * passthrough device or not.
1285 */
1286 if (strcmp(periph->periph_name, "pass") == 0) {
1287 /*
1288 * Fill in the getdevlist fields.
1289 */
1290 strcpy(ccb->cgdl.periph_name,
1291 periph->periph_name);
1292 ccb->cgdl.unit_number =
1293 periph->unit_number;
1294 if (SLIST_NEXT(periph, periph_links))
1295 ccb->cgdl.status =
1296 CAM_GDEVLIST_MORE_DEVS;
1297 else
1298 ccb->cgdl.status =
1299 CAM_GDEVLIST_LAST_DEVICE;
1300 ccb->cgdl.generation =
1301 device->generation;
1302 ccb->cgdl.index = i;
1303 /*
1304 * Fill in some CCB header fields
1305 * that the user may want.
1306 */
1307 ccb->ccb_h.path_id =
1308 periph->path->bus->path_id;
1309 ccb->ccb_h.target_id =
1310 periph->path->target->target_id;
1311 ccb->ccb_h.target_lun =
1312 periph->path->device->lun_id;
1313 ccb->ccb_h.status = CAM_REQ_CMP;
1314 break;
1315 }
1316 }
1317 }
1318
1319 /*
1320 * If the periph is null here, one of two things has
1321 * happened. The first possibility is that we couldn't
1322 * find the unit number of the particular peripheral driver
1323 * that the user is asking about. e.g. the user asks for
1324 * the passthrough driver for "da11". We find the list of
1325 * "da" peripherals all right, but there is no unit 11.
1326 * The other possibility is that we went through the list
1327 * of peripheral drivers attached to the device structure,
1328 * but didn't find one with the name "pass". Either way,
1329 * we return ENOENT, since we couldn't find something.
1330 */
1331 if (periph == NULL) {
1332 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1333 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1334 *ccb->cgdl.periph_name = '\0';
1335 ccb->cgdl.unit_number = 0;
1336 error = ENOENT;
1337 /*
1338 * It is unfortunate that this is even necessary,
1339 * but there are many, many clueless users out there.
1340 * If this is true, the user is looking for the
1341 * passthrough driver, but doesn't have one in his
1342 * kernel.
1343 */
1344 if (base_periph_found == 1) {
1345 printf("xptioctl: pass driver is not in the "
1346 "kernel\n");
1347 printf("xptioctl: put \"device pass0\" in "
1348 "your kernel config file\n");
1349 }
1350 }
1351 splx(s);
1352 break;
1353 }
1354 default:
1355 error = ENOTTY;
1356 break;
1357 }
1358
1359 return(error);
1360}
1361
1362static int
1363cam_module_event_handler(module_t mod, int what, void *arg)
1364{
1365 if (what == MOD_LOAD) {
1366 xpt_init(NULL);
1367 } else if (what == MOD_UNLOAD) {
1368 return EBUSY;
1369 } else {
1370 return EOPNOTSUPP;
1371 }
1372
1373 return 0;
1374}
1375
1376/* Functions accessed by the peripheral drivers */
1377static void
1378xpt_init(dummy)
1379 void *dummy;
1380{
1381 struct cam_sim *xpt_sim;
1382 struct cam_path *path;
1383 struct cam_devq *devq;
1384 cam_status status;
1385
1386 TAILQ_INIT(&xpt_busses);
1387 TAILQ_INIT(&cam_bioq);
1388 SLIST_INIT(&ccb_freeq);
1389 STAILQ_INIT(&highpowerq);
1390
1391 mtx_init(&cam_bioq_lock, "CAM BIOQ lock", NULL, MTX_DEF);
1392
1393 /*
1394 * The xpt layer is, itself, the equivelent of a SIM.
1395 * Allow 16 ccbs in the ccb pool for it. This should
1396 * give decent parallelism when we probe busses and
1397 * perform other XPT functions.
1398 */
1399 devq = cam_simq_alloc(16);
1400 xpt_sim = cam_sim_alloc(xptaction,
1401 xptpoll,
1402 "xpt",
1403 /*softc*/NULL,
1404 /*unit*/0,
1405 /*max_dev_transactions*/0,
1406 /*max_tagged_dev_transactions*/0,
1407 devq);
1408 xpt_max_ccbs = 16;
1409
1410 xpt_bus_register(xpt_sim, /*bus #*/0);
1411
1412 /*
1413 * Looking at the XPT from the SIM layer, the XPT is
1414 * the equivelent of a peripheral driver. Allocate
1415 * a peripheral driver entry for us.
1416 */
1417 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1418 CAM_TARGET_WILDCARD,
1419 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1420 printf("xpt_init: xpt_create_path failed with status %#x,"
1421 " failing attach\n", status);
1422 return;
1423 }
1424
1425 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1426 path, NULL, 0, NULL);
1427 xpt_free_path(path);
1428
1429 xpt_sim->softc = xpt_periph;
1430
1431 /*
1432 * Register a callback for when interrupts are enabled.
1433 */
1434 xpt_config_hook =
1435 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1436 M_TEMP, M_NOWAIT | M_ZERO);
1437 if (xpt_config_hook == NULL) {
1438 printf("xpt_init: Cannot malloc config hook "
1439 "- failing attach\n");
1440 return;
1441 }
1442
1443 xpt_config_hook->ich_func = xpt_config;
1444 if (config_intrhook_establish(xpt_config_hook) != 0) {
1445 free (xpt_config_hook, M_TEMP);
1446 printf("xpt_init: config_intrhook_establish failed "
1447 "- failing attach\n");
1448 }
1449
1450 /* Install our software interrupt handlers */
1451 swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, 0, &cambio_ih);
1452}
1453
1454static cam_status
1455xptregister(struct cam_periph *periph, void *arg)
1456{
1457 if (periph == NULL) {
1458 printf("xptregister: periph was NULL!!\n");
1459 return(CAM_REQ_CMP_ERR);
1460 }
1461
1462 periph->softc = NULL;
1463
1464 xpt_periph = periph;
1465
1466 return(CAM_REQ_CMP);
1467}
1468
1469int32_t
1470xpt_add_periph(struct cam_periph *periph)
1471{
1472 struct cam_ed *device;
1473 int32_t status;
1474 struct periph_list *periph_head;
1475
1476 GIANT_REQUIRED;
1477
1478 device = periph->path->device;
1479
1480 periph_head = &device->periphs;
1481
1482 status = CAM_REQ_CMP;
1483
1484 if (device != NULL) {
1485 int s;
1486
1487 /*
1488 * Make room for this peripheral
1489 * so it will fit in the queue
1490 * when it's scheduled to run
1491 */
1492 s = splsoftcam();
1493 status = camq_resize(&device->drvq,
1494 device->drvq.array_size + 1);
1495
1496 device->generation++;
1497
1498 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1499
1500 splx(s);
1501 }
1502
1503 xsoftc.generation++;
1504
1505 return (status);
1506}
1507
1508void
1509xpt_remove_periph(struct cam_periph *periph)
1510{
1511 struct cam_ed *device;
1512
1513 GIANT_REQUIRED;
1514
1515 device = periph->path->device;
1516
1517 if (device != NULL) {
1518 int s;
1519 struct periph_list *periph_head;
1520
1521 periph_head = &device->periphs;
1522
1523 /* Release the slot for this peripheral */
1524 s = splsoftcam();
1525 camq_resize(&device->drvq, device->drvq.array_size - 1);
1526
1527 device->generation++;
1528
1529 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1530
1531 splx(s);
1532 }
1533
1534 xsoftc.generation++;
1535
1536}
1537
1538#ifdef CAM_NEW_TRAN_CODE
1539
1540void
1541xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1542{
1543 struct ccb_pathinq cpi;
1544 struct ccb_trans_settings cts;
1545 struct cam_path *path;
1546 u_int speed;
1547 u_int freq;
1548 u_int mb;
1549 int s;
1550
1551 GIANT_REQUIRED;
1552
1553 path = periph->path;
1554 /*
1555 * To ensure that this is printed in one piece,
1556 * mask out CAM interrupts.
1557 */
1558 s = splsoftcam();
1559 printf("%s%d at %s%d bus %d target %d lun %d\n",
1560 periph->periph_name, periph->unit_number,
1561 path->bus->sim->sim_name,
1562 path->bus->sim->unit_number,
1563 path->bus->sim->bus_id,
1564 path->target->target_id,
1565 path->device->lun_id);
1566 printf("%s%d: ", periph->periph_name, periph->unit_number);
1567 scsi_print_inquiry(&path->device->inq_data);
1568 if (bootverbose && path->device->serial_num_len > 0) {
1569 /* Don't wrap the screen - print only the first 60 chars */
1570 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1571 periph->unit_number, path->device->serial_num);
1572 }
1573 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1574 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1575 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1576 xpt_action((union ccb*)&cts);
1577
1578 /* Ask the SIM for its base transfer speed */
1579 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1580 cpi.ccb_h.func_code = XPT_PATH_INQ;
1581 xpt_action((union ccb *)&cpi);
1582
1583 speed = cpi.base_transfer_speed;
1584 freq = 0;
1585 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1586 struct ccb_trans_settings_spi *spi;
1587
1588 spi = &cts.xport_specific.spi;
1589 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1590 && spi->sync_offset != 0) {
1591 freq = scsi_calc_syncsrate(spi->sync_period);
1592 speed = freq;
1593 }
1594
1595 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1596 speed *= (0x01 << spi->bus_width);
1597 }
1598
1599 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1600 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1601 if (fc->valid & CTS_FC_VALID_SPEED) {
1602 speed = fc->bitrate;
1603 }
1604 }
1605
1606 mb = speed / 1000;
1607 if (mb > 0)
1608 printf("%s%d: %d.%03dMB/s transfers",
1609 periph->periph_name, periph->unit_number,
1610 mb, speed % 1000);
1611 else
1612 printf("%s%d: %dKB/s transfers", periph->periph_name,
1613 periph->unit_number, speed);
1614 /* Report additional information about SPI connections */
1615 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1616 struct ccb_trans_settings_spi *spi;
1617
1618 spi = &cts.xport_specific.spi;
1619 if (freq != 0) {
1620 printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1621 freq % 1000,
1622 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1623 ? " DT" : "",
1624 spi->sync_offset);
1625 }
1626 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1627 && spi->bus_width > 0) {
1628 if (freq != 0) {
1629 printf(", ");
1630 } else {
1631 printf(" (");
1632 }
1633 printf("%dbit)", 8 * (0x01 << spi->bus_width));
1634 } else if (freq != 0) {
1635 printf(")");
1636 }
1637 }
1638 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1639 struct ccb_trans_settings_fc *fc;
1640
1641 fc = &cts.xport_specific.fc;
1642 if (fc->valid & CTS_FC_VALID_WWNN)
1643 printf(" WWNN 0x%llx", (long long) fc->wwnn);
1644 if (fc->valid & CTS_FC_VALID_WWPN)
1645 printf(" WWPN 0x%llx", (long long) fc->wwpn);
1646 if (fc->valid & CTS_FC_VALID_PORT)
1647 printf(" PortID 0x%x", fc->port);
1648 }
1649
1650 if (path->device->inq_flags & SID_CmdQue
1651 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1652 printf("\n%s%d: Tagged Queueing Enabled",
1653 periph->periph_name, periph->unit_number);
1654 }
1655 printf("\n");
1656
1657 /*
1658 * We only want to print the caller's announce string if they've
1659 * passed one in..
1660 */
1661 if (announce_string != NULL)
1662 printf("%s%d: %s\n", periph->periph_name,
1663 periph->unit_number, announce_string);
1664 splx(s);
1665}
1666#else /* CAM_NEW_TRAN_CODE */
1667void
1668xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1669{
1670 int s;
1671 u_int mb;
1672 struct cam_path *path;
1673 struct ccb_trans_settings cts;
1674
1675 GIANT_REQUIRED;
1676
1677 path = periph->path;
1678 /*
1679 * To ensure that this is printed in one piece,
1680 * mask out CAM interrupts.
1681 */
1682 s = splsoftcam();
1683 printf("%s%d at %s%d bus %d target %d lun %d\n",
1684 periph->periph_name, periph->unit_number,
1685 path->bus->sim->sim_name,
1686 path->bus->sim->unit_number,
1687 path->bus->sim->bus_id,
1688 path->target->target_id,
1689 path->device->lun_id);
1690 printf("%s%d: ", periph->periph_name, periph->unit_number);
1691 scsi_print_inquiry(&path->device->inq_data);
1692 if ((bootverbose)
1693 && (path->device->serial_num_len > 0)) {
1694 /* Don't wrap the screen - print only the first 60 chars */
1695 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1696 periph->unit_number, path->device->serial_num);
1697 }
1698 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1699 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1700 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1701 xpt_action((union ccb*)&cts);
1702 if (cts.ccb_h.status == CAM_REQ_CMP) {
1703 u_int speed;
1704 u_int freq;
1705
1706 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1707 && cts.sync_offset != 0) {
1708 freq = scsi_calc_syncsrate(cts.sync_period);
1709 speed = freq;
1710 } else {
1711 struct ccb_pathinq cpi;
1712
1713 /* Ask the SIM for its base transfer speed */
1714 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1715 cpi.ccb_h.func_code = XPT_PATH_INQ;
1716 xpt_action((union ccb *)&cpi);
1717
1718 speed = cpi.base_transfer_speed;
1719 freq = 0;
1720 }
1721 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1722 speed *= (0x01 << cts.bus_width);
1723 mb = speed / 1000;
1724 if (mb > 0)
1725 printf("%s%d: %d.%03dMB/s transfers",
1726 periph->periph_name, periph->unit_number,
1727 mb, speed % 1000);
1728 else
1729 printf("%s%d: %dKB/s transfers", periph->periph_name,
1730 periph->unit_number, speed);
1731 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1732 && cts.sync_offset != 0) {
1733 printf(" (%d.%03dMHz, offset %d", freq / 1000,
1734 freq % 1000, cts.sync_offset);
1735 }
1736 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1737 && cts.bus_width > 0) {
1738 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1739 && cts.sync_offset != 0) {
1740 printf(", ");
1741 } else {
1742 printf(" (");
1743 }
1744 printf("%dbit)", 8 * (0x01 << cts.bus_width));
1745 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1746 && cts.sync_offset != 0) {
1747 printf(")");
1748 }
1749
1750 if (path->device->inq_flags & SID_CmdQue
1751 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1752 printf(", Tagged Queueing Enabled");
1753 }
1754
1755 printf("\n");
1756 } else if (path->device->inq_flags & SID_CmdQue
1757 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1758 printf("%s%d: Tagged Queueing Enabled\n",
1759 periph->periph_name, periph->unit_number);
1760 }
1761
1762 /*
1763 * We only want to print the caller's announce string if they've
1764 * passed one in..
1765 */
1766 if (announce_string != NULL)
1767 printf("%s%d: %s\n", periph->periph_name,
1768 periph->unit_number, announce_string);
1769 splx(s);
1770}
1771
1772#endif /* CAM_NEW_TRAN_CODE */
1773
1774static dev_match_ret
1775xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1776 struct cam_eb *bus)
1777{
1778 dev_match_ret retval;
1779 int i;
1780
1781 retval = DM_RET_NONE;
1782
1783 /*
1784 * If we aren't given something to match against, that's an error.
1785 */
1786 if (bus == NULL)
1787 return(DM_RET_ERROR);
1788
1789 /*
1790 * If there are no match entries, then this bus matches no
1791 * matter what.
1792 */
1793 if ((patterns == NULL) || (num_patterns == 0))
1794 return(DM_RET_DESCEND | DM_RET_COPY);
1795
1796 for (i = 0; i < num_patterns; i++) {
1797 struct bus_match_pattern *cur_pattern;
1798
1799 /*
1800 * If the pattern in question isn't for a bus node, we
1801 * aren't interested. However, we do indicate to the
1802 * calling routine that we should continue descending the
1803 * tree, since the user wants to match against lower-level
1804 * EDT elements.
1805 */
1806 if (patterns[i].type != DEV_MATCH_BUS) {
1807 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1808 retval |= DM_RET_DESCEND;
1809 continue;
1810 }
1811
1812 cur_pattern = &patterns[i].pattern.bus_pattern;
1813
1814 /*
1815 * If they want to match any bus node, we give them any
1816 * device node.
1817 */
1818 if (cur_pattern->flags == BUS_MATCH_ANY) {
1819 /* set the copy flag */
1820 retval |= DM_RET_COPY;
1821
1822 /*
1823 * If we've already decided on an action, go ahead
1824 * and return.
1825 */
1826 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1827 return(retval);
1828 }
1829
1830 /*
1831 * Not sure why someone would do this...
1832 */
1833 if (cur_pattern->flags == BUS_MATCH_NONE)
1834 continue;
1835
1836 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1837 && (cur_pattern->path_id != bus->path_id))
1838 continue;
1839
1840 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1841 && (cur_pattern->bus_id != bus->sim->bus_id))
1842 continue;
1843
1844 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1845 && (cur_pattern->unit_number != bus->sim->unit_number))
1846 continue;
1847
1848 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1849 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1850 DEV_IDLEN) != 0))
1851 continue;
1852
1853 /*
1854 * If we get to this point, the user definitely wants
1855 * information on this bus. So tell the caller to copy the
1856 * data out.
1857 */
1858 retval |= DM_RET_COPY;
1859
1860 /*
1861 * If the return action has been set to descend, then we
1862 * know that we've already seen a non-bus matching
1863 * expression, therefore we need to further descend the tree.
1864 * This won't change by continuing around the loop, so we
1865 * go ahead and return. If we haven't seen a non-bus
1866 * matching expression, we keep going around the loop until
1867 * we exhaust the matching expressions. We'll set the stop
1868 * flag once we fall out of the loop.
1869 */
1870 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1871 return(retval);
1872 }
1873
1874 /*
1875 * If the return action hasn't been set to descend yet, that means
1876 * we haven't seen anything other than bus matching patterns. So
1877 * tell the caller to stop descending the tree -- the user doesn't
1878 * want to match against lower level tree elements.
1879 */
1880 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1881 retval |= DM_RET_STOP;
1882
1883 return(retval);
1884}
1885
1886static dev_match_ret
1887xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1888 struct cam_ed *device)
1889{
1890 dev_match_ret retval;
1891 int i;
1892
1893 retval = DM_RET_NONE;
1894
1895 /*
1896 * If we aren't given something to match against, that's an error.
1897 */
1898 if (device == NULL)
1899 return(DM_RET_ERROR);
1900
1901 /*
1902 * If there are no match entries, then this device matches no
1903 * matter what.
1904 */
1905 if ((patterns == NULL) || (num_patterns == 0))
1906 return(DM_RET_DESCEND | DM_RET_COPY);
1907
1908 for (i = 0; i < num_patterns; i++) {
1909 struct device_match_pattern *cur_pattern;
1910
1911 /*
1912 * If the pattern in question isn't for a device node, we
1913 * aren't interested.
1914 */
1915 if (patterns[i].type != DEV_MATCH_DEVICE) {
1916 if ((patterns[i].type == DEV_MATCH_PERIPH)
1917 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1918 retval |= DM_RET_DESCEND;
1919 continue;
1920 }
1921
1922 cur_pattern = &patterns[i].pattern.device_pattern;
1923
1924 /*
1925 * If they want to match any device node, we give them any
1926 * device node.
1927 */
1928 if (cur_pattern->flags == DEV_MATCH_ANY) {
1929 /* set the copy flag */
1930 retval |= DM_RET_COPY;
1931
1932
1933 /*
1934 * If we've already decided on an action, go ahead
1935 * and return.
1936 */
1937 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1938 return(retval);
1939 }
1940
1941 /*
1942 * Not sure why someone would do this...
1943 */
1944 if (cur_pattern->flags == DEV_MATCH_NONE)
1945 continue;
1946
1947 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1948 && (cur_pattern->path_id != device->target->bus->path_id))
1949 continue;
1950
1951 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1952 && (cur_pattern->target_id != device->target->target_id))
1953 continue;
1954
1955 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1956 && (cur_pattern->target_lun != device->lun_id))
1957 continue;
1958
1959 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1960 && (cam_quirkmatch((caddr_t)&device->inq_data,
1961 (caddr_t)&cur_pattern->inq_pat,
1962 1, sizeof(cur_pattern->inq_pat),
1963 scsi_static_inquiry_match) == NULL))
1964 continue;
1965
1966 /*
1967 * If we get to this point, the user definitely wants
1968 * information on this device. So tell the caller to copy
1969 * the data out.
1970 */
1971 retval |= DM_RET_COPY;
1972
1973 /*
1974 * If the return action has been set to descend, then we
1975 * know that we've already seen a peripheral matching
1976 * expression, therefore we need to further descend the tree.
1977 * This won't change by continuing around the loop, so we
1978 * go ahead and return. If we haven't seen a peripheral
1979 * matching expression, we keep going around the loop until
1980 * we exhaust the matching expressions. We'll set the stop
1981 * flag once we fall out of the loop.
1982 */
1983 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1984 return(retval);
1985 }
1986
1987 /*
1988 * If the return action hasn't been set to descend yet, that means
1989 * we haven't seen any peripheral matching patterns. So tell the
1990 * caller to stop descending the tree -- the user doesn't want to
1991 * match against lower level tree elements.
1992 */
1993 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1994 retval |= DM_RET_STOP;
1995
1996 return(retval);
1997}
1998
1999/*
2000 * Match a single peripheral against any number of match patterns.
2001 */
2002static dev_match_ret
2003xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
2004 struct cam_periph *periph)
2005{
2006 dev_match_ret retval;
2007 int i;
2008
2009 /*
2010 * If we aren't given something to match against, that's an error.
2011 */
2012 if (periph == NULL)
2013 return(DM_RET_ERROR);
2014
2015 /*
2016 * If there are no match entries, then this peripheral matches no
2017 * matter what.
2018 */
2019 if ((patterns == NULL) || (num_patterns == 0))
2020 return(DM_RET_STOP | DM_RET_COPY);
2021
2022 /*
2023 * There aren't any nodes below a peripheral node, so there's no
2024 * reason to descend the tree any further.
2025 */
2026 retval = DM_RET_STOP;
2027
2028 for (i = 0; i < num_patterns; i++) {
2029 struct periph_match_pattern *cur_pattern;
2030
2031 /*
2032 * If the pattern in question isn't for a peripheral, we
2033 * aren't interested.
2034 */
2035 if (patterns[i].type != DEV_MATCH_PERIPH)
2036 continue;
2037
2038 cur_pattern = &patterns[i].pattern.periph_pattern;
2039
2040 /*
2041 * If they want to match on anything, then we will do so.
2042 */
2043 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
2044 /* set the copy flag */
2045 retval |= DM_RET_COPY;
2046
2047 /*
2048 * We've already set the return action to stop,
2049 * since there are no nodes below peripherals in
2050 * the tree.
2051 */
2052 return(retval);
2053 }
2054
2055 /*
2056 * Not sure why someone would do this...
2057 */
2058 if (cur_pattern->flags == PERIPH_MATCH_NONE)
2059 continue;
2060
2061 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
2062 && (cur_pattern->path_id != periph->path->bus->path_id))
2063 continue;
2064
2065 /*
2066 * For the target and lun id's, we have to make sure the
2067 * target and lun pointers aren't NULL. The xpt peripheral
2068 * has a wildcard target and device.
2069 */
2070 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
2071 && ((periph->path->target == NULL)
2072 ||(cur_pattern->target_id != periph->path->target->target_id)))
2073 continue;
2074
2075 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
2076 && ((periph->path->device == NULL)
2077 || (cur_pattern->target_lun != periph->path->device->lun_id)))
2078 continue;
2079
2080 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
2081 && (cur_pattern->unit_number != periph->unit_number))
2082 continue;
2083
2084 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
2085 && (strncmp(cur_pattern->periph_name, periph->periph_name,
2086 DEV_IDLEN) != 0))
2087 continue;
2088
2089 /*
2090 * If we get to this point, the user definitely wants
2091 * information on this peripheral. So tell the caller to
2092 * copy the data out.
2093 */
2094 retval |= DM_RET_COPY;
2095
2096 /*
2097 * The return action has already been set to stop, since
2098 * peripherals don't have any nodes below them in the EDT.
2099 */
2100 return(retval);
2101 }
2102
2103 /*
2104 * If we get to this point, the peripheral that was passed in
2105 * doesn't match any of the patterns.
2106 */
2107 return(retval);
2108}
2109
2110static int
2111xptedtbusfunc(struct cam_eb *bus, void *arg)
2112{
2113 struct ccb_dev_match *cdm;
2114 dev_match_ret retval;
2115
2116 cdm = (struct ccb_dev_match *)arg;
2117
2118 /*
2119 * If our position is for something deeper in the tree, that means
2120 * that we've already seen this node. So, we keep going down.
2121 */
2122 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2123 && (cdm->pos.cookie.bus == bus)
2124 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2125 && (cdm->pos.cookie.target != NULL))
2126 retval = DM_RET_DESCEND;
2127 else
2128 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
2129
2130 /*
2131 * If we got an error, bail out of the search.
2132 */
2133 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2134 cdm->status = CAM_DEV_MATCH_ERROR;
2135 return(0);
2136 }
2137
2138 /*
2139 * If the copy flag is set, copy this bus out.
2140 */
2141 if (retval & DM_RET_COPY) {
2142 int spaceleft, j;
2143
2144 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2145 sizeof(struct dev_match_result));
2146
2147 /*
2148 * If we don't have enough space to put in another
2149 * match result, save our position and tell the
2150 * user there are more devices to check.
2151 */
2152 if (spaceleft < sizeof(struct dev_match_result)) {
2153 bzero(&cdm->pos, sizeof(cdm->pos));
2154 cdm->pos.position_type =
2155 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
2156
2157 cdm->pos.cookie.bus = bus;
2158 cdm->pos.generations[CAM_BUS_GENERATION]=
2159 bus_generation;
2160 cdm->status = CAM_DEV_MATCH_MORE;
2161 return(0);
2162 }
2163 j = cdm->num_matches;
2164 cdm->num_matches++;
2165 cdm->matches[j].type = DEV_MATCH_BUS;
2166 cdm->matches[j].result.bus_result.path_id = bus->path_id;
2167 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
2168 cdm->matches[j].result.bus_result.unit_number =
2169 bus->sim->unit_number;
2170 strncpy(cdm->matches[j].result.bus_result.dev_name,
2171 bus->sim->sim_name, DEV_IDLEN);
2172 }
2173
2174 /*
2175 * If the user is only interested in busses, there's no
2176 * reason to descend to the next level in the tree.
2177 */
2178 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2179 return(1);
2180
2181 /*
2182 * If there is a target generation recorded, check it to
2183 * make sure the target list hasn't changed.
2184 */
2185 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2186 && (bus == cdm->pos.cookie.bus)
2187 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2188 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
2189 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
2190 bus->generation)) {
2191 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2192 return(0);
2193 }
2194
2195 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2196 && (cdm->pos.cookie.bus == bus)
2197 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2198 && (cdm->pos.cookie.target != NULL))
2199 return(xpttargettraverse(bus,
2200 (struct cam_et *)cdm->pos.cookie.target,
2201 xptedttargetfunc, arg));
2202 else
2203 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2204}
2205
2206static int
2207xptedttargetfunc(struct cam_et *target, void *arg)
2208{
2209 struct ccb_dev_match *cdm;
2210
2211 cdm = (struct ccb_dev_match *)arg;
2212
2213 /*
2214 * If there is a device list generation recorded, check it to
2215 * make sure the device list hasn't changed.
2216 */
2217 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2218 && (cdm->pos.cookie.bus == target->bus)
2219 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2220 && (cdm->pos.cookie.target == target)
2221 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2222 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2223 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2224 target->generation)) {
2225 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2226 return(0);
2227 }
2228
2229 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2230 && (cdm->pos.cookie.bus == target->bus)
2231 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2232 && (cdm->pos.cookie.target == target)
2233 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2234 && (cdm->pos.cookie.device != NULL))
2235 return(xptdevicetraverse(target,
2236 (struct cam_ed *)cdm->pos.cookie.device,
2237 xptedtdevicefunc, arg));
2238 else
2239 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2240}
2241
2242static int
2243xptedtdevicefunc(struct cam_ed *device, void *arg)
2244{
2245
2246 struct ccb_dev_match *cdm;
2247 dev_match_ret retval;
2248
2249 cdm = (struct ccb_dev_match *)arg;
2250
2251 /*
2252 * If our position is for something deeper in the tree, that means
2253 * that we've already seen this node. So, we keep going down.
2254 */
2255 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2256 && (cdm->pos.cookie.device == device)
2257 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2258 && (cdm->pos.cookie.periph != NULL))
2259 retval = DM_RET_DESCEND;
2260 else
2261 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2262 device);
2263
2264 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2265 cdm->status = CAM_DEV_MATCH_ERROR;
2266 return(0);
2267 }
2268
2269 /*
2270 * If the copy flag is set, copy this device out.
2271 */
2272 if (retval & DM_RET_COPY) {
2273 int spaceleft, j;
2274
2275 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2276 sizeof(struct dev_match_result));
2277
2278 /*
2279 * If we don't have enough space to put in another
2280 * match result, save our position and tell the
2281 * user there are more devices to check.
2282 */
2283 if (spaceleft < sizeof(struct dev_match_result)) {
2284 bzero(&cdm->pos, sizeof(cdm->pos));
2285 cdm->pos.position_type =
2286 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2287 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2288
2289 cdm->pos.cookie.bus = device->target->bus;
2290 cdm->pos.generations[CAM_BUS_GENERATION]=
2291 bus_generation;
2292 cdm->pos.cookie.target = device->target;
2293 cdm->pos.generations[CAM_TARGET_GENERATION] =
2294 device->target->bus->generation;
2295 cdm->pos.cookie.device = device;
2296 cdm->pos.generations[CAM_DEV_GENERATION] =
2297 device->target->generation;
2298 cdm->status = CAM_DEV_MATCH_MORE;
2299 return(0);
2300 }
2301 j = cdm->num_matches;
2302 cdm->num_matches++;
2303 cdm->matches[j].type = DEV_MATCH_DEVICE;
2304 cdm->matches[j].result.device_result.path_id =
2305 device->target->bus->path_id;
2306 cdm->matches[j].result.device_result.target_id =
2307 device->target->target_id;
2308 cdm->matches[j].result.device_result.target_lun =
2309 device->lun_id;
2310 bcopy(&device->inq_data,
2311 &cdm->matches[j].result.device_result.inq_data,
2312 sizeof(struct scsi_inquiry_data));
2313
2314 /* Let the user know whether this device is unconfigured */
2315 if (device->flags & CAM_DEV_UNCONFIGURED)
2316 cdm->matches[j].result.device_result.flags =
2317 DEV_RESULT_UNCONFIGURED;
2318 else
2319 cdm->matches[j].result.device_result.flags =
2320 DEV_RESULT_NOFLAG;
2321 }
2322
2323 /*
2324 * If the user isn't interested in peripherals, don't descend
2325 * the tree any further.
2326 */
2327 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2328 return(1);
2329
2330 /*
2331 * If there is a peripheral list generation recorded, make sure
2332 * it hasn't changed.
2333 */
2334 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2335 && (device->target->bus == cdm->pos.cookie.bus)
2336 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2337 && (device->target == cdm->pos.cookie.target)
2338 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2339 && (device == cdm->pos.cookie.device)
2340 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2341 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2342 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2343 device->generation)){
2344 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2345 return(0);
2346 }
2347
2348 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2349 && (cdm->pos.cookie.bus == device->target->bus)
2350 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2351 && (cdm->pos.cookie.target == device->target)
2352 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2353 && (cdm->pos.cookie.device == device)
2354 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2355 && (cdm->pos.cookie.periph != NULL))
2356 return(xptperiphtraverse(device,
2357 (struct cam_periph *)cdm->pos.cookie.periph,
2358 xptedtperiphfunc, arg));
2359 else
2360 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2361}
2362
2363static int
2364xptedtperiphfunc(struct cam_periph *periph, void *arg)
2365{
2366 struct ccb_dev_match *cdm;
2367 dev_match_ret retval;
2368
2369 cdm = (struct ccb_dev_match *)arg;
2370
2371 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2372
2373 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2374 cdm->status = CAM_DEV_MATCH_ERROR;
2375 return(0);
2376 }
2377
2378 /*
2379 * If the copy flag is set, copy this peripheral out.
2380 */
2381 if (retval & DM_RET_COPY) {
2382 int spaceleft, j;
2383
2384 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2385 sizeof(struct dev_match_result));
2386
2387 /*
2388 * If we don't have enough space to put in another
2389 * match result, save our position and tell the
2390 * user there are more devices to check.
2391 */
2392 if (spaceleft < sizeof(struct dev_match_result)) {
2393 bzero(&cdm->pos, sizeof(cdm->pos));
2394 cdm->pos.position_type =
2395 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2396 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2397 CAM_DEV_POS_PERIPH;
2398
2399 cdm->pos.cookie.bus = periph->path->bus;
2400 cdm->pos.generations[CAM_BUS_GENERATION]=
2401 bus_generation;
2402 cdm->pos.cookie.target = periph->path->target;
2403 cdm->pos.generations[CAM_TARGET_GENERATION] =
2404 periph->path->bus->generation;
2405 cdm->pos.cookie.device = periph->path->device;
2406 cdm->pos.generations[CAM_DEV_GENERATION] =
2407 periph->path->target->generation;
2408 cdm->pos.cookie.periph = periph;
2409 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2410 periph->path->device->generation;
2411 cdm->status = CAM_DEV_MATCH_MORE;
2412 return(0);
2413 }
2414
2415 j = cdm->num_matches;
2416 cdm->num_matches++;
2417 cdm->matches[j].type = DEV_MATCH_PERIPH;
2418 cdm->matches[j].result.periph_result.path_id =
2419 periph->path->bus->path_id;
2420 cdm->matches[j].result.periph_result.target_id =
2421 periph->path->target->target_id;
2422 cdm->matches[j].result.periph_result.target_lun =
2423 periph->path->device->lun_id;
2424 cdm->matches[j].result.periph_result.unit_number =
2425 periph->unit_number;
2426 strncpy(cdm->matches[j].result.periph_result.periph_name,
2427 periph->periph_name, DEV_IDLEN);
2428 }
2429
2430 return(1);
2431}
2432
2433static int
2434xptedtmatch(struct ccb_dev_match *cdm)
2435{
2436 int ret;
2437
2438 cdm->num_matches = 0;
2439
2440 /*
2441 * Check the bus list generation. If it has changed, the user
2442 * needs to reset everything and start over.
2443 */
2444 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2445 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2446 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2447 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2448 return(0);
2449 }
2450
2451 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2452 && (cdm->pos.cookie.bus != NULL))
2453 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2454 xptedtbusfunc, cdm);
2455 else
2456 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2457
2458 /*
2459 * If we get back 0, that means that we had to stop before fully
2460 * traversing the EDT. It also means that one of the subroutines
2461 * has set the status field to the proper value. If we get back 1,
2462 * we've fully traversed the EDT and copied out any matching entries.
2463 */
2464 if (ret == 1)
2465 cdm->status = CAM_DEV_MATCH_LAST;
2466
2467 return(ret);
2468}
2469
2470static int
2471xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2472{
2473 struct ccb_dev_match *cdm;
2474
2475 cdm = (struct ccb_dev_match *)arg;
2476
2477 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2478 && (cdm->pos.cookie.pdrv == pdrv)
2479 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2480 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2481 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2482 (*pdrv)->generation)) {
2483 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2484 return(0);
2485 }
2486
2487 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2488 && (cdm->pos.cookie.pdrv == pdrv)
2489 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2490 && (cdm->pos.cookie.periph != NULL))
2491 return(xptpdperiphtraverse(pdrv,
2492 (struct cam_periph *)cdm->pos.cookie.periph,
2493 xptplistperiphfunc, arg));
2494 else
2495 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2496}
2497
2498static int
2499xptplistperiphfunc(struct cam_periph *periph, void *arg)
2500{
2501 struct ccb_dev_match *cdm;
2502 dev_match_ret retval;
2503
2504 cdm = (struct ccb_dev_match *)arg;
2505
2506 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2507
2508 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2509 cdm->status = CAM_DEV_MATCH_ERROR;
2510 return(0);
2511 }
2512
2513 /*
2514 * If the copy flag is set, copy this peripheral out.
2515 */
2516 if (retval & DM_RET_COPY) {
2517 int spaceleft, j;
2518
2519 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2520 sizeof(struct dev_match_result));
2521
2522 /*
2523 * If we don't have enough space to put in another
2524 * match result, save our position and tell the
2525 * user there are more devices to check.
2526 */
2527 if (spaceleft < sizeof(struct dev_match_result)) {
2528 struct periph_driver **pdrv;
2529
2530 pdrv = NULL;
2531 bzero(&cdm->pos, sizeof(cdm->pos));
2532 cdm->pos.position_type =
2533 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2534 CAM_DEV_POS_PERIPH;
2535
2536 /*
2537 * This may look a bit non-sensical, but it is
2538 * actually quite logical. There are very few
2539 * peripheral drivers, and bloating every peripheral
2540 * structure with a pointer back to its parent
2541 * peripheral driver linker set entry would cost
2542 * more in the long run than doing this quick lookup.
2543 */
2544 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2545 if (strcmp((*pdrv)->driver_name,
2546 periph->periph_name) == 0)
2547 break;
2548 }
2549
2550 if (*pdrv == NULL) {
2551 cdm->status = CAM_DEV_MATCH_ERROR;
2552 return(0);
2553 }
2554
2555 cdm->pos.cookie.pdrv = pdrv;
2556 /*
2557 * The periph generation slot does double duty, as
2558 * does the periph pointer slot. They are used for
2559 * both edt and pdrv lookups and positioning.
2560 */
2561 cdm->pos.cookie.periph = periph;
2562 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2563 (*pdrv)->generation;
2564 cdm->status = CAM_DEV_MATCH_MORE;
2565 return(0);
2566 }
2567
2568 j = cdm->num_matches;
2569 cdm->num_matches++;
2570 cdm->matches[j].type = DEV_MATCH_PERIPH;
2571 cdm->matches[j].result.periph_result.path_id =
2572 periph->path->bus->path_id;
2573
2574 /*
2575 * The transport layer peripheral doesn't have a target or
2576 * lun.
2577 */
2578 if (periph->path->target)
2579 cdm->matches[j].result.periph_result.target_id =
2580 periph->path->target->target_id;
2581 else
2582 cdm->matches[j].result.periph_result.target_id = -1;
2583
2584 if (periph->path->device)
2585 cdm->matches[j].result.periph_result.target_lun =
2586 periph->path->device->lun_id;
2587 else
2588 cdm->matches[j].result.periph_result.target_lun = -1;
2589
2590 cdm->matches[j].result.periph_result.unit_number =
2591 periph->unit_number;
2592 strncpy(cdm->matches[j].result.periph_result.periph_name,
2593 periph->periph_name, DEV_IDLEN);
2594 }
2595
2596 return(1);
2597}
2598
2599static int
2600xptperiphlistmatch(struct ccb_dev_match *cdm)
2601{
2602 int ret;
2603
2604 cdm->num_matches = 0;
2605
2606 /*
2607 * At this point in the edt traversal function, we check the bus
2608 * list generation to make sure that no busses have been added or
2609 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2610 * For the peripheral driver list traversal function, however, we
2611 * don't have to worry about new peripheral driver types coming or
2612 * going; they're in a linker set, and therefore can't change
2613 * without a recompile.
2614 */
2615
2616 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2617 && (cdm->pos.cookie.pdrv != NULL))
2618 ret = xptpdrvtraverse(
2619 (struct periph_driver **)cdm->pos.cookie.pdrv,
2620 xptplistpdrvfunc, cdm);
2621 else
2622 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2623
2624 /*
2625 * If we get back 0, that means that we had to stop before fully
2626 * traversing the peripheral driver tree. It also means that one of
2627 * the subroutines has set the status field to the proper value. If
2628 * we get back 1, we've fully traversed the EDT and copied out any
2629 * matching entries.
2630 */
2631 if (ret == 1)
2632 cdm->status = CAM_DEV_MATCH_LAST;
2633
2634 return(ret);
2635}
2636
2637static int
2638xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2639{
2640 struct cam_eb *bus, *next_bus;
2641 int retval;
2642
2643 retval = 1;
2644
2645 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2646 bus != NULL;
2647 bus = next_bus) {
2648 next_bus = TAILQ_NEXT(bus, links);
2649
2650 retval = tr_func(bus, arg);
2651 if (retval == 0)
2652 return(retval);
2653 }
2654
2655 return(retval);
2656}
2657
2658static int
2659xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2660 xpt_targetfunc_t *tr_func, void *arg)
2661{
2662 struct cam_et *target, *next_target;
2663 int retval;
2664
2665 retval = 1;
2666 for (target = (start_target ? start_target :
2667 TAILQ_FIRST(&bus->et_entries));
2668 target != NULL; target = next_target) {
2669
2670 next_target = TAILQ_NEXT(target, links);
2671
2672 retval = tr_func(target, arg);
2673
2674 if (retval == 0)
2675 return(retval);
2676 }
2677
2678 return(retval);
2679}
2680
2681static int
2682xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2683 xpt_devicefunc_t *tr_func, void *arg)
2684{
2685 struct cam_ed *device, *next_device;
2686 int retval;
2687
2688 retval = 1;
2689 for (device = (start_device ? start_device :
2690 TAILQ_FIRST(&target->ed_entries));
2691 device != NULL;
2692 device = next_device) {
2693
2694 next_device = TAILQ_NEXT(device, links);
2695
2696 retval = tr_func(device, arg);
2697
2698 if (retval == 0)
2699 return(retval);
2700 }
2701
2702 return(retval);
2703}
2704
2705static int
2706xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2707 xpt_periphfunc_t *tr_func, void *arg)
2708{
2709 struct cam_periph *periph, *next_periph;
2710 int retval;
2711
2712 retval = 1;
2713
2714 for (periph = (start_periph ? start_periph :
2715 SLIST_FIRST(&device->periphs));
2716 periph != NULL;
2717 periph = next_periph) {
2718
2719 next_periph = SLIST_NEXT(periph, periph_links);
2720
2721 retval = tr_func(periph, arg);
2722 if (retval == 0)
2723 return(retval);
2724 }
2725
2726 return(retval);
2727}
2728
2729static int
2730xptpdrvtraverse(struct periph_driver **start_pdrv,
2731 xpt_pdrvfunc_t *tr_func, void *arg)
2732{
2733 struct periph_driver **pdrv;
2734 int retval;
2735
2736 retval = 1;
2737
2738 /*
2739 * We don't traverse the peripheral driver list like we do the
2740 * other lists, because it is a linker set, and therefore cannot be
2741 * changed during runtime. If the peripheral driver list is ever
2742 * re-done to be something other than a linker set (i.e. it can
2743 * change while the system is running), the list traversal should
2744 * be modified to work like the other traversal functions.
2745 */
2746 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2747 *pdrv != NULL; pdrv++) {
2748 retval = tr_func(pdrv, arg);
2749
2750 if (retval == 0)
2751 return(retval);
2752 }
2753
2754 return(retval);
2755}
2756
2757static int
2758xptpdperiphtraverse(struct periph_driver **pdrv,
2759 struct cam_periph *start_periph,
2760 xpt_periphfunc_t *tr_func, void *arg)
2761{
2762 struct cam_periph *periph, *next_periph;
2763 int retval;
2764
2765 retval = 1;
2766
2767 for (periph = (start_periph ? start_periph :
2768 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2769 periph = next_periph) {
2770
2771 next_periph = TAILQ_NEXT(periph, unit_links);
2772
2773 retval = tr_func(periph, arg);
2774 if (retval == 0)
2775 return(retval);
2776 }
2777 return(retval);
2778}
2779
2780static int
2781xptdefbusfunc(struct cam_eb *bus, void *arg)
2782{
2783 struct xpt_traverse_config *tr_config;
2784
2785 tr_config = (struct xpt_traverse_config *)arg;
2786
2787 if (tr_config->depth == XPT_DEPTH_BUS) {
2788 xpt_busfunc_t *tr_func;
2789
2790 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2791
2792 return(tr_func(bus, tr_config->tr_arg));
2793 } else
2794 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2795}
2796
2797static int
2798xptdeftargetfunc(struct cam_et *target, void *arg)
2799{
2800 struct xpt_traverse_config *tr_config;
2801
2802 tr_config = (struct xpt_traverse_config *)arg;
2803
2804 if (tr_config->depth == XPT_DEPTH_TARGET) {
2805 xpt_targetfunc_t *tr_func;
2806
2807 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2808
2809 return(tr_func(target, tr_config->tr_arg));
2810 } else
2811 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2812}
2813
2814static int
2815xptdefdevicefunc(struct cam_ed *device, void *arg)
2816{
2817 struct xpt_traverse_config *tr_config;
2818
2819 tr_config = (struct xpt_traverse_config *)arg;
2820
2821 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2822 xpt_devicefunc_t *tr_func;
2823
2824 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2825
2826 return(tr_func(device, tr_config->tr_arg));
2827 } else
2828 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2829}
2830
2831static int
2832xptdefperiphfunc(struct cam_periph *periph, void *arg)
2833{
2834 struct xpt_traverse_config *tr_config;
2835 xpt_periphfunc_t *tr_func;
2836
2837 tr_config = (struct xpt_traverse_config *)arg;
2838
2839 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2840
2841 /*
2842 * Unlike the other default functions, we don't check for depth
2843 * here. The peripheral driver level is the last level in the EDT,
2844 * so if we're here, we should execute the function in question.
2845 */
2846 return(tr_func(periph, tr_config->tr_arg));
2847}
2848
2849/*
2850 * Execute the given function for every bus in the EDT.
2851 */
2852static int
2853xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2854{
2855 struct xpt_traverse_config tr_config;
2856
2857 tr_config.depth = XPT_DEPTH_BUS;
2858 tr_config.tr_func = tr_func;
2859 tr_config.tr_arg = arg;
2860
2861 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2862}
2863
2864#ifdef notusedyet
2865/*
2866 * Execute the given function for every target in the EDT.
2867 */
2868static int
2869xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2870{
2871 struct xpt_traverse_config tr_config;
2872
2873 tr_config.depth = XPT_DEPTH_TARGET;
2874 tr_config.tr_func = tr_func;
2875 tr_config.tr_arg = arg;
2876
2877 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2878}
2879#endif /* notusedyet */
2880
2881/*
2882 * Execute the given function for every device in the EDT.
2883 */
2884static int
2885xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2886{
2887 struct xpt_traverse_config tr_config;
2888
2889 tr_config.depth = XPT_DEPTH_DEVICE;
2890 tr_config.tr_func = tr_func;
2891 tr_config.tr_arg = arg;
2892
2893 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2894}
2895
2896#ifdef notusedyet
2897/*
2898 * Execute the given function for every peripheral in the EDT.
2899 */
2900static int
2901xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2902{
2903 struct xpt_traverse_config tr_config;
2904
2905 tr_config.depth = XPT_DEPTH_PERIPH;
2906 tr_config.tr_func = tr_func;
2907 tr_config.tr_arg = arg;
2908
2909 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2910}
2911#endif /* notusedyet */
2912
2913static int
2914xptsetasyncfunc(struct cam_ed *device, void *arg)
2915{
2916 struct cam_path path;
2917 struct ccb_getdev cgd;
2918 struct async_node *cur_entry;
2919
2920 cur_entry = (struct async_node *)arg;
2921
2922 /*
2923 * Don't report unconfigured devices (Wildcard devs,
2924 * devices only for target mode, device instances
2925 * that have been invalidated but are waiting for
2926 * their last reference count to be released).
2927 */
2928 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2929 return (1);
2930
2931 xpt_compile_path(&path,
2932 NULL,
2933 device->target->bus->path_id,
2934 device->target->target_id,
2935 device->lun_id);
2936 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2937 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2938 xpt_action((union ccb *)&cgd);
2939 cur_entry->callback(cur_entry->callback_arg,
2940 AC_FOUND_DEVICE,
2941 &path, &cgd);
2942 xpt_release_path(&path);
2943
2944 return(1);
2945}
2946
2947static int
2948xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2949{
2950 struct cam_path path;
2951 struct ccb_pathinq cpi;
2952 struct async_node *cur_entry;
2953
2954 cur_entry = (struct async_node *)arg;
2955
2956 xpt_compile_path(&path, /*periph*/NULL,
2957 bus->sim->path_id,
2958 CAM_TARGET_WILDCARD,
2959 CAM_LUN_WILDCARD);
2960 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2961 cpi.ccb_h.func_code = XPT_PATH_INQ;
2962 xpt_action((union ccb *)&cpi);
2963 cur_entry->callback(cur_entry->callback_arg,
2964 AC_PATH_REGISTERED,
2965 &path, &cpi);
2966 xpt_release_path(&path);
2967
2968 return(1);
2969}
2970
2971void
2972xpt_action(union ccb *start_ccb)
2973{
2974 int iopl;
2975
2976 GIANT_REQUIRED;
2977
2978 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2979
2980 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2981
2982 iopl = splsoftcam();
2983 switch (start_ccb->ccb_h.func_code) {
2984 case XPT_SCSI_IO:
2985 {
2986#ifdef CAM_NEW_TRAN_CODE
2987 struct cam_ed *device;
2988#endif /* CAM_NEW_TRAN_CODE */
2989#ifdef CAMDEBUG
2990 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2991 struct cam_path *path;
2992
2993 path = start_ccb->ccb_h.path;
2994#endif
2995
2996 /*
2997 * For the sake of compatibility with SCSI-1
2998 * devices that may not understand the identify
2999 * message, we include lun information in the
3000 * second byte of all commands. SCSI-1 specifies
3001 * that luns are a 3 bit value and reserves only 3
3002 * bits for lun information in the CDB. Later
3003 * revisions of the SCSI spec allow for more than 8
3004 * luns, but have deprecated lun information in the
3005 * CDB. So, if the lun won't fit, we must omit.
3006 *
3007 * Also be aware that during initial probing for devices,
3008 * the inquiry information is unknown but initialized to 0.
3009 * This means that this code will be exercised while probing
3010 * devices with an ANSI revision greater than 2.
3011 */
3012#ifdef CAM_NEW_TRAN_CODE
3013 device = start_ccb->ccb_h.path->device;
3014 if (device->protocol_version <= SCSI_REV_2
3015#else /* CAM_NEW_TRAN_CODE */
3016 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
3017#endif /* CAM_NEW_TRAN_CODE */
3018 && start_ccb->ccb_h.target_lun < 8
3019 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
3020
3021 start_ccb->csio.cdb_io.cdb_bytes[1] |=
3022 start_ccb->ccb_h.target_lun << 5;
3023 }
3024 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
3025 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
3026 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
3027 &path->device->inq_data),
3028 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
3029 cdb_str, sizeof(cdb_str))));
3030 }
3031 /* FALLTHROUGH */
3032 case XPT_TARGET_IO:
3033 case XPT_CONT_TARGET_IO:
3034 start_ccb->csio.sense_resid = 0;
3035 start_ccb->csio.resid = 0;
3036 /* FALLTHROUGH */
3037 case XPT_RESET_DEV:
3038 case XPT_ENG_EXEC:
3039 {
3040 struct cam_path *path;
3041 int s;
3042 int runq;
3043
3044 path = start_ccb->ccb_h.path;
3045 s = splsoftcam();
3046
3047 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
3048 if (path->device->qfrozen_cnt == 0)
3049 runq = xpt_schedule_dev_sendq(path->bus, path->device);
3050 else
3051 runq = 0;
3052 splx(s);
3053 if (runq != 0)
3054 xpt_run_dev_sendq(path->bus);
3055 break;
3056 }
3057 case XPT_SET_TRAN_SETTINGS:
3058 {
3059 xpt_set_transfer_settings(&start_ccb->cts,
3060 start_ccb->ccb_h.path->device,
3061 /*async_update*/FALSE);
3062 break;
3063 }
3064 case XPT_CALC_GEOMETRY:
3065 {
3066 struct cam_sim *sim;
3067
3068 /* Filter out garbage */
3069 if (start_ccb->ccg.block_size == 0
3070 || start_ccb->ccg.volume_size == 0) {
3071 start_ccb->ccg.cylinders = 0;
3072 start_ccb->ccg.heads = 0;
3073 start_ccb->ccg.secs_per_track = 0;
3074 start_ccb->ccb_h.status = CAM_REQ_CMP;
3075 break;
3076 }
3077#ifdef PC98
3078 /*
3079 * In a PC-98 system, geometry translation depens on
3080 * the "real" device geometry obtained from mode page 4.
3081 * SCSI geometry translation is performed in the
3082 * initialization routine of the SCSI BIOS and the result
3083 * stored in host memory. If the translation is available
3084 * in host memory, use it. If not, rely on the default
3085 * translation the device driver performs.
3086 */
3087 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
3088 start_ccb->ccb_h.status = CAM_REQ_CMP;
3089 break;
3090 }
3091#endif
3092 sim = start_ccb->ccb_h.path->bus->sim;
3093 (*(sim->sim_action))(sim, start_ccb);
3094 break;
3095 }
3096 case XPT_ABORT:
3097 {
3098 union ccb* abort_ccb;
3099 int s;
3100
3101 abort_ccb = start_ccb->cab.abort_ccb;
3102 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
3103
3104 if (abort_ccb->ccb_h.pinfo.index >= 0) {
3105 struct cam_ccbq *ccbq;
3106
3107 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
3108 cam_ccbq_remove_ccb(ccbq, abort_ccb);
3109 abort_ccb->ccb_h.status =
3110 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3111 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3112 s = splcam();
3113 xpt_done(abort_ccb);
3114 splx(s);
3115 start_ccb->ccb_h.status = CAM_REQ_CMP;
3116 break;
3117 }
3118 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
3119 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
3120 /*
3121 * We've caught this ccb en route to
3122 * the SIM. Flag it for abort and the
3123 * SIM will do so just before starting
3124 * real work on the CCB.
3125 */
3126 abort_ccb->ccb_h.status =
3127 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3128 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3129 start_ccb->ccb_h.status = CAM_REQ_CMP;
3130 break;
3131 }
3132 }
3133 if (XPT_FC_IS_QUEUED(abort_ccb)
3134 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
3135 /*
3136 * It's already completed but waiting
3137 * for our SWI to get to it.
3138 */
3139 start_ccb->ccb_h.status = CAM_UA_ABORT;
3140 break;
3141 }
3142 /*
3143 * If we weren't able to take care of the abort request
3144 * in the XPT, pass the request down to the SIM for processing.
3145 */
3146 }
3147 /* FALLTHROUGH */
3148 case XPT_ACCEPT_TARGET_IO:
3149 case XPT_EN_LUN:
3150 case XPT_IMMED_NOTIFY:
3151 case XPT_NOTIFY_ACK:
3152 case XPT_GET_TRAN_SETTINGS:
3153 case XPT_RESET_BUS:
3154 {
3155 struct cam_sim *sim;
3156
3157 sim = start_ccb->ccb_h.path->bus->sim;
3158 (*(sim->sim_action))(sim, start_ccb);
3159 break;
3160 }
3161 case XPT_PATH_INQ:
3162 {
3163 struct cam_sim *sim;
3164
3165 sim = start_ccb->ccb_h.path->bus->sim;
3166 (*(sim->sim_action))(sim, start_ccb);
3167 break;
3168 }
3169 case XPT_PATH_STATS:
3170 start_ccb->cpis.last_reset =
3171 start_ccb->ccb_h.path->bus->last_reset;
3172 start_ccb->ccb_h.status = CAM_REQ_CMP;
3173 break;
3174 case XPT_GDEV_TYPE:
3175 {
3176 struct cam_ed *dev;
3177 int s;
3178
3179 dev = start_ccb->ccb_h.path->device;
3180 s = splcam();
3181 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3182 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3183 } else {
3184 struct ccb_getdev *cgd;
3185 struct cam_eb *bus;
3186 struct cam_et *tar;
3187
3188 cgd = &start_ccb->cgd;
3189 bus = cgd->ccb_h.path->bus;
3190 tar = cgd->ccb_h.path->target;
3191 cgd->inq_data = dev->inq_data;
3192 cgd->ccb_h.status = CAM_REQ_CMP;
3193 cgd->serial_num_len = dev->serial_num_len;
3194 if ((dev->serial_num_len > 0)
3195 && (dev->serial_num != NULL))
3196 bcopy(dev->serial_num, cgd->serial_num,
3197 dev->serial_num_len);
3198 }
3199 splx(s);
3200 break;
3201 }
3202 case XPT_GDEV_STATS:
3203 {
3204 struct cam_ed *dev;
3205 int s;
3206
3207 dev = start_ccb->ccb_h.path->device;
3208 s = splcam();
3209 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3210 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3211 } else {
3212 struct ccb_getdevstats *cgds;
3213 struct cam_eb *bus;
3214 struct cam_et *tar;
3215
3216 cgds = &start_ccb->cgds;
3217 bus = cgds->ccb_h.path->bus;
3218 tar = cgds->ccb_h.path->target;
3219 cgds->dev_openings = dev->ccbq.dev_openings;
3220 cgds->dev_active = dev->ccbq.dev_active;
3221 cgds->devq_openings = dev->ccbq.devq_openings;
3222 cgds->devq_queued = dev->ccbq.queue.entries;
3223 cgds->held = dev->ccbq.held;
3224 cgds->last_reset = tar->last_reset;
3225 cgds->maxtags = dev->quirk->maxtags;
3226 cgds->mintags = dev->quirk->mintags;
3227 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3228 cgds->last_reset = bus->last_reset;
3229 cgds->ccb_h.status = CAM_REQ_CMP;
3230 }
3231 splx(s);
3232 break;
3233 }
3234 case XPT_GDEVLIST:
3235 {
3236 struct cam_periph *nperiph;
3237 struct periph_list *periph_head;
3238 struct ccb_getdevlist *cgdl;
3239 u_int i;
3240 int s;
3241 struct cam_ed *device;
3242 int found;
3243
3244
3245 found = 0;
3246
3247 /*
3248 * Don't want anyone mucking with our data.
3249 */
3250 s = splcam();
3251 device = start_ccb->ccb_h.path->device;
3252 periph_head = &device->periphs;
3253 cgdl = &start_ccb->cgdl;
3254
3255 /*
3256 * Check and see if the list has changed since the user
3257 * last requested a list member. If so, tell them that the
3258 * list has changed, and therefore they need to start over
3259 * from the beginning.
3260 */
3261 if ((cgdl->index != 0) &&
3262 (cgdl->generation != device->generation)) {
3263 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3264 splx(s);
3265 break;
3266 }
3267
3268 /*
3269 * Traverse the list of peripherals and attempt to find
3270 * the requested peripheral.
3271 */
3272 for (nperiph = SLIST_FIRST(periph_head), i = 0;
3273 (nperiph != NULL) && (i <= cgdl->index);
3274 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3275 if (i == cgdl->index) {
3276 strncpy(cgdl->periph_name,
3277 nperiph->periph_name,
3278 DEV_IDLEN);
3279 cgdl->unit_number = nperiph->unit_number;
3280 found = 1;
3281 }
3282 }
3283 if (found == 0) {
3284 cgdl->status = CAM_GDEVLIST_ERROR;
3285 splx(s);
3286 break;
3287 }
3288
3289 if (nperiph == NULL)
3290 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3291 else
3292 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3293
3294 cgdl->index++;
3295 cgdl->generation = device->generation;
3296
3297 splx(s);
3298 cgdl->ccb_h.status = CAM_REQ_CMP;
3299 break;
3300 }
3301 case XPT_DEV_MATCH:
3302 {
3303 int s;
3304 dev_pos_type position_type;
3305 struct ccb_dev_match *cdm;
3306
3307 cdm = &start_ccb->cdm;
3308
3309 /*
3310 * Prevent EDT changes while we traverse it.
3311 */
3312 s = splcam();
3313 /*
3314 * There are two ways of getting at information in the EDT.
3315 * The first way is via the primary EDT tree. It starts
3316 * with a list of busses, then a list of targets on a bus,
3317 * then devices/luns on a target, and then peripherals on a
3318 * device/lun. The "other" way is by the peripheral driver
3319 * lists. The peripheral driver lists are organized by
3320 * peripheral driver. (obviously) So it makes sense to
3321 * use the peripheral driver list if the user is looking
3322 * for something like "da1", or all "da" devices. If the
3323 * user is looking for something on a particular bus/target
3324 * or lun, it's generally better to go through the EDT tree.
3325 */
3326
3327 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3328 position_type = cdm->pos.position_type;
3329 else {
3330 u_int i;
3331
3332 position_type = CAM_DEV_POS_NONE;
3333
3334 for (i = 0; i < cdm->num_patterns; i++) {
3335 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3336 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3337 position_type = CAM_DEV_POS_EDT;
3338 break;
3339 }
3340 }
3341
3342 if (cdm->num_patterns == 0)
3343 position_type = CAM_DEV_POS_EDT;
3344 else if (position_type == CAM_DEV_POS_NONE)
3345 position_type = CAM_DEV_POS_PDRV;
3346 }
3347
3348 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3349 case CAM_DEV_POS_EDT:
3350 xptedtmatch(cdm);
3351 break;
3352 case CAM_DEV_POS_PDRV:
3353 xptperiphlistmatch(cdm);
3354 break;
3355 default:
3356 cdm->status = CAM_DEV_MATCH_ERROR;
3357 break;
3358 }
3359
3360 splx(s);
3361
3362 if (cdm->status == CAM_DEV_MATCH_ERROR)
3363 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3364 else
3365 start_ccb->ccb_h.status = CAM_REQ_CMP;
3366
3367 break;
3368 }
3369 case XPT_SASYNC_CB:
3370 {
3371 struct ccb_setasync *csa;
3372 struct async_node *cur_entry;
3373 struct async_list *async_head;
3374 u_int32_t added;
3375 int s;
3376
3377 csa = &start_ccb->csa;
3378 added = csa->event_enable;
3379 async_head = &csa->ccb_h.path->device->asyncs;
3380
3381 /*
3382 * If there is already an entry for us, simply
3383 * update it.
3384 */
3385 s = splcam();
3386 cur_entry = SLIST_FIRST(async_head);
3387 while (cur_entry != NULL) {
3388 if ((cur_entry->callback_arg == csa->callback_arg)
3389 && (cur_entry->callback == csa->callback))
3390 break;
3391 cur_entry = SLIST_NEXT(cur_entry, links);
3392 }
3393
3394 if (cur_entry != NULL) {
3395 /*
3396 * If the request has no flags set,
3397 * remove the entry.
3398 */
3399 added &= ~cur_entry->event_enable;
3400 if (csa->event_enable == 0) {
3401 SLIST_REMOVE(async_head, cur_entry,
3402 async_node, links);
3403 csa->ccb_h.path->device->refcount--;
3404 free(cur_entry, M_DEVBUF);
3405 } else {
3406 cur_entry->event_enable = csa->event_enable;
3407 }
3408 } else {
3409 cur_entry = malloc(sizeof(*cur_entry), M_DEVBUF,
3410 M_NOWAIT);
3411 if (cur_entry == NULL) {
3412 splx(s);
3413 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3414 break;
3415 }
3416 cur_entry->event_enable = csa->event_enable;
3417 cur_entry->callback_arg = csa->callback_arg;
3418 cur_entry->callback = csa->callback;
3419 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3420 csa->ccb_h.path->device->refcount++;
3421 }
3422
3423 if ((added & AC_FOUND_DEVICE) != 0) {
3424 /*
3425 * Get this peripheral up to date with all
3426 * the currently existing devices.
3427 */
3428 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3429 }
3430 if ((added & AC_PATH_REGISTERED) != 0) {
3431 /*
3432 * Get this peripheral up to date with all
3433 * the currently existing busses.
3434 */
3435 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3436 }
3437 splx(s);
3438 start_ccb->ccb_h.status = CAM_REQ_CMP;
3439 break;
3440 }
3441 case XPT_REL_SIMQ:
3442 {
3443 struct ccb_relsim *crs;
3444 struct cam_ed *dev;
3445 int s;
3446
3447 crs = &start_ccb->crs;
3448 dev = crs->ccb_h.path->device;
3449 if (dev == NULL) {
3450
3451 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3452 break;
3453 }
3454
3455 s = splcam();
3456
3457 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3458
3459 if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3460
3461 /* Don't ever go below one opening */
3462 if (crs->openings > 0) {
3463 xpt_dev_ccbq_resize(crs->ccb_h.path,
3464 crs->openings);
3465
3466 if (bootverbose) {
3467 xpt_print_path(crs->ccb_h.path);
3468 printf("tagged openings "
3469 "now %d\n",
3470 crs->openings);
3471 }
3472 }
3473 }
3474 }
3475
3476 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3477
3478 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3479
3480 /*
3481 * Just extend the old timeout and decrement
3482 * the freeze count so that a single timeout
3483 * is sufficient for releasing the queue.
3484 */
3485 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3486 untimeout(xpt_release_devq_timeout,
3487 dev, dev->c_handle);
3488 } else {
3489
3490 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3491 }
3492
3493 dev->c_handle =
3494 timeout(xpt_release_devq_timeout,
3495 dev,
3496 (crs->release_timeout * hz) / 1000);
3497
3498 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3499
3500 }
3501
3502 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3503
3504 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3505 /*
3506 * Decrement the freeze count so that a single
3507 * completion is still sufficient to unfreeze
3508 * the queue.
3509 */
3510 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3511 } else {
3512
3513 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3514 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3515 }
3516 }
3517
3518 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3519
3520 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3521 || (dev->ccbq.dev_active == 0)) {
3522
3523 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3524 } else {
3525
3526 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3527 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3528 }
3529 }
3530 splx(s);
3531
3532 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3533
3534 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3535 /*run_queue*/TRUE);
3536 }
3537 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3538 start_ccb->ccb_h.status = CAM_REQ_CMP;
3539 break;
3540 }
3541 case XPT_SCAN_BUS:
3542 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3543 break;
3544 case XPT_SCAN_LUN:
3545 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3546 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3547 start_ccb);
3548 break;
3549 case XPT_DEBUG: {
3550#ifdef CAMDEBUG
3551 int s;
3552
3553 s = splcam();
3554#ifdef CAM_DEBUG_DELAY
3555 cam_debug_delay = CAM_DEBUG_DELAY;
3556#endif
3557 cam_dflags = start_ccb->cdbg.flags;
3558 if (cam_dpath != NULL) {
3559 xpt_free_path(cam_dpath);
3560 cam_dpath = NULL;
3561 }
3562
3563 if (cam_dflags != CAM_DEBUG_NONE) {
3564 if (xpt_create_path(&cam_dpath, xpt_periph,
3565 start_ccb->ccb_h.path_id,
3566 start_ccb->ccb_h.target_id,
3567 start_ccb->ccb_h.target_lun) !=
3568 CAM_REQ_CMP) {
3569 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3570 cam_dflags = CAM_DEBUG_NONE;
3571 } else {
3572 start_ccb->ccb_h.status = CAM_REQ_CMP;
3573 xpt_print_path(cam_dpath);
3574 printf("debugging flags now %x\n", cam_dflags);
3575 }
3576 } else {
3577 cam_dpath = NULL;
3578 start_ccb->ccb_h.status = CAM_REQ_CMP;
3579 }
3580 splx(s);
3581#else /* !CAMDEBUG */
3582 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3583#endif /* CAMDEBUG */
3584 break;
3585 }
3586 case XPT_NOOP:
3587 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3588 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3589 start_ccb->ccb_h.status = CAM_REQ_CMP;
3590 break;
3591 default:
3592 case XPT_SDEV_TYPE:
3593 case XPT_TERM_IO:
3594 case XPT_ENG_INQ:
3595 /* XXX Implement */
3596 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3597 break;
3598 }
3599 splx(iopl);
3600}
3601
3602void
3603xpt_polled_action(union ccb *start_ccb)
3604{
3605 int s;
3606 u_int32_t timeout;
3607 struct cam_sim *sim;
3608 struct cam_devq *devq;
3609 struct cam_ed *dev;
3610
3611 GIANT_REQUIRED;
3612
3613 timeout = start_ccb->ccb_h.timeout;
3614 sim = start_ccb->ccb_h.path->bus->sim;
3615 devq = sim->devq;
3616 dev = start_ccb->ccb_h.path->device;
3617
3618 s = splcam();
3619
3620 /*
3621 * Steal an opening so that no other queued requests
3622 * can get it before us while we simulate interrupts.
3623 */
3624 dev->ccbq.devq_openings--;
3625 dev->ccbq.dev_openings--;
3626
3627 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3628 && (--timeout > 0)) {
3629 DELAY(1000);
3630 (*(sim->sim_poll))(sim);
3631 camisr(&cam_bioq);
3632 }
3633
3634 dev->ccbq.devq_openings++;
3635 dev->ccbq.dev_openings++;
3636
3637 if (timeout != 0) {
3638 xpt_action(start_ccb);
3639 while(--timeout > 0) {
3640 (*(sim->sim_poll))(sim);
3641 camisr(&cam_bioq);
3642 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3643 != CAM_REQ_INPROG)
3644 break;
3645 DELAY(1000);
3646 }
3647 if (timeout == 0) {
3648 /*
3649 * XXX Is it worth adding a sim_timeout entry
3650 * point so we can attempt recovery? If
3651 * this is only used for dumps, I don't think
3652 * it is.
3653 */
3654 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3655 }
3656 } else {
3657 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3658 }
3659 splx(s);
3660}
3661
3662/*
3663 * Schedule a peripheral driver to receive a ccb when it's
3664 * target device has space for more transactions.
3665 */
3666void
3667xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3668{
3669 struct cam_ed *device;
3670 int s;
3671 int runq;
3672
3673 GIANT_REQUIRED;
3674
3675 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3676 device = perph->path->device;
3677 s = splsoftcam();
3678 if (periph_is_queued(perph)) {
3679 /* Simply reorder based on new priority */
3680 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3681 (" change priority to %d\n", new_priority));
3682 if (new_priority < perph->pinfo.priority) {
3683 camq_change_priority(&device->drvq,
3684 perph->pinfo.index,
3685 new_priority);
3686 }
3687 runq = 0;
3688 } else {
3689 /* New entry on the queue */
3690 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3691 (" added periph to queue\n"));
3692 perph->pinfo.priority = new_priority;
3693 perph->pinfo.generation = ++device->drvq.generation;
3694 camq_insert(&device->drvq, &perph->pinfo);
3695 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3696 }
3697 splx(s);
3698 if (runq != 0) {
3699 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3700 (" calling xpt_run_devq\n"));
3701 xpt_run_dev_allocq(perph->path->bus);
3702 }
3703}
3704
3705
3706/*
3707 * Schedule a device to run on a given queue.
3708 * If the device was inserted as a new entry on the queue,
3709 * return 1 meaning the device queue should be run. If we
3710 * were already queued, implying someone else has already
3711 * started the queue, return 0 so the caller doesn't attempt
3712 * to run the queue. Must be run at either splsoftcam
3713 * (or splcam since that encompases splsoftcam).
3714 */
3715static int
3716xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3717 u_int32_t new_priority)
3718{
3719 int retval;
3720 u_int32_t old_priority;
3721
3722 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3723
3724 old_priority = pinfo->priority;
3725
3726 /*
3727 * Are we already queued?
3728 */
3729 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3730 /* Simply reorder based on new priority */
3731 if (new_priority < old_priority) {
3732 camq_change_priority(queue, pinfo->index,
3733 new_priority);
3734 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3735 ("changed priority to %d\n",
3736 new_priority));
3737 }
3738 retval = 0;
3739 } else {
3740 /* New entry on the queue */
3741 if (new_priority < old_priority)
3742 pinfo->priority = new_priority;
3743
3744 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3745 ("Inserting onto queue\n"));
3746 pinfo->generation = ++queue->generation;
3747 camq_insert(queue, pinfo);
3748 retval = 1;
3749 }
3750 return (retval);
3751}
3752
3753static void
3754xpt_run_dev_allocq(struct cam_eb *bus)
3755{
3756 struct cam_devq *devq;
3757 int s;
3758
3759 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3760 devq = bus->sim->devq;
3761
3762 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3763 (" qfrozen_cnt == 0x%x, entries == %d, "
3764 "openings == %d, active == %d\n",
3765 devq->alloc_queue.qfrozen_cnt,
3766 devq->alloc_queue.entries,
3767 devq->alloc_openings,
3768 devq->alloc_active));
3769
3770 s = splsoftcam();
3771 devq->alloc_queue.qfrozen_cnt++;
3772 while ((devq->alloc_queue.entries > 0)
3773 && (devq->alloc_openings > 0)
3774 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3775 struct cam_ed_qinfo *qinfo;
3776 struct cam_ed *device;
3777 union ccb *work_ccb;
3778 struct cam_periph *drv;
3779 struct camq *drvq;
3780
3781 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3782 CAMQ_HEAD);
3783 device = qinfo->device;
3784
3785 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3786 ("running device %p\n", device));
3787
3788 drvq = &device->drvq;
3789
3790#ifdef CAMDEBUG
3791 if (drvq->entries <= 0) {
3792 panic("xpt_run_dev_allocq: "
3793 "Device on queue without any work to do");
3794 }
3795#endif
3796 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3797 devq->alloc_openings--;
3798 devq->alloc_active++;
3799 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3800 splx(s);
3801 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3802 drv->pinfo.priority);
3803 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3804 ("calling periph start\n"));
3805 drv->periph_start(drv, work_ccb);
3806 } else {
3807 /*
3808 * Malloc failure in alloc_ccb
3809 */
3810 /*
3811 * XXX add us to a list to be run from free_ccb
3812 * if we don't have any ccbs active on this
3813 * device queue otherwise we may never get run
3814 * again.
3815 */
3816 break;
3817 }
3818
3819 /* Raise IPL for possible insertion and test at top of loop */
3820 s = splsoftcam();
3821
3822 if (drvq->entries > 0) {
3823 /* We have more work. Attempt to reschedule */
3824 xpt_schedule_dev_allocq(bus, device);
3825 }
3826 }
3827 devq->alloc_queue.qfrozen_cnt--;
3828 splx(s);
3829}
3830
3831static void
3832xpt_run_dev_sendq(struct cam_eb *bus)
3833{
3834 struct cam_devq *devq;
3835 int s;
3836
3837 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3838
3839 devq = bus->sim->devq;
3840
3841 s = splcam();
3842 devq->send_queue.qfrozen_cnt++;
3843 splx(s);
3844 s = splsoftcam();
3845 while ((devq->send_queue.entries > 0)
3846 && (devq->send_openings > 0)) {
3847 struct cam_ed_qinfo *qinfo;
3848 struct cam_ed *device;
3849 union ccb *work_ccb;
3850 struct cam_sim *sim;
3851 int ospl;
3852
3853 ospl = splcam();
3854 if (devq->send_queue.qfrozen_cnt > 1) {
3855 splx(ospl);
3856 break;
3857 }
3858
3859 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3860 CAMQ_HEAD);
3861 device = qinfo->device;
3862
3863 /*
3864 * If the device has been "frozen", don't attempt
3865 * to run it.
3866 */
3867 if (device->qfrozen_cnt > 0) {
3868 splx(ospl);
3869 continue;
3870 }
3871
3872 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3873 ("running device %p\n", device));
3874
3875 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3876 if (work_ccb == NULL) {
3877 printf("device on run queue with no ccbs???\n");
3878 splx(ospl);
3879 continue;
3880 }
3881
3882 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3883
3884 if (num_highpower <= 0) {
3885 /*
3886 * We got a high power command, but we
3887 * don't have any available slots. Freeze
3888 * the device queue until we have a slot
3889 * available.
3890 */
3891 device->qfrozen_cnt++;
3892 STAILQ_INSERT_TAIL(&highpowerq,
3893 &work_ccb->ccb_h,
3894 xpt_links.stqe);
3895
3896 splx(ospl);
3897 continue;
3898 } else {
3899 /*
3900 * Consume a high power slot while
3901 * this ccb runs.
3902 */
3903 num_highpower--;
3904 }
3905 }
3906 devq->active_dev = device;
3907 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3908
3909 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3910 splx(ospl);
3911
3912 devq->send_openings--;
3913 devq->send_active++;
3914
3915 if (device->ccbq.queue.entries > 0)
3916 xpt_schedule_dev_sendq(bus, device);
3917
3918 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3919 /*
3920 * The client wants to freeze the queue
3921 * after this CCB is sent.
3922 */
3923 ospl = splcam();
3924 device->qfrozen_cnt++;
3925 splx(ospl);
3926 }
3927
3928 splx(s);
3929
3930 /* In Target mode, the peripheral driver knows best... */
3931 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3932 if ((device->inq_flags & SID_CmdQue) != 0
3933 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3934 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3935 else
3936 /*
3937 * Clear this in case of a retried CCB that
3938 * failed due to a rejected tag.
3939 */
3940 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3941 }
3942
3943 /*
3944 * Device queues can be shared among multiple sim instances
3945 * that reside on different busses. Use the SIM in the queue
3946 * CCB's path, rather than the one in the bus that was passed
3947 * into this function.
3948 */
3949 sim = work_ccb->ccb_h.path->bus->sim;
3950 (*(sim->sim_action))(sim, work_ccb);
3951
3952 ospl = splcam();
3953 devq->active_dev = NULL;
3954 splx(ospl);
3955 /* Raise IPL for possible insertion and test at top of loop */
3956 s = splsoftcam();
3957 }
3958 splx(s);
3959 s = splcam();
3960 devq->send_queue.qfrozen_cnt--;
3961 splx(s);
3962}
3963
3964/*
3965 * This function merges stuff from the slave ccb into the master ccb, while
3966 * keeping important fields in the master ccb constant.
3967 */
3968void
3969xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3970{
3971 GIANT_REQUIRED;
3972
3973 /*
3974 * Pull fields that are valid for peripheral drivers to set
3975 * into the master CCB along with the CCB "payload".
3976 */
3977 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3978 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3979 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3980 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3981 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3982 sizeof(union ccb) - sizeof(struct ccb_hdr));
3983}
3984
3985void
3986xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3987{
3988 GIANT_REQUIRED;
3989
3990 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3991 ccb_h->pinfo.priority = priority;
3992 ccb_h->path = path;
3993 ccb_h->path_id = path->bus->path_id;
3994 if (path->target)
3995 ccb_h->target_id = path->target->target_id;
3996 else
3997 ccb_h->target_id = CAM_TARGET_WILDCARD;
3998 if (path->device) {
3999 ccb_h->target_lun = path->device->lun_id;
4000 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
4001 } else {
4002 ccb_h->target_lun = CAM_TARGET_WILDCARD;
4003 }
4004 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4005 ccb_h->flags = 0;
4006}
4007
4008/* Path manipulation functions */
4009cam_status
4010xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
4011 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4012{
4013 struct cam_path *path;
4014 cam_status status;
4015
4016 GIANT_REQUIRED;
4017
4018 path = (struct cam_path *)malloc(sizeof(*path), M_DEVBUF, M_NOWAIT);
4019
4020 if (path == NULL) {
4021 status = CAM_RESRC_UNAVAIL;
4022 return(status);
4023 }
4024 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
4025 if (status != CAM_REQ_CMP) {
4026 free(path, M_DEVBUF);
4027 path = NULL;
4028 }
4029 *new_path_ptr = path;
4030 return (status);
4031}
4032
4033static cam_status
4034xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
4035 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4036{
4037 struct cam_eb *bus;
4038 struct cam_et *target;
4039 struct cam_ed *device;
4040 cam_status status;
4041 int s;
4042
4043 status = CAM_REQ_CMP; /* Completed without error */
4044 target = NULL; /* Wildcarded */
4045 device = NULL; /* Wildcarded */
4046
4047 /*
4048 * We will potentially modify the EDT, so block interrupts
4049 * that may attempt to create cam paths.
4050 */
4051 s = splcam();
4052 bus = xpt_find_bus(path_id);
4053 if (bus == NULL) {
4054 status = CAM_PATH_INVALID;
4055 } else {
4056 target = xpt_find_target(bus, target_id);
4057 if (target == NULL) {
4058 /* Create one */
4059 struct cam_et *new_target;
4060
4061 new_target = xpt_alloc_target(bus, target_id);
4062 if (new_target == NULL) {
4063 status = CAM_RESRC_UNAVAIL;
4064 } else {
4065 target = new_target;
4066 }
4067 }
4068 if (target != NULL) {
4069 device = xpt_find_device(target, lun_id);
4070 if (device == NULL) {
4071 /* Create one */
4072 struct cam_ed *new_device;
4073
4074 new_device = xpt_alloc_device(bus,
4075 target,
4076 lun_id);
4077 if (new_device == NULL) {
4078 status = CAM_RESRC_UNAVAIL;
4079 } else {
4080 device = new_device;
4081 }
4082 }
4083 }
4084 }
4085 splx(s);
4086
4087 /*
4088 * Only touch the user's data if we are successful.
4089 */
4090 if (status == CAM_REQ_CMP) {
4091 new_path->periph = perph;
4092 new_path->bus = bus;
4093 new_path->target = target;
4094 new_path->device = device;
4095 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
4096 } else {
4097 if (device != NULL)
4098 xpt_release_device(bus, target, device);
4099 if (target != NULL)
4100 xpt_release_target(bus, target);
4101 if (bus != NULL)
4102 xpt_release_bus(bus);
4103 }
4104 return (status);
4105}
4106
4107static void
4108xpt_release_path(struct cam_path *path)
4109{
4110 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
4111 if (path->device != NULL) {
4112 xpt_release_device(path->bus, path->target, path->device);
4113 path->device = NULL;
4114 }
4115 if (path->target != NULL) {
4116 xpt_release_target(path->bus, path->target);
4117 path->target = NULL;
4118 }
4119 if (path->bus != NULL) {
4120 xpt_release_bus(path->bus);
4121 path->bus = NULL;
4122 }
4123}
4124
4125void
4126xpt_free_path(struct cam_path *path)
4127{
4128 GIANT_REQUIRED;
4129
4130 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
4131 xpt_release_path(path);
4132 free(path, M_DEVBUF);
4133}
4134
4135
4136/*
4137 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4138 * in path1, 2 for match with wildcards in path2.
4139 */
4140int
4141xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
4142{
4143 GIANT_REQUIRED;
4144
4145 int retval = 0;
4146
4147 if (path1->bus != path2->bus) {
4148 if (path1->bus->path_id == CAM_BUS_WILDCARD)
4149 retval = 1;
4150 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4151 retval = 2;
4152 else
4153 return (-1);
4154 }
4155 if (path1->target != path2->target) {
4156 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4157 if (retval == 0)
4158 retval = 1;
4159 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4160 retval = 2;
4161 else
4162 return (-1);
4163 }
4164 if (path1->device != path2->device) {
4165 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4166 if (retval == 0)
4167 retval = 1;
4168 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4169 retval = 2;
4170 else
4171 return (-1);
4172 }
4173 return (retval);
4174}
4175
4176void
4177xpt_print_path(struct cam_path *path)
4178{
4179 GIANT_REQUIRED;
4180
4181 if (path == NULL)
4182 printf("(nopath): ");
4183 else {
4184 if (path->periph != NULL)
4185 printf("(%s%d:", path->periph->periph_name,
4186 path->periph->unit_number);
4187 else
4188 printf("(noperiph:");
4189
4190 if (path->bus != NULL)
4191 printf("%s%d:%d:", path->bus->sim->sim_name,
4192 path->bus->sim->unit_number,
4193 path->bus->sim->bus_id);
4194 else
4195 printf("nobus:");
4196
4197 if (path->target != NULL)
4198 printf("%d:", path->target->target_id);
4199 else
4200 printf("X:");
4201
4202 if (path->device != NULL)
4203 printf("%d): ", path->device->lun_id);
4204 else
4205 printf("X): ");
4206 }
4207}
4208
4209int
4210xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4211{
4212 struct sbuf sb;
4213
4214 GIANT_REQUIRED;
4215
4216 sbuf_new(&sb, str, str_len, 0);
4217
4218 if (path == NULL)
4219 sbuf_printf(&sb, "(nopath): ");
4220 else {
4221 if (path->periph != NULL)
4222 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4223 path->periph->unit_number);
4224 else
4225 sbuf_printf(&sb, "(noperiph:");
4226
4227 if (path->bus != NULL)
4228 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
4229 path->bus->sim->unit_number,
4230 path->bus->sim->bus_id);
4231 else
4232 sbuf_printf(&sb, "nobus:");
4233
4234 if (path->target != NULL)
4235 sbuf_printf(&sb, "%d:", path->target->target_id);
4236 else
4237 sbuf_printf(&sb, "X:");
4238
4239 if (path->device != NULL)
4240 sbuf_printf(&sb, "%d): ", path->device->lun_id);
4241 else
4242 sbuf_printf(&sb, "X): ");
4243 }
4244 sbuf_finish(&sb);
4245
4246 return(sbuf_len(&sb));
4247}
4248
4249path_id_t
4250xpt_path_path_id(struct cam_path *path)
4251{
4252 GIANT_REQUIRED;
4253
4254 return(path->bus->path_id);
4255}
4256
4257target_id_t
4258xpt_path_target_id(struct cam_path *path)
4259{
4260 GIANT_REQUIRED;
4261
4262 if (path->target != NULL)
4263 return (path->target->target_id);
4264 else
4265 return (CAM_TARGET_WILDCARD);
4266}
4267
4268lun_id_t
4269xpt_path_lun_id(struct cam_path *path)
4270{
4271 GIANT_REQUIRED;
4272
4273 if (path->device != NULL)
4274 return (path->device->lun_id);
4275 else
4276 return (CAM_LUN_WILDCARD);
4277}
4278
4279struct cam_sim *
4280xpt_path_sim(struct cam_path *path)
4281{
4282 GIANT_REQUIRED;
4283
4284 return (path->bus->sim);
4285}
4286
4287struct cam_periph*
4288xpt_path_periph(struct cam_path *path)
4289{
4290 GIANT_REQUIRED;
4291
4292 return (path->periph);
4293}
4294
4295/*
4296 * Release a CAM control block for the caller. Remit the cost of the structure
4297 * to the device referenced by the path. If the this device had no 'credits'
4298 * and peripheral drivers have registered async callbacks for this notification
4299 * call them now.
4300 */
4301void
4302xpt_release_ccb(union ccb *free_ccb)
4303{
4304 int s;
4305 struct cam_path *path;
4306 struct cam_ed *device;
4307 struct cam_eb *bus;
4308
4309 GIANT_REQUIRED;
4310
4311 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4312 path = free_ccb->ccb_h.path;
4313 device = path->device;
4314 bus = path->bus;
4315 s = splsoftcam();
4316 cam_ccbq_release_opening(&device->ccbq);
4317 if (xpt_ccb_count > xpt_max_ccbs) {
4318 xpt_free_ccb(free_ccb);
4319 xpt_ccb_count--;
4320 } else {
4321 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4322 }
4323 bus->sim->devq->alloc_openings++;
4324 bus->sim->devq->alloc_active--;
4325 /* XXX Turn this into an inline function - xpt_run_device?? */
4326 if ((device_is_alloc_queued(device) == 0)
4327 && (device->drvq.entries > 0)) {
4328 xpt_schedule_dev_allocq(bus, device);
4329 }
4330 splx(s);
4331 if (dev_allocq_is_runnable(bus->sim->devq))
4332 xpt_run_dev_allocq(bus);
4333}
4334
4335/* Functions accessed by SIM drivers */
4336
4337/*
4338 * A sim structure, listing the SIM entry points and instance
4339 * identification info is passed to xpt_bus_register to hook the SIM
4340 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4341 * for this new bus and places it in the array of busses and assigns
4342 * it a path_id. The path_id may be influenced by "hard wiring"
4343 * information specified by the user. Once interrupt services are
4344 * availible, the bus will be probed.
4345 */
4346int32_t
4347xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4348{
4349 struct cam_eb *new_bus;
4350 struct cam_eb *old_bus;
4351 struct ccb_pathinq cpi;
4352 int s;
4353
4354 GIANT_REQUIRED;
4355
4356 sim->bus_id = bus;
4357 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4358 M_DEVBUF, M_NOWAIT);
4359 if (new_bus == NULL) {
4360 /* Couldn't satisfy request */
4361 return (CAM_RESRC_UNAVAIL);
4362 }
4363
4364 if (strcmp(sim->sim_name, "xpt") != 0) {
4365
4366 sim->path_id =
4367 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4368 }
4369
4370 TAILQ_INIT(&new_bus->et_entries);
4371 new_bus->path_id = sim->path_id;
4372 new_bus->sim = sim;
4373 timevalclear(&new_bus->last_reset);
4374 new_bus->flags = 0;
4375 new_bus->refcount = 1; /* Held until a bus_deregister event */
4376 new_bus->generation = 0;
4377 s = splcam();
4378 old_bus = TAILQ_FIRST(&xpt_busses);
4379 while (old_bus != NULL
4380 && old_bus->path_id < new_bus->path_id)
4381 old_bus = TAILQ_NEXT(old_bus, links);
4382 if (old_bus != NULL)
4383 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4384 else
4385 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4386 bus_generation++;
4387 splx(s);
4388
4389 /* Notify interested parties */
4390 if (sim->path_id != CAM_XPT_PATH_ID) {
4391 struct cam_path path;
4392
4393 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4394 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4395 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4396 cpi.ccb_h.func_code = XPT_PATH_INQ;
4397 xpt_action((union ccb *)&cpi);
4398 xpt_async(AC_PATH_REGISTERED, &path, &cpi);
4399 xpt_release_path(&path);
4400 }
4401 return (CAM_SUCCESS);
4402}
4403
4404int32_t
4405xpt_bus_deregister(path_id_t pathid)
4406{
4407 struct cam_path bus_path;
4408 cam_status status;
4409
4410 GIANT_REQUIRED;
4411
4412 status = xpt_compile_path(&bus_path, NULL, pathid,
4413 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4414 if (status != CAM_REQ_CMP)
4415 return (status);
4416
4417 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4418 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4419
4420 /* Release the reference count held while registered. */
4421 xpt_release_bus(bus_path.bus);
4422 xpt_release_path(&bus_path);
4423
4424 return (CAM_REQ_CMP);
4425}
4426
4427static path_id_t
4428xptnextfreepathid(void)
4429{
4430 struct cam_eb *bus;
4431 path_id_t pathid;
4432 const char *strval;
4433
4434 pathid = 0;
4435 bus = TAILQ_FIRST(&xpt_busses);
4436retry:
4437 /* Find an unoccupied pathid */
4438 while (bus != NULL
4439 && bus->path_id <= pathid) {
4440 if (bus->path_id == pathid)
4441 pathid++;
4442 bus = TAILQ_NEXT(bus, links);
4443 }
4444
4445 /*
4446 * Ensure that this pathid is not reserved for
4447 * a bus that may be registered in the future.
4448 */
4449 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4450 ++pathid;
4451 /* Start the search over */
4452 goto retry;
4453 }
4454 return (pathid);
4455}
4456
4457static path_id_t
4458xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4459{
4460 path_id_t pathid;
4461 int i, dunit, val;
4462 char buf[32];
4463 const char *dname;
4464
4465 pathid = CAM_XPT_PATH_ID;
4466 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4467 i = 0;
4468 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4469 if (strcmp(dname, "scbus")) {
4470 /* Avoid a bit of foot shooting. */
4471 continue;
4472 }
4473 if (dunit < 0) /* unwired?! */
4474 continue;
4475 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4476 if (sim_bus == val) {
4477 pathid = dunit;
4478 break;
4479 }
4480 } else if (sim_bus == 0) {
4481 /* Unspecified matches bus 0 */
4482 pathid = dunit;
4483 break;
4484 } else {
4485 printf("Ambiguous scbus configuration for %s%d "
4486 "bus %d, cannot wire down. The kernel "
4487 "config entry for scbus%d should "
4488 "specify a controller bus.\n"
4489 "Scbus will be assigned dynamically.\n",
4490 sim_name, sim_unit, sim_bus, dunit);
4491 break;
4492 }
4493 }
4494
4495 if (pathid == CAM_XPT_PATH_ID)
4496 pathid = xptnextfreepathid();
4497 return (pathid);
4498}
4499
4500void
4501xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4502{
4503 struct cam_eb *bus;
4504 struct cam_et *target, *next_target;
4505 struct cam_ed *device, *next_device;
4506 int s;
4507
4508 GIANT_REQUIRED;
4509
4510 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4511
4512 /*
4513 * Most async events come from a CAM interrupt context. In
4514 * a few cases, the error recovery code at the peripheral layer,
4515 * which may run from our SWI or a process context, may signal
4516 * deferred events with a call to xpt_async. Ensure async
4517 * notifications are serialized by blocking cam interrupts.
4518 */
4519 s = splcam();
4520
4521 bus = path->bus;
4522
4523 if (async_code == AC_BUS_RESET) {
4524 int s;
4525
4526 s = splclock();
4527 /* Update our notion of when the last reset occurred */
4528 microtime(&bus->last_reset);
4529 splx(s);
4530 }
4531
4532 for (target = TAILQ_FIRST(&bus->et_entries);
4533 target != NULL;
4534 target = next_target) {
4535
4536 next_target = TAILQ_NEXT(target, links);
4537
4538 if (path->target != target
4539 && path->target->target_id != CAM_TARGET_WILDCARD
4540 && target->target_id != CAM_TARGET_WILDCARD)
4541 continue;
4542
4543 if (async_code == AC_SENT_BDR) {
4544 int s;
4545
4546 /* Update our notion of when the last reset occurred */
4547 s = splclock();
4548 microtime(&path->target->last_reset);
4549 splx(s);
4550 }
4551
4552 for (device = TAILQ_FIRST(&target->ed_entries);
4553 device != NULL;
4554 device = next_device) {
4555
4556 next_device = TAILQ_NEXT(device, links);
4557
4558 if (path->device != device
4559 && path->device->lun_id != CAM_LUN_WILDCARD
4560 && device->lun_id != CAM_LUN_WILDCARD)
4561 continue;
4562
4563 xpt_dev_async(async_code, bus, target,
4564 device, async_arg);
4565
4566 xpt_async_bcast(&device->asyncs, async_code,
4567 path, async_arg);
4568 }
4569 }
4570
4571 /*
4572 * If this wasn't a fully wildcarded async, tell all
4573 * clients that want all async events.
4574 */
4575 if (bus != xpt_periph->path->bus)
4576 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4577 path, async_arg);
4578 splx(s);
4579}
4580
4581static void
4582xpt_async_bcast(struct async_list *async_head,
4583 u_int32_t async_code,
4584 struct cam_path *path, void *async_arg)
4585{
4586 struct async_node *cur_entry;
4587
4588 cur_entry = SLIST_FIRST(async_head);
4589 while (cur_entry != NULL) {
4590 struct async_node *next_entry;
4591 /*
4592 * Grab the next list entry before we call the current
4593 * entry's callback. This is because the callback function
4594 * can delete its async callback entry.
4595 */
4596 next_entry = SLIST_NEXT(cur_entry, links);
4597 if ((cur_entry->event_enable & async_code) != 0)
4598 cur_entry->callback(cur_entry->callback_arg,
4599 async_code, path,
4600 async_arg);
4601 cur_entry = next_entry;
4602 }
4603}
4604
4605/*
4606 * Handle any per-device event notifications that require action by the XPT.
4607 */
4608static void
4609xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4610 struct cam_ed *device, void *async_arg)
4611{
4612 cam_status status;
4613 struct cam_path newpath;
4614
4615 /*
4616 * We only need to handle events for real devices.
4617 */
4618 if (target->target_id == CAM_TARGET_WILDCARD
4619 || device->lun_id == CAM_LUN_WILDCARD)
4620 return;
4621
4622 /*
4623 * We need our own path with wildcards expanded to
4624 * handle certain types of events.
4625 */
4626 if ((async_code == AC_SENT_BDR)
4627 || (async_code == AC_BUS_RESET)
4628 || (async_code == AC_INQ_CHANGED))
4629 status = xpt_compile_path(&newpath, NULL,
4630 bus->path_id,
4631 target->target_id,
4632 device->lun_id);
4633 else
4634 status = CAM_REQ_CMP_ERR;
4635
4636 if (status == CAM_REQ_CMP) {
4637
4638 /*
4639 * Allow transfer negotiation to occur in a
4640 * tag free environment.
4641 */
4642 if (async_code == AC_SENT_BDR
4643 || async_code == AC_BUS_RESET)
4644 xpt_toggle_tags(&newpath);
4645
4646 if (async_code == AC_INQ_CHANGED) {
4647 /*
4648 * We've sent a start unit command, or
4649 * something similar to a device that
4650 * may have caused its inquiry data to
4651 * change. So we re-scan the device to
4652 * refresh the inquiry data for it.
4653 */
4654 xpt_scan_lun(newpath.periph, &newpath,
4655 CAM_EXPECT_INQ_CHANGE, NULL);
4656 }
4657 xpt_release_path(&newpath);
4658 } else if (async_code == AC_LOST_DEVICE) {
4659 device->flags |= CAM_DEV_UNCONFIGURED;
4660 } else if (async_code == AC_TRANSFER_NEG) {
4661 struct ccb_trans_settings *settings;
4662
4663 settings = (struct ccb_trans_settings *)async_arg;
4664 xpt_set_transfer_settings(settings, device,
4665 /*async_update*/TRUE);
4666 }
4667}
4668
4669u_int32_t
4670xpt_freeze_devq(struct cam_path *path, u_int count)
4671{
4672 int s;
4673 struct ccb_hdr *ccbh;
4674
4675 GIANT_REQUIRED;
4676
4677 s = splcam();
4678 path->device->qfrozen_cnt += count;
4679
4680 /*
4681 * Mark the last CCB in the queue as needing
4682 * to be requeued if the driver hasn't
4683 * changed it's state yet. This fixes a race
4684 * where a ccb is just about to be queued to
4685 * a controller driver when it's interrupt routine
4686 * freezes the queue. To completly close the
4687 * hole, controller drives must check to see
4688 * if a ccb's status is still CAM_REQ_INPROG
4689 * under spl protection just before they queue
4690 * the CCB. See ahc_action/ahc_freeze_devq for
4691 * an example.
4692 */
4693 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4694 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4695 ccbh->status = CAM_REQUEUE_REQ;
4696 splx(s);
4697 return (path->device->qfrozen_cnt);
4698}
4699
4700u_int32_t
4701xpt_freeze_simq(struct cam_sim *sim, u_int count)
4702{
4703 GIANT_REQUIRED;
4704
4705 sim->devq->send_queue.qfrozen_cnt += count;
4706 if (sim->devq->active_dev != NULL) {
4707 struct ccb_hdr *ccbh;
4708
4709 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4710 ccb_hdr_tailq);
4711 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4712 ccbh->status = CAM_REQUEUE_REQ;
4713 }
4714 return (sim->devq->send_queue.qfrozen_cnt);
4715}
4716
4717static void
4718xpt_release_devq_timeout(void *arg)
4719{
4720 struct cam_ed *device;
4721
4722 device = (struct cam_ed *)arg;
4723
4724 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4725}
4726
4727void
4728xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4729{
4730 GIANT_REQUIRED;
4731
4732 xpt_release_devq_device(path->device, count, run_queue);
4733}
4734
4735static void
4736xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4737{
4738 int rundevq;
4739 int s0, s1;
4740
4741 rundevq = 0;
4742 s0 = splsoftcam();
4743 s1 = splcam();
4744 if (dev->qfrozen_cnt > 0) {
4745
4746 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4747 dev->qfrozen_cnt -= count;
4748 if (dev->qfrozen_cnt == 0) {
4749
4750 /*
4751 * No longer need to wait for a successful
4752 * command completion.
4753 */
4754 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4755
4756 /*
4757 * Remove any timeouts that might be scheduled
4758 * to release this queue.
4759 */
4760 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4761 untimeout(xpt_release_devq_timeout, dev,
4762 dev->c_handle);
4763 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4764 }
4765
4766 /*
4767 * Now that we are unfrozen schedule the
4768 * device so any pending transactions are
4769 * run.
4770 */
4771 if ((dev->ccbq.queue.entries > 0)
4772 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4773 && (run_queue != 0)) {
4774 rundevq = 1;
4775 }
4776 }
4777 }
4778 splx(s1);
4779 if (rundevq != 0)
4780 xpt_run_dev_sendq(dev->target->bus);
4781 splx(s0);
4782}
4783
4784void
4785xpt_release_simq(struct cam_sim *sim, int run_queue)
4786{
4787 int s;
4788 struct camq *sendq;
4789
4790 GIANT_REQUIRED;
4791
4792 sendq = &(sim->devq->send_queue);
4793 s = splcam();
4794 if (sendq->qfrozen_cnt > 0) {
4795
4796 sendq->qfrozen_cnt--;
4797 if (sendq->qfrozen_cnt == 0) {
4798 struct cam_eb *bus;
4799
4800 /*
4801 * If there is a timeout scheduled to release this
4802 * sim queue, remove it. The queue frozen count is
4803 * already at 0.
4804 */
4805 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4806 untimeout(xpt_release_simq_timeout, sim,
4807 sim->c_handle);
4808 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4809 }
4810 bus = xpt_find_bus(sim->path_id);
4811 splx(s);
4812
4813 if (run_queue) {
4814 /*
4815 * Now that we are unfrozen run the send queue.
4816 */
4817 xpt_run_dev_sendq(bus);
4818 }
4819 xpt_release_bus(bus);
4820 } else
4821 splx(s);
4822 } else
4823 splx(s);
4824}
4825
4826static void
4827xpt_release_simq_timeout(void *arg)
4828{
4829 struct cam_sim *sim;
4830
4831 sim = (struct cam_sim *)arg;
4832 xpt_release_simq(sim, /* run_queue */ TRUE);
4833}
4834
4835void
4836xpt_done(union ccb *done_ccb)
4837{
4838 int s;
4839
4840 s = splcam();
4841
4842 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4843 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4844 /*
4845 * Queue up the request for handling by our SWI handler
4846 * any of the "non-immediate" type of ccbs.
4847 */
4848 switch (done_ccb->ccb_h.path->periph->type) {
4849 case CAM_PERIPH_BIO:
4850 mtx_lock(&cam_bioq_lock);
4851 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4852 sim_links.tqe);
4853 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4854 mtx_unlock(&cam_bioq_lock);
4855 swi_sched(cambio_ih, 0);
4856 break;
4857 default:
4858 panic("unknown periph type %d",
4859 done_ccb->ccb_h.path->periph->type);
4860 }
4861 }
4862 splx(s);
4863}
4864
4865union ccb *
4866xpt_alloc_ccb()
4867{
4868 union ccb *new_ccb;
4869
4870 GIANT_REQUIRED;
4871
4872 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_WAITOK);
4873 return (new_ccb);
4874}
4875
4876void
4877xpt_free_ccb(union ccb *free_ccb)
4878{
4879 free(free_ccb, M_DEVBUF);
4880}
4881
4882
4883
4884/* Private XPT functions */
4885
4886/*
4887 * Get a CAM control block for the caller. Charge the structure to the device
4888 * referenced by the path. If the this device has no 'credits' then the
4889 * device already has the maximum number of outstanding operations under way
4890 * and we return NULL. If we don't have sufficient resources to allocate more
4891 * ccbs, we also return NULL.
4892 */
4893static union ccb *
4894xpt_get_ccb(struct cam_ed *device)
4895{
4896 union ccb *new_ccb;
4897 int s;
4898
4899 s = splsoftcam();
4900 if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
4901 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_NOWAIT);
4902 if (new_ccb == NULL) {
4903 splx(s);
4904 return (NULL);
4905 }
4906 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4907 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4908 xpt_links.sle);
4909 xpt_ccb_count++;
4910 }
4911 cam_ccbq_take_opening(&device->ccbq);
4912 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4913 splx(s);
4914 return (new_ccb);
4915}
4916
4917static void
4918xpt_release_bus(struct cam_eb *bus)
4919{
4920 int s;
4921
4922 s = splcam();
4923 if ((--bus->refcount == 0)
4924 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4925 TAILQ_REMOVE(&xpt_busses, bus, links);
4926 bus_generation++;
4927 splx(s);
4928 free(bus, M_DEVBUF);
4929 } else
4930 splx(s);
4931}
4932
4933static struct cam_et *
4934xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4935{
4936 struct cam_et *target;
4937
4938 target = (struct cam_et *)malloc(sizeof(*target), M_DEVBUF, M_NOWAIT);
4939 if (target != NULL) {
4940 struct cam_et *cur_target;
4941
4942 TAILQ_INIT(&target->ed_entries);
4943 target->bus = bus;
4944 target->target_id = target_id;
4945 target->refcount = 1;
4946 target->generation = 0;
4947 timevalclear(&target->last_reset);
4948 /*
4949 * Hold a reference to our parent bus so it
4950 * will not go away before we do.
4951 */
4952 bus->refcount++;
4953
4954 /* Insertion sort into our bus's target list */
4955 cur_target = TAILQ_FIRST(&bus->et_entries);
4956 while (cur_target != NULL && cur_target->target_id < target_id)
4957 cur_target = TAILQ_NEXT(cur_target, links);
4958
4959 if (cur_target != NULL) {
4960 TAILQ_INSERT_BEFORE(cur_target, target, links);
4961 } else {
4962 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4963 }
4964 bus->generation++;
4965 }
4966 return (target);
4967}
4968
4969static void
4970xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4971{
4972 int s;
4973
4974 s = splcam();
4975 if ((--target->refcount == 0)
4976 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4977 TAILQ_REMOVE(&bus->et_entries, target, links);
4978 bus->generation++;
4979 splx(s);
4980 free(target, M_DEVBUF);
4981 xpt_release_bus(bus);
4982 } else
4983 splx(s);
4984}
4985
4986static struct cam_ed *
4987xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4988{
4989#ifdef CAM_NEW_TRAN_CODE
4990 struct cam_path path;
4991#endif /* CAM_NEW_TRAN_CODE */
4992 struct cam_ed *device;
4993 struct cam_devq *devq;
4994 cam_status status;
4995
4996 /* Make space for us in the device queue on our bus */
4997 devq = bus->sim->devq;
4998 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4999
5000 if (status != CAM_REQ_CMP) {
5001 device = NULL;
5002 } else {
5003 device = (struct cam_ed *)malloc(sizeof(*device),
5004 M_DEVBUF, M_NOWAIT);
5005 }
5006
5007 if (device != NULL) {
5008 struct cam_ed *cur_device;
5009
5010 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
5011 device->alloc_ccb_entry.device = device;
5012 cam_init_pinfo(&device->send_ccb_entry.pinfo);
5013 device->send_ccb_entry.device = device;
5014 device->target = target;
5015 device->lun_id = lun_id;
5016 /* Initialize our queues */
5017 if (camq_init(&device->drvq, 0) != 0) {
5018 free(device, M_DEVBUF);
5019 return (NULL);
5020 }
5021 if (cam_ccbq_init(&device->ccbq,
5022 bus->sim->max_dev_openings) != 0) {
5023 camq_fini(&device->drvq);
5024 free(device, M_DEVBUF);
5025 return (NULL);
5026 }
5027 SLIST_INIT(&device->asyncs);
5028 SLIST_INIT(&device->periphs);
5029 device->generation = 0;
5030 device->owner = NULL;
5031 /*
5032 * Take the default quirk entry until we have inquiry
5033 * data and can determine a better quirk to use.
5034 */
5035 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
5036 bzero(&device->inq_data, sizeof(device->inq_data));
5037 device->inq_flags = 0;
5038 device->queue_flags = 0;
5039 device->serial_num = NULL;
5040 device->serial_num_len = 0;
5041 device->qfrozen_cnt = 0;
5042 device->flags = CAM_DEV_UNCONFIGURED;
5043 device->tag_delay_count = 0;
5044 device->tag_saved_openings = 0;
5045 device->refcount = 1;
5046 callout_handle_init(&device->c_handle);
5047
5048 /*
5049 * Hold a reference to our parent target so it
5050 * will not go away before we do.
5051 */
5052 target->refcount++;
5053
5054 /*
5055 * XXX should be limited by number of CCBs this bus can
5056 * do.
5057 */
5058 xpt_max_ccbs += device->ccbq.devq_openings;
5059 /* Insertion sort into our target's device list */
5060 cur_device = TAILQ_FIRST(&target->ed_entries);
5061 while (cur_device != NULL && cur_device->lun_id < lun_id)
5062 cur_device = TAILQ_NEXT(cur_device, links);
5063 if (cur_device != NULL) {
5064 TAILQ_INSERT_BEFORE(cur_device, device, links);
5065 } else {
5066 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
5067 }
5068 target->generation++;
5069#ifdef CAM_NEW_TRAN_CODE
5070 if (lun_id != CAM_LUN_WILDCARD) {
5071 xpt_compile_path(&path,
5072 NULL,
5073 bus->path_id,
5074 target->target_id,
5075 lun_id);
5076 xpt_devise_transport(&path);
5077 xpt_release_path(&path);
5078 }
5079#endif /* CAM_NEW_TRAN_CODE */
5080 }
5081 return (device);
5082}
5083
5084static void
5085xpt_release_device(struct cam_eb *bus, struct cam_et *target,
5086 struct cam_ed *device)
5087{
5088 int s;
5089
5090 s = splcam();
5091 if ((--device->refcount == 0)
5092 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
5093 struct cam_devq *devq;
5094
5095 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
5096 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
5097 panic("Removing device while still queued for ccbs");
5098
5099 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
5100 untimeout(xpt_release_devq_timeout, device,
5101 device->c_handle);
5102
5103 TAILQ_REMOVE(&target->ed_entries, device,links);
5104 target->generation++;
5105 xpt_max_ccbs -= device->ccbq.devq_openings;
5106 /* Release our slot in the devq */
5107 devq = bus->sim->devq;
5108 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
5109 splx(s);
5110 camq_fini(&device->drvq);
5111 camq_fini(&device->ccbq.queue);
5112 free(device, M_DEVBUF);
5113 xpt_release_target(bus, target);
5114 } else
5115 splx(s);
5116}
5117
5118static u_int32_t
5119xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5120{
5121 int s;
5122 int diff;
5123 int result;
5124 struct cam_ed *dev;
5125
5126 dev = path->device;
5127 s = splsoftcam();
5128
5129 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
5130 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5131 if (result == CAM_REQ_CMP && (diff < 0)) {
5132 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
5133 }
5134 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5135 || (dev->inq_flags & SID_CmdQue) != 0)
5136 dev->tag_saved_openings = newopenings;
5137 /* Adjust the global limit */
5138 xpt_max_ccbs += diff;
5139 splx(s);
5140 return (result);
5141}
5142
5143static struct cam_eb *
5144xpt_find_bus(path_id_t path_id)
5145{
5146 struct cam_eb *bus;
5147
5148 for (bus = TAILQ_FIRST(&xpt_busses);
5149 bus != NULL;
5150 bus = TAILQ_NEXT(bus, links)) {
5151 if (bus->path_id == path_id) {
5152 bus->refcount++;
5153 break;
5154 }
5155 }
5156 return (bus);
5157}
5158
5159static struct cam_et *
5160xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5161{
5162 struct cam_et *target;
5163
5164 for (target = TAILQ_FIRST(&bus->et_entries);
5165 target != NULL;
5166 target = TAILQ_NEXT(target, links)) {
5167 if (target->target_id == target_id) {
5168 target->refcount++;
5169 break;
5170 }
5171 }
5172 return (target);
5173}
5174
5175static struct cam_ed *
5176xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5177{
5178 struct cam_ed *device;
5179
5180 for (device = TAILQ_FIRST(&target->ed_entries);
5181 device != NULL;
5182 device = TAILQ_NEXT(device, links)) {
5183 if (device->lun_id == lun_id) {
5184 device->refcount++;
5185 break;
5186 }
5187 }
5188 return (device);
5189}
5190
5191typedef struct {
5192 union ccb *request_ccb;
5193 struct ccb_pathinq *cpi;
5194 int pending_count;
5195} xpt_scan_bus_info;
5196
5197/*
5198 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5199 * As the scan progresses, xpt_scan_bus is used as the
5200 * callback on completion function.
5201 */
5202static void
5203xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
5204{
5205 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5206 ("xpt_scan_bus\n"));
5207 switch (request_ccb->ccb_h.func_code) {
5208 case XPT_SCAN_BUS:
5209 {
5210 xpt_scan_bus_info *scan_info;
5211 union ccb *work_ccb;
5212 struct cam_path *path;
5213 u_int i;
5214 u_int max_target;
5215 u_int initiator_id;
5216
5217 /* Find out the characteristics of the bus */
5218 work_ccb = xpt_alloc_ccb();
5219 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
5220 request_ccb->ccb_h.pinfo.priority);
5221 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5222 xpt_action(work_ccb);
5223 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5224 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
5225 xpt_free_ccb(work_ccb);
5226 xpt_done(request_ccb);
5227 return;
5228 }
5229
5230 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5231 /*
5232 * Can't scan the bus on an adapter that
5233 * cannot perform the initiator role.
5234 */
5235 request_ccb->ccb_h.status = CAM_REQ_CMP;
5236 xpt_free_ccb(work_ccb);
5237 xpt_done(request_ccb);
5238 return;
5239 }
5240
5241 /* Save some state for use while we probe for devices */
5242 scan_info = (xpt_scan_bus_info *)
5243 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
5244 scan_info->request_ccb = request_ccb;
5245 scan_info->cpi = &work_ccb->cpi;
5246
5247 /* Cache on our stack so we can work asynchronously */
5248 max_target = scan_info->cpi->max_target;
5249 initiator_id = scan_info->cpi->initiator_id;
5250
5251 /*
5252 * Don't count the initiator if the
5253 * initiator is addressable.
5254 */
5255 scan_info->pending_count = max_target + 1;
5256 if (initiator_id <= max_target)
5257 scan_info->pending_count--;
5258
5259 for (i = 0; i <= max_target; i++) {
5260 cam_status status;
5261 if (i == initiator_id)
5262 continue;
5263
5264 status = xpt_create_path(&path, xpt_periph,
5265 request_ccb->ccb_h.path_id,
5266 i, 0);
5267 if (status != CAM_REQ_CMP) {
5268 printf("xpt_scan_bus: xpt_create_path failed"
5269 " with status %#x, bus scan halted\n",
5270 status);
5271 break;
5272 }
5273 work_ccb = xpt_alloc_ccb();
5274 xpt_setup_ccb(&work_ccb->ccb_h, path,
5275 request_ccb->ccb_h.pinfo.priority);
5276 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5277 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5278 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
5279 work_ccb->crcn.flags = request_ccb->crcn.flags;
5280 xpt_action(work_ccb);
5281 }
5282 break;
5283 }
5284 case XPT_SCAN_LUN:
5285 {
5286 xpt_scan_bus_info *scan_info;
5287 path_id_t path_id;
5288 target_id_t target_id;
5289 lun_id_t lun_id;
5290
5291 /* Reuse the same CCB to query if a device was really found */
5292 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
5293 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
5294 request_ccb->ccb_h.pinfo.priority);
5295 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5296
5297 path_id = request_ccb->ccb_h.path_id;
5298 target_id = request_ccb->ccb_h.target_id;
5299 lun_id = request_ccb->ccb_h.target_lun;
5300 xpt_action(request_ccb);
5301
5302 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5303 struct cam_ed *device;
5304 struct cam_et *target;
5305 int s, phl;
5306
5307 /*
5308 * If we already probed lun 0 successfully, or
5309 * we have additional configured luns on this
5310 * target that might have "gone away", go onto
5311 * the next lun.
5312 */
5313 target = request_ccb->ccb_h.path->target;
5314 /*
5315 * We may touch devices that we don't
5316 * hold references too, so ensure they
5317 * don't disappear out from under us.
5318 * The target above is referenced by the
5319 * path in the request ccb.
5320 */
5321 phl = 0;
5322 s = splcam();
5323 device = TAILQ_FIRST(&target->ed_entries);
5324 if (device != NULL) {
5325 phl = CAN_SRCH_HI(device);
5326 if (device->lun_id == 0)
5327 device = TAILQ_NEXT(device, links);
5328 }
5329 splx(s);
5330 if ((lun_id != 0) || (device != NULL)) {
5331 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5332 lun_id++;
5333 }
5334 } else {
5335 struct cam_ed *device;
5336
5337 device = request_ccb->ccb_h.path->device;
5338
5339 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5340 /* Try the next lun */
5341 if (lun_id < (CAM_SCSI2_MAXLUN-1)
5342 || CAN_SRCH_HI(device))
5343 lun_id++;
5344 }
5345 }
5346
5347 xpt_free_path(request_ccb->ccb_h.path);
5348
5349 /* Check Bounds */
5350 if ((lun_id == request_ccb->ccb_h.target_lun)
5351 || lun_id > scan_info->cpi->max_lun) {
5352 /* We're done */
5353
5354 xpt_free_ccb(request_ccb);
5355 scan_info->pending_count--;
5356 if (scan_info->pending_count == 0) {
5357 xpt_free_ccb((union ccb *)scan_info->cpi);
5358 request_ccb = scan_info->request_ccb;
5359 free(scan_info, M_TEMP);
5360 request_ccb->ccb_h.status = CAM_REQ_CMP;
5361 xpt_done(request_ccb);
5362 }
5363 } else {
5364 /* Try the next device */
5365 struct cam_path *path;
5366 cam_status status;
5367
5368 status = xpt_create_path(&path, xpt_periph,
5369 path_id, target_id, lun_id);
5370 if (status != CAM_REQ_CMP) {
5371 printf("xpt_scan_bus: xpt_create_path failed "
5372 "with status %#x, halting LUN scan\n",
5373 status);
5374 xpt_free_ccb(request_ccb);
5375 scan_info->pending_count--;
5376 if (scan_info->pending_count == 0) {
5377 xpt_free_ccb(
5378 (union ccb *)scan_info->cpi);
5379 request_ccb = scan_info->request_ccb;
5380 free(scan_info, M_TEMP);
5381 request_ccb->ccb_h.status = CAM_REQ_CMP;
5382 xpt_done(request_ccb);
5383 }
5384 break;
5385 }
5386 xpt_setup_ccb(&request_ccb->ccb_h, path,
5387 request_ccb->ccb_h.pinfo.priority);
5388 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5389 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5390 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5391 request_ccb->crcn.flags =
5392 scan_info->request_ccb->crcn.flags;
5393 xpt_action(request_ccb);
5394 }
5395 break;
5396 }
5397 default:
5398 break;
5399 }
5400}
5401
5402typedef enum {
5403 PROBE_TUR,
5404 PROBE_INQUIRY,
5405 PROBE_FULL_INQUIRY,
5406 PROBE_MODE_SENSE,
5407 PROBE_SERIAL_NUM,
5408 PROBE_TUR_FOR_NEGOTIATION
5409} probe_action;
5410
5411typedef enum {
5412 PROBE_INQUIRY_CKSUM = 0x01,
5413 PROBE_SERIAL_CKSUM = 0x02,
5414 PROBE_NO_ANNOUNCE = 0x04
5415} probe_flags;
5416
5417typedef struct {
5418 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5419 probe_action action;
5420 union ccb saved_ccb;
5421 probe_flags flags;
5422 MD5_CTX context;
5423 u_int8_t digest[16];
5424} probe_softc;
5425
5426static void
5427xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5428 cam_flags flags, union ccb *request_ccb)
5429{
5430 struct ccb_pathinq cpi;
5431 cam_status status;
5432 struct cam_path *new_path;
5433 struct cam_periph *old_periph;
5434 int s;
5435
5436 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5437 ("xpt_scan_lun\n"));
5438
5439 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5440 cpi.ccb_h.func_code = XPT_PATH_INQ;
5441 xpt_action((union ccb *)&cpi);
5442
5443 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5444 if (request_ccb != NULL) {
5445 request_ccb->ccb_h.status = cpi.ccb_h.status;
5446 xpt_done(request_ccb);
5447 }
5448 return;
5449 }
5450
5451 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5452 /*
5453 * Can't scan the bus on an adapter that
5454 * cannot perform the initiator role.
5455 */
5456 if (request_ccb != NULL) {
5457 request_ccb->ccb_h.status = CAM_REQ_CMP;
5458 xpt_done(request_ccb);
5459 }
5460 return;
5461 }
5462
5463 if (request_ccb == NULL) {
5464 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5465 if (request_ccb == NULL) {
5466 xpt_print_path(path);
5467 printf("xpt_scan_lun: can't allocate CCB, can't "
5468 "continue\n");
5469 return;
5470 }
5471 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5472 if (new_path == NULL) {
5473 xpt_print_path(path);
5474 printf("xpt_scan_lun: can't allocate path, can't "
5475 "continue\n");
5476 free(request_ccb, M_TEMP);
5477 return;
5478 }
5479 status = xpt_compile_path(new_path, xpt_periph,
5480 path->bus->path_id,
5481 path->target->target_id,
5482 path->device->lun_id);
5483
5484 if (status != CAM_REQ_CMP) {
5485 xpt_print_path(path);
5486 printf("xpt_scan_lun: can't compile path, can't "
5487 "continue\n");
5488 free(request_ccb, M_TEMP);
5489 free(new_path, M_TEMP);
5490 return;
5491 }
5492 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5493 request_ccb->ccb_h.cbfcnp = xptscandone;
5494 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5495 request_ccb->crcn.flags = flags;
5496 }
5497
5498 s = splsoftcam();
5499 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5500 probe_softc *softc;
5501
5502 softc = (probe_softc *)old_periph->softc;
5503 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5504 periph_links.tqe);
5505 } else {
5506 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5507 probestart, "probe",
5508 CAM_PERIPH_BIO,
5509 request_ccb->ccb_h.path, NULL, 0,
5510 request_ccb);
5511
5512 if (status != CAM_REQ_CMP) {
5513 xpt_print_path(path);
5514 printf("xpt_scan_lun: cam_alloc_periph returned an "
5515 "error, can't continue probe\n");
5516 request_ccb->ccb_h.status = status;
5517 xpt_done(request_ccb);
5518 }
5519 }
5520 splx(s);
5521}
5522
5523static void
5524xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5525{
5526 xpt_release_path(done_ccb->ccb_h.path);
5527 free(done_ccb->ccb_h.path, M_TEMP);
5528 free(done_ccb, M_TEMP);
5529}
5530
5531static cam_status
5532proberegister(struct cam_periph *periph, void *arg)
5533{
5534 union ccb *request_ccb; /* CCB representing the probe request */
5535 probe_softc *softc;
5536
5537 request_ccb = (union ccb *)arg;
5538 if (periph == NULL) {
5539 printf("proberegister: periph was NULL!!\n");
5540 return(CAM_REQ_CMP_ERR);
5541 }
5542
5543 if (request_ccb == NULL) {
5544 printf("proberegister: no probe CCB, "
5545 "can't register device\n");
5546 return(CAM_REQ_CMP_ERR);
5547 }
5548
5549 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5550
5551 if (softc == NULL) {
5552 printf("proberegister: Unable to probe new device. "
5553 "Unable to allocate softc\n");
5554 return(CAM_REQ_CMP_ERR);
5555 }
5556 TAILQ_INIT(&softc->request_ccbs);
5557 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5558 periph_links.tqe);
5559 softc->flags = 0;
5560 periph->softc = softc;
5561 cam_periph_acquire(periph);
5562 /*
5563 * Ensure we've waited at least a bus settle
5564 * delay before attempting to probe the device.
5565 * For HBAs that don't do bus resets, this won't make a difference.
5566 */
5567 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5568 scsi_delay);
5569 probeschedule(periph);
5570 return(CAM_REQ_CMP);
5571}
5572
5573static void
5574probeschedule(struct cam_periph *periph)
5575{
5576 struct ccb_pathinq cpi;
5577 union ccb *ccb;
5578 probe_softc *softc;
5579
5580 softc = (probe_softc *)periph->softc;
5581 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5582
5583 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5584 cpi.ccb_h.func_code = XPT_PATH_INQ;
5585 xpt_action((union ccb *)&cpi);
5586
5587 /*
5588 * If a device has gone away and another device, or the same one,
5589 * is back in the same place, it should have a unit attention
5590 * condition pending. It will not report the unit attention in
5591 * response to an inquiry, which may leave invalid transfer
5592 * negotiations in effect. The TUR will reveal the unit attention
5593 * condition. Only send the TUR for lun 0, since some devices
5594 * will get confused by commands other than inquiry to non-existent
5595 * luns. If you think a device has gone away start your scan from
5596 * lun 0. This will insure that any bogus transfer settings are
5597 * invalidated.
5598 *
5599 * If we haven't seen the device before and the controller supports
5600 * some kind of transfer negotiation, negotiate with the first
5601 * sent command if no bus reset was performed at startup. This
5602 * ensures that the device is not confused by transfer negotiation
5603 * settings left over by loader or BIOS action.
5604 */
5605 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5606 && (ccb->ccb_h.target_lun == 0)) {
5607 softc->action = PROBE_TUR;
5608 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5609 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5610 proberequestdefaultnegotiation(periph);
5611 softc->action = PROBE_INQUIRY;
5612 } else {
5613 softc->action = PROBE_INQUIRY;
5614 }
5615
5616 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5617 softc->flags |= PROBE_NO_ANNOUNCE;
5618 else
5619 softc->flags &= ~PROBE_NO_ANNOUNCE;
5620
5621 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5622}
5623
5624static void
5625probestart(struct cam_periph *periph, union ccb *start_ccb)
5626{
5627 /* Probe the device that our peripheral driver points to */
5628 struct ccb_scsiio *csio;
5629 probe_softc *softc;
5630
5631 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5632
5633 softc = (probe_softc *)periph->softc;
5634 csio = &start_ccb->csio;
5635
5636 switch (softc->action) {
5637 case PROBE_TUR:
5638 case PROBE_TUR_FOR_NEGOTIATION:
5639 {
5640 scsi_test_unit_ready(csio,
5641 /*retries*/4,
5642 probedone,
5643 MSG_SIMPLE_Q_TAG,
5644 SSD_FULL_SIZE,
5645 /*timeout*/60000);
5646 break;
5647 }
5648 case PROBE_INQUIRY:
5649 case PROBE_FULL_INQUIRY:
5650 {
5651 u_int inquiry_len;
5652 struct scsi_inquiry_data *inq_buf;
5653
5654 inq_buf = &periph->path->device->inq_data;
5655 /*
5656 * If the device is currently configured, we calculate an
5657 * MD5 checksum of the inquiry data, and if the serial number
5658 * length is greater than 0, add the serial number data
5659 * into the checksum as well. Once the inquiry and the
5660 * serial number check finish, we attempt to figure out
5661 * whether we still have the same device.
5662 */
5663 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5664
5665 MD5Init(&softc->context);
5666 MD5Update(&softc->context, (unsigned char *)inq_buf,
5667 sizeof(struct scsi_inquiry_data));
5668 softc->flags |= PROBE_INQUIRY_CKSUM;
5669 if (periph->path->device->serial_num_len > 0) {
5670 MD5Update(&softc->context,
5671 periph->path->device->serial_num,
5672 periph->path->device->serial_num_len);
5673 softc->flags |= PROBE_SERIAL_CKSUM;
5674 }
5675 MD5Final(softc->digest, &softc->context);
5676 }
5677
5678 if (softc->action == PROBE_INQUIRY)
5679 inquiry_len = SHORT_INQUIRY_LENGTH;
5680 else
5681 inquiry_len = inq_buf->additional_length
5682 + offsetof(struct scsi_inquiry_data,
5683 additional_length) + 1;
5684
5685 /*
5686 * Some parallel SCSI devices fail to send an
5687 * ignore wide residue message when dealing with
5688 * odd length inquiry requests. Round up to be
5689 * safe.
5690 */
5691 inquiry_len = roundup2(inquiry_len, 2);
5692
5693 scsi_inquiry(csio,
5694 /*retries*/4,
5695 probedone,
5696 MSG_SIMPLE_Q_TAG,
5697 (u_int8_t *)inq_buf,
5698 inquiry_len,
5699 /*evpd*/FALSE,
5700 /*page_code*/0,
5701 SSD_MIN_SIZE,
5702 /*timeout*/60 * 1000);
5703 break;
5704 }
5705 case PROBE_MODE_SENSE:
5706 {
5707 void *mode_buf;
5708 int mode_buf_len;
5709
5710 mode_buf_len = sizeof(struct scsi_mode_header_6)
5711 + sizeof(struct scsi_mode_blk_desc)
5712 + sizeof(struct scsi_control_page);
5713 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5714 if (mode_buf != NULL) {
5715 scsi_mode_sense(csio,
5716 /*retries*/4,
5717 probedone,
5718 MSG_SIMPLE_Q_TAG,
5719 /*dbd*/FALSE,
5720 SMS_PAGE_CTRL_CURRENT,
5721 SMS_CONTROL_MODE_PAGE,
5722 mode_buf,
5723 mode_buf_len,
5724 SSD_FULL_SIZE,
5725 /*timeout*/60000);
5726 break;
5727 }
5728 xpt_print_path(periph->path);
5729 printf("Unable to mode sense control page - malloc failure\n");
5730 softc->action = PROBE_SERIAL_NUM;
5731 }
5732 /* FALLTHROUGH */
5733 case PROBE_SERIAL_NUM:
5734 {
5735 struct scsi_vpd_unit_serial_number *serial_buf;
5736 struct cam_ed* device;
5737
5738 serial_buf = NULL;
5739 device = periph->path->device;
5740 device->serial_num = NULL;
5741 device->serial_num_len = 0;
5742
5743 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5744 serial_buf = (struct scsi_vpd_unit_serial_number *)
5745 malloc(sizeof(*serial_buf), M_TEMP,
5746 M_NOWAIT | M_ZERO);
5747
5748 if (serial_buf != NULL) {
5749 scsi_inquiry(csio,
5750 /*retries*/4,
5751 probedone,
5752 MSG_SIMPLE_Q_TAG,
5753 (u_int8_t *)serial_buf,
5754 sizeof(*serial_buf),
5755 /*evpd*/TRUE,
5756 SVPD_UNIT_SERIAL_NUMBER,
5757 SSD_MIN_SIZE,
5758 /*timeout*/60 * 1000);
5759 break;
5760 }
5761 /*
5762 * We'll have to do without, let our probedone
5763 * routine finish up for us.
5764 */
5765 start_ccb->csio.data_ptr = NULL;
5766 probedone(periph, start_ccb);
5767 return;
5768 }
5769 }
5770 xpt_action(start_ccb);
5771}
5772
5773static void
5774proberequestdefaultnegotiation(struct cam_periph *periph)
5775{
5776 struct ccb_trans_settings cts;
5777
5778 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5779 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5780#ifdef CAM_NEW_TRAN_CODE
5781 cts.type = CTS_TYPE_USER_SETTINGS;
5782#else /* CAM_NEW_TRAN_CODE */
5783 cts.flags = CCB_TRANS_USER_SETTINGS;
5784#endif /* CAM_NEW_TRAN_CODE */
5785 xpt_action((union ccb *)&cts);
5786 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5787#ifdef CAM_NEW_TRAN_CODE
5788 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5789#else /* CAM_NEW_TRAN_CODE */
5790 cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5791 cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5792#endif /* CAM_NEW_TRAN_CODE */
5793 xpt_action((union ccb *)&cts);
5794}
5795
5796static void
5797probedone(struct cam_periph *periph, union ccb *done_ccb)
5798{
5799 probe_softc *softc;
5800 struct cam_path *path;
5801 u_int32_t priority;
5802
5803 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5804
5805 softc = (probe_softc *)periph->softc;
5806 path = done_ccb->ccb_h.path;
5807 priority = done_ccb->ccb_h.pinfo.priority;
5808
5809 switch (softc->action) {
5810 case PROBE_TUR:
5811 {
5812 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5813
5814 if (cam_periph_error(done_ccb, 0,
5815 SF_NO_PRINT, NULL) == ERESTART)
5816 return;
5817 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5818 /* Don't wedge the queue */
5819 xpt_release_devq(done_ccb->ccb_h.path,
5820 /*count*/1,
5821 /*run_queue*/TRUE);
5822 }
5823 softc->action = PROBE_INQUIRY;
5824 xpt_release_ccb(done_ccb);
5825 xpt_schedule(periph, priority);
5826 return;
5827 }
5828 case PROBE_INQUIRY:
5829 case PROBE_FULL_INQUIRY:
5830 {
5831 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5832 struct scsi_inquiry_data *inq_buf;
5833 u_int8_t periph_qual;
5834
5835 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5836 inq_buf = &path->device->inq_data;
5837
5838 periph_qual = SID_QUAL(inq_buf);
5839
5840 switch(periph_qual) {
5841 case SID_QUAL_LU_CONNECTED:
5842 {
5843 u_int8_t len;
5844
5845 /*
5846 * We conservatively request only
5847 * SHORT_INQUIRY_LEN bytes of inquiry
5848 * information during our first try
5849 * at sending an INQUIRY. If the device
5850 * has more information to give,
5851 * perform a second request specifying
5852 * the amount of information the device
5853 * is willing to give.
5854 */
5855 len = inq_buf->additional_length
5856 + offsetof(struct scsi_inquiry_data,
5857 additional_length) + 1;
5858 if (softc->action == PROBE_INQUIRY
5859 && len > SHORT_INQUIRY_LENGTH) {
5860 softc->action = PROBE_FULL_INQUIRY;
5861 xpt_release_ccb(done_ccb);
5862 xpt_schedule(periph, priority);
5863 return;
5864 }
5865
5866 xpt_find_quirk(path->device);
5867
5868#ifdef CAM_NEW_TRAN_CODE
5869 xpt_devise_transport(path);
5870#endif /* CAM_NEW_TRAN_CODE */
5871 if ((inq_buf->flags & SID_CmdQue) != 0)
5872 softc->action = PROBE_MODE_SENSE;
5873 else
5874 softc->action = PROBE_SERIAL_NUM;
5875
5876 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5877
5878 xpt_release_ccb(done_ccb);
5879 xpt_schedule(periph, priority);
5880 return;
5881 }
5882 default:
5883 break;
5884 }
5885 } else if (cam_periph_error(done_ccb, 0,
5886 done_ccb->ccb_h.target_lun > 0
5887 ? SF_RETRY_UA|SF_QUIET_IR
5888 : SF_RETRY_UA,
5889 &softc->saved_ccb) == ERESTART) {
5890 return;
5891 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5892 /* Don't wedge the queue */
5893 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5894 /*run_queue*/TRUE);
5895 }
5896 /*
5897 * If we get to this point, we got an error status back
5898 * from the inquiry and the error status doesn't require
5899 * automatically retrying the command. Therefore, the
5900 * inquiry failed. If we had inquiry information before
5901 * for this device, but this latest inquiry command failed,
5902 * the device has probably gone away. If this device isn't
5903 * already marked unconfigured, notify the peripheral
5904 * drivers that this device is no more.
5905 */
5906 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5907 /* Send the async notification. */
5908 xpt_async(AC_LOST_DEVICE, path, NULL);
5909
5910 xpt_release_ccb(done_ccb);
5911 break;
5912 }
5913 case PROBE_MODE_SENSE:
5914 {
5915 struct ccb_scsiio *csio;
5916 struct scsi_mode_header_6 *mode_hdr;
5917
5918 csio = &done_ccb->csio;
5919 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5920 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5921 struct scsi_control_page *page;
5922 u_int8_t *offset;
5923
5924 offset = ((u_int8_t *)&mode_hdr[1])
5925 + mode_hdr->blk_desc_len;
5926 page = (struct scsi_control_page *)offset;
5927 path->device->queue_flags = page->queue_flags;
5928 } else if (cam_periph_error(done_ccb, 0,
5929 SF_RETRY_UA|SF_NO_PRINT,
5930 &softc->saved_ccb) == ERESTART) {
5931 return;
5932 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5933 /* Don't wedge the queue */
5934 xpt_release_devq(done_ccb->ccb_h.path,
5935 /*count*/1, /*run_queue*/TRUE);
5936 }
5937 xpt_release_ccb(done_ccb);
5938 free(mode_hdr, M_TEMP);
5939 softc->action = PROBE_SERIAL_NUM;
5940 xpt_schedule(periph, priority);
5941 return;
5942 }
5943 case PROBE_SERIAL_NUM:
5944 {
5945 struct ccb_scsiio *csio;
5946 struct scsi_vpd_unit_serial_number *serial_buf;
5947 u_int32_t priority;
5948 int changed;
5949 int have_serialnum;
5950
5951 changed = 1;
5952 have_serialnum = 0;
5953 csio = &done_ccb->csio;
5954 priority = done_ccb->ccb_h.pinfo.priority;
5955 serial_buf =
5956 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5957
5958 /* Clean up from previous instance of this device */
5959 if (path->device->serial_num != NULL) {
5960 free(path->device->serial_num, M_DEVBUF);
5961 path->device->serial_num = NULL;
5962 path->device->serial_num_len = 0;
5963 }
5964
5965 if (serial_buf == NULL) {
5966 /*
5967 * Don't process the command as it was never sent
5968 */
5969 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5970 && (serial_buf->length > 0)) {
5971
5972 have_serialnum = 1;
5973 path->device->serial_num =
5974 (u_int8_t *)malloc((serial_buf->length + 1),
5975 M_DEVBUF, M_NOWAIT);
5976 if (path->device->serial_num != NULL) {
5977 bcopy(serial_buf->serial_num,
5978 path->device->serial_num,
5979 serial_buf->length);
5980 path->device->serial_num_len =
5981 serial_buf->length;
5982 path->device->serial_num[serial_buf->length]
5983 = '\0';
5984 }
5985 } else if (cam_periph_error(done_ccb, 0,
5986 SF_RETRY_UA|SF_NO_PRINT,
5987 &softc->saved_ccb) == ERESTART) {
5988 return;
5989 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5990 /* Don't wedge the queue */
5991 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5992 /*run_queue*/TRUE);
5993 }
5994
5995 /*
5996 * Let's see if we have seen this device before.
5997 */
5998 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
5999 MD5_CTX context;
6000 u_int8_t digest[16];
6001
6002 MD5Init(&context);
6003
6004 MD5Update(&context,
6005 (unsigned char *)&path->device->inq_data,
6006 sizeof(struct scsi_inquiry_data));
6007
6008 if (have_serialnum)
6009 MD5Update(&context, serial_buf->serial_num,
6010 serial_buf->length);
6011
6012 MD5Final(digest, &context);
6013 if (bcmp(softc->digest, digest, 16) == 0)
6014 changed = 0;
6015
6016 /*
6017 * XXX Do we need to do a TUR in order to ensure
6018 * that the device really hasn't changed???
6019 */
6020 if ((changed != 0)
6021 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
6022 xpt_async(AC_LOST_DEVICE, path, NULL);
6023 }
6024 if (serial_buf != NULL)
6025 free(serial_buf, M_TEMP);
6026
6027 if (changed != 0) {
6028 /*
6029 * Now that we have all the necessary
6030 * information to safely perform transfer
6031 * negotiations... Controllers don't perform
6032 * any negotiation or tagged queuing until
6033 * after the first XPT_SET_TRAN_SETTINGS ccb is
6034 * received. So, on a new device, just retreive
6035 * the user settings, and set them as the current
6036 * settings to set the device up.
6037 */
6038 proberequestdefaultnegotiation(periph);
6039 xpt_release_ccb(done_ccb);
6040
6041 /*
6042 * Perform a TUR to allow the controller to
6043 * perform any necessary transfer negotiation.
6044 */
6045 softc->action = PROBE_TUR_FOR_NEGOTIATION;
6046 xpt_schedule(periph, priority);
6047 return;
6048 }
6049 xpt_release_ccb(done_ccb);
6050 break;
6051 }
6052 case PROBE_TUR_FOR_NEGOTIATION:
6053 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6054 /* Don't wedge the queue */
6055 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6056 /*run_queue*/TRUE);
6057 }
6058
6059 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
6060
6061 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
6062 /* Inform the XPT that a new device has been found */
6063 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
6064 xpt_action(done_ccb);
6065
6066 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6067 done_ccb);
6068 }
6069 xpt_release_ccb(done_ccb);
6070 break;
6071 }
6072 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
6073 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
6074 done_ccb->ccb_h.status = CAM_REQ_CMP;
6075 xpt_done(done_ccb);
6076 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
6077 cam_periph_invalidate(periph);
6078 cam_periph_release(periph);
6079 } else {
6080 probeschedule(periph);
6081 }
6082}
6083
6084static void
6085probecleanup(struct cam_periph *periph)
6086{
6087 free(periph->softc, M_TEMP);
6088}
6089
6090static void
6091xpt_find_quirk(struct cam_ed *device)
6092{
6093 caddr_t match;
6094
6095 match = cam_quirkmatch((caddr_t)&device->inq_data,
6096 (caddr_t)xpt_quirk_table,
6097 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
6098 sizeof(*xpt_quirk_table), scsi_inquiry_match);
6099
6100 if (match == NULL)
6101 panic("xpt_find_quirk: device didn't match wildcard entry!!");
6102
6103 device->quirk = (struct xpt_quirk_entry *)match;
6104}
6105
6106#ifdef CAM_NEW_TRAN_CODE
6107
6108static void
6109xpt_devise_transport(struct cam_path *path)
6110{
6111 struct ccb_pathinq cpi;
6112 struct ccb_trans_settings cts;
6113 struct scsi_inquiry_data *inq_buf;
6114
6115 /* Get transport information from the SIM */
6116 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
6117 cpi.ccb_h.func_code = XPT_PATH_INQ;
6118 xpt_action((union ccb *)&cpi);
6119
6120 inq_buf = NULL;
6121 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
6122 inq_buf = &path->device->inq_data;
6123 path->device->protocol = PROTO_SCSI;
6124 path->device->protocol_version =
6125 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
6126 path->device->transport = cpi.transport;
6127 path->device->transport_version = cpi.transport_version;
6128
6129 /*
6130 * Any device not using SPI3 features should
6131 * be considered SPI2 or lower.
6132 */
6133 if (inq_buf != NULL) {
6134 if (path->device->transport == XPORT_SPI
6135 && (inq_buf->spi3data & SID_SPI_MASK) == 0
6136 && path->device->transport_version > 2)
6137 path->device->transport_version = 2;
6138 } else {
6139 struct cam_ed* otherdev;
6140
6141 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6142 otherdev != NULL;
6143 otherdev = TAILQ_NEXT(otherdev, links)) {
6144 if (otherdev != path->device)
6145 break;
6146 }
6147
6148 if (otherdev != NULL) {
6149 /*
6150 * Initially assume the same versioning as
6151 * prior luns for this target.
6152 */
6153 path->device->protocol_version =
6154 otherdev->protocol_version;
6155 path->device->transport_version =
6156 otherdev->transport_version;
6157 } else {
6158 /* Until we know better, opt for safty */
6159 path->device->protocol_version = 2;
6160 if (path->device->transport == XPORT_SPI)
6161 path->device->transport_version = 2;
6162 else
6163 path->device->transport_version = 0;
6164 }
6165 }
6166
6167 /*
6168 * XXX
6169 * For a device compliant with SPC-2 we should be able
6170 * to determine the transport version supported by
6171 * scrutinizing the version descriptors in the
6172 * inquiry buffer.
6173 */
6174
6175 /* Tell the controller what we think */
6176 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
6177 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6178 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6179 cts.transport = path->device->transport;
6180 cts.transport_version = path->device->transport_version;
6181 cts.protocol = path->device->protocol;
6182 cts.protocol_version = path->device->protocol_version;
6183 cts.proto_specific.valid = 0;
6184 cts.xport_specific.valid = 0;
6185 xpt_action((union ccb *)&cts);
6186}
6187
6188static void
6189xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6190 int async_update)
6191{
6192 struct ccb_pathinq cpi;
6193 struct ccb_trans_settings cur_cts;
6194 struct ccb_trans_settings_scsi *scsi;
6195 struct ccb_trans_settings_scsi *cur_scsi;
6196 struct cam_sim *sim;
6197 struct scsi_inquiry_data *inq_data;
6198
6199 if (device == NULL) {
6200 cts->ccb_h.status = CAM_PATH_INVALID;
6201 xpt_done((union ccb *)cts);
6202 return;
6203 }
6204
6205 if (cts->protocol == PROTO_UNKNOWN
6206 || cts->protocol == PROTO_UNSPECIFIED) {
6207 cts->protocol = device->protocol;
6208 cts->protocol_version = device->protocol_version;
6209 }
6210
6211 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
6212 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
6213 cts->protocol_version = device->protocol_version;
6214
6215 if (cts->protocol != device->protocol) {
6216 xpt_print_path(cts->ccb_h.path);
6217 printf("Uninitialized Protocol %x:%x?\n",
6218 cts->protocol, device->protocol);
6219 cts->protocol = device->protocol;
6220 }
6221
6222 if (cts->protocol_version > device->protocol_version) {
6223 if (bootverbose) {
6224 xpt_print_path(cts->ccb_h.path);
6225 printf("Down reving Protocol Version from %d to %d?\n",
6226 cts->protocol_version, device->protocol_version);
6227 }
6228 cts->protocol_version = device->protocol_version;
6229 }
6230
6231 if (cts->transport == XPORT_UNKNOWN
6232 || cts->transport == XPORT_UNSPECIFIED) {
6233 cts->transport = device->transport;
6234 cts->transport_version = device->transport_version;
6235 }
6236
6237 if (cts->transport_version == XPORT_VERSION_UNKNOWN
6238 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
6239 cts->transport_version = device->transport_version;
6240
6241 if (cts->transport != device->transport) {
6242 xpt_print_path(cts->ccb_h.path);
6243 printf("Uninitialized Transport %x:%x?\n",
6244 cts->transport, device->transport);
6245 cts->transport = device->transport;
6246 }
6247
6248 if (cts->transport_version > device->transport_version) {
6249 if (bootverbose) {
6250 xpt_print_path(cts->ccb_h.path);
6251 printf("Down reving Transport Version from %d to %d?\n",
6252 cts->transport_version,
6253 device->transport_version);
6254 }
6255 cts->transport_version = device->transport_version;
6256 }
6257
6258 sim = cts->ccb_h.path->bus->sim;
6259
6260 /*
6261 * Nothing more of interest to do unless
6262 * this is a device connected via the
6263 * SCSI protocol.
6264 */
6265 if (cts->protocol != PROTO_SCSI) {
6266 if (async_update == FALSE)
6267 (*(sim->sim_action))(sim, (union ccb *)cts);
6268 return;
6269 }
6270
6271 inq_data = &device->inq_data;
6272 scsi = &cts->proto_specific.scsi;
6273 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6274 cpi.ccb_h.func_code = XPT_PATH_INQ;
6275 xpt_action((union ccb *)&cpi);
6276
6277 /* SCSI specific sanity checking */
6278 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6279 || (inq_data->flags & SID_CmdQue) == 0
6280 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6281 || (device->quirk->mintags == 0)) {
6282 /*
6283 * Can't tag on hardware that doesn't support tags,
6284 * doesn't have it enabled, or has broken tag support.
6285 */
6286 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6287 }
6288
6289 if (async_update == FALSE) {
6290 /*
6291 * Perform sanity checking against what the
6292 * controller and device can do.
6293 */
6294 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6295 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6296 cur_cts.type = cts->type;
6297 xpt_action((union ccb *)&cur_cts);
6298
6299 cur_scsi = &cur_cts.proto_specific.scsi;
6300 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
6301 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6302 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
6303 }
6304 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
6305 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6306 }
6307
6308 /* SPI specific sanity checking */
6309 if (cts->transport == XPORT_SPI && async_update == FALSE) {
6310 u_int spi3caps;
6311 struct ccb_trans_settings_spi *spi;
6312 struct ccb_trans_settings_spi *cur_spi;
6313
6314 spi = &cts->xport_specific.spi;
6315
6316 cur_spi = &cur_cts.xport_specific.spi;
6317
6318 /* Fill in any gaps in what the user gave us */
6319 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6320 spi->sync_period = cur_spi->sync_period;
6321 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6322 spi->sync_period = 0;
6323 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6324 spi->sync_offset = cur_spi->sync_offset;
6325 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6326 spi->sync_offset = 0;
6327 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6328 spi->ppr_options = cur_spi->ppr_options;
6329 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6330 spi->ppr_options = 0;
6331 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6332 spi->bus_width = cur_spi->bus_width;
6333 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6334 spi->bus_width = 0;
6335 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
6336 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6337 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
6338 }
6339 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
6340 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6341 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6342 && (inq_data->flags & SID_Sync) == 0
6343 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6344 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6345 || (cur_spi->sync_offset == 0)
6346 || (cur_spi->sync_period == 0)) {
6347 /* Force async */
6348 spi->sync_period = 0;
6349 spi->sync_offset = 0;
6350 }
6351
6352 switch (spi->bus_width) {
6353 case MSG_EXT_WDTR_BUS_32_BIT:
6354 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6355 || (inq_data->flags & SID_WBus32) != 0
6356 || cts->type == CTS_TYPE_USER_SETTINGS)
6357 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6358 break;
6359 /* Fall Through to 16-bit */
6360 case MSG_EXT_WDTR_BUS_16_BIT:
6361 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6362 || (inq_data->flags & SID_WBus16) != 0
6363 || cts->type == CTS_TYPE_USER_SETTINGS)
6364 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6365 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6366 break;
6367 }
6368 /* Fall Through to 8-bit */
6369 default: /* New bus width?? */
6370 case MSG_EXT_WDTR_BUS_8_BIT:
6371 /* All targets can do this */
6372 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6373 break;
6374 }
6375
6376 spi3caps = cpi.xport_specific.spi.ppr_options;
6377 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6378 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6379 spi3caps &= inq_data->spi3data;
6380
6381 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
6382 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
6383
6384 if ((spi3caps & SID_SPI_IUS) == 0)
6385 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
6386
6387 if ((spi3caps & SID_SPI_QAS) == 0)
6388 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
6389
6390 /* No SPI Transfer settings are allowed unless we are wide */
6391 if (spi->bus_width == 0)
6392 spi->ppr_options = 0;
6393
6394 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
6395 /*
6396 * Can't tag queue without disconnection.
6397 */
6398 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6399 scsi->valid |= CTS_SCSI_VALID_TQ;
6400 }
6401
6402 /*
6403 * If we are currently performing tagged transactions to
6404 * this device and want to change its negotiation parameters,
6405 * go non-tagged for a bit to give the controller a chance to
6406 * negotiate unhampered by tag messages.
6407 */
6408 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6409 && (device->inq_flags & SID_CmdQue) != 0
6410 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6411 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
6412 CTS_SPI_VALID_SYNC_OFFSET|
6413 CTS_SPI_VALID_BUS_WIDTH)) != 0)
6414 xpt_toggle_tags(cts->ccb_h.path);
6415 }
6416
6417 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6418 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
6419 int device_tagenb;
6420
6421 /*
6422 * If we are transitioning from tags to no-tags or
6423 * vice-versa, we need to carefully freeze and restart
6424 * the queue so that we don't overlap tagged and non-tagged
6425 * commands. We also temporarily stop tags if there is
6426 * a change in transfer negotiation settings to allow
6427 * "tag-less" negotiation.
6428 */
6429 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6430 || (device->inq_flags & SID_CmdQue) != 0)
6431 device_tagenb = TRUE;
6432 else
6433 device_tagenb = FALSE;
6434
6435 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6436 && device_tagenb == FALSE)
6437 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
6438 && device_tagenb == TRUE)) {
6439
6440 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
6441 /*
6442 * Delay change to use tags until after a
6443 * few commands have gone to this device so
6444 * the controller has time to perform transfer
6445 * negotiations without tagged messages getting
6446 * in the way.
6447 */
6448 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6449 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6450 } else {
6451 struct ccb_relsim crs;
6452
6453 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6454 device->inq_flags &= ~SID_CmdQue;
6455 xpt_dev_ccbq_resize(cts->ccb_h.path,
6456 sim->max_dev_openings);
6457 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6458 device->tag_delay_count = 0;
6459
6460 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6461 /*priority*/1);
6462 crs.ccb_h.func_code = XPT_REL_SIMQ;
6463 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6464 crs.openings
6465 = crs.release_timeout
6466 = crs.qfrozen_cnt
6467 = 0;
6468 xpt_action((union ccb *)&crs);
6469 }
6470 }
6471 }
6472 if (async_update == FALSE)
6473 (*(sim->sim_action))(sim, (union ccb *)cts);
6474}
6475
6476#else /* CAM_NEW_TRAN_CODE */
6477
6478static void
6479xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6480 int async_update)
6481{
6482 struct cam_sim *sim;
6483 int qfrozen;
6484
6485 sim = cts->ccb_h.path->bus->sim;
6486 if (async_update == FALSE) {
6487 struct scsi_inquiry_data *inq_data;
6488 struct ccb_pathinq cpi;
6489 struct ccb_trans_settings cur_cts;
6490
6491 if (device == NULL) {
6492 cts->ccb_h.status = CAM_PATH_INVALID;
6493 xpt_done((union ccb *)cts);
6494 return;
6495 }
6496
6497 /*
6498 * Perform sanity checking against what the
6499 * controller and device can do.
6500 */
6501 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6502 cpi.ccb_h.func_code = XPT_PATH_INQ;
6503 xpt_action((union ccb *)&cpi);
6504 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6505 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6506 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
6507 xpt_action((union ccb *)&cur_cts);
6508 inq_data = &device->inq_data;
6509
6510 /* Fill in any gaps in what the user gave us */
6511 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
6512 cts->sync_period = cur_cts.sync_period;
6513 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
6514 cts->sync_offset = cur_cts.sync_offset;
6515 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
6516 cts->bus_width = cur_cts.bus_width;
6517 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
6518 cts->flags &= ~CCB_TRANS_DISC_ENB;
6519 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
6520 }
6521 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
6522 cts->flags &= ~CCB_TRANS_TAG_ENB;
6523 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
6524 }
6525
6526 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6527 && (inq_data->flags & SID_Sync) == 0)
6528 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6529 || (cts->sync_offset == 0)
6530 || (cts->sync_period == 0)) {
6531 /* Force async */
6532 cts->sync_period = 0;
6533 cts->sync_offset = 0;
6534 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6535 && (inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
6536 && cts->sync_period <= 0x9) {
6537 /*
6538 * Don't allow DT transmission rates if the
6539 * device does not support it.
6540 */
6541 cts->sync_period = 0xa;
6542 }
6543
6544 switch (cts->bus_width) {
6545 case MSG_EXT_WDTR_BUS_32_BIT:
6546 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6547 || (inq_data->flags & SID_WBus32) != 0)
6548 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6549 break;
6550 /* FALLTHROUGH to 16-bit */
6551 case MSG_EXT_WDTR_BUS_16_BIT:
6552 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6553 || (inq_data->flags & SID_WBus16) != 0)
6554 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6555 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6556 break;
6557 }
6558 /* FALLTHROUGH to 8-bit */
6559 default: /* New bus width?? */
6560 case MSG_EXT_WDTR_BUS_8_BIT:
6561 /* All targets can do this */
6562 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6563 break;
6564 }
6565
6566 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
6567 /*
6568 * Can't tag queue without disconnection.
6569 */
6570 cts->flags &= ~CCB_TRANS_TAG_ENB;
6571 cts->valid |= CCB_TRANS_TQ_VALID;
6572 }
6573
6574 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6575 || (inq_data->flags & SID_CmdQue) == 0
6576 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6577 || (device->quirk->mintags == 0)) {
6578 /*
6579 * Can't tag on hardware that doesn't support,
6580 * doesn't have it enabled, or has broken tag support.
6581 */
6582 cts->flags &= ~CCB_TRANS_TAG_ENB;
6583 }
6584 }
6585
6586 qfrozen = FALSE;
6587 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
6588 int device_tagenb;
6589
6590 /*
6591 * If we are transitioning from tags to no-tags or
6592 * vice-versa, we need to carefully freeze and restart
6593 * the queue so that we don't overlap tagged and non-tagged
6594 * commands. We also temporarily stop tags if there is
6595 * a change in transfer negotiation settings to allow
6596 * "tag-less" negotiation.
6597 */
6598 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6599 || (device->inq_flags & SID_CmdQue) != 0)
6600 device_tagenb = TRUE;
6601 else
6602 device_tagenb = FALSE;
6603
6604 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
6605 && device_tagenb == FALSE)
6606 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
6607 && device_tagenb == TRUE)) {
6608
6609 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
6610 /*
6611 * Delay change to use tags until after a
6612 * few commands have gone to this device so
6613 * the controller has time to perform transfer
6614 * negotiations without tagged messages getting
6615 * in the way.
6616 */
6617 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6618 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6619 } else {
6620 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6621 qfrozen = TRUE;
6622 device->inq_flags &= ~SID_CmdQue;
6623 xpt_dev_ccbq_resize(cts->ccb_h.path,
6624 sim->max_dev_openings);
6625 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6626 device->tag_delay_count = 0;
6627 }
6628 }
6629 }
6630
6631 if (async_update == FALSE) {
6632 /*
6633 * If we are currently performing tagged transactions to
6634 * this device and want to change its negotiation parameters,
6635 * go non-tagged for a bit to give the controller a chance to
6636 * negotiate unhampered by tag messages.
6637 */
6638 if ((device->inq_flags & SID_CmdQue) != 0
6639 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
6640 CCB_TRANS_SYNC_OFFSET_VALID|
6641 CCB_TRANS_BUS_WIDTH_VALID)) != 0)
6642 xpt_toggle_tags(cts->ccb_h.path);
6643
6644 (*(sim->sim_action))(sim, (union ccb *)cts);
6645 }
6646
6647 if (qfrozen) {
6648 struct ccb_relsim crs;
6649
6650 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6651 /*priority*/1);
6652 crs.ccb_h.func_code = XPT_REL_SIMQ;
6653 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6654 crs.openings
6655 = crs.release_timeout
6656 = crs.qfrozen_cnt
6657 = 0;
6658 xpt_action((union ccb *)&crs);
6659 }
6660}
6661
6662
6663#endif /* CAM_NEW_TRAN_CODE */
6664
6665static void
6666xpt_toggle_tags(struct cam_path *path)
6667{
6668 struct cam_ed *dev;
6669
6670 /*
6671 * Give controllers a chance to renegotiate
6672 * before starting tag operations. We
6673 * "toggle" tagged queuing off then on
6674 * which causes the tag enable command delay
6675 * counter to come into effect.
6676 */
6677 dev = path->device;
6678 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6679 || ((dev->inq_flags & SID_CmdQue) != 0
6680 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6681 struct ccb_trans_settings cts;
6682
6683 xpt_setup_ccb(&cts.ccb_h, path, 1);
6684#ifdef CAM_NEW_TRAN_CODE
6685 cts.protocol = PROTO_SCSI;
6686 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
6687 cts.transport = XPORT_UNSPECIFIED;
6688 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
6689 cts.proto_specific.scsi.flags = 0;
6690 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
6691#else /* CAM_NEW_TRAN_CODE */
6692 cts.flags = 0;
6693 cts.valid = CCB_TRANS_TQ_VALID;
6694#endif /* CAM_NEW_TRAN_CODE */
6695 xpt_set_transfer_settings(&cts, path->device,
6696 /*async_update*/TRUE);
6697#ifdef CAM_NEW_TRAN_CODE
6698 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
6699#else /* CAM_NEW_TRAN_CODE */
6700 cts.flags = CCB_TRANS_TAG_ENB;
6701#endif /* CAM_NEW_TRAN_CODE */
6702 xpt_set_transfer_settings(&cts, path->device,
6703 /*async_update*/TRUE);
6704 }
6705}
6706
6707static void
6708xpt_start_tags(struct cam_path *path)
6709{
6710 struct ccb_relsim crs;
6711 struct cam_ed *device;
6712 struct cam_sim *sim;
6713 int newopenings;
6714
6715 device = path->device;
6716 sim = path->bus->sim;
6717 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6718 xpt_freeze_devq(path, /*count*/1);
6719 device->inq_flags |= SID_CmdQue;
6720 if (device->tag_saved_openings != 0)
6721 newopenings = device->tag_saved_openings;
6722 else
6723 newopenings = min(device->quirk->maxtags,
6724 sim->max_tagged_dev_openings);
6725 xpt_dev_ccbq_resize(path, newopenings);
6726 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6727 crs.ccb_h.func_code = XPT_REL_SIMQ;
6728 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6729 crs.openings
6730 = crs.release_timeout
6731 = crs.qfrozen_cnt
6732 = 0;
6733 xpt_action((union ccb *)&crs);
6734}
6735
6736static int busses_to_config;
6737static int busses_to_reset;
6738
6739static int
6740xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6741{
6742 if (bus->path_id != CAM_XPT_PATH_ID) {
6743 struct cam_path path;
6744 struct ccb_pathinq cpi;
6745 int can_negotiate;
6746
6747 busses_to_config++;
6748 xpt_compile_path(&path, NULL, bus->path_id,
6749 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6750 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6751 cpi.ccb_h.func_code = XPT_PATH_INQ;
6752 xpt_action((union ccb *)&cpi);
6753 can_negotiate = cpi.hba_inquiry;
6754 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6755 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6756 && can_negotiate)
6757 busses_to_reset++;
6758 xpt_release_path(&path);
6759 }
6760
6761 return(1);
6762}
6763
6764static int
6765xptconfigfunc(struct cam_eb *bus, void *arg)
6766{
6767 struct cam_path *path;
6768 union ccb *work_ccb;
6769
6770 if (bus->path_id != CAM_XPT_PATH_ID) {
6771 cam_status status;
6772 int can_negotiate;
6773
6774 work_ccb = xpt_alloc_ccb();
6775 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6776 CAM_TARGET_WILDCARD,
6777 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6778 printf("xptconfigfunc: xpt_create_path failed with "
6779 "status %#x for bus %d\n", status, bus->path_id);
6780 printf("xptconfigfunc: halting bus configuration\n");
6781 xpt_free_ccb(work_ccb);
6782 busses_to_config--;
6783 xpt_finishconfig(xpt_periph, NULL);
6784 return(0);
6785 }
6786 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6787 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6788 xpt_action(work_ccb);
6789 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6790 printf("xptconfigfunc: CPI failed on bus %d "
6791 "with status %d\n", bus->path_id,
6792 work_ccb->ccb_h.status);
6793 xpt_finishconfig(xpt_periph, work_ccb);
6794 return(1);
6795 }
6796
6797 can_negotiate = work_ccb->cpi.hba_inquiry;
6798 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6799 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6800 && (can_negotiate != 0)) {
6801 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6802 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6803 work_ccb->ccb_h.cbfcnp = NULL;
6804 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6805 ("Resetting Bus\n"));
6806 xpt_action(work_ccb);
6807 xpt_finishconfig(xpt_periph, work_ccb);
6808 } else {
6809 /* Act as though we performed a successful BUS RESET */
6810 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6811 xpt_finishconfig(xpt_periph, work_ccb);
6812 }
6813 }
6814
6815 return(1);
6816}
6817
6818static void
6819xpt_config(void *arg)
6820{
6821 /*
6822 * Now that interrupts are enabled, go find our devices
6823 */
6824
6825#ifdef CAMDEBUG
6826 /* Setup debugging flags and path */
6827#ifdef CAM_DEBUG_FLAGS
6828 cam_dflags = CAM_DEBUG_FLAGS;
6829#else /* !CAM_DEBUG_FLAGS */
6830 cam_dflags = CAM_DEBUG_NONE;
6831#endif /* CAM_DEBUG_FLAGS */
6832#ifdef CAM_DEBUG_BUS
6833 if (cam_dflags != CAM_DEBUG_NONE) {
6834 if (xpt_create_path(&cam_dpath, xpt_periph,
6835 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6836 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6837 printf("xpt_config: xpt_create_path() failed for debug"
6838 " target %d:%d:%d, debugging disabled\n",
6839 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6840 cam_dflags = CAM_DEBUG_NONE;
6841 }
6842 } else
6843 cam_dpath = NULL;
6844#else /* !CAM_DEBUG_BUS */
6845 cam_dpath = NULL;
6846#endif /* CAM_DEBUG_BUS */
6847#endif /* CAMDEBUG */
6848
6849 /*
6850 * Scan all installed busses.
6851 */
6852 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6853
6854 if (busses_to_config == 0) {
6855 /* Call manually because we don't have any busses */
6856 xpt_finishconfig(xpt_periph, NULL);
6857 } else {
6858 if (busses_to_reset > 0 && scsi_delay >= 2000) {
6859 printf("Waiting %d seconds for SCSI "
6860 "devices to settle\n", scsi_delay/1000);
6861 }
6862 xpt_for_all_busses(xptconfigfunc, NULL);
6863 }
6864}
6865
6866/*
6867 * If the given device only has one peripheral attached to it, and if that
6868 * peripheral is the passthrough driver, announce it. This insures that the
6869 * user sees some sort of announcement for every peripheral in their system.
6870 */
6871static int
6872xptpassannouncefunc(struct cam_ed *device, void *arg)
6873{
6874 struct cam_periph *periph;
6875 int i;
6876
6877 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6878 periph = SLIST_NEXT(periph, periph_links), i++);
6879
6880 periph = SLIST_FIRST(&device->periphs);
6881 if ((i == 1)
6882 && (strncmp(periph->periph_name, "pass", 4) == 0))
6883 xpt_announce_periph(periph, NULL);
6884
6885 return(1);
6886}
6887
6888static void
6889xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6890{
6891 struct periph_driver **p_drv;
6892 int i;
6893
6894 if (done_ccb != NULL) {
6895 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6896 ("xpt_finishconfig\n"));
6897 switch(done_ccb->ccb_h.func_code) {
6898 case XPT_RESET_BUS:
6899 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6900 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6901 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6902 done_ccb->crcn.flags = 0;
6903 xpt_action(done_ccb);
6904 return;
6905 }
6906 /* FALLTHROUGH */
6907 case XPT_SCAN_BUS:
6908 default:
6909 xpt_free_path(done_ccb->ccb_h.path);
6910 busses_to_config--;
6911 break;
6912 }
6913 }
6914
6915 if (busses_to_config == 0) {
6916 /* Register all the peripheral drivers */
6917 /* XXX This will have to change when we have loadable modules */
6918 p_drv = periph_drivers;
6919 for (i = 0; p_drv[i] != NULL; i++) {
6920 (*p_drv[i]->init)();
6921 }
6922
6923 /*
6924 * Check for devices with no "standard" peripheral driver
6925 * attached. For any devices like that, announce the
6926 * passthrough driver so the user will see something.
6927 */
6928 xpt_for_all_devices(xptpassannouncefunc, NULL);
6929
6930 /* Release our hook so that the boot can continue. */
6931 config_intrhook_disestablish(xpt_config_hook);
6932 free(xpt_config_hook, M_TEMP);
6933 xpt_config_hook = NULL;
6934 }
6935 if (done_ccb != NULL)
6936 xpt_free_ccb(done_ccb);
6937}
6938
6939static void
6940xptaction(struct cam_sim *sim, union ccb *work_ccb)
6941{
6942 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6943
6944 switch (work_ccb->ccb_h.func_code) {
6945 /* Common cases first */
6946 case XPT_PATH_INQ: /* Path routing inquiry */
6947 {
6948 struct ccb_pathinq *cpi;
6949
6950 cpi = &work_ccb->cpi;
6951 cpi->version_num = 1; /* XXX??? */
6952 cpi->hba_inquiry = 0;
6953 cpi->target_sprt = 0;
6954 cpi->hba_misc = 0;
6955 cpi->hba_eng_cnt = 0;
6956 cpi->max_target = 0;
6957 cpi->max_lun = 0;
6958 cpi->initiator_id = 0;
6959 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
6960 strncpy(cpi->hba_vid, "", HBA_IDLEN);
6961 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
6962 cpi->unit_number = sim->unit_number;
6963 cpi->bus_id = sim->bus_id;
6964 cpi->base_transfer_speed = 0;
6965#ifdef CAM_NEW_TRAN_CODE
6966 cpi->protocol = PROTO_UNSPECIFIED;
6967 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
6968 cpi->transport = XPORT_UNSPECIFIED;
6969 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
6970#endif /* CAM_NEW_TRAN_CODE */
6971 cpi->ccb_h.status = CAM_REQ_CMP;
6972 xpt_done(work_ccb);
6973 break;
6974 }
6975 default:
6976 work_ccb->ccb_h.status = CAM_REQ_INVALID;
6977 xpt_done(work_ccb);
6978 break;
6979 }
6980}
6981
6982/*
6983 * The xpt as a "controller" has no interrupt sources, so polling
6984 * is a no-op.
6985 */
6986static void
6987xptpoll(struct cam_sim *sim)
6988{
6989}
6990
6991static void
6992camisr(void *V_queue)
6993{
6994 cam_isrq_t *oqueue = V_queue;
6995 cam_isrq_t queue;
6996 int s;
6997 struct ccb_hdr *ccb_h;
6998
6999 /*
7000 * Transfer the ccb_bioq list to a temporary list so we can operate
7001 * on it without needing to lock/unlock on every loop. The concat
7002 * function with re-init the real list for us.
7003 */
7004 s = splcam();
7005 mtx_lock(&cam_bioq_lock);
7006 TAILQ_INIT(&queue);
7007 TAILQ_CONCAT(&queue, oqueue, sim_links.tqe);
7008 mtx_unlock(&cam_bioq_lock);
7009
7010 while ((ccb_h = TAILQ_FIRST(&queue)) != NULL) {
7011 int runq;
7012
7013 TAILQ_REMOVE(&queue, ccb_h, sim_links.tqe);
7014 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
7015 splx(s);
7016
7017 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
7018 ("camisr\n"));
7019
7020 runq = FALSE;
7021
7022 if (ccb_h->flags & CAM_HIGH_POWER) {
7023 struct highpowerlist *hphead;
7024 union ccb *send_ccb;
7025
7026 hphead = &highpowerq;
7027
7028 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
7029
7030 /*
7031 * Increment the count since this command is done.
7032 */
7033 num_highpower++;
7034
7035 /*
7036 * Any high powered commands queued up?
7037 */
7038 if (send_ccb != NULL) {
7039
7040 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
7041
7042 xpt_release_devq(send_ccb->ccb_h.path,
7043 /*count*/1, /*runqueue*/TRUE);
7044 }
7045 }
7046 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
7047 struct cam_ed *dev;
7048
7049 dev = ccb_h->path->device;
7050
7051 s = splcam();
7052 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
7053
7054 ccb_h->path->bus->sim->devq->send_active--;
7055 ccb_h->path->bus->sim->devq->send_openings++;
7056 splx(s);
7057
7058 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
7059 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
7060 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
7061 && (dev->ccbq.dev_active == 0))) {
7062
7063 xpt_release_devq(ccb_h->path, /*count*/1,
7064 /*run_queue*/TRUE);
7065 }
7066
7067 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
7068 && (--dev->tag_delay_count == 0))
7069 xpt_start_tags(ccb_h->path);
7070
7071 if ((dev->ccbq.queue.entries > 0)
7072 && (dev->qfrozen_cnt == 0)
7073 && (device_is_send_queued(dev) == 0)) {
7074 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
7075 dev);
7076 }
7077 }
7078
7079 if (ccb_h->status & CAM_RELEASE_SIMQ) {
7080 xpt_release_simq(ccb_h->path->bus->sim,
7081 /*run_queue*/TRUE);
7082 ccb_h->status &= ~CAM_RELEASE_SIMQ;
7083 runq = FALSE;
7084 }
7085
7086 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
7087 && (ccb_h->status & CAM_DEV_QFRZN)) {
7088 xpt_release_devq(ccb_h->path, /*count*/1,
7089 /*run_queue*/TRUE);
7090 ccb_h->status &= ~CAM_DEV_QFRZN;
7091 } else if (runq) {
7092 xpt_run_dev_sendq(ccb_h->path->bus);
7093 }
7094
7095 /* Call the peripheral driver's callback */
7096 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
7097
7098 /* Raise IPL for while test */
7099 s = splcam();
7100 }
7101 splx(s);
7102}
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 147571 2005-06-24 08:09:05Z 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 */
68
69/*
70 * Definition of an async handler callback block. These are used to add
71 * SIMs and peripherals to the async callback lists.
72 */
73struct async_node {
74 SLIST_ENTRY(async_node) links;
75 u_int32_t event_enable; /* Async Event enables */
76 void (*callback)(void *arg, u_int32_t code,
77 struct cam_path *path, void *args);
78 void *callback_arg;
79};
80
81SLIST_HEAD(async_list, async_node);
82SLIST_HEAD(periph_list, cam_periph);
83static STAILQ_HEAD(highpowerlist, ccb_hdr) highpowerq;
84
85/*
86 * This is the maximum number of high powered commands (e.g. start unit)
87 * that can be outstanding at a particular time.
88 */
89#ifndef CAM_MAX_HIGHPOWER
90#define CAM_MAX_HIGHPOWER 4
91#endif
92
93/* number of high powered commands that can go through right now */
94static int num_highpower = CAM_MAX_HIGHPOWER;
95
96/*
97 * Structure for queueing a device in a run queue.
98 * There is one run queue for allocating new ccbs,
99 * and another for sending ccbs to the controller.
100 */
101struct cam_ed_qinfo {
102 cam_pinfo pinfo;
103 struct cam_ed *device;
104};
105
106/*
107 * The CAM EDT (Existing Device Table) contains the device information for
108 * all devices for all busses in the system. The table contains a
109 * cam_ed structure for each device on the bus.
110 */
111struct cam_ed {
112 TAILQ_ENTRY(cam_ed) links;
113 struct cam_ed_qinfo alloc_ccb_entry;
114 struct cam_ed_qinfo send_ccb_entry;
115 struct cam_et *target;
116 lun_id_t lun_id;
117 struct camq drvq; /*
118 * Queue of type drivers wanting to do
119 * work on this device.
120 */
121 struct cam_ccbq ccbq; /* Queue of pending ccbs */
122 struct async_list asyncs; /* Async callback info for this B/T/L */
123 struct periph_list periphs; /* All attached devices */
124 u_int generation; /* Generation number */
125 struct cam_periph *owner; /* Peripheral driver's ownership tag */
126 struct xpt_quirk_entry *quirk; /* Oddities about this device */
127 /* Storage for the inquiry data */
128#ifdef CAM_NEW_TRAN_CODE
129 cam_proto protocol;
130 u_int protocol_version;
131 cam_xport transport;
132 u_int transport_version;
133#endif /* CAM_NEW_TRAN_CODE */
134 struct scsi_inquiry_data inq_data;
135 u_int8_t inq_flags; /*
136 * Current settings for inquiry flags.
137 * This allows us to override settings
138 * like disconnection and tagged
139 * queuing for a device.
140 */
141 u_int8_t queue_flags; /* Queue flags from the control page */
142 u_int8_t serial_num_len;
143 u_int8_t *serial_num;
144 u_int32_t qfrozen_cnt;
145 u_int32_t flags;
146#define CAM_DEV_UNCONFIGURED 0x01
147#define CAM_DEV_REL_TIMEOUT_PENDING 0x02
148#define CAM_DEV_REL_ON_COMPLETE 0x04
149#define CAM_DEV_REL_ON_QUEUE_EMPTY 0x08
150#define CAM_DEV_RESIZE_QUEUE_NEEDED 0x10
151#define CAM_DEV_TAG_AFTER_COUNT 0x20
152#define CAM_DEV_INQUIRY_DATA_VALID 0x40
153 u_int32_t tag_delay_count;
154#define CAM_TAG_DELAY_COUNT 5
155 u_int32_t tag_saved_openings;
156 u_int32_t refcount;
157 struct callout_handle c_handle;
158};
159
160/*
161 * Each target is represented by an ET (Existing Target). These
162 * entries are created when a target is successfully probed with an
163 * identify, and removed when a device fails to respond after a number
164 * of retries, or a bus rescan finds the device missing.
165 */
166struct cam_et {
167 TAILQ_HEAD(, cam_ed) ed_entries;
168 TAILQ_ENTRY(cam_et) links;
169 struct cam_eb *bus;
170 target_id_t target_id;
171 u_int32_t refcount;
172 u_int generation;
173 struct timeval last_reset;
174};
175
176/*
177 * Each bus is represented by an EB (Existing Bus). These entries
178 * are created by calls to xpt_bus_register and deleted by calls to
179 * xpt_bus_deregister.
180 */
181struct cam_eb {
182 TAILQ_HEAD(, cam_et) et_entries;
183 TAILQ_ENTRY(cam_eb) links;
184 path_id_t path_id;
185 struct cam_sim *sim;
186 struct timeval last_reset;
187 u_int32_t flags;
188#define CAM_EB_RUNQ_SCHEDULED 0x01
189 u_int32_t refcount;
190 u_int generation;
191};
192
193struct cam_path {
194 struct cam_periph *periph;
195 struct cam_eb *bus;
196 struct cam_et *target;
197 struct cam_ed *device;
198};
199
200struct xpt_quirk_entry {
201 struct scsi_inquiry_pattern inq_pat;
202 u_int8_t quirks;
203#define CAM_QUIRK_NOLUNS 0x01
204#define CAM_QUIRK_NOSERIAL 0x02
205#define CAM_QUIRK_HILUNS 0x04
206#define CAM_QUIRK_NOHILUNS 0x08
207 u_int mintags;
208 u_int maxtags;
209};
210#define CAM_SCSI2_MAXLUN 8
211/*
212 * If we're not quirked to search <= the first 8 luns
213 * and we are either quirked to search above lun 8,
214 * or we're > SCSI-2, we can look for luns above lun 8.
215 */
216#define CAN_SRCH_HI(dv) \
217 (((dv->quirk->quirks & CAM_QUIRK_NOHILUNS) == 0) \
218 && ((dv->quirk->quirks & CAM_QUIRK_HILUNS) \
219 || SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2))
220
221typedef enum {
222 XPT_FLAG_OPEN = 0x01
223} xpt_flags;
224
225struct xpt_softc {
226 xpt_flags flags;
227 u_int32_t generation;
228};
229
230static const char quantum[] = "QUANTUM";
231static const char sony[] = "SONY";
232static const char west_digital[] = "WDIGTL";
233static const char samsung[] = "SAMSUNG";
234static const char seagate[] = "SEAGATE";
235static const char microp[] = "MICROP";
236
237static struct xpt_quirk_entry xpt_quirk_table[] =
238{
239 {
240 /* Reports QUEUE FULL for temporary resource shortages */
241 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
242 /*quirks*/0, /*mintags*/24, /*maxtags*/32
243 },
244 {
245 /* Reports QUEUE FULL for temporary resource shortages */
246 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
247 /*quirks*/0, /*mintags*/24, /*maxtags*/32
248 },
249 {
250 /* Reports QUEUE FULL for temporary resource shortages */
251 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
252 /*quirks*/0, /*mintags*/24, /*maxtags*/32
253 },
254 {
255 /* Broken tagged queuing drive */
256 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
257 /*quirks*/0, /*mintags*/0, /*maxtags*/0
258 },
259 {
260 /* Broken tagged queuing drive */
261 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
262 /*quirks*/0, /*mintags*/0, /*maxtags*/0
263 },
264 {
265 /* Broken tagged queuing drive */
266 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
267 /*quirks*/0, /*mintags*/0, /*maxtags*/0
268 },
269 {
270 /*
271 * Unfortunately, the Quantum Atlas III has the same
272 * problem as the Atlas II drives above.
273 * Reported by: "Johan Granlund" <johan@granlund.nu>
274 *
275 * For future reference, the drive with the problem was:
276 * QUANTUM QM39100TD-SW N1B0
277 *
278 * It's possible that Quantum will fix the problem in later
279 * firmware revisions. If that happens, the quirk entry
280 * will need to be made specific to the firmware revisions
281 * with the problem.
282 *
283 */
284 /* Reports QUEUE FULL for temporary resource shortages */
285 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
286 /*quirks*/0, /*mintags*/24, /*maxtags*/32
287 },
288 {
289 /*
290 * 18 Gig Atlas III, same problem as the 9G version.
291 * Reported by: Andre Albsmeier
292 * <andre.albsmeier@mchp.siemens.de>
293 *
294 * For future reference, the drive with the problem was:
295 * QUANTUM QM318000TD-S N491
296 */
297 /* Reports QUEUE FULL for temporary resource shortages */
298 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
299 /*quirks*/0, /*mintags*/24, /*maxtags*/32
300 },
301 {
302 /*
303 * Broken tagged queuing drive
304 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
305 * and: Martin Renters <martin@tdc.on.ca>
306 */
307 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
308 /*quirks*/0, /*mintags*/0, /*maxtags*/0
309 },
310 /*
311 * The Seagate Medalist Pro drives have very poor write
312 * performance with anything more than 2 tags.
313 *
314 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
315 * Drive: <SEAGATE ST36530N 1444>
316 *
317 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
318 * Drive: <SEAGATE ST34520W 1281>
319 *
320 * No one has actually reported that the 9G version
321 * (ST39140*) of the Medalist Pro has the same problem, but
322 * we're assuming that it does because the 4G and 6.5G
323 * versions of the drive are broken.
324 */
325 {
326 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
327 /*quirks*/0, /*mintags*/2, /*maxtags*/2
328 },
329 {
330 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
331 /*quirks*/0, /*mintags*/2, /*maxtags*/2
332 },
333 {
334 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
335 /*quirks*/0, /*mintags*/2, /*maxtags*/2
336 },
337 {
338 /*
339 * Slow when tagged queueing is enabled. Write performance
340 * steadily drops off with more and more concurrent
341 * transactions. Best sequential write performance with
342 * tagged queueing turned off and write caching turned on.
343 *
344 * PR: kern/10398
345 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
346 * Drive: DCAS-34330 w/ "S65A" firmware.
347 *
348 * The drive with the problem had the "S65A" firmware
349 * revision, and has also been reported (by Stephen J.
350 * Roznowski <sjr@home.net>) for a drive with the "S61A"
351 * firmware revision.
352 *
353 * Although no one has reported problems with the 2 gig
354 * version of the DCAS drive, the assumption is that it
355 * has the same problems as the 4 gig version. Therefore
356 * this quirk entries disables tagged queueing for all
357 * DCAS drives.
358 */
359 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
360 /*quirks*/0, /*mintags*/0, /*maxtags*/0
361 },
362 {
363 /* Broken tagged queuing drive */
364 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
365 /*quirks*/0, /*mintags*/0, /*maxtags*/0
366 },
367 {
368 /* Broken tagged queuing drive */
369 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
370 /*quirks*/0, /*mintags*/0, /*maxtags*/0
371 },
372 {
373 /*
374 * Broken tagged queuing drive.
375 * Submitted by:
376 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
377 * in PR kern/9535
378 */
379 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
380 /*quirks*/0, /*mintags*/0, /*maxtags*/0
381 },
382 {
383 /*
384 * Slow when tagged queueing is enabled. (1.5MB/sec versus
385 * 8MB/sec.)
386 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
387 * Best performance with these drives is achieved with
388 * tagged queueing turned off, and write caching turned on.
389 */
390 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
391 /*quirks*/0, /*mintags*/0, /*maxtags*/0
392 },
393 {
394 /*
395 * Slow when tagged queueing is enabled. (1.5MB/sec versus
396 * 8MB/sec.)
397 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
398 * Best performance with these drives is achieved with
399 * tagged queueing turned off, and write caching turned on.
400 */
401 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
402 /*quirks*/0, /*mintags*/0, /*maxtags*/0
403 },
404 {
405 /*
406 * Doesn't handle queue full condition correctly,
407 * so we need to limit maxtags to what the device
408 * can handle instead of determining this automatically.
409 */
410 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
411 /*quirks*/0, /*mintags*/2, /*maxtags*/32
412 },
413 {
414 /* Really only one LUN */
415 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
416 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
417 },
418 {
419 /* I can't believe we need a quirk for DPT volumes. */
420 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
421 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS,
422 /*mintags*/0, /*maxtags*/255
423 },
424 {
425 /*
426 * Many Sony CDROM drives don't like multi-LUN probing.
427 */
428 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
429 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
430 },
431 {
432 /*
433 * This drive doesn't like multiple LUN probing.
434 * Submitted by: Parag Patel <parag@cgt.com>
435 */
436 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
437 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
438 },
439 {
440 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
441 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
442 },
443 {
444 /*
445 * The 8200 doesn't like multi-lun probing, and probably
446 * don't like serial number requests either.
447 */
448 {
449 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
450 "EXB-8200*", "*"
451 },
452 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
453 },
454 {
455 /*
456 * Let's try the same as above, but for a drive that says
457 * it's an IPL-6860 but is actually an EXB 8200.
458 */
459 {
460 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
461 "IPL-6860*", "*"
462 },
463 CAM_QUIRK_NOSERIAL|CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
464 },
465 {
466 /*
467 * These Hitachi drives don't like multi-lun probing.
468 * The PR submitter has a DK319H, but says that the Linux
469 * kernel has a similar work-around for the DK312 and DK314,
470 * so all DK31* drives are quirked here.
471 * PR: misc/18793
472 * Submitted by: Paul Haddad <paul@pth.com>
473 */
474 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
475 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
476 },
477 {
478 /*
479 * The Hitachi CJ series with J8A8 firmware apparantly has
480 * problems with tagged commands.
481 * PR: 23536
482 * Reported by: amagai@nue.org
483 */
484 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
485 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
486 },
487 {
488 /*
489 * These are the large storage arrays.
490 * Submitted by: William Carrel <william.carrel@infospace.com>
491 */
492 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
493 CAM_QUIRK_HILUNS, 2, 1024
494 },
495 {
496 /*
497 * This old revision of the TDC3600 is also SCSI-1, and
498 * hangs upon serial number probing.
499 */
500 {
501 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
502 " TDC 3600", "U07:"
503 },
504 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
505 },
506 {
507 /*
508 * Maxtor Personal Storage 3000XT (Firewire)
509 * hangs upon serial number probing.
510 */
511 {
512 T_DIRECT, SIP_MEDIA_FIXED, "Maxtor",
513 "1394 storage", "*"
514 },
515 CAM_QUIRK_NOSERIAL, /*mintags*/0, /*maxtags*/0
516 },
517 {
518 /*
519 * Would repond to all LUNs if asked for.
520 */
521 {
522 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
523 "CP150", "*"
524 },
525 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
526 },
527 {
528 /*
529 * Would repond to all LUNs if asked for.
530 */
531 {
532 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
533 "96X2*", "*"
534 },
535 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
536 },
537 {
538 /* Submitted by: Matthew Dodd <winter@jurai.net> */
539 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
540 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
541 },
542 {
543 /* Submitted by: Matthew Dodd <winter@jurai.net> */
544 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
545 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
546 },
547 {
548 /* TeraSolutions special settings for TRC-22 RAID */
549 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
550 /*quirks*/0, /*mintags*/55, /*maxtags*/255
551 },
552 {
553 /* Veritas Storage Appliance */
554 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
555 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
556 },
557 {
558 /*
559 * Would respond to all LUNs. Device type and removable
560 * flag are jumper-selectable.
561 */
562 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
563 "Tahiti 1", "*"
564 },
565 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
566 },
567 {
568 /* EasyRAID E5A aka. areca ARC-6010 */
569 { T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
570 CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
571 },
572 {
573 /* Default tagged queuing parameters for all devices */
574 {
575 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
576 /*vendor*/"*", /*product*/"*", /*revision*/"*"
577 },
578 /*quirks*/0, /*mintags*/2, /*maxtags*/255
579 },
580};
581
582static const int xpt_quirk_table_size =
583 sizeof(xpt_quirk_table) / sizeof(*xpt_quirk_table);
584
585typedef enum {
586 DM_RET_COPY = 0x01,
587 DM_RET_FLAG_MASK = 0x0f,
588 DM_RET_NONE = 0x00,
589 DM_RET_STOP = 0x10,
590 DM_RET_DESCEND = 0x20,
591 DM_RET_ERROR = 0x30,
592 DM_RET_ACTION_MASK = 0xf0
593} dev_match_ret;
594
595typedef enum {
596 XPT_DEPTH_BUS,
597 XPT_DEPTH_TARGET,
598 XPT_DEPTH_DEVICE,
599 XPT_DEPTH_PERIPH
600} xpt_traverse_depth;
601
602struct xpt_traverse_config {
603 xpt_traverse_depth depth;
604 void *tr_func;
605 void *tr_arg;
606};
607
608typedef int xpt_busfunc_t (struct cam_eb *bus, void *arg);
609typedef int xpt_targetfunc_t (struct cam_et *target, void *arg);
610typedef int xpt_devicefunc_t (struct cam_ed *device, void *arg);
611typedef int xpt_periphfunc_t (struct cam_periph *periph, void *arg);
612typedef int xpt_pdrvfunc_t (struct periph_driver **pdrv, void *arg);
613
614/* Transport layer configuration information */
615static struct xpt_softc xsoftc;
616
617/* Queues for our software interrupt handler */
618typedef TAILQ_HEAD(cam_isrq, ccb_hdr) cam_isrq_t;
619static cam_isrq_t cam_bioq;
620static struct mtx cam_bioq_lock;
621
622/* "Pool" of inactive ccbs managed by xpt_alloc_ccb and xpt_free_ccb */
623static SLIST_HEAD(,ccb_hdr) ccb_freeq;
624static u_int xpt_max_ccbs; /*
625 * Maximum size of ccb pool. Modified as
626 * devices are added/removed or have their
627 * opening counts changed.
628 */
629static u_int xpt_ccb_count; /* Current count of allocated ccbs */
630
631struct cam_periph *xpt_periph;
632
633static periph_init_t xpt_periph_init;
634
635static periph_init_t probe_periph_init;
636
637static struct periph_driver xpt_driver =
638{
639 xpt_periph_init, "xpt",
640 TAILQ_HEAD_INITIALIZER(xpt_driver.units)
641};
642
643static struct periph_driver probe_driver =
644{
645 probe_periph_init, "probe",
646 TAILQ_HEAD_INITIALIZER(probe_driver.units)
647};
648
649PERIPHDRIVER_DECLARE(xpt, xpt_driver);
650PERIPHDRIVER_DECLARE(probe, probe_driver);
651
652
653static d_open_t xptopen;
654static d_close_t xptclose;
655static d_ioctl_t xptioctl;
656
657static struct cdevsw xpt_cdevsw = {
658 .d_version = D_VERSION,
659 .d_flags = D_NEEDGIANT,
660 .d_open = xptopen,
661 .d_close = xptclose,
662 .d_ioctl = xptioctl,
663 .d_name = "xpt",
664};
665
666static struct intr_config_hook *xpt_config_hook;
667
668/* Registered busses */
669static TAILQ_HEAD(,cam_eb) xpt_busses;
670static u_int bus_generation;
671
672/* Storage for debugging datastructures */
673#ifdef CAMDEBUG
674struct cam_path *cam_dpath;
675u_int32_t cam_dflags;
676u_int32_t cam_debug_delay;
677#endif
678
679/* Pointers to software interrupt handlers */
680static void *cambio_ih;
681
682#if defined(CAM_DEBUG_FLAGS) && !defined(CAMDEBUG)
683#error "You must have options CAMDEBUG to use options CAM_DEBUG_FLAGS"
684#endif
685
686/*
687 * In order to enable the CAM_DEBUG_* options, the user must have CAMDEBUG
688 * enabled. Also, the user must have either none, or all of CAM_DEBUG_BUS,
689 * CAM_DEBUG_TARGET, and CAM_DEBUG_LUN specified.
690 */
691#if defined(CAM_DEBUG_BUS) || defined(CAM_DEBUG_TARGET) \
692 || defined(CAM_DEBUG_LUN)
693#ifdef CAMDEBUG
694#if !defined(CAM_DEBUG_BUS) || !defined(CAM_DEBUG_TARGET) \
695 || !defined(CAM_DEBUG_LUN)
696#error "You must define all or none of CAM_DEBUG_BUS, CAM_DEBUG_TARGET \
697 and CAM_DEBUG_LUN"
698#endif /* !CAM_DEBUG_BUS || !CAM_DEBUG_TARGET || !CAM_DEBUG_LUN */
699#else /* !CAMDEBUG */
700#error "You must use options CAMDEBUG if you use the CAM_DEBUG_* options"
701#endif /* CAMDEBUG */
702#endif /* CAM_DEBUG_BUS || CAM_DEBUG_TARGET || CAM_DEBUG_LUN */
703
704/* Our boot-time initialization hook */
705static int cam_module_event_handler(module_t, int /*modeventtype_t*/, void *);
706
707static moduledata_t cam_moduledata = {
708 "cam",
709 cam_module_event_handler,
710 NULL
711};
712
713static void xpt_init(void *);
714
715DECLARE_MODULE(cam, cam_moduledata, SI_SUB_CONFIGURE, SI_ORDER_SECOND);
716MODULE_VERSION(cam, 1);
717
718
719static cam_status xpt_compile_path(struct cam_path *new_path,
720 struct cam_periph *perph,
721 path_id_t path_id,
722 target_id_t target_id,
723 lun_id_t lun_id);
724
725static void xpt_release_path(struct cam_path *path);
726
727static void xpt_async_bcast(struct async_list *async_head,
728 u_int32_t async_code,
729 struct cam_path *path,
730 void *async_arg);
731static void xpt_dev_async(u_int32_t async_code,
732 struct cam_eb *bus,
733 struct cam_et *target,
734 struct cam_ed *device,
735 void *async_arg);
736static path_id_t xptnextfreepathid(void);
737static path_id_t xptpathid(const char *sim_name, int sim_unit, int sim_bus);
738static union ccb *xpt_get_ccb(struct cam_ed *device);
739static int xpt_schedule_dev(struct camq *queue, cam_pinfo *dev_pinfo,
740 u_int32_t new_priority);
741static void xpt_run_dev_allocq(struct cam_eb *bus);
742static void xpt_run_dev_sendq(struct cam_eb *bus);
743static timeout_t xpt_release_devq_timeout;
744static timeout_t xpt_release_simq_timeout;
745static void xpt_release_bus(struct cam_eb *bus);
746static void xpt_release_devq_device(struct cam_ed *dev, u_int count,
747 int run_queue);
748static struct cam_et*
749 xpt_alloc_target(struct cam_eb *bus, target_id_t target_id);
750static void xpt_release_target(struct cam_eb *bus, struct cam_et *target);
751static struct cam_ed*
752 xpt_alloc_device(struct cam_eb *bus, struct cam_et *target,
753 lun_id_t lun_id);
754static void xpt_release_device(struct cam_eb *bus, struct cam_et *target,
755 struct cam_ed *device);
756static u_int32_t xpt_dev_ccbq_resize(struct cam_path *path, int newopenings);
757static struct cam_eb*
758 xpt_find_bus(path_id_t path_id);
759static struct cam_et*
760 xpt_find_target(struct cam_eb *bus, target_id_t target_id);
761static struct cam_ed*
762 xpt_find_device(struct cam_et *target, lun_id_t lun_id);
763static void xpt_scan_bus(struct cam_periph *periph, union ccb *ccb);
764static void xpt_scan_lun(struct cam_periph *periph,
765 struct cam_path *path, cam_flags flags,
766 union ccb *ccb);
767static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
768static xpt_busfunc_t xptconfigbuscountfunc;
769static xpt_busfunc_t xptconfigfunc;
770static void xpt_config(void *arg);
771static xpt_devicefunc_t xptpassannouncefunc;
772static void xpt_finishconfig(struct cam_periph *periph, union ccb *ccb);
773static void xptaction(struct cam_sim *sim, union ccb *work_ccb);
774static void xptpoll(struct cam_sim *sim);
775static void camisr(void *);
776#if 0
777static void xptstart(struct cam_periph *periph, union ccb *work_ccb);
778static void xptasync(struct cam_periph *periph,
779 u_int32_t code, cam_path *path);
780#endif
781static dev_match_ret xptbusmatch(struct dev_match_pattern *patterns,
782 u_int num_patterns, struct cam_eb *bus);
783static dev_match_ret xptdevicematch(struct dev_match_pattern *patterns,
784 u_int num_patterns,
785 struct cam_ed *device);
786static dev_match_ret xptperiphmatch(struct dev_match_pattern *patterns,
787 u_int num_patterns,
788 struct cam_periph *periph);
789static xpt_busfunc_t xptedtbusfunc;
790static xpt_targetfunc_t xptedttargetfunc;
791static xpt_devicefunc_t xptedtdevicefunc;
792static xpt_periphfunc_t xptedtperiphfunc;
793static xpt_pdrvfunc_t xptplistpdrvfunc;
794static xpt_periphfunc_t xptplistperiphfunc;
795static int xptedtmatch(struct ccb_dev_match *cdm);
796static int xptperiphlistmatch(struct ccb_dev_match *cdm);
797static int xptbustraverse(struct cam_eb *start_bus,
798 xpt_busfunc_t *tr_func, void *arg);
799static int xpttargettraverse(struct cam_eb *bus,
800 struct cam_et *start_target,
801 xpt_targetfunc_t *tr_func, void *arg);
802static int xptdevicetraverse(struct cam_et *target,
803 struct cam_ed *start_device,
804 xpt_devicefunc_t *tr_func, void *arg);
805static int xptperiphtraverse(struct cam_ed *device,
806 struct cam_periph *start_periph,
807 xpt_periphfunc_t *tr_func, void *arg);
808static int xptpdrvtraverse(struct periph_driver **start_pdrv,
809 xpt_pdrvfunc_t *tr_func, void *arg);
810static int xptpdperiphtraverse(struct periph_driver **pdrv,
811 struct cam_periph *start_periph,
812 xpt_periphfunc_t *tr_func,
813 void *arg);
814static xpt_busfunc_t xptdefbusfunc;
815static xpt_targetfunc_t xptdeftargetfunc;
816static xpt_devicefunc_t xptdefdevicefunc;
817static xpt_periphfunc_t xptdefperiphfunc;
818static int xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg);
819#ifdef notusedyet
820static int xpt_for_all_targets(xpt_targetfunc_t *tr_func,
821 void *arg);
822#endif
823static int xpt_for_all_devices(xpt_devicefunc_t *tr_func,
824 void *arg);
825#ifdef notusedyet
826static int xpt_for_all_periphs(xpt_periphfunc_t *tr_func,
827 void *arg);
828#endif
829static xpt_devicefunc_t xptsetasyncfunc;
830static xpt_busfunc_t xptsetasyncbusfunc;
831static cam_status xptregister(struct cam_periph *periph,
832 void *arg);
833static cam_status proberegister(struct cam_periph *periph,
834 void *arg);
835static void probeschedule(struct cam_periph *probe_periph);
836static void probestart(struct cam_periph *periph, union ccb *start_ccb);
837static void proberequestdefaultnegotiation(struct cam_periph *periph);
838static void probedone(struct cam_periph *periph, union ccb *done_ccb);
839static void probecleanup(struct cam_periph *periph);
840static void xpt_find_quirk(struct cam_ed *device);
841#ifdef CAM_NEW_TRAN_CODE
842static void xpt_devise_transport(struct cam_path *path);
843#endif /* CAM_NEW_TRAN_CODE */
844static void xpt_set_transfer_settings(struct ccb_trans_settings *cts,
845 struct cam_ed *device,
846 int async_update);
847static void xpt_toggle_tags(struct cam_path *path);
848static void xpt_start_tags(struct cam_path *path);
849static __inline int xpt_schedule_dev_allocq(struct cam_eb *bus,
850 struct cam_ed *dev);
851static __inline int xpt_schedule_dev_sendq(struct cam_eb *bus,
852 struct cam_ed *dev);
853static __inline int periph_is_queued(struct cam_periph *periph);
854static __inline int device_is_alloc_queued(struct cam_ed *device);
855static __inline int device_is_send_queued(struct cam_ed *device);
856static __inline int dev_allocq_is_runnable(struct cam_devq *devq);
857
858static __inline int
859xpt_schedule_dev_allocq(struct cam_eb *bus, struct cam_ed *dev)
860{
861 int retval;
862
863 if (dev->ccbq.devq_openings > 0) {
864 if ((dev->flags & CAM_DEV_RESIZE_QUEUE_NEEDED) != 0) {
865 cam_ccbq_resize(&dev->ccbq,
866 dev->ccbq.dev_openings
867 + dev->ccbq.dev_active);
868 dev->flags &= ~CAM_DEV_RESIZE_QUEUE_NEEDED;
869 }
870 /*
871 * The priority of a device waiting for CCB resources
872 * is that of the the highest priority peripheral driver
873 * enqueued.
874 */
875 retval = xpt_schedule_dev(&bus->sim->devq->alloc_queue,
876 &dev->alloc_ccb_entry.pinfo,
877 CAMQ_GET_HEAD(&dev->drvq)->priority);
878 } else {
879 retval = 0;
880 }
881
882 return (retval);
883}
884
885static __inline int
886xpt_schedule_dev_sendq(struct cam_eb *bus, struct cam_ed *dev)
887{
888 int retval;
889
890 if (dev->ccbq.dev_openings > 0) {
891 /*
892 * The priority of a device waiting for controller
893 * resources is that of the the highest priority CCB
894 * enqueued.
895 */
896 retval =
897 xpt_schedule_dev(&bus->sim->devq->send_queue,
898 &dev->send_ccb_entry.pinfo,
899 CAMQ_GET_HEAD(&dev->ccbq.queue)->priority);
900 } else {
901 retval = 0;
902 }
903 return (retval);
904}
905
906static __inline int
907periph_is_queued(struct cam_periph *periph)
908{
909 return (periph->pinfo.index != CAM_UNQUEUED_INDEX);
910}
911
912static __inline int
913device_is_alloc_queued(struct cam_ed *device)
914{
915 return (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
916}
917
918static __inline int
919device_is_send_queued(struct cam_ed *device)
920{
921 return (device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX);
922}
923
924static __inline int
925dev_allocq_is_runnable(struct cam_devq *devq)
926{
927 /*
928 * Have work to do.
929 * Have space to do more work.
930 * Allowed to do work.
931 */
932 return ((devq->alloc_queue.qfrozen_cnt == 0)
933 && (devq->alloc_queue.entries > 0)
934 && (devq->alloc_openings > 0));
935}
936
937static void
938xpt_periph_init()
939{
940 make_dev(&xpt_cdevsw, 0, UID_ROOT, GID_OPERATOR, 0600, "xpt0");
941}
942
943static void
944probe_periph_init()
945{
946}
947
948
949static void
950xptdone(struct cam_periph *periph, union ccb *done_ccb)
951{
952 /* Caller will release the CCB */
953 wakeup(&done_ccb->ccb_h.cbfcnp);
954}
955
956static int
957xptopen(struct cdev *dev, int flags, int fmt, struct thread *td)
958{
959 int unit;
960
961 unit = minor(dev) & 0xff;
962
963 /*
964 * Only allow read-write access.
965 */
966 if (((flags & FWRITE) == 0) || ((flags & FREAD) == 0))
967 return(EPERM);
968
969 /*
970 * We don't allow nonblocking access.
971 */
972 if ((flags & O_NONBLOCK) != 0) {
973 printf("xpt%d: can't do nonblocking access\n", unit);
974 return(ENODEV);
975 }
976
977 /*
978 * We only have one transport layer right now. If someone accesses
979 * us via something other than minor number 1, point out their
980 * mistake.
981 */
982 if (unit != 0) {
983 printf("xptopen: got invalid xpt unit %d\n", unit);
984 return(ENXIO);
985 }
986
987 /* Mark ourselves open */
988 xsoftc.flags |= XPT_FLAG_OPEN;
989
990 return(0);
991}
992
993static int
994xptclose(struct cdev *dev, int flag, int fmt, struct thread *td)
995{
996 int unit;
997
998 unit = minor(dev) & 0xff;
999
1000 /*
1001 * We only have one transport layer right now. If someone accesses
1002 * us via something other than minor number 1, point out their
1003 * mistake.
1004 */
1005 if (unit != 0) {
1006 printf("xptclose: got invalid xpt unit %d\n", unit);
1007 return(ENXIO);
1008 }
1009
1010 /* Mark ourselves closed */
1011 xsoftc.flags &= ~XPT_FLAG_OPEN;
1012
1013 return(0);
1014}
1015
1016static int
1017xptioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, struct thread *td)
1018{
1019 int unit, error;
1020
1021 error = 0;
1022 unit = minor(dev) & 0xff;
1023
1024 /*
1025 * We only have one transport layer right now. If someone accesses
1026 * us via something other than minor number 1, point out their
1027 * mistake.
1028 */
1029 if (unit != 0) {
1030 printf("xptioctl: got invalid xpt unit %d\n", unit);
1031 return(ENXIO);
1032 }
1033
1034 switch(cmd) {
1035 /*
1036 * For the transport layer CAMIOCOMMAND ioctl, we really only want
1037 * to accept CCB types that don't quite make sense to send through a
1038 * passthrough driver. XPT_PATH_INQ is an exception to this, as stated
1039 * in the CAM spec.
1040 */
1041 case CAMIOCOMMAND: {
1042 union ccb *ccb;
1043 union ccb *inccb;
1044
1045 inccb = (union ccb *)addr;
1046
1047 switch(inccb->ccb_h.func_code) {
1048 case XPT_SCAN_BUS:
1049 case XPT_RESET_BUS:
1050 if ((inccb->ccb_h.target_id != CAM_TARGET_WILDCARD)
1051 || (inccb->ccb_h.target_lun != CAM_LUN_WILDCARD)) {
1052 error = EINVAL;
1053 break;
1054 }
1055 /* FALLTHROUGH */
1056 case XPT_PATH_INQ:
1057 case XPT_ENG_INQ:
1058 case XPT_SCAN_LUN:
1059
1060 ccb = xpt_alloc_ccb();
1061
1062 /*
1063 * Create a path using the bus, target, and lun the
1064 * user passed in.
1065 */
1066 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
1067 inccb->ccb_h.path_id,
1068 inccb->ccb_h.target_id,
1069 inccb->ccb_h.target_lun) !=
1070 CAM_REQ_CMP){
1071 error = EINVAL;
1072 xpt_free_ccb(ccb);
1073 break;
1074 }
1075 /* Ensure all of our fields are correct */
1076 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path,
1077 inccb->ccb_h.pinfo.priority);
1078 xpt_merge_ccb(ccb, inccb);
1079 ccb->ccb_h.cbfcnp = xptdone;
1080 cam_periph_runccb(ccb, NULL, 0, 0, NULL);
1081 bcopy(ccb, inccb, sizeof(union ccb));
1082 xpt_free_path(ccb->ccb_h.path);
1083 xpt_free_ccb(ccb);
1084 break;
1085
1086 case XPT_DEBUG: {
1087 union ccb ccb;
1088
1089 /*
1090 * This is an immediate CCB, so it's okay to
1091 * allocate it on the stack.
1092 */
1093
1094 /*
1095 * Create a path using the bus, target, and lun the
1096 * user passed in.
1097 */
1098 if (xpt_create_path(&ccb.ccb_h.path, xpt_periph,
1099 inccb->ccb_h.path_id,
1100 inccb->ccb_h.target_id,
1101 inccb->ccb_h.target_lun) !=
1102 CAM_REQ_CMP){
1103 error = EINVAL;
1104 break;
1105 }
1106 /* Ensure all of our fields are correct */
1107 xpt_setup_ccb(&ccb.ccb_h, ccb.ccb_h.path,
1108 inccb->ccb_h.pinfo.priority);
1109 xpt_merge_ccb(&ccb, inccb);
1110 ccb.ccb_h.cbfcnp = xptdone;
1111 xpt_action(&ccb);
1112 bcopy(&ccb, inccb, sizeof(union ccb));
1113 xpt_free_path(ccb.ccb_h.path);
1114 break;
1115
1116 }
1117 case XPT_DEV_MATCH: {
1118 struct cam_periph_map_info mapinfo;
1119 struct cam_path *old_path;
1120
1121 /*
1122 * We can't deal with physical addresses for this
1123 * type of transaction.
1124 */
1125 if (inccb->ccb_h.flags & CAM_DATA_PHYS) {
1126 error = EINVAL;
1127 break;
1128 }
1129
1130 /*
1131 * Save this in case the caller had it set to
1132 * something in particular.
1133 */
1134 old_path = inccb->ccb_h.path;
1135
1136 /*
1137 * We really don't need a path for the matching
1138 * code. The path is needed because of the
1139 * debugging statements in xpt_action(). They
1140 * assume that the CCB has a valid path.
1141 */
1142 inccb->ccb_h.path = xpt_periph->path;
1143
1144 bzero(&mapinfo, sizeof(mapinfo));
1145
1146 /*
1147 * Map the pattern and match buffers into kernel
1148 * virtual address space.
1149 */
1150 error = cam_periph_mapmem(inccb, &mapinfo);
1151
1152 if (error) {
1153 inccb->ccb_h.path = old_path;
1154 break;
1155 }
1156
1157 /*
1158 * This is an immediate CCB, we can send it on directly.
1159 */
1160 xpt_action(inccb);
1161
1162 /*
1163 * Map the buffers back into user space.
1164 */
1165 cam_periph_unmapmem(inccb, &mapinfo);
1166
1167 inccb->ccb_h.path = old_path;
1168
1169 error = 0;
1170 break;
1171 }
1172 default:
1173 error = ENOTSUP;
1174 break;
1175 }
1176 break;
1177 }
1178 /*
1179 * This is the getpassthru ioctl. It takes a XPT_GDEVLIST ccb as input,
1180 * with the periphal driver name and unit name filled in. The other
1181 * fields don't really matter as input. The passthrough driver name
1182 * ("pass"), and unit number are passed back in the ccb. The current
1183 * device generation number, and the index into the device peripheral
1184 * driver list, and the status are also passed back. Note that
1185 * since we do everything in one pass, unlike the XPT_GDEVLIST ccb,
1186 * we never return a status of CAM_GDEVLIST_LIST_CHANGED. It is
1187 * (or rather should be) impossible for the device peripheral driver
1188 * list to change since we look at the whole thing in one pass, and
1189 * we do it with splcam protection.
1190 *
1191 */
1192 case CAMGETPASSTHRU: {
1193 union ccb *ccb;
1194 struct cam_periph *periph;
1195 struct periph_driver **p_drv;
1196 char *name;
1197 u_int unit;
1198 u_int cur_generation;
1199 int base_periph_found;
1200 int splbreaknum;
1201 int s;
1202
1203 ccb = (union ccb *)addr;
1204 unit = ccb->cgdl.unit_number;
1205 name = ccb->cgdl.periph_name;
1206 /*
1207 * Every 100 devices, we want to drop our spl protection to
1208 * give the software interrupt handler a chance to run.
1209 * Most systems won't run into this check, but this should
1210 * avoid starvation in the software interrupt handler in
1211 * large systems.
1212 */
1213 splbreaknum = 100;
1214
1215 ccb = (union ccb *)addr;
1216
1217 base_periph_found = 0;
1218
1219 /*
1220 * Sanity check -- make sure we don't get a null peripheral
1221 * driver name.
1222 */
1223 if (*ccb->cgdl.periph_name == '\0') {
1224 error = EINVAL;
1225 break;
1226 }
1227
1228 /* Keep the list from changing while we traverse it */
1229 s = splcam();
1230ptstartover:
1231 cur_generation = xsoftc.generation;
1232
1233 /* first find our driver in the list of drivers */
1234 for (p_drv = periph_drivers; *p_drv != NULL; p_drv++)
1235 if (strcmp((*p_drv)->driver_name, name) == 0)
1236 break;
1237
1238 if (*p_drv == NULL) {
1239 splx(s);
1240 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1241 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1242 *ccb->cgdl.periph_name = '\0';
1243 ccb->cgdl.unit_number = 0;
1244 error = ENOENT;
1245 break;
1246 }
1247
1248 /*
1249 * Run through every peripheral instance of this driver
1250 * and check to see whether it matches the unit passed
1251 * in by the user. If it does, get out of the loops and
1252 * find the passthrough driver associated with that
1253 * peripheral driver.
1254 */
1255 for (periph = TAILQ_FIRST(&(*p_drv)->units); periph != NULL;
1256 periph = TAILQ_NEXT(periph, unit_links)) {
1257
1258 if (periph->unit_number == unit) {
1259 break;
1260 } else if (--splbreaknum == 0) {
1261 splx(s);
1262 s = splcam();
1263 splbreaknum = 100;
1264 if (cur_generation != xsoftc.generation)
1265 goto ptstartover;
1266 }
1267 }
1268 /*
1269 * If we found the peripheral driver that the user passed
1270 * in, go through all of the peripheral drivers for that
1271 * particular device and look for a passthrough driver.
1272 */
1273 if (periph != NULL) {
1274 struct cam_ed *device;
1275 int i;
1276
1277 base_periph_found = 1;
1278 device = periph->path->device;
1279 for (i = 0, periph = SLIST_FIRST(&device->periphs);
1280 periph != NULL;
1281 periph = SLIST_NEXT(periph, periph_links), i++) {
1282 /*
1283 * Check to see whether we have a
1284 * passthrough device or not.
1285 */
1286 if (strcmp(periph->periph_name, "pass") == 0) {
1287 /*
1288 * Fill in the getdevlist fields.
1289 */
1290 strcpy(ccb->cgdl.periph_name,
1291 periph->periph_name);
1292 ccb->cgdl.unit_number =
1293 periph->unit_number;
1294 if (SLIST_NEXT(periph, periph_links))
1295 ccb->cgdl.status =
1296 CAM_GDEVLIST_MORE_DEVS;
1297 else
1298 ccb->cgdl.status =
1299 CAM_GDEVLIST_LAST_DEVICE;
1300 ccb->cgdl.generation =
1301 device->generation;
1302 ccb->cgdl.index = i;
1303 /*
1304 * Fill in some CCB header fields
1305 * that the user may want.
1306 */
1307 ccb->ccb_h.path_id =
1308 periph->path->bus->path_id;
1309 ccb->ccb_h.target_id =
1310 periph->path->target->target_id;
1311 ccb->ccb_h.target_lun =
1312 periph->path->device->lun_id;
1313 ccb->ccb_h.status = CAM_REQ_CMP;
1314 break;
1315 }
1316 }
1317 }
1318
1319 /*
1320 * If the periph is null here, one of two things has
1321 * happened. The first possibility is that we couldn't
1322 * find the unit number of the particular peripheral driver
1323 * that the user is asking about. e.g. the user asks for
1324 * the passthrough driver for "da11". We find the list of
1325 * "da" peripherals all right, but there is no unit 11.
1326 * The other possibility is that we went through the list
1327 * of peripheral drivers attached to the device structure,
1328 * but didn't find one with the name "pass". Either way,
1329 * we return ENOENT, since we couldn't find something.
1330 */
1331 if (periph == NULL) {
1332 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
1333 ccb->cgdl.status = CAM_GDEVLIST_ERROR;
1334 *ccb->cgdl.periph_name = '\0';
1335 ccb->cgdl.unit_number = 0;
1336 error = ENOENT;
1337 /*
1338 * It is unfortunate that this is even necessary,
1339 * but there are many, many clueless users out there.
1340 * If this is true, the user is looking for the
1341 * passthrough driver, but doesn't have one in his
1342 * kernel.
1343 */
1344 if (base_periph_found == 1) {
1345 printf("xptioctl: pass driver is not in the "
1346 "kernel\n");
1347 printf("xptioctl: put \"device pass0\" in "
1348 "your kernel config file\n");
1349 }
1350 }
1351 splx(s);
1352 break;
1353 }
1354 default:
1355 error = ENOTTY;
1356 break;
1357 }
1358
1359 return(error);
1360}
1361
1362static int
1363cam_module_event_handler(module_t mod, int what, void *arg)
1364{
1365 if (what == MOD_LOAD) {
1366 xpt_init(NULL);
1367 } else if (what == MOD_UNLOAD) {
1368 return EBUSY;
1369 } else {
1370 return EOPNOTSUPP;
1371 }
1372
1373 return 0;
1374}
1375
1376/* Functions accessed by the peripheral drivers */
1377static void
1378xpt_init(dummy)
1379 void *dummy;
1380{
1381 struct cam_sim *xpt_sim;
1382 struct cam_path *path;
1383 struct cam_devq *devq;
1384 cam_status status;
1385
1386 TAILQ_INIT(&xpt_busses);
1387 TAILQ_INIT(&cam_bioq);
1388 SLIST_INIT(&ccb_freeq);
1389 STAILQ_INIT(&highpowerq);
1390
1391 mtx_init(&cam_bioq_lock, "CAM BIOQ lock", NULL, MTX_DEF);
1392
1393 /*
1394 * The xpt layer is, itself, the equivelent of a SIM.
1395 * Allow 16 ccbs in the ccb pool for it. This should
1396 * give decent parallelism when we probe busses and
1397 * perform other XPT functions.
1398 */
1399 devq = cam_simq_alloc(16);
1400 xpt_sim = cam_sim_alloc(xptaction,
1401 xptpoll,
1402 "xpt",
1403 /*softc*/NULL,
1404 /*unit*/0,
1405 /*max_dev_transactions*/0,
1406 /*max_tagged_dev_transactions*/0,
1407 devq);
1408 xpt_max_ccbs = 16;
1409
1410 xpt_bus_register(xpt_sim, /*bus #*/0);
1411
1412 /*
1413 * Looking at the XPT from the SIM layer, the XPT is
1414 * the equivelent of a peripheral driver. Allocate
1415 * a peripheral driver entry for us.
1416 */
1417 if ((status = xpt_create_path(&path, NULL, CAM_XPT_PATH_ID,
1418 CAM_TARGET_WILDCARD,
1419 CAM_LUN_WILDCARD)) != CAM_REQ_CMP) {
1420 printf("xpt_init: xpt_create_path failed with status %#x,"
1421 " failing attach\n", status);
1422 return;
1423 }
1424
1425 cam_periph_alloc(xptregister, NULL, NULL, NULL, "xpt", CAM_PERIPH_BIO,
1426 path, NULL, 0, NULL);
1427 xpt_free_path(path);
1428
1429 xpt_sim->softc = xpt_periph;
1430
1431 /*
1432 * Register a callback for when interrupts are enabled.
1433 */
1434 xpt_config_hook =
1435 (struct intr_config_hook *)malloc(sizeof(struct intr_config_hook),
1436 M_TEMP, M_NOWAIT | M_ZERO);
1437 if (xpt_config_hook == NULL) {
1438 printf("xpt_init: Cannot malloc config hook "
1439 "- failing attach\n");
1440 return;
1441 }
1442
1443 xpt_config_hook->ich_func = xpt_config;
1444 if (config_intrhook_establish(xpt_config_hook) != 0) {
1445 free (xpt_config_hook, M_TEMP);
1446 printf("xpt_init: config_intrhook_establish failed "
1447 "- failing attach\n");
1448 }
1449
1450 /* Install our software interrupt handlers */
1451 swi_add(NULL, "cambio", camisr, &cam_bioq, SWI_CAMBIO, 0, &cambio_ih);
1452}
1453
1454static cam_status
1455xptregister(struct cam_periph *periph, void *arg)
1456{
1457 if (periph == NULL) {
1458 printf("xptregister: periph was NULL!!\n");
1459 return(CAM_REQ_CMP_ERR);
1460 }
1461
1462 periph->softc = NULL;
1463
1464 xpt_periph = periph;
1465
1466 return(CAM_REQ_CMP);
1467}
1468
1469int32_t
1470xpt_add_periph(struct cam_periph *periph)
1471{
1472 struct cam_ed *device;
1473 int32_t status;
1474 struct periph_list *periph_head;
1475
1476 GIANT_REQUIRED;
1477
1478 device = periph->path->device;
1479
1480 periph_head = &device->periphs;
1481
1482 status = CAM_REQ_CMP;
1483
1484 if (device != NULL) {
1485 int s;
1486
1487 /*
1488 * Make room for this peripheral
1489 * so it will fit in the queue
1490 * when it's scheduled to run
1491 */
1492 s = splsoftcam();
1493 status = camq_resize(&device->drvq,
1494 device->drvq.array_size + 1);
1495
1496 device->generation++;
1497
1498 SLIST_INSERT_HEAD(periph_head, periph, periph_links);
1499
1500 splx(s);
1501 }
1502
1503 xsoftc.generation++;
1504
1505 return (status);
1506}
1507
1508void
1509xpt_remove_periph(struct cam_periph *periph)
1510{
1511 struct cam_ed *device;
1512
1513 GIANT_REQUIRED;
1514
1515 device = periph->path->device;
1516
1517 if (device != NULL) {
1518 int s;
1519 struct periph_list *periph_head;
1520
1521 periph_head = &device->periphs;
1522
1523 /* Release the slot for this peripheral */
1524 s = splsoftcam();
1525 camq_resize(&device->drvq, device->drvq.array_size - 1);
1526
1527 device->generation++;
1528
1529 SLIST_REMOVE(periph_head, periph, cam_periph, periph_links);
1530
1531 splx(s);
1532 }
1533
1534 xsoftc.generation++;
1535
1536}
1537
1538#ifdef CAM_NEW_TRAN_CODE
1539
1540void
1541xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1542{
1543 struct ccb_pathinq cpi;
1544 struct ccb_trans_settings cts;
1545 struct cam_path *path;
1546 u_int speed;
1547 u_int freq;
1548 u_int mb;
1549 int s;
1550
1551 GIANT_REQUIRED;
1552
1553 path = periph->path;
1554 /*
1555 * To ensure that this is printed in one piece,
1556 * mask out CAM interrupts.
1557 */
1558 s = splsoftcam();
1559 printf("%s%d at %s%d bus %d target %d lun %d\n",
1560 periph->periph_name, periph->unit_number,
1561 path->bus->sim->sim_name,
1562 path->bus->sim->unit_number,
1563 path->bus->sim->bus_id,
1564 path->target->target_id,
1565 path->device->lun_id);
1566 printf("%s%d: ", periph->periph_name, periph->unit_number);
1567 scsi_print_inquiry(&path->device->inq_data);
1568 if (bootverbose && path->device->serial_num_len > 0) {
1569 /* Don't wrap the screen - print only the first 60 chars */
1570 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1571 periph->unit_number, path->device->serial_num);
1572 }
1573 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1574 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1575 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1576 xpt_action((union ccb*)&cts);
1577
1578 /* Ask the SIM for its base transfer speed */
1579 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1580 cpi.ccb_h.func_code = XPT_PATH_INQ;
1581 xpt_action((union ccb *)&cpi);
1582
1583 speed = cpi.base_transfer_speed;
1584 freq = 0;
1585 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1586 struct ccb_trans_settings_spi *spi;
1587
1588 spi = &cts.xport_specific.spi;
1589 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
1590 && spi->sync_offset != 0) {
1591 freq = scsi_calc_syncsrate(spi->sync_period);
1592 speed = freq;
1593 }
1594
1595 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
1596 speed *= (0x01 << spi->bus_width);
1597 }
1598
1599 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1600 struct ccb_trans_settings_fc *fc = &cts.xport_specific.fc;
1601 if (fc->valid & CTS_FC_VALID_SPEED) {
1602 speed = fc->bitrate;
1603 }
1604 }
1605
1606 mb = speed / 1000;
1607 if (mb > 0)
1608 printf("%s%d: %d.%03dMB/s transfers",
1609 periph->periph_name, periph->unit_number,
1610 mb, speed % 1000);
1611 else
1612 printf("%s%d: %dKB/s transfers", periph->periph_name,
1613 periph->unit_number, speed);
1614 /* Report additional information about SPI connections */
1615 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
1616 struct ccb_trans_settings_spi *spi;
1617
1618 spi = &cts.xport_specific.spi;
1619 if (freq != 0) {
1620 printf(" (%d.%03dMHz%s, offset %d", freq / 1000,
1621 freq % 1000,
1622 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
1623 ? " DT" : "",
1624 spi->sync_offset);
1625 }
1626 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
1627 && spi->bus_width > 0) {
1628 if (freq != 0) {
1629 printf(", ");
1630 } else {
1631 printf(" (");
1632 }
1633 printf("%dbit)", 8 * (0x01 << spi->bus_width));
1634 } else if (freq != 0) {
1635 printf(")");
1636 }
1637 }
1638 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
1639 struct ccb_trans_settings_fc *fc;
1640
1641 fc = &cts.xport_specific.fc;
1642 if (fc->valid & CTS_FC_VALID_WWNN)
1643 printf(" WWNN 0x%llx", (long long) fc->wwnn);
1644 if (fc->valid & CTS_FC_VALID_WWPN)
1645 printf(" WWPN 0x%llx", (long long) fc->wwpn);
1646 if (fc->valid & CTS_FC_VALID_PORT)
1647 printf(" PortID 0x%x", fc->port);
1648 }
1649
1650 if (path->device->inq_flags & SID_CmdQue
1651 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1652 printf("\n%s%d: Tagged Queueing Enabled",
1653 periph->periph_name, periph->unit_number);
1654 }
1655 printf("\n");
1656
1657 /*
1658 * We only want to print the caller's announce string if they've
1659 * passed one in..
1660 */
1661 if (announce_string != NULL)
1662 printf("%s%d: %s\n", periph->periph_name,
1663 periph->unit_number, announce_string);
1664 splx(s);
1665}
1666#else /* CAM_NEW_TRAN_CODE */
1667void
1668xpt_announce_periph(struct cam_periph *periph, char *announce_string)
1669{
1670 int s;
1671 u_int mb;
1672 struct cam_path *path;
1673 struct ccb_trans_settings cts;
1674
1675 GIANT_REQUIRED;
1676
1677 path = periph->path;
1678 /*
1679 * To ensure that this is printed in one piece,
1680 * mask out CAM interrupts.
1681 */
1682 s = splsoftcam();
1683 printf("%s%d at %s%d bus %d target %d lun %d\n",
1684 periph->periph_name, periph->unit_number,
1685 path->bus->sim->sim_name,
1686 path->bus->sim->unit_number,
1687 path->bus->sim->bus_id,
1688 path->target->target_id,
1689 path->device->lun_id);
1690 printf("%s%d: ", periph->periph_name, periph->unit_number);
1691 scsi_print_inquiry(&path->device->inq_data);
1692 if ((bootverbose)
1693 && (path->device->serial_num_len > 0)) {
1694 /* Don't wrap the screen - print only the first 60 chars */
1695 printf("%s%d: Serial Number %.60s\n", periph->periph_name,
1696 periph->unit_number, path->device->serial_num);
1697 }
1698 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
1699 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1700 cts.flags = CCB_TRANS_CURRENT_SETTINGS;
1701 xpt_action((union ccb*)&cts);
1702 if (cts.ccb_h.status == CAM_REQ_CMP) {
1703 u_int speed;
1704 u_int freq;
1705
1706 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1707 && cts.sync_offset != 0) {
1708 freq = scsi_calc_syncsrate(cts.sync_period);
1709 speed = freq;
1710 } else {
1711 struct ccb_pathinq cpi;
1712
1713 /* Ask the SIM for its base transfer speed */
1714 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
1715 cpi.ccb_h.func_code = XPT_PATH_INQ;
1716 xpt_action((union ccb *)&cpi);
1717
1718 speed = cpi.base_transfer_speed;
1719 freq = 0;
1720 }
1721 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0)
1722 speed *= (0x01 << cts.bus_width);
1723 mb = speed / 1000;
1724 if (mb > 0)
1725 printf("%s%d: %d.%03dMB/s transfers",
1726 periph->periph_name, periph->unit_number,
1727 mb, speed % 1000);
1728 else
1729 printf("%s%d: %dKB/s transfers", periph->periph_name,
1730 periph->unit_number, speed);
1731 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1732 && cts.sync_offset != 0) {
1733 printf(" (%d.%03dMHz, offset %d", freq / 1000,
1734 freq % 1000, cts.sync_offset);
1735 }
1736 if ((cts.valid & CCB_TRANS_BUS_WIDTH_VALID) != 0
1737 && cts.bus_width > 0) {
1738 if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1739 && cts.sync_offset != 0) {
1740 printf(", ");
1741 } else {
1742 printf(" (");
1743 }
1744 printf("%dbit)", 8 * (0x01 << cts.bus_width));
1745 } else if ((cts.valid & CCB_TRANS_SYNC_OFFSET_VALID) != 0
1746 && cts.sync_offset != 0) {
1747 printf(")");
1748 }
1749
1750 if (path->device->inq_flags & SID_CmdQue
1751 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1752 printf(", Tagged Queueing Enabled");
1753 }
1754
1755 printf("\n");
1756 } else if (path->device->inq_flags & SID_CmdQue
1757 || path->device->flags & CAM_DEV_TAG_AFTER_COUNT) {
1758 printf("%s%d: Tagged Queueing Enabled\n",
1759 periph->periph_name, periph->unit_number);
1760 }
1761
1762 /*
1763 * We only want to print the caller's announce string if they've
1764 * passed one in..
1765 */
1766 if (announce_string != NULL)
1767 printf("%s%d: %s\n", periph->periph_name,
1768 periph->unit_number, announce_string);
1769 splx(s);
1770}
1771
1772#endif /* CAM_NEW_TRAN_CODE */
1773
1774static dev_match_ret
1775xptbusmatch(struct dev_match_pattern *patterns, u_int num_patterns,
1776 struct cam_eb *bus)
1777{
1778 dev_match_ret retval;
1779 int i;
1780
1781 retval = DM_RET_NONE;
1782
1783 /*
1784 * If we aren't given something to match against, that's an error.
1785 */
1786 if (bus == NULL)
1787 return(DM_RET_ERROR);
1788
1789 /*
1790 * If there are no match entries, then this bus matches no
1791 * matter what.
1792 */
1793 if ((patterns == NULL) || (num_patterns == 0))
1794 return(DM_RET_DESCEND | DM_RET_COPY);
1795
1796 for (i = 0; i < num_patterns; i++) {
1797 struct bus_match_pattern *cur_pattern;
1798
1799 /*
1800 * If the pattern in question isn't for a bus node, we
1801 * aren't interested. However, we do indicate to the
1802 * calling routine that we should continue descending the
1803 * tree, since the user wants to match against lower-level
1804 * EDT elements.
1805 */
1806 if (patterns[i].type != DEV_MATCH_BUS) {
1807 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1808 retval |= DM_RET_DESCEND;
1809 continue;
1810 }
1811
1812 cur_pattern = &patterns[i].pattern.bus_pattern;
1813
1814 /*
1815 * If they want to match any bus node, we give them any
1816 * device node.
1817 */
1818 if (cur_pattern->flags == BUS_MATCH_ANY) {
1819 /* set the copy flag */
1820 retval |= DM_RET_COPY;
1821
1822 /*
1823 * If we've already decided on an action, go ahead
1824 * and return.
1825 */
1826 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1827 return(retval);
1828 }
1829
1830 /*
1831 * Not sure why someone would do this...
1832 */
1833 if (cur_pattern->flags == BUS_MATCH_NONE)
1834 continue;
1835
1836 if (((cur_pattern->flags & BUS_MATCH_PATH) != 0)
1837 && (cur_pattern->path_id != bus->path_id))
1838 continue;
1839
1840 if (((cur_pattern->flags & BUS_MATCH_BUS_ID) != 0)
1841 && (cur_pattern->bus_id != bus->sim->bus_id))
1842 continue;
1843
1844 if (((cur_pattern->flags & BUS_MATCH_UNIT) != 0)
1845 && (cur_pattern->unit_number != bus->sim->unit_number))
1846 continue;
1847
1848 if (((cur_pattern->flags & BUS_MATCH_NAME) != 0)
1849 && (strncmp(cur_pattern->dev_name, bus->sim->sim_name,
1850 DEV_IDLEN) != 0))
1851 continue;
1852
1853 /*
1854 * If we get to this point, the user definitely wants
1855 * information on this bus. So tell the caller to copy the
1856 * data out.
1857 */
1858 retval |= DM_RET_COPY;
1859
1860 /*
1861 * If the return action has been set to descend, then we
1862 * know that we've already seen a non-bus matching
1863 * expression, therefore we need to further descend the tree.
1864 * This won't change by continuing around the loop, so we
1865 * go ahead and return. If we haven't seen a non-bus
1866 * matching expression, we keep going around the loop until
1867 * we exhaust the matching expressions. We'll set the stop
1868 * flag once we fall out of the loop.
1869 */
1870 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1871 return(retval);
1872 }
1873
1874 /*
1875 * If the return action hasn't been set to descend yet, that means
1876 * we haven't seen anything other than bus matching patterns. So
1877 * tell the caller to stop descending the tree -- the user doesn't
1878 * want to match against lower level tree elements.
1879 */
1880 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1881 retval |= DM_RET_STOP;
1882
1883 return(retval);
1884}
1885
1886static dev_match_ret
1887xptdevicematch(struct dev_match_pattern *patterns, u_int num_patterns,
1888 struct cam_ed *device)
1889{
1890 dev_match_ret retval;
1891 int i;
1892
1893 retval = DM_RET_NONE;
1894
1895 /*
1896 * If we aren't given something to match against, that's an error.
1897 */
1898 if (device == NULL)
1899 return(DM_RET_ERROR);
1900
1901 /*
1902 * If there are no match entries, then this device matches no
1903 * matter what.
1904 */
1905 if ((patterns == NULL) || (num_patterns == 0))
1906 return(DM_RET_DESCEND | DM_RET_COPY);
1907
1908 for (i = 0; i < num_patterns; i++) {
1909 struct device_match_pattern *cur_pattern;
1910
1911 /*
1912 * If the pattern in question isn't for a device node, we
1913 * aren't interested.
1914 */
1915 if (patterns[i].type != DEV_MATCH_DEVICE) {
1916 if ((patterns[i].type == DEV_MATCH_PERIPH)
1917 && ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE))
1918 retval |= DM_RET_DESCEND;
1919 continue;
1920 }
1921
1922 cur_pattern = &patterns[i].pattern.device_pattern;
1923
1924 /*
1925 * If they want to match any device node, we give them any
1926 * device node.
1927 */
1928 if (cur_pattern->flags == DEV_MATCH_ANY) {
1929 /* set the copy flag */
1930 retval |= DM_RET_COPY;
1931
1932
1933 /*
1934 * If we've already decided on an action, go ahead
1935 * and return.
1936 */
1937 if ((retval & DM_RET_ACTION_MASK) != DM_RET_NONE)
1938 return(retval);
1939 }
1940
1941 /*
1942 * Not sure why someone would do this...
1943 */
1944 if (cur_pattern->flags == DEV_MATCH_NONE)
1945 continue;
1946
1947 if (((cur_pattern->flags & DEV_MATCH_PATH) != 0)
1948 && (cur_pattern->path_id != device->target->bus->path_id))
1949 continue;
1950
1951 if (((cur_pattern->flags & DEV_MATCH_TARGET) != 0)
1952 && (cur_pattern->target_id != device->target->target_id))
1953 continue;
1954
1955 if (((cur_pattern->flags & DEV_MATCH_LUN) != 0)
1956 && (cur_pattern->target_lun != device->lun_id))
1957 continue;
1958
1959 if (((cur_pattern->flags & DEV_MATCH_INQUIRY) != 0)
1960 && (cam_quirkmatch((caddr_t)&device->inq_data,
1961 (caddr_t)&cur_pattern->inq_pat,
1962 1, sizeof(cur_pattern->inq_pat),
1963 scsi_static_inquiry_match) == NULL))
1964 continue;
1965
1966 /*
1967 * If we get to this point, the user definitely wants
1968 * information on this device. So tell the caller to copy
1969 * the data out.
1970 */
1971 retval |= DM_RET_COPY;
1972
1973 /*
1974 * If the return action has been set to descend, then we
1975 * know that we've already seen a peripheral matching
1976 * expression, therefore we need to further descend the tree.
1977 * This won't change by continuing around the loop, so we
1978 * go ahead and return. If we haven't seen a peripheral
1979 * matching expression, we keep going around the loop until
1980 * we exhaust the matching expressions. We'll set the stop
1981 * flag once we fall out of the loop.
1982 */
1983 if ((retval & DM_RET_ACTION_MASK) == DM_RET_DESCEND)
1984 return(retval);
1985 }
1986
1987 /*
1988 * If the return action hasn't been set to descend yet, that means
1989 * we haven't seen any peripheral matching patterns. So tell the
1990 * caller to stop descending the tree -- the user doesn't want to
1991 * match against lower level tree elements.
1992 */
1993 if ((retval & DM_RET_ACTION_MASK) == DM_RET_NONE)
1994 retval |= DM_RET_STOP;
1995
1996 return(retval);
1997}
1998
1999/*
2000 * Match a single peripheral against any number of match patterns.
2001 */
2002static dev_match_ret
2003xptperiphmatch(struct dev_match_pattern *patterns, u_int num_patterns,
2004 struct cam_periph *periph)
2005{
2006 dev_match_ret retval;
2007 int i;
2008
2009 /*
2010 * If we aren't given something to match against, that's an error.
2011 */
2012 if (periph == NULL)
2013 return(DM_RET_ERROR);
2014
2015 /*
2016 * If there are no match entries, then this peripheral matches no
2017 * matter what.
2018 */
2019 if ((patterns == NULL) || (num_patterns == 0))
2020 return(DM_RET_STOP | DM_RET_COPY);
2021
2022 /*
2023 * There aren't any nodes below a peripheral node, so there's no
2024 * reason to descend the tree any further.
2025 */
2026 retval = DM_RET_STOP;
2027
2028 for (i = 0; i < num_patterns; i++) {
2029 struct periph_match_pattern *cur_pattern;
2030
2031 /*
2032 * If the pattern in question isn't for a peripheral, we
2033 * aren't interested.
2034 */
2035 if (patterns[i].type != DEV_MATCH_PERIPH)
2036 continue;
2037
2038 cur_pattern = &patterns[i].pattern.periph_pattern;
2039
2040 /*
2041 * If they want to match on anything, then we will do so.
2042 */
2043 if (cur_pattern->flags == PERIPH_MATCH_ANY) {
2044 /* set the copy flag */
2045 retval |= DM_RET_COPY;
2046
2047 /*
2048 * We've already set the return action to stop,
2049 * since there are no nodes below peripherals in
2050 * the tree.
2051 */
2052 return(retval);
2053 }
2054
2055 /*
2056 * Not sure why someone would do this...
2057 */
2058 if (cur_pattern->flags == PERIPH_MATCH_NONE)
2059 continue;
2060
2061 if (((cur_pattern->flags & PERIPH_MATCH_PATH) != 0)
2062 && (cur_pattern->path_id != periph->path->bus->path_id))
2063 continue;
2064
2065 /*
2066 * For the target and lun id's, we have to make sure the
2067 * target and lun pointers aren't NULL. The xpt peripheral
2068 * has a wildcard target and device.
2069 */
2070 if (((cur_pattern->flags & PERIPH_MATCH_TARGET) != 0)
2071 && ((periph->path->target == NULL)
2072 ||(cur_pattern->target_id != periph->path->target->target_id)))
2073 continue;
2074
2075 if (((cur_pattern->flags & PERIPH_MATCH_LUN) != 0)
2076 && ((periph->path->device == NULL)
2077 || (cur_pattern->target_lun != periph->path->device->lun_id)))
2078 continue;
2079
2080 if (((cur_pattern->flags & PERIPH_MATCH_UNIT) != 0)
2081 && (cur_pattern->unit_number != periph->unit_number))
2082 continue;
2083
2084 if (((cur_pattern->flags & PERIPH_MATCH_NAME) != 0)
2085 && (strncmp(cur_pattern->periph_name, periph->periph_name,
2086 DEV_IDLEN) != 0))
2087 continue;
2088
2089 /*
2090 * If we get to this point, the user definitely wants
2091 * information on this peripheral. So tell the caller to
2092 * copy the data out.
2093 */
2094 retval |= DM_RET_COPY;
2095
2096 /*
2097 * The return action has already been set to stop, since
2098 * peripherals don't have any nodes below them in the EDT.
2099 */
2100 return(retval);
2101 }
2102
2103 /*
2104 * If we get to this point, the peripheral that was passed in
2105 * doesn't match any of the patterns.
2106 */
2107 return(retval);
2108}
2109
2110static int
2111xptedtbusfunc(struct cam_eb *bus, void *arg)
2112{
2113 struct ccb_dev_match *cdm;
2114 dev_match_ret retval;
2115
2116 cdm = (struct ccb_dev_match *)arg;
2117
2118 /*
2119 * If our position is for something deeper in the tree, that means
2120 * that we've already seen this node. So, we keep going down.
2121 */
2122 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2123 && (cdm->pos.cookie.bus == bus)
2124 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2125 && (cdm->pos.cookie.target != NULL))
2126 retval = DM_RET_DESCEND;
2127 else
2128 retval = xptbusmatch(cdm->patterns, cdm->num_patterns, bus);
2129
2130 /*
2131 * If we got an error, bail out of the search.
2132 */
2133 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2134 cdm->status = CAM_DEV_MATCH_ERROR;
2135 return(0);
2136 }
2137
2138 /*
2139 * If the copy flag is set, copy this bus out.
2140 */
2141 if (retval & DM_RET_COPY) {
2142 int spaceleft, j;
2143
2144 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2145 sizeof(struct dev_match_result));
2146
2147 /*
2148 * If we don't have enough space to put in another
2149 * match result, save our position and tell the
2150 * user there are more devices to check.
2151 */
2152 if (spaceleft < sizeof(struct dev_match_result)) {
2153 bzero(&cdm->pos, sizeof(cdm->pos));
2154 cdm->pos.position_type =
2155 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS;
2156
2157 cdm->pos.cookie.bus = bus;
2158 cdm->pos.generations[CAM_BUS_GENERATION]=
2159 bus_generation;
2160 cdm->status = CAM_DEV_MATCH_MORE;
2161 return(0);
2162 }
2163 j = cdm->num_matches;
2164 cdm->num_matches++;
2165 cdm->matches[j].type = DEV_MATCH_BUS;
2166 cdm->matches[j].result.bus_result.path_id = bus->path_id;
2167 cdm->matches[j].result.bus_result.bus_id = bus->sim->bus_id;
2168 cdm->matches[j].result.bus_result.unit_number =
2169 bus->sim->unit_number;
2170 strncpy(cdm->matches[j].result.bus_result.dev_name,
2171 bus->sim->sim_name, DEV_IDLEN);
2172 }
2173
2174 /*
2175 * If the user is only interested in busses, there's no
2176 * reason to descend to the next level in the tree.
2177 */
2178 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2179 return(1);
2180
2181 /*
2182 * If there is a target generation recorded, check it to
2183 * make sure the target list hasn't changed.
2184 */
2185 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2186 && (bus == cdm->pos.cookie.bus)
2187 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2188 && (cdm->pos.generations[CAM_TARGET_GENERATION] != 0)
2189 && (cdm->pos.generations[CAM_TARGET_GENERATION] !=
2190 bus->generation)) {
2191 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2192 return(0);
2193 }
2194
2195 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2196 && (cdm->pos.cookie.bus == bus)
2197 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2198 && (cdm->pos.cookie.target != NULL))
2199 return(xpttargettraverse(bus,
2200 (struct cam_et *)cdm->pos.cookie.target,
2201 xptedttargetfunc, arg));
2202 else
2203 return(xpttargettraverse(bus, NULL, xptedttargetfunc, arg));
2204}
2205
2206static int
2207xptedttargetfunc(struct cam_et *target, void *arg)
2208{
2209 struct ccb_dev_match *cdm;
2210
2211 cdm = (struct ccb_dev_match *)arg;
2212
2213 /*
2214 * If there is a device list generation recorded, check it to
2215 * make sure the device list hasn't changed.
2216 */
2217 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2218 && (cdm->pos.cookie.bus == target->bus)
2219 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2220 && (cdm->pos.cookie.target == target)
2221 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2222 && (cdm->pos.generations[CAM_DEV_GENERATION] != 0)
2223 && (cdm->pos.generations[CAM_DEV_GENERATION] !=
2224 target->generation)) {
2225 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2226 return(0);
2227 }
2228
2229 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2230 && (cdm->pos.cookie.bus == target->bus)
2231 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2232 && (cdm->pos.cookie.target == target)
2233 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2234 && (cdm->pos.cookie.device != NULL))
2235 return(xptdevicetraverse(target,
2236 (struct cam_ed *)cdm->pos.cookie.device,
2237 xptedtdevicefunc, arg));
2238 else
2239 return(xptdevicetraverse(target, NULL, xptedtdevicefunc, arg));
2240}
2241
2242static int
2243xptedtdevicefunc(struct cam_ed *device, void *arg)
2244{
2245
2246 struct ccb_dev_match *cdm;
2247 dev_match_ret retval;
2248
2249 cdm = (struct ccb_dev_match *)arg;
2250
2251 /*
2252 * If our position is for something deeper in the tree, that means
2253 * that we've already seen this node. So, we keep going down.
2254 */
2255 if ((cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2256 && (cdm->pos.cookie.device == device)
2257 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2258 && (cdm->pos.cookie.periph != NULL))
2259 retval = DM_RET_DESCEND;
2260 else
2261 retval = xptdevicematch(cdm->patterns, cdm->num_patterns,
2262 device);
2263
2264 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2265 cdm->status = CAM_DEV_MATCH_ERROR;
2266 return(0);
2267 }
2268
2269 /*
2270 * If the copy flag is set, copy this device out.
2271 */
2272 if (retval & DM_RET_COPY) {
2273 int spaceleft, j;
2274
2275 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2276 sizeof(struct dev_match_result));
2277
2278 /*
2279 * If we don't have enough space to put in another
2280 * match result, save our position and tell the
2281 * user there are more devices to check.
2282 */
2283 if (spaceleft < sizeof(struct dev_match_result)) {
2284 bzero(&cdm->pos, sizeof(cdm->pos));
2285 cdm->pos.position_type =
2286 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2287 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE;
2288
2289 cdm->pos.cookie.bus = device->target->bus;
2290 cdm->pos.generations[CAM_BUS_GENERATION]=
2291 bus_generation;
2292 cdm->pos.cookie.target = device->target;
2293 cdm->pos.generations[CAM_TARGET_GENERATION] =
2294 device->target->bus->generation;
2295 cdm->pos.cookie.device = device;
2296 cdm->pos.generations[CAM_DEV_GENERATION] =
2297 device->target->generation;
2298 cdm->status = CAM_DEV_MATCH_MORE;
2299 return(0);
2300 }
2301 j = cdm->num_matches;
2302 cdm->num_matches++;
2303 cdm->matches[j].type = DEV_MATCH_DEVICE;
2304 cdm->matches[j].result.device_result.path_id =
2305 device->target->bus->path_id;
2306 cdm->matches[j].result.device_result.target_id =
2307 device->target->target_id;
2308 cdm->matches[j].result.device_result.target_lun =
2309 device->lun_id;
2310 bcopy(&device->inq_data,
2311 &cdm->matches[j].result.device_result.inq_data,
2312 sizeof(struct scsi_inquiry_data));
2313
2314 /* Let the user know whether this device is unconfigured */
2315 if (device->flags & CAM_DEV_UNCONFIGURED)
2316 cdm->matches[j].result.device_result.flags =
2317 DEV_RESULT_UNCONFIGURED;
2318 else
2319 cdm->matches[j].result.device_result.flags =
2320 DEV_RESULT_NOFLAG;
2321 }
2322
2323 /*
2324 * If the user isn't interested in peripherals, don't descend
2325 * the tree any further.
2326 */
2327 if ((retval & DM_RET_ACTION_MASK) == DM_RET_STOP)
2328 return(1);
2329
2330 /*
2331 * If there is a peripheral list generation recorded, make sure
2332 * it hasn't changed.
2333 */
2334 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2335 && (device->target->bus == cdm->pos.cookie.bus)
2336 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2337 && (device->target == cdm->pos.cookie.target)
2338 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2339 && (device == cdm->pos.cookie.device)
2340 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2341 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2342 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2343 device->generation)){
2344 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2345 return(0);
2346 }
2347
2348 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2349 && (cdm->pos.cookie.bus == device->target->bus)
2350 && (cdm->pos.position_type & CAM_DEV_POS_TARGET)
2351 && (cdm->pos.cookie.target == device->target)
2352 && (cdm->pos.position_type & CAM_DEV_POS_DEVICE)
2353 && (cdm->pos.cookie.device == device)
2354 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2355 && (cdm->pos.cookie.periph != NULL))
2356 return(xptperiphtraverse(device,
2357 (struct cam_periph *)cdm->pos.cookie.periph,
2358 xptedtperiphfunc, arg));
2359 else
2360 return(xptperiphtraverse(device, NULL, xptedtperiphfunc, arg));
2361}
2362
2363static int
2364xptedtperiphfunc(struct cam_periph *periph, void *arg)
2365{
2366 struct ccb_dev_match *cdm;
2367 dev_match_ret retval;
2368
2369 cdm = (struct ccb_dev_match *)arg;
2370
2371 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2372
2373 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2374 cdm->status = CAM_DEV_MATCH_ERROR;
2375 return(0);
2376 }
2377
2378 /*
2379 * If the copy flag is set, copy this peripheral out.
2380 */
2381 if (retval & DM_RET_COPY) {
2382 int spaceleft, j;
2383
2384 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2385 sizeof(struct dev_match_result));
2386
2387 /*
2388 * If we don't have enough space to put in another
2389 * match result, save our position and tell the
2390 * user there are more devices to check.
2391 */
2392 if (spaceleft < sizeof(struct dev_match_result)) {
2393 bzero(&cdm->pos, sizeof(cdm->pos));
2394 cdm->pos.position_type =
2395 CAM_DEV_POS_EDT | CAM_DEV_POS_BUS |
2396 CAM_DEV_POS_TARGET | CAM_DEV_POS_DEVICE |
2397 CAM_DEV_POS_PERIPH;
2398
2399 cdm->pos.cookie.bus = periph->path->bus;
2400 cdm->pos.generations[CAM_BUS_GENERATION]=
2401 bus_generation;
2402 cdm->pos.cookie.target = periph->path->target;
2403 cdm->pos.generations[CAM_TARGET_GENERATION] =
2404 periph->path->bus->generation;
2405 cdm->pos.cookie.device = periph->path->device;
2406 cdm->pos.generations[CAM_DEV_GENERATION] =
2407 periph->path->target->generation;
2408 cdm->pos.cookie.periph = periph;
2409 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2410 periph->path->device->generation;
2411 cdm->status = CAM_DEV_MATCH_MORE;
2412 return(0);
2413 }
2414
2415 j = cdm->num_matches;
2416 cdm->num_matches++;
2417 cdm->matches[j].type = DEV_MATCH_PERIPH;
2418 cdm->matches[j].result.periph_result.path_id =
2419 periph->path->bus->path_id;
2420 cdm->matches[j].result.periph_result.target_id =
2421 periph->path->target->target_id;
2422 cdm->matches[j].result.periph_result.target_lun =
2423 periph->path->device->lun_id;
2424 cdm->matches[j].result.periph_result.unit_number =
2425 periph->unit_number;
2426 strncpy(cdm->matches[j].result.periph_result.periph_name,
2427 periph->periph_name, DEV_IDLEN);
2428 }
2429
2430 return(1);
2431}
2432
2433static int
2434xptedtmatch(struct ccb_dev_match *cdm)
2435{
2436 int ret;
2437
2438 cdm->num_matches = 0;
2439
2440 /*
2441 * Check the bus list generation. If it has changed, the user
2442 * needs to reset everything and start over.
2443 */
2444 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2445 && (cdm->pos.generations[CAM_BUS_GENERATION] != 0)
2446 && (cdm->pos.generations[CAM_BUS_GENERATION] != bus_generation)) {
2447 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2448 return(0);
2449 }
2450
2451 if ((cdm->pos.position_type & CAM_DEV_POS_BUS)
2452 && (cdm->pos.cookie.bus != NULL))
2453 ret = xptbustraverse((struct cam_eb *)cdm->pos.cookie.bus,
2454 xptedtbusfunc, cdm);
2455 else
2456 ret = xptbustraverse(NULL, xptedtbusfunc, cdm);
2457
2458 /*
2459 * If we get back 0, that means that we had to stop before fully
2460 * traversing the EDT. It also means that one of the subroutines
2461 * has set the status field to the proper value. If we get back 1,
2462 * we've fully traversed the EDT and copied out any matching entries.
2463 */
2464 if (ret == 1)
2465 cdm->status = CAM_DEV_MATCH_LAST;
2466
2467 return(ret);
2468}
2469
2470static int
2471xptplistpdrvfunc(struct periph_driver **pdrv, void *arg)
2472{
2473 struct ccb_dev_match *cdm;
2474
2475 cdm = (struct ccb_dev_match *)arg;
2476
2477 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2478 && (cdm->pos.cookie.pdrv == pdrv)
2479 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2480 && (cdm->pos.generations[CAM_PERIPH_GENERATION] != 0)
2481 && (cdm->pos.generations[CAM_PERIPH_GENERATION] !=
2482 (*pdrv)->generation)) {
2483 cdm->status = CAM_DEV_MATCH_LIST_CHANGED;
2484 return(0);
2485 }
2486
2487 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2488 && (cdm->pos.cookie.pdrv == pdrv)
2489 && (cdm->pos.position_type & CAM_DEV_POS_PERIPH)
2490 && (cdm->pos.cookie.periph != NULL))
2491 return(xptpdperiphtraverse(pdrv,
2492 (struct cam_periph *)cdm->pos.cookie.periph,
2493 xptplistperiphfunc, arg));
2494 else
2495 return(xptpdperiphtraverse(pdrv, NULL,xptplistperiphfunc, arg));
2496}
2497
2498static int
2499xptplistperiphfunc(struct cam_periph *periph, void *arg)
2500{
2501 struct ccb_dev_match *cdm;
2502 dev_match_ret retval;
2503
2504 cdm = (struct ccb_dev_match *)arg;
2505
2506 retval = xptperiphmatch(cdm->patterns, cdm->num_patterns, periph);
2507
2508 if ((retval & DM_RET_ACTION_MASK) == DM_RET_ERROR) {
2509 cdm->status = CAM_DEV_MATCH_ERROR;
2510 return(0);
2511 }
2512
2513 /*
2514 * If the copy flag is set, copy this peripheral out.
2515 */
2516 if (retval & DM_RET_COPY) {
2517 int spaceleft, j;
2518
2519 spaceleft = cdm->match_buf_len - (cdm->num_matches *
2520 sizeof(struct dev_match_result));
2521
2522 /*
2523 * If we don't have enough space to put in another
2524 * match result, save our position and tell the
2525 * user there are more devices to check.
2526 */
2527 if (spaceleft < sizeof(struct dev_match_result)) {
2528 struct periph_driver **pdrv;
2529
2530 pdrv = NULL;
2531 bzero(&cdm->pos, sizeof(cdm->pos));
2532 cdm->pos.position_type =
2533 CAM_DEV_POS_PDRV | CAM_DEV_POS_PDPTR |
2534 CAM_DEV_POS_PERIPH;
2535
2536 /*
2537 * This may look a bit non-sensical, but it is
2538 * actually quite logical. There are very few
2539 * peripheral drivers, and bloating every peripheral
2540 * structure with a pointer back to its parent
2541 * peripheral driver linker set entry would cost
2542 * more in the long run than doing this quick lookup.
2543 */
2544 for (pdrv = periph_drivers; *pdrv != NULL; pdrv++) {
2545 if (strcmp((*pdrv)->driver_name,
2546 periph->periph_name) == 0)
2547 break;
2548 }
2549
2550 if (*pdrv == NULL) {
2551 cdm->status = CAM_DEV_MATCH_ERROR;
2552 return(0);
2553 }
2554
2555 cdm->pos.cookie.pdrv = pdrv;
2556 /*
2557 * The periph generation slot does double duty, as
2558 * does the periph pointer slot. They are used for
2559 * both edt and pdrv lookups and positioning.
2560 */
2561 cdm->pos.cookie.periph = periph;
2562 cdm->pos.generations[CAM_PERIPH_GENERATION] =
2563 (*pdrv)->generation;
2564 cdm->status = CAM_DEV_MATCH_MORE;
2565 return(0);
2566 }
2567
2568 j = cdm->num_matches;
2569 cdm->num_matches++;
2570 cdm->matches[j].type = DEV_MATCH_PERIPH;
2571 cdm->matches[j].result.periph_result.path_id =
2572 periph->path->bus->path_id;
2573
2574 /*
2575 * The transport layer peripheral doesn't have a target or
2576 * lun.
2577 */
2578 if (periph->path->target)
2579 cdm->matches[j].result.periph_result.target_id =
2580 periph->path->target->target_id;
2581 else
2582 cdm->matches[j].result.periph_result.target_id = -1;
2583
2584 if (periph->path->device)
2585 cdm->matches[j].result.periph_result.target_lun =
2586 periph->path->device->lun_id;
2587 else
2588 cdm->matches[j].result.periph_result.target_lun = -1;
2589
2590 cdm->matches[j].result.periph_result.unit_number =
2591 periph->unit_number;
2592 strncpy(cdm->matches[j].result.periph_result.periph_name,
2593 periph->periph_name, DEV_IDLEN);
2594 }
2595
2596 return(1);
2597}
2598
2599static int
2600xptperiphlistmatch(struct ccb_dev_match *cdm)
2601{
2602 int ret;
2603
2604 cdm->num_matches = 0;
2605
2606 /*
2607 * At this point in the edt traversal function, we check the bus
2608 * list generation to make sure that no busses have been added or
2609 * removed since the user last sent a XPT_DEV_MATCH ccb through.
2610 * For the peripheral driver list traversal function, however, we
2611 * don't have to worry about new peripheral driver types coming or
2612 * going; they're in a linker set, and therefore can't change
2613 * without a recompile.
2614 */
2615
2616 if ((cdm->pos.position_type & CAM_DEV_POS_PDPTR)
2617 && (cdm->pos.cookie.pdrv != NULL))
2618 ret = xptpdrvtraverse(
2619 (struct periph_driver **)cdm->pos.cookie.pdrv,
2620 xptplistpdrvfunc, cdm);
2621 else
2622 ret = xptpdrvtraverse(NULL, xptplistpdrvfunc, cdm);
2623
2624 /*
2625 * If we get back 0, that means that we had to stop before fully
2626 * traversing the peripheral driver tree. It also means that one of
2627 * the subroutines has set the status field to the proper value. If
2628 * we get back 1, we've fully traversed the EDT and copied out any
2629 * matching entries.
2630 */
2631 if (ret == 1)
2632 cdm->status = CAM_DEV_MATCH_LAST;
2633
2634 return(ret);
2635}
2636
2637static int
2638xptbustraverse(struct cam_eb *start_bus, xpt_busfunc_t *tr_func, void *arg)
2639{
2640 struct cam_eb *bus, *next_bus;
2641 int retval;
2642
2643 retval = 1;
2644
2645 for (bus = (start_bus ? start_bus : TAILQ_FIRST(&xpt_busses));
2646 bus != NULL;
2647 bus = next_bus) {
2648 next_bus = TAILQ_NEXT(bus, links);
2649
2650 retval = tr_func(bus, arg);
2651 if (retval == 0)
2652 return(retval);
2653 }
2654
2655 return(retval);
2656}
2657
2658static int
2659xpttargettraverse(struct cam_eb *bus, struct cam_et *start_target,
2660 xpt_targetfunc_t *tr_func, void *arg)
2661{
2662 struct cam_et *target, *next_target;
2663 int retval;
2664
2665 retval = 1;
2666 for (target = (start_target ? start_target :
2667 TAILQ_FIRST(&bus->et_entries));
2668 target != NULL; target = next_target) {
2669
2670 next_target = TAILQ_NEXT(target, links);
2671
2672 retval = tr_func(target, arg);
2673
2674 if (retval == 0)
2675 return(retval);
2676 }
2677
2678 return(retval);
2679}
2680
2681static int
2682xptdevicetraverse(struct cam_et *target, struct cam_ed *start_device,
2683 xpt_devicefunc_t *tr_func, void *arg)
2684{
2685 struct cam_ed *device, *next_device;
2686 int retval;
2687
2688 retval = 1;
2689 for (device = (start_device ? start_device :
2690 TAILQ_FIRST(&target->ed_entries));
2691 device != NULL;
2692 device = next_device) {
2693
2694 next_device = TAILQ_NEXT(device, links);
2695
2696 retval = tr_func(device, arg);
2697
2698 if (retval == 0)
2699 return(retval);
2700 }
2701
2702 return(retval);
2703}
2704
2705static int
2706xptperiphtraverse(struct cam_ed *device, struct cam_periph *start_periph,
2707 xpt_periphfunc_t *tr_func, void *arg)
2708{
2709 struct cam_periph *periph, *next_periph;
2710 int retval;
2711
2712 retval = 1;
2713
2714 for (periph = (start_periph ? start_periph :
2715 SLIST_FIRST(&device->periphs));
2716 periph != NULL;
2717 periph = next_periph) {
2718
2719 next_periph = SLIST_NEXT(periph, periph_links);
2720
2721 retval = tr_func(periph, arg);
2722 if (retval == 0)
2723 return(retval);
2724 }
2725
2726 return(retval);
2727}
2728
2729static int
2730xptpdrvtraverse(struct periph_driver **start_pdrv,
2731 xpt_pdrvfunc_t *tr_func, void *arg)
2732{
2733 struct periph_driver **pdrv;
2734 int retval;
2735
2736 retval = 1;
2737
2738 /*
2739 * We don't traverse the peripheral driver list like we do the
2740 * other lists, because it is a linker set, and therefore cannot be
2741 * changed during runtime. If the peripheral driver list is ever
2742 * re-done to be something other than a linker set (i.e. it can
2743 * change while the system is running), the list traversal should
2744 * be modified to work like the other traversal functions.
2745 */
2746 for (pdrv = (start_pdrv ? start_pdrv : periph_drivers);
2747 *pdrv != NULL; pdrv++) {
2748 retval = tr_func(pdrv, arg);
2749
2750 if (retval == 0)
2751 return(retval);
2752 }
2753
2754 return(retval);
2755}
2756
2757static int
2758xptpdperiphtraverse(struct periph_driver **pdrv,
2759 struct cam_periph *start_periph,
2760 xpt_periphfunc_t *tr_func, void *arg)
2761{
2762 struct cam_periph *periph, *next_periph;
2763 int retval;
2764
2765 retval = 1;
2766
2767 for (periph = (start_periph ? start_periph :
2768 TAILQ_FIRST(&(*pdrv)->units)); periph != NULL;
2769 periph = next_periph) {
2770
2771 next_periph = TAILQ_NEXT(periph, unit_links);
2772
2773 retval = tr_func(periph, arg);
2774 if (retval == 0)
2775 return(retval);
2776 }
2777 return(retval);
2778}
2779
2780static int
2781xptdefbusfunc(struct cam_eb *bus, void *arg)
2782{
2783 struct xpt_traverse_config *tr_config;
2784
2785 tr_config = (struct xpt_traverse_config *)arg;
2786
2787 if (tr_config->depth == XPT_DEPTH_BUS) {
2788 xpt_busfunc_t *tr_func;
2789
2790 tr_func = (xpt_busfunc_t *)tr_config->tr_func;
2791
2792 return(tr_func(bus, tr_config->tr_arg));
2793 } else
2794 return(xpttargettraverse(bus, NULL, xptdeftargetfunc, arg));
2795}
2796
2797static int
2798xptdeftargetfunc(struct cam_et *target, void *arg)
2799{
2800 struct xpt_traverse_config *tr_config;
2801
2802 tr_config = (struct xpt_traverse_config *)arg;
2803
2804 if (tr_config->depth == XPT_DEPTH_TARGET) {
2805 xpt_targetfunc_t *tr_func;
2806
2807 tr_func = (xpt_targetfunc_t *)tr_config->tr_func;
2808
2809 return(tr_func(target, tr_config->tr_arg));
2810 } else
2811 return(xptdevicetraverse(target, NULL, xptdefdevicefunc, arg));
2812}
2813
2814static int
2815xptdefdevicefunc(struct cam_ed *device, void *arg)
2816{
2817 struct xpt_traverse_config *tr_config;
2818
2819 tr_config = (struct xpt_traverse_config *)arg;
2820
2821 if (tr_config->depth == XPT_DEPTH_DEVICE) {
2822 xpt_devicefunc_t *tr_func;
2823
2824 tr_func = (xpt_devicefunc_t *)tr_config->tr_func;
2825
2826 return(tr_func(device, tr_config->tr_arg));
2827 } else
2828 return(xptperiphtraverse(device, NULL, xptdefperiphfunc, arg));
2829}
2830
2831static int
2832xptdefperiphfunc(struct cam_periph *periph, void *arg)
2833{
2834 struct xpt_traverse_config *tr_config;
2835 xpt_periphfunc_t *tr_func;
2836
2837 tr_config = (struct xpt_traverse_config *)arg;
2838
2839 tr_func = (xpt_periphfunc_t *)tr_config->tr_func;
2840
2841 /*
2842 * Unlike the other default functions, we don't check for depth
2843 * here. The peripheral driver level is the last level in the EDT,
2844 * so if we're here, we should execute the function in question.
2845 */
2846 return(tr_func(periph, tr_config->tr_arg));
2847}
2848
2849/*
2850 * Execute the given function for every bus in the EDT.
2851 */
2852static int
2853xpt_for_all_busses(xpt_busfunc_t *tr_func, void *arg)
2854{
2855 struct xpt_traverse_config tr_config;
2856
2857 tr_config.depth = XPT_DEPTH_BUS;
2858 tr_config.tr_func = tr_func;
2859 tr_config.tr_arg = arg;
2860
2861 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2862}
2863
2864#ifdef notusedyet
2865/*
2866 * Execute the given function for every target in the EDT.
2867 */
2868static int
2869xpt_for_all_targets(xpt_targetfunc_t *tr_func, void *arg)
2870{
2871 struct xpt_traverse_config tr_config;
2872
2873 tr_config.depth = XPT_DEPTH_TARGET;
2874 tr_config.tr_func = tr_func;
2875 tr_config.tr_arg = arg;
2876
2877 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2878}
2879#endif /* notusedyet */
2880
2881/*
2882 * Execute the given function for every device in the EDT.
2883 */
2884static int
2885xpt_for_all_devices(xpt_devicefunc_t *tr_func, void *arg)
2886{
2887 struct xpt_traverse_config tr_config;
2888
2889 tr_config.depth = XPT_DEPTH_DEVICE;
2890 tr_config.tr_func = tr_func;
2891 tr_config.tr_arg = arg;
2892
2893 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2894}
2895
2896#ifdef notusedyet
2897/*
2898 * Execute the given function for every peripheral in the EDT.
2899 */
2900static int
2901xpt_for_all_periphs(xpt_periphfunc_t *tr_func, void *arg)
2902{
2903 struct xpt_traverse_config tr_config;
2904
2905 tr_config.depth = XPT_DEPTH_PERIPH;
2906 tr_config.tr_func = tr_func;
2907 tr_config.tr_arg = arg;
2908
2909 return(xptbustraverse(NULL, xptdefbusfunc, &tr_config));
2910}
2911#endif /* notusedyet */
2912
2913static int
2914xptsetasyncfunc(struct cam_ed *device, void *arg)
2915{
2916 struct cam_path path;
2917 struct ccb_getdev cgd;
2918 struct async_node *cur_entry;
2919
2920 cur_entry = (struct async_node *)arg;
2921
2922 /*
2923 * Don't report unconfigured devices (Wildcard devs,
2924 * devices only for target mode, device instances
2925 * that have been invalidated but are waiting for
2926 * their last reference count to be released).
2927 */
2928 if ((device->flags & CAM_DEV_UNCONFIGURED) != 0)
2929 return (1);
2930
2931 xpt_compile_path(&path,
2932 NULL,
2933 device->target->bus->path_id,
2934 device->target->target_id,
2935 device->lun_id);
2936 xpt_setup_ccb(&cgd.ccb_h, &path, /*priority*/1);
2937 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
2938 xpt_action((union ccb *)&cgd);
2939 cur_entry->callback(cur_entry->callback_arg,
2940 AC_FOUND_DEVICE,
2941 &path, &cgd);
2942 xpt_release_path(&path);
2943
2944 return(1);
2945}
2946
2947static int
2948xptsetasyncbusfunc(struct cam_eb *bus, void *arg)
2949{
2950 struct cam_path path;
2951 struct ccb_pathinq cpi;
2952 struct async_node *cur_entry;
2953
2954 cur_entry = (struct async_node *)arg;
2955
2956 xpt_compile_path(&path, /*periph*/NULL,
2957 bus->sim->path_id,
2958 CAM_TARGET_WILDCARD,
2959 CAM_LUN_WILDCARD);
2960 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
2961 cpi.ccb_h.func_code = XPT_PATH_INQ;
2962 xpt_action((union ccb *)&cpi);
2963 cur_entry->callback(cur_entry->callback_arg,
2964 AC_PATH_REGISTERED,
2965 &path, &cpi);
2966 xpt_release_path(&path);
2967
2968 return(1);
2969}
2970
2971void
2972xpt_action(union ccb *start_ccb)
2973{
2974 int iopl;
2975
2976 GIANT_REQUIRED;
2977
2978 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_action\n"));
2979
2980 start_ccb->ccb_h.status = CAM_REQ_INPROG;
2981
2982 iopl = splsoftcam();
2983 switch (start_ccb->ccb_h.func_code) {
2984 case XPT_SCSI_IO:
2985 {
2986#ifdef CAM_NEW_TRAN_CODE
2987 struct cam_ed *device;
2988#endif /* CAM_NEW_TRAN_CODE */
2989#ifdef CAMDEBUG
2990 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
2991 struct cam_path *path;
2992
2993 path = start_ccb->ccb_h.path;
2994#endif
2995
2996 /*
2997 * For the sake of compatibility with SCSI-1
2998 * devices that may not understand the identify
2999 * message, we include lun information in the
3000 * second byte of all commands. SCSI-1 specifies
3001 * that luns are a 3 bit value and reserves only 3
3002 * bits for lun information in the CDB. Later
3003 * revisions of the SCSI spec allow for more than 8
3004 * luns, but have deprecated lun information in the
3005 * CDB. So, if the lun won't fit, we must omit.
3006 *
3007 * Also be aware that during initial probing for devices,
3008 * the inquiry information is unknown but initialized to 0.
3009 * This means that this code will be exercised while probing
3010 * devices with an ANSI revision greater than 2.
3011 */
3012#ifdef CAM_NEW_TRAN_CODE
3013 device = start_ccb->ccb_h.path->device;
3014 if (device->protocol_version <= SCSI_REV_2
3015#else /* CAM_NEW_TRAN_CODE */
3016 if (SID_ANSI_REV(&start_ccb->ccb_h.path->device->inq_data) <= 2
3017#endif /* CAM_NEW_TRAN_CODE */
3018 && start_ccb->ccb_h.target_lun < 8
3019 && (start_ccb->ccb_h.flags & CAM_CDB_POINTER) == 0) {
3020
3021 start_ccb->csio.cdb_io.cdb_bytes[1] |=
3022 start_ccb->ccb_h.target_lun << 5;
3023 }
3024 start_ccb->csio.scsi_status = SCSI_STATUS_OK;
3025 CAM_DEBUG(path, CAM_DEBUG_CDB,("%s. CDB: %s\n",
3026 scsi_op_desc(start_ccb->csio.cdb_io.cdb_bytes[0],
3027 &path->device->inq_data),
3028 scsi_cdb_string(start_ccb->csio.cdb_io.cdb_bytes,
3029 cdb_str, sizeof(cdb_str))));
3030 }
3031 /* FALLTHROUGH */
3032 case XPT_TARGET_IO:
3033 case XPT_CONT_TARGET_IO:
3034 start_ccb->csio.sense_resid = 0;
3035 start_ccb->csio.resid = 0;
3036 /* FALLTHROUGH */
3037 case XPT_RESET_DEV:
3038 case XPT_ENG_EXEC:
3039 {
3040 struct cam_path *path;
3041 int s;
3042 int runq;
3043
3044 path = start_ccb->ccb_h.path;
3045 s = splsoftcam();
3046
3047 cam_ccbq_insert_ccb(&path->device->ccbq, start_ccb);
3048 if (path->device->qfrozen_cnt == 0)
3049 runq = xpt_schedule_dev_sendq(path->bus, path->device);
3050 else
3051 runq = 0;
3052 splx(s);
3053 if (runq != 0)
3054 xpt_run_dev_sendq(path->bus);
3055 break;
3056 }
3057 case XPT_SET_TRAN_SETTINGS:
3058 {
3059 xpt_set_transfer_settings(&start_ccb->cts,
3060 start_ccb->ccb_h.path->device,
3061 /*async_update*/FALSE);
3062 break;
3063 }
3064 case XPT_CALC_GEOMETRY:
3065 {
3066 struct cam_sim *sim;
3067
3068 /* Filter out garbage */
3069 if (start_ccb->ccg.block_size == 0
3070 || start_ccb->ccg.volume_size == 0) {
3071 start_ccb->ccg.cylinders = 0;
3072 start_ccb->ccg.heads = 0;
3073 start_ccb->ccg.secs_per_track = 0;
3074 start_ccb->ccb_h.status = CAM_REQ_CMP;
3075 break;
3076 }
3077#ifdef PC98
3078 /*
3079 * In a PC-98 system, geometry translation depens on
3080 * the "real" device geometry obtained from mode page 4.
3081 * SCSI geometry translation is performed in the
3082 * initialization routine of the SCSI BIOS and the result
3083 * stored in host memory. If the translation is available
3084 * in host memory, use it. If not, rely on the default
3085 * translation the device driver performs.
3086 */
3087 if (scsi_da_bios_params(&start_ccb->ccg) != 0) {
3088 start_ccb->ccb_h.status = CAM_REQ_CMP;
3089 break;
3090 }
3091#endif
3092 sim = start_ccb->ccb_h.path->bus->sim;
3093 (*(sim->sim_action))(sim, start_ccb);
3094 break;
3095 }
3096 case XPT_ABORT:
3097 {
3098 union ccb* abort_ccb;
3099 int s;
3100
3101 abort_ccb = start_ccb->cab.abort_ccb;
3102 if (XPT_FC_IS_DEV_QUEUED(abort_ccb)) {
3103
3104 if (abort_ccb->ccb_h.pinfo.index >= 0) {
3105 struct cam_ccbq *ccbq;
3106
3107 ccbq = &abort_ccb->ccb_h.path->device->ccbq;
3108 cam_ccbq_remove_ccb(ccbq, abort_ccb);
3109 abort_ccb->ccb_h.status =
3110 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3111 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3112 s = splcam();
3113 xpt_done(abort_ccb);
3114 splx(s);
3115 start_ccb->ccb_h.status = CAM_REQ_CMP;
3116 break;
3117 }
3118 if (abort_ccb->ccb_h.pinfo.index == CAM_UNQUEUED_INDEX
3119 && (abort_ccb->ccb_h.status & CAM_SIM_QUEUED) == 0) {
3120 /*
3121 * We've caught this ccb en route to
3122 * the SIM. Flag it for abort and the
3123 * SIM will do so just before starting
3124 * real work on the CCB.
3125 */
3126 abort_ccb->ccb_h.status =
3127 CAM_REQ_ABORTED|CAM_DEV_QFRZN;
3128 xpt_freeze_devq(abort_ccb->ccb_h.path, 1);
3129 start_ccb->ccb_h.status = CAM_REQ_CMP;
3130 break;
3131 }
3132 }
3133 if (XPT_FC_IS_QUEUED(abort_ccb)
3134 && (abort_ccb->ccb_h.pinfo.index == CAM_DONEQ_INDEX)) {
3135 /*
3136 * It's already completed but waiting
3137 * for our SWI to get to it.
3138 */
3139 start_ccb->ccb_h.status = CAM_UA_ABORT;
3140 break;
3141 }
3142 /*
3143 * If we weren't able to take care of the abort request
3144 * in the XPT, pass the request down to the SIM for processing.
3145 */
3146 }
3147 /* FALLTHROUGH */
3148 case XPT_ACCEPT_TARGET_IO:
3149 case XPT_EN_LUN:
3150 case XPT_IMMED_NOTIFY:
3151 case XPT_NOTIFY_ACK:
3152 case XPT_GET_TRAN_SETTINGS:
3153 case XPT_RESET_BUS:
3154 {
3155 struct cam_sim *sim;
3156
3157 sim = start_ccb->ccb_h.path->bus->sim;
3158 (*(sim->sim_action))(sim, start_ccb);
3159 break;
3160 }
3161 case XPT_PATH_INQ:
3162 {
3163 struct cam_sim *sim;
3164
3165 sim = start_ccb->ccb_h.path->bus->sim;
3166 (*(sim->sim_action))(sim, start_ccb);
3167 break;
3168 }
3169 case XPT_PATH_STATS:
3170 start_ccb->cpis.last_reset =
3171 start_ccb->ccb_h.path->bus->last_reset;
3172 start_ccb->ccb_h.status = CAM_REQ_CMP;
3173 break;
3174 case XPT_GDEV_TYPE:
3175 {
3176 struct cam_ed *dev;
3177 int s;
3178
3179 dev = start_ccb->ccb_h.path->device;
3180 s = splcam();
3181 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3182 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3183 } else {
3184 struct ccb_getdev *cgd;
3185 struct cam_eb *bus;
3186 struct cam_et *tar;
3187
3188 cgd = &start_ccb->cgd;
3189 bus = cgd->ccb_h.path->bus;
3190 tar = cgd->ccb_h.path->target;
3191 cgd->inq_data = dev->inq_data;
3192 cgd->ccb_h.status = CAM_REQ_CMP;
3193 cgd->serial_num_len = dev->serial_num_len;
3194 if ((dev->serial_num_len > 0)
3195 && (dev->serial_num != NULL))
3196 bcopy(dev->serial_num, cgd->serial_num,
3197 dev->serial_num_len);
3198 }
3199 splx(s);
3200 break;
3201 }
3202 case XPT_GDEV_STATS:
3203 {
3204 struct cam_ed *dev;
3205 int s;
3206
3207 dev = start_ccb->ccb_h.path->device;
3208 s = splcam();
3209 if ((dev->flags & CAM_DEV_UNCONFIGURED) != 0) {
3210 start_ccb->ccb_h.status = CAM_DEV_NOT_THERE;
3211 } else {
3212 struct ccb_getdevstats *cgds;
3213 struct cam_eb *bus;
3214 struct cam_et *tar;
3215
3216 cgds = &start_ccb->cgds;
3217 bus = cgds->ccb_h.path->bus;
3218 tar = cgds->ccb_h.path->target;
3219 cgds->dev_openings = dev->ccbq.dev_openings;
3220 cgds->dev_active = dev->ccbq.dev_active;
3221 cgds->devq_openings = dev->ccbq.devq_openings;
3222 cgds->devq_queued = dev->ccbq.queue.entries;
3223 cgds->held = dev->ccbq.held;
3224 cgds->last_reset = tar->last_reset;
3225 cgds->maxtags = dev->quirk->maxtags;
3226 cgds->mintags = dev->quirk->mintags;
3227 if (timevalcmp(&tar->last_reset, &bus->last_reset, <))
3228 cgds->last_reset = bus->last_reset;
3229 cgds->ccb_h.status = CAM_REQ_CMP;
3230 }
3231 splx(s);
3232 break;
3233 }
3234 case XPT_GDEVLIST:
3235 {
3236 struct cam_periph *nperiph;
3237 struct periph_list *periph_head;
3238 struct ccb_getdevlist *cgdl;
3239 u_int i;
3240 int s;
3241 struct cam_ed *device;
3242 int found;
3243
3244
3245 found = 0;
3246
3247 /*
3248 * Don't want anyone mucking with our data.
3249 */
3250 s = splcam();
3251 device = start_ccb->ccb_h.path->device;
3252 periph_head = &device->periphs;
3253 cgdl = &start_ccb->cgdl;
3254
3255 /*
3256 * Check and see if the list has changed since the user
3257 * last requested a list member. If so, tell them that the
3258 * list has changed, and therefore they need to start over
3259 * from the beginning.
3260 */
3261 if ((cgdl->index != 0) &&
3262 (cgdl->generation != device->generation)) {
3263 cgdl->status = CAM_GDEVLIST_LIST_CHANGED;
3264 splx(s);
3265 break;
3266 }
3267
3268 /*
3269 * Traverse the list of peripherals and attempt to find
3270 * the requested peripheral.
3271 */
3272 for (nperiph = SLIST_FIRST(periph_head), i = 0;
3273 (nperiph != NULL) && (i <= cgdl->index);
3274 nperiph = SLIST_NEXT(nperiph, periph_links), i++) {
3275 if (i == cgdl->index) {
3276 strncpy(cgdl->periph_name,
3277 nperiph->periph_name,
3278 DEV_IDLEN);
3279 cgdl->unit_number = nperiph->unit_number;
3280 found = 1;
3281 }
3282 }
3283 if (found == 0) {
3284 cgdl->status = CAM_GDEVLIST_ERROR;
3285 splx(s);
3286 break;
3287 }
3288
3289 if (nperiph == NULL)
3290 cgdl->status = CAM_GDEVLIST_LAST_DEVICE;
3291 else
3292 cgdl->status = CAM_GDEVLIST_MORE_DEVS;
3293
3294 cgdl->index++;
3295 cgdl->generation = device->generation;
3296
3297 splx(s);
3298 cgdl->ccb_h.status = CAM_REQ_CMP;
3299 break;
3300 }
3301 case XPT_DEV_MATCH:
3302 {
3303 int s;
3304 dev_pos_type position_type;
3305 struct ccb_dev_match *cdm;
3306
3307 cdm = &start_ccb->cdm;
3308
3309 /*
3310 * Prevent EDT changes while we traverse it.
3311 */
3312 s = splcam();
3313 /*
3314 * There are two ways of getting at information in the EDT.
3315 * The first way is via the primary EDT tree. It starts
3316 * with a list of busses, then a list of targets on a bus,
3317 * then devices/luns on a target, and then peripherals on a
3318 * device/lun. The "other" way is by the peripheral driver
3319 * lists. The peripheral driver lists are organized by
3320 * peripheral driver. (obviously) So it makes sense to
3321 * use the peripheral driver list if the user is looking
3322 * for something like "da1", or all "da" devices. If the
3323 * user is looking for something on a particular bus/target
3324 * or lun, it's generally better to go through the EDT tree.
3325 */
3326
3327 if (cdm->pos.position_type != CAM_DEV_POS_NONE)
3328 position_type = cdm->pos.position_type;
3329 else {
3330 u_int i;
3331
3332 position_type = CAM_DEV_POS_NONE;
3333
3334 for (i = 0; i < cdm->num_patterns; i++) {
3335 if ((cdm->patterns[i].type == DEV_MATCH_BUS)
3336 ||(cdm->patterns[i].type == DEV_MATCH_DEVICE)){
3337 position_type = CAM_DEV_POS_EDT;
3338 break;
3339 }
3340 }
3341
3342 if (cdm->num_patterns == 0)
3343 position_type = CAM_DEV_POS_EDT;
3344 else if (position_type == CAM_DEV_POS_NONE)
3345 position_type = CAM_DEV_POS_PDRV;
3346 }
3347
3348 switch(position_type & CAM_DEV_POS_TYPEMASK) {
3349 case CAM_DEV_POS_EDT:
3350 xptedtmatch(cdm);
3351 break;
3352 case CAM_DEV_POS_PDRV:
3353 xptperiphlistmatch(cdm);
3354 break;
3355 default:
3356 cdm->status = CAM_DEV_MATCH_ERROR;
3357 break;
3358 }
3359
3360 splx(s);
3361
3362 if (cdm->status == CAM_DEV_MATCH_ERROR)
3363 start_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
3364 else
3365 start_ccb->ccb_h.status = CAM_REQ_CMP;
3366
3367 break;
3368 }
3369 case XPT_SASYNC_CB:
3370 {
3371 struct ccb_setasync *csa;
3372 struct async_node *cur_entry;
3373 struct async_list *async_head;
3374 u_int32_t added;
3375 int s;
3376
3377 csa = &start_ccb->csa;
3378 added = csa->event_enable;
3379 async_head = &csa->ccb_h.path->device->asyncs;
3380
3381 /*
3382 * If there is already an entry for us, simply
3383 * update it.
3384 */
3385 s = splcam();
3386 cur_entry = SLIST_FIRST(async_head);
3387 while (cur_entry != NULL) {
3388 if ((cur_entry->callback_arg == csa->callback_arg)
3389 && (cur_entry->callback == csa->callback))
3390 break;
3391 cur_entry = SLIST_NEXT(cur_entry, links);
3392 }
3393
3394 if (cur_entry != NULL) {
3395 /*
3396 * If the request has no flags set,
3397 * remove the entry.
3398 */
3399 added &= ~cur_entry->event_enable;
3400 if (csa->event_enable == 0) {
3401 SLIST_REMOVE(async_head, cur_entry,
3402 async_node, links);
3403 csa->ccb_h.path->device->refcount--;
3404 free(cur_entry, M_DEVBUF);
3405 } else {
3406 cur_entry->event_enable = csa->event_enable;
3407 }
3408 } else {
3409 cur_entry = malloc(sizeof(*cur_entry), M_DEVBUF,
3410 M_NOWAIT);
3411 if (cur_entry == NULL) {
3412 splx(s);
3413 csa->ccb_h.status = CAM_RESRC_UNAVAIL;
3414 break;
3415 }
3416 cur_entry->event_enable = csa->event_enable;
3417 cur_entry->callback_arg = csa->callback_arg;
3418 cur_entry->callback = csa->callback;
3419 SLIST_INSERT_HEAD(async_head, cur_entry, links);
3420 csa->ccb_h.path->device->refcount++;
3421 }
3422
3423 if ((added & AC_FOUND_DEVICE) != 0) {
3424 /*
3425 * Get this peripheral up to date with all
3426 * the currently existing devices.
3427 */
3428 xpt_for_all_devices(xptsetasyncfunc, cur_entry);
3429 }
3430 if ((added & AC_PATH_REGISTERED) != 0) {
3431 /*
3432 * Get this peripheral up to date with all
3433 * the currently existing busses.
3434 */
3435 xpt_for_all_busses(xptsetasyncbusfunc, cur_entry);
3436 }
3437 splx(s);
3438 start_ccb->ccb_h.status = CAM_REQ_CMP;
3439 break;
3440 }
3441 case XPT_REL_SIMQ:
3442 {
3443 struct ccb_relsim *crs;
3444 struct cam_ed *dev;
3445 int s;
3446
3447 crs = &start_ccb->crs;
3448 dev = crs->ccb_h.path->device;
3449 if (dev == NULL) {
3450
3451 crs->ccb_h.status = CAM_DEV_NOT_THERE;
3452 break;
3453 }
3454
3455 s = splcam();
3456
3457 if ((crs->release_flags & RELSIM_ADJUST_OPENINGS) != 0) {
3458
3459 if ((dev->inq_data.flags & SID_CmdQue) != 0) {
3460
3461 /* Don't ever go below one opening */
3462 if (crs->openings > 0) {
3463 xpt_dev_ccbq_resize(crs->ccb_h.path,
3464 crs->openings);
3465
3466 if (bootverbose) {
3467 xpt_print_path(crs->ccb_h.path);
3468 printf("tagged openings "
3469 "now %d\n",
3470 crs->openings);
3471 }
3472 }
3473 }
3474 }
3475
3476 if ((crs->release_flags & RELSIM_RELEASE_AFTER_TIMEOUT) != 0) {
3477
3478 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
3479
3480 /*
3481 * Just extend the old timeout and decrement
3482 * the freeze count so that a single timeout
3483 * is sufficient for releasing the queue.
3484 */
3485 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3486 untimeout(xpt_release_devq_timeout,
3487 dev, dev->c_handle);
3488 } else {
3489
3490 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3491 }
3492
3493 dev->c_handle =
3494 timeout(xpt_release_devq_timeout,
3495 dev,
3496 (crs->release_timeout * hz) / 1000);
3497
3498 dev->flags |= CAM_DEV_REL_TIMEOUT_PENDING;
3499
3500 }
3501
3502 if ((crs->release_flags & RELSIM_RELEASE_AFTER_CMDCMPLT) != 0) {
3503
3504 if ((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0) {
3505 /*
3506 * Decrement the freeze count so that a single
3507 * completion is still sufficient to unfreeze
3508 * the queue.
3509 */
3510 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3511 } else {
3512
3513 dev->flags |= CAM_DEV_REL_ON_COMPLETE;
3514 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3515 }
3516 }
3517
3518 if ((crs->release_flags & RELSIM_RELEASE_AFTER_QEMPTY) != 0) {
3519
3520 if ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
3521 || (dev->ccbq.dev_active == 0)) {
3522
3523 start_ccb->ccb_h.flags &= ~CAM_DEV_QFREEZE;
3524 } else {
3525
3526 dev->flags |= CAM_DEV_REL_ON_QUEUE_EMPTY;
3527 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
3528 }
3529 }
3530 splx(s);
3531
3532 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) == 0) {
3533
3534 xpt_release_devq(crs->ccb_h.path, /*count*/1,
3535 /*run_queue*/TRUE);
3536 }
3537 start_ccb->crs.qfrozen_cnt = dev->qfrozen_cnt;
3538 start_ccb->ccb_h.status = CAM_REQ_CMP;
3539 break;
3540 }
3541 case XPT_SCAN_BUS:
3542 xpt_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
3543 break;
3544 case XPT_SCAN_LUN:
3545 xpt_scan_lun(start_ccb->ccb_h.path->periph,
3546 start_ccb->ccb_h.path, start_ccb->crcn.flags,
3547 start_ccb);
3548 break;
3549 case XPT_DEBUG: {
3550#ifdef CAMDEBUG
3551 int s;
3552
3553 s = splcam();
3554#ifdef CAM_DEBUG_DELAY
3555 cam_debug_delay = CAM_DEBUG_DELAY;
3556#endif
3557 cam_dflags = start_ccb->cdbg.flags;
3558 if (cam_dpath != NULL) {
3559 xpt_free_path(cam_dpath);
3560 cam_dpath = NULL;
3561 }
3562
3563 if (cam_dflags != CAM_DEBUG_NONE) {
3564 if (xpt_create_path(&cam_dpath, xpt_periph,
3565 start_ccb->ccb_h.path_id,
3566 start_ccb->ccb_h.target_id,
3567 start_ccb->ccb_h.target_lun) !=
3568 CAM_REQ_CMP) {
3569 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3570 cam_dflags = CAM_DEBUG_NONE;
3571 } else {
3572 start_ccb->ccb_h.status = CAM_REQ_CMP;
3573 xpt_print_path(cam_dpath);
3574 printf("debugging flags now %x\n", cam_dflags);
3575 }
3576 } else {
3577 cam_dpath = NULL;
3578 start_ccb->ccb_h.status = CAM_REQ_CMP;
3579 }
3580 splx(s);
3581#else /* !CAMDEBUG */
3582 start_ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
3583#endif /* CAMDEBUG */
3584 break;
3585 }
3586 case XPT_NOOP:
3587 if ((start_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0)
3588 xpt_freeze_devq(start_ccb->ccb_h.path, 1);
3589 start_ccb->ccb_h.status = CAM_REQ_CMP;
3590 break;
3591 default:
3592 case XPT_SDEV_TYPE:
3593 case XPT_TERM_IO:
3594 case XPT_ENG_INQ:
3595 /* XXX Implement */
3596 start_ccb->ccb_h.status = CAM_PROVIDE_FAIL;
3597 break;
3598 }
3599 splx(iopl);
3600}
3601
3602void
3603xpt_polled_action(union ccb *start_ccb)
3604{
3605 int s;
3606 u_int32_t timeout;
3607 struct cam_sim *sim;
3608 struct cam_devq *devq;
3609 struct cam_ed *dev;
3610
3611 GIANT_REQUIRED;
3612
3613 timeout = start_ccb->ccb_h.timeout;
3614 sim = start_ccb->ccb_h.path->bus->sim;
3615 devq = sim->devq;
3616 dev = start_ccb->ccb_h.path->device;
3617
3618 s = splcam();
3619
3620 /*
3621 * Steal an opening so that no other queued requests
3622 * can get it before us while we simulate interrupts.
3623 */
3624 dev->ccbq.devq_openings--;
3625 dev->ccbq.dev_openings--;
3626
3627 while((devq->send_openings <= 0 || dev->ccbq.dev_openings < 0)
3628 && (--timeout > 0)) {
3629 DELAY(1000);
3630 (*(sim->sim_poll))(sim);
3631 camisr(&cam_bioq);
3632 }
3633
3634 dev->ccbq.devq_openings++;
3635 dev->ccbq.dev_openings++;
3636
3637 if (timeout != 0) {
3638 xpt_action(start_ccb);
3639 while(--timeout > 0) {
3640 (*(sim->sim_poll))(sim);
3641 camisr(&cam_bioq);
3642 if ((start_ccb->ccb_h.status & CAM_STATUS_MASK)
3643 != CAM_REQ_INPROG)
3644 break;
3645 DELAY(1000);
3646 }
3647 if (timeout == 0) {
3648 /*
3649 * XXX Is it worth adding a sim_timeout entry
3650 * point so we can attempt recovery? If
3651 * this is only used for dumps, I don't think
3652 * it is.
3653 */
3654 start_ccb->ccb_h.status = CAM_CMD_TIMEOUT;
3655 }
3656 } else {
3657 start_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
3658 }
3659 splx(s);
3660}
3661
3662/*
3663 * Schedule a peripheral driver to receive a ccb when it's
3664 * target device has space for more transactions.
3665 */
3666void
3667xpt_schedule(struct cam_periph *perph, u_int32_t new_priority)
3668{
3669 struct cam_ed *device;
3670 int s;
3671 int runq;
3672
3673 GIANT_REQUIRED;
3674
3675 CAM_DEBUG(perph->path, CAM_DEBUG_TRACE, ("xpt_schedule\n"));
3676 device = perph->path->device;
3677 s = splsoftcam();
3678 if (periph_is_queued(perph)) {
3679 /* Simply reorder based on new priority */
3680 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3681 (" change priority to %d\n", new_priority));
3682 if (new_priority < perph->pinfo.priority) {
3683 camq_change_priority(&device->drvq,
3684 perph->pinfo.index,
3685 new_priority);
3686 }
3687 runq = 0;
3688 } else {
3689 /* New entry on the queue */
3690 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3691 (" added periph to queue\n"));
3692 perph->pinfo.priority = new_priority;
3693 perph->pinfo.generation = ++device->drvq.generation;
3694 camq_insert(&device->drvq, &perph->pinfo);
3695 runq = xpt_schedule_dev_allocq(perph->path->bus, device);
3696 }
3697 splx(s);
3698 if (runq != 0) {
3699 CAM_DEBUG(perph->path, CAM_DEBUG_SUBTRACE,
3700 (" calling xpt_run_devq\n"));
3701 xpt_run_dev_allocq(perph->path->bus);
3702 }
3703}
3704
3705
3706/*
3707 * Schedule a device to run on a given queue.
3708 * If the device was inserted as a new entry on the queue,
3709 * return 1 meaning the device queue should be run. If we
3710 * were already queued, implying someone else has already
3711 * started the queue, return 0 so the caller doesn't attempt
3712 * to run the queue. Must be run at either splsoftcam
3713 * (or splcam since that encompases splsoftcam).
3714 */
3715static int
3716xpt_schedule_dev(struct camq *queue, cam_pinfo *pinfo,
3717 u_int32_t new_priority)
3718{
3719 int retval;
3720 u_int32_t old_priority;
3721
3722 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_schedule_dev\n"));
3723
3724 old_priority = pinfo->priority;
3725
3726 /*
3727 * Are we already queued?
3728 */
3729 if (pinfo->index != CAM_UNQUEUED_INDEX) {
3730 /* Simply reorder based on new priority */
3731 if (new_priority < old_priority) {
3732 camq_change_priority(queue, pinfo->index,
3733 new_priority);
3734 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3735 ("changed priority to %d\n",
3736 new_priority));
3737 }
3738 retval = 0;
3739 } else {
3740 /* New entry on the queue */
3741 if (new_priority < old_priority)
3742 pinfo->priority = new_priority;
3743
3744 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3745 ("Inserting onto queue\n"));
3746 pinfo->generation = ++queue->generation;
3747 camq_insert(queue, pinfo);
3748 retval = 1;
3749 }
3750 return (retval);
3751}
3752
3753static void
3754xpt_run_dev_allocq(struct cam_eb *bus)
3755{
3756 struct cam_devq *devq;
3757 int s;
3758
3759 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_allocq\n"));
3760 devq = bus->sim->devq;
3761
3762 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3763 (" qfrozen_cnt == 0x%x, entries == %d, "
3764 "openings == %d, active == %d\n",
3765 devq->alloc_queue.qfrozen_cnt,
3766 devq->alloc_queue.entries,
3767 devq->alloc_openings,
3768 devq->alloc_active));
3769
3770 s = splsoftcam();
3771 devq->alloc_queue.qfrozen_cnt++;
3772 while ((devq->alloc_queue.entries > 0)
3773 && (devq->alloc_openings > 0)
3774 && (devq->alloc_queue.qfrozen_cnt <= 1)) {
3775 struct cam_ed_qinfo *qinfo;
3776 struct cam_ed *device;
3777 union ccb *work_ccb;
3778 struct cam_periph *drv;
3779 struct camq *drvq;
3780
3781 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->alloc_queue,
3782 CAMQ_HEAD);
3783 device = qinfo->device;
3784
3785 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3786 ("running device %p\n", device));
3787
3788 drvq = &device->drvq;
3789
3790#ifdef CAMDEBUG
3791 if (drvq->entries <= 0) {
3792 panic("xpt_run_dev_allocq: "
3793 "Device on queue without any work to do");
3794 }
3795#endif
3796 if ((work_ccb = xpt_get_ccb(device)) != NULL) {
3797 devq->alloc_openings--;
3798 devq->alloc_active++;
3799 drv = (struct cam_periph*)camq_remove(drvq, CAMQ_HEAD);
3800 splx(s);
3801 xpt_setup_ccb(&work_ccb->ccb_h, drv->path,
3802 drv->pinfo.priority);
3803 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3804 ("calling periph start\n"));
3805 drv->periph_start(drv, work_ccb);
3806 } else {
3807 /*
3808 * Malloc failure in alloc_ccb
3809 */
3810 /*
3811 * XXX add us to a list to be run from free_ccb
3812 * if we don't have any ccbs active on this
3813 * device queue otherwise we may never get run
3814 * again.
3815 */
3816 break;
3817 }
3818
3819 /* Raise IPL for possible insertion and test at top of loop */
3820 s = splsoftcam();
3821
3822 if (drvq->entries > 0) {
3823 /* We have more work. Attempt to reschedule */
3824 xpt_schedule_dev_allocq(bus, device);
3825 }
3826 }
3827 devq->alloc_queue.qfrozen_cnt--;
3828 splx(s);
3829}
3830
3831static void
3832xpt_run_dev_sendq(struct cam_eb *bus)
3833{
3834 struct cam_devq *devq;
3835 int s;
3836
3837 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_run_dev_sendq\n"));
3838
3839 devq = bus->sim->devq;
3840
3841 s = splcam();
3842 devq->send_queue.qfrozen_cnt++;
3843 splx(s);
3844 s = splsoftcam();
3845 while ((devq->send_queue.entries > 0)
3846 && (devq->send_openings > 0)) {
3847 struct cam_ed_qinfo *qinfo;
3848 struct cam_ed *device;
3849 union ccb *work_ccb;
3850 struct cam_sim *sim;
3851 int ospl;
3852
3853 ospl = splcam();
3854 if (devq->send_queue.qfrozen_cnt > 1) {
3855 splx(ospl);
3856 break;
3857 }
3858
3859 qinfo = (struct cam_ed_qinfo *)camq_remove(&devq->send_queue,
3860 CAMQ_HEAD);
3861 device = qinfo->device;
3862
3863 /*
3864 * If the device has been "frozen", don't attempt
3865 * to run it.
3866 */
3867 if (device->qfrozen_cnt > 0) {
3868 splx(ospl);
3869 continue;
3870 }
3871
3872 CAM_DEBUG_PRINT(CAM_DEBUG_XPT,
3873 ("running device %p\n", device));
3874
3875 work_ccb = cam_ccbq_peek_ccb(&device->ccbq, CAMQ_HEAD);
3876 if (work_ccb == NULL) {
3877 printf("device on run queue with no ccbs???\n");
3878 splx(ospl);
3879 continue;
3880 }
3881
3882 if ((work_ccb->ccb_h.flags & CAM_HIGH_POWER) != 0) {
3883
3884 if (num_highpower <= 0) {
3885 /*
3886 * We got a high power command, but we
3887 * don't have any available slots. Freeze
3888 * the device queue until we have a slot
3889 * available.
3890 */
3891 device->qfrozen_cnt++;
3892 STAILQ_INSERT_TAIL(&highpowerq,
3893 &work_ccb->ccb_h,
3894 xpt_links.stqe);
3895
3896 splx(ospl);
3897 continue;
3898 } else {
3899 /*
3900 * Consume a high power slot while
3901 * this ccb runs.
3902 */
3903 num_highpower--;
3904 }
3905 }
3906 devq->active_dev = device;
3907 cam_ccbq_remove_ccb(&device->ccbq, work_ccb);
3908
3909 cam_ccbq_send_ccb(&device->ccbq, work_ccb);
3910 splx(ospl);
3911
3912 devq->send_openings--;
3913 devq->send_active++;
3914
3915 if (device->ccbq.queue.entries > 0)
3916 xpt_schedule_dev_sendq(bus, device);
3917
3918 if (work_ccb && (work_ccb->ccb_h.flags & CAM_DEV_QFREEZE) != 0){
3919 /*
3920 * The client wants to freeze the queue
3921 * after this CCB is sent.
3922 */
3923 ospl = splcam();
3924 device->qfrozen_cnt++;
3925 splx(ospl);
3926 }
3927
3928 splx(s);
3929
3930 /* In Target mode, the peripheral driver knows best... */
3931 if (work_ccb->ccb_h.func_code == XPT_SCSI_IO) {
3932 if ((device->inq_flags & SID_CmdQue) != 0
3933 && work_ccb->csio.tag_action != CAM_TAG_ACTION_NONE)
3934 work_ccb->ccb_h.flags |= CAM_TAG_ACTION_VALID;
3935 else
3936 /*
3937 * Clear this in case of a retried CCB that
3938 * failed due to a rejected tag.
3939 */
3940 work_ccb->ccb_h.flags &= ~CAM_TAG_ACTION_VALID;
3941 }
3942
3943 /*
3944 * Device queues can be shared among multiple sim instances
3945 * that reside on different busses. Use the SIM in the queue
3946 * CCB's path, rather than the one in the bus that was passed
3947 * into this function.
3948 */
3949 sim = work_ccb->ccb_h.path->bus->sim;
3950 (*(sim->sim_action))(sim, work_ccb);
3951
3952 ospl = splcam();
3953 devq->active_dev = NULL;
3954 splx(ospl);
3955 /* Raise IPL for possible insertion and test at top of loop */
3956 s = splsoftcam();
3957 }
3958 splx(s);
3959 s = splcam();
3960 devq->send_queue.qfrozen_cnt--;
3961 splx(s);
3962}
3963
3964/*
3965 * This function merges stuff from the slave ccb into the master ccb, while
3966 * keeping important fields in the master ccb constant.
3967 */
3968void
3969xpt_merge_ccb(union ccb *master_ccb, union ccb *slave_ccb)
3970{
3971 GIANT_REQUIRED;
3972
3973 /*
3974 * Pull fields that are valid for peripheral drivers to set
3975 * into the master CCB along with the CCB "payload".
3976 */
3977 master_ccb->ccb_h.retry_count = slave_ccb->ccb_h.retry_count;
3978 master_ccb->ccb_h.func_code = slave_ccb->ccb_h.func_code;
3979 master_ccb->ccb_h.timeout = slave_ccb->ccb_h.timeout;
3980 master_ccb->ccb_h.flags = slave_ccb->ccb_h.flags;
3981 bcopy(&(&slave_ccb->ccb_h)[1], &(&master_ccb->ccb_h)[1],
3982 sizeof(union ccb) - sizeof(struct ccb_hdr));
3983}
3984
3985void
3986xpt_setup_ccb(struct ccb_hdr *ccb_h, struct cam_path *path, u_int32_t priority)
3987{
3988 GIANT_REQUIRED;
3989
3990 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_setup_ccb\n"));
3991 ccb_h->pinfo.priority = priority;
3992 ccb_h->path = path;
3993 ccb_h->path_id = path->bus->path_id;
3994 if (path->target)
3995 ccb_h->target_id = path->target->target_id;
3996 else
3997 ccb_h->target_id = CAM_TARGET_WILDCARD;
3998 if (path->device) {
3999 ccb_h->target_lun = path->device->lun_id;
4000 ccb_h->pinfo.generation = ++path->device->ccbq.queue.generation;
4001 } else {
4002 ccb_h->target_lun = CAM_TARGET_WILDCARD;
4003 }
4004 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
4005 ccb_h->flags = 0;
4006}
4007
4008/* Path manipulation functions */
4009cam_status
4010xpt_create_path(struct cam_path **new_path_ptr, struct cam_periph *perph,
4011 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4012{
4013 struct cam_path *path;
4014 cam_status status;
4015
4016 GIANT_REQUIRED;
4017
4018 path = (struct cam_path *)malloc(sizeof(*path), M_DEVBUF, M_NOWAIT);
4019
4020 if (path == NULL) {
4021 status = CAM_RESRC_UNAVAIL;
4022 return(status);
4023 }
4024 status = xpt_compile_path(path, perph, path_id, target_id, lun_id);
4025 if (status != CAM_REQ_CMP) {
4026 free(path, M_DEVBUF);
4027 path = NULL;
4028 }
4029 *new_path_ptr = path;
4030 return (status);
4031}
4032
4033static cam_status
4034xpt_compile_path(struct cam_path *new_path, struct cam_periph *perph,
4035 path_id_t path_id, target_id_t target_id, lun_id_t lun_id)
4036{
4037 struct cam_eb *bus;
4038 struct cam_et *target;
4039 struct cam_ed *device;
4040 cam_status status;
4041 int s;
4042
4043 status = CAM_REQ_CMP; /* Completed without error */
4044 target = NULL; /* Wildcarded */
4045 device = NULL; /* Wildcarded */
4046
4047 /*
4048 * We will potentially modify the EDT, so block interrupts
4049 * that may attempt to create cam paths.
4050 */
4051 s = splcam();
4052 bus = xpt_find_bus(path_id);
4053 if (bus == NULL) {
4054 status = CAM_PATH_INVALID;
4055 } else {
4056 target = xpt_find_target(bus, target_id);
4057 if (target == NULL) {
4058 /* Create one */
4059 struct cam_et *new_target;
4060
4061 new_target = xpt_alloc_target(bus, target_id);
4062 if (new_target == NULL) {
4063 status = CAM_RESRC_UNAVAIL;
4064 } else {
4065 target = new_target;
4066 }
4067 }
4068 if (target != NULL) {
4069 device = xpt_find_device(target, lun_id);
4070 if (device == NULL) {
4071 /* Create one */
4072 struct cam_ed *new_device;
4073
4074 new_device = xpt_alloc_device(bus,
4075 target,
4076 lun_id);
4077 if (new_device == NULL) {
4078 status = CAM_RESRC_UNAVAIL;
4079 } else {
4080 device = new_device;
4081 }
4082 }
4083 }
4084 }
4085 splx(s);
4086
4087 /*
4088 * Only touch the user's data if we are successful.
4089 */
4090 if (status == CAM_REQ_CMP) {
4091 new_path->periph = perph;
4092 new_path->bus = bus;
4093 new_path->target = target;
4094 new_path->device = device;
4095 CAM_DEBUG(new_path, CAM_DEBUG_TRACE, ("xpt_compile_path\n"));
4096 } else {
4097 if (device != NULL)
4098 xpt_release_device(bus, target, device);
4099 if (target != NULL)
4100 xpt_release_target(bus, target);
4101 if (bus != NULL)
4102 xpt_release_bus(bus);
4103 }
4104 return (status);
4105}
4106
4107static void
4108xpt_release_path(struct cam_path *path)
4109{
4110 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_release_path\n"));
4111 if (path->device != NULL) {
4112 xpt_release_device(path->bus, path->target, path->device);
4113 path->device = NULL;
4114 }
4115 if (path->target != NULL) {
4116 xpt_release_target(path->bus, path->target);
4117 path->target = NULL;
4118 }
4119 if (path->bus != NULL) {
4120 xpt_release_bus(path->bus);
4121 path->bus = NULL;
4122 }
4123}
4124
4125void
4126xpt_free_path(struct cam_path *path)
4127{
4128 GIANT_REQUIRED;
4129
4130 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_free_path\n"));
4131 xpt_release_path(path);
4132 free(path, M_DEVBUF);
4133}
4134
4135
4136/*
4137 * Return -1 for failure, 0 for exact match, 1 for match with wildcards
4138 * in path1, 2 for match with wildcards in path2.
4139 */
4140int
4141xpt_path_comp(struct cam_path *path1, struct cam_path *path2)
4142{
4143 GIANT_REQUIRED;
4144
4145 int retval = 0;
4146
4147 if (path1->bus != path2->bus) {
4148 if (path1->bus->path_id == CAM_BUS_WILDCARD)
4149 retval = 1;
4150 else if (path2->bus->path_id == CAM_BUS_WILDCARD)
4151 retval = 2;
4152 else
4153 return (-1);
4154 }
4155 if (path1->target != path2->target) {
4156 if (path1->target->target_id == CAM_TARGET_WILDCARD) {
4157 if (retval == 0)
4158 retval = 1;
4159 } else if (path2->target->target_id == CAM_TARGET_WILDCARD)
4160 retval = 2;
4161 else
4162 return (-1);
4163 }
4164 if (path1->device != path2->device) {
4165 if (path1->device->lun_id == CAM_LUN_WILDCARD) {
4166 if (retval == 0)
4167 retval = 1;
4168 } else if (path2->device->lun_id == CAM_LUN_WILDCARD)
4169 retval = 2;
4170 else
4171 return (-1);
4172 }
4173 return (retval);
4174}
4175
4176void
4177xpt_print_path(struct cam_path *path)
4178{
4179 GIANT_REQUIRED;
4180
4181 if (path == NULL)
4182 printf("(nopath): ");
4183 else {
4184 if (path->periph != NULL)
4185 printf("(%s%d:", path->periph->periph_name,
4186 path->periph->unit_number);
4187 else
4188 printf("(noperiph:");
4189
4190 if (path->bus != NULL)
4191 printf("%s%d:%d:", path->bus->sim->sim_name,
4192 path->bus->sim->unit_number,
4193 path->bus->sim->bus_id);
4194 else
4195 printf("nobus:");
4196
4197 if (path->target != NULL)
4198 printf("%d:", path->target->target_id);
4199 else
4200 printf("X:");
4201
4202 if (path->device != NULL)
4203 printf("%d): ", path->device->lun_id);
4204 else
4205 printf("X): ");
4206 }
4207}
4208
4209int
4210xpt_path_string(struct cam_path *path, char *str, size_t str_len)
4211{
4212 struct sbuf sb;
4213
4214 GIANT_REQUIRED;
4215
4216 sbuf_new(&sb, str, str_len, 0);
4217
4218 if (path == NULL)
4219 sbuf_printf(&sb, "(nopath): ");
4220 else {
4221 if (path->periph != NULL)
4222 sbuf_printf(&sb, "(%s%d:", path->periph->periph_name,
4223 path->periph->unit_number);
4224 else
4225 sbuf_printf(&sb, "(noperiph:");
4226
4227 if (path->bus != NULL)
4228 sbuf_printf(&sb, "%s%d:%d:", path->bus->sim->sim_name,
4229 path->bus->sim->unit_number,
4230 path->bus->sim->bus_id);
4231 else
4232 sbuf_printf(&sb, "nobus:");
4233
4234 if (path->target != NULL)
4235 sbuf_printf(&sb, "%d:", path->target->target_id);
4236 else
4237 sbuf_printf(&sb, "X:");
4238
4239 if (path->device != NULL)
4240 sbuf_printf(&sb, "%d): ", path->device->lun_id);
4241 else
4242 sbuf_printf(&sb, "X): ");
4243 }
4244 sbuf_finish(&sb);
4245
4246 return(sbuf_len(&sb));
4247}
4248
4249path_id_t
4250xpt_path_path_id(struct cam_path *path)
4251{
4252 GIANT_REQUIRED;
4253
4254 return(path->bus->path_id);
4255}
4256
4257target_id_t
4258xpt_path_target_id(struct cam_path *path)
4259{
4260 GIANT_REQUIRED;
4261
4262 if (path->target != NULL)
4263 return (path->target->target_id);
4264 else
4265 return (CAM_TARGET_WILDCARD);
4266}
4267
4268lun_id_t
4269xpt_path_lun_id(struct cam_path *path)
4270{
4271 GIANT_REQUIRED;
4272
4273 if (path->device != NULL)
4274 return (path->device->lun_id);
4275 else
4276 return (CAM_LUN_WILDCARD);
4277}
4278
4279struct cam_sim *
4280xpt_path_sim(struct cam_path *path)
4281{
4282 GIANT_REQUIRED;
4283
4284 return (path->bus->sim);
4285}
4286
4287struct cam_periph*
4288xpt_path_periph(struct cam_path *path)
4289{
4290 GIANT_REQUIRED;
4291
4292 return (path->periph);
4293}
4294
4295/*
4296 * Release a CAM control block for the caller. Remit the cost of the structure
4297 * to the device referenced by the path. If the this device had no 'credits'
4298 * and peripheral drivers have registered async callbacks for this notification
4299 * call them now.
4300 */
4301void
4302xpt_release_ccb(union ccb *free_ccb)
4303{
4304 int s;
4305 struct cam_path *path;
4306 struct cam_ed *device;
4307 struct cam_eb *bus;
4308
4309 GIANT_REQUIRED;
4310
4311 CAM_DEBUG_PRINT(CAM_DEBUG_XPT, ("xpt_release_ccb\n"));
4312 path = free_ccb->ccb_h.path;
4313 device = path->device;
4314 bus = path->bus;
4315 s = splsoftcam();
4316 cam_ccbq_release_opening(&device->ccbq);
4317 if (xpt_ccb_count > xpt_max_ccbs) {
4318 xpt_free_ccb(free_ccb);
4319 xpt_ccb_count--;
4320 } else {
4321 SLIST_INSERT_HEAD(&ccb_freeq, &free_ccb->ccb_h, xpt_links.sle);
4322 }
4323 bus->sim->devq->alloc_openings++;
4324 bus->sim->devq->alloc_active--;
4325 /* XXX Turn this into an inline function - xpt_run_device?? */
4326 if ((device_is_alloc_queued(device) == 0)
4327 && (device->drvq.entries > 0)) {
4328 xpt_schedule_dev_allocq(bus, device);
4329 }
4330 splx(s);
4331 if (dev_allocq_is_runnable(bus->sim->devq))
4332 xpt_run_dev_allocq(bus);
4333}
4334
4335/* Functions accessed by SIM drivers */
4336
4337/*
4338 * A sim structure, listing the SIM entry points and instance
4339 * identification info is passed to xpt_bus_register to hook the SIM
4340 * into the CAM framework. xpt_bus_register creates a cam_eb entry
4341 * for this new bus and places it in the array of busses and assigns
4342 * it a path_id. The path_id may be influenced by "hard wiring"
4343 * information specified by the user. Once interrupt services are
4344 * availible, the bus will be probed.
4345 */
4346int32_t
4347xpt_bus_register(struct cam_sim *sim, u_int32_t bus)
4348{
4349 struct cam_eb *new_bus;
4350 struct cam_eb *old_bus;
4351 struct ccb_pathinq cpi;
4352 int s;
4353
4354 GIANT_REQUIRED;
4355
4356 sim->bus_id = bus;
4357 new_bus = (struct cam_eb *)malloc(sizeof(*new_bus),
4358 M_DEVBUF, M_NOWAIT);
4359 if (new_bus == NULL) {
4360 /* Couldn't satisfy request */
4361 return (CAM_RESRC_UNAVAIL);
4362 }
4363
4364 if (strcmp(sim->sim_name, "xpt") != 0) {
4365
4366 sim->path_id =
4367 xptpathid(sim->sim_name, sim->unit_number, sim->bus_id);
4368 }
4369
4370 TAILQ_INIT(&new_bus->et_entries);
4371 new_bus->path_id = sim->path_id;
4372 new_bus->sim = sim;
4373 timevalclear(&new_bus->last_reset);
4374 new_bus->flags = 0;
4375 new_bus->refcount = 1; /* Held until a bus_deregister event */
4376 new_bus->generation = 0;
4377 s = splcam();
4378 old_bus = TAILQ_FIRST(&xpt_busses);
4379 while (old_bus != NULL
4380 && old_bus->path_id < new_bus->path_id)
4381 old_bus = TAILQ_NEXT(old_bus, links);
4382 if (old_bus != NULL)
4383 TAILQ_INSERT_BEFORE(old_bus, new_bus, links);
4384 else
4385 TAILQ_INSERT_TAIL(&xpt_busses, new_bus, links);
4386 bus_generation++;
4387 splx(s);
4388
4389 /* Notify interested parties */
4390 if (sim->path_id != CAM_XPT_PATH_ID) {
4391 struct cam_path path;
4392
4393 xpt_compile_path(&path, /*periph*/NULL, sim->path_id,
4394 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4395 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
4396 cpi.ccb_h.func_code = XPT_PATH_INQ;
4397 xpt_action((union ccb *)&cpi);
4398 xpt_async(AC_PATH_REGISTERED, &path, &cpi);
4399 xpt_release_path(&path);
4400 }
4401 return (CAM_SUCCESS);
4402}
4403
4404int32_t
4405xpt_bus_deregister(path_id_t pathid)
4406{
4407 struct cam_path bus_path;
4408 cam_status status;
4409
4410 GIANT_REQUIRED;
4411
4412 status = xpt_compile_path(&bus_path, NULL, pathid,
4413 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
4414 if (status != CAM_REQ_CMP)
4415 return (status);
4416
4417 xpt_async(AC_LOST_DEVICE, &bus_path, NULL);
4418 xpt_async(AC_PATH_DEREGISTERED, &bus_path, NULL);
4419
4420 /* Release the reference count held while registered. */
4421 xpt_release_bus(bus_path.bus);
4422 xpt_release_path(&bus_path);
4423
4424 return (CAM_REQ_CMP);
4425}
4426
4427static path_id_t
4428xptnextfreepathid(void)
4429{
4430 struct cam_eb *bus;
4431 path_id_t pathid;
4432 const char *strval;
4433
4434 pathid = 0;
4435 bus = TAILQ_FIRST(&xpt_busses);
4436retry:
4437 /* Find an unoccupied pathid */
4438 while (bus != NULL
4439 && bus->path_id <= pathid) {
4440 if (bus->path_id == pathid)
4441 pathid++;
4442 bus = TAILQ_NEXT(bus, links);
4443 }
4444
4445 /*
4446 * Ensure that this pathid is not reserved for
4447 * a bus that may be registered in the future.
4448 */
4449 if (resource_string_value("scbus", pathid, "at", &strval) == 0) {
4450 ++pathid;
4451 /* Start the search over */
4452 goto retry;
4453 }
4454 return (pathid);
4455}
4456
4457static path_id_t
4458xptpathid(const char *sim_name, int sim_unit, int sim_bus)
4459{
4460 path_id_t pathid;
4461 int i, dunit, val;
4462 char buf[32];
4463 const char *dname;
4464
4465 pathid = CAM_XPT_PATH_ID;
4466 snprintf(buf, sizeof(buf), "%s%d", sim_name, sim_unit);
4467 i = 0;
4468 while ((resource_find_match(&i, &dname, &dunit, "at", buf)) == 0) {
4469 if (strcmp(dname, "scbus")) {
4470 /* Avoid a bit of foot shooting. */
4471 continue;
4472 }
4473 if (dunit < 0) /* unwired?! */
4474 continue;
4475 if (resource_int_value("scbus", dunit, "bus", &val) == 0) {
4476 if (sim_bus == val) {
4477 pathid = dunit;
4478 break;
4479 }
4480 } else if (sim_bus == 0) {
4481 /* Unspecified matches bus 0 */
4482 pathid = dunit;
4483 break;
4484 } else {
4485 printf("Ambiguous scbus configuration for %s%d "
4486 "bus %d, cannot wire down. The kernel "
4487 "config entry for scbus%d should "
4488 "specify a controller bus.\n"
4489 "Scbus will be assigned dynamically.\n",
4490 sim_name, sim_unit, sim_bus, dunit);
4491 break;
4492 }
4493 }
4494
4495 if (pathid == CAM_XPT_PATH_ID)
4496 pathid = xptnextfreepathid();
4497 return (pathid);
4498}
4499
4500void
4501xpt_async(u_int32_t async_code, struct cam_path *path, void *async_arg)
4502{
4503 struct cam_eb *bus;
4504 struct cam_et *target, *next_target;
4505 struct cam_ed *device, *next_device;
4506 int s;
4507
4508 GIANT_REQUIRED;
4509
4510 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("xpt_async\n"));
4511
4512 /*
4513 * Most async events come from a CAM interrupt context. In
4514 * a few cases, the error recovery code at the peripheral layer,
4515 * which may run from our SWI or a process context, may signal
4516 * deferred events with a call to xpt_async. Ensure async
4517 * notifications are serialized by blocking cam interrupts.
4518 */
4519 s = splcam();
4520
4521 bus = path->bus;
4522
4523 if (async_code == AC_BUS_RESET) {
4524 int s;
4525
4526 s = splclock();
4527 /* Update our notion of when the last reset occurred */
4528 microtime(&bus->last_reset);
4529 splx(s);
4530 }
4531
4532 for (target = TAILQ_FIRST(&bus->et_entries);
4533 target != NULL;
4534 target = next_target) {
4535
4536 next_target = TAILQ_NEXT(target, links);
4537
4538 if (path->target != target
4539 && path->target->target_id != CAM_TARGET_WILDCARD
4540 && target->target_id != CAM_TARGET_WILDCARD)
4541 continue;
4542
4543 if (async_code == AC_SENT_BDR) {
4544 int s;
4545
4546 /* Update our notion of when the last reset occurred */
4547 s = splclock();
4548 microtime(&path->target->last_reset);
4549 splx(s);
4550 }
4551
4552 for (device = TAILQ_FIRST(&target->ed_entries);
4553 device != NULL;
4554 device = next_device) {
4555
4556 next_device = TAILQ_NEXT(device, links);
4557
4558 if (path->device != device
4559 && path->device->lun_id != CAM_LUN_WILDCARD
4560 && device->lun_id != CAM_LUN_WILDCARD)
4561 continue;
4562
4563 xpt_dev_async(async_code, bus, target,
4564 device, async_arg);
4565
4566 xpt_async_bcast(&device->asyncs, async_code,
4567 path, async_arg);
4568 }
4569 }
4570
4571 /*
4572 * If this wasn't a fully wildcarded async, tell all
4573 * clients that want all async events.
4574 */
4575 if (bus != xpt_periph->path->bus)
4576 xpt_async_bcast(&xpt_periph->path->device->asyncs, async_code,
4577 path, async_arg);
4578 splx(s);
4579}
4580
4581static void
4582xpt_async_bcast(struct async_list *async_head,
4583 u_int32_t async_code,
4584 struct cam_path *path, void *async_arg)
4585{
4586 struct async_node *cur_entry;
4587
4588 cur_entry = SLIST_FIRST(async_head);
4589 while (cur_entry != NULL) {
4590 struct async_node *next_entry;
4591 /*
4592 * Grab the next list entry before we call the current
4593 * entry's callback. This is because the callback function
4594 * can delete its async callback entry.
4595 */
4596 next_entry = SLIST_NEXT(cur_entry, links);
4597 if ((cur_entry->event_enable & async_code) != 0)
4598 cur_entry->callback(cur_entry->callback_arg,
4599 async_code, path,
4600 async_arg);
4601 cur_entry = next_entry;
4602 }
4603}
4604
4605/*
4606 * Handle any per-device event notifications that require action by the XPT.
4607 */
4608static void
4609xpt_dev_async(u_int32_t async_code, struct cam_eb *bus, struct cam_et *target,
4610 struct cam_ed *device, void *async_arg)
4611{
4612 cam_status status;
4613 struct cam_path newpath;
4614
4615 /*
4616 * We only need to handle events for real devices.
4617 */
4618 if (target->target_id == CAM_TARGET_WILDCARD
4619 || device->lun_id == CAM_LUN_WILDCARD)
4620 return;
4621
4622 /*
4623 * We need our own path with wildcards expanded to
4624 * handle certain types of events.
4625 */
4626 if ((async_code == AC_SENT_BDR)
4627 || (async_code == AC_BUS_RESET)
4628 || (async_code == AC_INQ_CHANGED))
4629 status = xpt_compile_path(&newpath, NULL,
4630 bus->path_id,
4631 target->target_id,
4632 device->lun_id);
4633 else
4634 status = CAM_REQ_CMP_ERR;
4635
4636 if (status == CAM_REQ_CMP) {
4637
4638 /*
4639 * Allow transfer negotiation to occur in a
4640 * tag free environment.
4641 */
4642 if (async_code == AC_SENT_BDR
4643 || async_code == AC_BUS_RESET)
4644 xpt_toggle_tags(&newpath);
4645
4646 if (async_code == AC_INQ_CHANGED) {
4647 /*
4648 * We've sent a start unit command, or
4649 * something similar to a device that
4650 * may have caused its inquiry data to
4651 * change. So we re-scan the device to
4652 * refresh the inquiry data for it.
4653 */
4654 xpt_scan_lun(newpath.periph, &newpath,
4655 CAM_EXPECT_INQ_CHANGE, NULL);
4656 }
4657 xpt_release_path(&newpath);
4658 } else if (async_code == AC_LOST_DEVICE) {
4659 device->flags |= CAM_DEV_UNCONFIGURED;
4660 } else if (async_code == AC_TRANSFER_NEG) {
4661 struct ccb_trans_settings *settings;
4662
4663 settings = (struct ccb_trans_settings *)async_arg;
4664 xpt_set_transfer_settings(settings, device,
4665 /*async_update*/TRUE);
4666 }
4667}
4668
4669u_int32_t
4670xpt_freeze_devq(struct cam_path *path, u_int count)
4671{
4672 int s;
4673 struct ccb_hdr *ccbh;
4674
4675 GIANT_REQUIRED;
4676
4677 s = splcam();
4678 path->device->qfrozen_cnt += count;
4679
4680 /*
4681 * Mark the last CCB in the queue as needing
4682 * to be requeued if the driver hasn't
4683 * changed it's state yet. This fixes a race
4684 * where a ccb is just about to be queued to
4685 * a controller driver when it's interrupt routine
4686 * freezes the queue. To completly close the
4687 * hole, controller drives must check to see
4688 * if a ccb's status is still CAM_REQ_INPROG
4689 * under spl protection just before they queue
4690 * the CCB. See ahc_action/ahc_freeze_devq for
4691 * an example.
4692 */
4693 ccbh = TAILQ_LAST(&path->device->ccbq.active_ccbs, ccb_hdr_tailq);
4694 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4695 ccbh->status = CAM_REQUEUE_REQ;
4696 splx(s);
4697 return (path->device->qfrozen_cnt);
4698}
4699
4700u_int32_t
4701xpt_freeze_simq(struct cam_sim *sim, u_int count)
4702{
4703 GIANT_REQUIRED;
4704
4705 sim->devq->send_queue.qfrozen_cnt += count;
4706 if (sim->devq->active_dev != NULL) {
4707 struct ccb_hdr *ccbh;
4708
4709 ccbh = TAILQ_LAST(&sim->devq->active_dev->ccbq.active_ccbs,
4710 ccb_hdr_tailq);
4711 if (ccbh && ccbh->status == CAM_REQ_INPROG)
4712 ccbh->status = CAM_REQUEUE_REQ;
4713 }
4714 return (sim->devq->send_queue.qfrozen_cnt);
4715}
4716
4717static void
4718xpt_release_devq_timeout(void *arg)
4719{
4720 struct cam_ed *device;
4721
4722 device = (struct cam_ed *)arg;
4723
4724 xpt_release_devq_device(device, /*count*/1, /*run_queue*/TRUE);
4725}
4726
4727void
4728xpt_release_devq(struct cam_path *path, u_int count, int run_queue)
4729{
4730 GIANT_REQUIRED;
4731
4732 xpt_release_devq_device(path->device, count, run_queue);
4733}
4734
4735static void
4736xpt_release_devq_device(struct cam_ed *dev, u_int count, int run_queue)
4737{
4738 int rundevq;
4739 int s0, s1;
4740
4741 rundevq = 0;
4742 s0 = splsoftcam();
4743 s1 = splcam();
4744 if (dev->qfrozen_cnt > 0) {
4745
4746 count = (count > dev->qfrozen_cnt) ? dev->qfrozen_cnt : count;
4747 dev->qfrozen_cnt -= count;
4748 if (dev->qfrozen_cnt == 0) {
4749
4750 /*
4751 * No longer need to wait for a successful
4752 * command completion.
4753 */
4754 dev->flags &= ~CAM_DEV_REL_ON_COMPLETE;
4755
4756 /*
4757 * Remove any timeouts that might be scheduled
4758 * to release this queue.
4759 */
4760 if ((dev->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0) {
4761 untimeout(xpt_release_devq_timeout, dev,
4762 dev->c_handle);
4763 dev->flags &= ~CAM_DEV_REL_TIMEOUT_PENDING;
4764 }
4765
4766 /*
4767 * Now that we are unfrozen schedule the
4768 * device so any pending transactions are
4769 * run.
4770 */
4771 if ((dev->ccbq.queue.entries > 0)
4772 && (xpt_schedule_dev_sendq(dev->target->bus, dev))
4773 && (run_queue != 0)) {
4774 rundevq = 1;
4775 }
4776 }
4777 }
4778 splx(s1);
4779 if (rundevq != 0)
4780 xpt_run_dev_sendq(dev->target->bus);
4781 splx(s0);
4782}
4783
4784void
4785xpt_release_simq(struct cam_sim *sim, int run_queue)
4786{
4787 int s;
4788 struct camq *sendq;
4789
4790 GIANT_REQUIRED;
4791
4792 sendq = &(sim->devq->send_queue);
4793 s = splcam();
4794 if (sendq->qfrozen_cnt > 0) {
4795
4796 sendq->qfrozen_cnt--;
4797 if (sendq->qfrozen_cnt == 0) {
4798 struct cam_eb *bus;
4799
4800 /*
4801 * If there is a timeout scheduled to release this
4802 * sim queue, remove it. The queue frozen count is
4803 * already at 0.
4804 */
4805 if ((sim->flags & CAM_SIM_REL_TIMEOUT_PENDING) != 0){
4806 untimeout(xpt_release_simq_timeout, sim,
4807 sim->c_handle);
4808 sim->flags &= ~CAM_SIM_REL_TIMEOUT_PENDING;
4809 }
4810 bus = xpt_find_bus(sim->path_id);
4811 splx(s);
4812
4813 if (run_queue) {
4814 /*
4815 * Now that we are unfrozen run the send queue.
4816 */
4817 xpt_run_dev_sendq(bus);
4818 }
4819 xpt_release_bus(bus);
4820 } else
4821 splx(s);
4822 } else
4823 splx(s);
4824}
4825
4826static void
4827xpt_release_simq_timeout(void *arg)
4828{
4829 struct cam_sim *sim;
4830
4831 sim = (struct cam_sim *)arg;
4832 xpt_release_simq(sim, /* run_queue */ TRUE);
4833}
4834
4835void
4836xpt_done(union ccb *done_ccb)
4837{
4838 int s;
4839
4840 s = splcam();
4841
4842 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xpt_done\n"));
4843 if ((done_ccb->ccb_h.func_code & XPT_FC_QUEUED) != 0) {
4844 /*
4845 * Queue up the request for handling by our SWI handler
4846 * any of the "non-immediate" type of ccbs.
4847 */
4848 switch (done_ccb->ccb_h.path->periph->type) {
4849 case CAM_PERIPH_BIO:
4850 mtx_lock(&cam_bioq_lock);
4851 TAILQ_INSERT_TAIL(&cam_bioq, &done_ccb->ccb_h,
4852 sim_links.tqe);
4853 done_ccb->ccb_h.pinfo.index = CAM_DONEQ_INDEX;
4854 mtx_unlock(&cam_bioq_lock);
4855 swi_sched(cambio_ih, 0);
4856 break;
4857 default:
4858 panic("unknown periph type %d",
4859 done_ccb->ccb_h.path->periph->type);
4860 }
4861 }
4862 splx(s);
4863}
4864
4865union ccb *
4866xpt_alloc_ccb()
4867{
4868 union ccb *new_ccb;
4869
4870 GIANT_REQUIRED;
4871
4872 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_WAITOK);
4873 return (new_ccb);
4874}
4875
4876void
4877xpt_free_ccb(union ccb *free_ccb)
4878{
4879 free(free_ccb, M_DEVBUF);
4880}
4881
4882
4883
4884/* Private XPT functions */
4885
4886/*
4887 * Get a CAM control block for the caller. Charge the structure to the device
4888 * referenced by the path. If the this device has no 'credits' then the
4889 * device already has the maximum number of outstanding operations under way
4890 * and we return NULL. If we don't have sufficient resources to allocate more
4891 * ccbs, we also return NULL.
4892 */
4893static union ccb *
4894xpt_get_ccb(struct cam_ed *device)
4895{
4896 union ccb *new_ccb;
4897 int s;
4898
4899 s = splsoftcam();
4900 if ((new_ccb = (union ccb *)SLIST_FIRST(&ccb_freeq)) == NULL) {
4901 new_ccb = malloc(sizeof(*new_ccb), M_DEVBUF, M_NOWAIT);
4902 if (new_ccb == NULL) {
4903 splx(s);
4904 return (NULL);
4905 }
4906 callout_handle_init(&new_ccb->ccb_h.timeout_ch);
4907 SLIST_INSERT_HEAD(&ccb_freeq, &new_ccb->ccb_h,
4908 xpt_links.sle);
4909 xpt_ccb_count++;
4910 }
4911 cam_ccbq_take_opening(&device->ccbq);
4912 SLIST_REMOVE_HEAD(&ccb_freeq, xpt_links.sle);
4913 splx(s);
4914 return (new_ccb);
4915}
4916
4917static void
4918xpt_release_bus(struct cam_eb *bus)
4919{
4920 int s;
4921
4922 s = splcam();
4923 if ((--bus->refcount == 0)
4924 && (TAILQ_FIRST(&bus->et_entries) == NULL)) {
4925 TAILQ_REMOVE(&xpt_busses, bus, links);
4926 bus_generation++;
4927 splx(s);
4928 free(bus, M_DEVBUF);
4929 } else
4930 splx(s);
4931}
4932
4933static struct cam_et *
4934xpt_alloc_target(struct cam_eb *bus, target_id_t target_id)
4935{
4936 struct cam_et *target;
4937
4938 target = (struct cam_et *)malloc(sizeof(*target), M_DEVBUF, M_NOWAIT);
4939 if (target != NULL) {
4940 struct cam_et *cur_target;
4941
4942 TAILQ_INIT(&target->ed_entries);
4943 target->bus = bus;
4944 target->target_id = target_id;
4945 target->refcount = 1;
4946 target->generation = 0;
4947 timevalclear(&target->last_reset);
4948 /*
4949 * Hold a reference to our parent bus so it
4950 * will not go away before we do.
4951 */
4952 bus->refcount++;
4953
4954 /* Insertion sort into our bus's target list */
4955 cur_target = TAILQ_FIRST(&bus->et_entries);
4956 while (cur_target != NULL && cur_target->target_id < target_id)
4957 cur_target = TAILQ_NEXT(cur_target, links);
4958
4959 if (cur_target != NULL) {
4960 TAILQ_INSERT_BEFORE(cur_target, target, links);
4961 } else {
4962 TAILQ_INSERT_TAIL(&bus->et_entries, target, links);
4963 }
4964 bus->generation++;
4965 }
4966 return (target);
4967}
4968
4969static void
4970xpt_release_target(struct cam_eb *bus, struct cam_et *target)
4971{
4972 int s;
4973
4974 s = splcam();
4975 if ((--target->refcount == 0)
4976 && (TAILQ_FIRST(&target->ed_entries) == NULL)) {
4977 TAILQ_REMOVE(&bus->et_entries, target, links);
4978 bus->generation++;
4979 splx(s);
4980 free(target, M_DEVBUF);
4981 xpt_release_bus(bus);
4982 } else
4983 splx(s);
4984}
4985
4986static struct cam_ed *
4987xpt_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
4988{
4989#ifdef CAM_NEW_TRAN_CODE
4990 struct cam_path path;
4991#endif /* CAM_NEW_TRAN_CODE */
4992 struct cam_ed *device;
4993 struct cam_devq *devq;
4994 cam_status status;
4995
4996 /* Make space for us in the device queue on our bus */
4997 devq = bus->sim->devq;
4998 status = cam_devq_resize(devq, devq->alloc_queue.array_size + 1);
4999
5000 if (status != CAM_REQ_CMP) {
5001 device = NULL;
5002 } else {
5003 device = (struct cam_ed *)malloc(sizeof(*device),
5004 M_DEVBUF, M_NOWAIT);
5005 }
5006
5007 if (device != NULL) {
5008 struct cam_ed *cur_device;
5009
5010 cam_init_pinfo(&device->alloc_ccb_entry.pinfo);
5011 device->alloc_ccb_entry.device = device;
5012 cam_init_pinfo(&device->send_ccb_entry.pinfo);
5013 device->send_ccb_entry.device = device;
5014 device->target = target;
5015 device->lun_id = lun_id;
5016 /* Initialize our queues */
5017 if (camq_init(&device->drvq, 0) != 0) {
5018 free(device, M_DEVBUF);
5019 return (NULL);
5020 }
5021 if (cam_ccbq_init(&device->ccbq,
5022 bus->sim->max_dev_openings) != 0) {
5023 camq_fini(&device->drvq);
5024 free(device, M_DEVBUF);
5025 return (NULL);
5026 }
5027 SLIST_INIT(&device->asyncs);
5028 SLIST_INIT(&device->periphs);
5029 device->generation = 0;
5030 device->owner = NULL;
5031 /*
5032 * Take the default quirk entry until we have inquiry
5033 * data and can determine a better quirk to use.
5034 */
5035 device->quirk = &xpt_quirk_table[xpt_quirk_table_size - 1];
5036 bzero(&device->inq_data, sizeof(device->inq_data));
5037 device->inq_flags = 0;
5038 device->queue_flags = 0;
5039 device->serial_num = NULL;
5040 device->serial_num_len = 0;
5041 device->qfrozen_cnt = 0;
5042 device->flags = CAM_DEV_UNCONFIGURED;
5043 device->tag_delay_count = 0;
5044 device->tag_saved_openings = 0;
5045 device->refcount = 1;
5046 callout_handle_init(&device->c_handle);
5047
5048 /*
5049 * Hold a reference to our parent target so it
5050 * will not go away before we do.
5051 */
5052 target->refcount++;
5053
5054 /*
5055 * XXX should be limited by number of CCBs this bus can
5056 * do.
5057 */
5058 xpt_max_ccbs += device->ccbq.devq_openings;
5059 /* Insertion sort into our target's device list */
5060 cur_device = TAILQ_FIRST(&target->ed_entries);
5061 while (cur_device != NULL && cur_device->lun_id < lun_id)
5062 cur_device = TAILQ_NEXT(cur_device, links);
5063 if (cur_device != NULL) {
5064 TAILQ_INSERT_BEFORE(cur_device, device, links);
5065 } else {
5066 TAILQ_INSERT_TAIL(&target->ed_entries, device, links);
5067 }
5068 target->generation++;
5069#ifdef CAM_NEW_TRAN_CODE
5070 if (lun_id != CAM_LUN_WILDCARD) {
5071 xpt_compile_path(&path,
5072 NULL,
5073 bus->path_id,
5074 target->target_id,
5075 lun_id);
5076 xpt_devise_transport(&path);
5077 xpt_release_path(&path);
5078 }
5079#endif /* CAM_NEW_TRAN_CODE */
5080 }
5081 return (device);
5082}
5083
5084static void
5085xpt_release_device(struct cam_eb *bus, struct cam_et *target,
5086 struct cam_ed *device)
5087{
5088 int s;
5089
5090 s = splcam();
5091 if ((--device->refcount == 0)
5092 && ((device->flags & CAM_DEV_UNCONFIGURED) != 0)) {
5093 struct cam_devq *devq;
5094
5095 if (device->alloc_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX
5096 || device->send_ccb_entry.pinfo.index != CAM_UNQUEUED_INDEX)
5097 panic("Removing device while still queued for ccbs");
5098
5099 if ((device->flags & CAM_DEV_REL_TIMEOUT_PENDING) != 0)
5100 untimeout(xpt_release_devq_timeout, device,
5101 device->c_handle);
5102
5103 TAILQ_REMOVE(&target->ed_entries, device,links);
5104 target->generation++;
5105 xpt_max_ccbs -= device->ccbq.devq_openings;
5106 /* Release our slot in the devq */
5107 devq = bus->sim->devq;
5108 cam_devq_resize(devq, devq->alloc_queue.array_size - 1);
5109 splx(s);
5110 camq_fini(&device->drvq);
5111 camq_fini(&device->ccbq.queue);
5112 free(device, M_DEVBUF);
5113 xpt_release_target(bus, target);
5114 } else
5115 splx(s);
5116}
5117
5118static u_int32_t
5119xpt_dev_ccbq_resize(struct cam_path *path, int newopenings)
5120{
5121 int s;
5122 int diff;
5123 int result;
5124 struct cam_ed *dev;
5125
5126 dev = path->device;
5127 s = splsoftcam();
5128
5129 diff = newopenings - (dev->ccbq.dev_active + dev->ccbq.dev_openings);
5130 result = cam_ccbq_resize(&dev->ccbq, newopenings);
5131 if (result == CAM_REQ_CMP && (diff < 0)) {
5132 dev->flags |= CAM_DEV_RESIZE_QUEUE_NEEDED;
5133 }
5134 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
5135 || (dev->inq_flags & SID_CmdQue) != 0)
5136 dev->tag_saved_openings = newopenings;
5137 /* Adjust the global limit */
5138 xpt_max_ccbs += diff;
5139 splx(s);
5140 return (result);
5141}
5142
5143static struct cam_eb *
5144xpt_find_bus(path_id_t path_id)
5145{
5146 struct cam_eb *bus;
5147
5148 for (bus = TAILQ_FIRST(&xpt_busses);
5149 bus != NULL;
5150 bus = TAILQ_NEXT(bus, links)) {
5151 if (bus->path_id == path_id) {
5152 bus->refcount++;
5153 break;
5154 }
5155 }
5156 return (bus);
5157}
5158
5159static struct cam_et *
5160xpt_find_target(struct cam_eb *bus, target_id_t target_id)
5161{
5162 struct cam_et *target;
5163
5164 for (target = TAILQ_FIRST(&bus->et_entries);
5165 target != NULL;
5166 target = TAILQ_NEXT(target, links)) {
5167 if (target->target_id == target_id) {
5168 target->refcount++;
5169 break;
5170 }
5171 }
5172 return (target);
5173}
5174
5175static struct cam_ed *
5176xpt_find_device(struct cam_et *target, lun_id_t lun_id)
5177{
5178 struct cam_ed *device;
5179
5180 for (device = TAILQ_FIRST(&target->ed_entries);
5181 device != NULL;
5182 device = TAILQ_NEXT(device, links)) {
5183 if (device->lun_id == lun_id) {
5184 device->refcount++;
5185 break;
5186 }
5187 }
5188 return (device);
5189}
5190
5191typedef struct {
5192 union ccb *request_ccb;
5193 struct ccb_pathinq *cpi;
5194 int pending_count;
5195} xpt_scan_bus_info;
5196
5197/*
5198 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
5199 * As the scan progresses, xpt_scan_bus is used as the
5200 * callback on completion function.
5201 */
5202static void
5203xpt_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
5204{
5205 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5206 ("xpt_scan_bus\n"));
5207 switch (request_ccb->ccb_h.func_code) {
5208 case XPT_SCAN_BUS:
5209 {
5210 xpt_scan_bus_info *scan_info;
5211 union ccb *work_ccb;
5212 struct cam_path *path;
5213 u_int i;
5214 u_int max_target;
5215 u_int initiator_id;
5216
5217 /* Find out the characteristics of the bus */
5218 work_ccb = xpt_alloc_ccb();
5219 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
5220 request_ccb->ccb_h.pinfo.priority);
5221 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
5222 xpt_action(work_ccb);
5223 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
5224 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
5225 xpt_free_ccb(work_ccb);
5226 xpt_done(request_ccb);
5227 return;
5228 }
5229
5230 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5231 /*
5232 * Can't scan the bus on an adapter that
5233 * cannot perform the initiator role.
5234 */
5235 request_ccb->ccb_h.status = CAM_REQ_CMP;
5236 xpt_free_ccb(work_ccb);
5237 xpt_done(request_ccb);
5238 return;
5239 }
5240
5241 /* Save some state for use while we probe for devices */
5242 scan_info = (xpt_scan_bus_info *)
5243 malloc(sizeof(xpt_scan_bus_info), M_TEMP, M_WAITOK);
5244 scan_info->request_ccb = request_ccb;
5245 scan_info->cpi = &work_ccb->cpi;
5246
5247 /* Cache on our stack so we can work asynchronously */
5248 max_target = scan_info->cpi->max_target;
5249 initiator_id = scan_info->cpi->initiator_id;
5250
5251 /*
5252 * Don't count the initiator if the
5253 * initiator is addressable.
5254 */
5255 scan_info->pending_count = max_target + 1;
5256 if (initiator_id <= max_target)
5257 scan_info->pending_count--;
5258
5259 for (i = 0; i <= max_target; i++) {
5260 cam_status status;
5261 if (i == initiator_id)
5262 continue;
5263
5264 status = xpt_create_path(&path, xpt_periph,
5265 request_ccb->ccb_h.path_id,
5266 i, 0);
5267 if (status != CAM_REQ_CMP) {
5268 printf("xpt_scan_bus: xpt_create_path failed"
5269 " with status %#x, bus scan halted\n",
5270 status);
5271 break;
5272 }
5273 work_ccb = xpt_alloc_ccb();
5274 xpt_setup_ccb(&work_ccb->ccb_h, path,
5275 request_ccb->ccb_h.pinfo.priority);
5276 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5277 work_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5278 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
5279 work_ccb->crcn.flags = request_ccb->crcn.flags;
5280 xpt_action(work_ccb);
5281 }
5282 break;
5283 }
5284 case XPT_SCAN_LUN:
5285 {
5286 xpt_scan_bus_info *scan_info;
5287 path_id_t path_id;
5288 target_id_t target_id;
5289 lun_id_t lun_id;
5290
5291 /* Reuse the same CCB to query if a device was really found */
5292 scan_info = (xpt_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
5293 xpt_setup_ccb(&request_ccb->ccb_h, request_ccb->ccb_h.path,
5294 request_ccb->ccb_h.pinfo.priority);
5295 request_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
5296
5297 path_id = request_ccb->ccb_h.path_id;
5298 target_id = request_ccb->ccb_h.target_id;
5299 lun_id = request_ccb->ccb_h.target_lun;
5300 xpt_action(request_ccb);
5301
5302 if (request_ccb->ccb_h.status != CAM_REQ_CMP) {
5303 struct cam_ed *device;
5304 struct cam_et *target;
5305 int s, phl;
5306
5307 /*
5308 * If we already probed lun 0 successfully, or
5309 * we have additional configured luns on this
5310 * target that might have "gone away", go onto
5311 * the next lun.
5312 */
5313 target = request_ccb->ccb_h.path->target;
5314 /*
5315 * We may touch devices that we don't
5316 * hold references too, so ensure they
5317 * don't disappear out from under us.
5318 * The target above is referenced by the
5319 * path in the request ccb.
5320 */
5321 phl = 0;
5322 s = splcam();
5323 device = TAILQ_FIRST(&target->ed_entries);
5324 if (device != NULL) {
5325 phl = CAN_SRCH_HI(device);
5326 if (device->lun_id == 0)
5327 device = TAILQ_NEXT(device, links);
5328 }
5329 splx(s);
5330 if ((lun_id != 0) || (device != NULL)) {
5331 if (lun_id < (CAM_SCSI2_MAXLUN-1) || phl)
5332 lun_id++;
5333 }
5334 } else {
5335 struct cam_ed *device;
5336
5337 device = request_ccb->ccb_h.path->device;
5338
5339 if ((device->quirk->quirks & CAM_QUIRK_NOLUNS) == 0) {
5340 /* Try the next lun */
5341 if (lun_id < (CAM_SCSI2_MAXLUN-1)
5342 || CAN_SRCH_HI(device))
5343 lun_id++;
5344 }
5345 }
5346
5347 xpt_free_path(request_ccb->ccb_h.path);
5348
5349 /* Check Bounds */
5350 if ((lun_id == request_ccb->ccb_h.target_lun)
5351 || lun_id > scan_info->cpi->max_lun) {
5352 /* We're done */
5353
5354 xpt_free_ccb(request_ccb);
5355 scan_info->pending_count--;
5356 if (scan_info->pending_count == 0) {
5357 xpt_free_ccb((union ccb *)scan_info->cpi);
5358 request_ccb = scan_info->request_ccb;
5359 free(scan_info, M_TEMP);
5360 request_ccb->ccb_h.status = CAM_REQ_CMP;
5361 xpt_done(request_ccb);
5362 }
5363 } else {
5364 /* Try the next device */
5365 struct cam_path *path;
5366 cam_status status;
5367
5368 status = xpt_create_path(&path, xpt_periph,
5369 path_id, target_id, lun_id);
5370 if (status != CAM_REQ_CMP) {
5371 printf("xpt_scan_bus: xpt_create_path failed "
5372 "with status %#x, halting LUN scan\n",
5373 status);
5374 xpt_free_ccb(request_ccb);
5375 scan_info->pending_count--;
5376 if (scan_info->pending_count == 0) {
5377 xpt_free_ccb(
5378 (union ccb *)scan_info->cpi);
5379 request_ccb = scan_info->request_ccb;
5380 free(scan_info, M_TEMP);
5381 request_ccb->ccb_h.status = CAM_REQ_CMP;
5382 xpt_done(request_ccb);
5383 }
5384 break;
5385 }
5386 xpt_setup_ccb(&request_ccb->ccb_h, path,
5387 request_ccb->ccb_h.pinfo.priority);
5388 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5389 request_ccb->ccb_h.cbfcnp = xpt_scan_bus;
5390 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
5391 request_ccb->crcn.flags =
5392 scan_info->request_ccb->crcn.flags;
5393 xpt_action(request_ccb);
5394 }
5395 break;
5396 }
5397 default:
5398 break;
5399 }
5400}
5401
5402typedef enum {
5403 PROBE_TUR,
5404 PROBE_INQUIRY,
5405 PROBE_FULL_INQUIRY,
5406 PROBE_MODE_SENSE,
5407 PROBE_SERIAL_NUM,
5408 PROBE_TUR_FOR_NEGOTIATION
5409} probe_action;
5410
5411typedef enum {
5412 PROBE_INQUIRY_CKSUM = 0x01,
5413 PROBE_SERIAL_CKSUM = 0x02,
5414 PROBE_NO_ANNOUNCE = 0x04
5415} probe_flags;
5416
5417typedef struct {
5418 TAILQ_HEAD(, ccb_hdr) request_ccbs;
5419 probe_action action;
5420 union ccb saved_ccb;
5421 probe_flags flags;
5422 MD5_CTX context;
5423 u_int8_t digest[16];
5424} probe_softc;
5425
5426static void
5427xpt_scan_lun(struct cam_periph *periph, struct cam_path *path,
5428 cam_flags flags, union ccb *request_ccb)
5429{
5430 struct ccb_pathinq cpi;
5431 cam_status status;
5432 struct cam_path *new_path;
5433 struct cam_periph *old_periph;
5434 int s;
5435
5436 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
5437 ("xpt_scan_lun\n"));
5438
5439 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
5440 cpi.ccb_h.func_code = XPT_PATH_INQ;
5441 xpt_action((union ccb *)&cpi);
5442
5443 if (cpi.ccb_h.status != CAM_REQ_CMP) {
5444 if (request_ccb != NULL) {
5445 request_ccb->ccb_h.status = cpi.ccb_h.status;
5446 xpt_done(request_ccb);
5447 }
5448 return;
5449 }
5450
5451 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
5452 /*
5453 * Can't scan the bus on an adapter that
5454 * cannot perform the initiator role.
5455 */
5456 if (request_ccb != NULL) {
5457 request_ccb->ccb_h.status = CAM_REQ_CMP;
5458 xpt_done(request_ccb);
5459 }
5460 return;
5461 }
5462
5463 if (request_ccb == NULL) {
5464 request_ccb = malloc(sizeof(union ccb), M_TEMP, M_NOWAIT);
5465 if (request_ccb == NULL) {
5466 xpt_print_path(path);
5467 printf("xpt_scan_lun: can't allocate CCB, can't "
5468 "continue\n");
5469 return;
5470 }
5471 new_path = malloc(sizeof(*new_path), M_TEMP, M_NOWAIT);
5472 if (new_path == NULL) {
5473 xpt_print_path(path);
5474 printf("xpt_scan_lun: can't allocate path, can't "
5475 "continue\n");
5476 free(request_ccb, M_TEMP);
5477 return;
5478 }
5479 status = xpt_compile_path(new_path, xpt_periph,
5480 path->bus->path_id,
5481 path->target->target_id,
5482 path->device->lun_id);
5483
5484 if (status != CAM_REQ_CMP) {
5485 xpt_print_path(path);
5486 printf("xpt_scan_lun: can't compile path, can't "
5487 "continue\n");
5488 free(request_ccb, M_TEMP);
5489 free(new_path, M_TEMP);
5490 return;
5491 }
5492 xpt_setup_ccb(&request_ccb->ccb_h, new_path, /*priority*/ 1);
5493 request_ccb->ccb_h.cbfcnp = xptscandone;
5494 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
5495 request_ccb->crcn.flags = flags;
5496 }
5497
5498 s = splsoftcam();
5499 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
5500 probe_softc *softc;
5501
5502 softc = (probe_softc *)old_periph->softc;
5503 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5504 periph_links.tqe);
5505 } else {
5506 status = cam_periph_alloc(proberegister, NULL, probecleanup,
5507 probestart, "probe",
5508 CAM_PERIPH_BIO,
5509 request_ccb->ccb_h.path, NULL, 0,
5510 request_ccb);
5511
5512 if (status != CAM_REQ_CMP) {
5513 xpt_print_path(path);
5514 printf("xpt_scan_lun: cam_alloc_periph returned an "
5515 "error, can't continue probe\n");
5516 request_ccb->ccb_h.status = status;
5517 xpt_done(request_ccb);
5518 }
5519 }
5520 splx(s);
5521}
5522
5523static void
5524xptscandone(struct cam_periph *periph, union ccb *done_ccb)
5525{
5526 xpt_release_path(done_ccb->ccb_h.path);
5527 free(done_ccb->ccb_h.path, M_TEMP);
5528 free(done_ccb, M_TEMP);
5529}
5530
5531static cam_status
5532proberegister(struct cam_periph *periph, void *arg)
5533{
5534 union ccb *request_ccb; /* CCB representing the probe request */
5535 probe_softc *softc;
5536
5537 request_ccb = (union ccb *)arg;
5538 if (periph == NULL) {
5539 printf("proberegister: periph was NULL!!\n");
5540 return(CAM_REQ_CMP_ERR);
5541 }
5542
5543 if (request_ccb == NULL) {
5544 printf("proberegister: no probe CCB, "
5545 "can't register device\n");
5546 return(CAM_REQ_CMP_ERR);
5547 }
5548
5549 softc = (probe_softc *)malloc(sizeof(*softc), M_TEMP, M_NOWAIT);
5550
5551 if (softc == NULL) {
5552 printf("proberegister: Unable to probe new device. "
5553 "Unable to allocate softc\n");
5554 return(CAM_REQ_CMP_ERR);
5555 }
5556 TAILQ_INIT(&softc->request_ccbs);
5557 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
5558 periph_links.tqe);
5559 softc->flags = 0;
5560 periph->softc = softc;
5561 cam_periph_acquire(periph);
5562 /*
5563 * Ensure we've waited at least a bus settle
5564 * delay before attempting to probe the device.
5565 * For HBAs that don't do bus resets, this won't make a difference.
5566 */
5567 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
5568 scsi_delay);
5569 probeschedule(periph);
5570 return(CAM_REQ_CMP);
5571}
5572
5573static void
5574probeschedule(struct cam_periph *periph)
5575{
5576 struct ccb_pathinq cpi;
5577 union ccb *ccb;
5578 probe_softc *softc;
5579
5580 softc = (probe_softc *)periph->softc;
5581 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
5582
5583 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
5584 cpi.ccb_h.func_code = XPT_PATH_INQ;
5585 xpt_action((union ccb *)&cpi);
5586
5587 /*
5588 * If a device has gone away and another device, or the same one,
5589 * is back in the same place, it should have a unit attention
5590 * condition pending. It will not report the unit attention in
5591 * response to an inquiry, which may leave invalid transfer
5592 * negotiations in effect. The TUR will reveal the unit attention
5593 * condition. Only send the TUR for lun 0, since some devices
5594 * will get confused by commands other than inquiry to non-existent
5595 * luns. If you think a device has gone away start your scan from
5596 * lun 0. This will insure that any bogus transfer settings are
5597 * invalidated.
5598 *
5599 * If we haven't seen the device before and the controller supports
5600 * some kind of transfer negotiation, negotiate with the first
5601 * sent command if no bus reset was performed at startup. This
5602 * ensures that the device is not confused by transfer negotiation
5603 * settings left over by loader or BIOS action.
5604 */
5605 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5606 && (ccb->ccb_h.target_lun == 0)) {
5607 softc->action = PROBE_TUR;
5608 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
5609 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
5610 proberequestdefaultnegotiation(periph);
5611 softc->action = PROBE_INQUIRY;
5612 } else {
5613 softc->action = PROBE_INQUIRY;
5614 }
5615
5616 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
5617 softc->flags |= PROBE_NO_ANNOUNCE;
5618 else
5619 softc->flags &= ~PROBE_NO_ANNOUNCE;
5620
5621 xpt_schedule(periph, ccb->ccb_h.pinfo.priority);
5622}
5623
5624static void
5625probestart(struct cam_periph *periph, union ccb *start_ccb)
5626{
5627 /* Probe the device that our peripheral driver points to */
5628 struct ccb_scsiio *csio;
5629 probe_softc *softc;
5630
5631 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
5632
5633 softc = (probe_softc *)periph->softc;
5634 csio = &start_ccb->csio;
5635
5636 switch (softc->action) {
5637 case PROBE_TUR:
5638 case PROBE_TUR_FOR_NEGOTIATION:
5639 {
5640 scsi_test_unit_ready(csio,
5641 /*retries*/4,
5642 probedone,
5643 MSG_SIMPLE_Q_TAG,
5644 SSD_FULL_SIZE,
5645 /*timeout*/60000);
5646 break;
5647 }
5648 case PROBE_INQUIRY:
5649 case PROBE_FULL_INQUIRY:
5650 {
5651 u_int inquiry_len;
5652 struct scsi_inquiry_data *inq_buf;
5653
5654 inq_buf = &periph->path->device->inq_data;
5655 /*
5656 * If the device is currently configured, we calculate an
5657 * MD5 checksum of the inquiry data, and if the serial number
5658 * length is greater than 0, add the serial number data
5659 * into the checksum as well. Once the inquiry and the
5660 * serial number check finish, we attempt to figure out
5661 * whether we still have the same device.
5662 */
5663 if ((periph->path->device->flags & CAM_DEV_UNCONFIGURED) == 0) {
5664
5665 MD5Init(&softc->context);
5666 MD5Update(&softc->context, (unsigned char *)inq_buf,
5667 sizeof(struct scsi_inquiry_data));
5668 softc->flags |= PROBE_INQUIRY_CKSUM;
5669 if (periph->path->device->serial_num_len > 0) {
5670 MD5Update(&softc->context,
5671 periph->path->device->serial_num,
5672 periph->path->device->serial_num_len);
5673 softc->flags |= PROBE_SERIAL_CKSUM;
5674 }
5675 MD5Final(softc->digest, &softc->context);
5676 }
5677
5678 if (softc->action == PROBE_INQUIRY)
5679 inquiry_len = SHORT_INQUIRY_LENGTH;
5680 else
5681 inquiry_len = inq_buf->additional_length
5682 + offsetof(struct scsi_inquiry_data,
5683 additional_length) + 1;
5684
5685 /*
5686 * Some parallel SCSI devices fail to send an
5687 * ignore wide residue message when dealing with
5688 * odd length inquiry requests. Round up to be
5689 * safe.
5690 */
5691 inquiry_len = roundup2(inquiry_len, 2);
5692
5693 scsi_inquiry(csio,
5694 /*retries*/4,
5695 probedone,
5696 MSG_SIMPLE_Q_TAG,
5697 (u_int8_t *)inq_buf,
5698 inquiry_len,
5699 /*evpd*/FALSE,
5700 /*page_code*/0,
5701 SSD_MIN_SIZE,
5702 /*timeout*/60 * 1000);
5703 break;
5704 }
5705 case PROBE_MODE_SENSE:
5706 {
5707 void *mode_buf;
5708 int mode_buf_len;
5709
5710 mode_buf_len = sizeof(struct scsi_mode_header_6)
5711 + sizeof(struct scsi_mode_blk_desc)
5712 + sizeof(struct scsi_control_page);
5713 mode_buf = malloc(mode_buf_len, M_TEMP, M_NOWAIT);
5714 if (mode_buf != NULL) {
5715 scsi_mode_sense(csio,
5716 /*retries*/4,
5717 probedone,
5718 MSG_SIMPLE_Q_TAG,
5719 /*dbd*/FALSE,
5720 SMS_PAGE_CTRL_CURRENT,
5721 SMS_CONTROL_MODE_PAGE,
5722 mode_buf,
5723 mode_buf_len,
5724 SSD_FULL_SIZE,
5725 /*timeout*/60000);
5726 break;
5727 }
5728 xpt_print_path(periph->path);
5729 printf("Unable to mode sense control page - malloc failure\n");
5730 softc->action = PROBE_SERIAL_NUM;
5731 }
5732 /* FALLTHROUGH */
5733 case PROBE_SERIAL_NUM:
5734 {
5735 struct scsi_vpd_unit_serial_number *serial_buf;
5736 struct cam_ed* device;
5737
5738 serial_buf = NULL;
5739 device = periph->path->device;
5740 device->serial_num = NULL;
5741 device->serial_num_len = 0;
5742
5743 if ((device->quirk->quirks & CAM_QUIRK_NOSERIAL) == 0)
5744 serial_buf = (struct scsi_vpd_unit_serial_number *)
5745 malloc(sizeof(*serial_buf), M_TEMP,
5746 M_NOWAIT | M_ZERO);
5747
5748 if (serial_buf != NULL) {
5749 scsi_inquiry(csio,
5750 /*retries*/4,
5751 probedone,
5752 MSG_SIMPLE_Q_TAG,
5753 (u_int8_t *)serial_buf,
5754 sizeof(*serial_buf),
5755 /*evpd*/TRUE,
5756 SVPD_UNIT_SERIAL_NUMBER,
5757 SSD_MIN_SIZE,
5758 /*timeout*/60 * 1000);
5759 break;
5760 }
5761 /*
5762 * We'll have to do without, let our probedone
5763 * routine finish up for us.
5764 */
5765 start_ccb->csio.data_ptr = NULL;
5766 probedone(periph, start_ccb);
5767 return;
5768 }
5769 }
5770 xpt_action(start_ccb);
5771}
5772
5773static void
5774proberequestdefaultnegotiation(struct cam_periph *periph)
5775{
5776 struct ccb_trans_settings cts;
5777
5778 xpt_setup_ccb(&cts.ccb_h, periph->path, /*priority*/1);
5779 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
5780#ifdef CAM_NEW_TRAN_CODE
5781 cts.type = CTS_TYPE_USER_SETTINGS;
5782#else /* CAM_NEW_TRAN_CODE */
5783 cts.flags = CCB_TRANS_USER_SETTINGS;
5784#endif /* CAM_NEW_TRAN_CODE */
5785 xpt_action((union ccb *)&cts);
5786 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
5787#ifdef CAM_NEW_TRAN_CODE
5788 cts.type = CTS_TYPE_CURRENT_SETTINGS;
5789#else /* CAM_NEW_TRAN_CODE */
5790 cts.flags &= ~CCB_TRANS_USER_SETTINGS;
5791 cts.flags |= CCB_TRANS_CURRENT_SETTINGS;
5792#endif /* CAM_NEW_TRAN_CODE */
5793 xpt_action((union ccb *)&cts);
5794}
5795
5796static void
5797probedone(struct cam_periph *periph, union ccb *done_ccb)
5798{
5799 probe_softc *softc;
5800 struct cam_path *path;
5801 u_int32_t priority;
5802
5803 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
5804
5805 softc = (probe_softc *)periph->softc;
5806 path = done_ccb->ccb_h.path;
5807 priority = done_ccb->ccb_h.pinfo.priority;
5808
5809 switch (softc->action) {
5810 case PROBE_TUR:
5811 {
5812 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
5813
5814 if (cam_periph_error(done_ccb, 0,
5815 SF_NO_PRINT, NULL) == ERESTART)
5816 return;
5817 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
5818 /* Don't wedge the queue */
5819 xpt_release_devq(done_ccb->ccb_h.path,
5820 /*count*/1,
5821 /*run_queue*/TRUE);
5822 }
5823 softc->action = PROBE_INQUIRY;
5824 xpt_release_ccb(done_ccb);
5825 xpt_schedule(periph, priority);
5826 return;
5827 }
5828 case PROBE_INQUIRY:
5829 case PROBE_FULL_INQUIRY:
5830 {
5831 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5832 struct scsi_inquiry_data *inq_buf;
5833 u_int8_t periph_qual;
5834
5835 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
5836 inq_buf = &path->device->inq_data;
5837
5838 periph_qual = SID_QUAL(inq_buf);
5839
5840 switch(periph_qual) {
5841 case SID_QUAL_LU_CONNECTED:
5842 {
5843 u_int8_t len;
5844
5845 /*
5846 * We conservatively request only
5847 * SHORT_INQUIRY_LEN bytes of inquiry
5848 * information during our first try
5849 * at sending an INQUIRY. If the device
5850 * has more information to give,
5851 * perform a second request specifying
5852 * the amount of information the device
5853 * is willing to give.
5854 */
5855 len = inq_buf->additional_length
5856 + offsetof(struct scsi_inquiry_data,
5857 additional_length) + 1;
5858 if (softc->action == PROBE_INQUIRY
5859 && len > SHORT_INQUIRY_LENGTH) {
5860 softc->action = PROBE_FULL_INQUIRY;
5861 xpt_release_ccb(done_ccb);
5862 xpt_schedule(periph, priority);
5863 return;
5864 }
5865
5866 xpt_find_quirk(path->device);
5867
5868#ifdef CAM_NEW_TRAN_CODE
5869 xpt_devise_transport(path);
5870#endif /* CAM_NEW_TRAN_CODE */
5871 if ((inq_buf->flags & SID_CmdQue) != 0)
5872 softc->action = PROBE_MODE_SENSE;
5873 else
5874 softc->action = PROBE_SERIAL_NUM;
5875
5876 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
5877
5878 xpt_release_ccb(done_ccb);
5879 xpt_schedule(periph, priority);
5880 return;
5881 }
5882 default:
5883 break;
5884 }
5885 } else if (cam_periph_error(done_ccb, 0,
5886 done_ccb->ccb_h.target_lun > 0
5887 ? SF_RETRY_UA|SF_QUIET_IR
5888 : SF_RETRY_UA,
5889 &softc->saved_ccb) == ERESTART) {
5890 return;
5891 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5892 /* Don't wedge the queue */
5893 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5894 /*run_queue*/TRUE);
5895 }
5896 /*
5897 * If we get to this point, we got an error status back
5898 * from the inquiry and the error status doesn't require
5899 * automatically retrying the command. Therefore, the
5900 * inquiry failed. If we had inquiry information before
5901 * for this device, but this latest inquiry command failed,
5902 * the device has probably gone away. If this device isn't
5903 * already marked unconfigured, notify the peripheral
5904 * drivers that this device is no more.
5905 */
5906 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
5907 /* Send the async notification. */
5908 xpt_async(AC_LOST_DEVICE, path, NULL);
5909
5910 xpt_release_ccb(done_ccb);
5911 break;
5912 }
5913 case PROBE_MODE_SENSE:
5914 {
5915 struct ccb_scsiio *csio;
5916 struct scsi_mode_header_6 *mode_hdr;
5917
5918 csio = &done_ccb->csio;
5919 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
5920 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
5921 struct scsi_control_page *page;
5922 u_int8_t *offset;
5923
5924 offset = ((u_int8_t *)&mode_hdr[1])
5925 + mode_hdr->blk_desc_len;
5926 page = (struct scsi_control_page *)offset;
5927 path->device->queue_flags = page->queue_flags;
5928 } else if (cam_periph_error(done_ccb, 0,
5929 SF_RETRY_UA|SF_NO_PRINT,
5930 &softc->saved_ccb) == ERESTART) {
5931 return;
5932 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5933 /* Don't wedge the queue */
5934 xpt_release_devq(done_ccb->ccb_h.path,
5935 /*count*/1, /*run_queue*/TRUE);
5936 }
5937 xpt_release_ccb(done_ccb);
5938 free(mode_hdr, M_TEMP);
5939 softc->action = PROBE_SERIAL_NUM;
5940 xpt_schedule(periph, priority);
5941 return;
5942 }
5943 case PROBE_SERIAL_NUM:
5944 {
5945 struct ccb_scsiio *csio;
5946 struct scsi_vpd_unit_serial_number *serial_buf;
5947 u_int32_t priority;
5948 int changed;
5949 int have_serialnum;
5950
5951 changed = 1;
5952 have_serialnum = 0;
5953 csio = &done_ccb->csio;
5954 priority = done_ccb->ccb_h.pinfo.priority;
5955 serial_buf =
5956 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
5957
5958 /* Clean up from previous instance of this device */
5959 if (path->device->serial_num != NULL) {
5960 free(path->device->serial_num, M_DEVBUF);
5961 path->device->serial_num = NULL;
5962 path->device->serial_num_len = 0;
5963 }
5964
5965 if (serial_buf == NULL) {
5966 /*
5967 * Don't process the command as it was never sent
5968 */
5969 } else if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP
5970 && (serial_buf->length > 0)) {
5971
5972 have_serialnum = 1;
5973 path->device->serial_num =
5974 (u_int8_t *)malloc((serial_buf->length + 1),
5975 M_DEVBUF, M_NOWAIT);
5976 if (path->device->serial_num != NULL) {
5977 bcopy(serial_buf->serial_num,
5978 path->device->serial_num,
5979 serial_buf->length);
5980 path->device->serial_num_len =
5981 serial_buf->length;
5982 path->device->serial_num[serial_buf->length]
5983 = '\0';
5984 }
5985 } else if (cam_periph_error(done_ccb, 0,
5986 SF_RETRY_UA|SF_NO_PRINT,
5987 &softc->saved_ccb) == ERESTART) {
5988 return;
5989 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
5990 /* Don't wedge the queue */
5991 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
5992 /*run_queue*/TRUE);
5993 }
5994
5995 /*
5996 * Let's see if we have seen this device before.
5997 */
5998 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
5999 MD5_CTX context;
6000 u_int8_t digest[16];
6001
6002 MD5Init(&context);
6003
6004 MD5Update(&context,
6005 (unsigned char *)&path->device->inq_data,
6006 sizeof(struct scsi_inquiry_data));
6007
6008 if (have_serialnum)
6009 MD5Update(&context, serial_buf->serial_num,
6010 serial_buf->length);
6011
6012 MD5Final(digest, &context);
6013 if (bcmp(softc->digest, digest, 16) == 0)
6014 changed = 0;
6015
6016 /*
6017 * XXX Do we need to do a TUR in order to ensure
6018 * that the device really hasn't changed???
6019 */
6020 if ((changed != 0)
6021 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
6022 xpt_async(AC_LOST_DEVICE, path, NULL);
6023 }
6024 if (serial_buf != NULL)
6025 free(serial_buf, M_TEMP);
6026
6027 if (changed != 0) {
6028 /*
6029 * Now that we have all the necessary
6030 * information to safely perform transfer
6031 * negotiations... Controllers don't perform
6032 * any negotiation or tagged queuing until
6033 * after the first XPT_SET_TRAN_SETTINGS ccb is
6034 * received. So, on a new device, just retreive
6035 * the user settings, and set them as the current
6036 * settings to set the device up.
6037 */
6038 proberequestdefaultnegotiation(periph);
6039 xpt_release_ccb(done_ccb);
6040
6041 /*
6042 * Perform a TUR to allow the controller to
6043 * perform any necessary transfer negotiation.
6044 */
6045 softc->action = PROBE_TUR_FOR_NEGOTIATION;
6046 xpt_schedule(periph, priority);
6047 return;
6048 }
6049 xpt_release_ccb(done_ccb);
6050 break;
6051 }
6052 case PROBE_TUR_FOR_NEGOTIATION:
6053 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
6054 /* Don't wedge the queue */
6055 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
6056 /*run_queue*/TRUE);
6057 }
6058
6059 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
6060
6061 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
6062 /* Inform the XPT that a new device has been found */
6063 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
6064 xpt_action(done_ccb);
6065
6066 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
6067 done_ccb);
6068 }
6069 xpt_release_ccb(done_ccb);
6070 break;
6071 }
6072 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
6073 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
6074 done_ccb->ccb_h.status = CAM_REQ_CMP;
6075 xpt_done(done_ccb);
6076 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
6077 cam_periph_invalidate(periph);
6078 cam_periph_release(periph);
6079 } else {
6080 probeschedule(periph);
6081 }
6082}
6083
6084static void
6085probecleanup(struct cam_periph *periph)
6086{
6087 free(periph->softc, M_TEMP);
6088}
6089
6090static void
6091xpt_find_quirk(struct cam_ed *device)
6092{
6093 caddr_t match;
6094
6095 match = cam_quirkmatch((caddr_t)&device->inq_data,
6096 (caddr_t)xpt_quirk_table,
6097 sizeof(xpt_quirk_table)/sizeof(*xpt_quirk_table),
6098 sizeof(*xpt_quirk_table), scsi_inquiry_match);
6099
6100 if (match == NULL)
6101 panic("xpt_find_quirk: device didn't match wildcard entry!!");
6102
6103 device->quirk = (struct xpt_quirk_entry *)match;
6104}
6105
6106#ifdef CAM_NEW_TRAN_CODE
6107
6108static void
6109xpt_devise_transport(struct cam_path *path)
6110{
6111 struct ccb_pathinq cpi;
6112 struct ccb_trans_settings cts;
6113 struct scsi_inquiry_data *inq_buf;
6114
6115 /* Get transport information from the SIM */
6116 xpt_setup_ccb(&cpi.ccb_h, path, /*priority*/1);
6117 cpi.ccb_h.func_code = XPT_PATH_INQ;
6118 xpt_action((union ccb *)&cpi);
6119
6120 inq_buf = NULL;
6121 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
6122 inq_buf = &path->device->inq_data;
6123 path->device->protocol = PROTO_SCSI;
6124 path->device->protocol_version =
6125 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
6126 path->device->transport = cpi.transport;
6127 path->device->transport_version = cpi.transport_version;
6128
6129 /*
6130 * Any device not using SPI3 features should
6131 * be considered SPI2 or lower.
6132 */
6133 if (inq_buf != NULL) {
6134 if (path->device->transport == XPORT_SPI
6135 && (inq_buf->spi3data & SID_SPI_MASK) == 0
6136 && path->device->transport_version > 2)
6137 path->device->transport_version = 2;
6138 } else {
6139 struct cam_ed* otherdev;
6140
6141 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
6142 otherdev != NULL;
6143 otherdev = TAILQ_NEXT(otherdev, links)) {
6144 if (otherdev != path->device)
6145 break;
6146 }
6147
6148 if (otherdev != NULL) {
6149 /*
6150 * Initially assume the same versioning as
6151 * prior luns for this target.
6152 */
6153 path->device->protocol_version =
6154 otherdev->protocol_version;
6155 path->device->transport_version =
6156 otherdev->transport_version;
6157 } else {
6158 /* Until we know better, opt for safty */
6159 path->device->protocol_version = 2;
6160 if (path->device->transport == XPORT_SPI)
6161 path->device->transport_version = 2;
6162 else
6163 path->device->transport_version = 0;
6164 }
6165 }
6166
6167 /*
6168 * XXX
6169 * For a device compliant with SPC-2 we should be able
6170 * to determine the transport version supported by
6171 * scrutinizing the version descriptors in the
6172 * inquiry buffer.
6173 */
6174
6175 /* Tell the controller what we think */
6176 xpt_setup_ccb(&cts.ccb_h, path, /*priority*/1);
6177 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
6178 cts.type = CTS_TYPE_CURRENT_SETTINGS;
6179 cts.transport = path->device->transport;
6180 cts.transport_version = path->device->transport_version;
6181 cts.protocol = path->device->protocol;
6182 cts.protocol_version = path->device->protocol_version;
6183 cts.proto_specific.valid = 0;
6184 cts.xport_specific.valid = 0;
6185 xpt_action((union ccb *)&cts);
6186}
6187
6188static void
6189xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6190 int async_update)
6191{
6192 struct ccb_pathinq cpi;
6193 struct ccb_trans_settings cur_cts;
6194 struct ccb_trans_settings_scsi *scsi;
6195 struct ccb_trans_settings_scsi *cur_scsi;
6196 struct cam_sim *sim;
6197 struct scsi_inquiry_data *inq_data;
6198
6199 if (device == NULL) {
6200 cts->ccb_h.status = CAM_PATH_INVALID;
6201 xpt_done((union ccb *)cts);
6202 return;
6203 }
6204
6205 if (cts->protocol == PROTO_UNKNOWN
6206 || cts->protocol == PROTO_UNSPECIFIED) {
6207 cts->protocol = device->protocol;
6208 cts->protocol_version = device->protocol_version;
6209 }
6210
6211 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
6212 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
6213 cts->protocol_version = device->protocol_version;
6214
6215 if (cts->protocol != device->protocol) {
6216 xpt_print_path(cts->ccb_h.path);
6217 printf("Uninitialized Protocol %x:%x?\n",
6218 cts->protocol, device->protocol);
6219 cts->protocol = device->protocol;
6220 }
6221
6222 if (cts->protocol_version > device->protocol_version) {
6223 if (bootverbose) {
6224 xpt_print_path(cts->ccb_h.path);
6225 printf("Down reving Protocol Version from %d to %d?\n",
6226 cts->protocol_version, device->protocol_version);
6227 }
6228 cts->protocol_version = device->protocol_version;
6229 }
6230
6231 if (cts->transport == XPORT_UNKNOWN
6232 || cts->transport == XPORT_UNSPECIFIED) {
6233 cts->transport = device->transport;
6234 cts->transport_version = device->transport_version;
6235 }
6236
6237 if (cts->transport_version == XPORT_VERSION_UNKNOWN
6238 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
6239 cts->transport_version = device->transport_version;
6240
6241 if (cts->transport != device->transport) {
6242 xpt_print_path(cts->ccb_h.path);
6243 printf("Uninitialized Transport %x:%x?\n",
6244 cts->transport, device->transport);
6245 cts->transport = device->transport;
6246 }
6247
6248 if (cts->transport_version > device->transport_version) {
6249 if (bootverbose) {
6250 xpt_print_path(cts->ccb_h.path);
6251 printf("Down reving Transport Version from %d to %d?\n",
6252 cts->transport_version,
6253 device->transport_version);
6254 }
6255 cts->transport_version = device->transport_version;
6256 }
6257
6258 sim = cts->ccb_h.path->bus->sim;
6259
6260 /*
6261 * Nothing more of interest to do unless
6262 * this is a device connected via the
6263 * SCSI protocol.
6264 */
6265 if (cts->protocol != PROTO_SCSI) {
6266 if (async_update == FALSE)
6267 (*(sim->sim_action))(sim, (union ccb *)cts);
6268 return;
6269 }
6270
6271 inq_data = &device->inq_data;
6272 scsi = &cts->proto_specific.scsi;
6273 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6274 cpi.ccb_h.func_code = XPT_PATH_INQ;
6275 xpt_action((union ccb *)&cpi);
6276
6277 /* SCSI specific sanity checking */
6278 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6279 || (inq_data->flags & SID_CmdQue) == 0
6280 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6281 || (device->quirk->mintags == 0)) {
6282 /*
6283 * Can't tag on hardware that doesn't support tags,
6284 * doesn't have it enabled, or has broken tag support.
6285 */
6286 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6287 }
6288
6289 if (async_update == FALSE) {
6290 /*
6291 * Perform sanity checking against what the
6292 * controller and device can do.
6293 */
6294 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6295 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6296 cur_cts.type = cts->type;
6297 xpt_action((union ccb *)&cur_cts);
6298
6299 cur_scsi = &cur_cts.proto_specific.scsi;
6300 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
6301 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6302 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
6303 }
6304 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
6305 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6306 }
6307
6308 /* SPI specific sanity checking */
6309 if (cts->transport == XPORT_SPI && async_update == FALSE) {
6310 u_int spi3caps;
6311 struct ccb_trans_settings_spi *spi;
6312 struct ccb_trans_settings_spi *cur_spi;
6313
6314 spi = &cts->xport_specific.spi;
6315
6316 cur_spi = &cur_cts.xport_specific.spi;
6317
6318 /* Fill in any gaps in what the user gave us */
6319 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6320 spi->sync_period = cur_spi->sync_period;
6321 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
6322 spi->sync_period = 0;
6323 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6324 spi->sync_offset = cur_spi->sync_offset;
6325 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
6326 spi->sync_offset = 0;
6327 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6328 spi->ppr_options = cur_spi->ppr_options;
6329 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
6330 spi->ppr_options = 0;
6331 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6332 spi->bus_width = cur_spi->bus_width;
6333 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
6334 spi->bus_width = 0;
6335 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
6336 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6337 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
6338 }
6339 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
6340 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
6341 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6342 && (inq_data->flags & SID_Sync) == 0
6343 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6344 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6345 || (cur_spi->sync_offset == 0)
6346 || (cur_spi->sync_period == 0)) {
6347 /* Force async */
6348 spi->sync_period = 0;
6349 spi->sync_offset = 0;
6350 }
6351
6352 switch (spi->bus_width) {
6353 case MSG_EXT_WDTR_BUS_32_BIT:
6354 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6355 || (inq_data->flags & SID_WBus32) != 0
6356 || cts->type == CTS_TYPE_USER_SETTINGS)
6357 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6358 break;
6359 /* Fall Through to 16-bit */
6360 case MSG_EXT_WDTR_BUS_16_BIT:
6361 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6362 || (inq_data->flags & SID_WBus16) != 0
6363 || cts->type == CTS_TYPE_USER_SETTINGS)
6364 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6365 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6366 break;
6367 }
6368 /* Fall Through to 8-bit */
6369 default: /* New bus width?? */
6370 case MSG_EXT_WDTR_BUS_8_BIT:
6371 /* All targets can do this */
6372 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6373 break;
6374 }
6375
6376 spi3caps = cpi.xport_specific.spi.ppr_options;
6377 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6378 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
6379 spi3caps &= inq_data->spi3data;
6380
6381 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
6382 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
6383
6384 if ((spi3caps & SID_SPI_IUS) == 0)
6385 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
6386
6387 if ((spi3caps & SID_SPI_QAS) == 0)
6388 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
6389
6390 /* No SPI Transfer settings are allowed unless we are wide */
6391 if (spi->bus_width == 0)
6392 spi->ppr_options = 0;
6393
6394 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0) {
6395 /*
6396 * Can't tag queue without disconnection.
6397 */
6398 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
6399 scsi->valid |= CTS_SCSI_VALID_TQ;
6400 }
6401
6402 /*
6403 * If we are currently performing tagged transactions to
6404 * this device and want to change its negotiation parameters,
6405 * go non-tagged for a bit to give the controller a chance to
6406 * negotiate unhampered by tag messages.
6407 */
6408 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6409 && (device->inq_flags & SID_CmdQue) != 0
6410 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6411 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
6412 CTS_SPI_VALID_SYNC_OFFSET|
6413 CTS_SPI_VALID_BUS_WIDTH)) != 0)
6414 xpt_toggle_tags(cts->ccb_h.path);
6415 }
6416
6417 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
6418 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
6419 int device_tagenb;
6420
6421 /*
6422 * If we are transitioning from tags to no-tags or
6423 * vice-versa, we need to carefully freeze and restart
6424 * the queue so that we don't overlap tagged and non-tagged
6425 * commands. We also temporarily stop tags if there is
6426 * a change in transfer negotiation settings to allow
6427 * "tag-less" negotiation.
6428 */
6429 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6430 || (device->inq_flags & SID_CmdQue) != 0)
6431 device_tagenb = TRUE;
6432 else
6433 device_tagenb = FALSE;
6434
6435 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
6436 && device_tagenb == FALSE)
6437 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
6438 && device_tagenb == TRUE)) {
6439
6440 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
6441 /*
6442 * Delay change to use tags until after a
6443 * few commands have gone to this device so
6444 * the controller has time to perform transfer
6445 * negotiations without tagged messages getting
6446 * in the way.
6447 */
6448 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6449 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6450 } else {
6451 struct ccb_relsim crs;
6452
6453 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6454 device->inq_flags &= ~SID_CmdQue;
6455 xpt_dev_ccbq_resize(cts->ccb_h.path,
6456 sim->max_dev_openings);
6457 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6458 device->tag_delay_count = 0;
6459
6460 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6461 /*priority*/1);
6462 crs.ccb_h.func_code = XPT_REL_SIMQ;
6463 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6464 crs.openings
6465 = crs.release_timeout
6466 = crs.qfrozen_cnt
6467 = 0;
6468 xpt_action((union ccb *)&crs);
6469 }
6470 }
6471 }
6472 if (async_update == FALSE)
6473 (*(sim->sim_action))(sim, (union ccb *)cts);
6474}
6475
6476#else /* CAM_NEW_TRAN_CODE */
6477
6478static void
6479xpt_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_ed *device,
6480 int async_update)
6481{
6482 struct cam_sim *sim;
6483 int qfrozen;
6484
6485 sim = cts->ccb_h.path->bus->sim;
6486 if (async_update == FALSE) {
6487 struct scsi_inquiry_data *inq_data;
6488 struct ccb_pathinq cpi;
6489 struct ccb_trans_settings cur_cts;
6490
6491 if (device == NULL) {
6492 cts->ccb_h.status = CAM_PATH_INVALID;
6493 xpt_done((union ccb *)cts);
6494 return;
6495 }
6496
6497 /*
6498 * Perform sanity checking against what the
6499 * controller and device can do.
6500 */
6501 xpt_setup_ccb(&cpi.ccb_h, cts->ccb_h.path, /*priority*/1);
6502 cpi.ccb_h.func_code = XPT_PATH_INQ;
6503 xpt_action((union ccb *)&cpi);
6504 xpt_setup_ccb(&cur_cts.ccb_h, cts->ccb_h.path, /*priority*/1);
6505 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
6506 cur_cts.flags = CCB_TRANS_CURRENT_SETTINGS;
6507 xpt_action((union ccb *)&cur_cts);
6508 inq_data = &device->inq_data;
6509
6510 /* Fill in any gaps in what the user gave us */
6511 if ((cts->valid & CCB_TRANS_SYNC_RATE_VALID) == 0)
6512 cts->sync_period = cur_cts.sync_period;
6513 if ((cts->valid & CCB_TRANS_SYNC_OFFSET_VALID) == 0)
6514 cts->sync_offset = cur_cts.sync_offset;
6515 if ((cts->valid & CCB_TRANS_BUS_WIDTH_VALID) == 0)
6516 cts->bus_width = cur_cts.bus_width;
6517 if ((cts->valid & CCB_TRANS_DISC_VALID) == 0) {
6518 cts->flags &= ~CCB_TRANS_DISC_ENB;
6519 cts->flags |= cur_cts.flags & CCB_TRANS_DISC_ENB;
6520 }
6521 if ((cts->valid & CCB_TRANS_TQ_VALID) == 0) {
6522 cts->flags &= ~CCB_TRANS_TAG_ENB;
6523 cts->flags |= cur_cts.flags & CCB_TRANS_TAG_ENB;
6524 }
6525
6526 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6527 && (inq_data->flags & SID_Sync) == 0)
6528 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)
6529 || (cts->sync_offset == 0)
6530 || (cts->sync_period == 0)) {
6531 /* Force async */
6532 cts->sync_period = 0;
6533 cts->sync_offset = 0;
6534 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
6535 && (inq_data->spi3data & SID_SPI_CLOCK_DT) == 0
6536 && cts->sync_period <= 0x9) {
6537 /*
6538 * Don't allow DT transmission rates if the
6539 * device does not support it.
6540 */
6541 cts->sync_period = 0xa;
6542 }
6543
6544 switch (cts->bus_width) {
6545 case MSG_EXT_WDTR_BUS_32_BIT:
6546 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6547 || (inq_data->flags & SID_WBus32) != 0)
6548 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
6549 break;
6550 /* FALLTHROUGH to 16-bit */
6551 case MSG_EXT_WDTR_BUS_16_BIT:
6552 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
6553 || (inq_data->flags & SID_WBus16) != 0)
6554 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
6555 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
6556 break;
6557 }
6558 /* FALLTHROUGH to 8-bit */
6559 default: /* New bus width?? */
6560 case MSG_EXT_WDTR_BUS_8_BIT:
6561 /* All targets can do this */
6562 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
6563 break;
6564 }
6565
6566 if ((cts->flags & CCB_TRANS_DISC_ENB) == 0) {
6567 /*
6568 * Can't tag queue without disconnection.
6569 */
6570 cts->flags &= ~CCB_TRANS_TAG_ENB;
6571 cts->valid |= CCB_TRANS_TQ_VALID;
6572 }
6573
6574 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
6575 || (inq_data->flags & SID_CmdQue) == 0
6576 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
6577 || (device->quirk->mintags == 0)) {
6578 /*
6579 * Can't tag on hardware that doesn't support,
6580 * doesn't have it enabled, or has broken tag support.
6581 */
6582 cts->flags &= ~CCB_TRANS_TAG_ENB;
6583 }
6584 }
6585
6586 qfrozen = FALSE;
6587 if ((cts->valid & CCB_TRANS_TQ_VALID) != 0) {
6588 int device_tagenb;
6589
6590 /*
6591 * If we are transitioning from tags to no-tags or
6592 * vice-versa, we need to carefully freeze and restart
6593 * the queue so that we don't overlap tagged and non-tagged
6594 * commands. We also temporarily stop tags if there is
6595 * a change in transfer negotiation settings to allow
6596 * "tag-less" negotiation.
6597 */
6598 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6599 || (device->inq_flags & SID_CmdQue) != 0)
6600 device_tagenb = TRUE;
6601 else
6602 device_tagenb = FALSE;
6603
6604 if (((cts->flags & CCB_TRANS_TAG_ENB) != 0
6605 && device_tagenb == FALSE)
6606 || ((cts->flags & CCB_TRANS_TAG_ENB) == 0
6607 && device_tagenb == TRUE)) {
6608
6609 if ((cts->flags & CCB_TRANS_TAG_ENB) != 0) {
6610 /*
6611 * Delay change to use tags until after a
6612 * few commands have gone to this device so
6613 * the controller has time to perform transfer
6614 * negotiations without tagged messages getting
6615 * in the way.
6616 */
6617 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
6618 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
6619 } else {
6620 xpt_freeze_devq(cts->ccb_h.path, /*count*/1);
6621 qfrozen = TRUE;
6622 device->inq_flags &= ~SID_CmdQue;
6623 xpt_dev_ccbq_resize(cts->ccb_h.path,
6624 sim->max_dev_openings);
6625 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6626 device->tag_delay_count = 0;
6627 }
6628 }
6629 }
6630
6631 if (async_update == FALSE) {
6632 /*
6633 * If we are currently performing tagged transactions to
6634 * this device and want to change its negotiation parameters,
6635 * go non-tagged for a bit to give the controller a chance to
6636 * negotiate unhampered by tag messages.
6637 */
6638 if ((device->inq_flags & SID_CmdQue) != 0
6639 && (cts->flags & (CCB_TRANS_SYNC_RATE_VALID|
6640 CCB_TRANS_SYNC_OFFSET_VALID|
6641 CCB_TRANS_BUS_WIDTH_VALID)) != 0)
6642 xpt_toggle_tags(cts->ccb_h.path);
6643
6644 (*(sim->sim_action))(sim, (union ccb *)cts);
6645 }
6646
6647 if (qfrozen) {
6648 struct ccb_relsim crs;
6649
6650 xpt_setup_ccb(&crs.ccb_h, cts->ccb_h.path,
6651 /*priority*/1);
6652 crs.ccb_h.func_code = XPT_REL_SIMQ;
6653 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6654 crs.openings
6655 = crs.release_timeout
6656 = crs.qfrozen_cnt
6657 = 0;
6658 xpt_action((union ccb *)&crs);
6659 }
6660}
6661
6662
6663#endif /* CAM_NEW_TRAN_CODE */
6664
6665static void
6666xpt_toggle_tags(struct cam_path *path)
6667{
6668 struct cam_ed *dev;
6669
6670 /*
6671 * Give controllers a chance to renegotiate
6672 * before starting tag operations. We
6673 * "toggle" tagged queuing off then on
6674 * which causes the tag enable command delay
6675 * counter to come into effect.
6676 */
6677 dev = path->device;
6678 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
6679 || ((dev->inq_flags & SID_CmdQue) != 0
6680 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
6681 struct ccb_trans_settings cts;
6682
6683 xpt_setup_ccb(&cts.ccb_h, path, 1);
6684#ifdef CAM_NEW_TRAN_CODE
6685 cts.protocol = PROTO_SCSI;
6686 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
6687 cts.transport = XPORT_UNSPECIFIED;
6688 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
6689 cts.proto_specific.scsi.flags = 0;
6690 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
6691#else /* CAM_NEW_TRAN_CODE */
6692 cts.flags = 0;
6693 cts.valid = CCB_TRANS_TQ_VALID;
6694#endif /* CAM_NEW_TRAN_CODE */
6695 xpt_set_transfer_settings(&cts, path->device,
6696 /*async_update*/TRUE);
6697#ifdef CAM_NEW_TRAN_CODE
6698 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
6699#else /* CAM_NEW_TRAN_CODE */
6700 cts.flags = CCB_TRANS_TAG_ENB;
6701#endif /* CAM_NEW_TRAN_CODE */
6702 xpt_set_transfer_settings(&cts, path->device,
6703 /*async_update*/TRUE);
6704 }
6705}
6706
6707static void
6708xpt_start_tags(struct cam_path *path)
6709{
6710 struct ccb_relsim crs;
6711 struct cam_ed *device;
6712 struct cam_sim *sim;
6713 int newopenings;
6714
6715 device = path->device;
6716 sim = path->bus->sim;
6717 device->flags &= ~CAM_DEV_TAG_AFTER_COUNT;
6718 xpt_freeze_devq(path, /*count*/1);
6719 device->inq_flags |= SID_CmdQue;
6720 if (device->tag_saved_openings != 0)
6721 newopenings = device->tag_saved_openings;
6722 else
6723 newopenings = min(device->quirk->maxtags,
6724 sim->max_tagged_dev_openings);
6725 xpt_dev_ccbq_resize(path, newopenings);
6726 xpt_setup_ccb(&crs.ccb_h, path, /*priority*/1);
6727 crs.ccb_h.func_code = XPT_REL_SIMQ;
6728 crs.release_flags = RELSIM_RELEASE_AFTER_QEMPTY;
6729 crs.openings
6730 = crs.release_timeout
6731 = crs.qfrozen_cnt
6732 = 0;
6733 xpt_action((union ccb *)&crs);
6734}
6735
6736static int busses_to_config;
6737static int busses_to_reset;
6738
6739static int
6740xptconfigbuscountfunc(struct cam_eb *bus, void *arg)
6741{
6742 if (bus->path_id != CAM_XPT_PATH_ID) {
6743 struct cam_path path;
6744 struct ccb_pathinq cpi;
6745 int can_negotiate;
6746
6747 busses_to_config++;
6748 xpt_compile_path(&path, NULL, bus->path_id,
6749 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
6750 xpt_setup_ccb(&cpi.ccb_h, &path, /*priority*/1);
6751 cpi.ccb_h.func_code = XPT_PATH_INQ;
6752 xpt_action((union ccb *)&cpi);
6753 can_negotiate = cpi.hba_inquiry;
6754 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6755 if ((cpi.hba_misc & PIM_NOBUSRESET) == 0
6756 && can_negotiate)
6757 busses_to_reset++;
6758 xpt_release_path(&path);
6759 }
6760
6761 return(1);
6762}
6763
6764static int
6765xptconfigfunc(struct cam_eb *bus, void *arg)
6766{
6767 struct cam_path *path;
6768 union ccb *work_ccb;
6769
6770 if (bus->path_id != CAM_XPT_PATH_ID) {
6771 cam_status status;
6772 int can_negotiate;
6773
6774 work_ccb = xpt_alloc_ccb();
6775 if ((status = xpt_create_path(&path, xpt_periph, bus->path_id,
6776 CAM_TARGET_WILDCARD,
6777 CAM_LUN_WILDCARD)) !=CAM_REQ_CMP){
6778 printf("xptconfigfunc: xpt_create_path failed with "
6779 "status %#x for bus %d\n", status, bus->path_id);
6780 printf("xptconfigfunc: halting bus configuration\n");
6781 xpt_free_ccb(work_ccb);
6782 busses_to_config--;
6783 xpt_finishconfig(xpt_periph, NULL);
6784 return(0);
6785 }
6786 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6787 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
6788 xpt_action(work_ccb);
6789 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
6790 printf("xptconfigfunc: CPI failed on bus %d "
6791 "with status %d\n", bus->path_id,
6792 work_ccb->ccb_h.status);
6793 xpt_finishconfig(xpt_periph, work_ccb);
6794 return(1);
6795 }
6796
6797 can_negotiate = work_ccb->cpi.hba_inquiry;
6798 can_negotiate &= (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE);
6799 if ((work_ccb->cpi.hba_misc & PIM_NOBUSRESET) == 0
6800 && (can_negotiate != 0)) {
6801 xpt_setup_ccb(&work_ccb->ccb_h, path, /*priority*/1);
6802 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6803 work_ccb->ccb_h.cbfcnp = NULL;
6804 CAM_DEBUG(path, CAM_DEBUG_SUBTRACE,
6805 ("Resetting Bus\n"));
6806 xpt_action(work_ccb);
6807 xpt_finishconfig(xpt_periph, work_ccb);
6808 } else {
6809 /* Act as though we performed a successful BUS RESET */
6810 work_ccb->ccb_h.func_code = XPT_RESET_BUS;
6811 xpt_finishconfig(xpt_periph, work_ccb);
6812 }
6813 }
6814
6815 return(1);
6816}
6817
6818static void
6819xpt_config(void *arg)
6820{
6821 /*
6822 * Now that interrupts are enabled, go find our devices
6823 */
6824
6825#ifdef CAMDEBUG
6826 /* Setup debugging flags and path */
6827#ifdef CAM_DEBUG_FLAGS
6828 cam_dflags = CAM_DEBUG_FLAGS;
6829#else /* !CAM_DEBUG_FLAGS */
6830 cam_dflags = CAM_DEBUG_NONE;
6831#endif /* CAM_DEBUG_FLAGS */
6832#ifdef CAM_DEBUG_BUS
6833 if (cam_dflags != CAM_DEBUG_NONE) {
6834 if (xpt_create_path(&cam_dpath, xpt_periph,
6835 CAM_DEBUG_BUS, CAM_DEBUG_TARGET,
6836 CAM_DEBUG_LUN) != CAM_REQ_CMP) {
6837 printf("xpt_config: xpt_create_path() failed for debug"
6838 " target %d:%d:%d, debugging disabled\n",
6839 CAM_DEBUG_BUS, CAM_DEBUG_TARGET, CAM_DEBUG_LUN);
6840 cam_dflags = CAM_DEBUG_NONE;
6841 }
6842 } else
6843 cam_dpath = NULL;
6844#else /* !CAM_DEBUG_BUS */
6845 cam_dpath = NULL;
6846#endif /* CAM_DEBUG_BUS */
6847#endif /* CAMDEBUG */
6848
6849 /*
6850 * Scan all installed busses.
6851 */
6852 xpt_for_all_busses(xptconfigbuscountfunc, NULL);
6853
6854 if (busses_to_config == 0) {
6855 /* Call manually because we don't have any busses */
6856 xpt_finishconfig(xpt_periph, NULL);
6857 } else {
6858 if (busses_to_reset > 0 && scsi_delay >= 2000) {
6859 printf("Waiting %d seconds for SCSI "
6860 "devices to settle\n", scsi_delay/1000);
6861 }
6862 xpt_for_all_busses(xptconfigfunc, NULL);
6863 }
6864}
6865
6866/*
6867 * If the given device only has one peripheral attached to it, and if that
6868 * peripheral is the passthrough driver, announce it. This insures that the
6869 * user sees some sort of announcement for every peripheral in their system.
6870 */
6871static int
6872xptpassannouncefunc(struct cam_ed *device, void *arg)
6873{
6874 struct cam_periph *periph;
6875 int i;
6876
6877 for (periph = SLIST_FIRST(&device->periphs), i = 0; periph != NULL;
6878 periph = SLIST_NEXT(periph, periph_links), i++);
6879
6880 periph = SLIST_FIRST(&device->periphs);
6881 if ((i == 1)
6882 && (strncmp(periph->periph_name, "pass", 4) == 0))
6883 xpt_announce_periph(periph, NULL);
6884
6885 return(1);
6886}
6887
6888static void
6889xpt_finishconfig(struct cam_periph *periph, union ccb *done_ccb)
6890{
6891 struct periph_driver **p_drv;
6892 int i;
6893
6894 if (done_ccb != NULL) {
6895 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE,
6896 ("xpt_finishconfig\n"));
6897 switch(done_ccb->ccb_h.func_code) {
6898 case XPT_RESET_BUS:
6899 if (done_ccb->ccb_h.status == CAM_REQ_CMP) {
6900 done_ccb->ccb_h.func_code = XPT_SCAN_BUS;
6901 done_ccb->ccb_h.cbfcnp = xpt_finishconfig;
6902 done_ccb->crcn.flags = 0;
6903 xpt_action(done_ccb);
6904 return;
6905 }
6906 /* FALLTHROUGH */
6907 case XPT_SCAN_BUS:
6908 default:
6909 xpt_free_path(done_ccb->ccb_h.path);
6910 busses_to_config--;
6911 break;
6912 }
6913 }
6914
6915 if (busses_to_config == 0) {
6916 /* Register all the peripheral drivers */
6917 /* XXX This will have to change when we have loadable modules */
6918 p_drv = periph_drivers;
6919 for (i = 0; p_drv[i] != NULL; i++) {
6920 (*p_drv[i]->init)();
6921 }
6922
6923 /*
6924 * Check for devices with no "standard" peripheral driver
6925 * attached. For any devices like that, announce the
6926 * passthrough driver so the user will see something.
6927 */
6928 xpt_for_all_devices(xptpassannouncefunc, NULL);
6929
6930 /* Release our hook so that the boot can continue. */
6931 config_intrhook_disestablish(xpt_config_hook);
6932 free(xpt_config_hook, M_TEMP);
6933 xpt_config_hook = NULL;
6934 }
6935 if (done_ccb != NULL)
6936 xpt_free_ccb(done_ccb);
6937}
6938
6939static void
6940xptaction(struct cam_sim *sim, union ccb *work_ccb)
6941{
6942 CAM_DEBUG(work_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("xptaction\n"));
6943
6944 switch (work_ccb->ccb_h.func_code) {
6945 /* Common cases first */
6946 case XPT_PATH_INQ: /* Path routing inquiry */
6947 {
6948 struct ccb_pathinq *cpi;
6949
6950 cpi = &work_ccb->cpi;
6951 cpi->version_num = 1; /* XXX??? */
6952 cpi->hba_inquiry = 0;
6953 cpi->target_sprt = 0;
6954 cpi->hba_misc = 0;
6955 cpi->hba_eng_cnt = 0;
6956 cpi->max_target = 0;
6957 cpi->max_lun = 0;
6958 cpi->initiator_id = 0;
6959 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
6960 strncpy(cpi->hba_vid, "", HBA_IDLEN);
6961 strncpy(cpi->dev_name, sim->sim_name, DEV_IDLEN);
6962 cpi->unit_number = sim->unit_number;
6963 cpi->bus_id = sim->bus_id;
6964 cpi->base_transfer_speed = 0;
6965#ifdef CAM_NEW_TRAN_CODE
6966 cpi->protocol = PROTO_UNSPECIFIED;
6967 cpi->protocol_version = PROTO_VERSION_UNSPECIFIED;
6968 cpi->transport = XPORT_UNSPECIFIED;
6969 cpi->transport_version = XPORT_VERSION_UNSPECIFIED;
6970#endif /* CAM_NEW_TRAN_CODE */
6971 cpi->ccb_h.status = CAM_REQ_CMP;
6972 xpt_done(work_ccb);
6973 break;
6974 }
6975 default:
6976 work_ccb->ccb_h.status = CAM_REQ_INVALID;
6977 xpt_done(work_ccb);
6978 break;
6979 }
6980}
6981
6982/*
6983 * The xpt as a "controller" has no interrupt sources, so polling
6984 * is a no-op.
6985 */
6986static void
6987xptpoll(struct cam_sim *sim)
6988{
6989}
6990
6991static void
6992camisr(void *V_queue)
6993{
6994 cam_isrq_t *oqueue = V_queue;
6995 cam_isrq_t queue;
6996 int s;
6997 struct ccb_hdr *ccb_h;
6998
6999 /*
7000 * Transfer the ccb_bioq list to a temporary list so we can operate
7001 * on it without needing to lock/unlock on every loop. The concat
7002 * function with re-init the real list for us.
7003 */
7004 s = splcam();
7005 mtx_lock(&cam_bioq_lock);
7006 TAILQ_INIT(&queue);
7007 TAILQ_CONCAT(&queue, oqueue, sim_links.tqe);
7008 mtx_unlock(&cam_bioq_lock);
7009
7010 while ((ccb_h = TAILQ_FIRST(&queue)) != NULL) {
7011 int runq;
7012
7013 TAILQ_REMOVE(&queue, ccb_h, sim_links.tqe);
7014 ccb_h->pinfo.index = CAM_UNQUEUED_INDEX;
7015 splx(s);
7016
7017 CAM_DEBUG(ccb_h->path, CAM_DEBUG_TRACE,
7018 ("camisr\n"));
7019
7020 runq = FALSE;
7021
7022 if (ccb_h->flags & CAM_HIGH_POWER) {
7023 struct highpowerlist *hphead;
7024 union ccb *send_ccb;
7025
7026 hphead = &highpowerq;
7027
7028 send_ccb = (union ccb *)STAILQ_FIRST(hphead);
7029
7030 /*
7031 * Increment the count since this command is done.
7032 */
7033 num_highpower++;
7034
7035 /*
7036 * Any high powered commands queued up?
7037 */
7038 if (send_ccb != NULL) {
7039
7040 STAILQ_REMOVE_HEAD(hphead, xpt_links.stqe);
7041
7042 xpt_release_devq(send_ccb->ccb_h.path,
7043 /*count*/1, /*runqueue*/TRUE);
7044 }
7045 }
7046 if ((ccb_h->func_code & XPT_FC_USER_CCB) == 0) {
7047 struct cam_ed *dev;
7048
7049 dev = ccb_h->path->device;
7050
7051 s = splcam();
7052 cam_ccbq_ccb_done(&dev->ccbq, (union ccb *)ccb_h);
7053
7054 ccb_h->path->bus->sim->devq->send_active--;
7055 ccb_h->path->bus->sim->devq->send_openings++;
7056 splx(s);
7057
7058 if (((dev->flags & CAM_DEV_REL_ON_COMPLETE) != 0
7059 && (ccb_h->status&CAM_STATUS_MASK) != CAM_REQUEUE_REQ)
7060 || ((dev->flags & CAM_DEV_REL_ON_QUEUE_EMPTY) != 0
7061 && (dev->ccbq.dev_active == 0))) {
7062
7063 xpt_release_devq(ccb_h->path, /*count*/1,
7064 /*run_queue*/TRUE);
7065 }
7066
7067 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
7068 && (--dev->tag_delay_count == 0))
7069 xpt_start_tags(ccb_h->path);
7070
7071 if ((dev->ccbq.queue.entries > 0)
7072 && (dev->qfrozen_cnt == 0)
7073 && (device_is_send_queued(dev) == 0)) {
7074 runq = xpt_schedule_dev_sendq(ccb_h->path->bus,
7075 dev);
7076 }
7077 }
7078
7079 if (ccb_h->status & CAM_RELEASE_SIMQ) {
7080 xpt_release_simq(ccb_h->path->bus->sim,
7081 /*run_queue*/TRUE);
7082 ccb_h->status &= ~CAM_RELEASE_SIMQ;
7083 runq = FALSE;
7084 }
7085
7086 if ((ccb_h->flags & CAM_DEV_QFRZDIS)
7087 && (ccb_h->status & CAM_DEV_QFRZN)) {
7088 xpt_release_devq(ccb_h->path, /*count*/1,
7089 /*run_queue*/TRUE);
7090 ccb_h->status &= ~CAM_DEV_QFRZN;
7091 } else if (runq) {
7092 xpt_run_dev_sendq(ccb_h->path->bus);
7093 }
7094
7095 /* Call the peripheral driver's callback */
7096 (*ccb_h->cbfcnp)(ccb_h->path->periph, (union ccb *)ccb_h);
7097
7098 /* Raise IPL for while test */
7099 s = splcam();
7100 }
7101 splx(s);
7102}