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