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.2.2.4 2003/04/08 13:22:08 scottl Exp $
27 * $DragonFly: src/sys/dev/raid/aac/aac_cam.c,v 1.5 2006/10/25 20:56:00 dillon Exp $
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>
40 #include <sys/malloc.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_sim.h>
46 #include <bus/cam/cam_xpt_sim.h>
47 #include <bus/cam/scsi/scsi_all.h>
48 #include <bus/cam/scsi/scsi_message.h>
50 #include "aac_compat.h"
53 #include <sys/devicestat.h>
57 #include <machine/md_var.h>
63 #include "aac_ioctl.h"
69 struct aac_cam_inf *inf;
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 u_int32_t aac_cam_reset_bus(struct cam_sim *, union ccb *);
81 static u_int32_t aac_cam_abort_ccb(struct cam_sim *, union ccb *);
82 static u_int32_t aac_cam_term_io(struct cam_sim *, union ccb *);
83 static int aac_cam_get_tran_settings(struct aac_softc *, struct ccb_trans_settings *, u_int32_t);
85 static devclass_t aac_pass_devclass;
87 static device_method_t aac_pass_methods[] = {
88 DEVMETHOD(device_probe, aac_cam_probe),
89 DEVMETHOD(device_attach, aac_cam_attach),
90 DEVMETHOD(device_detach, aac_cam_detach),
94 static driver_t aac_pass_driver = {
97 sizeof(struct aac_cam)
100 DRIVER_MODULE(aacp, aac, aac_pass_driver, aac_pass_devclass, 0, 0);
101 MODULE_DEPEND(aacp, cam, 1, 1, 1);
103 MALLOC_DEFINE(M_AACCAM, "aaccam", "AAC CAM info");
106 aac_cam_probe(device_t dev)
115 aac_cam_detach(device_t dev)
122 * Register the driver as a CAM SIM
125 aac_cam_attach(device_t dev)
127 struct cam_devq *devq;
129 struct cam_path *path;
130 struct aac_cam *camsc;
131 struct aac_cam_inf *inf;
135 camsc = (struct aac_cam *)device_get_softc(dev);
136 inf = (struct aac_cam_inf *)device_get_ivars(dev);
139 devq = cam_simq_alloc(inf->TargetsPerBus);
143 sim = cam_sim_alloc(aac_cam_action, aac_cam_poll, "aacp", camsc,
144 device_get_unit(dev), 1, 1, devq);
145 cam_simq_release(devq);
150 /* Since every bus has it's own sim, every bus 'appears' as bus 0 */
151 if (xpt_bus_register(sim, 0) != CAM_SUCCESS) {
156 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
157 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
158 xpt_bus_deregister(cam_sim_path(sim));
170 aac_cam_action(struct cam_sim *sim, union ccb *ccb)
172 struct aac_cam *camsc;
173 struct aac_softc *sc;
174 struct aac_srb32 *srb;
176 struct aac_command *cm;
180 camsc = (struct aac_cam *)cam_sim_softc(sim);
181 sc = camsc->inf->aac_sc;
183 /* Synchronous ops, and ops that don't require communication with the
185 switch(ccb->ccb_h.func_code) {
188 /* These are handled down below */
190 case XPT_CALC_GEOMETRY:
192 struct ccb_calc_geometry *ccg;
194 u_int32_t secs_per_cylinder;
197 size_mb = ccg->volume_size /
198 ((1024L * 1024L) / ccg->block_size);
199 if (size_mb >= (2 * 1024)) { /* 2GB */
201 ccg->secs_per_track = 63;
202 } else if (size_mb >= (1 * 1024)) { /* 1GB */
204 ccg->secs_per_track = 32;
207 ccg->secs_per_track = 32;
209 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
210 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
212 ccb->ccb_h.status = CAM_REQ_CMP;
218 struct ccb_pathinq *cpi = &ccb->cpi;
220 cpi->version_num = 1;
221 cpi->hba_inquiry = PI_WIDE_16;
222 cpi->target_sprt = 0;
223 cpi->hba_misc = PIM_NOBUSRESET;
224 cpi->hba_eng_cnt = 0;
225 cpi->max_target = camsc->inf->TargetsPerBus;
226 cpi->max_lun = 8; /* Per the controller spec */
227 cpi->initiator_id = camsc->inf->InitiatorBusId;
228 cpi->bus_id = camsc->inf->BusNumber;
229 cpi->base_transfer_speed = 3300;
230 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
231 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
232 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
233 cpi->unit_number = cam_sim_unit(sim);
235 ccb->ccb_h.status = CAM_REQ_CMP;
239 case XPT_GET_TRAN_SETTINGS:
243 handle = AAC_BTL_TO_HANDLE(camsc->inf->BusNumber,
244 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
245 ccb->ccb_h.status = aac_cam_get_tran_settings(sc, &ccb->cts,
250 case XPT_SET_TRAN_SETTINGS:
251 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
255 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
256 ccb->ccb_h.status = aac_cam_reset_bus(sim, ccb);
258 ccb->ccb_h.status = CAM_REQ_CMP;
263 ccb->ccb_h.status = aac_cam_abort_ccb(sim, ccb);
267 ccb->ccb_h.status = aac_cam_term_io(sim, ccb);
271 device_printf(sc->aac_dev, "Unsupported command 0x%x\n",
272 ccb->ccb_h.func_code);
273 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
278 /* Async ops that require communcation with the controller */
280 if (aac_alloc_command(sc, &cm)) {
281 xpt_freeze_simq(sim, 1);
282 ccb->ccb_h.status = CAM_REQUEUE_REQ;
288 srb = (struct aac_srb32 *)&fib->data[0];
291 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
293 srb->flags = AAC_SRB_FLAGS_DATA_IN;
294 cm->cm_flags |= AAC_CMD_DATAIN;
297 srb->flags = AAC_SRB_FLAGS_DATA_OUT;
298 cm->cm_flags |= AAC_CMD_DATAOUT;
301 srb->flags = AAC_SRB_FLAGS_NO_DATA_XFER;
304 srb->flags = AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION;
305 cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT;
309 switch(ccb->ccb_h.func_code) {
312 struct ccb_scsiio *csio = &ccb->csio;
314 srb->function = AAC_SRB_FUNC_EXECUTE_SCSI;
317 * Copy the CDB into the SRB. It's only 6-16 bytes,
318 * so a copy is not too expensive.
320 srb->cdb_len = csio->cdb_len;
321 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
322 bcopy(csio->cdb_io.cdb_ptr, (u_int8_t *)&srb->cdb[0],
325 bcopy(csio->cdb_io.cdb_bytes, (u_int8_t *)&srb->cdb[0],
328 /* Map the s/g list. XXX 32bit addresses only! */
329 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
330 if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
331 srb->data_len = csio->dxfer_len;
332 if (ccb->ccb_h.flags & CAM_DATA_PHYS) {
333 srb->sg_map32.SgCount = 1;
334 srb->sg_map32.SgEntry[0].SgAddress =
335 (u_int32_t)csio->data_ptr;
336 srb->sg_map32.SgEntry[0].SgByteCount =
340 * Arrange things so that the S/G
341 * map will get set up automagically
343 cm->cm_data = (void *)csio->data_ptr;
344 cm->cm_datalen = csio->dxfer_len;
345 cm->cm_sgtable = &srb->sg_map32;
348 /* XXX Need to handle multiple s/g elements */
349 panic("aac_cam: multiple s/g elements");
352 srb->sg_map32.SgCount = 0;
353 srb->sg_map32.SgEntry[0].SgByteCount = 0;
360 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
361 srb->function = AAC_SRB_FUNC_RESET_DEVICE;
364 ccb->ccb_h.status = CAM_REQ_CMP;
372 srb->bus = camsc->inf->BusNumber; /* Bus number relative to the card */
373 srb->target = ccb->ccb_h.target_id;
374 srb->lun = ccb->ccb_h.target_lun;
375 srb->timeout = ccb->ccb_h.timeout; /* XXX */
376 srb->retry_limit = 0;
378 cm->cm_complete = aac_cam_complete;
379 cm->cm_private = ccb;
380 cm->cm_timestamp = time_second;
381 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
383 fib->Header.XferState =
384 AAC_FIBSTATE_HOSTOWNED |
385 AAC_FIBSTATE_INITIALISED |
386 AAC_FIBSTATE_FROMHOST |
387 AAC_FIBSTATE_REXPECTED |
389 fib->Header.Command = ScsiPortCommand;
390 fib->Header.Size = sizeof(struct aac_fib_header) +
391 sizeof(struct aac_srb32);
393 aac_enqueue_ready(cm);
394 aac_startio(cm->cm_sc);
400 aac_cam_poll(struct cam_sim *sim)
403 * Pinging the interrupt routine isn't very safe, nor is it
404 * really necessary. Do nothing.
409 aac_cam_complete(struct aac_command *cm)
412 struct aac_srb_response *srbr;
413 struct aac_softc *sc;
418 ccb = cm->cm_private;
419 srbr = (struct aac_srb_response *)&cm->cm_fib->data[0];
421 if (srbr->fib_status != 0) {
422 device_printf(sc->aac_dev, "Passthru FIB failed!\n");
423 ccb->ccb_h.status = CAM_REQ_ABORTED;
426 * The SRB error codes just happen to match the CAM error
427 * codes. How convienient!
429 ccb->ccb_h.status = srbr->srb_status;
431 /* Take care of SCSI_IO ops. */
432 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
433 u_int8_t command, device;
435 ccb->csio.scsi_status = srbr->scsi_status;
437 /* Take care of autosense */
438 if (srbr->sense_len) {
439 int sense_len, scsi_sense_len;
441 scsi_sense_len = sizeof(struct scsi_sense_data);
442 bzero(&ccb->csio.sense_data, scsi_sense_len);
443 sense_len = (srbr->sense_len >
444 scsi_sense_len) ? scsi_sense_len :
446 bcopy(&srbr->sense[0], &ccb->csio.sense_data,
448 ccb->csio.sense_len = sense_len;
449 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
450 scsi_sense_print(&ccb->csio);
453 /* If this is an inquiry command, fake things out */
454 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
455 command = ccb->csio.cdb_io.cdb_ptr[0];
457 command = ccb->csio.cdb_io.cdb_bytes[0];
459 if ((command == INQUIRY) &&
460 (ccb->ccb_h.status == CAM_REQ_CMP)) {
461 device = ccb->csio.data_ptr[0] & 0x1f;
463 * We want DASD and PROC devices to only be
464 * visible through the pass device.
466 if ((device == T_DIRECT) ||
467 (device == T_PROCESSOR) ||
468 (sc->flags & AAC_FLAGS_CAM_PASSONLY))
469 ccb->csio.data_ptr[0] =
470 ((device & 0xe0) | T_NODEVICE);
475 aac_release_command(cm);
483 aac_cam_reset_bus(struct cam_sim *sim, union ccb *ccb)
486 struct aac_softc *sc;
487 struct aac_cam *camsc;
488 struct aac_vmioctl *vmi;
489 struct aac_resetbus *rbc;
492 camsc = (struct aac_cam *)cam_sim_softc(sim);
493 sc = camsc->inf->aac_sc;
496 printf("Null sc?\n");
497 return (CAM_REQ_ABORTED);
500 aac_alloc_sync_fib(sc, &fib, 0);
502 vmi = (struct aac_vmioctl *)&fib->data[0];
503 bzero(vmi, sizeof(struct aac_vmioctl));
505 vmi->Command = VM_Ioctl;
506 vmi->ObjType = FT_DRIVE;
507 vmi->MethId = sc->scsi_method_id;
509 vmi->IoctlCmd = ResetBus;
511 rbc = (struct aac_resetbus *)&vmi->IoctlBuf[0];
512 rbc->BusNumber = camsc->inf->BusNumber;
514 e = aac_sync_fib(sc, ContainerCommand, 0, fib,
515 sizeof(struct aac_vmioctl));
517 device_printf(sc->aac_dev, "Error 0x%x sending passthrough\n",
519 aac_release_sync_fib(sc);
520 return (CAM_REQ_ABORTED);
523 aac_release_sync_fib(sc);
524 return (CAM_REQ_CMP);
528 aac_cam_abort_ccb(struct cam_sim *sim, union ccb *ccb)
530 return (CAM_UA_ABORT);
534 aac_cam_term_io(struct cam_sim *sim, union ccb *ccb)
536 return (CAM_UA_TERMIO);
540 aac_cam_get_tran_settings(struct aac_softc *sc, struct ccb_trans_settings *cts, u_int32_t handle)
543 struct aac_vmioctl *vmi;
544 struct aac_vmi_devinfo_resp *vmi_resp;
547 aac_alloc_sync_fib(sc, &fib, 0);
548 vmi = (struct aac_vmioctl *)&fib->data[0];
549 bzero(vmi, sizeof(struct aac_vmioctl));
551 vmi->Command = VM_Ioctl;
552 vmi->ObjType = FT_DRIVE;
553 vmi->MethId = sc->scsi_method_id;
555 vmi->IoctlCmd = GetDeviceProbeInfo;
557 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
558 sizeof(struct aac_vmioctl));
560 device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n",
562 aac_release_sync_fib(sc);
563 return (CAM_REQ_INVALID);
566 vmi_resp = (struct aac_vmi_devinfo_resp *)&fib->data[0];
567 if (vmi_resp->Status != ST_OK) {
568 debug(1, "VM_Ioctl returned %d\n", vmi_resp->Status);
569 aac_release_sync_fib(sc);
570 return (CAM_REQ_CMP_ERR);
573 cts->bus_width = ((vmi_resp->Inquiry7 & 0x60) >> 5);
574 if (vmi_resp->ScsiRate) {
576 scsi_calc_syncparam((10000 / vmi_resp->ScsiRate));
577 cts->sync_offset = vmi_resp->ScsiOffset;
579 cts->sync_period = 0;
580 cts->sync_offset = 0;
582 cts->flags &= ~(CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB);
583 cts->valid = CCB_TRANS_DISC_VALID |
584 CCB_TRANS_SYNC_RATE_VALID |
585 CCB_TRANS_SYNC_OFFSET_VALID |
586 CCB_TRANS_BUS_WIDTH_VALID |
589 aac_release_sync_fib(sc);
590 return (CAM_REQ_CMP);