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
26 * $FreeBSD: head/sys/dev/aac/aac_cam.c 251115 2013-05-30 00:11:22Z marius $
30 * CAM front-end for communicating with non-DASD devices
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
42 #include <bus/cam/cam.h>
43 #include <bus/cam/cam_ccb.h>
44 #include <bus/cam/cam_debug.h>
45 #include <bus/cam/cam_periph.h>
46 #include <bus/cam/cam_sim.h>
47 #include <bus/cam/cam_xpt_periph.h>
48 #include <bus/cam/cam_xpt_sim.h>
49 #include <bus/cam/scsi/scsi_all.h>
50 #include <bus/cam/scsi/scsi_message.h>
56 #include <machine/md_var.h>
62 #include <dev/raid/aac/aacreg.h>
63 #include <dev/raid/aac/aac_ioctl.h>
64 #include <dev/raid/aac/aacvar.h>
70 struct cam_path *path;
73 static int aac_cam_probe(device_t dev);
74 static int aac_cam_attach(device_t dev);
75 static int aac_cam_detach(device_t dev);
76 static void aac_cam_action(struct cam_sim *, union ccb *);
77 static void aac_cam_poll(struct cam_sim *);
78 static void aac_cam_complete(struct aac_command *);
79 static void aac_cam_rescan(struct aac_softc *sc, uint32_t channel,
81 static void aac_bus_scan_cb(struct cam_periph *periph, union ccb *ccb);
83 static u_int32_t aac_cam_reset_bus(struct cam_sim *, union ccb *);
84 static u_int32_t aac_cam_abort_ccb(struct cam_sim *, union ccb *);
85 static u_int32_t aac_cam_term_io(struct cam_sim *, union ccb *);
87 static devclass_t aac_pass_devclass;
89 static device_method_t aac_pass_methods[] = {
90 DEVMETHOD(device_probe, aac_cam_probe),
91 DEVMETHOD(device_attach, aac_cam_attach),
92 DEVMETHOD(device_detach, aac_cam_detach),
96 static driver_t aac_pass_driver = {
99 sizeof(struct aac_cam)
102 DRIVER_MODULE(aacp, aac, aac_pass_driver, aac_pass_devclass, NULL, NULL);
103 MODULE_DEPEND(aacp, cam, 1, 1, 1);
105 static MALLOC_DEFINE(M_AACCAM, "aaccam", "AAC CAM info");
108 aac_cam_rescan(struct aac_softc *sc, uint32_t channel, uint32_t target_id)
112 struct aac_cam *camsc;
114 if (target_id == AAC_CAM_TARGET_WILDCARD)
115 target_id = CAM_TARGET_WILDCARD;
117 TAILQ_FOREACH(sim, &sc->aac_sim_tqh, sim_link) {
118 camsc = sim->aac_cam;
119 if (camsc == NULL || camsc->inf == NULL ||
120 camsc->inf->BusNumber != channel)
123 ccb = xpt_alloc_ccb();
125 device_printf(sc->aac_dev,
126 "Cannot allocate ccb for bus rescan.\n");
130 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
131 cam_sim_path(camsc->sim),
132 target_id, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
134 device_printf(sc->aac_dev,
135 "Cannot create path for bus rescan.\n");
138 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5/*priority (low)*/);
139 ccb->ccb_h.func_code = XPT_SCAN_BUS;
140 ccb->ccb_h.cbfcnp = aac_bus_scan_cb;
141 ccb->crcn.flags = CAM_FLAG_NONE;
148 aac_bus_scan_cb(struct cam_periph *periph, union ccb *ccb)
150 if (ccb->ccb_h.status != CAM_REQ_CMP)
151 kprintf("cam_scan_callback: failure status = %x\n",
154 xpt_free_path(ccb->ccb_h.path);
159 aac_cam_event(struct aac_softc *sc, struct aac_event *event, void *arg)
162 struct aac_cam *camsc;
164 switch (event->ev_type) {
165 case AAC_EVENT_CMFREE:
167 camsc = ccb->ccb_h.sim_priv.entries[0].ptr;
168 kfree(event, M_AACCAM);
169 xpt_release_simq(camsc->sim, 1);
170 ccb->ccb_h.status = CAM_REQUEUE_REQ;
174 device_printf(sc->aac_dev, "unknown event %d in aac_cam\n",
183 aac_cam_probe(device_t dev)
185 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
191 aac_cam_detach(device_t dev)
193 struct aac_softc *sc;
194 struct aac_cam *camsc;
195 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
197 camsc = (struct aac_cam *)device_get_softc(dev);
198 sc = camsc->inf->aac_sc;
199 camsc->inf->aac_cam = NULL;
201 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
203 xpt_async(AC_LOST_DEVICE, camsc->path, NULL);
204 xpt_free_path(camsc->path);
205 xpt_bus_deregister(cam_sim_path(camsc->sim));
206 cam_sim_free(camsc->sim);
208 sc->cam_rescan_cb = NULL;
210 lockmgr(&sc->aac_io_lock, LK_RELEASE);
216 * Register the driver as a CAM SIM
219 aac_cam_attach(device_t dev)
221 struct cam_devq *devq;
223 struct cam_path *path;
224 struct aac_cam *camsc;
227 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
229 camsc = (struct aac_cam *)device_get_softc(dev);
230 inf = (struct aac_sim *)device_get_ivars(dev);
232 camsc->inf->aac_cam = camsc;
234 devq = cam_simq_alloc(inf->TargetsPerBus);
238 sim = cam_sim_alloc(aac_cam_action, aac_cam_poll, "aacp", camsc,
239 device_get_unit(dev), &inf->aac_sc->aac_io_lock, 1, 1, devq);
240 cam_simq_release(devq);
244 /* Since every bus has it's own sim, every bus 'appears' as bus 0 */
245 lockmgr(&inf->aac_sc->aac_io_lock, LK_EXCLUSIVE);
246 if (xpt_bus_register(sim, 0) != CAM_SUCCESS) {
248 lockmgr(&inf->aac_sc->aac_io_lock, LK_RELEASE);
252 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
253 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
254 xpt_bus_deregister(cam_sim_path(sim));
256 lockmgr(&inf->aac_sc->aac_io_lock, LK_RELEASE);
259 inf->aac_sc->cam_rescan_cb = aac_cam_rescan;
260 lockmgr(&inf->aac_sc->aac_io_lock, LK_RELEASE);
269 aac_cam_action(struct cam_sim *sim, union ccb *ccb)
271 struct aac_cam *camsc;
272 struct aac_softc *sc;
275 struct aac_command *cm;
277 camsc = (struct aac_cam *)cam_sim_softc(sim);
278 sc = camsc->inf->aac_sc;
279 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
281 /* Synchronous ops, and ops that don't require communication with the
283 switch(ccb->ccb_h.func_code) {
286 /* These are handled down below */
288 case XPT_CALC_GEOMETRY:
290 struct ccb_calc_geometry *ccg;
292 u_int32_t secs_per_cylinder;
295 size_mb = ccg->volume_size /
296 ((1024L * 1024L) / ccg->block_size);
297 if (size_mb >= (2 * 1024)) { /* 2GB */
299 ccg->secs_per_track = 63;
300 } else if (size_mb >= (1 * 1024)) { /* 1GB */
302 ccg->secs_per_track = 32;
305 ccg->secs_per_track = 32;
307 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
308 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
310 ccb->ccb_h.status = CAM_REQ_CMP;
316 struct ccb_pathinq *cpi = &ccb->cpi;
318 cpi->version_num = 1;
319 cpi->hba_inquiry = PI_WIDE_16;
320 cpi->target_sprt = 0;
323 * Resetting via the passthrough or parallel bus scan
326 cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN;
327 cpi->hba_eng_cnt = 0;
328 cpi->max_target = camsc->inf->TargetsPerBus;
329 cpi->max_lun = 8; /* Per the controller spec */
330 cpi->initiator_id = camsc->inf->InitiatorBusId;
331 cpi->bus_id = camsc->inf->BusNumber;
332 cpi->base_transfer_speed = 3300;
333 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
334 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
335 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
336 cpi->unit_number = cam_sim_unit(sim);
337 cpi->transport = XPORT_SPI;
338 cpi->transport_version = 2;
339 cpi->protocol = PROTO_SCSI;
340 cpi->protocol_version = SCSI_REV_2;
341 ccb->ccb_h.status = CAM_REQ_CMP;
345 case XPT_GET_TRAN_SETTINGS:
347 struct ccb_trans_settings_scsi *scsi =
348 &ccb->cts.proto_specific.scsi;
349 struct ccb_trans_settings_spi *spi =
350 &ccb->cts.xport_specific.spi;
351 ccb->cts.protocol = PROTO_SCSI;
352 ccb->cts.protocol_version = SCSI_REV_2;
353 ccb->cts.transport = XPORT_SPI;
354 ccb->cts.transport_version = 2;
355 if (ccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
356 scsi->valid = CTS_SCSI_VALID_TQ;
357 spi->valid |= CTS_SPI_VALID_DISC;
361 ccb->ccb_h.status = CAM_REQ_CMP;
365 case XPT_SET_TRAN_SETTINGS:
366 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
370 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
371 ccb->ccb_h.status = aac_cam_reset_bus(sim, ccb);
373 ccb->ccb_h.status = CAM_REQ_CMP;
378 ccb->ccb_h.status = aac_cam_abort_ccb(sim, ccb);
382 ccb->ccb_h.status = aac_cam_term_io(sim, ccb);
386 device_printf(sc->aac_dev, "Unsupported command 0x%x\n",
387 ccb->ccb_h.func_code);
388 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
393 /* Async ops that require communcation with the controller */
395 if (aac_alloc_command(sc, &cm)) {
396 struct aac_event *event;
398 xpt_freeze_simq(sim, 1);
399 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
400 ccb->ccb_h.sim_priv.entries[0].ptr = camsc;
401 event = kmalloc(sizeof(struct aac_event), M_AACCAM,
403 event->ev_callback = aac_cam_event;
405 event->ev_type = AAC_EVENT_CMFREE;
406 aac_add_event(sc, event);
411 srb = (struct aac_srb *)&fib->data[0];
414 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
416 srb->flags = AAC_SRB_FLAGS_DATA_IN;
417 cm->cm_flags |= AAC_CMD_DATAIN;
420 srb->flags = AAC_SRB_FLAGS_DATA_OUT;
421 cm->cm_flags |= AAC_CMD_DATAOUT;
424 srb->flags = AAC_SRB_FLAGS_NO_DATA_XFER;
427 srb->flags = AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION;
428 cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT;
432 switch(ccb->ccb_h.func_code) {
435 struct ccb_scsiio *csio = &ccb->csio;
437 srb->function = AAC_SRB_FUNC_EXECUTE_SCSI;
440 * Copy the CDB into the SRB. It's only 6-16 bytes,
441 * so a copy is not too expensive.
443 srb->cdb_len = csio->cdb_len;
444 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
445 bcopy(csio->cdb_io.cdb_ptr, (u_int8_t *)&srb->cdb[0],
448 bcopy(csio->cdb_io.cdb_bytes, (u_int8_t *)&srb->cdb[0],
452 fib->Header.Command = (sc->flags & AAC_FLAGS_SG_64BIT) ?
453 ScsiPortCommandU64 : ScsiPortCommand;
455 /* Map the s/g list. XXX 32bit addresses only! */
456 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
457 if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
458 srb->data_len = csio->dxfer_len;
459 if (ccb->ccb_h.flags & CAM_DATA_PHYS) {
460 /* Send a 32bit command */
461 fib->Header.Command = ScsiPortCommand;
462 srb->sg_map.SgCount = 1;
463 srb->sg_map.SgEntry[0].SgAddress =
464 (uint32_t)(uintptr_t)csio->data_ptr;
465 srb->sg_map.SgEntry[0].SgByteCount =
469 * Arrange things so that the S/G
470 * map will get set up automagically
472 cm->cm_data = (void *)csio->data_ptr;
473 cm->cm_datalen = csio->dxfer_len;
474 cm->cm_sgtable = &srb->sg_map;
477 /* XXX Need to handle multiple s/g elements */
478 panic("aac_cam: multiple s/g elements");
481 srb->sg_map.SgCount = 0;
482 srb->sg_map.SgEntry[0].SgByteCount = 0;
489 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
490 srb->function = AAC_SRB_FUNC_RESET_DEVICE;
493 ccb->ccb_h.status = CAM_REQ_CMP;
501 srb->bus = camsc->inf->BusNumber; /* Bus number relative to the card */
502 srb->target = ccb->ccb_h.target_id;
503 srb->lun = ccb->ccb_h.target_lun;
504 srb->timeout = ccb->ccb_h.timeout; /* XXX */
505 srb->retry_limit = 0;
507 cm->cm_complete = aac_cam_complete;
508 cm->cm_private = ccb;
509 cm->cm_timestamp = time_uptime;
511 fib->Header.XferState =
512 AAC_FIBSTATE_HOSTOWNED |
513 AAC_FIBSTATE_INITIALISED |
514 AAC_FIBSTATE_FROMHOST |
515 AAC_FIBSTATE_REXPECTED |
517 fib->Header.Size = sizeof(struct aac_fib_header) +
518 sizeof(struct aac_srb);
520 aac_enqueue_ready(cm);
521 aac_startio(cm->cm_sc);
527 aac_cam_poll(struct cam_sim *sim)
530 * Pinging the interrupt routine isn't very safe, nor is it
531 * really necessary. Do nothing.
536 aac_cam_fix_inquiry(struct aac_softc *sc, union ccb *ccb)
538 struct scsi_inquiry_data *inq;
540 uint8_t device, qual;
542 /* If this is an inquiry command, fake things out */
543 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
544 data = ccb->csio.cdb_io.cdb_ptr;
546 data = ccb->csio.cdb_io.cdb_bytes;
548 if (data[0] != INQUIRY)
551 if (ccb->ccb_h.status == CAM_REQ_CMP) {
552 inq = (struct scsi_inquiry_data *)ccb->csio.data_ptr;
553 device = SID_TYPE(inq);
554 qual = SID_QUAL(inq);
557 * We want DASD and PROC devices to only be
558 * visible through the pass device.
560 if (((device == T_DIRECT) ||
561 (device == T_PROCESSOR) ||
562 (sc->flags & AAC_FLAGS_CAM_PASSONLY))) {
564 * Some aac(4) adapters will always report that a direct
565 * access device is offline in response to a INQUIRY
566 * command that does not retreive vital product data.
567 * Force the qualifier to connected so that upper layers
568 * correctly recognize that a disk is present.
570 if ((data[1] & SI_EVPD) == 0 && device == T_DIRECT &&
571 qual == SID_QUAL_LU_OFFLINE)
572 qual = SID_QUAL_LU_CONNECTED;
573 ccb->csio.data_ptr[0] = (qual << 5) | T_NODEVICE;
575 } else if (ccb->ccb_h.status == CAM_SEL_TIMEOUT &&
576 ccb->ccb_h.target_lun != 0) {
577 /* fix for INQUIRYs on Lun>0 */
578 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
583 aac_cam_complete(struct aac_command *cm)
586 struct aac_srb_response *srbr;
587 struct aac_softc *sc;
591 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
592 ccb = cm->cm_private;
593 srbr = (struct aac_srb_response *)&cm->cm_fib->data[0];
595 if (srbr->fib_status != 0) {
596 device_printf(sc->aac_dev, "Passthru FIB failed!\n");
597 ccb->ccb_h.status = CAM_REQ_ABORTED;
600 * The SRB error codes just happen to match the CAM error
601 * codes. How convienient!
603 ccb->ccb_h.status = srbr->srb_status;
605 /* Take care of SCSI_IO ops. */
606 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
607 ccb->csio.scsi_status = srbr->scsi_status;
609 /* Take care of autosense */
610 if (srbr->sense_len) {
611 sense_returned = srbr->sense_len;
612 if (sense_returned < ccb->csio.sense_len)
613 ccb->csio.sense_resid =
614 ccb->csio.sense_len -
617 ccb->csio.sense_resid = 0;
618 bzero(&ccb->csio.sense_data,
619 sizeof(struct scsi_sense_data));
620 bcopy(&srbr->sense[0], &ccb->csio.sense_data,
621 min(ccb->csio.sense_len, sense_returned));
622 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
623 // scsi_sense_print(&ccb->csio);
626 aac_cam_fix_inquiry(sc, ccb);
630 aac_release_command(cm);
637 aac_cam_reset_bus(struct cam_sim *sim, union ccb *ccb)
640 struct aac_softc *sc;
641 struct aac_cam *camsc;
642 struct aac_vmioctl *vmi;
643 struct aac_resetbus *rbc;
646 camsc = (struct aac_cam *)cam_sim_softc(sim);
647 sc = camsc->inf->aac_sc;
650 kprintf("aac: Null sc?\n");
651 return (CAM_REQ_ABORTED);
654 aac_alloc_sync_fib(sc, &fib);
656 vmi = (struct aac_vmioctl *)&fib->data[0];
657 bzero(vmi, sizeof(struct aac_vmioctl));
659 vmi->Command = VM_Ioctl;
660 vmi->ObjType = FT_DRIVE;
661 vmi->MethId = sc->scsi_method_id;
663 vmi->IoctlCmd = ResetBus;
665 rbc = (struct aac_resetbus *)&vmi->IoctlBuf[0];
666 rbc->BusNumber = camsc->inf->BusNumber;
668 e = aac_sync_fib(sc, ContainerCommand, 0, fib,
669 sizeof(struct aac_vmioctl));
671 device_printf(sc->aac_dev,"Error %d sending ResetBus command\n",
673 aac_release_sync_fib(sc);
674 return (CAM_REQ_ABORTED);
677 aac_release_sync_fib(sc);
678 return (CAM_REQ_CMP);
682 aac_cam_abort_ccb(struct cam_sim *sim, union ccb *ccb)
684 return (CAM_UA_ABORT);
688 aac_cam_term_io(struct cam_sim *sim, union ccb *ccb)
690 return (CAM_UA_TERMIO);