Merge from vendor branch LIBPCAP:
[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.7 2004/04/07 06:22:15 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     scp = malloc(sizeof(struct atapi_xpt_softc), M_ATACAM, M_INTWAIT | M_ZERO);
122     scp->ata_ch = ata_ch;
123     TAILQ_INIT(&scp->pending_hcbs);
124     LIST_INSERT_HEAD(&all_buses, scp, chain);
125     unit = device_get_unit(ata_ch->dev);
126
127     devq = cam_simq_alloc(16);
128     sim = cam_sim_alloc(atapi_action, atapi_poll, "ata", (void *)scp, 
129                         unit, 1, 1, devq);
130     cam_simq_release(devq);
131     if (sim == NULL)
132         goto error;
133     scp->sim = sim;
134
135     if (xpt_bus_register(sim, 0) != CAM_SUCCESS) {
136         goto error;
137     }
138     scp->flags |= BUS_REGISTERED;
139
140     if (xpt_create_path(&path, /*periph*/ NULL,
141                 cam_sim_path(sim), CAM_TARGET_WILDCARD,
142                 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
143         goto error;
144     }
145     scp->path = path;
146
147     CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Registered SIM for ata%d\n", unit));
148
149     setup_async_cb(scp, AC_LOST_DEVICE);
150     reinit_bus(scp, cold ? BOOT_ATTACH : ATTACH);
151     return;
152
153 error:
154     free_softc(scp);
155 }
156
157 void 
158 atapi_cam_detach_bus(struct ata_channel *ata_ch)
159 {
160     struct atapi_xpt_softc *scp = get_softc(ata_ch);
161     free_softc(scp);
162 }
163
164 void
165 atapi_cam_reinit_bus(struct ata_channel *ata_ch) {
166     struct atapi_xpt_softc *scp = get_softc(ata_ch);
167
168     /*
169      * scp might be null if the bus is being reinitialised during
170      * the boot-up sequence, before the ATAPI bus is registered.
171      */
172
173     if (scp != NULL)
174         reinit_bus(scp, RESET);
175 }
176
177 static void
178 reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason) {
179     if (scp->ata_ch->devices & ATA_ATAPI_MASTER)
180         setup_dev(scp, &scp->ata_ch->device[MASTER]);
181     if (scp->ata_ch->devices & ATA_ATAPI_SLAVE)
182         setup_dev(scp, &scp->ata_ch->device[SLAVE]);
183
184     switch (reason) {
185         case BOOT_ATTACH:
186             break;
187         case RESET:
188             xpt_async(AC_BUS_RESET, scp->path, NULL);
189             /*FALLTHROUGH*/
190         case ATTACH:
191             cam_rescan(scp->sim);
192             break;
193     }
194 }
195
196 static void
197 setup_dev(struct atapi_xpt_softc *scp, struct ata_device *atp)
198 {
199     if (atp->driver == NULL) {
200         ata_set_name(atp, "atapicam",
201                      2 * device_get_unit(atp->channel->dev) +
202                      (atp->unit == ATA_MASTER) ? 0 : 1);
203         atp->driver = (void *)scp;
204     }
205 }
206
207 static void
208 setup_async_cb(struct atapi_xpt_softc *scp, uint32_t events)
209 {
210     struct ccb_setasync csa;
211
212     xpt_setup_ccb(&csa.ccb_h, scp->path, /*priority*/ 5);
213     csa.ccb_h.func_code = XPT_SASYNC_CB;
214     csa.event_enable = events;
215     csa.callback = &atapi_async;
216     csa.callback_arg = scp->sim;
217     xpt_action((union ccb *) &csa);
218 }
219
220 static void 
221 atapi_action(struct cam_sim *sim, union ccb *ccb)
222 {
223     struct atapi_xpt_softc *softc = (struct atapi_xpt_softc*)cam_sim_softc(sim);
224     struct ccb_hdr *ccb_h = &ccb->ccb_h;
225     struct atapi_hcb *hcb = NULL;
226     int unit = cam_sim_unit(sim);
227     int bus = cam_sim_bus(sim);
228     int len, s;
229     char *buf;
230
231     switch (ccb_h->func_code) {
232     case XPT_PATH_INQ: {
233         struct ccb_pathinq *cpi = &ccb->cpi;
234
235         cpi->version_num = 1;
236         cpi->hba_inquiry = 0;
237         cpi->target_sprt = 0;
238 #if !defined(NO_ATANG)
239         cpi->hba_misc = PIM_NO_6_BYTE;
240 #else
241         cpi->hba_misc = 0;
242 #endif
243         cpi->hba_eng_cnt = 0;
244         bzero(cpi->vuhba_flags, sizeof(cpi->vuhba_flags));
245         cpi->max_target = 1;
246         cpi->max_lun = 0;
247         cpi->async_flags = 0;
248         cpi->hpath_id = 0;
249         cpi->initiator_id = 7;
250         strncpy(cpi->sim_vid, "FreeBSD", sizeof(cpi->sim_vid));
251         strncpy(cpi->hba_vid, "ATAPI", sizeof(cpi->hba_vid));
252         strncpy(cpi->dev_name, cam_sim_name(sim), sizeof cpi->dev_name);
253         cpi->unit_number = cam_sim_unit(sim);
254         cpi->bus_id = cam_sim_bus(sim);
255         cpi->base_transfer_speed = 3300;
256         if (softc->ata_ch && ccb_h->target_id != CAM_TARGET_WILDCARD) {
257             switch (softc->ata_ch->device[ccb_h->target_id].mode) {
258             case ATA_PIO1:
259                 cpi->base_transfer_speed = 5200;
260                 break;
261             case ATA_PIO2:
262                 cpi->base_transfer_speed = 7000;
263                 break;
264             case ATA_PIO3:
265                 cpi->base_transfer_speed = 11000;
266                 break;
267             case ATA_PIO4:
268             case ATA_DMA:
269             case ATA_WDMA2:
270                 cpi->base_transfer_speed = 16000;
271                 break;
272             case ATA_UDMA2:
273                 cpi->base_transfer_speed = 33000;
274                 break;
275             case ATA_UDMA4:
276                 cpi->base_transfer_speed = 66000;
277                 break;
278             case ATA_UDMA5:
279                 cpi->base_transfer_speed = 100000;
280                 break;
281             case ATA_UDMA6:
282                 cpi->base_transfer_speed = 133000;
283                 break;
284             default:
285                 break;
286             }
287         }
288         ccb->ccb_h.status = CAM_REQ_CMP;
289         xpt_done(ccb);
290         return;
291     }
292
293     case XPT_RESET_DEV: {
294         int tid = ccb_h->target_id;
295         struct ata_device *dev = get_ata_device(softc, tid);
296
297         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("dev reset\n"));
298         atapi_reinit(dev);
299         ccb->ccb_h.status = CAM_REQ_CMP;
300         xpt_done(ccb);
301         return;
302     }
303
304     case XPT_RESET_BUS:
305         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("bus reset\n"));
306         ata_reinit(softc->ata_ch);
307         ccb->ccb_h.status = CAM_REQ_CMP;
308         xpt_done(ccb);
309         return;
310
311     case XPT_SET_TRAN_SETTINGS:
312         /* ignore these, we're not doing SCSI here */
313         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
314                   ("SET_TRAN_SETTINGS not supported\n"));
315         ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
316         xpt_done(ccb);
317         return;
318
319     case XPT_GET_TRAN_SETTINGS: {
320         struct ccb_trans_settings *cts = &ccb->cts;
321
322         /*
323          * XXX The default CAM transport code is very scsi specific and
324          * doesn't understand IDE speeds very well.  Be silent about it
325          * here and let it default to what is set in XPT_PATH_INQ
326          */
327         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("GET_TRAN_SETTINGS\n"));
328         cts->valid = (CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID);
329         cts->flags &= ~(CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB);
330         ccb->ccb_h.status = CAM_REQ_CMP;
331         xpt_done(ccb);
332         return;
333     }
334
335     case XPT_CALC_GEOMETRY: {
336         struct ccb_calc_geometry *ccg;
337         unsigned int size_mb;
338         unsigned int secs_per_cylinder;
339         int extended;
340
341         CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("CALC_GEOMETRY\n"));
342         ccg = &ccb->ccg;
343         size_mb = ccg->volume_size / ((1024L * 1024L) / ccg->block_size);
344         extended = 1;
345
346         if (size_mb > 1024 && extended) {
347             ccg->heads = 255;
348             ccg->secs_per_track = 63;
349         } else {
350             ccg->heads = 64;
351             ccg->secs_per_track = 32;
352         }
353         secs_per_cylinder = ccg->heads * ccg->secs_per_track;
354         ccg->cylinders = ccg->volume_size / secs_per_cylinder;
355         ccb->ccb_h.status = CAM_REQ_CMP;
356         xpt_done(ccb);
357         return;
358     }
359
360     case XPT_SCSI_IO: {
361         struct ccb_scsiio *csio = &ccb->csio;
362         int tid = ccb_h->target_id, lid = ccb_h->target_lun;
363         struct ata_device *dev = get_ata_device(softc, tid);
364
365         CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, ("XPT_SCSI_IO\n"));
366
367         /* check that this request was not aborted already */
368         if ((ccb_h->status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
369             printf("XPT_SCSI_IO received but already in progress?\n");
370             xpt_done(ccb);
371             return;
372         }
373         if (dev == NULL) {
374             CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
375                       ("SCSI IO received for invalid device\n"));
376             ccb_h->status = CAM_TID_INVALID;
377             xpt_done(ccb);
378             return;
379         }
380         if (lid > 0) {
381             CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
382                       ("SCSI IO received for invalid lun %d\n", lid));
383             ccb_h->status = CAM_LUN_INVALID;
384             xpt_done(ccb);
385             return;
386         }
387         if ((ccb_h->flags & CAM_SCATTER_VALID)) {
388             /* scatter-gather not supported */
389             xpt_print_path(ccb_h->path);
390             printf("ATAPI-CAM does not support scatter-gather yet!\n");
391             break;
392         }
393         if ((hcb = allocate_hcb(softc, unit, bus, ccb)) == NULL)
394             goto action_oom;
395
396         ccb_h->status |= CAM_SIM_QUEUED;
397
398         bcopy((ccb_h->flags & CAM_CDB_POINTER) ?
399               csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes,
400               hcb->cmd, csio->cdb_len);
401 #ifdef CAMDEBUG
402         if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_CDB)) {
403                 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
404
405                 printf("atapi_action: hcb@%p: %s\n", hcb,
406                        scsi_cdb_string(hcb->cmd, cdb_str, sizeof(cdb_str)));
407         }
408 #endif
409
410         len = csio->dxfer_len;
411         buf = csio->data_ptr;
412
413         /* some SCSI commands require special processing */
414         switch (hcb->cmd[0]) {
415         case INQUIRY: {
416             /*
417              * many ATAPI devices seem to report more than
418              * SHORT_INQUIRY_LENGTH bytes of available INQUIRY
419              * information, but respond with some incorrect condition
420              * when actually asked for it, so we are going to pretend
421              * that only SHORT_INQUIRY_LENGTH are expected, anyway.
422              */
423             struct scsi_inquiry *inq = (struct scsi_inquiry *) &hcb->cmd[0];
424
425             if (inq->byte2 == 0 && inq->page_code == 0 &&
426                 inq->length > SHORT_INQUIRY_LENGTH) {
427                 bzero(buf, len);
428                 len = inq->length = SHORT_INQUIRY_LENGTH;
429             }
430             break;
431         }
432 #if defined(NO_ATANG)           /* EXITED IN ORIGINAL, DELETED IN ATANG */
433         case MODE_SELECT_6:
434             /* FALLTHROUGH */
435
436         case MODE_SENSE_6:
437             /*
438              * not supported by ATAPI/MMC devices (per SCSI MMC spec)
439              * translate to _10 equivalent.
440              * (actually we should do this only if we have tried 
441              * MODE_foo_6 and received ILLEGAL_REQUEST or
442              * INVALID COMMAND OPERATION CODE)
443              * alternative fix: behave like a honest CAM transport, 
444              * do not muck with CDB contents, and change scsi_cd to 
445              * always use MODE_SENSE_10 in cdgetmode(), or let scsi_cd
446              * know that this specific unit is an ATAPI/MMC one, 
447              * and in /that case/ use MODE_SENSE_10
448              */
449
450             CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, 
451                       ("Translating %s into _10 equivalent\n",
452                       (hcb->cmd[0] == MODE_SELECT_6) ?
453                       "MODE_SELECT_6" : "MODE_SENSE_6"));
454             hcb->cmd[0] |= 0x40;
455             hcb->cmd[6] = 0;
456             hcb->cmd[7] = 0;
457             hcb->cmd[8] = hcb->cmd[4];
458             hcb->cmd[9] = hcb->cmd[5];
459             hcb->cmd[4] = 0;
460             hcb->cmd[5] = 0;
461             break;
462 #endif
463
464         case READ_6:
465             /* FALLTHROUGH */
466
467         case WRITE_6:
468             CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, 
469                       ("Translating %s into _10 equivalent\n",
470                       (hcb->cmd[0] == READ_6) ? "READ_6" : "WRITE_6"));
471             hcb->cmd[0] |= 0x20;
472             hcb->cmd[9] = hcb->cmd[5];
473             hcb->cmd[8] = hcb->cmd[4];
474             hcb->cmd[7] = 0;
475             hcb->cmd[6] = 0;
476             hcb->cmd[5] = hcb->cmd[3];
477             hcb->cmd[4] = hcb->cmd[2];
478             hcb->cmd[3] = hcb->cmd[1] & 0x1f;
479             hcb->cmd[2] = 0;
480             hcb->cmd[1] = 0;
481             break;
482         }
483
484         if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN && (len & 1)) {
485             /* ATA always transfers an even number of bytes */
486             ++len;
487             buf = hcb->dxfer_alloc = malloc(len, M_ATACAM, M_INTWAIT | M_ZERO);
488         }
489         s = splbio();
490         TAILQ_INSERT_TAIL(&softc->pending_hcbs, hcb, chain);
491         splx(s);
492         if (atapi_queue_cmd(dev, hcb->cmd, buf, len,
493                             (((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN) ?
494                             ATPR_F_READ : 0) | ATPR_F_QUIET,
495                             ccb_h->timeout, atapi_cb, (void *)hcb) == 0)
496             return;
497         break;
498     }
499
500     default:
501         CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
502                   ("unsupported function code 0x%02x\n", ccb_h->func_code));
503         ccb_h->status = CAM_REQ_INVALID;
504         xpt_done(ccb);
505         return;
506     }
507
508 action_oom:
509     if (hcb != NULL)
510         free_hcb(hcb);
511     xpt_print_path(ccb_h->path);
512     printf("out of memory, freezing queue.\n");
513     softc->flags |= RESOURCE_SHORTAGE;
514     xpt_freeze_simq(sim, /*count*/ 1);
515     ccb_h->status = CAM_REQUEUE_REQ;
516     xpt_done(ccb);
517 }
518
519 static void 
520 atapi_poll(struct cam_sim *sim)
521 {
522     /* do nothing - we do not actually service any interrupts */
523     printf("atapi_poll called!\n");
524 }
525
526 static int 
527 atapi_cb(struct atapi_request *req)
528 {
529     struct atapi_hcb *hcb = (struct atapi_hcb *) req->driver;
530     struct ccb_scsiio *csio = &hcb->ccb->csio;
531     int hcb_status = req->result;
532     int s = splbio();
533
534 #ifdef CAMDEBUG
535         if (CAM_DEBUGGED(csio->ccb_h.path, CAM_DEBUG_CDB)) {
536                 printf("atapi_cb: hcb@%p status = %02x: (sk = %02x%s%s%s)\n",
537                        hcb, hcb_status, hcb_status >> 4,
538                        (hcb_status & 4) ? " ABRT" : "",
539                        (hcb_status & 2) ? " EOM" : "",
540                        (hcb_status & 1) ? " ILI" : "");
541                 printf("    %s: cmd %02x - sk=%02x asc=%02x ascq=%02x\n",
542                        req->device->name, req->ccb[0], req->sense.sense_key,
543                        req->sense.asc, req->sense.ascq);
544         }
545 #endif
546     if (hcb_status != 0) {
547         csio->scsi_status = SCSI_STATUS_CHECK_COND;
548         if ((csio->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0) {
549             csio->ccb_h.status |= CAM_AUTOSNS_VALID;
550             bcopy((void *)&req->sense, (void *)&csio->sense_data,
551                   sizeof(struct atapi_reqsense));
552         }
553         free_hcb_and_ccb_done(hcb, CAM_SCSI_STATUS_ERROR);
554     } 
555     else {
556         if (((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) &&
557             hcb->dxfer_alloc != NULL)
558             bcopy(hcb->dxfer_alloc, csio->data_ptr, csio->dxfer_len);
559         csio->scsi_status = SCSI_STATUS_OK;
560         free_hcb_and_ccb_done(hcb, CAM_REQ_CMP);
561     }
562     splx(s);
563     return 0;
564 }
565
566 static void
567 free_hcb_and_ccb_done(struct atapi_hcb *hcb, u_int32_t status)
568 {
569     struct atapi_xpt_softc *softc = hcb->softc;
570     union ccb *ccb = hcb->ccb;
571
572     if (hcb != NULL) {
573         /* we're about to free a hcb, so the shortage has ended */
574         if (softc->flags & RESOURCE_SHORTAGE) {
575             softc->flags &= ~RESOURCE_SHORTAGE;
576             status |= CAM_RELEASE_SIMQ;
577         }
578         free_hcb(hcb);
579     }
580     ccb->ccb_h.status = 
581         status | (ccb->ccb_h.status & ~(CAM_STATUS_MASK | CAM_SIM_QUEUED));
582     xpt_done(ccb);
583 }
584
585 static void 
586 atapi_async(void *callback_arg, u_int32_t code,
587             struct cam_path *path, void *arg)
588 {
589     int s = splbio();
590
591     atapi_async1(callback_arg, code, path, arg);
592     splx(s);
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         free(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 = malloc(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 = malloc(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         free(hcb->dxfer_alloc, M_ATACAM);
675     free(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         free(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 }