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