Merge from vendor branch OPENSSH:
[dragonfly.git] / sys / dev / disk / ata / atapi-cam.c
1 /*-
2  * Copyright (c) 2001,2002 Thomas Quinot <thomas@cuivre.fr.eu.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  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  *
28  * $FreeBSD: src/sys/dev/ata/atapi-cam.c,v 1.10.2.3 2003/05/21 09:24:55 thomas Exp $
29  * $DragonFly: src/sys/dev/disk/ata/atapi-cam.c,v 1.9 2006/09/05 00:55:37 dillon Exp $
30  */
31
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/bus.h>
35 #include <sys/devicestat.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 #include <sys/ata.h>
39 #include <sys/thread2.h>
40 #include <machine/bus.h>
41
42 #include <bus/cam/cam.h>
43 #include <bus/cam/cam_ccb.h>
44 #include <bus/cam/cam_periph.h>
45 #include <bus/cam/cam_sim.h>
46 #include <bus/cam/cam_xpt_sim.h>
47 #include <bus/cam/cam_debug.h>
48 #include <bus/cam/scsi/scsi_all.h>
49
50 #include "ata-all.h"
51 #include "atapi-all.h"
52
53 /* hardware command descriptor block */
54 struct atapi_hcb {
55     struct atapi_xpt_softc *softc;
56     int                 unit;
57     int                 bus;
58     int                 target;
59     int                 lun;
60     union ccb           *ccb;
61     u_int8_t            cmd[CAM_MAX_CDBLEN];
62     int                 flags;
63 #define DOING_AUTOSENSE 1
64
65     char                *dxfer_alloc;
66     TAILQ_ENTRY(atapi_hcb) chain;
67 };
68
69 /* private data associated with an ATA bus */
70 struct atapi_xpt_softc {
71     struct ata_channel  *ata_ch;
72     struct cam_path     *path;
73     struct cam_sim      *sim;
74     int                 flags;
75 #define BUS_REGISTERED          0x01
76 #define RESOURCE_SHORTAGE       0x02
77
78     TAILQ_HEAD(,atapi_hcb) pending_hcbs;
79     LIST_ENTRY(atapi_xpt_softc) chain;
80 };
81
82 enum reinit_reason { BOOT_ATTACH, ATTACH, RESET };
83
84 static LIST_HEAD(,atapi_xpt_softc) all_buses = LIST_HEAD_INITIALIZER(all_buses);
85
86 /* CAM XPT methods */
87 static void atapi_action(struct cam_sim *, union ccb *);
88 static void atapi_poll(struct cam_sim *);
89 static void atapi_async(void *, u_int32_t, struct cam_path *, void *);
90 static void atapi_async1(void *, u_int32_t, struct cam_path *, void *);
91 static int atapi_cb(struct atapi_request *);
92
93 /* internal functions */
94 static void reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason);
95 static void setup_dev(struct atapi_xpt_softc *, struct ata_device *);
96 static void setup_async_cb(struct atapi_xpt_softc *, uint32_t);
97 static void cam_rescan_callback(struct cam_periph *, union ccb *);
98 static void cam_rescan(struct cam_sim *);
99 static void free_hcb_and_ccb_done(struct atapi_hcb *, u_int32_t);
100 static struct atapi_hcb *allocate_hcb(struct atapi_xpt_softc *, int, int, union ccb *);
101 static void free_hcb(struct atapi_hcb *hcb);
102 static void free_softc(struct atapi_xpt_softc *scp);
103 static struct atapi_xpt_softc *get_softc(struct ata_channel *ata_ch);
104 static struct ata_device *get_ata_device(struct atapi_xpt_softc *scp, int id);
105
106 static MALLOC_DEFINE(M_ATACAM, "ATA CAM transport", "ATA driver CAM-XPT layer");
107
108 void 
109 atapi_cam_attach_bus(struct ata_channel *ata_ch)
110 {
111     struct atapi_xpt_softc *scp = NULL;
112     struct cam_devq *devq = NULL;
113     struct cam_sim *sim = NULL;
114     struct cam_path *path = NULL;
115     int unit;
116
117     LIST_FOREACH(scp, &all_buses, chain) {
118         if (scp->ata_ch == ata_ch)
119             return;
120     }
121
122     scp = kmalloc(sizeof(struct atapi_xpt_softc), M_ATACAM, M_INTWAIT | M_ZERO);
123     scp->ata_ch = ata_ch;
124     TAILQ_INIT(&scp->pending_hcbs);
125     LIST_INSERT_HEAD(&all_buses, scp, chain);
126     unit = device_get_unit(ata_ch->dev);
127
128     devq = cam_simq_alloc(16);
129     sim = cam_sim_alloc(atapi_action, atapi_poll, "ata", (void *)scp, 
130                         unit, 1, 1, devq);
131     cam_simq_release(devq);
132     if (sim == NULL)
133         goto error;
134     scp->sim = sim;
135
136     if (xpt_bus_register(sim, 0) != CAM_SUCCESS) {
137         goto error;
138     }
139     scp->flags |= BUS_REGISTERED;
140
141     if (xpt_create_path(&path, /*periph*/ NULL,
142                 cam_sim_path(sim), CAM_TARGET_WILDCARD,
143                 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
144         goto error;
145     }
146     scp->path = path;
147
148     CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Registered SIM for ata%d\n", unit));
149
150     setup_async_cb(scp, AC_LOST_DEVICE);
151     reinit_bus(scp, cold ? BOOT_ATTACH : ATTACH);
152     return;
153
154 error:
155     free_softc(scp);
156 }
157
158 void 
159 atapi_cam_detach_bus(struct ata_channel *ata_ch)
160 {
161     struct atapi_xpt_softc *scp = get_softc(ata_ch);
162     free_softc(scp);
163 }
164
165 void
166 atapi_cam_reinit_bus(struct ata_channel *ata_ch) {
167     struct atapi_xpt_softc *scp = get_softc(ata_ch);
168
169     /*
170      * scp might be null if the bus is being reinitialised during
171      * the boot-up sequence, before the ATAPI bus is registered.
172      */
173
174     if (scp != NULL)
175         reinit_bus(scp, RESET);
176 }
177
178 static void
179 reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason) {
180     if (scp->ata_ch->devices & ATA_ATAPI_MASTER)
181         setup_dev(scp, &scp->ata_ch->device[MASTER]);
182     if (scp->ata_ch->devices & ATA_ATAPI_SLAVE)
183         setup_dev(scp, &scp->ata_ch->device[SLAVE]);
184
185     switch (reason) {
186         case BOOT_ATTACH:
187             break;
188         case RESET:
189             xpt_async(AC_BUS_RESET, scp->path, NULL);
190             /*FALLTHROUGH*/
191         case ATTACH:
192             cam_rescan(scp->sim);
193             break;
194     }
195 }
196
197 static void
198 setup_dev(struct atapi_xpt_softc *scp, struct ata_device *atp)
199 {
200     if (atp->driver == NULL) {
201         ata_set_name(atp, "atapicam",
202                      2 * device_get_unit(atp->channel->dev) +
203                      (atp->unit == ATA_MASTER) ? 0 : 1);
204         atp->driver = (void *)scp;
205     }
206 }
207
208 static void
209 setup_async_cb(struct atapi_xpt_softc *scp, uint32_t events)
210 {
211     struct ccb_setasync csa;
212
213     xpt_setup_ccb(&csa.ccb_h, scp->path, /*priority*/ 5);
214     csa.ccb_h.func_code = XPT_SASYNC_CB;
215     csa.event_enable = events;
216     csa.callback = &atapi_async;
217     csa.callback_arg = scp->sim;
218     xpt_action((union ccb *) &csa);
219 }
220
221 static void 
222 atapi_action(struct cam_sim *sim, union ccb *ccb)
223 {
224     struct atapi_xpt_softc *softc = (struct atapi_xpt_softc*)cam_sim_softc(sim);
225     struct ccb_hdr *ccb_h = &ccb->ccb_h;
226     struct atapi_hcb *hcb = NULL;
227     int unit = cam_sim_unit(sim);
228     int bus = cam_sim_bus(sim);
229     int len;
230     char *buf;
231
232     switch (ccb_h->func_code) {
233     case XPT_PATH_INQ: {
234         struct ccb_pathinq *cpi = &ccb->cpi;
235
236         cpi->version_num = 1;
237         cpi->hba_inquiry = 0;
238         cpi->target_sprt = 0;
239 #if !defined(NO_ATANG)
240         cpi->hba_misc = PIM_NO_6_BYTE;
241 #else
242         cpi->hba_misc = 0;
243 #endif
244         cpi->hba_eng_cnt = 0;
245         bzero(cpi->vuhba_flags, sizeof(cpi->vuhba_flags));
246         cpi->max_target = 1;
247         cpi->max_lun = 0;
248         cpi->async_flags = 0;
249         cpi->hpath_id = 0;
250         cpi->initiator_id = 7;
251         strncpy(cpi->sim_vid, "FreeBSD", sizeof(cpi->sim_vid));
252         strncpy(cpi->hba_vid, "ATAPI", sizeof(cpi->hba_vid));
253         strncpy(cpi->dev_name, cam_sim_name(sim), sizeof cpi->dev_name);
254         cpi->unit_number = cam_sim_unit(sim);
255         cpi->bus_id = cam_sim_bus(sim);
256         cpi->base_transfer_speed = 3300;
257         if (softc->ata_ch && ccb_h->target_id != CAM_TARGET_WILDCARD) {
258             switch (softc->ata_ch->device[ccb_h->target_id].mode) {
259             case ATA_PIO1:
260                 cpi->base_transfer_speed = 5200;
261                 break;
262             case ATA_PIO2:
263                 cpi->base_transfer_speed = 7000;
264                 break;
265             case ATA_PIO3:
266                 cpi->base_transfer_speed = 11000;
267                 break;
268             case ATA_PIO4:
269             case ATA_DMA:
270             case ATA_WDMA2:
271                 cpi->base_transfer_speed = 16000;
272                 break;
273             case ATA_UDMA2:
274                 cpi->base_transfer_speed = 33000;
275                 break;
276             case ATA_UDMA4:
277                 cpi->base_transfer_speed = 66000;
278                 break;
279             case ATA_UDMA5:
280                 cpi->base_transfer_speed = 100000;
281                 break;
282             case ATA_UDMA6:
283                 cpi->base_transfer_speed = 133000;
284                 break;
285             default:
286                 break;
287             }
288         }
289         ccb->ccb_h.status = CAM_REQ_CMP;
290         xpt_done(ccb);
291         return;
292     }
293
294     case XPT_RESET_DEV: {
295         int tid = ccb_h->target_id;
296         struct ata_device *dev = get_ata_device(softc, tid);
297
298         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("dev reset\n"));
299         atapi_reinit(dev);
300         ccb->ccb_h.status = CAM_REQ_CMP;
301         xpt_done(ccb);
302         return;
303     }
304
305     case XPT_RESET_BUS:
306         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("bus reset\n"));
307         ata_reinit(softc->ata_ch);
308         ccb->ccb_h.status = CAM_REQ_CMP;
309         xpt_done(ccb);
310         return;
311
312     case XPT_SET_TRAN_SETTINGS:
313         /* ignore these, we're not doing SCSI here */
314         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
315                   ("SET_TRAN_SETTINGS not supported\n"));
316         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
317         xpt_done(ccb);
318         return;
319
320     case XPT_GET_TRAN_SETTINGS: {
321         struct ccb_trans_settings *cts = &ccb->cts;
322
323         /*
324          * XXX The default CAM transport code is very scsi specific and
325          * doesn't understand IDE speeds very well.  Be silent about it
326          * here and let it default to what is set in XPT_PATH_INQ
327          */
328         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("GET_TRAN_SETTINGS\n"));
329         cts->valid = (CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID);
330         cts->flags &= ~(CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB);
331         ccb->ccb_h.status = CAM_REQ_CMP;
332         xpt_done(ccb);
333         return;
334     }
335
336     case XPT_CALC_GEOMETRY: {
337         struct ccb_calc_geometry *ccg;
338         unsigned int size_mb;
339         unsigned int secs_per_cylinder;
340         int extended;
341
342         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("CALC_GEOMETRY\n"));
343         ccg = &ccb->ccg;
344         size_mb = ccg->volume_size / ((1024L * 1024L) / ccg->block_size);
345         extended = 1;
346
347         if (size_mb > 1024 && extended) {
348             ccg->heads = 255;
349             ccg->secs_per_track = 63;
350         } else {
351             ccg->heads = 64;
352             ccg->secs_per_track = 32;
353         }
354         secs_per_cylinder = ccg->heads * ccg->secs_per_track;
355         ccg->cylinders = ccg->volume_size / secs_per_cylinder;
356         ccb->ccb_h.status = CAM_REQ_CMP;
357         xpt_done(ccb);
358         return;
359     }
360
361     case XPT_SCSI_IO: {
362         struct ccb_scsiio *csio = &ccb->csio;
363         int tid = ccb_h->target_id, lid = ccb_h->target_lun;
364         struct ata_device *dev = get_ata_device(softc, tid);
365
366         CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, ("XPT_SCSI_IO\n"));
367
368         /* check that this request was not aborted already */
369         if ((ccb_h->status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
370             printf("XPT_SCSI_IO received but already in progress?\n");
371             xpt_done(ccb);
372             return;
373         }
374         if (dev == NULL) {
375             CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
376                       ("SCSI IO received for invalid device\n"));
377             ccb_h->status = CAM_TID_INVALID;
378             xpt_done(ccb);
379             return;
380         }
381         if (lid > 0) {
382             CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
383                       ("SCSI IO received for invalid lun %d\n", lid));
384             ccb_h->status = CAM_LUN_INVALID;
385             xpt_done(ccb);
386             return;
387         }
388         if ((ccb_h->flags & CAM_SCATTER_VALID)) {
389             /* scatter-gather not supported */
390             xpt_print_path(ccb_h->path);
391             printf("ATAPI-CAM does not support scatter-gather yet!\n");
392             break;
393         }
394         if ((hcb = allocate_hcb(softc, unit, bus, ccb)) == NULL)
395             goto action_oom;
396
397         ccb_h->status |= CAM_SIM_QUEUED;
398
399         bcopy((ccb_h->flags & CAM_CDB_POINTER) ?
400               csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes,
401               hcb->cmd, csio->cdb_len);
402 #ifdef CAMDEBUG
403         if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_CDB)) {
404                 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
405
406                 printf("atapi_action: hcb@%p: %s\n", hcb,
407                        scsi_cdb_string(hcb->cmd, cdb_str, sizeof(cdb_str)));
408         }
409 #endif
410
411         len = csio->dxfer_len;
412         buf = csio->data_ptr;
413
414         /* some SCSI commands require special processing */
415         switch (hcb->cmd[0]) {
416         case INQUIRY: {
417             /*
418              * many ATAPI devices seem to report more than
419              * SHORT_INQUIRY_LENGTH bytes of available INQUIRY
420              * information, but respond with some incorrect condition
421              * when actually asked for it, so we are going to pretend
422              * that only SHORT_INQUIRY_LENGTH are expected, anyway.
423              */
424             struct scsi_inquiry *inq = (struct scsi_inquiry *) &hcb->cmd[0];
425
426             if (inq->byte2 == 0 && inq->page_code == 0 &&
427                 inq->length > SHORT_INQUIRY_LENGTH) {
428                 bzero(buf, len);
429                 len = inq->length = SHORT_INQUIRY_LENGTH;
430             }
431             break;
432         }
433 #if defined(NO_ATANG)           /* EXITED IN ORIGINAL, DELETED IN ATANG */
434         case MODE_SELECT_6:
435             /* FALLTHROUGH */
436
437         case MODE_SENSE_6:
438             /*
439              * not supported by ATAPI/MMC devices (per SCSI MMC spec)
440              * translate to _10 equivalent.
441              * (actually we should do this only if we have tried 
442              * MODE_foo_6 and received ILLEGAL_REQUEST or
443              * INVALID COMMAND OPERATION CODE)
444              * alternative fix: behave like a honest CAM transport, 
445              * do not muck with CDB contents, and change scsi_cd to 
446              * always use MODE_SENSE_10 in cdgetmode(), or let scsi_cd
447              * know that this specific unit is an ATAPI/MMC one, 
448              * and in /that case/ use MODE_SENSE_10
449              */
450
451             CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, 
452                       ("Translating %s into _10 equivalent\n",
453                       (hcb->cmd[0] == MODE_SELECT_6) ?
454                       "MODE_SELECT_6" : "MODE_SENSE_6"));
455             hcb->cmd[0] |= 0x40;
456             hcb->cmd[6] = 0;
457             hcb->cmd[7] = 0;
458             hcb->cmd[8] = hcb->cmd[4];
459             hcb->cmd[9] = hcb->cmd[5];
460             hcb->cmd[4] = 0;
461             hcb->cmd[5] = 0;
462             break;
463 #endif
464
465         case READ_6:
466             /* FALLTHROUGH */
467
468         case WRITE_6:
469             CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, 
470                       ("Translating %s into _10 equivalent\n",
471                       (hcb->cmd[0] == READ_6) ? "READ_6" : "WRITE_6"));
472             hcb->cmd[0] |= 0x20;
473             hcb->cmd[9] = hcb->cmd[5];
474             hcb->cmd[8] = hcb->cmd[4];
475             hcb->cmd[7] = 0;
476             hcb->cmd[6] = 0;
477             hcb->cmd[5] = hcb->cmd[3];
478             hcb->cmd[4] = hcb->cmd[2];
479             hcb->cmd[3] = hcb->cmd[1] & 0x1f;
480             hcb->cmd[2] = 0;
481             hcb->cmd[1] = 0;
482             break;
483         }
484
485         if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN && (len & 1)) {
486             /* ATA always transfers an even number of bytes */
487             ++len;
488             buf = hcb->dxfer_alloc = kmalloc(len, M_ATACAM, M_INTWAIT | M_ZERO);
489         }
490         crit_enter();
491         TAILQ_INSERT_TAIL(&softc->pending_hcbs, hcb, chain);
492         crit_exit();
493         if (atapi_queue_cmd(dev, hcb->cmd, buf, len,
494                             (((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN) ?
495                             ATPR_F_READ : 0) | ATPR_F_QUIET,
496                             ccb_h->timeout, atapi_cb, (void *)hcb) == 0)
497             return;
498         break;
499     }
500
501     default:
502         CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
503                   ("unsupported function code 0x%02x\n", ccb_h->func_code));
504         ccb_h->status = CAM_REQ_INVALID;
505         xpt_done(ccb);
506         return;
507     }
508
509 action_oom:
510     if (hcb != NULL)
511         free_hcb(hcb);
512     xpt_print_path(ccb_h->path);
513     printf("out of memory, freezing queue.\n");
514     softc->flags |= RESOURCE_SHORTAGE;
515     xpt_freeze_simq(sim, /*count*/ 1);
516     ccb_h->status = CAM_REQUEUE_REQ;
517     xpt_done(ccb);
518 }
519
520 static void 
521 atapi_poll(struct cam_sim *sim)
522 {
523     /* do nothing - we do not actually service any interrupts */
524     printf("atapi_poll called!\n");
525 }
526
527 static int 
528 atapi_cb(struct atapi_request *req)
529 {
530     struct atapi_hcb *hcb = (struct atapi_hcb *) req->driver;
531     struct ccb_scsiio *csio = &hcb->ccb->csio;
532     int hcb_status = req->result;
533
534     crit_enter();
535 #ifdef CAMDEBUG
536         if (CAM_DEBUGGED(csio->ccb_h.path, CAM_DEBUG_CDB)) {
537                 printf("atapi_cb: hcb@%p status = %02x: (sk = %02x%s%s%s)\n",
538                        hcb, hcb_status, hcb_status >> 4,
539                        (hcb_status & 4) ? " ABRT" : "",
540                        (hcb_status & 2) ? " EOM" : "",
541                        (hcb_status & 1) ? " ILI" : "");
542                 printf("    %s: cmd %02x - sk=%02x asc=%02x ascq=%02x\n",
543                        req->device->name, req->ccb[0], req->sense.sense_key,
544                        req->sense.asc, req->sense.ascq);
545         }
546 #endif
547     if (hcb_status != 0) {
548         csio->scsi_status = SCSI_STATUS_CHECK_COND;
549         if ((csio->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0) {
550             csio->ccb_h.status |= CAM_AUTOSNS_VALID;
551             bcopy((void *)&req->sense, (void *)&csio->sense_data,
552                   sizeof(struct atapi_reqsense));
553         }
554         free_hcb_and_ccb_done(hcb, CAM_SCSI_STATUS_ERROR);
555     } 
556     else {
557         if (((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) &&
558             hcb->dxfer_alloc != NULL)
559             bcopy(hcb->dxfer_alloc, csio->data_ptr, csio->dxfer_len);
560         csio->scsi_status = SCSI_STATUS_OK;
561         free_hcb_and_ccb_done(hcb, CAM_REQ_CMP);
562     }
563     crit_exit();
564     return 0;
565 }
566
567 static void
568 free_hcb_and_ccb_done(struct atapi_hcb *hcb, u_int32_t status)
569 {
570     struct atapi_xpt_softc *softc = hcb->softc;
571     union ccb *ccb = hcb->ccb;
572
573     if (hcb != NULL) {
574         /* we're about to free a hcb, so the shortage has ended */
575         if (softc->flags & RESOURCE_SHORTAGE) {
576             softc->flags &= ~RESOURCE_SHORTAGE;
577             status |= CAM_RELEASE_SIMQ;
578         }
579         free_hcb(hcb);
580     }
581     ccb->ccb_h.status = 
582         status | (ccb->ccb_h.status & ~(CAM_STATUS_MASK | CAM_SIM_QUEUED));
583     xpt_done(ccb);
584 }
585
586 static void 
587 atapi_async(void *callback_arg, u_int32_t code,
588             struct cam_path *path, void *arg)
589 {
590     crit_enter();
591     atapi_async1(callback_arg, code, path, arg);
592     crit_exit();
593 }
594
595 static void 
596 atapi_async1(void *callback_arg, u_int32_t code,
597              struct cam_path* path, void *arg)
598 {
599     struct atapi_xpt_softc *softc;
600     struct cam_sim *sim;
601     int targ;
602
603     sim = (struct cam_sim *) callback_arg;
604     softc = (struct atapi_xpt_softc *) cam_sim_softc(sim);
605     switch (code) {
606     case AC_LOST_DEVICE:
607         targ = xpt_path_target_id(path);
608         xpt_print_path(path);
609         if (targ == -1)
610                 printf("Lost host adapter\n");
611         else
612                 printf("Lost target %d???\n", targ);
613         break;
614
615     default:
616         break;
617     }
618 }
619
620 static void
621 cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
622 {
623         if (ccb->ccb_h.status != CAM_REQ_CMP) {
624             CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
625                       ("Rescan failed, 0x%04x\n", ccb->ccb_h.status));
626         } else {
627             CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
628                       ("Rescan succeeded\n"));
629         }
630         xpt_free_path(ccb->ccb_h.path);
631         kfree(ccb, M_ATACAM);
632 }
633
634 static void
635 cam_rescan(struct cam_sim *sim)
636 {
637     struct cam_path *path;
638     union ccb *ccb = kmalloc(sizeof(union ccb), M_ATACAM, M_INTWAIT | M_ZERO);
639     
640     if (xpt_create_path(&path, xpt_periph, cam_sim_path(sim),
641                         CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP)
642         return;
643
644     CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("Rescanning ATAPI bus.\n"));
645     xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
646     ccb->ccb_h.func_code = XPT_SCAN_BUS;
647     ccb->ccb_h.cbfcnp = cam_rescan_callback;
648     ccb->crcn.flags = CAM_FLAG_NONE;
649     xpt_action(ccb);
650     /* scan is in progress now */
651 }
652
653 static struct atapi_hcb *
654 allocate_hcb(struct atapi_xpt_softc *softc, int unit, int bus, union ccb *ccb)
655 {
656     struct atapi_hcb *hcb;
657
658     hcb = kmalloc(sizeof(struct atapi_hcb), M_ATACAM, M_INTWAIT | M_ZERO);
659
660     if (hcb != NULL) {
661         hcb->softc = softc;
662         hcb->unit = unit;
663         hcb->bus = bus;
664         hcb->ccb = ccb;
665     }
666     return hcb;
667 }
668
669 static void 
670 free_hcb(struct atapi_hcb *hcb)
671 {
672     TAILQ_REMOVE(&hcb->softc->pending_hcbs, hcb, chain);
673     if (hcb->dxfer_alloc != NULL)
674         kfree(hcb->dxfer_alloc, M_ATACAM);
675     kfree(hcb, M_ATACAM);
676 }
677
678 static void
679 free_softc(struct atapi_xpt_softc *scp)
680 {
681     struct atapi_hcb *hcb;
682
683     if (scp != NULL) {
684         TAILQ_FOREACH(hcb, &scp->pending_hcbs, chain) {
685             free_hcb_and_ccb_done(hcb, CAM_UNREC_HBA_ERROR);
686         }
687         if (scp->path != NULL) {
688             setup_async_cb(scp, 0);
689             xpt_free_path(scp->path);
690         }
691         if ((scp->flags & BUS_REGISTERED) != 0) {
692             if (xpt_bus_deregister(cam_sim_path(scp->sim)) == CAM_REQ_CMP)
693                 scp->flags &= ~BUS_REGISTERED;
694         }
695         if (scp->sim != NULL) {
696             if ((scp->flags & BUS_REGISTERED) == 0)
697                 cam_sim_free(scp->sim);
698             else
699                 printf("Can't free %s SIM (still registered)\n",
700                        cam_sim_name(scp->sim));
701         }
702         LIST_REMOVE(scp, chain);
703         kfree(scp, M_ATACAM);
704     }
705 }
706
707 static struct atapi_xpt_softc *
708 get_softc(struct ata_channel *ata_ch) {
709     struct atapi_xpt_softc *scp;
710     LIST_FOREACH(scp, &all_buses, chain) {
711         if (scp->ata_ch == ata_ch)
712             return scp;
713     }
714     return NULL;
715 }
716
717 static struct ata_device *
718 get_ata_device(struct atapi_xpt_softc *scp, int id)
719 {
720     int role = ATA_ATAPI_MASTER;
721
722     switch (id) {
723     case 1:
724         role = ATA_ATAPI_SLAVE;
725         /* FALLTHROUGH */
726
727     case 0:
728         if (scp->ata_ch->devices & role)
729             return &scp->ata_ch->device[id];
730         /* FALLTHROUGH */
731
732     default:
733         return NULL;
734     }
735 }