FreeBSD/Linux Kernel Cross Reference
sys/cam/scsi/scsi_da.c
1 /*-
2 * Implementation of SCSI Direct Access Peripheral driver for CAM.
3 *
4 * Copyright (c) 1997 Justin T. Gibbs.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification, immediately at the beginning of the file.
13 * 2. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD: releng/5.4/sys/cam/scsi/scsi_da.c 141016 2005-01-30 01:00:13Z imp $");
31
32 #ifdef _KERNEL
33 #include "opt_hw_wdog.h"
34 #endif /* _KERNEL */
35
36 #include <sys/param.h>
37
38 #ifdef _KERNEL
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/bio.h>
42 #include <sys/sysctl.h>
43 #include <sys/taskqueue.h>
44 #endif /* _KERNEL */
45
46 #include <sys/devicestat.h>
47 #include <sys/conf.h>
48 #include <sys/eventhandler.h>
49 #include <sys/malloc.h>
50 #include <sys/cons.h>
51
52 #include <machine/md_var.h>
53
54 #include <vm/vm.h>
55 #include <vm/pmap.h>
56
57 #include <geom/geom_disk.h>
58
59 #ifndef _KERNEL
60 #include <stdio.h>
61 #include <string.h>
62 #endif /* _KERNEL */
63
64 #include <cam/cam.h>
65 #include <cam/cam_ccb.h>
66 #include <cam/cam_periph.h>
67 #include <cam/cam_xpt_periph.h>
68
69 #include <cam/scsi/scsi_message.h>
70
71 #ifndef _KERNEL
72 #include <cam/scsi/scsi_da.h>
73 #endif /* !_KERNEL */
74
75 #ifdef _KERNEL
76 typedef enum {
77 DA_STATE_PROBE,
78 DA_STATE_PROBE2,
79 DA_STATE_NORMAL
80 } da_state;
81
82 typedef enum {
83 DA_FLAG_PACK_INVALID = 0x001,
84 DA_FLAG_NEW_PACK = 0x002,
85 DA_FLAG_PACK_LOCKED = 0x004,
86 DA_FLAG_PACK_REMOVABLE = 0x008,
87 DA_FLAG_TAGGED_QUEUING = 0x010,
88 DA_FLAG_NEED_OTAG = 0x020,
89 DA_FLAG_WENT_IDLE = 0x040,
90 DA_FLAG_RETRY_UA = 0x080,
91 DA_FLAG_OPEN = 0x100,
92 DA_FLAG_SCTX_INIT = 0x200
93 } da_flags;
94
95 typedef enum {
96 DA_Q_NONE = 0x00,
97 DA_Q_NO_SYNC_CACHE = 0x01,
98 DA_Q_NO_6_BYTE = 0x02,
99 DA_Q_NO_PREVENT = 0x04
100 } da_quirks;
101
102 typedef enum {
103 DA_CCB_PROBE = 0x01,
104 DA_CCB_PROBE2 = 0x02,
105 DA_CCB_BUFFER_IO = 0x03,
106 DA_CCB_WAITING = 0x04,
107 DA_CCB_DUMP = 0x05,
108 DA_CCB_TYPE_MASK = 0x0F,
109 DA_CCB_RETRY_UA = 0x10
110 } da_ccb_state;
111
112 /* Offsets into our private area for storing information */
113 #define ccb_state ppriv_field0
114 #define ccb_bp ppriv_ptr1
115
116 struct disk_params {
117 u_int8_t heads;
118 u_int32_t cylinders;
119 u_int8_t secs_per_track;
120 u_int32_t secsize; /* Number of bytes/sector */
121 u_int64_t sectors; /* total number sectors */
122 };
123
124 struct da_softc {
125 struct bio_queue_head bio_queue;
126 SLIST_ENTRY(da_softc) links;
127 LIST_HEAD(, ccb_hdr) pending_ccbs;
128 da_state state;
129 da_flags flags;
130 da_quirks quirks;
131 int minimum_cmd_size;
132 int ordered_tag_count;
133 int outstanding_cmds;
134 struct disk_params params;
135 struct disk *disk;
136 union ccb saved_ccb;
137 struct task sysctl_task;
138 struct sysctl_ctx_list sysctl_ctx;
139 struct sysctl_oid *sysctl_tree;
140 };
141
142 struct da_quirk_entry {
143 struct scsi_inquiry_pattern inq_pat;
144 da_quirks quirks;
145 };
146
147 static const char quantum[] = "QUANTUM";
148 static const char microp[] = "MICROP";
149
150 static struct da_quirk_entry da_quirk_table[] =
151 {
152 /* SPI, FC devices */
153 {
154 /*
155 * Fujitsu M2513A MO drives.
156 * Tested devices: M2513A2 firmware versions 1200 & 1300.
157 * (dip switch selects whether T_DIRECT or T_OPTICAL device)
158 * Reported by: W.Scholten <whs@xs4all.nl>
159 */
160 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
161 /*quirks*/ DA_Q_NO_SYNC_CACHE
162 },
163 {
164 /* See above. */
165 {T_OPTICAL, SIP_MEDIA_REMOVABLE, "FUJITSU", "M2513A", "*"},
166 /*quirks*/ DA_Q_NO_SYNC_CACHE
167 },
168 {
169 /*
170 * This particular Fujitsu drive doesn't like the
171 * synchronize cache command.
172 * Reported by: Tom Jackson <toj@gorilla.net>
173 */
174 {T_DIRECT, SIP_MEDIA_FIXED, "FUJITSU", "M2954*", "*"},
175 /*quirks*/ DA_Q_NO_SYNC_CACHE
176
177 },
178 {
179 /*
180 * This drive doesn't like the synchronize cache command
181 * either. Reported by: Matthew Jacob <mjacob@feral.com>
182 * in NetBSD PR kern/6027, August 24, 1998.
183 */
184 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2217*", "*"},
185 /*quirks*/ DA_Q_NO_SYNC_CACHE
186 },
187 {
188 /*
189 * This drive doesn't like the synchronize cache command
190 * either. Reported by: Hellmuth Michaelis (hm@kts.org)
191 * (PR 8882).
192 */
193 {T_DIRECT, SIP_MEDIA_FIXED, microp, "2112*", "*"},
194 /*quirks*/ DA_Q_NO_SYNC_CACHE
195 },
196 {
197 /*
198 * Doesn't like the synchronize cache command.
199 * Reported by: Blaz Zupan <blaz@gold.amis.net>
200 */
201 {T_DIRECT, SIP_MEDIA_FIXED, "NEC", "D3847*", "*"},
202 /*quirks*/ DA_Q_NO_SYNC_CACHE
203 },
204 {
205 /*
206 * Doesn't like the synchronize cache command.
207 */
208 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "MAVERICK 540S", "*"},
209 /*quirks*/ DA_Q_NO_SYNC_CACHE
210 },
211 {
212 /*
213 * Doesn't like the synchronize cache command.
214 */
215 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "LPS525S", "*"},
216 /*quirks*/ DA_Q_NO_SYNC_CACHE
217 },
218 {
219 /*
220 * Doesn't work correctly with 6 byte reads/writes.
221 * Returns illegal request, and points to byte 9 of the
222 * 6-byte CDB.
223 * Reported by: Adam McDougall <bsdx@spawnet.com>
224 */
225 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 4*", "*"},
226 /*quirks*/ DA_Q_NO_6_BYTE
227 },
228 {
229 /* See above. */
230 {T_DIRECT, SIP_MEDIA_FIXED, quantum, "VIKING 2*", "*"},
231 /*quirks*/ DA_Q_NO_6_BYTE
232 },
233 {
234 /*
235 * The CISS RAID controllers do not support SYNC_CACHE
236 */
237 {T_DIRECT, SIP_MEDIA_FIXED, "COMPAQ", "RAID*", "*"},
238 /*quirks*/ DA_Q_NO_SYNC_CACHE
239 },
240 /* USB mass storage devices supported by umass(4) */
241 {
242 /*
243 * EXATELECOM (Sigmatel) i-Bead 100/105 USB Flash MP3 Player
244 * PR: kern/51675
245 */
246 {T_DIRECT, SIP_MEDIA_REMOVABLE, "EXATEL", "i-BEAD10*", "*"},
247 /*quirks*/ DA_Q_NO_SYNC_CACHE
248 },
249 {
250 /*
251 * Power Quotient Int. (PQI) USB flash key
252 * PR: kern/53067
253 */
254 {T_DIRECT, SIP_MEDIA_REMOVABLE, "Generic*", "USB Flash Disk*",
255 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
256 },
257 {
258 /*
259 * Creative Nomad MUVO mp3 player (USB)
260 * PR: kern/53094
261 */
262 {T_DIRECT, SIP_MEDIA_REMOVABLE, "CREATIVE", "NOMAD_MUVO", "*"},
263 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
264 },
265 {
266 /*
267 * Jungsoft NEXDISK USB flash key
268 * PR: kern/54737
269 */
270 {T_DIRECT, SIP_MEDIA_REMOVABLE, "JUNGSOFT", "NEXDISK*", "*"},
271 /*quirks*/ DA_Q_NO_SYNC_CACHE
272 },
273 {
274 /*
275 * FreeDik USB Mini Data Drive
276 * PR: kern/54786
277 */
278 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FreeDik*", "Mini Data Drive",
279 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
280 },
281 {
282 /*
283 * Sigmatel USB Flash MP3 Player
284 * PR: kern/57046
285 */
286 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SigmaTel", "MSCN", "*"},
287 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
288 },
289 {
290 /*
291 * Neuros USB Digital Audio Computer
292 * PR: kern/63645
293 */
294 {T_DIRECT, SIP_MEDIA_REMOVABLE, "NEUROS", "dig. audio comp.",
295 "*"}, /*quirks*/ DA_Q_NO_SYNC_CACHE
296 },
297 {
298 /*
299 * SEAGRAND NP-900 MP3 Player
300 * PR: kern/64563
301 */
302 {T_DIRECT, SIP_MEDIA_REMOVABLE, "SEAGRAND", "NP-900*", "*"},
303 /*quirks*/ DA_Q_NO_SYNC_CACHE|DA_Q_NO_PREVENT
304 },
305 {
306 /*
307 * iRiver iFP MP3 player (with UMS Firmware)
308 * PR: kern/54881, i386/63941, kern/66124
309 */
310 {T_DIRECT, SIP_MEDIA_REMOVABLE, "iRiver", "iFP*", "*"},
311 /*quirks*/ DA_Q_NO_SYNC_CACHE
312 },
313 {
314 /*
315 * Frontier Labs NEX IA+ Digital Audio Player, rev 1.10/0.01
316 * PR: kern/70158
317 */
318 {T_DIRECT, SIP_MEDIA_REMOVABLE, "FL" , "NexIA+*", "*"},
319 /*quirks*/ DA_Q_NO_SYNC_CACHE
320 },
321 };
322
323 static disk_strategy_t dastrategy;
324 static dumper_t dadump;
325 static periph_init_t dainit;
326 static void daasync(void *callback_arg, u_int32_t code,
327 struct cam_path *path, void *arg);
328 static void dasysctlinit(void *context, int pending);
329 static int dacmdsizesysctl(SYSCTL_HANDLER_ARGS);
330 static periph_ctor_t daregister;
331 static periph_dtor_t dacleanup;
332 static periph_start_t dastart;
333 static periph_oninv_t daoninvalidate;
334 static void dadone(struct cam_periph *periph,
335 union ccb *done_ccb);
336 static int daerror(union ccb *ccb, u_int32_t cam_flags,
337 u_int32_t sense_flags);
338 static void daprevent(struct cam_periph *periph, int action);
339 static int dagetcapacity(struct cam_periph *periph);
340 static void dasetgeom(struct cam_periph *periph, uint32_t block_len,
341 uint64_t maxsector);
342 static timeout_t dasendorderedtag;
343 static void dashutdown(void *arg, int howto);
344
345 #ifndef DA_DEFAULT_TIMEOUT
346 #define DA_DEFAULT_TIMEOUT 60 /* Timeout in seconds */
347 #endif
348
349 #ifndef DA_DEFAULT_RETRY
350 #define DA_DEFAULT_RETRY 4
351 #endif
352
353 static int da_retry_count = DA_DEFAULT_RETRY;
354 static int da_default_timeout = DA_DEFAULT_TIMEOUT;
355
356 SYSCTL_NODE(_kern_cam, OID_AUTO, da, CTLFLAG_RD, 0,
357 "CAM Direct Access Disk driver");
358 SYSCTL_INT(_kern_cam_da, OID_AUTO, retry_count, CTLFLAG_RW,
359 &da_retry_count, 0, "Normal I/O retry count");
360 TUNABLE_INT("kern.cam.da.retry_count", &da_retry_count);
361 SYSCTL_INT(_kern_cam_da, OID_AUTO, default_timeout, CTLFLAG_RW,
362 &da_default_timeout, 0, "Normal I/O timeout (in seconds)");
363 TUNABLE_INT("kern.cam.da.default_timeout", &da_default_timeout);
364
365 /*
366 * DA_ORDEREDTAG_INTERVAL determines how often, relative
367 * to the default timeout, we check to see whether an ordered
368 * tagged transaction is appropriate to prevent simple tag
369 * starvation. Since we'd like to ensure that there is at least
370 * 1/2 of the timeout length left for a starved transaction to
371 * complete after we've sent an ordered tag, we must poll at least
372 * four times in every timeout period. This takes care of the worst
373 * case where a starved transaction starts during an interval that
374 * meets the requirement "don't send an ordered tag" test so it takes
375 * us two intervals to determine that a tag must be sent.
376 */
377 #ifndef DA_ORDEREDTAG_INTERVAL
378 #define DA_ORDEREDTAG_INTERVAL 4
379 #endif
380
381 static struct periph_driver dadriver =
382 {
383 dainit, "da",
384 TAILQ_HEAD_INITIALIZER(dadriver.units), /* generation */ 0
385 };
386
387 PERIPHDRIVER_DECLARE(da, dadriver);
388
389 static SLIST_HEAD(,da_softc) softc_list;
390
391 static int
392 daopen(struct disk *dp)
393 {
394 struct cam_periph *periph;
395 struct da_softc *softc;
396 int unit;
397 int error;
398 int s;
399
400 s = splsoftcam();
401 periph = (struct cam_periph *)dp->d_drv1;
402 if (periph == NULL) {
403 splx(s);
404 return (ENXIO);
405 }
406 unit = periph->unit_number;
407
408 softc = (struct da_softc *)periph->softc;
409
410 CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
411 ("daopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
412 unit));
413
414 if ((error = cam_periph_lock(periph, PRIBIO|PCATCH)) != 0)
415 return (error); /* error code from tsleep */
416
417 if (cam_periph_acquire(periph) != CAM_REQ_CMP)
418 return(ENXIO);
419 softc->flags |= DA_FLAG_OPEN;
420
421 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0) {
422 /* Invalidate our pack information. */
423 softc->flags &= ~DA_FLAG_PACK_INVALID;
424 }
425 splx(s);
426
427 error = dagetcapacity(periph);
428
429 if (error == 0) {
430
431 softc->disk->d_sectorsize = softc->params.secsize;
432 softc->disk->d_mediasize = softc->params.secsize * (off_t)softc->params.sectors;
433 /* XXX: these are not actually "firmware" values, so they may be wrong */
434 softc->disk->d_fwsectors = softc->params.secs_per_track;
435 softc->disk->d_fwheads = softc->params.heads;
436 softc->disk->d_devstat->block_size = softc->params.secsize;
437 softc->disk->d_devstat->flags &= ~DEVSTAT_BS_UNAVAILABLE;
438 }
439
440 if (error == 0) {
441 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0 &&
442 (softc->quirks & DA_Q_NO_PREVENT) == 0)
443 daprevent(periph, PR_PREVENT);
444 } else {
445 softc->flags &= ~DA_FLAG_OPEN;
446 cam_periph_release(periph);
447 }
448 cam_periph_unlock(periph);
449 return (error);
450 }
451
452 static int
453 daclose(struct disk *dp)
454 {
455 struct cam_periph *periph;
456 struct da_softc *softc;
457 int error;
458
459 periph = (struct cam_periph *)dp->d_drv1;
460 if (periph == NULL)
461 return (ENXIO);
462
463 softc = (struct da_softc *)periph->softc;
464
465 if ((error = cam_periph_lock(periph, PRIBIO)) != 0) {
466 return (error); /* error code from tsleep */
467 }
468
469 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
470 union ccb *ccb;
471
472 ccb = cam_periph_getccb(periph, /*priority*/1);
473
474 scsi_synchronize_cache(&ccb->csio,
475 /*retries*/1,
476 /*cbfcnp*/dadone,
477 MSG_SIMPLE_Q_TAG,
478 /*begin_lba*/0,/* Cover the whole disk */
479 /*lb_count*/0,
480 SSD_FULL_SIZE,
481 5 * 60 * 1000);
482
483 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
484 /*sense_flags*/SF_RETRY_UA,
485 softc->disk->d_devstat);
486
487 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
488 if ((ccb->ccb_h.status & CAM_STATUS_MASK) ==
489 CAM_SCSI_STATUS_ERROR) {
490 int asc, ascq;
491 int sense_key, error_code;
492
493 scsi_extract_sense(&ccb->csio.sense_data,
494 &error_code,
495 &sense_key,
496 &asc, &ascq);
497 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
498 scsi_sense_print(&ccb->csio);
499 } else {
500 xpt_print_path(periph->path);
501 printf("Synchronize cache failed, status "
502 "== 0x%x, scsi status == 0x%x\n",
503 ccb->csio.ccb_h.status,
504 ccb->csio.scsi_status);
505 }
506 }
507
508 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
509 cam_release_devq(ccb->ccb_h.path,
510 /*relsim_flags*/0,
511 /*reduction*/0,
512 /*timeout*/0,
513 /*getcount_only*/0);
514
515 xpt_release_ccb(ccb);
516
517 }
518
519 if ((softc->flags & DA_FLAG_PACK_REMOVABLE) != 0) {
520 if ((softc->quirks & DA_Q_NO_PREVENT) == 0)
521 daprevent(periph, PR_ALLOW);
522 /*
523 * If we've got removeable media, mark the blocksize as
524 * unavailable, since it could change when new media is
525 * inserted.
526 */
527 softc->disk->d_devstat->flags |= DEVSTAT_BS_UNAVAILABLE;
528 }
529
530 softc->flags &= ~DA_FLAG_OPEN;
531 cam_periph_unlock(periph);
532 cam_periph_release(periph);
533 return (0);
534 }
535
536 /*
537 * Actually translate the requested transfer into one the physical driver
538 * can understand. The transfer is described by a buf and will include
539 * only one physical transfer.
540 */
541 static void
542 dastrategy(struct bio *bp)
543 {
544 struct cam_periph *periph;
545 struct da_softc *softc;
546 int s;
547
548 periph = (struct cam_periph *)bp->bio_disk->d_drv1;
549 if (periph == NULL) {
550 biofinish(bp, NULL, ENXIO);
551 return;
552 }
553 softc = (struct da_softc *)periph->softc;
554 #if 0
555 /*
556 * check it's not too big a transfer for our adapter
557 */
558 scsi_minphys(bp,&sd_switch);
559 #endif
560
561 /*
562 * Mask interrupts so that the pack cannot be invalidated until
563 * after we are in the queue. Otherwise, we might not properly
564 * clean up one of the buffers.
565 */
566 s = splbio();
567
568 /*
569 * If the device has been made invalid, error out
570 */
571 if ((softc->flags & DA_FLAG_PACK_INVALID)) {
572 splx(s);
573 biofinish(bp, NULL, ENXIO);
574 return;
575 }
576
577 /*
578 * Place it in the queue of disk activities for this disk
579 */
580 bioq_disksort(&softc->bio_queue, bp);
581
582 splx(s);
583
584 /*
585 * Schedule ourselves for performing the work.
586 */
587 xpt_schedule(periph, /* XXX priority */1);
588
589 return;
590 }
591
592 static int
593 dadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
594 {
595 struct cam_periph *periph;
596 struct da_softc *softc;
597 u_int secsize;
598 struct ccb_scsiio csio;
599 struct disk *dp;
600
601 dp = arg;
602 periph = dp->d_drv1;
603 if (periph == NULL)
604 return (ENXIO);
605 softc = (struct da_softc *)periph->softc;
606 secsize = softc->params.secsize;
607
608 if ((softc->flags & DA_FLAG_PACK_INVALID) != 0)
609 return (ENXIO);
610
611 if (length > 0) {
612 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
613 csio.ccb_h.ccb_state = DA_CCB_DUMP;
614 scsi_read_write(&csio,
615 /*retries*/1,
616 dadone,
617 MSG_ORDERED_Q_TAG,
618 /*read*/FALSE,
619 /*byte2*/0,
620 /*minimum_cmd_size*/ softc->minimum_cmd_size,
621 offset / secsize,
622 length / secsize,
623 /*data_ptr*/(u_int8_t *) virtual,
624 /*dxfer_len*/length,
625 /*sense_len*/SSD_FULL_SIZE,
626 DA_DEFAULT_TIMEOUT * 1000);
627 xpt_polled_action((union ccb *)&csio);
628
629 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
630 printf("Aborting dump due to I/O error.\n");
631 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
632 CAM_SCSI_STATUS_ERROR)
633 scsi_sense_print(&csio);
634 else
635 printf("status == 0x%x, scsi status == 0x%x\n",
636 csio.ccb_h.status, csio.scsi_status);
637 return(EIO);
638 }
639 return(0);
640 }
641
642 /*
643 * Sync the disk cache contents to the physical media.
644 */
645 if ((softc->quirks & DA_Q_NO_SYNC_CACHE) == 0) {
646
647 xpt_setup_ccb(&csio.ccb_h, periph->path, /*priority*/1);
648 csio.ccb_h.ccb_state = DA_CCB_DUMP;
649 scsi_synchronize_cache(&csio,
650 /*retries*/1,
651 /*cbfcnp*/dadone,
652 MSG_SIMPLE_Q_TAG,
653 /*begin_lba*/0,/* Cover the whole disk */
654 /*lb_count*/0,
655 SSD_FULL_SIZE,
656 5 * 60 * 1000);
657 xpt_polled_action((union ccb *)&csio);
658
659 if ((csio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
660 if ((csio.ccb_h.status & CAM_STATUS_MASK) ==
661 CAM_SCSI_STATUS_ERROR) {
662 int asc, ascq;
663 int sense_key, error_code;
664
665 scsi_extract_sense(&csio.sense_data,
666 &error_code,
667 &sense_key,
668 &asc, &ascq);
669 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
670 scsi_sense_print(&csio);
671 } else {
672 xpt_print_path(periph->path);
673 printf("Synchronize cache failed, status "
674 "== 0x%x, scsi status == 0x%x\n",
675 csio.ccb_h.status, csio.scsi_status);
676 }
677 }
678 }
679 return (0);
680 }
681
682 static void
683 dainit(void)
684 {
685 cam_status status;
686 struct cam_path *path;
687
688 SLIST_INIT(&softc_list);
689
690 /*
691 * Install a global async callback. This callback will
692 * receive async callbacks like "new device found".
693 */
694 status = xpt_create_path(&path, /*periph*/NULL, CAM_XPT_PATH_ID,
695 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD);
696
697 if (status == CAM_REQ_CMP) {
698 struct ccb_setasync csa;
699
700 xpt_setup_ccb(&csa.ccb_h, path, /*priority*/5);
701 csa.ccb_h.func_code = XPT_SASYNC_CB;
702 csa.event_enable = AC_FOUND_DEVICE;
703 csa.callback = daasync;
704 csa.callback_arg = NULL;
705 xpt_action((union ccb *)&csa);
706 status = csa.ccb_h.status;
707 xpt_free_path(path);
708 }
709
710 if (status != CAM_REQ_CMP) {
711 printf("da: Failed to attach master async callback "
712 "due to status 0x%x!\n", status);
713 } else {
714
715 /*
716 * Schedule a periodic event to occasionally send an
717 * ordered tag to a device.
718 */
719 timeout(dasendorderedtag, NULL,
720 (DA_DEFAULT_TIMEOUT * hz) / DA_ORDEREDTAG_INTERVAL);
721
722 /* Register our shutdown event handler */
723 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, dashutdown,
724 NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
725 printf("dainit: shutdown event registration failed!\n");
726 }
727 }
728
729 static void
730 daoninvalidate(struct cam_periph *periph)
731 {
732 int s;
733 struct da_softc *softc;
734 struct ccb_setasync csa;
735
736 softc = (struct da_softc *)periph->softc;
737
738 /*
739 * De-register any async callbacks.
740 */
741 xpt_setup_ccb(&csa.ccb_h, periph->path,
742 /* priority */ 5);
743 csa.ccb_h.func_code = XPT_SASYNC_CB;
744 csa.event_enable = 0;
745 csa.callback = daasync;
746 csa.callback_arg = periph;
747 xpt_action((union ccb *)&csa);
748
749 softc->flags |= DA_FLAG_PACK_INVALID;
750
751 /*
752 * Although the oninvalidate() routines are always called at
753 * splsoftcam, we need to be at splbio() here to keep the buffer
754 * queue from being modified while we traverse it.
755 */
756 s = splbio();
757
758 /*
759 * Return all queued I/O with ENXIO.
760 * XXX Handle any transactions queued to the card
761 * with XPT_ABORT_CCB.
762 */
763 bioq_flush(&softc->bio_queue, NULL, ENXIO);
764 splx(s);
765
766 SLIST_REMOVE(&softc_list, softc, da_softc, links);
767
768 xpt_print_path(periph->path);
769 printf("lost device\n");
770 }
771
772 static void
773 dacleanup(struct cam_periph *periph)
774 {
775 struct da_softc *softc;
776
777 softc = (struct da_softc *)periph->softc;
778
779 xpt_print_path(periph->path);
780 printf("removing device entry\n");
781 /*
782 * If we can't free the sysctl tree, oh well...
783 */
784 if ((softc->flags & DA_FLAG_SCTX_INIT) != 0
785 && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
786 xpt_print_path(periph->path);
787 printf("can't remove sysctl context\n");
788 }
789 disk_destroy(softc->disk);
790 free(softc, M_DEVBUF);
791 }
792
793 static void
794 daasync(void *callback_arg, u_int32_t code,
795 struct cam_path *path, void *arg)
796 {
797 struct cam_periph *periph;
798
799 periph = (struct cam_periph *)callback_arg;
800 switch (code) {
801 case AC_FOUND_DEVICE:
802 {
803 struct ccb_getdev *cgd;
804 cam_status status;
805
806 cgd = (struct ccb_getdev *)arg;
807 if (cgd == NULL)
808 break;
809
810 if (SID_TYPE(&cgd->inq_data) != T_DIRECT
811 && SID_TYPE(&cgd->inq_data) != T_RBC
812 && SID_TYPE(&cgd->inq_data) != T_OPTICAL)
813 break;
814
815 /*
816 * Allocate a peripheral instance for
817 * this device and start the probe
818 * process.
819 */
820 status = cam_periph_alloc(daregister, daoninvalidate,
821 dacleanup, dastart,
822 "da", CAM_PERIPH_BIO,
823 cgd->ccb_h.path, daasync,
824 AC_FOUND_DEVICE, cgd);
825
826 if (status != CAM_REQ_CMP
827 && status != CAM_REQ_INPROG)
828 printf("daasync: Unable to attach to new device "
829 "due to status 0x%x\n", status);
830 break;
831 }
832 case AC_SENT_BDR:
833 case AC_BUS_RESET:
834 {
835 struct da_softc *softc;
836 struct ccb_hdr *ccbh;
837 int s;
838
839 softc = (struct da_softc *)periph->softc;
840 s = splsoftcam();
841 /*
842 * Don't fail on the expected unit attention
843 * that will occur.
844 */
845 softc->flags |= DA_FLAG_RETRY_UA;
846 LIST_FOREACH(ccbh, &softc->pending_ccbs, periph_links.le)
847 ccbh->ccb_state |= DA_CCB_RETRY_UA;
848 splx(s);
849 /* FALLTHROUGH*/
850 }
851 default:
852 cam_periph_async(periph, code, path, arg);
853 break;
854 }
855 }
856
857 static void
858 dasysctlinit(void *context, int pending)
859 {
860 struct cam_periph *periph;
861 struct da_softc *softc;
862 char tmpstr[80], tmpstr2[80];
863
864 periph = (struct cam_periph *)context;
865 softc = (struct da_softc *)periph->softc;
866
867 snprintf(tmpstr, sizeof(tmpstr), "CAM DA unit %d", periph->unit_number);
868 snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
869
870 mtx_lock(&Giant);
871 sysctl_ctx_init(&softc->sysctl_ctx);
872 softc->flags |= DA_FLAG_SCTX_INIT;
873 softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
874 SYSCTL_STATIC_CHILDREN(_kern_cam_da), OID_AUTO, tmpstr2,
875 CTLFLAG_RD, 0, tmpstr);
876 if (softc->sysctl_tree == NULL) {
877 printf("dasysctlinit: unable to allocate sysctl tree\n");
878 return;
879 }
880
881 /*
882 * Now register the sysctl handler, so the user can the value on
883 * the fly.
884 */
885 SYSCTL_ADD_PROC(&softc->sysctl_ctx,SYSCTL_CHILDREN(softc->sysctl_tree),
886 OID_AUTO, "minimum_cmd_size", CTLTYPE_INT | CTLFLAG_RW,
887 &softc->minimum_cmd_size, 0, dacmdsizesysctl, "I",
888 "Minimum CDB size");
889
890 mtx_unlock(&Giant);
891 }
892
893 static int
894 dacmdsizesysctl(SYSCTL_HANDLER_ARGS)
895 {
896 int error, value;
897
898 value = *(int *)arg1;
899
900 error = sysctl_handle_int(oidp, &value, 0, req);
901
902 if ((error != 0)
903 || (req->newptr == NULL))
904 return (error);
905
906 /*
907 * Acceptable values here are 6, 10, 12 or 16.
908 */
909 if (value < 6)
910 value = 6;
911 else if ((value > 6)
912 && (value <= 10))
913 value = 10;
914 else if ((value > 10)
915 && (value <= 12))
916 value = 12;
917 else if (value > 12)
918 value = 16;
919
920 *(int *)arg1 = value;
921
922 return (0);
923 }
924
925 static cam_status
926 daregister(struct cam_periph *periph, void *arg)
927 {
928 int s;
929 struct da_softc *softc;
930 struct ccb_setasync csa;
931 struct ccb_pathinq cpi;
932 struct ccb_getdev *cgd;
933 char tmpstr[80];
934 caddr_t match;
935
936 cgd = (struct ccb_getdev *)arg;
937 if (periph == NULL) {
938 printf("daregister: periph was NULL!!\n");
939 return(CAM_REQ_CMP_ERR);
940 }
941
942 if (cgd == NULL) {
943 printf("daregister: no getdev CCB, can't register device\n");
944 return(CAM_REQ_CMP_ERR);
945 }
946
947 softc = (struct da_softc *)malloc(sizeof(*softc), M_DEVBUF,
948 M_NOWAIT|M_ZERO);
949
950 if (softc == NULL) {
951 printf("daregister: Unable to probe new device. "
952 "Unable to allocate softc\n");
953 return(CAM_REQ_CMP_ERR);
954 }
955
956 LIST_INIT(&softc->pending_ccbs);
957 softc->state = DA_STATE_PROBE;
958 bioq_init(&softc->bio_queue);
959 if (SID_IS_REMOVABLE(&cgd->inq_data))
960 softc->flags |= DA_FLAG_PACK_REMOVABLE;
961 if ((cgd->inq_data.flags & SID_CmdQue) != 0)
962 softc->flags |= DA_FLAG_TAGGED_QUEUING;
963
964 periph->softc = softc;
965
966 /*
967 * See if this device has any quirks.
968 */
969 match = cam_quirkmatch((caddr_t)&cgd->inq_data,
970 (caddr_t)da_quirk_table,
971 sizeof(da_quirk_table)/sizeof(*da_quirk_table),
972 sizeof(*da_quirk_table), scsi_inquiry_match);
973
974 if (match != NULL)
975 softc->quirks = ((struct da_quirk_entry *)match)->quirks;
976 else
977 softc->quirks = DA_Q_NONE;
978
979 /* Check if the SIM does not want 6 byte commands */
980 xpt_setup_ccb(&cpi.ccb_h, periph->path, /*priority*/1);
981 cpi.ccb_h.func_code = XPT_PATH_INQ;
982 xpt_action((union ccb *)&cpi);
983 if (cpi.ccb_h.status == CAM_REQ_CMP && (cpi.hba_misc & PIM_NO_6_BYTE))
984 softc->quirks |= DA_Q_NO_6_BYTE;
985
986 TASK_INIT(&softc->sysctl_task, 0, dasysctlinit, periph);
987
988 /*
989 * RBC devices don't have to support READ(6), only READ(10).
990 */
991 if (softc->quirks & DA_Q_NO_6_BYTE || SID_TYPE(&cgd->inq_data) == T_RBC)
992 softc->minimum_cmd_size = 10;
993 else
994 softc->minimum_cmd_size = 6;
995
996 /*
997 * Load the user's default, if any.
998 */
999 snprintf(tmpstr, sizeof(tmpstr), "kern.cam.da.%d.minimum_cmd_size",
1000 periph->unit_number);
1001 TUNABLE_INT_FETCH(tmpstr, &softc->minimum_cmd_size);
1002
1003 /*
1004 * 6, 10, 12 and 16 are the currently permissible values.
1005 */
1006 if (softc->minimum_cmd_size < 6)
1007 softc->minimum_cmd_size = 6;
1008 else if ((softc->minimum_cmd_size > 6)
1009 && (softc->minimum_cmd_size <= 10))
1010 softc->minimum_cmd_size = 10;
1011 else if ((softc->minimum_cmd_size > 10)
1012 && (softc->minimum_cmd_size <= 12))
1013 softc->minimum_cmd_size = 12;
1014 else if (softc->minimum_cmd_size > 12)
1015 softc->minimum_cmd_size = 16;
1016
1017 /*
1018 * Block our timeout handler while we
1019 * add this softc to the dev list.
1020 */
1021 s = splsoftclock();
1022 SLIST_INSERT_HEAD(&softc_list, softc, links);
1023 splx(s);
1024
1025 /*
1026 * Register this media as a disk
1027 */
1028
1029 softc->disk = disk_alloc();
1030 softc->disk->d_open = daopen;
1031 softc->disk->d_close = daclose;
1032 softc->disk->d_strategy = dastrategy;
1033 softc->disk->d_dump = dadump;
1034 softc->disk->d_name = "da";
1035 softc->disk->d_drv1 = periph;
1036 softc->disk->d_maxsize = DFLTPHYS; /* XXX: probably not arbitrary */
1037 softc->disk->d_unit = periph->unit_number;
1038 softc->disk->d_flags = DISKFLAG_NEEDSGIANT;
1039 disk_create(softc->disk, DISK_VERSION);
1040
1041 /*
1042 * Add async callbacks for bus reset and
1043 * bus device reset calls. I don't bother
1044 * checking if this fails as, in most cases,
1045 * the system will function just fine without
1046 * them and the only alternative would be to
1047 * not attach the device on failure.
1048 */
1049 xpt_setup_ccb(&csa.ccb_h, periph->path, /*priority*/5);
1050 csa.ccb_h.func_code = XPT_SASYNC_CB;
1051 csa.event_enable = AC_SENT_BDR | AC_BUS_RESET | AC_LOST_DEVICE;
1052 csa.callback = daasync;
1053 csa.callback_arg = periph;
1054 xpt_action((union ccb *)&csa);
1055 /*
1056 * Lock this peripheral until we are setup.
1057 * This first call can't block
1058 */
1059 (void)cam_periph_lock(periph, PRIBIO);
1060 xpt_schedule(periph, /*priority*/5);
1061
1062 return(CAM_REQ_CMP);
1063 }
1064
1065 static void
1066 dastart(struct cam_periph *periph, union ccb *start_ccb)
1067 {
1068 struct da_softc *softc;
1069
1070 softc = (struct da_softc *)periph->softc;
1071
1072
1073 switch (softc->state) {
1074 case DA_STATE_NORMAL:
1075 {
1076 /* Pull a buffer from the queue and get going on it */
1077 struct bio *bp;
1078 int s;
1079
1080 /*
1081 * See if there is a buf with work for us to do..
1082 */
1083 s = splbio();
1084 bp = bioq_first(&softc->bio_queue);
1085 if (periph->immediate_priority <= periph->pinfo.priority) {
1086 CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
1087 ("queuing for immediate ccb\n"));
1088 start_ccb->ccb_h.ccb_state = DA_CCB_WAITING;
1089 SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
1090 periph_links.sle);
1091 periph->immediate_priority = CAM_PRIORITY_NONE;
1092 splx(s);
1093 wakeup(&periph->ccb_list);
1094 } else if (bp == NULL) {
1095 splx(s);
1096 xpt_release_ccb(start_ccb);
1097 } else {
1098 int oldspl;
1099 u_int8_t tag_code;
1100
1101 bioq_remove(&softc->bio_queue, bp);
1102
1103 if ((softc->flags & DA_FLAG_NEED_OTAG) != 0) {
1104 softc->flags &= ~DA_FLAG_NEED_OTAG;
1105 softc->ordered_tag_count++;
1106 tag_code = MSG_ORDERED_Q_TAG;
1107 } else {
1108 tag_code = MSG_SIMPLE_Q_TAG;
1109 }
1110 scsi_read_write(&start_ccb->csio,
1111 /*retries*/da_retry_count,
1112 /*cbfcnp*/dadone,
1113 /*tag_action*/tag_code,
1114 /*read_op*/bp->bio_cmd == BIO_READ,
1115 /*byte2*/0,
1116 softc->minimum_cmd_size,
1117 /*lba*/bp->bio_pblkno,
1118 /*block_count*/bp->bio_bcount /
1119 softc->params.secsize,
1120 /*data_ptr*/ bp->bio_data,
1121 /*dxfer_len*/ bp->bio_bcount,
1122 /*sense_len*/SSD_FULL_SIZE,
1123 /*timeout*/da_default_timeout*1000);
1124 start_ccb->ccb_h.ccb_state = DA_CCB_BUFFER_IO;
1125
1126 /*
1127 * Block out any asyncronous callbacks
1128 * while we touch the pending ccb list.
1129 */
1130 oldspl = splcam();
1131 LIST_INSERT_HEAD(&softc->pending_ccbs,
1132 &start_ccb->ccb_h, periph_links.le);
1133 softc->outstanding_cmds++;
1134 splx(oldspl);
1135
1136 /* We expect a unit attention from this device */
1137 if ((softc->flags & DA_FLAG_RETRY_UA) != 0) {
1138 start_ccb->ccb_h.ccb_state |= DA_CCB_RETRY_UA;
1139 softc->flags &= ~DA_FLAG_RETRY_UA;
1140 }
1141
1142 start_ccb->ccb_h.ccb_bp = bp;
1143 bp = bioq_first(&softc->bio_queue);
1144 splx(s);
1145
1146 xpt_action(start_ccb);
1147 }
1148
1149 if (bp != NULL) {
1150 /* Have more work to do, so ensure we stay scheduled */
1151 xpt_schedule(periph, /* XXX priority */1);
1152 }
1153 break;
1154 }
1155 case DA_STATE_PROBE:
1156 {
1157 struct ccb_scsiio *csio;
1158 struct scsi_read_capacity_data *rcap;
1159
1160 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcap),
1161 M_TEMP,
1162 M_NOWAIT);
1163 if (rcap == NULL) {
1164 printf("dastart: Couldn't malloc read_capacity data\n");
1165 /* da_free_periph??? */
1166 break;
1167 }
1168 csio = &start_ccb->csio;
1169 scsi_read_capacity(csio,
1170 /*retries*/4,
1171 dadone,
1172 MSG_SIMPLE_Q_TAG,
1173 rcap,
1174 SSD_FULL_SIZE,
1175 /*timeout*/5000);
1176 start_ccb->ccb_h.ccb_bp = NULL;
1177 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE;
1178 xpt_action(start_ccb);
1179 break;
1180 }
1181 case DA_STATE_PROBE2:
1182 {
1183 struct ccb_scsiio *csio;
1184 struct scsi_read_capacity_data_long *rcaplong;
1185
1186 rcaplong = (struct scsi_read_capacity_data_long *)
1187 malloc(sizeof(*rcaplong), M_TEMP, M_NOWAIT);
1188 if (rcaplong == NULL) {
1189 printf("dastart: Couldn't malloc read_capacity data\n");
1190 /* da_free_periph??? */
1191 break;
1192 }
1193 csio = &start_ccb->csio;
1194 scsi_read_capacity_16(csio,
1195 /*retries*/ 4,
1196 /*cbfcnp*/ dadone,
1197 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1198 /*lba*/ 0,
1199 /*reladr*/ 0,
1200 /*pmi*/ 0,
1201 rcaplong,
1202 /*sense_len*/ SSD_FULL_SIZE,
1203 /*timeout*/ 60000);
1204 start_ccb->ccb_h.ccb_bp = NULL;
1205 start_ccb->ccb_h.ccb_state = DA_CCB_PROBE2;
1206 xpt_action(start_ccb);
1207 break;
1208 }
1209 }
1210 }
1211
1212 static int
1213 cmd6workaround(union ccb *ccb)
1214 {
1215 struct scsi_rw_6 cmd6;
1216 struct scsi_rw_10 *cmd10;
1217 struct da_softc *softc;
1218 u_int8_t *cdb;
1219 int frozen;
1220
1221 cdb = ccb->csio.cdb_io.cdb_bytes;
1222
1223 /* Translation only possible if CDB is an array and cmd is R/W6 */
1224 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0 ||
1225 (*cdb != READ_6 && *cdb != WRITE_6))
1226 return 0;
1227
1228 xpt_print_path(ccb->ccb_h.path);
1229 printf("READ(6)/WRITE(6) not supported, "
1230 "increasing minimum_cmd_size to 10.\n");
1231 softc = (struct da_softc *)xpt_path_periph(ccb->ccb_h.path)->softc;
1232 softc->minimum_cmd_size = 10;
1233
1234 bcopy(cdb, &cmd6, sizeof(struct scsi_rw_6));
1235 cmd10 = (struct scsi_rw_10 *)cdb;
1236 cmd10->opcode = (cmd6.opcode == READ_6) ? READ_10 : WRITE_10;
1237 cmd10->byte2 = 0;
1238 scsi_ulto4b(scsi_3btoul(cmd6.addr), cmd10->addr);
1239 cmd10->reserved = 0;
1240 scsi_ulto2b(cmd6.length, cmd10->length);
1241 cmd10->control = cmd6.control;
1242 ccb->csio.cdb_len = sizeof(*cmd10);
1243
1244 /* Requeue request, unfreezing queue if necessary */
1245 frozen = (ccb->ccb_h.status & CAM_DEV_QFRZN) != 0;
1246 ccb->ccb_h.status = CAM_REQUEUE_REQ;
1247 xpt_action(ccb);
1248 if (frozen) {
1249 cam_release_devq(ccb->ccb_h.path,
1250 /*relsim_flags*/0,
1251 /*reduction*/0,
1252 /*timeout*/0,
1253 /*getcount_only*/0);
1254 }
1255 return (ERESTART);
1256 }
1257
1258 static void
1259 dadone(struct cam_periph *periph, union ccb *done_ccb)
1260 {
1261 struct da_softc *softc;
1262 struct ccb_scsiio *csio;
1263
1264 softc = (struct da_softc *)periph->softc;
1265 csio = &done_ccb->csio;
1266 switch (csio->ccb_h.ccb_state & DA_CCB_TYPE_MASK) {
1267 case DA_CCB_BUFFER_IO:
1268 {
1269 struct bio *bp;
1270 int oldspl;
1271
1272 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1273 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1274 int error;
1275 int s;
1276 int sf;
1277
1278 if ((csio->ccb_h.ccb_state & DA_CCB_RETRY_UA) != 0)
1279 sf = SF_RETRY_UA;
1280 else
1281 sf = 0;
1282
1283 error = daerror(done_ccb, CAM_RETRY_SELTO, sf);
1284 if (error == ERESTART) {
1285 /*
1286 * A retry was scheuled, so
1287 * just return.
1288 */
1289 return;
1290 }
1291 if (error != 0) {
1292
1293 s = splbio();
1294
1295 if (error == ENXIO) {
1296 /*
1297 * Catastrophic error. Mark our pack as
1298 * invalid.
1299 */
1300 /* XXX See if this is really a media
1301 * change first.
1302 */
1303 xpt_print_path(periph->path);
1304 printf("Invalidating pack\n");
1305 softc->flags |= DA_FLAG_PACK_INVALID;
1306 }
1307
1308 /*
1309 * return all queued I/O with EIO, so that
1310 * the client can retry these I/Os in the
1311 * proper order should it attempt to recover.
1312 */
1313 bioq_flush(&softc->bio_queue, NULL, EIO);
1314 splx(s);
1315 bp->bio_error = error;
1316 bp->bio_resid = bp->bio_bcount;
1317 bp->bio_flags |= BIO_ERROR;
1318 } else {
1319 bp->bio_resid = csio->resid;
1320 bp->bio_error = 0;
1321 if (bp->bio_resid != 0)
1322 bp->bio_flags |= BIO_ERROR;
1323 }
1324 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1325 cam_release_devq(done_ccb->ccb_h.path,
1326 /*relsim_flags*/0,
1327 /*reduction*/0,
1328 /*timeout*/0,
1329 /*getcount_only*/0);
1330 } else {
1331 if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1332 panic("REQ_CMP with QFRZN");
1333 bp->bio_resid = csio->resid;
1334 if (csio->resid > 0)
1335 bp->bio_flags |= BIO_ERROR;
1336 }
1337
1338 /*
1339 * Block out any asyncronous callbacks
1340 * while we touch the pending ccb list.
1341 */
1342 oldspl = splcam();
1343 LIST_REMOVE(&done_ccb->ccb_h, periph_links.le);
1344 softc->outstanding_cmds--;
1345 if (softc->outstanding_cmds == 0)
1346 softc->flags |= DA_FLAG_WENT_IDLE;
1347 splx(oldspl);
1348
1349 biodone(bp);
1350 break;
1351 }
1352 case DA_CCB_PROBE:
1353 case DA_CCB_PROBE2:
1354 {
1355 struct scsi_read_capacity_data *rdcap;
1356 struct scsi_read_capacity_data_long *rcaplong;
1357 char announce_buf[80];
1358
1359 rdcap = NULL;
1360 rcaplong = NULL;
1361 if (softc->state == DA_STATE_PROBE)
1362 rdcap =(struct scsi_read_capacity_data *)csio->data_ptr;
1363 else
1364 rcaplong = (struct scsi_read_capacity_data_long *)
1365 csio->data_ptr;
1366
1367 if ((csio->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_CMP) {
1368 struct disk_params *dp;
1369 uint32_t block_size;
1370 uint64_t maxsector;
1371
1372 if (softc->state == DA_STATE_PROBE) {
1373 block_size = scsi_4btoul(rdcap->length);
1374 maxsector = scsi_4btoul(rdcap->addr);
1375
1376 /*
1377 * According to SBC-2, if the standard 10
1378 * byte READ CAPACITY command returns 2^32,
1379 * we should issue the 16 byte version of
1380 * the command, since the device in question
1381 * has more sectors than can be represented
1382 * with the short version of the command.
1383 */
1384 if (maxsector == 0xffffffff) {
1385 softc->state = DA_STATE_PROBE2;
1386 free(rdcap, M_TEMP);
1387 xpt_release_ccb(done_ccb);
1388 xpt_schedule(periph, /*priority*/5);
1389 return;
1390 }
1391 } else {
1392 block_size = scsi_4btoul(rcaplong->length);
1393 maxsector = scsi_8btou64(rcaplong->addr);
1394 }
1395 dasetgeom(periph, block_size, maxsector);
1396 dp = &softc->params;
1397 snprintf(announce_buf, sizeof(announce_buf),
1398 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
1399 (uintmax_t) (((uintmax_t)dp->secsize *
1400 dp->sectors) / (1024*1024)),
1401 (uintmax_t)dp->sectors,
1402 dp->secsize, dp->heads, dp->secs_per_track,
1403 dp->cylinders);
1404 } else {
1405 int error;
1406
1407 announce_buf[0] = '\0';
1408
1409 /*
1410 * Retry any UNIT ATTENTION type errors. They
1411 * are expected at boot.
1412 */
1413 error = daerror(done_ccb, CAM_RETRY_SELTO,
1414 SF_RETRY_UA|SF_NO_PRINT);
1415 if (error == ERESTART) {
1416 /*
1417 * A retry was scheuled, so
1418 * just return.
1419 */
1420 return;
1421 } else if (error != 0) {
1422 struct scsi_sense_data *sense;
1423 int asc, ascq;
1424 int sense_key, error_code;
1425 int have_sense;
1426 cam_status status;
1427 struct ccb_getdev cgd;
1428
1429 /* Don't wedge this device's queue */
1430 status = done_ccb->ccb_h.status;
1431 if ((status & CAM_DEV_QFRZN) != 0)
1432 cam_release_devq(done_ccb->ccb_h.path,
1433 /*relsim_flags*/0,
1434 /*reduction*/0,
1435 /*timeout*/0,
1436 /*getcount_only*/0);
1437
1438
1439 xpt_setup_ccb(&cgd.ccb_h,
1440 done_ccb->ccb_h.path,
1441 /* priority */ 1);
1442 cgd.ccb_h.func_code = XPT_GDEV_TYPE;
1443 xpt_action((union ccb *)&cgd);
1444
1445 if (((csio->ccb_h.flags & CAM_SENSE_PHYS) != 0)
1446 || ((csio->ccb_h.flags & CAM_SENSE_PTR) != 0)
1447 || ((status & CAM_AUTOSNS_VALID) == 0))
1448 have_sense = FALSE;
1449 else
1450 have_sense = TRUE;
1451
1452 if (have_sense) {
1453 sense = &csio->sense_data;
1454 scsi_extract_sense(sense, &error_code,
1455 &sense_key,
1456 &asc, &ascq);
1457 }
1458 /*
1459 * Attach to anything that claims to be a
1460 * direct access or optical disk device,
1461 * as long as it doesn't return a "Logical
1462 * unit not supported" (0x25) error.
1463 */
1464 if ((have_sense) && (asc != 0x25)
1465 && (error_code == SSD_CURRENT_ERROR)) {
1466 const char *sense_key_desc;
1467 const char *asc_desc;
1468
1469 scsi_sense_desc(sense_key, asc, ascq,
1470 &cgd.inq_data,
1471 &sense_key_desc,
1472 &asc_desc);
1473 snprintf(announce_buf,
1474 sizeof(announce_buf),
1475 "Attempt to query device "
1476 "size failed: %s, %s",
1477 sense_key_desc,
1478 asc_desc);
1479 } else {
1480 if (have_sense)
1481 scsi_sense_print(
1482 &done_ccb->csio);
1483 else {
1484 xpt_print_path(periph->path);
1485 printf("got CAM status %#x\n",
1486 done_ccb->ccb_h.status);
1487 }
1488
1489 xpt_print_path(periph->path);
1490 printf("fatal error, failed"
1491 " to attach to device\n");
1492
1493 /*
1494 * Free up resources.
1495 */
1496 cam_periph_invalidate(periph);
1497 }
1498 }
1499 }
1500 free(csio->data_ptr, M_TEMP);
1501 if (announce_buf[0] != '\0') {
1502 xpt_announce_periph(periph, announce_buf);
1503 /*
1504 * Create our sysctl variables, now that we know
1505 * we have successfully attached.
1506 */
1507 taskqueue_enqueue(taskqueue_thread,&softc->sysctl_task);
1508 }
1509 softc->state = DA_STATE_NORMAL;
1510 /*
1511 * Since our peripheral may be invalidated by an error
1512 * above or an external event, we must release our CCB
1513 * before releasing the probe lock on the peripheral.
1514 * The peripheral will only go away once the last lock
1515 * is removed, and we need it around for the CCB release
1516 * operation.
1517 */
1518 xpt_release_ccb(done_ccb);
1519 cam_periph_unlock(periph);
1520 return;
1521 }
1522 case DA_CCB_WAITING:
1523 {
1524 /* Caller will release the CCB */
1525 wakeup(&done_ccb->ccb_h.cbfcnp);
1526 return;
1527 }
1528 case DA_CCB_DUMP:
1529 /* No-op. We're polling */
1530 return;
1531 default:
1532 break;
1533 }
1534 xpt_release_ccb(done_ccb);
1535 }
1536
1537 static int
1538 daerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1539 {
1540 struct da_softc *softc;
1541 struct cam_periph *periph;
1542 int error;
1543
1544 periph = xpt_path_periph(ccb->ccb_h.path);
1545 softc = (struct da_softc *)periph->softc;
1546
1547 /*
1548 * Automatically detect devices that do not support
1549 * READ(6)/WRITE(6) and upgrade to using 10 byte cdbs.
1550 */
1551 error = 0;
1552 if ((ccb->ccb_h.status & CAM_STATUS_MASK) == CAM_REQ_INVALID) {
1553 error = cmd6workaround(ccb);
1554 } else if (((ccb->ccb_h.status & CAM_STATUS_MASK) ==
1555 CAM_SCSI_STATUS_ERROR)
1556 && (ccb->ccb_h.status & CAM_AUTOSNS_VALID)
1557 && (ccb->csio.scsi_status == SCSI_STATUS_CHECK_COND)
1558 && ((ccb->ccb_h.flags & CAM_SENSE_PHYS) == 0)
1559 && ((ccb->ccb_h.flags & CAM_SENSE_PTR) == 0)) {
1560 int sense_key, error_code, asc, ascq;
1561
1562 scsi_extract_sense(&ccb->csio.sense_data,
1563 &error_code, &sense_key, &asc, &ascq);
1564 if (sense_key == SSD_KEY_ILLEGAL_REQUEST)
1565 error = cmd6workaround(ccb);
1566 }
1567 if (error == ERESTART)
1568 return (ERESTART);
1569
1570 /*
1571 * XXX
1572 * Until we have a better way of doing pack validation,
1573 * don't treat UAs as errors.
1574 */
1575 sense_flags |= SF_RETRY_UA;
1576 return(cam_periph_error(ccb, cam_flags, sense_flags,
1577 &softc->saved_ccb));
1578 }
1579
1580 static void
1581 daprevent(struct cam_periph *periph, int action)
1582 {
1583 struct da_softc *softc;
1584 union ccb *ccb;
1585 int error;
1586
1587 softc = (struct da_softc *)periph->softc;
1588
1589 if (((action == PR_ALLOW)
1590 && (softc->flags & DA_FLAG_PACK_LOCKED) == 0)
1591 || ((action == PR_PREVENT)
1592 && (softc->flags & DA_FLAG_PACK_LOCKED) != 0)) {
1593 return;
1594 }
1595
1596 ccb = cam_periph_getccb(periph, /*priority*/1);
1597
1598 scsi_prevent(&ccb->csio,
1599 /*retries*/1,
1600 /*cbcfp*/dadone,
1601 MSG_SIMPLE_Q_TAG,
1602 action,
1603 SSD_FULL_SIZE,
1604 5000);
1605
1606 error = cam_periph_runccb(ccb, /*error_routine*/NULL, CAM_RETRY_SELTO,
1607 SF_RETRY_UA, softc->disk->d_devstat);
1608
1609 if (error == 0) {
1610 if (action == PR_ALLOW)
1611 softc->flags &= ~DA_FLAG_PACK_LOCKED;
1612 else
1613 softc->flags |= DA_FLAG_PACK_LOCKED;
1614 }
1615
1616 xpt_release_ccb(ccb);
1617 }
1618
1619 static int
1620 dagetcapacity(struct cam_periph *periph)
1621 {
1622 struct da_softc *softc;
1623 union ccb *ccb;
1624 struct scsi_read_capacity_data *rcap;
1625 struct scsi_read_capacity_data_long *rcaplong;
1626 uint32_t block_len;
1627 uint64_t maxsector;
1628 int error;
1629
1630 softc = (struct da_softc *)periph->softc;
1631 block_len = 0;
1632 maxsector = 0;
1633 error = 0;
1634
1635 /* Do a read capacity */
1636 rcap = (struct scsi_read_capacity_data *)malloc(sizeof(*rcaplong),
1637 M_TEMP,
1638 M_WAITOK);
1639
1640 ccb = cam_periph_getccb(periph, /*priority*/1);
1641 scsi_read_capacity(&ccb->csio,
1642 /*retries*/4,
1643 /*cbfncp*/dadone,
1644 MSG_SIMPLE_Q_TAG,
1645 rcap,
1646 SSD_FULL_SIZE,
1647 /*timeout*/60000);
1648 ccb->ccb_h.ccb_bp = NULL;
1649
1650 error = cam_periph_runccb(ccb, daerror,
1651 /*cam_flags*/CAM_RETRY_SELTO,
1652 /*sense_flags*/SF_RETRY_UA,
1653 softc->disk->d_devstat);
1654
1655 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1656 cam_release_devq(ccb->ccb_h.path,
1657 /*relsim_flags*/0,
1658 /*reduction*/0,
1659 /*timeout*/0,
1660 /*getcount_only*/0);
1661
1662 if (error == 0) {
1663 block_len = scsi_4btoul(rcap->length);
1664 maxsector = scsi_4btoul(rcap->addr);
1665
1666 if (maxsector != 0xffffffff)
1667 goto done;
1668 } else
1669 goto done;
1670
1671 rcaplong = (struct scsi_read_capacity_data_long *)rcap;
1672
1673 scsi_read_capacity_16(&ccb->csio,
1674 /*retries*/ 4,
1675 /*cbfcnp*/ dadone,
1676 /*tag_action*/ MSG_SIMPLE_Q_TAG,
1677 /*lba*/ 0,
1678 /*reladr*/ 0,
1679 /*pmi*/ 0,
1680 rcaplong,
1681 /*sense_len*/ SSD_FULL_SIZE,
1682 /*timeout*/ 60000);
1683 ccb->ccb_h.ccb_bp = NULL;
1684
1685 error = cam_periph_runccb(ccb, daerror,
1686 /*cam_flags*/CAM_RETRY_SELTO,
1687 /*sense_flags*/SF_RETRY_UA,
1688 softc->disk->d_devstat);
1689
1690 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1691 cam_release_devq(ccb->ccb_h.path,
1692 /*relsim_flags*/0,
1693 /*reduction*/0,
1694 /*timeout*/0,
1695 /*getcount_only*/0);
1696
1697 if (error == 0) {
1698 block_len = scsi_4btoul(rcaplong->length);
1699 maxsector = scsi_8btou64(rcaplong->addr);
1700 }
1701
1702 done:
1703
1704 if (error == 0)
1705 dasetgeom(periph, block_len, maxsector);
1706
1707 xpt_release_ccb(ccb);
1708
1709 free(rcap, M_TEMP);
1710
1711 return (error);
1712 }
1713
1714 static void
1715 dasetgeom(struct cam_periph *periph, uint32_t block_len, uint64_t maxsector)
1716 {
1717 struct ccb_calc_geometry ccg;
1718 struct da_softc *softc;
1719 struct disk_params *dp;
1720
1721 softc = (struct da_softc *)periph->softc;
1722
1723 dp = &softc->params;
1724 dp->secsize = block_len;
1725 dp->sectors = maxsector + 1;
1726 /*
1727 * Have the controller provide us with a geometry
1728 * for this disk. The only time the geometry
1729 * matters is when we boot and the controller
1730 * is the only one knowledgeable enough to come
1731 * up with something that will make this a bootable
1732 * device.
1733 */
1734 xpt_setup_ccb(&ccg.ccb_h, periph->path, /*priority*/1);
1735 ccg.ccb_h.func_code = XPT_CALC_GEOMETRY;
1736 ccg.block_size = dp->secsize;
1737 ccg.volume_size = dp->sectors;
1738 ccg.heads = 0;
1739 ccg.secs_per_track = 0;
1740 ccg.cylinders = 0;
1741 xpt_action((union ccb*)&ccg);
1742 dp->heads = ccg.heads;
1743 dp->secs_per_track = ccg.secs_per_track;
1744 dp->cylinders = ccg.cylinders;
1745 }
1746
1747 static void
1748 dasendorderedtag(void *arg)
1749 {
1750 struct da_softc *softc;
1751 int s;
1752
1753 for (softc = SLIST_FIRST(&softc_list);
1754 softc != NULL;
1755 softc = SLIST_NEXT(softc, links)) {
1756 s = splsoftcam();
1757 if ((softc->ordered_tag_count == 0)
1758 && ((softc->flags & DA_FLAG_WENT_IDLE) == 0)) {
1759 softc->flags |= DA_FLAG_NEED_OTAG;
1760 }
1761 if (softc->outstanding_cmds > 0)
1762 softc->flags &= ~DA_FLAG_WENT_IDLE;
1763
1764 softc->ordered_tag_count = 0;
1765 splx(s);
1766 }
1767 /* Queue us up again */
1768 timeout(dasendorderedtag, NULL,
1769 (da_default_timeout * hz) / DA_ORDEREDTAG_INTERVAL);
1770 }
1771
1772 /*
1773 * Step through all DA peripheral drivers, and if the device is still open,
1774 * sync the disk cache to physical media.
1775 */
1776 static void
1777 dashutdown(void * arg, int howto)
1778 {
1779 struct cam_periph *periph;
1780 struct da_softc *softc;
1781
1782 TAILQ_FOREACH(periph, &dadriver.units, unit_links) {
1783 union ccb ccb;
1784 softc = (struct da_softc *)periph->softc;
1785
1786 /*
1787 * We only sync the cache if the drive is still open, and
1788 * if the drive is capable of it..
1789 */
1790 if (((softc->flags & DA_FLAG_OPEN) == 0)
1791 || (softc->quirks & DA_Q_NO_SYNC_CACHE))
1792 continue;
1793
1794 xpt_setup_ccb(&ccb.ccb_h, periph->path, /*priority*/1);
1795
1796 ccb.ccb_h.ccb_state = DA_CCB_DUMP;
1797 scsi_synchronize_cache(&ccb.csio,
1798 /*retries*/1,
1799 /*cbfcnp*/dadone,
1800 MSG_SIMPLE_Q_TAG,
1801 /*begin_lba*/0, /* whole disk */
1802 /*lb_count*/0,
1803 SSD_FULL_SIZE,
1804 60 * 60 * 1000);
1805
1806 xpt_polled_action(&ccb);
1807
1808 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1809 if (((ccb.ccb_h.status & CAM_STATUS_MASK) ==
1810 CAM_SCSI_STATUS_ERROR)
1811 && (ccb.csio.scsi_status == SCSI_STATUS_CHECK_COND)){
1812 int error_code, sense_key, asc, ascq;
1813
1814 scsi_extract_sense(&ccb.csio.sense_data,
1815 &error_code, &sense_key,
1816 &asc, &ascq);
1817
1818 if (sense_key != SSD_KEY_ILLEGAL_REQUEST)
1819 scsi_sense_print(&ccb.csio);
1820 } else {
1821 xpt_print_path(periph->path);
1822 printf("Synchronize cache failed, status "
1823 "== 0x%x, scsi status == 0x%x\n",
1824 ccb.ccb_h.status, ccb.csio.scsi_status);
1825 }
1826 }
1827
1828 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1829 cam_release_devq(ccb.ccb_h.path,
1830 /*relsim_flags*/0,
1831 /*reduction*/0,
1832 /*timeout*/0,
1833 /*getcount_only*/0);
1834
1835 }
1836 }
1837
1838 #else /* !_KERNEL */
1839
1840 /*
1841 * XXX This is only left out of the kernel build to silence warnings. If,
1842 * for some reason this function is used in the kernel, the ifdefs should
1843 * be moved so it is included both in the kernel and userland.
1844 */
1845 void
1846 scsi_format_unit(struct ccb_scsiio *csio, u_int32_t retries,
1847 void (*cbfcnp)(struct cam_periph *, union ccb *),
1848 u_int8_t tag_action, u_int8_t byte2, u_int16_t ileave,
1849 u_int8_t *data_ptr, u_int32_t dxfer_len, u_int8_t sense_len,
1850 u_int32_t timeout)
1851 {
1852 struct scsi_format_unit *scsi_cmd;
1853
1854 scsi_cmd = (struct scsi_format_unit *)&csio->cdb_io.cdb_bytes;
1855 scsi_cmd->opcode = FORMAT_UNIT;
1856 scsi_cmd->byte2 = byte2;
1857 scsi_ulto2b(ileave, scsi_cmd->interleave);
1858
1859 cam_fill_csio(csio,
1860 retries,
1861 cbfcnp,
1862 /*flags*/ (dxfer_len > 0) ? CAM_DIR_OUT : CAM_DIR_NONE,
1863 tag_action,
1864 data_ptr,
1865 dxfer_len,
1866 sense_len,
1867 sizeof(*scsi_cmd),
1868 timeout);
1869 }
1870
1871 #endif /* _KERNEL */
Cache object: fd255b7dd64bd2674bd842e27eefc2de
|