2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2000 Michael Smith
5 * Copyright (c) 2000 BSDi
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 * Copyright (c) 2002 Eric Moore
31 * Copyright (c) 2002 LSI Logic Corporation
32 * All rights reserved.
34 * Redistribution and use in source and binary forms, with or without
35 * modification, are permitted provided that the following conditions
37 * 1. Redistributions of source code must retain the above copyright
38 * notice, this list of conditions and the following disclaimer.
39 * 2. Redistributions in binary form must reproduce the above copyright
40 * notice, this list of conditions and the following disclaimer in the
41 * documentation and/or other materials provided with the distribution.
42 * 3. The party using or redistributing the source code and binary forms
43 * agrees to the disclaimer below and the terms and conditions set forth
46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
59 #include <sys/cdefs.h>
60 __FBSDID("$FreeBSD$");
62 #include <sys/param.h>
63 #include <sys/systm.h>
64 #include <sys/malloc.h>
65 #include <sys/kernel.h>
66 #include <sys/module.h>
74 #include <cam/cam_ccb.h>
75 #include <cam/cam_sim.h>
76 #include <cam/cam_xpt.h>
77 #include <cam/cam_xpt_sim.h>
78 #include <cam/cam_debug.h>
79 #include <cam/scsi/scsi_all.h>
80 #include <cam/scsi/scsi_message.h>
82 #include <machine/resource.h>
83 #include <machine/bus.h>
85 #include <dev/amr/amrreg.h>
86 #include <dev/amr/amrvar.h>
88 static int amr_cam_probe(device_t dev);
89 static int amr_cam_attach(device_t dev);
90 static int amr_cam_detach(device_t dev);
91 static void amr_cam_action(struct cam_sim *sim, union ccb *ccb);
92 static void amr_cam_poll(struct cam_sim *sim);
93 static void amr_cam_complete(struct amr_command *ac);
94 static int amr_cam_command(struct amr_softc *sc, struct amr_command **acp);
96 static devclass_t amr_pass_devclass;
98 static device_method_t amr_pass_methods[] = {
99 DEVMETHOD(device_probe, amr_cam_probe),
100 DEVMETHOD(device_attach, amr_cam_attach),
101 DEVMETHOD(device_detach, amr_cam_detach),
105 static driver_t amr_pass_driver = {
111 DRIVER_MODULE(amrp, amr, amr_pass_driver, amr_pass_devclass, 0, 0);
112 MODULE_DEPEND(amrp, cam, 1, 1, 1);
114 static MALLOC_DEFINE(M_AMRCAM, "amrcam", "AMR CAM memory");
116 /***********************************************************************
117 * Enqueue/dequeue functions
120 amr_enqueue_ccb(struct amr_softc *sc, union ccb *ccb)
123 TAILQ_INSERT_TAIL(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
127 amr_requeue_ccb(struct amr_softc *sc, union ccb *ccb)
130 TAILQ_INSERT_HEAD(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
133 static __inline union ccb *
134 amr_dequeue_ccb(struct amr_softc *sc)
138 if ((ccb = (union ccb *)TAILQ_FIRST(&sc->amr_cam_ccbq)) != NULL)
139 TAILQ_REMOVE(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
144 amr_cam_probe(device_t dev)
149 /********************************************************************************
150 * Attach our 'real' SCSI channels to CAM
153 amr_cam_attach(device_t dev)
155 struct amr_softc *sc;
156 struct cam_devq *devq;
159 sc = device_get_softc(dev);
161 /* initialise the ccb queue */
162 TAILQ_INIT(&sc->amr_cam_ccbq);
165 * Allocate a devq for all our channels combined. This should
166 * allow for the maximum number of SCSI commands we will accept
167 * at one time. Save the pointer in the softc so we can find it later
170 if ((devq = cam_simq_alloc(AMR_MAX_SCSI_CMDS)) == NULL)
172 sc->amr_cam_devq = devq;
175 * Iterate over our channels, registering them with CAM
177 for (chn = 0; chn < sc->amr_maxchan; chn++) {
180 if ((sc->amr_cam_sim[chn] = cam_sim_alloc(amr_cam_action,
181 amr_cam_poll, "amr", sc, device_get_unit(sc->amr_dev),
182 &sc->amr_list_lock, 1, AMR_MAX_SCSI_CMDS, devq)) == NULL) {
184 device_printf(sc->amr_dev, "CAM SIM attach failed\n");
188 /* register the bus ID so we can get it later */
189 mtx_lock(&sc->amr_list_lock);
190 error = xpt_bus_register(sc->amr_cam_sim[chn], sc->amr_dev,chn);
191 mtx_unlock(&sc->amr_list_lock);
193 device_printf(sc->amr_dev,
194 "CAM XPT bus registration failed\n");
199 * XXX we should scan the config and work out which devices are
200 * actually protected.
202 sc->amr_cam_command = amr_cam_command;
206 /********************************************************************************
207 * Disconnect ourselves from CAM
210 amr_cam_detach(device_t dev)
212 struct amr_softc *sc;
215 sc = device_get_softc(dev);
216 mtx_lock(&sc->amr_list_lock);
217 for (chn = 0; chn < sc->amr_maxchan; chn++) {
219 * If a sim was allocated for this channel, free it
221 if (sc->amr_cam_sim[chn] != NULL) {
222 xpt_bus_deregister(cam_sim_path(sc->amr_cam_sim[chn]));
223 cam_sim_free(sc->amr_cam_sim[chn], FALSE);
226 mtx_unlock(&sc->amr_list_lock);
228 /* Now free the devq */
229 if (sc->amr_cam_devq != NULL)
230 cam_simq_free(sc->amr_cam_devq);
235 /***********************************************************************
236 ***********************************************************************
237 CAM passthrough interface
238 ***********************************************************************
239 ***********************************************************************/
241 /***********************************************************************
242 * Handle a request for action from CAM
245 amr_cam_action(struct cam_sim *sim, union ccb *ccb)
247 struct amr_softc *sc = cam_sim_softc(sim);
249 switch(ccb->ccb_h.func_code) {
252 * Perform SCSI I/O to a physical device.
256 struct ccb_hdr *ccbh = &ccb->ccb_h;
257 struct ccb_scsiio *csio = &ccb->csio;
259 /* Validate the CCB */
260 ccbh->status = CAM_REQ_INPROG;
262 /* check the CDB length */
263 if (csio->cdb_len > AMR_MAX_EXTCDB_LEN)
264 ccbh->status = CAM_REQ_INVALID;
266 if ((csio->cdb_len > AMR_MAX_CDB_LEN) &&
267 (sc->support_ext_cdb == 0))
268 ccbh->status = CAM_REQ_INVALID;
270 /* check that the CDB pointer is not to a physical address */
271 if ((ccbh->flags & CAM_CDB_POINTER) &&
272 (ccbh->flags & CAM_CDB_PHYS))
273 ccbh->status = CAM_REQ_INVALID;
275 * if there is data transfer, it must be to/from a virtual
278 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
279 if ((ccbh->flags & CAM_DATA_MASK) != CAM_DATA_VADDR)
280 /* we can't map it */
281 ccbh->status = CAM_REQ_INVALID;
285 * If the command is to a LUN other than 0, fail it.
286 * This is probably incorrect, but during testing the
287 * firmware did not seem to respect the LUN field, and thus
288 * devices appear echoed.
290 if (csio->ccb_h.target_lun != 0)
291 ccbh->status = CAM_DEV_NOT_THERE;
293 /* if we're happy with the request, queue it for attention */
294 if (ccbh->status == CAM_REQ_INPROG) {
296 /* save the channel number in the ccb */
297 csio->ccb_h.sim_priv.entries[0].field= cam_sim_bus(sim);
299 amr_enqueue_ccb(sc, ccb);
306 case XPT_CALC_GEOMETRY:
308 cam_calc_geometry(&ccb->ccg, /*extended*/1);
313 * Return path stats. Some of these should probably be amended.
317 struct ccb_pathinq *cpi = & ccb->cpi;
319 debug(3, "XPT_PATH_INQ");
320 cpi->version_num = 1; /* XXX??? */
321 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
322 cpi->target_sprt = 0;
323 cpi->hba_misc = PIM_NOBUSRESET|PIM_SEQSCAN;
324 cpi->hba_eng_cnt = 0;
325 cpi->max_target = AMR_MAX_TARGETS;
326 cpi->max_lun = 0 /* AMR_MAX_LUNS*/;
327 cpi->initiator_id = 7; /* XXX variable? */
328 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
329 strlcpy(cpi->hba_vid, "LSI", HBA_IDLEN);
330 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
331 cpi->unit_number = cam_sim_unit(sim);
332 cpi->bus_id = cam_sim_bus(sim);
333 cpi->base_transfer_speed = 132 * 1024; /* XXX */
334 cpi->transport = XPORT_SPI;
335 cpi->transport_version = 2;
336 cpi->protocol = PROTO_SCSI;
337 cpi->protocol_version = SCSI_REV_2;
338 cpi->ccb_h.status = CAM_REQ_CMP;
345 struct ccb_pathinq *cpi = & ccb->cpi;
347 debug(1, "XPT_RESET_BUS");
348 cpi->ccb_h.status = CAM_REQ_CMP;
354 debug(1, "XPT_RESET_DEV");
355 ccb->ccb_h.status = CAM_REQ_CMP;
359 case XPT_GET_TRAN_SETTINGS:
361 struct ccb_trans_settings *cts = &(ccb->cts);
363 debug(3, "XPT_GET_TRAN_SETTINGS");
365 struct ccb_trans_settings_scsi *scsi;
366 struct ccb_trans_settings_spi *spi;
368 scsi = &cts->proto_specific.scsi;
369 spi = &cts->xport_specific.spi;
371 cts->protocol = PROTO_SCSI;
372 cts->protocol_version = SCSI_REV_2;
373 cts->transport = XPORT_SPI;
374 cts->transport_version = 2;
376 if (cts->type == CTS_TYPE_USER_SETTINGS) {
377 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
381 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
382 spi->bus_width = MSG_EXT_WDTR_BUS_32_BIT;
383 spi->sync_period = 6; /* 40MHz how wide is this bus? */
384 spi->sync_offset = 31; /* How to extract this from board? */
386 spi->valid = CTS_SPI_VALID_SYNC_RATE
387 | CTS_SPI_VALID_SYNC_OFFSET
388 | CTS_SPI_VALID_BUS_WIDTH
389 | CTS_SPI_VALID_DISC;
390 scsi->valid = CTS_SCSI_VALID_TQ;
391 ccb->ccb_h.status = CAM_REQ_CMP;
395 case XPT_SET_TRAN_SETTINGS:
396 debug(3, "XPT_SET_TRAN_SETTINGS");
397 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
402 * Reject anything else as unsupported.
405 /* we can't do this */
406 ccb->ccb_h.status = CAM_REQ_INVALID;
410 mtx_assert(&sc->amr_list_lock, MA_OWNED);
414 /***********************************************************************
415 * Convert a CAM CCB off the top of the CCB queue to a passthrough SCSI
419 amr_cam_command(struct amr_softc *sc, struct amr_command **acp)
421 struct amr_command *ac;
422 struct amr_passthrough *ap;
423 struct amr_ext_passthrough *aep;
424 struct ccb_scsiio *csio;
425 int bus, target, error;
432 /* check to see if there is a ccb for us to work with */
433 if ((csio = (struct ccb_scsiio *)amr_dequeue_ccb(sc)) == NULL)
436 /* get bus/target, XXX validate against protected devices? */
437 bus = csio->ccb_h.sim_priv.entries[0].field;
438 target = csio->ccb_h.target_id;
441 * Build a passthrough command.
444 /* construct command */
445 if ((ac = amr_alloccmd(sc)) == NULL) {
450 /* construct passthrough */
451 if (sc->support_ext_cdb ) {
452 aep = &ac->ac_ccb->ccb_epthru;
455 aep->ap_request_sense_length = 14;
456 aep->ap_islogical = 0;
457 aep->ap_channel = bus;
458 aep->ap_scsi_id = target;
459 aep->ap_logical_drive_no = csio->ccb_h.target_lun;
460 aep->ap_cdb_length = csio->cdb_len;
461 aep->ap_data_transfer_length = csio->dxfer_len;
462 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
463 bcopy(csio->cdb_io.cdb_ptr, aep->ap_cdb, csio->cdb_len);
465 bcopy(csio->cdb_io.cdb_bytes, aep->ap_cdb,
469 * we leave the data s/g list and s/g count to the map routine
473 debug(2, " COMMAND %x/%d+%d to %d:%d:%d", aep->ap_cdb[0],
474 aep->ap_cdb_length, csio->dxfer_len, aep->ap_channel,
475 aep->ap_scsi_id, aep->ap_logical_drive_no);
478 ap = &ac->ac_ccb->ccb_pthru;
481 ap->ap_request_sense_length = 14;
482 ap->ap_islogical = 0;
483 ap->ap_channel = bus;
484 ap->ap_scsi_id = target;
485 ap->ap_logical_drive_no = csio->ccb_h.target_lun;
486 ap->ap_cdb_length = csio->cdb_len;
487 ap->ap_data_transfer_length = csio->dxfer_len;
488 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
489 bcopy(csio->cdb_io.cdb_ptr, ap->ap_cdb, csio->cdb_len);
491 bcopy(csio->cdb_io.cdb_bytes, ap->ap_cdb,
495 * we leave the data s/g list and s/g count to the map routine
499 debug(2, " COMMAND %x/%d+%d to %d:%d:%d", ap->ap_cdb[0],
500 ap->ap_cdb_length, csio->dxfer_len, ap->ap_channel,
501 ap->ap_scsi_id, ap->ap_logical_drive_no);
504 ac->ac_flags |= AMR_CMD_CCB;
506 ac->ac_data = csio->data_ptr;
507 ac->ac_length = csio->dxfer_len;
508 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
509 ac->ac_flags |= AMR_CMD_DATAIN;
510 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
511 ac->ac_flags |= AMR_CMD_DATAOUT;
513 ac->ac_private = csio;
514 ac->ac_complete = amr_cam_complete;
515 if ( sc->support_ext_cdb ) {
516 ac->ac_mailbox.mb_command = AMR_CMD_EXTPASS;
518 ac->ac_mailbox.mb_command = AMR_CMD_PASS;
526 /* put it back and try again later */
527 amr_requeue_ccb(sc, (union ccb *)csio);
533 /***********************************************************************
534 * Check for interrupt status
537 amr_cam_poll(struct cam_sim *sim)
540 amr_done(cam_sim_softc(sim));
543 /**********************************************************************
544 * Handle completion of a command submitted via CAM.
547 amr_cam_complete(struct amr_command *ac)
549 struct amr_passthrough *ap;
550 struct amr_ext_passthrough *aep;
551 struct ccb_scsiio *csio;
552 struct scsi_inquiry_data *inq;
553 int scsi_status, cdb0;
555 ap = &ac->ac_ccb->ccb_pthru;
556 aep = &ac->ac_ccb->ccb_epthru;
557 csio = (struct ccb_scsiio *)ac->ac_private;
558 inq = (struct scsi_inquiry_data *)csio->data_ptr;
560 if (ac->ac_mailbox.mb_command == AMR_CMD_EXTPASS)
561 scsi_status = aep->ap_scsi_status;
563 scsi_status = ap->ap_scsi_status;
564 debug(1, "status 0x%x AP scsi_status 0x%x", ac->ac_status,
567 /* Make sure the status is sane */
568 if ((ac->ac_status != AMR_STATUS_SUCCESS) && (scsi_status == 0)) {
569 csio->ccb_h.status = CAM_REQ_CMP_ERR;
574 * Hide disks from CAM so that they're not picked up and treated as
577 * If the configuration provides a mechanism to mark a disk a "not
578 * managed", we could add handling for that to allow disks to be
579 * selectively visible.
582 /* handle passthrough SCSI status */
583 switch(scsi_status) {
584 case 0: /* completed OK */
585 if (ac->ac_mailbox.mb_command == AMR_CMD_EXTPASS)
586 cdb0 = aep->ap_cdb[0];
588 cdb0 = ap->ap_cdb[0];
589 if ((cdb0 == INQUIRY) && (SID_TYPE(inq) == T_DIRECT))
590 inq->device = (inq->device & 0xe0) | T_NODEVICE;
591 csio->ccb_h.status = CAM_REQ_CMP;
595 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
596 csio->scsi_status = SCSI_STATUS_CHECK_COND;
597 if (ac->ac_mailbox.mb_command == AMR_CMD_EXTPASS)
598 bcopy(aep->ap_request_sense_area, &csio->sense_data,
599 AMR_MAX_REQ_SENSE_LEN);
601 bcopy(ap->ap_request_sense_area, &csio->sense_data,
602 AMR_MAX_REQ_SENSE_LEN);
603 csio->sense_len = AMR_MAX_REQ_SENSE_LEN;
604 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
608 csio->ccb_h.status = CAM_SCSI_BUSY;
615 * Non-zero LUNs are already filtered, so there's no need
616 * to return CAM_DEV_NOT_THERE.
618 csio->ccb_h.status = CAM_SEL_TIMEOUT;
623 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
624 debug(2, "%*D\n", imin(csio->dxfer_len, 16), csio->data_ptr,
627 mtx_lock(&ac->ac_sc->amr_list_lock);
628 xpt_done((union ccb *)csio);
630 mtx_unlock(&ac->ac_sc->amr_list_lock);