ATAng stage 5: sync additional function API changes from FBsd-4. We now
[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 $
c35323b6 29 * $DragonFly: src/sys/dev/disk/ata/atapi-cam.c,v 1.4 2003/11/30 20:14:18 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
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
158error:
159 free_softc(scp);
160}
161
162void
163atapi_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
169void
170atapi_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
182static void
183reinit_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
201static void
202setup_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
212static void
213setup_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
225static void
226atapi_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 cpi->hba_misc = 0;
244 cpi->hba_eng_cnt = 0;
245 bzero(cpi->vuhba_flags, sizeof(cpi->vuhba_flags));
246 cpi->max_target = 1;
247 cpi->max_lun = 0;
248 cpi->async_flags = 0;
249 cpi->hpath_id = 0;
250 cpi->initiator_id = 7;
251 strncpy(cpi->sim_vid, "FreeBSD", sizeof(cpi->sim_vid));
252 strncpy(cpi->hba_vid, "ATAPI", sizeof(cpi->hba_vid));
253 strncpy(cpi->dev_name, cam_sim_name(sim), sizeof cpi->dev_name);
254 cpi->unit_number = cam_sim_unit(sim);
255 cpi->bus_id = cam_sim_bus(sim);
256 if (softc->ata_ch && ccb_h->target_id >= 0) {
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: cpi->base_transfer_speed = 3300;
285 }
286 }
287 ccb->ccb_h.status = CAM_REQ_CMP;
288 xpt_done(ccb);
289 return;
290 }
291
292 case XPT_RESET_DEV: {
293 int tid = ccb_h->target_id;
294 struct ata_device *dev = get_ata_device(softc, tid);
295
296 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("dev reset\n"));
297 atapi_reinit(dev);
298 ccb->ccb_h.status = CAM_REQ_CMP;
299 xpt_done(ccb);
300 return;
301 }
302
303 case XPT_RESET_BUS:
304 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("bus reset\n"));
305 ata_reinit(softc->ata_ch);
306 ccb->ccb_h.status = CAM_REQ_CMP;
307 xpt_done(ccb);
308 return;
309
310 case XPT_SET_TRAN_SETTINGS:
311 /* ignore these, we're not doing SCSI here */
312 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
313 ("SET_TRAN_SETTINGS not supported\n"));
314 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
315 xpt_done(ccb);
316 return;
317
318 case XPT_GET_TRAN_SETTINGS: {
319 struct ccb_trans_settings *cts = &ccb->cts;
320
321 /*
322 * XXX The default CAM transport code is very scsi specific and
323 * doesn't understand IDE speeds very well. Be silent about it
324 * here and let it default to what is set in XPT_PATH_INQ
325 */
326 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("GET_TRAN_SETTINGS\n"));
327 cts->valid = (CCB_TRANS_DISC_VALID | CCB_TRANS_TQ_VALID);
328 cts->flags &= ~(CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB);
329 ccb->ccb_h.status = CAM_REQ_CMP;
330 xpt_done(ccb);
331 return;
332 }
333
334 case XPT_CALC_GEOMETRY: {
335 struct ccb_calc_geometry *ccg;
336 unsigned int size_mb;
337 unsigned int secs_per_cylinder;
338 int extended;
339
340 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("CALC_GEOMETRY\n"));
341 ccg = &ccb->ccg;
342 size_mb = ccg->volume_size / ((1024L * 1024L) / ccg->block_size);
343 extended = 1;
344
345 if (size_mb > 1024 && extended) {
346 ccg->heads = 255;
347 ccg->secs_per_track = 63;
348 } else {
349 ccg->heads = 64;
350 ccg->secs_per_track = 32;
351 }
352 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
353 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
354 ccb->ccb_h.status = CAM_REQ_CMP;
355 xpt_done(ccb);
356 return;
357 }
358
359 case XPT_SCSI_IO: {
360 struct ccb_scsiio *csio = &ccb->csio;
361 int tid = ccb_h->target_id, lid = ccb_h->target_lun;
362 struct ata_device *dev = get_ata_device(softc, tid);
363
364 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, ("XPT_SCSI_IO\n"));
365
366 /* check that this request was not aborted already */
367 if ((ccb_h->status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
368 printf("XPT_SCSI_IO received but already in progress?\n");
369 xpt_done(ccb);
370 return;
371 }
372 if (dev == NULL) {
373 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
374 ("SCSI IO received for invalid device\n"));
375 ccb_h->status = CAM_TID_INVALID;
376 xpt_done(ccb);
377 return;
378 }
379 if (lid > 0) {
380 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
381 ("SCSI IO received for invalid lun %d\n", lid));
382 ccb_h->status = CAM_LUN_INVALID;
383 xpt_done(ccb);
384 return;
385 }
386 if ((ccb_h->flags & CAM_SCATTER_VALID)) {
387 /* scatter-gather not supported */
388 xpt_print_path(ccb_h->path);
389 printf("ATAPI-CAM does not support scatter-gather yet!\n");
390 break;
391 }
392 if ((hcb = allocate_hcb(softc, unit, bus, ccb)) == NULL)
393 goto action_oom;
394
395 ccb_h->status |= CAM_SIM_QUEUED;
396
397 bcopy((ccb_h->flags & CAM_CDB_POINTER) ?
398 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes,
399 hcb->cmd, csio->cdb_len);
400#ifdef CAMDEBUG
401 if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_CDB)) {
402 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
403
404 printf("atapi_action: hcb@%p: %s\n", hcb,
405 scsi_cdb_string(hcb->cmd, cdb_str, sizeof(cdb_str)));
406 }
407#endif
408
409 len = csio->dxfer_len;
410 buf = csio->data_ptr;
411
412 /* some SCSI commands require special processing */
413 switch (hcb->cmd[0]) {
414 case INQUIRY: {
415 /*
416 * many ATAPI devices seem to report more than
417 * SHORT_INQUIRY_LENGTH bytes of available INQUIRY
418 * information, but respond with some incorrect condition
419 * when actually asked for it, so we are going to pretend
420 * that only SHORT_INQUIRY_LENGTH are expected, anyway.
421 */
422 struct scsi_inquiry *inq = (struct scsi_inquiry *) &hcb->cmd[0];
423
424 if (inq->byte2 == 0 && inq->page_code == 0 &&
425 inq->length > SHORT_INQUIRY_LENGTH) {
426 bzero(buf, len);
427 len = inq->length = SHORT_INQUIRY_LENGTH;
428 }
429 break;
430 }
431 case MODE_SELECT_6:
432 /* FALLTHROUGH */
433
434 case MODE_SENSE_6:
435 /*
436 * not supported by ATAPI/MMC devices (per SCSI MMC spec)
437 * translate to _10 equivalent.
438 * (actually we should do this only if we have tried
439 * MODE_foo_6 and received ILLEGAL_REQUEST or
440 * INVALID COMMAND OPERATION CODE)
441 * alternative fix: behave like a honest CAM transport,
442 * do not muck with CDB contents, and change scsi_cd to
443 * always use MODE_SENSE_10 in cdgetmode(), or let scsi_cd
444 * know that this specific unit is an ATAPI/MMC one,
445 * and in /that case/ use MODE_SENSE_10
446 */
447
448 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
449 ("Translating %s into _10 equivalent\n",
450 (hcb->cmd[0] == MODE_SELECT_6) ?
451 "MODE_SELECT_6" : "MODE_SENSE_6"));
452 hcb->cmd[0] |= 0x40;
453 hcb->cmd[6] = 0;
454 hcb->cmd[7] = 0;
455 hcb->cmd[8] = hcb->cmd[4];
456 hcb->cmd[9] = hcb->cmd[5];
457 hcb->cmd[4] = 0;
458 hcb->cmd[5] = 0;
459 break;
460
461 case READ_6:
462 /* FALLTHROUGH */
463
464 case WRITE_6:
465 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
466 ("Translating %s into _10 equivalent\n",
467 (hcb->cmd[0] == READ_6) ? "READ_6" : "WRITE_6"));
468 hcb->cmd[0] |= 0x20;
469 hcb->cmd[9] = hcb->cmd[5];
470 hcb->cmd[8] = hcb->cmd[4];
471 hcb->cmd[7] = 0;
472 hcb->cmd[6] = 0;
473 hcb->cmd[5] = hcb->cmd[3];
474 hcb->cmd[4] = hcb->cmd[2];
475 hcb->cmd[3] = hcb->cmd[1] & 0x1f;
476 hcb->cmd[2] = 0;
477 hcb->cmd[1] = 0;
478 break;
479 }
480
481 if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN && (len & 1)) {
482 /* ATA always transfers an even number of bytes */
483 if (!(buf = hcb->dxfer_alloc = malloc(++len, M_ATACAM,
c35323b6 484 M_WAITOK | M_ZERO)))
984263bc
MD
485 goto action_oom;
486 }
487 s = splbio();
488 TAILQ_INSERT_TAIL(&softc->pending_hcbs, hcb, chain);
489 splx(s);
490 if (atapi_queue_cmd(dev, hcb->cmd, buf, len,
491 (((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN) ?
492 ATPR_F_READ : 0) | ATPR_F_QUIET,
493 ccb_h->timeout, atapi_cb, (void *)hcb) == 0)
494 return;
495 break;
496 }
497
498 default:
499 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
500 ("unsupported function code 0x%02x\n", ccb_h->func_code));
501 ccb_h->status = CAM_REQ_INVALID;
502 xpt_done(ccb);
503 return;
504 }
505
506action_oom:
507 if (hcb != NULL)
508 free_hcb(hcb);
509 xpt_print_path(ccb_h->path);
510 printf("out of memory, freezing queue.\n");
511 softc->flags |= RESOURCE_SHORTAGE;
512 xpt_freeze_simq(sim, /*count*/ 1);
513 ccb_h->status = CAM_REQUEUE_REQ;
514 xpt_done(ccb);
515}
516
517static void
518atapi_poll(struct cam_sim *sim)
519{
520 /* do nothing - we do not actually service any interrupts */
521 printf("atapi_poll called!\n");
522}
523
524static int
525atapi_cb(struct atapi_request *req)
526{
527 struct atapi_hcb *hcb = (struct atapi_hcb *) req->driver;
528 struct ccb_scsiio *csio = &hcb->ccb->csio;
529 int hcb_status = req->result;
530 int s = splbio();
531
532#ifdef CAMDEBUG
533 if (CAM_DEBUGGED(csio->ccb_h.path, CAM_DEBUG_CDB)) {
534 printf("atapi_cb: hcb@%p status = %02x: (sk = %02x%s%s%s)\n",
535 hcb, hcb_status, hcb_status >> 4,
536 (hcb_status & 4) ? " ABRT" : "",
537 (hcb_status & 2) ? " EOM" : "",
538 (hcb_status & 1) ? " ILI" : "");
539 printf(" %s: cmd %02x - sk=%02x asc=%02x ascq=%02x\n",
540 req->device->name, req->ccb[0], req->sense.sense_key,
541 req->sense.asc, req->sense.ascq);
542 }
543#endif
544 if (hcb_status != 0) {
545 csio->scsi_status = SCSI_STATUS_CHECK_COND;
546 if ((csio->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0) {
547 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
548 bcopy((void *)&req->sense, (void *)&csio->sense_data,
549 sizeof(struct atapi_reqsense));
550 }
551 free_hcb_and_ccb_done(hcb, CAM_SCSI_STATUS_ERROR);
552 }
553 else {
554 if (((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) &&
555 hcb->dxfer_alloc != NULL)
556 bcopy(hcb->dxfer_alloc, csio->data_ptr, csio->dxfer_len);
557 csio->scsi_status = SCSI_STATUS_OK;
558 free_hcb_and_ccb_done(hcb, CAM_REQ_CMP);
559 }
560 splx(s);
561 return 0;
562}
563
564static void
565free_hcb_and_ccb_done(struct atapi_hcb *hcb, u_int32_t status)
566{
567 struct atapi_xpt_softc *softc = hcb->softc;
568 union ccb *ccb = hcb->ccb;
569
570 if (hcb != NULL) {
571 /* we're about to free a hcb, so the shortage has ended */
572 if (softc->flags & RESOURCE_SHORTAGE) {
573 softc->flags &= ~RESOURCE_SHORTAGE;
574 status |= CAM_RELEASE_SIMQ;
575 }
576 free_hcb(hcb);
577 }
578 ccb->ccb_h.status =
579 status | (ccb->ccb_h.status & ~(CAM_STATUS_MASK | CAM_SIM_QUEUED));
580 xpt_done(ccb);
581}
582
583static void
584atapi_async(void *callback_arg, u_int32_t code,
585 struct cam_path *path, void *arg)
586{
587 int s = splbio();
588
589 atapi_async1(callback_arg, code, path, arg);
590 splx(s);
591}
592
593static void
594atapi_async1(void *callback_arg, u_int32_t code,
595 struct cam_path* path, void *arg)
596{
597 struct atapi_xpt_softc *softc;
598 struct cam_sim *sim;
599 int targ;
600
601 sim = (struct cam_sim *) callback_arg;
602 softc = (struct atapi_xpt_softc *) cam_sim_softc(sim);
603 switch (code) {
604 case AC_LOST_DEVICE:
605 targ = xpt_path_target_id(path);
606 xpt_print_path(path);
607 if (targ == -1)
608 printf("Lost host adapter\n");
609 else
610 printf("Lost target %d???\n", targ);
611 break;
612
613 default:
614 break;
615 }
616}
617
618static void
619cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
620{
621 if (ccb->ccb_h.status != CAM_REQ_CMP) {
622 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
623 ("Rescan failed, 0x%04x\n", ccb->ccb_h.status));
624 } else {
625 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
626 ("Rescan succeeded\n"));
627 }
628 xpt_free_path(ccb->ccb_h.path);
629 free(ccb, M_ATACAM);
630}
631
632static void
633cam_rescan(struct cam_sim *sim)
634{
635 struct cam_path *path;
636 union ccb *ccb = malloc(sizeof(union ccb), M_ATACAM, M_WAITOK | M_ZERO);
637
638 if (xpt_create_path(&path, xpt_periph, cam_sim_path(sim),
639 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP)
640 return;
641
642 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("Rescanning ATAPI bus.\n"));
643 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
644 ccb->ccb_h.func_code = XPT_SCAN_BUS;
645 ccb->ccb_h.cbfcnp = cam_rescan_callback;
646 ccb->crcn.flags = CAM_FLAG_NONE;
647 xpt_action(ccb);
648 /* scan is in progress now */
649}
650
651static struct atapi_hcb *
652allocate_hcb(struct atapi_xpt_softc *softc, int unit, int bus, union ccb *ccb)
653{
654 struct atapi_hcb *hcb = (struct atapi_hcb *)
c35323b6 655 malloc(sizeof(struct atapi_hcb), M_ATACAM, M_WAITOK | M_ZERO);
984263bc
MD
656
657 if (hcb != NULL) {
658 hcb->softc = softc;
659 hcb->unit = unit;
660 hcb->bus = bus;
661 hcb->ccb = ccb;
662 }
663 return hcb;
664}
665
666static void
667free_hcb(struct atapi_hcb *hcb)
668{
669 TAILQ_REMOVE(&hcb->softc->pending_hcbs, hcb, chain);
670 if (hcb->dxfer_alloc != NULL)
671 free(hcb->dxfer_alloc, M_ATACAM);
672 free(hcb, M_ATACAM);
673}
674
675static void
676free_softc(struct atapi_xpt_softc *scp)
677{
678 struct atapi_hcb *hcb;
679
680 if (scp != NULL) {
681 TAILQ_FOREACH(hcb, &scp->pending_hcbs, chain) {
682 free_hcb_and_ccb_done(hcb, CAM_UNREC_HBA_ERROR);
683 }
684 if (scp->path != NULL) {
685 setup_async_cb(scp, 0);
686 xpt_free_path(scp->path);
687 }
688 if ((scp->flags & BUS_REGISTERED) != 0) {
689 if (xpt_bus_deregister(cam_sim_path(scp->sim)) == CAM_REQ_CMP)
690 scp->flags &= ~BUS_REGISTERED;
691 }
692 if (scp->sim != NULL) {
693 if ((scp->flags & BUS_REGISTERED) == 0)
694 cam_sim_free(scp->sim, /*free_devq*/TRUE);
695 else
696 printf("Can't free %s SIM (still registered)\n",
697 cam_sim_name(scp->sim));
698 }
699 LIST_REMOVE(scp, chain);
700 free(scp, M_ATACAM);
701 }
702}
703
704static struct atapi_xpt_softc *
705get_softc(struct ata_channel *ata_ch) {
706 struct atapi_xpt_softc *scp;
707 LIST_FOREACH(scp, &all_buses, chain) {
708 if (scp->ata_ch == ata_ch)
709 return scp;
710 }
711 return NULL;
712}
713
714static struct ata_device *
715get_ata_device(struct atapi_xpt_softc *scp, int id)
716{
717 int role = ATA_ATAPI_MASTER;
718
719 switch (id) {
720 case 1:
721 role = ATA_ATAPI_SLAVE;
722 /* FALLTHROUGH */
723
724 case 0:
725 if (scp->ata_ch->devices & role)
726 return &scp->ata_ch->device[id];
727 /* FALLTHROUGH */
728
729 default:
730 return NULL;
731 }
732}