2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * Copyright (c) 2002 Eric Moore
29 * Copyright (c) 2002 LSI Logic Corporation
30 * All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. The party using or redistributing the source code and binary forms
41 * agrees to the disclaimer below and the terms and conditions set forth
44 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * $FreeBSD: src/sys/dev/amr/amr_cam.c,v 1.28 2008/11/03 00:53:54 scottl Exp $
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/malloc.h>
62 #include <sys/kernel.h>
63 #include <sys/module.h>
70 #include <bus/cam/cam.h>
71 #include <bus/cam/cam_ccb.h>
72 #include <bus/cam/cam_sim.h>
73 #include <bus/cam/cam_xpt.h>
74 #include <bus/cam/cam_xpt_sim.h>
75 #include <bus/cam/cam_debug.h>
76 #include <bus/cam/scsi/scsi_all.h>
77 #include <bus/cam/scsi/scsi_message.h>
79 #include <dev/raid/amr/amrreg.h>
80 #include <dev/raid/amr/amrvar.h>
82 static int amr_cam_probe(device_t dev);
83 static int amr_cam_attach(device_t dev);
84 static int amr_cam_detach(device_t dev);
85 static void amr_cam_action(struct cam_sim *sim, union ccb *ccb);
86 static void amr_cam_poll(struct cam_sim *sim);
87 static void amr_cam_complete(struct amr_command *ac);
88 static int amr_cam_command(struct amr_softc *sc, struct amr_command **acp);
90 static devclass_t amr_pass_devclass;
92 static device_method_t amr_pass_methods[] = {
93 DEVMETHOD(device_probe, amr_cam_probe),
94 DEVMETHOD(device_attach, amr_cam_attach),
95 DEVMETHOD(device_detach, amr_cam_detach),
99 static driver_t amr_pass_driver = {
105 DRIVER_MODULE(amrp, amr, amr_pass_driver, amr_pass_devclass, 0, 0);
106 MODULE_DEPEND(amrp, cam, 1, 1, 1);
108 MALLOC_DEFINE(M_AMRCAM, "amrcam", "AMR CAM memory");
110 /***********************************************************************
111 * Enqueue/dequeue functions
114 amr_enqueue_ccb(struct amr_softc *sc, union ccb *ccb)
117 TAILQ_INSERT_TAIL(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
121 amr_requeue_ccb(struct amr_softc *sc, union ccb *ccb)
124 TAILQ_INSERT_HEAD(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
127 static __inline union ccb *
128 amr_dequeue_ccb(struct amr_softc *sc)
132 if ((ccb = (union ccb *)TAILQ_FIRST(&sc->amr_cam_ccbq)) != NULL)
133 TAILQ_REMOVE(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
138 amr_cam_probe(device_t dev)
143 /********************************************************************************
144 * Attach our 'real' SCSI channels to CAM
147 amr_cam_attach(device_t dev)
149 struct amr_softc *sc;
150 struct cam_devq *devq;
153 sc = device_get_softc(dev);
155 /* initialise the ccb queue */
156 TAILQ_INIT(&sc->amr_cam_ccbq);
159 * Allocate a devq for all our channels combined. This should
160 * allow for the maximum number of SCSI commands we will accept
161 * at one time. Save the pointer in the softc so we can find it later
164 if ((devq = cam_simq_alloc(AMR_MAX_SCSI_CMDS)) == NULL)
166 sc->amr_cam_devq = devq;
169 * Iterate over our channels, registering them with CAM
171 for (chn = 0; chn < sc->amr_maxchan; chn++) {
174 if ((sc->amr_cam_sim[chn] = cam_sim_alloc(amr_cam_action,
175 amr_cam_poll, "amr", sc, device_get_unit(sc->amr_dev),
176 &sc->amr_list_lock, 1, AMR_MAX_SCSI_CMDS, devq)) == NULL) {
177 device_printf(sc->amr_dev, "CAM SIM attach failed\n");
181 /* register the bus ID so we can get it later */
182 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
183 error = xpt_bus_register(sc->amr_cam_sim[chn], chn);
184 lockmgr(&sc->amr_list_lock, LK_RELEASE);
186 device_printf(sc->amr_dev,
187 "CAM XPT bus registration failed\n");
192 * XXX we should scan the config and work out which devices are
193 * actually protected.
195 sc->amr_cam_command = amr_cam_command;
199 /********************************************************************************
200 * Disconnect ourselves from CAM
203 amr_cam_detach(device_t dev)
205 struct amr_softc *sc;
208 sc = device_get_softc(dev);
209 lockmgr(&sc->amr_list_lock, LK_EXCLUSIVE);
210 for (chn = 0; chn < sc->amr_maxchan; chn++) {
212 * If a sim was allocated for this channel, free it
214 if (sc->amr_cam_sim[chn] != NULL) {
215 xpt_bus_deregister(cam_sim_path(sc->amr_cam_sim[chn]));
216 cam_sim_free(sc->amr_cam_sim[chn]);
219 lockmgr(&sc->amr_list_lock, LK_RELEASE);
224 /***********************************************************************
225 ***********************************************************************
226 CAM passthrough interface
227 ***********************************************************************
228 ***********************************************************************/
230 /***********************************************************************
231 * Handle a request for action from CAM
234 amr_cam_action(struct cam_sim *sim, union ccb *ccb)
236 struct amr_softc *sc = cam_sim_softc(sim);
238 switch(ccb->ccb_h.func_code) {
241 * Perform SCSI I/O to a physical device.
245 struct ccb_hdr *ccbh = &ccb->ccb_h;
246 struct ccb_scsiio *csio = &ccb->csio;
248 /* Validate the CCB */
249 ccbh->status = CAM_REQ_INPROG;
251 /* check the CDB length */
252 if (csio->cdb_len > AMR_MAX_EXTCDB_LEN)
253 ccbh->status = CAM_REQ_INVALID;
255 if ((csio->cdb_len > AMR_MAX_CDB_LEN) &&
256 (sc->support_ext_cdb == 0))
257 ccbh->status = CAM_REQ_INVALID;
259 /* check that the CDB pointer is not to a physical address */
260 if ((ccbh->flags & CAM_CDB_POINTER) &&
261 (ccbh->flags & CAM_CDB_PHYS))
262 ccbh->status = CAM_REQ_INVALID;
264 * if there is data transfer, it must be to/from a virtual
267 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
268 if (ccbh->flags & CAM_DATA_PHYS)
269 /* we can't map it */
270 ccbh->status = CAM_REQ_INVALID;
271 if (ccbh->flags & CAM_SCATTER_VALID)
272 /* we want to do the s/g setup */
273 ccbh->status = CAM_REQ_INVALID;
277 * If the command is to a LUN other than 0, fail it.
278 * This is probably incorrect, but during testing the
279 * firmware did not seem to respect the LUN field, and thus
280 * devices appear echoed.
282 if (csio->ccb_h.target_lun != 0)
283 ccbh->status = CAM_DEV_NOT_THERE;
285 /* if we're happy with the request, queue it for attention */
286 if (ccbh->status == CAM_REQ_INPROG) {
288 /* save the channel number in the ccb */
289 csio->ccb_h.sim_priv.entries[0].field= cam_sim_bus(sim);
291 amr_enqueue_ccb(sc, ccb);
298 case XPT_CALC_GEOMETRY:
300 cam_calc_geometry(&ccb->ccg, /*extended*/1);
305 * Return path stats. Some of these should probably be amended.
309 struct ccb_pathinq *cpi = & ccb->cpi;
311 debug(3, "XPT_PATH_INQ");
312 cpi->version_num = 1; /* XXX??? */
313 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
314 cpi->target_sprt = 0;
315 cpi->hba_misc = PIM_NOBUSRESET|PIM_SEQSCAN;
316 cpi->hba_eng_cnt = 0;
317 cpi->max_target = AMR_MAX_TARGETS;
318 cpi->max_lun = 0 /* AMR_MAX_LUNS*/;
319 cpi->initiator_id = 7; /* XXX variable? */
320 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
321 strncpy(cpi->hba_vid, "LSI", HBA_IDLEN);
322 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
323 cpi->unit_number = cam_sim_unit(sim);
324 cpi->bus_id = cam_sim_bus(sim);
325 cpi->base_transfer_speed = 132 * 1024; /* XXX */
326 cpi->transport = XPORT_SPI;
327 cpi->transport_version = 2;
328 cpi->protocol = PROTO_SCSI;
329 cpi->protocol_version = SCSI_REV_2;
330 cpi->ccb_h.status = CAM_REQ_CMP;
337 struct ccb_pathinq *cpi = & ccb->cpi;
339 debug(1, "XPT_RESET_BUS");
340 cpi->ccb_h.status = CAM_REQ_CMP;
346 debug(1, "XPT_RESET_DEV");
347 ccb->ccb_h.status = CAM_REQ_CMP;
351 case XPT_GET_TRAN_SETTINGS:
353 struct ccb_trans_settings *cts = &(ccb->cts);
355 debug(3, "XPT_GET_TRAN_SETTINGS");
357 struct ccb_trans_settings_scsi *scsi;
358 struct ccb_trans_settings_spi *spi;
360 scsi = &cts->proto_specific.scsi;
361 spi = &cts->xport_specific.spi;
363 cts->protocol = PROTO_SCSI;
364 cts->protocol_version = SCSI_REV_2;
365 cts->transport = XPORT_SPI;
366 cts->transport_version = 2;
368 if (cts->type == CTS_TYPE_USER_SETTINGS) {
369 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
373 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
374 spi->bus_width = MSG_EXT_WDTR_BUS_32_BIT;
375 spi->sync_period = 6; /* 40MHz how wide is this bus? */
376 spi->sync_offset = 31; /* How to extract this from board? */
378 spi->valid = CTS_SPI_VALID_SYNC_RATE
379 | CTS_SPI_VALID_SYNC_OFFSET
380 | CTS_SPI_VALID_BUS_WIDTH
381 | CTS_SPI_VALID_DISC;
382 scsi->valid = CTS_SCSI_VALID_TQ;
383 ccb->ccb_h.status = CAM_REQ_CMP;
387 case XPT_SET_TRAN_SETTINGS:
388 debug(3, "XPT_SET_TRAN_SETTINGS");
389 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
394 * Reject anything else as unsupported.
397 /* we can't do this */
398 ccb->ccb_h.status = CAM_REQ_INVALID;
402 KKASSERT(lockstatus(&sc->amr_list_lock, curthread) != 0);
406 /***********************************************************************
407 * Convert a CAM CCB off the top of the CCB queue to a passthrough SCSI
411 amr_cam_command(struct amr_softc *sc, struct amr_command **acp)
413 struct amr_command *ac;
414 struct amr_passthrough *ap;
415 struct amr_ext_passthrough *aep;
416 struct ccb_scsiio *csio;
417 int bus, target, error;
424 /* check to see if there is a ccb for us to work with */
425 if ((csio = (struct ccb_scsiio *)amr_dequeue_ccb(sc)) == NULL)
428 /* get bus/target, XXX validate against protected devices? */
429 bus = csio->ccb_h.sim_priv.entries[0].field;
430 target = csio->ccb_h.target_id;
433 * Build a passthrough command.
436 /* construct command */
437 if ((ac = amr_alloccmd(sc)) == NULL) {
442 /* construct passthrough */
443 if (sc->support_ext_cdb ) {
444 aep = &ac->ac_ccb->ccb_epthru;
447 aep->ap_request_sense_length = 14;
448 aep->ap_islogical = 0;
449 aep->ap_channel = bus;
450 aep->ap_scsi_id = target;
451 aep->ap_logical_drive_no = csio->ccb_h.target_lun;
452 aep->ap_cdb_length = csio->cdb_len;
453 aep->ap_data_transfer_length = csio->dxfer_len;
454 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
455 bcopy(csio->cdb_io.cdb_ptr, aep->ap_cdb, csio->cdb_len);
457 bcopy(csio->cdb_io.cdb_bytes, aep->ap_cdb,
461 * we leave the data s/g list and s/g count to the map routine
465 debug(2, " COMMAND %x/%d+%d to %d:%d:%d", aep->ap_cdb[0],
466 aep->ap_cdb_length, csio->dxfer_len, aep->ap_channel,
467 aep->ap_scsi_id, aep->ap_logical_drive_no);
470 ap = &ac->ac_ccb->ccb_pthru;
473 ap->ap_request_sense_length = 14;
474 ap->ap_islogical = 0;
475 ap->ap_channel = bus;
476 ap->ap_scsi_id = target;
477 ap->ap_logical_drive_no = csio->ccb_h.target_lun;
478 ap->ap_cdb_length = csio->cdb_len;
479 ap->ap_data_transfer_length = csio->dxfer_len;
480 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
481 bcopy(csio->cdb_io.cdb_ptr, ap->ap_cdb, csio->cdb_len);
483 bcopy(csio->cdb_io.cdb_bytes, ap->ap_cdb,
487 * we leave the data s/g list and s/g count to the map routine
491 debug(2, " COMMAND %x/%d+%d to %d:%d:%d", ap->ap_cdb[0],
492 ap->ap_cdb_length, csio->dxfer_len, ap->ap_channel,
493 ap->ap_scsi_id, ap->ap_logical_drive_no);
496 ac->ac_flags |= AMR_CMD_CCB;
498 ac->ac_data = csio->data_ptr;
499 ac->ac_length = csio->dxfer_len;
500 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
501 ac->ac_flags |= AMR_CMD_DATAIN;
502 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
503 ac->ac_flags |= AMR_CMD_DATAOUT;
505 ac->ac_private = csio;
506 ac->ac_complete = amr_cam_complete;
507 if ( sc->support_ext_cdb ) {
508 ac->ac_mailbox.mb_command = AMR_CMD_EXTPASS;
510 ac->ac_mailbox.mb_command = AMR_CMD_PASS;
518 /* put it back and try again later */
519 amr_requeue_ccb(sc, (union ccb *)csio);
525 /***********************************************************************
526 * Check for interrupt status
529 amr_cam_poll(struct cam_sim *sim)
532 amr_done(cam_sim_softc(sim));
535 /**********************************************************************
536 * Handle completion of a command submitted via CAM.
539 amr_cam_complete(struct amr_command *ac)
541 struct amr_passthrough *ap;
542 struct amr_ext_passthrough *aep;
543 struct ccb_scsiio *csio;
544 struct scsi_inquiry_data *inq;
545 int scsi_status, cdb0;
547 ap = &ac->ac_ccb->ccb_pthru;
548 aep = &ac->ac_ccb->ccb_epthru;
549 csio = (struct ccb_scsiio *)ac->ac_private;
550 inq = (struct scsi_inquiry_data *)csio->data_ptr;
552 if (ac->ac_mailbox.mb_command == AMR_CMD_EXTPASS)
553 scsi_status = aep->ap_scsi_status;
555 scsi_status = ap->ap_scsi_status;
556 debug(1, "status 0x%x AP scsi_status 0x%x", ac->ac_status,
559 /* Make sure the status is sane */
560 if ((ac->ac_status != AMR_STATUS_SUCCESS) && (scsi_status == 0)) {
561 csio->ccb_h.status = CAM_REQ_CMP_ERR;
566 * Hide disks from CAM so that they're not picked up and treated as
569 * If the configuration provides a mechanism to mark a disk a "not
570 * managed", we could add handling for that to allow disks to be
571 * selectively visible.
574 /* handle passthrough SCSI status */
575 switch(scsi_status) {
576 case 0: /* completed OK */
577 if (ac->ac_mailbox.mb_command == AMR_CMD_EXTPASS)
578 cdb0 = aep->ap_cdb[0];
580 cdb0 = ap->ap_cdb[0];
581 if ((cdb0 == INQUIRY) && (SID_TYPE(inq) == T_DIRECT))
582 inq->device = (inq->device & 0xe0) | T_NODEVICE;
583 csio->ccb_h.status = CAM_REQ_CMP;
587 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
588 csio->scsi_status = SCSI_STATUS_CHECK_COND;
589 if (ac->ac_mailbox.mb_command == AMR_CMD_EXTPASS)
590 bcopy(aep->ap_request_sense_area, &csio->sense_data,
591 AMR_MAX_REQ_SENSE_LEN);
593 bcopy(ap->ap_request_sense_area, &csio->sense_data,
594 AMR_MAX_REQ_SENSE_LEN);
595 csio->sense_len = AMR_MAX_REQ_SENSE_LEN;
596 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
600 csio->ccb_h.status = CAM_SCSI_BUSY;
607 * Non-zero LUNs are already filtered, so there's no need
608 * to return CAM_DEV_NOT_THERE.
610 csio->ccb_h.status = CAM_SEL_TIMEOUT;
615 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
616 debug(2, "%*D\n", imin(csio->dxfer_len, 16), csio->data_ptr,
619 lockmgr(&ac->ac_sc->amr_list_lock, LK_EXCLUSIVE);
620 xpt_done((union ccb *)csio);
622 lockmgr(&ac->ac_sc->amr_list_lock, LK_RELEASE);