FreeBSD/Linux Kernel Cross Reference
sys/cam/scsi/scsi_da.c
1 /*-
2 * Implementation of SCSI Direct Access Peripheral driver for CAM.
3 *
4 * Copyright (c) 1997 Justin T. Gibbs.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: releng/8.3/sys/cam/scsi/scsi_da.c 231523 2012-02-11 14:58:46Z avg $");
31
32 #include <sys/param.h>
33
34 #ifdef _KERNEL
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/bio.h>
38 #include <sys/sysctl.h>
39 #include <sys/taskqueue.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/conf.h>
43 #include <sys/devicestat.h>
44 #include <sys/eventhandler.h>
45 #include <sys/malloc.h>
46 #include <sys/cons.h>
47 #include <geom/geom_disk.h>
48 #endif /* _KERNEL */
49
50 #ifndef _KERNEL
51 #include <stdio.h>
52 #include <string.h>
53 #endif /* _KERNEL */
54
55 #include <cam/cam.h>
56 #include <cam/cam_ccb.h>
57 #include <cam/cam_periph.h>
58 #include <cam/cam_xpt_periph.h>
59 #include <cam/cam_sim.h>
60
61 #include <cam/scsi/scsi_message.h>
62
63 #ifndef _KERNEL
64 #include <cam/scsi/scsi_da.h>
65 #endif /* !_KERNEL */
66
67 #ifdef _KERNEL
68 typedef enum {
69 DA_STATE_PROBE,
70 DA_STATE_PROBE2,
71 DA_STATE_NORMAL
72 } da_state;
73
74 typedef enum {
75 DA_FLAG_PACK_INVALID = 0x001,
76 DA_FLAG_NEW_PACK = 0x002,
77 DA_FLAG_PACK_LOCKED = 0x004,
78 DA_FLAG_PACK_REMOVABLE = 0x008,
79 DA_FLAG_TAGGED_QUEUING = 0x010,
80 DA_FLAG_NEED_OTAG = 0x020,
81 DA_FLAG_WENT_IDLE = 0x040,
82 DA_FLAG_RETRY_UA = 0x080,
83 DA_FLAG_OPEN = 0x100,
84 DA_FLAG_SCTX_INIT = 0x200
85 } da_flags;
86
87 typedef enum {
88 DA_Q_NONE = 0x00,
89 DA_Q_NO_SYNC_CACHE = 0x01,
90 DA_Q_NO_6_BYTE = 0x02,
91 DA_Q_NO_PREVENT = 0x04,
92 DA_Q_4K = 0x08
93 } da_quirks;
94
95 typedef enum {
96 DA_CCB_PROBE = 0x01,
97 DA_CCB_PROBE2 = 0x02,
98 DA_CCB_BUFFER_IO = 0x03,
99 DA_CCB_WAITING = 0x04,
100 DA_CCB_DUMP = 0x05,
101 DA_CCB_TYPE_MASK = 0x0F,
102 DA_CCB_RETRY_UA = 0x10
103 } da_ccb_state;
104
105 /* Offsets into our private area for storing information */
106 #define ccb_state ppriv_field0
107 #define ccb_bp ppriv_ptr1
108
109 struct disk_params {
110 u_int8_t heads;
111 u_int32_t cylinders;
112 u_int8_t secs_per_track;
113 u_int32_t secsize; /* Number of bytes/sector */
114 u_int64_t sectors; /* total number sectors */
115 u_int stripesize;
116 u_int stripeoffset;
117 };
118
119 struct da_softc {
120 struct bio_queue_head bio_queue;
121 SLIST_ENTRY(da_softc) links;
122 LIST_HEAD(, ccb_hdr) pending_ccbs;
123 da_state state;
124 da_flags flags;
125 da_quirks quirks;
126 int minimum_cmd_size;
127 int error_inject;
128 int ordered_tag_count;
129 int outstanding_cmds;
130 struct disk_params params;
131 struct disk *disk;
132 union ccb saved_ccb;
133 struct task sysctl_task;
134 struct sysctl_ctx_list sysctl_ctx;
135 struct sysctl_oid *sysctl_tree;
136 struct callout sendordered_c;
137 };
138
139 struct da_quirk_entry {
140 struct scsi_inquiry_pattern inq_pat;
141 da_quirks quirks;
142 };
143
144 static const char quantum[] = "QUANTUM";
145 static const char microp[] = "MICROP";
146
147 static struct da_quirk_entry da_quirk_table[] =
148 {
149 /* SPI, FC devices */
150 {
151 /*
152 * Fujitsu M2513A MO drives.
153 * Tested devices: M2513A2 firmware versions 1200 & 1300.
154 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
155 * Reported by: W.Scholten <whs@xs4all.nl>
156 */
157 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
158 /*quirks*/ DA_Q_NO_SYNC_CACHE
159 },
160 {
161 /* See above. */
162 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
163 /*quirks*/ DA_Q_NO_SYNC_CACHE
164 },
165 {
166 /*
167 * This particular Fujitsu drive doesn't like the
168 * synchronize cache command.
169 * Reported by: Tom Jackson <toj@gorilla.net>
170 */
171 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
172 /*quirks*/ DA_Q_NO_SYNC_CACHE
173 },
174 {
175 /*
176 * This drive doesn't like the synchronize cache command
177 * either. Reported by: Matthew Jacob <mjacob@feral.com>
178 * in NetBSD PR kern/6027, August 24, 1998.
179 */
180 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
181 /*quirks*/ DA_Q_NO_SYNC_CACHE
182 },
183 {
184 /*
185 * This drive doesn't like the synchronize cache command
186 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
187 * (PR 8882).
188 */
189 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
190 /*quirks*/ DA_Q_NO_SYNC_CACHE
191 },
192 {
193 /*
194 * Doesn't like the synchronize cache command.
195 * Reported by: Blaz Zupan <blaz@gold.amis.net>
196 */
197 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
198 /*quirks*/ DA_Q_NO_SYNC_CACHE
199 },
200 {
201 /*
202 * Doesn't like the synchronize cache command.
203 * Reported by: Blaz Zupan <blaz@gold.amis.net>
204 */
205 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
206 /*quirks*/ DA_Q_NO_SYNC_CACHE
207 },
208 {
209 /*
210 * Doesn't like the synchronize cache command.
211 */
212 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
213 /*quirks*/ DA_Q_NO_SYNC_CACHE
214 },
215 {
216 /*
217 * Doesn't like the synchronize cache command.
218 * Reported by: walter@pelissero.de
219 */
220 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS540S", "*"},
221 /*quirks*/ DA_Q_NO_SYNC_CACHE
222 },
223 {
224 /*
225 * Doesn't work correctly with 6 byte reads/writes.
226 * Returns illegal request, and points to byte 9 of the
227 * 6-byte CDB.
228 * Reported by: Adam McDougall <bsdx@spawnet.com>
229 */
230 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
231 /*quirks*/ DA_Q_NO_6_BYTE
232 },
233 {
234 /* See above. */
235 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
236 /*quirks*/ DA_Q_NO_6_BYTE
237 },
238 {
239 /*
240 * Doesn't like the synchronize cache command.
241 * Reported by: walter@pelissero.de
242 */
243 {T_DIRECT, SIP_MEDIA_FIXED, "CONNER", "CP3500*", "*"},
244 /*quirks*/ DA_Q_NO_SYNC_CACHE
245 },
246 {
247 /*
248 * The CISS RAID controllers do not support SYNC_CACHE
249 */
250 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
251 /*quirks*/ DA_Q_NO_SYNC_CACHE
252 },
253 /* USB mass storage devices supported by umass(4) */
254 {
255 /*
256 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
257 * PR: kern/51675
258 */
259 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
260 /*quirks*/ DA_Q_NO_SYNC_CACHE
261 },
262 {
263 /*
264 * Power Quotient Int. (PQI) USB flash key
265 * PR: kern/53067
266 */
267 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
268 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
269 },
270 {
271 /*
272 * Creative Nomad MUVO mp3 player (USB)
273 * PR: kern/53094
274 */
275 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
276 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
277 },
278 {
279 /*
280 * Jungsoft NEXDISK USB flash key
281 * PR: kern/54737
282 */
283 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
284 /*quirks*/ DA_Q_NO_SYNC_CACHE
285 },
286 {
287 /*
288 * FreeDik USB Mini Data Drive
289 * PR: kern/54786
290 */
291 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
292 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
293 },
294 {
295 /*
296 * Sigmatel USB Flash MP3 Player
297 * PR: kern/57046
298 */
299 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
300 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
301 },
302 {
303 /*
304 * Neuros USB Digital Audio Computer
305 * PR: kern/63645
306 */
307 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
308 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
309 },
310 {
311 /*
312 * SEAGRAND NP-900 MP3 Player
313 * PR: kern/64563
314 */
315 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
316 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
317 },
318 {
319 /*
320 * iRiver iFP MP3 player (with UMS Firmware)
321 * PR: kern/54881, i386/63941, kern/66124
322 */
323 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
324 /*quirks*/ DA_Q_NO_SYNC_CACHE
325 },
326 {
327 /*
328 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
329 * PR: kern/70158
330 */
331 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "Nex*", "*"},
332 /*quirks*/ DA_Q_NO_SYNC_CACHE
333 },
334 {
335 /*
336 * ZICPlay USB MP3 Player with FM
337 * PR: kern/75057
338 */
339 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ACTIONS*" , "USB DISK*", "*"},
340 /*quirks*/ DA_Q_NO_SYNC_CACHE
341 },
342 {
343 /*
344 * TEAC USB floppy mechanisms
345 */
346 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TEAC" , "FD-05*", "*"},
347 /*quirks*/ DA_Q_NO_SYNC_CACHE
348 },
349 {
350 /*
351 * Kingston DataTraveler II+ USB Pen-Drive.
352 * Reported by: Pawel Jakub Dawidek <pjd@FreeBSD.org>
353 */
354 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston" , "DataTraveler II+",
355 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
356 },
357 {
358 /*
359 * Motorola E398 Mobile Phone (TransFlash memory card).
360 * Reported by: Wojciech A. Koszek <dunstan@FreeBSD.czest.pl>
361 * PR: usb/89889
362 */
363 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Motorola" , "Motorola Phone",
364 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
365 },
366 {
367 /*
368 * Qware BeatZkey! Pro
369 * PR: usb/79164
370 */
371 {T_DIRECT, SIP_MEDIA_REMOVABLE, "GENERIC", "USB DISK DEVICE",
372 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
373 },
374 {
375 /*
376 * Time DPA20B 1GB MP3 Player
377 * PR: usb/81846
378 */
379 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB2.0*", "(FS) FLASH DISK*",
380 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
381 },
382 {
383 /*
384 * Samsung USB key 128Mb
385 * PR: usb/90081
386 */
387 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB-DISK", "FreeDik-FlashUsb",
388 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
389 },
390 {
391 /*
392 * Kingston DataTraveler 2.0 USB Flash memory.
393 * PR: usb/89196
394 */
395 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Kingston", "DataTraveler 2.0",
396 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
397 },
398 {
399 /*
400 * Creative MUVO Slim mp3 player (USB)
401 * PR: usb/86131
402 */
403 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "MuVo Slim",
404 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
405 },
406 {
407 /*
408 * United MP5512 Portable MP3 Player (2-in-1 USB DISK/MP3)
409 * PR: usb/80487
410 */
411 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "MUSIC DISK",
412 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
413 },
414 {
415 /*
416 * SanDisk Micro Cruzer 128MB
417 * PR: usb/75970
418 */
419 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SanDisk" , "Micro Cruzer",
420 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
421 },
422 {
423 /*
424 * TOSHIBA TransMemory USB sticks
425 * PR: kern/94660
426 */
427 {T_DIRECT, SIP_MEDIA_REMOVABLE, "TOSHIBA", "TransMemory",
428 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
429 },
430 {
431 /*
432 * PNY USB Flash keys
433 * PR: usb/75578, usb/72344, usb/65436
434 */
435 {T_DIRECT, SIP_MEDIA_REMOVABLE, "*" , "USB DISK*",
436 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
437 },
438 {
439 /*
440 * Genesys 6-in-1 Card Reader
441 * PR: usb/94647
442 */
443 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "STORAGE DEVICE*",
444 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
445 },
446 {
447 /*
448 * Rekam Digital CAMERA
449 * PR: usb/98713
450 */
451 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CAMERA*", "4MP-9J6*",
452 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
453 },
454 {
455 /*
456 * iRiver H10 MP3 player
457 * PR: usb/102547
458 */
459 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "H10*",
460 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
461 },
462 {
463 /*
464 * iRiver U10 MP3 player
465 * PR: usb/92306
466 */
467 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iriver", "U10*",
468 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
469 },
470 {
471 /*
472 * X-Micro Flash Disk
473 * PR: usb/96901
474 */
475 {T_DIRECT, SIP_MEDIA_REMOVABLE, "X-Micro", "Flash Disk",
476 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
477 },
478 {
479 /*
480 * EasyMP3 EM732X USB 2.0 Flash MP3 Player
481 * PR: usb/96546
482 */
483 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EM732X", "MP3 Player*",
484 "1.00"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
485 },
486 {
487 /*
488 * Denver MP3 player
489 * PR: usb/107101
490 */
491 {T_DIRECT, SIP_MEDIA_REMOVABLE, "DENVER", "MP3 PLAYER",
492 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
493 },
494 {
495 /*
496 * Philips USB Key Audio KEY013
497 * PR: usb/68412
498 */
499 {T_DIRECT, SIP_MEDIA_REMOVABLE, "PHILIPS", "Key*", "*"},
500 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
501 },
502 {
503 /*
504 * JNC MP3 Player
505 * PR: usb/94439
506 */
507 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JNC*" , "MP3 Player*",
508 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
509 },
510 {
511 /*
512 * SAMSUNG MP0402H
513 * PR: usb/108427
514 */
515 {T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "MP0402H", "*"},
516 /*quirks*/ DA_Q_NO_SYNC_CACHE
517 },
518 {
519 /*
520 * I/O Magic USB flash - Giga Bank
521 * PR: usb/108810
522 */
523 {T_DIRECT, SIP_MEDIA_FIXED, "GS-Magic", "stor*", "*"},
524 /*quirks*/ DA_Q_NO_SYNC_CACHE
525 },
526 {
527 /*
528 * JoyFly 128mb USB Flash Drive
529 * PR: 96133
530 */
531 {T_DIRECT, SIP_MEDIA_REMOVABLE, "USB 2.0", "Flash Disk*",
532 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
533 },
534 {
535 /*
536 * ChipsBnk usb stick
537 * PR: 103702
538 */
539 {T_DIRECT, SIP_MEDIA_REMOVABLE, "ChipsBnk", "USB*",
540 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
541 },
542 {
543 /*
544 * Storcase (Kingston) InfoStation IFS FC2/SATA-R 201A
545 * PR: 129858
546 */
547 {T_DIRECT, SIP_MEDIA_FIXED, "IFS", "FC2/SATA-R*",
548 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
549 },
550 {
551 /*
552 * Samsung YP-U3 mp3-player
553 * PR: 125398
554 */
555 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Samsung", "YP-U3",
556 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
557 },
558 {
559 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Netac", "OnlyDisk*",
560 "2000"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
561 },
562 {
563 /*
564 * Sony Cyber-Shot DSC cameras
565 * PR: usb/137035
566 */
567 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Sony", "Sony DSC", "*"},
568 /*quirks*/ DA_Q_NO_SYNC_CACHE | DA_Q_NO_PREVENT
569 },
570 /* ATA/SATA devices over SAS/USB/... */
571 {
572 /* Hitachi Advanced Format (4k) drives */
573 { T_DIRECT, SIP_MEDIA_FIXED, "Hitachi", "H??????????E3*", "*" },
574 /*quirks*/DA_Q_4K
575 },
576 {
577 /* Samsung Advanced Format (4k) drives */
578 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD155UI*", "*" },
579 /*quirks*/DA_Q_4K
580 },
581 {
582 /* Samsung Advanced Format (4k) drives */
583 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD155UI*", "*" },
584 /*quirks*/DA_Q_4K
585 },
586 {
587 /* Samsung Advanced Format (4k) drives */
588 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "SAMSUNG HD204UI*", "*" },
589 /*quirks*/DA_Q_4K
590 },
591 {
592 /* Samsung Advanced Format (4k) drives */
593 { T_DIRECT, SIP_MEDIA_FIXED, "SAMSUNG", "HD204UI*", "*" },
594 /*quirks*/DA_Q_4K
595 },
596 {
597 /* Seagate Barracuda Green Advanced Format (4k) drives */
598 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DL*", "*" },
599 /*quirks*/DA_Q_4K
600 },
601 {
602 /* Seagate Barracuda Green Advanced Format (4k) drives */
603 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DL", "*", "*" },
604 /*quirks*/DA_Q_4K
605 },
606 {
607 /* Seagate Barracuda Green Advanced Format (4k) drives */
608 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???DM*", "*" },
609 /*quirks*/DA_Q_4K
610 },
611 {
612 /* Seagate Barracuda Green Advanced Format (4k) drives */
613 { T_DIRECT, SIP_MEDIA_FIXED, "ST???DM*", "*", "*" },
614 /*quirks*/DA_Q_4K
615 },
616 {
617 /* Seagate Barracuda Green Advanced Format (4k) drives */
618 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST????DM*", "*" },
619 /*quirks*/DA_Q_4K
620 },
621 {
622 /* Seagate Barracuda Green Advanced Format (4k) drives */
623 { T_DIRECT, SIP_MEDIA_FIXED, "ST????DM", "*", "*" },
624 /*quirks*/DA_Q_4K
625 },
626 {
627 /* Seagate Momentus Advanced Format (4k) drives */
628 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500423AS*", "*" },
629 /*quirks*/DA_Q_4K
630 },
631 {
632 /* Seagate Momentus Advanced Format (4k) drives */
633 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "3AS*", "*" },
634 /*quirks*/DA_Q_4K
635 },
636 {
637 /* Seagate Momentus Advanced Format (4k) drives */
638 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9500424AS*", "*" },
639 /*quirks*/DA_Q_4K
640 },
641 {
642 /* Seagate Momentus Advanced Format (4k) drives */
643 { T_DIRECT, SIP_MEDIA_FIXED, "ST950042", "4AS*", "*" },
644 /*quirks*/DA_Q_4K
645 },
646 {
647 /* Seagate Momentus Advanced Format (4k) drives */
648 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640423AS*", "*" },
649 /*quirks*/DA_Q_4K
650 },
651 {
652 /* Seagate Momentus Advanced Format (4k) drives */
653 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "3AS*", "*" },
654 /*quirks*/DA_Q_4K
655 },
656 {
657 /* Seagate Momentus Advanced Format (4k) drives */
658 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9640424AS*", "*" },
659 /*quirks*/DA_Q_4K
660 },
661 {
662 /* Seagate Momentus Advanced Format (4k) drives */
663 { T_DIRECT, SIP_MEDIA_FIXED, "ST964042", "4AS*", "*" },
664 /*quirks*/DA_Q_4K
665 },
666 {
667 /* Seagate Momentus Advanced Format (4k) drives */
668 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750420AS*", "*" },
669 /*quirks*/DA_Q_4K
670 },
671 {
672 /* Seagate Momentus Advanced Format (4k) drives */
673 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "0AS*", "*" },
674 /*quirks*/DA_Q_4K
675 },
676 {
677 /* Seagate Momentus Advanced Format (4k) drives */
678 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750422AS*", "*" },
679 /*quirks*/DA_Q_4K
680 },
681 {
682 /* Seagate Momentus Advanced Format (4k) drives */
683 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "2AS*", "*" },
684 /*quirks*/DA_Q_4K
685 },
686 {
687 /* Seagate Momentus Advanced Format (4k) drives */
688 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST9750423AS*", "*" },
689 /*quirks*/DA_Q_4K
690 },
691 {
692 /* Seagate Momentus Advanced Format (4k) drives */
693 { T_DIRECT, SIP_MEDIA_FIXED, "ST975042", "3AS*", "*" },
694 /*quirks*/DA_Q_4K
695 },
696 {
697 /* Seagate Momentus Thin Advanced Format (4k) drives */
698 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "ST???LT*", "*" },
699 /*quirks*/DA_Q_4K
700 },
701 {
702 /* Seagate Momentus Thin Advanced Format (4k) drives */
703 { T_DIRECT, SIP_MEDIA_FIXED, "ST???LT*", "*", "*" },
704 /*quirks*/DA_Q_4K
705 },
706 {
707 /* WDC Caviar Green Advanced Format (4k) drives */
708 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RS*", "*" },
709 /*quirks*/DA_Q_4K
710 },
711 {
712 /* WDC Caviar Green Advanced Format (4k) drives */
713 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RS*", "*" },
714 /*quirks*/DA_Q_4K
715 },
716 {
717 /* WDC Caviar Green Advanced Format (4k) drives */
718 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD????RX*", "*" },
719 /*quirks*/DA_Q_4K
720 },
721 {
722 /* WDC Caviar Green Advanced Format (4k) drives */
723 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "??RX*", "*" },
724 /*quirks*/DA_Q_4K
725 },
726 {
727 /* WDC Caviar Green Advanced Format (4k) drives */
728 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RS*", "*" },
729 /*quirks*/DA_Q_4K
730 },
731 {
732 /* WDC Caviar Green Advanced Format (4k) drives */
733 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RS*", "*" },
734 /*quirks*/DA_Q_4K
735 },
736 {
737 /* WDC Caviar Green Advanced Format (4k) drives */
738 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD??????RX*", "*" },
739 /*quirks*/DA_Q_4K
740 },
741 {
742 /* WDC Caviar Green Advanced Format (4k) drives */
743 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "????RX*", "*" },
744 /*quirks*/DA_Q_4K
745 },
746 {
747 /* WDC Scorpio Black Advanced Format (4k) drives */
748 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PKT*", "*" },
749 /*quirks*/DA_Q_4K
750 },
751 {
752 /* WDC Scorpio Black Advanced Format (4k) drives */
753 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PKT*", "*" },
754 /*quirks*/DA_Q_4K
755 },
756 {
757 /* WDC Scorpio Black Advanced Format (4k) drives */
758 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PKT*", "*" },
759 /*quirks*/DA_Q_4K
760 },
761 {
762 /* WDC Scorpio Black Advanced Format (4k) drives */
763 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PKT*", "*" },
764 /*quirks*/DA_Q_4K
765 },
766 {
767 /* WDC Scorpio Blue Advanced Format (4k) drives */
768 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD???PVT*", "*" },
769 /*quirks*/DA_Q_4K
770 },
771 {
772 /* WDC Scorpio Blue Advanced Format (4k) drives */
773 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "?PVT*", "*" },
774 /*quirks*/DA_Q_4K
775 },
776 {
777 /* WDC Scorpio Blue Advanced Format (4k) drives */
778 { T_DIRECT, SIP_MEDIA_FIXED, "ATA", "WDC WD?????PVT*", "*" },
779 /*quirks*/DA_Q_4K
780 },
781 {
782 /* WDC Scorpio Blue Advanced Format (4k) drives */
783 { T_DIRECT, SIP_MEDIA_FIXED, "WDC WD??", "???PVT*", "*" },
784 /*quirks*/DA_Q_4K
785 },
786 };
787
788 static disk_strategy_t dastrategy;
789 static dumper_t dadump;
790 static periph_init_t dainit;
791 static void daasync(void *callback_arg, u_int32_t code,
792 struct cam_path *path, void *arg);
793 static void dasysctlinit(void *context, int pending);
794 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
795 static periph_ctor_t daregister;
796 static periph_dtor_t dacleanup;
797 static periph_start_t dastart;
798 static periph_oninv_t daoninvalidate;
799 static void dadone(struct cam_periph *periph,
800 union ccb *done_ccb);
801 static int daerror(union ccb *ccb, u_int32_t cam_flags,
802 u_int32_t sense_flags);
803 static void daprevent(struct cam_periph *periph, int action);
804 static int dagetcapacity(struct cam_periph *periph);
805 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
806 uint64_t maxsector, u_int lbppbe, u_int lalba);
807 static timeout_t dasendorderedtag;
808 static void dashutdown(void *arg, int howto);
809
810 #ifndef DA_DEFAULT_TIMEOUT
811 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
812 #endif
813
814 #ifndef DA_DEFAULT_RETRY
815 #define DA_DEFAULT_RETRY 4
816 #endif
817
818 #ifndef DA_DEFAULT_SEND_ORDERED
819 #define DA_DEFAULT_SEND_ORDERED 1
820 #endif
821
822
823 static int da_retry_count = DA_DEFAULT_RETRY;
824 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
825 static int da_send_ordered = DA_DEFAULT_SEND_ORDERED;
826
827 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
828 "CAM Direct Access Disk driver");
829 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
830 &da_retry_count, 0, "Normal I/O retry count");
831 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
832 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
833 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
834 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
835 SYSCTL_INT(_kern_cam_da, OID_AUTO, da_send_ordered, CTLFLAG_RW,
836 &da_send_ordered, 0, "Send Ordered Tags");
837 TUNABLE_INT("kern.cam.da.da_send_ordered", &da_send_ordered);
838
839 /*
840 * DA_ORDEREDTAG_INTERVAL determines how often, relative
841 * to the default timeout, we check to see whether an ordered
842 * tagged transaction is appropriate to prevent simple tag
843 * starvation. Since we'd like to ensure that there is at least
844 * 1/2 of the timeout length left for a starved transaction to
845 * complete after we've sent an ordered tag, we must poll at least
846 * four times in every timeout period. This takes care of the worst
847 * case where a starved transaction starts during an interval that
848 * meets the requirement "don't send an ordered tag" test so it takes
849 * us two intervals to determine that a tag must be sent.
850 */
851 #ifndef DA_ORDEREDTAG_INTERVAL
852 #define DA_ORDEREDTAG_INTERVAL 4
853 #endif
854
855 static struct periph_driver dadriver =
856 {
857 dainit, "da",
858 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
859 };
860
861 PERIPHDRIVER_DECLARE(da, dadriver);
862
863 MALLOC_DEFINE(M_SCSIDA, "scsi_da", "scsi_da buffers");
864
865 static int
866 daopen(struct disk *dp)
867 {
868 struct cam_periph *periph;
869 struct da_softc *softc;
870 int unit;
871 int error;
872
873 periph = (struct cam_periph *)dp->d_drv1;
874 if (periph == NULL) {
875 return (ENXIO);
876 }
877
878 if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
879 return (ENXIO);
880 }
881
882 cam_periph_lock(periph);
883 if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
884 cam_periph_unlock(periph);
885 cam_periph_release(periph);
886 return (error);
887 }
888
889 unit = periph->unit_number;
890 softc = (struct da_softc *)periph->softc;
891 softc->flags |= DA_FLAG_OPEN;
892
893 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
894 ("daopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
895 unit));
896
897 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
898 /* Invalidate our pack information. */
899 softc->flags &= ~DA_FLAG_PACK_INVALID;
900 }
901
902 error = dagetcapacity(periph);
903
904 if (error == 0) {
905
906 softc->disk->d_sectorsize = softc->params.secsize;
907 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
908 softc->disk->d_stripesize = softc->params.stripesize;
909 softc->disk->d_stripeoffset = softc->params.stripeoffset;
910 /* XXX: these are not actually "firmware" values, so they may be wrong */
911 softc->disk->d_fwsectors = softc->params.secs_per_track;
912 softc->disk->d_fwheads = softc->params.heads;
913 softc->disk->d_devstat->block_size = softc->params.secsize;
914 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
915
916 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
917 (softc->quirks & DA_Q_NO_PREVENT) == 0)
918 daprevent(periph, PR_PREVENT);
919 } else
920 softc->flags &= ~DA_FLAG_OPEN;
921
922 cam_periph_unhold(periph);
923 cam_periph_unlock(periph);
924
925 if (error != 0) {
926 cam_periph_release(periph);
927 }
928 return (error);
929 }
930
931 static int
932 daclose(struct disk *dp)
933 {
934 struct cam_periph *periph;
935 struct da_softc *softc;
936 int error;
937
938 periph = (struct cam_periph *)dp->d_drv1;
939 if (periph == NULL)
940 return (0);
941
942 cam_periph_lock(periph);
943 if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
944 cam_periph_unlock(periph);
945 cam_periph_release(periph);
946 return (0);
947 }
948
949 softc = (struct da_softc *)periph->softc;
950
951 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
952 union ccb *ccb;
953
954 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
955
956 scsi_synchronize_cache(&ccb->csio,
957 /*retries*/1,
958 /*cbfcnp*/dadone,
959 MSG_SIMPLE_Q_TAG,
960 /*begin_lba*/0,/* Cover the whole disk */
961 /*lb_count*/0,
962 SSD_FULL_SIZE,
963 5 * 60 * 1000);
964
965 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
966 /*sense_flags*/SF_RETRY_UA,
967 softc->disk->d_devstat);
968
969 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
970 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
971 CAM_SCSI_STATUS_ERROR) {
972 int asc, ascq;
973 int sense_key, error_code;
974
975 scsi_extract_sense(&ccb->csio.sense_data,
976 &error_code,
977 &sense_key,
978 &asc, &ascq);
979 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
980 scsi_sense_print(&ccb->csio);
981 } else {
982 xpt_print(periph->path, "Synchronize cache "
983 "failed, status == 0x%x, scsi status == "
984 "0x%x\n", ccb->csio.ccb_h.status,
985 ccb->csio.scsi_status);
986 }
987 }
988
989 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
990 cam_release_devq(ccb->ccb_h.path,
991 /*relsim_flags*/0,
992 /*reduction*/0,
993 /*timeout*/0,
994 /*getcount_only*/0);
995
996 xpt_release_ccb(ccb);
997
998 }
999
1000 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
1001 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
1002 daprevent(periph, PR_ALLOW);
1003 /*
1004 * If we've got removeable media, mark the blocksize as
1005 * unavailable, since it could change when new media is
1006 * inserted.
1007 */
1008 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
1009 }
1010
1011 softc->flags &= ~DA_FLAG_OPEN;
1012 cam_periph_unhold(periph);
1013 cam_periph_unlock(periph);
1014 cam_periph_release(periph);
1015 return (0);
1016 }
1017
1018 /*
1019 * Actually translate the requested transfer into one the physical driver
1020 * can understand. The transfer is described by a buf and will include
1021 * only one physical transfer.
1022 */
1023 static void
1024 dastrategy(struct bio *bp)
1025 {
1026 struct cam_periph *periph;
1027 struct da_softc *softc;
1028
1029 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
1030 if (periph == NULL) {
1031 biofinish(bp, NULL, ENXIO);
1032 return;
1033 }
1034 softc = (struct da_softc *)periph->softc;
1035
1036 cam_periph_lock(periph);
1037
1038 /*
1039 * If the device has been made invalid, error out
1040 */
1041 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
1042 cam_periph_unlock(periph);
1043 biofinish(bp, NULL, ENXIO);
1044 return;
1045 }
1046
1047 /*
1048 * Place it in the queue of disk activities for this disk
1049 */
1050 bioq_disksort(&softc->bio_queue, bp);
1051
1052 /*
1053 * Schedule ourselves for performing the work.
1054 */
1055 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1056 cam_periph_unlock(periph);
1057
1058 return;
1059 }
1060
1061 static int
1062 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
1063 {
1064 struct cam_periph *periph;
1065 struct da_softc *softc;
1066 u_int secsize;
1067 struct ccb_scsiio csio;
1068 struct disk *dp;
1069
1070 dp = arg;
1071 periph = dp->d_drv1;
1072 if (periph == NULL)
1073 return (ENXIO);
1074 softc = (struct da_softc *)periph->softc;
1075 cam_periph_lock(periph);
1076 secsize = softc->params.secsize;
1077
1078 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
1079 cam_periph_unlock(periph);
1080 return (ENXIO);
1081 }
1082
1083 if (length > 0) {
1084 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1085 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1086 scsi_read_write(&csio,
1087 /*retries*/1,
1088 dadone,
1089 MSG_ORDERED_Q_TAG,
1090 /*read*/FALSE,
1091 /*byte2*/0,
1092 /*minimum_cmd_size*/ softc->minimum_cmd_size,
1093 offset / secsize,
1094 length / secsize,
1095 /*data_ptr*/(u_int8_t *) virtual,
1096 /*dxfer_len*/length,
1097 /*sense_len*/SSD_FULL_SIZE,
1098 da_default_timeout * 1000);
1099 xpt_polled_action((union ccb *)&csio);
1100 cam_periph_unlock(periph);
1101
1102 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1103 printf("Aborting dump due to I/O error.\n");
1104 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1105 CAM_SCSI_STATUS_ERROR)
1106 scsi_sense_print(&csio);
1107 else
1108 printf("status == 0x%x, scsi status == 0x%x\n",
1109 csio.ccb_h.status, csio.scsi_status);
1110 return(EIO);
1111 }
1112 return(0);
1113 }
1114
1115 /*
1116 * Sync the disk cache contents to the physical media.
1117 */
1118 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
1119
1120 xpt_setup_ccb(&csio.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1121 csio.ccb_h.ccb_state = DA_CCB_DUMP;
1122 scsi_synchronize_cache(&csio,
1123 /*retries*/1,
1124 /*cbfcnp*/dadone,
1125 MSG_SIMPLE_Q_TAG,
1126 /*begin_lba*/0,/* Cover the whole disk */
1127 /*lb_count*/0,
1128 SSD_FULL_SIZE,
1129 5 * 60 * 1000);
1130 xpt_polled_action((union ccb *)&csio);
1131
1132 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1133 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
1134 CAM_SCSI_STATUS_ERROR) {
1135 int asc, ascq;
1136 int sense_key, error_code;
1137
1138 scsi_extract_sense(&csio.sense_data,
1139 &error_code,
1140 &sense_key,
1141 &asc, &ascq);
1142 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1143 scsi_sense_print(&csio);
1144 } else {
1145 xpt_print(periph->path, "Synchronize cache "
1146 "failed, status == 0x%x, scsi status == "
1147 "0x%x\n", csio.ccb_h.status,
1148 csio.scsi_status);
1149 }
1150 }
1151 }
1152 cam_periph_unlock(periph);
1153 return (0);
1154 }
1155
1156 static void
1157 dainit(void)
1158 {
1159 cam_status status;
1160
1161 /*
1162 * Install a global async callback. This callback will
1163 * receive async callbacks like "new device found".
1164 */
1165 status = xpt_register_async(AC_FOUND_DEVICE, daasync, NULL, NULL);
1166
1167 if (status != CAM_REQ_CMP) {
1168 printf("da: Failed to attach master async callback "
1169 "due to status 0x%x!\n", status);
1170 } else if (da_send_ordered) {
1171
1172 /* Register our shutdown event handler */
1173 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
1174 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
1175 printf("dainit: shutdown event registration failed!\n");
1176 }
1177 }
1178
1179 static void
1180 daoninvalidate(struct cam_periph *periph)
1181 {
1182 struct da_softc *softc;
1183
1184 softc = (struct da_softc *)periph->softc;
1185
1186 /*
1187 * De-register any async callbacks.
1188 */
1189 xpt_register_async(0, daasync, periph, periph->path);
1190
1191 softc->flags |= DA_FLAG_PACK_INVALID;
1192
1193 /*
1194 * Return all queued I/O with ENXIO.
1195 * XXX Handle any transactions queued to the card
1196 * with XPT_ABORT_CCB.
1197 */
1198 bioq_flush(&softc->bio_queue, NULL, ENXIO);
1199
1200 disk_gone(softc->disk);
1201 xpt_print(periph->path, "lost device - %d outstanding, %d refs\n",
1202 softc->outstanding_cmds, periph->refcount);
1203 }
1204
1205 static void
1206 dacleanup(struct cam_periph *periph)
1207 {
1208 struct da_softc *softc;
1209
1210 softc = (struct da_softc *)periph->softc;
1211
1212 xpt_print(periph->path, "removing device entry\n");
1213 cam_periph_unlock(periph);
1214
1215 /*
1216 * If we can't free the sysctl tree, oh well...
1217 */
1218 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
1219 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
1220 xpt_print(periph->path, "can't remove sysctl context\n");
1221 }
1222
1223 disk_destroy(softc->disk);
1224 callout_drain(&softc->sendordered_c);
1225 free(softc, M_DEVBUF);
1226 cam_periph_lock(periph);
1227 }
1228
1229 static void
1230 daasync(void *callback_arg, u_int32_t code,
1231 struct cam_path *path, void *arg)
1232 {
1233 struct cam_periph *periph;
1234
1235 periph = (struct cam_periph *)callback_arg;
1236 switch (code) {
1237 case AC_FOUND_DEVICE:
1238 {
1239 struct ccb_getdev *cgd;
1240 cam_status status;
1241
1242 cgd = (struct ccb_getdev *)arg;
1243 if (cgd == NULL)
1244 break;
1245
1246 if (cgd->protocol != PROTO_SCSI)
1247 break;
1248
1249 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
1250 && SID_TYPE(&cgd->inq_data) != T_RBC
1251 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
1252 break;
1253
1254 /*
1255 * Allocate a peripheral instance for
1256 * this device and start the probe
1257 * process.
1258 */
1259 status = cam_periph_alloc(daregister, daoninvalidate,
1260 dacleanup, dastart,
1261 "da", CAM_PERIPH_BIO,
1262 cgd->ccb_h.path, daasync,
1263 AC_FOUND_DEVICE, cgd);
1264
1265 if (status != CAM_REQ_CMP
1266 && status != CAM_REQ_INPROG)
1267 printf("daasync: Unable to attach to new device "
1268 "due to status 0x%x\n", status);
1269 break;
1270 }
1271 case AC_SENT_BDR:
1272 case AC_BUS_RESET:
1273 {
1274 struct da_softc *softc;
1275 struct ccb_hdr *ccbh;
1276
1277 softc = (struct da_softc *)periph->softc;
1278 /*
1279 * Don't fail on the expected unit attention
1280 * that will occur.
1281 */
1282 softc->flags |= DA_FLAG_RETRY_UA;
1283 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
1284 ccbh->ccb_state |= DA_CCB_RETRY_UA;
1285 /* FALLTHROUGH*/
1286 }
1287 default:
1288 cam_periph_async(periph, code, path, arg);
1289 break;
1290 }
1291 }
1292
1293 static void
1294 dasysctlinit(void *context, int pending)
1295 {
1296 struct cam_periph *periph;
1297 struct da_softc *softc;
1298 char tmpstr[80], tmpstr2[80];
1299
1300 periph = (struct cam_periph *)context;
1301 /*
1302 * periph was held for us when this task was enqueued
1303 */
1304 if (periph->flags & CAM_PERIPH_INVALID) {
1305 cam_periph_release(periph);
1306 return;
1307 }
1308
1309 softc = (struct da_softc *)periph->softc;
1310 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
1311 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
1312
1313 sysctl_ctx_init(&softc->sysctl_ctx);
1314 softc->flags |= DA_FLAG_SCTX_INIT;
1315 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
1316 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
1317 CTLFLAG_RD, 0, tmpstr);
1318 if (softc->sysctl_tree == NULL) {
1319 printf("dasysctlinit: unable to allocate sysctl tree\n");
1320 cam_periph_release(periph);
1321 return;
1322 }
1323
1324 /*
1325 * Now register the sysctl handler, so the user can the value on
1326 * the fly.
1327 */
1328 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
1329 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
1330 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
1331 "Minimum CDB size");
1332
1333 cam_periph_release(periph);
1334 }
1335
1336 static int
1337 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
1338 {
1339 int error, value;
1340
1341 value = *(int *)arg1;
1342
1343 error = sysctl_handle_int(oidp, &value, 0, req);
1344
1345 if ((error != 0)
1346 || (req->newptr == NULL))
1347 return (error);
1348
1349 /*
1350 * Acceptable values here are 6, 10, 12 or 16.
1351 */
1352 if (value < 6)
1353 value = 6;
1354 else if ((value > 6)
1355 && (value <= 10))
1356 value = 10;
1357 else if ((value > 10)
1358 && (value <= 12))
1359 value = 12;
1360 else if (value > 12)
1361 value = 16;
1362
1363 *(int *)arg1 = value;
1364
1365 return (0);
1366 }
1367
1368 static cam_status
1369 daregister(struct cam_periph *periph, void *arg)
1370 {
1371 struct da_softc *softc;
1372 struct ccb_pathinq cpi;
1373 struct ccb_getdev *cgd;
1374 char tmpstr[80];
1375 caddr_t match;
1376
1377 cgd = (struct ccb_getdev *)arg;
1378 if (periph == NULL) {
1379 printf("daregister: periph was NULL!!\n");
1380 return(CAM_REQ_CMP_ERR);
1381 }
1382
1383 if (cgd == NULL) {
1384 printf("daregister: no getdev CCB, can't register device\n");
1385 return(CAM_REQ_CMP_ERR);
1386 }
1387
1388 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
1389 M_NOWAIT|M_ZERO);
1390
1391 if (softc == NULL) {
1392 printf("daregister: Unable to probe new device. "
1393 "Unable to allocate softc\n");
1394 return(CAM_REQ_CMP_ERR);
1395 }
1396
1397 LIST_INIT(&softc->pending_ccbs);
1398 softc->state = DA_STATE_PROBE;
1399 bioq_init(&softc->bio_queue);
1400 if (SID_IS_REMOVABLE(&cgd->inq_data))
1401 softc->flags |= DA_FLAG_PACK_REMOVABLE;
1402 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
1403 softc->flags |= DA_FLAG_TAGGED_QUEUING;
1404
1405 periph->softc = softc;
1406
1407 /*
1408 * See if this device has any quirks.
1409 */
1410 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
1411 (caddr_t)da_quirk_table,
1412 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
1413 sizeof(*da_quirk_table), scsi_inquiry_match);
1414
1415 if (match != NULL)
1416 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
1417 else
1418 softc->quirks = DA_Q_NONE;
1419
1420 /* Check if the SIM does not want 6 byte commands */
1421 bzero(&cpi, sizeof(cpi));
1422 xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1423 cpi.ccb_h.func_code = XPT_PATH_INQ;
1424 xpt_action((union ccb *)&cpi);
1425 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
1426 softc->quirks |= DA_Q_NO_6_BYTE;
1427
1428 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
1429
1430 /*
1431 * Add async callbacks for bus reset and
1432 * bus device reset calls. I don't bother
1433 * checking if this fails as, in most cases,
1434 * the system will function just fine without
1435 * them and the only alternative would be to
1436 * not attach the device on failure.
1437 */
1438 xpt_register_async(AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE,
1439 daasync, periph, periph->path);
1440
1441 /*
1442 * Take an exclusive refcount on the periph while dastart is called
1443 * to finish the probe. The reference will be dropped in dadone at
1444 * the end of probe.
1445 */
1446 (void)cam_periph_hold(periph, PRIBIO);
1447
1448 /*
1449 * Schedule a periodic event to occasionally send an
1450 * ordered tag to a device.
1451 */
1452 callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
1453 callout_reset(&softc->sendordered_c,
1454 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
1455 dasendorderedtag, softc);
1456
1457 mtx_unlock(periph->sim->mtx);
1458 /*
1459 * RBC devices don't have to support READ(6), only READ(10).
1460 */
1461 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
1462 softc->minimum_cmd_size = 10;
1463 else
1464 softc->minimum_cmd_size = 6;
1465
1466 /*
1467 * Load the user's default, if any.
1468 */
1469 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1470 periph->unit_number);
1471 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1472
1473 /*
1474 * 6, 10, 12 and 16 are the currently permissible values.
1475 */
1476 if (softc->minimum_cmd_size < 6)
1477 softc->minimum_cmd_size = 6;
1478 else if ((softc->minimum_cmd_size > 6)
1479 && (softc->minimum_cmd_size <= 10))
1480 softc->minimum_cmd_size = 10;
1481 else if ((softc->minimum_cmd_size > 10)
1482 && (softc->minimum_cmd_size <= 12))
1483 softc->minimum_cmd_size = 12;
1484 else if (softc->minimum_cmd_size > 12)
1485 softc->minimum_cmd_size = 16;
1486
1487 /*
1488 * Register this media as a disk.
1489 */
1490 softc->disk = disk_alloc();
1491 softc->disk->d_devstat = devstat_new_entry(periph->periph_name,
1492 periph->unit_number, 0,
1493 DEVSTAT_BS_UNAVAILABLE,
1494 SID_TYPE(&cgd->inq_data) |
1495 XPORT_DEVSTAT_TYPE(cpi.transport),
1496 DEVSTAT_PRIORITY_DISK);
1497 softc->disk->d_open = daopen;
1498 softc->disk->d_close = daclose;
1499 softc->disk->d_strategy = dastrategy;
1500 softc->disk->d_dump = dadump;
1501 softc->disk->d_name = "da";
1502 softc->disk->d_drv1 = periph;
1503 if (cpi.maxio == 0)
1504 softc->disk->d_maxsize = DFLTPHYS; /* traditional default */
1505 else if (cpi.maxio > MAXPHYS)
1506 softc->disk->d_maxsize = MAXPHYS; /* for safety */
1507 else
1508 softc->disk->d_maxsize = cpi.maxio;
1509 softc->disk->d_unit = periph->unit_number;
1510 softc->disk->d_flags = 0;
1511 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0)
1512 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
1513 strlcpy(softc->disk->d_ident, cgd->serial_num,
1514 MIN(sizeof(softc->disk->d_ident), cgd->serial_num_len + 1));
1515 disk_create(softc->disk, DISK_VERSION);
1516 mtx_lock(periph->sim->mtx);
1517
1518 xpt_schedule(periph, CAM_PRIORITY_DEV);
1519
1520 return(CAM_REQ_CMP);
1521 }
1522
1523 static void
1524 dastart(struct cam_periph *periph, union ccb *start_ccb)
1525 {
1526 struct da_softc *softc;
1527
1528 softc = (struct da_softc *)periph->softc;
1529
1530 switch (softc->state) {
1531 case DA_STATE_NORMAL:
1532 {
1533 /* Pull a buffer from the queue and get going on it */
1534 struct bio *bp;
1535
1536 /*
1537 * See if there is a buf with work for us to do..
1538 */
1539 bp = bioq_first(&softc->bio_queue);
1540 if (periph->immediate_priority <= periph->pinfo.priority) {
1541 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1542 ("queuing for immediate ccb\n"));
1543 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1544 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1545 periph_links.sle);
1546 periph->immediate_priority = CAM_PRIORITY_NONE;
1547 wakeup(&periph->ccb_list);
1548 } else if (bp == NULL) {
1549 xpt_release_ccb(start_ccb);
1550 } else {
1551 u_int8_t tag_code;
1552
1553 bioq_remove(&softc->bio_queue, bp);
1554
1555 if ((bp->bio_flags & BIO_ORDERED) != 0
1556 || (softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1557 softc->flags &= ~DA_FLAG_NEED_OTAG;
1558 softc->ordered_tag_count++;
1559 tag_code = MSG_ORDERED_Q_TAG;
1560 } else {
1561 tag_code = MSG_SIMPLE_Q_TAG;
1562 }
1563 switch (bp->bio_cmd) {
1564 case BIO_READ:
1565 case BIO_WRITE:
1566 scsi_read_write(&start_ccb->csio,
1567 /*retries*/da_retry_count,
1568 /*cbfcnp*/dadone,
1569 /*tag_action*/tag_code,
1570 /*read_op*/bp->bio_cmd
1571 == BIO_READ,
1572 /*byte2*/0,
1573 softc->minimum_cmd_size,
1574 /*lba*/bp->bio_pblkno,
1575 /*block_count*/bp->bio_bcount /
1576 softc->params.secsize,
1577 /*data_ptr*/ bp->bio_data,
1578 /*dxfer_len*/ bp->bio_bcount,
1579 /*sense_len*/SSD_FULL_SIZE,
1580 da_default_timeout * 1000);
1581 break;
1582 case BIO_FLUSH:
1583 /*
1584 * BIO_FLUSH doesn't currently communicate
1585 * range data, so we synchronize the cache
1586 * over the whole disk. We also force
1587 * ordered tag semantics the flush applies
1588 * to all previously queued I/O.
1589 */
1590 scsi_synchronize_cache(&start_ccb->csio,
1591 /*retries*/1,
1592 /*cbfcnp*/dadone,
1593 MSG_ORDERED_Q_TAG,
1594 /*begin_lba*/0,
1595 /*lb_count*/0,
1596 SSD_FULL_SIZE,
1597 da_default_timeout*1000);
1598 break;
1599 }
1600 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1601
1602 /*
1603 * Block out any asyncronous callbacks
1604 * while we touch the pending ccb list.
1605 */
1606 LIST_INSERT_HEAD(&softc->pending_ccbs,
1607 &start_ccb->ccb_h, periph_links.le);
1608 softc->outstanding_cmds++;
1609
1610 /* We expect a unit attention from this device */
1611 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1612 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1613 softc->flags &= ~DA_FLAG_RETRY_UA;
1614 }
1615
1616 start_ccb->ccb_h.ccb_bp = bp;
1617 bp = bioq_first(&softc->bio_queue);
1618
1619 xpt_action(start_ccb);
1620 }
1621
1622 if (bp != NULL) {
1623 /* Have more work to do, so ensure we stay scheduled */
1624 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
1625 }
1626 break;
1627 }
1628 case DA_STATE_PROBE:
1629 {
1630 struct ccb_scsiio *csio;
1631 struct scsi_read_capacity_data *rcap;
1632
1633 rcap = (struct scsi_read_capacity_data *)
1634 malloc(sizeof(*rcap), M_SCSIDA, M_NOWAIT|M_ZERO);
1635 if (rcap == NULL) {
1636 printf("dastart: Couldn't malloc read_capacity data\n");
1637 /* da_free_periph??? */
1638 break;
1639 }
1640 csio = &start_ccb->csio;
1641 scsi_read_capacity(csio,
1642 /*retries*/4,
1643 dadone,
1644 MSG_SIMPLE_Q_TAG,
1645 rcap,
1646 SSD_FULL_SIZE,
1647 /*timeout*/5000);
1648 start_ccb->ccb_h.ccb_bp = NULL;
1649 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1650 xpt_action(start_ccb);
1651 break;
1652 }
1653 case DA_STATE_PROBE2:
1654 {
1655 struct ccb_scsiio *csio;
1656 struct scsi_read_capacity_data_long *rcaplong;
1657
1658 rcaplong = (struct scsi_read_capacity_data_long *)
1659 malloc(sizeof(*rcaplong), M_SCSIDA, M_NOWAIT|M_ZERO);
1660 if (rcaplong == NULL) {
1661 printf("dastart: Couldn't malloc read_capacity data\n");
1662 /* da_free_periph??? */
1663 break;
1664 }
1665 csio = &start_ccb->csio;
1666 scsi_read_capacity_16(csio,
1667 /*retries*/ 4,
1668 /*cbfcnp*/ dadone,
1669 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1670 /*lba*/ 0,
1671 /*reladr*/ 0,
1672 /*pmi*/ 0,
1673 rcaplong,
1674 /*sense_len*/ SSD_FULL_SIZE,
1675 /*timeout*/ 60000);
1676 start_ccb->ccb_h.ccb_bp = NULL;
1677 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1678 xpt_action(start_ccb);
1679 break;
1680 }
1681 }
1682 }
1683
1684 static int
1685 cmd6workaround(union ccb *ccb)
1686 {
1687 struct scsi_rw_6 cmd6;
1688 struct scsi_rw_10 *cmd10;
1689 struct da_softc *softc;
1690 u_int8_t *cdb;
1691 int frozen;
1692
1693 cdb = ccb->csio.cdb_io.cdb_bytes;
1694
1695 /* Translation only possible if CDB is an array and cmd is R/W6 */
1696 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1697 (*cdb != READ_6 && *cdb != WRITE_6))
1698 return 0;
1699
1700 xpt_print(ccb->ccb_h.path, "READ(6)/WRITE(6) not supported, "
1701 "increasing minimum_cmd_size to 10.\n");
1702 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1703 softc->minimum_cmd_size = 10;
1704
1705 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1706 cmd10 = (struct scsi_rw_10 *)cdb;
1707 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1708 cmd10->byte2 = 0;
1709 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1710 cmd10->reserved = 0;
1711 scsi_ulto2b(cmd6.length, cmd10->length);
1712 cmd10->control = cmd6.control;
1713 ccb->csio.cdb_len = sizeof(*cmd10);
1714
1715 /* Requeue request, unfreezing queue if necessary */
1716 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1717 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1718 xpt_action(ccb);
1719 if (frozen) {
1720 cam_release_devq(ccb->ccb_h.path,
1721 /*relsim_flags*/0,
1722 /*reduction*/0,
1723 /*timeout*/0,
1724 /*getcount_only*/0);
1725 }
1726 return (ERESTART);
1727 }
1728
1729 static void
1730 dadone(struct cam_periph *periph, union ccb *done_ccb)
1731 {
1732 struct da_softc *softc;
1733 struct ccb_scsiio *csio;
1734 u_int32_t priority;
1735
1736 softc = (struct da_softc *)periph->softc;
1737 priority = done_ccb->ccb_h.pinfo.priority;
1738 csio = &done_ccb->csio;
1739 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1740 case DA_CCB_BUFFER_IO:
1741 {
1742 struct bio *bp;
1743
1744 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1745 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1746 int error;
1747 int sf;
1748
1749 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1750 sf = SF_RETRY_UA;
1751 else
1752 sf = 0;
1753
1754 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1755 if (error == ERESTART) {
1756 /*
1757 * A retry was scheuled, so
1758 * just return.
1759 */
1760 return;
1761 }
1762 if (error != 0) {
1763
1764 if (error == ENXIO) {
1765 /*
1766 * Catastrophic error. Mark our pack as
1767 * invalid.
1768 */
1769 /*
1770 * XXX See if this is really a media
1771 * XXX change first?
1772 */
1773 xpt_print(periph->path,
1774 "Invalidating pack\n");
1775 softc->flags |= DA_FLAG_PACK_INVALID;
1776 }
1777
1778 /*
1779 * return all queued I/O with EIO, so that
1780 * the client can retry these I/Os in the
1781 * proper order should it attempt to recover.
1782 */
1783 bioq_flush(&softc->bio_queue, NULL, EIO);
1784 bp->bio_error = error;
1785 bp->bio_resid = bp->bio_bcount;
1786 bp->bio_flags |= BIO_ERROR;
1787 } else {
1788 bp->bio_resid = csio->resid;
1789 bp->bio_error = 0;
1790 if (bp->bio_resid != 0)
1791 bp->bio_flags |= BIO_ERROR;
1792 }
1793 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1794 cam_release_devq(done_ccb->ccb_h.path,
1795 /*relsim_flags*/0,
1796 /*reduction*/0,
1797 /*timeout*/0,
1798 /*getcount_only*/0);
1799 } else {
1800 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1801 panic("REQ_CMP with QFRZN");
1802 bp->bio_resid = csio->resid;
1803 if (csio->resid > 0)
1804 bp->bio_flags |= BIO_ERROR;
1805 if (softc->error_inject != 0) {
1806 bp->bio_error = softc->error_inject;
1807 bp->bio_resid = bp->bio_bcount;
1808 bp->bio_flags |= BIO_ERROR;
1809 softc->error_inject = 0;
1810 }
1811
1812 }
1813
1814 /*
1815 * Block out any asyncronous callbacks
1816 * while we touch the pending ccb list.
1817 */
1818 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1819 softc->outstanding_cmds--;
1820 if (softc->outstanding_cmds == 0)
1821 softc->flags |= DA_FLAG_WENT_IDLE;
1822
1823 biodone(bp);
1824 break;
1825 }
1826 case DA_CCB_PROBE:
1827 case DA_CCB_PROBE2:
1828 {
1829 struct scsi_read_capacity_data *rdcap;
1830 struct scsi_read_capacity_data_long *rcaplong;
1831 char announce_buf[80];
1832
1833 rdcap = NULL;
1834 rcaplong = NULL;
1835 if (softc->state == DA_STATE_PROBE)
1836 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1837 else
1838 rcaplong = (struct scsi_read_capacity_data_long *)
1839 csio->data_ptr;
1840
1841 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1842 struct disk_params *dp;
1843 uint32_t block_size;
1844 uint64_t maxsector;
1845
1846 if (softc->state == DA_STATE_PROBE) {
1847 block_size = scsi_4btoul(rdcap->length);
1848 maxsector = scsi_4btoul(rdcap->addr);
1849
1850 /*
1851 * According to SBC-2, if the standard 10
1852 * byte READ CAPACITY command returns 2^32,
1853 * we should issue the 16 byte version of
1854 * the command, since the device in question
1855 * has more sectors than can be represented
1856 * with the short version of the command.
1857 */
1858 if (maxsector == 0xffffffff) {
1859 softc->state = DA_STATE_PROBE2;
1860 free(rdcap, M_SCSIDA);
1861 xpt_release_ccb(done_ccb);
1862 xpt_schedule(periph, priority);
1863 return;
1864 }
1865 } else {
1866 block_size = scsi_4btoul(rcaplong->length);
1867 maxsector = scsi_8btou64(rcaplong->addr);
1868 }
1869
1870 /*
1871 * Because GEOM code just will panic us if we
1872 * give them an 'illegal' value we'll avoid that
1873 * here.
1874 */
1875 if (block_size == 0 && maxsector == 0) {
1876 snprintf(announce_buf, sizeof(announce_buf),
1877 "0MB (no media?)");
1878 } else if (block_size >= MAXPHYS || block_size == 0) {
1879 xpt_print(periph->path,
1880 "unsupportable block size %ju\n",
1881 (uintmax_t) block_size);
1882 announce_buf[0] = '\0';
1883 cam_periph_invalidate(periph);
1884 } else {
1885 dasetgeom(periph, block_size, maxsector, 0, 0);
1886 dp = &softc->params;
1887 snprintf(announce_buf, sizeof(announce_buf),
1888 "%juMB (%ju %u byte sectors: %dH %dS/T "
1889 "%dC)", (uintmax_t)
1890 (((uintmax_t)dp->secsize *
1891 dp->sectors) / (1024*1024)),
1892 (uintmax_t)dp->sectors,
1893 dp->secsize, dp->heads,
1894 dp->secs_per_track, dp->cylinders);
1895 }
1896 } else {
1897 int error;
1898
1899 announce_buf[0] = '\0';
1900
1901 /*
1902 * Retry any UNIT ATTENTION type errors. They
1903 * are expected at boot.
1904 */
1905 error = daerror(done_ccb, CAM_RETRY_SELTO,
1906 SF_RETRY_UA|SF_NO_PRINT);
1907 if (error == ERESTART) {
1908 /*
1909 * A retry was scheuled, so
1910 * just return.
1911 */
1912 return;
1913 } else if (error != 0) {
1914 struct scsi_sense_data *sense;
1915 int asc, ascq;
1916 int sense_key, error_code;
1917 int have_sense;
1918 cam_status status;
1919 struct ccb_getdev cgd;
1920
1921 /* Don't wedge this device's queue */
1922 status = done_ccb->ccb_h.status;
1923 if ((status & CAM_DEV_QFRZN) != 0)
1924 cam_release_devq(done_ccb->ccb_h.path,
1925 /*relsim_flags*/0,
1926 /*reduction*/0,
1927 /*timeout*/0,
1928 /*getcount_only*/0);
1929
1930
1931 xpt_setup_ccb(&cgd.ccb_h,
1932 done_ccb->ccb_h.path,
1933 CAM_PRIORITY_NORMAL);
1934 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1935 xpt_action((union ccb *)&cgd);
1936
1937 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1938 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1939 || ((status & CAM_AUTOSNS_VALID) == 0))
1940 have_sense = FALSE;
1941 else
1942 have_sense = TRUE;
1943
1944 if (have_sense) {
1945 sense = &csio->sense_data;
1946 scsi_extract_sense(sense, &error_code,
1947 &sense_key,
1948 &asc, &ascq);
1949 }
1950 /*
1951 * Attach to anything that claims to be a
1952 * direct access or optical disk device,
1953 * as long as it doesn't return a "Logical
1954 * unit not supported" (0x25) error.
1955 */
1956 if ((have_sense) && (asc != 0x25)
1957 && (error_code == SSD_CURRENT_ERROR)) {
1958 const char *sense_key_desc;
1959 const char *asc_desc;
1960
1961 scsi_sense_desc(sense_key, asc, ascq,
1962 &cgd.inq_data,
1963 &sense_key_desc,
1964 &asc_desc);
1965 snprintf(announce_buf,
1966 sizeof(announce_buf),
1967 "Attempt to query device "
1968 "size failed: %s, %s",
1969 sense_key_desc,
1970 asc_desc);
1971 } else {
1972 if (have_sense)
1973 scsi_sense_print(
1974 &done_ccb->csio);
1975 else {
1976 xpt_print(periph->path,
1977 "got CAM status %#x\n",
1978 done_ccb->ccb_h.status);
1979 }
1980
1981 xpt_print(periph->path, "fatal error, "
1982 "failed to attach to device\n");
1983
1984 /*
1985 * Free up resources.
1986 */
1987 cam_periph_invalidate(periph);
1988 }
1989 }
1990 }
1991 free(csio->data_ptr, M_SCSIDA);
1992 if (announce_buf[0] != '\0') {
1993 /*
1994 * Create our sysctl variables, now that we know
1995 * we have successfully attached.
1996 */
1997 /* increase the refcount */
1998 if (cam_periph_acquire(periph) == CAM_REQ_CMP) {
1999 taskqueue_enqueue(taskqueue_thread,
2000 &softc->sysctl_task);
2001 xpt_announce_periph(periph, announce_buf);
2002 } else {
2003 xpt_print(periph->path, "fatal error, "
2004 "could not acquire reference count\n");
2005 }
2006
2007 }
2008 softc->state = DA_STATE_NORMAL;
2009 /*
2010 * Since our peripheral may be invalidated by an error
2011 * above or an external event, we must release our CCB
2012 * before releasing the probe lock on the peripheral.
2013 * The peripheral will only go away once the last lock
2014 * is removed, and we need it around for the CCB release
2015 * operation.
2016 */
2017 xpt_release_ccb(done_ccb);
2018 cam_periph_unhold(periph);
2019 return;
2020 }
2021 case DA_CCB_WAITING:
2022 {
2023 /* Caller will release the CCB */
2024 wakeup(&done_ccb->ccb_h.cbfcnp);
2025 return;
2026 }
2027 case DA_CCB_DUMP:
2028 /* No-op. We're polling */
2029 return;
2030 default:
2031 break;
2032 }
2033 xpt_release_ccb(done_ccb);
2034 }
2035
2036 static int
2037 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
2038 {
2039 struct da_softc *softc;
2040 struct cam_periph *periph;
2041 int error;
2042
2043 periph = xpt_path_periph(ccb->ccb_h.path);
2044 softc = (struct da_softc *)periph->softc;
2045
2046 /*
2047 * Automatically detect devices that do not support
2048 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
2049 */
2050 error = 0;
2051 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
2052 error = cmd6workaround(ccb);
2053 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
2054 CAM_SCSI_STATUS_ERROR)
2055 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
2056 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
2057 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
2058 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
2059 int sense_key, error_code, asc, ascq;
2060
2061 scsi_extract_sense(&ccb->csio.sense_data,
2062 &error_code, &sense_key, &asc, &ascq);
2063 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
2064 error = cmd6workaround(ccb);
2065 }
2066 if (error == ERESTART)
2067 return (ERESTART);
2068
2069 /*
2070 * XXX
2071 * Until we have a better way of doing pack validation,
2072 * don't treat UAs as errors.
2073 */
2074 sense_flags |= SF_RETRY_UA;
2075 return(cam_periph_error(ccb, cam_flags, sense_flags,
2076 &softc->saved_ccb));
2077 }
2078
2079 static void
2080 daprevent(struct cam_periph *periph, int action)
2081 {
2082 struct da_softc *softc;
2083 union ccb *ccb;
2084 int error;
2085
2086 softc = (struct da_softc *)periph->softc;
2087
2088 if (((action == PR_ALLOW)
2089 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
2090 || ((action == PR_PREVENT)
2091 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
2092 return;
2093 }
2094
2095 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2096
2097 scsi_prevent(&ccb->csio,
2098 /*retries*/1,
2099 /*cbcfp*/dadone,
2100 MSG_SIMPLE_Q_TAG,
2101 action,
2102 SSD_FULL_SIZE,
2103 5000);
2104
2105 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
2106 SF_RETRY_UA, softc->disk->d_devstat);
2107
2108 if (error == 0) {
2109 if (action == PR_ALLOW)
2110 softc->flags &= ~DA_FLAG_PACK_LOCKED;
2111 else
2112 softc->flags |= DA_FLAG_PACK_LOCKED;
2113 }
2114
2115 xpt_release_ccb(ccb);
2116 }
2117
2118 static int
2119 dagetcapacity(struct cam_periph *periph)
2120 {
2121 struct da_softc *softc;
2122 union ccb *ccb;
2123 struct scsi_read_capacity_data *rcap;
2124 struct scsi_read_capacity_data_long *rcaplong;
2125 uint32_t block_len;
2126 uint64_t maxsector;
2127 int error;
2128 u_int32_t sense_flags;
2129
2130 softc = (struct da_softc *)periph->softc;
2131 block_len = 0;
2132 maxsector = 0;
2133 error = 0;
2134 sense_flags = SF_RETRY_UA;
2135 if (softc->flags & DA_FLAG_PACK_REMOVABLE)
2136 sense_flags |= SF_NO_PRINT;
2137
2138 /* Do a read capacity */
2139 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
2140 M_SCSIDA,
2141 M_NOWAIT);
2142 if (rcap == NULL)
2143 return (ENOMEM);
2144
2145 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
2146 scsi_read_capacity(&ccb->csio,
2147 /*retries*/4,
2148 /*cbfncp*/dadone,
2149 MSG_SIMPLE_Q_TAG,
2150 rcap,
2151 SSD_FULL_SIZE,
2152 /*timeout*/60000);
2153 ccb->ccb_h.ccb_bp = NULL;
2154
2155 error = cam_periph_runccb(ccb, daerror,
2156 /*cam_flags*/CAM_RETRY_SELTO,
2157 sense_flags,
2158 softc->disk->d_devstat);
2159
2160 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2161 cam_release_devq(ccb->ccb_h.path,
2162 /*relsim_flags*/0,
2163 /*reduction*/0,
2164 /*timeout*/0,
2165 /*getcount_only*/0);
2166
2167 if (error == 0) {
2168 block_len = scsi_4btoul(rcap->length);
2169 maxsector = scsi_4btoul(rcap->addr);
2170
2171 if (maxsector != 0xffffffff)
2172 goto done;
2173 } else
2174 goto done;
2175
2176 rcaplong = (struct scsi_read_capacity_data_long *)rcap;
2177
2178 scsi_read_capacity_16(&ccb->csio,
2179 /*retries*/ 4,
2180 /*cbfcnp*/ dadone,
2181 /*tag_action*/ MSG_SIMPLE_Q_TAG,
2182 /*lba*/ 0,
2183 /*reladr*/ 0,
2184 /*pmi*/ 0,
2185 rcaplong,
2186 /*sense_len*/ SSD_FULL_SIZE,
2187 /*timeout*/ 60000);
2188 ccb->ccb_h.ccb_bp = NULL;
2189
2190 error = cam_periph_runccb(ccb, daerror,
2191 /*cam_flags*/CAM_RETRY_SELTO,
2192 sense_flags,
2193 softc->disk->d_devstat);
2194
2195 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
2196 cam_release_devq(ccb->ccb_h.path,
2197 /*relsim_flags*/0,
2198 /*reduction*/0,
2199 /*timeout*/0,
2200 /*getcount_only*/0);
2201
2202 if (error == 0) {
2203 block_len = scsi_4btoul(rcaplong->length);
2204 maxsector = scsi_8btou64(rcaplong->addr);
2205 }
2206
2207 done:
2208
2209 if (error == 0) {
2210 if (block_len >= MAXPHYS || block_len == 0) {
2211 xpt_print(periph->path,
2212 "unsupportable block size %ju\n",
2213 (uintmax_t) block_len);
2214 error = EINVAL;
2215 } else
2216 dasetgeom(periph, block_len, maxsector, 0, 0);
2217 }
2218
2219 xpt_release_ccb(ccb);
2220
2221 free(rcap, M_SCSIDA);
2222
2223 return (error);
2224 }
2225
2226 static void
2227 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector,
2228 u_int lbppbe, u_int lalba)
2229 {
2230 struct ccb_calc_geometry ccg;
2231 struct da_softc *softc;
2232 struct disk_params *dp;
2233
2234 softc = (struct da_softc *)periph->softc;
2235
2236 dp = &softc->params;
2237 dp->secsize = block_len;
2238 dp->sectors = maxsector + 1;
2239 if (lbppbe > 0) {
2240 dp->stripesize = block_len << lbppbe;
2241 dp->stripeoffset = (dp->stripesize - block_len * lalba) %
2242 dp->stripesize;
2243 } else if (softc->quirks & DA_Q_4K) {
2244 dp->stripesize = 4096;
2245 dp->stripeoffset = 0;
2246 } else {
2247 dp->stripesize = 0;
2248 dp->stripeoffset = 0;
2249 }
2250 /*
2251 * Have the controller provide us with a geometry
2252 * for this disk. The only time the geometry
2253 * matters is when we boot and the controller
2254 * is the only one knowledgeable enough to come
2255 * up with something that will make this a bootable
2256 * device.
2257 */
2258 xpt_setup_ccb(&ccg.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2259 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
2260 ccg.block_size = dp->secsize;
2261 ccg.volume_size = dp->sectors;
2262 ccg.heads = 0;
2263 ccg.secs_per_track = 0;
2264 ccg.cylinders = 0;
2265 xpt_action((union ccb*)&ccg);
2266 if ((ccg.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2267 /*
2268 * We don't know what went wrong here- but just pick
2269 * a geometry so we don't have nasty things like divide
2270 * by zero.
2271 */
2272 dp->heads = 255;
2273 dp->secs_per_track = 255;
2274 dp->cylinders = dp->sectors / (255 * 255);
2275 if (dp->cylinders == 0) {
2276 dp->cylinders = 1;
2277 }
2278 } else {
2279 dp->heads = ccg.heads;
2280 dp->secs_per_track = ccg.secs_per_track;
2281 dp->cylinders = ccg.cylinders;
2282 }
2283 }
2284
2285 static void
2286 dasendorderedtag(void *arg)
2287 {
2288 struct da_softc *softc = arg;
2289
2290 if (da_send_ordered) {
2291 if ((softc->ordered_tag_count == 0)
2292 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
2293 softc->flags |= DA_FLAG_NEED_OTAG;
2294 }
2295 if (softc->outstanding_cmds > 0)
2296 softc->flags &= ~DA_FLAG_WENT_IDLE;
2297
2298 softc->ordered_tag_count = 0;
2299 }
2300 /* Queue us up again */
2301 callout_reset(&softc->sendordered_c,
2302 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL,
2303 dasendorderedtag, softc);
2304 }
2305
2306 /*
2307 * Step through all DA peripheral drivers, and if the device is still open,
2308 * sync the disk cache to physical media.
2309 */
2310 static void
2311 dashutdown(void * arg, int howto)
2312 {
2313 struct cam_periph *periph;
2314 struct da_softc *softc;
2315
2316 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
2317 union ccb ccb;
2318
2319 cam_periph_lock(periph);
2320 softc = (struct da_softc *)periph->softc;
2321
2322 /*
2323 * We only sync the cache if the drive is still open, and
2324 * if the drive is capable of it..
2325 */
2326 if (((softc->flags & DA_FLAG_OPEN) == 0)
2327 || (softc->quirks & DA_Q_NO_SYNC_CACHE)) {
2328 cam_periph_unlock(periph);
2329 continue;
2330 }
2331
2332 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
2333
2334 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
2335 scsi_synchronize_cache(&ccb.csio,
2336 /*retries*/1,
2337 /*cbfcnp*/dadone,
2338 MSG_SIMPLE_Q_TAG,
2339 /*begin_lba*/0, /* whole disk */
2340 /*lb_count*/0,
2341 SSD_FULL_SIZE,
2342 60 * 60 * 1000);
2343
2344 xpt_polled_action(&ccb);
2345
2346 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
2347 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
2348 CAM_SCSI_STATUS_ERROR)
2349 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
2350 int error_code, sense_key, asc, ascq;
2351
2352 scsi_extract_sense(&ccb.csio.sense_data,
2353 &error_code, &sense_key,
2354 &asc, &ascq);
2355
2356 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
2357 scsi_sense_print(&ccb.csio);
2358 } else {
2359 xpt_print(periph->path, "Synchronize "
2360 "cache failed, status == 0x%x, scsi status "
2361 "== 0x%x\n", ccb.ccb_h.status,
2362 ccb.csio.scsi_status);
2363 }
2364 }
2365
2366 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
2367 cam_release_devq(ccb.ccb_h.path,
2368 /*relsim_flags*/0,
2369 /*reduction*/0,
2370 /*timeout*/0,
2371 /*getcount_only*/0);
2372 cam_periph_unlock(periph);
2373 }
2374 }
2375
2376 #else /* !_KERNEL */
2377
2378 /*
2379 * XXX This is only left out of the kernel build to silence warnings. If,
2380 * for some reason this function is used in the kernel, the ifdefs should
2381 * be moved so it is included both in the kernel and userland.
2382 */
2383 void
2384 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
2385 void (*cbfcnp)(struct cam_periph *, union ccb *),
2386 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
2387 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
2388 u_int32_t timeout)
2389 {
2390 struct scsi_format_unit *scsi_cmd;
2391
2392 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
2393 scsi_cmd->opcode = FORMAT_UNIT;
2394 scsi_cmd->byte2 = byte2;
2395 scsi_ulto2b(ileave, scsi_cmd->interleave);
2396
2397 cam_fill_csio(csio,
2398 retries,
2399 cbfcnp,
2400 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
2401 tag_action,
2402 data_ptr,
2403 dxfer_len,
2404 sense_len,
2405 sizeof(*scsi_cmd),
2406 timeout);
2407 }
2408
2409 #endif /* _KERNEL */
Cache object: 6749e5022d2c2048d7dbbfa77a375633
|