2 * Copyright (c) 2001 Michael Smith
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * $FreeBSD: src/sys/dev/ciss/ciss.c,v 1.2.2.6 2003/02/18 22:27:41 ps Exp $
27 * $DragonFly: src/sys/dev/raid/ciss/ciss.c,v 1.7 2004/05/13 23:49:18 dillon Exp $
31 * Common Interface for SCSI-3 Support driver.
33 * CISS claims to provide a common interface between a generic SCSI
34 * transport and an intelligent host adapter.
36 * This driver supports CISS as defined in the document "CISS Command
37 * Interface for SCSI-3 Support Open Specification", Version 1.04,
38 * Valence Number 1, dated 20001127, produced by Compaq Computer
39 * Corporation. This document appears to be a hastily and somewhat
40 * arbitrarlily cut-down version of a larger (and probably even more
41 * chaotic and inconsistent) Compaq internal document. Various
42 * details were also gleaned from Compaq's "cciss" driver for Linux.
44 * We provide a shim layer between the CISS interface and CAM,
45 * offloading most of the queueing and being-a-disk chores onto CAM.
46 * Entry to the driver is via the PCI bus attachment (ciss_probe,
47 * ciss_attach, etc) and via the CAM interface (ciss_cam_action,
48 * ciss_cam_poll). The Compaq CISS adapters are, however, poor SCSI
49 * citizens and we have to fake up some responses to get reasonable
50 * behaviour out of them. In addition, the CISS command set is by no
51 * means adequate to support the functionality of a RAID controller,
52 * and thus the supported Compaq adapters utilise portions of the
53 * control protocol from earlier Compaq adapter families.
55 * Note that we only support the "simple" transport layer over PCI.
56 * This interface (ab)uses the I2O register set (specifically the post
57 * queues) to exchange commands with the adapter. Other interfaces
58 * are available, but we aren't supposed to know about them, and it is
59 * dubious whether they would provide major performance improvements
60 * except under extreme load.
62 * Currently the only supported CISS adapters are the Compaq Smart
63 * Array 5* series (5300, 5i, 532). Even with only three adapters,
64 * Compaq still manage to have interface variations.
67 * Thanks must go to Fred Harris and Darryl DeVinney at Compaq, as
68 * well as Paul Saab at Yahoo! for their assistance in making this
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/malloc.h>
75 #include <sys/kernel.h>
78 #include <sys/devicestat.h>
81 #include <bus/cam/cam.h>
82 #include <bus/cam/cam_ccb.h>
83 #include <bus/cam/cam_periph.h>
84 #include <bus/cam/cam_sim.h>
85 #include <bus/cam/cam_xpt_sim.h>
86 #include <bus/cam/scsi/scsi_all.h>
87 #include <bus/cam/scsi/scsi_message.h>
89 #include <machine/clock.h>
90 #include <machine/bus_memio.h>
91 #include <machine/bus.h>
92 #include <machine/endian.h>
93 #include <machine/resource.h>
96 #include <bus/pci/pcireg.h>
97 #include <bus/pci/pcivar.h>
103 MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data", "ciss internal data buffers");
106 static int ciss_lookup(device_t dev);
107 static int ciss_probe(device_t dev);
108 static int ciss_attach(device_t dev);
109 static int ciss_detach(device_t dev);
110 static int ciss_shutdown(device_t dev);
112 /* (de)initialisation functions, control wrappers */
113 static int ciss_init_pci(struct ciss_softc *sc);
114 static int ciss_wait_adapter(struct ciss_softc *sc);
115 static int ciss_flush_adapter(struct ciss_softc *sc);
116 static int ciss_init_requests(struct ciss_softc *sc);
117 static void ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
118 int nseg, int error);
119 static int ciss_identify_adapter(struct ciss_softc *sc);
120 static int ciss_init_logical(struct ciss_softc *sc);
121 static int ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
122 static int ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld);
123 static int ciss_update_config(struct ciss_softc *sc);
124 static int ciss_accept_media(struct ciss_softc *sc, int ldrive, int async);
125 static void ciss_accept_media_complete(struct ciss_request *cr);
126 static void ciss_free(struct ciss_softc *sc);
128 /* request submission/completion */
129 static int ciss_start(struct ciss_request *cr);
130 static void ciss_done(struct ciss_softc *sc);
131 static void ciss_intr(void *arg);
132 static void ciss_complete(struct ciss_softc *sc);
133 static int ciss_report_request(struct ciss_request *cr, int *command_status,
135 static int ciss_synch_request(struct ciss_request *cr, int timeout);
136 static int ciss_poll_request(struct ciss_request *cr, int timeout);
137 static int ciss_wait_request(struct ciss_request *cr, int timeout);
139 static int ciss_abort_request(struct ciss_request *cr);
142 /* request queueing */
143 static int ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
144 static void ciss_preen_command(struct ciss_request *cr);
145 static void ciss_release_request(struct ciss_request *cr);
147 /* request helpers */
148 static int ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
149 int opcode, void **bufp, size_t bufsize);
150 static int ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
153 static int ciss_map_request(struct ciss_request *cr);
154 static void ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
155 int nseg, int error);
156 static void ciss_unmap_request(struct ciss_request *cr);
159 static int ciss_cam_init(struct ciss_softc *sc);
160 static void ciss_cam_rescan_target(struct ciss_softc *sc, int target);
161 static void ciss_cam_rescan_all(struct ciss_softc *sc);
162 static void ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb);
163 static void ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
164 static int ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
165 static int ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
166 static void ciss_cam_poll(struct cam_sim *sim);
167 static void ciss_cam_complete(struct ciss_request *cr);
168 static void ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
169 static struct cam_periph *ciss_find_periph(struct ciss_softc *sc, int target);
170 static int ciss_name_device(struct ciss_softc *sc, int target);
172 /* periodic status monitoring */
173 static void ciss_periodic(void *arg);
174 static void ciss_notify_event(struct ciss_softc *sc);
175 static void ciss_notify_complete(struct ciss_request *cr);
176 static int ciss_notify_abort(struct ciss_softc *sc);
177 static int ciss_notify_abort_bmic(struct ciss_softc *sc);
178 static void ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
179 static void ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
181 /* debugging output */
182 static void ciss_print_request(struct ciss_request *cr);
183 static void ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
184 static const char *ciss_name_ldrive_status(int status);
185 static int ciss_decode_ldrive_status(int status);
186 static const char *ciss_name_ldrive_org(int org);
187 static const char *ciss_name_command_status(int status);
192 static device_method_t ciss_methods[] = {
193 /* Device interface */
194 DEVMETHOD(device_probe, ciss_probe),
195 DEVMETHOD(device_attach, ciss_attach),
196 DEVMETHOD(device_detach, ciss_detach),
197 DEVMETHOD(device_shutdown, ciss_shutdown),
201 static driver_t ciss_pci_driver = {
204 sizeof(struct ciss_softc)
207 static devclass_t ciss_devclass;
209 DECLARE_DUMMY_MODULE(ciss);
210 DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
213 * Control device interface.
215 static d_open_t ciss_open;
216 static d_close_t ciss_close;
217 static d_ioctl_t ciss_ioctl;
219 #define CISS_CDEV_MAJOR 166
221 static struct cdevsw ciss_cdevsw = {
223 /* cmaj */ CISS_CDEV_MAJOR,
227 ciss_open, ciss_close, noread, nowrite, ciss_ioctl,
228 nopoll, nommap, nostrategy,
229 nodump, nopsize, nokqfilter
232 /************************************************************************
233 * CISS adapters amazingly don't have a defined programming interface
234 * value. (One could say some very despairing things about PCI and
235 * people just not getting the general idea.) So we are forced to
236 * stick with matching against subvendor/subdevice, and thus have to
237 * be updated for every new CISS adapter that appears.
239 #define CISS_BOARD_SA5 (1<<0)
240 #define CISS_BOARD_SA5B (1<<1)
248 } ciss_vendor_data[] = {
249 { 0x0e11, 0x4070, CISS_BOARD_SA5, "Compaq Smart Array 5300" },
250 { 0x0e11, 0x4080, CISS_BOARD_SA5B, "Compaq Smart Array 5i" },
251 { 0x0e11, 0x4082, CISS_BOARD_SA5B, "Compaq Smart Array 532" },
252 { 0x0e11, 0x4083, CISS_BOARD_SA5B, "HP Smart Array 5312" },
253 { 0x0e11, 0x409A, CISS_BOARD_SA5B, "HP Smart Array 641" },
254 { 0x0e11, 0x409B, CISS_BOARD_SA5B, "HP Smart Array 642" },
255 { 0x0e11, 0x409C, CISS_BOARD_SA5B, "HP Smart Array 6400" },
259 /************************************************************************
260 * Find a match for the device in our list of known adapters.
263 ciss_lookup(device_t dev)
267 for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
268 if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
269 (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
275 /************************************************************************
276 * Match a known CISS adapter.
279 ciss_probe(device_t dev)
283 i = ciss_lookup(dev);
285 device_set_desc(dev, ciss_vendor_data[i].desc);
291 /************************************************************************
292 * Attach the driver to this adapter.
295 ciss_attach(device_t dev)
297 struct ciss_softc *sc;
303 /* print structure/union sizes */
304 debug_struct(ciss_command);
305 debug_struct(ciss_header);
306 debug_union(ciss_device_address);
307 debug_struct(ciss_cdb);
308 debug_struct(ciss_report_cdb);
309 debug_struct(ciss_notify_cdb);
310 debug_struct(ciss_notify);
311 debug_struct(ciss_message_cdb);
312 debug_struct(ciss_error_info_pointer);
313 debug_struct(ciss_error_info);
314 debug_struct(ciss_sg_entry);
315 debug_struct(ciss_config_table);
316 debug_struct(ciss_bmic_cdb);
317 debug_struct(ciss_bmic_id_ldrive);
318 debug_struct(ciss_bmic_id_lstatus);
319 debug_struct(ciss_bmic_id_table);
320 debug_struct(ciss_bmic_id_pdrive);
321 debug_struct(ciss_bmic_blink_pdrive);
322 debug_struct(ciss_bmic_flush_cache);
323 debug_const(CISS_MAX_REQUESTS);
324 debug_const(CISS_MAX_LOGICAL);
325 debug_const(CISS_INTERRUPT_COALESCE_DELAY);
326 debug_const(CISS_INTERRUPT_COALESCE_COUNT);
327 debug_const(CISS_COMMAND_ALLOC_SIZE);
328 debug_const(CISS_COMMAND_SG_LENGTH);
330 debug_type(cciss_pci_info_struct);
331 debug_type(cciss_coalint_struct);
332 debug_type(cciss_coalint_struct);
333 debug_type(NodeName_type);
334 debug_type(NodeName_type);
335 debug_type(Heartbeat_type);
336 debug_type(BusTypes_type);
337 debug_type(FirmwareVer_type);
338 debug_type(DriverVer_type);
339 debug_type(IOCTL_Command_struct);
342 sc = device_get_softc(dev);
346 * Work out adapter type.
348 i = ciss_lookup(dev);
349 if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
350 sc->ciss_interrupt_mask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
351 } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
352 sc->ciss_interrupt_mask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
354 /* really an error on our part */
355 ciss_printf(sc, "unable to determine hardware type\n");
361 * Do PCI-specific init.
363 if ((error = ciss_init_pci(sc)) != 0)
367 * Initialise driver queues.
371 ciss_initq_complete(sc);
374 * Initialise command/request pool.
376 if ((error = ciss_init_requests(sc)) != 0)
380 * Get adapter information.
382 if ((error = ciss_identify_adapter(sc)) != 0)
386 * Build our private table of logical devices.
388 if ((error = ciss_init_logical(sc)) != 0)
392 * Enable interrupts so that the CAM scan can complete.
394 CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
397 * Initialise the CAM interface.
399 if ((error = ciss_cam_init(sc)) != 0)
403 * Start the heartbeat routine and event chain.
408 * Create the control device.
410 sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
411 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
412 "ciss%d", device_get_unit(sc->ciss_dev));
413 sc->ciss_dev_t->si_drv1 = sc;
416 * The adapter is running; synchronous commands can now sleep
417 * waiting for an interrupt to signal completion.
419 sc->ciss_flags |= CISS_FLAG_RUNNING;
428 /************************************************************************
429 * Detach the driver from this adapter.
432 ciss_detach(device_t dev)
434 struct ciss_softc *sc = device_get_softc(dev);
438 /* flush adapter cache */
439 ciss_flush_adapter(sc);
441 /* release all resources */
448 /************************************************************************
449 * Prepare adapter for system shutdown.
452 ciss_shutdown(device_t dev)
454 struct ciss_softc *sc = device_get_softc(dev);
458 /* flush adapter cache */
459 ciss_flush_adapter(sc);
464 /************************************************************************
465 * Perform PCI-specific attachment actions.
468 ciss_init_pci(struct ciss_softc *sc)
470 uintptr_t cbase, csize, cofs;
476 * Allocate register window first (we need this to find the config
480 sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
481 if ((sc->ciss_regs_resource =
482 bus_alloc_resource(sc->ciss_dev, SYS_RES_MEMORY, &sc->ciss_regs_rid,
483 0, ~0, 1, RF_ACTIVE)) == NULL) {
484 ciss_printf(sc, "can't allocate register window\n");
487 sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
488 sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
491 * Find the BAR holding the config structure. If it's not the one
492 * we already mapped for registers, map it too.
494 sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
495 if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
496 if ((sc->ciss_cfg_resource =
497 bus_alloc_resource(sc->ciss_dev, SYS_RES_MEMORY, &sc->ciss_cfg_rid,
498 0, ~0, 1, RF_ACTIVE)) == NULL) {
499 ciss_printf(sc, "can't allocate config window\n");
502 cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
503 csize = rman_get_end(sc->ciss_cfg_resource) -
504 rman_get_start(sc->ciss_cfg_resource) + 1;
506 cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
507 csize = rman_get_end(sc->ciss_regs_resource) -
508 rman_get_start(sc->ciss_regs_resource) + 1;
510 cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
513 * Use the base/size/offset values we just calculated to
514 * sanity-check the config structure. If it's OK, point to it.
516 if ((cofs + sizeof(struct ciss_config_table)) > csize) {
517 ciss_printf(sc, "config table outside window\n");
520 sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
521 debug(1, "config struct at %p", sc->ciss_cfg);
524 * Validate the config structure. If we supported other transport
525 * methods, we could select amongst them at this point in time.
527 if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
528 ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
529 sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
530 sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
533 if ((sc->ciss_cfg->valence < CISS_MIN_VALENCE) ||
534 (sc->ciss_cfg->valence > CISS_MAX_VALENCE)) {
535 ciss_printf(sc, "adapter interface specification (%d) unsupported\n",
536 sc->ciss_cfg->valence);
541 * Put the board into simple mode, and tell it we're using the low
542 * 4GB of RAM. Set the default interrupt coalescing options.
544 if (!(sc->ciss_cfg->supported_methods & CISS_TRANSPORT_METHOD_SIMPLE)) {
545 ciss_printf(sc, "adapter does not support 'simple' transport layer\n");
548 sc->ciss_cfg->requested_method = CISS_TRANSPORT_METHOD_SIMPLE;
549 sc->ciss_cfg->command_physlimit = 0;
550 sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
551 sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
553 if (ciss_update_config(sc)) {
554 ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
555 CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
558 if (!(sc->ciss_cfg->active_method != CISS_TRANSPORT_METHOD_SIMPLE)) {
560 "adapter refuses to go into 'simple' transport mode (0x%x, 0x%x)\n",
561 sc->ciss_cfg->supported_methods, sc->ciss_cfg->active_method);
566 * Wait for the adapter to come ready.
568 if ((error = ciss_wait_adapter(sc)) != 0)
572 * Turn off interrupts before we go routing anything.
574 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
577 * Allocate and set up our interrupt.
579 sc->ciss_irq_rid = 0;
580 if ((sc->ciss_irq_resource =
581 bus_alloc_resource(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid, 0, ~0, 1,
582 RF_ACTIVE | RF_SHAREABLE)) == NULL) {
583 ciss_printf(sc, "can't allocate interrupt\n");
586 if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource, INTR_TYPE_CAM, ciss_intr, sc,
588 ciss_printf(sc, "can't set up interrupt\n");
593 * Allocate the parent bus DMA tag appropriate for our PCI
596 * Note that "simple" adapters can only address within a 32-bit
599 if (bus_dma_tag_create(NULL, /* parent */
600 1, 0, /* alignment, boundary */
601 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
602 BUS_SPACE_MAXADDR, /* highaddr */
603 NULL, NULL, /* filter, filterarg */
604 MAXBSIZE, CISS_COMMAND_SG_LENGTH, /* maxsize, nsegments */
605 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
606 BUS_DMA_ALLOCNOW, /* flags */
607 &sc->ciss_parent_dmat)) {
608 ciss_printf(sc, "can't allocate parent DMA tag\n");
613 * Create DMA tag for mapping buffers into adapter-addressable
616 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
617 1, 0, /* alignment, boundary */
618 BUS_SPACE_MAXADDR, /* lowaddr */
619 BUS_SPACE_MAXADDR, /* highaddr */
620 NULL, NULL, /* filter, filterarg */
621 MAXBSIZE, CISS_COMMAND_SG_LENGTH, /* maxsize, nsegments */
622 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
624 &sc->ciss_buffer_dmat)) {
625 ciss_printf(sc, "can't allocate buffer DMA tag\n");
631 /************************************************************************
632 * Wait for the adapter to come ready.
635 ciss_wait_adapter(struct ciss_softc *sc)
642 * Wait for the adapter to come ready.
644 if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
645 ciss_printf(sc, "waiting for adapter to come ready...\n");
646 for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
647 DELAY(1000000); /* one second */
649 ciss_printf(sc, "timed out waiting for adapter to come ready\n");
657 /************************************************************************
658 * Flush the adapter cache.
661 ciss_flush_adapter(struct ciss_softc *sc)
663 struct ciss_request *cr;
664 struct ciss_bmic_flush_cache *cbfc;
665 int error, command_status;
673 * Build a BMIC request to flush the cache. We don't disable
674 * it, as we may be going to do more I/O (eg. we are emulating
675 * the Synchronise Cache command).
677 if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
681 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
682 (void **)&cbfc, sizeof(*cbfc))) != 0)
686 * Submit the request and wait for it to complete.
688 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
689 ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
696 ciss_report_request(cr, &command_status, NULL);
697 switch(command_status) {
698 case CISS_CMD_STATUS_SUCCESS:
701 ciss_printf(sc, "error flushing cache (%s)\n",
702 ciss_name_command_status(command_status));
709 free(cbfc, CISS_MALLOC_CLASS);
711 ciss_release_request(cr);
715 /************************************************************************
716 * Allocate memory for the adapter command structures, initialise
717 * the request structures.
719 * Note that the entire set of commands are allocated in a single
723 ciss_init_requests(struct ciss_softc *sc)
725 struct ciss_request *cr;
731 * Calculate the number of request structures/commands we are
732 * going to provide for this adapter.
734 sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
736 if (1/*bootverbose*/)
737 ciss_printf(sc, "using %d of %d available commands\n",
738 sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
741 * Create the DMA tag for commands.
743 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
744 1, 0, /* alignment, boundary */
745 BUS_SPACE_MAXADDR, /* lowaddr */
746 BUS_SPACE_MAXADDR, /* highaddr */
747 NULL, NULL, /* filter, filterarg */
748 CISS_COMMAND_ALLOC_SIZE *
749 sc->ciss_max_requests, 1, /* maxsize, nsegments */
750 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
752 &sc->ciss_command_dmat)) {
753 ciss_printf(sc, "can't allocate command DMA tag\n");
757 * Allocate memory and make it available for DMA.
759 if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
760 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
761 ciss_printf(sc, "can't allocate command memory\n");
764 bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map, sc->ciss_command,
765 sizeof(struct ciss_command) * sc->ciss_max_requests,
766 ciss_command_map_helper, sc, 0);
767 bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
770 * Set up the request and command structures, push requests onto
773 for (i = 1; i < sc->ciss_max_requests; i++) {
774 cr = &sc->ciss_request[i];
777 ciss_enqueue_free(cr);
783 ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
785 struct ciss_softc *sc = (struct ciss_softc *)arg;
787 sc->ciss_command_phys = segs->ds_addr;
790 /************************************************************************
791 * Identify the adapter, print some information about it.
794 ciss_identify_adapter(struct ciss_softc *sc)
796 struct ciss_request *cr;
797 int error, command_status;
804 * Get a request, allocate storage for the adapter data.
806 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
807 (void **)&sc->ciss_id,
808 sizeof(*sc->ciss_id))) != 0)
812 * Submit the request and wait for it to complete.
814 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
815 ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
822 ciss_report_request(cr, &command_status, NULL);
823 switch(command_status) {
824 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
826 case CISS_CMD_STATUS_DATA_UNDERRUN:
827 case CISS_CMD_STATUS_DATA_OVERRUN:
828 ciss_printf(sc, "data over/underrun reading adapter information\n");
830 ciss_printf(sc, "error reading adapter information (%s)\n",
831 ciss_name_command_status(command_status));
836 /* sanity-check reply */
837 if (!sc->ciss_id->big_map_supported) {
838 ciss_printf(sc, "adapter does not support BIG_MAP\n");
844 /* XXX later revisions may not need this */
845 sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
848 /* XXX only really required for old 5300 adapters? */
849 sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
851 /* print information */
852 if (1/*bootverbose*/) {
853 ciss_printf(sc, " %d logical drive%s configured\n",
854 sc->ciss_id->configured_logical_drives,
855 (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
856 ciss_printf(sc, " firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
857 ciss_printf(sc, " %d SCSI channels\n", sc->ciss_id->scsi_bus_count);
859 ciss_printf(sc, " signature '%.4s'\n", sc->ciss_cfg->signature);
860 ciss_printf(sc, " valence %d\n", sc->ciss_cfg->valence);
861 ciss_printf(sc, " supported I/O methods 0x%b\n",
862 sc->ciss_cfg->supported_methods,
863 "\20\1READY\2simple\3performant\4MEMQ\n");
864 ciss_printf(sc, " active I/O method 0x%b\n",
865 sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
866 ciss_printf(sc, " 4G page base 0x%08x\n",
867 sc->ciss_cfg->command_physlimit);
868 ciss_printf(sc, " interrupt coalesce delay %dus\n",
869 sc->ciss_cfg->interrupt_coalesce_delay);
870 ciss_printf(sc, " interrupt coalesce count %d\n",
871 sc->ciss_cfg->interrupt_coalesce_count);
872 ciss_printf(sc, " max outstanding commands %d\n",
873 sc->ciss_cfg->max_outstanding_commands);
874 ciss_printf(sc, " bus types 0x%b\n", sc->ciss_cfg->bus_types,
875 "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
876 ciss_printf(sc, " server name '%.16s'\n", sc->ciss_cfg->server_name);
877 ciss_printf(sc, " heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
882 if (sc->ciss_id != NULL) {
883 free(sc->ciss_id, CISS_MALLOC_CLASS);
888 ciss_release_request(cr);
892 /************************************************************************
893 * Find logical drives on the adapter.
896 ciss_init_logical(struct ciss_softc *sc)
898 struct ciss_request *cr;
899 struct ciss_command *cc;
900 struct ciss_report_cdb *crc;
901 struct ciss_lun_report *cll;
913 * Get a request, allocate storage for the address list.
915 if ((error = ciss_get_request(sc, &cr)) != 0)
917 report_size = sizeof(*cll) + CISS_MAX_LOGICAL * sizeof(union ciss_device_address);
918 if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
919 ciss_printf(sc, "can't allocate memory for logical drive list\n");
925 * Build the Report Logical LUNs command.
927 cc = CISS_FIND_COMMAND(cr);
929 cr->cr_length = report_size;
930 cr->cr_flags = CISS_REQ_DATAIN;
932 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
933 cc->header.address.physical.bus = 0;
934 cc->header.address.physical.target = 0;
935 cc->cdb.cdb_length = sizeof(*crc);
936 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
937 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
938 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
939 cc->cdb.timeout = 30; /* XXX better suggestions? */
941 crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
942 bzero(crc, sizeof(*crc));
943 crc->opcode = CISS_OPCODE_REPORT_LOGICAL_LUNS;
944 crc->length = htonl(report_size); /* big-endian field */
945 cll->list_size = htonl(report_size - sizeof(*cll)); /* big-endian field */
948 * Submit the request and wait for it to complete. (timeout
949 * here should be much greater than above)
951 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
952 ciss_printf(sc, "error sending Report Logical LUNs command (%d)\n", error);
957 * Check response. Note that data over/underrun is OK.
959 ciss_report_request(cr, &command_status, NULL);
960 switch(command_status) {
961 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
962 case CISS_CMD_STATUS_DATA_UNDERRUN: /* buffer too large, not bad */
964 case CISS_CMD_STATUS_DATA_OVERRUN:
965 ciss_printf(sc, "WARNING: more logical drives than driver limit (%d), adjust CISS_MAX_LOGICAL\n",
969 ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
970 ciss_name_command_status(command_status));
974 ciss_release_request(cr);
977 /* sanity-check reply */
978 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
979 if ((ndrives < 0) || (ndrives > CISS_MAX_LOGICAL)) {
980 ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
981 ndrives, CISS_MAX_LOGICAL);
986 * Save logical drive information.
988 if (1/*bootverbose*/)
989 ciss_printf(sc, "%d logical drive%s\n", ndrives, (ndrives > 1) ? "s" : "");
990 if (ndrives != sc->ciss_id->configured_logical_drives)
991 ciss_printf(sc, "logical drive map claims %d drives, but adapter claims %d\n",
992 ndrives, sc->ciss_id->configured_logical_drives);
993 for (i = 0; i < CISS_MAX_LOGICAL; i++) {
995 sc->ciss_logical[i].cl_address = cll->lun[i]; /* XXX endianness? */
996 if (ciss_identify_logical(sc, &sc->ciss_logical[i]) != 0)
999 * If the drive has had media exchanged, we should bring it online.
1001 if (sc->ciss_logical[i].cl_lstatus->media_exchanged)
1002 ciss_accept_media(sc, i, 0);
1005 sc->ciss_logical[i].cl_status = CISS_LD_NONEXISTENT;
1012 * Note that if the error is a timeout, we are taking a slight
1013 * risk here and assuming that the adapter will not respond at a
1014 * later time, scribbling over host memory.
1017 ciss_release_request(cr);
1019 free(cll, CISS_MALLOC_CLASS);
1024 ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1026 struct ciss_request *cr;
1027 struct ciss_command *cc;
1028 struct scsi_inquiry *inq;
1034 lun = ld->cl_address.logical.lun;
1036 bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1038 if ((error = ciss_get_request(sc, &cr)) != 0)
1041 cc = CISS_FIND_COMMAND(cr);
1042 cr->cr_data = &ld->cl_geometry;
1043 cr->cr_length = sizeof(ld->cl_geometry);
1044 cr->cr_flags = CISS_REQ_DATAIN;
1046 cc->header.address.logical.mode = CISS_HDR_ADDRESS_MODE_LOGICAL;
1047 cc->header.address.logical.lun = lun;
1048 cc->cdb.cdb_length = 6;
1049 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1050 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1051 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1052 cc->cdb.timeout = 30;
1054 inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1055 inq->opcode = INQUIRY;
1056 inq->byte2 = SI_EVPD;
1057 inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1058 inq->length = sizeof(ld->cl_geometry);
1060 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1061 ciss_printf(sc, "error getting geometry (%d)\n", error);
1065 ciss_report_request(cr, &command_status, NULL);
1066 switch(command_status) {
1067 case CISS_CMD_STATUS_SUCCESS:
1068 case CISS_CMD_STATUS_DATA_UNDERRUN:
1070 case CISS_CMD_STATUS_DATA_OVERRUN:
1071 ciss_printf(sc, "WARNING: Data overrun\n");
1074 ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1075 ciss_name_command_status(command_status));
1081 ciss_release_request(cr);
1084 /************************************************************************
1085 * Identify a logical drive, initialise state related to it.
1088 ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1090 struct ciss_request *cr;
1091 struct ciss_command *cc;
1092 struct ciss_bmic_cdb *cbc;
1093 int error, command_status;
1100 * Build a BMIC request to fetch the drive ID.
1102 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
1103 (void **)&ld->cl_ldrive,
1104 sizeof(*ld->cl_ldrive))) != 0)
1106 cc = CISS_FIND_COMMAND(cr);
1107 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1108 cbc->log_drive = ld->cl_address.logical.lun;
1111 * Submit the request and wait for it to complete.
1113 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1114 ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
1121 ciss_report_request(cr, &command_status, NULL);
1122 switch(command_status) {
1123 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1125 case CISS_CMD_STATUS_DATA_UNDERRUN:
1126 case CISS_CMD_STATUS_DATA_OVERRUN:
1127 ciss_printf(sc, "data over/underrun reading logical drive ID\n");
1129 ciss_printf(sc, "error reading logical drive ID (%s)\n",
1130 ciss_name_command_status(command_status));
1134 ciss_release_request(cr);
1138 * Build a CISS BMIC command to get the logical drive status.
1140 if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
1144 * Get the logical drive geometry.
1146 if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1150 * Print the drive's basic characteristics.
1152 if (1/*bootverbose*/) {
1153 ciss_printf(sc, "logical drive %d: %s, %dMB ",
1154 cbc->log_drive, ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
1155 ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
1156 ld->cl_ldrive->block_size));
1158 ciss_print_ldrive(sc, ld);
1162 /* make the drive not-exist */
1163 ld->cl_status = CISS_LD_NONEXISTENT;
1164 if (ld->cl_ldrive != NULL) {
1165 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
1166 ld->cl_ldrive = NULL;
1168 if (ld->cl_lstatus != NULL) {
1169 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
1170 ld->cl_lstatus = NULL;
1174 ciss_release_request(cr);
1179 /************************************************************************
1180 * Get status for a logical drive.
1182 * XXX should we also do this in response to Test Unit Ready?
1185 ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld)
1187 struct ciss_request *cr;
1188 struct ciss_command *cc;
1189 struct ciss_bmic_cdb *cbc;
1190 int error, command_status;
1193 * Build a CISS BMIC command to get the logical drive status.
1195 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
1196 (void **)&ld->cl_lstatus,
1197 sizeof(*ld->cl_lstatus))) != 0)
1199 cc = CISS_FIND_COMMAND(cr);
1200 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1201 cbc->log_drive = ld->cl_address.logical.lun;
1204 * Submit the request and wait for it to complete.
1206 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1207 ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1214 ciss_report_request(cr, &command_status, NULL);
1215 switch(command_status) {
1216 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1218 case CISS_CMD_STATUS_DATA_UNDERRUN:
1219 case CISS_CMD_STATUS_DATA_OVERRUN:
1220 ciss_printf(sc, "data over/underrun reading logical drive status\n");
1222 ciss_printf(sc, "error reading logical drive status (%s)\n",
1223 ciss_name_command_status(command_status));
1229 * Set the drive's summary status based on the returned status.
1231 * XXX testing shows that a failed JBOD drive comes back at next
1232 * boot in "queued for expansion" mode. WTF?
1234 ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
1238 ciss_release_request(cr);
1242 /************************************************************************
1243 * Notify the adapter of a config update.
1246 ciss_update_config(struct ciss_softc *sc)
1252 CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
1253 for (i = 0; i < 1000; i++) {
1254 if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
1255 CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
1263 /************************************************************************
1264 * Accept new media into a logical drive.
1266 * XXX The drive has previously been offline; it would be good if we
1267 * could make sure it's not open right now.
1270 ciss_accept_media(struct ciss_softc *sc, int ldrive, int async)
1272 struct ciss_request *cr;
1273 struct ciss_command *cc;
1274 struct ciss_bmic_cdb *cbc;
1277 debug(0, "bringing logical drive %d back online %ssynchronously",
1278 ldrive, async ? "a" : "");
1281 * Build a CISS BMIC command to bring the drive back online.
1283 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
1286 cc = CISS_FIND_COMMAND(cr);
1287 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1288 cbc->log_drive = ldrive;
1291 * Dispatch the request asynchronously if we can't sleep waiting
1292 * for it to complete.
1295 cr->cr_complete = ciss_accept_media_complete;
1296 if ((error = ciss_start(cr)) != 0)
1301 * Submit the request and wait for it to complete.
1303 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1304 ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1310 * Call the completion callback manually.
1312 ciss_accept_media_complete(cr);
1317 ciss_release_request(cr);
1322 ciss_accept_media_complete(struct ciss_request *cr)
1329 ciss_report_request(cr, &command_status, NULL);
1330 switch(command_status) {
1331 case CISS_CMD_STATUS_SUCCESS: /* all OK */
1332 /* we should get a logical drive status changed event here */
1335 ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
1336 ciss_name_command_status(command_status));
1339 ciss_release_request(cr);
1342 /************************************************************************
1343 * Release adapter resources.
1346 ciss_free(struct ciss_softc *sc)
1350 /* we're going away */
1351 sc->ciss_flags |= CISS_FLAG_ABORTING;
1353 /* terminate the periodic heartbeat routine */
1354 untimeout(ciss_periodic, sc, sc->ciss_periodic);
1356 /* cancel the Event Notify chain */
1357 ciss_notify_abort(sc);
1359 /* free the controller data */
1360 if (sc->ciss_id != NULL)
1361 free(sc->ciss_id, CISS_MALLOC_CLASS);
1363 /* release I/O resources */
1364 if (sc->ciss_regs_resource != NULL)
1365 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1366 sc->ciss_regs_rid, sc->ciss_regs_resource);
1367 if (sc->ciss_cfg_resource != NULL)
1368 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1369 sc->ciss_cfg_rid, sc->ciss_cfg_resource);
1370 if (sc->ciss_intr != NULL)
1371 bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
1372 if (sc->ciss_irq_resource != NULL)
1373 bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
1374 sc->ciss_irq_rid, sc->ciss_irq_resource);
1376 /* destroy DMA tags */
1377 if (sc->ciss_parent_dmat)
1378 bus_dma_tag_destroy(sc->ciss_parent_dmat);
1379 if (sc->ciss_buffer_dmat)
1380 bus_dma_tag_destroy(sc->ciss_buffer_dmat);
1382 /* destroy command memory and DMA tag */
1383 if (sc->ciss_command != NULL) {
1384 bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
1385 bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
1387 if (sc->ciss_buffer_dmat)
1388 bus_dma_tag_destroy(sc->ciss_command_dmat);
1390 /* disconnect from CAM */
1391 if (sc->ciss_cam_sim) {
1392 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim));
1393 cam_sim_free(sc->ciss_cam_sim);
1395 if (sc->ciss_cam_devq)
1396 cam_simq_release(sc->ciss_cam_devq);
1397 /* XXX what about ciss_cam_path? */
1400 /************************************************************************
1401 * Give a command to the adapter.
1403 * Note that this uses the simple transport layer directly. If we
1404 * want to add support for other layers, we'll need a switch of some
1407 * Note that the simple transport layer has no way of refusing a
1408 * command; we only have as many request structures as the adapter
1409 * supports commands, so we don't have to check (this presumes that
1410 * the adapter can handle commands as fast as we throw them at it).
1413 ciss_start(struct ciss_request *cr)
1415 struct ciss_command *cc; /* XXX debugging only */
1418 cc = CISS_FIND_COMMAND(cr);
1419 debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
1422 * Map the request's data.
1424 if ((error = ciss_map_request(cr)))
1428 ciss_print_request(cr);
1432 * Post the command to the adapter.
1434 ciss_enqueue_busy(cr);
1435 CISS_TL_SIMPLE_POST_CMD(cr->cr_sc, CISS_FIND_COMMANDPHYS(cr));
1440 /************************************************************************
1441 * Fetch completed request(s) from the adapter, queue them for
1442 * completion handling.
1444 * Note that this uses the simple transport layer directly. If we
1445 * want to add support for other layers, we'll need a switch of some
1448 * Note that the simple transport mechanism does not require any
1449 * reentrancy protection; the OPQ read is atomic. If there is a
1450 * chance of a race with something else that might move the request
1451 * off the busy list, then we will have to lock against that
1452 * (eg. timeouts, etc.)
1455 ciss_done(struct ciss_softc *sc)
1457 struct ciss_request *cr;
1458 struct ciss_command *cc;
1459 u_int32_t tag, index;
1465 * Loop quickly taking requests from the adapter and moving them
1466 * from the busy queue to the completed queue.
1471 /* see if the OPQ contains anything */
1472 if (!CISS_TL_SIMPLE_OPQ_INTERRUPT(sc))
1475 tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
1476 if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
1479 debug(2, "completed command %d%s", index,
1480 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
1481 if (index >= sc->ciss_max_requests) {
1482 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
1485 cr = &(sc->ciss_request[index]);
1486 cc = CISS_FIND_COMMAND(cr);
1487 cc->header.host_tag = tag; /* not updated by adapter */
1488 if (ciss_remove_busy(cr)) {
1489 /* assume this is garbage out of the adapter */
1490 ciss_printf(sc, "completed nonbusy request %d\n", index);
1492 ciss_enqueue_complete(cr);
1498 * Invoke completion processing. If we can defer this out of
1499 * interrupt context, that'd be good.
1505 /************************************************************************
1506 * Take an interrupt from the adapter.
1509 ciss_intr(void *arg)
1511 struct ciss_softc *sc = (struct ciss_softc *)arg;
1514 * The only interrupt we recognise indicates that there are
1515 * entries in the outbound post queue.
1520 /************************************************************************
1521 * Process completed requests.
1523 * Requests can be completed in three fashions:
1525 * - by invoking a callback function (cr_complete is non-null)
1526 * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
1527 * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
1530 ciss_complete(struct ciss_softc *sc)
1532 struct ciss_request *cr;
1537 * Loop taking requests off the completed queue and performing
1538 * completion processing on them.
1541 if ((cr = ciss_dequeue_complete(sc)) == NULL)
1543 ciss_unmap_request(cr);
1546 * If the request has a callback, invoke it.
1548 if (cr->cr_complete != NULL) {
1549 cr->cr_complete(cr);
1554 * If someone is sleeping on this request, wake them up.
1556 if (cr->cr_flags & CISS_REQ_SLEEP) {
1557 cr->cr_flags &= ~CISS_REQ_SLEEP;
1563 * If someone is polling this request for completion, signal.
1565 if (cr->cr_flags & CISS_REQ_POLL) {
1566 cr->cr_flags &= ~CISS_REQ_POLL;
1571 * Give up and throw the request back on the free queue. This
1572 * should never happen; resources will probably be lost.
1574 ciss_printf(sc, "WARNING: completed command with no submitter\n");
1575 ciss_enqueue_free(cr);
1579 /************************************************************************
1580 * Report on the completion status of a request, and pass back SCSI
1581 * and command status values.
1584 ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status)
1586 struct ciss_command *cc;
1587 struct ciss_error_info *ce;
1591 cc = CISS_FIND_COMMAND(cr);
1592 ce = (struct ciss_error_info *)&(cc->sg[0]);
1595 * We don't consider data under/overrun an error for the Report
1596 * Logical/Physical LUNs commands.
1598 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
1599 ((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
1600 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS))) {
1601 cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
1602 debug(2, "ignoring irrelevant under/overrun error");
1606 * Check the command's error bit, if clear, there's no status and
1609 if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
1610 if (scsi_status != NULL)
1611 *scsi_status = SCSI_STATUS_OK;
1612 if (command_status != NULL)
1613 *command_status = CISS_CMD_STATUS_SUCCESS;
1616 if (command_status != NULL)
1617 *command_status = ce->command_status;
1618 if (scsi_status != NULL) {
1619 if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
1620 *scsi_status = ce->scsi_status;
1626 ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
1627 ce->command_status, ciss_name_command_status(ce->command_status),
1629 if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
1630 ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x\n",
1631 ce->additional_error_info.invalid_command.offense_size,
1632 ce->additional_error_info.invalid_command.offense_offset,
1633 ce->additional_error_info.invalid_command.offense_value);
1639 /************************************************************************
1640 * Issue a request and don't return until it's completed.
1642 * Depending on adapter status, we may poll or sleep waiting for
1646 ciss_synch_request(struct ciss_request *cr, int timeout)
1648 if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
1649 return(ciss_wait_request(cr, timeout));
1651 return(ciss_poll_request(cr, timeout));
1655 /************************************************************************
1656 * Issue a request and poll for completion.
1658 * Timeout in milliseconds.
1661 ciss_poll_request(struct ciss_request *cr, int timeout)
1667 cr->cr_flags |= CISS_REQ_POLL;
1668 if ((error = ciss_start(cr)) != 0)
1672 ciss_done(cr->cr_sc);
1673 if (!(cr->cr_flags & CISS_REQ_POLL))
1676 } while (timeout-- >= 0);
1677 return(EWOULDBLOCK);
1680 /************************************************************************
1681 * Issue a request and sleep waiting for completion.
1683 * Timeout in milliseconds. Note that a spurious wakeup will reset
1687 ciss_wait_request(struct ciss_request *cr, int timeout)
1693 cr->cr_flags |= CISS_REQ_SLEEP;
1694 if ((error = ciss_start(cr)) != 0)
1698 while (cr->cr_flags & CISS_REQ_SLEEP) {
1699 error = tsleep(cr, PCATCH, "cissREQ", (timeout * hz) / 1000);
1701 * On wakeup or interruption due to restartable activity, go
1702 * back and check to see if we're done.
1704 if ((error == 0) || (error == ERESTART)) {
1709 * Timeout, interrupted system call, etc.
1718 /************************************************************************
1719 * Abort a request. Note that a potential exists here to race the
1720 * request being completed; the caller must deal with this.
1723 ciss_abort_request(struct ciss_request *ar)
1725 struct ciss_request *cr;
1726 struct ciss_command *cc;
1727 struct ciss_message_cdb *cmc;
1733 if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
1736 /* build the abort command */
1737 cc = CISS_FIND_COMMAND(cr);
1738 cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL; /* addressing? */
1739 cc->header.address.physical.target = 0;
1740 cc->header.address.physical.bus = 0;
1741 cc->cdb.cdb_length = sizeof(*cmc);
1742 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
1743 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1744 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
1745 cc->cdb.timeout = 30;
1747 cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
1748 cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
1749 cmc->type = CISS_MESSAGE_ABORT_TASK;
1750 cmc->abort_tag = ar->cr_tag; /* endianness?? */
1753 * Send the request and wait for a response. If we believe we
1754 * aborted the request OK, clear the flag that indicates it's
1757 error = ciss_synch_request(cr, 35 * 1000);
1759 error = ciss_report_request(cr, NULL, NULL);
1760 ciss_release_request(cr);
1767 /************************************************************************
1768 * Fetch and initialise a request
1771 ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
1773 struct ciss_request *cr;
1778 * Get a request and clean it up.
1780 if ((cr = ciss_dequeue_free(sc)) == NULL)
1785 cr->cr_complete = NULL;
1787 ciss_preen_command(cr);
1793 ciss_preen_command(struct ciss_request *cr)
1795 struct ciss_command *cc;
1799 * Clean up the command structure.
1801 * Note that we set up the error_info structure here, since the
1802 * length can be overwritten by any command.
1804 cc = CISS_FIND_COMMAND(cr);
1805 cc->header.sg_in_list = 0; /* kinda inefficient this way */
1806 cc->header.sg_total = 0;
1807 cc->header.host_tag = cr->cr_tag << 2;
1808 cc->header.host_tag_zeroes = 0;
1809 cmdphys = CISS_FIND_COMMANDPHYS(cr);
1810 cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
1811 cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
1815 /************************************************************************
1816 * Release a request to the free list.
1819 ciss_release_request(struct ciss_request *cr)
1821 struct ciss_softc *sc;
1827 /* release the request to the free queue */
1828 ciss_requeue_free(cr);
1831 /************************************************************************
1832 * Allocate a request that will be used to send a BMIC command. Do some
1833 * of the common setup here to avoid duplicating it everywhere else.
1836 ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
1837 int opcode, void **bufp, size_t bufsize)
1839 struct ciss_request *cr;
1840 struct ciss_command *cc;
1841 struct ciss_bmic_cdb *cbc;
1854 if ((error = ciss_get_request(sc, &cr)) != 0)
1858 * Allocate data storage if requested, determine the data direction.
1861 if ((bufsize > 0) && (bufp != NULL)) {
1862 if (*bufp == NULL) {
1863 if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1869 dataout = 1; /* we are given a buffer, so we are writing */
1874 * Build a CISS BMIC command to get the logical drive ID.
1877 cr->cr_length = bufsize;
1879 cr->cr_flags = CISS_REQ_DATAIN;
1881 cc = CISS_FIND_COMMAND(cr);
1882 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
1883 cc->header.address.physical.bus = 0;
1884 cc->header.address.physical.target = 0;
1885 cc->cdb.cdb_length = sizeof(*cbc);
1886 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1887 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1888 cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
1889 cc->cdb.timeout = 0;
1891 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1892 bzero(cbc, sizeof(*cbc));
1893 cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
1894 cbc->bmic_opcode = opcode;
1895 cbc->size = htons((u_int16_t)bufsize);
1900 ciss_release_request(cr);
1901 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
1902 free(buf, CISS_MALLOC_CLASS);
1905 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
1911 /************************************************************************
1912 * Handle a command passed in from userspace.
1915 ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
1917 struct ciss_request *cr;
1918 struct ciss_command *cc;
1919 struct ciss_error_info *ce;
1929 if ((error = ciss_get_request(sc, &cr)) != 0)
1931 cc = CISS_FIND_COMMAND(cr);
1934 * Allocate an in-kernel databuffer if required, copy in user data.
1936 cr->cr_length = ioc->buf_size;
1937 if (ioc->buf_size > 0) {
1938 if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_WAITOK)) == NULL) {
1942 if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
1943 debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
1949 * Build the request based on the user command.
1951 bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
1952 bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
1954 /* XXX anything else to populate here? */
1959 if ((error = ciss_synch_request(cr, 60 * 1000))) {
1960 debug(0, "request failed - %d", error);
1965 * Copy the results back to the user.
1967 ce = (struct ciss_error_info *)&(cc->sg[0]);
1968 bcopy(ce, &ioc->error_info, sizeof(*ce));
1969 if ((ioc->buf_size > 0) &&
1970 (error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
1971 debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
1979 if ((cr != NULL) && (cr->cr_data != NULL))
1980 free(cr->cr_data, CISS_MALLOC_CLASS);
1982 ciss_release_request(cr);
1986 /************************************************************************
1987 * Map a request into bus-visible space, initialise the scatter/gather
1991 ciss_map_request(struct ciss_request *cr)
1993 struct ciss_softc *sc;
1999 /* check that mapping is necessary */
2000 if ((cr->cr_flags & CISS_REQ_MAPPED) || (cr->cr_data == NULL))
2003 bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap, cr->cr_data, cr->cr_length,
2004 ciss_request_map_helper, CISS_FIND_COMMAND(cr), 0);
2006 if (cr->cr_flags & CISS_REQ_DATAIN)
2007 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2008 if (cr->cr_flags & CISS_REQ_DATAOUT)
2009 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2011 cr->cr_flags |= CISS_REQ_MAPPED;
2016 ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2018 struct ciss_command *cc;
2023 cc = (struct ciss_command *)arg;
2024 for (i = 0; i < nseg; i++) {
2025 cc->sg[i].address = segs[i].ds_addr;
2026 cc->sg[i].length = segs[i].ds_len;
2027 cc->sg[i].extension = 0;
2029 /* we leave the s/g table entirely within the command */
2030 cc->header.sg_in_list = nseg;
2031 cc->header.sg_total = nseg;
2034 /************************************************************************
2035 * Unmap a request from bus-visible space.
2038 ciss_unmap_request(struct ciss_request *cr)
2040 struct ciss_softc *sc;
2046 /* check that unmapping is necessary */
2047 if (!(cr->cr_flags & CISS_REQ_MAPPED) || (cr->cr_data == NULL))
2050 if (cr->cr_flags & CISS_REQ_DATAIN)
2051 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
2052 if (cr->cr_flags & CISS_REQ_DATAOUT)
2053 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
2055 bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2056 cr->cr_flags &= ~CISS_REQ_MAPPED;
2059 /************************************************************************
2060 * Attach the driver to CAM.
2062 * We put all the logical drives on a single SCSI bus.
2065 ciss_cam_init(struct ciss_softc *sc)
2071 * Allocate a devq. We can reuse this for the masked physical
2072 * devices if we decide to export these as well.
2074 if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests)) == NULL) {
2075 ciss_printf(sc, "can't allocate CAM SIM queue\n");
2082 if ((sc->ciss_cam_sim = cam_sim_alloc(ciss_cam_action, ciss_cam_poll, "ciss", sc,
2083 device_get_unit(sc->ciss_dev),
2084 sc->ciss_max_requests - 2,
2086 sc->ciss_cam_devq)) == NULL) {
2087 ciss_printf(sc, "can't allocate CAM SIM\n");
2092 * Register bus 0 (the 'logical drives' bus) with this SIM.
2094 if (xpt_bus_register(sc->ciss_cam_sim, 0) != 0) {
2095 ciss_printf(sc, "can't register SCSI bus 0\n");
2100 * Initiate a rescan of the bus.
2102 ciss_cam_rescan_all(sc);
2107 /************************************************************************
2108 * Initiate a rescan of the 'logical devices' SIM
2111 ciss_cam_rescan_target(struct ciss_softc *sc, int target)
2117 if ((ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK | M_ZERO)) == NULL) {
2118 ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
2122 if (xpt_create_path(&sc->ciss_cam_path, xpt_periph, cam_sim_path(sc->ciss_cam_sim), target, 0)
2124 ciss_printf(sc, "rescan failed (can't create path)\n");
2128 xpt_setup_ccb(&ccb->ccb_h, sc->ciss_cam_path, 5/*priority (low)*/);
2129 ccb->ccb_h.func_code = XPT_SCAN_BUS;
2130 ccb->ccb_h.cbfcnp = ciss_cam_rescan_callback;
2131 ccb->crcn.flags = CAM_FLAG_NONE;
2134 /* scan is now in progress */
2138 ciss_cam_rescan_all(struct ciss_softc *sc)
2140 return(ciss_cam_rescan_target(sc, 0));
2144 ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2146 xpt_free_path(ccb->ccb_h.path);
2150 /************************************************************************
2151 * Handle requests coming from CAM
2154 ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
2156 struct ciss_softc *sc;
2157 struct ccb_scsiio *csio;
2160 sc = cam_sim_softc(sim);
2161 csio = (struct ccb_scsiio *)&ccb->csio;
2162 target = csio->ccb_h.target_id;
2164 switch (ccb->ccb_h.func_code) {
2166 /* perform SCSI I/O */
2168 if (!ciss_cam_action_io(sim, csio))
2172 /* perform geometry calculations */
2173 case XPT_CALC_GEOMETRY:
2175 struct ccb_calc_geometry *ccg = &ccb->ccg;
2176 struct ciss_ldrive *ld = &sc->ciss_logical[target];
2178 debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2181 * Use the cached geometry settings unless the fault tolerance
2184 if (ld->cl_geometry.fault_tolerance == 0xFF) {
2185 u_int32_t secs_per_cylinder;
2188 ccg->secs_per_track = 32;
2189 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2190 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2192 ccg->heads = ld->cl_geometry.heads;
2193 ccg->secs_per_track = ld->cl_geometry.sectors;
2194 ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2196 ccb->ccb_h.status = CAM_REQ_CMP;
2200 /* handle path attribute inquiry */
2203 struct ccb_pathinq *cpi = &ccb->cpi;
2205 debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2207 cpi->version_num = 1;
2208 cpi->hba_inquiry = PI_TAG_ABLE; /* XXX is this correct? */
2209 cpi->target_sprt = 0;
2211 cpi->max_target = CISS_MAX_LOGICAL;
2212 cpi->max_lun = 0; /* 'logical drive' channel only */
2213 cpi->initiator_id = CISS_MAX_LOGICAL;
2214 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2215 strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
2216 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2217 cpi->unit_number = cam_sim_unit(sim);
2218 cpi->bus_id = cam_sim_bus(sim);
2219 cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */
2220 ccb->ccb_h.status = CAM_REQ_CMP;
2224 case XPT_GET_TRAN_SETTINGS:
2226 struct ccb_trans_settings *cts = &ccb->cts;
2229 bus = cam_sim_bus(sim);
2230 target = cts->ccb_h.target_id;
2232 debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
2235 /* disconnect always OK */
2236 cts->flags |= CCB_TRANS_DISC_ENB;
2237 cts->valid |= CCB_TRANS_DISC_VALID;
2239 cts->ccb_h.status = CAM_REQ_CMP;
2243 default: /* we can't do this */
2244 debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
2245 ccb->ccb_h.status = CAM_REQ_INVALID;
2252 /************************************************************************
2253 * Handle a CAM SCSI I/O request.
2256 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
2258 struct ciss_softc *sc;
2260 struct ciss_request *cr;
2261 struct ciss_command *cc;
2264 sc = cam_sim_softc(sim);
2265 bus = cam_sim_bus(sim);
2266 target = csio->ccb_h.target_id;
2268 debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
2270 /* check for I/O attempt to nonexistent device */
2272 (target > CISS_MAX_LOGICAL) ||
2273 (sc->ciss_logical[target].cl_status == CISS_LD_NONEXISTENT)) {
2274 debug(3, " device does not exist");
2275 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2278 /* firmware does not support commands > 10 bytes */
2279 if (csio->cdb_len > 12/*CISS_CDB_BUFFER_SIZE*/) {
2280 debug(3, " command too large (%d > %d)", csio->cdb_len, CISS_CDB_BUFFER_SIZE);
2281 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2284 /* check that the CDB pointer is not to a physical address */
2285 if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
2286 debug(3, " CDB pointer is to physical address");
2287 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2290 /* if there is data transfer, it must be to/from a virtual address */
2291 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
2292 if (csio->ccb_h.flags & CAM_DATA_PHYS) { /* we can't map it */
2293 debug(3, " data pointer is to physical address");
2294 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2296 if (csio->ccb_h.flags & CAM_SCATTER_VALID) { /* we want to do the s/g setup */
2297 debug(3, " data has premature s/g setup");
2298 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2302 /* abandon aborted ccbs or those that have failed validation */
2303 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
2304 debug(3, "abandoning CCB due to abort/validation failure");
2308 /* handle emulation of some SCSI commands ourself */
2309 if (ciss_cam_emulate(sc, csio))
2313 * Get a request to manage this command. If we can't, return the
2314 * ccb, freeze the queue and flag so that we unfreeze it when a
2315 * request completes.
2317 if ((error = ciss_get_request(sc, &cr)) != 0) {
2318 xpt_freeze_simq(sc->ciss_cam_sim, 1);
2319 csio->ccb_h.status |= CAM_REQUEUE_REQ;
2324 * Build the command.
2326 cc = CISS_FIND_COMMAND(cr);
2327 cr->cr_data = csio->data_ptr;
2328 cr->cr_length = csio->dxfer_len;
2329 cr->cr_complete = ciss_cam_complete;
2330 cr->cr_private = csio;
2332 cc->header.address.logical.mode = CISS_HDR_ADDRESS_MODE_LOGICAL;
2333 cc->header.address.logical.lun = target;
2334 cc->cdb.cdb_length = csio->cdb_len;
2335 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2336 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; /* XXX ordered tags? */
2337 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2338 cr->cr_flags = CISS_REQ_DATAOUT;
2339 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
2340 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2341 cr->cr_flags = CISS_REQ_DATAIN;
2342 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
2345 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2347 cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
2348 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
2349 bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
2351 bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
2355 * Submit the request to the adapter.
2357 * Note that this may fail if we're unable to map the request (and
2358 * if we ever learn a transport layer other than simple, may fail
2359 * if the adapter rejects the command).
2361 if ((error = ciss_start(cr)) != 0) {
2362 xpt_freeze_simq(sc->ciss_cam_sim, 1);
2363 csio->ccb_h.status |= CAM_REQUEUE_REQ;
2364 ciss_release_request(cr);
2371 /************************************************************************
2372 * Emulate SCSI commands the adapter doesn't handle as we might like.
2375 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
2381 target = csio->ccb_h.target_id;
2382 opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
2383 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
2386 * Handle requests for volumes that don't exist. A selection timeout
2387 * is slightly better than an illegal request. Other errors might be
2390 if (sc->ciss_logical[target].cl_status == CISS_LD_NONEXISTENT) {
2391 csio->ccb_h.status = CAM_SEL_TIMEOUT;
2392 xpt_done((union ccb *)csio);
2397 * Handle requests for volumes that exist but are offline.
2399 * I/O operations should fail, everything else should work.
2401 if (sc->ciss_logical[target].cl_status == CISS_LD_OFFLINE) {
2409 csio->ccb_h.status = CAM_SEL_TIMEOUT;
2410 xpt_done((union ccb *)csio);
2416 /* if we have to fake Synchronise Cache */
2417 if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
2420 * If this is a Synchronise Cache command, typically issued when
2421 * a device is closed, flush the adapter and complete now.
2423 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
2424 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
2425 ciss_flush_adapter(sc);
2426 csio->ccb_h.status = CAM_REQ_CMP;
2427 xpt_done((union ccb *)csio);
2435 /************************************************************************
2436 * Check for possibly-completed commands.
2439 ciss_cam_poll(struct cam_sim *sim)
2441 struct ciss_softc *sc = cam_sim_softc(sim);
2448 /************************************************************************
2449 * Handle completion of a command - pass results back through the CCB
2452 ciss_cam_complete(struct ciss_request *cr)
2454 struct ciss_softc *sc;
2455 struct ciss_command *cc;
2456 struct ciss_error_info *ce;
2457 struct ccb_scsiio *csio;
2464 cc = CISS_FIND_COMMAND(cr);
2465 ce = (struct ciss_error_info *)&(cc->sg[0]);
2466 csio = (struct ccb_scsiio *)cr->cr_private;
2469 * Extract status values from request.
2471 ciss_report_request(cr, &command_status, &scsi_status);
2472 csio->scsi_status = scsi_status;
2475 * Handle specific SCSI status values.
2477 switch(scsi_status) {
2478 /* no status due to adapter error */
2480 debug(0, "adapter error");
2481 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2484 /* no status due to command completed OK */
2485 case SCSI_STATUS_OK: /* CISS_SCSI_STATUS_GOOD */
2486 debug(2, "SCSI_STATUS_OK");
2487 csio->ccb_h.status = CAM_REQ_CMP;
2490 /* check condition, sense data included */
2491 case SCSI_STATUS_CHECK_COND: /* CISS_SCSI_STATUS_CHECK_CONDITION */
2492 debug(0, "SCSI_STATUS_CHECK_COND sense size %d resid %d",
2493 ce->sense_length, ce->residual_count);
2494 bzero(&csio->sense_data, SSD_FULL_SIZE);
2495 bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
2496 csio->sense_len = ce->sense_length;
2497 csio->resid = ce->residual_count;
2498 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
2501 struct scsi_sense_data *sns = (struct scsi_sense_data *)&ce->sense_info[0];
2502 debug(0, "sense key %x", sns->flags & SSD_KEY);
2507 case SCSI_STATUS_BUSY: /* CISS_SCSI_STATUS_BUSY */
2508 debug(0, "SCSI_STATUS_BUSY");
2509 csio->ccb_h.status = CAM_SCSI_BUSY;
2513 debug(0, "unknown status 0x%x", csio->scsi_status);
2514 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2518 /* handle post-command fixup */
2519 ciss_cam_complete_fixup(sc, csio);
2521 /* tell CAM we're ready for more commands */
2522 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
2524 xpt_done((union ccb *)csio);
2525 ciss_release_request(cr);
2528 /********************************************************************************
2529 * Fix up the result of some commands here.
2532 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
2534 struct scsi_inquiry_data *inq;
2535 struct ciss_ldrive *cl;
2538 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
2539 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == INQUIRY) {
2541 inq = (struct scsi_inquiry_data *)csio->data_ptr;
2542 target = csio->ccb_h.target_id;
2543 cl = &sc->ciss_logical[target];
2545 padstr(inq->vendor, "COMPAQ", 8);
2546 padstr(inq->product, ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance), 8);
2547 padstr(inq->revision, ciss_name_ldrive_status(cl->cl_lstatus->status), 16);
2552 /********************************************************************************
2553 * Find a peripheral attached at (target)
2555 static struct cam_periph *
2556 ciss_find_periph(struct ciss_softc *sc, int target)
2558 struct cam_periph *periph;
2559 struct cam_path *path;
2562 status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim), target, 0);
2563 if (status == CAM_REQ_CMP) {
2564 periph = cam_periph_find(path, NULL);
2565 xpt_free_path(path);
2572 /********************************************************************************
2573 * Name the device at (target)
2575 * XXX is this strictly correct?
2578 ciss_name_device(struct ciss_softc *sc, int target)
2580 struct cam_periph *periph;
2582 if ((periph = ciss_find_periph(sc, target)) != NULL) {
2583 sprintf(sc->ciss_logical[target].cl_name, "%s%d", periph->periph_name, periph->unit_number);
2586 sc->ciss_logical[target].cl_name[0] = 0;
2590 /************************************************************************
2591 * Periodic status monitoring.
2594 ciss_periodic(void *arg)
2596 struct ciss_softc *sc;
2600 sc = (struct ciss_softc *)arg;
2603 * Check the adapter heartbeat.
2605 if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
2606 sc->ciss_heart_attack++;
2607 debug(0, "adapter heart attack in progress 0x%x/%d",
2608 sc->ciss_heartbeat, sc->ciss_heart_attack);
2609 if (sc->ciss_heart_attack == 3) {
2610 ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
2611 /* XXX should reset adapter here */
2614 sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
2615 sc->ciss_heart_attack = 0;
2616 debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
2620 * If the notify event request has died for some reason, or has
2621 * not started yet, restart it.
2623 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
2624 debug(0, "(re)starting Event Notify chain");
2625 ciss_notify_event(sc);
2631 if (!(sc->ciss_flags & CISS_FLAG_ABORTING))
2632 sc->ciss_periodic = timeout(ciss_periodic, sc, CISS_HEARTBEAT_RATE * hz);
2635 /************************************************************************
2636 * Request a notification response from the adapter.
2638 * If (cr) is NULL, this is the first request of the adapter, so
2639 * reset the adapter's message pointer and start with the oldest
2640 * message available.
2643 ciss_notify_event(struct ciss_softc *sc)
2645 struct ciss_request *cr;
2646 struct ciss_command *cc;
2647 struct ciss_notify_cdb *cnc;
2652 cr = sc->ciss_periodic_notify;
2654 /* get a request if we don't already have one */
2656 if ((error = ciss_get_request(sc, &cr)) != 0) {
2657 debug(0, "can't get notify event request");
2660 sc->ciss_periodic_notify = cr;
2661 cr->cr_complete = ciss_notify_complete;
2662 debug(1, "acquired request %d", cr->cr_tag);
2666 * Get a databuffer if we don't already have one, note that the
2667 * adapter command wants a larger buffer than the actual
2670 if (cr->cr_data == NULL) {
2671 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
2672 debug(0, "can't get notify event request buffer");
2676 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
2679 /* re-setup the request's command (since we never release it) XXX overkill*/
2680 ciss_preen_command(cr);
2682 /* (re)build the notify event command */
2683 cc = CISS_FIND_COMMAND(cr);
2684 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2685 cc->header.address.physical.bus = 0;
2686 cc->header.address.physical.target = 0;
2688 cc->cdb.cdb_length = sizeof(*cnc);
2689 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2690 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2691 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
2692 cc->cdb.timeout = 0; /* no timeout, we hope */
2694 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
2695 bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
2696 cnc->opcode = CISS_OPCODE_READ;
2697 cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
2698 cnc->timeout = 0; /* no timeout, we hope */
2699 cnc->synchronous = 0;
2701 cnc->seek_to_oldest = 0;
2703 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
2705 /* submit the request */
2706 error = ciss_start(cr);
2711 if (cr->cr_data != NULL)
2712 free(cr->cr_data, CISS_MALLOC_CLASS);
2713 ciss_release_request(cr);
2715 sc->ciss_periodic_notify = NULL;
2716 debug(0, "can't submit notify event request");
2717 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2719 debug(1, "notify event submitted");
2720 sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
2725 ciss_notify_complete(struct ciss_request *cr)
2727 struct ciss_command *cc;
2728 struct ciss_notify *cn;
2729 struct ciss_softc *sc;
2735 cc = CISS_FIND_COMMAND(cr);
2736 cn = (struct ciss_notify *)cr->cr_data;
2740 * Report request results, decode status.
2742 ciss_report_request(cr, &command_status, &scsi_status);
2745 * Abort the chain on a fatal error.
2747 * XXX which of these are actually errors?
2749 if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
2750 (command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
2751 (command_status != CISS_CMD_STATUS_TIMEOUT)) { /* XXX timeout? */
2752 ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
2753 ciss_name_command_status(command_status));
2754 ciss_release_request(cr);
2755 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2760 * If the adapter gave us a text message, print it.
2762 if (cn->message[0] != 0)
2763 ciss_printf(sc, "*** %.80s\n", cn->message);
2765 debug(0, "notify event class %d subclass %d detail %d",
2766 cn->class, cn->subclass, cn->detail);
2769 * If there's room, save the event for a user-level tool.
2771 if (((sc->ciss_notify_head + 1) % CISS_MAX_EVENTS) != sc->ciss_notify_tail) {
2772 sc->ciss_notify[sc->ciss_notify_head] = *cn;
2773 sc->ciss_notify_head = (sc->ciss_notify_head + 1) % CISS_MAX_EVENTS;
2777 * Some events are directly of interest to us.
2779 switch (cn->class) {
2780 case CISS_NOTIFY_LOGICAL:
2781 ciss_notify_logical(sc, cn);
2783 case CISS_NOTIFY_PHYSICAL:
2784 ciss_notify_physical(sc, cn);
2789 * If the response indicates that the notifier has been aborted,
2790 * release the notifier command.
2792 if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
2793 (cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
2794 (cn->detail == 1)) {
2795 debug(0, "notifier exiting");
2796 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2797 ciss_release_request(cr);
2798 sc->ciss_periodic_notify = NULL;
2799 wakeup(&sc->ciss_periodic_notify);
2803 * Send a new notify event command, if we're not aborting.
2805 if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
2806 ciss_notify_event(sc);
2810 /************************************************************************
2811 * Abort the Notify Event chain.
2813 * Note that we can't just abort the command in progress; we have to
2814 * explicitly issue an Abort Notify Event command in order for the
2815 * adapter to clean up correctly.
2817 * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
2818 * the chain will not restart itself.
2821 ciss_notify_abort(struct ciss_softc *sc)
2823 struct ciss_request *cr;
2824 struct ciss_command *cc;
2825 struct ciss_notify_cdb *cnc;
2826 int error, s, command_status, scsi_status;
2833 /* verify that there's an outstanding command */
2834 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
2837 /* get a command to issue the abort with */
2838 if ((error = ciss_get_request(sc, &cr)))
2841 /* get a buffer for the result */
2842 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
2843 debug(0, "can't get notify event request buffer");
2847 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
2850 cc = CISS_FIND_COMMAND(cr);
2851 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2852 cc->header.address.physical.bus = 0;
2853 cc->header.address.physical.target = 0;
2854 cc->cdb.cdb_length = sizeof(*cnc);
2855 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2856 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2857 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
2858 cc->cdb.timeout = 0; /* no timeout, we hope */
2860 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
2861 bzero(cnc, sizeof(*cnc));
2862 cnc->opcode = CISS_OPCODE_WRITE;
2863 cnc->command = CISS_COMMAND_ABORT_NOTIFY;
2864 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
2866 ciss_print_request(cr);
2869 * Submit the request and wait for it to complete.
2871 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
2872 ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
2879 ciss_report_request(cr, &command_status, &scsi_status);
2880 switch(command_status) {
2881 case CISS_CMD_STATUS_SUCCESS:
2883 case CISS_CMD_STATUS_INVALID_COMMAND:
2885 * Some older adapters don't support the CISS version of this
2886 * command. Fall back to using the BMIC version.
2888 error = ciss_notify_abort_bmic(sc);
2893 case CISS_CMD_STATUS_TARGET_STATUS:
2895 * This can happen if the adapter thinks there wasn't an outstanding
2896 * Notify Event command but we did. We clean up here.
2898 if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
2899 if (sc->ciss_periodic_notify != NULL)
2900 ciss_release_request(sc->ciss_periodic_notify);
2907 ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
2908 ciss_name_command_status(command_status));
2914 * Sleep waiting for the notifier command to complete. Note
2915 * that if it doesn't, we may end up in a bad situation, since
2916 * the adapter may deliver it later. Also note that the adapter
2917 * requires the Notify Event command to be cancelled in order to
2918 * maintain internal bookkeeping.
2921 while (sc->ciss_periodic_notify != NULL) {
2922 error = tsleep(&sc->ciss_periodic_notify, 0, "cissNEA", hz * 5);
2923 if (error == EWOULDBLOCK) {
2924 ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
2931 /* release the cancel request */
2933 if (cr->cr_data != NULL)
2934 free(cr->cr_data, CISS_MALLOC_CLASS);
2935 ciss_release_request(cr);
2938 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2942 /************************************************************************
2943 * Abort the Notify Event chain using a BMIC command.
2946 ciss_notify_abort_bmic(struct ciss_softc *sc)
2948 struct ciss_request *cr;
2949 int error, command_status;
2956 /* verify that there's an outstanding command */
2957 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
2961 * Build a BMIC command to cancel the Notify on Event command.
2963 * Note that we are sending a CISS opcode here. Odd.
2965 if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
2970 * Submit the request and wait for it to complete.
2972 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
2973 ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
2980 ciss_report_request(cr, &command_status, NULL);
2981 switch(command_status) {
2982 case CISS_CMD_STATUS_SUCCESS:
2985 ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
2986 ciss_name_command_status(command_status));
2993 ciss_release_request(cr);
2997 /************************************************************************
2998 * Handle a notify event relating to the status of a logical drive.
3000 * XXX need to be able to defer some of these to properly handle
3001 * calling the "ID Physical drive" command, unless the 'extended'
3002 * drive IDs are always in BIG_MAP format.
3005 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
3007 struct ciss_ldrive *ld;
3012 ld = &sc->ciss_logical[cn->data.logical_status.logical_drive];
3014 switch (cn->subclass) {
3015 case CISS_NOTIFY_LOGICAL_STATUS:
3016 switch (cn->detail) {
3018 ciss_name_device(sc, cn->data.logical_status.logical_drive);
3019 ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
3020 cn->data.logical_status.logical_drive, ld->cl_name,
3021 ciss_name_ldrive_status(cn->data.logical_status.previous_state),
3022 ciss_name_ldrive_status(cn->data.logical_status.new_state),
3023 cn->data.logical_status.spare_state,
3024 "\20\1configured\2rebuilding\3failed\4in use\5available\n");
3027 * Update our idea of the drive's status.
3029 ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
3030 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3031 if (ld->cl_lstatus != NULL)
3032 ld->cl_lstatus->status = cn->data.logical_status.new_state;
3036 * Have CAM rescan the drive if its status has changed.
3038 if (ostatus != ld->cl_status)
3039 ciss_cam_rescan_target(sc, cn->data.logical_status.logical_drive);
3044 case 1: /* logical drive has recognised new media, needs Accept Media Exchange */
3045 ciss_name_device(sc, cn->data.logical_status.logical_drive);
3046 ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
3047 cn->data.logical_status.logical_drive, ld->cl_name);
3048 ciss_accept_media(sc, cn->data.logical_status.logical_drive, 1);
3053 ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
3054 cn->data.rebuild_aborted.logical_drive,
3055 sc->ciss_logical[cn->data.rebuild_aborted.logical_drive].cl_name,
3056 (cn->detail == 2) ? "read" : "write");
3061 case CISS_NOTIFY_LOGICAL_ERROR:
3062 if (cn->detail == 0) {
3063 ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
3064 cn->data.io_error.logical_drive,
3065 sc->ciss_logical[cn->data.io_error.logical_drive].cl_name,
3066 cn->data.io_error.failure_bus,
3067 cn->data.io_error.failure_drive);
3068 /* XXX should we take the drive down at this point, or will we be told? */
3072 case CISS_NOTIFY_LOGICAL_SURFACE:
3073 if (cn->detail == 0)
3074 ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
3075 cn->data.consistency_completed.logical_drive,
3076 sc->ciss_logical[cn->data.consistency_completed.logical_drive].cl_name);
3081 /************************************************************************
3082 * Handle a notify event relating to the status of a physical drive.
3085 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
3090 /************************************************************************
3094 ciss_print_request(struct ciss_request *cr)
3096 struct ciss_softc *sc;
3097 struct ciss_command *cc;
3101 cc = CISS_FIND_COMMAND(cr);
3103 ciss_printf(sc, "REQUEST @ %p\n", cr);
3104 ciss_printf(sc, " data %p/%d tag %d flags %b\n",
3105 cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
3106 "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
3107 ciss_printf(sc, " sg list/total %d/%d host tag 0x%x\n",
3108 cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
3109 switch(cc->header.address.mode.mode) {
3110 case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
3111 case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
3112 ciss_printf(sc, " physical bus %d target %d\n",
3113 cc->header.address.physical.bus, cc->header.address.physical.target);
3115 case CISS_HDR_ADDRESS_MODE_LOGICAL:
3116 ciss_printf(sc, " logical unit %d\n", cc->header.address.logical.lun);
3119 ciss_printf(sc, " %s cdb length %d type %s attribute %s\n",
3120 (cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
3121 (cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
3122 (cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
3124 (cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
3125 (cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
3126 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
3127 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
3128 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
3129 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
3130 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
3131 ciss_printf(sc, " %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
3133 if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
3134 /* XXX print error info */
3136 /* since we don't use chained s/g, don't support it here */
3137 for (i = 0; i < cc->header.sg_in_list; i++) {
3139 ciss_printf(sc, " ");
3140 printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
3141 if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
3147 /************************************************************************
3148 * Print information about the status of a logical drive.
3151 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
3155 if (ld->cl_lstatus == NULL) {
3156 printf("does not exist\n");
3160 /* print drive status */
3161 switch(ld->cl_lstatus->status) {
3162 case CISS_LSTATUS_OK:
3165 case CISS_LSTATUS_INTERIM_RECOVERY:
3166 printf("in interim recovery mode\n");
3168 case CISS_LSTATUS_READY_RECOVERY:
3169 printf("ready to begin recovery\n");
3171 case CISS_LSTATUS_RECOVERING:
3172 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
3173 target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
3174 printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
3175 bus, target, ld->cl_lstatus->blocks_to_recover);
3177 case CISS_LSTATUS_EXPANDING:
3178 printf("being expanded, %u blocks remaining\n",
3179 ld->cl_lstatus->blocks_to_recover);
3181 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
3182 printf("queued for expansion\n");
3184 case CISS_LSTATUS_FAILED:
3185 printf("queued for expansion\n");
3187 case CISS_LSTATUS_WRONG_PDRIVE:
3188 printf("wrong physical drive inserted\n");
3190 case CISS_LSTATUS_MISSING_PDRIVE:
3191 printf("missing a needed physical drive\n");
3193 case CISS_LSTATUS_BECOMING_READY:
3194 printf("becoming ready\n");
3198 /* print failed physical drives */
3199 for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
3200 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
3201 target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
3204 ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
3205 ld->cl_lstatus->drive_failure_map[i]);
3210 /************************************************************************
3211 * Print information about the controller/driver.
3214 ciss_print_adapter(struct ciss_softc *sc)
3218 ciss_printf(sc, "ADAPTER:\n");
3219 for (i = 0; i < CISSQ_COUNT; i++) {
3220 ciss_printf(sc, "%s %d/%d\n",
3222 i == 1 ? "busy" : "complete",
3223 sc->ciss_qstat[i].q_length,
3224 sc->ciss_qstat[i].q_max);
3226 ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
3227 ciss_printf(sc, "notify_head/tail %d/%d\n",
3228 sc->ciss_notify_head, sc->ciss_notify_tail);
3229 ciss_printf(sc, "flags %b\n", sc->ciss_flags,
3230 "\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
3232 for (i = 0; i < CISS_MAX_LOGICAL; i++) {
3233 ciss_printf(sc, "LOGICAL DRIVE %d: ", i);
3234 ciss_print_ldrive(sc, sc->ciss_logical + i);
3237 for (i = 1; i < sc->ciss_max_requests; i++)
3238 ciss_print_request(sc->ciss_request + i);
3246 struct ciss_softc *sc;
3248 sc = devclass_get_softc(devclass_find("ciss"), 0);
3250 printf("no ciss controllers\n");
3252 ciss_print_adapter(sc);
3257 /************************************************************************
3258 * Return a name for a logical drive status value.
3261 ciss_name_ldrive_status(int status)
3264 case CISS_LSTATUS_OK:
3266 case CISS_LSTATUS_FAILED:
3268 case CISS_LSTATUS_NOT_CONFIGURED:
3269 return("not configured");
3270 case CISS_LSTATUS_INTERIM_RECOVERY:
3271 return("interim recovery");
3272 case CISS_LSTATUS_READY_RECOVERY:
3273 return("ready for recovery");
3274 case CISS_LSTATUS_RECOVERING:
3275 return("recovering");
3276 case CISS_LSTATUS_WRONG_PDRIVE:
3277 return("wrong physical drive inserted");
3278 case CISS_LSTATUS_MISSING_PDRIVE:
3279 return("missing physical drive");
3280 case CISS_LSTATUS_EXPANDING:
3281 return("expanding");
3282 case CISS_LSTATUS_BECOMING_READY:
3283 return("becoming ready");
3284 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
3285 return("queued for expansion");
3287 return("unknown status");
3290 /************************************************************************
3291 * Return an online/offline/nonexistent value for a logical drive
3295 ciss_decode_ldrive_status(int status)
3298 case CISS_LSTATUS_NOT_CONFIGURED:
3299 return(CISS_LD_NONEXISTENT);
3301 case CISS_LSTATUS_OK:
3302 case CISS_LSTATUS_INTERIM_RECOVERY:
3303 case CISS_LSTATUS_READY_RECOVERY:
3304 case CISS_LSTATUS_RECOVERING:
3305 case CISS_LSTATUS_EXPANDING:
3306 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
3307 return(CISS_LD_ONLINE);
3309 case CISS_LSTATUS_FAILED:
3310 case CISS_LSTATUS_WRONG_PDRIVE:
3311 case CISS_LSTATUS_MISSING_PDRIVE:
3312 case CISS_LSTATUS_BECOMING_READY:
3314 return(CISS_LD_OFFLINE);
3319 /************************************************************************
3320 * Return a name for a logical drive's organisation.
3323 ciss_name_ldrive_org(int org)
3326 case CISS_LDRIVE_RAID0:
3328 case CISS_LDRIVE_RAID1:
3330 case CISS_LDRIVE_RAID4:
3332 case CISS_LDRIVE_RAID5:
3338 /************************************************************************
3339 * Return a name for a command status value.
3342 ciss_name_command_status(int status)
3345 case CISS_CMD_STATUS_SUCCESS:
3347 case CISS_CMD_STATUS_TARGET_STATUS:
3348 return("target status");
3349 case CISS_CMD_STATUS_DATA_UNDERRUN:
3350 return("data underrun");
3351 case CISS_CMD_STATUS_DATA_OVERRUN:
3352 return("data overrun");
3353 case CISS_CMD_STATUS_INVALID_COMMAND:
3354 return("invalid command");
3355 case CISS_CMD_STATUS_PROTOCOL_ERROR:
3356 return("protocol error");
3357 case CISS_CMD_STATUS_HARDWARE_ERROR:
3358 return("hardware error");
3359 case CISS_CMD_STATUS_CONNECTION_LOST:
3360 return("connection lost");
3361 case CISS_CMD_STATUS_ABORTED:
3363 case CISS_CMD_STATUS_ABORT_FAILED:
3364 return("abort failed");
3365 case CISS_CMD_STATUS_UNSOLICITED_ABORT:
3366 return("unsolicited abort");
3367 case CISS_CMD_STATUS_TIMEOUT:
3369 case CISS_CMD_STATUS_UNABORTABLE:
3370 return("unabortable");
3372 return("unknown status");
3375 /************************************************************************
3376 * Handle an open on the control device.
3379 ciss_open(dev_t dev, int flags, int fmt, d_thread_t *p)
3381 struct ciss_softc *sc;
3385 sc = (struct ciss_softc *)dev->si_drv1;
3387 /* we might want to veto if someone already has us open */
3389 sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
3393 /************************************************************************
3394 * Handle the last close on the control device.
3397 ciss_close(dev_t dev, int flags, int fmt, d_thread_t *p)
3399 struct ciss_softc *sc;
3403 sc = (struct ciss_softc *)dev->si_drv1;
3405 sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
3409 /********************************************************************************
3410 * Handle adapter-specific control operations.
3412 * Note that the API here is compatible with the Linux driver, in order to
3413 * simplify the porting of Compaq's userland tools.
3416 ciss_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *p)
3418 struct ciss_softc *sc;
3423 sc = (struct ciss_softc *)dev->si_drv1;
3427 case CCISS_GETPCIINFO:
3429 cciss_pci_info_struct *pis = (cciss_pci_info_struct *)addr;
3431 pis->bus = pci_get_bus(sc->ciss_dev);
3432 pis->dev_fn = pci_get_slot(sc->ciss_dev);
3433 pis->board_id = pci_get_devid(sc->ciss_dev);
3438 case CCISS_GETINTINFO:
3440 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
3442 cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
3443 cis->count = sc->ciss_cfg->interrupt_coalesce_count;
3448 case CCISS_SETINTINFO:
3450 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
3452 if ((cis->delay == 0) && (cis->count == 0)) {
3458 * XXX apparently this is only safe if the controller is idle,
3459 * we should suspend it before doing this.
3461 sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
3462 sc->ciss_cfg->interrupt_coalesce_count = cis->count;
3464 if (ciss_update_config(sc))
3467 /* XXX resume the controller here */
3471 case CCISS_GETNODENAME:
3472 bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
3473 sizeof(NodeName_type));
3476 case CCISS_SETNODENAME:
3477 bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
3478 sizeof(NodeName_type));
3479 if (ciss_update_config(sc))
3483 case CCISS_GETHEARTBEAT:
3484 *(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
3487 case CCISS_GETBUSTYPES:
3488 *(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
3491 case CCISS_GETFIRMVER:
3492 bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
3493 sizeof(FirmwareVer_type));
3496 case CCISS_GETDRIVERVER:
3497 *(DriverVer_type *)addr = CISS_DRIVER_VERSION;
3500 case CCISS_REVALIDVOLS:
3502 * This is a bit ugly; to do it "right" we really need
3503 * to find any disks that have changed, kick CAM off them,
3504 * then rescan only these disks. It'd be nice if they
3505 * a) told us which disk(s) they were going to play with,
3506 * and b) which ones had arrived. 8(
3510 case CCISS_PASSTHRU:
3511 error = ciss_user_command(sc, (IOCTL_Command_struct *)addr);
3515 debug(0, "unknown ioctl 0x%lx", cmd);
3517 debug(1, "CCISS_GETPCIINFO: 0x%lx", CCISS_GETPCIINFO);
3518 debug(1, "CCISS_GETINTINFO: 0x%lx", CCISS_GETINTINFO);
3519 debug(1, "CCISS_SETINTINFO: 0x%lx", CCISS_SETINTINFO);
3520 debug(1, "CCISS_GETNODENAME: 0x%lx", CCISS_GETNODENAME);
3521 debug(1, "CCISS_SETNODENAME: 0x%lx", CCISS_SETNODENAME);
3522 debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
3523 debug(1, "CCISS_GETBUSTYPES: 0x%lx", CCISS_GETBUSTYPES);
3524 debug(1, "CCISS_GETFIRMVER: 0x%lx", CCISS_GETFIRMVER);
3525 debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
3526 debug(1, "CCISS_REVALIDVOLS: 0x%lx", CCISS_REVALIDVOLS);
3527 debug(1, "CCISS_PASSTHRU: 0x%lx", CCISS_PASSTHRU);