2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2002 Adaptec, Inc.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 * CAM front-end for communicating with non-DASD devices
38 #include <sys/param.h>
39 #include <sys/systm.h>
40 #include <sys/kernel.h>
41 #include <sys/sysctl.h>
43 #include <sys/malloc.h>
44 #include <sys/module.h>
45 #include <sys/mutex.h>
48 #include <cam/cam_ccb.h>
49 #include <cam/cam_debug.h>
50 #include <cam/cam_periph.h>
51 #include <cam/cam_sim.h>
52 #include <cam/cam_xpt_sim.h>
53 #include <cam/scsi/scsi_all.h>
54 #include <cam/scsi/scsi_message.h>
60 #include <machine/md_var.h>
61 #include <machine/bus.h>
67 #include <dev/aac/aacreg.h>
68 #include <sys/aac_ioctl.h>
69 #include <dev/aac/aacvar.h>
75 struct cam_path *path;
78 static int aac_cam_probe(device_t dev);
79 static int aac_cam_attach(device_t dev);
80 static int aac_cam_detach(device_t dev);
81 static void aac_cam_action(struct cam_sim *, union ccb *);
82 static void aac_cam_poll(struct cam_sim *);
83 static void aac_cam_complete(struct aac_command *);
84 static void aac_cam_rescan(struct aac_softc *sc, uint32_t channel,
87 static u_int32_t aac_cam_reset_bus(struct cam_sim *, union ccb *);
88 static u_int32_t aac_cam_abort_ccb(struct cam_sim *, union ccb *);
89 static u_int32_t aac_cam_term_io(struct cam_sim *, union ccb *);
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, NULL, NULL);
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, NULL,
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");
146 aac_cam_event(struct aac_softc *sc, struct aac_event *event, void *arg)
149 struct aac_cam *camsc;
151 switch (event->ev_type) {
152 case AAC_EVENT_CMFREE:
154 camsc = ccb->ccb_h.sim_priv.entries[0].ptr;
155 free(event, M_AACCAM);
156 xpt_release_simq(camsc->sim, 1);
157 ccb->ccb_h.status = CAM_REQUEUE_REQ;
161 device_printf(sc->aac_dev, "unknown event %d in aac_cam\n",
168 aac_cam_probe(device_t dev)
170 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
176 aac_cam_detach(device_t dev)
178 struct aac_softc *sc;
179 struct aac_cam *camsc;
180 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
182 camsc = (struct aac_cam *)device_get_softc(dev);
183 sc = camsc->inf->aac_sc;
184 camsc->inf->aac_cam = NULL;
186 mtx_lock(&sc->aac_io_lock);
188 xpt_async(AC_LOST_DEVICE, camsc->path, NULL);
189 xpt_free_path(camsc->path);
190 xpt_bus_deregister(cam_sim_path(camsc->sim));
191 cam_sim_free(camsc->sim, /*free_devq*/TRUE);
193 sc->cam_rescan_cb = NULL;
195 mtx_unlock(&sc->aac_io_lock);
201 * Register the driver as a CAM SIM
204 aac_cam_attach(device_t dev)
206 struct cam_devq *devq;
208 struct cam_path *path;
209 struct aac_cam *camsc;
212 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
214 camsc = (struct aac_cam *)device_get_softc(dev);
215 inf = (struct aac_sim *)device_get_ivars(dev);
217 camsc->inf->aac_cam = camsc;
219 devq = cam_simq_alloc(inf->TargetsPerBus);
223 sim = cam_sim_alloc(aac_cam_action, aac_cam_poll, "aacp", camsc,
224 device_get_unit(dev), &inf->aac_sc->aac_io_lock, 1, 1, devq);
230 /* Since every bus has it's own sim, every bus 'appears' as bus 0 */
231 mtx_lock(&inf->aac_sc->aac_io_lock);
232 if (xpt_bus_register(sim, dev, 0) != CAM_SUCCESS) {
233 cam_sim_free(sim, TRUE);
234 mtx_unlock(&inf->aac_sc->aac_io_lock);
238 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
239 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
240 xpt_bus_deregister(cam_sim_path(sim));
241 cam_sim_free(sim, TRUE);
242 mtx_unlock(&inf->aac_sc->aac_io_lock);
245 inf->aac_sc->cam_rescan_cb = aac_cam_rescan;
246 mtx_unlock(&inf->aac_sc->aac_io_lock);
255 aac_cam_action(struct cam_sim *sim, union ccb *ccb)
257 struct aac_cam *camsc;
258 struct aac_softc *sc;
261 struct aac_command *cm;
263 camsc = (struct aac_cam *)cam_sim_softc(sim);
264 sc = camsc->inf->aac_sc;
265 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
267 /* Synchronous ops, and ops that don't require communication with the
269 switch(ccb->ccb_h.func_code) {
272 /* These are handled down below */
274 case XPT_CALC_GEOMETRY:
276 struct ccb_calc_geometry *ccg;
278 u_int32_t secs_per_cylinder;
281 size_mb = ccg->volume_size /
282 ((1024L * 1024L) / ccg->block_size);
283 if (size_mb >= (2 * 1024)) { /* 2GB */
285 ccg->secs_per_track = 63;
286 } else if (size_mb >= (1 * 1024)) { /* 1GB */
288 ccg->secs_per_track = 32;
291 ccg->secs_per_track = 32;
293 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
294 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
296 ccb->ccb_h.status = CAM_REQ_CMP;
302 struct ccb_pathinq *cpi = &ccb->cpi;
304 cpi->version_num = 1;
305 cpi->hba_inquiry = PI_WIDE_16;
306 cpi->target_sprt = 0;
309 * Resetting via the passthrough or parallel bus scan
312 cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN;
313 cpi->hba_eng_cnt = 0;
314 cpi->max_target = camsc->inf->TargetsPerBus;
315 cpi->max_lun = 8; /* Per the controller spec */
316 cpi->initiator_id = camsc->inf->InitiatorBusId;
317 cpi->bus_id = camsc->inf->BusNumber;
318 cpi->base_transfer_speed = 3300;
319 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
320 strlcpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
321 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
322 cpi->unit_number = cam_sim_unit(sim);
323 cpi->transport = XPORT_SPI;
324 cpi->transport_version = 2;
325 cpi->protocol = PROTO_SCSI;
326 cpi->protocol_version = SCSI_REV_2;
327 ccb->ccb_h.status = CAM_REQ_CMP;
331 case XPT_GET_TRAN_SETTINGS:
333 struct ccb_trans_settings_scsi *scsi =
334 &ccb->cts.proto_specific.scsi;
335 struct ccb_trans_settings_spi *spi =
336 &ccb->cts.xport_specific.spi;
337 ccb->cts.protocol = PROTO_SCSI;
338 ccb->cts.protocol_version = SCSI_REV_2;
339 ccb->cts.transport = XPORT_SPI;
340 ccb->cts.transport_version = 2;
341 if (ccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
342 scsi->valid = CTS_SCSI_VALID_TQ;
343 spi->valid |= CTS_SPI_VALID_DISC;
347 ccb->ccb_h.status = CAM_REQ_CMP;
351 case XPT_SET_TRAN_SETTINGS:
352 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
356 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
357 ccb->ccb_h.status = aac_cam_reset_bus(sim, ccb);
359 ccb->ccb_h.status = CAM_REQ_CMP;
364 ccb->ccb_h.status = aac_cam_abort_ccb(sim, ccb);
368 ccb->ccb_h.status = aac_cam_term_io(sim, ccb);
372 device_printf(sc->aac_dev, "Unsupported command 0x%x\n",
373 ccb->ccb_h.func_code);
374 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
379 /* Async ops that require communcation with the controller */
381 if (aac_alloc_command(sc, &cm)) {
382 struct aac_event *event;
384 xpt_freeze_simq(sim, 1);
385 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
386 ccb->ccb_h.sim_priv.entries[0].ptr = camsc;
387 event = malloc(sizeof(struct aac_event), M_AACCAM,
390 device_printf(sc->aac_dev,
391 "Warning, out of memory for event\n");
394 event->ev_callback = aac_cam_event;
396 event->ev_type = AAC_EVENT_CMFREE;
397 aac_add_event(sc, event);
402 srb = (struct aac_srb *)&fib->data[0];
405 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
407 srb->flags = AAC_SRB_FLAGS_DATA_IN;
408 cm->cm_flags |= AAC_CMD_DATAIN;
411 srb->flags = AAC_SRB_FLAGS_DATA_OUT;
412 cm->cm_flags |= AAC_CMD_DATAOUT;
415 srb->flags = AAC_SRB_FLAGS_NO_DATA_XFER;
418 srb->flags = AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION;
419 cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT;
423 switch(ccb->ccb_h.func_code) {
426 struct ccb_scsiio *csio = &ccb->csio;
428 srb->function = AAC_SRB_FUNC_EXECUTE_SCSI;
431 * Copy the CDB into the SRB. It's only 6-16 bytes,
432 * so a copy is not too expensive.
434 srb->cdb_len = csio->cdb_len;
435 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
436 bcopy(csio->cdb_io.cdb_ptr, (u_int8_t *)&srb->cdb[0],
439 bcopy(csio->cdb_io.cdb_bytes, (u_int8_t *)&srb->cdb[0],
443 fib->Header.Command = (sc->flags & AAC_FLAGS_SG_64BIT) ?
444 ScsiPortCommandU64 : ScsiPortCommand;
446 /* Map the s/g list. XXX 32bit addresses only! */
447 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
448 switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
450 srb->data_len = csio->dxfer_len;
452 * Arrange things so that the S/G
453 * map will get set up automagically
455 cm->cm_data = (void *)csio->data_ptr;
456 cm->cm_datalen = csio->dxfer_len;
457 cm->cm_sgtable = &srb->sg_map;
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 =
467 srb->data_len = csio->dxfer_len;
470 /* XXX Need to handle multiple s/g elements */
471 panic("aac_cam: multiple s/g elements");
474 srb->sg_map.SgCount = 0;
475 srb->sg_map.SgEntry[0].SgByteCount = 0;
482 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
483 srb->function = AAC_SRB_FUNC_RESET_DEVICE;
486 ccb->ccb_h.status = CAM_REQ_CMP;
494 srb->bus = camsc->inf->BusNumber; /* Bus number relative to the card */
495 srb->target = ccb->ccb_h.target_id;
496 srb->lun = ccb->ccb_h.target_lun;
497 srb->timeout = ccb->ccb_h.timeout; /* XXX */
498 srb->retry_limit = 0;
500 cm->cm_complete = aac_cam_complete;
501 cm->cm_private = ccb;
502 cm->cm_timestamp = time_uptime;
504 fib->Header.XferState =
505 AAC_FIBSTATE_HOSTOWNED |
506 AAC_FIBSTATE_INITIALISED |
507 AAC_FIBSTATE_FROMHOST |
508 AAC_FIBSTATE_REXPECTED |
510 fib->Header.Size = sizeof(struct aac_fib_header) +
511 sizeof(struct aac_srb);
513 aac_enqueue_ready(cm);
514 aac_startio(cm->cm_sc);
518 aac_cam_poll(struct cam_sim *sim)
521 * Pinging the interrupt routine isn't very safe, nor is it
522 * really necessary. Do nothing.
527 aac_cam_fix_inquiry(struct aac_softc *sc, union ccb *ccb)
529 struct scsi_inquiry_data *inq;
531 uint8_t device, qual;
533 /* If this is an inquiry command, fake things out */
534 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
535 data = ccb->csio.cdb_io.cdb_ptr;
537 data = ccb->csio.cdb_io.cdb_bytes;
539 if (data[0] != INQUIRY)
542 if (ccb->ccb_h.status == CAM_REQ_CMP) {
543 inq = (struct scsi_inquiry_data *)ccb->csio.data_ptr;
544 device = SID_TYPE(inq);
545 qual = SID_QUAL(inq);
548 * We want DASD and PROC devices to only be
549 * visible through the pass device.
551 if (((device == T_DIRECT) ||
552 (device == T_PROCESSOR) ||
553 (sc->flags & AAC_FLAGS_CAM_PASSONLY))) {
555 * Some aac(4) adapters will always report that a direct
556 * access device is offline in response to a INQUIRY
557 * command that does not retrieve vital product data.
558 * Force the qualifier to connected so that upper layers
559 * correctly recognize that a disk is present.
561 if ((data[1] & SI_EVPD) == 0 && device == T_DIRECT &&
562 qual == SID_QUAL_LU_OFFLINE)
563 qual = SID_QUAL_LU_CONNECTED;
564 ccb->csio.data_ptr[0] = (qual << 5) | T_NODEVICE;
566 } else if (ccb->ccb_h.status == CAM_SEL_TIMEOUT &&
567 ccb->ccb_h.target_lun != 0) {
568 /* fix for INQUIRYs on Lun>0 */
569 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
574 aac_cam_complete(struct aac_command *cm)
577 struct aac_srb_response *srbr;
578 struct aac_softc *sc;
582 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
583 ccb = cm->cm_private;
584 srbr = (struct aac_srb_response *)&cm->cm_fib->data[0];
586 if (srbr->fib_status != 0) {
587 device_printf(sc->aac_dev, "Passthru FIB failed!\n");
588 ccb->ccb_h.status = CAM_REQ_ABORTED;
591 * The SRB error codes just happen to match the CAM error
592 * codes. How convenient!
594 ccb->ccb_h.status = srbr->srb_status;
596 /* Take care of SCSI_IO ops. */
597 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
598 ccb->csio.scsi_status = srbr->scsi_status;
600 /* Take care of autosense */
601 if (srbr->sense_len) {
602 sense_returned = srbr->sense_len;
603 if (sense_returned < ccb->csio.sense_len)
604 ccb->csio.sense_resid =
605 ccb->csio.sense_len -
608 ccb->csio.sense_resid = 0;
609 bzero(&ccb->csio.sense_data,
610 sizeof(struct scsi_sense_data));
611 bcopy(&srbr->sense[0], &ccb->csio.sense_data,
612 min(ccb->csio.sense_len, sense_returned));
613 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
614 // scsi_sense_print(&ccb->csio);
617 aac_cam_fix_inquiry(sc, ccb);
621 aac_release_command(cm);
626 aac_cam_reset_bus(struct cam_sim *sim, union ccb *ccb)
629 struct aac_softc *sc;
630 struct aac_cam *camsc;
631 struct aac_vmioctl *vmi;
632 struct aac_resetbus *rbc;
635 camsc = (struct aac_cam *)cam_sim_softc(sim);
636 sc = camsc->inf->aac_sc;
639 printf("aac: Null sc?\n");
640 return (CAM_REQ_ABORTED);
643 aac_alloc_sync_fib(sc, &fib);
645 vmi = (struct aac_vmioctl *)&fib->data[0];
646 bzero(vmi, sizeof(struct aac_vmioctl));
648 vmi->Command = VM_Ioctl;
649 vmi->ObjType = FT_DRIVE;
650 vmi->MethId = sc->scsi_method_id;
652 vmi->IoctlCmd = ResetBus;
654 rbc = (struct aac_resetbus *)&vmi->IoctlBuf[0];
655 rbc->BusNumber = camsc->inf->BusNumber;
657 e = aac_sync_fib(sc, ContainerCommand, 0, fib,
658 sizeof(struct aac_vmioctl));
660 device_printf(sc->aac_dev,"Error %d sending ResetBus command\n",
662 aac_release_sync_fib(sc);
663 return (CAM_REQ_ABORTED);
666 aac_release_sync_fib(sc);
667 return (CAM_REQ_CMP);
671 aac_cam_abort_ccb(struct cam_sim *sim, union ccb *ccb)
673 return (CAM_UA_ABORT);
677 aac_cam_term_io(struct cam_sim *sim, union ccb *ccb)
679 return (CAM_UA_TERMIO);