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.7 2006/12/20 18:14:40 dillon Exp $
31 * CAM interface for FreeBSD
34 #include <sys/param.h>
35 #include <sys/systm.h>
37 #include <sys/devicestat.h>
38 #include <sys/thread2.h>
40 #include <bus/cam/cam.h>
41 #include <bus/cam/cam_ccb.h>
42 #include <bus/cam/cam_periph.h>
43 #include <bus/cam/cam_sim.h>
44 #include <bus/cam/cam_xpt_sim.h>
45 #include <bus/cam/scsi/scsi_all.h>
46 #include <bus/cam/scsi/scsi_message.h>
51 #include "mly_tables.h"
53 static void mly_cam_poll(struct cam_sim *sim);
54 static void mly_cam_action(struct cam_sim *sim, union ccb *ccb);
55 static void mly_cam_complete(struct mly_command *mc);
56 static struct cam_periph *mly_find_periph(struct mly_softc *sc, int bus, int target);
58 /********************************************************************************
59 * CAM-specific queue primitives
62 mly_initq_ccb(struct mly_softc *sc)
64 TAILQ_INIT(&sc->mly_cam_ccbq);
65 MLYQ_INIT(sc, MLYQ_CCB);
69 mly_enqueue_ccb(struct mly_softc *sc, union ccb *ccb)
72 TAILQ_INSERT_TAIL(&sc->mly_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
73 MLYQ_ADD(sc, MLYQ_CCB);
78 mly_requeue_ccb(struct mly_softc *sc, union ccb *ccb)
81 TAILQ_INSERT_HEAD(&sc->mly_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
82 MLYQ_ADD(sc, MLYQ_CCB);
86 static __inline union ccb *
87 mly_dequeue_ccb(struct mly_softc *sc)
92 if ((ccb = (union ccb *)TAILQ_FIRST(&sc->mly_cam_ccbq)) != NULL) {
93 TAILQ_REMOVE(&sc->mly_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
94 MLYQ_REMOVE(sc, MLYQ_CCB);
100 /********************************************************************************
101 * space-fill a character string
104 padstr(char *targ, char *src, int len)
115 /********************************************************************************
116 * Attach the real and virtual SCSI busses to CAM
119 mly_cam_attach(struct mly_softc *sc)
121 struct cam_devq *devq;
126 /* initialise the CCB queue */
130 * Allocate a devq for all our channels combined.
132 if ((devq = cam_simq_alloc(sc->mly_controllerinfo->maximum_parallel_commands)) == NULL) {
133 mly_printf(sc, "can't allocate CAM SIM\n");
138 * Iterate over channels, registering them with CAM.
140 * Physical channels are set up to support tagged commands and only a single
141 * untagged command. Virtual channels do not support tags, and don't need them.
143 for (i = 0, chn = 0; i < sc->mly_controllerinfo->physical_channels_present; i++, chn++) {
145 if ((sc->mly_cam_sim[chn] = cam_sim_alloc(mly_cam_action,
149 device_get_unit(sc->mly_dev),
151 sc->mly_controllerinfo->maximum_parallel_commands,
153 mly_printf(sc, "CAM SIM attach failed\n");
157 for (i = 0; i < sc->mly_controllerinfo->virtual_channels_present; i++, chn++) {
159 if ((sc->mly_cam_sim[chn] = cam_sim_alloc(mly_cam_action,
163 device_get_unit(sc->mly_dev),
164 sc->mly_controllerinfo->maximum_parallel_commands,
167 mly_printf(sc, "CAM SIM attach failed\n");
171 cam_simq_release(devq);
173 for (i = 0; i < chn; i++) {
174 /* register the bus IDs so we can get them later */
175 if (xpt_bus_register(sc->mly_cam_sim[i], i)) {
176 mly_printf(sc, "CAM XPT bus registration failed\n");
179 debug(1, "registered sim %p bus %d", sc->mly_cam_sim[i], i);
185 /********************************************************************************
189 mly_cam_detach(struct mly_softc *sc)
195 nchn = sc->mly_controllerinfo->physical_channels_present +
196 sc->mly_controllerinfo->virtual_channels_present;
199 * Iterate over channels, deregistering as we go.
201 nchn = sc->mly_controllerinfo->physical_channels_present +
202 sc->mly_controllerinfo->virtual_channels_present;
203 for (chn = 0; chn < nchn; chn++) {
206 * If a sim was registered for this channel, free it.
208 if (sc->mly_cam_sim[chn] != NULL) {
209 debug(1, "deregister bus %d", chn);
210 xpt_bus_deregister(cam_sim_path(sc->mly_cam_sim[chn]));
211 debug(1, "free sim for channel %d", chn);
212 cam_sim_free(sc->mly_cam_sim[chn]);
217 /********************************************************************************
218 * Handle an action requested by CAM
221 mly_cam_action(struct cam_sim *sim, union ccb *ccb)
223 struct mly_softc *sc = cam_sim_softc(sim);
227 switch (ccb->ccb_h.func_code) {
229 /* perform SCSI I/O */
232 struct ccb_scsiio *csio = &ccb->csio;
235 bus = cam_sim_bus(sim);
236 target = csio->ccb_h.target_id;
238 debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, ccb->ccb_h.target_lun);
240 /* check for I/O attempt to a protected device */
241 if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_PROTECTED) {
242 debug(2, " device protected");
243 csio->ccb_h.status = CAM_REQ_CMP_ERR;
246 /* check for I/O attempt to nonexistent device */
247 if (!(sc->mly_btl[bus][target].mb_flags & (MLY_BTL_LOGICAL | MLY_BTL_PHYSICAL))) {
248 debug(2, " device does not exist");
249 csio->ccb_h.status = CAM_REQ_CMP_ERR;
252 /* XXX increase if/when we support large SCSI commands */
253 if (csio->cdb_len > MLY_CMD_SCSI_SMALL_CDB) {
254 debug(2, " command too large (%d > %d)", csio->cdb_len, MLY_CMD_SCSI_SMALL_CDB);
255 csio->ccb_h.status = CAM_REQ_CMP_ERR;
258 /* check that the CDB pointer is not to a physical address */
259 if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
260 debug(2, " CDB pointer is to physical address");
261 csio->ccb_h.status = CAM_REQ_CMP_ERR;
264 /* if there is data transfer, it must be to/from a virtual address */
265 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
266 if (csio->ccb_h.flags & CAM_DATA_PHYS) { /* we can't map it */
267 debug(2, " data pointer is to physical address");
268 csio->ccb_h.status = CAM_REQ_CMP_ERR;
270 if (csio->ccb_h.flags & CAM_SCATTER_VALID) { /* we want to do the s/g setup */
271 debug(2, " data has premature s/g setup");
272 csio->ccb_h.status = CAM_REQ_CMP_ERR;
276 /* abandon aborted ccbs or those that have failed validation */
277 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
278 debug(2, "abandoning CCB due to abort/validation failure");
282 /* save the channel number in the ccb */
283 csio->ccb_h.sim_priv.entries[0].field = bus;
285 /* enqueue the ccb and start I/O */
286 mly_enqueue_ccb(sc, ccb);
291 /* perform geometry calculations */
292 case XPT_CALC_GEOMETRY:
294 struct ccb_calc_geometry *ccg = &ccb->ccg;
295 u_int32_t secs_per_cylinder;
297 debug(2, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
299 if (sc->mly_controllerparam->bios_geometry == MLY_BIOSGEOM_8G) {
301 ccg->secs_per_track = 63;
302 } else { /* MLY_BIOSGEOM_2G */
304 ccg->secs_per_track = 32;
306 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
307 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
308 ccb->ccb_h.status = CAM_REQ_CMP;
312 /* handle path attribute inquiry */
315 struct ccb_pathinq *cpi = &ccb->cpi;
317 debug(2, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
319 cpi->version_num = 1;
320 cpi->hba_inquiry = PI_TAG_ABLE; /* XXX extra flags for physical channels? */
321 cpi->target_sprt = 0;
323 cpi->max_target = MLY_MAX_TARGETS - 1;
324 cpi->max_lun = MLY_MAX_LUNS - 1;
325 cpi->initiator_id = sc->mly_controllerparam->initiator_id;
326 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
327 strncpy(cpi->hba_vid, "BSDi", HBA_IDLEN);
328 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
329 cpi->unit_number = cam_sim_unit(sim);
330 cpi->bus_id = cam_sim_bus(sim);
331 cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */
332 ccb->ccb_h.status = CAM_REQ_CMP;
336 case XPT_GET_TRAN_SETTINGS:
338 struct ccb_trans_settings *cts = &ccb->cts;
341 bus = cam_sim_bus(sim);
342 target = cts->ccb_h.target_id;
344 debug(2, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
347 /* logical device? */
348 if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_LOGICAL) {
349 /* nothing special for these */
351 /* physical device? */
352 } else if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_PHYSICAL) {
353 /* allow CAM to try tagged transactions */
354 cts->flags |= CCB_TRANS_TAG_ENB;
355 cts->valid |= CCB_TRANS_TQ_VALID;
357 /* convert speed (MHz) to usec */
358 if (sc->mly_btl[bus][target].mb_speed == 0) {
359 cts->sync_period = 1000000 / 5;
361 cts->sync_period = 1000000 / sc->mly_btl[bus][target].mb_speed;
364 /* convert bus width to CAM internal encoding */
365 switch (sc->mly_btl[bus][target].mb_width) {
367 cts->bus_width = MSG_EXT_WDTR_BUS_32_BIT;
370 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
374 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
377 cts->valid |= CCB_TRANS_SYNC_RATE_VALID | CCB_TRANS_BUS_WIDTH_VALID;
379 /* not a device, bail out */
381 cts->ccb_h.status = CAM_REQ_CMP_ERR;
385 /* disconnect always OK */
386 cts->flags |= CCB_TRANS_DISC_ENB;
387 cts->valid |= CCB_TRANS_DISC_VALID;
389 cts->ccb_h.status = CAM_REQ_CMP;
393 default: /* we can't do this */
394 debug(2, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
395 ccb->ccb_h.status = CAM_REQ_INVALID;
402 /********************************************************************************
403 * Check for possibly-completed commands.
406 mly_cam_poll(struct cam_sim *sim)
408 struct mly_softc *sc = cam_sim_softc(sim);
415 /********************************************************************************
416 * Pull a CCB off the work queue and turn it into a command.
419 mly_cam_command(struct mly_softc *sc, struct mly_command **mcp)
421 struct mly_command *mc;
422 struct mly_command_scsi_small *ss;
423 struct ccb_scsiio *csio;
432 /* check for a CCB */
433 if (!(csio = (struct ccb_scsiio *)mly_dequeue_ccb(sc)))
436 /* get a command to back it */
437 if (mly_alloc_command(sc, &mc)) {
442 /* build the command */
443 mc->mc_data = csio->data_ptr;
444 mc->mc_length = csio->dxfer_len;
445 mc->mc_complete = mly_cam_complete;
446 mc->mc_private = csio;
448 /* build the packet for the controller */
449 ss = &mc->mc_packet->scsi_small;
450 ss->opcode = MDACMD_SCSI;
451 if (csio->ccb_h.flags * CAM_DIS_DISCONNECT)
452 ss->command_control.disable_disconnect = 1;
453 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
454 ss->command_control.data_direction = MLY_CCB_WRITE;
455 ss->data_size = csio->dxfer_len;
456 ss->addr.phys.lun = csio->ccb_h.target_lun;
457 ss->addr.phys.target = csio->ccb_h.target_id;
458 ss->addr.phys.channel = csio->ccb_h.sim_priv.entries[0].field;
459 if (csio->ccb_h.timeout < (60 * 1000)) {
460 ss->timeout.value = csio->ccb_h.timeout / 1000;
461 ss->timeout.scale = MLY_TIMEOUT_SECONDS;
462 } else if (csio->ccb_h.timeout < (60 * 60 * 1000)) {
463 ss->timeout.value = csio->ccb_h.timeout / (60 * 1000);
464 ss->timeout.scale = MLY_TIMEOUT_MINUTES;
466 ss->timeout.value = csio->ccb_h.timeout / (60 * 60 * 1000); /* overflow? */
467 ss->timeout.scale = MLY_TIMEOUT_HOURS;
469 ss->maximum_sense_size = csio->sense_len;
470 ss->cdb_length = csio->cdb_len;
471 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
472 bcopy(csio->cdb_io.cdb_ptr, ss->cdb, csio->cdb_len);
474 bcopy(csio->cdb_io.cdb_bytes, ss->cdb, csio->cdb_len);
480 mly_release_command(mc);
484 mly_requeue_ccb(sc, (union ccb *)csio);
490 /********************************************************************************
491 * Handle completion of a command - pass results back through the CCB
494 mly_cam_complete(struct mly_command *mc)
496 struct mly_softc *sc = mc->mc_sc;
497 struct ccb_scsiio *csio = (struct ccb_scsiio *)mc->mc_private;
498 struct scsi_inquiry_data *inq = (struct scsi_inquiry_data *)csio->data_ptr;
505 csio->scsi_status = mc->mc_status;
506 switch(mc->mc_status) {
509 * In order to report logical device type and status, we overwrite
510 * the result of the INQUIRY command to logical devices.
512 bus = csio->ccb_h.sim_priv.entries[0].field;
513 if (bus >= sc->mly_controllerinfo->physical_channels_present) {
514 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
515 cmd = *csio->cdb_io.cdb_ptr;
517 cmd = csio->cdb_io.cdb_bytes[0];
519 if (cmd == INQUIRY) {
520 target = csio->ccb_h.target_id;
521 btl = &sc->mly_btl[bus][target];
522 padstr(inq->vendor, mly_describe_code(mly_table_device_type, btl->mb_type), 8);
523 padstr(inq->product, mly_describe_code(mly_table_device_state, btl->mb_state), 16);
524 padstr(inq->revision, "", 4);
528 debug(2, "SCSI_STATUS_OK");
529 csio->ccb_h.status = CAM_REQ_CMP;
532 case SCSI_STATUS_CHECK_COND:
533 debug(2, "SCSI_STATUS_CHECK_COND sense %d resid %d", mc->mc_sense, mc->mc_resid);
534 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
535 bzero(&csio->sense_data, SSD_FULL_SIZE);
536 bcopy(mc->mc_packet, &csio->sense_data, mc->mc_sense);
537 csio->sense_len = mc->mc_sense;
538 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
539 csio->resid = mc->mc_resid; /* XXX this is a signed value... */
542 case SCSI_STATUS_BUSY:
543 debug(2, "SCSI_STATUS_BUSY");
544 csio->ccb_h.status = CAM_SCSI_BUSY;
548 debug(2, "unknown status 0x%x", csio->scsi_status);
549 csio->ccb_h.status = CAM_REQ_CMP_ERR;
552 xpt_done((union ccb *)csio);
553 mly_release_command(mc);
556 /********************************************************************************
557 * Find a peripheral attahed at (bus),(target)
559 static struct cam_periph *
560 mly_find_periph(struct mly_softc *sc, int bus, int target)
562 struct cam_periph *periph;
563 struct cam_path *path;
566 status = xpt_create_path(&path, NULL, cam_sim_path(sc->mly_cam_sim[bus]), target, 0);
567 if (status == CAM_REQ_CMP) {
568 periph = cam_periph_find(path, NULL);
576 /********************************************************************************
577 * Name the device at (bus)(target)
580 mly_name_device(struct mly_softc *sc, int bus, int target)
582 struct cam_periph *periph;
584 if ((periph = mly_find_periph(sc, bus, target)) != NULL) {
585 ksprintf(sc->mly_btl[bus][target].mb_name, "%s%d", periph->periph_name, periph->unit_number);
588 sc->mly_btl[bus][target].mb_name[0] = 0;