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