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.10 2008/05/18 20:30:23 pavalos 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>
41 #include <sys/module.h>
43 #include <bus/cam/cam.h>
44 #include <bus/cam/cam_ccb.h>
45 #include <bus/cam/cam_debug.h>
46 #include <bus/cam/cam_sim.h>
47 #include <bus/cam/cam_xpt_sim.h>
48 #include <bus/cam/scsi/scsi_all.h>
49 #include <bus/cam/scsi/scsi_message.h>
53 #include <sys/devicestat.h>
57 #include <machine/md_var.h>
59 #include <sys/mplock2.h>
65 #include "aac_ioctl.h"
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 *);
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_event(struct aac_softc *sc, struct aac_event *event, void *arg)
108 struct aac_cam *camsc;
110 switch (event->ev_type) {
111 case AAC_EVENT_CMFREE:
113 kfree(event, M_AACCAM);
114 xpt_release_simq(camsc->sim, 1);
117 device_printf(sc->aac_dev, "unknown event %d in aac_cam\n",
126 aac_cam_probe(device_t dev)
134 aac_cam_detach(device_t dev)
136 struct aac_cam *camsc;
139 camsc = (struct aac_cam *)device_get_softc(dev);
143 xpt_async(AC_LOST_DEVICE, camsc->path, NULL);
144 xpt_free_path(camsc->path);
145 xpt_bus_deregister(cam_sim_path(camsc->sim));
146 cam_sim_free(camsc->sim);
154 * Register the driver as a CAM SIM
157 aac_cam_attach(device_t dev)
159 struct cam_devq *devq;
161 struct cam_path *path;
162 struct aac_cam *camsc;
167 camsc = (struct aac_cam *)device_get_softc(dev);
168 inf = (struct aac_sim *)device_get_ivars(dev);
171 devq = cam_simq_alloc(inf->TargetsPerBus);
175 sim = cam_sim_alloc(aac_cam_action, aac_cam_poll, "aacp", camsc,
176 device_get_unit(dev), &sim_mplock, 1, 1, devq);
177 cam_simq_release(devq);
182 /* Since every bus has it's own sim, every bus 'appears' as bus 0 */
183 if (xpt_bus_register(sim, 0) != CAM_SUCCESS) {
188 if (xpt_create_path(&path, NULL, cam_sim_path(sim),
189 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
190 xpt_bus_deregister(cam_sim_path(sim));
202 aac_cam_action(struct cam_sim *sim, union ccb *ccb)
204 struct aac_cam *camsc;
205 struct aac_softc *sc;
206 struct aac_srb32 *srb;
208 struct aac_command *cm;
212 camsc = (struct aac_cam *)cam_sim_softc(sim);
213 sc = camsc->inf->aac_sc;
215 /* Synchronous ops, and ops that don't require communication with the
217 switch(ccb->ccb_h.func_code) {
220 /* These are handled down below */
222 case XPT_CALC_GEOMETRY:
224 struct ccb_calc_geometry *ccg;
226 u_int32_t secs_per_cylinder;
229 size_mb = ccg->volume_size /
230 ((1024L * 1024L) / ccg->block_size);
231 if (size_mb >= (2 * 1024)) { /* 2GB */
233 ccg->secs_per_track = 63;
234 } else if (size_mb >= (1 * 1024)) { /* 1GB */
236 ccg->secs_per_track = 32;
239 ccg->secs_per_track = 32;
241 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
242 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
244 ccb->ccb_h.status = CAM_REQ_CMP;
250 struct ccb_pathinq *cpi = &ccb->cpi;
252 cpi->version_num = 1;
253 cpi->hba_inquiry = PI_WIDE_16;
254 cpi->target_sprt = 0;
256 /* Resetting via the passthrough causes problems. */
257 cpi->hba_misc = PIM_NOBUSRESET;
258 cpi->hba_eng_cnt = 0;
259 cpi->max_target = camsc->inf->TargetsPerBus;
260 cpi->max_lun = 8; /* Per the controller spec */
261 cpi->initiator_id = camsc->inf->InitiatorBusId;
262 cpi->bus_id = camsc->inf->BusNumber;
263 cpi->base_transfer_speed = 3300;
264 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
265 strncpy(cpi->hba_vid, "Adaptec", HBA_IDLEN);
266 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
267 cpi->unit_number = cam_sim_unit(sim);
268 cpi->transport = XPORT_SPI;
269 cpi->transport_version = 2;
270 cpi->protocol = PROTO_SCSI;
271 cpi->protocol_version = SCSI_REV_2;
272 ccb->ccb_h.status = CAM_REQ_CMP;
276 case XPT_GET_TRAN_SETTINGS:
278 struct ccb_trans_settings_scsi *scsi =
279 &ccb->cts.proto_specific.scsi;
280 struct ccb_trans_settings_spi *spi =
281 &ccb->cts.xport_specific.spi;
282 ccb->cts.protocol = PROTO_SCSI;
283 ccb->cts.protocol_version = SCSI_REV_2;
284 ccb->cts.transport = XPORT_SPI;
285 ccb->cts.transport_version = 2;
286 if (ccb->ccb_h.target_lun != CAM_LUN_WILDCARD) {
287 scsi->valid = CTS_SCSI_VALID_TQ;
288 spi->valid |= CTS_SPI_VALID_DISC;
292 ccb->ccb_h.status = CAM_REQ_CMP;
296 case XPT_SET_TRAN_SETTINGS:
297 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
301 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
302 ccb->ccb_h.status = aac_cam_reset_bus(sim, ccb);
304 ccb->ccb_h.status = CAM_REQ_CMP;
309 ccb->ccb_h.status = aac_cam_abort_ccb(sim, ccb);
313 ccb->ccb_h.status = aac_cam_term_io(sim, ccb);
317 device_printf(sc->aac_dev, "Unsupported command 0x%x\n",
318 ccb->ccb_h.func_code);
319 ccb->ccb_h.status = CAM_PROVIDE_FAIL;
324 /* Async ops that require communcation with the controller */
326 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
327 if (aac_alloc_command(sc, &cm)) {
328 struct aac_event *event;
330 xpt_freeze_simq(sim, 1);
331 ccb->ccb_h.status = CAM_REQUEUE_REQ;
333 event = kmalloc(sizeof(struct aac_event), M_AACCAM,
335 event->ev_callback = aac_cam_event;
336 event->ev_arg = camsc;
337 event->ev_type = AAC_EVENT_CMFREE;
338 aac_add_event(sc, event);
339 AAC_LOCK_RELEASE(&sc->aac_io_lock);
344 srb = (struct aac_srb32 *)&fib->data[0];
347 switch (ccb->ccb_h.flags & CAM_DIR_MASK) {
349 srb->flags = AAC_SRB_FLAGS_DATA_IN;
350 cm->cm_flags |= AAC_CMD_DATAIN;
353 srb->flags = AAC_SRB_FLAGS_DATA_OUT;
354 cm->cm_flags |= AAC_CMD_DATAOUT;
357 srb->flags = AAC_SRB_FLAGS_NO_DATA_XFER;
360 srb->flags = AAC_SRB_FLAGS_UNSPECIFIED_DIRECTION;
361 cm->cm_flags |= AAC_CMD_DATAIN | AAC_CMD_DATAOUT;
365 switch(ccb->ccb_h.func_code) {
368 struct ccb_scsiio *csio = &ccb->csio;
370 srb->function = AAC_SRB_FUNC_EXECUTE_SCSI;
373 * Copy the CDB into the SRB. It's only 6-16 bytes,
374 * so a copy is not too expensive.
376 srb->cdb_len = csio->cdb_len;
377 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
378 bcopy(csio->cdb_io.cdb_ptr, (u_int8_t *)&srb->cdb[0],
381 bcopy(csio->cdb_io.cdb_bytes, (u_int8_t *)&srb->cdb[0],
384 /* Map the s/g list. XXX 32bit addresses only! */
385 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
386 if ((ccb->ccb_h.flags & CAM_SCATTER_VALID) == 0) {
387 srb->data_len = csio->dxfer_len;
388 if (ccb->ccb_h.flags & CAM_DATA_PHYS) {
390 * XXX This isn't 64-bit clean.
391 * However, this condition is not
392 * normally used in CAM.
394 srb->sg_map32.SgCount = 1;
395 srb->sg_map32.SgEntry[0].SgAddress =
396 (uint32_t)(uintptr_t)csio->data_ptr;
397 srb->sg_map32.SgEntry[0].SgByteCount =
401 * Arrange things so that the S/G
402 * map will get set up automagically
404 cm->cm_data = (void *)csio->data_ptr;
405 cm->cm_datalen = csio->dxfer_len;
406 cm->cm_sgtable = &srb->sg_map32;
409 /* XXX Need to handle multiple s/g elements */
410 panic("aac_cam: multiple s/g elements");
413 srb->sg_map32.SgCount = 0;
414 srb->sg_map32.SgEntry[0].SgByteCount = 0;
421 if (!(sc->flags & AAC_FLAGS_CAM_NORESET)) {
422 srb->function = AAC_SRB_FUNC_RESET_DEVICE;
425 ccb->ccb_h.status = CAM_REQ_CMP;
427 AAC_LOCK_RELEASE(&sc->aac_io_lock);
434 srb->bus = camsc->inf->BusNumber; /* Bus number relative to the card */
435 srb->target = ccb->ccb_h.target_id;
436 srb->lun = ccb->ccb_h.target_lun;
437 srb->timeout = ccb->ccb_h.timeout; /* XXX */
438 srb->retry_limit = 0;
440 cm->cm_complete = aac_cam_complete;
441 cm->cm_private = ccb;
442 cm->cm_timestamp = time_second;
443 cm->cm_queue = AAC_ADAP_NORM_CMD_QUEUE;
445 fib->Header.XferState =
446 AAC_FIBSTATE_HOSTOWNED |
447 AAC_FIBSTATE_INITIALISED |
448 AAC_FIBSTATE_FROMHOST |
449 AAC_FIBSTATE_REXPECTED |
451 fib->Header.Command = ScsiPortCommand;
452 fib->Header.Size = sizeof(struct aac_fib_header) +
453 sizeof(struct aac_srb32);
455 aac_enqueue_ready(cm);
456 aac_startio(cm->cm_sc);
458 AAC_LOCK_RELEASE(&sc->aac_io_lock);
464 aac_cam_poll(struct cam_sim *sim)
467 * Pinging the interrupt routine isn't very safe, nor is it
468 * really necessary. Do nothing.
473 aac_cam_complete(struct aac_command *cm)
476 struct aac_srb_response *srbr;
477 struct aac_softc *sc;
482 ccb = cm->cm_private;
483 srbr = (struct aac_srb_response *)&cm->cm_fib->data[0];
485 if (srbr->fib_status != 0) {
486 device_printf(sc->aac_dev, "Passthru FIB failed!\n");
487 ccb->ccb_h.status = CAM_REQ_ABORTED;
490 * The SRB error codes just happen to match the CAM error
491 * codes. How convienient!
493 ccb->ccb_h.status = srbr->srb_status;
495 /* Take care of SCSI_IO ops. */
496 if (ccb->ccb_h.func_code == XPT_SCSI_IO) {
497 u_int8_t command, device;
499 ccb->csio.scsi_status = srbr->scsi_status;
501 /* Take care of autosense */
502 if (srbr->sense_len) {
503 int sense_len, scsi_sense_len;
505 scsi_sense_len = sizeof(struct scsi_sense_data);
506 bzero(&ccb->csio.sense_data, scsi_sense_len);
507 sense_len = (srbr->sense_len >
508 scsi_sense_len) ? scsi_sense_len :
510 bcopy(&srbr->sense[0], &ccb->csio.sense_data,
512 ccb->csio.sense_len = sense_len;
513 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
514 /* scsi_sense_print(&ccb->csio); */
517 /* If this is an inquiry command, fake things out */
518 if (ccb->ccb_h.flags & CAM_CDB_POINTER)
519 command = ccb->csio.cdb_io.cdb_ptr[0];
521 command = ccb->csio.cdb_io.cdb_bytes[0];
523 if ((command == INQUIRY) &&
524 (ccb->ccb_h.status == CAM_REQ_CMP)) {
525 device = ccb->csio.data_ptr[0] & 0x1f;
527 * We want DASD and PROC devices to only be
528 * visible through the pass device.
530 if ((device == T_DIRECT) ||
531 (device == T_PROCESSOR) ||
532 (sc->flags & AAC_FLAGS_CAM_PASSONLY))
533 ccb->csio.data_ptr[0] =
534 ((device & 0xe0) | T_NODEVICE);
539 aac_release_command(cm);
546 aac_cam_reset_bus(struct cam_sim *sim, union ccb *ccb)
549 struct aac_softc *sc;
550 struct aac_cam *camsc;
551 struct aac_vmioctl *vmi;
552 struct aac_resetbus *rbc;
555 camsc = (struct aac_cam *)cam_sim_softc(sim);
556 sc = camsc->inf->aac_sc;
559 kprintf("Null sc?\n");
560 return (CAM_REQ_ABORTED);
563 AAC_LOCK_ACQUIRE(&sc->aac_io_lock);
564 aac_alloc_sync_fib(sc, &fib);
566 vmi = (struct aac_vmioctl *)&fib->data[0];
567 bzero(vmi, sizeof(struct aac_vmioctl));
569 vmi->Command = VM_Ioctl;
570 vmi->ObjType = FT_DRIVE;
571 vmi->MethId = sc->scsi_method_id;
573 vmi->IoctlCmd = ResetBus;
575 rbc = (struct aac_resetbus *)&vmi->IoctlBuf[0];
576 rbc->BusNumber = camsc->inf->BusNumber;
578 e = aac_sync_fib(sc, ContainerCommand, 0, fib,
579 sizeof(struct aac_vmioctl));
581 device_printf(sc->aac_dev,"Error %d sending ResetBus command\n",
583 aac_release_sync_fib(sc);
584 AAC_LOCK_RELEASE(&sc->aac_io_lock);
585 return (CAM_REQ_ABORTED);
588 aac_release_sync_fib(sc);
589 AAC_LOCK_RELEASE(&sc->aac_io_lock);
590 return (CAM_REQ_CMP);
594 aac_cam_abort_ccb(struct cam_sim *sim, union ccb *ccb)
596 return (CAM_UA_ABORT);
600 aac_cam_term_io(struct cam_sim *sim, union ccb *ccb)
602 return (CAM_UA_TERMIO);