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
27 * Copyright (c) 2002 Eric Moore
28 * Copyright (c) 2002 LSI Logic Corporation
29 * All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
34 * 1. Redistributions of source code must retain the above copyright
35 * notice, this list of conditions and the following disclaimer.
36 * 2. Redistributions in binary form must reproduce the above copyright
37 * notice, this list of conditions and the following disclaimer in the
38 * documentation and/or other materials provided with the distribution.
39 * 3. The party using or redistributing the source code and binary forms
40 * agrees to the disclaimer below and the terms and conditions set forth
43 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
55 * $FreeBSD: src/sys/dev/amr/amr_cam.c,v 1.1.2.3 2002/11/11 13:19:10 emoore Exp $
56 * $DragonFly: src/sys/dev/raid/amr/amr_cam.c,v 1.3 2003/08/07 21:17:08 dillon Exp $
59 #include <sys/param.h>
60 #include <sys/systm.h>
61 #include <sys/malloc.h>
62 #include <sys/kernel.h>
64 #include "amr_compat.h"
67 #include <sys/devicestat.h>
71 #include <bus/cam/cam.h>
72 #include <bus/cam/cam_ccb.h>
73 #include <bus/cam/cam_sim.h>
74 #include <bus/cam/cam_xpt.h>
75 #include <bus/cam/cam_xpt_sim.h>
76 #include <bus/cam/cam_debug.h>
77 #include <bus/cam/scsi/scsi_all.h>
78 #include <bus/cam/scsi/scsi_message.h>
80 #include <machine/resource.h>
81 #include <machine/bus.h>
86 static void amr_cam_action(struct cam_sim *sim, union ccb *ccb);
87 static void amr_cam_poll(struct cam_sim *sim);
88 static void amr_cam_complete(struct amr_command *ac);
89 static void amr_cam_complete_extcdb(struct amr_command *ac);
92 /********************************************************************************
93 * Enqueue/dequeue functions
96 amr_enqueue_ccb(struct amr_softc *sc, union ccb *ccb)
101 TAILQ_INSERT_TAIL(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
106 amr_requeue_ccb(struct amr_softc *sc, union ccb *ccb)
111 TAILQ_INSERT_HEAD(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
115 static __inline union ccb *
116 amr_dequeue_ccb(struct amr_softc *sc)
122 if ((ccb = (union ccb *)TAILQ_FIRST(&sc->amr_cam_ccbq)) != NULL)
123 TAILQ_REMOVE(&sc->amr_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
128 /********************************************************************************
129 * Attach our 'real' SCSI channels to CAM
132 amr_cam_attach(struct amr_softc *sc)
134 struct cam_devq *devq;
137 /* initialise the ccb queue */
138 TAILQ_INIT(&sc->amr_cam_ccbq);
141 * Allocate a devq for all our channels combined. This should
142 * allow for the maximum number of SCSI commands we will accept
145 if ((devq = cam_simq_alloc(AMR_MAX_SCSI_CMDS)) == NULL)
149 * Iterate over our channels, registering them with CAM
151 for (chn = 0; chn < sc->amr_maxchan; chn++) {
154 if ((sc->amr_cam_sim[chn] = cam_sim_alloc(amr_cam_action,
158 device_get_unit(sc->amr_dev),
163 device_printf(sc->amr_dev, "CAM SIM attach failed\n");
167 /* register the bus ID so we can get it later */
168 if (xpt_bus_register(sc->amr_cam_sim[chn], chn)) {
169 device_printf(sc->amr_dev, "CAM XPT bus registration failed\n");
174 * XXX we should scan the config and work out which devices are actually
180 /********************************************************************************
181 * Disconnect ourselves from CAM
184 amr_cam_detach(struct amr_softc *sc)
188 for (chn = 0, first = 1; chn < sc->amr_maxchan; chn++) {
191 * If a sim was allocated for this channel, free it
193 if (sc->amr_cam_sim[chn] != NULL) {
194 xpt_bus_deregister(cam_sim_path(sc->amr_cam_sim[chn]));
195 cam_sim_free(sc->amr_cam_sim[chn], first ? TRUE : FALSE);
201 /********************************************************************************
202 ********************************************************************************
203 CAM passthrough interface
204 ********************************************************************************
205 ********************************************************************************/
207 /********************************************************************************
208 * Handle a request for action from CAM
211 amr_cam_action(struct cam_sim *sim, union ccb *ccb)
213 struct amr_softc *sc = cam_sim_softc(sim);
215 switch(ccb->ccb_h.func_code) {
218 * Perform SCSI I/O to a physical device.
222 struct ccb_hdr *ccbh = &ccb->ccb_h;
223 struct ccb_scsiio *csio = &ccb->csio;
225 /* Validate the CCB */
226 ccbh->status = CAM_REQ_INPROG;
228 /* check the CDB length */
229 if (csio->cdb_len > AMR_MAX_EXTCDB_LEN)
230 ccbh->status = CAM_REQ_CMP_ERR;
232 if ((csio->cdb_len > AMR_MAX_CDB_LEN) && (sc->support_ext_cdb == 0 ))
233 ccbh->status = CAM_REQ_CMP_ERR;
235 /* check that the CDB pointer is not to a physical address */
236 if ((ccbh->flags & CAM_CDB_POINTER) && (ccbh->flags & CAM_CDB_PHYS))
237 ccbh->status = CAM_REQ_CMP_ERR;
239 /* if there is data transfer, it must be to/from a virtual address */
240 if ((ccbh->flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
241 if (ccbh->flags & CAM_DATA_PHYS) /* we can't map it */
242 ccbh->status = CAM_REQ_CMP_ERR;
243 if (ccbh->flags & CAM_SCATTER_VALID) /* we want to do the s/g setup */
244 ccbh->status = CAM_REQ_CMP_ERR;
248 * If the command is to a LUN other than 0, fail it.
249 * This is probably incorrect, but during testing the firmware did not
250 * seem to respect the LUN field, and thus devices appear echoed.
252 if (csio->ccb_h.target_lun != 0)
253 ccbh->status = CAM_REQ_CMP_ERR;
255 /* if we're happy with the request, queue it for attention */
256 if (ccbh->status == CAM_REQ_INPROG) {
258 /* save the channel number in the ccb */
259 csio->ccb_h.sim_priv.entries[0].field = cam_sim_bus(sim);
261 amr_enqueue_ccb(sc, ccb);
268 case XPT_CALC_GEOMETRY:
270 struct ccb_calc_geometry *ccg = &ccb->ccg;
271 u_int32_t size_in_mb;
272 u_int32_t secs_per_cylinder;
274 size_in_mb = ccg->volume_size / ((1024L * 1024L) / ccg->block_size);
276 if (size_in_mb > 1024) {
278 ccg->secs_per_track = 63;
281 ccg->secs_per_track = 32;
283 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
284 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
285 ccb->ccb_h.status = CAM_REQ_CMP;
290 * Return path stats. Some of these should probably be
295 struct ccb_pathinq *cpi = & ccb->cpi;
297 debug(3, "XPT_PATH_INQ");
298 cpi->version_num = 1; /* XXX??? */
299 cpi->hba_inquiry = PI_SDTR_ABLE|PI_TAG_ABLE|PI_WIDE_16;
300 cpi->target_sprt = 0;
301 cpi->hba_misc = PIM_NOBUSRESET;
302 cpi->hba_eng_cnt = 0;
303 cpi->max_target = AMR_MAX_TARGETS;
304 cpi->max_lun = 0 /* AMR_MAX_LUNS*/;
305 cpi->initiator_id = 7; /* XXX variable? */
306 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
307 strncpy(cpi->hba_vid, "LSI", HBA_IDLEN);
308 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
309 cpi->unit_number = cam_sim_unit(sim);
310 cpi->bus_id = cam_sim_bus(sim);
311 cpi->base_transfer_speed = 132 * 1024; /* XXX get from controller? */
312 cpi->ccb_h.status = CAM_REQ_CMP;
319 struct ccb_pathinq *cpi = & ccb->cpi;
321 debug(1, "XPT_RESET_BUS");
322 cpi->ccb_h.status = CAM_REQ_CMP;
328 debug(1, "XPT_RESET_DEV");
329 ccb->ccb_h.status = CAM_REQ_CMP;
333 case XPT_GET_TRAN_SETTINGS:
335 struct ccb_trans_settings *cts;
337 debug(3, "XPT_GET_TRAN_SETTINGS");
341 if ((cts->flags & CCB_TRANS_USER_SETTINGS) == 0) {
342 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
346 cts->flags = CCB_TRANS_DISC_ENB|CCB_TRANS_TAG_ENB;
347 cts->bus_width = MSG_EXT_WDTR_BUS_32_BIT;
348 cts->sync_period = 6; /* 40MHz how wide is this bus? */
349 cts->sync_offset = 31; /* How to extract this from board? */
351 cts->valid = CCB_TRANS_SYNC_RATE_VALID
352 | CCB_TRANS_SYNC_OFFSET_VALID
353 | CCB_TRANS_BUS_WIDTH_VALID
354 | CCB_TRANS_DISC_VALID
355 | CCB_TRANS_TQ_VALID;
356 ccb->ccb_h.status = CAM_REQ_CMP;
360 case XPT_SET_TRAN_SETTINGS:
361 debug(3, "XPT_SET_TRAN_SETTINGS");
362 ccb->ccb_h.status = CAM_FUNC_NOTAVAIL;
367 * Reject anything else as unsupported.
370 /* we can't do this */
371 ccb->ccb_h.status = CAM_REQ_INVALID;
377 /********************************************************************************
378 * Convert a CAM CCB off the top of the CCB queue to a passthrough SCSI command.
381 amr_cam_command(struct amr_softc *sc, struct amr_command **acp)
383 struct amr_command *ac;
384 struct amr_passthrough *ap;
385 struct amr_ext_passthrough *aep;
386 struct ccb_scsiio *csio;
387 int bus, target, error;
394 /* check to see if there is a ccb for us to work with */
395 if ((csio = (struct ccb_scsiio *)amr_dequeue_ccb(sc)) == NULL)
398 /* get bus/target, XXX validate against protected devices? */
399 bus = csio->ccb_h.sim_priv.entries[0].field;
400 target = csio->ccb_h.target_id;
403 * Build a passthrough command.
406 /* construct passthrough */
407 if (sc->support_ext_cdb ) {
408 if ((aep = malloc(sizeof(*aep), M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL) {
414 aep->ap_request_sense_length = 14;
415 aep->ap_islogical = 0;
416 aep->ap_channel = bus;
417 aep->ap_scsi_id = target;
418 aep->ap_logical_drive_no = csio->ccb_h.target_lun;
419 aep->ap_cdb_length = csio->cdb_len;
420 aep->ap_data_transfer_length = csio->dxfer_len;
421 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
422 bcopy(csio->cdb_io.cdb_ptr, aep->ap_cdb, csio->cdb_len);
424 bcopy(csio->cdb_io.cdb_bytes, aep->ap_cdb, csio->cdb_len);
426 /* we leave the data s/g list and s/g count to the map routine later */
428 debug(2, " COMMAND %x/%d+%d to %d:%d:%d", aep->ap_cdb[0], aep->ap_cdb_length, csio->dxfer_len,
429 aep->ap_channel, aep->ap_scsi_id, aep->ap_logical_drive_no);
432 if ((ap = malloc(sizeof(*ap), M_DEVBUF, M_NOWAIT | M_ZERO)) == NULL) {
438 ap->ap_request_sense_length = 14;
439 ap->ap_islogical = 0;
440 ap->ap_channel = bus;
441 ap->ap_scsi_id = target;
442 ap->ap_logical_drive_no = csio->ccb_h.target_lun;
443 ap->ap_cdb_length = csio->cdb_len;
444 ap->ap_data_transfer_length = csio->dxfer_len;
445 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
446 bcopy(csio->cdb_io.cdb_ptr, ap->ap_cdb, csio->cdb_len);
448 bcopy(csio->cdb_io.cdb_bytes, ap->ap_cdb, csio->cdb_len);
450 /* we leave the data s/g list and s/g count to the map routine later */
452 debug(2, " COMMAND %x/%d+%d to %d:%d:%d", ap->ap_cdb[0], ap->ap_cdb_length, csio->dxfer_len,
453 ap->ap_channel, ap->ap_scsi_id, ap->ap_logical_drive_no);
456 /* construct command */
457 if ((ac = amr_alloccmd(sc)) == NULL) {
462 ac->ac_flags |= AMR_CMD_DATAOUT;
464 ac->ac_ccb_data = csio->data_ptr;
465 ac->ac_ccb_length = csio->dxfer_len;
466 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
467 ac->ac_flags |= AMR_CMD_CCB_DATAIN;
468 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
469 ac->ac_flags |= AMR_CMD_CCB_DATAOUT;
471 ac->ac_private = csio;
472 if ( sc->support_ext_cdb ) {
474 ac->ac_length = sizeof(*aep);
475 ac->ac_complete = amr_cam_complete_extcdb;
476 ac->ac_mailbox.mb_command = AMR_CMD_EXTPASS;
479 ac->ac_length = sizeof(*ap);
480 ac->ac_complete = amr_cam_complete;
481 ac->ac_mailbox.mb_command = AMR_CMD_PASS;
492 if (csio != NULL) /* put it back and try again later */
493 amr_requeue_ccb(sc, (union ccb *)csio);
499 /********************************************************************************
500 * Check for interrupt status
503 amr_cam_poll(struct cam_sim *sim)
505 amr_done(cam_sim_softc(sim));
508 /********************************************************************************
509 * Handle completion of a command submitted via CAM.
512 amr_cam_complete(struct amr_command *ac)
514 struct amr_passthrough *ap = (struct amr_passthrough *)ac->ac_data;
515 struct ccb_scsiio *csio = (struct ccb_scsiio *)ac->ac_private;
516 struct scsi_inquiry_data *inq = (struct scsi_inquiry_data *)csio->data_ptr;
518 /* XXX note that we're ignoring ac->ac_status - good idea? */
520 debug(1, "status 0x%x scsi_status 0x%x", ac->ac_status, ap->ap_scsi_status);
523 * Hide disks from CAM so that they're not picked up and treated as 'normal' disks.
525 * If the configuration provides a mechanism to mark a disk a "not managed", we
526 * could add handling for that to allow disks to be selectively visible.
529 if ((ap->ap_cdb[0] == INQUIRY) && (SID_TYPE(inq) == T_DIRECT)) {
530 bzero(csio->data_ptr, csio->dxfer_len);
531 if (ap->ap_scsi_status == 0xf0) {
532 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
534 csio->ccb_h.status = CAM_DEV_NOT_THERE;
538 /* handle passthrough SCSI status */
539 switch(ap->ap_scsi_status) {
540 case 0: /* completed OK */
541 csio->ccb_h.status = CAM_REQ_CMP;
545 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
546 csio->scsi_status = SCSI_STATUS_CHECK_COND;
547 bcopy(ap->ap_request_sense_area, &csio->sense_data, AMR_MAX_REQ_SENSE_LEN);
548 csio->sense_len = AMR_MAX_REQ_SENSE_LEN;
549 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
553 csio->ccb_h.status = CAM_SCSI_BUSY;
559 csio->ccb_h.status = CAM_REQ_CMP_ERR;
564 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
565 debug(2, "%*D\n", imin(csio->dxfer_len, 16), csio->data_ptr, " ");
566 xpt_done((union ccb *)csio);
570 /********************************************************************************
571 * Handle completion of a command submitted via CAM.
572 * Completion for extended cdb
575 amr_cam_complete_extcdb(struct amr_command *ac)
577 struct amr_ext_passthrough *aep = (struct amr_ext_passthrough *)ac->ac_data;
578 struct ccb_scsiio *csio = (struct ccb_scsiio *)ac->ac_private;
579 struct scsi_inquiry_data *inq = (struct scsi_inquiry_data *)csio->data_ptr;
581 /* XXX note that we're ignoring ac->ac_status - good idea? */
583 debug(1, "status 0x%x scsi_status 0x%x", ac->ac_status, aep->ap_scsi_status);
586 * Hide disks from CAM so that they're not picked up and treated as 'normal' disks.
588 * If the configuration provides a mechanism to mark a disk a "not managed", we
589 * could add handling for that to allow disks to be selectively visible.
592 if ((aep->ap_cdb[0] == INQUIRY) && (SID_TYPE(inq) == T_DIRECT)) {
593 bzero(csio->data_ptr, csio->dxfer_len);
594 if (aep->ap_scsi_status == 0xf0) {
595 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
597 csio->ccb_h.status = CAM_DEV_NOT_THERE;
601 /* handle passthrough SCSI status */
602 switch(aep->ap_scsi_status) {
603 case 0: /* completed OK */
604 csio->ccb_h.status = CAM_REQ_CMP;
608 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
609 csio->scsi_status = SCSI_STATUS_CHECK_COND;
610 bcopy(aep->ap_request_sense_area, &csio->sense_data, AMR_MAX_REQ_SENSE_LEN);
611 csio->sense_len = AMR_MAX_REQ_SENSE_LEN;
612 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
616 csio->ccb_h.status = CAM_SCSI_BUSY;
622 csio->ccb_h.status = CAM_REQ_CMP_ERR;
627 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE)
628 debug(2, "%*D\n", imin(csio->dxfer_len, 16), csio->data_ptr, " ");
629 xpt_done((union ccb *)csio);