2 * Copyright (c) 2000, 2001 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 * $FreeBSD: src/sys/dev/mly/mly_cam.c,v 1.1.2.3 2001/04/21 04:09:06 msmith Exp $
28 * $DragonFly: src/sys/dev/raid/mly/Attic/mly_cam.c,v 1.4 2004/03/15 03:05:11 dillon Exp $
31 * CAM interface for FreeBSD
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/devicestat.h>
39 #include <bus/cam/cam.h>
40 #include <bus/cam/cam_ccb.h>
41 #include <bus/cam/cam_periph.h>
42 #include <bus/cam/cam_sim.h>
43 #include <bus/cam/cam_xpt_sim.h>
44 #include <bus/cam/scsi/scsi_all.h>
45 #include <bus/cam/scsi/scsi_message.h>
47 #include <machine/resource.h>
48 #include <machine/bus.h>
53 #include "mly_tables.h"
55 static void mly_cam_poll(struct cam_sim *sim);
56 static void mly_cam_action(struct cam_sim *sim, union ccb *ccb);
57 static void mly_cam_complete(struct mly_command *mc);
58 static struct cam_periph *mly_find_periph(struct mly_softc *sc, int bus, int target);
60 /********************************************************************************
61 * CAM-specific queue primitives
64 mly_initq_ccb(struct mly_softc *sc)
66 TAILQ_INIT(&sc->mly_cam_ccbq);
67 MLYQ_INIT(sc, MLYQ_CCB);
71 mly_enqueue_ccb(struct mly_softc *sc, union ccb *ccb)
76 TAILQ_INSERT_TAIL(&sc->mly_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
77 MLYQ_ADD(sc, MLYQ_CCB);
82 mly_requeue_ccb(struct mly_softc *sc, union ccb *ccb)
87 TAILQ_INSERT_HEAD(&sc->mly_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
88 MLYQ_ADD(sc, MLYQ_CCB);
92 static __inline union ccb *
93 mly_dequeue_ccb(struct mly_softc *sc)
99 if ((ccb = (union ccb *)TAILQ_FIRST(&sc->mly_cam_ccbq)) != NULL) {
100 TAILQ_REMOVE(&sc->mly_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
101 MLYQ_REMOVE(sc, MLYQ_CCB);
107 /********************************************************************************
108 * space-fill a character string
111 padstr(char *targ, char *src, int len)
122 /********************************************************************************
123 * Attach the real and virtual SCSI busses to CAM
126 mly_cam_attach(struct mly_softc *sc)
128 struct cam_devq *devq;
133 /* initialise the CCB queue */
137 * Allocate a devq for all our channels combined.
139 if ((devq = cam_simq_alloc(sc->mly_controllerinfo->maximum_parallel_commands)) == NULL) {
140 mly_printf(sc, "can't allocate CAM SIM\n");
145 * Iterate over channels, registering them with CAM.
147 * Physical channels are set up to support tagged commands and only a single
148 * untagged command. Virtual channels do not support tags, and don't need them.
150 for (i = 0, chn = 0; i < sc->mly_controllerinfo->physical_channels_present; i++, chn++) {
152 if ((sc->mly_cam_sim[chn] = cam_sim_alloc(mly_cam_action,
156 device_get_unit(sc->mly_dev),
158 sc->mly_controllerinfo->maximum_parallel_commands,
160 mly_printf(sc, "CAM SIM attach failed\n");
164 for (i = 0; i < sc->mly_controllerinfo->virtual_channels_present; i++, chn++) {
166 if ((sc->mly_cam_sim[chn] = cam_sim_alloc(mly_cam_action,
170 device_get_unit(sc->mly_dev),
171 sc->mly_controllerinfo->maximum_parallel_commands,
174 mly_printf(sc, "CAM SIM attach failed\n");
178 cam_simq_release(devq);
180 for (i = 0; i < chn; i++) {
181 /* register the bus IDs so we can get them later */
182 if (xpt_bus_register(sc->mly_cam_sim[i], i)) {
183 mly_printf(sc, "CAM XPT bus registration failed\n");
186 debug(1, "registered sim %p bus %d", sc->mly_cam_sim[i], i);
192 /********************************************************************************
196 mly_cam_detach(struct mly_softc *sc)
202 nchn = sc->mly_controllerinfo->physical_channels_present +
203 sc->mly_controllerinfo->virtual_channels_present;
206 * Iterate over channels, deregistering as we go.
208 nchn = sc->mly_controllerinfo->physical_channels_present +
209 sc->mly_controllerinfo->virtual_channels_present;
210 for (chn = 0; chn < nchn; chn++) {
213 * If a sim was registered for this channel, free it.
215 if (sc->mly_cam_sim[chn] != NULL) {
216 debug(1, "deregister bus %d", chn);
217 xpt_bus_deregister(cam_sim_path(sc->mly_cam_sim[chn]));
218 debug(1, "free sim for channel %d", chn);
219 cam_sim_free(sc->mly_cam_sim[chn]);
224 /********************************************************************************
225 * Handle an action requested by CAM
228 mly_cam_action(struct cam_sim *sim, union ccb *ccb)
230 struct mly_softc *sc = cam_sim_softc(sim);
234 switch (ccb->ccb_h.func_code) {
236 /* perform SCSI I/O */
239 struct ccb_scsiio *csio = &ccb->csio;
242 bus = cam_sim_bus(sim);
243 target = csio->ccb_h.target_id;
245 debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, ccb->ccb_h.target_lun);
247 /* check for I/O attempt to a protected device */
248 if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_PROTECTED) {
249 debug(2, " device protected");
250 csio->ccb_h.status = CAM_REQ_CMP_ERR;
253 /* check for I/O attempt to nonexistent device */
254 if (!(sc->mly_btl[bus][target].mb_flags & (MLY_BTL_LOGICAL | MLY_BTL_PHYSICAL))) {
255 debug(2, " device does not exist");
256 csio->ccb_h.status = CAM_REQ_CMP_ERR;
259 /* XXX increase if/when we support large SCSI commands */
260 if (csio->cdb_len > MLY_CMD_SCSI_SMALL_CDB) {
261 debug(2, " command too large (%d > %d)", csio->cdb_len, MLY_CMD_SCSI_SMALL_CDB);
262 csio->ccb_h.status = CAM_REQ_CMP_ERR;
265 /* check that the CDB pointer is not to a physical address */
266 if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
267 debug(2, " CDB pointer is to physical address");
268 csio->ccb_h.status = CAM_REQ_CMP_ERR;
271 /* if there is data transfer, it must be to/from a virtual address */
272 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
273 if (csio->ccb_h.flags & CAM_DATA_PHYS) { /* we can't map it */
274 debug(2, " data pointer is to physical address");
275 csio->ccb_h.status = CAM_REQ_CMP_ERR;
277 if (csio->ccb_h.flags & CAM_SCATTER_VALID) { /* we want to do the s/g setup */
278 debug(2, " data has premature s/g setup");
279 csio->ccb_h.status = CAM_REQ_CMP_ERR;
283 /* abandon aborted ccbs or those that have failed validation */
284 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
285 debug(2, "abandoning CCB due to abort/validation failure");
289 /* save the channel number in the ccb */
290 csio->ccb_h.sim_priv.entries[0].field = bus;
292 /* enqueue the ccb and start I/O */
293 mly_enqueue_ccb(sc, ccb);
298 /* perform geometry calculations */
299 case XPT_CALC_GEOMETRY:
301 struct ccb_calc_geometry *ccg = &ccb->ccg;
302 u_int32_t secs_per_cylinder;
304 debug(2, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
306 if (sc->mly_controllerparam->bios_geometry == MLY_BIOSGEOM_8G) {
308 ccg->secs_per_track = 63;
309 } else { /* MLY_BIOSGEOM_2G */
311 ccg->secs_per_track = 32;
313 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
314 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
315 ccb->ccb_h.status = CAM_REQ_CMP;
319 /* handle path attribute inquiry */
322 struct ccb_pathinq *cpi = &ccb->cpi;
324 debug(2, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
326 cpi->version_num = 1;
327 cpi->hba_inquiry = PI_TAG_ABLE; /* XXX extra flags for physical channels? */
328 cpi->target_sprt = 0;
330 cpi->max_target = MLY_MAX_TARGETS - 1;
331 cpi->max_lun = MLY_MAX_LUNS - 1;
332 cpi->initiator_id = sc->mly_controllerparam->initiator_id;
333 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
334 strncpy(cpi->hba_vid, "BSDi", HBA_IDLEN);
335 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
336 cpi->unit_number = cam_sim_unit(sim);
337 cpi->bus_id = cam_sim_bus(sim);
338 cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */
339 ccb->ccb_h.status = CAM_REQ_CMP;
343 case XPT_GET_TRAN_SETTINGS:
345 struct ccb_trans_settings *cts = &ccb->cts;
348 bus = cam_sim_bus(sim);
349 target = cts->ccb_h.target_id;
351 debug(2, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
354 /* logical device? */
355 if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_LOGICAL) {
356 /* nothing special for these */
358 /* physical device? */
359 } else if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_PHYSICAL) {
360 /* allow CAM to try tagged transactions */
361 cts->flags |= CCB_TRANS_TAG_ENB;
362 cts->valid |= CCB_TRANS_TQ_VALID;
364 /* convert speed (MHz) to usec */
365 if (sc->mly_btl[bus][target].mb_speed == 0) {
366 cts->sync_period = 1000000 / 5;
368 cts->sync_period = 1000000 / sc->mly_btl[bus][target].mb_speed;
371 /* convert bus width to CAM internal encoding */
372 switch (sc->mly_btl[bus][target].mb_width) {
374 cts->bus_width = MSG_EXT_WDTR_BUS_32_BIT;
377 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
381 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
384 cts->valid |= CCB_TRANS_SYNC_RATE_VALID | CCB_TRANS_BUS_WIDTH_VALID;
386 /* not a device, bail out */
388 cts->ccb_h.status = CAM_REQ_CMP_ERR;
392 /* disconnect always OK */
393 cts->flags |= CCB_TRANS_DISC_ENB;
394 cts->valid |= CCB_TRANS_DISC_VALID;
396 cts->ccb_h.status = CAM_REQ_CMP;
400 default: /* we can't do this */
401 debug(2, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
402 ccb->ccb_h.status = CAM_REQ_INVALID;
409 /********************************************************************************
410 * Check for possibly-completed commands.
413 mly_cam_poll(struct cam_sim *sim)
415 struct mly_softc *sc = cam_sim_softc(sim);
422 /********************************************************************************
423 * Pull a CCB off the work queue and turn it into a command.
426 mly_cam_command(struct mly_softc *sc, struct mly_command **mcp)
428 struct mly_command *mc;
429 struct mly_command_scsi_small *ss;
430 struct ccb_scsiio *csio;
439 /* check for a CCB */
440 if (!(csio = (struct ccb_scsiio *)mly_dequeue_ccb(sc)))
443 /* get a command to back it */
444 if (mly_alloc_command(sc, &mc)) {
449 /* build the command */
450 mc->mc_data = csio->data_ptr;
451 mc->mc_length = csio->dxfer_len;
452 mc->mc_complete = mly_cam_complete;
453 mc->mc_private = csio;
455 /* build the packet for the controller */
456 ss = &mc->mc_packet->scsi_small;
457 ss->opcode = MDACMD_SCSI;
458 if (csio->ccb_h.flags * CAM_DIS_DISCONNECT)
459 ss->command_control.disable_disconnect = 1;
460 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
461 ss->command_control.data_direction = MLY_CCB_WRITE;
462 ss->data_size = csio->dxfer_len;
463 ss->addr.phys.lun = csio->ccb_h.target_lun;
464 ss->addr.phys.target = csio->ccb_h.target_id;
465 ss->addr.phys.channel = csio->ccb_h.sim_priv.entries[0].field;
466 if (csio->ccb_h.timeout < (60 * 1000)) {
467 ss->timeout.value = csio->ccb_h.timeout / 1000;
468 ss->timeout.scale = MLY_TIMEOUT_SECONDS;
469 } else if (csio->ccb_h.timeout < (60 * 60 * 1000)) {
470 ss->timeout.value = csio->ccb_h.timeout / (60 * 1000);
471 ss->timeout.scale = MLY_TIMEOUT_MINUTES;
473 ss->timeout.value = csio->ccb_h.timeout / (60 * 60 * 1000); /* overflow? */
474 ss->timeout.scale = MLY_TIMEOUT_HOURS;
476 ss->maximum_sense_size = csio->sense_len;
477 ss->cdb_length = csio->cdb_len;
478 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
479 bcopy(csio->cdb_io.cdb_ptr, ss->cdb, csio->cdb_len);
481 bcopy(csio->cdb_io.cdb_bytes, ss->cdb, csio->cdb_len);
487 mly_release_command(mc);
491 mly_requeue_ccb(sc, (union ccb *)csio);
497 /********************************************************************************
498 * Handle completion of a command - pass results back through the CCB
501 mly_cam_complete(struct mly_command *mc)
503 struct mly_softc *sc = mc->mc_sc;
504 struct ccb_scsiio *csio = (struct ccb_scsiio *)mc->mc_private;
505 struct scsi_inquiry_data *inq = (struct scsi_inquiry_data *)csio->data_ptr;
512 csio->scsi_status = mc->mc_status;
513 switch(mc->mc_status) {
516 * In order to report logical device type and status, we overwrite
517 * the result of the INQUIRY command to logical devices.
519 bus = csio->ccb_h.sim_priv.entries[0].field;
520 if (bus >= sc->mly_controllerinfo->physical_channels_present) {
521 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
522 cmd = *csio->cdb_io.cdb_ptr;
524 cmd = csio->cdb_io.cdb_bytes[0];
526 if (cmd == INQUIRY) {
527 target = csio->ccb_h.target_id;
528 btl = &sc->mly_btl[bus][target];
529 padstr(inq->vendor, mly_describe_code(mly_table_device_type, btl->mb_type), 8);
530 padstr(inq->product, mly_describe_code(mly_table_device_state, btl->mb_state), 16);
531 padstr(inq->revision, "", 4);
535 debug(2, "SCSI_STATUS_OK");
536 csio->ccb_h.status = CAM_REQ_CMP;
539 case SCSI_STATUS_CHECK_COND:
540 debug(2, "SCSI_STATUS_CHECK_COND sense %d resid %d", mc->mc_sense, mc->mc_resid);
541 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
542 bzero(&csio->sense_data, SSD_FULL_SIZE);
543 bcopy(mc->mc_packet, &csio->sense_data, mc->mc_sense);
544 csio->sense_len = mc->mc_sense;
545 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
546 csio->resid = mc->mc_resid; /* XXX this is a signed value... */
549 case SCSI_STATUS_BUSY:
550 debug(2, "SCSI_STATUS_BUSY");
551 csio->ccb_h.status = CAM_SCSI_BUSY;
555 debug(2, "unknown status 0x%x", csio->scsi_status);
556 csio->ccb_h.status = CAM_REQ_CMP_ERR;
559 xpt_done((union ccb *)csio);
560 mly_release_command(mc);
563 /********************************************************************************
564 * Find a peripheral attahed at (bus),(target)
566 static struct cam_periph *
567 mly_find_periph(struct mly_softc *sc, int bus, int target)
569 struct cam_periph *periph;
570 struct cam_path *path;
573 status = xpt_create_path(&path, NULL, cam_sim_path(sc->mly_cam_sim[bus]), target, 0);
574 if (status == CAM_REQ_CMP) {
575 periph = cam_periph_find(path, NULL);
583 /********************************************************************************
584 * Name the device at (bus)(target)
587 mly_name_device(struct mly_softc *sc, int bus, int target)
589 struct cam_periph *periph;
591 if ((periph = mly_find_periph(sc, bus, target)) != NULL) {
592 sprintf(sc->mly_btl[bus][target].mb_name, "%s%d", periph->periph_name, periph->unit_number);
595 sc->mly_btl[bus][target].mb_name[0] = 0;