2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 devclass_t aac_pass_devclass;
93 static device_method_t aac_pass_methods[] = {
94 DEVMETHOD(device_probe, aac_cam_probe),
95 DEVMETHOD(device_attach, aac_cam_attach),
96 DEVMETHOD(device_detach, aac_cam_detach),
100 static driver_t aac_pass_driver = {
103 sizeof(struct aac_cam)
106 DRIVER_MODULE(aacp, aac, aac_pass_driver, aac_pass_devclass, NULL, NULL);
107 MODULE_DEPEND(aacp, cam, 1, 1, 1);
109 static MALLOC_DEFINE(M_AACCAM, "aaccam", "AAC CAM info");
112 aac_cam_rescan(struct aac_softc *sc, uint32_t channel, uint32_t target_id)
116 struct aac_cam *camsc;
118 if (target_id == AAC_CAM_TARGET_WILDCARD)
119 target_id = CAM_TARGET_WILDCARD;
121 TAILQ_FOREACH(sim, &sc->aac_sim_tqh, sim_link) {
122 camsc = sim->aac_cam;
123 if (camsc == NULL || camsc->inf == NULL ||
124 camsc->inf->BusNumber != channel)
127 ccb = xpt_alloc_ccb_nowait();
129 device_printf(sc->aac_dev,
130 "Cannot allocate ccb for bus rescan.\n");
134 if (xpt_create_path(&ccb->ccb_h.path, NULL,
135 cam_sim_path(camsc->sim),
136 target_id, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
138 device_printf(sc->aac_dev,
139 "Cannot create path for bus rescan.\n");
148 aac_cam_event(struct aac_softc *sc, struct aac_event *event, void *arg)
151 struct aac_cam *camsc;
153 switch (event->ev_type) {
154 case AAC_EVENT_CMFREE:
156 camsc = ccb->ccb_h.sim_priv.entries[0].ptr;
157 free(event, M_AACCAM);
158 xpt_release_simq(camsc->sim, 1);
159 ccb->ccb_h.status = CAM_REQUEUE_REQ;
163 device_printf(sc->aac_dev, "unknown event %d in aac_cam\n",
170 aac_cam_probe(device_t dev)
172 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
178 aac_cam_detach(device_t dev)
180 struct aac_softc *sc;
181 struct aac_cam *camsc;
182 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
184 camsc = (struct aac_cam *)device_get_softc(dev);
185 sc = camsc->inf->aac_sc;
186 camsc->inf->aac_cam = NULL;
188 mtx_lock(&sc->aac_io_lock);
190 xpt_async(AC_LOST_DEVICE, camsc->path, NULL);
191 xpt_free_path(camsc->path);
192 xpt_bus_deregister(cam_sim_path(camsc->sim));
193 cam_sim_free(camsc->sim, /*free_devq*/TRUE);
195 sc->cam_rescan_cb = NULL;
197 mtx_unlock(&sc->aac_io_lock);
203 * Register the driver as a CAM SIM
206 aac_cam_attach(device_t dev)
208 struct cam_devq *devq;
210 struct cam_path *path;
211 struct aac_cam *camsc;
214 fwprintf(NULL, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
216 camsc = (struct aac_cam *)device_get_softc(dev);
217 inf = (struct aac_sim *)device_get_ivars(dev);
219 camsc->inf->aac_cam = camsc;
221 devq = cam_simq_alloc(inf->TargetsPerBus);
225 sim = cam_sim_alloc(aac_cam_action, aac_cam_poll, "aacp", camsc,
226 device_get_unit(dev), &inf->aac_sc->aac_io_lock, 1, 1, devq);
232 /* Since every bus has it's own sim, every bus 'appears' as bus 0 */
233 mtx_lock(&inf->aac_sc->aac_io_lock);
234 if (xpt_bus_register(sim, dev, 0) != CAM_SUCCESS) {
235 cam_sim_free(sim, TRUE);
236 mtx_unlock(&inf->aac_sc->aac_io_lock);
240 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
241 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
242 xpt_bus_deregister(cam_sim_path(sim));
243 cam_sim_free(sim, TRUE);
244 mtx_unlock(&inf->aac_sc->aac_io_lock);
247 inf->aac_sc->cam_rescan_cb = aac_cam_rescan;
248 mtx_unlock(&inf->aac_sc->aac_io_lock);
257 aac_cam_action(struct cam_sim *sim, union ccb *ccb)
259 struct aac_cam *camsc;
260 struct aac_softc *sc;
263 struct aac_command *cm;
265 camsc = (struct aac_cam *)cam_sim_softc(sim);
266 sc = camsc->inf->aac_sc;
267 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
269 /* Synchronous ops, and ops that don't require communication with the
271 switch(ccb->ccb_h.func_code) {
274 /* These are handled down below */
276 case XPT_CALC_GEOMETRY:
278 struct ccb_calc_geometry *ccg;
280 u_int32_t secs_per_cylinder;
283 size_mb = ccg->volume_size /
284 ((1024L * 1024L) / ccg->block_size);
285 if (size_mb >= (2 * 1024)) { /* 2GB */
287 ccg->secs_per_track = 63;
288 } else if (size_mb >= (1 * 1024)) { /* 1GB */
290 ccg->secs_per_track = 32;
293 ccg->secs_per_track = 32;
295 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
296 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
298 ccb->ccb_h.status = CAM_REQ_CMP;
304 struct ccb_pathinq *cpi = &ccb->cpi;
306 cpi->version_num = 1;
307 cpi->hba_inquiry = PI_WIDE_16;
308 cpi->target_sprt = 0;
311 * Resetting via the passthrough or parallel bus scan
314 cpi->hba_misc = PIM_NOBUSRESET | PIM_SEQSCAN;
315 cpi->hba_eng_cnt = 0;
316 cpi->max_target = camsc->inf->TargetsPerBus;
317 cpi->max_lun = 8; /* Per the controller spec */
318 cpi->initiator_id = camsc->inf->InitiatorBusId;
319 cpi->bus_id = camsc->inf->BusNumber;
320 cpi->base_transfer_speed = 3300;
321 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
322 strlcpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
323 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
324 cpi->unit_number = cam_sim_unit(sim);
325 cpi->transport = XPORT_SPI;
326 cpi->transport_version = 2;
327 cpi->protocol = PROTO_SCSI;
328 cpi->protocol_version = SCSI_REV_2;
329 ccb->ccb_h.status = CAM_REQ_CMP;
333 case XPT_GET_TRAN_SETTINGS:
335 struct ccb_trans_settings_scsi *scsi =
336 &ccb->cts.proto_specific.scsi;
337 struct ccb_trans_settings_spi *spi =
338 &ccb->cts.xport_specific.spi;
339 ccb->cts.protocol = PROTO_SCSI;
340 ccb->cts.protocol_version = SCSI_REV_2;
341 ccb->cts.transport = XPORT_SPI;
342 ccb->cts.transport_version = 2;
343 if (ccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
344 scsi->valid = CTS_SCSI_VALID_TQ;
345 spi->valid |= CTS_SPI_VALID_DISC;
349 ccb->ccb_h.status = CAM_REQ_CMP;
353 case XPT_SET_TRAN_SETTINGS:
354 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
358 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
359 ccb->ccb_h.status = aac_cam_reset_bus(sim, ccb);
361 ccb->ccb_h.status = CAM_REQ_CMP;
366 ccb->ccb_h.status = aac_cam_abort_ccb(sim, ccb);
370 ccb->ccb_h.status = aac_cam_term_io(sim, ccb);
374 device_printf(sc->aac_dev, "Unsupported command 0x%x\n",
375 ccb->ccb_h.func_code);
376 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
381 /* Async ops that require communcation with the controller */
383 if (aac_alloc_command(sc, &cm)) {
384 struct aac_event *event;
386 xpt_freeze_simq(sim, 1);
387 ccb->ccb_h.status = CAM_RESRC_UNAVAIL;
388 ccb->ccb_h.sim_priv.entries[0].ptr = camsc;
389 event = malloc(sizeof(struct aac_event), M_AACCAM,
392 device_printf(sc->aac_dev,
393 "Warning, out of memory for event\n");
396 event->ev_callback = aac_cam_event;
398 event->ev_type = AAC_EVENT_CMFREE;
399 aac_add_event(sc, event);
404 srb = (struct aac_srb *)&fib->data[0];
407 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
409 srb->flags = AAC_SRB_FLAGS_DATA_IN;
410 cm->cm_flags |= AAC_CMD_DATAIN;
413 srb->flags = AAC_SRB_FLAGS_DATA_OUT;
414 cm->cm_flags |= AAC_CMD_DATAOUT;
417 srb->flags = AAC_SRB_FLAGS_NO_DATA_XFER;
420 srb->flags = AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION;
421 cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT;
425 switch(ccb->ccb_h.func_code) {
428 struct ccb_scsiio *csio = &ccb->csio;
430 srb->function = AAC_SRB_FUNC_EXECUTE_SCSI;
433 * Copy the CDB into the SRB. It's only 6-16 bytes,
434 * so a copy is not too expensive.
436 srb->cdb_len = csio->cdb_len;
437 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
438 bcopy(csio->cdb_io.cdb_ptr, (u_int8_t *)&srb->cdb[0],
441 bcopy(csio->cdb_io.cdb_bytes, (u_int8_t *)&srb->cdb[0],
445 fib->Header.Command = (sc->flags & AAC_FLAGS_SG_64BIT) ?
446 ScsiPortCommandU64 : ScsiPortCommand;
448 /* Map the s/g list. XXX 32bit addresses only! */
449 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
450 switch ((ccb->ccb_h.flags & CAM_DATA_MASK)) {
452 srb->data_len = csio->dxfer_len;
454 * Arrange things so that the S/G
455 * map will get set up automagically
457 cm->cm_data = (void *)csio->data_ptr;
458 cm->cm_datalen = csio->dxfer_len;
459 cm->cm_sgtable = &srb->sg_map;
462 /* Send a 32bit command */
463 fib->Header.Command = ScsiPortCommand;
464 srb->sg_map.SgCount = 1;
465 srb->sg_map.SgEntry[0].SgAddress =
466 (uint32_t)(uintptr_t)csio->data_ptr;
467 srb->sg_map.SgEntry[0].SgByteCount =
469 srb->data_len = csio->dxfer_len;
472 /* XXX Need to handle multiple s/g elements */
473 panic("aac_cam: multiple s/g elements");
476 srb->sg_map.SgCount = 0;
477 srb->sg_map.SgEntry[0].SgByteCount = 0;
484 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
485 srb->function = AAC_SRB_FUNC_RESET_DEVICE;
488 ccb->ccb_h.status = CAM_REQ_CMP;
496 srb->bus = camsc->inf->BusNumber; /* Bus number relative to the card */
497 srb->target = ccb->ccb_h.target_id;
498 srb->lun = ccb->ccb_h.target_lun;
499 srb->timeout = ccb->ccb_h.timeout; /* XXX */
500 srb->retry_limit = 0;
502 cm->cm_complete = aac_cam_complete;
503 cm->cm_private = ccb;
504 cm->cm_timestamp = time_uptime;
506 fib->Header.XferState =
507 AAC_FIBSTATE_HOSTOWNED |
508 AAC_FIBSTATE_INITIALISED |
509 AAC_FIBSTATE_FROMHOST |
510 AAC_FIBSTATE_REXPECTED |
512 fib->Header.Size = sizeof(struct aac_fib_header) +
513 sizeof(struct aac_srb);
515 aac_enqueue_ready(cm);
516 aac_startio(cm->cm_sc);
520 aac_cam_poll(struct cam_sim *sim)
523 * Pinging the interrupt routine isn't very safe, nor is it
524 * really necessary. Do nothing.
529 aac_cam_fix_inquiry(struct aac_softc *sc, union ccb *ccb)
531 struct scsi_inquiry_data *inq;
533 uint8_t device, qual;
535 /* If this is an inquiry command, fake things out */
536 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
537 data = ccb->csio.cdb_io.cdb_ptr;
539 data = ccb->csio.cdb_io.cdb_bytes;
541 if (data[0] != INQUIRY)
544 if (ccb->ccb_h.status == CAM_REQ_CMP) {
545 inq = (struct scsi_inquiry_data *)ccb->csio.data_ptr;
546 device = SID_TYPE(inq);
547 qual = SID_QUAL(inq);
550 * We want DASD and PROC devices to only be
551 * visible through the pass device.
553 if (((device == T_DIRECT) ||
554 (device == T_PROCESSOR) ||
555 (sc->flags & AAC_FLAGS_CAM_PASSONLY))) {
557 * Some aac(4) adapters will always report that a direct
558 * access device is offline in response to a INQUIRY
559 * command that does not retrieve vital product data.
560 * Force the qualifier to connected so that upper layers
561 * correctly recognize that a disk is present.
563 if ((data[1] & SI_EVPD) == 0 && device == T_DIRECT &&
564 qual == SID_QUAL_LU_OFFLINE)
565 qual = SID_QUAL_LU_CONNECTED;
566 ccb->csio.data_ptr[0] = (qual << 5) | T_NODEVICE;
568 } else if (ccb->ccb_h.status == CAM_SEL_TIMEOUT &&
569 ccb->ccb_h.target_lun != 0) {
570 /* fix for INQUIRYs on Lun>0 */
571 ccb->ccb_h.status = CAM_DEV_NOT_THERE;
576 aac_cam_complete(struct aac_command *cm)
579 struct aac_srb_response *srbr;
580 struct aac_softc *sc;
584 fwprintf(sc, HBA_FLAGS_DBG_FUNCTION_ENTRY_B, "");
585 ccb = cm->cm_private;
586 srbr = (struct aac_srb_response *)&cm->cm_fib->data[0];
588 if (srbr->fib_status != 0) {
589 device_printf(sc->aac_dev, "Passthru FIB failed!\n");
590 ccb->ccb_h.status = CAM_REQ_ABORTED;
593 * The SRB error codes just happen to match the CAM error
594 * codes. How convenient!
596 ccb->ccb_h.status = srbr->srb_status;
598 /* Take care of SCSI_IO ops. */
599 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
600 ccb->csio.scsi_status = srbr->scsi_status;
602 /* Take care of autosense */
603 if (srbr->sense_len) {
604 sense_returned = srbr->sense_len;
605 if (sense_returned < ccb->csio.sense_len)
606 ccb->csio.sense_resid =
607 ccb->csio.sense_len -
610 ccb->csio.sense_resid = 0;
611 bzero(&ccb->csio.sense_data,
612 sizeof(struct scsi_sense_data));
613 bcopy(&srbr->sense[0], &ccb->csio.sense_data,
614 min(ccb->csio.sense_len, sense_returned));
615 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
616 // scsi_sense_print(&ccb->csio);
619 aac_cam_fix_inquiry(sc, ccb);
623 aac_release_command(cm);
628 aac_cam_reset_bus(struct cam_sim *sim, union ccb *ccb)
631 struct aac_softc *sc;
632 struct aac_cam *camsc;
633 struct aac_vmioctl *vmi;
634 struct aac_resetbus *rbc;
637 camsc = (struct aac_cam *)cam_sim_softc(sim);
638 sc = camsc->inf->aac_sc;
641 printf("aac: Null sc?\n");
642 return (CAM_REQ_ABORTED);
645 aac_alloc_sync_fib(sc, &fib);
647 vmi = (struct aac_vmioctl *)&fib->data[0];
648 bzero(vmi, sizeof(struct aac_vmioctl));
650 vmi->Command = VM_Ioctl;
651 vmi->ObjType = FT_DRIVE;
652 vmi->MethId = sc->scsi_method_id;
654 vmi->IoctlCmd = ResetBus;
656 rbc = (struct aac_resetbus *)&vmi->IoctlBuf[0];
657 rbc->BusNumber = camsc->inf->BusNumber;
659 e = aac_sync_fib(sc, ContainerCommand, 0, fib,
660 sizeof(struct aac_vmioctl));
662 device_printf(sc->aac_dev,"Error %d sending ResetBus command\n",
664 aac_release_sync_fib(sc);
665 return (CAM_REQ_ABORTED);
668 aac_release_sync_fib(sc);
669 return (CAM_REQ_CMP);
673 aac_cam_abort_ccb(struct cam_sim *sim, union ccb *ccb)
675 return (CAM_UA_ABORT);
679 aac_cam_term_io(struct cam_sim *sim, union ccb *ccb)
681 return (CAM_UA_TERMIO);