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.3 2003/08/07 21:17:09 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,
161 mly_printf(sc, "CAM SIM attach failed\n");
165 for (i = 0; i < sc->mly_controllerinfo->virtual_channels_present; i++, chn++) {
167 if ((sc->mly_cam_sim[chn] = cam_sim_alloc(mly_cam_action,
171 device_get_unit(sc->mly_dev),
172 sc->mly_controllerinfo->maximum_parallel_commands,
176 mly_printf(sc, "CAM SIM attach failed\n");
181 for (i = 0; i < chn; i++) {
182 /* register the bus IDs so we can get them later */
183 if (xpt_bus_register(sc->mly_cam_sim[i], i)) {
184 mly_printf(sc, "CAM XPT bus registration failed\n");
187 debug(1, "registered sim %p bus %d", sc->mly_cam_sim[i], i);
193 /********************************************************************************
197 mly_cam_detach(struct mly_softc *sc)
199 int chn, nchn, first;
203 nchn = sc->mly_controllerinfo->physical_channels_present +
204 sc->mly_controllerinfo->virtual_channels_present;
207 * Iterate over channels, deregistering as we go.
209 nchn = sc->mly_controllerinfo->physical_channels_present +
210 sc->mly_controllerinfo->virtual_channels_present;
211 for (chn = 0, first = 1; chn < nchn; chn++) {
214 * If a sim was registered for this channel, free it.
216 if (sc->mly_cam_sim[chn] != NULL) {
217 debug(1, "deregister bus %d", chn);
218 xpt_bus_deregister(cam_sim_path(sc->mly_cam_sim[chn]));
219 debug(1, "free sim for channel %d (%sfree queue)", chn, first ? "" : "don't ");
220 cam_sim_free(sc->mly_cam_sim[chn], first ? TRUE : FALSE);
226 /********************************************************************************
227 * Handle an action requested by CAM
230 mly_cam_action(struct cam_sim *sim, union ccb *ccb)
232 struct mly_softc *sc = cam_sim_softc(sim);
236 switch (ccb->ccb_h.func_code) {
238 /* perform SCSI I/O */
241 struct ccb_scsiio *csio = &ccb->csio;
244 bus = cam_sim_bus(sim);
245 target = csio->ccb_h.target_id;
247 debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, ccb->ccb_h.target_lun);
249 /* check for I/O attempt to a protected device */
250 if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_PROTECTED) {
251 debug(2, " device protected");
252 csio->ccb_h.status = CAM_REQ_CMP_ERR;
255 /* check for I/O attempt to nonexistent device */
256 if (!(sc->mly_btl[bus][target].mb_flags & (MLY_BTL_LOGICAL | MLY_BTL_PHYSICAL))) {
257 debug(2, " device does not exist");
258 csio->ccb_h.status = CAM_REQ_CMP_ERR;
261 /* XXX increase if/when we support large SCSI commands */
262 if (csio->cdb_len > MLY_CMD_SCSI_SMALL_CDB) {
263 debug(2, " command too large (%d > %d)", csio->cdb_len, MLY_CMD_SCSI_SMALL_CDB);
264 csio->ccb_h.status = CAM_REQ_CMP_ERR;
267 /* check that the CDB pointer is not to a physical address */
268 if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
269 debug(2, " CDB pointer is to physical address");
270 csio->ccb_h.status = CAM_REQ_CMP_ERR;
273 /* if there is data transfer, it must be to/from a virtual address */
274 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
275 if (csio->ccb_h.flags & CAM_DATA_PHYS) { /* we can't map it */
276 debug(2, " data pointer is to physical address");
277 csio->ccb_h.status = CAM_REQ_CMP_ERR;
279 if (csio->ccb_h.flags & CAM_SCATTER_VALID) { /* we want to do the s/g setup */
280 debug(2, " data has premature s/g setup");
281 csio->ccb_h.status = CAM_REQ_CMP_ERR;
285 /* abandon aborted ccbs or those that have failed validation */
286 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
287 debug(2, "abandoning CCB due to abort/validation failure");
291 /* save the channel number in the ccb */
292 csio->ccb_h.sim_priv.entries[0].field = bus;
294 /* enqueue the ccb and start I/O */
295 mly_enqueue_ccb(sc, ccb);
300 /* perform geometry calculations */
301 case XPT_CALC_GEOMETRY:
303 struct ccb_calc_geometry *ccg = &ccb->ccg;
304 u_int32_t secs_per_cylinder;
306 debug(2, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
308 if (sc->mly_controllerparam->bios_geometry == MLY_BIOSGEOM_8G) {
310 ccg->secs_per_track = 63;
311 } else { /* MLY_BIOSGEOM_2G */
313 ccg->secs_per_track = 32;
315 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
316 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
317 ccb->ccb_h.status = CAM_REQ_CMP;
321 /* handle path attribute inquiry */
324 struct ccb_pathinq *cpi = &ccb->cpi;
326 debug(2, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
328 cpi->version_num = 1;
329 cpi->hba_inquiry = PI_TAG_ABLE; /* XXX extra flags for physical channels? */
330 cpi->target_sprt = 0;
332 cpi->max_target = MLY_MAX_TARGETS - 1;
333 cpi->max_lun = MLY_MAX_LUNS - 1;
334 cpi->initiator_id = sc->mly_controllerparam->initiator_id;
335 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
336 strncpy(cpi->hba_vid, "BSDi", HBA_IDLEN);
337 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
338 cpi->unit_number = cam_sim_unit(sim);
339 cpi->bus_id = cam_sim_bus(sim);
340 cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */
341 ccb->ccb_h.status = CAM_REQ_CMP;
345 case XPT_GET_TRAN_SETTINGS:
347 struct ccb_trans_settings *cts = &ccb->cts;
350 bus = cam_sim_bus(sim);
351 target = cts->ccb_h.target_id;
353 debug(2, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
356 /* logical device? */
357 if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_LOGICAL) {
358 /* nothing special for these */
360 /* physical device? */
361 } else if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_PHYSICAL) {
362 /* allow CAM to try tagged transactions */
363 cts->flags |= CCB_TRANS_TAG_ENB;
364 cts->valid |= CCB_TRANS_TQ_VALID;
366 /* convert speed (MHz) to usec */
367 if (sc->mly_btl[bus][target].mb_speed == 0) {
368 cts->sync_period = 1000000 / 5;
370 cts->sync_period = 1000000 / sc->mly_btl[bus][target].mb_speed;
373 /* convert bus width to CAM internal encoding */
374 switch (sc->mly_btl[bus][target].mb_width) {
376 cts->bus_width = MSG_EXT_WDTR_BUS_32_BIT;
379 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
383 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
386 cts->valid |= CCB_TRANS_SYNC_RATE_VALID | CCB_TRANS_BUS_WIDTH_VALID;
388 /* not a device, bail out */
390 cts->ccb_h.status = CAM_REQ_CMP_ERR;
394 /* disconnect always OK */
395 cts->flags |= CCB_TRANS_DISC_ENB;
396 cts->valid |= CCB_TRANS_DISC_VALID;
398 cts->ccb_h.status = CAM_REQ_CMP;
402 default: /* we can't do this */
403 debug(2, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
404 ccb->ccb_h.status = CAM_REQ_INVALID;
411 /********************************************************************************
412 * Check for possibly-completed commands.
415 mly_cam_poll(struct cam_sim *sim)
417 struct mly_softc *sc = cam_sim_softc(sim);
424 /********************************************************************************
425 * Pull a CCB off the work queue and turn it into a command.
428 mly_cam_command(struct mly_softc *sc, struct mly_command **mcp)
430 struct mly_command *mc;
431 struct mly_command_scsi_small *ss;
432 struct ccb_scsiio *csio;
441 /* check for a CCB */
442 if (!(csio = (struct ccb_scsiio *)mly_dequeue_ccb(sc)))
445 /* get a command to back it */
446 if (mly_alloc_command(sc, &mc)) {
451 /* build the command */
452 mc->mc_data = csio->data_ptr;
453 mc->mc_length = csio->dxfer_len;
454 mc->mc_complete = mly_cam_complete;
455 mc->mc_private = csio;
457 /* build the packet for the controller */
458 ss = &mc->mc_packet->scsi_small;
459 ss->opcode = MDACMD_SCSI;
460 if (csio->ccb_h.flags * CAM_DIS_DISCONNECT)
461 ss->command_control.disable_disconnect = 1;
462 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
463 ss->command_control.data_direction = MLY_CCB_WRITE;
464 ss->data_size = csio->dxfer_len;
465 ss->addr.phys.lun = csio->ccb_h.target_lun;
466 ss->addr.phys.target = csio->ccb_h.target_id;
467 ss->addr.phys.channel = csio->ccb_h.sim_priv.entries[0].field;
468 if (csio->ccb_h.timeout < (60 * 1000)) {
469 ss->timeout.value = csio->ccb_h.timeout / 1000;
470 ss->timeout.scale = MLY_TIMEOUT_SECONDS;
471 } else if (csio->ccb_h.timeout < (60 * 60 * 1000)) {
472 ss->timeout.value = csio->ccb_h.timeout / (60 * 1000);
473 ss->timeout.scale = MLY_TIMEOUT_MINUTES;
475 ss->timeout.value = csio->ccb_h.timeout / (60 * 60 * 1000); /* overflow? */
476 ss->timeout.scale = MLY_TIMEOUT_HOURS;
478 ss->maximum_sense_size = csio->sense_len;
479 ss->cdb_length = csio->cdb_len;
480 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
481 bcopy(csio->cdb_io.cdb_ptr, ss->cdb, csio->cdb_len);
483 bcopy(csio->cdb_io.cdb_bytes, ss->cdb, csio->cdb_len);
489 mly_release_command(mc);
493 mly_requeue_ccb(sc, (union ccb *)csio);
499 /********************************************************************************
500 * Handle completion of a command - pass results back through the CCB
503 mly_cam_complete(struct mly_command *mc)
505 struct mly_softc *sc = mc->mc_sc;
506 struct ccb_scsiio *csio = (struct ccb_scsiio *)mc->mc_private;
507 struct scsi_inquiry_data *inq = (struct scsi_inquiry_data *)csio->data_ptr;
514 csio->scsi_status = mc->mc_status;
515 switch(mc->mc_status) {
518 * In order to report logical device type and status, we overwrite
519 * the result of the INQUIRY command to logical devices.
521 bus = csio->ccb_h.sim_priv.entries[0].field;
522 if (bus >= sc->mly_controllerinfo->physical_channels_present) {
523 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
524 cmd = *csio->cdb_io.cdb_ptr;
526 cmd = csio->cdb_io.cdb_bytes[0];
528 if (cmd == INQUIRY) {
529 target = csio->ccb_h.target_id;
530 btl = &sc->mly_btl[bus][target];
531 padstr(inq->vendor, mly_describe_code(mly_table_device_type, btl->mb_type), 8);
532 padstr(inq->product, mly_describe_code(mly_table_device_state, btl->mb_state), 16);
533 padstr(inq->revision, "", 4);
537 debug(2, "SCSI_STATUS_OK");
538 csio->ccb_h.status = CAM_REQ_CMP;
541 case SCSI_STATUS_CHECK_COND:
542 debug(2, "SCSI_STATUS_CHECK_COND sense %d resid %d", mc->mc_sense, mc->mc_resid);
543 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
544 bzero(&csio->sense_data, SSD_FULL_SIZE);
545 bcopy(mc->mc_packet, &csio->sense_data, mc->mc_sense);
546 csio->sense_len = mc->mc_sense;
547 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
548 csio->resid = mc->mc_resid; /* XXX this is a signed value... */
551 case SCSI_STATUS_BUSY:
552 debug(2, "SCSI_STATUS_BUSY");
553 csio->ccb_h.status = CAM_SCSI_BUSY;
557 debug(2, "unknown status 0x%x", csio->scsi_status);
558 csio->ccb_h.status = CAM_REQ_CMP_ERR;
561 xpt_done((union ccb *)csio);
562 mly_release_command(mc);
565 /********************************************************************************
566 * Find a peripheral attahed at (bus),(target)
568 static struct cam_periph *
569 mly_find_periph(struct mly_softc *sc, int bus, int target)
571 struct cam_periph *periph;
572 struct cam_path *path;
575 status = xpt_create_path(&path, NULL, cam_sim_path(sc->mly_cam_sim[bus]), target, 0);
576 if (status == CAM_REQ_CMP) {
577 periph = cam_periph_find(path, NULL);
585 /********************************************************************************
586 * Name the device at (bus)(target)
589 mly_name_device(struct mly_softc *sc, int bus, int target)
591 struct cam_periph *periph;
593 if ((periph = mly_find_periph(sc, bus, target)) != NULL) {
594 sprintf(sc->mly_btl[bus][target].mb_name, "%s%d", periph->periph_name, periph->unit_number);
597 sc->mly_btl[bus][target].mb_name[0] = 0;