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: src/sys/dev/aac/aac_cam.c,v 1.40 2010/12/06 17:06:21 jhb Exp $
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>
38 #include <sys/sysctl.h>
40 #include <sys/malloc.h>
41 #include <sys/module.h>
43 #include <bus/cam/cam.h>
44 #include <bus/cam/cam_ccb.h>
45 #include <bus/cam/cam_debug.h>
46 #include <bus/cam/cam_periph.h>
47 #include <bus/cam/cam_sim.h>
48 #include <bus/cam/cam_xpt_periph.h>
49 #include <bus/cam/cam_xpt_sim.h>
50 #include <bus/cam/scsi/scsi_all.h>
51 #include <bus/cam/scsi/scsi_message.h>
57 #include <machine/md_var.h>
63 #include <dev/raid/aac/aacreg.h>
64 #include <dev/raid/aac/aac_ioctl.h>
65 #include <dev/raid/aac/aacvar.h>
71 struct cam_path *path;
74 static int aac_cam_probe(device_t dev);
75 static int aac_cam_attach(device_t dev);
76 static int aac_cam_detach(device_t dev);
77 static void aac_cam_action(struct cam_sim *, union ccb *);
78 static void aac_cam_poll(struct cam_sim *);
79 static void aac_cam_complete(struct aac_command *);
80 static void aac_cam_rescan(struct aac_softc *sc, uint32_t channel,
82 static void aac_bus_scan_cb(struct cam_periph *periph, union ccb *ccb);
84 static u_int32_t aac_cam_reset_bus(struct cam_sim *, union ccb *);
85 static u_int32_t aac_cam_abort_ccb(struct cam_sim *, union ccb *);
86 static u_int32_t aac_cam_term_io(struct cam_sim *, union ccb *);
88 static devclass_t aac_pass_devclass;
90 static device_method_t aac_pass_methods[] = {
91 DEVMETHOD(device_probe, aac_cam_probe),
92 DEVMETHOD(device_attach, aac_cam_attach),
93 DEVMETHOD(device_detach, aac_cam_detach),
97 static driver_t aac_pass_driver = {
100 sizeof(struct aac_cam)
103 DRIVER_MODULE(aacp, aac, aac_pass_driver, aac_pass_devclass, 0, 0);
104 MODULE_DEPEND(aacp, cam, 1, 1, 1);
106 MALLOC_DEFINE(M_AACCAM, "aaccam", "AAC CAM info");
109 aac_cam_rescan(struct aac_softc *sc, uint32_t channel, uint32_t target_id)
113 struct aac_cam *camsc;
115 if (target_id == AAC_CAM_TARGET_WILDCARD)
116 target_id = CAM_TARGET_WILDCARD;
118 TAILQ_FOREACH(sim, &sc->aac_sim_tqh, sim_link) {
119 camsc = sim->aac_cam;
120 if (camsc == NULL || camsc->inf == NULL ||
121 camsc->inf->BusNumber != channel)
124 ccb = xpt_alloc_ccb();
126 device_printf(sc->aac_dev,
127 "Cannot allocate ccb for bus rescan.\n");
131 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
132 cam_sim_path(camsc->sim),
133 target_id, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
135 device_printf(sc->aac_dev,
136 "Cannot create path for bus rescan.\n");
139 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5/*priority (low)*/);
140 ccb->ccb_h.func_code = XPT_SCAN_BUS;
141 ccb->ccb_h.cbfcnp = aac_bus_scan_cb;
142 ccb->crcn.flags = CAM_FLAG_NONE;
149 aac_bus_scan_cb(struct cam_periph *periph, union ccb *ccb)
151 if (ccb->ccb_h.status != CAM_REQ_CMP)
152 kprintf("cam_scan_callback: failure status = %x\n",
155 xpt_free_path(ccb->ccb_h.path);
160 aac_cam_event(struct aac_softc *sc, struct aac_event *event, void *arg)
163 struct aac_cam *camsc;
165 switch (event->ev_type) {
166 case AAC_EVENT_CMFREE:
168 camsc = ccb->ccb_h.sim_priv.entries[0].ptr;
169 kfree(event, M_AACCAM);
170 xpt_release_simq(camsc->sim, 1);
171 ccb->ccb_h.status = CAM_REQUEUE_REQ;
175 device_printf(sc->aac_dev, "unknown event %d in aac_cam\n",
184 aac_cam_probe(device_t dev)
186 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
192 aac_cam_detach(device_t dev)
194 struct aac_softc *sc;
195 struct aac_cam *camsc;
196 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
198 camsc = (struct aac_cam *)device_get_softc(dev);
199 sc = camsc->inf->aac_sc;
200 camsc->inf->aac_cam = NULL;
202 lockmgr(&sc->aac_io_lock, LK_EXCLUSIVE);
204 xpt_async(AC_LOST_DEVICE, camsc->path, NULL);
205 xpt_free_path(camsc->path);
206 xpt_bus_deregister(cam_sim_path(camsc->sim));
207 cam_sim_free(camsc->sim);
209 sc->cam_rescan_cb = NULL;
211 lockmgr(&sc->aac_io_lock, LK_RELEASE);
217 * Register the driver as a CAM SIM
220 aac_cam_attach(device_t dev)
222 struct cam_devq *devq;
224 struct cam_path *path;
225 struct aac_cam *camsc;
228 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
230 camsc = (struct aac_cam *)device_get_softc(dev);
231 inf = (struct aac_sim *)device_get_ivars(dev);
233 camsc->inf->aac_cam = camsc;
235 devq = cam_simq_alloc(inf->TargetsPerBus);
239 sim = cam_sim_alloc(aac_cam_action, aac_cam_poll, "aacp", camsc,
240 device_get_unit(dev), &inf->aac_sc->aac_io_lock, 1, 1, devq);
241 cam_simq_release(devq);
245 /* Since every bus has it's own sim, every bus 'appears' as bus 0 */
246 lockmgr(&inf->aac_sc->aac_io_lock, LK_EXCLUSIVE);
247 if (xpt_bus_register(sim, 0) != CAM_SUCCESS) {
249 lockmgr(&inf->aac_sc->aac_io_lock, LK_RELEASE);
253 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
254 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
255 xpt_bus_deregister(cam_sim_path(sim));
257 lockmgr(&inf->aac_sc->aac_io_lock, LK_RELEASE);
260 inf->aac_sc->cam_rescan_cb = aac_cam_rescan;
261 lockmgr(&inf->aac_sc->aac_io_lock, LK_RELEASE);
270 aac_cam_action(struct cam_sim *sim, union ccb *ccb)
272 struct aac_cam *camsc;
273 struct aac_softc *sc;
276 struct aac_command *cm;
278 camsc = (struct aac_cam *)cam_sim_softc(sim);
279 sc = camsc->inf->aac_sc;
280 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
282 /* Synchronous ops, and ops that don't require communication with the
284 switch(ccb->ccb_h.func_code) {
287 /* These are handled down below */
289 case XPT_CALC_GEOMETRY:
291 struct ccb_calc_geometry *ccg;
293 u_int32_t secs_per_cylinder;
296 size_mb = ccg->volume_size /
297 ((1024L * 1024L) / ccg->block_size);
298 if (size_mb >= (2 * 1024)) { /* 2GB */
300 ccg->secs_per_track = 63;
301 } else if (size_mb >= (1 * 1024)) { /* 1GB */
303 ccg->secs_per_track = 32;
306 ccg->secs_per_track = 32;
308 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
309 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
311 ccb->ccb_h.status = CAM_REQ_CMP;
317 struct ccb_pathinq *cpi = &ccb->cpi;
319 cpi->version_num = 1;
320 cpi->hba_inquiry = PI_WIDE_16;
321 cpi->target_sprt = 0;
324 * Resetting via the passthrough or parallel bus scan
327 cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN;
328 cpi->hba_eng_cnt = 0;
329 cpi->max_target = camsc->inf->TargetsPerBus;
330 cpi->max_lun = 8; /* Per the controller spec */
331 cpi->initiator_id = camsc->inf->InitiatorBusId;
332 cpi->bus_id = camsc->inf->BusNumber;
333 cpi->base_transfer_speed = 3300;
334 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
335 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
336 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
337 cpi->unit_number = cam_sim_unit(sim);
338 cpi->transport = XPORT_SPI;
339 cpi->transport_version = 2;
340 cpi->protocol = PROTO_SCSI;
341 cpi->protocol_version = SCSI_REV_2;
342 ccb->ccb_h.status = CAM_REQ_CMP;
346 case XPT_GET_TRAN_SETTINGS:
348 struct ccb_trans_settings_scsi *scsi =
349 &ccb->cts.proto_specific.scsi;
350 struct ccb_trans_settings_spi *spi =
351 &ccb->cts.xport_specific.spi;
352 ccb->cts.protocol = PROTO_SCSI;
353 ccb->cts.protocol_version = SCSI_REV_2;
354 ccb->cts.transport = XPORT_SPI;
355 ccb->cts.transport_version = 2;
356 if (ccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
357 scsi->valid = CTS_SCSI_VALID_TQ;
358 spi->valid |= CTS_SPI_VALID_DISC;
362 ccb->ccb_h.status = CAM_REQ_CMP;
366 case XPT_SET_TRAN_SETTINGS:
367 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
371 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
372 ccb->ccb_h.status = aac_cam_reset_bus(sim, ccb);
374 ccb->ccb_h.status = CAM_REQ_CMP;
379 ccb->ccb_h.status = aac_cam_abort_ccb(sim, ccb);
383 ccb->ccb_h.status = aac_cam_term_io(sim, ccb);
387 device_printf(sc->aac_dev, "Unsupported command 0x%x\n",
388 ccb->ccb_h.func_code);
389 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
394 /* Async ops that require communcation with the controller */
396 if (aac_alloc_command(sc, &cm)) {
397 struct aac_event *event;
399 xpt_freeze_simq(sim, 1);
400 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
401 ccb->ccb_h.sim_priv.entries[0].ptr = camsc;
402 event = kmalloc(sizeof(struct aac_event), M_AACCAM,
404 event->ev_callback = aac_cam_event;
406 event->ev_type = AAC_EVENT_CMFREE;
407 aac_add_event(sc, event);
412 srb = (struct aac_srb *)&fib->data[0];
415 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
417 srb->flags = AAC_SRB_FLAGS_DATA_IN;
418 cm->cm_flags |= AAC_CMD_DATAIN;
421 srb->flags = AAC_SRB_FLAGS_DATA_OUT;
422 cm->cm_flags |= AAC_CMD_DATAOUT;
425 srb->flags = AAC_SRB_FLAGS_NO_DATA_XFER;
428 srb->flags = AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION;
429 cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT;
433 switch(ccb->ccb_h.func_code) {
436 struct ccb_scsiio *csio = &ccb->csio;
438 srb->function = AAC_SRB_FUNC_EXECUTE_SCSI;
441 * Copy the CDB into the SRB. It's only 6-16 bytes,
442 * so a copy is not too expensive.
444 srb->cdb_len = csio->cdb_len;
445 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
446 bcopy(csio->cdb_io.cdb_ptr, (u_int8_t *)&srb->cdb[0],
449 bcopy(csio->cdb_io.cdb_bytes, (u_int8_t *)&srb->cdb[0],
453 fib->Header.Command = (sc->flags & AAC_FLAGS_SG_64BIT) ?
454 ScsiPortCommandU64 : ScsiPortCommand;
456 /* Map the s/g list. XXX 32bit addresses only! */
457 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
458 if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
459 srb->data_len = csio->dxfer_len;
460 if (ccb->ccb_h.flags & CAM_DATA_PHYS) {
461 /* Send a 32bit command */
462 fib->Header.Command = ScsiPortCommand;
463 srb->sg_map.SgCount = 1;
464 srb->sg_map.SgEntry[0].SgAddress =
465 (uint32_t)(uintptr_t)csio->data_ptr;
466 srb->sg_map.SgEntry[0].SgByteCount =
470 * Arrange things so that the S/G
471 * map will get set up automagically
473 cm->cm_data = (void *)csio->data_ptr;
474 cm->cm_datalen = csio->dxfer_len;
475 cm->cm_sgtable = &srb->sg_map;
478 /* XXX Need to handle multiple s/g elements */
479 panic("aac_cam: multiple s/g elements");
482 srb->sg_map.SgCount = 0;
483 srb->sg_map.SgEntry[0].SgByteCount = 0;
490 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
491 srb->function = AAC_SRB_FUNC_RESET_DEVICE;
494 ccb->ccb_h.status = CAM_REQ_CMP;
502 srb->bus = camsc->inf->BusNumber; /* Bus number relative to the card */
503 srb->target = ccb->ccb_h.target_id;
504 srb->lun = ccb->ccb_h.target_lun;
505 srb->timeout = ccb->ccb_h.timeout; /* XXX */
506 srb->retry_limit = 0;
508 cm->cm_complete = aac_cam_complete;
509 cm->cm_private = ccb;
510 cm->cm_timestamp = time_second;
512 fib->Header.XferState =
513 AAC_FIBSTATE_HOSTOWNED |
514 AAC_FIBSTATE_INITIALISED |
515 AAC_FIBSTATE_FROMHOST |
516 AAC_FIBSTATE_REXPECTED |
518 fib->Header.Size = sizeof(struct aac_fib_header) +
519 sizeof(struct aac_srb);
521 aac_enqueue_ready(cm);
522 aac_startio(cm->cm_sc);
528 aac_cam_poll(struct cam_sim *sim)
531 * Pinging the interrupt routine isn't very safe, nor is it
532 * really necessary. Do nothing.
537 aac_cam_complete(struct aac_command *cm)
540 struct aac_srb_response *srbr;
541 struct aac_softc *sc;
544 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
545 ccb = cm->cm_private;
546 srbr = (struct aac_srb_response *)&cm->cm_fib->data[0];
548 if (srbr->fib_status != 0) {
549 device_printf(sc->aac_dev, "Passthru FIB failed!\n");
550 ccb->ccb_h.status = CAM_REQ_ABORTED;
553 * The SRB error codes just happen to match the CAM error
554 * codes. How convienient!
556 ccb->ccb_h.status = srbr->srb_status;
558 /* Take care of SCSI_IO ops. */
559 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
560 u_int8_t command, device;
562 ccb->csio.scsi_status = srbr->scsi_status;
564 /* Take care of autosense */
565 if (srbr->sense_len) {
566 int sense_len, scsi_sense_len;
568 scsi_sense_len = sizeof(struct scsi_sense_data);
569 bzero(&ccb->csio.sense_data, scsi_sense_len);
570 sense_len = (srbr->sense_len >
571 scsi_sense_len) ? scsi_sense_len :
573 bcopy(&srbr->sense[0], &ccb->csio.sense_data,
575 ccb->csio.sense_len = sense_len;
576 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
577 // scsi_sense_print(&ccb->csio);
580 /* If this is an inquiry command, fake things out */
581 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
582 command = ccb->csio.cdb_io.cdb_ptr[0];
584 command = ccb->csio.cdb_io.cdb_bytes[0];
586 if (command == INQUIRY) {
587 if (ccb->ccb_h.status == CAM_REQ_CMP) {
588 device = ccb->csio.data_ptr[0] & 0x1f;
590 * We want DASD and PROC devices to only be
591 * visible through the pass device.
593 if ((device == T_DIRECT) ||
594 (device == T_PROCESSOR) ||
595 (sc->flags & AAC_FLAGS_CAM_PASSONLY))
596 ccb->csio.data_ptr[0] =
597 ((ccb->csio.data_ptr[0] & 0xe0) |
599 } else if (ccb->ccb_h.status == CAM_SEL_TIMEOUT &&
600 ccb->ccb_h.target_lun != 0) {
601 /* fix for INQUIRYs on Lun>0 */
602 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
608 aac_release_command(cm);
615 aac_cam_reset_bus(struct cam_sim *sim, union ccb *ccb)
618 struct aac_softc *sc;
619 struct aac_cam *camsc;
620 struct aac_vmioctl *vmi;
621 struct aac_resetbus *rbc;
624 camsc = (struct aac_cam *)cam_sim_softc(sim);
625 sc = camsc->inf->aac_sc;
628 kprintf("aac: Null sc?\n");
629 return (CAM_REQ_ABORTED);
632 aac_alloc_sync_fib(sc, &fib);
634 vmi = (struct aac_vmioctl *)&fib->data[0];
635 bzero(vmi, sizeof(struct aac_vmioctl));
637 vmi->Command = VM_Ioctl;
638 vmi->ObjType = FT_DRIVE;
639 vmi->MethId = sc->scsi_method_id;
641 vmi->IoctlCmd = ResetBus;
643 rbc = (struct aac_resetbus *)&vmi->IoctlBuf[0];
644 rbc->BusNumber = camsc->inf->BusNumber;
646 e = aac_sync_fib(sc, ContainerCommand, 0, fib,
647 sizeof(struct aac_vmioctl));
649 device_printf(sc->aac_dev,"Error %d sending ResetBus command\n",
651 aac_release_sync_fib(sc);
652 return (CAM_REQ_ABORTED);
655 aac_release_sync_fib(sc);
656 return (CAM_REQ_CMP);
660 aac_cam_abort_ccb(struct cam_sim *sim, union ccb *ccb)
662 return (CAM_UA_ABORT);
666 aac_cam_term_io(struct cam_sim *sim, union ccb *ccb)
668 return (CAM_UA_TERMIO);