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