We don't have /dev/audio support any more, but make it symlink to /dev/dsp
[dragonfly.git] / sys / dev / disk / nata / atapi-cam.c
CommitLineData
c1b3d7c5
TS
1/*-
2 * Copyright (c) 2001-2003 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.44 2006/03/31 08:09:05 sos Exp $
c436b8b4 29 * $DragonFly: src/sys/dev/disk/nata/atapi-cam.c,v 1.11 2008/11/17 07:45:48 hasso Exp $
c1b3d7c5
TS
30 */
31
32#include "opt_ata.h"
d6aaf612 33#include "opt_scsi.h"
c1b3d7c5
TS
34
35#include <sys/param.h>
36#include <sys/bus.h>
37#include <sys/libkern.h>
38#include <sys/lock.h> /* for {get,rel}_mplock() */
39#include <sys/malloc.h>
40#include <sys/module.h>
41#include <sys/nata.h>
42#include <sys/spinlock.h>
43#include <sys/spinlock2.h>
44#include <sys/queue.h>
45#include <sys/systm.h>
46#include <sys/thread2.h>
47
48#include <bus/cam/cam.h>
49#include <bus/cam/cam_ccb.h>
50#include <bus/cam/cam_periph.h>
51#include <bus/cam/cam_sim.h>
52#include <bus/cam/cam_xpt_sim.h>
53#include <bus/cam/cam_debug.h>
54#include <bus/cam/scsi/scsi_all.h>
55
56#include "ata-all.h"
c1b3d7c5
TS
57#include "ata_if.h"
58
59/* private data associated with an ATA bus */
60struct atapi_xpt_softc {
61 struct ata_device atapi_cam_dev; /* must be first */
62 device_t dev;
63 device_t parent;
64 struct ata_channel *ata_ch;
65 struct cam_path *path;
66 struct cam_sim *sim;
67 int flags;
68#define BUS_REGISTERED 0x01
69#define RESOURCE_SHORTAGE 0x02
70#define DETACHING 0x04
71
72 TAILQ_HEAD(,atapi_hcb) pending_hcbs;
73 struct ata_device *atadev[2];
74 struct spinlock state_lock;
75};
76
77/* hardware command descriptor block */
78struct atapi_hcb {
79 struct atapi_xpt_softc *softc;
80 int unit;
81 int bus;
82 int target;
83 int lun;
84 union ccb *ccb;
85 int flags;
86#define QUEUED 0x0001
87#define AUTOSENSE 0x0002
88 char *dxfer_alloc;
89 TAILQ_ENTRY(atapi_hcb) chain;
90};
91
dbcd0c9b 92enum reinit_reason { ATTACH, RESET };
c1b3d7c5
TS
93
94/* Device methods */
95static void atapi_cam_identify(device_t *dev, device_t parent);
96static int atapi_cam_probe(device_t dev);
97static int atapi_cam_attach(device_t dev);
98static int atapi_cam_detach(device_t dev);
99static int atapi_cam_reinit(device_t dev);
100
101/* CAM XPT methods */
102static void atapi_action(struct cam_sim *, union ccb *);
103static void atapi_poll(struct cam_sim *);
104static void atapi_async(void *, u_int32_t, struct cam_path *, void *);
105static void atapi_cb(struct ata_request *);
106
107/* Module methods */
108static int atapi_cam_event_handler(module_t mod, int what, void *arg);
109
110/* internal functions */
111static void reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason);
112static void setup_async_cb(struct atapi_xpt_softc *, uint32_t);
113static void cam_rescan_callback(struct cam_periph *, union ccb *);
114static void cam_rescan(struct cam_sim *);
115static void free_hcb_and_ccb_done(struct atapi_hcb *, u_int32_t);
116static struct atapi_hcb *allocate_hcb(struct atapi_xpt_softc *, int, int, union ccb *);
117static void free_hcb(struct atapi_hcb *hcb);
118static void free_softc(struct atapi_xpt_softc *scp);
119
120static MALLOC_DEFINE(M_ATACAM, "ata_cam", "ATA driver CAM-XPT layer");
121
122static device_method_t atapi_cam_methods[] = {
123 DEVMETHOD(device_identify, atapi_cam_identify),
124 DEVMETHOD(device_probe, atapi_cam_probe),
125 DEVMETHOD(device_attach, atapi_cam_attach),
126 DEVMETHOD(device_detach, atapi_cam_detach),
127 DEVMETHOD(ata_reinit, atapi_cam_reinit),
128 {0, 0}
129};
130
131static driver_t atapi_cam_driver = {
132 "atapicam",
133 atapi_cam_methods,
134 sizeof(struct atapi_xpt_softc)
135};
136
137static devclass_t atapi_cam_devclass;
138DRIVER_MODULE(atapicam, ata,
139 atapi_cam_driver,
140 atapi_cam_devclass,
141 atapi_cam_event_handler,
142 /*arg*/NULL);
143MODULE_VERSION(atapicam, 1);
144MODULE_DEPEND(atapicam, ata, 1, 1, 1);
145MODULE_DEPEND(atapicam, cam, 1, 1, 1);
146
147static void
148atapi_cam_identify(device_t *dev, device_t parent)
149{
150 struct atapi_xpt_softc *scp =
a01741bb 151 kmalloc(sizeof(struct atapi_xpt_softc), M_ATACAM, M_INTWAIT|M_ZERO);
c1b3d7c5
TS
152 device_t child;
153
c1b3d7c5
TS
154 /* Assume one atapicam instance per parent channel instance. */
155 child = device_add_child(parent, "atapicam", -1);
156 if (child == NULL) {
157 /* XXX TGEN Use device_printf()? */
e3869ec7 158 kprintf("atapi_cam_identify: out of memory, can't add child");
c1b3d7c5
TS
159 kfree(scp, M_ATACAM);
160 return;
161 }
162 scp->atapi_cam_dev.unit = -1;
163 scp->atapi_cam_dev.dev = child;
164 device_quiet(child);
165 device_set_softc(child, scp);
166}
167
168static int
169atapi_cam_probe(device_t dev)
170{
171 struct ata_device *atadev = device_get_softc (dev);
172
173 KASSERT(atadev != NULL, ("expect valid struct ata_device"));
174 if (atadev->unit < 0) {
175 device_set_desc(dev, "ATAPI CAM Attachment");
176 return(0);
177 } else {
178 return ENXIO;
179 }
180}
181
182static int
183atapi_cam_attach(device_t dev)
184{
185 struct atapi_xpt_softc *scp = NULL;
186 struct cam_devq *devq = NULL;
187 struct cam_sim *sim = NULL;
188 struct cam_path *path = NULL;
189 int unit, error;
190
191 scp = (struct atapi_xpt_softc *)device_get_softc(dev);
192 if (scp == NULL) {
193 device_printf(dev, "Cannot get softc\n");
194 return ENOMEM;
195 }
196
197 spin_init(&scp->state_lock);
198
199 scp->dev = dev;
200 scp->parent = device_get_parent(dev);
201 scp->ata_ch = device_get_softc(scp->parent);
202 TAILQ_INIT(&scp->pending_hcbs);
203 unit = device_get_unit(dev);
204
205 if ((devq = cam_simq_alloc(16)) == NULL) {
206 error = ENOMEM;
207 goto out;
208 }
209
210 if ((sim = cam_sim_alloc(atapi_action, atapi_poll, "ata",
1c8b7a9a 211 (void *)scp, unit, &sim_mplock, 1, 1, devq)) == NULL) {
c1b3d7c5
TS
212 error = ENOMEM;
213 goto out;
214 }
215 scp->sim = sim;
216
217 if (xpt_bus_register(sim, 0) != CAM_SUCCESS) {
218 error = EINVAL;
219 goto out;
220 }
221 scp->flags |= BUS_REGISTERED;
222
223 if (xpt_create_path(&path, /*periph*/ NULL,
224 cam_sim_path(sim), CAM_TARGET_WILDCARD,
225 CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
226 error = ENOMEM;
227 goto out;
228 }
229 scp->path = path;
230
231 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Registered SIM for ata%d\n", unit));
232
233 setup_async_cb(scp, AC_LOST_DEVICE);
dbcd0c9b 234 reinit_bus(scp, ATTACH);
c1b3d7c5
TS
235 error = 0;
236
237out:
238 if (error != 0)
239 free_softc(scp);
240
241 return(error);
242}
243
244static int
245atapi_cam_detach(device_t dev)
246{
247 struct atapi_xpt_softc *scp = device_get_softc(dev);
248
249 get_mplock();
250 xpt_freeze_simq(scp->sim, 1 /*count*/);
251 rel_mplock();
252 spin_lock_wr(&scp->state_lock);
253 scp->flags |= DETACHING;
254 spin_unlock_wr(&scp->state_lock);
255 free_softc(scp);
256 return (0);
257}
258
259static int
260atapi_cam_reinit(device_t dev) {
261 struct atapi_xpt_softc *scp = device_get_softc(dev);
262
263 /*
264 * scp might be null if the bus is being reinitialised during
265 * the boot-up sequence, before the ATAPI bus is registered.
266 */
267
268 if (scp != NULL) {
269 reinit_bus(scp, RESET);
270 }
271 return 0;
272}
273
274static void
275reinit_bus(struct atapi_xpt_softc *scp, enum reinit_reason reason) {
c436b8b4 276 struct ata_device *old_atadev[2], *atadev;
c1b3d7c5 277 device_t *children;
c436b8b4 278 int nchildren, i, dev_changed;
c1b3d7c5
TS
279
280 if (device_get_children(scp->parent, &children, &nchildren) != 0) {
281 return;
282 }
283
284 spin_lock_wr(&scp->state_lock);
c436b8b4
HT
285 old_atadev[0] = scp->atadev[0];
286 old_atadev[1] = scp->atadev[1];
c1b3d7c5
TS
287 scp->atadev[0] = NULL;
288 scp->atadev[1] = NULL;
289
290 for (i = 0; i < nchildren; i++) {
291 /* XXX Does the child need to actually be attached yet? */
292 if (children[i] != NULL) {
293 atadev = device_get_softc(children[i]);
294 if ((atadev->unit == ATA_MASTER) &&
295 (scp->ata_ch->devices & ATA_ATAPI_MASTER) != 0)
296 scp->atadev[0] = atadev;
297 if ((atadev->unit == ATA_SLAVE) &&
298 (scp->ata_ch->devices & ATA_ATAPI_SLAVE) != 0)
299 scp->atadev[1] = atadev;
300 }
301 }
c436b8b4
HT
302 dev_changed = (old_atadev[0] != scp->atadev[0])
303 || (old_atadev[1] != scp->atadev[1]);
c1b3d7c5
TS
304 spin_unlock_wr(&scp->state_lock);
305 kfree(children, M_TEMP);
306
307 switch (reason) {
c1b3d7c5
TS
308 case RESET:
309 xpt_async(AC_BUS_RESET, scp->path, NULL);
c436b8b4
HT
310
311 if (!dev_changed)
312 break;
313
c1b3d7c5
TS
314 /*FALLTHROUGH*/
315 case ATTACH:
316 cam_rescan(scp->sim);
317 break;
318 }
319}
320
321static void
322setup_async_cb(struct atapi_xpt_softc *scp, uint32_t events)
323{
324 struct ccb_setasync csa;
325
326 get_mplock();
327 xpt_setup_ccb(&csa.ccb_h, scp->path, /*priority*/ 5);
328 csa.ccb_h.func_code = XPT_SASYNC_CB;
329 csa.event_enable = events;
330 csa.callback = &atapi_async;
331 csa.callback_arg = scp->sim;
332 xpt_action((union ccb *) &csa);
333 rel_mplock();
334}
335
336static void
337atapi_action(struct cam_sim *sim, union ccb *ccb)
338{
339 struct atapi_xpt_softc *softc = (struct atapi_xpt_softc*)cam_sim_softc(sim);
340 struct ccb_hdr *ccb_h = &ccb->ccb_h;
341 struct atapi_hcb *hcb = NULL;
342 struct ata_request *request = NULL;
343 int unit = cam_sim_unit(sim);
344 int bus = cam_sim_bus(sim);
345 int len;
346 char *buf;
347
348 switch (ccb_h->func_code) {
349 case XPT_PATH_INQ: {
350 struct ccb_pathinq *cpi = &ccb->cpi;
351 int tid = ccb_h->target_id;
352
353 cpi->version_num = 1;
354 cpi->hba_inquiry = 0;
355 cpi->target_sprt = 0;
356 cpi->hba_misc = PIM_NO_6_BYTE;
357 cpi->hba_eng_cnt = 0;
358 bzero(cpi->vuhba_flags, sizeof(cpi->vuhba_flags));
359 cpi->max_target = 1;
360 cpi->max_lun = 0;
361 cpi->async_flags = 0;
362 cpi->hpath_id = 0;
363 cpi->initiator_id = 7;
364 strncpy(cpi->sim_vid, "FreeBSD", sizeof(cpi->sim_vid));
365 strncpy(cpi->hba_vid, "ATAPI", sizeof(cpi->hba_vid));
366 strncpy(cpi->dev_name, cam_sim_name(sim), sizeof cpi->dev_name);
367 cpi->unit_number = cam_sim_unit(sim);
368 cpi->bus_id = cam_sim_bus(sim);
369 cpi->base_transfer_speed = 3300;
f19fcfb0
PA
370 cpi->transport = XPORT_ATA;
371 cpi->transport_version = 2;
372 cpi->protocol = PROTO_SCSI;
373 cpi->protocol_version = SCSI_REV_2;
c1b3d7c5
TS
374
375 if (softc->ata_ch && tid != CAM_TARGET_WILDCARD) {
376 spin_lock_wr(&softc->state_lock);
377 if (softc->atadev[tid] == NULL) {
378 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
379 xpt_done(ccb);
380 spin_unlock_wr(&softc->state_lock);
381 return;
382 }
383 switch (softc->atadev[ccb_h->target_id]->mode) {
384 case ATA_PIO1:
385 cpi->base_transfer_speed = 5200;
386 break;
387 case ATA_PIO2:
388 cpi->base_transfer_speed = 7000;
389 break;
390 case ATA_PIO3:
391 cpi->base_transfer_speed = 11000;
392 break;
393 case ATA_PIO4:
394 case ATA_DMA:
395 case ATA_WDMA2:
396 cpi->base_transfer_speed = 16000;
397 break;
398 case ATA_UDMA2:
399 cpi->base_transfer_speed = 33000;
400 break;
401 case ATA_UDMA4:
402 cpi->base_transfer_speed = 66000;
403 break;
404 case ATA_UDMA5:
405 cpi->base_transfer_speed = 100000;
406 break;
407 case ATA_UDMA6:
408 cpi->base_transfer_speed = 133000;
409 break;
410 default:
411 break;
412 }
413 spin_unlock_wr(&softc->state_lock);
414 }
415 ccb->ccb_h.status = CAM_REQ_CMP;
416 xpt_done(ccb);
417 return;
418 }
419
420 case XPT_RESET_DEV: {
421 int tid = ccb_h->target_id;
422
423 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("dev reset\n"));
424 ata_controlcmd(softc->atadev[tid]->dev, ATA_DEVICE_RESET, 0, 0, 0);
425 ccb->ccb_h.status = CAM_REQ_CMP;
426 xpt_done(ccb);
427 return;
428 }
429
430 case XPT_RESET_BUS:
431 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("bus reset\n"));
432 ata_reinit(softc->parent);
433 ccb->ccb_h.status = CAM_REQ_CMP;
434 xpt_done(ccb);
435 return;
436
437 case XPT_SET_TRAN_SETTINGS:
438 /* ignore these, we're not doing SCSI here */
439 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE,
440 ("SET_TRAN_SETTINGS not supported\n"));
441 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
442 xpt_done(ccb);
443 return;
444
445 case XPT_GET_TRAN_SETTINGS: {
446 struct ccb_trans_settings *cts = &ccb->cts;
f19fcfb0
PA
447 cts->protocol = PROTO_SCSI;
448 cts->protocol_version = SCSI_REV_2;
449 cts->transport = XPORT_ATA;
450 cts->transport_version = XPORT_VERSION_UNSPECIFIED;
451 cts->proto_specific.valid = 0;
452 cts->xport_specific.valid = 0;
453 /* nothing more to do */
c1b3d7c5 454 ccb->ccb_h.status = CAM_REQ_CMP;
f19fcfb0 455 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("GET_TRAN_SETTINGS\n"));
c1b3d7c5
TS
456 xpt_done(ccb);
457 return;
458 }
459
460 case XPT_CALC_GEOMETRY: {
461 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_SUBTRACE, ("CALC_GEOMETRY\n"));
462 cam_calc_geometry(&ccb->ccg, /*extended*/1);
463 xpt_done(ccb);
464 return;
465 }
466
467 case XPT_SCSI_IO: {
468 struct ccb_scsiio *csio = &ccb->csio;
469 int tid = ccb_h->target_id, lid = ccb_h->target_lun;
c436b8b4 470 int request_flags = ATA_R_ATAPI;
c1b3d7c5
TS
471
472 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE, ("XPT_SCSI_IO\n"));
473
474 spin_lock_wr(&softc->state_lock);
475 if (softc->flags & DETACHING) {
476 ccb->ccb_h.status = CAM_REQ_ABORTED;
477 xpt_done(ccb);
478 spin_unlock_wr(&softc->state_lock);
479 return;
480 }
481
482 if (softc->atadev[tid] == NULL) {
483 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
484 xpt_done(ccb);
485 spin_unlock_wr(&softc->state_lock);
486 return;
487 }
488
489 /* check that this request was not aborted already */
490 if ((ccb_h->status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
e3869ec7 491 kprintf("XPT_SCSI_IO received but already in progress?\n");
c1b3d7c5
TS
492 xpt_done(ccb);
493 spin_unlock_wr(&softc->state_lock);
494 return;
495 }
496 if (lid > 0) {
497 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
498 ("SCSI IO received for invalid lun %d\n", lid));
499 goto action_invalid;
500 }
501 if (csio->cdb_len > sizeof request->u.atapi.ccb) {
502 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
503 ("CAM CCB too long for ATAPI"));
504 goto action_invalid;
505 }
506 if ((ccb_h->flags & CAM_SCATTER_VALID)) {
507 /* scatter-gather not supported */
508 xpt_print_path(ccb_h->path);
e3869ec7 509 kprintf("ATAPI/CAM does not support scatter-gather yet!\n");
c1b3d7c5
TS
510 goto action_invalid;
511 }
512
513 switch (ccb_h->flags & CAM_DIR_MASK) {
514 case CAM_DIR_IN:
c436b8b4 515 request_flags |= ATA_R_READ;
c1b3d7c5
TS
516 break;
517 case CAM_DIR_OUT:
c436b8b4 518 request_flags |= ATA_R_WRITE;
c1b3d7c5
TS
519 break;
520 case CAM_DIR_NONE:
521 /* No flags need to be set */
522 break;
523 default:
524 device_printf(softc->dev, "unknown IO operation\n");
525 goto action_invalid;
526 }
c1b3d7c5
TS
527
528 if ((hcb = allocate_hcb(softc, unit, bus, ccb)) == NULL) {
e3869ec7 529 kprintf("cannot allocate ATAPI/CAM hcb\n");
c1b3d7c5
TS
530 goto action_oom;
531 }
532 if ((request = ata_alloc_request()) == NULL) {
e3869ec7 533 kprintf("cannot allocate ATAPI/CAM request\n");
c1b3d7c5
TS
534 goto action_oom;
535 }
536
537 bcopy((ccb_h->flags & CAM_CDB_POINTER) ?
538 csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes,
539 request->u.atapi.ccb, csio->cdb_len);
540#ifdef CAMDEBUG
541 if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_CDB)) {
542 char cdb_str[(SCSI_MAX_CDBLEN * 3) + 1];
543
e3869ec7 544 kprintf("atapi_action: hcb@%p: %s\n", hcb,
c1b3d7c5
TS
545 scsi_cdb_string(request->u.atapi.ccb, cdb_str, sizeof(cdb_str)));
546 }
c436b8b4
HT
547 if (CAM_DEBUGGED(ccb_h->path, CAM_DEBUG_SUBTRACE)) {
548 request_flags |= ATA_R_DEBUG;
549 }
c1b3d7c5
TS
550#endif
551
552 len = csio->dxfer_len;
553 buf = csio->data_ptr;
554
555 /* some SCSI commands require special processing */
556 switch (request->u.atapi.ccb[0]) {
557 case INQUIRY: {
558 /*
559 * many ATAPI devices seem to report more than
560 * SHORT_INQUIRY_LENGTH bytes of available INQUIRY
561 * information, but respond with some incorrect condition
562 * when actually asked for it, so we are going to pretend
563 * that only SHORT_INQUIRY_LENGTH are expected, anyway.
564 */
565 struct scsi_inquiry *inq = (struct scsi_inquiry *) &request->u.atapi.ccb[0];
566
567 if (inq->byte2 == 0 && inq->page_code == 0 &&
568 inq->length > SHORT_INQUIRY_LENGTH) {
569 bzero(buf, len);
570 len = inq->length = SHORT_INQUIRY_LENGTH;
571 }
572 break;
573 }
574 case READ_6:
575 /* FALLTHROUGH */
576
577 case WRITE_6:
578 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
579 ("Translating %s into _10 equivalent\n",
580 (request->u.atapi.ccb[0] == READ_6) ? "READ_6" : "WRITE_6"));
581 request->u.atapi.ccb[0] |= 0x20;
582 request->u.atapi.ccb[9] = request->u.atapi.ccb[5];
583 request->u.atapi.ccb[8] = request->u.atapi.ccb[4];
584 request->u.atapi.ccb[7] = 0;
585 request->u.atapi.ccb[6] = 0;
586 request->u.atapi.ccb[5] = request->u.atapi.ccb[3];
587 request->u.atapi.ccb[4] = request->u.atapi.ccb[2];
588 request->u.atapi.ccb[3] = request->u.atapi.ccb[1] & 0x1f;
589 request->u.atapi.ccb[2] = 0;
590 request->u.atapi.ccb[1] = 0;
c436b8b4
HT
591 /* FALLTHROUGH */
592
593 case READ_10:
594 /* FALLTHROUGH */
595 case WRITE_10:
596 /* FALLTHROUGH */
597 case READ_12:
598 /* FALLTHROUGH */
599 case WRITE_12:
600 /*
601 * Enable DMA (if target supports it) for READ and WRITE commands
602 * only, as some combinations of drive, controller and chipset do
603 * not behave correctly when DMA is enabled for other commands.
604 */
605 if (softc->atadev[tid]->mode >= ATA_DMA)
606 request_flags |= ATA_R_DMA;
c1b3d7c5
TS
607 break;
608 }
609
610 if ((ccb_h->flags & CAM_DIR_MASK) == CAM_DIR_IN && (len & 1)) {
611 /* ATA always transfers an even number of bytes */
a01741bb
MD
612 buf = hcb->dxfer_alloc =
613 kmalloc(++len, M_ATACAM, M_INTWAIT | M_ZERO);
c1b3d7c5
TS
614 }
615 request->dev = softc->atadev[tid]->dev;
616 request->driver = hcb;
617 request->data = buf;
618 request->bytecount = len;
405f453f
MD
619 request->transfersize = min(request->bytecount,
620 min(softc->ata_ch->dma->max_iosize, 65534));
c1b3d7c5 621 request->timeout = ccb_h->timeout / 1000; /* XXX lost granularity */
c1b3d7c5
TS
622 request->callback = &atapi_cb;
623 request->flags = request_flags;
624
c436b8b4
HT
625 /*
626 * no retries are to be performed at the ATA level; any retries
627 * will be done by CAM .
628 */
629 request->retries = 0;
630
c1b3d7c5
TS
631 TAILQ_INSERT_TAIL(&softc->pending_hcbs, hcb, chain);
632 hcb->flags |= QUEUED;
633 ccb_h->status |= CAM_SIM_QUEUED;
634 spin_unlock_wr(&softc->state_lock);
635
636 ata_queue_request(request);
637 return;
638 }
639
640 default:
641 CAM_DEBUG(ccb_h->path, CAM_DEBUG_SUBTRACE,
642 ("unsupported function code 0x%02x\n", ccb_h->func_code));
643 goto action_invalid;
644 }
645
646 /* NOTREACHED */
647
648action_oom:
649 if (request != NULL)
650 ata_free_request(request);
651 if (hcb != NULL)
652 free_hcb(hcb);
653 spin_unlock_wr(&softc->state_lock);
654 get_mplock();
655 xpt_print_path(ccb_h->path);
e3869ec7 656 kprintf("out of memory, freezing queue.\n");
c1b3d7c5
TS
657 softc->flags |= RESOURCE_SHORTAGE;
658 xpt_freeze_simq(sim, /*count*/ 1);
659 rel_mplock();
660 ccb_h->status = CAM_REQUEUE_REQ;
661 xpt_done(ccb);
662 return;
663
664action_invalid:
665 spin_unlock_wr(&softc->state_lock);
666 ccb_h->status = CAM_REQ_INVALID;
667 xpt_done(ccb);
668 return;
669}
670
671static void
672atapi_poll(struct cam_sim *sim)
673{
674 /* do nothing - we do not actually service any interrupts */
e3869ec7 675 kprintf("atapi_poll called!\n");
c1b3d7c5
TS
676}
677
678static void
679atapi_cb(struct ata_request *request)
680{
681 struct atapi_xpt_softc *scp;
682 struct atapi_hcb *hcb;
683 struct ccb_scsiio *csio;
684 u_int32_t rc;
685
686 hcb = (struct atapi_hcb *)request->driver;
687 scp = hcb->softc;
688 csio = &hcb->ccb->csio;
689
690#ifdef CAMDEBUG
691# define err (request->u.atapi.sense.key)
692 if (CAM_DEBUGGED(csio->ccb_h.path, CAM_DEBUG_CDB)) {
c436b8b4
HT
693 kprintf("atapi_cb: hcb@%p sense = %02x: sk = %01x%s%s%s\n",
694 hcb, err, err & 0x0f,
695 (err & 0x80) ? ", Filemark" : "",
696 (err & 0x40) ? ", EOM" : "",
697 (err & 0x20) ? ", ILI" : "");
698 device_printf(request->dev,
699 "cmd %s status %02x result %02x error %02x\n",
700 ata_cmd2str(request),
701 request->status, request->result, request->error);
c1b3d7c5
TS
702 }
703#endif
704
705 if ((hcb->flags & AUTOSENSE) != 0) {
706 rc = CAM_SCSI_STATUS_ERROR;
707 if (request->result == 0) {
708 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
709 }
710 } else if (request->result != 0) {
c436b8b4
HT
711 if ((request->flags & ATA_R_TIMEOUT) != 0) {
712 rc = CAM_CMD_TIMEOUT;
713 } else {
714 rc = CAM_SCSI_STATUS_ERROR;
715 csio->scsi_status = SCSI_STATUS_CHECK_COND;
c1b3d7c5 716
c436b8b4 717 if ((csio->ccb_h.flags & CAM_DIS_AUTOSENSE) == 0) {
c1b3d7c5 718#if 0
c436b8b4
HT
719 static const int8_t ccb[16] = { ATAPI_REQUEST_SENSE, 0, 0, 0,
720 sizeof(struct atapi_sense), 0, 0, 0, 0, 0, 0,
721 0, 0, 0, 0, 0 };
722
723 bcopy (ccb, request->u.atapi.ccb, sizeof ccb);
724 request->data = (caddr_t)&csio->sense_data;
725 request->bytecount = sizeof(struct atapi_sense);
726 request->transfersize = min(request->bytecount, 65534);
727 request->timeout = csio->ccb_h.timeout / 1000;
728 request->retries = 2;
729 request->flags = ATA_R_QUIET|ATA_R_ATAPI|ATA_R_IMMEDIATE;
730 hcb->flags |= AUTOSENSE;
731
732 ata_queue_request(request);
733 return;
c1b3d7c5 734#else
c436b8b4
HT
735 /*
736 * Use auto-sense data from the ATA layer, if it has
737 * issued a REQUEST SENSE automatically and that operation
738 * returned without error.
739 */
740 if (request->u.atapi.sense.key != 0 && request->error == 0) {
741 bcopy (&request->u.atapi.sense, &csio->sense_data, sizeof(struct atapi_sense));
742 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
743 }
c1b3d7c5
TS
744 }
745#endif
746 }
747 } else {
748 rc = CAM_REQ_CMP;
749 csio->scsi_status = SCSI_STATUS_OK;
750 if (((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) &&
751 hcb->dxfer_alloc != NULL)
752 {
753 bcopy(hcb->dxfer_alloc, csio->data_ptr, csio->dxfer_len);
754 }
755 }
756
757 spin_lock_wr(&scp->state_lock);
758 free_hcb_and_ccb_done(hcb, rc);
759 spin_unlock_wr(&scp->state_lock);
760
761 ata_free_request(request);
762}
763
764static void
765free_hcb_and_ccb_done(struct atapi_hcb *hcb, u_int32_t status)
766{
767 struct atapi_xpt_softc *softc;
768 union ccb *ccb;
769
770 if (hcb == NULL)
771 return;
772
773 softc = hcb->softc;
774 ccb = hcb->ccb;
775
776 /* we're about to free a hcb, so the shortage has ended */
777 if (softc->flags & RESOURCE_SHORTAGE) {
778 softc->flags &= ~RESOURCE_SHORTAGE;
779 status |= CAM_RELEASE_SIMQ;
780 }
781 free_hcb(hcb);
782 ccb->ccb_h.status =
783 status | (ccb->ccb_h.status & ~(CAM_STATUS_MASK | CAM_SIM_QUEUED));
784 xpt_done(ccb);
785}
786
787static void
788atapi_async(void *callback_arg, u_int32_t code,
789 struct cam_path* path, void *arg)
790{
791 struct atapi_xpt_softc *softc;
792 struct cam_sim *sim;
793 int targ;
794
795 crit_enter();
796
797 sim = (struct cam_sim *) callback_arg;
798 softc = (struct atapi_xpt_softc *) cam_sim_softc(sim);
799 switch (code) {
800 case AC_LOST_DEVICE:
801 targ = xpt_path_target_id(path);
802 xpt_print_path(path);
803 if (targ == -1)
e3869ec7 804 kprintf("Lost host adapter\n");
c1b3d7c5 805 else
e3869ec7 806 kprintf("Lost target %d???\n", targ);
c1b3d7c5
TS
807 break;
808
809 default:
810 break;
811 }
812
813 crit_exit();
814}
815
816static void
817cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
818{
819 if (ccb->ccb_h.status != CAM_REQ_CMP) {
820 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
821 ("Rescan failed, 0x%04x\n", ccb->ccb_h.status));
822 } else {
823 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE,
824 ("Rescan succeeded\n"));
825 }
826 xpt_free_path(ccb->ccb_h.path);
827 kfree(ccb, M_ATACAM);
828}
829
830static void
831cam_rescan(struct cam_sim *sim)
832{
833 struct cam_path *path;
834 union ccb *ccb = kmalloc(sizeof(union ccb), M_ATACAM, M_WAITOK | M_ZERO);
835
836 get_mplock();
837 if (xpt_create_path(&path, xpt_periph, cam_sim_path(sim),
838 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
839 rel_mplock();
840 kfree(ccb, M_ATACAM);
841 return;
842 }
843
c436b8b4 844 CAM_DEBUG(path, CAM_DEBUG_TRACE, ("Rescanning ATAPI bus.\n"));
c1b3d7c5
TS
845 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
846 ccb->ccb_h.func_code = XPT_SCAN_BUS;
847 ccb->ccb_h.cbfcnp = cam_rescan_callback;
848 ccb->crcn.flags = CAM_FLAG_NONE;
849 xpt_action(ccb);
850 /* scan is in progress now */
851 rel_mplock();
852}
853
854static struct atapi_hcb *
855allocate_hcb(struct atapi_xpt_softc *softc, int unit, int bus, union ccb *ccb)
856{
857 struct atapi_hcb *hcb = (struct atapi_hcb *)
a01741bb 858 kmalloc(sizeof(struct atapi_hcb), M_ATACAM, M_INTWAIT | M_ZERO);
c1b3d7c5 859
a01741bb
MD
860 hcb->softc = softc;
861 hcb->unit = unit;
862 hcb->bus = bus;
863 hcb->ccb = ccb;
c1b3d7c5
TS
864 return hcb;
865}
866
867static void
868free_hcb(struct atapi_hcb *hcb)
869{
870 if ((hcb->flags & QUEUED) != 0)
871 TAILQ_REMOVE(&hcb->softc->pending_hcbs, hcb, chain);
872 if (hcb->dxfer_alloc != NULL)
873 kfree(hcb->dxfer_alloc, M_ATACAM);
874 kfree(hcb, M_ATACAM);
875}
876
877static void
878free_softc(struct atapi_xpt_softc *scp)
879{
880 struct atapi_hcb *hcb;
881
882 if (scp != NULL) {
883 spin_lock_wr(&scp->state_lock);
884 TAILQ_FOREACH(hcb, &scp->pending_hcbs, chain) {
885 free_hcb_and_ccb_done(hcb, CAM_UNREC_HBA_ERROR);
886 }
887 spin_unlock_wr(&scp->state_lock);
888 get_mplock();
889 if (scp->path != NULL) {
890 setup_async_cb(scp, 0);
891 xpt_free_path(scp->path);
892 }
893 if ((scp->flags & BUS_REGISTERED) != 0) {
894 if (xpt_bus_deregister(cam_sim_path(scp->sim)) == CAM_REQ_CMP)
895 scp->flags &= ~BUS_REGISTERED;
896 }
897 if (scp->sim != NULL) {
898 if ((scp->flags & BUS_REGISTERED) == 0)
d6aaf612 899 cam_sim_free(scp->sim);
c1b3d7c5 900 else
e3869ec7 901 kprintf("Can't free %s SIM (still registered)\n",
c1b3d7c5
TS
902 cam_sim_name(scp->sim));
903 }
904 rel_mplock();
905 spin_uninit(&scp->state_lock);
906 }
907}
908
909static int
910atapi_cam_event_handler(module_t mod, int what, void *arg) {
911 device_t *devlist;
912 int devcount;
913
914 switch (what) {
915 case MOD_UNLOAD:
916 if (devclass_get_devices(atapi_cam_devclass, &devlist, &devcount)
917 != 0)
918 return ENXIO;
919 if (devlist != NULL) {
920 while (devlist != NULL && devcount > 0) {
921 device_t child = devlist[--devcount];
922 struct atapi_xpt_softc *scp = device_get_softc(child);
923
924 device_delete_child(device_get_parent(child),child);
925 if (scp != NULL)
926 kfree(scp, M_ATACAM);
927 }
928 kfree(devlist, M_TEMP);
929 }
930 break;
931
932 default:
933 break;
934 }
935 return 0;
936}