]> CyberLeo.Net >> Repos - FreeBSD/releng/8.2.git/blob - sys/cam/ata/ata_da.c
Copy stable/8 to releng/8.2 in preparation for FreeBSD-8.2 release.
[FreeBSD/releng/8.2.git] / sys / cam / ata / ata_da.c
1 /*-
2  * Copyright (c) 2009 Alexander Motin <mav@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer,
10  *    without modification, immediately at the beginning of the file.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29
30 #include <sys/param.h>
31
32 #ifdef _KERNEL
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/bio.h>
36 #include <sys/sysctl.h>
37 #include <sys/taskqueue.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/conf.h>
41 #include <sys/devicestat.h>
42 #include <sys/eventhandler.h>
43 #include <sys/malloc.h>
44 #include <sys/cons.h>
45 #include <sys/reboot.h>
46 #include <geom/geom_disk.h>
47 #endif /* _KERNEL */
48
49 #ifndef _KERNEL
50 #include <stdio.h>
51 #include <string.h>
52 #endif /* _KERNEL */
53
54 #include <cam/cam.h>
55 #include <cam/cam_ccb.h>
56 #include <cam/cam_periph.h>
57 #include <cam/cam_xpt_periph.h>
58 #include <cam/cam_sim.h>
59
60 #include <cam/ata/ata_all.h>
61
62 #include <machine/md_var.h>     /* geometry translation */
63
64 #ifdef _KERNEL
65
66 #define ATA_MAX_28BIT_LBA               268435455UL
67
68 typedef enum {
69         ADA_STATE_NORMAL
70 } ada_state;
71
72 typedef enum {
73         ADA_FLAG_PACK_INVALID   = 0x001,
74         ADA_FLAG_CAN_48BIT      = 0x002,
75         ADA_FLAG_CAN_FLUSHCACHE = 0x004,
76         ADA_FLAG_CAN_NCQ        = 0x008,
77         ADA_FLAG_CAN_DMA        = 0x010,
78         ADA_FLAG_NEED_OTAG      = 0x020,
79         ADA_FLAG_WENT_IDLE      = 0x040,
80         ADA_FLAG_CAN_TRIM       = 0x080,
81         ADA_FLAG_OPEN           = 0x100,
82         ADA_FLAG_SCTX_INIT      = 0x200,
83         ADA_FLAG_CAN_CFA        = 0x400,
84         ADA_FLAG_CAN_POWERMGT   = 0x800
85 } ada_flags;
86
87 typedef enum {
88         ADA_Q_NONE              = 0x00
89 } ada_quirks;
90
91 typedef enum {
92         ADA_CCB_BUFFER_IO       = 0x03,
93         ADA_CCB_WAITING         = 0x04,
94         ADA_CCB_DUMP            = 0x05,
95         ADA_CCB_TRIM            = 0x06,
96         ADA_CCB_TYPE_MASK       = 0x0F,
97 } ada_ccb_state;
98
99 /* Offsets into our private area for storing information */
100 #define ccb_state       ppriv_field0
101 #define ccb_bp          ppriv_ptr1
102
103 struct disk_params {
104         u_int8_t  heads;
105         u_int8_t  secs_per_track;
106         u_int32_t cylinders;
107         u_int32_t secsize;      /* Number of bytes/logical sector */
108         u_int64_t sectors;      /* Total number sectors */
109 };
110
111 #define TRIM_MAX_BLOCKS 4
112 #define TRIM_MAX_RANGES TRIM_MAX_BLOCKS * 64
113 struct trim_request {
114         uint8_t         data[TRIM_MAX_RANGES * 8];
115         struct bio      *bps[TRIM_MAX_RANGES];
116 };
117
118 struct ada_softc {
119         struct   bio_queue_head bio_queue;
120         struct   bio_queue_head trim_queue;
121         ada_state state;
122         ada_flags flags;        
123         ada_quirks quirks;
124         int      ordered_tag_count;
125         int      outstanding_cmds;
126         int      trim_max_ranges;
127         int      trim_running;
128         struct   disk_params params;
129         struct   disk *disk;
130         struct task             sysctl_task;
131         struct sysctl_ctx_list  sysctl_ctx;
132         struct sysctl_oid       *sysctl_tree;
133         struct callout          sendordered_c;
134         struct trim_request     trim_req;
135 };
136
137 struct ada_quirk_entry {
138         struct scsi_inquiry_pattern inq_pat;
139         ada_quirks quirks;
140 };
141
142 static struct ada_quirk_entry ada_quirk_table[] =
143 {
144         {
145                 /* Default */
146                 {
147                   T_ANY, SIP_MEDIA_REMOVABLE|SIP_MEDIA_FIXED,
148                   /*vendor*/"*", /*product*/"*", /*revision*/"*"
149                 },
150                 /*quirks*/0
151         },
152 };
153
154 static  disk_strategy_t adastrategy;
155 static  dumper_t        adadump;
156 static  periph_init_t   adainit;
157 static  void            adaasync(void *callback_arg, u_int32_t code,
158                                 struct cam_path *path, void *arg);
159 static  void            adasysctlinit(void *context, int pending);
160 static  periph_ctor_t   adaregister;
161 static  periph_dtor_t   adacleanup;
162 static  periph_start_t  adastart;
163 static  periph_oninv_t  adaoninvalidate;
164 static  void            adadone(struct cam_periph *periph,
165                                union ccb *done_ccb);
166 static  int             adaerror(union ccb *ccb, u_int32_t cam_flags,
167                                 u_int32_t sense_flags);
168 static void             adagetparams(struct cam_periph *periph,
169                                 struct ccb_getdev *cgd);
170 static timeout_t        adasendorderedtag;
171 static void             adashutdown(void *arg, int howto);
172
173 #ifndef ADA_DEFAULT_TIMEOUT
174 #define ADA_DEFAULT_TIMEOUT 30  /* Timeout in seconds */
175 #endif
176
177 #ifndef ADA_DEFAULT_RETRY
178 #define ADA_DEFAULT_RETRY       4
179 #endif
180
181 #ifndef ADA_DEFAULT_SEND_ORDERED
182 #define ADA_DEFAULT_SEND_ORDERED        1
183 #endif
184
185 #ifndef ADA_DEFAULT_SPINDOWN_SHUTDOWN
186 #define ADA_DEFAULT_SPINDOWN_SHUTDOWN   1
187 #endif
188
189 /*
190  * Most platforms map firmware geometry to actual, but some don't.  If
191  * not overridden, default to nothing.
192  */
193 #ifndef ata_disk_firmware_geom_adjust
194 #define ata_disk_firmware_geom_adjust(disk)
195 #endif
196
197 static int ada_retry_count = ADA_DEFAULT_RETRY;
198 static int ada_default_timeout = ADA_DEFAULT_TIMEOUT;
199 static int ada_send_ordered = ADA_DEFAULT_SEND_ORDERED;
200 static int ada_spindown_shutdown = ADA_DEFAULT_SPINDOWN_SHUTDOWN;
201
202 SYSCTL_NODE(_kern_cam, OID_AUTO, ada, CTLFLAG_RD, 0,
203             "CAM Direct Access Disk driver");
204 SYSCTL_INT(_kern_cam_ada, OID_AUTO, retry_count, CTLFLAG_RW,
205            &ada_retry_count, 0, "Normal I/O retry count");
206 TUNABLE_INT("kern.cam.ada.retry_count", &ada_retry_count);
207 SYSCTL_INT(_kern_cam_ada, OID_AUTO, default_timeout, CTLFLAG_RW,
208            &ada_default_timeout, 0, "Normal I/O timeout (in seconds)");
209 TUNABLE_INT("kern.cam.ada.default_timeout", &ada_default_timeout);
210 SYSCTL_INT(_kern_cam_ada, OID_AUTO, ada_send_ordered, CTLFLAG_RW,
211            &ada_send_ordered, 0, "Send Ordered Tags");
212 TUNABLE_INT("kern.cam.ada.ada_send_ordered", &ada_send_ordered);
213 SYSCTL_INT(_kern_cam_ada, OID_AUTO, spindown_shutdown, CTLFLAG_RW,
214            &ada_spindown_shutdown, 0, "Spin down upon shutdown");
215 TUNABLE_INT("kern.cam.ada.spindown_shutdown", &ada_spindown_shutdown);
216
217 /*
218  * ADA_ORDEREDTAG_INTERVAL determines how often, relative
219  * to the default timeout, we check to see whether an ordered
220  * tagged transaction is appropriate to prevent simple tag
221  * starvation.  Since we'd like to ensure that there is at least
222  * 1/2 of the timeout length left for a starved transaction to
223  * complete after we've sent an ordered tag, we must poll at least
224  * four times in every timeout period.  This takes care of the worst
225  * case where a starved transaction starts during an interval that
226  * meets the requirement "don't send an ordered tag" test so it takes
227  * us two intervals to determine that a tag must be sent.
228  */
229 #ifndef ADA_ORDEREDTAG_INTERVAL
230 #define ADA_ORDEREDTAG_INTERVAL 4
231 #endif
232
233 static struct periph_driver adadriver =
234 {
235         adainit, "ada",
236         TAILQ_HEAD_INITIALIZER(adadriver.units), /* generation */ 0
237 };
238
239 PERIPHDRIVER_DECLARE(ada, adadriver);
240
241 MALLOC_DEFINE(M_ATADA, "ata_da", "ata_da buffers");
242
243 static int
244 adaopen(struct disk *dp)
245 {
246         struct cam_periph *periph;
247         struct ada_softc *softc;
248         int unit;
249         int error;
250
251         periph = (struct cam_periph *)dp->d_drv1;
252         if (periph == NULL) {
253                 return (ENXIO); 
254         }
255
256         if (cam_periph_acquire(periph) != CAM_REQ_CMP) {
257                 return(ENXIO);
258         }
259
260         cam_periph_lock(periph);
261         if ((error = cam_periph_hold(periph, PRIBIO|PCATCH)) != 0) {
262                 cam_periph_unlock(periph);
263                 cam_periph_release(periph);
264                 return (error);
265         }
266
267         unit = periph->unit_number;
268         softc = (struct ada_softc *)periph->softc;
269         softc->flags |= ADA_FLAG_OPEN;
270
271         CAM_DEBUG(periph->path, CAM_DEBUG_TRACE,
272             ("adaopen: disk=%s%d (unit %d)\n", dp->d_name, dp->d_unit,
273              unit));
274
275         if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) {
276                 /* Invalidate our pack information. */
277                 softc->flags &= ~ADA_FLAG_PACK_INVALID;
278         }
279
280         cam_periph_unhold(periph);
281         cam_periph_unlock(periph);
282         return (0);
283 }
284
285 static int
286 adaclose(struct disk *dp)
287 {
288         struct  cam_periph *periph;
289         struct  ada_softc *softc;
290         union ccb *ccb;
291         int error;
292
293         periph = (struct cam_periph *)dp->d_drv1;
294         if (periph == NULL)
295                 return (ENXIO); 
296
297         cam_periph_lock(periph);
298         if ((error = cam_periph_hold(periph, PRIBIO)) != 0) {
299                 cam_periph_unlock(periph);
300                 cam_periph_release(periph);
301                 return (error);
302         }
303
304         softc = (struct ada_softc *)periph->softc;
305         /* We only sync the cache if the drive is capable of it. */
306         if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
307
308                 ccb = cam_periph_getccb(periph, CAM_PRIORITY_NORMAL);
309                 cam_fill_ataio(&ccb->ataio,
310                                     1,
311                                     adadone,
312                                     CAM_DIR_NONE,
313                                     0,
314                                     NULL,
315                                     0,
316                                     ada_default_timeout*1000);
317
318                 if (softc->flags & ADA_FLAG_CAN_48BIT)
319                         ata_48bit_cmd(&ccb->ataio, ATA_FLUSHCACHE48, 0, 0, 0);
320                 else
321                         ata_28bit_cmd(&ccb->ataio, ATA_FLUSHCACHE, 0, 0, 0);
322                 cam_periph_runccb(ccb, /*error_routine*/NULL, /*cam_flags*/0,
323                     /*sense_flags*/0, softc->disk->d_devstat);
324
325                 if ((ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
326                         xpt_print(periph->path, "Synchronize cache failed\n");
327
328                 if ((ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
329                         cam_release_devq(ccb->ccb_h.path,
330                                          /*relsim_flags*/0,
331                                          /*reduction*/0,
332                                          /*timeout*/0,
333                                          /*getcount_only*/0);
334                 xpt_release_ccb(ccb);
335         }
336
337         softc->flags &= ~ADA_FLAG_OPEN;
338         cam_periph_unhold(periph);
339         cam_periph_unlock(periph);
340         cam_periph_release(periph);
341         return (0);     
342 }
343
344 static void
345 adaschedule(struct cam_periph *periph)
346 {
347         struct ada_softc *softc = (struct ada_softc *)periph->softc;
348
349         if (bioq_first(&softc->bio_queue) ||
350             (!softc->trim_running && bioq_first(&softc->trim_queue))) {
351                 /* Have more work to do, so ensure we stay scheduled */
352                 xpt_schedule(periph, CAM_PRIORITY_NORMAL);
353         }
354 }
355
356 /*
357  * Actually translate the requested transfer into one the physical driver
358  * can understand.  The transfer is described by a buf and will include
359  * only one physical transfer.
360  */
361 static void
362 adastrategy(struct bio *bp)
363 {
364         struct cam_periph *periph;
365         struct ada_softc *softc;
366         
367         periph = (struct cam_periph *)bp->bio_disk->d_drv1;
368         if (periph == NULL) {
369                 biofinish(bp, NULL, ENXIO);
370                 return;
371         }
372         softc = (struct ada_softc *)periph->softc;
373
374         cam_periph_lock(periph);
375
376         /*
377          * If the device has been made invalid, error out
378          */
379         if ((softc->flags & ADA_FLAG_PACK_INVALID)) {
380                 cam_periph_unlock(periph);
381                 biofinish(bp, NULL, ENXIO);
382                 return;
383         }
384         
385         /*
386          * Place it in the queue of disk activities for this disk
387          */
388         if (bp->bio_cmd == BIO_DELETE &&
389             (softc->flags & ADA_FLAG_CAN_TRIM))
390                 bioq_disksort(&softc->trim_queue, bp);
391         else
392                 bioq_disksort(&softc->bio_queue, bp);
393
394         /*
395          * Schedule ourselves for performing the work.
396          */
397         adaschedule(periph);
398         cam_periph_unlock(periph);
399
400         return;
401 }
402
403 static int
404 adadump(void *arg, void *virtual, vm_offset_t physical, off_t offset, size_t length)
405 {
406         struct      cam_periph *periph;
407         struct      ada_softc *softc;
408         u_int       secsize;
409         union       ccb ccb;
410         struct      disk *dp;
411         uint64_t    lba;
412         uint16_t    count;
413
414         dp = arg;
415         periph = dp->d_drv1;
416         if (periph == NULL)
417                 return (ENXIO);
418         softc = (struct ada_softc *)periph->softc;
419         cam_periph_lock(periph);
420         secsize = softc->params.secsize;
421         lba = offset / secsize;
422         count = length / secsize;
423         
424         if ((softc->flags & ADA_FLAG_PACK_INVALID) != 0) {
425                 cam_periph_unlock(periph);
426                 return (ENXIO);
427         }
428
429         if (length > 0) {
430                 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
431                 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
432                 cam_fill_ataio(&ccb.ataio,
433                     0,
434                     adadone,
435                     CAM_DIR_OUT,
436                     0,
437                     (u_int8_t *) virtual,
438                     length,
439                     ada_default_timeout*1000);
440                 if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
441                     (lba + count >= ATA_MAX_28BIT_LBA ||
442                     count >= 256)) {
443                         ata_48bit_cmd(&ccb.ataio, ATA_WRITE_DMA48,
444                             0, lba, count);
445                 } else {
446                         ata_28bit_cmd(&ccb.ataio, ATA_WRITE_DMA,
447                             0, lba, count);
448                 }
449                 xpt_polled_action(&ccb);
450
451                 if ((ccb.ataio.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
452                         printf("Aborting dump due to I/O error.\n");
453                         cam_periph_unlock(periph);
454                         return(EIO);
455                 }
456                 cam_periph_unlock(periph);
457                 return(0);
458         }
459
460         if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) {
461                 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
462
463                 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
464                 cam_fill_ataio(&ccb.ataio,
465                                     1,
466                                     adadone,
467                                     CAM_DIR_NONE,
468                                     0,
469                                     NULL,
470                                     0,
471                                     ada_default_timeout*1000);
472
473                 if (softc->flags & ADA_FLAG_CAN_48BIT)
474                         ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
475                 else
476                         ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
477                 xpt_polled_action(&ccb);
478
479                 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
480                         xpt_print(periph->path, "Synchronize cache failed\n");
481
482                 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
483                         cam_release_devq(ccb.ccb_h.path,
484                                          /*relsim_flags*/0,
485                                          /*reduction*/0,
486                                          /*timeout*/0,
487                                          /*getcount_only*/0);
488         }
489         cam_periph_unlock(periph);
490         return (0);
491 }
492
493 static void
494 adainit(void)
495 {
496         cam_status status;
497
498         /*
499          * Install a global async callback.  This callback will
500          * receive async callbacks like "new device found".
501          */
502         status = xpt_register_async(AC_FOUND_DEVICE, adaasync, NULL, NULL);
503
504         if (status != CAM_REQ_CMP) {
505                 printf("ada: Failed to attach master async callback "
506                        "due to status 0x%x!\n", status);
507         } else if (ada_send_ordered) {
508
509                 /* Register our shutdown event handler */
510                 if ((EVENTHANDLER_REGISTER(shutdown_post_sync, adashutdown, 
511                                            NULL, SHUTDOWN_PRI_DEFAULT)) == NULL)
512                     printf("adainit: shutdown event registration failed!\n");
513         }
514 }
515
516 static void
517 adaoninvalidate(struct cam_periph *periph)
518 {
519         struct ada_softc *softc;
520
521         softc = (struct ada_softc *)periph->softc;
522
523         /*
524          * De-register any async callbacks.
525          */
526         xpt_register_async(0, adaasync, periph, periph->path);
527
528         softc->flags |= ADA_FLAG_PACK_INVALID;
529
530         /*
531          * Return all queued I/O with ENXIO.
532          * XXX Handle any transactions queued to the card
533          *     with XPT_ABORT_CCB.
534          */
535         bioq_flush(&softc->bio_queue, NULL, ENXIO);
536         bioq_flush(&softc->trim_queue, NULL, ENXIO);
537
538         disk_gone(softc->disk);
539         xpt_print(periph->path, "lost device\n");
540 }
541
542 static void
543 adacleanup(struct cam_periph *periph)
544 {
545         struct ada_softc *softc;
546
547         softc = (struct ada_softc *)periph->softc;
548
549         xpt_print(periph->path, "removing device entry\n");
550         cam_periph_unlock(periph);
551
552         /*
553          * If we can't free the sysctl tree, oh well...
554          */
555         if ((softc->flags & ADA_FLAG_SCTX_INIT) != 0
556             && sysctl_ctx_free(&softc->sysctl_ctx) != 0) {
557                 xpt_print(periph->path, "can't remove sysctl context\n");
558         }
559
560         disk_destroy(softc->disk);
561         callout_drain(&softc->sendordered_c);
562         free(softc, M_DEVBUF);
563         cam_periph_lock(periph);
564 }
565
566 static void
567 adaasync(void *callback_arg, u_int32_t code,
568         struct cam_path *path, void *arg)
569 {
570         struct cam_periph *periph;
571
572         periph = (struct cam_periph *)callback_arg;
573         switch (code) {
574         case AC_FOUND_DEVICE:
575         {
576                 struct ccb_getdev *cgd;
577                 cam_status status;
578  
579                 cgd = (struct ccb_getdev *)arg;
580                 if (cgd == NULL)
581                         break;
582
583                 if (cgd->protocol != PROTO_ATA)
584                         break;
585
586                 /*
587                  * Allocate a peripheral instance for
588                  * this device and start the probe
589                  * process.
590                  */
591                 status = cam_periph_alloc(adaregister, adaoninvalidate,
592                                           adacleanup, adastart,
593                                           "ada", CAM_PERIPH_BIO,
594                                           cgd->ccb_h.path, adaasync,
595                                           AC_FOUND_DEVICE, cgd);
596
597                 if (status != CAM_REQ_CMP
598                  && status != CAM_REQ_INPROG)
599                         printf("adaasync: Unable to attach to new device "
600                                 "due to status 0x%x\n", status);
601                 break;
602         }
603         default:
604                 cam_periph_async(periph, code, path, arg);
605                 break;
606         }
607 }
608
609 static void
610 adasysctlinit(void *context, int pending)
611 {
612         struct cam_periph *periph;
613         struct ada_softc *softc;
614         char tmpstr[80], tmpstr2[80];
615
616         periph = (struct cam_periph *)context;
617         if (cam_periph_acquire(periph) != CAM_REQ_CMP)
618                 return;
619
620         softc = (struct ada_softc *)periph->softc;
621         snprintf(tmpstr, sizeof(tmpstr), "CAM ADA unit %d", periph->unit_number);
622         snprintf(tmpstr2, sizeof(tmpstr2), "%d", periph->unit_number);
623
624         sysctl_ctx_init(&softc->sysctl_ctx);
625         softc->flags |= ADA_FLAG_SCTX_INIT;
626         softc->sysctl_tree = SYSCTL_ADD_NODE(&softc->sysctl_ctx,
627                 SYSCTL_STATIC_CHILDREN(_kern_cam_ada), OID_AUTO, tmpstr2,
628                 CTLFLAG_RD, 0, tmpstr);
629         if (softc->sysctl_tree == NULL) {
630                 printf("adasysctlinit: unable to allocate sysctl tree\n");
631                 cam_periph_release(periph);
632                 return;
633         }
634
635         cam_periph_release(periph);
636 }
637
638 static cam_status
639 adaregister(struct cam_periph *periph, void *arg)
640 {
641         struct ada_softc *softc;
642         struct ccb_pathinq cpi;
643         struct ccb_getdev *cgd;
644         char   announce_buf[80];
645         struct disk_params *dp;
646         caddr_t match;
647         u_int maxio;
648
649         cgd = (struct ccb_getdev *)arg;
650         if (periph == NULL) {
651                 printf("adaregister: periph was NULL!!\n");
652                 return(CAM_REQ_CMP_ERR);
653         }
654
655         if (cgd == NULL) {
656                 printf("adaregister: no getdev CCB, can't register device\n");
657                 return(CAM_REQ_CMP_ERR);
658         }
659
660         softc = (struct ada_softc *)malloc(sizeof(*softc), M_DEVBUF,
661             M_NOWAIT|M_ZERO);
662
663         if (softc == NULL) {
664                 printf("adaregister: Unable to probe new device. "
665                     "Unable to allocate softc\n");
666                 return(CAM_REQ_CMP_ERR);
667         }
668
669         bioq_init(&softc->bio_queue);
670         bioq_init(&softc->trim_queue);
671
672         if (cgd->ident_data.capabilities1 & ATA_SUPPORT_DMA)
673                 softc->flags |= ADA_FLAG_CAN_DMA;
674         if (cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48)
675                 softc->flags |= ADA_FLAG_CAN_48BIT;
676         if (cgd->ident_data.support.command2 & ATA_SUPPORT_FLUSHCACHE)
677                 softc->flags |= ADA_FLAG_CAN_FLUSHCACHE;
678         if (cgd->ident_data.support.command1 & ATA_SUPPORT_POWERMGT)
679                 softc->flags |= ADA_FLAG_CAN_POWERMGT;
680         if (cgd->ident_data.satacapabilities & ATA_SUPPORT_NCQ &&
681             cgd->inq_flags & SID_CmdQue)
682                 softc->flags |= ADA_FLAG_CAN_NCQ;
683         if (cgd->ident_data.support_dsm & ATA_SUPPORT_DSM_TRIM) {
684                 softc->flags |= ADA_FLAG_CAN_TRIM;
685                 softc->trim_max_ranges = TRIM_MAX_RANGES;
686                 if (cgd->ident_data.max_dsm_blocks != 0) {
687                         softc->trim_max_ranges =
688                             min(cgd->ident_data.max_dsm_blocks * 64,
689                                 softc->trim_max_ranges);
690                 }
691         }
692         if (cgd->ident_data.support.command2 & ATA_SUPPORT_CFA)
693                 softc->flags |= ADA_FLAG_CAN_CFA;
694         softc->state = ADA_STATE_NORMAL;
695
696         periph->softc = softc;
697
698         /*
699          * See if this device has any quirks.
700          */
701         match = cam_quirkmatch((caddr_t)&cgd->ident_data,
702                                (caddr_t)ada_quirk_table,
703                                sizeof(ada_quirk_table)/sizeof(*ada_quirk_table),
704                                sizeof(*ada_quirk_table), ata_identify_match);
705         if (match != NULL)
706                 softc->quirks = ((struct ada_quirk_entry *)match)->quirks;
707         else
708                 softc->quirks = ADA_Q_NONE;
709
710         bzero(&cpi, sizeof(cpi));
711         xpt_setup_ccb(&cpi.ccb_h, periph->path, CAM_PRIORITY_NONE);
712         cpi.ccb_h.func_code = XPT_PATH_INQ;
713         xpt_action((union ccb *)&cpi);
714
715         TASK_INIT(&softc->sysctl_task, 0, adasysctlinit, periph);
716
717         /*
718          * Register this media as a disk
719          */
720         mtx_unlock(periph->sim->mtx);
721         adagetparams(periph, cgd);
722         softc->disk = disk_alloc();
723         softc->disk->d_open = adaopen;
724         softc->disk->d_close = adaclose;
725         softc->disk->d_strategy = adastrategy;
726         softc->disk->d_dump = adadump;
727         softc->disk->d_name = "ada";
728         softc->disk->d_drv1 = periph;
729         maxio = cpi.maxio;              /* Honor max I/O size of SIM */
730         if (maxio == 0)
731                 maxio = DFLTPHYS;       /* traditional default */
732         else if (maxio > MAXPHYS)
733                 maxio = MAXPHYS;        /* for safety */
734         if (softc->flags & ADA_FLAG_CAN_48BIT)
735                 maxio = min(maxio, 65536 * softc->params.secsize);
736         else                                    /* 28bit ATA command limit */
737                 maxio = min(maxio, 256 * softc->params.secsize);
738         softc->disk->d_maxsize = maxio;
739         softc->disk->d_unit = periph->unit_number;
740         softc->disk->d_flags = 0;
741         if (softc->flags & ADA_FLAG_CAN_FLUSHCACHE)
742                 softc->disk->d_flags |= DISKFLAG_CANFLUSHCACHE;
743         if ((softc->flags & ADA_FLAG_CAN_TRIM) ||
744             ((softc->flags & ADA_FLAG_CAN_CFA) &&
745             !(softc->flags & ADA_FLAG_CAN_48BIT)))
746                 softc->disk->d_flags |= DISKFLAG_CANDELETE;
747         strlcpy(softc->disk->d_ident, cgd->serial_num,
748             MIN(sizeof(softc->disk->d_ident), cgd->serial_num_len + 1));
749
750         softc->disk->d_sectorsize = softc->params.secsize;
751         softc->disk->d_mediasize = (off_t)softc->params.sectors *
752             softc->params.secsize;
753         if (ata_physical_sector_size(&cgd->ident_data) !=
754             softc->params.secsize) {
755                 softc->disk->d_stripesize =
756                     ata_physical_sector_size(&cgd->ident_data);
757                 softc->disk->d_stripeoffset = (softc->disk->d_stripesize -
758                     ata_logical_sector_offset(&cgd->ident_data)) %
759                     softc->disk->d_stripesize;
760         }
761         softc->disk->d_fwsectors = softc->params.secs_per_track;
762         softc->disk->d_fwheads = softc->params.heads;
763         ata_disk_firmware_geom_adjust(softc->disk);
764
765         disk_create(softc->disk, DISK_VERSION);
766         mtx_lock(periph->sim->mtx);
767
768         dp = &softc->params;
769         snprintf(announce_buf, sizeof(announce_buf),
770                 "%juMB (%ju %u byte sectors: %dH %dS/T %dC)",
771                 (uintmax_t)(((uintmax_t)dp->secsize *
772                 dp->sectors) / (1024*1024)),
773                 (uintmax_t)dp->sectors,
774                 dp->secsize, dp->heads,
775                 dp->secs_per_track, dp->cylinders);
776         xpt_announce_periph(periph, announce_buf);
777         /*
778          * Add async callbacks for bus reset and
779          * bus device reset calls.  I don't bother
780          * checking if this fails as, in most cases,
781          * the system will function just fine without
782          * them and the only alternative would be to
783          * not attach the device on failure.
784          */
785         xpt_register_async(AC_LOST_DEVICE,
786                            adaasync, periph, periph->path);
787
788         /*
789          * Schedule a periodic event to occasionally send an
790          * ordered tag to a device.
791          */
792         callout_init_mtx(&softc->sendordered_c, periph->sim->mtx, 0);
793         callout_reset(&softc->sendordered_c,
794             (ADA_DEFAULT_TIMEOUT * hz) / ADA_ORDEREDTAG_INTERVAL,
795             adasendorderedtag, softc);
796
797         return(CAM_REQ_CMP);
798 }
799
800 static void
801 adastart(struct cam_periph *periph, union ccb *start_ccb)
802 {
803         struct ada_softc *softc = (struct ada_softc *)periph->softc;
804         struct ccb_ataio *ataio = &start_ccb->ataio;
805
806         switch (softc->state) {
807         case ADA_STATE_NORMAL:
808         {
809                 struct bio *bp;
810                 u_int8_t tag_code;
811
812                 /* Execute immediate CCB if waiting. */
813                 if (periph->immediate_priority <= periph->pinfo.priority) {
814                         CAM_DEBUG_PRINT(CAM_DEBUG_SUBTRACE,
815                                         ("queuing for immediate ccb\n"));
816                         start_ccb->ccb_h.ccb_state = ADA_CCB_WAITING;
817                         SLIST_INSERT_HEAD(&periph->ccb_list, &start_ccb->ccb_h,
818                                           periph_links.sle);
819                         periph->immediate_priority = CAM_PRIORITY_NONE;
820                         wakeup(&periph->ccb_list);
821                         /* Have more work to do, so ensure we stay scheduled */
822                         adaschedule(periph);
823                         break;
824                 }
825                 /* Run TRIM if not running yet. */
826                 if (!softc->trim_running &&
827                     (bp = bioq_first(&softc->trim_queue)) != 0) {
828                         struct trim_request *req = &softc->trim_req;
829                         struct bio *bp1;
830                         int bps = 0, ranges = 0;
831
832                         softc->trim_running = 1;
833                         bzero(req, sizeof(*req));
834                         bp1 = bp;
835                         do {
836                                 uint64_t lba = bp1->bio_pblkno;
837                                 int count = bp1->bio_bcount /
838                                     softc->params.secsize;
839
840                                 bioq_remove(&softc->trim_queue, bp1);
841                                 while (count > 0) {
842                                         int c = min(count, 0xffff);
843                                         int off = ranges * 8;
844
845                                         req->data[off + 0] = lba & 0xff;
846                                         req->data[off + 1] = (lba >> 8) & 0xff;
847                                         req->data[off + 2] = (lba >> 16) & 0xff;
848                                         req->data[off + 3] = (lba >> 24) & 0xff;
849                                         req->data[off + 4] = (lba >> 32) & 0xff;
850                                         req->data[off + 5] = (lba >> 40) & 0xff;
851                                         req->data[off + 6] = c & 0xff;
852                                         req->data[off + 7] = (c >> 8) & 0xff;
853                                         lba += c;
854                                         count -= c;
855                                         ranges++;
856                                 }
857                                 req->bps[bps++] = bp1;
858                                 bp1 = bioq_first(&softc->trim_queue);
859                                 if (bp1 == NULL ||
860                                     bp1->bio_bcount / softc->params.secsize >
861                                     (softc->trim_max_ranges - ranges) * 0xffff)
862                                         break;
863                         } while (1);
864                         cam_fill_ataio(ataio,
865                             ada_retry_count,
866                             adadone,
867                             CAM_DIR_OUT,
868                             0,
869                             req->data,
870                             ((ranges + 63) / 64) * 512,
871                             ada_default_timeout * 1000);
872                         ata_48bit_cmd(ataio, ATA_DATA_SET_MANAGEMENT,
873                             ATA_DSM_TRIM, 0, (ranges + 63) / 64);
874                         start_ccb->ccb_h.ccb_state = ADA_CCB_TRIM;
875                         goto out;
876                 }
877                 /* Run regular command. */
878                 bp = bioq_first(&softc->bio_queue);
879                 if (bp == NULL) {
880                         xpt_release_ccb(start_ccb);
881                         break;
882                 }
883                 bioq_remove(&softc->bio_queue, bp);
884
885                 if ((bp->bio_flags & BIO_ORDERED) != 0
886                  || (softc->flags & ADA_FLAG_NEED_OTAG) != 0) {
887                         softc->flags &= ~ADA_FLAG_NEED_OTAG;
888                         softc->ordered_tag_count++;
889                         tag_code = 0;
890                 } else {
891                         tag_code = 1;
892                 }
893                 switch (bp->bio_cmd) {
894                 case BIO_READ:
895                 case BIO_WRITE:
896                 {
897                         uint64_t lba = bp->bio_pblkno;
898                         uint16_t count = bp->bio_bcount / softc->params.secsize;
899
900                         cam_fill_ataio(ataio,
901                             ada_retry_count,
902                             adadone,
903                             bp->bio_cmd == BIO_READ ?
904                                 CAM_DIR_IN : CAM_DIR_OUT,
905                             tag_code,
906                             bp->bio_data,
907                             bp->bio_bcount,
908                             ada_default_timeout*1000);
909
910                         if ((softc->flags & ADA_FLAG_CAN_NCQ) && tag_code) {
911                                 if (bp->bio_cmd == BIO_READ) {
912                                         ata_ncq_cmd(ataio, ATA_READ_FPDMA_QUEUED,
913                                             lba, count);
914                                 } else {
915                                         ata_ncq_cmd(ataio, ATA_WRITE_FPDMA_QUEUED,
916                                             lba, count);
917                                 }
918                         } else if ((softc->flags & ADA_FLAG_CAN_48BIT) &&
919                             (lba + count >= ATA_MAX_28BIT_LBA ||
920                             count > 256)) {
921                                 if (softc->flags & ADA_FLAG_CAN_DMA) {
922                                         if (bp->bio_cmd == BIO_READ) {
923                                                 ata_48bit_cmd(ataio, ATA_READ_DMA48,
924                                                     0, lba, count);
925                                         } else {
926                                                 ata_48bit_cmd(ataio, ATA_WRITE_DMA48,
927                                                     0, lba, count);
928                                         }
929                                 } else {
930                                         if (bp->bio_cmd == BIO_READ) {
931                                                 ata_48bit_cmd(ataio, ATA_READ_MUL48,
932                                                     0, lba, count);
933                                         } else {
934                                                 ata_48bit_cmd(ataio, ATA_WRITE_MUL48,
935                                                     0, lba, count);
936                                         }
937                                 }
938                         } else {
939                                 if (count == 256)
940                                         count = 0;
941                                 if (softc->flags & ADA_FLAG_CAN_DMA) {
942                                         if (bp->bio_cmd == BIO_READ) {
943                                                 ata_28bit_cmd(ataio, ATA_READ_DMA,
944                                                     0, lba, count);
945                                         } else {
946                                                 ata_28bit_cmd(ataio, ATA_WRITE_DMA,
947                                                     0, lba, count);
948                                         }
949                                 } else {
950                                         if (bp->bio_cmd == BIO_READ) {
951                                                 ata_28bit_cmd(ataio, ATA_READ_MUL,
952                                                     0, lba, count);
953                                         } else {
954                                                 ata_28bit_cmd(ataio, ATA_WRITE_MUL,
955                                                     0, lba, count);
956                                         }
957                                 }
958                         }
959                         break;
960                 }
961                 case BIO_DELETE:
962                 {
963                         uint64_t lba = bp->bio_pblkno;
964                         uint16_t count = bp->bio_bcount / softc->params.secsize;
965
966                         cam_fill_ataio(ataio,
967                             ada_retry_count,
968                             adadone,
969                             CAM_DIR_NONE,
970                             0,
971                             NULL,
972                             0,
973                             ada_default_timeout*1000);
974
975                         if (count >= 256)
976                                 count = 0;
977                         ata_28bit_cmd(ataio, ATA_CFA_ERASE, 0, lba, count);
978                         break;
979                 }
980                 case BIO_FLUSH:
981                         cam_fill_ataio(ataio,
982                             1,
983                             adadone,
984                             CAM_DIR_NONE,
985                             0,
986                             NULL,
987                             0,
988                             ada_default_timeout*1000);
989
990                         if (softc->flags & ADA_FLAG_CAN_48BIT)
991                                 ata_48bit_cmd(ataio, ATA_FLUSHCACHE48, 0, 0, 0);
992                         else
993                                 ata_28bit_cmd(ataio, ATA_FLUSHCACHE, 0, 0, 0);
994                         break;
995                 }
996                 start_ccb->ccb_h.ccb_state = ADA_CCB_BUFFER_IO;
997 out:
998                 start_ccb->ccb_h.ccb_bp = bp;
999                 softc->outstanding_cmds++;
1000                 xpt_action(start_ccb);
1001
1002                 /* May have more work to do, so ensure we stay scheduled */
1003                 adaschedule(periph);
1004                 break;
1005         }
1006         }
1007 }
1008
1009 static void
1010 adadone(struct cam_periph *periph, union ccb *done_ccb)
1011 {
1012         struct ada_softc *softc;
1013         struct ccb_ataio *ataio;
1014
1015         softc = (struct ada_softc *)periph->softc;
1016         ataio = &done_ccb->ataio;
1017         switch (ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) {
1018         case ADA_CCB_BUFFER_IO:
1019         case ADA_CCB_TRIM:
1020         {
1021                 struct bio *bp;
1022
1023                 bp = (struct bio *)done_ccb->ccb_h.ccb_bp;
1024                 if ((done_ccb->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP) {
1025                         int error;
1026                         
1027                         error = adaerror(done_ccb, 0, 0);
1028                         if (error == ERESTART) {
1029                                 /* A retry was scheduled, so just return. */
1030                                 return;
1031                         }
1032                         if (error != 0) {
1033                                 if (error == ENXIO) {
1034                                         /*
1035                                          * Catastrophic error.  Mark our pack as
1036                                          * invalid.
1037                                          */
1038                                         /*
1039                                          * XXX See if this is really a media
1040                                          * XXX change first?
1041                                          */
1042                                         xpt_print(periph->path,
1043                                             "Invalidating pack\n");
1044                                         softc->flags |= ADA_FLAG_PACK_INVALID;
1045                                 }
1046                                 bp->bio_error = error;
1047                                 bp->bio_resid = bp->bio_bcount;
1048                                 bp->bio_flags |= BIO_ERROR;
1049                         } else {
1050                                 bp->bio_resid = ataio->resid;
1051                                 bp->bio_error = 0;
1052                                 if (bp->bio_resid != 0)
1053                                         bp->bio_flags |= BIO_ERROR;
1054                         }
1055                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1056                                 cam_release_devq(done_ccb->ccb_h.path,
1057                                                  /*relsim_flags*/0,
1058                                                  /*reduction*/0,
1059                                                  /*timeout*/0,
1060                                                  /*getcount_only*/0);
1061                 } else {
1062                         if ((done_ccb->ccb_h.status & CAM_DEV_QFRZN) != 0)
1063                                 panic("REQ_CMP with QFRZN");
1064                         bp->bio_resid = ataio->resid;
1065                         if (ataio->resid > 0)
1066                                 bp->bio_flags |= BIO_ERROR;
1067                 }
1068                 softc->outstanding_cmds--;
1069                 if (softc->outstanding_cmds == 0)
1070                         softc->flags |= ADA_FLAG_WENT_IDLE;
1071                 if ((ataio->ccb_h.ccb_state & ADA_CCB_TYPE_MASK) ==
1072                     ADA_CCB_TRIM) {
1073                         struct trim_request *req =
1074                             (struct trim_request *)ataio->data_ptr;
1075                         int i;
1076
1077                         for (i = 1; i < softc->trim_max_ranges &&
1078                             req->bps[i]; i++) {
1079                                 struct bio *bp1 = req->bps[i];
1080                                 
1081                                 bp1->bio_resid = bp->bio_resid;
1082                                 bp1->bio_error = bp->bio_error;
1083                                 if (bp->bio_flags & BIO_ERROR)
1084                                         bp1->bio_flags |= BIO_ERROR;
1085                                 biodone(bp1);
1086                         }
1087                         softc->trim_running = 0;
1088                         biodone(bp);
1089                         adaschedule(periph);
1090                 } else
1091                         biodone(bp);
1092                 break;
1093         }
1094         case ADA_CCB_WAITING:
1095         {
1096                 /* Caller will release the CCB */
1097                 wakeup(&done_ccb->ccb_h.cbfcnp);
1098                 return;
1099         }
1100         case ADA_CCB_DUMP:
1101                 /* No-op.  We're polling */
1102                 return;
1103         default:
1104                 break;
1105         }
1106         xpt_release_ccb(done_ccb);
1107 }
1108
1109 static int
1110 adaerror(union ccb *ccb, u_int32_t cam_flags, u_int32_t sense_flags)
1111 {
1112         struct ada_softc          *softc;
1113         struct cam_periph *periph;
1114
1115         periph = xpt_path_periph(ccb->ccb_h.path);
1116         softc = (struct ada_softc *)periph->softc;
1117
1118         return(cam_periph_error(ccb, cam_flags, sense_flags, NULL));
1119 }
1120
1121 static void
1122 adagetparams(struct cam_periph *periph, struct ccb_getdev *cgd)
1123 {
1124         struct ada_softc *softc = (struct ada_softc *)periph->softc;
1125         struct disk_params *dp = &softc->params;
1126         u_int64_t lbasize48;
1127         u_int32_t lbasize;
1128
1129         dp->secsize = ata_logical_sector_size(&cgd->ident_data);
1130         if ((cgd->ident_data.atavalid & ATA_FLAG_54_58) &&
1131                 cgd->ident_data.current_heads && cgd->ident_data.current_sectors) {
1132                 dp->heads = cgd->ident_data.current_heads;
1133                 dp->secs_per_track = cgd->ident_data.current_sectors;
1134                 dp->cylinders = cgd->ident_data.cylinders;
1135                 dp->sectors = (u_int32_t)cgd->ident_data.current_size_1 |
1136                           ((u_int32_t)cgd->ident_data.current_size_2 << 16);
1137         } else {
1138                 dp->heads = cgd->ident_data.heads;
1139                 dp->secs_per_track = cgd->ident_data.sectors;
1140                 dp->cylinders = cgd->ident_data.cylinders;
1141                 dp->sectors = cgd->ident_data.cylinders * dp->heads * dp->secs_per_track;  
1142         }
1143         lbasize = (u_int32_t)cgd->ident_data.lba_size_1 |
1144                   ((u_int32_t)cgd->ident_data.lba_size_2 << 16);
1145
1146         /* use the 28bit LBA size if valid or bigger than the CHS mapping */
1147         if (cgd->ident_data.cylinders == 16383 || dp->sectors < lbasize)
1148                 dp->sectors = lbasize;
1149
1150         /* use the 48bit LBA size if valid */
1151         lbasize48 = ((u_int64_t)cgd->ident_data.lba_size48_1) |
1152                     ((u_int64_t)cgd->ident_data.lba_size48_2 << 16) |
1153                     ((u_int64_t)cgd->ident_data.lba_size48_3 << 32) |
1154                     ((u_int64_t)cgd->ident_data.lba_size48_4 << 48);
1155         if ((cgd->ident_data.support.command2 & ATA_SUPPORT_ADDRESS48) &&
1156             lbasize48 > ATA_MAX_28BIT_LBA)
1157                 dp->sectors = lbasize48;
1158 }
1159
1160 static void
1161 adasendorderedtag(void *arg)
1162 {
1163         struct ada_softc *softc = arg;
1164
1165         if (ada_send_ordered) {
1166                 if ((softc->ordered_tag_count == 0) 
1167                  && ((softc->flags & ADA_FLAG_WENT_IDLE) == 0)) {
1168                         softc->flags |= ADA_FLAG_NEED_OTAG;
1169                 }
1170                 if (softc->outstanding_cmds > 0)
1171                         softc->flags &= ~ADA_FLAG_WENT_IDLE;
1172
1173                 softc->ordered_tag_count = 0;
1174         }
1175         /* Queue us up again */
1176         callout_reset(&softc->sendordered_c,
1177             (ADA_DEFAULT_TIMEOUT * hz) / ADA_ORDEREDTAG_INTERVAL,
1178             adasendorderedtag, softc);
1179 }
1180
1181 /*
1182  * Step through all ADA peripheral drivers, and if the device is still open,
1183  * sync the disk cache to physical media.
1184  */
1185 static void
1186 adashutdown(void * arg, int howto)
1187 {
1188         struct cam_periph *periph;
1189         struct ada_softc *softc;
1190
1191         TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1192                 union ccb ccb;
1193
1194                 /* If we paniced with lock held - not recurse here. */
1195                 if (cam_periph_owned(periph))
1196                         continue;
1197                 cam_periph_lock(periph);
1198                 softc = (struct ada_softc *)periph->softc;
1199                 /*
1200                  * We only sync the cache if the drive is still open, and
1201                  * if the drive is capable of it..
1202                  */
1203                 if (((softc->flags & ADA_FLAG_OPEN) == 0) ||
1204                     (softc->flags & ADA_FLAG_CAN_FLUSHCACHE) == 0) {
1205                         cam_periph_unlock(periph);
1206                         continue;
1207                 }
1208
1209                 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1210
1211                 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
1212                 cam_fill_ataio(&ccb.ataio,
1213                                     1,
1214                                     adadone,
1215                                     CAM_DIR_NONE,
1216                                     0,
1217                                     NULL,
1218                                     0,
1219                                     ada_default_timeout*1000);
1220
1221                 if (softc->flags & ADA_FLAG_CAN_48BIT)
1222                         ata_48bit_cmd(&ccb.ataio, ATA_FLUSHCACHE48, 0, 0, 0);
1223                 else
1224                         ata_28bit_cmd(&ccb.ataio, ATA_FLUSHCACHE, 0, 0, 0);
1225                 xpt_polled_action(&ccb);
1226
1227                 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1228                         xpt_print(periph->path, "Synchronize cache failed\n");
1229
1230                 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1231                         cam_release_devq(ccb.ccb_h.path,
1232                                          /*relsim_flags*/0,
1233                                          /*reduction*/0,
1234                                          /*timeout*/0,
1235                                          /*getcount_only*/0);
1236                 cam_periph_unlock(periph);
1237         }
1238
1239         if (ada_spindown_shutdown == 0 ||
1240             (howto & (RB_HALT | RB_POWEROFF)) == 0)
1241                 return;
1242
1243         TAILQ_FOREACH(periph, &adadriver.units, unit_links) {
1244                 union ccb ccb;
1245
1246                 /* If we paniced with lock held - not recurse here. */
1247                 if (cam_periph_owned(periph))
1248                         continue;
1249                 cam_periph_lock(periph);
1250                 softc = (struct ada_softc *)periph->softc;
1251                 /*
1252                  * We only spin-down the drive if it is capable of it..
1253                  */
1254                 if ((softc->flags & ADA_FLAG_CAN_POWERMGT) == 0) {
1255                         cam_periph_unlock(periph);
1256                         continue;
1257                 }
1258
1259                 if (bootverbose)
1260                         xpt_print(periph->path, "spin-down\n");
1261
1262                 xpt_setup_ccb(&ccb.ccb_h, periph->path, CAM_PRIORITY_NORMAL);
1263
1264                 ccb.ccb_h.ccb_state = ADA_CCB_DUMP;
1265                 cam_fill_ataio(&ccb.ataio,
1266                                     1,
1267                                     adadone,
1268                                     CAM_DIR_NONE,
1269                                     0,
1270                                     NULL,
1271                                     0,
1272                                     ada_default_timeout*1000);
1273
1274                 ata_28bit_cmd(&ccb.ataio, ATA_STANDBY_IMMEDIATE, 0, 0, 0);
1275                 xpt_polled_action(&ccb);
1276
1277                 if ((ccb.ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_CMP)
1278                         xpt_print(periph->path, "Spin-down disk failed\n");
1279
1280                 if ((ccb.ccb_h.status & CAM_DEV_QFRZN) != 0)
1281                         cam_release_devq(ccb.ccb_h.path,
1282                                          /*relsim_flags*/0,
1283                                          /*reduction*/0,
1284                                          /*timeout*/0,
1285                                          /*getcount_only*/0);
1286                 cam_periph_unlock(periph);
1287         }
1288 }
1289
1290 #endif /* _KERNEL */