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.13 2005/04/25 07:05:55 joerg 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);
344 callout_init(&sc->ciss_periodic);
347 * Work out adapter type.
349 i = ciss_lookup(dev);
350 if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
351 sc->ciss_interrupt_mask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
352 } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
353 sc->ciss_interrupt_mask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
355 /* really an error on our part */
356 ciss_printf(sc, "unable to determine hardware type\n");
362 * Do PCI-specific init.
364 if ((error = ciss_init_pci(sc)) != 0)
368 * Initialise driver queues.
372 ciss_initq_complete(sc);
375 * Initialise command/request pool.
377 if ((error = ciss_init_requests(sc)) != 0)
381 * Get adapter information.
383 if ((error = ciss_identify_adapter(sc)) != 0)
387 * Build our private table of logical devices.
389 if ((error = ciss_init_logical(sc)) != 0)
393 * Enable interrupts so that the CAM scan can complete.
395 CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
398 * Initialise the CAM interface.
400 if ((error = ciss_cam_init(sc)) != 0)
404 * Start the heartbeat routine and event chain.
409 * Create the control device.
411 cdevsw_add(&ciss_cdevsw, -1, device_get_unit(sc->ciss_dev));
412 sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
413 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
414 "ciss%d", device_get_unit(sc->ciss_dev));
415 sc->ciss_dev_t->si_drv1 = sc;
418 * The adapter is running; synchronous commands can now sleep
419 * waiting for an interrupt to signal completion.
421 sc->ciss_flags |= CISS_FLAG_RUNNING;
430 /************************************************************************
431 * Detach the driver from this adapter.
434 ciss_detach(device_t dev)
436 struct ciss_softc *sc = device_get_softc(dev);
440 /* flush adapter cache */
441 ciss_flush_adapter(sc);
443 /* release all resources */
450 /************************************************************************
451 * Prepare adapter for system shutdown.
454 ciss_shutdown(device_t dev)
456 struct ciss_softc *sc = device_get_softc(dev);
460 /* flush adapter cache */
461 ciss_flush_adapter(sc);
466 /************************************************************************
467 * Perform PCI-specific attachment actions.
470 ciss_init_pci(struct ciss_softc *sc)
472 uintptr_t cbase, csize, cofs;
478 * Allocate register window first (we need this to find the config
482 sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
483 if ((sc->ciss_regs_resource =
484 bus_alloc_resource(sc->ciss_dev, SYS_RES_MEMORY, &sc->ciss_regs_rid,
485 0, ~0, 1, RF_ACTIVE)) == NULL) {
486 ciss_printf(sc, "can't allocate register window\n");
489 sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
490 sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
493 * Find the BAR holding the config structure. If it's not the one
494 * we already mapped for registers, map it too.
496 sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
497 if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
498 if ((sc->ciss_cfg_resource =
499 bus_alloc_resource(sc->ciss_dev, SYS_RES_MEMORY, &sc->ciss_cfg_rid,
500 0, ~0, 1, RF_ACTIVE)) == NULL) {
501 ciss_printf(sc, "can't allocate config window\n");
504 cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
505 csize = rman_get_end(sc->ciss_cfg_resource) -
506 rman_get_start(sc->ciss_cfg_resource) + 1;
508 cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
509 csize = rman_get_end(sc->ciss_regs_resource) -
510 rman_get_start(sc->ciss_regs_resource) + 1;
512 cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
515 * Use the base/size/offset values we just calculated to
516 * sanity-check the config structure. If it's OK, point to it.
518 if ((cofs + sizeof(struct ciss_config_table)) > csize) {
519 ciss_printf(sc, "config table outside window\n");
522 sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
523 debug(1, "config struct at %p", sc->ciss_cfg);
526 * Validate the config structure. If we supported other transport
527 * methods, we could select amongst them at this point in time.
529 if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
530 ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
531 sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
532 sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
535 if ((sc->ciss_cfg->valence < CISS_MIN_VALENCE) ||
536 (sc->ciss_cfg->valence > CISS_MAX_VALENCE)) {
537 ciss_printf(sc, "adapter interface specification (%d) unsupported\n",
538 sc->ciss_cfg->valence);
543 * Put the board into simple mode, and tell it we're using the low
544 * 4GB of RAM. Set the default interrupt coalescing options.
546 if (!(sc->ciss_cfg->supported_methods & CISS_TRANSPORT_METHOD_SIMPLE)) {
547 ciss_printf(sc, "adapter does not support 'simple' transport layer\n");
550 sc->ciss_cfg->requested_method = CISS_TRANSPORT_METHOD_SIMPLE;
551 sc->ciss_cfg->command_physlimit = 0;
552 sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
553 sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
555 if (ciss_update_config(sc)) {
556 ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
557 CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
560 if (!(sc->ciss_cfg->active_method != CISS_TRANSPORT_METHOD_SIMPLE)) {
562 "adapter refuses to go into 'simple' transport mode (0x%x, 0x%x)\n",
563 sc->ciss_cfg->supported_methods, sc->ciss_cfg->active_method);
568 * Wait for the adapter to come ready.
570 if ((error = ciss_wait_adapter(sc)) != 0)
574 * Turn off interrupts before we go routing anything.
576 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
579 * Allocate and set up our interrupt.
581 sc->ciss_irq_rid = 0;
582 if ((sc->ciss_irq_resource =
583 bus_alloc_resource(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid, 0, ~0, 1,
584 RF_ACTIVE | RF_SHAREABLE)) == NULL) {
585 ciss_printf(sc, "can't allocate interrupt\n");
588 if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource, INTR_TYPE_CAM, ciss_intr, sc,
590 ciss_printf(sc, "can't set up interrupt\n");
595 * Allocate the parent bus DMA tag appropriate for our PCI
598 * Note that "simple" adapters can only address within a 32-bit
601 if (bus_dma_tag_create(NULL, /* parent */
602 1, 0, /* alignment, boundary */
603 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
604 BUS_SPACE_MAXADDR, /* highaddr */
605 NULL, NULL, /* filter, filterarg */
606 MAXBSIZE, CISS_COMMAND_SG_LENGTH, /* maxsize, nsegments */
607 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
608 BUS_DMA_ALLOCNOW, /* flags */
609 &sc->ciss_parent_dmat)) {
610 ciss_printf(sc, "can't allocate parent DMA tag\n");
615 * Create DMA tag for mapping buffers into adapter-addressable
618 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
619 1, 0, /* alignment, boundary */
620 BUS_SPACE_MAXADDR, /* lowaddr */
621 BUS_SPACE_MAXADDR, /* highaddr */
622 NULL, NULL, /* filter, filterarg */
623 MAXBSIZE, CISS_COMMAND_SG_LENGTH, /* maxsize, nsegments */
624 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
626 &sc->ciss_buffer_dmat)) {
627 ciss_printf(sc, "can't allocate buffer DMA tag\n");
633 /************************************************************************
634 * Wait for the adapter to come ready.
637 ciss_wait_adapter(struct ciss_softc *sc)
644 * Wait for the adapter to come ready.
646 if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
647 ciss_printf(sc, "waiting for adapter to come ready...\n");
648 for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
649 DELAY(1000000); /* one second */
651 ciss_printf(sc, "timed out waiting for adapter to come ready\n");
659 /************************************************************************
660 * Flush the adapter cache.
663 ciss_flush_adapter(struct ciss_softc *sc)
665 struct ciss_request *cr;
666 struct ciss_bmic_flush_cache *cbfc;
667 int error, command_status;
675 * Build a BMIC request to flush the cache. We don't disable
676 * it, as we may be going to do more I/O (eg. we are emulating
677 * the Synchronise Cache command).
679 cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_INTWAIT | M_ZERO);
680 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
681 (void **)&cbfc, sizeof(*cbfc))) != 0)
685 * Submit the request and wait for it to complete.
687 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
688 ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
695 ciss_report_request(cr, &command_status, NULL);
696 switch(command_status) {
697 case CISS_CMD_STATUS_SUCCESS:
700 ciss_printf(sc, "error flushing cache (%s)\n",
701 ciss_name_command_status(command_status));
708 free(cbfc, CISS_MALLOC_CLASS);
710 ciss_release_request(cr);
714 /************************************************************************
715 * Allocate memory for the adapter command structures, initialise
716 * the request structures.
718 * Note that the entire set of commands are allocated in a single
722 ciss_init_requests(struct ciss_softc *sc)
724 struct ciss_request *cr;
730 * Calculate the number of request structures/commands we are
731 * going to provide for this adapter.
733 sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
735 if (1/*bootverbose*/)
736 ciss_printf(sc, "using %d of %d available commands\n",
737 sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
740 * Create the DMA tag for commands.
742 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
743 1, 0, /* alignment, boundary */
744 BUS_SPACE_MAXADDR, /* lowaddr */
745 BUS_SPACE_MAXADDR, /* highaddr */
746 NULL, NULL, /* filter, filterarg */
747 CISS_COMMAND_ALLOC_SIZE *
748 sc->ciss_max_requests, 1, /* maxsize, nsegments */
749 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
751 &sc->ciss_command_dmat)) {
752 ciss_printf(sc, "can't allocate command DMA tag\n");
756 * Allocate memory and make it available for DMA.
758 if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
759 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
760 ciss_printf(sc, "can't allocate command memory\n");
763 bus_dmamap_create(sc->ciss_command_dmat, 0, &sc->ciss_command_map);
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 cll = malloc(report_size, CISS_MALLOC_CLASS, M_INTWAIT | M_ZERO);
921 * Build the Report Logical LUNs command.
923 cc = CISS_FIND_COMMAND(cr);
925 cr->cr_length = report_size;
926 cr->cr_flags = CISS_REQ_DATAIN;
928 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
929 cc->header.address.physical.bus = 0;
930 cc->header.address.physical.target = 0;
931 cc->cdb.cdb_length = sizeof(*crc);
932 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
933 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
934 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
935 cc->cdb.timeout = 30; /* XXX better suggestions? */
937 crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
938 bzero(crc, sizeof(*crc));
939 crc->opcode = CISS_OPCODE_REPORT_LOGICAL_LUNS;
940 crc->length = htonl(report_size); /* big-endian field */
941 cll->list_size = htonl(report_size - sizeof(*cll)); /* big-endian field */
944 * Submit the request and wait for it to complete. (timeout
945 * here should be much greater than above)
947 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
948 ciss_printf(sc, "error sending Report Logical LUNs command (%d)\n", error);
953 * Check response. Note that data over/underrun is OK.
955 ciss_report_request(cr, &command_status, NULL);
956 switch(command_status) {
957 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
958 case CISS_CMD_STATUS_DATA_UNDERRUN: /* buffer too large, not bad */
960 case CISS_CMD_STATUS_DATA_OVERRUN:
961 ciss_printf(sc, "WARNING: more logical drives than driver limit (%d), adjust CISS_MAX_LOGICAL\n",
965 ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
966 ciss_name_command_status(command_status));
970 ciss_release_request(cr);
973 /* sanity-check reply */
974 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
975 if ((ndrives < 0) || (ndrives > CISS_MAX_LOGICAL)) {
976 ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
977 ndrives, CISS_MAX_LOGICAL);
982 * Save logical drive information.
984 if (1/*bootverbose*/)
985 ciss_printf(sc, "%d logical drive%s\n", ndrives, (ndrives > 1) ? "s" : "");
986 if (ndrives != sc->ciss_id->configured_logical_drives)
987 ciss_printf(sc, "logical drive map claims %d drives, but adapter claims %d\n",
988 ndrives, sc->ciss_id->configured_logical_drives);
989 for (i = 0; i < CISS_MAX_LOGICAL; i++) {
991 sc->ciss_logical[i].cl_address = cll->lun[i]; /* XXX endianness? */
992 if (ciss_identify_logical(sc, &sc->ciss_logical[i]) != 0)
995 * If the drive has had media exchanged, we should bring it online.
997 if (sc->ciss_logical[i].cl_lstatus->media_exchanged)
998 ciss_accept_media(sc, i, 0);
1001 sc->ciss_logical[i].cl_status = CISS_LD_NONEXISTENT;
1008 * Note that if the error is a timeout, we are taking a slight
1009 * risk here and assuming that the adapter will not respond at a
1010 * later time, scribbling over host memory.
1013 ciss_release_request(cr);
1015 free(cll, CISS_MALLOC_CLASS);
1020 ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1022 struct ciss_request *cr;
1023 struct ciss_command *cc;
1024 struct scsi_inquiry *inq;
1030 lun = ld->cl_address.logical.lun;
1032 bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1034 if ((error = ciss_get_request(sc, &cr)) != 0)
1037 cc = CISS_FIND_COMMAND(cr);
1038 cr->cr_data = &ld->cl_geometry;
1039 cr->cr_length = sizeof(ld->cl_geometry);
1040 cr->cr_flags = CISS_REQ_DATAIN;
1042 cc->header.address.logical.mode = CISS_HDR_ADDRESS_MODE_LOGICAL;
1043 cc->header.address.logical.lun = lun;
1044 cc->cdb.cdb_length = 6;
1045 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1046 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1047 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1048 cc->cdb.timeout = 30;
1050 inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1051 inq->opcode = INQUIRY;
1052 inq->byte2 = SI_EVPD;
1053 inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1054 inq->length = sizeof(ld->cl_geometry);
1056 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1057 ciss_printf(sc, "error getting geometry (%d)\n", error);
1061 ciss_report_request(cr, &command_status, NULL);
1062 switch(command_status) {
1063 case CISS_CMD_STATUS_SUCCESS:
1064 case CISS_CMD_STATUS_DATA_UNDERRUN:
1066 case CISS_CMD_STATUS_DATA_OVERRUN:
1067 ciss_printf(sc, "WARNING: Data overrun\n");
1070 ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1071 ciss_name_command_status(command_status));
1077 ciss_release_request(cr);
1080 /************************************************************************
1081 * Identify a logical drive, initialise state related to it.
1084 ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1086 struct ciss_request *cr;
1087 struct ciss_command *cc;
1088 struct ciss_bmic_cdb *cbc;
1089 int error, command_status;
1096 * Build a BMIC request to fetch the drive ID.
1098 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
1099 (void **)&ld->cl_ldrive,
1100 sizeof(*ld->cl_ldrive))) != 0)
1102 cc = CISS_FIND_COMMAND(cr);
1103 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1104 cbc->log_drive = ld->cl_address.logical.lun;
1107 * Submit the request and wait for it to complete.
1109 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1110 ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
1117 ciss_report_request(cr, &command_status, NULL);
1118 switch(command_status) {
1119 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1121 case CISS_CMD_STATUS_DATA_UNDERRUN:
1122 case CISS_CMD_STATUS_DATA_OVERRUN:
1123 ciss_printf(sc, "data over/underrun reading logical drive ID\n");
1125 ciss_printf(sc, "error reading logical drive ID (%s)\n",
1126 ciss_name_command_status(command_status));
1130 ciss_release_request(cr);
1134 * Build a CISS BMIC command to get the logical drive status.
1136 if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
1140 * Get the logical drive geometry.
1142 if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1146 * Print the drive's basic characteristics.
1148 if (1/*bootverbose*/) {
1149 ciss_printf(sc, "logical drive %d: %s, %dMB ",
1150 cbc->log_drive, ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
1151 ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
1152 ld->cl_ldrive->block_size));
1154 ciss_print_ldrive(sc, ld);
1158 /* make the drive not-exist */
1159 ld->cl_status = CISS_LD_NONEXISTENT;
1160 if (ld->cl_ldrive != NULL) {
1161 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
1162 ld->cl_ldrive = NULL;
1164 if (ld->cl_lstatus != NULL) {
1165 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
1166 ld->cl_lstatus = NULL;
1170 ciss_release_request(cr);
1175 /************************************************************************
1176 * Get status for a logical drive.
1178 * XXX should we also do this in response to Test Unit Ready?
1181 ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld)
1183 struct ciss_request *cr;
1184 struct ciss_command *cc;
1185 struct ciss_bmic_cdb *cbc;
1186 int error, command_status;
1189 * Build a CISS BMIC command to get the logical drive status.
1191 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
1192 (void **)&ld->cl_lstatus,
1193 sizeof(*ld->cl_lstatus))) != 0)
1195 cc = CISS_FIND_COMMAND(cr);
1196 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1197 cbc->log_drive = ld->cl_address.logical.lun;
1200 * Submit the request and wait for it to complete.
1202 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1203 ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1210 ciss_report_request(cr, &command_status, NULL);
1211 switch(command_status) {
1212 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1214 case CISS_CMD_STATUS_DATA_UNDERRUN:
1215 case CISS_CMD_STATUS_DATA_OVERRUN:
1216 ciss_printf(sc, "data over/underrun reading logical drive status\n");
1218 ciss_printf(sc, "error reading logical drive status (%s)\n",
1219 ciss_name_command_status(command_status));
1225 * Set the drive's summary status based on the returned status.
1227 * XXX testing shows that a failed JBOD drive comes back at next
1228 * boot in "queued for expansion" mode. WTF?
1230 ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
1234 ciss_release_request(cr);
1238 /************************************************************************
1239 * Notify the adapter of a config update.
1242 ciss_update_config(struct ciss_softc *sc)
1248 CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
1249 for (i = 0; i < 1000; i++) {
1250 if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
1251 CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
1259 /************************************************************************
1260 * Accept new media into a logical drive.
1262 * XXX The drive has previously been offline; it would be good if we
1263 * could make sure it's not open right now.
1266 ciss_accept_media(struct ciss_softc *sc, int ldrive, int async)
1268 struct ciss_request *cr;
1269 struct ciss_command *cc;
1270 struct ciss_bmic_cdb *cbc;
1273 debug(0, "bringing logical drive %d back online %ssynchronously",
1274 ldrive, async ? "a" : "");
1277 * Build a CISS BMIC command to bring the drive back online.
1279 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
1282 cc = CISS_FIND_COMMAND(cr);
1283 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1284 cbc->log_drive = ldrive;
1287 * Dispatch the request asynchronously if we can't sleep waiting
1288 * for it to complete.
1291 cr->cr_complete = ciss_accept_media_complete;
1292 if ((error = ciss_start(cr)) != 0)
1297 * Submit the request and wait for it to complete.
1299 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1300 ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1306 * Call the completion callback manually.
1308 ciss_accept_media_complete(cr);
1313 ciss_release_request(cr);
1318 ciss_accept_media_complete(struct ciss_request *cr)
1325 ciss_report_request(cr, &command_status, NULL);
1326 switch(command_status) {
1327 case CISS_CMD_STATUS_SUCCESS: /* all OK */
1328 /* we should get a logical drive status changed event here */
1331 ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
1332 ciss_name_command_status(command_status));
1335 ciss_release_request(cr);
1338 /************************************************************************
1339 * Release adapter resources.
1342 ciss_free(struct ciss_softc *sc)
1346 /* we're going away */
1347 sc->ciss_flags |= CISS_FLAG_ABORTING;
1349 /* terminate the periodic heartbeat routine */
1350 callout_stop(&sc->ciss_periodic);
1352 /* cancel the Event Notify chain */
1353 ciss_notify_abort(sc);
1355 /* free the controller data */
1356 if (sc->ciss_id != NULL)
1357 free(sc->ciss_id, CISS_MALLOC_CLASS);
1359 /* release I/O resources */
1360 if (sc->ciss_regs_resource != NULL)
1361 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1362 sc->ciss_regs_rid, sc->ciss_regs_resource);
1363 if (sc->ciss_cfg_resource != NULL)
1364 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1365 sc->ciss_cfg_rid, sc->ciss_cfg_resource);
1366 if (sc->ciss_intr != NULL)
1367 bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
1368 if (sc->ciss_irq_resource != NULL)
1369 bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
1370 sc->ciss_irq_rid, sc->ciss_irq_resource);
1372 /* destroy DMA tags */
1373 if (sc->ciss_parent_dmat)
1374 bus_dma_tag_destroy(sc->ciss_parent_dmat);
1375 if (sc->ciss_buffer_dmat)
1376 bus_dma_tag_destroy(sc->ciss_buffer_dmat);
1378 /* destroy command memory and DMA tag */
1379 if (sc->ciss_command != NULL) {
1380 bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
1381 bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
1382 bus_dmamap_destroy(sc->ciss_command_dmat, sc->ciss_command_map);
1384 if (sc->ciss_buffer_dmat)
1385 bus_dma_tag_destroy(sc->ciss_command_dmat);
1387 /* disconnect from CAM */
1388 if (sc->ciss_cam_sim) {
1389 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim));
1390 cam_sim_free(sc->ciss_cam_sim);
1392 if (sc->ciss_cam_devq)
1393 cam_simq_release(sc->ciss_cam_devq);
1394 /* XXX what about ciss_cam_path? */
1397 /************************************************************************
1398 * Give a command to the adapter.
1400 * Note that this uses the simple transport layer directly. If we
1401 * want to add support for other layers, we'll need a switch of some
1404 * Note that the simple transport layer has no way of refusing a
1405 * command; we only have as many request structures as the adapter
1406 * supports commands, so we don't have to check (this presumes that
1407 * the adapter can handle commands as fast as we throw them at it).
1410 ciss_start(struct ciss_request *cr)
1412 struct ciss_command *cc; /* XXX debugging only */
1415 cc = CISS_FIND_COMMAND(cr);
1416 debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
1419 * Map the request's data.
1421 if ((error = ciss_map_request(cr)))
1425 ciss_print_request(cr);
1429 * Post the command to the adapter.
1431 ciss_enqueue_busy(cr);
1432 CISS_TL_SIMPLE_POST_CMD(cr->cr_sc, CISS_FIND_COMMANDPHYS(cr));
1437 /************************************************************************
1438 * Fetch completed request(s) from the adapter, queue them for
1439 * completion handling.
1441 * Note that this uses the simple transport layer directly. If we
1442 * want to add support for other layers, we'll need a switch of some
1445 * Note that the simple transport mechanism does not require any
1446 * reentrancy protection; the OPQ read is atomic. If there is a
1447 * chance of a race with something else that might move the request
1448 * off the busy list, then we will have to lock against that
1449 * (eg. timeouts, etc.)
1452 ciss_done(struct ciss_softc *sc)
1454 struct ciss_request *cr;
1455 struct ciss_command *cc;
1456 u_int32_t tag, index;
1462 * Loop quickly taking requests from the adapter and moving them
1463 * from the busy queue to the completed queue.
1468 /* see if the OPQ contains anything */
1469 if (!CISS_TL_SIMPLE_OPQ_INTERRUPT(sc))
1472 tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
1473 if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
1476 debug(2, "completed command %d%s", index,
1477 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
1478 if (index >= sc->ciss_max_requests) {
1479 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
1482 cr = &(sc->ciss_request[index]);
1483 cc = CISS_FIND_COMMAND(cr);
1484 cc->header.host_tag = tag; /* not updated by adapter */
1485 if (ciss_remove_busy(cr)) {
1486 /* assume this is garbage out of the adapter */
1487 ciss_printf(sc, "completed nonbusy request %d\n", index);
1489 ciss_enqueue_complete(cr);
1495 * Invoke completion processing. If we can defer this out of
1496 * interrupt context, that'd be good.
1502 /************************************************************************
1503 * Take an interrupt from the adapter.
1506 ciss_intr(void *arg)
1508 struct ciss_softc *sc = (struct ciss_softc *)arg;
1511 * The only interrupt we recognise indicates that there are
1512 * entries in the outbound post queue.
1517 /************************************************************************
1518 * Process completed requests.
1520 * Requests can be completed in three fashions:
1522 * - by invoking a callback function (cr_complete is non-null)
1523 * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
1524 * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
1527 ciss_complete(struct ciss_softc *sc)
1529 struct ciss_request *cr;
1534 * Loop taking requests off the completed queue and performing
1535 * completion processing on them.
1538 if ((cr = ciss_dequeue_complete(sc)) == NULL)
1540 ciss_unmap_request(cr);
1543 * If the request has a callback, invoke it.
1545 if (cr->cr_complete != NULL) {
1546 cr->cr_complete(cr);
1551 * If someone is sleeping on this request, wake them up.
1553 if (cr->cr_flags & CISS_REQ_SLEEP) {
1554 cr->cr_flags &= ~CISS_REQ_SLEEP;
1560 * If someone is polling this request for completion, signal.
1562 if (cr->cr_flags & CISS_REQ_POLL) {
1563 cr->cr_flags &= ~CISS_REQ_POLL;
1568 * Give up and throw the request back on the free queue. This
1569 * should never happen; resources will probably be lost.
1571 ciss_printf(sc, "WARNING: completed command with no submitter\n");
1572 ciss_enqueue_free(cr);
1576 /************************************************************************
1577 * Report on the completion status of a request, and pass back SCSI
1578 * and command status values.
1581 ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status)
1583 struct ciss_command *cc;
1584 struct ciss_error_info *ce;
1588 cc = CISS_FIND_COMMAND(cr);
1589 ce = (struct ciss_error_info *)&(cc->sg[0]);
1592 * We don't consider data under/overrun an error for the Report
1593 * Logical/Physical LUNs commands.
1595 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
1596 ((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
1597 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS))) {
1598 cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
1599 debug(2, "ignoring irrelevant under/overrun error");
1603 * Check the command's error bit, if clear, there's no status and
1606 if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
1607 if (scsi_status != NULL)
1608 *scsi_status = SCSI_STATUS_OK;
1609 if (command_status != NULL)
1610 *command_status = CISS_CMD_STATUS_SUCCESS;
1613 if (command_status != NULL)
1614 *command_status = ce->command_status;
1615 if (scsi_status != NULL) {
1616 if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
1617 *scsi_status = ce->scsi_status;
1623 ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
1624 ce->command_status, ciss_name_command_status(ce->command_status),
1626 if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
1627 ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x\n",
1628 ce->additional_error_info.invalid_command.offense_size,
1629 ce->additional_error_info.invalid_command.offense_offset,
1630 ce->additional_error_info.invalid_command.offense_value);
1636 /************************************************************************
1637 * Issue a request and don't return until it's completed.
1639 * Depending on adapter status, we may poll or sleep waiting for
1643 ciss_synch_request(struct ciss_request *cr, int timeout)
1645 if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
1646 return(ciss_wait_request(cr, timeout));
1648 return(ciss_poll_request(cr, timeout));
1652 /************************************************************************
1653 * Issue a request and poll for completion.
1655 * Timeout in milliseconds.
1658 ciss_poll_request(struct ciss_request *cr, int timeout)
1664 cr->cr_flags |= CISS_REQ_POLL;
1665 if ((error = ciss_start(cr)) != 0)
1669 ciss_done(cr->cr_sc);
1670 if (!(cr->cr_flags & CISS_REQ_POLL))
1673 } while (timeout-- >= 0);
1674 return(EWOULDBLOCK);
1677 /************************************************************************
1678 * Issue a request and sleep waiting for completion.
1680 * Timeout in milliseconds. Note that a spurious wakeup will reset
1684 ciss_wait_request(struct ciss_request *cr, int timeout)
1690 cr->cr_flags |= CISS_REQ_SLEEP;
1691 if ((error = ciss_start(cr)) != 0)
1695 while (cr->cr_flags & CISS_REQ_SLEEP) {
1696 error = tsleep(cr, PCATCH, "cissREQ", (timeout * hz) / 1000);
1698 * On wakeup or interruption due to restartable activity, go
1699 * back and check to see if we're done.
1701 if ((error == 0) || (error == ERESTART)) {
1706 * Timeout, interrupted system call, etc.
1715 /************************************************************************
1716 * Abort a request. Note that a potential exists here to race the
1717 * request being completed; the caller must deal with this.
1720 ciss_abort_request(struct ciss_request *ar)
1722 struct ciss_request *cr;
1723 struct ciss_command *cc;
1724 struct ciss_message_cdb *cmc;
1730 if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
1733 /* build the abort command */
1734 cc = CISS_FIND_COMMAND(cr);
1735 cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL; /* addressing? */
1736 cc->header.address.physical.target = 0;
1737 cc->header.address.physical.bus = 0;
1738 cc->cdb.cdb_length = sizeof(*cmc);
1739 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
1740 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1741 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
1742 cc->cdb.timeout = 30;
1744 cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
1745 cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
1746 cmc->type = CISS_MESSAGE_ABORT_TASK;
1747 cmc->abort_tag = ar->cr_tag; /* endianness?? */
1750 * Send the request and wait for a response. If we believe we
1751 * aborted the request OK, clear the flag that indicates it's
1754 error = ciss_synch_request(cr, 35 * 1000);
1756 error = ciss_report_request(cr, NULL, NULL);
1757 ciss_release_request(cr);
1764 /************************************************************************
1765 * Fetch and initialise a request
1768 ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
1770 struct ciss_request *cr;
1775 * Get a request and clean it up.
1777 if ((cr = ciss_dequeue_free(sc)) == NULL)
1782 cr->cr_complete = NULL;
1784 ciss_preen_command(cr);
1790 ciss_preen_command(struct ciss_request *cr)
1792 struct ciss_command *cc;
1796 * Clean up the command structure.
1798 * Note that we set up the error_info structure here, since the
1799 * length can be overwritten by any command.
1801 cc = CISS_FIND_COMMAND(cr);
1802 cc->header.sg_in_list = 0; /* kinda inefficient this way */
1803 cc->header.sg_total = 0;
1804 cc->header.host_tag = cr->cr_tag << 2;
1805 cc->header.host_tag_zeroes = 0;
1806 cmdphys = CISS_FIND_COMMANDPHYS(cr);
1807 cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
1808 cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
1812 /************************************************************************
1813 * Release a request to the free list.
1816 ciss_release_request(struct ciss_request *cr)
1818 struct ciss_softc *sc;
1824 /* release the request to the free queue */
1825 ciss_requeue_free(cr);
1828 /************************************************************************
1829 * Allocate a request that will be used to send a BMIC command. Do some
1830 * of the common setup here to avoid duplicating it everywhere else.
1833 ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
1834 int opcode, void **bufp, size_t bufsize)
1836 struct ciss_request *cr;
1837 struct ciss_command *cc;
1838 struct ciss_bmic_cdb *cbc;
1851 if ((error = ciss_get_request(sc, &cr)) != 0)
1855 * Allocate data storage if requested, determine the data direction.
1858 if ((bufsize > 0) && (bufp != NULL)) {
1859 if (*bufp == NULL) {
1860 buf = malloc(bufsize, CISS_MALLOC_CLASS, M_INTWAIT | M_ZERO);
1863 dataout = 1; /* we are given a buffer, so we are writing */
1868 * Build a CISS BMIC command to get the logical drive ID.
1871 cr->cr_length = bufsize;
1873 cr->cr_flags = CISS_REQ_DATAIN;
1875 cc = CISS_FIND_COMMAND(cr);
1876 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
1877 cc->header.address.physical.bus = 0;
1878 cc->header.address.physical.target = 0;
1879 cc->cdb.cdb_length = sizeof(*cbc);
1880 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1881 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1882 cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
1883 cc->cdb.timeout = 0;
1885 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1886 bzero(cbc, sizeof(*cbc));
1887 cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
1888 cbc->bmic_opcode = opcode;
1889 cbc->size = htons((u_int16_t)bufsize);
1894 ciss_release_request(cr);
1895 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
1896 free(buf, CISS_MALLOC_CLASS);
1899 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
1905 /************************************************************************
1906 * Handle a command passed in from userspace.
1909 ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
1911 struct ciss_request *cr;
1912 struct ciss_command *cc;
1913 struct ciss_error_info *ce;
1923 if ((error = ciss_get_request(sc, &cr)) != 0)
1925 cc = CISS_FIND_COMMAND(cr);
1928 * Allocate an in-kernel databuffer if required, copy in user data.
1930 cr->cr_length = ioc->buf_size;
1931 if (ioc->buf_size > 0) {
1932 if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_WAITOK)) == NULL) {
1936 if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
1937 debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
1943 * Build the request based on the user command.
1945 bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
1946 bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
1948 /* XXX anything else to populate here? */
1953 if ((error = ciss_synch_request(cr, 60 * 1000))) {
1954 debug(0, "request failed - %d", error);
1959 * Copy the results back to the user.
1961 ce = (struct ciss_error_info *)&(cc->sg[0]);
1962 bcopy(ce, &ioc->error_info, sizeof(*ce));
1963 if ((ioc->buf_size > 0) &&
1964 (error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
1965 debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
1973 if ((cr != NULL) && (cr->cr_data != NULL))
1974 free(cr->cr_data, CISS_MALLOC_CLASS);
1976 ciss_release_request(cr);
1980 /************************************************************************
1981 * Map a request into bus-visible space, initialise the scatter/gather
1985 ciss_map_request(struct ciss_request *cr)
1987 struct ciss_softc *sc;
1993 /* check that mapping is necessary */
1994 if ((cr->cr_flags & CISS_REQ_MAPPED) || (cr->cr_data == NULL))
1997 bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
1998 bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap, cr->cr_data, cr->cr_length,
1999 ciss_request_map_helper, CISS_FIND_COMMAND(cr), 0);
2001 if (cr->cr_flags & CISS_REQ_DATAIN)
2002 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2003 if (cr->cr_flags & CISS_REQ_DATAOUT)
2004 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2006 cr->cr_flags |= CISS_REQ_MAPPED;
2011 ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2013 struct ciss_command *cc;
2018 cc = (struct ciss_command *)arg;
2019 for (i = 0; i < nseg; i++) {
2020 cc->sg[i].address = segs[i].ds_addr;
2021 cc->sg[i].length = segs[i].ds_len;
2022 cc->sg[i].extension = 0;
2024 /* we leave the s/g table entirely within the command */
2025 cc->header.sg_in_list = nseg;
2026 cc->header.sg_total = nseg;
2029 /************************************************************************
2030 * Unmap a request from bus-visible space.
2033 ciss_unmap_request(struct ciss_request *cr)
2035 struct ciss_softc *sc;
2041 /* check that unmapping is necessary */
2042 if (!(cr->cr_flags & CISS_REQ_MAPPED) || (cr->cr_data == NULL))
2045 if (cr->cr_flags & CISS_REQ_DATAIN)
2046 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
2047 if (cr->cr_flags & CISS_REQ_DATAOUT)
2048 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
2050 bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2051 bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
2052 cr->cr_flags &= ~CISS_REQ_MAPPED;
2055 /************************************************************************
2056 * Attach the driver to CAM.
2058 * We put all the logical drives on a single SCSI bus.
2061 ciss_cam_init(struct ciss_softc *sc)
2067 * Allocate a devq. We can reuse this for the masked physical
2068 * devices if we decide to export these as well.
2070 if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests)) == NULL) {
2071 ciss_printf(sc, "can't allocate CAM SIM queue\n");
2078 if ((sc->ciss_cam_sim = cam_sim_alloc(ciss_cam_action, ciss_cam_poll, "ciss", sc,
2079 device_get_unit(sc->ciss_dev),
2080 sc->ciss_max_requests - 2,
2082 sc->ciss_cam_devq)) == NULL) {
2083 ciss_printf(sc, "can't allocate CAM SIM\n");
2088 * Register bus 0 (the 'logical drives' bus) with this SIM.
2090 if (xpt_bus_register(sc->ciss_cam_sim, 0) != 0) {
2091 ciss_printf(sc, "can't register SCSI bus 0\n");
2096 * Initiate a rescan of the bus.
2098 ciss_cam_rescan_all(sc);
2103 /************************************************************************
2104 * Initiate a rescan of the 'logical devices' SIM
2107 ciss_cam_rescan_target(struct ciss_softc *sc, int target)
2113 if ((ccb = malloc(sizeof(union ccb), M_TEMP, M_WAITOK | M_ZERO)) == NULL) {
2114 ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
2118 if (xpt_create_path(&sc->ciss_cam_path, xpt_periph, cam_sim_path(sc->ciss_cam_sim), target, 0)
2120 ciss_printf(sc, "rescan failed (can't create path)\n");
2124 xpt_setup_ccb(&ccb->ccb_h, sc->ciss_cam_path, 5/*priority (low)*/);
2125 ccb->ccb_h.func_code = XPT_SCAN_BUS;
2126 ccb->ccb_h.cbfcnp = ciss_cam_rescan_callback;
2127 ccb->crcn.flags = CAM_FLAG_NONE;
2130 /* scan is now in progress */
2134 ciss_cam_rescan_all(struct ciss_softc *sc)
2136 return(ciss_cam_rescan_target(sc, 0));
2140 ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2142 xpt_free_path(ccb->ccb_h.path);
2146 /************************************************************************
2147 * Handle requests coming from CAM
2150 ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
2152 struct ciss_softc *sc;
2153 struct ccb_scsiio *csio;
2156 sc = cam_sim_softc(sim);
2157 csio = (struct ccb_scsiio *)&ccb->csio;
2158 target = csio->ccb_h.target_id;
2160 switch (ccb->ccb_h.func_code) {
2162 /* perform SCSI I/O */
2164 if (!ciss_cam_action_io(sim, csio))
2168 /* perform geometry calculations */
2169 case XPT_CALC_GEOMETRY:
2171 struct ccb_calc_geometry *ccg = &ccb->ccg;
2172 struct ciss_ldrive *ld = &sc->ciss_logical[target];
2174 debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2177 * Use the cached geometry settings unless the fault tolerance
2180 if (ld->cl_geometry.fault_tolerance == 0xFF) {
2181 u_int32_t secs_per_cylinder;
2184 ccg->secs_per_track = 32;
2185 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2186 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2188 ccg->heads = ld->cl_geometry.heads;
2189 ccg->secs_per_track = ld->cl_geometry.sectors;
2190 ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2192 ccb->ccb_h.status = CAM_REQ_CMP;
2196 /* handle path attribute inquiry */
2199 struct ccb_pathinq *cpi = &ccb->cpi;
2201 debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2203 cpi->version_num = 1;
2204 cpi->hba_inquiry = PI_TAG_ABLE; /* XXX is this correct? */
2205 cpi->target_sprt = 0;
2207 cpi->max_target = CISS_MAX_LOGICAL;
2208 cpi->max_lun = 0; /* 'logical drive' channel only */
2209 cpi->initiator_id = CISS_MAX_LOGICAL;
2210 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2211 strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
2212 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2213 cpi->unit_number = cam_sim_unit(sim);
2214 cpi->bus_id = cam_sim_bus(sim);
2215 cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */
2216 ccb->ccb_h.status = CAM_REQ_CMP;
2220 case XPT_GET_TRAN_SETTINGS:
2222 struct ccb_trans_settings *cts = &ccb->cts;
2225 bus = cam_sim_bus(sim);
2226 target = cts->ccb_h.target_id;
2228 debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
2231 /* disconnect always OK */
2232 cts->flags |= CCB_TRANS_DISC_ENB;
2233 cts->valid |= CCB_TRANS_DISC_VALID;
2235 cts->ccb_h.status = CAM_REQ_CMP;
2239 default: /* we can't do this */
2240 debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
2241 ccb->ccb_h.status = CAM_REQ_INVALID;
2248 /************************************************************************
2249 * Handle a CAM SCSI I/O request.
2252 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
2254 struct ciss_softc *sc;
2256 struct ciss_request *cr;
2257 struct ciss_command *cc;
2260 sc = cam_sim_softc(sim);
2261 bus = cam_sim_bus(sim);
2262 target = csio->ccb_h.target_id;
2264 debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
2266 /* check for I/O attempt to nonexistent device */
2268 (target > CISS_MAX_LOGICAL) ||
2269 (sc->ciss_logical[target].cl_status == CISS_LD_NONEXISTENT)) {
2270 debug(3, " device does not exist");
2271 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2274 /* firmware does not support commands > 10 bytes */
2275 if (csio->cdb_len > 12/*CISS_CDB_BUFFER_SIZE*/) {
2276 debug(3, " command too large (%d > %d)", csio->cdb_len, CISS_CDB_BUFFER_SIZE);
2277 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2280 /* check that the CDB pointer is not to a physical address */
2281 if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
2282 debug(3, " CDB pointer is to physical address");
2283 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2286 /* if there is data transfer, it must be to/from a virtual address */
2287 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
2288 if (csio->ccb_h.flags & CAM_DATA_PHYS) { /* we can't map it */
2289 debug(3, " data pointer is to physical address");
2290 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2292 if (csio->ccb_h.flags & CAM_SCATTER_VALID) { /* we want to do the s/g setup */
2293 debug(3, " data has premature s/g setup");
2294 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2298 /* abandon aborted ccbs or those that have failed validation */
2299 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
2300 debug(3, "abandoning CCB due to abort/validation failure");
2304 /* handle emulation of some SCSI commands ourself */
2305 if (ciss_cam_emulate(sc, csio))
2309 * Get a request to manage this command. If we can't, return the
2310 * ccb, freeze the queue and flag so that we unfreeze it when a
2311 * request completes.
2313 if ((error = ciss_get_request(sc, &cr)) != 0) {
2314 xpt_freeze_simq(sc->ciss_cam_sim, 1);
2315 csio->ccb_h.status |= CAM_REQUEUE_REQ;
2320 * Build the command.
2322 cc = CISS_FIND_COMMAND(cr);
2323 cr->cr_data = csio->data_ptr;
2324 cr->cr_length = csio->dxfer_len;
2325 cr->cr_complete = ciss_cam_complete;
2326 cr->cr_private = csio;
2328 cc->header.address.logical.mode = CISS_HDR_ADDRESS_MODE_LOGICAL;
2329 cc->header.address.logical.lun = target;
2330 cc->cdb.cdb_length = csio->cdb_len;
2331 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2332 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; /* XXX ordered tags? */
2333 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2334 cr->cr_flags = CISS_REQ_DATAOUT;
2335 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
2336 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2337 cr->cr_flags = CISS_REQ_DATAIN;
2338 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
2341 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2343 cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
2344 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
2345 bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
2347 bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
2351 * Submit the request to the adapter.
2353 * Note that this may fail if we're unable to map the request (and
2354 * if we ever learn a transport layer other than simple, may fail
2355 * if the adapter rejects the command).
2357 if ((error = ciss_start(cr)) != 0) {
2358 xpt_freeze_simq(sc->ciss_cam_sim, 1);
2359 csio->ccb_h.status |= CAM_REQUEUE_REQ;
2360 ciss_release_request(cr);
2367 /************************************************************************
2368 * Emulate SCSI commands the adapter doesn't handle as we might like.
2371 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
2377 target = csio->ccb_h.target_id;
2378 opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
2379 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
2382 * Handle requests for volumes that don't exist. A selection timeout
2383 * is slightly better than an illegal request. Other errors might be
2386 if (sc->ciss_logical[target].cl_status == CISS_LD_NONEXISTENT) {
2387 csio->ccb_h.status = CAM_SEL_TIMEOUT;
2388 xpt_done((union ccb *)csio);
2393 * Handle requests for volumes that exist but are offline.
2395 * I/O operations should fail, everything else should work.
2397 if (sc->ciss_logical[target].cl_status == CISS_LD_OFFLINE) {
2405 csio->ccb_h.status = CAM_SEL_TIMEOUT;
2406 xpt_done((union ccb *)csio);
2412 /* if we have to fake Synchronise Cache */
2413 if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
2416 * If this is a Synchronise Cache command, typically issued when
2417 * a device is closed, flush the adapter and complete now.
2419 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
2420 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
2421 ciss_flush_adapter(sc);
2422 csio->ccb_h.status = CAM_REQ_CMP;
2423 xpt_done((union ccb *)csio);
2431 /************************************************************************
2432 * Check for possibly-completed commands.
2435 ciss_cam_poll(struct cam_sim *sim)
2437 struct ciss_softc *sc = cam_sim_softc(sim);
2444 /************************************************************************
2445 * Handle completion of a command - pass results back through the CCB
2448 ciss_cam_complete(struct ciss_request *cr)
2450 struct ciss_softc *sc;
2451 struct ciss_command *cc;
2452 struct ciss_error_info *ce;
2453 struct ccb_scsiio *csio;
2460 cc = CISS_FIND_COMMAND(cr);
2461 ce = (struct ciss_error_info *)&(cc->sg[0]);
2462 csio = (struct ccb_scsiio *)cr->cr_private;
2465 * Extract status values from request.
2467 ciss_report_request(cr, &command_status, &scsi_status);
2468 csio->scsi_status = scsi_status;
2471 * Handle specific SCSI status values.
2473 switch(scsi_status) {
2474 /* no status due to adapter error */
2476 debug(0, "adapter error");
2477 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2480 /* no status due to command completed OK */
2481 case SCSI_STATUS_OK: /* CISS_SCSI_STATUS_GOOD */
2482 debug(2, "SCSI_STATUS_OK");
2483 csio->ccb_h.status = CAM_REQ_CMP;
2486 /* check condition, sense data included */
2487 case SCSI_STATUS_CHECK_COND: /* CISS_SCSI_STATUS_CHECK_CONDITION */
2488 debug(0, "SCSI_STATUS_CHECK_COND sense size %d resid %d",
2489 ce->sense_length, ce->residual_count);
2490 bzero(&csio->sense_data, SSD_FULL_SIZE);
2491 bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
2492 csio->sense_len = ce->sense_length;
2493 csio->resid = ce->residual_count;
2494 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
2497 struct scsi_sense_data *sns = (struct scsi_sense_data *)&ce->sense_info[0];
2498 debug(0, "sense key %x", sns->flags & SSD_KEY);
2503 case SCSI_STATUS_BUSY: /* CISS_SCSI_STATUS_BUSY */
2504 debug(0, "SCSI_STATUS_BUSY");
2505 csio->ccb_h.status = CAM_SCSI_BUSY;
2509 debug(0, "unknown status 0x%x", csio->scsi_status);
2510 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2514 /* handle post-command fixup */
2515 ciss_cam_complete_fixup(sc, csio);
2517 /* tell CAM we're ready for more commands */
2518 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
2520 xpt_done((union ccb *)csio);
2521 ciss_release_request(cr);
2524 /********************************************************************************
2525 * Fix up the result of some commands here.
2528 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
2530 struct scsi_inquiry_data *inq;
2531 struct ciss_ldrive *cl;
2534 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
2535 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == INQUIRY) {
2537 inq = (struct scsi_inquiry_data *)csio->data_ptr;
2538 target = csio->ccb_h.target_id;
2539 cl = &sc->ciss_logical[target];
2541 padstr(inq->vendor, "COMPAQ", 8);
2542 padstr(inq->product, ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance), 8);
2543 padstr(inq->revision, ciss_name_ldrive_status(cl->cl_lstatus->status), 16);
2548 /********************************************************************************
2549 * Find a peripheral attached at (target)
2551 static struct cam_periph *
2552 ciss_find_periph(struct ciss_softc *sc, int target)
2554 struct cam_periph *periph;
2555 struct cam_path *path;
2558 status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim), target, 0);
2559 if (status == CAM_REQ_CMP) {
2560 periph = cam_periph_find(path, NULL);
2561 xpt_free_path(path);
2568 /********************************************************************************
2569 * Name the device at (target)
2571 * XXX is this strictly correct?
2574 ciss_name_device(struct ciss_softc *sc, int target)
2576 struct cam_periph *periph;
2578 if ((periph = ciss_find_periph(sc, target)) != NULL) {
2579 sprintf(sc->ciss_logical[target].cl_name, "%s%d", periph->periph_name, periph->unit_number);
2582 sc->ciss_logical[target].cl_name[0] = 0;
2586 /************************************************************************
2587 * Periodic status monitoring.
2590 ciss_periodic(void *arg)
2592 struct ciss_softc *sc;
2596 sc = (struct ciss_softc *)arg;
2599 * Check the adapter heartbeat.
2601 if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
2602 sc->ciss_heart_attack++;
2603 debug(0, "adapter heart attack in progress 0x%x/%d",
2604 sc->ciss_heartbeat, sc->ciss_heart_attack);
2605 if (sc->ciss_heart_attack == 3) {
2606 ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
2607 /* XXX should reset adapter here */
2610 sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
2611 sc->ciss_heart_attack = 0;
2612 debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
2616 * If the notify event request has died for some reason, or has
2617 * not started yet, restart it.
2619 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
2620 debug(0, "(re)starting Event Notify chain");
2621 ciss_notify_event(sc);
2627 if (!(sc->ciss_flags & CISS_FLAG_ABORTING))
2628 callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz,
2632 /************************************************************************
2633 * Request a notification response from the adapter.
2635 * If (cr) is NULL, this is the first request of the adapter, so
2636 * reset the adapter's message pointer and start with the oldest
2637 * message available.
2640 ciss_notify_event(struct ciss_softc *sc)
2642 struct ciss_request *cr;
2643 struct ciss_command *cc;
2644 struct ciss_notify_cdb *cnc;
2649 cr = sc->ciss_periodic_notify;
2651 /* get a request if we don't already have one */
2653 if ((error = ciss_get_request(sc, &cr)) != 0) {
2654 debug(0, "can't get notify event request");
2657 sc->ciss_periodic_notify = cr;
2658 cr->cr_complete = ciss_notify_complete;
2659 debug(1, "acquired request %d", cr->cr_tag);
2663 * Get a databuffer if we don't already have one, note that the
2664 * adapter command wants a larger buffer than the actual
2667 if (cr->cr_data == NULL) {
2668 cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_INTWAIT);
2669 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
2672 /* re-setup the request's command (since we never release it) XXX overkill*/
2673 ciss_preen_command(cr);
2675 /* (re)build the notify event command */
2676 cc = CISS_FIND_COMMAND(cr);
2677 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2678 cc->header.address.physical.bus = 0;
2679 cc->header.address.physical.target = 0;
2681 cc->cdb.cdb_length = sizeof(*cnc);
2682 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2683 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2684 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
2685 cc->cdb.timeout = 0; /* no timeout, we hope */
2687 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
2688 bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
2689 cnc->opcode = CISS_OPCODE_READ;
2690 cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
2691 cnc->timeout = 0; /* no timeout, we hope */
2692 cnc->synchronous = 0;
2694 cnc->seek_to_oldest = 0;
2696 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
2698 /* submit the request */
2699 error = ciss_start(cr);
2704 if (cr->cr_data != NULL)
2705 free(cr->cr_data, CISS_MALLOC_CLASS);
2706 ciss_release_request(cr);
2708 sc->ciss_periodic_notify = NULL;
2709 debug(0, "can't submit notify event request");
2710 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2712 debug(1, "notify event submitted");
2713 sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
2718 ciss_notify_complete(struct ciss_request *cr)
2720 struct ciss_command *cc;
2721 struct ciss_notify *cn;
2722 struct ciss_softc *sc;
2728 cc = CISS_FIND_COMMAND(cr);
2729 cn = (struct ciss_notify *)cr->cr_data;
2733 * Report request results, decode status.
2735 ciss_report_request(cr, &command_status, &scsi_status);
2738 * Abort the chain on a fatal error.
2740 * XXX which of these are actually errors?
2742 if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
2743 (command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
2744 (command_status != CISS_CMD_STATUS_TIMEOUT)) { /* XXX timeout? */
2745 ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
2746 ciss_name_command_status(command_status));
2747 ciss_release_request(cr);
2748 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2753 * If the adapter gave us a text message, print it.
2755 if (cn->message[0] != 0)
2756 ciss_printf(sc, "*** %.80s\n", cn->message);
2758 debug(0, "notify event class %d subclass %d detail %d",
2759 cn->class, cn->subclass, cn->detail);
2762 * If there's room, save the event for a user-level tool.
2764 if (((sc->ciss_notify_head + 1) % CISS_MAX_EVENTS) != sc->ciss_notify_tail) {
2765 sc->ciss_notify[sc->ciss_notify_head] = *cn;
2766 sc->ciss_notify_head = (sc->ciss_notify_head + 1) % CISS_MAX_EVENTS;
2770 * Some events are directly of interest to us.
2772 switch (cn->class) {
2773 case CISS_NOTIFY_LOGICAL:
2774 ciss_notify_logical(sc, cn);
2776 case CISS_NOTIFY_PHYSICAL:
2777 ciss_notify_physical(sc, cn);
2782 * If the response indicates that the notifier has been aborted,
2783 * release the notifier command.
2785 if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
2786 (cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
2787 (cn->detail == 1)) {
2788 debug(0, "notifier exiting");
2789 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2790 ciss_release_request(cr);
2791 sc->ciss_periodic_notify = NULL;
2792 wakeup(&sc->ciss_periodic_notify);
2796 * Send a new notify event command, if we're not aborting.
2798 if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
2799 ciss_notify_event(sc);
2803 /************************************************************************
2804 * Abort the Notify Event chain.
2806 * Note that we can't just abort the command in progress; we have to
2807 * explicitly issue an Abort Notify Event command in order for the
2808 * adapter to clean up correctly.
2810 * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
2811 * the chain will not restart itself.
2814 ciss_notify_abort(struct ciss_softc *sc)
2816 struct ciss_request *cr;
2817 struct ciss_command *cc;
2818 struct ciss_notify_cdb *cnc;
2819 int error, s, command_status, scsi_status;
2826 /* verify that there's an outstanding command */
2827 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
2830 /* get a command to issue the abort with */
2831 if ((error = ciss_get_request(sc, &cr)))
2834 /* get a buffer for the result */
2835 cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_INTWAIT);
2836 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
2839 cc = CISS_FIND_COMMAND(cr);
2840 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2841 cc->header.address.physical.bus = 0;
2842 cc->header.address.physical.target = 0;
2843 cc->cdb.cdb_length = sizeof(*cnc);
2844 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2845 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2846 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
2847 cc->cdb.timeout = 0; /* no timeout, we hope */
2849 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
2850 bzero(cnc, sizeof(*cnc));
2851 cnc->opcode = CISS_OPCODE_WRITE;
2852 cnc->command = CISS_COMMAND_ABORT_NOTIFY;
2853 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
2855 ciss_print_request(cr);
2858 * Submit the request and wait for it to complete.
2860 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
2861 ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
2868 ciss_report_request(cr, &command_status, &scsi_status);
2869 switch(command_status) {
2870 case CISS_CMD_STATUS_SUCCESS:
2872 case CISS_CMD_STATUS_INVALID_COMMAND:
2874 * Some older adapters don't support the CISS version of this
2875 * command. Fall back to using the BMIC version.
2877 error = ciss_notify_abort_bmic(sc);
2882 case CISS_CMD_STATUS_TARGET_STATUS:
2884 * This can happen if the adapter thinks there wasn't an outstanding
2885 * Notify Event command but we did. We clean up here.
2887 if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
2888 if (sc->ciss_periodic_notify != NULL)
2889 ciss_release_request(sc->ciss_periodic_notify);
2896 ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
2897 ciss_name_command_status(command_status));
2903 * Sleep waiting for the notifier command to complete. Note
2904 * that if it doesn't, we may end up in a bad situation, since
2905 * the adapter may deliver it later. Also note that the adapter
2906 * requires the Notify Event command to be cancelled in order to
2907 * maintain internal bookkeeping.
2910 while (sc->ciss_periodic_notify != NULL) {
2911 error = tsleep(&sc->ciss_periodic_notify, 0, "cissNEA", hz * 5);
2912 if (error == EWOULDBLOCK) {
2913 ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
2920 /* release the cancel request */
2922 if (cr->cr_data != NULL)
2923 free(cr->cr_data, CISS_MALLOC_CLASS);
2924 ciss_release_request(cr);
2927 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2931 /************************************************************************
2932 * Abort the Notify Event chain using a BMIC command.
2935 ciss_notify_abort_bmic(struct ciss_softc *sc)
2937 struct ciss_request *cr;
2938 int error, command_status;
2945 /* verify that there's an outstanding command */
2946 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
2950 * Build a BMIC command to cancel the Notify on Event command.
2952 * Note that we are sending a CISS opcode here. Odd.
2954 if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
2959 * Submit the request and wait for it to complete.
2961 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
2962 ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
2969 ciss_report_request(cr, &command_status, NULL);
2970 switch(command_status) {
2971 case CISS_CMD_STATUS_SUCCESS:
2974 ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
2975 ciss_name_command_status(command_status));
2982 ciss_release_request(cr);
2986 /************************************************************************
2987 * Handle a notify event relating to the status of a logical drive.
2989 * XXX need to be able to defer some of these to properly handle
2990 * calling the "ID Physical drive" command, unless the 'extended'
2991 * drive IDs are always in BIG_MAP format.
2994 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
2996 struct ciss_ldrive *ld;
3001 ld = &sc->ciss_logical[cn->data.logical_status.logical_drive];
3003 switch (cn->subclass) {
3004 case CISS_NOTIFY_LOGICAL_STATUS:
3005 switch (cn->detail) {
3007 ciss_name_device(sc, cn->data.logical_status.logical_drive);
3008 ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
3009 cn->data.logical_status.logical_drive, ld->cl_name,
3010 ciss_name_ldrive_status(cn->data.logical_status.previous_state),
3011 ciss_name_ldrive_status(cn->data.logical_status.new_state),
3012 cn->data.logical_status.spare_state,
3013 "\20\1configured\2rebuilding\3failed\4in use\5available\n");
3016 * Update our idea of the drive's status.
3018 ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
3019 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3020 if (ld->cl_lstatus != NULL)
3021 ld->cl_lstatus->status = cn->data.logical_status.new_state;
3025 * Have CAM rescan the drive if its status has changed.
3027 if (ostatus != ld->cl_status)
3028 ciss_cam_rescan_target(sc, cn->data.logical_status.logical_drive);
3033 case 1: /* logical drive has recognised new media, needs Accept Media Exchange */
3034 ciss_name_device(sc, cn->data.logical_status.logical_drive);
3035 ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
3036 cn->data.logical_status.logical_drive, ld->cl_name);
3037 ciss_accept_media(sc, cn->data.logical_status.logical_drive, 1);
3042 ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
3043 cn->data.rebuild_aborted.logical_drive,
3044 sc->ciss_logical[cn->data.rebuild_aborted.logical_drive].cl_name,
3045 (cn->detail == 2) ? "read" : "write");
3050 case CISS_NOTIFY_LOGICAL_ERROR:
3051 if (cn->detail == 0) {
3052 ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
3053 cn->data.io_error.logical_drive,
3054 sc->ciss_logical[cn->data.io_error.logical_drive].cl_name,
3055 cn->data.io_error.failure_bus,
3056 cn->data.io_error.failure_drive);
3057 /* XXX should we take the drive down at this point, or will we be told? */
3061 case CISS_NOTIFY_LOGICAL_SURFACE:
3062 if (cn->detail == 0)
3063 ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
3064 cn->data.consistency_completed.logical_drive,
3065 sc->ciss_logical[cn->data.consistency_completed.logical_drive].cl_name);
3070 /************************************************************************
3071 * Handle a notify event relating to the status of a physical drive.
3074 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
3079 /************************************************************************
3083 ciss_print_request(struct ciss_request *cr)
3085 struct ciss_softc *sc;
3086 struct ciss_command *cc;
3090 cc = CISS_FIND_COMMAND(cr);
3092 ciss_printf(sc, "REQUEST @ %p\n", cr);
3093 ciss_printf(sc, " data %p/%d tag %d flags %b\n",
3094 cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
3095 "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
3096 ciss_printf(sc, " sg list/total %d/%d host tag 0x%x\n",
3097 cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
3098 switch(cc->header.address.mode.mode) {
3099 case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
3100 case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
3101 ciss_printf(sc, " physical bus %d target %d\n",
3102 cc->header.address.physical.bus, cc->header.address.physical.target);
3104 case CISS_HDR_ADDRESS_MODE_LOGICAL:
3105 ciss_printf(sc, " logical unit %d\n", cc->header.address.logical.lun);
3108 ciss_printf(sc, " %s cdb length %d type %s attribute %s\n",
3109 (cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
3110 (cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
3111 (cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
3113 (cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
3114 (cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
3115 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
3116 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
3117 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
3118 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
3119 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
3120 ciss_printf(sc, " %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
3122 if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
3123 /* XXX print error info */
3125 /* since we don't use chained s/g, don't support it here */
3126 for (i = 0; i < cc->header.sg_in_list; i++) {
3128 ciss_printf(sc, " ");
3129 printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
3130 if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
3136 /************************************************************************
3137 * Print information about the status of a logical drive.
3140 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
3144 if (ld->cl_lstatus == NULL) {
3145 printf("does not exist\n");
3149 /* print drive status */
3150 switch(ld->cl_lstatus->status) {
3151 case CISS_LSTATUS_OK:
3154 case CISS_LSTATUS_INTERIM_RECOVERY:
3155 printf("in interim recovery mode\n");
3157 case CISS_LSTATUS_READY_RECOVERY:
3158 printf("ready to begin recovery\n");
3160 case CISS_LSTATUS_RECOVERING:
3161 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
3162 target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
3163 printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
3164 bus, target, ld->cl_lstatus->blocks_to_recover);
3166 case CISS_LSTATUS_EXPANDING:
3167 printf("being expanded, %u blocks remaining\n",
3168 ld->cl_lstatus->blocks_to_recover);
3170 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
3171 printf("queued for expansion\n");
3173 case CISS_LSTATUS_FAILED:
3174 printf("queued for expansion\n");
3176 case CISS_LSTATUS_WRONG_PDRIVE:
3177 printf("wrong physical drive inserted\n");
3179 case CISS_LSTATUS_MISSING_PDRIVE:
3180 printf("missing a needed physical drive\n");
3182 case CISS_LSTATUS_BECOMING_READY:
3183 printf("becoming ready\n");
3187 /* print failed physical drives */
3188 for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
3189 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
3190 target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
3193 ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
3194 ld->cl_lstatus->drive_failure_map[i]);
3199 /************************************************************************
3200 * Print information about the controller/driver.
3203 ciss_print_adapter(struct ciss_softc *sc)
3207 ciss_printf(sc, "ADAPTER:\n");
3208 for (i = 0; i < CISSQ_COUNT; i++) {
3209 ciss_printf(sc, "%s %d/%d\n",
3211 i == 1 ? "busy" : "complete",
3212 sc->ciss_qstat[i].q_length,
3213 sc->ciss_qstat[i].q_max);
3215 ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
3216 ciss_printf(sc, "notify_head/tail %d/%d\n",
3217 sc->ciss_notify_head, sc->ciss_notify_tail);
3218 ciss_printf(sc, "flags %b\n", sc->ciss_flags,
3219 "\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
3221 for (i = 0; i < CISS_MAX_LOGICAL; i++) {
3222 ciss_printf(sc, "LOGICAL DRIVE %d: ", i);
3223 ciss_print_ldrive(sc, sc->ciss_logical + i);
3226 for (i = 1; i < sc->ciss_max_requests; i++)
3227 ciss_print_request(sc->ciss_request + i);
3231 void ciss_print0(void);
3237 struct ciss_softc *sc;
3239 sc = devclass_get_softc(devclass_find("ciss"), 0);
3241 printf("no ciss controllers\n");
3243 ciss_print_adapter(sc);
3248 /************************************************************************
3249 * Return a name for a logical drive status value.
3252 ciss_name_ldrive_status(int status)
3255 case CISS_LSTATUS_OK:
3257 case CISS_LSTATUS_FAILED:
3259 case CISS_LSTATUS_NOT_CONFIGURED:
3260 return("not configured");
3261 case CISS_LSTATUS_INTERIM_RECOVERY:
3262 return("interim recovery");
3263 case CISS_LSTATUS_READY_RECOVERY:
3264 return("ready for recovery");
3265 case CISS_LSTATUS_RECOVERING:
3266 return("recovering");
3267 case CISS_LSTATUS_WRONG_PDRIVE:
3268 return("wrong physical drive inserted");
3269 case CISS_LSTATUS_MISSING_PDRIVE:
3270 return("missing physical drive");
3271 case CISS_LSTATUS_EXPANDING:
3272 return("expanding");
3273 case CISS_LSTATUS_BECOMING_READY:
3274 return("becoming ready");
3275 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
3276 return("queued for expansion");
3278 return("unknown status");
3281 /************************************************************************
3282 * Return an online/offline/nonexistent value for a logical drive
3286 ciss_decode_ldrive_status(int status)
3289 case CISS_LSTATUS_NOT_CONFIGURED:
3290 return(CISS_LD_NONEXISTENT);
3292 case CISS_LSTATUS_OK:
3293 case CISS_LSTATUS_INTERIM_RECOVERY:
3294 case CISS_LSTATUS_READY_RECOVERY:
3295 case CISS_LSTATUS_RECOVERING:
3296 case CISS_LSTATUS_EXPANDING:
3297 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
3298 return(CISS_LD_ONLINE);
3300 case CISS_LSTATUS_FAILED:
3301 case CISS_LSTATUS_WRONG_PDRIVE:
3302 case CISS_LSTATUS_MISSING_PDRIVE:
3303 case CISS_LSTATUS_BECOMING_READY:
3305 return(CISS_LD_OFFLINE);
3310 /************************************************************************
3311 * Return a name for a logical drive's organisation.
3314 ciss_name_ldrive_org(int org)
3317 case CISS_LDRIVE_RAID0:
3319 case CISS_LDRIVE_RAID1:
3321 case CISS_LDRIVE_RAID4:
3323 case CISS_LDRIVE_RAID5:
3329 /************************************************************************
3330 * Return a name for a command status value.
3333 ciss_name_command_status(int status)
3336 case CISS_CMD_STATUS_SUCCESS:
3338 case CISS_CMD_STATUS_TARGET_STATUS:
3339 return("target status");
3340 case CISS_CMD_STATUS_DATA_UNDERRUN:
3341 return("data underrun");
3342 case CISS_CMD_STATUS_DATA_OVERRUN:
3343 return("data overrun");
3344 case CISS_CMD_STATUS_INVALID_COMMAND:
3345 return("invalid command");
3346 case CISS_CMD_STATUS_PROTOCOL_ERROR:
3347 return("protocol error");
3348 case CISS_CMD_STATUS_HARDWARE_ERROR:
3349 return("hardware error");
3350 case CISS_CMD_STATUS_CONNECTION_LOST:
3351 return("connection lost");
3352 case CISS_CMD_STATUS_ABORTED:
3354 case CISS_CMD_STATUS_ABORT_FAILED:
3355 return("abort failed");
3356 case CISS_CMD_STATUS_UNSOLICITED_ABORT:
3357 return("unsolicited abort");
3358 case CISS_CMD_STATUS_TIMEOUT:
3360 case CISS_CMD_STATUS_UNABORTABLE:
3361 return("unabortable");
3363 return("unknown status");
3366 /************************************************************************
3367 * Handle an open on the control device.
3370 ciss_open(dev_t dev, int flags, int fmt, d_thread_t *p)
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(dev_t dev, int flags, int fmt, d_thread_t *p)
3390 struct ciss_softc *sc;
3394 sc = (struct ciss_softc *)dev->si_drv1;
3396 sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
3400 /********************************************************************************
3401 * Handle adapter-specific control operations.
3403 * Note that the API here is compatible with the Linux driver, in order to
3404 * simplify the porting of Compaq's userland tools.
3407 ciss_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *p)
3409 struct ciss_softc *sc;
3414 sc = (struct ciss_softc *)dev->si_drv1;
3418 case CCISS_GETPCIINFO:
3420 cciss_pci_info_struct *pis = (cciss_pci_info_struct *)addr;
3422 pis->bus = pci_get_bus(sc->ciss_dev);
3423 pis->dev_fn = pci_get_slot(sc->ciss_dev);
3424 pis->board_id = pci_get_devid(sc->ciss_dev);
3429 case CCISS_GETINTINFO:
3431 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
3433 cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
3434 cis->count = sc->ciss_cfg->interrupt_coalesce_count;
3439 case CCISS_SETINTINFO:
3441 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
3443 if ((cis->delay == 0) && (cis->count == 0)) {
3449 * XXX apparently this is only safe if the controller is idle,
3450 * we should suspend it before doing this.
3452 sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
3453 sc->ciss_cfg->interrupt_coalesce_count = cis->count;
3455 if (ciss_update_config(sc))
3458 /* XXX resume the controller here */
3462 case CCISS_GETNODENAME:
3463 bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
3464 sizeof(NodeName_type));
3467 case CCISS_SETNODENAME:
3468 bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
3469 sizeof(NodeName_type));
3470 if (ciss_update_config(sc))
3474 case CCISS_GETHEARTBEAT:
3475 *(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
3478 case CCISS_GETBUSTYPES:
3479 *(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
3482 case CCISS_GETFIRMVER:
3483 bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
3484 sizeof(FirmwareVer_type));
3487 case CCISS_GETDRIVERVER:
3488 *(DriverVer_type *)addr = CISS_DRIVER_VERSION;
3491 case CCISS_REVALIDVOLS:
3493 * This is a bit ugly; to do it "right" we really need
3494 * to find any disks that have changed, kick CAM off them,
3495 * then rescan only these disks. It'd be nice if they
3496 * a) told us which disk(s) they were going to play with,
3497 * and b) which ones had arrived. 8(
3501 case CCISS_PASSTHRU:
3502 error = ciss_user_command(sc, (IOCTL_Command_struct *)addr);
3506 debug(0, "unknown ioctl 0x%lx", cmd);
3508 debug(1, "CCISS_GETPCIINFO: 0x%lx", CCISS_GETPCIINFO);
3509 debug(1, "CCISS_GETINTINFO: 0x%lx", CCISS_GETINTINFO);
3510 debug(1, "CCISS_SETINTINFO: 0x%lx", CCISS_SETINTINFO);
3511 debug(1, "CCISS_GETNODENAME: 0x%lx", CCISS_GETNODENAME);
3512 debug(1, "CCISS_SETNODENAME: 0x%lx", CCISS_SETNODENAME);
3513 debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
3514 debug(1, "CCISS_GETBUSTYPES: 0x%lx", CCISS_GETBUSTYPES);
3515 debug(1, "CCISS_GETFIRMVER: 0x%lx", CCISS_GETFIRMVER);
3516 debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
3517 debug(1, "CCISS_REVALIDVOLS: 0x%lx", CCISS_REVALIDVOLS);
3518 debug(1, "CCISS_PASSTHRU: 0x%lx", CCISS_PASSTHRU);