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.18 2006/09/05 00:55:41 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/device.h>
75 #include <sys/malloc.h>
76 #include <sys/kernel.h>
79 #include <sys/devicestat.h>
82 #include <bus/cam/cam.h>
83 #include <bus/cam/cam_ccb.h>
84 #include <bus/cam/cam_periph.h>
85 #include <bus/cam/cam_sim.h>
86 #include <bus/cam/cam_xpt_sim.h>
87 #include <bus/cam/scsi/scsi_all.h>
88 #include <bus/cam/scsi/scsi_message.h>
90 #include <machine/clock.h>
91 #include <machine/bus_memio.h>
92 #include <machine/bus.h>
93 #include <machine/endian.h>
94 #include <machine/resource.h>
97 #include <bus/pci/pcireg.h>
98 #include <bus/pci/pcivar.h>
104 MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data", "ciss internal data buffers");
107 static int ciss_lookup(device_t dev);
108 static int ciss_probe(device_t dev);
109 static int ciss_attach(device_t dev);
110 static int ciss_detach(device_t dev);
111 static int ciss_shutdown(device_t dev);
113 /* (de)initialisation functions, control wrappers */
114 static int ciss_init_pci(struct ciss_softc *sc);
115 static int ciss_wait_adapter(struct ciss_softc *sc);
116 static int ciss_flush_adapter(struct ciss_softc *sc);
117 static int ciss_init_requests(struct ciss_softc *sc);
118 static void ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
119 int nseg, int error);
120 static int ciss_identify_adapter(struct ciss_softc *sc);
121 static int ciss_init_logical(struct ciss_softc *sc);
122 static int ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
123 static int ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld);
124 static int ciss_update_config(struct ciss_softc *sc);
125 static int ciss_accept_media(struct ciss_softc *sc, int ldrive, int async);
126 static void ciss_accept_media_complete(struct ciss_request *cr);
127 static void ciss_free(struct ciss_softc *sc);
129 /* request submission/completion */
130 static int ciss_start(struct ciss_request *cr);
131 static void ciss_done(struct ciss_softc *sc);
132 static void ciss_intr(void *arg);
133 static void ciss_complete(struct ciss_softc *sc);
134 static int ciss_report_request(struct ciss_request *cr, int *command_status,
136 static int ciss_synch_request(struct ciss_request *cr, int timeout);
137 static int ciss_poll_request(struct ciss_request *cr, int timeout);
138 static int ciss_wait_request(struct ciss_request *cr, int timeout);
140 static int ciss_abort_request(struct ciss_request *cr);
143 /* request queueing */
144 static int ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
145 static void ciss_preen_command(struct ciss_request *cr);
146 static void ciss_release_request(struct ciss_request *cr);
148 /* request helpers */
149 static int ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
150 int opcode, void **bufp, size_t bufsize);
151 static int ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
154 static int ciss_map_request(struct ciss_request *cr);
155 static void ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
156 int nseg, int error);
157 static void ciss_unmap_request(struct ciss_request *cr);
160 static int ciss_cam_init(struct ciss_softc *sc);
161 static void ciss_cam_rescan_target(struct ciss_softc *sc, int target);
162 static void ciss_cam_rescan_all(struct ciss_softc *sc);
163 static void ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb);
164 static void ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
165 static int ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
166 static int ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
167 static void ciss_cam_poll(struct cam_sim *sim);
168 static void ciss_cam_complete(struct ciss_request *cr);
169 static void ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
170 static struct cam_periph *ciss_find_periph(struct ciss_softc *sc, int target);
171 static int ciss_name_device(struct ciss_softc *sc, int target);
173 /* periodic status monitoring */
174 static void ciss_periodic(void *arg);
175 static void ciss_notify_event(struct ciss_softc *sc);
176 static void ciss_notify_complete(struct ciss_request *cr);
177 static int ciss_notify_abort(struct ciss_softc *sc);
178 static int ciss_notify_abort_bmic(struct ciss_softc *sc);
179 static void ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
180 static void ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
182 /* debugging output */
183 static void ciss_print_request(struct ciss_request *cr);
184 static void ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
185 static const char *ciss_name_ldrive_status(int status);
186 static int ciss_decode_ldrive_status(int status);
187 static const char *ciss_name_ldrive_org(int org);
188 static const char *ciss_name_command_status(int status);
193 static device_method_t ciss_methods[] = {
194 /* Device interface */
195 DEVMETHOD(device_probe, ciss_probe),
196 DEVMETHOD(device_attach, ciss_attach),
197 DEVMETHOD(device_detach, ciss_detach),
198 DEVMETHOD(device_shutdown, ciss_shutdown),
202 static driver_t ciss_pci_driver = {
205 sizeof(struct ciss_softc)
208 static devclass_t ciss_devclass;
210 DECLARE_DUMMY_MODULE(ciss);
211 DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
214 * Control device interface.
216 static d_open_t ciss_open;
217 static d_close_t ciss_close;
218 static d_ioctl_t ciss_ioctl;
220 #define CISS_CDEV_MAJOR 166
222 static struct dev_ops ciss_ops = {
223 { "ciss", CISS_CDEV_MAJOR, 0 },
225 .d_close = ciss_close,
226 .d_ioctl = ciss_ioctl
229 /************************************************************************
230 * CISS adapters amazingly don't have a defined programming interface
231 * value. (One could say some very despairing things about PCI and
232 * people just not getting the general idea.) So we are forced to
233 * stick with matching against subvendor/subdevice, and thus have to
234 * be updated for every new CISS adapter that appears.
236 #define CISS_BOARD_SA5 (1<<0)
237 #define CISS_BOARD_SA5B (1<<1)
245 } ciss_vendor_data[] = {
246 { 0x0e11, 0x4070, CISS_BOARD_SA5, "Compaq Smart Array 5300" },
247 { 0x0e11, 0x4080, CISS_BOARD_SA5B, "Compaq Smart Array 5i" },
248 { 0x0e11, 0x4082, CISS_BOARD_SA5B, "Compaq Smart Array 532" },
249 { 0x0e11, 0x4083, CISS_BOARD_SA5B, "HP Smart Array 5312" },
250 { 0x0e11, 0x409A, CISS_BOARD_SA5B, "HP Smart Array 641" },
251 { 0x0e11, 0x409B, CISS_BOARD_SA5B, "HP Smart Array 642" },
252 { 0x0e11, 0x409C, CISS_BOARD_SA5B, "HP Smart Array 6400" },
256 /************************************************************************
257 * Find a match for the device in our list of known adapters.
260 ciss_lookup(device_t dev)
264 for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
265 if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
266 (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
272 /************************************************************************
273 * Match a known CISS adapter.
276 ciss_probe(device_t dev)
280 i = ciss_lookup(dev);
282 device_set_desc(dev, ciss_vendor_data[i].desc);
288 /************************************************************************
289 * Attach the driver to this adapter.
292 ciss_attach(device_t dev)
294 struct ciss_softc *sc;
300 /* print structure/union sizes */
301 debug_struct(ciss_command);
302 debug_struct(ciss_header);
303 debug_union(ciss_device_address);
304 debug_struct(ciss_cdb);
305 debug_struct(ciss_report_cdb);
306 debug_struct(ciss_notify_cdb);
307 debug_struct(ciss_notify);
308 debug_struct(ciss_message_cdb);
309 debug_struct(ciss_error_info_pointer);
310 debug_struct(ciss_error_info);
311 debug_struct(ciss_sg_entry);
312 debug_struct(ciss_config_table);
313 debug_struct(ciss_bmic_cdb);
314 debug_struct(ciss_bmic_id_ldrive);
315 debug_struct(ciss_bmic_id_lstatus);
316 debug_struct(ciss_bmic_id_table);
317 debug_struct(ciss_bmic_id_pdrive);
318 debug_struct(ciss_bmic_blink_pdrive);
319 debug_struct(ciss_bmic_flush_cache);
320 debug_const(CISS_MAX_REQUESTS);
321 debug_const(CISS_MAX_LOGICAL);
322 debug_const(CISS_INTERRUPT_COALESCE_DELAY);
323 debug_const(CISS_INTERRUPT_COALESCE_COUNT);
324 debug_const(CISS_COMMAND_ALLOC_SIZE);
325 debug_const(CISS_COMMAND_SG_LENGTH);
327 debug_type(cciss_pci_info_struct);
328 debug_type(cciss_coalint_struct);
329 debug_type(cciss_coalint_struct);
330 debug_type(NodeName_type);
331 debug_type(NodeName_type);
332 debug_type(Heartbeat_type);
333 debug_type(BusTypes_type);
334 debug_type(FirmwareVer_type);
335 debug_type(DriverVer_type);
336 debug_type(IOCTL_Command_struct);
339 sc = device_get_softc(dev);
341 callout_init(&sc->ciss_periodic);
344 * Work out adapter type.
346 i = ciss_lookup(dev);
347 if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
348 sc->ciss_interrupt_mask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
349 } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
350 sc->ciss_interrupt_mask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
352 /* really an error on our part */
353 ciss_printf(sc, "unable to determine hardware type\n");
359 * Do PCI-specific init.
361 if ((error = ciss_init_pci(sc)) != 0)
365 * Initialise driver queues.
369 ciss_initq_complete(sc);
372 * Initialise command/request pool.
374 if ((error = ciss_init_requests(sc)) != 0)
378 * Get adapter information.
380 if ((error = ciss_identify_adapter(sc)) != 0)
384 * Build our private table of logical devices.
386 if ((error = ciss_init_logical(sc)) != 0)
390 * Enable interrupts so that the CAM scan can complete.
392 CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
395 * Initialise the CAM interface.
397 if ((error = ciss_cam_init(sc)) != 0)
401 * Start the heartbeat routine and event chain.
406 * Create the control device.
408 dev_ops_add(&ciss_ops, -1, device_get_unit(sc->ciss_dev));
409 sc->ciss_dev_t = make_dev(&ciss_ops, device_get_unit(sc->ciss_dev),
410 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
411 "ciss%d", device_get_unit(sc->ciss_dev));
412 sc->ciss_dev_t->si_drv1 = sc;
415 * The adapter is running; synchronous commands can now sleep
416 * waiting for an interrupt to signal completion.
418 sc->ciss_flags |= CISS_FLAG_RUNNING;
427 /************************************************************************
428 * Detach the driver from this adapter.
431 ciss_detach(device_t dev)
433 struct ciss_softc *sc = device_get_softc(dev);
437 /* flush adapter cache */
438 ciss_flush_adapter(sc);
440 /* release all resources */
447 /************************************************************************
448 * Prepare adapter for system shutdown.
451 ciss_shutdown(device_t dev)
453 struct ciss_softc *sc = device_get_softc(dev);
457 /* flush adapter cache */
458 ciss_flush_adapter(sc);
463 /************************************************************************
464 * Perform PCI-specific attachment actions.
467 ciss_init_pci(struct ciss_softc *sc)
469 uintptr_t cbase, csize, cofs;
475 * Allocate register window first (we need this to find the config
479 sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
480 if ((sc->ciss_regs_resource =
481 bus_alloc_resource(sc->ciss_dev, SYS_RES_MEMORY, &sc->ciss_regs_rid,
482 0, ~0, 1, RF_ACTIVE)) == NULL) {
483 ciss_printf(sc, "can't allocate register window\n");
486 sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
487 sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
490 * Find the BAR holding the config structure. If it's not the one
491 * we already mapped for registers, map it too.
493 sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
494 if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
495 if ((sc->ciss_cfg_resource =
496 bus_alloc_resource(sc->ciss_dev, SYS_RES_MEMORY, &sc->ciss_cfg_rid,
497 0, ~0, 1, RF_ACTIVE)) == NULL) {
498 ciss_printf(sc, "can't allocate config window\n");
501 cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
502 csize = rman_get_end(sc->ciss_cfg_resource) -
503 rman_get_start(sc->ciss_cfg_resource) + 1;
505 cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
506 csize = rman_get_end(sc->ciss_regs_resource) -
507 rman_get_start(sc->ciss_regs_resource) + 1;
509 cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
512 * Use the base/size/offset values we just calculated to
513 * sanity-check the config structure. If it's OK, point to it.
515 if ((cofs + sizeof(struct ciss_config_table)) > csize) {
516 ciss_printf(sc, "config table outside window\n");
519 sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
520 debug(1, "config struct at %p", sc->ciss_cfg);
523 * Validate the config structure. If we supported other transport
524 * methods, we could select amongst them at this point in time.
526 if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
527 ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
528 sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
529 sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
532 if ((sc->ciss_cfg->valence < CISS_MIN_VALENCE) ||
533 (sc->ciss_cfg->valence > CISS_MAX_VALENCE)) {
534 ciss_printf(sc, "adapter interface specification (%d) unsupported\n",
535 sc->ciss_cfg->valence);
540 * Put the board into simple mode, and tell it we're using the low
541 * 4GB of RAM. Set the default interrupt coalescing options.
543 if (!(sc->ciss_cfg->supported_methods & CISS_TRANSPORT_METHOD_SIMPLE)) {
544 ciss_printf(sc, "adapter does not support 'simple' transport layer\n");
547 sc->ciss_cfg->requested_method = CISS_TRANSPORT_METHOD_SIMPLE;
548 sc->ciss_cfg->command_physlimit = 0;
549 sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
550 sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
552 if (ciss_update_config(sc)) {
553 ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
554 CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
557 if (!(sc->ciss_cfg->active_method != CISS_TRANSPORT_METHOD_SIMPLE)) {
559 "adapter refuses to go into 'simple' transport mode (0x%x, 0x%x)\n",
560 sc->ciss_cfg->supported_methods, sc->ciss_cfg->active_method);
565 * Wait for the adapter to come ready.
567 if ((error = ciss_wait_adapter(sc)) != 0)
571 * Turn off interrupts before we go routing anything.
573 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
576 * Allocate and set up our interrupt.
578 sc->ciss_irq_rid = 0;
579 if ((sc->ciss_irq_resource =
580 bus_alloc_resource(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid, 0, ~0, 1,
581 RF_ACTIVE | RF_SHAREABLE)) == NULL) {
582 ciss_printf(sc, "can't allocate interrupt\n");
585 error = bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
587 &sc->ciss_intr, NULL);
589 ciss_printf(sc, "can't set up interrupt\n");
594 * Allocate the parent bus DMA tag appropriate for our PCI
597 * Note that "simple" adapters can only address within a 32-bit
600 if (bus_dma_tag_create(NULL, /* parent */
601 1, 0, /* alignment, boundary */
602 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
603 BUS_SPACE_MAXADDR, /* highaddr */
604 NULL, NULL, /* filter, filterarg */
605 MAXBSIZE, CISS_COMMAND_SG_LENGTH, /* maxsize, nsegments */
606 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
607 BUS_DMA_ALLOCNOW, /* flags */
608 &sc->ciss_parent_dmat)) {
609 ciss_printf(sc, "can't allocate parent DMA tag\n");
614 * Create DMA tag for mapping buffers into adapter-addressable
617 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
618 1, 0, /* alignment, boundary */
619 BUS_SPACE_MAXADDR, /* lowaddr */
620 BUS_SPACE_MAXADDR, /* highaddr */
621 NULL, NULL, /* filter, filterarg */
622 MAXBSIZE, CISS_COMMAND_SG_LENGTH, /* maxsize, nsegments */
623 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
625 &sc->ciss_buffer_dmat)) {
626 ciss_printf(sc, "can't allocate buffer DMA tag\n");
632 /************************************************************************
633 * Wait for the adapter to come ready.
636 ciss_wait_adapter(struct ciss_softc *sc)
643 * Wait for the adapter to come ready.
645 if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
646 ciss_printf(sc, "waiting for adapter to come ready...\n");
647 for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
648 DELAY(1000000); /* one second */
650 ciss_printf(sc, "timed out waiting for adapter to come ready\n");
658 /************************************************************************
659 * Flush the adapter cache.
662 ciss_flush_adapter(struct ciss_softc *sc)
664 struct ciss_request *cr;
665 struct ciss_bmic_flush_cache *cbfc;
666 int error, command_status;
674 * Build a BMIC request to flush the cache. We don't disable
675 * it, as we may be going to do more I/O (eg. we are emulating
676 * the Synchronise Cache command).
678 cbfc = kmalloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_INTWAIT | M_ZERO);
679 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
680 (void **)&cbfc, sizeof(*cbfc))) != 0)
684 * Submit the request and wait for it to complete.
686 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
687 ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
694 ciss_report_request(cr, &command_status, NULL);
695 switch(command_status) {
696 case CISS_CMD_STATUS_SUCCESS:
699 ciss_printf(sc, "error flushing cache (%s)\n",
700 ciss_name_command_status(command_status));
707 kfree(cbfc, CISS_MALLOC_CLASS);
709 ciss_release_request(cr);
713 /************************************************************************
714 * Allocate memory for the adapter command structures, initialise
715 * the request structures.
717 * Note that the entire set of commands are allocated in a single
721 ciss_init_requests(struct ciss_softc *sc)
723 struct ciss_request *cr;
729 * Calculate the number of request structures/commands we are
730 * going to provide for this adapter.
732 sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
734 if (1/*bootverbose*/)
735 ciss_printf(sc, "using %d of %d available commands\n",
736 sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
739 * Create the DMA tag for commands.
741 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
742 1, 0, /* alignment, boundary */
743 BUS_SPACE_MAXADDR, /* lowaddr */
744 BUS_SPACE_MAXADDR, /* highaddr */
745 NULL, NULL, /* filter, filterarg */
746 CISS_COMMAND_ALLOC_SIZE *
747 sc->ciss_max_requests, 1, /* maxsize, nsegments */
748 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
750 &sc->ciss_command_dmat)) {
751 ciss_printf(sc, "can't allocate command DMA tag\n");
755 * Allocate memory and make it available for DMA.
757 if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
758 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
759 ciss_printf(sc, "can't allocate command memory\n");
762 bus_dmamap_create(sc->ciss_command_dmat, 0, &sc->ciss_command_map);
763 bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map, sc->ciss_command,
764 sizeof(struct ciss_command) * sc->ciss_max_requests,
765 ciss_command_map_helper, sc, 0);
766 bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
769 * Set up the request and command structures, push requests onto
772 for (i = 1; i < sc->ciss_max_requests; i++) {
773 cr = &sc->ciss_request[i];
776 ciss_enqueue_free(cr);
782 ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
784 struct ciss_softc *sc = (struct ciss_softc *)arg;
786 sc->ciss_command_phys = segs->ds_addr;
789 /************************************************************************
790 * Identify the adapter, print some information about it.
793 ciss_identify_adapter(struct ciss_softc *sc)
795 struct ciss_request *cr;
796 int error, command_status;
803 * Get a request, allocate storage for the adapter data.
805 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
806 (void **)&sc->ciss_id,
807 sizeof(*sc->ciss_id))) != 0)
811 * Submit the request and wait for it to complete.
813 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
814 ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
821 ciss_report_request(cr, &command_status, NULL);
822 switch(command_status) {
823 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
825 case CISS_CMD_STATUS_DATA_UNDERRUN:
826 case CISS_CMD_STATUS_DATA_OVERRUN:
827 ciss_printf(sc, "data over/underrun reading adapter information\n");
829 ciss_printf(sc, "error reading adapter information (%s)\n",
830 ciss_name_command_status(command_status));
835 /* sanity-check reply */
836 if (!sc->ciss_id->big_map_supported) {
837 ciss_printf(sc, "adapter does not support BIG_MAP\n");
843 /* XXX later revisions may not need this */
844 sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
847 /* XXX only really required for old 5300 adapters? */
848 sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
850 /* print information */
851 if (1/*bootverbose*/) {
852 ciss_printf(sc, " %d logical drive%s configured\n",
853 sc->ciss_id->configured_logical_drives,
854 (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
855 ciss_printf(sc, " firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
856 ciss_printf(sc, " %d SCSI channels\n", sc->ciss_id->scsi_bus_count);
858 ciss_printf(sc, " signature '%.4s'\n", sc->ciss_cfg->signature);
859 ciss_printf(sc, " valence %d\n", sc->ciss_cfg->valence);
860 ciss_printf(sc, " supported I/O methods 0x%b\n",
861 sc->ciss_cfg->supported_methods,
862 "\20\1READY\2simple\3performant\4MEMQ\n");
863 ciss_printf(sc, " active I/O method 0x%b\n",
864 sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
865 ciss_printf(sc, " 4G page base 0x%08x\n",
866 sc->ciss_cfg->command_physlimit);
867 ciss_printf(sc, " interrupt coalesce delay %dus\n",
868 sc->ciss_cfg->interrupt_coalesce_delay);
869 ciss_printf(sc, " interrupt coalesce count %d\n",
870 sc->ciss_cfg->interrupt_coalesce_count);
871 ciss_printf(sc, " max outstanding commands %d\n",
872 sc->ciss_cfg->max_outstanding_commands);
873 ciss_printf(sc, " bus types 0x%b\n", sc->ciss_cfg->bus_types,
874 "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
875 ciss_printf(sc, " server name '%.16s'\n", sc->ciss_cfg->server_name);
876 ciss_printf(sc, " heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
881 if (sc->ciss_id != NULL) {
882 kfree(sc->ciss_id, CISS_MALLOC_CLASS);
887 ciss_release_request(cr);
891 /************************************************************************
892 * Find logical drives on the adapter.
895 ciss_init_logical(struct ciss_softc *sc)
897 struct ciss_request *cr;
898 struct ciss_command *cc;
899 struct ciss_report_cdb *crc;
900 struct ciss_lun_report *cll;
912 * Get a request, allocate storage for the address list.
914 if ((error = ciss_get_request(sc, &cr)) != 0)
916 report_size = sizeof(*cll) + CISS_MAX_LOGICAL * sizeof(union ciss_device_address);
917 cll = kmalloc(report_size, CISS_MALLOC_CLASS, M_INTWAIT | M_ZERO);
920 * Build the Report Logical LUNs command.
922 cc = CISS_FIND_COMMAND(cr);
924 cr->cr_length = report_size;
925 cr->cr_flags = CISS_REQ_DATAIN;
927 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
928 cc->header.address.physical.bus = 0;
929 cc->header.address.physical.target = 0;
930 cc->cdb.cdb_length = sizeof(*crc);
931 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
932 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
933 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
934 cc->cdb.timeout = 30; /* XXX better suggestions? */
936 crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
937 bzero(crc, sizeof(*crc));
938 crc->opcode = CISS_OPCODE_REPORT_LOGICAL_LUNS;
939 crc->length = htonl(report_size); /* big-endian field */
940 cll->list_size = htonl(report_size - sizeof(*cll)); /* big-endian field */
943 * Submit the request and wait for it to complete. (timeout
944 * here should be much greater than above)
946 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
947 ciss_printf(sc, "error sending Report Logical LUNs command (%d)\n", error);
952 * Check response. Note that data over/underrun is OK.
954 ciss_report_request(cr, &command_status, NULL);
955 switch(command_status) {
956 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
957 case CISS_CMD_STATUS_DATA_UNDERRUN: /* buffer too large, not bad */
959 case CISS_CMD_STATUS_DATA_OVERRUN:
960 ciss_printf(sc, "WARNING: more logical drives than driver limit (%d), adjust CISS_MAX_LOGICAL\n",
964 ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
965 ciss_name_command_status(command_status));
969 ciss_release_request(cr);
972 /* sanity-check reply */
973 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
974 if ((ndrives < 0) || (ndrives > CISS_MAX_LOGICAL)) {
975 ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
976 ndrives, CISS_MAX_LOGICAL);
981 * Save logical drive information.
983 if (1/*bootverbose*/)
984 ciss_printf(sc, "%d logical drive%s\n", ndrives, (ndrives > 1) ? "s" : "");
985 if (ndrives != sc->ciss_id->configured_logical_drives)
986 ciss_printf(sc, "logical drive map claims %d drives, but adapter claims %d\n",
987 ndrives, sc->ciss_id->configured_logical_drives);
988 for (i = 0; i < CISS_MAX_LOGICAL; i++) {
990 sc->ciss_logical[i].cl_address = cll->lun[i]; /* XXX endianness? */
991 if (ciss_identify_logical(sc, &sc->ciss_logical[i]) != 0)
994 * If the drive has had media exchanged, we should bring it online.
996 if (sc->ciss_logical[i].cl_lstatus->media_exchanged)
997 ciss_accept_media(sc, i, 0);
1000 sc->ciss_logical[i].cl_status = CISS_LD_NONEXISTENT;
1007 * Note that if the error is a timeout, we are taking a slight
1008 * risk here and assuming that the adapter will not respond at a
1009 * later time, scribbling over host memory.
1012 ciss_release_request(cr);
1014 kfree(cll, CISS_MALLOC_CLASS);
1019 ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1021 struct ciss_request *cr;
1022 struct ciss_command *cc;
1023 struct scsi_inquiry *inq;
1029 lun = ld->cl_address.logical.lun;
1031 bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1033 if ((error = ciss_get_request(sc, &cr)) != 0)
1036 cc = CISS_FIND_COMMAND(cr);
1037 cr->cr_data = &ld->cl_geometry;
1038 cr->cr_length = sizeof(ld->cl_geometry);
1039 cr->cr_flags = CISS_REQ_DATAIN;
1041 cc->header.address.logical.mode = CISS_HDR_ADDRESS_MODE_LOGICAL;
1042 cc->header.address.logical.lun = lun;
1043 cc->cdb.cdb_length = 6;
1044 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1045 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1046 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1047 cc->cdb.timeout = 30;
1049 inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1050 inq->opcode = INQUIRY;
1051 inq->byte2 = SI_EVPD;
1052 inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1053 inq->length = sizeof(ld->cl_geometry);
1055 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1056 ciss_printf(sc, "error getting geometry (%d)\n", error);
1060 ciss_report_request(cr, &command_status, NULL);
1061 switch(command_status) {
1062 case CISS_CMD_STATUS_SUCCESS:
1063 case CISS_CMD_STATUS_DATA_UNDERRUN:
1065 case CISS_CMD_STATUS_DATA_OVERRUN:
1066 ciss_printf(sc, "WARNING: Data overrun\n");
1069 ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1070 ciss_name_command_status(command_status));
1076 ciss_release_request(cr);
1079 /************************************************************************
1080 * Identify a logical drive, initialise state related to it.
1083 ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1085 struct ciss_request *cr;
1086 struct ciss_command *cc;
1087 struct ciss_bmic_cdb *cbc;
1088 int error, command_status;
1095 * Build a BMIC request to fetch the drive ID.
1097 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
1098 (void **)&ld->cl_ldrive,
1099 sizeof(*ld->cl_ldrive))) != 0)
1101 cc = CISS_FIND_COMMAND(cr);
1102 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1103 cbc->log_drive = ld->cl_address.logical.lun;
1106 * Submit the request and wait for it to complete.
1108 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1109 ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
1116 ciss_report_request(cr, &command_status, NULL);
1117 switch(command_status) {
1118 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1120 case CISS_CMD_STATUS_DATA_UNDERRUN:
1121 case CISS_CMD_STATUS_DATA_OVERRUN:
1122 ciss_printf(sc, "data over/underrun reading logical drive ID\n");
1124 ciss_printf(sc, "error reading logical drive ID (%s)\n",
1125 ciss_name_command_status(command_status));
1129 ciss_release_request(cr);
1133 * Build a CISS BMIC command to get the logical drive status.
1135 if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
1139 * Get the logical drive geometry.
1141 if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1145 * Print the drive's basic characteristics.
1147 if (1/*bootverbose*/) {
1148 ciss_printf(sc, "logical drive %d: %s, %dMB ",
1149 cbc->log_drive, ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
1150 ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
1151 ld->cl_ldrive->block_size));
1153 ciss_print_ldrive(sc, ld);
1157 /* make the drive not-exist */
1158 ld->cl_status = CISS_LD_NONEXISTENT;
1159 if (ld->cl_ldrive != NULL) {
1160 kfree(ld->cl_ldrive, CISS_MALLOC_CLASS);
1161 ld->cl_ldrive = NULL;
1163 if (ld->cl_lstatus != NULL) {
1164 kfree(ld->cl_lstatus, CISS_MALLOC_CLASS);
1165 ld->cl_lstatus = NULL;
1169 ciss_release_request(cr);
1174 /************************************************************************
1175 * Get status for a logical drive.
1177 * XXX should we also do this in response to Test Unit Ready?
1180 ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld)
1182 struct ciss_request *cr;
1183 struct ciss_command *cc;
1184 struct ciss_bmic_cdb *cbc;
1185 int error, command_status;
1188 * Build a CISS BMIC command to get the logical drive status.
1190 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
1191 (void **)&ld->cl_lstatus,
1192 sizeof(*ld->cl_lstatus))) != 0)
1194 cc = CISS_FIND_COMMAND(cr);
1195 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1196 cbc->log_drive = ld->cl_address.logical.lun;
1199 * Submit the request and wait for it to complete.
1201 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1202 ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1209 ciss_report_request(cr, &command_status, NULL);
1210 switch(command_status) {
1211 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1213 case CISS_CMD_STATUS_DATA_UNDERRUN:
1214 case CISS_CMD_STATUS_DATA_OVERRUN:
1215 ciss_printf(sc, "data over/underrun reading logical drive status\n");
1217 ciss_printf(sc, "error reading logical drive status (%s)\n",
1218 ciss_name_command_status(command_status));
1224 * Set the drive's summary status based on the returned status.
1226 * XXX testing shows that a failed JBOD drive comes back at next
1227 * boot in "queued for expansion" mode. WTF?
1229 ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
1233 ciss_release_request(cr);
1237 /************************************************************************
1238 * Notify the adapter of a config update.
1241 ciss_update_config(struct ciss_softc *sc)
1247 CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
1248 for (i = 0; i < 1000; i++) {
1249 if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
1250 CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
1258 /************************************************************************
1259 * Accept new media into a logical drive.
1261 * XXX The drive has previously been offline; it would be good if we
1262 * could make sure it's not open right now.
1265 ciss_accept_media(struct ciss_softc *sc, int ldrive, int async)
1267 struct ciss_request *cr;
1268 struct ciss_command *cc;
1269 struct ciss_bmic_cdb *cbc;
1272 debug(0, "bringing logical drive %d back online %ssynchronously",
1273 ldrive, async ? "a" : "");
1276 * Build a CISS BMIC command to bring the drive back online.
1278 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
1281 cc = CISS_FIND_COMMAND(cr);
1282 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1283 cbc->log_drive = ldrive;
1286 * Dispatch the request asynchronously if we can't sleep waiting
1287 * for it to complete.
1290 cr->cr_complete = ciss_accept_media_complete;
1291 if ((error = ciss_start(cr)) != 0)
1296 * Submit the request and wait for it to complete.
1298 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1299 ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1305 * Call the completion callback manually.
1307 ciss_accept_media_complete(cr);
1312 ciss_release_request(cr);
1317 ciss_accept_media_complete(struct ciss_request *cr)
1324 ciss_report_request(cr, &command_status, NULL);
1325 switch(command_status) {
1326 case CISS_CMD_STATUS_SUCCESS: /* all OK */
1327 /* we should get a logical drive status changed event here */
1330 ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
1331 ciss_name_command_status(command_status));
1334 ciss_release_request(cr);
1337 /************************************************************************
1338 * Release adapter resources.
1341 ciss_free(struct ciss_softc *sc)
1345 /* we're going away */
1346 sc->ciss_flags |= CISS_FLAG_ABORTING;
1348 /* terminate the periodic heartbeat routine */
1349 callout_stop(&sc->ciss_periodic);
1351 /* cancel the Event Notify chain */
1352 ciss_notify_abort(sc);
1354 /* free the controller data */
1355 if (sc->ciss_id != NULL)
1356 kfree(sc->ciss_id, CISS_MALLOC_CLASS);
1358 /* release I/O resources */
1359 if (sc->ciss_regs_resource != NULL)
1360 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1361 sc->ciss_regs_rid, sc->ciss_regs_resource);
1362 if (sc->ciss_cfg_resource != NULL)
1363 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1364 sc->ciss_cfg_rid, sc->ciss_cfg_resource);
1365 if (sc->ciss_intr != NULL)
1366 bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
1367 if (sc->ciss_irq_resource != NULL)
1368 bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
1369 sc->ciss_irq_rid, sc->ciss_irq_resource);
1371 /* destroy DMA tags */
1372 if (sc->ciss_parent_dmat)
1373 bus_dma_tag_destroy(sc->ciss_parent_dmat);
1374 if (sc->ciss_buffer_dmat)
1375 bus_dma_tag_destroy(sc->ciss_buffer_dmat);
1377 /* destroy command memory and DMA tag */
1378 if (sc->ciss_command != NULL) {
1379 bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
1380 bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
1381 bus_dmamap_destroy(sc->ciss_command_dmat, sc->ciss_command_map);
1383 if (sc->ciss_buffer_dmat)
1384 bus_dma_tag_destroy(sc->ciss_command_dmat);
1386 /* disconnect from CAM */
1387 if (sc->ciss_cam_sim) {
1388 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim));
1389 cam_sim_free(sc->ciss_cam_sim);
1391 if (sc->ciss_cam_devq)
1392 cam_simq_release(sc->ciss_cam_devq);
1393 /* XXX what about ciss_cam_path? */
1396 /************************************************************************
1397 * Give a command to the adapter.
1399 * Note that this uses the simple transport layer directly. If we
1400 * want to add support for other layers, we'll need a switch of some
1403 * Note that the simple transport layer has no way of refusing a
1404 * command; we only have as many request structures as the adapter
1405 * supports commands, so we don't have to check (this presumes that
1406 * the adapter can handle commands as fast as we throw them at it).
1409 ciss_start(struct ciss_request *cr)
1411 struct ciss_command *cc; /* XXX debugging only */
1414 cc = CISS_FIND_COMMAND(cr);
1415 debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
1418 * Map the request's data.
1420 if ((error = ciss_map_request(cr)))
1424 ciss_print_request(cr);
1428 * Post the command to the adapter.
1430 ciss_enqueue_busy(cr);
1431 CISS_TL_SIMPLE_POST_CMD(cr->cr_sc, CISS_FIND_COMMANDPHYS(cr));
1436 /************************************************************************
1437 * Fetch completed request(s) from the adapter, queue them for
1438 * completion handling.
1440 * Note that this uses the simple transport layer directly. If we
1441 * want to add support for other layers, we'll need a switch of some
1444 * Note that the simple transport mechanism does not require any
1445 * reentrancy protection; the OPQ read is atomic. If there is a
1446 * chance of a race with something else that might move the request
1447 * off the busy list, then we will have to lock against that
1448 * (eg. timeouts, etc.)
1451 ciss_done(struct ciss_softc *sc)
1453 struct ciss_request *cr;
1454 struct ciss_command *cc;
1455 u_int32_t tag, index;
1461 * Loop quickly taking requests from the adapter and moving them
1462 * from the busy queue to the completed queue.
1467 /* see if the OPQ contains anything */
1468 if (!CISS_TL_SIMPLE_OPQ_INTERRUPT(sc))
1471 tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
1472 if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
1475 debug(2, "completed command %d%s", index,
1476 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
1477 if (index >= sc->ciss_max_requests) {
1478 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
1481 cr = &(sc->ciss_request[index]);
1482 cc = CISS_FIND_COMMAND(cr);
1483 cc->header.host_tag = tag; /* not updated by adapter */
1484 if (ciss_remove_busy(cr)) {
1485 /* assume this is garbage out of the adapter */
1486 ciss_printf(sc, "completed nonbusy request %d\n", index);
1488 ciss_enqueue_complete(cr);
1494 * Invoke completion processing. If we can defer this out of
1495 * interrupt context, that'd be good.
1501 /************************************************************************
1502 * Take an interrupt from the adapter.
1505 ciss_intr(void *arg)
1507 struct ciss_softc *sc = (struct ciss_softc *)arg;
1510 * The only interrupt we recognise indicates that there are
1511 * entries in the outbound post queue.
1516 /************************************************************************
1517 * Process completed requests.
1519 * Requests can be completed in three fashions:
1521 * - by invoking a callback function (cr_complete is non-null)
1522 * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
1523 * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
1526 ciss_complete(struct ciss_softc *sc)
1528 struct ciss_request *cr;
1533 * Loop taking requests off the completed queue and performing
1534 * completion processing on them.
1537 if ((cr = ciss_dequeue_complete(sc)) == NULL)
1539 ciss_unmap_request(cr);
1542 * If the request has a callback, invoke it.
1544 if (cr->cr_complete != NULL) {
1545 cr->cr_complete(cr);
1550 * If someone is sleeping on this request, wake them up.
1552 if (cr->cr_flags & CISS_REQ_SLEEP) {
1553 cr->cr_flags &= ~CISS_REQ_SLEEP;
1559 * If someone is polling this request for completion, signal.
1561 if (cr->cr_flags & CISS_REQ_POLL) {
1562 cr->cr_flags &= ~CISS_REQ_POLL;
1567 * Give up and throw the request back on the free queue. This
1568 * should never happen; resources will probably be lost.
1570 ciss_printf(sc, "WARNING: completed command with no submitter\n");
1571 ciss_enqueue_free(cr);
1575 /************************************************************************
1576 * Report on the completion status of a request, and pass back SCSI
1577 * and command status values.
1580 ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status)
1582 struct ciss_command *cc;
1583 struct ciss_error_info *ce;
1587 cc = CISS_FIND_COMMAND(cr);
1588 ce = (struct ciss_error_info *)&(cc->sg[0]);
1591 * We don't consider data under/overrun an error for the Report
1592 * Logical/Physical LUNs commands.
1594 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
1595 ((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
1596 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS))) {
1597 cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
1598 debug(2, "ignoring irrelevant under/overrun error");
1602 * Check the command's error bit, if clear, there's no status and
1605 if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
1606 if (scsi_status != NULL)
1607 *scsi_status = SCSI_STATUS_OK;
1608 if (command_status != NULL)
1609 *command_status = CISS_CMD_STATUS_SUCCESS;
1612 if (command_status != NULL)
1613 *command_status = ce->command_status;
1614 if (scsi_status != NULL) {
1615 if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
1616 *scsi_status = ce->scsi_status;
1622 ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
1623 ce->command_status, ciss_name_command_status(ce->command_status),
1625 if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
1626 ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x\n",
1627 ce->additional_error_info.invalid_command.offense_size,
1628 ce->additional_error_info.invalid_command.offense_offset,
1629 ce->additional_error_info.invalid_command.offense_value);
1635 /************************************************************************
1636 * Issue a request and don't return until it's completed.
1638 * Depending on adapter status, we may poll or sleep waiting for
1642 ciss_synch_request(struct ciss_request *cr, int timeout)
1644 if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
1645 return(ciss_wait_request(cr, timeout));
1647 return(ciss_poll_request(cr, timeout));
1651 /************************************************************************
1652 * Issue a request and poll for completion.
1654 * Timeout in milliseconds.
1657 ciss_poll_request(struct ciss_request *cr, int timeout)
1663 cr->cr_flags |= CISS_REQ_POLL;
1664 if ((error = ciss_start(cr)) != 0)
1668 ciss_done(cr->cr_sc);
1669 if (!(cr->cr_flags & CISS_REQ_POLL))
1672 } while (timeout-- >= 0);
1673 return(EWOULDBLOCK);
1676 /************************************************************************
1677 * Issue a request and sleep waiting for completion.
1679 * Timeout in milliseconds. Note that a spurious wakeup will reset
1683 ciss_wait_request(struct ciss_request *cr, int timeout)
1689 cr->cr_flags |= CISS_REQ_SLEEP;
1690 if ((error = ciss_start(cr)) != 0)
1694 while (cr->cr_flags & CISS_REQ_SLEEP) {
1695 error = tsleep(cr, PCATCH, "cissREQ", (timeout * hz) / 1000);
1697 * On wakeup or interruption due to restartable activity, go
1698 * back and check to see if we're done.
1700 if ((error == 0) || (error == ERESTART)) {
1705 * Timeout, interrupted system call, etc.
1714 /************************************************************************
1715 * Abort a request. Note that a potential exists here to race the
1716 * request being completed; the caller must deal with this.
1719 ciss_abort_request(struct ciss_request *ar)
1721 struct ciss_request *cr;
1722 struct ciss_command *cc;
1723 struct ciss_message_cdb *cmc;
1729 if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
1732 /* build the abort command */
1733 cc = CISS_FIND_COMMAND(cr);
1734 cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL; /* addressing? */
1735 cc->header.address.physical.target = 0;
1736 cc->header.address.physical.bus = 0;
1737 cc->cdb.cdb_length = sizeof(*cmc);
1738 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
1739 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1740 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
1741 cc->cdb.timeout = 30;
1743 cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
1744 cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
1745 cmc->type = CISS_MESSAGE_ABORT_TASK;
1746 cmc->abort_tag = ar->cr_tag; /* endianness?? */
1749 * Send the request and wait for a response. If we believe we
1750 * aborted the request OK, clear the flag that indicates it's
1753 error = ciss_synch_request(cr, 35 * 1000);
1755 error = ciss_report_request(cr, NULL, NULL);
1756 ciss_release_request(cr);
1763 /************************************************************************
1764 * Fetch and initialise a request
1767 ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
1769 struct ciss_request *cr;
1774 * Get a request and clean it up.
1776 if ((cr = ciss_dequeue_free(sc)) == NULL)
1781 cr->cr_complete = NULL;
1783 ciss_preen_command(cr);
1789 ciss_preen_command(struct ciss_request *cr)
1791 struct ciss_command *cc;
1795 * Clean up the command structure.
1797 * Note that we set up the error_info structure here, since the
1798 * length can be overwritten by any command.
1800 cc = CISS_FIND_COMMAND(cr);
1801 cc->header.sg_in_list = 0; /* kinda inefficient this way */
1802 cc->header.sg_total = 0;
1803 cc->header.host_tag = cr->cr_tag << 2;
1804 cc->header.host_tag_zeroes = 0;
1805 cmdphys = CISS_FIND_COMMANDPHYS(cr);
1806 cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
1807 cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
1811 /************************************************************************
1812 * Release a request to the free list.
1815 ciss_release_request(struct ciss_request *cr)
1817 struct ciss_softc *sc;
1823 /* release the request to the free queue */
1824 ciss_requeue_free(cr);
1827 /************************************************************************
1828 * Allocate a request that will be used to send a BMIC command. Do some
1829 * of the common setup here to avoid duplicating it everywhere else.
1832 ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
1833 int opcode, void **bufp, size_t bufsize)
1835 struct ciss_request *cr;
1836 struct ciss_command *cc;
1837 struct ciss_bmic_cdb *cbc;
1850 if ((error = ciss_get_request(sc, &cr)) != 0)
1854 * Allocate data storage if requested, determine the data direction.
1857 if ((bufsize > 0) && (bufp != NULL)) {
1858 if (*bufp == NULL) {
1859 buf = kmalloc(bufsize, CISS_MALLOC_CLASS, M_INTWAIT | M_ZERO);
1862 dataout = 1; /* we are given a buffer, so we are writing */
1867 * Build a CISS BMIC command to get the logical drive ID.
1870 cr->cr_length = bufsize;
1872 cr->cr_flags = CISS_REQ_DATAIN;
1874 cc = CISS_FIND_COMMAND(cr);
1875 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
1876 cc->header.address.physical.bus = 0;
1877 cc->header.address.physical.target = 0;
1878 cc->cdb.cdb_length = sizeof(*cbc);
1879 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1880 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1881 cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
1882 cc->cdb.timeout = 0;
1884 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1885 bzero(cbc, sizeof(*cbc));
1886 cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
1887 cbc->bmic_opcode = opcode;
1888 cbc->size = htons((u_int16_t)bufsize);
1893 ciss_release_request(cr);
1894 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
1895 kfree(buf, CISS_MALLOC_CLASS);
1898 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
1904 /************************************************************************
1905 * Handle a command passed in from userspace.
1908 ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
1910 struct ciss_request *cr;
1911 struct ciss_command *cc;
1912 struct ciss_error_info *ce;
1922 if ((error = ciss_get_request(sc, &cr)) != 0)
1924 cc = CISS_FIND_COMMAND(cr);
1927 * Allocate an in-kernel databuffer if required, copy in user data.
1929 cr->cr_length = ioc->buf_size;
1930 if (ioc->buf_size > 0) {
1931 if ((cr->cr_data = kmalloc(ioc->buf_size, CISS_MALLOC_CLASS, M_WAITOK)) == NULL) {
1935 if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
1936 debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
1942 * Build the request based on the user command.
1944 bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
1945 bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
1947 /* XXX anything else to populate here? */
1952 if ((error = ciss_synch_request(cr, 60 * 1000))) {
1953 debug(0, "request failed - %d", error);
1958 * Copy the results back to the user.
1960 ce = (struct ciss_error_info *)&(cc->sg[0]);
1961 bcopy(ce, &ioc->error_info, sizeof(*ce));
1962 if ((ioc->buf_size > 0) &&
1963 (error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
1964 debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
1972 if ((cr != NULL) && (cr->cr_data != NULL))
1973 kfree(cr->cr_data, CISS_MALLOC_CLASS);
1975 ciss_release_request(cr);
1979 /************************************************************************
1980 * Map a request into bus-visible space, initialise the scatter/gather
1984 ciss_map_request(struct ciss_request *cr)
1986 struct ciss_softc *sc;
1992 /* check that mapping is necessary */
1993 if ((cr->cr_flags & CISS_REQ_MAPPED) || (cr->cr_data == NULL))
1996 bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
1997 bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap, cr->cr_data, cr->cr_length,
1998 ciss_request_map_helper, CISS_FIND_COMMAND(cr), 0);
2000 if (cr->cr_flags & CISS_REQ_DATAIN)
2001 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2002 if (cr->cr_flags & CISS_REQ_DATAOUT)
2003 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2005 cr->cr_flags |= CISS_REQ_MAPPED;
2010 ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2012 struct ciss_command *cc;
2017 cc = (struct ciss_command *)arg;
2018 for (i = 0; i < nseg; i++) {
2019 cc->sg[i].address = segs[i].ds_addr;
2020 cc->sg[i].length = segs[i].ds_len;
2021 cc->sg[i].extension = 0;
2023 /* we leave the s/g table entirely within the command */
2024 cc->header.sg_in_list = nseg;
2025 cc->header.sg_total = nseg;
2028 /************************************************************************
2029 * Unmap a request from bus-visible space.
2032 ciss_unmap_request(struct ciss_request *cr)
2034 struct ciss_softc *sc;
2040 /* check that unmapping is necessary */
2041 if (!(cr->cr_flags & CISS_REQ_MAPPED) || (cr->cr_data == NULL))
2044 if (cr->cr_flags & CISS_REQ_DATAIN)
2045 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
2046 if (cr->cr_flags & CISS_REQ_DATAOUT)
2047 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
2049 bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2050 bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
2051 cr->cr_flags &= ~CISS_REQ_MAPPED;
2054 /************************************************************************
2055 * Attach the driver to CAM.
2057 * We put all the logical drives on a single SCSI bus.
2060 ciss_cam_init(struct ciss_softc *sc)
2066 * Allocate a devq. We can reuse this for the masked physical
2067 * devices if we decide to export these as well.
2069 if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests)) == NULL) {
2070 ciss_printf(sc, "can't allocate CAM SIM queue\n");
2077 if ((sc->ciss_cam_sim = cam_sim_alloc(ciss_cam_action, ciss_cam_poll, "ciss", sc,
2078 device_get_unit(sc->ciss_dev),
2079 sc->ciss_max_requests - 2,
2081 sc->ciss_cam_devq)) == NULL) {
2082 ciss_printf(sc, "can't allocate CAM SIM\n");
2087 * Register bus 0 (the 'logical drives' bus) with this SIM.
2089 if (xpt_bus_register(sc->ciss_cam_sim, 0) != 0) {
2090 ciss_printf(sc, "can't register SCSI bus 0\n");
2095 * Initiate a rescan of the bus.
2097 ciss_cam_rescan_all(sc);
2102 /************************************************************************
2103 * Initiate a rescan of the 'logical devices' SIM
2106 ciss_cam_rescan_target(struct ciss_softc *sc, int target)
2112 if ((ccb = kmalloc(sizeof(union ccb), M_TEMP, M_WAITOK | M_ZERO)) == NULL) {
2113 ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
2117 if (xpt_create_path(&sc->ciss_cam_path, xpt_periph, cam_sim_path(sc->ciss_cam_sim), target, 0)
2119 ciss_printf(sc, "rescan failed (can't create path)\n");
2123 xpt_setup_ccb(&ccb->ccb_h, sc->ciss_cam_path, 5/*priority (low)*/);
2124 ccb->ccb_h.func_code = XPT_SCAN_BUS;
2125 ccb->ccb_h.cbfcnp = ciss_cam_rescan_callback;
2126 ccb->crcn.flags = CAM_FLAG_NONE;
2129 /* scan is now in progress */
2133 ciss_cam_rescan_all(struct ciss_softc *sc)
2135 return(ciss_cam_rescan_target(sc, 0));
2139 ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2141 xpt_free_path(ccb->ccb_h.path);
2145 /************************************************************************
2146 * Handle requests coming from CAM
2149 ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
2151 struct ciss_softc *sc;
2152 struct ccb_scsiio *csio;
2155 sc = cam_sim_softc(sim);
2156 csio = (struct ccb_scsiio *)&ccb->csio;
2157 target = csio->ccb_h.target_id;
2159 switch (ccb->ccb_h.func_code) {
2161 /* perform SCSI I/O */
2163 if (!ciss_cam_action_io(sim, csio))
2167 /* perform geometry calculations */
2168 case XPT_CALC_GEOMETRY:
2170 struct ccb_calc_geometry *ccg = &ccb->ccg;
2171 struct ciss_ldrive *ld = &sc->ciss_logical[target];
2173 debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2176 * Use the cached geometry settings unless the fault tolerance
2179 if (ld->cl_geometry.fault_tolerance == 0xFF) {
2180 u_int32_t secs_per_cylinder;
2183 ccg->secs_per_track = 32;
2184 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2185 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2187 ccg->heads = ld->cl_geometry.heads;
2188 ccg->secs_per_track = ld->cl_geometry.sectors;
2189 ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2191 ccb->ccb_h.status = CAM_REQ_CMP;
2195 /* handle path attribute inquiry */
2198 struct ccb_pathinq *cpi = &ccb->cpi;
2200 debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2202 cpi->version_num = 1;
2203 cpi->hba_inquiry = PI_TAG_ABLE; /* XXX is this correct? */
2204 cpi->target_sprt = 0;
2206 cpi->max_target = CISS_MAX_LOGICAL;
2207 cpi->max_lun = 0; /* 'logical drive' channel only */
2208 cpi->initiator_id = CISS_MAX_LOGICAL;
2209 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2210 strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
2211 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2212 cpi->unit_number = cam_sim_unit(sim);
2213 cpi->bus_id = cam_sim_bus(sim);
2214 cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */
2215 ccb->ccb_h.status = CAM_REQ_CMP;
2219 case XPT_GET_TRAN_SETTINGS:
2221 struct ccb_trans_settings *cts = &ccb->cts;
2224 bus = cam_sim_bus(sim);
2225 target = cts->ccb_h.target_id;
2227 debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
2230 /* disconnect always OK */
2231 cts->flags |= CCB_TRANS_DISC_ENB;
2232 cts->valid |= CCB_TRANS_DISC_VALID;
2234 cts->ccb_h.status = CAM_REQ_CMP;
2238 default: /* we can't do this */
2239 debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
2240 ccb->ccb_h.status = CAM_REQ_INVALID;
2247 /************************************************************************
2248 * Handle a CAM SCSI I/O request.
2251 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
2253 struct ciss_softc *sc;
2255 struct ciss_request *cr;
2256 struct ciss_command *cc;
2259 sc = cam_sim_softc(sim);
2260 bus = cam_sim_bus(sim);
2261 target = csio->ccb_h.target_id;
2263 debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
2265 /* check for I/O attempt to nonexistent device */
2267 (target > CISS_MAX_LOGICAL) ||
2268 (sc->ciss_logical[target].cl_status == CISS_LD_NONEXISTENT)) {
2269 debug(3, " device does not exist");
2270 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2273 /* firmware does not support commands > 10 bytes */
2274 if (csio->cdb_len > 12/*CISS_CDB_BUFFER_SIZE*/) {
2275 debug(3, " command too large (%d > %d)", csio->cdb_len, CISS_CDB_BUFFER_SIZE);
2276 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2279 /* check that the CDB pointer is not to a physical address */
2280 if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
2281 debug(3, " CDB pointer is to physical address");
2282 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2285 /* if there is data transfer, it must be to/from a virtual address */
2286 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
2287 if (csio->ccb_h.flags & CAM_DATA_PHYS) { /* we can't map it */
2288 debug(3, " data pointer is to physical address");
2289 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2291 if (csio->ccb_h.flags & CAM_SCATTER_VALID) { /* we want to do the s/g setup */
2292 debug(3, " data has premature s/g setup");
2293 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2297 /* abandon aborted ccbs or those that have failed validation */
2298 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
2299 debug(3, "abandoning CCB due to abort/validation failure");
2303 /* handle emulation of some SCSI commands ourself */
2304 if (ciss_cam_emulate(sc, csio))
2308 * Get a request to manage this command. If we can't, return the
2309 * ccb, freeze the queue and flag so that we unfreeze it when a
2310 * request completes.
2312 if ((error = ciss_get_request(sc, &cr)) != 0) {
2313 xpt_freeze_simq(sc->ciss_cam_sim, 1);
2314 csio->ccb_h.status |= CAM_REQUEUE_REQ;
2319 * Build the command.
2321 cc = CISS_FIND_COMMAND(cr);
2322 cr->cr_data = csio->data_ptr;
2323 cr->cr_length = csio->dxfer_len;
2324 cr->cr_complete = ciss_cam_complete;
2325 cr->cr_private = csio;
2327 cc->header.address.logical.mode = CISS_HDR_ADDRESS_MODE_LOGICAL;
2328 cc->header.address.logical.lun = target;
2329 cc->cdb.cdb_length = csio->cdb_len;
2330 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2331 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; /* XXX ordered tags? */
2332 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2333 cr->cr_flags = CISS_REQ_DATAOUT;
2334 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
2335 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2336 cr->cr_flags = CISS_REQ_DATAIN;
2337 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
2340 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2342 cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
2343 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
2344 bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
2346 bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
2350 * Submit the request to the adapter.
2352 * Note that this may fail if we're unable to map the request (and
2353 * if we ever learn a transport layer other than simple, may fail
2354 * if the adapter rejects the command).
2356 if ((error = ciss_start(cr)) != 0) {
2357 xpt_freeze_simq(sc->ciss_cam_sim, 1);
2358 csio->ccb_h.status |= CAM_REQUEUE_REQ;
2359 ciss_release_request(cr);
2366 /************************************************************************
2367 * Emulate SCSI commands the adapter doesn't handle as we might like.
2370 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
2376 target = csio->ccb_h.target_id;
2377 opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
2378 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
2381 * Handle requests for volumes that don't exist. A selection timeout
2382 * is slightly better than an illegal request. Other errors might be
2385 if (sc->ciss_logical[target].cl_status == CISS_LD_NONEXISTENT) {
2386 csio->ccb_h.status = CAM_SEL_TIMEOUT;
2387 xpt_done((union ccb *)csio);
2392 * Handle requests for volumes that exist but are offline.
2394 * I/O operations should fail, everything else should work.
2396 if (sc->ciss_logical[target].cl_status == CISS_LD_OFFLINE) {
2404 csio->ccb_h.status = CAM_SEL_TIMEOUT;
2405 xpt_done((union ccb *)csio);
2411 /* if we have to fake Synchronise Cache */
2412 if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
2415 * If this is a Synchronise Cache command, typically issued when
2416 * a device is closed, flush the adapter and complete now.
2418 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
2419 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
2420 ciss_flush_adapter(sc);
2421 csio->ccb_h.status = CAM_REQ_CMP;
2422 xpt_done((union ccb *)csio);
2430 /************************************************************************
2431 * Check for possibly-completed commands.
2434 ciss_cam_poll(struct cam_sim *sim)
2436 struct ciss_softc *sc = cam_sim_softc(sim);
2443 /************************************************************************
2444 * Handle completion of a command - pass results back through the CCB
2447 ciss_cam_complete(struct ciss_request *cr)
2449 struct ciss_softc *sc;
2450 struct ciss_command *cc;
2451 struct ciss_error_info *ce;
2452 struct ccb_scsiio *csio;
2459 cc = CISS_FIND_COMMAND(cr);
2460 ce = (struct ciss_error_info *)&(cc->sg[0]);
2461 csio = (struct ccb_scsiio *)cr->cr_private;
2464 * Extract status values from request.
2466 ciss_report_request(cr, &command_status, &scsi_status);
2467 csio->scsi_status = scsi_status;
2470 * Handle specific SCSI status values.
2472 switch(scsi_status) {
2473 /* no status due to adapter error */
2475 debug(0, "adapter error");
2476 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2479 /* no status due to command completed OK */
2480 case SCSI_STATUS_OK: /* CISS_SCSI_STATUS_GOOD */
2481 debug(2, "SCSI_STATUS_OK");
2482 csio->ccb_h.status = CAM_REQ_CMP;
2485 /* check condition, sense data included */
2486 case SCSI_STATUS_CHECK_COND: /* CISS_SCSI_STATUS_CHECK_CONDITION */
2487 debug(0, "SCSI_STATUS_CHECK_COND sense size %d resid %d",
2488 ce->sense_length, ce->residual_count);
2489 bzero(&csio->sense_data, SSD_FULL_SIZE);
2490 bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
2491 csio->sense_len = ce->sense_length;
2492 csio->resid = ce->residual_count;
2493 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
2496 struct scsi_sense_data *sns = (struct scsi_sense_data *)&ce->sense_info[0];
2497 debug(0, "sense key %x", sns->flags & SSD_KEY);
2502 case SCSI_STATUS_BUSY: /* CISS_SCSI_STATUS_BUSY */
2503 debug(0, "SCSI_STATUS_BUSY");
2504 csio->ccb_h.status = CAM_SCSI_BUSY;
2508 debug(0, "unknown status 0x%x", csio->scsi_status);
2509 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2513 /* handle post-command fixup */
2514 ciss_cam_complete_fixup(sc, csio);
2516 /* tell CAM we're ready for more commands */
2517 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
2519 xpt_done((union ccb *)csio);
2520 ciss_release_request(cr);
2523 /********************************************************************************
2524 * Fix up the result of some commands here.
2527 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
2529 struct scsi_inquiry_data *inq;
2530 struct ciss_ldrive *cl;
2533 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
2534 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == INQUIRY) {
2536 inq = (struct scsi_inquiry_data *)csio->data_ptr;
2537 target = csio->ccb_h.target_id;
2538 cl = &sc->ciss_logical[target];
2540 padstr(inq->vendor, "COMPAQ", 8);
2541 padstr(inq->product, ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance), 8);
2542 padstr(inq->revision, ciss_name_ldrive_status(cl->cl_lstatus->status), 16);
2547 /********************************************************************************
2548 * Find a peripheral attached at (target)
2550 static struct cam_periph *
2551 ciss_find_periph(struct ciss_softc *sc, int target)
2553 struct cam_periph *periph;
2554 struct cam_path *path;
2557 status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim), target, 0);
2558 if (status == CAM_REQ_CMP) {
2559 periph = cam_periph_find(path, NULL);
2560 xpt_free_path(path);
2567 /********************************************************************************
2568 * Name the device at (target)
2570 * XXX is this strictly correct?
2573 ciss_name_device(struct ciss_softc *sc, int target)
2575 struct cam_periph *periph;
2577 if ((periph = ciss_find_periph(sc, target)) != NULL) {
2578 sprintf(sc->ciss_logical[target].cl_name, "%s%d", periph->periph_name, periph->unit_number);
2581 sc->ciss_logical[target].cl_name[0] = 0;
2585 /************************************************************************
2586 * Periodic status monitoring.
2589 ciss_periodic(void *arg)
2591 struct ciss_softc *sc;
2595 sc = (struct ciss_softc *)arg;
2598 * Check the adapter heartbeat.
2600 if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
2601 sc->ciss_heart_attack++;
2602 debug(0, "adapter heart attack in progress 0x%x/%d",
2603 sc->ciss_heartbeat, sc->ciss_heart_attack);
2604 if (sc->ciss_heart_attack == 3) {
2605 ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
2606 /* XXX should reset adapter here */
2609 sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
2610 sc->ciss_heart_attack = 0;
2611 debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
2615 * If the notify event request has died for some reason, or has
2616 * not started yet, restart it.
2618 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
2619 debug(0, "(re)starting Event Notify chain");
2620 ciss_notify_event(sc);
2626 if (!(sc->ciss_flags & CISS_FLAG_ABORTING))
2627 callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz,
2631 /************************************************************************
2632 * Request a notification response from the adapter.
2634 * If (cr) is NULL, this is the first request of the adapter, so
2635 * reset the adapter's message pointer and start with the oldest
2636 * message available.
2639 ciss_notify_event(struct ciss_softc *sc)
2641 struct ciss_request *cr;
2642 struct ciss_command *cc;
2643 struct ciss_notify_cdb *cnc;
2648 cr = sc->ciss_periodic_notify;
2650 /* get a request if we don't already have one */
2652 if ((error = ciss_get_request(sc, &cr)) != 0) {
2653 debug(0, "can't get notify event request");
2656 sc->ciss_periodic_notify = cr;
2657 cr->cr_complete = ciss_notify_complete;
2658 debug(1, "acquired request %d", cr->cr_tag);
2662 * Get a databuffer if we don't already have one, note that the
2663 * adapter command wants a larger buffer than the actual
2666 if (cr->cr_data == NULL) {
2667 cr->cr_data = kmalloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_INTWAIT);
2668 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
2671 /* re-setup the request's command (since we never release it) XXX overkill*/
2672 ciss_preen_command(cr);
2674 /* (re)build the notify event command */
2675 cc = CISS_FIND_COMMAND(cr);
2676 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2677 cc->header.address.physical.bus = 0;
2678 cc->header.address.physical.target = 0;
2680 cc->cdb.cdb_length = sizeof(*cnc);
2681 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2682 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2683 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
2684 cc->cdb.timeout = 0; /* no timeout, we hope */
2686 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
2687 bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
2688 cnc->opcode = CISS_OPCODE_READ;
2689 cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
2690 cnc->timeout = 0; /* no timeout, we hope */
2691 cnc->synchronous = 0;
2693 cnc->seek_to_oldest = 0;
2695 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
2697 /* submit the request */
2698 error = ciss_start(cr);
2703 if (cr->cr_data != NULL)
2704 kfree(cr->cr_data, CISS_MALLOC_CLASS);
2705 ciss_release_request(cr);
2707 sc->ciss_periodic_notify = NULL;
2708 debug(0, "can't submit notify event request");
2709 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2711 debug(1, "notify event submitted");
2712 sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
2717 ciss_notify_complete(struct ciss_request *cr)
2719 struct ciss_command *cc;
2720 struct ciss_notify *cn;
2721 struct ciss_softc *sc;
2727 cc = CISS_FIND_COMMAND(cr);
2728 cn = (struct ciss_notify *)cr->cr_data;
2732 * Report request results, decode status.
2734 ciss_report_request(cr, &command_status, &scsi_status);
2737 * Abort the chain on a fatal error.
2739 * XXX which of these are actually errors?
2741 if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
2742 (command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
2743 (command_status != CISS_CMD_STATUS_TIMEOUT)) { /* XXX timeout? */
2744 ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
2745 ciss_name_command_status(command_status));
2746 ciss_release_request(cr);
2747 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2752 * If the adapter gave us a text message, print it.
2754 if (cn->message[0] != 0)
2755 ciss_printf(sc, "*** %.80s\n", cn->message);
2757 debug(0, "notify event class %d subclass %d detail %d",
2758 cn->class, cn->subclass, cn->detail);
2761 * If there's room, save the event for a user-level tool.
2763 if (((sc->ciss_notify_head + 1) % CISS_MAX_EVENTS) != sc->ciss_notify_tail) {
2764 sc->ciss_notify[sc->ciss_notify_head] = *cn;
2765 sc->ciss_notify_head = (sc->ciss_notify_head + 1) % CISS_MAX_EVENTS;
2769 * Some events are directly of interest to us.
2771 switch (cn->class) {
2772 case CISS_NOTIFY_LOGICAL:
2773 ciss_notify_logical(sc, cn);
2775 case CISS_NOTIFY_PHYSICAL:
2776 ciss_notify_physical(sc, cn);
2781 * If the response indicates that the notifier has been aborted,
2782 * release the notifier command.
2784 if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
2785 (cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
2786 (cn->detail == 1)) {
2787 debug(0, "notifier exiting");
2788 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2789 ciss_release_request(cr);
2790 sc->ciss_periodic_notify = NULL;
2791 wakeup(&sc->ciss_periodic_notify);
2795 * Send a new notify event command, if we're not aborting.
2797 if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
2798 ciss_notify_event(sc);
2802 /************************************************************************
2803 * Abort the Notify Event chain.
2805 * Note that we can't just abort the command in progress; we have to
2806 * explicitly issue an Abort Notify Event command in order for the
2807 * adapter to clean up correctly.
2809 * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
2810 * the chain will not restart itself.
2813 ciss_notify_abort(struct ciss_softc *sc)
2815 struct ciss_request *cr;
2816 struct ciss_command *cc;
2817 struct ciss_notify_cdb *cnc;
2818 int error, command_status, scsi_status;
2825 /* verify that there's an outstanding command */
2826 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
2829 /* get a command to issue the abort with */
2830 if ((error = ciss_get_request(sc, &cr)))
2833 /* get a buffer for the result */
2834 cr->cr_data = kmalloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_INTWAIT);
2835 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
2838 cc = CISS_FIND_COMMAND(cr);
2839 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2840 cc->header.address.physical.bus = 0;
2841 cc->header.address.physical.target = 0;
2842 cc->cdb.cdb_length = sizeof(*cnc);
2843 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2844 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2845 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
2846 cc->cdb.timeout = 0; /* no timeout, we hope */
2848 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
2849 bzero(cnc, sizeof(*cnc));
2850 cnc->opcode = CISS_OPCODE_WRITE;
2851 cnc->command = CISS_COMMAND_ABORT_NOTIFY;
2852 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
2854 ciss_print_request(cr);
2857 * Submit the request and wait for it to complete.
2859 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
2860 ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
2867 ciss_report_request(cr, &command_status, &scsi_status);
2868 switch(command_status) {
2869 case CISS_CMD_STATUS_SUCCESS:
2871 case CISS_CMD_STATUS_INVALID_COMMAND:
2873 * Some older adapters don't support the CISS version of this
2874 * command. Fall back to using the BMIC version.
2876 error = ciss_notify_abort_bmic(sc);
2881 case CISS_CMD_STATUS_TARGET_STATUS:
2883 * This can happen if the adapter thinks there wasn't an outstanding
2884 * Notify Event command but we did. We clean up here.
2886 if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
2887 if (sc->ciss_periodic_notify != NULL)
2888 ciss_release_request(sc->ciss_periodic_notify);
2895 ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
2896 ciss_name_command_status(command_status));
2902 * Sleep waiting for the notifier command to complete. Note
2903 * that if it doesn't, we may end up in a bad situation, since
2904 * the adapter may deliver it later. Also note that the adapter
2905 * requires the Notify Event command to be cancelled in order to
2906 * maintain internal bookkeeping.
2909 while (sc->ciss_periodic_notify != NULL) {
2910 error = tsleep(&sc->ciss_periodic_notify, 0, "cissNEA", hz * 5);
2911 if (error == EWOULDBLOCK) {
2912 ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
2919 /* release the cancel request */
2921 if (cr->cr_data != NULL)
2922 kfree(cr->cr_data, CISS_MALLOC_CLASS);
2923 ciss_release_request(cr);
2926 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2930 /************************************************************************
2931 * Abort the Notify Event chain using a BMIC command.
2934 ciss_notify_abort_bmic(struct ciss_softc *sc)
2936 struct ciss_request *cr;
2937 int error, command_status;
2944 /* verify that there's an outstanding command */
2945 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
2949 * Build a BMIC command to cancel the Notify on Event command.
2951 * Note that we are sending a CISS opcode here. Odd.
2953 if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
2958 * Submit the request and wait for it to complete.
2960 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
2961 ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
2968 ciss_report_request(cr, &command_status, NULL);
2969 switch(command_status) {
2970 case CISS_CMD_STATUS_SUCCESS:
2973 ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
2974 ciss_name_command_status(command_status));
2981 ciss_release_request(cr);
2985 /************************************************************************
2986 * Handle a notify event relating to the status of a logical drive.
2988 * XXX need to be able to defer some of these to properly handle
2989 * calling the "ID Physical drive" command, unless the 'extended'
2990 * drive IDs are always in BIG_MAP format.
2993 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
2995 struct ciss_ldrive *ld;
3000 ld = &sc->ciss_logical[cn->data.logical_status.logical_drive];
3002 switch (cn->subclass) {
3003 case CISS_NOTIFY_LOGICAL_STATUS:
3004 switch (cn->detail) {
3006 ciss_name_device(sc, cn->data.logical_status.logical_drive);
3007 ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
3008 cn->data.logical_status.logical_drive, ld->cl_name,
3009 ciss_name_ldrive_status(cn->data.logical_status.previous_state),
3010 ciss_name_ldrive_status(cn->data.logical_status.new_state),
3011 cn->data.logical_status.spare_state,
3012 "\20\1configured\2rebuilding\3failed\4in use\5available\n");
3015 * Update our idea of the drive's status.
3017 ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
3018 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3019 if (ld->cl_lstatus != NULL)
3020 ld->cl_lstatus->status = cn->data.logical_status.new_state;
3024 * Have CAM rescan the drive if its status has changed.
3026 if (ostatus != ld->cl_status)
3027 ciss_cam_rescan_target(sc, cn->data.logical_status.logical_drive);
3032 case 1: /* logical drive has recognised new media, needs Accept Media Exchange */
3033 ciss_name_device(sc, cn->data.logical_status.logical_drive);
3034 ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
3035 cn->data.logical_status.logical_drive, ld->cl_name);
3036 ciss_accept_media(sc, cn->data.logical_status.logical_drive, 1);
3041 ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
3042 cn->data.rebuild_aborted.logical_drive,
3043 sc->ciss_logical[cn->data.rebuild_aborted.logical_drive].cl_name,
3044 (cn->detail == 2) ? "read" : "write");
3049 case CISS_NOTIFY_LOGICAL_ERROR:
3050 if (cn->detail == 0) {
3051 ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
3052 cn->data.io_error.logical_drive,
3053 sc->ciss_logical[cn->data.io_error.logical_drive].cl_name,
3054 cn->data.io_error.failure_bus,
3055 cn->data.io_error.failure_drive);
3056 /* XXX should we take the drive down at this point, or will we be told? */
3060 case CISS_NOTIFY_LOGICAL_SURFACE:
3061 if (cn->detail == 0)
3062 ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
3063 cn->data.consistency_completed.logical_drive,
3064 sc->ciss_logical[cn->data.consistency_completed.logical_drive].cl_name);
3069 /************************************************************************
3070 * Handle a notify event relating to the status of a physical drive.
3073 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
3078 /************************************************************************
3082 ciss_print_request(struct ciss_request *cr)
3084 struct ciss_softc *sc;
3085 struct ciss_command *cc;
3089 cc = CISS_FIND_COMMAND(cr);
3091 ciss_printf(sc, "REQUEST @ %p\n", cr);
3092 ciss_printf(sc, " data %p/%d tag %d flags %b\n",
3093 cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
3094 "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
3095 ciss_printf(sc, " sg list/total %d/%d host tag 0x%x\n",
3096 cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
3097 switch(cc->header.address.mode.mode) {
3098 case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
3099 case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
3100 ciss_printf(sc, " physical bus %d target %d\n",
3101 cc->header.address.physical.bus, cc->header.address.physical.target);
3103 case CISS_HDR_ADDRESS_MODE_LOGICAL:
3104 ciss_printf(sc, " logical unit %d\n", cc->header.address.logical.lun);
3107 ciss_printf(sc, " %s cdb length %d type %s attribute %s\n",
3108 (cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
3109 (cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
3110 (cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
3112 (cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
3113 (cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
3114 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
3115 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
3116 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
3117 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
3118 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
3119 ciss_printf(sc, " %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
3121 if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
3122 /* XXX print error info */
3124 /* since we don't use chained s/g, don't support it here */
3125 for (i = 0; i < cc->header.sg_in_list; i++) {
3127 ciss_printf(sc, " ");
3128 printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
3129 if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
3135 /************************************************************************
3136 * Print information about the status of a logical drive.
3139 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
3143 if (ld->cl_lstatus == NULL) {
3144 printf("does not exist\n");
3148 /* print drive status */
3149 switch(ld->cl_lstatus->status) {
3150 case CISS_LSTATUS_OK:
3153 case CISS_LSTATUS_INTERIM_RECOVERY:
3154 printf("in interim recovery mode\n");
3156 case CISS_LSTATUS_READY_RECOVERY:
3157 printf("ready to begin recovery\n");
3159 case CISS_LSTATUS_RECOVERING:
3160 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
3161 target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
3162 printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
3163 bus, target, ld->cl_lstatus->blocks_to_recover);
3165 case CISS_LSTATUS_EXPANDING:
3166 printf("being expanded, %u blocks remaining\n",
3167 ld->cl_lstatus->blocks_to_recover);
3169 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
3170 printf("queued for expansion\n");
3172 case CISS_LSTATUS_FAILED:
3173 printf("queued for expansion\n");
3175 case CISS_LSTATUS_WRONG_PDRIVE:
3176 printf("wrong physical drive inserted\n");
3178 case CISS_LSTATUS_MISSING_PDRIVE:
3179 printf("missing a needed physical drive\n");
3181 case CISS_LSTATUS_BECOMING_READY:
3182 printf("becoming ready\n");
3186 /* print failed physical drives */
3187 for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
3188 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
3189 target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
3192 ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
3193 ld->cl_lstatus->drive_failure_map[i]);
3198 /************************************************************************
3199 * Print information about the controller/driver.
3202 ciss_print_adapter(struct ciss_softc *sc)
3206 ciss_printf(sc, "ADAPTER:\n");
3207 for (i = 0; i < CISSQ_COUNT; i++) {
3208 ciss_printf(sc, "%s %d/%d\n",
3210 i == 1 ? "busy" : "complete",
3211 sc->ciss_qstat[i].q_length,
3212 sc->ciss_qstat[i].q_max);
3214 ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
3215 ciss_printf(sc, "notify_head/tail %d/%d\n",
3216 sc->ciss_notify_head, sc->ciss_notify_tail);
3217 ciss_printf(sc, "flags %b\n", sc->ciss_flags,
3218 "\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
3220 for (i = 0; i < CISS_MAX_LOGICAL; i++) {
3221 ciss_printf(sc, "LOGICAL DRIVE %d: ", i);
3222 ciss_print_ldrive(sc, sc->ciss_logical + i);
3225 for (i = 1; i < sc->ciss_max_requests; i++)
3226 ciss_print_request(sc->ciss_request + i);
3230 void ciss_print0(void);
3236 struct ciss_softc *sc;
3238 sc = devclass_get_softc(devclass_find("ciss"), 0);
3240 printf("no ciss controllers\n");
3242 ciss_print_adapter(sc);
3247 /************************************************************************
3248 * Return a name for a logical drive status value.
3251 ciss_name_ldrive_status(int status)
3254 case CISS_LSTATUS_OK:
3256 case CISS_LSTATUS_FAILED:
3258 case CISS_LSTATUS_NOT_CONFIGURED:
3259 return("not configured");
3260 case CISS_LSTATUS_INTERIM_RECOVERY:
3261 return("interim recovery");
3262 case CISS_LSTATUS_READY_RECOVERY:
3263 return("ready for recovery");
3264 case CISS_LSTATUS_RECOVERING:
3265 return("recovering");
3266 case CISS_LSTATUS_WRONG_PDRIVE:
3267 return("wrong physical drive inserted");
3268 case CISS_LSTATUS_MISSING_PDRIVE:
3269 return("missing physical drive");
3270 case CISS_LSTATUS_EXPANDING:
3271 return("expanding");
3272 case CISS_LSTATUS_BECOMING_READY:
3273 return("becoming ready");
3274 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
3275 return("queued for expansion");
3277 return("unknown status");
3280 /************************************************************************
3281 * Return an online/offline/nonexistent value for a logical drive
3285 ciss_decode_ldrive_status(int status)
3288 case CISS_LSTATUS_NOT_CONFIGURED:
3289 return(CISS_LD_NONEXISTENT);
3291 case CISS_LSTATUS_OK:
3292 case CISS_LSTATUS_INTERIM_RECOVERY:
3293 case CISS_LSTATUS_READY_RECOVERY:
3294 case CISS_LSTATUS_RECOVERING:
3295 case CISS_LSTATUS_EXPANDING:
3296 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
3297 return(CISS_LD_ONLINE);
3299 case CISS_LSTATUS_FAILED:
3300 case CISS_LSTATUS_WRONG_PDRIVE:
3301 case CISS_LSTATUS_MISSING_PDRIVE:
3302 case CISS_LSTATUS_BECOMING_READY:
3304 return(CISS_LD_OFFLINE);
3309 /************************************************************************
3310 * Return a name for a logical drive's organisation.
3313 ciss_name_ldrive_org(int org)
3316 case CISS_LDRIVE_RAID0:
3318 case CISS_LDRIVE_RAID1:
3320 case CISS_LDRIVE_RAID4:
3322 case CISS_LDRIVE_RAID5:
3328 /************************************************************************
3329 * Return a name for a command status value.
3332 ciss_name_command_status(int status)
3335 case CISS_CMD_STATUS_SUCCESS:
3337 case CISS_CMD_STATUS_TARGET_STATUS:
3338 return("target status");
3339 case CISS_CMD_STATUS_DATA_UNDERRUN:
3340 return("data underrun");
3341 case CISS_CMD_STATUS_DATA_OVERRUN:
3342 return("data overrun");
3343 case CISS_CMD_STATUS_INVALID_COMMAND:
3344 return("invalid command");
3345 case CISS_CMD_STATUS_PROTOCOL_ERROR:
3346 return("protocol error");
3347 case CISS_CMD_STATUS_HARDWARE_ERROR:
3348 return("hardware error");
3349 case CISS_CMD_STATUS_CONNECTION_LOST:
3350 return("connection lost");
3351 case CISS_CMD_STATUS_ABORTED:
3353 case CISS_CMD_STATUS_ABORT_FAILED:
3354 return("abort failed");
3355 case CISS_CMD_STATUS_UNSOLICITED_ABORT:
3356 return("unsolicited abort");
3357 case CISS_CMD_STATUS_TIMEOUT:
3359 case CISS_CMD_STATUS_UNABORTABLE:
3360 return("unabortable");
3362 return("unknown status");
3365 /************************************************************************
3366 * Handle an open on the control device.
3369 ciss_open(struct dev_open_args *ap)
3371 dev_t dev = ap->a_head.a_dev;
3372 struct ciss_softc *sc;
3376 sc = (struct ciss_softc *)dev->si_drv1;
3378 /* we might want to veto if someone already has us open */
3380 sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
3384 /************************************************************************
3385 * Handle the last close on the control device.
3388 ciss_close(struct dev_close_args *ap)
3390 dev_t dev = ap->a_head.a_dev;
3391 struct ciss_softc *sc;
3395 sc = (struct ciss_softc *)dev->si_drv1;
3397 sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
3401 /********************************************************************************
3402 * Handle adapter-specific control operations.
3404 * Note that the API here is compatible with the Linux driver, in order to
3405 * simplify the porting of Compaq's userland tools.
3408 ciss_ioctl(struct dev_ioctl_args *ap)
3410 dev_t dev = ap->a_head.a_dev;
3411 struct ciss_softc *sc;
3416 sc = (struct ciss_softc *)dev->si_drv1;
3420 case CCISS_GETPCIINFO:
3422 cciss_pci_info_struct *pis = (cciss_pci_info_struct *)ap->a_data;
3424 pis->bus = pci_get_bus(sc->ciss_dev);
3425 pis->dev_fn = pci_get_slot(sc->ciss_dev);
3426 pis->board_id = pci_get_devid(sc->ciss_dev);
3431 case CCISS_GETINTINFO:
3433 cciss_coalint_struct *cis = (cciss_coalint_struct *)ap->a_data;
3435 cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
3436 cis->count = sc->ciss_cfg->interrupt_coalesce_count;
3441 case CCISS_SETINTINFO:
3443 cciss_coalint_struct *cis = (cciss_coalint_struct *)ap->a_data;
3445 if ((cis->delay == 0) && (cis->count == 0)) {
3451 * XXX apparently this is only safe if the controller is idle,
3452 * we should suspend it before doing this.
3454 sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
3455 sc->ciss_cfg->interrupt_coalesce_count = cis->count;
3457 if (ciss_update_config(sc))
3460 /* XXX resume the controller here */
3464 case CCISS_GETNODENAME:
3465 bcopy(sc->ciss_cfg->server_name, (NodeName_type *)ap->a_data,
3466 sizeof(NodeName_type));
3469 case CCISS_SETNODENAME:
3470 bcopy((NodeName_type *)ap->a_data, sc->ciss_cfg->server_name,
3471 sizeof(NodeName_type));
3472 if (ciss_update_config(sc))
3476 case CCISS_GETHEARTBEAT:
3477 *(Heartbeat_type *)ap->a_data = sc->ciss_cfg->heartbeat;
3480 case CCISS_GETBUSTYPES:
3481 *(BusTypes_type *)ap->a_data = sc->ciss_cfg->bus_types;
3484 case CCISS_GETFIRMVER:
3485 bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)ap->a_data,
3486 sizeof(FirmwareVer_type));
3489 case CCISS_GETDRIVERVER:
3490 *(DriverVer_type *)ap->a_data = CISS_DRIVER_VERSION;
3493 case CCISS_REVALIDVOLS:
3495 * This is a bit ugly; to do it "right" we really need
3496 * to find any disks that have changed, kick CAM off them,
3497 * then rescan only these disks. It'd be nice if they
3498 * a) told us which disk(s) they were going to play with,
3499 * and b) which ones had arrived. 8(
3503 case CCISS_PASSTHRU:
3504 error = ciss_user_command(sc, (IOCTL_Command_struct *)ap->a_data);
3508 debug(0, "unknown ioctl 0x%lx", ap->a_cmd);
3510 debug(1, "CCISS_GETPCIINFO: 0x%lx", CCISS_GETPCIINFO);
3511 debug(1, "CCISS_GETINTINFO: 0x%lx", CCISS_GETINTINFO);
3512 debug(1, "CCISS_SETINTINFO: 0x%lx", CCISS_SETINTINFO);
3513 debug(1, "CCISS_GETNODENAME: 0x%lx", CCISS_GETNODENAME);
3514 debug(1, "CCISS_SETNODENAME: 0x%lx", CCISS_SETNODENAME);
3515 debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
3516 debug(1, "CCISS_GETBUSTYPES: 0x%lx", CCISS_GETBUSTYPES);
3517 debug(1, "CCISS_GETFIRMVER: 0x%lx", CCISS_GETFIRMVER);
3518 debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
3519 debug(1, "CCISS_REVALIDVOLS: 0x%lx", CCISS_REVALIDVOLS);
3520 debug(1, "CCISS_PASSTHRU: 0x%lx", CCISS_PASSTHRU);