2 * Copyright (c) 2002 Adaptec, Inc.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
31 * CAM front-end for communicating with non-DASD devices
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/sysctl.h>
41 #include <sys/malloc.h>
42 #include <sys/module.h>
43 #include <sys/mutex.h>
46 #include <cam/cam_ccb.h>
47 #include <cam/cam_debug.h>
48 #include <cam/cam_periph.h>
49 #include <cam/cam_sim.h>
50 #include <cam/cam_xpt_sim.h>
51 #include <cam/scsi/scsi_all.h>
52 #include <cam/scsi/scsi_message.h>
58 #include <machine/md_var.h>
59 #include <machine/bus.h>
65 #include <dev/aac/aacreg.h>
66 #include <sys/aac_ioctl.h>
67 #include <dev/aac/aacvar.h>
73 struct cam_path *path;
76 static int aac_cam_probe(device_t dev);
77 static int aac_cam_attach(device_t dev);
78 static int aac_cam_detach(device_t dev);
79 static void aac_cam_action(struct cam_sim *, union ccb *);
80 static void aac_cam_poll(struct cam_sim *);
81 static void aac_cam_complete(struct aac_command *);
82 static void aac_cam_rescan(struct aac_softc *sc, uint32_t channel,
85 static u_int32_t aac_cam_reset_bus(struct cam_sim *, union ccb *);
86 static u_int32_t aac_cam_abort_ccb(struct cam_sim *, union ccb *);
87 static u_int32_t aac_cam_term_io(struct cam_sim *, union ccb *);
89 static devclass_t aac_pass_devclass;
91 static device_method_t aac_pass_methods[] = {
92 DEVMETHOD(device_probe, aac_cam_probe),
93 DEVMETHOD(device_attach, aac_cam_attach),
94 DEVMETHOD(device_detach, aac_cam_detach),
98 static driver_t aac_pass_driver = {
101 sizeof(struct aac_cam)
104 DRIVER_MODULE(aacp, aac, aac_pass_driver, aac_pass_devclass, 0, 0);
105 MODULE_DEPEND(aacp, cam, 1, 1, 1);
107 static MALLOC_DEFINE(M_AACCAM, "aaccam", "AAC CAM info");
110 aac_cam_rescan(struct aac_softc *sc, uint32_t channel, uint32_t target_id)
114 struct aac_cam *camsc;
116 if (target_id == AAC_CAM_TARGET_WILDCARD)
117 target_id = CAM_TARGET_WILDCARD;
119 TAILQ_FOREACH(sim, &sc->aac_sim_tqh, sim_link) {
120 camsc = sim->aac_cam;
121 if (camsc == NULL || camsc->inf == NULL ||
122 camsc->inf->BusNumber != channel)
125 ccb = xpt_alloc_ccb_nowait();
127 device_printf(sc->aac_dev,
128 "Cannot allocate ccb for bus rescan.\n");
132 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
133 cam_sim_path(camsc->sim),
134 target_id, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
136 device_printf(sc->aac_dev,
137 "Cannot create path for bus rescan.\n");
147 aac_cam_event(struct aac_softc *sc, struct aac_event *event, void *arg)
150 struct aac_cam *camsc;
152 switch (event->ev_type) {
153 case AAC_EVENT_CMFREE:
155 camsc = ccb->ccb_h.sim_priv.entries[0].ptr;
156 free(event, M_AACCAM);
157 xpt_release_simq(camsc->sim, 1);
158 ccb->ccb_h.status = CAM_REQUEUE_REQ;
162 device_printf(sc->aac_dev, "unknown event %d in aac_cam\n",
171 aac_cam_probe(device_t dev)
173 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
179 aac_cam_detach(device_t dev)
181 struct aac_softc *sc;
182 struct aac_cam *camsc;
183 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
185 camsc = (struct aac_cam *)device_get_softc(dev);
186 sc = camsc->inf->aac_sc;
187 camsc->inf->aac_cam = NULL;
189 mtx_lock(&sc->aac_io_lock);
191 xpt_async(AC_LOST_DEVICE, camsc->path, NULL);
192 xpt_free_path(camsc->path);
193 xpt_bus_deregister(cam_sim_path(camsc->sim));
194 cam_sim_free(camsc->sim, /*free_devq*/TRUE);
196 sc->cam_rescan_cb = NULL;
198 mtx_unlock(&sc->aac_io_lock);
204 * Register the driver as a CAM SIM
207 aac_cam_attach(device_t dev)
209 struct cam_devq *devq;
211 struct cam_path *path;
212 struct aac_cam *camsc;
215 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
217 camsc = (struct aac_cam *)device_get_softc(dev);
218 inf = (struct aac_sim *)device_get_ivars(dev);
220 camsc->inf->aac_cam = camsc;
222 devq = cam_simq_alloc(inf->TargetsPerBus);
226 sim = cam_sim_alloc(aac_cam_action, aac_cam_poll, "aacp", camsc,
227 device_get_unit(dev), &inf->aac_sc->aac_io_lock, 1, 1, devq);
233 /* Since every bus has it's own sim, every bus 'appears' as bus 0 */
234 mtx_lock(&inf->aac_sc->aac_io_lock);
235 if (xpt_bus_register(sim, dev, 0) != CAM_SUCCESS) {
236 cam_sim_free(sim, TRUE);
237 mtx_unlock(&inf->aac_sc->aac_io_lock);
241 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
242 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
243 xpt_bus_deregister(cam_sim_path(sim));
244 cam_sim_free(sim, TRUE);
245 mtx_unlock(&inf->aac_sc->aac_io_lock);
248 inf->aac_sc->cam_rescan_cb = aac_cam_rescan;
249 mtx_unlock(&inf->aac_sc->aac_io_lock);
258 aac_cam_action(struct cam_sim *sim, union ccb *ccb)
260 struct aac_cam *camsc;
261 struct aac_softc *sc;
264 struct aac_command *cm;
266 camsc = (struct aac_cam *)cam_sim_softc(sim);
267 sc = camsc->inf->aac_sc;
268 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
270 /* Synchronous ops, and ops that don't require communication with the
272 switch(ccb->ccb_h.func_code) {
275 /* These are handled down below */
277 case XPT_CALC_GEOMETRY:
279 struct ccb_calc_geometry *ccg;
281 u_int32_t secs_per_cylinder;
284 size_mb = ccg->volume_size /
285 ((1024L * 1024L) / ccg->block_size);
286 if (size_mb >= (2 * 1024)) { /* 2GB */
288 ccg->secs_per_track = 63;
289 } else if (size_mb >= (1 * 1024)) { /* 1GB */
291 ccg->secs_per_track = 32;
294 ccg->secs_per_track = 32;
296 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
297 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
299 ccb->ccb_h.status = CAM_REQ_CMP;
305 struct ccb_pathinq *cpi = &ccb->cpi;
307 cpi->version_num = 1;
308 cpi->hba_inquiry = PI_WIDE_16;
309 cpi->target_sprt = 0;
312 * Resetting via the passthrough or parallel bus scan
315 cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN;
316 cpi->hba_eng_cnt = 0;
317 cpi->max_target = camsc->inf->TargetsPerBus;
318 cpi->max_lun = 8; /* Per the controller spec */
319 cpi->initiator_id = camsc->inf->InitiatorBusId;
320 cpi->bus_id = camsc->inf->BusNumber;
321 cpi->base_transfer_speed = 3300;
322 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
323 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
324 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
325 cpi->unit_number = cam_sim_unit(sim);
326 cpi->transport = XPORT_SPI;
327 cpi->transport_version = 2;
328 cpi->protocol = PROTO_SCSI;
329 cpi->protocol_version = SCSI_REV_2;
330 ccb->ccb_h.status = CAM_REQ_CMP;
334 case XPT_GET_TRAN_SETTINGS:
336 struct ccb_trans_settings_scsi *scsi =
337 &ccb->cts.proto_specific.scsi;
338 struct ccb_trans_settings_spi *spi =
339 &ccb->cts.xport_specific.spi;
340 ccb->cts.protocol = PROTO_SCSI;
341 ccb->cts.protocol_version = SCSI_REV_2;
342 ccb->cts.transport = XPORT_SPI;
343 ccb->cts.transport_version = 2;
344 if (ccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
345 scsi->valid = CTS_SCSI_VALID_TQ;
346 spi->valid |= CTS_SPI_VALID_DISC;
350 ccb->ccb_h.status = CAM_REQ_CMP;
354 case XPT_SET_TRAN_SETTINGS:
355 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
359 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
360 ccb->ccb_h.status = aac_cam_reset_bus(sim, ccb);
362 ccb->ccb_h.status = CAM_REQ_CMP;
367 ccb->ccb_h.status = aac_cam_abort_ccb(sim, ccb);
371 ccb->ccb_h.status = aac_cam_term_io(sim, ccb);
375 device_printf(sc->aac_dev, "Unsupported command 0x%x\n",
376 ccb->ccb_h.func_code);
377 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
382 /* Async ops that require communcation with the controller */
384 if (aac_alloc_command(sc, &cm)) {
385 struct aac_event *event;
387 xpt_freeze_simq(sim, 1);
388 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
389 ccb->ccb_h.sim_priv.entries[0].ptr = camsc;
390 event = malloc(sizeof(struct aac_event), M_AACCAM,
393 device_printf(sc->aac_dev,
394 "Warning, out of memory for event\n");
397 event->ev_callback = aac_cam_event;
399 event->ev_type = AAC_EVENT_CMFREE;
400 aac_add_event(sc, event);
405 srb = (struct aac_srb *)&fib->data[0];
408 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
410 srb->flags = AAC_SRB_FLAGS_DATA_IN;
411 cm->cm_flags |= AAC_CMD_DATAIN;
414 srb->flags = AAC_SRB_FLAGS_DATA_OUT;
415 cm->cm_flags |= AAC_CMD_DATAOUT;
418 srb->flags = AAC_SRB_FLAGS_NO_DATA_XFER;
421 srb->flags = AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION;
422 cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT;
426 switch(ccb->ccb_h.func_code) {
429 struct ccb_scsiio *csio = &ccb->csio;
431 srb->function = AAC_SRB_FUNC_EXECUTE_SCSI;
434 * Copy the CDB into the SRB. It's only 6-16 bytes,
435 * so a copy is not too expensive.
437 srb->cdb_len = csio->cdb_len;
438 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
439 bcopy(csio->cdb_io.cdb_ptr, (u_int8_t *)&srb->cdb[0],
442 bcopy(csio->cdb_io.cdb_bytes, (u_int8_t *)&srb->cdb[0],
446 fib->Header.Command = (sc->flags & AAC_FLAGS_SG_64BIT) ?
447 ScsiPortCommandU64 : ScsiPortCommand;
449 /* Map the s/g list. XXX 32bit addresses only! */
450 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
451 if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
452 srb->data_len = csio->dxfer_len;
453 if (ccb->ccb_h.flags & CAM_DATA_PHYS) {
454 /* Send a 32bit command */
455 fib->Header.Command = ScsiPortCommand;
456 srb->sg_map.SgCount = 1;
457 srb->sg_map.SgEntry[0].SgAddress =
458 (uint32_t)(uintptr_t)csio->data_ptr;
459 srb->sg_map.SgEntry[0].SgByteCount =
463 * Arrange things so that the S/G
464 * map will get set up automagically
466 cm->cm_data = (void *)csio->data_ptr;
467 cm->cm_datalen = csio->dxfer_len;
468 cm->cm_sgtable = &srb->sg_map;
471 /* XXX Need to handle multiple s/g elements */
472 panic("aac_cam: multiple s/g elements");
475 srb->sg_map.SgCount = 0;
476 srb->sg_map.SgEntry[0].SgByteCount = 0;
483 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
484 srb->function = AAC_SRB_FUNC_RESET_DEVICE;
487 ccb->ccb_h.status = CAM_REQ_CMP;
495 srb->bus = camsc->inf->BusNumber; /* Bus number relative to the card */
496 srb->target = ccb->ccb_h.target_id;
497 srb->lun = ccb->ccb_h.target_lun;
498 srb->timeout = ccb->ccb_h.timeout; /* XXX */
499 srb->retry_limit = 0;
501 cm->cm_complete = aac_cam_complete;
502 cm->cm_private = ccb;
503 cm->cm_timestamp = time_uptime;
505 fib->Header.XferState =
506 AAC_FIBSTATE_HOSTOWNED |
507 AAC_FIBSTATE_INITIALISED |
508 AAC_FIBSTATE_FROMHOST |
509 AAC_FIBSTATE_REXPECTED |
511 fib->Header.Size = sizeof(struct aac_fib_header) +
512 sizeof(struct aac_srb);
514 aac_enqueue_ready(cm);
515 aac_startio(cm->cm_sc);
521 aac_cam_poll(struct cam_sim *sim)
524 * Pinging the interrupt routine isn't very safe, nor is it
525 * really necessary. Do nothing.
530 aac_cam_complete(struct aac_command *cm)
533 struct aac_srb_response *srbr;
534 struct aac_softc *sc;
538 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
539 ccb = cm->cm_private;
540 srbr = (struct aac_srb_response *)&cm->cm_fib->data[0];
542 if (srbr->fib_status != 0) {
543 device_printf(sc->aac_dev, "Passthru FIB failed!\n");
544 ccb->ccb_h.status = CAM_REQ_ABORTED;
547 * The SRB error codes just happen to match the CAM error
548 * codes. How convienient!
550 ccb->ccb_h.status = srbr->srb_status;
552 /* Take care of SCSI_IO ops. */
553 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
554 u_int8_t command, device;
556 ccb->csio.scsi_status = srbr->scsi_status;
558 /* Take care of autosense */
559 if (srbr->sense_len) {
560 sense_returned = srbr->sense_len;
561 if (sense_returned < ccb->csio.sense_len)
562 ccb->csio.sense_resid =
563 ccb->csio.sense_len -
566 ccb->csio.sense_resid = 0;
567 bzero(&ccb->csio.sense_data,
568 sizeof(struct scsi_sense_data));
569 bcopy(&srbr->sense[0], &ccb->csio.sense_data,
570 min(ccb->csio.sense_len, sense_returned));
571 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
572 // scsi_sense_print(&ccb->csio);
575 /* If this is an inquiry command, fake things out */
576 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
577 command = ccb->csio.cdb_io.cdb_ptr[0];
579 command = ccb->csio.cdb_io.cdb_bytes[0];
581 if (command == INQUIRY) {
582 if (ccb->ccb_h.status == CAM_REQ_CMP) {
583 device = ccb->csio.data_ptr[0] & 0x1f;
585 * We want DASD and PROC devices to only be
586 * visible through the pass device.
588 if ((device == T_DIRECT) ||
589 (device == T_PROCESSOR) ||
590 (sc->flags & AAC_FLAGS_CAM_PASSONLY))
591 ccb->csio.data_ptr[0] =
592 ((ccb->csio.data_ptr[0] & 0xe0) |
594 } else if (ccb->ccb_h.status == CAM_SEL_TIMEOUT &&
595 ccb->ccb_h.target_lun != 0) {
596 /* fix for INQUIRYs on Lun>0 */
597 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
603 aac_release_command(cm);
610 aac_cam_reset_bus(struct cam_sim *sim, union ccb *ccb)
613 struct aac_softc *sc;
614 struct aac_cam *camsc;
615 struct aac_vmioctl *vmi;
616 struct aac_resetbus *rbc;
619 camsc = (struct aac_cam *)cam_sim_softc(sim);
620 sc = camsc->inf->aac_sc;
623 printf("aac: Null sc?\n");
624 return (CAM_REQ_ABORTED);
627 aac_alloc_sync_fib(sc, &fib);
629 vmi = (struct aac_vmioctl *)&fib->data[0];
630 bzero(vmi, sizeof(struct aac_vmioctl));
632 vmi->Command = VM_Ioctl;
633 vmi->ObjType = FT_DRIVE;
634 vmi->MethId = sc->scsi_method_id;
636 vmi->IoctlCmd = ResetBus;
638 rbc = (struct aac_resetbus *)&vmi->IoctlBuf[0];
639 rbc->BusNumber = camsc->inf->BusNumber;
641 e = aac_sync_fib(sc, ContainerCommand, 0, fib,
642 sizeof(struct aac_vmioctl));
644 device_printf(sc->aac_dev,"Error %d sending ResetBus command\n",
646 aac_release_sync_fib(sc);
647 return (CAM_REQ_ABORTED);
650 aac_release_sync_fib(sc);
651 return (CAM_REQ_CMP);
655 aac_cam_abort_ccb(struct cam_sim *sim, union ccb *ccb)
657 return (CAM_UA_ABORT);
661 aac_cam_term_io(struct cam_sim *sim, union ccb *ccb)
663 return (CAM_UA_TERMIO);