1 /*-
2 * Implementation of the SCSI Transport
3 *
4 * SPDX-License-Identifier: BSD-2-Clause
5 *
6 * Copyright (c) 1997, 1998, 1999 Justin T. Gibbs.
7 * Copyright (c) 1997, 1998, 1999 Kenneth D. Merry.
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions, and the following disclaimer,
15 * without modification, immediately at the beginning of the file.
16 * 2. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
23 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/systm.h>
35 #include <sys/types.h>
36 #include <sys/malloc.h>
37 #include <sys/kernel.h>
38 #include <sys/time.h>
39 #include <sys/conf.h>
40 #include <sys/fcntl.h>
41 #include <sys/md5.h>
42 #include <sys/sbuf.h>
43
44 #include <sys/lock.h>
45 #include <sys/mutex.h>
46 #include <sys/sysctl.h>
47
48 #include <cam/cam.h>
49 #include <cam/cam_ccb.h>
50 #include <cam/cam_queue.h>
51 #include <cam/cam_periph.h>
52 #include <cam/cam_sim.h>
53 #include <cam/cam_xpt.h>
54 #include <cam/cam_xpt_sim.h>
55 #include <cam/cam_xpt_periph.h>
56 #include <cam/cam_xpt_internal.h>
57 #include <cam/cam_debug.h>
58
59 #include <cam/scsi/scsi_all.h>
60 #include <cam/scsi/scsi_message.h>
61 #include <cam/scsi/scsi_pass.h>
62 #include <machine/stdarg.h> /* for xpt_print below */
63
64 struct scsi_quirk_entry {
65 struct scsi_inquiry_pattern inq_pat;
66 uint8_t quirks;
67 #define CAM_QUIRK_NOLUNS 0x01
68 #define CAM_QUIRK_NOVPDS 0x02
69 #define CAM_QUIRK_HILUNS 0x04
70 #define CAM_QUIRK_NOHILUNS 0x08
71 #define CAM_QUIRK_NORPTLUNS 0x10
72 u_int mintags;
73 u_int maxtags;
74 };
75 #define SCSI_QUIRK(dev) ((struct scsi_quirk_entry *)((dev)->quirk))
76
77 static int cam_srch_hi = 0;
78 SYSCTL_INT(_kern_cam, OID_AUTO, cam_srch_hi, CTLFLAG_RWTUN,
79 &cam_srch_hi, 0, "Search above LUN 7 for SCSI3 and greater devices");
80
81 #define CAM_SCSI2_MAXLUN 8
82 #define CAM_CAN_GET_SIMPLE_LUN(x, i) \
83 ((((x)->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) == \
84 RPL_LUNDATA_ATYP_PERIPH) || \
85 (((x)->luns[i].lundata[0] & RPL_LUNDATA_ATYP_MASK) == \
86 RPL_LUNDATA_ATYP_FLAT))
87 #define CAM_GET_SIMPLE_LUN(lp, i, lval) \
88 if (((lp)->luns[(i)].lundata[0] & RPL_LUNDATA_ATYP_MASK) == \
89 RPL_LUNDATA_ATYP_PERIPH) { \
90 (lval) = (lp)->luns[(i)].lundata[1]; \
91 } else { \
92 (lval) = (lp)->luns[(i)].lundata[0]; \
93 (lval) &= RPL_LUNDATA_FLAT_LUN_MASK; \
94 (lval) <<= 8; \
95 (lval) |= (lp)->luns[(i)].lundata[1]; \
96 }
97 #define CAM_GET_LUN(lp, i, lval) \
98 (lval) = scsi_8btou64((lp)->luns[(i)].lundata); \
99 (lval) = CAM_EXTLUN_BYTE_SWIZZLE(lval);
100
101 /*
102 * If we're not quirked to search <= the first 8 luns
103 * and we are either quirked to search above lun 8,
104 * or we're > SCSI-2 and we've enabled hilun searching,
105 * or we're > SCSI-2 and the last lun was a success,
106 * we can look for luns above lun 8.
107 */
108 #define CAN_SRCH_HI_SPARSE(dv) \
109 (((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_NOHILUNS) == 0) \
110 && ((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_HILUNS) \
111 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2 && cam_srch_hi)))
112
113 #define CAN_SRCH_HI_DENSE(dv) \
114 (((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_NOHILUNS) == 0) \
115 && ((SCSI_QUIRK(dv)->quirks & CAM_QUIRK_HILUNS) \
116 || (SID_ANSI_REV(&dv->inq_data) > SCSI_REV_2)))
117
118 static periph_init_t probe_periph_init;
119
120 static struct periph_driver probe_driver =
121 {
122 probe_periph_init, "probe",
123 TAILQ_HEAD_INITIALIZER(probe_driver.units), /* generation */ 0,
124 CAM_PERIPH_DRV_EARLY
125 };
126
127 PERIPHDRIVER_DECLARE(probe, probe_driver);
128
129 typedef enum {
130 PROBE_TUR,
131 PROBE_INQUIRY, /* this counts as DV0 for Basic Domain Validation */
132 PROBE_FULL_INQUIRY,
133 PROBE_REPORT_LUNS,
134 PROBE_MODE_SENSE,
135 PROBE_SUPPORTED_VPD_LIST,
136 PROBE_DEVICE_ID,
137 PROBE_EXTENDED_INQUIRY,
138 PROBE_SERIAL_NUM,
139 PROBE_TUR_FOR_NEGOTIATION,
140 PROBE_INQUIRY_BASIC_DV1,
141 PROBE_INQUIRY_BASIC_DV2,
142 PROBE_DV_EXIT,
143 PROBE_DONE,
144 PROBE_INVALID
145 } probe_action;
146
147 static char *probe_action_text[] = {
148 "PROBE_TUR",
149 "PROBE_INQUIRY",
150 "PROBE_FULL_INQUIRY",
151 "PROBE_REPORT_LUNS",
152 "PROBE_MODE_SENSE",
153 "PROBE_SUPPORTED_VPD_LIST",
154 "PROBE_DEVICE_ID",
155 "PROBE_EXTENDED_INQUIRY",
156 "PROBE_SERIAL_NUM",
157 "PROBE_TUR_FOR_NEGOTIATION",
158 "PROBE_INQUIRY_BASIC_DV1",
159 "PROBE_INQUIRY_BASIC_DV2",
160 "PROBE_DV_EXIT",
161 "PROBE_DONE",
162 "PROBE_INVALID"
163 };
164
165 #define PROBE_SET_ACTION(softc, newaction) \
166 do { \
167 char **text; \
168 text = probe_action_text; \
169 CAM_DEBUG((softc)->periph->path, CAM_DEBUG_PROBE, \
170 ("Probe %s to %s\n", text[(softc)->action], \
171 text[(newaction)])); \
172 (softc)->action = (newaction); \
173 } while(0)
174
175 typedef enum {
176 PROBE_INQUIRY_CKSUM = 0x01,
177 PROBE_NO_ANNOUNCE = 0x04,
178 PROBE_EXTLUN = 0x08
179 } probe_flags;
180
181 typedef struct {
182 TAILQ_HEAD(, ccb_hdr) request_ccbs;
183 probe_action action;
184 probe_flags flags;
185 MD5_CTX context;
186 uint8_t digest[16];
187 struct cam_periph *periph;
188 } probe_softc;
189
190 static const char quantum[] = "QUANTUM";
191 static const char sony[] = "SONY";
192 static const char west_digital[] = "WDIGTL";
193 static const char samsung[] = "SAMSUNG";
194 static const char seagate[] = "SEAGATE";
195 static const char microp[] = "MICROP";
196
197 static struct scsi_quirk_entry scsi_quirk_table[] =
198 {
199 {
200 /* Reports QUEUE FULL for temporary resource shortages */
201 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP39100*", "*" },
202 /*quirks*/0, /*mintags*/24, /*maxtags*/32
203 },
204 {
205 /* Reports QUEUE FULL for temporary resource shortages */
206 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP34550*", "*" },
207 /*quirks*/0, /*mintags*/24, /*maxtags*/32
208 },
209 {
210 /* Reports QUEUE FULL for temporary resource shortages */
211 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "XP32275*", "*" },
212 /*quirks*/0, /*mintags*/24, /*maxtags*/32
213 },
214 {
215 /* Broken tagged queuing drive */
216 { T_DIRECT, SIP_MEDIA_FIXED, microp, "4421-07*", "*" },
217 /*quirks*/0, /*mintags*/0, /*maxtags*/0
218 },
219 {
220 /* Broken tagged queuing drive */
221 { T_DIRECT, SIP_MEDIA_FIXED, "HP", "C372*", "*" },
222 /*quirks*/0, /*mintags*/0, /*maxtags*/0
223 },
224 {
225 /* Broken tagged queuing drive */
226 { T_DIRECT, SIP_MEDIA_FIXED, microp, "3391*", "x43h" },
227 /*quirks*/0, /*mintags*/0, /*maxtags*/0
228 },
229 {
230 /*
231 * Unfortunately, the Quantum Atlas III has the same
232 * problem as the Atlas II drives above.
233 * Reported by: "Johan Granlund" <johan@granlund.nu>
234 *
235 * For future reference, the drive with the problem was:
236 * QUANTUM QM39100TD-SW N1B0
237 *
238 * It's possible that Quantum will fix the problem in later
239 * firmware revisions. If that happens, the quirk entry
240 * will need to be made specific to the firmware revisions
241 * with the problem.
242 *
243 */
244 /* Reports QUEUE FULL for temporary resource shortages */
245 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM39100*", "*" },
246 /*quirks*/0, /*mintags*/24, /*maxtags*/32
247 },
248 {
249 /*
250 * 18 Gig Atlas III, same problem as the 9G version.
251 * Reported by: Andre Albsmeier
252 * <andre.albsmeier@mchp.siemens.de>
253 *
254 * For future reference, the drive with the problem was:
255 * QUANTUM QM318000TD-S N491
256 */
257 /* Reports QUEUE FULL for temporary resource shortages */
258 { T_DIRECT, SIP_MEDIA_FIXED, quantum, "QM318000*", "*" },
259 /*quirks*/0, /*mintags*/24, /*maxtags*/32
260 },
261 {
262 /*
263 * Broken tagged queuing drive
264 * Reported by: Bret Ford <bford@uop.cs.uop.edu>
265 * and: Martin Renters <martin@tdc.on.ca>
266 */
267 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST410800*", "71*" },
268 /*quirks*/0, /*mintags*/0, /*maxtags*/0
269 },
270 /*
271 * The Seagate Medalist Pro drives have very poor write
272 * performance with anything more than 2 tags.
273 *
274 * Reported by: Paul van der Zwan <paulz@trantor.xs4all.nl>
275 * Drive: <SEAGATE ST36530N 1444>
276 *
277 * Reported by: Jeremy Lea <reg@shale.csir.co.za>
278 * Drive: <SEAGATE ST34520W 1281>
279 *
280 * No one has actually reported that the 9G version
281 * (ST39140*) of the Medalist Pro has the same problem, but
282 * we're assuming that it does because the 4G and 6.5G
283 * versions of the drive are broken.
284 */
285 {
286 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST34520*", "*"},
287 /*quirks*/0, /*mintags*/2, /*maxtags*/2
288 },
289 {
290 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST36530*", "*"},
291 /*quirks*/0, /*mintags*/2, /*maxtags*/2
292 },
293 {
294 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST39140*", "*"},
295 /*quirks*/0, /*mintags*/2, /*maxtags*/2
296 },
297 {
298 /*
299 * Experiences command timeouts under load with a
300 * tag count higher than 55.
301 */
302 { T_DIRECT, SIP_MEDIA_FIXED, seagate, "ST3146855LW", "*"},
303 /*quirks*/0, /*mintags*/2, /*maxtags*/55
304 },
305 {
306 /*
307 * Slow when tagged queueing is enabled. Write performance
308 * steadily drops off with more and more concurrent
309 * transactions. Best sequential write performance with
310 * tagged queueing turned off and write caching turned on.
311 *
312 * PR: kern/10398
313 * Submitted by: Hideaki Okada <hokada@isl.melco.co.jp>
314 * Drive: DCAS-34330 w/ "S65A" firmware.
315 *
316 * The drive with the problem had the "S65A" firmware
317 * revision, and has also been reported (by Stephen J.
318 * Roznowski <sjr@home.net>) for a drive with the "S61A"
319 * firmware revision.
320 *
321 * Although no one has reported problems with the 2 gig
322 * version of the DCAS drive, the assumption is that it
323 * has the same problems as the 4 gig version. Therefore
324 * this quirk entries disables tagged queueing for all
325 * DCAS drives.
326 */
327 { T_DIRECT, SIP_MEDIA_FIXED, "IBM", "DCAS*", "*" },
328 /*quirks*/0, /*mintags*/0, /*maxtags*/0
329 },
330 {
331 /* Broken tagged queuing drive */
332 { T_DIRECT, SIP_MEDIA_REMOVABLE, "iomega", "jaz*", "*" },
333 /*quirks*/0, /*mintags*/0, /*maxtags*/0
334 },
335 {
336 /* Broken tagged queuing drive */
337 { T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CFP2107*", "*" },
338 /*quirks*/0, /*mintags*/0, /*maxtags*/0
339 },
340 {
341 /* This does not support other than LUN 0 */
342 { T_DIRECT, SIP_MEDIA_FIXED, "VMware*", "*", "*" },
343 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
344 },
345 {
346 /*
347 * Broken tagged queuing drive.
348 * Submitted by:
349 * NAKAJI Hiroyuki <nakaji@zeisei.dpri.kyoto-u.ac.jp>
350 * in PR kern/9535
351 */
352 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN34324U*", "*" },
353 /*quirks*/0, /*mintags*/0, /*maxtags*/0
354 },
355 {
356 /*
357 * Slow when tagged queueing is enabled. (1.5MB/sec versus
358 * 8MB/sec.)
359 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
360 * Best performance with these drives is achieved with
361 * tagged queueing turned off, and write caching turned on.
362 */
363 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "WDE*", "*" },
364 /*quirks*/0, /*mintags*/0, /*maxtags*/0
365 },
366 {
367 /*
368 * Slow when tagged queueing is enabled. (1.5MB/sec versus
369 * 8MB/sec.)
370 * Submitted by: Andrew Gallatin <gallatin@cs.duke.edu>
371 * Best performance with these drives is achieved with
372 * tagged queueing turned off, and write caching turned on.
373 */
374 { T_DIRECT, SIP_MEDIA_FIXED, west_digital, "ENTERPRISE", "*" },
375 /*quirks*/0, /*mintags*/0, /*maxtags*/0
376 },
377 {
378 /*
379 * Doesn't handle queue full condition correctly,
380 * so we need to limit maxtags to what the device
381 * can handle instead of determining this automatically.
382 */
383 { T_DIRECT, SIP_MEDIA_FIXED, samsung, "WN321010S*", "*" },
384 /*quirks*/0, /*mintags*/2, /*maxtags*/32
385 },
386 {
387 /* Really only one LUN */
388 { T_ENCLOSURE, SIP_MEDIA_FIXED, "SUN", "SENA", "*" },
389 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
390 },
391 {
392 /* I can't believe we need a quirk for DPT volumes. */
393 { T_ANY, SIP_MEDIA_FIXED|SIP_MEDIA_REMOVABLE, "DPT", "*", "*" },
394 CAM_QUIRK_NOLUNS,
395 /*mintags*/0, /*maxtags*/255
396 },
397 {
398 /*
399 * Many Sony CDROM drives don't like multi-LUN probing.
400 */
401 { T_CDROM, SIP_MEDIA_REMOVABLE, sony, "CD-ROM CDU*", "*" },
402 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
403 },
404 {
405 /*
406 * This drive doesn't like multiple LUN probing.
407 * Submitted by: Parag Patel <parag@cgt.com>
408 */
409 { T_WORM, SIP_MEDIA_REMOVABLE, sony, "CD-R CDU9*", "*" },
410 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
411 },
412 {
413 { T_WORM, SIP_MEDIA_REMOVABLE, "YAMAHA", "CDR100*", "*" },
414 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
415 },
416 {
417 /*
418 * The 8200 doesn't like multi-lun probing, and probably
419 * don't like serial number requests either.
420 */
421 {
422 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
423 "EXB-8200*", "*"
424 },
425 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
426 },
427 {
428 /*
429 * Let's try the same as above, but for a drive that says
430 * it's an IPL-6860 but is actually an EXB 8200.
431 */
432 {
433 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "EXABYTE",
434 "IPL-6860*", "*"
435 },
436 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
437 },
438 {
439 /*
440 * These Hitachi drives don't like multi-lun probing.
441 * The PR submitter has a DK319H, but says that the Linux
442 * kernel has a similar work-around for the DK312 and DK314,
443 * so all DK31* drives are quirked here.
444 * PR: misc/18793
445 * Submitted by: Paul Haddad <paul@pth.com>
446 */
447 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK31*", "*" },
448 CAM_QUIRK_NOLUNS, /*mintags*/2, /*maxtags*/255
449 },
450 {
451 /*
452 * The Hitachi CJ series with J8A8 firmware apparently has
453 * problems with tagged commands.
454 * PR: 23536
455 * Reported by: amagai@nue.org
456 */
457 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "DK32CJ*", "J8A8" },
458 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
459 },
460 {
461 /*
462 * These are the large storage arrays.
463 * Submitted by: William Carrel <william.carrel@infospace.com>
464 */
465 { T_DIRECT, SIP_MEDIA_FIXED, "HITACHI", "OPEN*", "*" },
466 CAM_QUIRK_HILUNS, 2, 1024
467 },
468 {
469 /*
470 * This old revision of the TDC3600 is also SCSI-1, and
471 * hangs upon serial number probing.
472 */
473 {
474 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "TANDBERG",
475 " TDC 3600", "U07:"
476 },
477 CAM_QUIRK_NOVPDS, /*mintags*/0, /*maxtags*/0
478 },
479 {
480 /*
481 * Would repond to all LUNs if asked for.
482 */
483 {
484 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "CALIPER",
485 "CP150", "*"
486 },
487 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
488 },
489 {
490 /*
491 * Would repond to all LUNs if asked for.
492 */
493 {
494 T_SEQUENTIAL, SIP_MEDIA_REMOVABLE, "KENNEDY",
495 "96X2*", "*"
496 },
497 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
498 },
499 {
500 /* Submitted by: Matthew Dodd <winter@jurai.net> */
501 { T_PROCESSOR, SIP_MEDIA_FIXED, "Cabletrn", "EA41*", "*" },
502 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
503 },
504 {
505 /* Submitted by: Matthew Dodd <winter@jurai.net> */
506 { T_PROCESSOR, SIP_MEDIA_FIXED, "CABLETRN", "EA41*", "*" },
507 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
508 },
509 {
510 /* TeraSolutions special settings for TRC-22 RAID */
511 { T_DIRECT, SIP_MEDIA_FIXED, "TERASOLU", "TRC-22", "*" },
512 /*quirks*/0, /*mintags*/55, /*maxtags*/255
513 },
514 {
515 /* Veritas Storage Appliance */
516 { T_DIRECT, SIP_MEDIA_FIXED, "VERITAS", "*", "*" },
517 CAM_QUIRK_HILUNS, /*mintags*/2, /*maxtags*/1024
518 },
519 {
520 /*
521 * Would respond to all LUNs. Device type and removable
522 * flag are jumper-selectable.
523 */
524 { T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED, "MaxOptix",
525 "Tahiti 1", "*"
526 },
527 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
528 },
529 {
530 /* EasyRAID E5A aka. areca ARC-6010 */
531 { T_DIRECT, SIP_MEDIA_FIXED, "easyRAID", "*", "*" },
532 CAM_QUIRK_NOHILUNS, /*mintags*/2, /*maxtags*/255
533 },
534 {
535 { T_ENCLOSURE, SIP_MEDIA_FIXED, "DP", "BACKPLANE", "*" },
536 CAM_QUIRK_NOLUNS, /*mintags*/0, /*maxtags*/0
537 },
538 {
539 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Garmin", "*", "*" },
540 CAM_QUIRK_NORPTLUNS, /*mintags*/2, /*maxtags*/255
541 },
542 {
543 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic", "STORAGE DEVICE*", "120?" },
544 CAM_QUIRK_NORPTLUNS, /*mintags*/2, /*maxtags*/255
545 },
546 {
547 { T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic", "MassStorageClass", "1533" },
548 CAM_QUIRK_NORPTLUNS, /*mintags*/2, /*maxtags*/255
549 },
550 {
551 /* Default tagged queuing parameters for all devices */
552 {
553 T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
554 /*vendor*/"*", /*product*/"*", /*revision*/"*"
555 },
556 /*quirks*/0, /*mintags*/2, /*maxtags*/255
557 },
558 };
559
560 static cam_status proberegister(struct cam_periph *periph,
561 void *arg);
562 static void probeschedule(struct cam_periph *probe_periph);
563 static void probestart(struct cam_periph *periph, union ccb *start_ccb);
564 static void proberequestdefaultnegotiation(struct cam_periph *periph);
565 static int proberequestbackoff(struct cam_periph *periph,
566 struct cam_ed *device);
567 static void probedone(struct cam_periph *periph, union ccb *done_ccb);
568 static void probe_purge_old(struct cam_path *path,
569 struct scsi_report_luns_data *new,
570 probe_flags flags);
571 static void probecleanup(struct cam_periph *periph);
572 static void scsi_find_quirk(struct cam_ed *device);
573 static void scsi_scan_bus(struct cam_periph *periph, union ccb *ccb);
574 static void scsi_scan_lun(struct cam_periph *periph,
575 struct cam_path *path, cam_flags flags,
576 union ccb *ccb);
577 static void xptscandone(struct cam_periph *periph, union ccb *done_ccb);
578 static struct cam_ed *
579 scsi_alloc_device(struct cam_eb *bus, struct cam_et *target,
580 lun_id_t lun_id);
581 static void scsi_devise_transport(struct cam_path *path);
582 static void scsi_set_transfer_settings(struct ccb_trans_settings *cts,
583 struct cam_path *path,
584 int async_update);
585 static void scsi_toggle_tags(struct cam_path *path);
586 static void scsi_dev_async(uint32_t async_code,
587 struct cam_eb *bus,
588 struct cam_et *target,
589 struct cam_ed *device,
590 void *async_arg);
591 static void scsi_action(union ccb *start_ccb);
592 static void scsi_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb);
593 static void scsi_proto_announce_sbuf(struct cam_ed *device,
594 struct sbuf *sb);
595 static void scsi_proto_denounce_sbuf(struct cam_ed *device,
596 struct sbuf *sb);
597 static void scsi_proto_debug_out(union ccb *ccb);
598 static void _scsi_announce_periph(struct cam_periph *, u_int *, u_int *, struct ccb_trans_settings *);
599
600 static struct xpt_xport_ops scsi_xport_ops = {
601 .alloc_device = scsi_alloc_device,
602 .action = scsi_action,
603 .async = scsi_dev_async,
604 .announce_sbuf = scsi_announce_periph_sbuf,
605 };
606 #define SCSI_XPT_XPORT(x, X) \
607 static struct xpt_xport scsi_xport_ ## x = { \
608 .xport = XPORT_ ## X, \
609 .name = #x, \
610 .ops = &scsi_xport_ops, \
611 }; \
612 CAM_XPT_XPORT(scsi_xport_ ## x);
613
614 SCSI_XPT_XPORT(spi, SPI);
615 SCSI_XPT_XPORT(sas, SAS);
616 SCSI_XPT_XPORT(fc, FC);
617 SCSI_XPT_XPORT(usb, USB);
618 SCSI_XPT_XPORT(iscsi, ISCSI);
619 SCSI_XPT_XPORT(srp, SRP);
620 SCSI_XPT_XPORT(ppb, PPB);
621
622 #undef SCSI_XPORT_XPORT
623
624 static struct xpt_proto_ops scsi_proto_ops = {
625 .announce_sbuf = scsi_proto_announce_sbuf,
626 .denounce_sbuf = scsi_proto_denounce_sbuf,
627 .debug_out = scsi_proto_debug_out,
628 };
629 static struct xpt_proto scsi_proto = {
630 .proto = PROTO_SCSI,
631 .name = "scsi",
632 .ops = &scsi_proto_ops,
633 };
634 CAM_XPT_PROTO(scsi_proto);
635
636 static void
probe_periph_init(void)637 probe_periph_init(void)
638 {
639 }
640
641 static cam_status
proberegister(struct cam_periph * periph,void * arg)642 proberegister(struct cam_periph *periph, void *arg)
643 {
644 union ccb *request_ccb; /* CCB representing the probe request */
645 probe_softc *softc;
646
647 request_ccb = (union ccb *)arg;
648 if (request_ccb == NULL) {
649 printf("proberegister: no probe CCB, can't register device\n");
650 return(CAM_REQ_CMP_ERR);
651 }
652
653 softc = (probe_softc *)malloc(sizeof(*softc), M_CAMXPT, M_NOWAIT);
654
655 if (softc == NULL) {
656 printf("proberegister: Unable to probe new device. Unable to allocate softc\n");
657 return(CAM_REQ_CMP_ERR);
658 }
659 TAILQ_INIT(&softc->request_ccbs);
660 TAILQ_INSERT_TAIL(&softc->request_ccbs, &request_ccb->ccb_h,
661 periph_links.tqe);
662 softc->flags = 0;
663 periph->softc = softc;
664 softc->periph = periph;
665 softc->action = PROBE_INVALID;
666 if (cam_periph_acquire(periph) != 0)
667 return (CAM_REQ_CMP_ERR);
668
669 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe started\n"));
670 scsi_devise_transport(periph->path);
671
672 /*
673 * Ensure we've waited at least a bus settle
674 * delay before attempting to probe the device.
675 * For HBAs that don't do bus resets, this won't make a difference.
676 */
677 cam_periph_freeze_after_event(periph, &periph->path->bus->last_reset,
678 scsi_delay);
679 probeschedule(periph);
680 return(CAM_REQ_CMP);
681 }
682
683 static void
probeschedule(struct cam_periph * periph)684 probeschedule(struct cam_periph *periph)
685 {
686 struct ccb_pathinq cpi;
687 union ccb *ccb;
688 probe_softc *softc;
689
690 softc = (probe_softc *)periph->softc;
691 ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
692
693 xpt_path_inq(&cpi, periph->path);
694
695 /*
696 * If a device has gone away and another device, or the same one,
697 * is back in the same place, it should have a unit attention
698 * condition pending. It will not report the unit attention in
699 * response to an inquiry, which may leave invalid transfer
700 * negotiations in effect. The TUR will reveal the unit attention
701 * condition. Only send the TUR for lun 0, since some devices
702 * will get confused by commands other than inquiry to non-existent
703 * luns. If you think a device has gone away start your scan from
704 * lun 0. This will insure that any bogus transfer settings are
705 * invalidated.
706 *
707 * If we haven't seen the device before and the controller supports
708 * some kind of transfer negotiation, negotiate with the first
709 * sent command if no bus reset was performed at startup. This
710 * ensures that the device is not confused by transfer negotiation
711 * settings left over by loader or BIOS action.
712 */
713 if (((ccb->ccb_h.path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
714 && (ccb->ccb_h.target_lun == 0)) {
715 PROBE_SET_ACTION(softc, PROBE_TUR);
716 } else if ((cpi.hba_inquiry & (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) != 0
717 && (cpi.hba_misc & PIM_NOBUSRESET) != 0) {
718 proberequestdefaultnegotiation(periph);
719 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
720 } else {
721 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
722 }
723
724 if (ccb->crcn.flags & CAM_EXPECT_INQ_CHANGE)
725 softc->flags |= PROBE_NO_ANNOUNCE;
726 else
727 softc->flags &= ~PROBE_NO_ANNOUNCE;
728
729 if (cpi.hba_misc & PIM_EXTLUNS)
730 softc->flags |= PROBE_EXTLUN;
731 else
732 softc->flags &= ~PROBE_EXTLUN;
733
734 xpt_schedule(periph, CAM_PRIORITY_XPT);
735 }
736
737 static void
probestart(struct cam_periph * periph,union ccb * start_ccb)738 probestart(struct cam_periph *periph, union ccb *start_ccb)
739 {
740 /* Probe the device that our peripheral driver points to */
741 struct ccb_scsiio *csio;
742 probe_softc *softc;
743
744 CAM_DEBUG(start_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probestart\n"));
745
746 softc = (probe_softc *)periph->softc;
747 csio = &start_ccb->csio;
748 again:
749
750 switch (softc->action) {
751 case PROBE_TUR:
752 case PROBE_TUR_FOR_NEGOTIATION:
753 case PROBE_DV_EXIT:
754 {
755 scsi_test_unit_ready(csio,
756 /*retries*/4,
757 probedone,
758 MSG_SIMPLE_Q_TAG,
759 SSD_FULL_SIZE,
760 /*timeout*/60000);
761 break;
762 }
763 case PROBE_INQUIRY:
764 case PROBE_FULL_INQUIRY:
765 {
766 u_int inquiry_len;
767 struct scsi_inquiry_data *inq_buf;
768
769 inq_buf = &periph->path->device->inq_data;
770
771 /*
772 * If the device is currently configured, we calculate an
773 * MD5 checksum of the inquiry data, and if the serial number
774 * length is greater than 0, add the serial number data
775 * into the checksum as well. Once the inquiry and the
776 * serial number check finish, we attempt to figure out
777 * whether we still have the same device.
778 */
779 if (periph->path->device->flags & CAM_DEV_UNCONFIGURED) {
780 softc->flags &= ~PROBE_INQUIRY_CKSUM;
781 } else if ((softc->flags & PROBE_INQUIRY_CKSUM) == 0) {
782 MD5Init(&softc->context);
783 MD5Update(&softc->context, (unsigned char *)inq_buf,
784 sizeof(struct scsi_inquiry_data));
785 if (periph->path->device->serial_num_len > 0) {
786 MD5Update(&softc->context,
787 periph->path->device->serial_num,
788 periph->path->device->serial_num_len);
789 }
790 MD5Final(softc->digest, &softc->context);
791 softc->flags |= PROBE_INQUIRY_CKSUM;
792 }
793
794 if (softc->action == PROBE_INQUIRY)
795 inquiry_len = SHORT_INQUIRY_LENGTH;
796 else
797 inquiry_len = SID_ADDITIONAL_LENGTH(inq_buf);
798
799 /*
800 * Some parallel SCSI devices fail to send an
801 * ignore wide residue message when dealing with
802 * odd length inquiry requests. Round up to be
803 * safe.
804 */
805 inquiry_len = roundup2(inquiry_len, 2);
806
807 scsi_inquiry(csio,
808 /*retries*/4,
809 probedone,
810 MSG_SIMPLE_Q_TAG,
811 (uint8_t *)inq_buf,
812 inquiry_len,
813 /*evpd*/FALSE,
814 /*page_code*/0,
815 SSD_MIN_SIZE,
816 /*timeout*/60 * 1000);
817 break;
818 }
819 case PROBE_REPORT_LUNS:
820 {
821 void *rp;
822
823 rp = malloc(periph->path->target->rpl_size,
824 M_CAMXPT, M_NOWAIT | M_ZERO);
825 if (rp == NULL) {
826 struct scsi_inquiry_data *inq_buf;
827 inq_buf = &periph->path->device->inq_data;
828 xpt_print(periph->path,
829 "Unable to alloc report luns storage\n");
830 if (INQ_DATA_TQ_ENABLED(inq_buf))
831 PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
832 else
833 PROBE_SET_ACTION(softc,
834 PROBE_SUPPORTED_VPD_LIST);
835 goto again;
836 }
837 scsi_report_luns(csio, 5, probedone, MSG_SIMPLE_Q_TAG,
838 RPL_REPORT_DEFAULT, rp, periph->path->target->rpl_size,
839 SSD_FULL_SIZE, 60000);
840 break;
841 }
842 case PROBE_MODE_SENSE:
843 {
844 void *mode_buf;
845 int mode_buf_len;
846
847 mode_buf_len = sizeof(struct scsi_mode_header_6)
848 + sizeof(struct scsi_mode_blk_desc)
849 + sizeof(struct scsi_control_page);
850 mode_buf = malloc(mode_buf_len, M_CAMXPT, M_NOWAIT);
851 if (mode_buf != NULL) {
852 scsi_mode_sense(csio,
853 /*retries*/4,
854 probedone,
855 MSG_SIMPLE_Q_TAG,
856 /*dbd*/FALSE,
857 SMS_PAGE_CTRL_CURRENT,
858 SMS_CONTROL_MODE_PAGE,
859 mode_buf,
860 mode_buf_len,
861 SSD_FULL_SIZE,
862 /*timeout*/60000);
863 break;
864 }
865 xpt_print(periph->path,
866 "Unable to mode sense control page - malloc failure\n");
867 PROBE_SET_ACTION(softc, PROBE_SUPPORTED_VPD_LIST);
868 }
869 /* FALLTHROUGH */
870 case PROBE_SUPPORTED_VPD_LIST:
871 {
872 struct scsi_vpd_supported_page_list *vpd_list;
873 struct cam_ed *device;
874
875 vpd_list = NULL;
876 device = periph->path->device;
877
878 if ((SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOVPDS) == 0)
879 vpd_list = malloc(sizeof(*vpd_list), M_CAMXPT,
880 M_NOWAIT | M_ZERO);
881
882 if (vpd_list != NULL) {
883 scsi_inquiry(csio,
884 /*retries*/4,
885 probedone,
886 MSG_SIMPLE_Q_TAG,
887 (uint8_t *)vpd_list,
888 sizeof(*vpd_list),
889 /*evpd*/TRUE,
890 SVPD_SUPPORTED_PAGE_LIST,
891 SSD_MIN_SIZE,
892 /*timeout*/60 * 1000);
893 break;
894 }
895 done:
896 /*
897 * We'll have to do without, let our probedone
898 * routine finish up for us.
899 */
900 start_ccb->csio.data_ptr = NULL;
901 cam_freeze_devq(periph->path);
902 cam_periph_doacquire(periph);
903 probedone(periph, start_ccb);
904 return;
905 }
906 case PROBE_DEVICE_ID:
907 {
908 struct scsi_vpd_device_id *devid;
909
910 devid = NULL;
911 if (scsi_vpd_supported_page(periph, SVPD_DEVICE_ID))
912 devid = malloc(SVPD_DEVICE_ID_MAX_SIZE, M_CAMXPT,
913 M_NOWAIT | M_ZERO);
914
915 if (devid != NULL) {
916 scsi_inquiry(csio,
917 /*retries*/4,
918 probedone,
919 MSG_SIMPLE_Q_TAG,
920 (uint8_t *)devid,
921 SVPD_DEVICE_ID_MAX_SIZE,
922 /*evpd*/TRUE,
923 SVPD_DEVICE_ID,
924 SSD_MIN_SIZE,
925 /*timeout*/60 * 1000);
926 break;
927 }
928 goto done;
929 }
930 case PROBE_EXTENDED_INQUIRY:
931 {
932 struct scsi_vpd_extended_inquiry_data *ext_inq;
933
934 ext_inq = NULL;
935 if (scsi_vpd_supported_page(periph, SVPD_EXTENDED_INQUIRY_DATA))
936 ext_inq = malloc(sizeof(*ext_inq), M_CAMXPT,
937 M_NOWAIT | M_ZERO);
938
939 if (ext_inq != NULL) {
940 scsi_inquiry(csio,
941 /*retries*/4,
942 probedone,
943 MSG_SIMPLE_Q_TAG,
944 (uint8_t *)ext_inq,
945 sizeof(*ext_inq),
946 /*evpd*/TRUE,
947 SVPD_EXTENDED_INQUIRY_DATA,
948 SSD_MIN_SIZE,
949 /*timeout*/60 * 1000);
950 break;
951 }
952 /*
953 * We'll have to do without, let our probedone
954 * routine finish up for us.
955 */
956 goto done;
957 }
958 case PROBE_SERIAL_NUM:
959 {
960 struct scsi_vpd_unit_serial_number *serial_buf;
961 struct cam_ed* device;
962
963 serial_buf = NULL;
964 device = periph->path->device;
965 if (device->serial_num != NULL) {
966 free(device->serial_num, M_CAMXPT);
967 device->serial_num = NULL;
968 device->serial_num_len = 0;
969 }
970
971 if (scsi_vpd_supported_page(periph, SVPD_UNIT_SERIAL_NUMBER))
972 serial_buf = (struct scsi_vpd_unit_serial_number *)
973 malloc(sizeof(*serial_buf), M_CAMXPT,
974 M_NOWAIT|M_ZERO);
975
976 if (serial_buf != NULL) {
977 scsi_inquiry(csio,
978 /*retries*/4,
979 probedone,
980 MSG_SIMPLE_Q_TAG,
981 (uint8_t *)serial_buf,
982 sizeof(*serial_buf),
983 /*evpd*/TRUE,
984 SVPD_UNIT_SERIAL_NUMBER,
985 SSD_MIN_SIZE,
986 /*timeout*/60 * 1000);
987 break;
988 }
989 goto done;
990 }
991 case PROBE_INQUIRY_BASIC_DV1:
992 case PROBE_INQUIRY_BASIC_DV2:
993 {
994 u_int inquiry_len;
995 struct scsi_inquiry_data *inq_buf;
996
997 inq_buf = &periph->path->device->inq_data;
998 inquiry_len = roundup2(SID_ADDITIONAL_LENGTH(inq_buf), 2);
999 inq_buf = malloc(inquiry_len, M_CAMXPT, M_NOWAIT);
1000 if (inq_buf == NULL) {
1001 xpt_print(periph->path,
1002 "malloc failure- skipping Basic Domain Validation\n");
1003 PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
1004 scsi_test_unit_ready(csio,
1005 /*retries*/4,
1006 probedone,
1007 MSG_SIMPLE_Q_TAG,
1008 SSD_FULL_SIZE,
1009 /*timeout*/60000);
1010 break;
1011 }
1012
1013 scsi_inquiry(csio,
1014 /*retries*/4,
1015 probedone,
1016 MSG_SIMPLE_Q_TAG,
1017 (uint8_t *)inq_buf,
1018 inquiry_len,
1019 /*evpd*/FALSE,
1020 /*page_code*/0,
1021 SSD_MIN_SIZE,
1022 /*timeout*/60 * 1000);
1023 break;
1024 }
1025 default:
1026 panic("probestart: invalid action state 0x%x\n", softc->action);
1027 }
1028 start_ccb->ccb_h.flags |= CAM_DEV_QFREEZE;
1029 cam_periph_doacquire(periph);
1030 xpt_action(start_ccb);
1031 }
1032
1033 static void
proberequestdefaultnegotiation(struct cam_periph * periph)1034 proberequestdefaultnegotiation(struct cam_periph *periph)
1035 {
1036 struct ccb_trans_settings cts;
1037
1038 memset(&cts, 0, sizeof(cts));
1039 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1040 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1041 cts.type = CTS_TYPE_USER_SETTINGS;
1042 xpt_action((union ccb *)&cts);
1043 if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1044 return;
1045 }
1046 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1047 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1048 xpt_action((union ccb *)&cts);
1049 }
1050
1051 /*
1052 * Backoff Negotiation Code- only pertinent for SPI devices.
1053 */
1054 static int
proberequestbackoff(struct cam_periph * periph,struct cam_ed * device)1055 proberequestbackoff(struct cam_periph *periph, struct cam_ed *device)
1056 {
1057 struct ccb_trans_settings cts;
1058 struct ccb_trans_settings_spi *spi;
1059
1060 memset(&cts, 0, sizeof (cts));
1061 xpt_setup_ccb(&cts.ccb_h, periph->path, CAM_PRIORITY_NONE);
1062 cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
1063 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1064 xpt_action((union ccb *)&cts);
1065 if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1066 if (bootverbose) {
1067 xpt_print(periph->path,
1068 "failed to get current device settings\n");
1069 }
1070 return (0);
1071 }
1072 if (cts.transport != XPORT_SPI) {
1073 if (bootverbose) {
1074 xpt_print(periph->path, "not SPI transport\n");
1075 }
1076 return (0);
1077 }
1078 spi = &cts.xport_specific.spi;
1079
1080 /*
1081 * We cannot renegotiate sync rate if we don't have one.
1082 */
1083 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0) {
1084 if (bootverbose) {
1085 xpt_print(periph->path, "no sync rate known\n");
1086 }
1087 return (0);
1088 }
1089
1090 /*
1091 * We'll assert that we don't have to touch PPR options- the
1092 * SIM will see what we do with period and offset and adjust
1093 * the PPR options as appropriate.
1094 */
1095
1096 /*
1097 * A sync rate with unknown or zero offset is nonsensical.
1098 * A sync period of zero means Async.
1099 */
1100 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0
1101 || spi->sync_offset == 0 || spi->sync_period == 0) {
1102 if (bootverbose) {
1103 xpt_print(periph->path, "no sync rate available\n");
1104 }
1105 return (0);
1106 }
1107
1108 if (device->flags & CAM_DEV_DV_HIT_BOTTOM) {
1109 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1110 ("hit async: giving up on DV\n"));
1111 return (0);
1112 }
1113
1114 /*
1115 * Jump sync_period up by one, but stop at 5MHz and fall back to Async.
1116 * We don't try to remember 'last' settings to see if the SIM actually
1117 * gets into the speed we want to set. We check on the SIM telling
1118 * us that a requested speed is bad, but otherwise don't try and
1119 * check the speed due to the asynchronous and handshake nature
1120 * of speed setting.
1121 */
1122 spi->valid = CTS_SPI_VALID_SYNC_RATE | CTS_SPI_VALID_SYNC_OFFSET;
1123 for (;;) {
1124 spi->sync_period++;
1125 if (spi->sync_period >= 0xf) {
1126 spi->sync_period = 0;
1127 spi->sync_offset = 0;
1128 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1129 ("setting to async for DV\n"));
1130 /*
1131 * Once we hit async, we don't want to try
1132 * any more settings.
1133 */
1134 device->flags |= CAM_DEV_DV_HIT_BOTTOM;
1135 } else if (bootverbose) {
1136 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1137 ("DV: period 0x%x\n", spi->sync_period));
1138 printf("setting period to 0x%x\n", spi->sync_period);
1139 }
1140 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
1141 cts.type = CTS_TYPE_CURRENT_SETTINGS;
1142 xpt_action((union ccb *)&cts);
1143 if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP) {
1144 break;
1145 }
1146 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1147 ("DV: failed to set period 0x%x\n", spi->sync_period));
1148 if (spi->sync_period == 0) {
1149 return (0);
1150 }
1151 }
1152 return (1);
1153 }
1154
1155 #define CCB_COMPLETED_OK(ccb) (((ccb).status & CAM_STATUS_MASK) == CAM_REQ_CMP)
1156
1157 static void
probedone(struct cam_periph * periph,union ccb * done_ccb)1158 probedone(struct cam_periph *periph, union ccb *done_ccb)
1159 {
1160 probe_softc *softc;
1161 struct cam_path *path;
1162 struct scsi_inquiry_data *inq_buf;
1163 uint32_t priority;
1164
1165 CAM_DEBUG(done_ccb->ccb_h.path, CAM_DEBUG_TRACE, ("probedone\n"));
1166
1167 softc = (probe_softc *)periph->softc;
1168 path = done_ccb->ccb_h.path;
1169 priority = done_ccb->ccb_h.pinfo.priority;
1170 cam_periph_assert(periph, MA_OWNED);
1171
1172 switch (softc->action) {
1173 case PROBE_TUR:
1174 {
1175 if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1176 if (cam_periph_error(done_ccb, 0, SF_NO_PRINT) ==
1177 ERESTART) {
1178 outr:
1179 /* Drop freeze taken due to CAM_DEV_QFREEZE */
1180 cam_release_devq(path, 0, 0, 0, FALSE);
1181 return;
1182 }
1183 else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1184 /* Don't wedge the queue */
1185 xpt_release_devq(done_ccb->ccb_h.path,
1186 /*count*/1,
1187 /*run_queue*/TRUE);
1188 }
1189 PROBE_SET_ACTION(softc, PROBE_INQUIRY);
1190 xpt_release_ccb(done_ccb);
1191 xpt_schedule(periph, priority);
1192 out:
1193 /* Drop freeze taken due to CAM_DEV_QFREEZE and release. */
1194 cam_release_devq(path, 0, 0, 0, FALSE);
1195 cam_periph_release_locked(periph);
1196 return;
1197 }
1198 case PROBE_INQUIRY:
1199 case PROBE_FULL_INQUIRY:
1200 {
1201 if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
1202 uint8_t periph_qual;
1203
1204 path->device->flags |= CAM_DEV_INQUIRY_DATA_VALID;
1205 scsi_find_quirk(path->device);
1206 inq_buf = &path->device->inq_data;
1207
1208 periph_qual = SID_QUAL(inq_buf);
1209
1210 if (periph_qual == SID_QUAL_LU_CONNECTED ||
1211 periph_qual == SID_QUAL_LU_OFFLINE) {
1212 /*
1213 * We conservatively request only
1214 * SHORT_INQUIRY_LEN bytes of inquiry
1215 * information during our first try
1216 * at sending an INQUIRY. If the device
1217 * has more information to give,
1218 * perform a second request specifying
1219 * the amount of information the device
1220 * is willing to give.
1221 */
1222 if (softc->action == PROBE_INQUIRY
1223 && SID_ADDITIONAL_LENGTH(inq_buf)
1224 > SHORT_INQUIRY_LENGTH) {
1225 PROBE_SET_ACTION(softc, PROBE_FULL_INQUIRY);
1226 xpt_release_ccb(done_ccb);
1227 xpt_schedule(periph, priority);
1228 goto out;
1229 }
1230
1231 scsi_devise_transport(path);
1232
1233 if (path->device->lun_id == 0 &&
1234 SID_ANSI_REV(inq_buf) > SCSI_REV_SPC2 &&
1235 (SCSI_QUIRK(path->device)->quirks &
1236 CAM_QUIRK_NORPTLUNS) == 0) {
1237 PROBE_SET_ACTION(softc,
1238 PROBE_REPORT_LUNS);
1239 /*
1240 * Start with room for *one* lun.
1241 */
1242 periph->path->target->rpl_size = 16;
1243 } else if (INQ_DATA_TQ_ENABLED(inq_buf))
1244 PROBE_SET_ACTION(softc,
1245 PROBE_MODE_SENSE);
1246 else
1247 PROBE_SET_ACTION(softc,
1248 PROBE_SUPPORTED_VPD_LIST);
1249
1250 if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1251 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1252 xpt_acquire_device(path->device);
1253 }
1254 xpt_release_ccb(done_ccb);
1255 xpt_schedule(periph, priority);
1256 goto out;
1257 } else if (path->device->lun_id == 0 &&
1258 SID_ANSI_REV(inq_buf) >= SCSI_REV_SPC2 &&
1259 (SCSI_QUIRK(path->device)->quirks &
1260 CAM_QUIRK_NORPTLUNS) == 0) {
1261 PROBE_SET_ACTION(softc, PROBE_REPORT_LUNS);
1262 periph->path->target->rpl_size = 16;
1263 xpt_release_ccb(done_ccb);
1264 xpt_schedule(periph, priority);
1265 goto out;
1266 }
1267 } else if (cam_periph_error(done_ccb, 0,
1268 done_ccb->ccb_h.target_lun > 0
1269 ? SF_RETRY_UA|SF_QUIET_IR
1270 : SF_RETRY_UA) == ERESTART) {
1271 goto outr;
1272 } else {
1273 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1274 /* Don't wedge the queue */
1275 xpt_release_devq(done_ccb->ccb_h.path,
1276 /*count*/1, /*run_queue*/TRUE);
1277 }
1278 path->device->flags &= ~CAM_DEV_INQUIRY_DATA_VALID;
1279 }
1280 /*
1281 * If we get to this point, we got an error status back
1282 * from the inquiry and the error status doesn't require
1283 * automatically retrying the command. Therefore, the
1284 * inquiry failed. If we had inquiry information before
1285 * for this device, but this latest inquiry command failed,
1286 * the device has probably gone away. If this device isn't
1287 * already marked unconfigured, notify the peripheral
1288 * drivers that this device is no more.
1289 */
1290 if ((path->device->flags & CAM_DEV_UNCONFIGURED) == 0)
1291 /* Send the async notification. */
1292 xpt_async(AC_LOST_DEVICE, path, NULL);
1293 PROBE_SET_ACTION(softc, PROBE_INVALID);
1294
1295 xpt_release_ccb(done_ccb);
1296 break;
1297 }
1298 case PROBE_REPORT_LUNS:
1299 {
1300 struct ccb_scsiio *csio;
1301 struct scsi_report_luns_data *lp;
1302 u_int nlun, maxlun;
1303
1304 csio = &done_ccb->csio;
1305
1306 lp = (struct scsi_report_luns_data *)csio->data_ptr;
1307 nlun = scsi_4btoul(lp->length) / 8;
1308 maxlun = (csio->dxfer_len / 8) - 1;
1309
1310 if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1311 if (cam_periph_error(done_ccb, 0,
1312 done_ccb->ccb_h.target_lun > 0 ?
1313 SF_RETRY_UA|SF_QUIET_IR : SF_RETRY_UA) ==
1314 ERESTART) {
1315 goto outr;
1316 }
1317 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1318 xpt_release_devq(done_ccb->ccb_h.path, 1,
1319 TRUE);
1320 }
1321 free(lp, M_CAMXPT);
1322 lp = NULL;
1323 } else if (nlun > maxlun) {
1324 /*
1325 * Reallocate and retry to cover all luns
1326 */
1327 CAM_DEBUG(path, CAM_DEBUG_PROBE,
1328 ("Probe: reallocating REPORT_LUNS for %u luns\n",
1329 nlun));
1330 free(lp, M_CAMXPT);
1331 path->target->rpl_size = (nlun << 3) + 8;
1332 xpt_release_ccb(done_ccb);
1333 xpt_schedule(periph, priority);
1334 goto out;
1335 } else if (nlun == 0) {
1336 /*
1337 * If there don't appear to be any luns, bail.
1338 */
1339 free(lp, M_CAMXPT);
1340 lp = NULL;
1341 } else {
1342 lun_id_t lun;
1343 int idx;
1344
1345 CAM_DEBUG(path, CAM_DEBUG_PROBE,
1346 ("Probe: %u lun(s) reported\n", nlun));
1347
1348 CAM_GET_LUN(lp, 0, lun);
1349 /*
1350 * If the first lun is not lun 0, then either there
1351 * is no lun 0 in the list, or the list is unsorted.
1352 */
1353 if (lun != 0) {
1354 for (idx = 0; idx < nlun; idx++) {
1355 CAM_GET_LUN(lp, idx, lun);
1356 if (lun == 0) {
1357 break;
1358 }
1359 }
1360 if (idx != nlun) {
1361 uint8_t tlun[8];
1362 memcpy(tlun,
1363 lp->luns[0].lundata, 8);
1364 memcpy(lp->luns[0].lundata,
1365 lp->luns[idx].lundata, 8);
1366 memcpy(lp->luns[idx].lundata,
1367 tlun, 8);
1368 CAM_DEBUG(path, CAM_DEBUG_PROBE,
1369 ("lun 0 in position %u\n", idx));
1370 }
1371 }
1372 /*
1373 * If we have an old lun list, We can either
1374 * retest luns that appear to have been dropped,
1375 * or just nuke them. We'll opt for the latter.
1376 * This function will also install the new list
1377 * in the target structure.
1378 */
1379 probe_purge_old(path, lp, softc->flags);
1380 lp = NULL;
1381 }
1382 /* The processing above should either exit via a `goto
1383 * out` or leave the `lp` variable `NULL` and (if
1384 * applicable) `free()` the storage to which it had
1385 * pointed. Assert here that is the case.
1386 */
1387 KASSERT(lp == NULL, ("%s: lp is not NULL", __func__));
1388 inq_buf = &path->device->inq_data;
1389 if (path->device->flags & CAM_DEV_INQUIRY_DATA_VALID &&
1390 (SID_QUAL(inq_buf) == SID_QUAL_LU_CONNECTED ||
1391 SID_QUAL(inq_buf) == SID_QUAL_LU_OFFLINE)) {
1392 if (INQ_DATA_TQ_ENABLED(inq_buf))
1393 PROBE_SET_ACTION(softc, PROBE_MODE_SENSE);
1394 else
1395 PROBE_SET_ACTION(softc,
1396 PROBE_SUPPORTED_VPD_LIST);
1397 xpt_release_ccb(done_ccb);
1398 xpt_schedule(periph, priority);
1399 goto out;
1400 }
1401 PROBE_SET_ACTION(softc, PROBE_INVALID);
1402 xpt_release_ccb(done_ccb);
1403 break;
1404 }
1405 case PROBE_MODE_SENSE:
1406 {
1407 struct ccb_scsiio *csio;
1408 struct scsi_mode_header_6 *mode_hdr;
1409
1410 csio = &done_ccb->csio;
1411 mode_hdr = (struct scsi_mode_header_6 *)csio->data_ptr;
1412 if (cam_ccb_status(done_ccb) == CAM_REQ_CMP) {
1413 struct scsi_control_page *page;
1414 uint8_t *offset;
1415
1416 offset = ((uint8_t *)&mode_hdr[1])
1417 + mode_hdr->blk_desc_len;
1418 page = (struct scsi_control_page *)offset;
1419 path->device->queue_flags = page->queue_flags;
1420 } else if (cam_periph_error(done_ccb, 0,
1421 SF_RETRY_UA|SF_NO_PRINT) == ERESTART) {
1422 goto outr;
1423 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1424 /* Don't wedge the queue */
1425 xpt_release_devq(done_ccb->ccb_h.path,
1426 /*count*/1, /*run_queue*/TRUE);
1427 }
1428 xpt_release_ccb(done_ccb);
1429 free(mode_hdr, M_CAMXPT);
1430 PROBE_SET_ACTION(softc, PROBE_SUPPORTED_VPD_LIST);
1431 xpt_schedule(periph, priority);
1432 goto out;
1433 }
1434 case PROBE_SUPPORTED_VPD_LIST:
1435 {
1436 struct ccb_scsiio *csio;
1437 struct scsi_vpd_supported_page_list *page_list;
1438
1439 csio = &done_ccb->csio;
1440 page_list =
1441 (struct scsi_vpd_supported_page_list *)csio->data_ptr;
1442
1443 if (path->device->supported_vpds != NULL) {
1444 free(path->device->supported_vpds, M_CAMXPT);
1445 path->device->supported_vpds = NULL;
1446 path->device->supported_vpds_len = 0;
1447 }
1448
1449 if (page_list == NULL) {
1450 /*
1451 * Don't process the command as it was never sent
1452 */
1453 } else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1454 /* Got vpd list */
1455 path->device->supported_vpds_len = page_list->length +
1456 SVPD_SUPPORTED_PAGES_HDR_LEN;
1457 path->device->supported_vpds = (uint8_t *)page_list;
1458 xpt_release_ccb(done_ccb);
1459 PROBE_SET_ACTION(softc, PROBE_DEVICE_ID);
1460 xpt_schedule(periph, priority);
1461 goto out;
1462 } else if (cam_periph_error(done_ccb, 0,
1463 SF_RETRY_UA|SF_NO_PRINT) == ERESTART) {
1464 goto outr;
1465 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1466 /* Don't wedge the queue */
1467 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1468 /*run_queue*/TRUE);
1469 }
1470
1471 if (page_list)
1472 free(page_list, M_CAMXPT);
1473 /* No VPDs available, skip to device check. */
1474 csio->data_ptr = NULL;
1475 goto probe_device_check;
1476 }
1477 case PROBE_DEVICE_ID:
1478 {
1479 struct scsi_vpd_device_id *devid;
1480 struct ccb_scsiio *csio;
1481 uint32_t length = 0;
1482
1483 csio = &done_ccb->csio;
1484 devid = (struct scsi_vpd_device_id *)csio->data_ptr;
1485
1486 /* Clean up from previous instance of this device */
1487 if (path->device->device_id != NULL) {
1488 path->device->device_id_len = 0;
1489 free(path->device->device_id, M_CAMXPT);
1490 path->device->device_id = NULL;
1491 }
1492
1493 if (devid == NULL) {
1494 /* Don't process the command as it was never sent */
1495 } else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1496 length = scsi_2btoul(devid->length);
1497 if (length != 0) {
1498 /*
1499 * NB: device_id_len is actual response
1500 * size, not buffer size.
1501 */
1502 path->device->device_id_len = length +
1503 SVPD_DEVICE_ID_HDR_LEN;
1504 path->device->device_id = (uint8_t *)devid;
1505 }
1506 } else if (cam_periph_error(done_ccb, 0,
1507 SF_RETRY_UA) == ERESTART) {
1508 goto outr;
1509 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1510 /* Don't wedge the queue */
1511 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1512 /*run_queue*/TRUE);
1513 }
1514
1515 /* Free the device id space if we don't use it */
1516 if (devid && length == 0)
1517 free(devid, M_CAMXPT);
1518 xpt_release_ccb(done_ccb);
1519 PROBE_SET_ACTION(softc, PROBE_EXTENDED_INQUIRY);
1520 xpt_schedule(periph, priority);
1521 goto out;
1522 }
1523 case PROBE_EXTENDED_INQUIRY: {
1524 struct scsi_vpd_extended_inquiry_data *ext_inq;
1525 struct ccb_scsiio *csio;
1526 int32_t length = 0;
1527
1528 csio = &done_ccb->csio;
1529 ext_inq = (struct scsi_vpd_extended_inquiry_data *)
1530 csio->data_ptr;
1531 if (path->device->ext_inq != NULL) {
1532 path->device->ext_inq_len = 0;
1533 free(path->device->ext_inq, M_CAMXPT);
1534 path->device->ext_inq = NULL;
1535 }
1536
1537 if (ext_inq == NULL) {
1538 /* Don't process the command as it was never sent */
1539 } else if (CCB_COMPLETED_OK(csio->ccb_h)) {
1540 length = scsi_2btoul(ext_inq->page_length) +
1541 __offsetof(struct scsi_vpd_extended_inquiry_data,
1542 flags1);
1543 length = min(length, sizeof(*ext_inq));
1544 length -= csio->resid;
1545 if (length > 0) {
1546 path->device->ext_inq_len = length;
1547 path->device->ext_inq = (uint8_t *)ext_inq;
1548 }
1549 } else if (cam_periph_error(done_ccb, 0, SF_RETRY_UA) ==
1550 ERESTART) {
1551 goto outr;
1552 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1553 /* Don't wedge the queue */
1554 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1555 /*run_queue*/TRUE);
1556 }
1557
1558 /* Free the device id space if we don't use it */
1559 if (ext_inq && length <= 0)
1560 free(ext_inq, M_CAMXPT);
1561 xpt_release_ccb(done_ccb);
1562 PROBE_SET_ACTION(softc, PROBE_SERIAL_NUM);
1563 xpt_schedule(periph, priority);
1564 goto out;
1565 }
1566
1567 probe_device_check:
1568 case PROBE_SERIAL_NUM:
1569 {
1570 struct ccb_scsiio *csio;
1571 struct scsi_vpd_unit_serial_number *serial_buf;
1572 uint32_t priority;
1573 int changed;
1574 int have_serialnum;
1575
1576 changed = 1;
1577 have_serialnum = 0;
1578 csio = &done_ccb->csio;
1579 priority = done_ccb->ccb_h.pinfo.priority;
1580 serial_buf =
1581 (struct scsi_vpd_unit_serial_number *)csio->data_ptr;
1582
1583 if (serial_buf == NULL) {
1584 /*
1585 * Don't process the command as it was never sent
1586 */
1587 } else if (cam_ccb_status(done_ccb) == CAM_REQ_CMP
1588 && (serial_buf->length > 0)) {
1589 have_serialnum = 1;
1590 path->device->serial_num =
1591 (uint8_t *)malloc((serial_buf->length + 1),
1592 M_CAMXPT, M_NOWAIT);
1593 if (path->device->serial_num != NULL) {
1594 int start, slen;
1595
1596 start = strspn(serial_buf->serial_num, " ");
1597 slen = serial_buf->length - start;
1598 if (slen <= 0) {
1599 /*
1600 * SPC5r05 says that an all-space serial
1601 * number means no product serial number
1602 * is available
1603 */
1604 slen = 0;
1605 }
1606 /*
1607 * In apparent violation of the spec, some
1608 * devices pad their serial numbers with
1609 * trailing spaces. Remove them.
1610 */
1611 while (slen > 0 &&
1612 serial_buf->serial_num[start + slen - 1] == ' ')
1613 slen--;
1614 memcpy(path->device->serial_num,
1615 &serial_buf->serial_num[start], slen);
1616 path->device->serial_num_len = slen;
1617 path->device->serial_num[slen] = '\0';
1618 }
1619 } else if (cam_periph_error(done_ccb, 0,
1620 SF_RETRY_UA|SF_NO_PRINT) == ERESTART) {
1621 goto outr;
1622 } else if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1623 /* Don't wedge the queue */
1624 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1625 /*run_queue*/TRUE);
1626 }
1627
1628 /*
1629 * Let's see if we have seen this device before.
1630 */
1631 if ((softc->flags & PROBE_INQUIRY_CKSUM) != 0) {
1632 MD5_CTX context;
1633 uint8_t digest[16];
1634
1635 MD5Init(&context);
1636
1637 MD5Update(&context,
1638 (unsigned char *)&path->device->inq_data,
1639 sizeof(struct scsi_inquiry_data));
1640
1641 if (have_serialnum)
1642 MD5Update(&context, path->device->serial_num,
1643 path->device->serial_num_len);
1644
1645 MD5Final(digest, &context);
1646 if (bcmp(softc->digest, digest, 16) == 0)
1647 changed = 0;
1648
1649 /*
1650 * XXX Do we need to do a TUR in order to ensure
1651 * that the device really hasn't changed???
1652 */
1653 if ((changed != 0)
1654 && ((softc->flags & PROBE_NO_ANNOUNCE) == 0))
1655 xpt_async(AC_LOST_DEVICE, path, NULL);
1656 }
1657 if (serial_buf != NULL)
1658 free(serial_buf, M_CAMXPT);
1659
1660 if (changed != 0) {
1661 /*
1662 * Now that we have all the necessary
1663 * information to safely perform transfer
1664 * negotiations... Controllers don't perform
1665 * any negotiation or tagged queuing until
1666 * after the first XPT_SET_TRAN_SETTINGS ccb is
1667 * received. So, on a new device, just retrieve
1668 * the user settings, and set them as the current
1669 * settings to set the device up.
1670 */
1671 proberequestdefaultnegotiation(periph);
1672 xpt_release_ccb(done_ccb);
1673
1674 /*
1675 * Perform a TUR to allow the controller to
1676 * perform any necessary transfer negotiation.
1677 */
1678 PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1679 xpt_schedule(periph, priority);
1680 goto out;
1681 }
1682 xpt_release_ccb(done_ccb);
1683 break;
1684 }
1685 case PROBE_TUR_FOR_NEGOTIATION:
1686 case PROBE_DV_EXIT:
1687 if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1688 if (cam_periph_error(done_ccb, 0, SF_NO_PRINT |
1689 SF_NO_RECOVERY | SF_NO_RETRY) == ERESTART)
1690 goto outr;
1691 }
1692 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1693 /* Don't wedge the queue */
1694 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1695 /*run_queue*/TRUE);
1696 }
1697 /*
1698 * Do Domain Validation for lun 0 on devices that claim
1699 * to support Synchronous Transfer modes.
1700 */
1701 if (softc->action == PROBE_TUR_FOR_NEGOTIATION
1702 && done_ccb->ccb_h.target_lun == 0
1703 && (path->device->inq_data.flags & SID_Sync) != 0
1704 && (path->device->flags & CAM_DEV_IN_DV) == 0) {
1705 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1706 ("Begin Domain Validation\n"));
1707 path->device->flags |= CAM_DEV_IN_DV;
1708 xpt_release_ccb(done_ccb);
1709 PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV1);
1710 xpt_schedule(periph, priority);
1711 goto out;
1712 }
1713 if (softc->action == PROBE_DV_EXIT) {
1714 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1715 ("Leave Domain Validation\n"));
1716 }
1717 if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1718 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1719 xpt_acquire_device(path->device);
1720 }
1721 path->device->flags &=
1722 ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1723 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1724 /* Inform the XPT that a new device has been found */
1725 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1726 xpt_action(done_ccb);
1727 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1728 done_ccb);
1729 }
1730 PROBE_SET_ACTION(softc, PROBE_DONE);
1731 xpt_release_ccb(done_ccb);
1732 break;
1733 case PROBE_INQUIRY_BASIC_DV1:
1734 case PROBE_INQUIRY_BASIC_DV2:
1735 {
1736 struct scsi_inquiry_data *nbuf;
1737 struct ccb_scsiio *csio;
1738
1739 if (cam_ccb_status(done_ccb) != CAM_REQ_CMP) {
1740 if (cam_periph_error(done_ccb, 0, SF_NO_PRINT |
1741 SF_NO_RECOVERY | SF_NO_RETRY) == ERESTART)
1742 goto outr;
1743 }
1744 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0) {
1745 /* Don't wedge the queue */
1746 xpt_release_devq(done_ccb->ccb_h.path, /*count*/1,
1747 /*run_queue*/TRUE);
1748 }
1749 csio = &done_ccb->csio;
1750 nbuf = (struct scsi_inquiry_data *)csio->data_ptr;
1751 if (bcmp(nbuf, &path->device->inq_data, SHORT_INQUIRY_LENGTH)) {
1752 xpt_print(path,
1753 "inquiry data fails comparison at DV%d step\n",
1754 softc->action == PROBE_INQUIRY_BASIC_DV1 ? 1 : 2);
1755 if (proberequestbackoff(periph, path->device)) {
1756 path->device->flags &= ~CAM_DEV_IN_DV;
1757 PROBE_SET_ACTION(softc, PROBE_TUR_FOR_NEGOTIATION);
1758 } else {
1759 /* give up */
1760 PROBE_SET_ACTION(softc, PROBE_DV_EXIT);
1761 }
1762 free(nbuf, M_CAMXPT);
1763 xpt_release_ccb(done_ccb);
1764 xpt_schedule(periph, priority);
1765 goto out;
1766 }
1767 free(nbuf, M_CAMXPT);
1768 if (softc->action == PROBE_INQUIRY_BASIC_DV1) {
1769 PROBE_SET_ACTION(softc, PROBE_INQUIRY_BASIC_DV2);
1770 xpt_release_ccb(done_ccb);
1771 xpt_schedule(periph, priority);
1772 goto out;
1773 }
1774 if (softc->action == PROBE_INQUIRY_BASIC_DV2) {
1775 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE,
1776 ("Leave Domain Validation Successfully\n"));
1777 }
1778 if (path->device->flags & CAM_DEV_UNCONFIGURED) {
1779 path->device->flags &= ~CAM_DEV_UNCONFIGURED;
1780 xpt_acquire_device(path->device);
1781 }
1782 path->device->flags &=
1783 ~(CAM_DEV_IN_DV|CAM_DEV_DV_HIT_BOTTOM);
1784 if ((softc->flags & PROBE_NO_ANNOUNCE) == 0) {
1785 /* Inform the XPT that a new device has been found */
1786 done_ccb->ccb_h.func_code = XPT_GDEV_TYPE;
1787 xpt_action(done_ccb);
1788 xpt_async(AC_FOUND_DEVICE, done_ccb->ccb_h.path,
1789 done_ccb);
1790 }
1791 PROBE_SET_ACTION(softc, PROBE_DONE);
1792 xpt_release_ccb(done_ccb);
1793 break;
1794 }
1795 default:
1796 panic("probedone: invalid action state 0x%x\n", softc->action);
1797 }
1798 done_ccb = (union ccb *)TAILQ_FIRST(&softc->request_ccbs);
1799 TAILQ_REMOVE(&softc->request_ccbs, &done_ccb->ccb_h, periph_links.tqe);
1800 done_ccb->ccb_h.status = CAM_REQ_CMP;
1801 xpt_done(done_ccb);
1802 if (TAILQ_FIRST(&softc->request_ccbs) == NULL) {
1803 CAM_DEBUG(periph->path, CAM_DEBUG_PROBE, ("Probe completed\n"));
1804 /* Drop freeze taken due to CAM_DEV_QFREEZE flag set. */
1805 cam_release_devq(path, 0, 0, 0, FALSE);
1806 cam_periph_release_locked(periph);
1807 cam_periph_invalidate(periph);
1808 cam_periph_release_locked(periph);
1809 } else {
1810 probeschedule(periph);
1811 goto out;
1812 }
1813 }
1814
1815 static void
probe_purge_old(struct cam_path * path,struct scsi_report_luns_data * new,probe_flags flags)1816 probe_purge_old(struct cam_path *path, struct scsi_report_luns_data *new,
1817 probe_flags flags)
1818 {
1819 struct cam_path *tp;
1820 struct scsi_report_luns_data *old;
1821 u_int idx1, idx2, nlun_old, nlun_new;
1822 lun_id_t this_lun;
1823 uint8_t *ol, *nl;
1824
1825 if (path->target == NULL) {
1826 return;
1827 }
1828 mtx_lock(&path->target->luns_mtx);
1829 old = path->target->luns;
1830 path->target->luns = new;
1831 mtx_unlock(&path->target->luns_mtx);
1832 if (old == NULL)
1833 return;
1834 nlun_old = scsi_4btoul(old->length) / 8;
1835 nlun_new = scsi_4btoul(new->length) / 8;
1836
1837 /*
1838 * We are not going to assume sorted lists. Deal.
1839 */
1840 for (idx1 = 0; idx1 < nlun_old; idx1++) {
1841 ol = old->luns[idx1].lundata;
1842 for (idx2 = 0; idx2 < nlun_new; idx2++) {
1843 nl = new->luns[idx2].lundata;
1844 if (memcmp(nl, ol, 8) == 0) {
1845 break;
1846 }
1847 }
1848 if (idx2 < nlun_new) {
1849 continue;
1850 }
1851 /*
1852 * An 'old' item not in the 'new' list.
1853 * Nuke it. Except that if it is lun 0,
1854 * that would be what the probe state
1855 * machine is currently working on,
1856 * so we won't do that.
1857 */
1858 CAM_GET_LUN(old, idx1, this_lun);
1859 if (this_lun == 0) {
1860 continue;
1861 }
1862
1863 /*
1864 * We also cannot nuke it if it is
1865 * not in a lun format we understand
1866 * and replace the LUN with a "simple" LUN
1867 * if that is all the HBA supports.
1868 */
1869 if (!(flags & PROBE_EXTLUN)) {
1870 if (!CAM_CAN_GET_SIMPLE_LUN(old, idx1))
1871 continue;
1872 CAM_GET_SIMPLE_LUN(old, idx1, this_lun);
1873 }
1874
1875 if (xpt_create_path(&tp, NULL, xpt_path_path_id(path),
1876 xpt_path_target_id(path), this_lun) == CAM_REQ_CMP) {
1877 xpt_async(AC_LOST_DEVICE, tp, NULL);
1878 xpt_free_path(tp);
1879 }
1880 }
1881 free(old, M_CAMXPT);
1882 }
1883
1884 static void
probecleanup(struct cam_periph * periph)1885 probecleanup(struct cam_periph *periph)
1886 {
1887 free(periph->softc, M_CAMXPT);
1888 }
1889
1890 static void
scsi_find_quirk(struct cam_ed * device)1891 scsi_find_quirk(struct cam_ed *device)
1892 {
1893 struct scsi_quirk_entry *quirk;
1894 caddr_t match;
1895
1896 match = cam_quirkmatch((caddr_t)&device->inq_data,
1897 (caddr_t)scsi_quirk_table,
1898 nitems(scsi_quirk_table),
1899 sizeof(*scsi_quirk_table), scsi_inquiry_match);
1900
1901 if (match == NULL)
1902 panic("xpt_find_quirk: device didn't match wildcard entry!!");
1903
1904 quirk = (struct scsi_quirk_entry *)match;
1905 device->quirk = quirk;
1906 device->mintags = quirk->mintags;
1907 device->maxtags = quirk->maxtags;
1908 }
1909
1910 typedef struct {
1911 union ccb *request_ccb;
1912 struct ccb_pathinq *cpi;
1913 int counter;
1914 int lunindex[0];
1915 } scsi_scan_bus_info;
1916
1917 /*
1918 * To start a scan, request_ccb is an XPT_SCAN_BUS ccb.
1919 * As the scan progresses, scsi_scan_bus is used as the
1920 * callback on completion function.
1921 */
1922 static void
scsi_scan_bus(struct cam_periph * periph,union ccb * request_ccb)1923 scsi_scan_bus(struct cam_periph *periph, union ccb *request_ccb)
1924 {
1925 struct mtx *mtx;
1926
1927 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1928 ("scsi_scan_bus\n"));
1929 switch (request_ccb->ccb_h.func_code) {
1930 case XPT_SCAN_BUS:
1931 case XPT_SCAN_TGT:
1932 {
1933 scsi_scan_bus_info *scan_info;
1934 union ccb *work_ccb, *reset_ccb;
1935 struct cam_path *path;
1936 u_int i;
1937 u_int low_target, max_target;
1938 u_int initiator_id;
1939
1940 /* Find out the characteristics of the bus */
1941 work_ccb = xpt_alloc_ccb_nowait();
1942 if (work_ccb == NULL) {
1943 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1944 xpt_done(request_ccb);
1945 return;
1946 }
1947 xpt_setup_ccb(&work_ccb->ccb_h, request_ccb->ccb_h.path,
1948 request_ccb->ccb_h.pinfo.priority);
1949 work_ccb->ccb_h.func_code = XPT_PATH_INQ;
1950 xpt_action(work_ccb);
1951 if (work_ccb->ccb_h.status != CAM_REQ_CMP) {
1952 request_ccb->ccb_h.status = work_ccb->ccb_h.status;
1953 xpt_free_ccb(work_ccb);
1954 xpt_done(request_ccb);
1955 return;
1956 }
1957
1958 if ((work_ccb->cpi.hba_misc & PIM_NOINITIATOR) != 0) {
1959 /*
1960 * Can't scan the bus on an adapter that
1961 * cannot perform the initiator role.
1962 */
1963 request_ccb->ccb_h.status = CAM_REQ_CMP;
1964 xpt_free_ccb(work_ccb);
1965 xpt_done(request_ccb);
1966 return;
1967 }
1968
1969 /* We may need to reset bus first, if we haven't done it yet. */
1970 if ((work_ccb->cpi.hba_inquiry &
1971 (PI_WIDE_32|PI_WIDE_16|PI_SDTR_ABLE)) &&
1972 !(work_ccb->cpi.hba_misc & PIM_NOBUSRESET) &&
1973 !timevalisset(&request_ccb->ccb_h.path->bus->last_reset) &&
1974 (reset_ccb = xpt_alloc_ccb_nowait()) != NULL) {
1975 xpt_setup_ccb(&reset_ccb->ccb_h, request_ccb->ccb_h.path,
1976 CAM_PRIORITY_NONE);
1977 reset_ccb->ccb_h.func_code = XPT_RESET_BUS;
1978 xpt_action(reset_ccb);
1979 if (reset_ccb->ccb_h.status != CAM_REQ_CMP) {
1980 request_ccb->ccb_h.status = reset_ccb->ccb_h.status;
1981 xpt_free_ccb(reset_ccb);
1982 xpt_free_ccb(work_ccb);
1983 xpt_done(request_ccb);
1984 return;
1985 }
1986 xpt_free_ccb(reset_ccb);
1987 }
1988
1989 /* Save some state for use while we probe for devices */
1990 scan_info = (scsi_scan_bus_info *) malloc(sizeof(scsi_scan_bus_info) +
1991 (work_ccb->cpi.max_target * sizeof (u_int)), M_CAMXPT, M_ZERO|M_NOWAIT);
1992 if (scan_info == NULL) {
1993 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
1994 xpt_free_ccb(work_ccb);
1995 xpt_done(request_ccb);
1996 return;
1997 }
1998 CAM_DEBUG(request_ccb->ccb_h.path, CAM_DEBUG_TRACE,
1999 ("SCAN start for %p\n", scan_info));
2000 scan_info->request_ccb = request_ccb;
2001 scan_info->cpi = &work_ccb->cpi;
2002
2003 /* Cache on our stack so we can work asynchronously */
2004 max_target = scan_info->cpi->max_target;
2005 low_target = 0;
2006 initiator_id = scan_info->cpi->initiator_id;
2007
2008 /*
2009 * We can scan all targets in parallel, or do it sequentially.
2010 */
2011
2012 if (request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
2013 max_target = low_target = request_ccb->ccb_h.target_id;
2014 scan_info->counter = 0;
2015 } else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
2016 max_target = 0;
2017 scan_info->counter = 0;
2018 } else {
2019 scan_info->counter = scan_info->cpi->max_target + 1;
2020 if (scan_info->cpi->initiator_id < scan_info->counter) {
2021 scan_info->counter--;
2022 }
2023 }
2024 mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
2025 mtx_unlock(mtx);
2026
2027 for (i = low_target; i <= max_target; i++) {
2028 cam_status status;
2029 if (i == initiator_id)
2030 continue;
2031
2032 status = xpt_create_path(&path, NULL,
2033 request_ccb->ccb_h.path_id,
2034 i, 0);
2035 if (status != CAM_REQ_CMP) {
2036 printf(
2037 "scsi_scan_bus: xpt_create_path failed with status %#x, bus scan halted\n",
2038 status);
2039 free(scan_info, M_CAMXPT);
2040 request_ccb->ccb_h.status = status;
2041 xpt_free_ccb(work_ccb);
2042 xpt_done(request_ccb);
2043 break;
2044 }
2045 work_ccb = xpt_alloc_ccb_nowait();
2046 if (work_ccb == NULL) {
2047 xpt_free_ccb((union ccb *)scan_info->cpi);
2048 free(scan_info, M_CAMXPT);
2049 xpt_free_path(path);
2050 request_ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
2051 xpt_done(request_ccb);
2052 break;
2053 }
2054 xpt_setup_ccb(&work_ccb->ccb_h, path,
2055 request_ccb->ccb_h.pinfo.priority);
2056 work_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2057 work_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2058 work_ccb->ccb_h.flags |= CAM_UNLOCKED;
2059 work_ccb->ccb_h.ppriv_ptr0 = scan_info;
2060 work_ccb->crcn.flags = request_ccb->crcn.flags;
2061 xpt_action(work_ccb);
2062 }
2063
2064 mtx_lock(mtx);
2065 break;
2066 }
2067 case XPT_SCAN_LUN:
2068 {
2069 cam_status status;
2070 struct cam_path *path, *oldpath;
2071 scsi_scan_bus_info *scan_info;
2072 struct cam_et *target;
2073 struct cam_ed *device, *nextdev;
2074 int next_target;
2075 path_id_t path_id;
2076 target_id_t target_id;
2077 lun_id_t lun_id;
2078
2079 oldpath = request_ccb->ccb_h.path;
2080
2081 status = cam_ccb_status(request_ccb);
2082 scan_info = (scsi_scan_bus_info *)request_ccb->ccb_h.ppriv_ptr0;
2083 path_id = request_ccb->ccb_h.path_id;
2084 target_id = request_ccb->ccb_h.target_id;
2085 lun_id = request_ccb->ccb_h.target_lun;
2086 target = request_ccb->ccb_h.path->target;
2087 next_target = 1;
2088
2089 mtx = xpt_path_mtx(scan_info->request_ccb->ccb_h.path);
2090 mtx_lock(mtx);
2091 mtx_lock(&target->luns_mtx);
2092 if (target->luns) {
2093 lun_id_t first;
2094 u_int nluns = scsi_4btoul(target->luns->length) / 8;
2095
2096 /*
2097 * Make sure we skip over lun 0 if it's the first member
2098 * of the list as we've actually just finished probing
2099 * it.
2100 */
2101 CAM_GET_LUN(target->luns, 0, first);
2102 if (first == 0 && scan_info->lunindex[target_id] == 0) {
2103 scan_info->lunindex[target_id]++;
2104 }
2105
2106 /*
2107 * Skip any LUNs that the HBA can't deal with.
2108 */
2109 while (scan_info->lunindex[target_id] < nluns) {
2110 if (scan_info->cpi->hba_misc & PIM_EXTLUNS) {
2111 CAM_GET_LUN(target->luns,
2112 scan_info->lunindex[target_id],
2113 lun_id);
2114 break;
2115 }
2116
2117 if (CAM_CAN_GET_SIMPLE_LUN(target->luns,
2118 scan_info->lunindex[target_id])) {
2119 CAM_GET_SIMPLE_LUN(target->luns,
2120 scan_info->lunindex[target_id],
2121 lun_id);
2122 break;
2123 }
2124
2125 scan_info->lunindex[target_id]++;
2126 }
2127
2128 if (scan_info->lunindex[target_id] < nluns) {
2129 mtx_unlock(&target->luns_mtx);
2130 next_target = 0;
2131 CAM_DEBUG(request_ccb->ccb_h.path,
2132 CAM_DEBUG_PROBE,
2133 ("next lun to try at index %u is %jx\n",
2134 scan_info->lunindex[target_id],
2135 (uintmax_t)lun_id));
2136 scan_info->lunindex[target_id]++;
2137 } else {
2138 mtx_unlock(&target->luns_mtx);
2139 /* We're done with scanning all luns. */
2140 }
2141 } else {
2142 mtx_unlock(&target->luns_mtx);
2143 device = request_ccb->ccb_h.path->device;
2144 /* Continue sequential LUN scan if: */
2145 /* -- we have more LUNs that need recheck */
2146 mtx_lock(&target->bus->eb_mtx);
2147 nextdev = device;
2148 while ((nextdev = TAILQ_NEXT(nextdev, links)) != NULL)
2149 if ((nextdev->flags & CAM_DEV_UNCONFIGURED) == 0)
2150 break;
2151 mtx_unlock(&target->bus->eb_mtx);
2152 if (nextdev != NULL) {
2153 next_target = 0;
2154 /* -- stop if CAM_QUIRK_NOLUNS is set. */
2155 } else if (SCSI_QUIRK(device)->quirks & CAM_QUIRK_NOLUNS) {
2156 next_target = 1;
2157 /* -- this LUN is connected and its SCSI version
2158 * allows more LUNs. */
2159 } else if ((device->flags & CAM_DEV_UNCONFIGURED) == 0) {
2160 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
2161 CAN_SRCH_HI_DENSE(device))
2162 next_target = 0;
2163 /* -- this LUN is disconnected, its SCSI version
2164 * allows more LUNs and we guess they may be. */
2165 } else if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0) {
2166 if (lun_id < (CAM_SCSI2_MAXLUN-1) ||
2167 CAN_SRCH_HI_SPARSE(device))
2168 next_target = 0;
2169 }
2170 if (next_target == 0) {
2171 lun_id++;
2172 if (lun_id > scan_info->cpi->max_lun)
2173 next_target = 1;
2174 }
2175 }
2176
2177 /*
2178 * Check to see if we scan any further luns.
2179 */
2180 if (next_target) {
2181 int done;
2182
2183 /*
2184 * Free the current request path- we're done with it.
2185 */
2186 xpt_free_path(oldpath);
2187 hop_again:
2188 done = 0;
2189 if (scan_info->request_ccb->ccb_h.func_code == XPT_SCAN_TGT) {
2190 done = 1;
2191 } else if (scan_info->cpi->hba_misc & PIM_SEQSCAN) {
2192 scan_info->counter++;
2193 if (scan_info->counter ==
2194 scan_info->cpi->initiator_id) {
2195 scan_info->counter++;
2196 }
2197 if (scan_info->counter >=
2198 scan_info->cpi->max_target+1) {
2199 done = 1;
2200 }
2201 } else {
2202 scan_info->counter--;
2203 if (scan_info->counter == 0) {
2204 done = 1;
2205 }
2206 }
2207 if (done) {
2208 mtx_unlock(mtx);
2209 xpt_free_ccb(request_ccb);
2210 xpt_free_ccb((union ccb *)scan_info->cpi);
2211 request_ccb = scan_info->request_ccb;
2212 CAM_DEBUG(request_ccb->ccb_h.path,
2213 CAM_DEBUG_TRACE,
2214 ("SCAN done for %p\n", scan_info));
2215 free(scan_info, M_CAMXPT);
2216 request_ccb->ccb_h.status = CAM_REQ_CMP;
2217 xpt_done(request_ccb);
2218 break;
2219 }
2220
2221 if ((scan_info->cpi->hba_misc & PIM_SEQSCAN) == 0) {
2222 mtx_unlock(mtx);
2223 xpt_free_ccb(request_ccb);
2224 break;
2225 }
2226 status = xpt_create_path(&path, NULL,
2227 scan_info->request_ccb->ccb_h.path_id,
2228 scan_info->counter, 0);
2229 if (status != CAM_REQ_CMP) {
2230 mtx_unlock(mtx);
2231 printf(
2232 "scsi_scan_bus: xpt_create_path failed with status %#x, bus scan halted\n",
2233 status);
2234 xpt_free_ccb(request_ccb);
2235 xpt_free_ccb((union ccb *)scan_info->cpi);
2236 request_ccb = scan_info->request_ccb;
2237 free(scan_info, M_CAMXPT);
2238 request_ccb->ccb_h.status = status;
2239 xpt_done(request_ccb);
2240 break;
2241 }
2242 xpt_setup_ccb(&request_ccb->ccb_h, path,
2243 request_ccb->ccb_h.pinfo.priority);
2244 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2245 request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2246 request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2247 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2248 request_ccb->crcn.flags =
2249 scan_info->request_ccb->crcn.flags;
2250 } else {
2251 status = xpt_create_path(&path, NULL,
2252 path_id, target_id, lun_id);
2253 /*
2254 * Free the old request path- we're done with it. We
2255 * do this *after* creating the new path so that
2256 * we don't remove a target that has our lun list
2257 * in the case that lun 0 is not present.
2258 */
2259 xpt_free_path(oldpath);
2260 if (status != CAM_REQ_CMP) {
2261 printf(
2262 "scsi_scan_bus: xpt_create_path failed with status %#x, halting LUN scan\n",
2263 status);
2264 goto hop_again;
2265 }
2266 xpt_setup_ccb(&request_ccb->ccb_h, path,
2267 request_ccb->ccb_h.pinfo.priority);
2268 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2269 request_ccb->ccb_h.cbfcnp = scsi_scan_bus;
2270 request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2271 request_ccb->ccb_h.ppriv_ptr0 = scan_info;
2272 request_ccb->crcn.flags =
2273 scan_info->request_ccb->crcn.flags;
2274 }
2275 mtx_unlock(mtx);
2276 xpt_action(request_ccb);
2277 break;
2278 }
2279 default:
2280 break;
2281 }
2282 }
2283
2284 static void
scsi_scan_lun(struct cam_periph * periph,struct cam_path * path,cam_flags flags,union ccb * request_ccb)2285 scsi_scan_lun(struct cam_periph *periph, struct cam_path *path,
2286 cam_flags flags, union ccb *request_ccb)
2287 {
2288 struct ccb_pathinq cpi;
2289 cam_status status;
2290 struct cam_path *new_path;
2291 struct cam_periph *old_periph;
2292 int lock;
2293
2294 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("scsi_scan_lun\n"));
2295
2296 memset(&cpi, 0, sizeof(cpi));
2297 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2298 cpi.ccb_h.func_code = XPT_PATH_INQ;
2299 xpt_action((union ccb *)&cpi);
2300
2301 if (cpi.ccb_h.status != CAM_REQ_CMP) {
2302 if (request_ccb != NULL) {
2303 request_ccb->ccb_h.status = cpi.ccb_h.status;
2304 xpt_done(request_ccb);
2305 }
2306 return;
2307 }
2308
2309 if ((cpi.hba_misc & PIM_NOINITIATOR) != 0) {
2310 /*
2311 * Can't scan the bus on an adapter that
2312 * cannot perform the initiator role.
2313 */
2314 if (request_ccb != NULL) {
2315 request_ccb->ccb_h.status = CAM_REQ_CMP;
2316 xpt_done(request_ccb);
2317 }
2318 return;
2319 }
2320
2321 if (request_ccb == NULL) {
2322 request_ccb = xpt_alloc_ccb_nowait();
2323 if (request_ccb == NULL) {
2324 xpt_print(path,
2325 "scsi_scan_lun: can't allocate CCB, can't continue\n");
2326 return;
2327 }
2328 status = xpt_create_path(&new_path, NULL,
2329 path->bus->path_id,
2330 path->target->target_id,
2331 path->device->lun_id);
2332 if (status != CAM_REQ_CMP) {
2333 xpt_print(path,
2334 "scsi_scan_lun: can't create path, can't continue\n");
2335 xpt_free_ccb(request_ccb);
2336 return;
2337 }
2338 xpt_setup_ccb(&request_ccb->ccb_h, new_path, CAM_PRIORITY_XPT);
2339 request_ccb->ccb_h.cbfcnp = xptscandone;
2340 request_ccb->ccb_h.func_code = XPT_SCAN_LUN;
2341 request_ccb->ccb_h.flags |= CAM_UNLOCKED;
2342 request_ccb->crcn.flags = flags;
2343 }
2344
2345 lock = (xpt_path_owned(path) == 0);
2346 if (lock)
2347 xpt_path_lock(path);
2348 if ((old_periph = cam_periph_find(path, "probe")) != NULL) {
2349 if ((old_periph->flags & CAM_PERIPH_INVALID) == 0) {
2350 probe_softc *softc;
2351
2352 softc = (probe_softc *)old_periph->softc;
2353 TAILQ_INSERT_TAIL(&softc->request_ccbs,
2354 &request_ccb->ccb_h, periph_links.tqe);
2355 } else {
2356 request_ccb->ccb_h.status = CAM_REQ_CMP_ERR;
2357 xpt_done(request_ccb);
2358 }
2359 } else {
2360 status = cam_periph_alloc(proberegister, NULL, probecleanup,
2361 probestart, "probe",
2362 CAM_PERIPH_BIO,
2363 request_ccb->ccb_h.path, NULL, 0,
2364 request_ccb);
2365
2366 if (status != CAM_REQ_CMP) {
2367 xpt_print(path,
2368 "scsi_scan_lun: cam_alloc_periph returned an error, can't continue probe\n");
2369 request_ccb->ccb_h.status = status;
2370 xpt_done(request_ccb);
2371 }
2372 }
2373 if (lock)
2374 xpt_path_unlock(path);
2375 }
2376
2377 static void
xptscandone(struct cam_periph * periph,union ccb * done_ccb)2378 xptscandone(struct cam_periph *periph, union ccb *done_ccb)
2379 {
2380
2381 xpt_free_path(done_ccb->ccb_h.path);
2382 xpt_free_ccb(done_ccb);
2383 }
2384
2385 static struct cam_ed *
scsi_alloc_device(struct cam_eb * bus,struct cam_et * target,lun_id_t lun_id)2386 scsi_alloc_device(struct cam_eb *bus, struct cam_et *target, lun_id_t lun_id)
2387 {
2388 struct scsi_quirk_entry *quirk;
2389 struct cam_ed *device;
2390
2391 device = xpt_alloc_device(bus, target, lun_id);
2392 if (device == NULL)
2393 return (NULL);
2394
2395 /*
2396 * Take the default quirk entry until we have inquiry
2397 * data and can determine a better quirk to use.
2398 */
2399 quirk = &scsi_quirk_table[nitems(scsi_quirk_table) - 1];
2400 device->quirk = (void *)quirk;
2401 device->mintags = quirk->mintags;
2402 device->maxtags = quirk->maxtags;
2403 bzero(&device->inq_data, sizeof(device->inq_data));
2404 device->inq_flags = 0;
2405 device->queue_flags = 0;
2406 device->serial_num = NULL;
2407 device->serial_num_len = 0;
2408 device->device_id = NULL;
2409 device->device_id_len = 0;
2410 device->supported_vpds = NULL;
2411 device->supported_vpds_len = 0;
2412 return (device);
2413 }
2414
2415 static void
scsi_devise_transport(struct cam_path * path)2416 scsi_devise_transport(struct cam_path *path)
2417 {
2418 struct ccb_pathinq cpi;
2419 struct ccb_trans_settings cts;
2420 struct scsi_inquiry_data *inq_buf;
2421
2422 /* Get transport information from the SIM */
2423 memset(&cpi, 0, sizeof(cpi));
2424 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2425 cpi.ccb_h.func_code = XPT_PATH_INQ;
2426 xpt_action((union ccb *)&cpi);
2427
2428 inq_buf = NULL;
2429 if ((path->device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0)
2430 inq_buf = &path->device->inq_data;
2431 path->device->protocol = PROTO_SCSI;
2432 path->device->protocol_version =
2433 inq_buf != NULL ? SID_ANSI_REV(inq_buf) : cpi.protocol_version;
2434 path->device->transport = cpi.transport;
2435 path->device->transport_version = cpi.transport_version;
2436
2437 /*
2438 * Any device not using SPI3 features should
2439 * be considered SPI2 or lower.
2440 */
2441 if (inq_buf != NULL) {
2442 if (path->device->transport == XPORT_SPI
2443 && (inq_buf->spi3data & SID_SPI_MASK) == 0
2444 && path->device->transport_version > 2)
2445 path->device->transport_version = 2;
2446 } else {
2447 struct cam_ed* otherdev;
2448
2449 for (otherdev = TAILQ_FIRST(&path->target->ed_entries);
2450 otherdev != NULL;
2451 otherdev = TAILQ_NEXT(otherdev, links)) {
2452 if (otherdev != path->device)
2453 break;
2454 }
2455
2456 if (otherdev != NULL) {
2457 /*
2458 * Initially assume the same versioning as
2459 * prior luns for this target.
2460 */
2461 path->device->protocol_version =
2462 otherdev->protocol_version;
2463 path->device->transport_version =
2464 otherdev->transport_version;
2465 } else {
2466 /* Until we know better, opt for safety */
2467 path->device->protocol_version = 2;
2468 if (path->device->transport == XPORT_SPI)
2469 path->device->transport_version = 2;
2470 else
2471 path->device->transport_version = 0;
2472 }
2473 }
2474
2475 /*
2476 * XXX
2477 * For a device compliant with SPC-2 we should be able
2478 * to determine the transport version supported by
2479 * scrutinizing the version descriptors in the
2480 * inquiry buffer.
2481 */
2482
2483 /* Tell the controller what we think */
2484 memset(&cts, 0, sizeof(cts));
2485 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2486 cts.ccb_h.func_code = XPT_SET_TRAN_SETTINGS;
2487 cts.type = CTS_TYPE_CURRENT_SETTINGS;
2488 cts.transport = path->device->transport;
2489 cts.transport_version = path->device->transport_version;
2490 cts.protocol = path->device->protocol;
2491 cts.protocol_version = path->device->protocol_version;
2492 cts.proto_specific.valid = 0;
2493 cts.xport_specific.valid = 0;
2494 xpt_action((union ccb *)&cts);
2495 }
2496
2497 static void
scsi_dev_advinfo(union ccb * start_ccb)2498 scsi_dev_advinfo(union ccb *start_ccb)
2499 {
2500 struct cam_ed *device;
2501 struct ccb_dev_advinfo *cdai;
2502 off_t amt;
2503
2504 xpt_path_assert(start_ccb->ccb_h.path, MA_OWNED);
2505 start_ccb->ccb_h.status = CAM_REQ_INVALID;
2506 device = start_ccb->ccb_h.path->device;
2507 cdai = &start_ccb->cdai;
2508 switch(cdai->buftype) {
2509 case CDAI_TYPE_SCSI_DEVID:
2510 if (cdai->flags & CDAI_FLAG_STORE)
2511 return;
2512 cdai->provsiz = device->device_id_len;
2513 if (device->device_id_len == 0)
2514 break;
2515 amt = device->device_id_len;
2516 if (cdai->provsiz > cdai->bufsiz)
2517 amt = cdai->bufsiz;
2518 memcpy(cdai->buf, device->device_id, amt);
2519 break;
2520 case CDAI_TYPE_SERIAL_NUM:
2521 if (cdai->flags & CDAI_FLAG_STORE)
2522 return;
2523 cdai->provsiz = device->serial_num_len;
2524 if (device->serial_num_len == 0)
2525 break;
2526 amt = device->serial_num_len;
2527 if (cdai->provsiz > cdai->bufsiz)
2528 amt = cdai->bufsiz;
2529 memcpy(cdai->buf, device->serial_num, amt);
2530 break;
2531 case CDAI_TYPE_PHYS_PATH:
2532 if (cdai->flags & CDAI_FLAG_STORE) {
2533 if (device->physpath != NULL) {
2534 free(device->physpath, M_CAMXPT);
2535 device->physpath = NULL;
2536 device->physpath_len = 0;
2537 }
2538 /* Clear existing buffer if zero length */
2539 if (cdai->bufsiz == 0)
2540 break;
2541 device->physpath = malloc(cdai->bufsiz, M_CAMXPT, M_NOWAIT);
2542 if (device->physpath == NULL) {
2543 start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2544 return;
2545 }
2546 device->physpath_len = cdai->bufsiz;
2547 memcpy(device->physpath, cdai->buf, cdai->bufsiz);
2548 } else {
2549 cdai->provsiz = device->physpath_len;
2550 if (device->physpath_len == 0)
2551 break;
2552 amt = device->physpath_len;
2553 if (cdai->provsiz > cdai->bufsiz)
2554 amt = cdai->bufsiz;
2555 memcpy(cdai->buf, device->physpath, amt);
2556 }
2557 break;
2558 case CDAI_TYPE_RCAPLONG:
2559 if (cdai->flags & CDAI_FLAG_STORE) {
2560 if (device->rcap_buf != NULL) {
2561 free(device->rcap_buf, M_CAMXPT);
2562 device->rcap_buf = NULL;
2563 }
2564
2565 device->rcap_len = cdai->bufsiz;
2566 /* Clear existing buffer if zero length */
2567 if (cdai->bufsiz == 0)
2568 break;
2569
2570 device->rcap_buf = malloc(cdai->bufsiz, M_CAMXPT,
2571 M_NOWAIT);
2572 if (device->rcap_buf == NULL) {
2573 start_ccb->ccb_h.status = CAM_REQ_ABORTED;
2574 return;
2575 }
2576
2577 memcpy(device->rcap_buf, cdai->buf, cdai->bufsiz);
2578 } else {
2579 cdai->provsiz = device->rcap_len;
2580 if (device->rcap_len == 0)
2581 break;
2582 amt = device->rcap_len;
2583 if (cdai->provsiz > cdai->bufsiz)
2584 amt = cdai->bufsiz;
2585 memcpy(cdai->buf, device->rcap_buf, amt);
2586 }
2587 break;
2588 case CDAI_TYPE_EXT_INQ:
2589 /*
2590 * We fetch extended inquiry data during probe, if
2591 * available. We don't allow changing it.
2592 */
2593 if (cdai->flags & CDAI_FLAG_STORE)
2594 return;
2595 cdai->provsiz = device->ext_inq_len;
2596 if (device->ext_inq_len == 0)
2597 break;
2598 amt = device->ext_inq_len;
2599 if (cdai->provsiz > cdai->bufsiz)
2600 amt = cdai->bufsiz;
2601 memcpy(cdai->buf, device->ext_inq, amt);
2602 break;
2603 default:
2604 return;
2605 }
2606 start_ccb->ccb_h.status = CAM_REQ_CMP;
2607
2608 if (cdai->flags & CDAI_FLAG_STORE) {
2609 xpt_async(AC_ADVINFO_CHANGED, start_ccb->ccb_h.path,
2610 (void *)(uintptr_t)cdai->buftype);
2611 }
2612 }
2613
2614 static void
scsi_action(union ccb * start_ccb)2615 scsi_action(union ccb *start_ccb)
2616 {
2617
2618 if (start_ccb->ccb_h.func_code != XPT_SCSI_IO) {
2619 KASSERT((start_ccb->ccb_h.alloc_flags & CAM_CCB_FROM_UMA) == 0,
2620 ("%s: ccb %p, func_code %#x should not be allocated from UMA zone\n",
2621 __func__, start_ccb, start_ccb->ccb_h.func_code));
2622 }
2623
2624 switch (start_ccb->ccb_h.func_code) {
2625 case XPT_SET_TRAN_SETTINGS:
2626 {
2627 scsi_set_transfer_settings(&start_ccb->cts,
2628 start_ccb->ccb_h.path,
2629 /*async_update*/FALSE);
2630 break;
2631 }
2632 case XPT_SCAN_BUS:
2633 case XPT_SCAN_TGT:
2634 scsi_scan_bus(start_ccb->ccb_h.path->periph, start_ccb);
2635 break;
2636 case XPT_SCAN_LUN:
2637 scsi_scan_lun(start_ccb->ccb_h.path->periph,
2638 start_ccb->ccb_h.path, start_ccb->crcn.flags,
2639 start_ccb);
2640 break;
2641 case XPT_DEV_ADVINFO:
2642 {
2643 scsi_dev_advinfo(start_ccb);
2644 break;
2645 }
2646 default:
2647 xpt_action_default(start_ccb);
2648 break;
2649 }
2650 }
2651
2652 static void
scsi_set_transfer_settings(struct ccb_trans_settings * cts,struct cam_path * path,int async_update)2653 scsi_set_transfer_settings(struct ccb_trans_settings *cts, struct cam_path *path,
2654 int async_update)
2655 {
2656 struct ccb_pathinq cpi;
2657 struct ccb_trans_settings cur_cts;
2658 struct ccb_trans_settings_scsi *scsi;
2659 struct ccb_trans_settings_scsi *cur_scsi;
2660 struct scsi_inquiry_data *inq_data;
2661 struct cam_ed *device;
2662
2663 if (path == NULL || (device = path->device) == NULL) {
2664 cts->ccb_h.status = CAM_PATH_INVALID;
2665 xpt_done((union ccb *)cts);
2666 return;
2667 }
2668
2669 if (cts->protocol == PROTO_UNKNOWN
2670 || cts->protocol == PROTO_UNSPECIFIED) {
2671 cts->protocol = device->protocol;
2672 cts->protocol_version = device->protocol_version;
2673 }
2674
2675 if (cts->protocol_version == PROTO_VERSION_UNKNOWN
2676 || cts->protocol_version == PROTO_VERSION_UNSPECIFIED)
2677 cts->protocol_version = device->protocol_version;
2678
2679 if (cts->protocol != device->protocol) {
2680 xpt_print(path, "Uninitialized Protocol %x:%x?\n",
2681 cts->protocol, device->protocol);
2682 cts->protocol = device->protocol;
2683 }
2684
2685 if (cts->protocol_version > device->protocol_version) {
2686 if (bootverbose) {
2687 xpt_print(path,
2688 "Down reving Protocol Version from %d to %d?\n",
2689 cts->protocol_version,
2690 device->protocol_version);
2691 }
2692 cts->protocol_version = device->protocol_version;
2693 }
2694
2695 if (cts->transport == XPORT_UNKNOWN
2696 || cts->transport == XPORT_UNSPECIFIED) {
2697 cts->transport = device->transport;
2698 cts->transport_version = device->transport_version;
2699 }
2700
2701 if (cts->transport_version == XPORT_VERSION_UNKNOWN
2702 || cts->transport_version == XPORT_VERSION_UNSPECIFIED)
2703 cts->transport_version = device->transport_version;
2704
2705 if (cts->transport != device->transport) {
2706 xpt_print(path, "Uninitialized Transport %x:%x?\n",
2707 cts->transport, device->transport);
2708 cts->transport = device->transport;
2709 }
2710
2711 if (cts->transport_version > device->transport_version) {
2712 if (bootverbose) {
2713 xpt_print(path,
2714 "Down reving Transport Version from %d to %d?\n",
2715 cts->transport_version,
2716 device->transport_version);
2717 }
2718 cts->transport_version = device->transport_version;
2719 }
2720
2721 /*
2722 * Nothing more of interest to do unless
2723 * this is a device connected via the
2724 * SCSI protocol.
2725 */
2726 if (cts->protocol != PROTO_SCSI) {
2727 if (async_update == FALSE)
2728 xpt_action_default((union ccb *)cts);
2729 return;
2730 }
2731
2732 inq_data = &device->inq_data;
2733 scsi = &cts->proto_specific.scsi;
2734 memset(&cpi, 0, sizeof(cpi));
2735 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NONE);
2736 cpi.ccb_h.func_code = XPT_PATH_INQ;
2737 xpt_action((union ccb *)&cpi);
2738
2739 /* SCSI specific sanity checking */
2740 if ((cpi.hba_inquiry & PI_TAG_ABLE) == 0
2741 || (INQ_DATA_TQ_ENABLED(inq_data)) == 0
2742 || (device->queue_flags & SCP_QUEUE_DQUE) != 0
2743 || (device->mintags == 0)) {
2744 /*
2745 * Can't tag on hardware that doesn't support tags,
2746 * doesn't have it enabled, or has broken tag support.
2747 */
2748 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2749 }
2750
2751 if (async_update == FALSE) {
2752 /*
2753 * Perform sanity checking against what the
2754 * controller and device can do.
2755 */
2756 memset(&cur_cts, 0, sizeof(cur_cts));
2757 xpt_setup_ccb(&cur_cts.ccb_h, path, CAM_PRIORITY_NONE);
2758 cur_cts.ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
2759 cur_cts.type = cts->type;
2760 xpt_action((union ccb *)&cur_cts);
2761 if (cam_ccb_status((union ccb *)&cur_cts) != CAM_REQ_CMP) {
2762 return;
2763 }
2764 cur_scsi = &cur_cts.proto_specific.scsi;
2765 if ((scsi->valid & CTS_SCSI_VALID_TQ) == 0) {
2766 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2767 scsi->flags |= cur_scsi->flags & CTS_SCSI_FLAGS_TAG_ENB;
2768 }
2769 if ((cur_scsi->valid & CTS_SCSI_VALID_TQ) == 0)
2770 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2771 }
2772
2773 /* SPI specific sanity checking */
2774 if (cts->transport == XPORT_SPI && async_update == FALSE) {
2775 u_int spi3caps;
2776 struct ccb_trans_settings_spi *spi;
2777 struct ccb_trans_settings_spi *cur_spi;
2778
2779 spi = &cts->xport_specific.spi;
2780
2781 cur_spi = &cur_cts.xport_specific.spi;
2782
2783 /* Fill in any gaps in what the user gave us */
2784 if ((spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2785 spi->sync_period = cur_spi->sync_period;
2786 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_RATE) == 0)
2787 spi->sync_period = 0;
2788 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2789 spi->sync_offset = cur_spi->sync_offset;
2790 if ((cur_spi->valid & CTS_SPI_VALID_SYNC_OFFSET) == 0)
2791 spi->sync_offset = 0;
2792 if ((spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2793 spi->ppr_options = cur_spi->ppr_options;
2794 if ((cur_spi->valid & CTS_SPI_VALID_PPR_OPTIONS) == 0)
2795 spi->ppr_options = 0;
2796 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2797 spi->bus_width = cur_spi->bus_width;
2798 if ((cur_spi->valid & CTS_SPI_VALID_BUS_WIDTH) == 0)
2799 spi->bus_width = 0;
2800 if ((spi->valid & CTS_SPI_VALID_DISC) == 0) {
2801 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2802 spi->flags |= cur_spi->flags & CTS_SPI_FLAGS_DISC_ENB;
2803 }
2804 if ((cur_spi->valid & CTS_SPI_VALID_DISC) == 0)
2805 spi->flags &= ~CTS_SPI_FLAGS_DISC_ENB;
2806 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2807 && (inq_data->flags & SID_Sync) == 0
2808 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2809 || ((cpi.hba_inquiry & PI_SDTR_ABLE) == 0)) {
2810 /* Force async */
2811 spi->sync_period = 0;
2812 spi->sync_offset = 0;
2813 }
2814
2815 switch (spi->bus_width) {
2816 case MSG_EXT_WDTR_BUS_32_BIT:
2817 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2818 || (inq_data->flags & SID_WBus32) != 0
2819 || cts->type == CTS_TYPE_USER_SETTINGS)
2820 && (cpi.hba_inquiry & PI_WIDE_32) != 0)
2821 break;
2822 /* Fall Through to 16-bit */
2823 case MSG_EXT_WDTR_BUS_16_BIT:
2824 if (((device->flags & CAM_DEV_INQUIRY_DATA_VALID) == 0
2825 || (inq_data->flags & SID_WBus16) != 0
2826 || cts->type == CTS_TYPE_USER_SETTINGS)
2827 && (cpi.hba_inquiry & PI_WIDE_16) != 0) {
2828 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
2829 break;
2830 }
2831 /* Fall Through to 8-bit */
2832 default: /* New bus width?? */
2833 case MSG_EXT_WDTR_BUS_8_BIT:
2834 /* All targets can do this */
2835 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
2836 break;
2837 }
2838
2839 spi3caps = cpi.xport_specific.spi.ppr_options;
2840 if ((device->flags & CAM_DEV_INQUIRY_DATA_VALID) != 0
2841 && cts->type == CTS_TYPE_CURRENT_SETTINGS)
2842 spi3caps &= inq_data->spi3data;
2843
2844 if ((spi3caps & SID_SPI_CLOCK_DT) == 0)
2845 spi->ppr_options &= ~MSG_EXT_PPR_DT_REQ;
2846
2847 if ((spi3caps & SID_SPI_IUS) == 0)
2848 spi->ppr_options &= ~MSG_EXT_PPR_IU_REQ;
2849
2850 if ((spi3caps & SID_SPI_QAS) == 0)
2851 spi->ppr_options &= ~MSG_EXT_PPR_QAS_REQ;
2852
2853 /* No SPI Transfer settings are allowed unless we are wide */
2854 if (spi->bus_width == 0)
2855 spi->ppr_options = 0;
2856
2857 if ((spi->valid & CTS_SPI_VALID_DISC)
2858 && ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) == 0)) {
2859 /*
2860 * Can't tag queue without disconnection.
2861 */
2862 scsi->flags &= ~CTS_SCSI_FLAGS_TAG_ENB;
2863 scsi->valid |= CTS_SCSI_VALID_TQ;
2864 }
2865
2866 /*
2867 * If we are currently performing tagged transactions to
2868 * this device and want to change its negotiation parameters,
2869 * go non-tagged for a bit to give the controller a chance to
2870 * negotiate unhampered by tag messages.
2871 */
2872 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2873 && (device->inq_flags & SID_CmdQue) != 0
2874 && (scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2875 && (spi->flags & (CTS_SPI_VALID_SYNC_RATE|
2876 CTS_SPI_VALID_SYNC_OFFSET|
2877 CTS_SPI_VALID_BUS_WIDTH)) != 0)
2878 scsi_toggle_tags(path);
2879 }
2880
2881 if (cts->type == CTS_TYPE_CURRENT_SETTINGS
2882 && (scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
2883 int device_tagenb;
2884
2885 /*
2886 * If we are transitioning from tags to no-tags or
2887 * vice-versa, we need to carefully freeze and restart
2888 * the queue so that we don't overlap tagged and non-tagged
2889 * commands. We also temporarily stop tags if there is
2890 * a change in transfer negotiation settings to allow
2891 * "tag-less" negotiation.
2892 */
2893 if ((device->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2894 || (device->inq_flags & SID_CmdQue) != 0)
2895 device_tagenb = TRUE;
2896 else
2897 device_tagenb = FALSE;
2898
2899 if (((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0
2900 && device_tagenb == FALSE)
2901 || ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) == 0
2902 && device_tagenb == TRUE)) {
2903 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0) {
2904 /*
2905 * Delay change to use tags until after a
2906 * few commands have gone to this device so
2907 * the controller has time to perform transfer
2908 * negotiations without tagged messages getting
2909 * in the way.
2910 */
2911 device->tag_delay_count = CAM_TAG_DELAY_COUNT;
2912 device->flags |= CAM_DEV_TAG_AFTER_COUNT;
2913 } else {
2914 xpt_stop_tags(path);
2915 }
2916 }
2917 }
2918 if (async_update == FALSE)
2919 xpt_action_default((union ccb *)cts);
2920 }
2921
2922 static void
scsi_toggle_tags(struct cam_path * path)2923 scsi_toggle_tags(struct cam_path *path)
2924 {
2925 struct cam_ed *dev;
2926
2927 /*
2928 * Give controllers a chance to renegotiate
2929 * before starting tag operations. We
2930 * "toggle" tagged queuing off then on
2931 * which causes the tag enable command delay
2932 * counter to come into effect.
2933 */
2934 dev = path->device;
2935 if ((dev->flags & CAM_DEV_TAG_AFTER_COUNT) != 0
2936 || ((dev->inq_flags & SID_CmdQue) != 0
2937 && (dev->inq_flags & (SID_Sync|SID_WBus16|SID_WBus32)) != 0)) {
2938 struct ccb_trans_settings cts;
2939
2940 memset(&cts, 0, sizeof(cts));
2941 xpt_setup_ccb(&cts.ccb_h, path, CAM_PRIORITY_NONE);
2942 cts.protocol = PROTO_SCSI;
2943 cts.protocol_version = PROTO_VERSION_UNSPECIFIED;
2944 cts.transport = XPORT_UNSPECIFIED;
2945 cts.transport_version = XPORT_VERSION_UNSPECIFIED;
2946 cts.proto_specific.scsi.flags = 0;
2947 cts.proto_specific.scsi.valid = CTS_SCSI_VALID_TQ;
2948 scsi_set_transfer_settings(&cts, path,
2949 /*async_update*/TRUE);
2950 cts.proto_specific.scsi.flags = CTS_SCSI_FLAGS_TAG_ENB;
2951 scsi_set_transfer_settings(&cts, path,
2952 /*async_update*/TRUE);
2953 }
2954 }
2955
2956 /*
2957 * Handle any per-device event notifications that require action by the XPT.
2958 */
2959 static void
scsi_dev_async(uint32_t async_code,struct cam_eb * bus,struct cam_et * target,struct cam_ed * device,void * async_arg)2960 scsi_dev_async(uint32_t async_code, struct cam_eb *bus, struct cam_et *target,
2961 struct cam_ed *device, void *async_arg)
2962 {
2963 cam_status status;
2964 struct cam_path newpath;
2965
2966 /*
2967 * We only need to handle events for real devices.
2968 */
2969 if (target->target_id == CAM_TARGET_WILDCARD
2970 || device->lun_id == CAM_LUN_WILDCARD)
2971 return;
2972
2973 /*
2974 * We need our own path with wildcards expanded to
2975 * handle certain types of events.
2976 */
2977 if ((async_code == AC_SENT_BDR)
2978 || (async_code == AC_BUS_RESET)
2979 || (async_code == AC_INQ_CHANGED))
2980 status = xpt_compile_path(&newpath, NULL,
2981 bus->path_id,
2982 target->target_id,
2983 device->lun_id);
2984 else
2985 status = CAM_REQ_CMP_ERR;
2986
2987 if (status == CAM_REQ_CMP) {
2988 /*
2989 * Allow transfer negotiation to occur in a
2990 * tag free environment and after settle delay.
2991 */
2992 if (async_code == AC_SENT_BDR
2993 || async_code == AC_BUS_RESET) {
2994 cam_freeze_devq(&newpath);
2995 cam_release_devq(&newpath,
2996 RELSIM_RELEASE_AFTER_TIMEOUT,
2997 /*reduction*/0,
2998 /*timeout*/scsi_delay,
2999 /*getcount_only*/0);
3000 scsi_toggle_tags(&newpath);
3001 }
3002
3003 if (async_code == AC_INQ_CHANGED) {
3004 /*
3005 * We've sent a start unit command, or
3006 * something similar to a device that
3007 * may have caused its inquiry data to
3008 * change. So we re-scan the device to
3009 * refresh the inquiry data for it.
3010 */
3011 scsi_scan_lun(newpath.periph, &newpath,
3012 CAM_EXPECT_INQ_CHANGE, NULL);
3013 }
3014 xpt_release_path(&newpath);
3015 } else if (async_code == AC_LOST_DEVICE &&
3016 (device->flags & CAM_DEV_UNCONFIGURED) == 0) {
3017 device->flags |= CAM_DEV_UNCONFIGURED;
3018 xpt_release_device(device);
3019 } else if (async_code == AC_TRANSFER_NEG) {
3020 struct ccb_trans_settings *settings;
3021 struct cam_path path;
3022
3023 settings = (struct ccb_trans_settings *)async_arg;
3024 xpt_compile_path(&path, NULL, bus->path_id, target->target_id,
3025 device->lun_id);
3026 scsi_set_transfer_settings(settings, &path,
3027 /*async_update*/TRUE);
3028 xpt_release_path(&path);
3029 }
3030 }
3031
3032 static void
_scsi_announce_periph(struct cam_periph * periph,u_int * speed,u_int * freq,struct ccb_trans_settings * cts)3033 _scsi_announce_periph(struct cam_periph *periph, u_int *speed, u_int *freq, struct ccb_trans_settings *cts)
3034 {
3035 struct ccb_pathinq cpi;
3036 struct cam_path *path = periph->path;
3037
3038 cam_periph_assert(periph, MA_OWNED);
3039
3040 xpt_setup_ccb(&cts->ccb_h, path, CAM_PRIORITY_NORMAL);
3041 cts->ccb_h.func_code = XPT_GET_TRAN_SETTINGS;
3042 cts->type = CTS_TYPE_CURRENT_SETTINGS;
3043 xpt_action((union ccb*)cts);
3044 if (cam_ccb_status((union ccb *)cts) != CAM_REQ_CMP)
3045 return;
3046
3047 /* Ask the SIM for its base transfer speed */
3048 memset(&cpi, 0, sizeof(cpi));
3049 xpt_setup_ccb(&cpi.ccb_h, path, CAM_PRIORITY_NORMAL);
3050 cpi.ccb_h.func_code = XPT_PATH_INQ;
3051 xpt_action((union ccb *)&cpi);
3052
3053 /* Report connection speed */
3054 *speed = cpi.base_transfer_speed;
3055 *freq = 0;
3056
3057 if (cts->ccb_h.status == CAM_REQ_CMP && cts->transport == XPORT_SPI) {
3058 struct ccb_trans_settings_spi *spi =
3059 &cts->xport_specific.spi;
3060
3061 if ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0
3062 && spi->sync_offset != 0) {
3063 *freq = scsi_calc_syncsrate(spi->sync_period);
3064 *speed = *freq;
3065 }
3066 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0)
3067 *speed *= (0x01 << spi->bus_width);
3068 }
3069 if (cts->ccb_h.status == CAM_REQ_CMP && cts->transport == XPORT_FC) {
3070 struct ccb_trans_settings_fc *fc =
3071 &cts->xport_specific.fc;
3072
3073 if (fc->valid & CTS_FC_VALID_SPEED)
3074 *speed = fc->bitrate;
3075 }
3076 if (cts->ccb_h.status == CAM_REQ_CMP && cts->transport == XPORT_SAS) {
3077 struct ccb_trans_settings_sas *sas =
3078 &cts->xport_specific.sas;
3079
3080 if (sas->valid & CTS_SAS_VALID_SPEED)
3081 *speed = sas->bitrate;
3082 }
3083 }
3084
3085 static void
scsi_announce_periph_sbuf(struct cam_periph * periph,struct sbuf * sb)3086 scsi_announce_periph_sbuf(struct cam_periph *periph, struct sbuf *sb)
3087 {
3088 struct ccb_trans_settings cts;
3089 u_int speed, freq, mb;
3090
3091 memset(&cts, 0, sizeof(cts));
3092 _scsi_announce_periph(periph, &speed, &freq, &cts);
3093 if (cam_ccb_status((union ccb *)&cts) != CAM_REQ_CMP)
3094 return;
3095
3096 mb = speed / 1000;
3097 if (mb > 0)
3098 sbuf_printf(sb, "%s%d: %d.%03dMB/s transfers",
3099 periph->periph_name, periph->unit_number,
3100 mb, speed % 1000);
3101 else
3102 sbuf_printf(sb, "%s%d: %dKB/s transfers", periph->periph_name,
3103 periph->unit_number, speed);
3104 /* Report additional information about SPI connections */
3105 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_SPI) {
3106 struct ccb_trans_settings_spi *spi;
3107
3108 spi = &cts.xport_specific.spi;
3109 if (freq != 0) {
3110 sbuf_printf(sb, " (%d.%03dMHz%s, offset %d", freq / 1000,
3111 freq % 1000,
3112 (spi->ppr_options & MSG_EXT_PPR_DT_REQ) != 0
3113 ? " DT" : "",
3114 spi->sync_offset);
3115 }
3116 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0
3117 && spi->bus_width > 0) {
3118 if (freq != 0) {
3119 sbuf_cat(sb, ", ");
3120 } else {
3121 sbuf_cat(sb, " (");
3122 }
3123 sbuf_printf(sb, "%dbit)", 8 * (0x01 << spi->bus_width));
3124 } else if (freq != 0) {
3125 sbuf_putc(sb, ')');
3126 }
3127 }
3128 if (cts.ccb_h.status == CAM_REQ_CMP && cts.transport == XPORT_FC) {
3129 struct ccb_trans_settings_fc *fc;
3130
3131 fc = &cts.xport_specific.fc;
3132 if (fc->valid & CTS_FC_VALID_WWNN)
3133 sbuf_printf(sb, " WWNN 0x%llx", (long long) fc->wwnn);
3134 if (fc->valid & CTS_FC_VALID_WWPN)
3135 sbuf_printf(sb, " WWPN 0x%llx", (long long) fc->wwpn);
3136 if (fc->valid & CTS_FC_VALID_PORT)
3137 sbuf_printf(sb, " PortID 0x%x", fc->port);
3138 }
3139 sbuf_putc(sb, '\n');
3140 }
3141
3142 static void
scsi_proto_announce_sbuf(struct cam_ed * device,struct sbuf * sb)3143 scsi_proto_announce_sbuf(struct cam_ed *device, struct sbuf *sb)
3144 {
3145 scsi_print_inquiry_sbuf(sb, &device->inq_data);
3146 }
3147
3148 static void
scsi_proto_denounce_sbuf(struct cam_ed * device,struct sbuf * sb)3149 scsi_proto_denounce_sbuf(struct cam_ed *device, struct sbuf *sb)
3150 {
3151 scsi_print_inquiry_short_sbuf(sb, &device->inq_data);
3152 }
3153
3154 static void
scsi_proto_debug_out(union ccb * ccb)3155 scsi_proto_debug_out(union ccb *ccb)
3156 {
3157 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
3158 struct cam_ed *device;
3159
3160 if (ccb->ccb_h.func_code != XPT_SCSI_IO)
3161 return;
3162
3163 device = ccb->ccb_h.path->device;
3164 CAM_DEBUG(ccb->ccb_h.path,
3165 CAM_DEBUG_CDB,("%s. CDB: %s\n",
3166 scsi_op_desc(scsiio_cdb_ptr(&ccb->csio)[0], &device->inq_data),
3167 scsi_cdb_string(scsiio_cdb_ptr(&ccb->csio), cdb_str, sizeof(cdb_str))));
3168 }
3169