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