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.4 2004/03/15 03:05:03 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>
56 #include <machine/md_var.h>
57 #include <machine/bus.h>
64 #include "aac_ioctl.h"
70 struct aac_cam_inf *inf;
72 struct cam_path *path;
75 static int aac_cam_probe(device_t dev);
76 static int aac_cam_attach(device_t dev);
77 static int aac_cam_detach(device_t dev);
78 static void aac_cam_action(struct cam_sim *, union ccb *);
79 static void aac_cam_poll(struct cam_sim *);
80 static void aac_cam_complete(struct aac_command *);
81 static u_int32_t aac_cam_reset_bus(struct cam_sim *, union ccb *);
82 static u_int32_t aac_cam_abort_ccb(struct cam_sim *, union ccb *);
83 static u_int32_t aac_cam_term_io(struct cam_sim *, union ccb *);
84 static int aac_cam_get_tran_settings(struct aac_softc *, struct ccb_trans_settings *, u_int32_t);
86 static devclass_t aac_pass_devclass;
88 static device_method_t aac_pass_methods[] = {
89 DEVMETHOD(device_probe, aac_cam_probe),
90 DEVMETHOD(device_attach, aac_cam_attach),
91 DEVMETHOD(device_detach, aac_cam_detach),
95 static driver_t aac_pass_driver = {
98 sizeof(struct aac_cam)
101 DRIVER_MODULE(aacp, aac, aac_pass_driver, aac_pass_devclass, 0, 0);
102 MODULE_DEPEND(aacp, cam, 1, 1, 1);
104 MALLOC_DEFINE(M_AACCAM, "aaccam", "AAC CAM info");
107 aac_cam_probe(device_t dev)
116 aac_cam_detach(device_t dev)
123 * Register the driver as a CAM SIM
126 aac_cam_attach(device_t dev)
128 struct cam_devq *devq;
130 struct cam_path *path;
131 struct aac_cam *camsc;
132 struct aac_cam_inf *inf;
136 camsc = (struct aac_cam *)device_get_softc(dev);
137 inf = (struct aac_cam_inf *)device_get_ivars(dev);
140 devq = cam_simq_alloc(inf->TargetsPerBus);
144 sim = cam_sim_alloc(aac_cam_action, aac_cam_poll, "aacp", camsc,
145 device_get_unit(dev), 1, 1, devq);
146 cam_simq_release(devq);
151 /* Since every bus has it's own sim, every bus 'appears' as bus 0 */
152 if (xpt_bus_register(sim, 0) != CAM_SUCCESS) {
157 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
158 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
159 xpt_bus_deregister(cam_sim_path(sim));
171 aac_cam_action(struct cam_sim *sim, union ccb *ccb)
173 struct aac_cam *camsc;
174 struct aac_softc *sc;
175 struct aac_srb32 *srb;
177 struct aac_command *cm;
181 camsc = (struct aac_cam *)cam_sim_softc(sim);
182 sc = camsc->inf->aac_sc;
184 /* Synchronous ops, and ops that don't require communication with the
186 switch(ccb->ccb_h.func_code) {
189 /* These are handled down below */
191 case XPT_CALC_GEOMETRY:
193 struct ccb_calc_geometry *ccg;
195 u_int32_t secs_per_cylinder;
198 size_mb = ccg->volume_size /
199 ((1024L * 1024L) / ccg->block_size);
200 if (size_mb >= (2 * 1024)) { /* 2GB */
202 ccg->secs_per_track = 63;
203 } else if (size_mb >= (1 * 1024)) { /* 1GB */
205 ccg->secs_per_track = 32;
208 ccg->secs_per_track = 32;
210 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
211 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
213 ccb->ccb_h.status = CAM_REQ_CMP;
219 struct ccb_pathinq *cpi = &ccb->cpi;
221 cpi->version_num = 1;
222 cpi->hba_inquiry = PI_WIDE_16;
223 cpi->target_sprt = 0;
224 cpi->hba_misc = PIM_NOBUSRESET;
225 cpi->hba_eng_cnt = 0;
226 cpi->max_target = camsc->inf->TargetsPerBus;
227 cpi->max_lun = 8; /* Per the controller spec */
228 cpi->initiator_id = camsc->inf->InitiatorBusId;
229 cpi->bus_id = camsc->inf->BusNumber;
230 cpi->base_transfer_speed = 3300;
231 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
232 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
233 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
234 cpi->unit_number = cam_sim_unit(sim);
236 ccb->ccb_h.status = CAM_REQ_CMP;
240 case XPT_GET_TRAN_SETTINGS:
244 handle = AAC_BTL_TO_HANDLE(camsc->inf->BusNumber,
245 ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
246 ccb->ccb_h.status = aac_cam_get_tran_settings(sc, &ccb->cts,
251 case XPT_SET_TRAN_SETTINGS:
252 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
256 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
257 ccb->ccb_h.status = aac_cam_reset_bus(sim, ccb);
259 ccb->ccb_h.status = CAM_REQ_CMP;
264 ccb->ccb_h.status = aac_cam_abort_ccb(sim, ccb);
268 ccb->ccb_h.status = aac_cam_term_io(sim, ccb);
272 device_printf(sc->aac_dev, "Unsupported command 0x%x\n",
273 ccb->ccb_h.func_code);
274 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
279 /* Async ops that require communcation with the controller */
281 if (aac_alloc_command(sc, &cm)) {
282 xpt_freeze_simq(sim, 1);
283 ccb->ccb_h.status = CAM_REQUEUE_REQ;
289 srb = (struct aac_srb32 *)&fib->data[0];
292 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
294 srb->flags = AAC_SRB_FLAGS_DATA_IN;
295 cm->cm_flags |= AAC_CMD_DATAIN;
298 srb->flags = AAC_SRB_FLAGS_DATA_OUT;
299 cm->cm_flags |= AAC_CMD_DATAOUT;
302 srb->flags = AAC_SRB_FLAGS_NO_DATA_XFER;
305 srb->flags = AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION;
306 cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT;
310 switch(ccb->ccb_h.func_code) {
313 struct ccb_scsiio *csio = &ccb->csio;
315 srb->function = AAC_SRB_FUNC_EXECUTE_SCSI;
318 * Copy the CDB into the SRB. It's only 6-16 bytes,
319 * so a copy is not too expensive.
321 srb->cdb_len = csio->cdb_len;
322 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
323 bcopy(csio->cdb_io.cdb_ptr, (u_int8_t *)&srb->cdb[0],
326 bcopy(csio->cdb_io.cdb_bytes, (u_int8_t *)&srb->cdb[0],
329 /* Map the s/g list. XXX 32bit addresses only! */
330 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
331 if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
332 srb->data_len = csio->dxfer_len;
333 if (ccb->ccb_h.flags & CAM_DATA_PHYS) {
334 srb->sg_map32.SgCount = 1;
335 srb->sg_map32.SgEntry[0].SgAddress =
336 (u_int32_t)csio->data_ptr;
337 srb->sg_map32.SgEntry[0].SgByteCount =
341 * Arrange things so that the S/G
342 * map will get set up automagically
344 cm->cm_data = (void *)csio->data_ptr;
345 cm->cm_datalen = csio->dxfer_len;
346 cm->cm_sgtable = &srb->sg_map32;
349 /* XXX Need to handle multiple s/g elements */
350 panic("aac_cam: multiple s/g elements");
353 srb->sg_map32.SgCount = 0;
354 srb->sg_map32.SgEntry[0].SgByteCount = 0;
361 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
362 srb->function = AAC_SRB_FUNC_RESET_DEVICE;
365 ccb->ccb_h.status = CAM_REQ_CMP;
373 srb->bus = camsc->inf->BusNumber; /* Bus number relative to the card */
374 srb->target = ccb->ccb_h.target_id;
375 srb->lun = ccb->ccb_h.target_lun;
376 srb->timeout = ccb->ccb_h.timeout; /* XXX */
377 srb->retry_limit = 0;
379 cm->cm_complete = aac_cam_complete;
380 cm->cm_private = ccb;
381 cm->cm_timestamp = time_second;
382 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
384 fib->Header.XferState =
385 AAC_FIBSTATE_HOSTOWNED |
386 AAC_FIBSTATE_INITIALISED |
387 AAC_FIBSTATE_FROMHOST |
388 AAC_FIBSTATE_REXPECTED |
390 fib->Header.Command = ScsiPortCommand;
391 fib->Header.Size = sizeof(struct aac_fib_header) +
392 sizeof(struct aac_srb32);
394 aac_enqueue_ready(cm);
395 aac_startio(cm->cm_sc);
401 aac_cam_poll(struct cam_sim *sim)
404 * Pinging the interrupt routine isn't very safe, nor is it
405 * really necessary. Do nothing.
410 aac_cam_complete(struct aac_command *cm)
413 struct aac_srb_response *srbr;
414 struct aac_softc *sc;
419 ccb = cm->cm_private;
420 srbr = (struct aac_srb_response *)&cm->cm_fib->data[0];
422 if (srbr->fib_status != 0) {
423 device_printf(sc->aac_dev, "Passthru FIB failed!\n");
424 ccb->ccb_h.status = CAM_REQ_ABORTED;
427 * The SRB error codes just happen to match the CAM error
428 * codes. How convienient!
430 ccb->ccb_h.status = srbr->srb_status;
432 /* Take care of SCSI_IO ops. */
433 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
434 u_int8_t command, device;
436 ccb->csio.scsi_status = srbr->scsi_status;
438 /* Take care of autosense */
439 if (srbr->sense_len) {
440 int sense_len, scsi_sense_len;
442 scsi_sense_len = sizeof(struct scsi_sense_data);
443 bzero(&ccb->csio.sense_data, scsi_sense_len);
444 sense_len = (srbr->sense_len >
445 scsi_sense_len) ? scsi_sense_len :
447 bcopy(&srbr->sense[0], &ccb->csio.sense_data,
449 ccb->csio.sense_len = sense_len;
450 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
451 scsi_sense_print(&ccb->csio);
454 /* If this is an inquiry command, fake things out */
455 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
456 command = ccb->csio.cdb_io.cdb_ptr[0];
458 command = ccb->csio.cdb_io.cdb_bytes[0];
460 if ((command == INQUIRY) &&
461 (ccb->ccb_h.status == CAM_REQ_CMP)) {
462 device = ccb->csio.data_ptr[0] & 0x1f;
464 * We want DASD and PROC devices to only be
465 * visible through the pass device.
467 if ((device == T_DIRECT) ||
468 (device == T_PROCESSOR) ||
469 (sc->flags & AAC_FLAGS_CAM_PASSONLY))
470 ccb->csio.data_ptr[0] =
471 ((device & 0xe0) | T_NODEVICE);
476 aac_release_command(cm);
484 aac_cam_reset_bus(struct cam_sim *sim, union ccb *ccb)
487 struct aac_softc *sc;
488 struct aac_cam *camsc;
489 struct aac_vmioctl *vmi;
490 struct aac_resetbus *rbc;
493 camsc = (struct aac_cam *)cam_sim_softc(sim);
494 sc = camsc->inf->aac_sc;
497 printf("Null sc?\n");
498 return (CAM_REQ_ABORTED);
501 aac_alloc_sync_fib(sc, &fib, 0);
503 vmi = (struct aac_vmioctl *)&fib->data[0];
504 bzero(vmi, sizeof(struct aac_vmioctl));
506 vmi->Command = VM_Ioctl;
507 vmi->ObjType = FT_DRIVE;
508 vmi->MethId = sc->scsi_method_id;
510 vmi->IoctlCmd = ResetBus;
512 rbc = (struct aac_resetbus *)&vmi->IoctlBuf[0];
513 rbc->BusNumber = camsc->inf->BusNumber;
515 e = aac_sync_fib(sc, ContainerCommand, 0, fib,
516 sizeof(struct aac_vmioctl));
518 device_printf(sc->aac_dev, "Error 0x%x sending passthrough\n",
520 aac_release_sync_fib(sc);
521 return (CAM_REQ_ABORTED);
524 aac_release_sync_fib(sc);
525 return (CAM_REQ_CMP);
529 aac_cam_abort_ccb(struct cam_sim *sim, union ccb *ccb)
531 return (CAM_UA_ABORT);
535 aac_cam_term_io(struct cam_sim *sim, union ccb *ccb)
537 return (CAM_UA_TERMIO);
541 aac_cam_get_tran_settings(struct aac_softc *sc, struct ccb_trans_settings *cts, u_int32_t handle)
544 struct aac_vmioctl *vmi;
545 struct aac_vmi_devinfo_resp *vmi_resp;
548 aac_alloc_sync_fib(sc, &fib, 0);
549 vmi = (struct aac_vmioctl *)&fib->data[0];
550 bzero(vmi, sizeof(struct aac_vmioctl));
552 vmi->Command = VM_Ioctl;
553 vmi->ObjType = FT_DRIVE;
554 vmi->MethId = sc->scsi_method_id;
556 vmi->IoctlCmd = GetDeviceProbeInfo;
558 error = aac_sync_fib(sc, ContainerCommand, 0, fib,
559 sizeof(struct aac_vmioctl));
561 device_printf(sc->aac_dev, "Error %d sending VMIoctl command\n",
563 aac_release_sync_fib(sc);
564 return (CAM_REQ_INVALID);
567 vmi_resp = (struct aac_vmi_devinfo_resp *)&fib->data[0];
568 if (vmi_resp->Status != ST_OK) {
569 debug(1, "VM_Ioctl returned %d\n", vmi_resp->Status);
570 aac_release_sync_fib(sc);
571 return (CAM_REQ_CMP_ERR);
574 cts->bus_width = ((vmi_resp->Inquiry7 & 0x60) >> 5);
575 if (vmi_resp->ScsiRate) {
577 scsi_calc_syncparam((10000 / vmi_resp->ScsiRate));
578 cts->sync_offset = vmi_resp->ScsiOffset;
580 cts->sync_period = 0;
581 cts->sync_offset = 0;
583 cts->flags &= ~(CCB_TRANS_DISC_ENB | CCB_TRANS_TAG_ENB);
584 cts->valid = CCB_TRANS_DISC_VALID |
585 CCB_TRANS_SYNC_RATE_VALID |
586 CCB_TRANS_SYNC_OFFSET_VALID |
587 CCB_TRANS_BUS_WIDTH_VALID |
590 aac_release_sync_fib(sc);
591 return (CAM_REQ_CMP);