2 * Copyright (c) 2001 Michael Smith
3 * Copyright (c) 2004 Paul Saab
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
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
71 * More thanks must go to John Cagle at HP for the countless hours
72 * spent making this driver "work" with the MSA* series storage
73 * enclosures. Without his help (and nagging), this driver could not
74 * be used with these enclosures.
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/malloc.h>
80 #include <sys/kernel.h>
84 #include <sys/kthread.h>
85 #include <sys/queue.h>
86 #include <sys/sysctl.h>
89 #include <cam/cam_ccb.h>
90 #include <cam/cam_periph.h>
91 #include <cam/cam_sim.h>
92 #include <cam/cam_xpt_sim.h>
93 #include <cam/scsi/scsi_all.h>
94 #include <cam/scsi/scsi_message.h>
96 #include <machine/bus.h>
97 #include <machine/endian.h>
98 #include <machine/resource.h>
101 #include <dev/pci/pcireg.h>
102 #include <dev/pci/pcivar.h>
104 #include <dev/ciss/cissreg.h>
105 #include <dev/ciss/cissvar.h>
106 #include <dev/ciss/cissio.h>
108 MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data", "ciss internal data buffers");
111 static int ciss_lookup(device_t dev);
112 static int ciss_probe(device_t dev);
113 static int ciss_attach(device_t dev);
114 static int ciss_detach(device_t dev);
115 static int ciss_shutdown(device_t dev);
117 /* (de)initialisation functions, control wrappers */
118 static int ciss_init_pci(struct ciss_softc *sc);
119 static int ciss_wait_adapter(struct ciss_softc *sc);
120 static int ciss_flush_adapter(struct ciss_softc *sc);
121 static int ciss_init_requests(struct ciss_softc *sc);
122 static void ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
123 int nseg, int error);
124 static int ciss_identify_adapter(struct ciss_softc *sc);
125 static int ciss_init_logical(struct ciss_softc *sc);
126 static int ciss_init_physical(struct ciss_softc *sc);
127 static int ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll);
128 static int ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
129 static int ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld);
130 static int ciss_update_config(struct ciss_softc *sc);
131 static int ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld);
132 static void ciss_init_sysctl(struct ciss_softc *sc);
133 static void ciss_soft_reset(struct ciss_softc *sc);
134 static void ciss_free(struct ciss_softc *sc);
135 static void ciss_spawn_notify_thread(struct ciss_softc *sc);
136 static void ciss_kill_notify_thread(struct ciss_softc *sc);
138 /* request submission/completion */
139 static int ciss_start(struct ciss_request *cr);
140 static void ciss_done(struct ciss_softc *sc);
141 static void ciss_intr(void *arg);
142 static void ciss_complete(struct ciss_softc *sc);
143 static int ciss_report_request(struct ciss_request *cr, int *command_status,
145 static int ciss_synch_request(struct ciss_request *cr, int timeout);
146 static int ciss_poll_request(struct ciss_request *cr, int timeout);
147 static int ciss_wait_request(struct ciss_request *cr, int timeout);
149 static int ciss_abort_request(struct ciss_request *cr);
152 /* request queueing */
153 static int ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
154 static void ciss_preen_command(struct ciss_request *cr);
155 static void ciss_release_request(struct ciss_request *cr);
157 /* request helpers */
158 static int ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
159 int opcode, void **bufp, size_t bufsize);
160 static int ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
163 static int ciss_map_request(struct ciss_request *cr);
164 static void ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
165 int nseg, int error);
166 static void ciss_unmap_request(struct ciss_request *cr);
169 static int ciss_cam_init(struct ciss_softc *sc);
170 static void ciss_cam_rescan_target(struct ciss_softc *sc,
171 int bus, int target);
172 static void ciss_cam_rescan_all(struct ciss_softc *sc);
173 static void ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb);
174 static void ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
175 static int ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
176 static int ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
177 static void ciss_cam_poll(struct cam_sim *sim);
178 static void ciss_cam_complete(struct ciss_request *cr);
179 static void ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
180 static struct cam_periph *ciss_find_periph(struct ciss_softc *sc,
181 int bus, int target);
182 static int ciss_name_device(struct ciss_softc *sc, int bus, int target);
184 /* periodic status monitoring */
185 static void ciss_periodic(void *arg);
186 static void ciss_nop_complete(struct ciss_request *cr);
187 static void ciss_disable_adapter(struct ciss_softc *sc);
188 static void ciss_notify_event(struct ciss_softc *sc);
189 static void ciss_notify_complete(struct ciss_request *cr);
190 static int ciss_notify_abort(struct ciss_softc *sc);
191 static int ciss_notify_abort_bmic(struct ciss_softc *sc);
192 static void ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
193 static void ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
194 static void ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
196 /* debugging output */
197 static void ciss_print_request(struct ciss_request *cr);
198 static void ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
199 static const char *ciss_name_ldrive_status(int status);
200 static int ciss_decode_ldrive_status(int status);
201 static const char *ciss_name_ldrive_org(int org);
202 static const char *ciss_name_command_status(int status);
207 static device_method_t ciss_methods[] = {
208 /* Device interface */
209 DEVMETHOD(device_probe, ciss_probe),
210 DEVMETHOD(device_attach, ciss_attach),
211 DEVMETHOD(device_detach, ciss_detach),
212 DEVMETHOD(device_shutdown, ciss_shutdown),
216 static driver_t ciss_pci_driver = {
219 sizeof(struct ciss_softc)
222 static devclass_t ciss_devclass;
223 DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
224 MODULE_DEPEND(ciss, cam, 1, 1, 1);
225 MODULE_DEPEND(ciss, pci, 1, 1, 1);
228 * Control device interface.
230 static d_open_t ciss_open;
231 static d_close_t ciss_close;
232 static d_ioctl_t ciss_ioctl;
234 static struct cdevsw ciss_cdevsw = {
235 .d_version = D_VERSION,
238 .d_close = ciss_close,
239 .d_ioctl = ciss_ioctl,
244 * This tunable can be set at boot time and controls whether physical devices
245 * that are marked hidden by the firmware should be exposed anyways.
247 static unsigned int ciss_expose_hidden_physical = 0;
248 TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
250 static unsigned int ciss_nop_message_heartbeat = 0;
251 TUNABLE_INT("hw.ciss.nop_message_heartbeat", &ciss_nop_message_heartbeat);
253 /************************************************************************
254 * CISS adapters amazingly don't have a defined programming interface
255 * value. (One could say some very despairing things about PCI and
256 * people just not getting the general idea.) So we are forced to
257 * stick with matching against subvendor/subdevice, and thus have to
258 * be updated for every new CISS adapter that appears.
260 #define CISS_BOARD_SA5 (1<<0)
261 #define CISS_BOARD_SA5B (1<<1)
269 } ciss_vendor_data[] = {
270 { 0x0e11, 0x4070, CISS_BOARD_SA5, "Compaq Smart Array 5300" },
271 { 0x0e11, 0x4080, CISS_BOARD_SA5B, "Compaq Smart Array 5i" },
272 { 0x0e11, 0x4082, CISS_BOARD_SA5B, "Compaq Smart Array 532" },
273 { 0x0e11, 0x4083, CISS_BOARD_SA5B, "HP Smart Array 5312" },
274 { 0x0e11, 0x4091, CISS_BOARD_SA5, "HP Smart Array 6i" },
275 { 0x0e11, 0x409A, CISS_BOARD_SA5, "HP Smart Array 641" },
276 { 0x0e11, 0x409B, CISS_BOARD_SA5, "HP Smart Array 642" },
277 { 0x0e11, 0x409C, CISS_BOARD_SA5, "HP Smart Array 6400" },
278 { 0x0e11, 0x409D, CISS_BOARD_SA5, "HP Smart Array 6400 EM" },
279 { 0x103C, 0x3211, CISS_BOARD_SA5, "HP Smart Array E200i" },
280 { 0x103C, 0x3212, CISS_BOARD_SA5, "HP Smart Array E200" },
281 { 0x103C, 0x3213, CISS_BOARD_SA5, "HP Smart Array E200i" },
282 { 0x103C, 0x3214, CISS_BOARD_SA5, "HP Smart Array E200i" },
283 { 0x103C, 0x3215, CISS_BOARD_SA5, "HP Smart Array E200i" },
284 { 0x103C, 0x3220, CISS_BOARD_SA5, "HP Smart Array" },
285 { 0x103C, 0x3222, CISS_BOARD_SA5, "HP Smart Array" },
286 { 0x103C, 0x3223, CISS_BOARD_SA5, "HP Smart Array P800" },
287 { 0x103C, 0x3225, CISS_BOARD_SA5, "HP Smart Array P600" },
288 { 0x103C, 0x3230, CISS_BOARD_SA5, "HP Smart Array" },
289 { 0x103C, 0x3231, CISS_BOARD_SA5, "HP Smart Array" },
290 { 0x103C, 0x3232, CISS_BOARD_SA5, "HP Smart Array" },
291 { 0x103C, 0x3233, CISS_BOARD_SA5, "HP Smart Array" },
292 { 0x103C, 0x3234, CISS_BOARD_SA5, "HP Smart Array P400" },
293 { 0x103C, 0x3235, CISS_BOARD_SA5, "HP Smart Array P400i" },
294 { 0x103C, 0x3236, CISS_BOARD_SA5, "HP Smart Array" },
295 { 0x103C, 0x3237, CISS_BOARD_SA5, "HP Smart Array" },
296 { 0x103C, 0x3238, CISS_BOARD_SA5, "HP Smart Array" },
297 { 0x103C, 0x3239, CISS_BOARD_SA5, "HP Smart Array" },
298 { 0x103C, 0x323A, CISS_BOARD_SA5, "HP Smart Array" },
299 { 0x103C, 0x323B, CISS_BOARD_SA5, "HP Smart Array" },
300 { 0x103C, 0x323C, CISS_BOARD_SA5, "HP Smart Array" },
301 { 0x103C, 0x3241, CISS_BOARD_SA5, "HP Smart Array P212" },
302 { 0x103C, 0x3243, CISS_BOARD_SA5, "HP Smart Array P410" },
303 { 0x103C, 0x3245, CISS_BOARD_SA5, "HP Smart Array P410i" },
304 { 0x103C, 0x3247, CISS_BOARD_SA5, "HP Smart Array P411" },
305 { 0x103C, 0x3249, CISS_BOARD_SA5, "HP Smart Array P812" },
309 /************************************************************************
310 * Find a match for the device in our list of known adapters.
313 ciss_lookup(device_t dev)
317 for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
318 if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
319 (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
325 /************************************************************************
326 * Match a known CISS adapter.
329 ciss_probe(device_t dev)
333 i = ciss_lookup(dev);
335 device_set_desc(dev, ciss_vendor_data[i].desc);
336 return(BUS_PROBE_DEFAULT);
341 /************************************************************************
342 * Attach the driver to this adapter.
345 ciss_attach(device_t dev)
347 struct ciss_softc *sc;
353 /* print structure/union sizes */
354 debug_struct(ciss_command);
355 debug_struct(ciss_header);
356 debug_union(ciss_device_address);
357 debug_struct(ciss_cdb);
358 debug_struct(ciss_report_cdb);
359 debug_struct(ciss_notify_cdb);
360 debug_struct(ciss_notify);
361 debug_struct(ciss_message_cdb);
362 debug_struct(ciss_error_info_pointer);
363 debug_struct(ciss_error_info);
364 debug_struct(ciss_sg_entry);
365 debug_struct(ciss_config_table);
366 debug_struct(ciss_bmic_cdb);
367 debug_struct(ciss_bmic_id_ldrive);
368 debug_struct(ciss_bmic_id_lstatus);
369 debug_struct(ciss_bmic_id_table);
370 debug_struct(ciss_bmic_id_pdrive);
371 debug_struct(ciss_bmic_blink_pdrive);
372 debug_struct(ciss_bmic_flush_cache);
373 debug_const(CISS_MAX_REQUESTS);
374 debug_const(CISS_MAX_LOGICAL);
375 debug_const(CISS_INTERRUPT_COALESCE_DELAY);
376 debug_const(CISS_INTERRUPT_COALESCE_COUNT);
377 debug_const(CISS_COMMAND_ALLOC_SIZE);
378 debug_const(CISS_COMMAND_SG_LENGTH);
380 debug_type(cciss_pci_info_struct);
381 debug_type(cciss_coalint_struct);
382 debug_type(cciss_coalint_struct);
383 debug_type(NodeName_type);
384 debug_type(NodeName_type);
385 debug_type(Heartbeat_type);
386 debug_type(BusTypes_type);
387 debug_type(FirmwareVer_type);
388 debug_type(DriverVer_type);
389 debug_type(IOCTL_Command_struct);
392 sc = device_get_softc(dev);
396 * Work out adapter type.
398 i = ciss_lookup(dev);
400 ciss_printf(sc, "unknown adapter type\n");
404 if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
405 sc->ciss_interrupt_mask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
406 } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
407 sc->ciss_interrupt_mask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
409 /* really an error on our part */
410 ciss_printf(sc, "unable to determine hardware type\n");
416 * Do PCI-specific init.
418 if ((error = ciss_init_pci(sc)) != 0)
422 * Initialise driver queues.
426 ciss_initq_complete(sc);
427 ciss_initq_notify(sc);
428 mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
429 callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);
432 * Initalize device sysctls.
434 ciss_init_sysctl(sc);
437 * Initialise command/request pool.
439 if ((error = ciss_init_requests(sc)) != 0)
443 * Get adapter information.
445 if ((error = ciss_identify_adapter(sc)) != 0)
449 * Find all the physical devices.
451 if ((error = ciss_init_physical(sc)) != 0)
455 * Build our private table of logical devices.
457 if ((error = ciss_init_logical(sc)) != 0)
461 * Enable interrupts so that the CAM scan can complete.
463 CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
466 * Initialise the CAM interface.
468 if ((error = ciss_cam_init(sc)) != 0)
472 * Start the heartbeat routine and event chain.
477 * Create the control device.
479 sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
480 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
481 "ciss%d", device_get_unit(sc->ciss_dev));
482 sc->ciss_dev_t->si_drv1 = sc;
485 * The adapter is running; synchronous commands can now sleep
486 * waiting for an interrupt to signal completion.
488 sc->ciss_flags |= CISS_FLAG_RUNNING;
490 ciss_spawn_notify_thread(sc);
499 /************************************************************************
500 * Detach the driver from this adapter.
503 ciss_detach(device_t dev)
505 struct ciss_softc *sc = device_get_softc(dev);
509 mtx_lock(&sc->ciss_mtx);
510 if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
511 mtx_unlock(&sc->ciss_mtx);
515 /* flush adapter cache */
516 ciss_flush_adapter(sc);
518 /* release all resources. The mutex is released and freed here too. */
524 /************************************************************************
525 * Prepare adapter for system shutdown.
528 ciss_shutdown(device_t dev)
530 struct ciss_softc *sc = device_get_softc(dev);
534 mtx_lock(&sc->ciss_mtx);
535 /* flush adapter cache */
536 ciss_flush_adapter(sc);
538 if (sc->ciss_soft_reset)
540 mtx_unlock(&sc->ciss_mtx);
546 ciss_init_sysctl(struct ciss_softc *sc)
549 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
550 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
551 OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
554 /************************************************************************
555 * Perform PCI-specific attachment actions.
558 ciss_init_pci(struct ciss_softc *sc)
560 uintptr_t cbase, csize, cofs;
566 * Allocate register window first (we need this to find the config
570 sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
571 if ((sc->ciss_regs_resource =
572 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
573 &sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
574 ciss_printf(sc, "can't allocate register window\n");
577 sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
578 sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
581 * Find the BAR holding the config structure. If it's not the one
582 * we already mapped for registers, map it too.
584 sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
585 if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
586 if ((sc->ciss_cfg_resource =
587 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
588 &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
589 ciss_printf(sc, "can't allocate config window\n");
592 cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
593 csize = rman_get_end(sc->ciss_cfg_resource) -
594 rman_get_start(sc->ciss_cfg_resource) + 1;
596 cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
597 csize = rman_get_end(sc->ciss_regs_resource) -
598 rman_get_start(sc->ciss_regs_resource) + 1;
600 cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
603 * Use the base/size/offset values we just calculated to
604 * sanity-check the config structure. If it's OK, point to it.
606 if ((cofs + sizeof(struct ciss_config_table)) > csize) {
607 ciss_printf(sc, "config table outside window\n");
610 sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
611 debug(1, "config struct at %p", sc->ciss_cfg);
614 * Validate the config structure. If we supported other transport
615 * methods, we could select amongst them at this point in time.
617 if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
618 ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
619 sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
620 sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
625 * Put the board into simple mode, and tell it we're using the low
626 * 4GB of RAM. Set the default interrupt coalescing options.
628 if (!(sc->ciss_cfg->supported_methods & CISS_TRANSPORT_METHOD_SIMPLE)) {
629 ciss_printf(sc, "adapter does not support 'simple' transport layer\n");
632 sc->ciss_cfg->requested_method = CISS_TRANSPORT_METHOD_SIMPLE;
633 sc->ciss_cfg->command_physlimit = 0;
634 sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
635 sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
638 sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
641 if (ciss_update_config(sc)) {
642 ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
643 CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
646 if (!(sc->ciss_cfg->active_method != CISS_TRANSPORT_METHOD_SIMPLE)) {
648 "adapter refuses to go into 'simple' transport mode (0x%x, 0x%x)\n",
649 sc->ciss_cfg->supported_methods, sc->ciss_cfg->active_method);
654 * Wait for the adapter to come ready.
656 if ((error = ciss_wait_adapter(sc)) != 0)
660 * Turn off interrupts before we go routing anything.
662 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
665 * Allocate and set up our interrupt.
667 sc->ciss_irq_rid = 0;
668 if ((sc->ciss_irq_resource =
669 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid,
670 RF_ACTIVE | RF_SHAREABLE)) == NULL) {
671 ciss_printf(sc, "can't allocate interrupt\n");
674 if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
675 INTR_TYPE_CAM|INTR_MPSAFE, NULL, ciss_intr, sc,
677 ciss_printf(sc, "can't set up interrupt\n");
682 * Allocate the parent bus DMA tag appropriate for our PCI
685 * Note that "simple" adapters can only address within a 32-bit
688 if (bus_dma_tag_create(NULL, /* parent */
689 1, 0, /* alignment, boundary */
690 BUS_SPACE_MAXADDR, /* lowaddr */
691 BUS_SPACE_MAXADDR, /* highaddr */
692 NULL, NULL, /* filter, filterarg */
693 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
694 CISS_COMMAND_SG_LENGTH, /* nsegments */
695 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
696 BUS_DMA_ALLOCNOW, /* flags */
697 NULL, NULL, /* lockfunc, lockarg */
698 &sc->ciss_parent_dmat)) {
699 ciss_printf(sc, "can't allocate parent DMA tag\n");
704 * Create DMA tag for mapping buffers into adapter-addressable
707 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
708 1, 0, /* alignment, boundary */
709 BUS_SPACE_MAXADDR, /* lowaddr */
710 BUS_SPACE_MAXADDR, /* highaddr */
711 NULL, NULL, /* filter, filterarg */
712 MAXBSIZE, CISS_COMMAND_SG_LENGTH, /* maxsize, nsegments */
713 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
715 busdma_lock_mutex, &sc->ciss_mtx, /* lockfunc, lockarg */
716 &sc->ciss_buffer_dmat)) {
717 ciss_printf(sc, "can't allocate buffer DMA tag\n");
723 /************************************************************************
724 * Wait for the adapter to come ready.
727 ciss_wait_adapter(struct ciss_softc *sc)
734 * Wait for the adapter to come ready.
736 if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
737 ciss_printf(sc, "waiting for adapter to come ready...\n");
738 for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
739 DELAY(1000000); /* one second */
741 ciss_printf(sc, "timed out waiting for adapter to come ready\n");
749 /************************************************************************
750 * Flush the adapter cache.
753 ciss_flush_adapter(struct ciss_softc *sc)
755 struct ciss_request *cr;
756 struct ciss_bmic_flush_cache *cbfc;
757 int error, command_status;
765 * Build a BMIC request to flush the cache. We don't disable
766 * it, as we may be going to do more I/O (eg. we are emulating
767 * the Synchronise Cache command).
769 if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
773 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
774 (void **)&cbfc, sizeof(*cbfc))) != 0)
778 * Submit the request and wait for it to complete.
780 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
781 ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
788 ciss_report_request(cr, &command_status, NULL);
789 switch(command_status) {
790 case CISS_CMD_STATUS_SUCCESS:
793 ciss_printf(sc, "error flushing cache (%s)\n",
794 ciss_name_command_status(command_status));
801 free(cbfc, CISS_MALLOC_CLASS);
803 ciss_release_request(cr);
808 ciss_soft_reset(struct ciss_softc *sc)
810 struct ciss_request *cr = NULL;
811 struct ciss_command *cc;
814 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
815 /* only reset proxy controllers */
816 if (sc->ciss_controllers[i].physical.bus == 0)
819 if ((error = ciss_get_request(sc, &cr)) != 0)
822 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
826 cc = CISS_FIND_COMMAND(cr);
827 cc->header.address = sc->ciss_controllers[i];
829 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
832 ciss_release_request(cr);
836 ciss_printf(sc, "error resetting controller (%d)\n", error);
839 ciss_release_request(cr);
842 /************************************************************************
843 * Allocate memory for the adapter command structures, initialise
844 * the request structures.
846 * Note that the entire set of commands are allocated in a single
850 ciss_init_requests(struct ciss_softc *sc)
852 struct ciss_request *cr;
858 * Calculate the number of request structures/commands we are
859 * going to provide for this adapter.
861 sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
864 ciss_printf(sc, "using %d of %d available commands\n",
865 sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
868 * Create the DMA tag for commands.
870 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
871 1, 0, /* alignment, boundary */
872 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
873 BUS_SPACE_MAXADDR, /* highaddr */
874 NULL, NULL, /* filter, filterarg */
875 CISS_COMMAND_ALLOC_SIZE *
876 sc->ciss_max_requests, 1, /* maxsize, nsegments */
877 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
878 BUS_DMA_ALLOCNOW, /* flags */
879 NULL, NULL, /* lockfunc, lockarg */
880 &sc->ciss_command_dmat)) {
881 ciss_printf(sc, "can't allocate command DMA tag\n");
885 * Allocate memory and make it available for DMA.
887 if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
888 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
889 ciss_printf(sc, "can't allocate command memory\n");
892 bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map, sc->ciss_command,
893 CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
894 ciss_command_map_helper, sc, 0);
895 bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
898 * Set up the request and command structures, push requests onto
901 for (i = 1; i < sc->ciss_max_requests; i++) {
902 cr = &sc->ciss_request[i];
905 bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
906 ciss_enqueue_free(cr);
912 ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
914 struct ciss_softc *sc = (struct ciss_softc *)arg;
916 sc->ciss_command_phys = segs->ds_addr;
919 /************************************************************************
920 * Identify the adapter, print some information about it.
923 ciss_identify_adapter(struct ciss_softc *sc)
925 struct ciss_request *cr;
926 int error, command_status;
933 * Get a request, allocate storage for the adapter data.
935 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
936 (void **)&sc->ciss_id,
937 sizeof(*sc->ciss_id))) != 0)
941 * Submit the request and wait for it to complete.
943 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
944 ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
951 ciss_report_request(cr, &command_status, NULL);
952 switch(command_status) {
953 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
955 case CISS_CMD_STATUS_DATA_UNDERRUN:
956 case CISS_CMD_STATUS_DATA_OVERRUN:
957 ciss_printf(sc, "data over/underrun reading adapter information\n");
959 ciss_printf(sc, "error reading adapter information (%s)\n",
960 ciss_name_command_status(command_status));
965 /* sanity-check reply */
966 if (!sc->ciss_id->big_map_supported) {
967 ciss_printf(sc, "adapter does not support BIG_MAP\n");
973 /* XXX later revisions may not need this */
974 sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
977 /* XXX only really required for old 5300 adapters? */
978 sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
980 /* print information */
982 #if 0 /* XXX proxy volumes??? */
983 ciss_printf(sc, " %d logical drive%s configured\n",
984 sc->ciss_id->configured_logical_drives,
985 (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
987 ciss_printf(sc, " firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
988 ciss_printf(sc, " %d SCSI channels\n", sc->ciss_id->scsi_bus_count);
990 ciss_printf(sc, " signature '%.4s'\n", sc->ciss_cfg->signature);
991 ciss_printf(sc, " valence %d\n", sc->ciss_cfg->valence);
992 ciss_printf(sc, " supported I/O methods 0x%b\n",
993 sc->ciss_cfg->supported_methods,
994 "\20\1READY\2simple\3performant\4MEMQ\n");
995 ciss_printf(sc, " active I/O method 0x%b\n",
996 sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
997 ciss_printf(sc, " 4G page base 0x%08x\n",
998 sc->ciss_cfg->command_physlimit);
999 ciss_printf(sc, " interrupt coalesce delay %dus\n",
1000 sc->ciss_cfg->interrupt_coalesce_delay);
1001 ciss_printf(sc, " interrupt coalesce count %d\n",
1002 sc->ciss_cfg->interrupt_coalesce_count);
1003 ciss_printf(sc, " max outstanding commands %d\n",
1004 sc->ciss_cfg->max_outstanding_commands);
1005 ciss_printf(sc, " bus types 0x%b\n", sc->ciss_cfg->bus_types,
1006 "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
1007 ciss_printf(sc, " server name '%.16s'\n", sc->ciss_cfg->server_name);
1008 ciss_printf(sc, " heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
1013 if (sc->ciss_id != NULL) {
1014 free(sc->ciss_id, CISS_MALLOC_CLASS);
1019 ciss_release_request(cr);
1023 /************************************************************************
1024 * Helper routine for generating a list of logical and physical luns.
1026 static struct ciss_lun_report *
1027 ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
1029 struct ciss_request *cr;
1030 struct ciss_command *cc;
1031 struct ciss_report_cdb *crc;
1032 struct ciss_lun_report *cll;
1043 * Get a request, allocate storage for the address list.
1045 if ((error = ciss_get_request(sc, &cr)) != 0)
1047 report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
1048 if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1049 ciss_printf(sc, "can't allocate memory for lun report\n");
1055 * Build the Report Logical/Physical LUNs command.
1057 cc = CISS_FIND_COMMAND(cr);
1059 cr->cr_length = report_size;
1060 cr->cr_flags = CISS_REQ_DATAIN;
1062 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
1063 cc->header.address.physical.bus = 0;
1064 cc->header.address.physical.target = 0;
1065 cc->cdb.cdb_length = sizeof(*crc);
1066 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1067 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1068 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1069 cc->cdb.timeout = 30; /* XXX better suggestions? */
1071 crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
1072 bzero(crc, sizeof(*crc));
1073 crc->opcode = opcode;
1074 crc->length = htonl(report_size); /* big-endian field */
1075 cll->list_size = htonl(report_size - sizeof(*cll)); /* big-endian field */
1078 * Submit the request and wait for it to complete. (timeout
1079 * here should be much greater than above)
1081 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1082 ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
1087 * Check response. Note that data over/underrun is OK.
1089 ciss_report_request(cr, &command_status, NULL);
1090 switch(command_status) {
1091 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1092 case CISS_CMD_STATUS_DATA_UNDERRUN: /* buffer too large, not bad */
1094 case CISS_CMD_STATUS_DATA_OVERRUN:
1095 ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
1099 ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
1100 ciss_name_command_status(command_status));
1104 ciss_release_request(cr);
1109 ciss_release_request(cr);
1110 if (error && cll != NULL) {
1111 free(cll, CISS_MALLOC_CLASS);
1117 /************************************************************************
1118 * Find logical drives on the adapter.
1121 ciss_init_logical(struct ciss_softc *sc)
1123 struct ciss_lun_report *cll;
1124 int error = 0, i, j;
1129 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
1136 /* sanity-check reply */
1137 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1138 if ((ndrives < 0) || (ndrives >= CISS_MAX_LOGICAL)) {
1139 ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
1140 ndrives, CISS_MAX_LOGICAL);
1146 * Save logical drive information.
1149 ciss_printf(sc, "%d logical drive%s\n",
1150 ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
1154 malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
1155 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1156 if (sc->ciss_logical == NULL) {
1161 for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1162 sc->ciss_logical[i] =
1163 malloc(CISS_MAX_LOGICAL * sizeof(struct ciss_ldrive),
1164 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1165 if (sc->ciss_logical[i] == NULL) {
1170 for (j = 0; j < CISS_MAX_LOGICAL; j++)
1171 sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
1175 for (i = 0; i < CISS_MAX_LOGICAL; i++) {
1177 struct ciss_ldrive *ld;
1180 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
1181 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
1182 ld = &sc->ciss_logical[bus][target];
1184 ld->cl_address = cll->lun[i];
1185 ld->cl_controller = &sc->ciss_controllers[bus];
1186 if (ciss_identify_logical(sc, ld) != 0)
1189 * If the drive has had media exchanged, we should bring it online.
1191 if (ld->cl_lstatus->media_exchanged)
1192 ciss_accept_media(sc, ld);
1199 free(cll, CISS_MALLOC_CLASS);
1204 ciss_init_physical(struct ciss_softc *sc)
1206 struct ciss_lun_report *cll;
1216 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
1223 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1226 ciss_printf(sc, "%d physical device%s\n",
1227 nphys, (nphys > 1 || nphys == 0) ? "s" : "");
1231 * Figure out the bus mapping.
1232 * Logical buses include both the local logical bus for local arrays and
1233 * proxy buses for remote arrays. Physical buses are numbered by the
1234 * controller and represent physical buses that hold physical devices.
1235 * We shift these bus numbers so that everything fits into a single flat
1236 * numbering space for CAM. Logical buses occupy the first 32 CAM bus
1237 * numbers, and the physical bus numbers are shifted to be above that.
1238 * This results in the various driver arrays being indexed as follows:
1240 * ciss_controllers[] - indexed by logical bus
1241 * ciss_cam_sim[] - indexed by both logical and physical, with physical
1242 * being shifted by 32.
1243 * ciss_logical[][] - indexed by logical bus
1244 * ciss_physical[][] - indexed by physical bus
1246 * XXX This is getting more and more hackish. CISS really doesn't play
1247 * well with a standard SCSI model; devices are addressed via magic
1248 * cookies, not via b/t/l addresses. Since there is no way to store
1249 * the cookie in the CAM device object, we have to keep these lookup
1250 * tables handy so that the devices can be found quickly at the cost
1251 * of wasting memory and having a convoluted lookup scheme. This
1252 * driver should probably be converted to block interface.
1255 * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
1256 * controller. A proxy controller is another physical controller
1257 * behind the primary PCI controller. We need to know about this
1258 * so that BMIC commands can be properly targeted. There can be
1259 * proxy controllers attached to a single PCI controller, so
1260 * find the highest numbered one so the array can be properly
1263 sc->ciss_max_logical_bus = 1;
1264 for (i = 0; i < nphys; i++) {
1265 if (cll->lun[i].physical.extra_address == 0) {
1266 bus = cll->lun[i].physical.bus;
1267 sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
1269 bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
1270 sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
1274 sc->ciss_controllers =
1275 malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
1276 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1278 if (sc->ciss_controllers == NULL) {
1279 ciss_printf(sc, "Could not allocate memory for controller map\n");
1284 /* setup a map of controller addresses */
1285 for (i = 0; i < nphys; i++) {
1286 if (cll->lun[i].physical.extra_address == 0) {
1287 sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
1292 malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
1293 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1294 if (sc->ciss_physical == NULL) {
1295 ciss_printf(sc, "Could not allocate memory for physical device map\n");
1300 for (i = 0; i < sc->ciss_max_physical_bus; i++) {
1301 sc->ciss_physical[i] =
1302 malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
1303 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1304 if (sc->ciss_physical[i] == NULL) {
1305 ciss_printf(sc, "Could not allocate memory for target map\n");
1311 ciss_filter_physical(sc, cll);
1315 free(cll, CISS_MALLOC_CLASS);
1321 ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
1327 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1328 for (i = 0; i < nphys; i++) {
1329 if (cll->lun[i].physical.extra_address == 0)
1333 * Filter out devices that we don't want. Level 3 LUNs could
1334 * probably be supported, but the docs don't give enough of a
1337 * The mode field of the physical address is likely set to have
1338 * hard disks masked out. Honor it unless the user has overridden
1339 * us with the tunable. We also munge the inquiry data for these
1340 * disks so that they only show up as passthrough devices. Keeping
1341 * them visible in this fashion is useful for doing things like
1342 * flashing firmware.
1344 ea = cll->lun[i].physical.extra_address;
1345 if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
1346 (CISS_EXTRA_MODE2(ea) == 0x3))
1348 if ((ciss_expose_hidden_physical == 0) &&
1349 (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
1353 * Note: CISS firmware numbers physical busses starting at '1', not
1354 * '0'. This numbering is internal to the firmware and is only
1355 * used as a hint here.
1357 bus = CISS_EXTRA_BUS2(ea) - 1;
1358 target = CISS_EXTRA_TARGET2(ea);
1359 sc->ciss_physical[bus][target].cp_address = cll->lun[i];
1360 sc->ciss_physical[bus][target].cp_online = 1;
1367 ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1369 struct ciss_request *cr;
1370 struct ciss_command *cc;
1371 struct scsi_inquiry *inq;
1377 bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1379 if ((error = ciss_get_request(sc, &cr)) != 0)
1382 cc = CISS_FIND_COMMAND(cr);
1383 cr->cr_data = &ld->cl_geometry;
1384 cr->cr_length = sizeof(ld->cl_geometry);
1385 cr->cr_flags = CISS_REQ_DATAIN;
1387 cc->header.address = ld->cl_address;
1388 cc->cdb.cdb_length = 6;
1389 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1390 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1391 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1392 cc->cdb.timeout = 30;
1394 inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1395 inq->opcode = INQUIRY;
1396 inq->byte2 = SI_EVPD;
1397 inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1398 inq->length = sizeof(ld->cl_geometry);
1400 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1401 ciss_printf(sc, "error getting geometry (%d)\n", error);
1405 ciss_report_request(cr, &command_status, NULL);
1406 switch(command_status) {
1407 case CISS_CMD_STATUS_SUCCESS:
1408 case CISS_CMD_STATUS_DATA_UNDERRUN:
1410 case CISS_CMD_STATUS_DATA_OVERRUN:
1411 ciss_printf(sc, "WARNING: Data overrun\n");
1414 ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1415 ciss_name_command_status(command_status));
1421 ciss_release_request(cr);
1424 /************************************************************************
1425 * Identify a logical drive, initialise state related to it.
1428 ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1430 struct ciss_request *cr;
1431 struct ciss_command *cc;
1432 struct ciss_bmic_cdb *cbc;
1433 int error, command_status;
1440 * Build a BMIC request to fetch the drive ID.
1442 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
1443 (void **)&ld->cl_ldrive,
1444 sizeof(*ld->cl_ldrive))) != 0)
1446 cc = CISS_FIND_COMMAND(cr);
1447 cc->header.address = *ld->cl_controller; /* target controller */
1448 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1449 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1452 * Submit the request and wait for it to complete.
1454 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1455 ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
1462 ciss_report_request(cr, &command_status, NULL);
1463 switch(command_status) {
1464 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1466 case CISS_CMD_STATUS_DATA_UNDERRUN:
1467 case CISS_CMD_STATUS_DATA_OVERRUN:
1468 ciss_printf(sc, "data over/underrun reading logical drive ID\n");
1470 ciss_printf(sc, "error reading logical drive ID (%s)\n",
1471 ciss_name_command_status(command_status));
1475 ciss_release_request(cr);
1479 * Build a CISS BMIC command to get the logical drive status.
1481 if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
1485 * Get the logical drive geometry.
1487 if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1491 * Print the drive's basic characteristics.
1494 ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
1495 CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
1496 CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
1497 ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
1498 ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
1499 ld->cl_ldrive->block_size));
1501 ciss_print_ldrive(sc, ld);
1505 /* make the drive not-exist */
1506 ld->cl_status = CISS_LD_NONEXISTENT;
1507 if (ld->cl_ldrive != NULL) {
1508 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
1509 ld->cl_ldrive = NULL;
1511 if (ld->cl_lstatus != NULL) {
1512 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
1513 ld->cl_lstatus = NULL;
1517 ciss_release_request(cr);
1522 /************************************************************************
1523 * Get status for a logical drive.
1525 * XXX should we also do this in response to Test Unit Ready?
1528 ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld)
1530 struct ciss_request *cr;
1531 struct ciss_command *cc;
1532 struct ciss_bmic_cdb *cbc;
1533 int error, command_status;
1536 * Build a CISS BMIC command to get the logical drive status.
1538 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
1539 (void **)&ld->cl_lstatus,
1540 sizeof(*ld->cl_lstatus))) != 0)
1542 cc = CISS_FIND_COMMAND(cr);
1543 cc->header.address = *ld->cl_controller; /* target controller */
1544 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1545 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1548 * Submit the request and wait for it to complete.
1550 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1551 ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1558 ciss_report_request(cr, &command_status, NULL);
1559 switch(command_status) {
1560 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1562 case CISS_CMD_STATUS_DATA_UNDERRUN:
1563 case CISS_CMD_STATUS_DATA_OVERRUN:
1564 ciss_printf(sc, "data over/underrun reading logical drive status\n");
1566 ciss_printf(sc, "error reading logical drive status (%s)\n",
1567 ciss_name_command_status(command_status));
1573 * Set the drive's summary status based on the returned status.
1575 * XXX testing shows that a failed JBOD drive comes back at next
1576 * boot in "queued for expansion" mode. WTF?
1578 ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
1582 ciss_release_request(cr);
1586 /************************************************************************
1587 * Notify the adapter of a config update.
1590 ciss_update_config(struct ciss_softc *sc)
1596 CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
1597 for (i = 0; i < 1000; i++) {
1598 if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
1599 CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
1607 /************************************************************************
1608 * Accept new media into a logical drive.
1610 * XXX The drive has previously been offline; it would be good if we
1611 * could make sure it's not open right now.
1614 ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
1616 struct ciss_request *cr;
1617 struct ciss_command *cc;
1618 struct ciss_bmic_cdb *cbc;
1620 int error = 0, ldrive;
1622 ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1624 debug(0, "bringing logical drive %d back online");
1627 * Build a CISS BMIC command to bring the drive back online.
1629 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
1632 cc = CISS_FIND_COMMAND(cr);
1633 cc->header.address = *ld->cl_controller; /* target controller */
1634 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1635 cbc->log_drive = ldrive;
1638 * Submit the request and wait for it to complete.
1640 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1641 ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
1648 ciss_report_request(cr, &command_status, NULL);
1649 switch(command_status) {
1650 case CISS_CMD_STATUS_SUCCESS: /* all OK */
1651 /* we should get a logical drive status changed event here */
1654 ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
1655 ciss_name_command_status(command_status));
1661 ciss_release_request(cr);
1665 /************************************************************************
1666 * Release adapter resources.
1669 ciss_free(struct ciss_softc *sc)
1671 struct ciss_request *cr;
1676 /* we're going away */
1677 sc->ciss_flags |= CISS_FLAG_ABORTING;
1679 /* terminate the periodic heartbeat routine */
1680 callout_stop(&sc->ciss_periodic);
1682 /* cancel the Event Notify chain */
1683 ciss_notify_abort(sc);
1685 ciss_kill_notify_thread(sc);
1687 /* disconnect from CAM */
1688 if (sc->ciss_cam_sim) {
1689 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1690 if (sc->ciss_cam_sim[i]) {
1691 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1692 cam_sim_free(sc->ciss_cam_sim[i], 0);
1695 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
1696 CISS_PHYSICAL_BASE; i++) {
1697 if (sc->ciss_cam_sim[i]) {
1698 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1699 cam_sim_free(sc->ciss_cam_sim[i], 0);
1702 free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
1704 if (sc->ciss_cam_devq)
1705 cam_simq_free(sc->ciss_cam_devq);
1707 /* remove the control device */
1708 mtx_unlock(&sc->ciss_mtx);
1709 if (sc->ciss_dev_t != NULL)
1710 destroy_dev(sc->ciss_dev_t);
1712 /* Final cleanup of the callout. */
1713 callout_drain(&sc->ciss_periodic);
1714 mtx_destroy(&sc->ciss_mtx);
1716 /* free the controller data */
1717 if (sc->ciss_id != NULL)
1718 free(sc->ciss_id, CISS_MALLOC_CLASS);
1720 /* release I/O resources */
1721 if (sc->ciss_regs_resource != NULL)
1722 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1723 sc->ciss_regs_rid, sc->ciss_regs_resource);
1724 if (sc->ciss_cfg_resource != NULL)
1725 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1726 sc->ciss_cfg_rid, sc->ciss_cfg_resource);
1727 if (sc->ciss_intr != NULL)
1728 bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
1729 if (sc->ciss_irq_resource != NULL)
1730 bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
1731 sc->ciss_irq_rid, sc->ciss_irq_resource);
1733 /* destroy DMA tags */
1734 if (sc->ciss_parent_dmat)
1735 bus_dma_tag_destroy(sc->ciss_parent_dmat);
1737 while ((cr = ciss_dequeue_free(sc)) != NULL)
1738 bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
1739 if (sc->ciss_buffer_dmat)
1740 bus_dma_tag_destroy(sc->ciss_buffer_dmat);
1742 /* destroy command memory and DMA tag */
1743 if (sc->ciss_command != NULL) {
1744 bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
1745 bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
1747 if (sc->ciss_command_dmat)
1748 bus_dma_tag_destroy(sc->ciss_command_dmat);
1750 if (sc->ciss_logical) {
1751 for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1752 for (j = 0; j < CISS_MAX_LOGICAL; j++) {
1753 if (sc->ciss_logical[i][j].cl_ldrive)
1754 free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
1755 if (sc->ciss_logical[i][j].cl_lstatus)
1756 free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
1758 free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
1760 free(sc->ciss_logical, CISS_MALLOC_CLASS);
1763 if (sc->ciss_physical) {
1764 for (i = 0; i < sc->ciss_max_physical_bus; i++)
1765 free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
1766 free(sc->ciss_physical, CISS_MALLOC_CLASS);
1769 if (sc->ciss_controllers)
1770 free(sc->ciss_controllers, CISS_MALLOC_CLASS);
1774 /************************************************************************
1775 * Give a command to the adapter.
1777 * Note that this uses the simple transport layer directly. If we
1778 * want to add support for other layers, we'll need a switch of some
1781 * Note that the simple transport layer has no way of refusing a
1782 * command; we only have as many request structures as the adapter
1783 * supports commands, so we don't have to check (this presumes that
1784 * the adapter can handle commands as fast as we throw them at it).
1787 ciss_start(struct ciss_request *cr)
1789 struct ciss_command *cc; /* XXX debugging only */
1792 cc = CISS_FIND_COMMAND(cr);
1793 debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
1796 * Map the request's data.
1798 if ((error = ciss_map_request(cr)))
1802 ciss_print_request(cr);
1808 /************************************************************************
1809 * Fetch completed request(s) from the adapter, queue them for
1810 * completion handling.
1812 * Note that this uses the simple transport layer directly. If we
1813 * want to add support for other layers, we'll need a switch of some
1816 * Note that the simple transport mechanism does not require any
1817 * reentrancy protection; the OPQ read is atomic. If there is a
1818 * chance of a race with something else that might move the request
1819 * off the busy list, then we will have to lock against that
1820 * (eg. timeouts, etc.)
1823 ciss_done(struct ciss_softc *sc)
1825 struct ciss_request *cr;
1826 struct ciss_command *cc;
1827 u_int32_t tag, index;
1833 * Loop quickly taking requests from the adapter and moving them
1834 * from the busy queue to the completed queue.
1839 /* see if the OPQ contains anything */
1840 if (!CISS_TL_SIMPLE_OPQ_INTERRUPT(sc))
1843 tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
1844 if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
1847 debug(2, "completed command %d%s", index,
1848 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
1849 if (index >= sc->ciss_max_requests) {
1850 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
1853 cr = &(sc->ciss_request[index]);
1854 cc = CISS_FIND_COMMAND(cr);
1855 cc->header.host_tag = tag; /* not updated by adapter */
1856 if (ciss_remove_busy(cr)) {
1857 /* assume this is garbage out of the adapter */
1858 ciss_printf(sc, "completed nonbusy request %d\n", index);
1860 ciss_enqueue_complete(cr);
1866 * Invoke completion processing. If we can defer this out of
1867 * interrupt context, that'd be good.
1873 /************************************************************************
1874 * Take an interrupt from the adapter.
1877 ciss_intr(void *arg)
1879 struct ciss_softc *sc = (struct ciss_softc *)arg;
1882 * The only interrupt we recognise indicates that there are
1883 * entries in the outbound post queue.
1885 mtx_lock(&sc->ciss_mtx);
1887 mtx_unlock(&sc->ciss_mtx);
1890 /************************************************************************
1891 * Process completed requests.
1893 * Requests can be completed in three fashions:
1895 * - by invoking a callback function (cr_complete is non-null)
1896 * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
1897 * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
1900 ciss_complete(struct ciss_softc *sc)
1902 struct ciss_request *cr;
1907 * Loop taking requests off the completed queue and performing
1908 * completion processing on them.
1911 if ((cr = ciss_dequeue_complete(sc)) == NULL)
1913 ciss_unmap_request(cr);
1916 * If the request has a callback, invoke it.
1918 if (cr->cr_complete != NULL) {
1919 cr->cr_complete(cr);
1924 * If someone is sleeping on this request, wake them up.
1926 if (cr->cr_flags & CISS_REQ_SLEEP) {
1927 cr->cr_flags &= ~CISS_REQ_SLEEP;
1933 * If someone is polling this request for completion, signal.
1935 if (cr->cr_flags & CISS_REQ_POLL) {
1936 cr->cr_flags &= ~CISS_REQ_POLL;
1941 * Give up and throw the request back on the free queue. This
1942 * should never happen; resources will probably be lost.
1944 ciss_printf(sc, "WARNING: completed command with no submitter\n");
1945 ciss_enqueue_free(cr);
1949 /************************************************************************
1950 * Report on the completion status of a request, and pass back SCSI
1951 * and command status values.
1954 ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status)
1956 struct ciss_command *cc;
1957 struct ciss_error_info *ce;
1961 cc = CISS_FIND_COMMAND(cr);
1962 ce = (struct ciss_error_info *)&(cc->sg[0]);
1965 * We don't consider data under/overrun an error for the Report
1966 * Logical/Physical LUNs commands.
1968 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
1969 ((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
1970 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
1971 ((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
1972 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
1973 (cc->cdb.cdb[0] == INQUIRY))) {
1974 cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
1975 debug(2, "ignoring irrelevant under/overrun error");
1979 * Check the command's error bit, if clear, there's no status and
1982 if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
1983 if (scsi_status != NULL)
1984 *scsi_status = SCSI_STATUS_OK;
1985 if (command_status != NULL)
1986 *command_status = CISS_CMD_STATUS_SUCCESS;
1989 if (command_status != NULL)
1990 *command_status = ce->command_status;
1991 if (scsi_status != NULL) {
1992 if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
1993 *scsi_status = ce->scsi_status;
1999 ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
2000 ce->command_status, ciss_name_command_status(ce->command_status),
2002 if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
2003 ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x\n",
2004 ce->additional_error_info.invalid_command.offense_size,
2005 ce->additional_error_info.invalid_command.offense_offset,
2006 ce->additional_error_info.invalid_command.offense_value);
2010 ciss_print_request(cr);
2015 /************************************************************************
2016 * Issue a request and don't return until it's completed.
2018 * Depending on adapter status, we may poll or sleep waiting for
2022 ciss_synch_request(struct ciss_request *cr, int timeout)
2024 if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
2025 return(ciss_wait_request(cr, timeout));
2027 return(ciss_poll_request(cr, timeout));
2031 /************************************************************************
2032 * Issue a request and poll for completion.
2034 * Timeout in milliseconds.
2037 ciss_poll_request(struct ciss_request *cr, int timeout)
2043 cr->cr_flags |= CISS_REQ_POLL;
2044 if ((error = ciss_start(cr)) != 0)
2048 ciss_done(cr->cr_sc);
2049 if (!(cr->cr_flags & CISS_REQ_POLL))
2052 } while (timeout-- >= 0);
2053 return(EWOULDBLOCK);
2056 /************************************************************************
2057 * Issue a request and sleep waiting for completion.
2059 * Timeout in milliseconds. Note that a spurious wakeup will reset
2063 ciss_wait_request(struct ciss_request *cr, int timeout)
2069 cr->cr_flags |= CISS_REQ_SLEEP;
2070 if ((error = ciss_start(cr)) != 0)
2074 while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
2075 error = msleep(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ", (timeout * hz) / 1000);
2082 /************************************************************************
2083 * Abort a request. Note that a potential exists here to race the
2084 * request being completed; the caller must deal with this.
2087 ciss_abort_request(struct ciss_request *ar)
2089 struct ciss_request *cr;
2090 struct ciss_command *cc;
2091 struct ciss_message_cdb *cmc;
2097 if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
2100 /* build the abort command */
2101 cc = CISS_FIND_COMMAND(cr);
2102 cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL; /* addressing? */
2103 cc->header.address.physical.target = 0;
2104 cc->header.address.physical.bus = 0;
2105 cc->cdb.cdb_length = sizeof(*cmc);
2106 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
2107 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2108 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2109 cc->cdb.timeout = 30;
2111 cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
2112 cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
2113 cmc->type = CISS_MESSAGE_ABORT_TASK;
2114 cmc->abort_tag = ar->cr_tag; /* endianness?? */
2117 * Send the request and wait for a response. If we believe we
2118 * aborted the request OK, clear the flag that indicates it's
2121 error = ciss_synch_request(cr, 35 * 1000);
2123 error = ciss_report_request(cr, NULL, NULL);
2124 ciss_release_request(cr);
2131 /************************************************************************
2132 * Fetch and initialise a request
2135 ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
2137 struct ciss_request *cr;
2142 * Get a request and clean it up.
2144 if ((cr = ciss_dequeue_free(sc)) == NULL)
2149 cr->cr_complete = NULL;
2150 cr->cr_private = NULL;
2152 ciss_preen_command(cr);
2158 ciss_preen_command(struct ciss_request *cr)
2160 struct ciss_command *cc;
2164 * Clean up the command structure.
2166 * Note that we set up the error_info structure here, since the
2167 * length can be overwritten by any command.
2169 cc = CISS_FIND_COMMAND(cr);
2170 cc->header.sg_in_list = 0; /* kinda inefficient this way */
2171 cc->header.sg_total = 0;
2172 cc->header.host_tag = cr->cr_tag << 2;
2173 cc->header.host_tag_zeroes = 0;
2174 cmdphys = CISS_FIND_COMMANDPHYS(cr);
2175 cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
2176 cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
2179 /************************************************************************
2180 * Release a request to the free list.
2183 ciss_release_request(struct ciss_request *cr)
2185 struct ciss_softc *sc;
2191 /* release the request to the free queue */
2192 ciss_requeue_free(cr);
2195 /************************************************************************
2196 * Allocate a request that will be used to send a BMIC command. Do some
2197 * of the common setup here to avoid duplicating it everywhere else.
2200 ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
2201 int opcode, void **bufp, size_t bufsize)
2203 struct ciss_request *cr;
2204 struct ciss_command *cc;
2205 struct ciss_bmic_cdb *cbc;
2218 if ((error = ciss_get_request(sc, &cr)) != 0)
2222 * Allocate data storage if requested, determine the data direction.
2225 if ((bufsize > 0) && (bufp != NULL)) {
2226 if (*bufp == NULL) {
2227 if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
2233 dataout = 1; /* we are given a buffer, so we are writing */
2238 * Build a CISS BMIC command to get the logical drive ID.
2241 cr->cr_length = bufsize;
2243 cr->cr_flags = CISS_REQ_DATAIN;
2245 cc = CISS_FIND_COMMAND(cr);
2246 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2247 cc->header.address.physical.bus = 0;
2248 cc->header.address.physical.target = 0;
2249 cc->cdb.cdb_length = sizeof(*cbc);
2250 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2251 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2252 cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
2253 cc->cdb.timeout = 0;
2255 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
2256 bzero(cbc, sizeof(*cbc));
2257 cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
2258 cbc->bmic_opcode = opcode;
2259 cbc->size = htons((u_int16_t)bufsize);
2264 ciss_release_request(cr);
2267 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
2273 /************************************************************************
2274 * Handle a command passed in from userspace.
2277 ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
2279 struct ciss_request *cr;
2280 struct ciss_command *cc;
2281 struct ciss_error_info *ce;
2291 while (ciss_get_request(sc, &cr) != 0)
2292 msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
2293 cc = CISS_FIND_COMMAND(cr);
2296 * Allocate an in-kernel databuffer if required, copy in user data.
2298 cr->cr_length = ioc->buf_size;
2299 if (ioc->buf_size > 0) {
2300 if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
2304 if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
2305 debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2311 * Build the request based on the user command.
2313 bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
2314 bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
2316 /* XXX anything else to populate here? */
2321 if ((error = ciss_synch_request(cr, 60 * 1000))) {
2322 debug(0, "request failed - %d", error);
2327 * Check to see if the command succeeded.
2329 ce = (struct ciss_error_info *)&(cc->sg[0]);
2330 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
2331 bzero(ce, sizeof(*ce));
2334 * Copy the results back to the user.
2336 bcopy(ce, &ioc->error_info, sizeof(*ce));
2337 if ((ioc->buf_size > 0) &&
2338 (error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
2339 debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2347 if ((cr != NULL) && (cr->cr_data != NULL))
2348 free(cr->cr_data, CISS_MALLOC_CLASS);
2350 ciss_release_request(cr);
2354 /************************************************************************
2355 * Map a request into bus-visible space, initialise the scatter/gather
2359 ciss_map_request(struct ciss_request *cr)
2361 struct ciss_softc *sc;
2368 /* check that mapping is necessary */
2369 if (cr->cr_flags & CISS_REQ_MAPPED)
2372 cr->cr_flags |= CISS_REQ_MAPPED;
2374 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2375 BUS_DMASYNC_PREWRITE);
2377 if (cr->cr_data != NULL) {
2378 error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2379 cr->cr_data, cr->cr_length,
2380 ciss_request_map_helper, cr, 0);
2385 * Post the command to the adapter.
2387 ciss_enqueue_busy(cr);
2388 CISS_TL_SIMPLE_POST_CMD(cr->cr_sc, CISS_FIND_COMMANDPHYS(cr));
2395 ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2397 struct ciss_command *cc;
2398 struct ciss_request *cr;
2399 struct ciss_softc *sc;
2404 cr = (struct ciss_request *)arg;
2406 cc = CISS_FIND_COMMAND(cr);
2408 for (i = 0; i < nseg; i++) {
2409 cc->sg[i].address = segs[i].ds_addr;
2410 cc->sg[i].length = segs[i].ds_len;
2411 cc->sg[i].extension = 0;
2413 /* we leave the s/g table entirely within the command */
2414 cc->header.sg_in_list = nseg;
2415 cc->header.sg_total = nseg;
2417 if (cr->cr_flags & CISS_REQ_DATAIN)
2418 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2419 if (cr->cr_flags & CISS_REQ_DATAOUT)
2420 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2423 * Post the command to the adapter.
2425 ciss_enqueue_busy(cr);
2426 CISS_TL_SIMPLE_POST_CMD(cr->cr_sc, CISS_FIND_COMMANDPHYS(cr));
2429 /************************************************************************
2430 * Unmap a request from bus-visible space.
2433 ciss_unmap_request(struct ciss_request *cr)
2435 struct ciss_softc *sc;
2441 /* check that unmapping is necessary */
2442 if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
2445 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2446 BUS_DMASYNC_POSTWRITE);
2448 if (cr->cr_data == NULL)
2451 if (cr->cr_flags & CISS_REQ_DATAIN)
2452 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
2453 if (cr->cr_flags & CISS_REQ_DATAOUT)
2454 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
2456 bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2458 cr->cr_flags &= ~CISS_REQ_MAPPED;
2461 /************************************************************************
2462 * Attach the driver to CAM.
2464 * We put all the logical drives on a single SCSI bus.
2467 ciss_cam_init(struct ciss_softc *sc)
2474 * Allocate a devq. We can reuse this for the masked physical
2475 * devices if we decide to export these as well.
2477 if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests)) == NULL) {
2478 ciss_printf(sc, "can't allocate CAM SIM queue\n");
2485 * This naturally wastes a bit of memory. The alternative is to allocate
2486 * and register each bus as it is found, and then track them on a linked
2487 * list. Unfortunately, the driver has a few places where it needs to
2488 * look up the SIM based solely on bus number, and it's unclear whether
2489 * a list traversal would work for these situations.
2491 maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
2492 CISS_PHYSICAL_BASE);
2493 sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
2494 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
2495 if (sc->ciss_cam_sim == NULL) {
2496 ciss_printf(sc, "can't allocate memory for controller SIM\n");
2500 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2501 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2503 device_get_unit(sc->ciss_dev),
2505 sc->ciss_max_requests - 2,
2506 sc->ciss_max_requests - 2,
2507 sc->ciss_cam_devq)) == NULL) {
2508 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2513 * Register bus with this SIM.
2515 mtx_lock(&sc->ciss_mtx);
2516 if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) {
2517 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2518 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2519 mtx_unlock(&sc->ciss_mtx);
2523 mtx_unlock(&sc->ciss_mtx);
2526 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
2527 CISS_PHYSICAL_BASE; i++) {
2528 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2530 device_get_unit(sc->ciss_dev),
2532 sc->ciss_max_requests - 2,
2533 sc->ciss_cam_devq)) == NULL) {
2534 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2538 mtx_lock(&sc->ciss_mtx);
2539 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2540 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2541 mtx_unlock(&sc->ciss_mtx);
2544 mtx_unlock(&sc->ciss_mtx);
2548 * Initiate a rescan of the bus.
2550 mtx_lock(&sc->ciss_mtx);
2551 ciss_cam_rescan_all(sc);
2552 mtx_unlock(&sc->ciss_mtx);
2557 /************************************************************************
2558 * Initiate a rescan of the 'logical devices' SIM
2561 ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
2563 struct cam_path *path;
2568 if ((ccb = malloc(sizeof(union ccb), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
2569 ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
2573 if (xpt_create_path(&path, xpt_periph, cam_sim_path(sc->ciss_cam_sim[bus]),
2574 target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2575 ciss_printf(sc, "rescan failed (can't create path)\n");
2576 free(ccb, CISS_MALLOC_CLASS);
2580 xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
2581 ccb->ccb_h.func_code = XPT_SCAN_BUS;
2582 ccb->ccb_h.cbfcnp = ciss_cam_rescan_callback;
2583 ccb->crcn.flags = CAM_FLAG_NONE;
2586 /* scan is now in progress */
2590 ciss_cam_rescan_all(struct ciss_softc *sc)
2594 /* Rescan the logical buses */
2595 for (i = 0; i < sc->ciss_max_logical_bus; i++)
2596 ciss_cam_rescan_target(sc, i, CAM_TARGET_WILDCARD);
2597 /* Rescan the physical buses */
2598 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
2599 CISS_PHYSICAL_BASE; i++)
2600 ciss_cam_rescan_target(sc, i, CAM_TARGET_WILDCARD);
2604 ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2606 xpt_free_path(ccb->ccb_h.path);
2607 free(ccb, CISS_MALLOC_CLASS);
2610 /************************************************************************
2611 * Handle requests coming from CAM
2614 ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
2616 struct ciss_softc *sc;
2617 struct ccb_scsiio *csio;
2621 sc = cam_sim_softc(sim);
2622 bus = cam_sim_bus(sim);
2623 csio = (struct ccb_scsiio *)&ccb->csio;
2624 target = csio->ccb_h.target_id;
2625 physical = CISS_IS_PHYSICAL(bus);
2627 switch (ccb->ccb_h.func_code) {
2629 /* perform SCSI I/O */
2631 if (!ciss_cam_action_io(sim, csio))
2635 /* perform geometry calculations */
2636 case XPT_CALC_GEOMETRY:
2638 struct ccb_calc_geometry *ccg = &ccb->ccg;
2639 struct ciss_ldrive *ld;
2641 debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2645 ld = &sc->ciss_logical[bus][target];
2648 * Use the cached geometry settings unless the fault tolerance
2651 if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
2652 u_int32_t secs_per_cylinder;
2655 ccg->secs_per_track = 32;
2656 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2657 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2659 ccg->heads = ld->cl_geometry.heads;
2660 ccg->secs_per_track = ld->cl_geometry.sectors;
2661 ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2663 ccb->ccb_h.status = CAM_REQ_CMP;
2667 /* handle path attribute inquiry */
2670 struct ccb_pathinq *cpi = &ccb->cpi;
2672 debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2674 cpi->version_num = 1;
2675 cpi->hba_inquiry = PI_TAG_ABLE; /* XXX is this correct? */
2676 cpi->target_sprt = 0;
2678 cpi->max_target = CISS_MAX_LOGICAL;
2679 cpi->max_lun = 0; /* 'logical drive' channel only */
2680 cpi->initiator_id = CISS_MAX_LOGICAL;
2681 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2682 strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
2683 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2684 cpi->unit_number = cam_sim_unit(sim);
2685 cpi->bus_id = cam_sim_bus(sim);
2686 cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */
2687 cpi->transport = XPORT_SPI;
2688 cpi->transport_version = 2;
2689 cpi->protocol = PROTO_SCSI;
2690 cpi->protocol_version = SCSI_REV_2;
2691 ccb->ccb_h.status = CAM_REQ_CMP;
2695 case XPT_GET_TRAN_SETTINGS:
2697 struct ccb_trans_settings *cts = &ccb->cts;
2699 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
2700 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
2702 bus = cam_sim_bus(sim);
2703 target = cts->ccb_h.target_id;
2705 debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
2706 /* disconnect always OK */
2707 cts->protocol = PROTO_SCSI;
2708 cts->protocol_version = SCSI_REV_2;
2709 cts->transport = XPORT_SPI;
2710 cts->transport_version = 2;
2712 spi->valid = CTS_SPI_VALID_DISC;
2713 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
2715 scsi->valid = CTS_SCSI_VALID_TQ;
2716 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
2718 cts->ccb_h.status = CAM_REQ_CMP;
2722 default: /* we can't do this */
2723 debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
2724 ccb->ccb_h.status = CAM_REQ_INVALID;
2731 /************************************************************************
2732 * Handle a CAM SCSI I/O request.
2735 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
2737 struct ciss_softc *sc;
2739 struct ciss_request *cr;
2740 struct ciss_command *cc;
2743 sc = cam_sim_softc(sim);
2744 bus = cam_sim_bus(sim);
2745 target = csio->ccb_h.target_id;
2747 debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
2749 /* check that the CDB pointer is not to a physical address */
2750 if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
2751 debug(3, " CDB pointer is to physical address");
2752 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2755 /* if there is data transfer, it must be to/from a virtual address */
2756 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
2757 if (csio->ccb_h.flags & CAM_DATA_PHYS) { /* we can't map it */
2758 debug(3, " data pointer is to physical address");
2759 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2761 if (csio->ccb_h.flags & CAM_SCATTER_VALID) { /* we want to do the s/g setup */
2762 debug(3, " data has premature s/g setup");
2763 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2767 /* abandon aborted ccbs or those that have failed validation */
2768 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
2769 debug(3, "abandoning CCB due to abort/validation failure");
2773 /* handle emulation of some SCSI commands ourself */
2774 if (ciss_cam_emulate(sc, csio))
2778 * Get a request to manage this command. If we can't, return the
2779 * ccb, freeze the queue and flag so that we unfreeze it when a
2780 * request completes.
2782 if ((error = ciss_get_request(sc, &cr)) != 0) {
2783 xpt_freeze_simq(sim, 1);
2784 csio->ccb_h.status |= CAM_REQUEUE_REQ;
2789 * Build the command.
2791 cc = CISS_FIND_COMMAND(cr);
2792 cr->cr_data = csio->data_ptr;
2793 cr->cr_length = csio->dxfer_len;
2794 cr->cr_complete = ciss_cam_complete;
2795 cr->cr_private = csio;
2798 * Target the right logical volume.
2800 if (CISS_IS_PHYSICAL(bus))
2801 cc->header.address =
2802 sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
2804 cc->header.address =
2805 sc->ciss_logical[bus][target].cl_address;
2806 cc->cdb.cdb_length = csio->cdb_len;
2807 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2808 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; /* XXX ordered tags? */
2809 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2810 cr->cr_flags = CISS_REQ_DATAOUT;
2811 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
2812 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2813 cr->cr_flags = CISS_REQ_DATAIN;
2814 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
2817 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2819 cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
2820 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
2821 bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
2823 bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
2827 * Submit the request to the adapter.
2829 * Note that this may fail if we're unable to map the request (and
2830 * if we ever learn a transport layer other than simple, may fail
2831 * if the adapter rejects the command).
2833 if ((error = ciss_start(cr)) != 0) {
2834 xpt_freeze_simq(sim, 1);
2835 if (error == EINPROGRESS) {
2836 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
2839 csio->ccb_h.status |= CAM_REQUEUE_REQ;
2840 ciss_release_request(cr);
2848 /************************************************************************
2849 * Emulate SCSI commands the adapter doesn't handle as we might like.
2852 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
2857 target = csio->ccb_h.target_id;
2858 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
2859 opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
2860 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
2862 if (CISS_IS_PHYSICAL(bus)) {
2863 if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
2864 csio->ccb_h.status = CAM_SEL_TIMEOUT;
2865 xpt_done((union ccb *)csio);
2872 * Handle requests for volumes that don't exist or are not online.
2873 * A selection timeout is slightly better than an illegal request.
2874 * Other errors might be better.
2876 if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
2877 csio->ccb_h.status = CAM_SEL_TIMEOUT;
2878 xpt_done((union ccb *)csio);
2882 /* if we have to fake Synchronise Cache */
2883 if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
2885 * If this is a Synchronise Cache command, typically issued when
2886 * a device is closed, flush the adapter and complete now.
2888 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
2889 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
2890 ciss_flush_adapter(sc);
2891 csio->ccb_h.status = CAM_REQ_CMP;
2892 xpt_done((union ccb *)csio);
2900 /************************************************************************
2901 * Check for possibly-completed commands.
2904 ciss_cam_poll(struct cam_sim *sim)
2906 struct ciss_softc *sc = cam_sim_softc(sim);
2913 /************************************************************************
2914 * Handle completion of a command - pass results back through the CCB
2917 ciss_cam_complete(struct ciss_request *cr)
2919 struct ciss_softc *sc;
2920 struct ciss_command *cc;
2921 struct ciss_error_info *ce;
2922 struct ccb_scsiio *csio;
2929 cc = CISS_FIND_COMMAND(cr);
2930 ce = (struct ciss_error_info *)&(cc->sg[0]);
2931 csio = (struct ccb_scsiio *)cr->cr_private;
2934 * Extract status values from request.
2936 ciss_report_request(cr, &command_status, &scsi_status);
2937 csio->scsi_status = scsi_status;
2940 * Handle specific SCSI status values.
2942 switch(scsi_status) {
2943 /* no status due to adapter error */
2945 debug(0, "adapter error");
2946 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2949 /* no status due to command completed OK */
2950 case SCSI_STATUS_OK: /* CISS_SCSI_STATUS_GOOD */
2951 debug(2, "SCSI_STATUS_OK");
2952 csio->ccb_h.status = CAM_REQ_CMP;
2955 /* check condition, sense data included */
2956 case SCSI_STATUS_CHECK_COND: /* CISS_SCSI_STATUS_CHECK_CONDITION */
2957 debug(0, "SCSI_STATUS_CHECK_COND sense size %d resid %d\n",
2958 ce->sense_length, ce->residual_count);
2959 bzero(&csio->sense_data, SSD_FULL_SIZE);
2960 bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
2961 csio->sense_len = ce->sense_length;
2962 csio->resid = ce->residual_count;
2963 csio->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
2966 struct scsi_sense_data *sns = (struct scsi_sense_data *)&ce->sense_info[0];
2967 debug(0, "sense key %x", sns->flags & SSD_KEY);
2972 case SCSI_STATUS_BUSY: /* CISS_SCSI_STATUS_BUSY */
2973 debug(0, "SCSI_STATUS_BUSY");
2974 csio->ccb_h.status = CAM_SCSI_BUSY;
2978 debug(0, "unknown status 0x%x", csio->scsi_status);
2979 csio->ccb_h.status = CAM_REQ_CMP_ERR;
2983 /* handle post-command fixup */
2984 ciss_cam_complete_fixup(sc, csio);
2986 /* tell CAM we're ready for more commands */
2987 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
2989 xpt_done((union ccb *)csio);
2990 ciss_release_request(cr);
2993 /********************************************************************************
2994 * Fix up the result of some commands here.
2997 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
2999 struct scsi_inquiry_data *inq;
3000 struct ciss_ldrive *cl;
3003 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
3004 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == INQUIRY) {
3006 inq = (struct scsi_inquiry_data *)csio->data_ptr;
3007 target = csio->ccb_h.target_id;
3008 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3011 * Don't let hard drives be seen by the DA driver. They will still be
3012 * attached by the PASS driver.
3014 if (CISS_IS_PHYSICAL(bus)) {
3015 if (SID_TYPE(inq) == T_DIRECT)
3016 inq->device = (inq->device & 0xe0) | T_NODEVICE;
3020 cl = &sc->ciss_logical[bus][target];
3022 padstr(inq->vendor, "COMPAQ", 8);
3023 padstr(inq->product, ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance), 8);
3024 padstr(inq->revision, ciss_name_ldrive_status(cl->cl_lstatus->status), 16);
3029 /********************************************************************************
3030 * Find a peripheral attached at (target)
3032 static struct cam_periph *
3033 ciss_find_periph(struct ciss_softc *sc, int bus, int target)
3035 struct cam_periph *periph;
3036 struct cam_path *path;
3039 status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
3041 if (status == CAM_REQ_CMP) {
3042 periph = cam_periph_find(path, NULL);
3043 xpt_free_path(path);
3050 /********************************************************************************
3051 * Name the device at (target)
3053 * XXX is this strictly correct?
3056 ciss_name_device(struct ciss_softc *sc, int bus, int target)
3058 struct cam_periph *periph;
3060 if (CISS_IS_PHYSICAL(bus))
3062 if ((periph = ciss_find_periph(sc, bus, target)) != NULL) {
3063 sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
3064 periph->periph_name, periph->unit_number);
3067 sc->ciss_logical[bus][target].cl_name[0] = 0;
3071 /************************************************************************
3072 * Periodic status monitoring.
3075 ciss_periodic(void *arg)
3077 struct ciss_softc *sc;
3078 struct ciss_request *cr = NULL;
3079 struct ciss_command *cc = NULL;
3084 sc = (struct ciss_softc *)arg;
3087 * Check the adapter heartbeat.
3089 if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
3090 sc->ciss_heart_attack++;
3091 debug(0, "adapter heart attack in progress 0x%x/%d",
3092 sc->ciss_heartbeat, sc->ciss_heart_attack);
3093 if (sc->ciss_heart_attack == 3) {
3094 ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
3095 ciss_disable_adapter(sc);
3099 sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
3100 sc->ciss_heart_attack = 0;
3101 debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
3105 * Send the NOP message and wait for a response.
3107 if (ciss_nop_message_heartbeat != 0 && (error = ciss_get_request(sc, &cr)) == 0) {
3108 cc = CISS_FIND_COMMAND(cr);
3109 cr->cr_complete = ciss_nop_complete;
3110 cc->cdb.cdb_length = 1;
3111 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
3112 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3113 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3114 cc->cdb.timeout = 0;
3115 cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
3117 if ((error = ciss_start(cr)) != 0) {
3118 ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3123 * If the notify event request has died for some reason, or has
3124 * not started yet, restart it.
3126 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
3127 debug(0, "(re)starting Event Notify chain");
3128 ciss_notify_event(sc);
3134 callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
3138 ciss_nop_complete(struct ciss_request *cr)
3140 struct ciss_softc *sc;
3141 static int first_time = 1;
3144 if (ciss_report_request(cr, NULL, NULL) != 0) {
3145 if (first_time == 1) {
3147 ciss_printf(sc, "SENDING NOP MESSAGE FAILED (not logging anymore)\n");
3151 ciss_release_request(cr);
3154 /************************************************************************
3155 * Disable the adapter.
3157 * The all requests in completed queue is failed with hardware error.
3158 * This will cause failover in a multipath configuration.
3161 ciss_disable_adapter(struct ciss_softc *sc)
3163 struct ciss_request *cr;
3164 struct ciss_command *cc;
3165 struct ciss_error_info *ce;
3170 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
3171 pci_disable_busmaster(sc->ciss_dev);
3172 sc->ciss_flags &= ~CISS_FLAG_RUNNING;
3175 if ((cr = ciss_dequeue_busy(sc)) == NULL)
3178 cc = CISS_FIND_COMMAND(cr);
3179 ce = (struct ciss_error_info *)&(cc->sg[0]);
3180 ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
3181 ciss_enqueue_complete(cr);
3185 if ((cr = ciss_dequeue_complete(sc)) == NULL)
3189 * If the request has a callback, invoke it.
3191 if (cr->cr_complete != NULL) {
3192 cr->cr_complete(cr);
3197 * If someone is sleeping on this request, wake them up.
3199 if (cr->cr_flags & CISS_REQ_SLEEP) {
3200 cr->cr_flags &= ~CISS_REQ_SLEEP;
3209 /************************************************************************
3210 * Request a notification response from the adapter.
3212 * If (cr) is NULL, this is the first request of the adapter, so
3213 * reset the adapter's message pointer and start with the oldest
3214 * message available.
3217 ciss_notify_event(struct ciss_softc *sc)
3219 struct ciss_request *cr;
3220 struct ciss_command *cc;
3221 struct ciss_notify_cdb *cnc;
3226 cr = sc->ciss_periodic_notify;
3228 /* get a request if we don't already have one */
3230 if ((error = ciss_get_request(sc, &cr)) != 0) {
3231 debug(0, "can't get notify event request");
3234 sc->ciss_periodic_notify = cr;
3235 cr->cr_complete = ciss_notify_complete;
3236 debug(1, "acquired request %d", cr->cr_tag);
3240 * Get a databuffer if we don't already have one, note that the
3241 * adapter command wants a larger buffer than the actual
3244 if (cr->cr_data == NULL) {
3245 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3246 debug(0, "can't get notify event request buffer");
3250 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3253 /* re-setup the request's command (since we never release it) XXX overkill*/
3254 ciss_preen_command(cr);
3256 /* (re)build the notify event command */
3257 cc = CISS_FIND_COMMAND(cr);
3258 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3259 cc->header.address.physical.bus = 0;
3260 cc->header.address.physical.target = 0;
3262 cc->cdb.cdb_length = sizeof(*cnc);
3263 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3264 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3265 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3266 cc->cdb.timeout = 0; /* no timeout, we hope */
3268 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3269 bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
3270 cnc->opcode = CISS_OPCODE_READ;
3271 cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
3272 cnc->timeout = 0; /* no timeout, we hope */
3273 cnc->synchronous = 0;
3275 cnc->seek_to_oldest = 0;
3276 if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
3280 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3282 /* submit the request */
3283 error = ciss_start(cr);
3288 if (cr->cr_data != NULL)
3289 free(cr->cr_data, CISS_MALLOC_CLASS);
3290 ciss_release_request(cr);
3292 sc->ciss_periodic_notify = NULL;
3293 debug(0, "can't submit notify event request");
3294 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3296 debug(1, "notify event submitted");
3297 sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
3302 ciss_notify_complete(struct ciss_request *cr)
3304 struct ciss_command *cc;
3305 struct ciss_notify *cn;
3306 struct ciss_softc *sc;
3311 cc = CISS_FIND_COMMAND(cr);
3312 cn = (struct ciss_notify *)cr->cr_data;
3316 * Report request results, decode status.
3318 ciss_report_request(cr, &command_status, &scsi_status);
3321 * Abort the chain on a fatal error.
3323 * XXX which of these are actually errors?
3325 if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
3326 (command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
3327 (command_status != CISS_CMD_STATUS_TIMEOUT)) { /* XXX timeout? */
3328 ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
3329 ciss_name_command_status(command_status));
3330 ciss_release_request(cr);
3331 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3336 * If the adapter gave us a text message, print it.
3338 if (cn->message[0] != 0)
3339 ciss_printf(sc, "*** %.80s\n", cn->message);
3341 debug(0, "notify event class %d subclass %d detail %d",
3342 cn->class, cn->subclass, cn->detail);
3345 * If the response indicates that the notifier has been aborted,
3346 * release the notifier command.
3348 if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
3349 (cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
3350 (cn->detail == 1)) {
3351 debug(0, "notifier exiting");
3352 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3353 ciss_release_request(cr);
3354 sc->ciss_periodic_notify = NULL;
3355 wakeup(&sc->ciss_periodic_notify);
3357 /* Handle notify events in a kernel thread */
3358 ciss_enqueue_notify(cr);
3359 sc->ciss_periodic_notify = NULL;
3360 wakeup(&sc->ciss_periodic_notify);
3361 wakeup(&sc->ciss_notify);
3364 * Send a new notify event command, if we're not aborting.
3366 if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
3367 ciss_notify_event(sc);
3371 /************************************************************************
3372 * Abort the Notify Event chain.
3374 * Note that we can't just abort the command in progress; we have to
3375 * explicitly issue an Abort Notify Event command in order for the
3376 * adapter to clean up correctly.
3378 * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
3379 * the chain will not restart itself.
3382 ciss_notify_abort(struct ciss_softc *sc)
3384 struct ciss_request *cr;
3385 struct ciss_command *cc;
3386 struct ciss_notify_cdb *cnc;
3387 int error, s, command_status, scsi_status;
3394 /* verify that there's an outstanding command */
3395 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3398 /* get a command to issue the abort with */
3399 if ((error = ciss_get_request(sc, &cr)))
3402 /* get a buffer for the result */
3403 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3404 debug(0, "can't get notify event request buffer");
3408 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3411 cc = CISS_FIND_COMMAND(cr);
3412 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3413 cc->header.address.physical.bus = 0;
3414 cc->header.address.physical.target = 0;
3415 cc->cdb.cdb_length = sizeof(*cnc);
3416 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3417 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3418 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3419 cc->cdb.timeout = 0; /* no timeout, we hope */
3421 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3422 bzero(cnc, sizeof(*cnc));
3423 cnc->opcode = CISS_OPCODE_WRITE;
3424 cnc->command = CISS_COMMAND_ABORT_NOTIFY;
3425 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3427 ciss_print_request(cr);
3430 * Submit the request and wait for it to complete.
3432 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3433 ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
3440 ciss_report_request(cr, &command_status, &scsi_status);
3441 switch(command_status) {
3442 case CISS_CMD_STATUS_SUCCESS:
3444 case CISS_CMD_STATUS_INVALID_COMMAND:
3446 * Some older adapters don't support the CISS version of this
3447 * command. Fall back to using the BMIC version.
3449 error = ciss_notify_abort_bmic(sc);
3454 case CISS_CMD_STATUS_TARGET_STATUS:
3456 * This can happen if the adapter thinks there wasn't an outstanding
3457 * Notify Event command but we did. We clean up here.
3459 if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
3460 if (sc->ciss_periodic_notify != NULL)
3461 ciss_release_request(sc->ciss_periodic_notify);
3468 ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
3469 ciss_name_command_status(command_status));
3475 * Sleep waiting for the notifier command to complete. Note
3476 * that if it doesn't, we may end up in a bad situation, since
3477 * the adapter may deliver it later. Also note that the adapter
3478 * requires the Notify Event command to be cancelled in order to
3479 * maintain internal bookkeeping.
3482 while (sc->ciss_periodic_notify != NULL) {
3483 error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
3484 if (error == EWOULDBLOCK) {
3485 ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
3492 /* release the cancel request */
3494 if (cr->cr_data != NULL)
3495 free(cr->cr_data, CISS_MALLOC_CLASS);
3496 ciss_release_request(cr);
3499 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3503 /************************************************************************
3504 * Abort the Notify Event chain using a BMIC command.
3507 ciss_notify_abort_bmic(struct ciss_softc *sc)
3509 struct ciss_request *cr;
3510 int error, command_status;
3517 /* verify that there's an outstanding command */
3518 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3522 * Build a BMIC command to cancel the Notify on Event command.
3524 * Note that we are sending a CISS opcode here. Odd.
3526 if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
3531 * Submit the request and wait for it to complete.
3533 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3534 ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
3541 ciss_report_request(cr, &command_status, NULL);
3542 switch(command_status) {
3543 case CISS_CMD_STATUS_SUCCESS:
3546 ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
3547 ciss_name_command_status(command_status));
3554 ciss_release_request(cr);
3558 /************************************************************************
3559 * Handle rescanning all the logical volumes when a notify event
3560 * causes the drives to come online or offline.
3563 ciss_notify_rescan_logical(struct ciss_softc *sc)
3565 struct ciss_lun_report *cll;
3566 struct ciss_ldrive *ld;
3570 * We must rescan all logical volumes to get the right logical
3573 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
3578 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
3581 * Delete any of the drives which were destroyed by the
3584 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
3585 for (j = 0; j < CISS_MAX_LOGICAL; j++) {
3586 ld = &sc->ciss_logical[i][j];
3588 if (ld->cl_update == 0)
3591 if (ld->cl_status != CISS_LD_ONLINE) {
3592 ciss_cam_rescan_target(sc, i, j);
3595 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
3597 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
3599 ld->cl_ldrive = NULL;
3600 ld->cl_lstatus = NULL;
3606 * Scan for new drives.
3608 for (i = 0; i < ndrives; i++) {
3611 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
3612 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
3613 ld = &sc->ciss_logical[bus][target];
3615 if (ld->cl_update == 0)
3619 ld->cl_address = cll->lun[i];
3620 ld->cl_controller = &sc->ciss_controllers[bus];
3621 if (ciss_identify_logical(sc, ld) == 0) {
3622 ciss_cam_rescan_target(sc, bus, target);
3625 free(cll, CISS_MALLOC_CLASS);
3628 /************************************************************************
3629 * Handle a notify event relating to the status of a logical drive.
3631 * XXX need to be able to defer some of these to properly handle
3632 * calling the "ID Physical drive" command, unless the 'extended'
3633 * drive IDs are always in BIG_MAP format.
3636 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
3638 struct ciss_ldrive *ld;
3639 int ostatus, bus, target;
3643 bus = cn->device.physical.bus;
3644 target = cn->data.logical_status.logical_drive;
3645 ld = &sc->ciss_logical[bus][target];
3647 switch (cn->subclass) {
3648 case CISS_NOTIFY_LOGICAL_STATUS:
3649 switch (cn->detail) {
3651 ciss_name_device(sc, bus, target);
3652 ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
3653 cn->data.logical_status.logical_drive, ld->cl_name,
3654 ciss_name_ldrive_status(cn->data.logical_status.previous_state),
3655 ciss_name_ldrive_status(cn->data.logical_status.new_state),
3656 cn->data.logical_status.spare_state,
3657 "\20\1configured\2rebuilding\3failed\4in use\5available\n");
3660 * Update our idea of the drive's status.
3662 ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
3663 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3664 if (ld->cl_lstatus != NULL)
3665 ld->cl_lstatus->status = cn->data.logical_status.new_state;
3668 * Have CAM rescan the drive if its status has changed.
3670 if (ostatus != ld->cl_status) {
3672 ciss_notify_rescan_logical(sc);
3677 case 1: /* logical drive has recognised new media, needs Accept Media Exchange */
3678 ciss_name_device(sc, bus, target);
3679 ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
3680 cn->data.logical_status.logical_drive, ld->cl_name);
3681 ciss_accept_media(sc, ld);
3684 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3685 ciss_notify_rescan_logical(sc);
3690 ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
3691 cn->data.rebuild_aborted.logical_drive,
3693 (cn->detail == 2) ? "read" : "write");
3698 case CISS_NOTIFY_LOGICAL_ERROR:
3699 if (cn->detail == 0) {
3700 ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
3701 cn->data.io_error.logical_drive,
3703 cn->data.io_error.failure_bus,
3704 cn->data.io_error.failure_drive);
3705 /* XXX should we take the drive down at this point, or will we be told? */
3709 case CISS_NOTIFY_LOGICAL_SURFACE:
3710 if (cn->detail == 0)
3711 ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
3712 cn->data.consistency_completed.logical_drive,
3718 /************************************************************************
3719 * Handle a notify event relating to the status of a physical drive.
3722 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
3726 /************************************************************************
3727 * Handle a notify event relating to the status of a physical drive.
3730 ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
3732 struct ciss_lun_report *cll = NULL;
3736 switch (cn->subclass) {
3737 case CISS_NOTIFY_HOTPLUG_PHYSICAL:
3738 case CISS_NOTIFY_HOTPLUG_NONDISK:
3739 bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
3741 CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
3744 if (cn->detail == 0) {
3746 * Mark the device offline so that it'll start producing selection
3747 * timeouts to the upper layer.
3749 if ((bus >= 0) && (target >= 0))
3750 sc->ciss_physical[bus][target].cp_online = 0;
3753 * Rescan the physical lun list for new items
3755 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
3758 ciss_printf(sc, "Warning, cannot get physical lun list\n");
3761 ciss_filter_physical(sc, cll);
3767 ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
3772 free(cll, CISS_MALLOC_CLASS);
3775 /************************************************************************
3776 * Handle deferred processing of notify events. Notify events may need
3777 * sleep which is unsafe during an interrupt.
3780 ciss_notify_thread(void *arg)
3782 struct ciss_softc *sc;
3783 struct ciss_request *cr;
3784 struct ciss_notify *cn;
3787 sc = (struct ciss_softc *)arg;
3788 #if __FreeBSD_version >= 500000
3789 mtx_lock(&sc->ciss_mtx);
3794 if (TAILQ_EMPTY(&sc->ciss_notify) != 0 &&
3795 (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
3796 msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
3799 if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
3802 cr = ciss_dequeue_notify(sc);
3807 cn = (struct ciss_notify *)cr->cr_data;
3809 switch (cn->class) {
3810 case CISS_NOTIFY_HOTPLUG:
3811 ciss_notify_hotplug(sc, cn);
3813 case CISS_NOTIFY_LOGICAL:
3814 ciss_notify_logical(sc, cn);
3816 case CISS_NOTIFY_PHYSICAL:
3817 ciss_notify_physical(sc, cn);
3821 ciss_release_request(cr);
3825 sc->ciss_notify_thread = NULL;
3826 wakeup(&sc->ciss_notify_thread);
3829 #if __FreeBSD_version >= 500000
3830 mtx_unlock(&sc->ciss_mtx);
3835 /************************************************************************
3836 * Start the notification kernel thread.
3839 ciss_spawn_notify_thread(struct ciss_softc *sc)
3842 #if __FreeBSD_version > 500005
3843 if (kthread_create((void(*)(void *))ciss_notify_thread, sc,
3844 &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
3845 device_get_unit(sc->ciss_dev)))
3847 if (kthread_create((void(*)(void *))ciss_notify_thread, sc,
3848 &sc->ciss_notify_thread, "ciss_notify%d",
3849 device_get_unit(sc->ciss_dev)))
3851 panic("Could not create notify thread\n");
3854 /************************************************************************
3855 * Kill the notification kernel thread.
3858 ciss_kill_notify_thread(struct ciss_softc *sc)
3861 if (sc->ciss_notify_thread == NULL)
3864 sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
3865 wakeup(&sc->ciss_notify);
3866 msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
3869 /************************************************************************
3873 ciss_print_request(struct ciss_request *cr)
3875 struct ciss_softc *sc;
3876 struct ciss_command *cc;
3880 cc = CISS_FIND_COMMAND(cr);
3882 ciss_printf(sc, "REQUEST @ %p\n", cr);
3883 ciss_printf(sc, " data %p/%d tag %d flags %b\n",
3884 cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
3885 "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
3886 ciss_printf(sc, " sg list/total %d/%d host tag 0x%x\n",
3887 cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
3888 switch(cc->header.address.mode.mode) {
3889 case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
3890 case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
3891 ciss_printf(sc, " physical bus %d target %d\n",
3892 cc->header.address.physical.bus, cc->header.address.physical.target);
3894 case CISS_HDR_ADDRESS_MODE_LOGICAL:
3895 ciss_printf(sc, " logical unit %d\n", cc->header.address.logical.lun);
3898 ciss_printf(sc, " %s cdb length %d type %s attribute %s\n",
3899 (cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
3900 (cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
3901 (cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
3903 (cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
3904 (cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
3905 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
3906 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
3907 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
3908 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
3909 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
3910 ciss_printf(sc, " %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
3912 if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
3913 /* XXX print error info */
3915 /* since we don't use chained s/g, don't support it here */
3916 for (i = 0; i < cc->header.sg_in_list; i++) {
3918 ciss_printf(sc, " ");
3919 printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
3920 if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
3926 /************************************************************************
3927 * Print information about the status of a logical drive.
3930 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
3934 if (ld->cl_lstatus == NULL) {
3935 printf("does not exist\n");
3939 /* print drive status */
3940 switch(ld->cl_lstatus->status) {
3941 case CISS_LSTATUS_OK:
3944 case CISS_LSTATUS_INTERIM_RECOVERY:
3945 printf("in interim recovery mode\n");
3947 case CISS_LSTATUS_READY_RECOVERY:
3948 printf("ready to begin recovery\n");
3950 case CISS_LSTATUS_RECOVERING:
3951 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
3952 target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
3953 printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
3954 bus, target, ld->cl_lstatus->blocks_to_recover);
3956 case CISS_LSTATUS_EXPANDING:
3957 printf("being expanded, %u blocks remaining\n",
3958 ld->cl_lstatus->blocks_to_recover);
3960 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
3961 printf("queued for expansion\n");
3963 case CISS_LSTATUS_FAILED:
3964 printf("queued for expansion\n");
3966 case CISS_LSTATUS_WRONG_PDRIVE:
3967 printf("wrong physical drive inserted\n");
3969 case CISS_LSTATUS_MISSING_PDRIVE:
3970 printf("missing a needed physical drive\n");
3972 case CISS_LSTATUS_BECOMING_READY:
3973 printf("becoming ready\n");
3977 /* print failed physical drives */
3978 for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
3979 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
3980 target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
3983 ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
3984 ld->cl_lstatus->drive_failure_map[i]);
3989 /************************************************************************
3990 * Print information about the controller/driver.
3993 ciss_print_adapter(struct ciss_softc *sc)
3997 ciss_printf(sc, "ADAPTER:\n");
3998 for (i = 0; i < CISSQ_COUNT; i++) {
3999 ciss_printf(sc, "%s %d/%d\n",
4001 i == 1 ? "busy" : "complete",
4002 sc->ciss_qstat[i].q_length,
4003 sc->ciss_qstat[i].q_max);
4005 ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
4006 ciss_printf(sc, "flags %b\n", sc->ciss_flags,
4007 "\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
4009 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
4010 for (j = 0; j < CISS_MAX_LOGICAL; j++) {
4011 ciss_printf(sc, "LOGICAL DRIVE %d: ", i);
4012 ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
4016 /* XXX Should physical drives be printed out here? */
4018 for (i = 1; i < sc->ciss_max_requests; i++)
4019 ciss_print_request(sc->ciss_request + i);
4026 struct ciss_softc *sc;
4028 sc = devclass_get_softc(devclass_find("ciss"), 0);
4030 printf("no ciss controllers\n");
4032 ciss_print_adapter(sc);
4037 /************************************************************************
4038 * Return a name for a logical drive status value.
4041 ciss_name_ldrive_status(int status)
4044 case CISS_LSTATUS_OK:
4046 case CISS_LSTATUS_FAILED:
4048 case CISS_LSTATUS_NOT_CONFIGURED:
4049 return("not configured");
4050 case CISS_LSTATUS_INTERIM_RECOVERY:
4051 return("interim recovery");
4052 case CISS_LSTATUS_READY_RECOVERY:
4053 return("ready for recovery");
4054 case CISS_LSTATUS_RECOVERING:
4055 return("recovering");
4056 case CISS_LSTATUS_WRONG_PDRIVE:
4057 return("wrong physical drive inserted");
4058 case CISS_LSTATUS_MISSING_PDRIVE:
4059 return("missing physical drive");
4060 case CISS_LSTATUS_EXPANDING:
4061 return("expanding");
4062 case CISS_LSTATUS_BECOMING_READY:
4063 return("becoming ready");
4064 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4065 return("queued for expansion");
4067 return("unknown status");
4070 /************************************************************************
4071 * Return an online/offline/nonexistent value for a logical drive
4075 ciss_decode_ldrive_status(int status)
4078 case CISS_LSTATUS_NOT_CONFIGURED:
4079 return(CISS_LD_NONEXISTENT);
4081 case CISS_LSTATUS_OK:
4082 case CISS_LSTATUS_INTERIM_RECOVERY:
4083 case CISS_LSTATUS_READY_RECOVERY:
4084 case CISS_LSTATUS_RECOVERING:
4085 case CISS_LSTATUS_EXPANDING:
4086 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4087 return(CISS_LD_ONLINE);
4089 case CISS_LSTATUS_FAILED:
4090 case CISS_LSTATUS_WRONG_PDRIVE:
4091 case CISS_LSTATUS_MISSING_PDRIVE:
4092 case CISS_LSTATUS_BECOMING_READY:
4094 return(CISS_LD_OFFLINE);
4099 /************************************************************************
4100 * Return a name for a logical drive's organisation.
4103 ciss_name_ldrive_org(int org)
4106 case CISS_LDRIVE_RAID0:
4108 case CISS_LDRIVE_RAID1:
4110 case CISS_LDRIVE_RAID4:
4112 case CISS_LDRIVE_RAID5:
4114 case CISS_LDRIVE_RAID51:
4116 case CISS_LDRIVE_RAIDADG:
4122 /************************************************************************
4123 * Return a name for a command status value.
4126 ciss_name_command_status(int status)
4129 case CISS_CMD_STATUS_SUCCESS:
4131 case CISS_CMD_STATUS_TARGET_STATUS:
4132 return("target status");
4133 case CISS_CMD_STATUS_DATA_UNDERRUN:
4134 return("data underrun");
4135 case CISS_CMD_STATUS_DATA_OVERRUN:
4136 return("data overrun");
4137 case CISS_CMD_STATUS_INVALID_COMMAND:
4138 return("invalid command");
4139 case CISS_CMD_STATUS_PROTOCOL_ERROR:
4140 return("protocol error");
4141 case CISS_CMD_STATUS_HARDWARE_ERROR:
4142 return("hardware error");
4143 case CISS_CMD_STATUS_CONNECTION_LOST:
4144 return("connection lost");
4145 case CISS_CMD_STATUS_ABORTED:
4147 case CISS_CMD_STATUS_ABORT_FAILED:
4148 return("abort failed");
4149 case CISS_CMD_STATUS_UNSOLICITED_ABORT:
4150 return("unsolicited abort");
4151 case CISS_CMD_STATUS_TIMEOUT:
4153 case CISS_CMD_STATUS_UNABORTABLE:
4154 return("unabortable");
4156 return("unknown status");
4159 /************************************************************************
4160 * Handle an open on the control device.
4163 ciss_open(struct cdev *dev, int flags, int fmt, d_thread_t *p)
4165 struct ciss_softc *sc;
4169 sc = (struct ciss_softc *)dev->si_drv1;
4171 /* we might want to veto if someone already has us open */
4173 mtx_lock(&sc->ciss_mtx);
4174 sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
4175 mtx_unlock(&sc->ciss_mtx);
4179 /************************************************************************
4180 * Handle the last close on the control device.
4183 ciss_close(struct cdev *dev, int flags, int fmt, d_thread_t *p)
4185 struct ciss_softc *sc;
4189 sc = (struct ciss_softc *)dev->si_drv1;
4191 mtx_lock(&sc->ciss_mtx);
4192 sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
4193 mtx_unlock(&sc->ciss_mtx);
4197 /********************************************************************************
4198 * Handle adapter-specific control operations.
4200 * Note that the API here is compatible with the Linux driver, in order to
4201 * simplify the porting of Compaq's userland tools.
4204 ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *p)
4206 struct ciss_softc *sc;
4207 IOCTL_Command_struct *ioc = (IOCTL_Command_struct *)addr;
4209 IOCTL_Command_struct32 *ioc32 = (IOCTL_Command_struct32 *)addr;
4210 IOCTL_Command_struct ioc_swab;
4216 sc = (struct ciss_softc *)dev->si_drv1;
4218 mtx_lock(&sc->ciss_mtx);
4221 case CCISS_GETPCIINFO:
4223 cciss_pci_info_struct *pis = (cciss_pci_info_struct *)addr;
4225 pis->bus = pci_get_bus(sc->ciss_dev);
4226 pis->dev_fn = pci_get_slot(sc->ciss_dev);
4227 pis->board_id = pci_get_devid(sc->ciss_dev);
4232 case CCISS_GETINTINFO:
4234 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4236 cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
4237 cis->count = sc->ciss_cfg->interrupt_coalesce_count;
4242 case CCISS_SETINTINFO:
4244 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4246 if ((cis->delay == 0) && (cis->count == 0)) {
4252 * XXX apparently this is only safe if the controller is idle,
4253 * we should suspend it before doing this.
4255 sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
4256 sc->ciss_cfg->interrupt_coalesce_count = cis->count;
4258 if (ciss_update_config(sc))
4261 /* XXX resume the controller here */
4265 case CCISS_GETNODENAME:
4266 bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
4267 sizeof(NodeName_type));
4270 case CCISS_SETNODENAME:
4271 bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
4272 sizeof(NodeName_type));
4273 if (ciss_update_config(sc))
4277 case CCISS_GETHEARTBEAT:
4278 *(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
4281 case CCISS_GETBUSTYPES:
4282 *(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
4285 case CCISS_GETFIRMVER:
4286 bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
4287 sizeof(FirmwareVer_type));
4290 case CCISS_GETDRIVERVER:
4291 *(DriverVer_type *)addr = CISS_DRIVER_VERSION;
4294 case CCISS_REVALIDVOLS:
4296 * This is a bit ugly; to do it "right" we really need
4297 * to find any disks that have changed, kick CAM off them,
4298 * then rescan only these disks. It'd be nice if they
4299 * a) told us which disk(s) they were going to play with,
4300 * and b) which ones had arrived. 8(
4305 case CCISS_PASSTHRU32:
4306 ioc_swab.LUN_info = ioc32->LUN_info;
4307 ioc_swab.Request = ioc32->Request;
4308 ioc_swab.error_info = ioc32->error_info;
4309 ioc_swab.buf_size = ioc32->buf_size;
4310 ioc_swab.buf = (u_int8_t *)(uintptr_t)ioc32->buf;
4315 case CCISS_PASSTHRU:
4316 error = ciss_user_command(sc, ioc);
4320 debug(0, "unknown ioctl 0x%lx", cmd);
4322 debug(1, "CCISS_GETPCIINFO: 0x%lx", CCISS_GETPCIINFO);
4323 debug(1, "CCISS_GETINTINFO: 0x%lx", CCISS_GETINTINFO);
4324 debug(1, "CCISS_SETINTINFO: 0x%lx", CCISS_SETINTINFO);
4325 debug(1, "CCISS_GETNODENAME: 0x%lx", CCISS_GETNODENAME);
4326 debug(1, "CCISS_SETNODENAME: 0x%lx", CCISS_SETNODENAME);
4327 debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
4328 debug(1, "CCISS_GETBUSTYPES: 0x%lx", CCISS_GETBUSTYPES);
4329 debug(1, "CCISS_GETFIRMVER: 0x%lx", CCISS_GETFIRMVER);
4330 debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
4331 debug(1, "CCISS_REVALIDVOLS: 0x%lx", CCISS_REVALIDVOLS);
4332 debug(1, "CCISS_PASSTHRU: 0x%lx", CCISS_PASSTHRU);
4338 mtx_unlock(&sc->ciss_mtx);