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 $
30 * CAM interface for FreeBSD
33 #include <sys/param.h>
34 #include <sys/systm.h>
36 #include <sys/devicestat.h>
39 #include <cam/cam_ccb.h>
40 #include <cam/cam_periph.h>
41 #include <cam/cam_sim.h>
42 #include <cam/cam_xpt_sim.h>
43 #include <cam/scsi/scsi_all.h>
44 #include <cam/scsi/scsi_message.h>
46 #include <machine/resource.h>
47 #include <machine/bus.h>
49 #include <dev/mly/mlyreg.h>
50 #include <dev/mly/mlyio.h>
51 #include <dev/mly/mlyvar.h>
52 #include <dev/mly/mly_tables.h>
54 static void mly_cam_poll(struct cam_sim *sim);
55 static void mly_cam_action(struct cam_sim *sim, union ccb *ccb);
56 static void mly_cam_complete(struct mly_command *mc);
57 static struct cam_periph *mly_find_periph(struct mly_softc *sc, int bus, int target);
59 /********************************************************************************
60 * CAM-specific queue primitives
63 mly_initq_ccb(struct mly_softc *sc)
65 TAILQ_INIT(&sc->mly_cam_ccbq);
66 MLYQ_INIT(sc, MLYQ_CCB);
70 mly_enqueue_ccb(struct mly_softc *sc, union ccb *ccb)
75 TAILQ_INSERT_TAIL(&sc->mly_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
76 MLYQ_ADD(sc, MLYQ_CCB);
81 mly_requeue_ccb(struct mly_softc *sc, union ccb *ccb)
86 TAILQ_INSERT_HEAD(&sc->mly_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
87 MLYQ_ADD(sc, MLYQ_CCB);
91 static __inline union ccb *
92 mly_dequeue_ccb(struct mly_softc *sc)
98 if ((ccb = (union ccb *)TAILQ_FIRST(&sc->mly_cam_ccbq)) != NULL) {
99 TAILQ_REMOVE(&sc->mly_cam_ccbq, &ccb->ccb_h, sim_links.tqe);
100 MLYQ_REMOVE(sc, MLYQ_CCB);
106 /********************************************************************************
107 * space-fill a character string
110 padstr(char *targ, char *src, int len)
121 /********************************************************************************
122 * Attach the real and virtual SCSI busses to CAM
125 mly_cam_attach(struct mly_softc *sc)
127 struct cam_devq *devq;
132 /* initialise the CCB queue */
136 * Allocate a devq for all our channels combined.
138 if ((devq = cam_simq_alloc(sc->mly_controllerinfo->maximum_parallel_commands)) == NULL) {
139 mly_printf(sc, "can't allocate CAM SIM\n");
144 * Iterate over channels, registering them with CAM.
146 * Physical channels are set up to support tagged commands and only a single
147 * untagged command. Virtual channels do not support tags, and don't need them.
149 for (i = 0, chn = 0; i < sc->mly_controllerinfo->physical_channels_present; i++, chn++) {
151 if ((sc->mly_cam_sim[chn] = cam_sim_alloc(mly_cam_action,
155 device_get_unit(sc->mly_dev),
157 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,
175 mly_printf(sc, "CAM SIM attach failed\n");
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)
198 int chn, nchn, first;
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, first = 1; 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 (%sfree queue)", chn, first ? "" : "don't ");
219 cam_sim_free(sc->mly_cam_sim[chn], first ? TRUE : FALSE);
225 /********************************************************************************
226 * Handle an action requested by CAM
229 mly_cam_action(struct cam_sim *sim, union ccb *ccb)
231 struct mly_softc *sc = cam_sim_softc(sim);
235 switch (ccb->ccb_h.func_code) {
237 /* perform SCSI I/O */
240 struct ccb_scsiio *csio = &ccb->csio;
243 bus = cam_sim_bus(sim);
244 target = csio->ccb_h.target_id;
246 debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, ccb->ccb_h.target_lun);
248 /* check for I/O attempt to a protected device */
249 if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_PROTECTED) {
250 debug(2, " device protected");
251 csio->ccb_h.status = CAM_REQ_CMP_ERR;
254 /* check for I/O attempt to nonexistent device */
255 if (!(sc->mly_btl[bus][target].mb_flags & (MLY_BTL_LOGICAL | MLY_BTL_PHYSICAL))) {
256 debug(2, " device does not exist");
257 csio->ccb_h.status = CAM_REQ_CMP_ERR;
260 /* XXX increase if/when we support large SCSI commands */
261 if (csio->cdb_len > MLY_CMD_SCSI_SMALL_CDB) {
262 debug(2, " command too large (%d > %d)", csio->cdb_len, MLY_CMD_SCSI_SMALL_CDB);
263 csio->ccb_h.status = CAM_REQ_CMP_ERR;
266 /* check that the CDB pointer is not to a physical address */
267 if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
268 debug(2, " CDB pointer is to physical address");
269 csio->ccb_h.status = CAM_REQ_CMP_ERR;
272 /* if there is data transfer, it must be to/from a virtual address */
273 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
274 if (csio->ccb_h.flags & CAM_DATA_PHYS) { /* we can't map it */
275 debug(2, " data pointer is to physical address");
276 csio->ccb_h.status = CAM_REQ_CMP_ERR;
278 if (csio->ccb_h.flags & CAM_SCATTER_VALID) { /* we want to do the s/g setup */
279 debug(2, " data has premature s/g setup");
280 csio->ccb_h.status = CAM_REQ_CMP_ERR;
284 /* abandon aborted ccbs or those that have failed validation */
285 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
286 debug(2, "abandoning CCB due to abort/validation failure");
290 /* save the channel number in the ccb */
291 csio->ccb_h.sim_priv.entries[0].field = bus;
293 /* enqueue the ccb and start I/O */
294 mly_enqueue_ccb(sc, ccb);
299 /* perform geometry calculations */
300 case XPT_CALC_GEOMETRY:
302 struct ccb_calc_geometry *ccg = &ccb->ccg;
303 u_int32_t secs_per_cylinder;
305 debug(2, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
307 if (sc->mly_controllerparam->bios_geometry == MLY_BIOSGEOM_8G) {
309 ccg->secs_per_track = 63;
310 } else { /* MLY_BIOSGEOM_2G */
312 ccg->secs_per_track = 32;
314 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
315 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
316 ccb->ccb_h.status = CAM_REQ_CMP;
320 /* handle path attribute inquiry */
323 struct ccb_pathinq *cpi = &ccb->cpi;
325 debug(2, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
327 cpi->version_num = 1;
328 cpi->hba_inquiry = PI_TAG_ABLE; /* XXX extra flags for physical channels? */
329 cpi->target_sprt = 0;
331 cpi->max_target = MLY_MAX_TARGETS - 1;
332 cpi->max_lun = MLY_MAX_LUNS - 1;
333 cpi->initiator_id = sc->mly_controllerparam->initiator_id;
334 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
335 strncpy(cpi->hba_vid, "BSDi", HBA_IDLEN);
336 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
337 cpi->unit_number = cam_sim_unit(sim);
338 cpi->bus_id = cam_sim_bus(sim);
339 cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */
340 ccb->ccb_h.status = CAM_REQ_CMP;
344 case XPT_GET_TRAN_SETTINGS:
346 struct ccb_trans_settings *cts = &ccb->cts;
349 bus = cam_sim_bus(sim);
350 target = cts->ccb_h.target_id;
352 debug(2, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
355 /* logical device? */
356 if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_LOGICAL) {
357 /* nothing special for these */
359 /* physical device? */
360 } else if (sc->mly_btl[bus][target].mb_flags & MLY_BTL_PHYSICAL) {
361 /* allow CAM to try tagged transactions */
362 cts->flags |= CCB_TRANS_TAG_ENB;
363 cts->valid |= CCB_TRANS_TQ_VALID;
365 /* convert speed (MHz) to usec */
366 if (sc->mly_btl[bus][target].mb_speed == 0) {
367 cts->sync_period = 1000000 / 5;
369 cts->sync_period = 1000000 / sc->mly_btl[bus][target].mb_speed;
372 /* convert bus width to CAM internal encoding */
373 switch (sc->mly_btl[bus][target].mb_width) {
375 cts->bus_width = MSG_EXT_WDTR_BUS_32_BIT;
378 cts->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
382 cts->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
385 cts->valid |= CCB_TRANS_SYNC_RATE_VALID | CCB_TRANS_BUS_WIDTH_VALID;
387 /* not a device, bail out */
389 cts->ccb_h.status = CAM_REQ_CMP_ERR;
393 /* disconnect always OK */
394 cts->flags |= CCB_TRANS_DISC_ENB;
395 cts->valid |= CCB_TRANS_DISC_VALID;
397 cts->ccb_h.status = CAM_REQ_CMP;
401 default: /* we can't do this */
402 debug(2, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
403 ccb->ccb_h.status = CAM_REQ_INVALID;
410 /********************************************************************************
411 * Check for possibly-completed commands.
414 mly_cam_poll(struct cam_sim *sim)
416 struct mly_softc *sc = cam_sim_softc(sim);
423 /********************************************************************************
424 * Pull a CCB off the work queue and turn it into a command.
427 mly_cam_command(struct mly_softc *sc, struct mly_command **mcp)
429 struct mly_command *mc;
430 struct mly_command_scsi_small *ss;
431 struct ccb_scsiio *csio;
440 /* check for a CCB */
441 if (!(csio = (struct ccb_scsiio *)mly_dequeue_ccb(sc)))
444 /* get a command to back it */
445 if (mly_alloc_command(sc, &mc)) {
450 /* build the command */
451 mc->mc_data = csio->data_ptr;
452 mc->mc_length = csio->dxfer_len;
453 mc->mc_complete = mly_cam_complete;
454 mc->mc_private = csio;
456 /* build the packet for the controller */
457 ss = &mc->mc_packet->scsi_small;
458 ss->opcode = MDACMD_SCSI;
459 if (csio->ccb_h.flags * CAM_DIS_DISCONNECT)
460 ss->command_control.disable_disconnect = 1;
461 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT)
462 ss->command_control.data_direction = MLY_CCB_WRITE;
463 ss->data_size = csio->dxfer_len;
464 ss->addr.phys.lun = csio->ccb_h.target_lun;
465 ss->addr.phys.target = csio->ccb_h.target_id;
466 ss->addr.phys.channel = csio->ccb_h.sim_priv.entries[0].field;
467 if (csio->ccb_h.timeout < (60 * 1000)) {
468 ss->timeout.value = csio->ccb_h.timeout / 1000;
469 ss->timeout.scale = MLY_TIMEOUT_SECONDS;
470 } else if (csio->ccb_h.timeout < (60 * 60 * 1000)) {
471 ss->timeout.value = csio->ccb_h.timeout / (60 * 1000);
472 ss->timeout.scale = MLY_TIMEOUT_MINUTES;
474 ss->timeout.value = csio->ccb_h.timeout / (60 * 60 * 1000); /* overflow? */
475 ss->timeout.scale = MLY_TIMEOUT_HOURS;
477 ss->maximum_sense_size = csio->sense_len;
478 ss->cdb_length = csio->cdb_len;
479 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
480 bcopy(csio->cdb_io.cdb_ptr, ss->cdb, csio->cdb_len);
482 bcopy(csio->cdb_io.cdb_bytes, ss->cdb, csio->cdb_len);
488 mly_release_command(mc);
492 mly_requeue_ccb(sc, (union ccb *)csio);
498 /********************************************************************************
499 * Handle completion of a command - pass results back through the CCB
502 mly_cam_complete(struct mly_command *mc)
504 struct mly_softc *sc = mc->mc_sc;
505 struct ccb_scsiio *csio = (struct ccb_scsiio *)mc->mc_private;
506 struct scsi_inquiry_data *inq = (struct scsi_inquiry_data *)csio->data_ptr;
513 csio->scsi_status = mc->mc_status;
514 switch(mc->mc_status) {
517 * In order to report logical device type and status, we overwrite
518 * the result of the INQUIRY command to logical devices.
520 bus = csio->ccb_h.sim_priv.entries[0].field;
521 if (bus >= sc->mly_controllerinfo->physical_channels_present) {
522 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
523 cmd = *csio->cdb_io.cdb_ptr;
525 cmd = csio->cdb_io.cdb_bytes[0];
527 if (cmd == INQUIRY) {
528 target = csio->ccb_h.target_id;
529 btl = &sc->mly_btl[bus][target];
530 padstr(inq->vendor, mly_describe_code(mly_table_device_type, btl->mb_type), 8);
531 padstr(inq->product, mly_describe_code(mly_table_device_state, btl->mb_state), 16);
532 padstr(inq->revision, "", 4);
536 debug(2, "SCSI_STATUS_OK");
537 csio->ccb_h.status = CAM_REQ_CMP;
540 case SCSI_STATUS_CHECK_COND:
541 debug(2, "SCSI_STATUS_CHECK_COND sense %d resid %d", mc->mc_sense, mc->mc_resid);
542 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR;
543 bzero(&csio->sense_data, SSD_FULL_SIZE);
544 bcopy(mc->mc_packet, &csio->sense_data, mc->mc_sense);
545 csio->sense_len = mc->mc_sense;
546 csio->ccb_h.status |= CAM_AUTOSNS_VALID;
547 csio->resid = mc->mc_resid; /* XXX this is a signed value... */
550 case SCSI_STATUS_BUSY:
551 debug(2, "SCSI_STATUS_BUSY");
552 csio->ccb_h.status = CAM_SCSI_BUSY;
556 debug(2, "unknown status 0x%x", csio->scsi_status);
557 csio->ccb_h.status = CAM_REQ_CMP_ERR;
560 xpt_done((union ccb *)csio);
561 mly_release_command(mc);
564 /********************************************************************************
565 * Find a peripheral attahed at (bus),(target)
567 static struct cam_periph *
568 mly_find_periph(struct mly_softc *sc, int bus, int target)
570 struct cam_periph *periph;
571 struct cam_path *path;
574 status = xpt_create_path(&path, NULL, cam_sim_path(sc->mly_cam_sim[bus]), target, 0);
575 if (status == CAM_REQ_CMP) {
576 periph = cam_periph_find(path, NULL);
584 /********************************************************************************
585 * Name the device at (bus)(target)
588 mly_name_device(struct mly_softc *sc, int bus, int target)
590 struct cam_periph *periph;
592 if ((periph = mly_find_periph(sc, bus, target)) != NULL) {
593 sprintf(sc->mly_btl[bus][target].mb_name, "%s%d", periph->periph_name, periph->unit_number);
596 sc->mly_btl[bus][target].mb_name[0] = 0;