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>
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 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, 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_nowait();
125 device_printf(sc->aac_dev,
126 "Cannot allocate ccb for bus rescan.\n");
130 if (xpt_create_path(&ccb->ccb_h.path, NULL,
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");
144 aac_cam_event(struct aac_softc *sc, struct aac_event *event, void *arg)
147 struct aac_cam *camsc;
149 switch (event->ev_type) {
150 case AAC_EVENT_CMFREE:
152 camsc = ccb->ccb_h.sim_priv.entries[0].ptr;
153 free(event, M_AACCAM);
154 xpt_release_simq(camsc->sim, 1);
155 ccb->ccb_h.status = CAM_REQUEUE_REQ;
159 device_printf(sc->aac_dev, "unknown event %d in aac_cam\n",
166 aac_cam_probe(device_t dev)
168 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
174 aac_cam_detach(device_t dev)
176 struct aac_softc *sc;
177 struct aac_cam *camsc;
178 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
180 camsc = (struct aac_cam *)device_get_softc(dev);
181 sc = camsc->inf->aac_sc;
182 camsc->inf->aac_cam = NULL;
184 mtx_lock(&sc->aac_io_lock);
186 xpt_async(AC_LOST_DEVICE, camsc->path, NULL);
187 xpt_free_path(camsc->path);
188 xpt_bus_deregister(cam_sim_path(camsc->sim));
189 cam_sim_free(camsc->sim, /*free_devq*/TRUE);
191 sc->cam_rescan_cb = NULL;
193 mtx_unlock(&sc->aac_io_lock);
199 * Register the driver as a CAM SIM
202 aac_cam_attach(device_t dev)
204 struct cam_devq *devq;
206 struct cam_path *path;
207 struct aac_cam *camsc;
210 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
212 camsc = (struct aac_cam *)device_get_softc(dev);
213 inf = (struct aac_sim *)device_get_ivars(dev);
215 camsc->inf->aac_cam = camsc;
217 devq = cam_simq_alloc(inf->TargetsPerBus);
221 sim = cam_sim_alloc(aac_cam_action, aac_cam_poll, "aacp", camsc,
222 device_get_unit(dev), &inf->aac_sc->aac_io_lock, 1, 1, devq);
228 /* Since every bus has it's own sim, every bus 'appears' as bus 0 */
229 mtx_lock(&inf->aac_sc->aac_io_lock);
230 if (xpt_bus_register(sim, dev, 0) != CAM_SUCCESS) {
231 cam_sim_free(sim, TRUE);
232 mtx_unlock(&inf->aac_sc->aac_io_lock);
236 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
237 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
238 xpt_bus_deregister(cam_sim_path(sim));
239 cam_sim_free(sim, TRUE);
240 mtx_unlock(&inf->aac_sc->aac_io_lock);
243 inf->aac_sc->cam_rescan_cb = aac_cam_rescan;
244 mtx_unlock(&inf->aac_sc->aac_io_lock);
253 aac_cam_action(struct cam_sim *sim, union ccb *ccb)
255 struct aac_cam *camsc;
256 struct aac_softc *sc;
259 struct aac_command *cm;
261 camsc = (struct aac_cam *)cam_sim_softc(sim);
262 sc = camsc->inf->aac_sc;
263 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
265 /* Synchronous ops, and ops that don't require communication with the
267 switch(ccb->ccb_h.func_code) {
270 /* These are handled down below */
272 case XPT_CALC_GEOMETRY:
274 struct ccb_calc_geometry *ccg;
276 u_int32_t secs_per_cylinder;
279 size_mb = ccg->volume_size /
280 ((1024L * 1024L) / ccg->block_size);
281 if (size_mb >= (2 * 1024)) { /* 2GB */
283 ccg->secs_per_track = 63;
284 } else if (size_mb >= (1 * 1024)) { /* 1GB */
286 ccg->secs_per_track = 32;
289 ccg->secs_per_track = 32;
291 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
292 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
294 ccb->ccb_h.status = CAM_REQ_CMP;
300 struct ccb_pathinq *cpi = &ccb->cpi;
302 cpi->version_num = 1;
303 cpi->hba_inquiry = PI_WIDE_16;
304 cpi->target_sprt = 0;
307 * Resetting via the passthrough or parallel bus scan
310 cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN;
311 cpi->hba_eng_cnt = 0;
312 cpi->max_target = camsc->inf->TargetsPerBus;
313 cpi->max_lun = 8; /* Per the controller spec */
314 cpi->initiator_id = camsc->inf->InitiatorBusId;
315 cpi->bus_id = camsc->inf->BusNumber;
316 cpi->base_transfer_speed = 3300;
317 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
318 strlcpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
319 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
320 cpi->unit_number = cam_sim_unit(sim);
321 cpi->transport = XPORT_SPI;
322 cpi->transport_version = 2;
323 cpi->protocol = PROTO_SCSI;
324 cpi->protocol_version = SCSI_REV_2;
325 ccb->ccb_h.status = CAM_REQ_CMP;
329 case XPT_GET_TRAN_SETTINGS:
331 struct ccb_trans_settings_scsi *scsi =
332 &ccb->cts.proto_specific.scsi;
333 struct ccb_trans_settings_spi *spi =
334 &ccb->cts.xport_specific.spi;
335 ccb->cts.protocol = PROTO_SCSI;
336 ccb->cts.protocol_version = SCSI_REV_2;
337 ccb->cts.transport = XPORT_SPI;
338 ccb->cts.transport_version = 2;
339 if (ccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
340 scsi->valid = CTS_SCSI_VALID_TQ;
341 spi->valid |= CTS_SPI_VALID_DISC;
345 ccb->ccb_h.status = CAM_REQ_CMP;
349 case XPT_SET_TRAN_SETTINGS:
350 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
354 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
355 ccb->ccb_h.status = aac_cam_reset_bus(sim, ccb);
357 ccb->ccb_h.status = CAM_REQ_CMP;
362 ccb->ccb_h.status = aac_cam_abort_ccb(sim, ccb);
366 ccb->ccb_h.status = aac_cam_term_io(sim, ccb);
370 device_printf(sc->aac_dev, "Unsupported command 0x%x\n",
371 ccb->ccb_h.func_code);
372 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
377 /* Async ops that require communcation with the controller */
379 if (aac_alloc_command(sc, &cm)) {
380 struct aac_event *event;
382 xpt_freeze_simq(sim, 1);
383 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
384 ccb->ccb_h.sim_priv.entries[0].ptr = camsc;
385 event = malloc(sizeof(struct aac_event), M_AACCAM,
388 device_printf(sc->aac_dev,
389 "Warning, out of memory for event\n");
392 event->ev_callback = aac_cam_event;
394 event->ev_type = AAC_EVENT_CMFREE;
395 aac_add_event(sc, event);
400 srb = (struct aac_srb *)&fib->data[0];
403 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
405 srb->flags = AAC_SRB_FLAGS_DATA_IN;
406 cm->cm_flags |= AAC_CMD_DATAIN;
409 srb->flags = AAC_SRB_FLAGS_DATA_OUT;
410 cm->cm_flags |= AAC_CMD_DATAOUT;
413 srb->flags = AAC_SRB_FLAGS_NO_DATA_XFER;
416 srb->flags = AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION;
417 cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT;
421 switch(ccb->ccb_h.func_code) {
424 struct ccb_scsiio *csio = &ccb->csio;
426 srb->function = AAC_SRB_FUNC_EXECUTE_SCSI;
429 * Copy the CDB into the SRB. It's only 6-16 bytes,
430 * so a copy is not too expensive.
432 srb->cdb_len = csio->cdb_len;
433 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
434 bcopy(csio->cdb_io.cdb_ptr, (u_int8_t *)&srb->cdb[0],
437 bcopy(csio->cdb_io.cdb_bytes, (u_int8_t *)&srb->cdb[0],
441 fib->Header.Command = (sc->flags & AAC_FLAGS_SG_64BIT) ?
442 ScsiPortCommandU64 : ScsiPortCommand;
444 /* Map the s/g list. XXX 32bit addresses only! */
445 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
446 switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
448 srb->data_len = csio->dxfer_len;
450 * Arrange things so that the S/G
451 * map will get set up automagically
453 cm->cm_data = (void *)csio->data_ptr;
454 cm->cm_datalen = csio->dxfer_len;
455 cm->cm_sgtable = &srb->sg_map;
458 /* Send a 32bit command */
459 fib->Header.Command = ScsiPortCommand;
460 srb->sg_map.SgCount = 1;
461 srb->sg_map.SgEntry[0].SgAddress =
462 (uint32_t)(uintptr_t)csio->data_ptr;
463 srb->sg_map.SgEntry[0].SgByteCount =
465 srb->data_len = csio->dxfer_len;
468 /* XXX Need to handle multiple s/g elements */
469 panic("aac_cam: multiple s/g elements");
472 srb->sg_map.SgCount = 0;
473 srb->sg_map.SgEntry[0].SgByteCount = 0;
480 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
481 srb->function = AAC_SRB_FUNC_RESET_DEVICE;
484 ccb->ccb_h.status = CAM_REQ_CMP;
492 srb->bus = camsc->inf->BusNumber; /* Bus number relative to the card */
493 srb->target = ccb->ccb_h.target_id;
494 srb->lun = ccb->ccb_h.target_lun;
495 srb->timeout = ccb->ccb_h.timeout; /* XXX */
496 srb->retry_limit = 0;
498 cm->cm_complete = aac_cam_complete;
499 cm->cm_private = ccb;
500 cm->cm_timestamp = time_uptime;
502 fib->Header.XferState =
503 AAC_FIBSTATE_HOSTOWNED |
504 AAC_FIBSTATE_INITIALISED |
505 AAC_FIBSTATE_FROMHOST |
506 AAC_FIBSTATE_REXPECTED |
508 fib->Header.Size = sizeof(struct aac_fib_header) +
509 sizeof(struct aac_srb);
511 aac_enqueue_ready(cm);
512 aac_startio(cm->cm_sc);
516 aac_cam_poll(struct cam_sim *sim)
519 * Pinging the interrupt routine isn't very safe, nor is it
520 * really necessary. Do nothing.
525 aac_cam_fix_inquiry(struct aac_softc *sc, union ccb *ccb)
527 struct scsi_inquiry_data *inq;
529 uint8_t device, qual;
531 /* If this is an inquiry command, fake things out */
532 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
533 data = ccb->csio.cdb_io.cdb_ptr;
535 data = ccb->csio.cdb_io.cdb_bytes;
537 if (data[0] != INQUIRY)
540 if (ccb->ccb_h.status == CAM_REQ_CMP) {
541 inq = (struct scsi_inquiry_data *)ccb->csio.data_ptr;
542 device = SID_TYPE(inq);
543 qual = SID_QUAL(inq);
546 * We want DASD and PROC devices to only be
547 * visible through the pass device.
549 if (((device == T_DIRECT) ||
550 (device == T_PROCESSOR) ||
551 (sc->flags & AAC_FLAGS_CAM_PASSONLY))) {
553 * Some aac(4) adapters will always report that a direct
554 * access device is offline in response to a INQUIRY
555 * command that does not retrieve vital product data.
556 * Force the qualifier to connected so that upper layers
557 * correctly recognize that a disk is present.
559 if ((data[1] & SI_EVPD) == 0 && device == T_DIRECT &&
560 qual == SID_QUAL_LU_OFFLINE)
561 qual = SID_QUAL_LU_CONNECTED;
562 ccb->csio.data_ptr[0] = (qual << 5) | T_NODEVICE;
564 } else if (ccb->ccb_h.status == CAM_SEL_TIMEOUT &&
565 ccb->ccb_h.target_lun != 0) {
566 /* fix for INQUIRYs on Lun>0 */
567 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
572 aac_cam_complete(struct aac_command *cm)
575 struct aac_srb_response *srbr;
576 struct aac_softc *sc;
580 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
581 ccb = cm->cm_private;
582 srbr = (struct aac_srb_response *)&cm->cm_fib->data[0];
584 if (srbr->fib_status != 0) {
585 device_printf(sc->aac_dev, "Passthru FIB failed!\n");
586 ccb->ccb_h.status = CAM_REQ_ABORTED;
589 * The SRB error codes just happen to match the CAM error
590 * codes. How convenient!
592 ccb->ccb_h.status = srbr->srb_status;
594 /* Take care of SCSI_IO ops. */
595 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
596 ccb->csio.scsi_status = srbr->scsi_status;
598 /* Take care of autosense */
599 if (srbr->sense_len) {
600 sense_returned = srbr->sense_len;
601 if (sense_returned < ccb->csio.sense_len)
602 ccb->csio.sense_resid =
603 ccb->csio.sense_len -
606 ccb->csio.sense_resid = 0;
607 bzero(&ccb->csio.sense_data,
608 sizeof(struct scsi_sense_data));
609 bcopy(&srbr->sense[0], &ccb->csio.sense_data,
610 min(ccb->csio.sense_len, sense_returned));
611 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
612 // scsi_sense_print(&ccb->csio);
615 aac_cam_fix_inquiry(sc, ccb);
619 aac_release_command(cm);
624 aac_cam_reset_bus(struct cam_sim *sim, union ccb *ccb)
627 struct aac_softc *sc;
628 struct aac_cam *camsc;
629 struct aac_vmioctl *vmi;
630 struct aac_resetbus *rbc;
633 camsc = (struct aac_cam *)cam_sim_softc(sim);
634 sc = camsc->inf->aac_sc;
637 printf("aac: Null sc?\n");
638 return (CAM_REQ_ABORTED);
641 aac_alloc_sync_fib(sc, &fib);
643 vmi = (struct aac_vmioctl *)&fib->data[0];
644 bzero(vmi, sizeof(struct aac_vmioctl));
646 vmi->Command = VM_Ioctl;
647 vmi->ObjType = FT_DRIVE;
648 vmi->MethId = sc->scsi_method_id;
650 vmi->IoctlCmd = ResetBus;
652 rbc = (struct aac_resetbus *)&vmi->IoctlBuf[0];
653 rbc->BusNumber = camsc->inf->BusNumber;
655 e = aac_sync_fib(sc, ContainerCommand, 0, fib,
656 sizeof(struct aac_vmioctl));
658 device_printf(sc->aac_dev,"Error %d sending ResetBus command\n",
660 aac_release_sync_fib(sc);
661 return (CAM_REQ_ABORTED);
664 aac_release_sync_fib(sc);
665 return (CAM_REQ_CMP);
669 aac_cam_abort_ccb(struct cam_sim *sim, union ccb *ccb)
671 return (CAM_UA_ABORT);
675 aac_cam_term_io(struct cam_sim *sim, union ccb *ccb)
677 return (CAM_UA_TERMIO);