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/cissio.h>
106 #include <dev/ciss/cissvar.h>
108 static MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data",
109 "ciss internal data buffers");
112 static int ciss_lookup(device_t dev);
113 static int ciss_probe(device_t dev);
114 static int ciss_attach(device_t dev);
115 static int ciss_detach(device_t dev);
116 static int ciss_shutdown(device_t dev);
118 /* (de)initialisation functions, control wrappers */
119 static int ciss_init_pci(struct ciss_softc *sc);
120 static int ciss_setup_msix(struct ciss_softc *sc);
121 static int ciss_init_perf(struct ciss_softc *sc);
122 static int ciss_wait_adapter(struct ciss_softc *sc);
123 static int ciss_flush_adapter(struct ciss_softc *sc);
124 static int ciss_init_requests(struct ciss_softc *sc);
125 static void ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
126 int nseg, int error);
127 static int ciss_identify_adapter(struct ciss_softc *sc);
128 static int ciss_init_logical(struct ciss_softc *sc);
129 static int ciss_init_physical(struct ciss_softc *sc);
130 static int ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll);
131 static int ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
132 static int ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld);
133 static int ciss_update_config(struct ciss_softc *sc);
134 static int ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld);
135 static void ciss_init_sysctl(struct ciss_softc *sc);
136 static void ciss_soft_reset(struct ciss_softc *sc);
137 static void ciss_free(struct ciss_softc *sc);
138 static void ciss_spawn_notify_thread(struct ciss_softc *sc);
139 static void ciss_kill_notify_thread(struct ciss_softc *sc);
141 /* request submission/completion */
142 static int ciss_start(struct ciss_request *cr);
143 static void ciss_done(struct ciss_softc *sc, cr_qhead_t *qh);
144 static void ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh);
145 static void ciss_intr(void *arg);
146 static void ciss_perf_intr(void *arg);
147 static void ciss_perf_msi_intr(void *arg);
148 static void ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh);
149 static int _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func);
150 static int ciss_synch_request(struct ciss_request *cr, int timeout);
151 static int ciss_poll_request(struct ciss_request *cr, int timeout);
152 static int ciss_wait_request(struct ciss_request *cr, int timeout);
154 static int ciss_abort_request(struct ciss_request *cr);
157 /* request queueing */
158 static int ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
159 static void ciss_preen_command(struct ciss_request *cr);
160 static void ciss_release_request(struct ciss_request *cr);
162 /* request helpers */
163 static int ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
164 int opcode, void **bufp, size_t bufsize);
165 static int ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
168 static int ciss_map_request(struct ciss_request *cr);
169 static void ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
170 int nseg, int error);
171 static void ciss_unmap_request(struct ciss_request *cr);
174 static int ciss_cam_init(struct ciss_softc *sc);
175 static void ciss_cam_rescan_target(struct ciss_softc *sc,
176 int bus, int target);
177 static void ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
178 static int ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
179 static int ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
180 static void ciss_cam_poll(struct cam_sim *sim);
181 static void ciss_cam_complete(struct ciss_request *cr);
182 static void ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
183 static struct cam_periph *ciss_find_periph(struct ciss_softc *sc,
184 int bus, int target);
185 static int ciss_name_device(struct ciss_softc *sc, int bus, int target);
187 /* periodic status monitoring */
188 static void ciss_periodic(void *arg);
189 static void ciss_nop_complete(struct ciss_request *cr);
190 static void ciss_disable_adapter(struct ciss_softc *sc);
191 static void ciss_notify_event(struct ciss_softc *sc);
192 static void ciss_notify_complete(struct ciss_request *cr);
193 static int ciss_notify_abort(struct ciss_softc *sc);
194 static int ciss_notify_abort_bmic(struct ciss_softc *sc);
195 static void ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
196 static void ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
197 static void ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
199 /* debugging output */
200 static void ciss_print_request(struct ciss_request *cr);
201 static void ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
202 static const char *ciss_name_ldrive_status(int status);
203 static int ciss_decode_ldrive_status(int status);
204 static const char *ciss_name_ldrive_org(int org);
205 static const char *ciss_name_command_status(int status);
210 static device_method_t ciss_methods[] = {
211 /* Device interface */
212 DEVMETHOD(device_probe, ciss_probe),
213 DEVMETHOD(device_attach, ciss_attach),
214 DEVMETHOD(device_detach, ciss_detach),
215 DEVMETHOD(device_shutdown, ciss_shutdown),
219 static driver_t ciss_pci_driver = {
222 sizeof(struct ciss_softc)
225 static devclass_t ciss_devclass;
226 DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
227 MODULE_DEPEND(ciss, cam, 1, 1, 1);
228 MODULE_DEPEND(ciss, pci, 1, 1, 1);
231 * Control device interface.
233 static d_open_t ciss_open;
234 static d_close_t ciss_close;
235 static d_ioctl_t ciss_ioctl;
237 static struct cdevsw ciss_cdevsw = {
238 .d_version = D_VERSION,
241 .d_close = ciss_close,
242 .d_ioctl = ciss_ioctl,
247 * This tunable can be set at boot time and controls whether physical devices
248 * that are marked hidden by the firmware should be exposed anyways.
250 static unsigned int ciss_expose_hidden_physical = 0;
251 TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
253 static unsigned int ciss_nop_message_heartbeat = 0;
254 TUNABLE_INT("hw.ciss.nop_message_heartbeat", &ciss_nop_message_heartbeat);
257 * This tunable can force a particular transport to be used:
260 * 2 : force performant
262 static int ciss_force_transport = 0;
263 TUNABLE_INT("hw.ciss.force_transport", &ciss_force_transport);
266 * This tunable can force a particular interrupt delivery method to be used:
271 static int ciss_force_interrupt = 0;
272 TUNABLE_INT("hw.ciss.force_interrupt", &ciss_force_interrupt);
274 /************************************************************************
275 * CISS adapters amazingly don't have a defined programming interface
276 * value. (One could say some very despairing things about PCI and
277 * people just not getting the general idea.) So we are forced to
278 * stick with matching against subvendor/subdevice, and thus have to
279 * be updated for every new CISS adapter that appears.
281 #define CISS_BOARD_UNKNWON 0
282 #define CISS_BOARD_SA5 1
283 #define CISS_BOARD_SA5B 2
284 #define CISS_BOARD_NOMSI (1<<4)
285 #define CISS_BOARD_SIMPLE (1<<5)
293 } ciss_vendor_data[] = {
294 { 0x0e11, 0x4070, CISS_BOARD_SA5|CISS_BOARD_NOMSI|CISS_BOARD_SIMPLE,
295 "Compaq Smart Array 5300" },
296 { 0x0e11, 0x4080, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "Compaq Smart Array 5i" },
297 { 0x0e11, 0x4082, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "Compaq Smart Array 532" },
298 { 0x0e11, 0x4083, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "HP Smart Array 5312" },
299 { 0x0e11, 0x4091, CISS_BOARD_SA5, "HP Smart Array 6i" },
300 { 0x0e11, 0x409A, CISS_BOARD_SA5, "HP Smart Array 641" },
301 { 0x0e11, 0x409B, CISS_BOARD_SA5, "HP Smart Array 642" },
302 { 0x0e11, 0x409C, CISS_BOARD_SA5, "HP Smart Array 6400" },
303 { 0x0e11, 0x409D, CISS_BOARD_SA5, "HP Smart Array 6400 EM" },
304 { 0x103C, 0x3211, CISS_BOARD_SA5, "HP Smart Array E200i" },
305 { 0x103C, 0x3212, CISS_BOARD_SA5, "HP Smart Array E200" },
306 { 0x103C, 0x3213, CISS_BOARD_SA5, "HP Smart Array E200i" },
307 { 0x103C, 0x3214, CISS_BOARD_SA5, "HP Smart Array E200i" },
308 { 0x103C, 0x3215, CISS_BOARD_SA5, "HP Smart Array E200i" },
309 { 0x103C, 0x3220, CISS_BOARD_SA5, "HP Smart Array" },
310 { 0x103C, 0x3222, CISS_BOARD_SA5, "HP Smart Array" },
311 { 0x103C, 0x3223, CISS_BOARD_SA5, "HP Smart Array P800" },
312 { 0x103C, 0x3225, CISS_BOARD_SA5, "HP Smart Array P600" },
313 { 0x103C, 0x3230, CISS_BOARD_SA5, "HP Smart Array" },
314 { 0x103C, 0x3231, CISS_BOARD_SA5, "HP Smart Array" },
315 { 0x103C, 0x3232, CISS_BOARD_SA5, "HP Smart Array" },
316 { 0x103C, 0x3233, CISS_BOARD_SA5, "HP Smart Array" },
317 { 0x103C, 0x3234, CISS_BOARD_SA5, "HP Smart Array P400" },
318 { 0x103C, 0x3235, CISS_BOARD_SA5, "HP Smart Array P400i" },
319 { 0x103C, 0x3236, CISS_BOARD_SA5, "HP Smart Array" },
320 { 0x103C, 0x3237, CISS_BOARD_SA5, "HP Smart Array E500" },
321 { 0x103C, 0x3238, CISS_BOARD_SA5, "HP Smart Array" },
322 { 0x103C, 0x3239, CISS_BOARD_SA5, "HP Smart Array" },
323 { 0x103C, 0x323A, CISS_BOARD_SA5, "HP Smart Array" },
324 { 0x103C, 0x323B, CISS_BOARD_SA5, "HP Smart Array" },
325 { 0x103C, 0x323C, CISS_BOARD_SA5, "HP Smart Array" },
326 { 0x103C, 0x323D, CISS_BOARD_SA5, "HP Smart Array P700m" },
327 { 0x103C, 0x3241, CISS_BOARD_SA5, "HP Smart Array P212" },
328 { 0x103C, 0x3243, CISS_BOARD_SA5, "HP Smart Array P410" },
329 { 0x103C, 0x3245, CISS_BOARD_SA5, "HP Smart Array P410i" },
330 { 0x103C, 0x3247, CISS_BOARD_SA5, "HP Smart Array P411" },
331 { 0x103C, 0x3249, CISS_BOARD_SA5, "HP Smart Array P812" },
332 { 0x103C, 0x324A, CISS_BOARD_SA5, "HP Smart Array P712m" },
333 { 0x103C, 0x324B, CISS_BOARD_SA5, "HP Smart Array" },
334 { 0x103C, 0x3350, CISS_BOARD_SA5, "HP Smart Array P222" },
335 { 0x103C, 0x3351, CISS_BOARD_SA5, "HP Smart Array P420" },
336 { 0x103C, 0x3352, CISS_BOARD_SA5, "HP Smart Array P421" },
337 { 0x103C, 0x3353, CISS_BOARD_SA5, "HP Smart Array P822" },
338 { 0x103C, 0x3354, CISS_BOARD_SA5, "HP Smart Array P420i" },
339 { 0x103C, 0x3355, CISS_BOARD_SA5, "HP Smart Array P220i" },
340 { 0x103C, 0x3356, CISS_BOARD_SA5, "HP Smart Array P721m" },
341 { 0x103C, 0x1920, CISS_BOARD_SA5, "HP Smart Array P430i" },
342 { 0x103C, 0x1921, CISS_BOARD_SA5, "HP Smart Array P830i" },
343 { 0x103C, 0x1922, CISS_BOARD_SA5, "HP Smart Array P430" },
344 { 0x103C, 0x1923, CISS_BOARD_SA5, "HP Smart Array P431" },
345 { 0x103C, 0x1924, CISS_BOARD_SA5, "HP Smart Array P830" },
346 { 0x103C, 0x1926, CISS_BOARD_SA5, "HP Smart Array P731m" },
347 { 0x103C, 0x1928, CISS_BOARD_SA5, "HP Smart Array P230i" },
348 { 0x103C, 0x1929, CISS_BOARD_SA5, "HP Smart Array P530" },
349 { 0x103C, 0x192A, CISS_BOARD_SA5, "HP Smart Array P531" },
353 /************************************************************************
354 * Find a match for the device in our list of known adapters.
357 ciss_lookup(device_t dev)
361 for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
362 if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
363 (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
369 /************************************************************************
370 * Match a known CISS adapter.
373 ciss_probe(device_t dev)
377 i = ciss_lookup(dev);
379 device_set_desc(dev, ciss_vendor_data[i].desc);
380 return(BUS_PROBE_DEFAULT);
385 /************************************************************************
386 * Attach the driver to this adapter.
389 ciss_attach(device_t dev)
391 struct ciss_softc *sc;
397 /* print structure/union sizes */
398 debug_struct(ciss_command);
399 debug_struct(ciss_header);
400 debug_union(ciss_device_address);
401 debug_struct(ciss_cdb);
402 debug_struct(ciss_report_cdb);
403 debug_struct(ciss_notify_cdb);
404 debug_struct(ciss_notify);
405 debug_struct(ciss_message_cdb);
406 debug_struct(ciss_error_info_pointer);
407 debug_struct(ciss_error_info);
408 debug_struct(ciss_sg_entry);
409 debug_struct(ciss_config_table);
410 debug_struct(ciss_bmic_cdb);
411 debug_struct(ciss_bmic_id_ldrive);
412 debug_struct(ciss_bmic_id_lstatus);
413 debug_struct(ciss_bmic_id_table);
414 debug_struct(ciss_bmic_id_pdrive);
415 debug_struct(ciss_bmic_blink_pdrive);
416 debug_struct(ciss_bmic_flush_cache);
417 debug_const(CISS_MAX_REQUESTS);
418 debug_const(CISS_MAX_LOGICAL);
419 debug_const(CISS_INTERRUPT_COALESCE_DELAY);
420 debug_const(CISS_INTERRUPT_COALESCE_COUNT);
421 debug_const(CISS_COMMAND_ALLOC_SIZE);
422 debug_const(CISS_COMMAND_SG_LENGTH);
424 debug_type(cciss_pci_info_struct);
425 debug_type(cciss_coalint_struct);
426 debug_type(cciss_coalint_struct);
427 debug_type(NodeName_type);
428 debug_type(NodeName_type);
429 debug_type(Heartbeat_type);
430 debug_type(BusTypes_type);
431 debug_type(FirmwareVer_type);
432 debug_type(DriverVer_type);
433 debug_type(IOCTL_Command_struct);
436 sc = device_get_softc(dev);
438 mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
439 callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);
442 * Do PCI-specific init.
444 if ((error = ciss_init_pci(sc)) != 0)
448 * Initialise driver queues.
451 ciss_initq_notify(sc);
454 * Initalize device sysctls.
456 ciss_init_sysctl(sc);
459 * Initialise command/request pool.
461 if ((error = ciss_init_requests(sc)) != 0)
465 * Get adapter information.
467 if ((error = ciss_identify_adapter(sc)) != 0)
471 * Find all the physical devices.
473 if ((error = ciss_init_physical(sc)) != 0)
477 * Build our private table of logical devices.
479 if ((error = ciss_init_logical(sc)) != 0)
483 * Enable interrupts so that the CAM scan can complete.
485 CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
488 * Initialise the CAM interface.
490 if ((error = ciss_cam_init(sc)) != 0)
494 * Start the heartbeat routine and event chain.
499 * Create the control device.
501 sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
502 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
503 "ciss%d", device_get_unit(sc->ciss_dev));
504 sc->ciss_dev_t->si_drv1 = sc;
507 * The adapter is running; synchronous commands can now sleep
508 * waiting for an interrupt to signal completion.
510 sc->ciss_flags |= CISS_FLAG_RUNNING;
512 ciss_spawn_notify_thread(sc);
517 /* ciss_free() expects the mutex to be held */
518 mtx_lock(&sc->ciss_mtx);
524 /************************************************************************
525 * Detach the driver from this adapter.
528 ciss_detach(device_t dev)
530 struct ciss_softc *sc = device_get_softc(dev);
534 mtx_lock(&sc->ciss_mtx);
535 if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
536 mtx_unlock(&sc->ciss_mtx);
540 /* flush adapter cache */
541 ciss_flush_adapter(sc);
543 /* release all resources. The mutex is released and freed here too. */
549 /************************************************************************
550 * Prepare adapter for system shutdown.
553 ciss_shutdown(device_t dev)
555 struct ciss_softc *sc = device_get_softc(dev);
559 mtx_lock(&sc->ciss_mtx);
560 /* flush adapter cache */
561 ciss_flush_adapter(sc);
563 if (sc->ciss_soft_reset)
565 mtx_unlock(&sc->ciss_mtx);
571 ciss_init_sysctl(struct ciss_softc *sc)
574 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
575 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
576 OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
579 /************************************************************************
580 * Perform PCI-specific attachment actions.
583 ciss_init_pci(struct ciss_softc *sc)
585 uintptr_t cbase, csize, cofs;
586 uint32_t method, supported_methods;
587 int error, sqmask, i;
593 * Work out adapter type.
595 i = ciss_lookup(sc->ciss_dev);
597 ciss_printf(sc, "unknown adapter type\n");
601 if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
602 sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
603 } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
604 sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
607 * XXX Big hammer, masks/unmasks all possible interrupts. This should
608 * work on all hardware variants. Need to add code to handle the
609 * "controller crashed" interupt bit that this unmasks.
615 * Allocate register window first (we need this to find the config
619 sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
620 if ((sc->ciss_regs_resource =
621 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
622 &sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
623 ciss_printf(sc, "can't allocate register window\n");
626 sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
627 sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
630 * Find the BAR holding the config structure. If it's not the one
631 * we already mapped for registers, map it too.
633 sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
634 if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
635 if ((sc->ciss_cfg_resource =
636 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
637 &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
638 ciss_printf(sc, "can't allocate config window\n");
641 cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
642 csize = rman_get_end(sc->ciss_cfg_resource) -
643 rman_get_start(sc->ciss_cfg_resource) + 1;
645 cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
646 csize = rman_get_end(sc->ciss_regs_resource) -
647 rman_get_start(sc->ciss_regs_resource) + 1;
649 cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
652 * Use the base/size/offset values we just calculated to
653 * sanity-check the config structure. If it's OK, point to it.
655 if ((cofs + sizeof(struct ciss_config_table)) > csize) {
656 ciss_printf(sc, "config table outside window\n");
659 sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
660 debug(1, "config struct at %p", sc->ciss_cfg);
663 * Calculate the number of request structures/commands we are
664 * going to provide for this adapter.
666 sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
669 * Validate the config structure. If we supported other transport
670 * methods, we could select amongst them at this point in time.
672 if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
673 ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
674 sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
675 sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
680 * Select the mode of operation, prefer Performant.
682 if (!(sc->ciss_cfg->supported_methods &
683 (CISS_TRANSPORT_METHOD_SIMPLE | CISS_TRANSPORT_METHOD_PERF))) {
684 ciss_printf(sc, "No supported transport layers: 0x%x\n",
685 sc->ciss_cfg->supported_methods);
688 switch (ciss_force_transport) {
690 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
693 supported_methods = CISS_TRANSPORT_METHOD_PERF;
697 * Override the capabilities of the BOARD and specify SIMPLE
700 if (ciss_vendor_data[i].flags & CISS_BOARD_SIMPLE)
701 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
703 supported_methods = sc->ciss_cfg->supported_methods;
708 if ((supported_methods & CISS_TRANSPORT_METHOD_PERF) != 0) {
709 method = CISS_TRANSPORT_METHOD_PERF;
710 sc->ciss_perf = (struct ciss_perf_config *)(cbase + cofs +
711 sc->ciss_cfg->transport_offset);
712 if (ciss_init_perf(sc)) {
713 supported_methods &= ~method;
716 } else if (supported_methods & CISS_TRANSPORT_METHOD_SIMPLE) {
717 method = CISS_TRANSPORT_METHOD_SIMPLE;
719 ciss_printf(sc, "No supported transport methods: 0x%x\n",
720 sc->ciss_cfg->supported_methods);
725 * Tell it we're using the low 4GB of RAM. Set the default interrupt
726 * coalescing options.
728 sc->ciss_cfg->requested_method = method;
729 sc->ciss_cfg->command_physlimit = 0;
730 sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
731 sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
734 sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
737 if (ciss_update_config(sc)) {
738 ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
739 CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
742 if ((sc->ciss_cfg->active_method & method) == 0) {
743 supported_methods &= ~method;
744 if (supported_methods == 0) {
745 ciss_printf(sc, "adapter refuses to go into available transports "
746 "mode (0x%x, 0x%x)\n", supported_methods,
747 sc->ciss_cfg->active_method);
754 * Wait for the adapter to come ready.
756 if ((error = ciss_wait_adapter(sc)) != 0)
759 /* Prepare to possibly use MSIX and/or PERFORMANT interrupts. Normal
760 * interrupts have a rid of 0, this will be overridden if MSIX is used.
762 sc->ciss_irq_rid[0] = 0;
763 if (method == CISS_TRANSPORT_METHOD_PERF) {
764 ciss_printf(sc, "PERFORMANT Transport\n");
765 if ((ciss_force_interrupt != 1) && (ciss_setup_msix(sc) == 0)) {
766 intr = ciss_perf_msi_intr;
768 intr = ciss_perf_intr;
770 /* XXX The docs say that the 0x01 bit is only for SAS controllers.
771 * Unfortunately, there is no good way to know if this is a SAS
772 * controller. Hopefully enabling this bit universally will work OK.
773 * It seems to work fine for SA6i controllers.
775 sc->ciss_interrupt_mask = CISS_TL_PERF_INTR_OPQ | CISS_TL_PERF_INTR_MSI;
778 ciss_printf(sc, "SIMPLE Transport\n");
779 /* MSIX doesn't seem to work in SIMPLE mode, only enable if it forced */
780 if (ciss_force_interrupt == 2)
781 /* If this fails, we automatically revert to INTx */
783 sc->ciss_perf = NULL;
785 sc->ciss_interrupt_mask = sqmask;
789 * Turn off interrupts before we go routing anything.
791 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
794 * Allocate and set up our interrupt.
796 if ((sc->ciss_irq_resource =
797 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid[0],
798 RF_ACTIVE | RF_SHAREABLE)) == NULL) {
799 ciss_printf(sc, "can't allocate interrupt\n");
803 if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
804 INTR_TYPE_CAM|INTR_MPSAFE, NULL, intr, sc,
806 ciss_printf(sc, "can't set up interrupt\n");
811 * Allocate the parent bus DMA tag appropriate for our PCI
814 * Note that "simple" adapters can only address within a 32-bit
817 if (bus_dma_tag_create(bus_get_dma_tag(sc->ciss_dev),/* PCI parent */
818 1, 0, /* alignment, boundary */
819 BUS_SPACE_MAXADDR, /* lowaddr */
820 BUS_SPACE_MAXADDR, /* highaddr */
821 NULL, NULL, /* filter, filterarg */
822 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
823 CISS_MAX_SG_ELEMENTS, /* nsegments */
824 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
826 NULL, NULL, /* lockfunc, lockarg */
827 &sc->ciss_parent_dmat)) {
828 ciss_printf(sc, "can't allocate parent DMA tag\n");
833 * Create DMA tag for mapping buffers into adapter-addressable
836 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
837 1, 0, /* alignment, boundary */
838 BUS_SPACE_MAXADDR, /* lowaddr */
839 BUS_SPACE_MAXADDR, /* highaddr */
840 NULL, NULL, /* filter, filterarg */
841 MAXBSIZE, CISS_MAX_SG_ELEMENTS, /* maxsize, nsegments */
842 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
843 BUS_DMA_ALLOCNOW, /* flags */
844 busdma_lock_mutex, &sc->ciss_mtx, /* lockfunc, lockarg */
845 &sc->ciss_buffer_dmat)) {
846 ciss_printf(sc, "can't allocate buffer DMA tag\n");
852 /************************************************************************
853 * Setup MSI/MSIX operation (Performant only)
854 * Four interrupts are available, but we only use 1 right now. If MSI-X
855 * isn't avaialble, try using MSI instead.
858 ciss_setup_msix(struct ciss_softc *sc)
862 /* Weed out devices that don't actually support MSI */
863 i = ciss_lookup(sc->ciss_dev);
864 if (ciss_vendor_data[i].flags & CISS_BOARD_NOMSI)
868 * Only need to use the minimum number of MSI vectors, as the driver
869 * doesn't support directed MSIX interrupts.
871 val = pci_msix_count(sc->ciss_dev);
872 if (val < CISS_MSI_COUNT) {
873 val = pci_msi_count(sc->ciss_dev);
874 device_printf(sc->ciss_dev, "got %d MSI messages]\n", val);
875 if (val < CISS_MSI_COUNT)
878 val = MIN(val, CISS_MSI_COUNT);
879 if (pci_alloc_msix(sc->ciss_dev, &val) != 0) {
880 if (pci_alloc_msi(sc->ciss_dev, &val) != 0)
886 ciss_printf(sc, "Using %d MSIX interrupt%s\n", val,
887 (val != 1) ? "s" : "");
889 for (i = 0; i < val; i++)
890 sc->ciss_irq_rid[i] = i + 1;
896 /************************************************************************
897 * Setup the Performant structures.
900 ciss_init_perf(struct ciss_softc *sc)
902 struct ciss_perf_config *pc = sc->ciss_perf;
906 * Create the DMA tag for the reply queue.
908 reply_size = sizeof(uint64_t) * sc->ciss_max_requests;
909 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
910 1, 0, /* alignment, boundary */
911 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
912 BUS_SPACE_MAXADDR, /* highaddr */
913 NULL, NULL, /* filter, filterarg */
914 reply_size, 1, /* maxsize, nsegments */
915 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
917 NULL, NULL, /* lockfunc, lockarg */
918 &sc->ciss_reply_dmat)) {
919 ciss_printf(sc, "can't allocate reply DMA tag\n");
923 * Allocate memory and make it available for DMA.
925 if (bus_dmamem_alloc(sc->ciss_reply_dmat, (void **)&sc->ciss_reply,
926 BUS_DMA_NOWAIT, &sc->ciss_reply_map)) {
927 ciss_printf(sc, "can't allocate reply memory\n");
930 bus_dmamap_load(sc->ciss_reply_dmat, sc->ciss_reply_map, sc->ciss_reply,
931 reply_size, ciss_command_map_helper, &sc->ciss_reply_phys, 0);
932 bzero(sc->ciss_reply, reply_size);
934 sc->ciss_cycle = 0x1;
938 * Preload the fetch table with common command sizes. This allows the
939 * hardware to not waste bus cycles for typical i/o commands, but also not
940 * tax the driver to be too exact in choosing sizes. The table is optimized
941 * for page-aligned i/o's, but since most i/o comes from the various pagers,
942 * it's a reasonable assumption to make.
944 pc->fetch_count[CISS_SG_FETCH_NONE] = (sizeof(struct ciss_command) + 15) / 16;
945 pc->fetch_count[CISS_SG_FETCH_1] =
946 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 1 + 15) / 16;
947 pc->fetch_count[CISS_SG_FETCH_2] =
948 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 2 + 15) / 16;
949 pc->fetch_count[CISS_SG_FETCH_4] =
950 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 4 + 15) / 16;
951 pc->fetch_count[CISS_SG_FETCH_8] =
952 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 8 + 15) / 16;
953 pc->fetch_count[CISS_SG_FETCH_16] =
954 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 16 + 15) / 16;
955 pc->fetch_count[CISS_SG_FETCH_32] =
956 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 32 + 15) / 16;
957 pc->fetch_count[CISS_SG_FETCH_MAX] = (CISS_COMMAND_ALLOC_SIZE + 15) / 16;
959 pc->rq_size = sc->ciss_max_requests; /* XXX less than the card supports? */
960 pc->rq_count = 1; /* XXX Hardcode for a single queue */
963 pc->rq[0].rq_addr_hi = 0x0;
964 pc->rq[0].rq_addr_lo = sc->ciss_reply_phys;
969 /************************************************************************
970 * Wait for the adapter to come ready.
973 ciss_wait_adapter(struct ciss_softc *sc)
980 * Wait for the adapter to come ready.
982 if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
983 ciss_printf(sc, "waiting for adapter to come ready...\n");
984 for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
985 DELAY(1000000); /* one second */
987 ciss_printf(sc, "timed out waiting for adapter to come ready\n");
995 /************************************************************************
996 * Flush the adapter cache.
999 ciss_flush_adapter(struct ciss_softc *sc)
1001 struct ciss_request *cr;
1002 struct ciss_bmic_flush_cache *cbfc;
1003 int error, command_status;
1011 * Build a BMIC request to flush the cache. We don't disable
1012 * it, as we may be going to do more I/O (eg. we are emulating
1013 * the Synchronise Cache command).
1015 if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1019 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
1020 (void **)&cbfc, sizeof(*cbfc))) != 0)
1024 * Submit the request and wait for it to complete.
1026 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1027 ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
1034 ciss_report_request(cr, &command_status, NULL);
1035 switch(command_status) {
1036 case CISS_CMD_STATUS_SUCCESS:
1039 ciss_printf(sc, "error flushing cache (%s)\n",
1040 ciss_name_command_status(command_status));
1047 free(cbfc, CISS_MALLOC_CLASS);
1049 ciss_release_request(cr);
1054 ciss_soft_reset(struct ciss_softc *sc)
1056 struct ciss_request *cr = NULL;
1057 struct ciss_command *cc;
1060 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1061 /* only reset proxy controllers */
1062 if (sc->ciss_controllers[i].physical.bus == 0)
1065 if ((error = ciss_get_request(sc, &cr)) != 0)
1068 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
1073 cc->header.address = sc->ciss_controllers[i];
1075 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
1078 ciss_release_request(cr);
1082 ciss_printf(sc, "error resetting controller (%d)\n", error);
1085 ciss_release_request(cr);
1088 /************************************************************************
1089 * Allocate memory for the adapter command structures, initialise
1090 * the request structures.
1092 * Note that the entire set of commands are allocated in a single
1096 ciss_init_requests(struct ciss_softc *sc)
1098 struct ciss_request *cr;
1104 ciss_printf(sc, "using %d of %d available commands\n",
1105 sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
1108 * Create the DMA tag for commands.
1110 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
1111 32, 0, /* alignment, boundary */
1112 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1113 BUS_SPACE_MAXADDR, /* highaddr */
1114 NULL, NULL, /* filter, filterarg */
1115 CISS_COMMAND_ALLOC_SIZE *
1116 sc->ciss_max_requests, 1, /* maxsize, nsegments */
1117 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1119 NULL, NULL, /* lockfunc, lockarg */
1120 &sc->ciss_command_dmat)) {
1121 ciss_printf(sc, "can't allocate command DMA tag\n");
1125 * Allocate memory and make it available for DMA.
1127 if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
1128 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
1129 ciss_printf(sc, "can't allocate command memory\n");
1132 bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map,sc->ciss_command,
1133 CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
1134 ciss_command_map_helper, &sc->ciss_command_phys, 0);
1135 bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
1138 * Set up the request and command structures, push requests onto
1141 for (i = 1; i < sc->ciss_max_requests; i++) {
1142 cr = &sc->ciss_request[i];
1145 cr->cr_cc = (struct ciss_command *)((uintptr_t)sc->ciss_command +
1146 CISS_COMMAND_ALLOC_SIZE * i);
1147 cr->cr_ccphys = sc->ciss_command_phys + CISS_COMMAND_ALLOC_SIZE * i;
1148 bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
1149 ciss_enqueue_free(cr);
1155 ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1160 *addr = segs[0].ds_addr;
1163 /************************************************************************
1164 * Identify the adapter, print some information about it.
1167 ciss_identify_adapter(struct ciss_softc *sc)
1169 struct ciss_request *cr;
1170 int error, command_status;
1177 * Get a request, allocate storage for the adapter data.
1179 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
1180 (void **)&sc->ciss_id,
1181 sizeof(*sc->ciss_id))) != 0)
1185 * Submit the request and wait for it to complete.
1187 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1188 ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
1195 ciss_report_request(cr, &command_status, NULL);
1196 switch(command_status) {
1197 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1199 case CISS_CMD_STATUS_DATA_UNDERRUN:
1200 case CISS_CMD_STATUS_DATA_OVERRUN:
1201 ciss_printf(sc, "data over/underrun reading adapter information\n");
1203 ciss_printf(sc, "error reading adapter information (%s)\n",
1204 ciss_name_command_status(command_status));
1209 /* sanity-check reply */
1210 if (!sc->ciss_id->big_map_supported) {
1211 ciss_printf(sc, "adapter does not support BIG_MAP\n");
1217 /* XXX later revisions may not need this */
1218 sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
1221 /* XXX only really required for old 5300 adapters? */
1222 sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
1225 * Earlier controller specs do not contain these config
1226 * entries, so assume that a 0 means its old and assign
1227 * these values to the defaults that were established
1228 * when this driver was developed for them
1230 if (sc->ciss_cfg->max_logical_supported == 0)
1231 sc->ciss_cfg->max_logical_supported = CISS_MAX_LOGICAL;
1232 if (sc->ciss_cfg->max_physical_supported == 0)
1233 sc->ciss_cfg->max_physical_supported = CISS_MAX_PHYSICAL;
1234 /* print information */
1236 ciss_printf(sc, " %d logical drive%s configured\n",
1237 sc->ciss_id->configured_logical_drives,
1238 (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
1239 ciss_printf(sc, " firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
1240 ciss_printf(sc, " %d SCSI channels\n", sc->ciss_id->scsi_bus_count);
1242 ciss_printf(sc, " signature '%.4s'\n", sc->ciss_cfg->signature);
1243 ciss_printf(sc, " valence %d\n", sc->ciss_cfg->valence);
1244 ciss_printf(sc, " supported I/O methods 0x%b\n",
1245 sc->ciss_cfg->supported_methods,
1246 "\20\1READY\2simple\3performant\4MEMQ\n");
1247 ciss_printf(sc, " active I/O method 0x%b\n",
1248 sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
1249 ciss_printf(sc, " 4G page base 0x%08x\n",
1250 sc->ciss_cfg->command_physlimit);
1251 ciss_printf(sc, " interrupt coalesce delay %dus\n",
1252 sc->ciss_cfg->interrupt_coalesce_delay);
1253 ciss_printf(sc, " interrupt coalesce count %d\n",
1254 sc->ciss_cfg->interrupt_coalesce_count);
1255 ciss_printf(sc, " max outstanding commands %d\n",
1256 sc->ciss_cfg->max_outstanding_commands);
1257 ciss_printf(sc, " bus types 0x%b\n", sc->ciss_cfg->bus_types,
1258 "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
1259 ciss_printf(sc, " server name '%.16s'\n", sc->ciss_cfg->server_name);
1260 ciss_printf(sc, " heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
1261 ciss_printf(sc, " max logical logical volumes: %d\n", sc->ciss_cfg->max_logical_supported);
1262 ciss_printf(sc, " max physical disks supported: %d\n", sc->ciss_cfg->max_physical_supported);
1263 ciss_printf(sc, " max physical disks per logical volume: %d\n", sc->ciss_cfg->max_physical_per_logical);
1268 if (sc->ciss_id != NULL) {
1269 free(sc->ciss_id, CISS_MALLOC_CLASS);
1274 ciss_release_request(cr);
1278 /************************************************************************
1279 * Helper routine for generating a list of logical and physical luns.
1281 static struct ciss_lun_report *
1282 ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
1284 struct ciss_request *cr;
1285 struct ciss_command *cc;
1286 struct ciss_report_cdb *crc;
1287 struct ciss_lun_report *cll;
1298 * Get a request, allocate storage for the address list.
1300 if ((error = ciss_get_request(sc, &cr)) != 0)
1302 report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
1303 if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1304 ciss_printf(sc, "can't allocate memory for lun report\n");
1310 * Build the Report Logical/Physical LUNs command.
1314 cr->cr_length = report_size;
1315 cr->cr_flags = CISS_REQ_DATAIN;
1317 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
1318 cc->header.address.physical.bus = 0;
1319 cc->header.address.physical.target = 0;
1320 cc->cdb.cdb_length = sizeof(*crc);
1321 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1322 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1323 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1324 cc->cdb.timeout = 30; /* XXX better suggestions? */
1326 crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
1327 bzero(crc, sizeof(*crc));
1328 crc->opcode = opcode;
1329 crc->length = htonl(report_size); /* big-endian field */
1330 cll->list_size = htonl(report_size - sizeof(*cll)); /* big-endian field */
1333 * Submit the request and wait for it to complete. (timeout
1334 * here should be much greater than above)
1336 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1337 ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
1342 * Check response. Note that data over/underrun is OK.
1344 ciss_report_request(cr, &command_status, NULL);
1345 switch(command_status) {
1346 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1347 case CISS_CMD_STATUS_DATA_UNDERRUN: /* buffer too large, not bad */
1349 case CISS_CMD_STATUS_DATA_OVERRUN:
1350 ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
1351 sc->ciss_cfg->max_logical_supported);
1354 ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
1355 ciss_name_command_status(command_status));
1359 ciss_release_request(cr);
1364 ciss_release_request(cr);
1365 if (error && cll != NULL) {
1366 free(cll, CISS_MALLOC_CLASS);
1372 /************************************************************************
1373 * Find logical drives on the adapter.
1376 ciss_init_logical(struct ciss_softc *sc)
1378 struct ciss_lun_report *cll;
1379 int error = 0, i, j;
1384 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
1385 sc->ciss_cfg->max_logical_supported);
1391 /* sanity-check reply */
1392 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1393 if ((ndrives < 0) || (ndrives > sc->ciss_cfg->max_logical_supported)) {
1394 ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
1395 ndrives, sc->ciss_cfg->max_logical_supported);
1401 * Save logical drive information.
1404 ciss_printf(sc, "%d logical drive%s\n",
1405 ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
1409 malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
1410 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1411 if (sc->ciss_logical == NULL) {
1416 for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1417 sc->ciss_logical[i] =
1418 malloc(sc->ciss_cfg->max_logical_supported *
1419 sizeof(struct ciss_ldrive),
1420 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1421 if (sc->ciss_logical[i] == NULL) {
1426 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++)
1427 sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
1431 for (i = 0; i < sc->ciss_cfg->max_logical_supported; i++) {
1433 struct ciss_ldrive *ld;
1436 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
1437 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
1438 ld = &sc->ciss_logical[bus][target];
1440 ld->cl_address = cll->lun[i];
1441 ld->cl_controller = &sc->ciss_controllers[bus];
1442 if (ciss_identify_logical(sc, ld) != 0)
1445 * If the drive has had media exchanged, we should bring it online.
1447 if (ld->cl_lstatus->media_exchanged)
1448 ciss_accept_media(sc, ld);
1455 free(cll, CISS_MALLOC_CLASS);
1460 ciss_init_physical(struct ciss_softc *sc)
1462 struct ciss_lun_report *cll;
1472 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
1473 sc->ciss_cfg->max_physical_supported);
1479 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1482 ciss_printf(sc, "%d physical device%s\n",
1483 nphys, (nphys > 1 || nphys == 0) ? "s" : "");
1487 * Figure out the bus mapping.
1488 * Logical buses include both the local logical bus for local arrays and
1489 * proxy buses for remote arrays. Physical buses are numbered by the
1490 * controller and represent physical buses that hold physical devices.
1491 * We shift these bus numbers so that everything fits into a single flat
1492 * numbering space for CAM. Logical buses occupy the first 32 CAM bus
1493 * numbers, and the physical bus numbers are shifted to be above that.
1494 * This results in the various driver arrays being indexed as follows:
1496 * ciss_controllers[] - indexed by logical bus
1497 * ciss_cam_sim[] - indexed by both logical and physical, with physical
1498 * being shifted by 32.
1499 * ciss_logical[][] - indexed by logical bus
1500 * ciss_physical[][] - indexed by physical bus
1502 * XXX This is getting more and more hackish. CISS really doesn't play
1503 * well with a standard SCSI model; devices are addressed via magic
1504 * cookies, not via b/t/l addresses. Since there is no way to store
1505 * the cookie in the CAM device object, we have to keep these lookup
1506 * tables handy so that the devices can be found quickly at the cost
1507 * of wasting memory and having a convoluted lookup scheme. This
1508 * driver should probably be converted to block interface.
1511 * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
1512 * controller. A proxy controller is another physical controller
1513 * behind the primary PCI controller. We need to know about this
1514 * so that BMIC commands can be properly targeted. There can be
1515 * proxy controllers attached to a single PCI controller, so
1516 * find the highest numbered one so the array can be properly
1519 sc->ciss_max_logical_bus = 1;
1520 for (i = 0; i < nphys; i++) {
1521 if (cll->lun[i].physical.extra_address == 0) {
1522 bus = cll->lun[i].physical.bus;
1523 sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
1525 bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
1526 sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
1530 sc->ciss_controllers =
1531 malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
1532 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1534 if (sc->ciss_controllers == NULL) {
1535 ciss_printf(sc, "Could not allocate memory for controller map\n");
1540 /* setup a map of controller addresses */
1541 for (i = 0; i < nphys; i++) {
1542 if (cll->lun[i].physical.extra_address == 0) {
1543 sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
1548 malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
1549 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1550 if (sc->ciss_physical == NULL) {
1551 ciss_printf(sc, "Could not allocate memory for physical device map\n");
1556 for (i = 0; i < sc->ciss_max_physical_bus; i++) {
1557 sc->ciss_physical[i] =
1558 malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
1559 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1560 if (sc->ciss_physical[i] == NULL) {
1561 ciss_printf(sc, "Could not allocate memory for target map\n");
1567 ciss_filter_physical(sc, cll);
1571 free(cll, CISS_MALLOC_CLASS);
1577 ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
1583 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1584 for (i = 0; i < nphys; i++) {
1585 if (cll->lun[i].physical.extra_address == 0)
1589 * Filter out devices that we don't want. Level 3 LUNs could
1590 * probably be supported, but the docs don't give enough of a
1593 * The mode field of the physical address is likely set to have
1594 * hard disks masked out. Honor it unless the user has overridden
1595 * us with the tunable. We also munge the inquiry data for these
1596 * disks so that they only show up as passthrough devices. Keeping
1597 * them visible in this fashion is useful for doing things like
1598 * flashing firmware.
1600 ea = cll->lun[i].physical.extra_address;
1601 if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
1602 (CISS_EXTRA_MODE2(ea) == 0x3))
1604 if ((ciss_expose_hidden_physical == 0) &&
1605 (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
1609 * Note: CISS firmware numbers physical busses starting at '1', not
1610 * '0'. This numbering is internal to the firmware and is only
1611 * used as a hint here.
1613 bus = CISS_EXTRA_BUS2(ea) - 1;
1614 target = CISS_EXTRA_TARGET2(ea);
1615 sc->ciss_physical[bus][target].cp_address = cll->lun[i];
1616 sc->ciss_physical[bus][target].cp_online = 1;
1623 ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1625 struct ciss_request *cr;
1626 struct ciss_command *cc;
1627 struct scsi_inquiry *inq;
1633 bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1635 if ((error = ciss_get_request(sc, &cr)) != 0)
1639 cr->cr_data = &ld->cl_geometry;
1640 cr->cr_length = sizeof(ld->cl_geometry);
1641 cr->cr_flags = CISS_REQ_DATAIN;
1643 cc->header.address = ld->cl_address;
1644 cc->cdb.cdb_length = 6;
1645 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1646 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1647 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1648 cc->cdb.timeout = 30;
1650 inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1651 inq->opcode = INQUIRY;
1652 inq->byte2 = SI_EVPD;
1653 inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1654 scsi_ulto2b(sizeof(ld->cl_geometry), inq->length);
1656 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1657 ciss_printf(sc, "error getting geometry (%d)\n", error);
1661 ciss_report_request(cr, &command_status, NULL);
1662 switch(command_status) {
1663 case CISS_CMD_STATUS_SUCCESS:
1664 case CISS_CMD_STATUS_DATA_UNDERRUN:
1666 case CISS_CMD_STATUS_DATA_OVERRUN:
1667 ciss_printf(sc, "WARNING: Data overrun\n");
1670 ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1671 ciss_name_command_status(command_status));
1677 ciss_release_request(cr);
1680 /************************************************************************
1681 * Identify a logical drive, initialise state related to it.
1684 ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1686 struct ciss_request *cr;
1687 struct ciss_command *cc;
1688 struct ciss_bmic_cdb *cbc;
1689 int error, command_status;
1696 * Build a BMIC request to fetch the drive ID.
1698 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
1699 (void **)&ld->cl_ldrive,
1700 sizeof(*ld->cl_ldrive))) != 0)
1703 cc->header.address = *ld->cl_controller; /* target controller */
1704 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1705 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1708 * Submit the request and wait for it to complete.
1710 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1711 ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
1718 ciss_report_request(cr, &command_status, NULL);
1719 switch(command_status) {
1720 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1722 case CISS_CMD_STATUS_DATA_UNDERRUN:
1723 case CISS_CMD_STATUS_DATA_OVERRUN:
1724 ciss_printf(sc, "data over/underrun reading logical drive ID\n");
1726 ciss_printf(sc, "error reading logical drive ID (%s)\n",
1727 ciss_name_command_status(command_status));
1731 ciss_release_request(cr);
1735 * Build a CISS BMIC command to get the logical drive status.
1737 if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
1741 * Get the logical drive geometry.
1743 if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1747 * Print the drive's basic characteristics.
1750 ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
1751 CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
1752 CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
1753 ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
1754 ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
1755 ld->cl_ldrive->block_size));
1757 ciss_print_ldrive(sc, ld);
1761 /* make the drive not-exist */
1762 ld->cl_status = CISS_LD_NONEXISTENT;
1763 if (ld->cl_ldrive != NULL) {
1764 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
1765 ld->cl_ldrive = NULL;
1767 if (ld->cl_lstatus != NULL) {
1768 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
1769 ld->cl_lstatus = NULL;
1773 ciss_release_request(cr);
1778 /************************************************************************
1779 * Get status for a logical drive.
1781 * XXX should we also do this in response to Test Unit Ready?
1784 ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld)
1786 struct ciss_request *cr;
1787 struct ciss_command *cc;
1788 struct ciss_bmic_cdb *cbc;
1789 int error, command_status;
1792 * Build a CISS BMIC command to get the logical drive status.
1794 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
1795 (void **)&ld->cl_lstatus,
1796 sizeof(*ld->cl_lstatus))) != 0)
1799 cc->header.address = *ld->cl_controller; /* target controller */
1800 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1801 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1804 * Submit the request and wait for it to complete.
1806 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1807 ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1814 ciss_report_request(cr, &command_status, NULL);
1815 switch(command_status) {
1816 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1818 case CISS_CMD_STATUS_DATA_UNDERRUN:
1819 case CISS_CMD_STATUS_DATA_OVERRUN:
1820 ciss_printf(sc, "data over/underrun reading logical drive status\n");
1822 ciss_printf(sc, "error reading logical drive status (%s)\n",
1823 ciss_name_command_status(command_status));
1829 * Set the drive's summary status based on the returned status.
1831 * XXX testing shows that a failed JBOD drive comes back at next
1832 * boot in "queued for expansion" mode. WTF?
1834 ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
1838 ciss_release_request(cr);
1842 /************************************************************************
1843 * Notify the adapter of a config update.
1846 ciss_update_config(struct ciss_softc *sc)
1852 CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
1853 for (i = 0; i < 1000; i++) {
1854 if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
1855 CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
1863 /************************************************************************
1864 * Accept new media into a logical drive.
1866 * XXX The drive has previously been offline; it would be good if we
1867 * could make sure it's not open right now.
1870 ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
1872 struct ciss_request *cr;
1873 struct ciss_command *cc;
1874 struct ciss_bmic_cdb *cbc;
1876 int error = 0, ldrive;
1878 ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1880 debug(0, "bringing logical drive %d back online", ldrive);
1883 * Build a CISS BMIC command to bring the drive back online.
1885 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
1889 cc->header.address = *ld->cl_controller; /* target controller */
1890 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1891 cbc->log_drive = ldrive;
1894 * Submit the request and wait for it to complete.
1896 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1897 ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
1904 ciss_report_request(cr, &command_status, NULL);
1905 switch(command_status) {
1906 case CISS_CMD_STATUS_SUCCESS: /* all OK */
1907 /* we should get a logical drive status changed event here */
1910 ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
1911 ciss_name_command_status(command_status));
1917 ciss_release_request(cr);
1921 /************************************************************************
1922 * Release adapter resources.
1925 ciss_free(struct ciss_softc *sc)
1927 struct ciss_request *cr;
1932 /* we're going away */
1933 sc->ciss_flags |= CISS_FLAG_ABORTING;
1935 /* terminate the periodic heartbeat routine */
1936 callout_stop(&sc->ciss_periodic);
1938 /* cancel the Event Notify chain */
1939 ciss_notify_abort(sc);
1941 ciss_kill_notify_thread(sc);
1943 /* disconnect from CAM */
1944 if (sc->ciss_cam_sim) {
1945 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1946 if (sc->ciss_cam_sim[i]) {
1947 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1948 cam_sim_free(sc->ciss_cam_sim[i], 0);
1951 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
1952 CISS_PHYSICAL_BASE; i++) {
1953 if (sc->ciss_cam_sim[i]) {
1954 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1955 cam_sim_free(sc->ciss_cam_sim[i], 0);
1958 free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
1960 if (sc->ciss_cam_devq)
1961 cam_simq_free(sc->ciss_cam_devq);
1963 /* remove the control device */
1964 mtx_unlock(&sc->ciss_mtx);
1965 if (sc->ciss_dev_t != NULL)
1966 destroy_dev(sc->ciss_dev_t);
1968 /* Final cleanup of the callout. */
1969 callout_drain(&sc->ciss_periodic);
1970 mtx_destroy(&sc->ciss_mtx);
1972 /* free the controller data */
1973 if (sc->ciss_id != NULL)
1974 free(sc->ciss_id, CISS_MALLOC_CLASS);
1976 /* release I/O resources */
1977 if (sc->ciss_regs_resource != NULL)
1978 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1979 sc->ciss_regs_rid, sc->ciss_regs_resource);
1980 if (sc->ciss_cfg_resource != NULL)
1981 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1982 sc->ciss_cfg_rid, sc->ciss_cfg_resource);
1983 if (sc->ciss_intr != NULL)
1984 bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
1985 if (sc->ciss_irq_resource != NULL)
1986 bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
1987 sc->ciss_irq_rid[0], sc->ciss_irq_resource);
1989 pci_release_msi(sc->ciss_dev);
1991 while ((cr = ciss_dequeue_free(sc)) != NULL)
1992 bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
1993 if (sc->ciss_buffer_dmat)
1994 bus_dma_tag_destroy(sc->ciss_buffer_dmat);
1996 /* destroy command memory and DMA tag */
1997 if (sc->ciss_command != NULL) {
1998 bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
1999 bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
2001 if (sc->ciss_command_dmat)
2002 bus_dma_tag_destroy(sc->ciss_command_dmat);
2004 if (sc->ciss_reply) {
2005 bus_dmamap_unload(sc->ciss_reply_dmat, sc->ciss_reply_map);
2006 bus_dmamem_free(sc->ciss_reply_dmat, sc->ciss_reply, sc->ciss_reply_map);
2008 if (sc->ciss_reply_dmat)
2009 bus_dma_tag_destroy(sc->ciss_reply_dmat);
2011 /* destroy DMA tags */
2012 if (sc->ciss_parent_dmat)
2013 bus_dma_tag_destroy(sc->ciss_parent_dmat);
2014 if (sc->ciss_logical) {
2015 for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
2016 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
2017 if (sc->ciss_logical[i][j].cl_ldrive)
2018 free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
2019 if (sc->ciss_logical[i][j].cl_lstatus)
2020 free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
2022 free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
2024 free(sc->ciss_logical, CISS_MALLOC_CLASS);
2027 if (sc->ciss_physical) {
2028 for (i = 0; i < sc->ciss_max_physical_bus; i++)
2029 free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
2030 free(sc->ciss_physical, CISS_MALLOC_CLASS);
2033 if (sc->ciss_controllers)
2034 free(sc->ciss_controllers, CISS_MALLOC_CLASS);
2038 /************************************************************************
2039 * Give a command to the adapter.
2041 * Note that this uses the simple transport layer directly. If we
2042 * want to add support for other layers, we'll need a switch of some
2045 * Note that the simple transport layer has no way of refusing a
2046 * command; we only have as many request structures as the adapter
2047 * supports commands, so we don't have to check (this presumes that
2048 * the adapter can handle commands as fast as we throw them at it).
2051 ciss_start(struct ciss_request *cr)
2053 struct ciss_command *cc; /* XXX debugging only */
2057 debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
2060 * Map the request's data.
2062 if ((error = ciss_map_request(cr)))
2066 ciss_print_request(cr);
2072 /************************************************************************
2073 * Fetch completed request(s) from the adapter, queue them for
2074 * completion handling.
2076 * Note that this uses the simple transport layer directly. If we
2077 * want to add support for other layers, we'll need a switch of some
2080 * Note that the simple transport mechanism does not require any
2081 * reentrancy protection; the OPQ read is atomic. If there is a
2082 * chance of a race with something else that might move the request
2083 * off the busy list, then we will have to lock against that
2084 * (eg. timeouts, etc.)
2087 ciss_done(struct ciss_softc *sc, cr_qhead_t *qh)
2089 struct ciss_request *cr;
2090 struct ciss_command *cc;
2091 u_int32_t tag, index;
2096 * Loop quickly taking requests from the adapter and moving them
2097 * to the completed queue.
2101 tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
2102 if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
2105 debug(2, "completed command %d%s", index,
2106 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2107 if (index >= sc->ciss_max_requests) {
2108 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2111 cr = &(sc->ciss_request[index]);
2113 cc->header.host_tag = tag; /* not updated by adapter */
2114 ciss_enqueue_complete(cr, qh);
2120 ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh)
2122 struct ciss_request *cr;
2123 struct ciss_command *cc;
2124 u_int32_t tag, index;
2129 * Loop quickly taking requests from the adapter and moving them
2130 * to the completed queue.
2133 tag = sc->ciss_reply[sc->ciss_rqidx];
2134 if ((tag & CISS_CYCLE_MASK) != sc->ciss_cycle)
2137 debug(2, "completed command %d%s\n", index,
2138 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2139 if (index < sc->ciss_max_requests) {
2140 cr = &(sc->ciss_request[index]);
2142 cc->header.host_tag = tag; /* not updated by adapter */
2143 ciss_enqueue_complete(cr, qh);
2145 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2147 if (++sc->ciss_rqidx == sc->ciss_max_requests) {
2149 sc->ciss_cycle ^= 1;
2155 /************************************************************************
2156 * Take an interrupt from the adapter.
2159 ciss_intr(void *arg)
2162 struct ciss_softc *sc = (struct ciss_softc *)arg;
2165 * The only interrupt we recognise indicates that there are
2166 * entries in the outbound post queue.
2170 mtx_lock(&sc->ciss_mtx);
2171 ciss_complete(sc, &qh);
2172 mtx_unlock(&sc->ciss_mtx);
2176 ciss_perf_intr(void *arg)
2178 struct ciss_softc *sc = (struct ciss_softc *)arg;
2180 /* Clear the interrupt and flush the bridges. Docs say that the flush
2181 * needs to be done twice, which doesn't seem right.
2183 CISS_TL_PERF_CLEAR_INT(sc);
2184 CISS_TL_PERF_FLUSH_INT(sc);
2186 ciss_perf_msi_intr(sc);
2190 ciss_perf_msi_intr(void *arg)
2193 struct ciss_softc *sc = (struct ciss_softc *)arg;
2196 ciss_perf_done(sc, &qh);
2197 mtx_lock(&sc->ciss_mtx);
2198 ciss_complete(sc, &qh);
2199 mtx_unlock(&sc->ciss_mtx);
2203 /************************************************************************
2204 * Process completed requests.
2206 * Requests can be completed in three fashions:
2208 * - by invoking a callback function (cr_complete is non-null)
2209 * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
2210 * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
2213 ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh)
2215 struct ciss_request *cr;
2220 * Loop taking requests off the completed queue and performing
2221 * completion processing on them.
2224 if ((cr = ciss_dequeue_complete(sc, qh)) == NULL)
2226 ciss_unmap_request(cr);
2228 if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
2229 ciss_printf(sc, "WARNING: completing non-busy request\n");
2230 cr->cr_flags &= ~CISS_REQ_BUSY;
2233 * If the request has a callback, invoke it.
2235 if (cr->cr_complete != NULL) {
2236 cr->cr_complete(cr);
2241 * If someone is sleeping on this request, wake them up.
2243 if (cr->cr_flags & CISS_REQ_SLEEP) {
2244 cr->cr_flags &= ~CISS_REQ_SLEEP;
2250 * If someone is polling this request for completion, signal.
2252 if (cr->cr_flags & CISS_REQ_POLL) {
2253 cr->cr_flags &= ~CISS_REQ_POLL;
2258 * Give up and throw the request back on the free queue. This
2259 * should never happen; resources will probably be lost.
2261 ciss_printf(sc, "WARNING: completed command with no submitter\n");
2262 ciss_enqueue_free(cr);
2266 /************************************************************************
2267 * Report on the completion status of a request, and pass back SCSI
2268 * and command status values.
2271 _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func)
2273 struct ciss_command *cc;
2274 struct ciss_error_info *ce;
2279 ce = (struct ciss_error_info *)&(cc->sg[0]);
2282 * We don't consider data under/overrun an error for the Report
2283 * Logical/Physical LUNs commands.
2285 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
2286 ((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
2287 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
2288 ((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
2289 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
2290 (cc->cdb.cdb[0] == INQUIRY))) {
2291 cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
2292 debug(2, "ignoring irrelevant under/overrun error");
2296 * Check the command's error bit, if clear, there's no status and
2299 if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
2300 if (scsi_status != NULL)
2301 *scsi_status = SCSI_STATUS_OK;
2302 if (command_status != NULL)
2303 *command_status = CISS_CMD_STATUS_SUCCESS;
2306 if (command_status != NULL)
2307 *command_status = ce->command_status;
2308 if (scsi_status != NULL) {
2309 if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
2310 *scsi_status = ce->scsi_status;
2316 ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
2317 ce->command_status, ciss_name_command_status(ce->command_status),
2319 if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
2320 ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x, function %s\n",
2321 ce->additional_error_info.invalid_command.offense_size,
2322 ce->additional_error_info.invalid_command.offense_offset,
2323 ce->additional_error_info.invalid_command.offense_value,
2328 ciss_print_request(cr);
2333 /************************************************************************
2334 * Issue a request and don't return until it's completed.
2336 * Depending on adapter status, we may poll or sleep waiting for
2340 ciss_synch_request(struct ciss_request *cr, int timeout)
2342 if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
2343 return(ciss_wait_request(cr, timeout));
2345 return(ciss_poll_request(cr, timeout));
2349 /************************************************************************
2350 * Issue a request and poll for completion.
2352 * Timeout in milliseconds.
2355 ciss_poll_request(struct ciss_request *cr, int timeout)
2358 struct ciss_softc *sc;
2365 cr->cr_flags |= CISS_REQ_POLL;
2366 if ((error = ciss_start(cr)) != 0)
2371 ciss_perf_done(sc, &qh);
2374 ciss_complete(sc, &qh);
2375 if (!(cr->cr_flags & CISS_REQ_POLL))
2378 } while (timeout-- >= 0);
2379 return(EWOULDBLOCK);
2382 /************************************************************************
2383 * Issue a request and sleep waiting for completion.
2385 * Timeout in milliseconds. Note that a spurious wakeup will reset
2389 ciss_wait_request(struct ciss_request *cr, int timeout)
2395 cr->cr_flags |= CISS_REQ_SLEEP;
2396 if ((error = ciss_start(cr)) != 0)
2399 while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
2400 error = msleep(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ", (timeout * hz) / 1000);
2406 /************************************************************************
2407 * Abort a request. Note that a potential exists here to race the
2408 * request being completed; the caller must deal with this.
2411 ciss_abort_request(struct ciss_request *ar)
2413 struct ciss_request *cr;
2414 struct ciss_command *cc;
2415 struct ciss_message_cdb *cmc;
2421 if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
2424 /* build the abort command */
2426 cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL; /* addressing? */
2427 cc->header.address.physical.target = 0;
2428 cc->header.address.physical.bus = 0;
2429 cc->cdb.cdb_length = sizeof(*cmc);
2430 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
2431 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2432 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2433 cc->cdb.timeout = 30;
2435 cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
2436 cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
2437 cmc->type = CISS_MESSAGE_ABORT_TASK;
2438 cmc->abort_tag = ar->cr_tag; /* endianness?? */
2441 * Send the request and wait for a response. If we believe we
2442 * aborted the request OK, clear the flag that indicates it's
2445 error = ciss_synch_request(cr, 35 * 1000);
2447 error = ciss_report_request(cr, NULL, NULL);
2448 ciss_release_request(cr);
2455 /************************************************************************
2456 * Fetch and initialise a request
2459 ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
2461 struct ciss_request *cr;
2466 * Get a request and clean it up.
2468 if ((cr = ciss_dequeue_free(sc)) == NULL)
2473 cr->cr_complete = NULL;
2474 cr->cr_private = NULL;
2475 cr->cr_sg_tag = CISS_SG_MAX; /* Backstop to prevent accidents */
2477 ciss_preen_command(cr);
2483 ciss_preen_command(struct ciss_request *cr)
2485 struct ciss_command *cc;
2489 * Clean up the command structure.
2491 * Note that we set up the error_info structure here, since the
2492 * length can be overwritten by any command.
2495 cc->header.sg_in_list = 0; /* kinda inefficient this way */
2496 cc->header.sg_total = 0;
2497 cc->header.host_tag = cr->cr_tag << 2;
2498 cc->header.host_tag_zeroes = 0;
2499 bzero(&(cc->sg[0]), CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command));
2500 cmdphys = cr->cr_ccphys;
2501 cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
2502 cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
2505 /************************************************************************
2506 * Release a request to the free list.
2509 ciss_release_request(struct ciss_request *cr)
2511 struct ciss_softc *sc;
2517 /* release the request to the free queue */
2518 ciss_requeue_free(cr);
2521 /************************************************************************
2522 * Allocate a request that will be used to send a BMIC command. Do some
2523 * of the common setup here to avoid duplicating it everywhere else.
2526 ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
2527 int opcode, void **bufp, size_t bufsize)
2529 struct ciss_request *cr;
2530 struct ciss_command *cc;
2531 struct ciss_bmic_cdb *cbc;
2544 if ((error = ciss_get_request(sc, &cr)) != 0)
2548 * Allocate data storage if requested, determine the data direction.
2551 if ((bufsize > 0) && (bufp != NULL)) {
2552 if (*bufp == NULL) {
2553 if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
2559 dataout = 1; /* we are given a buffer, so we are writing */
2564 * Build a CISS BMIC command to get the logical drive ID.
2567 cr->cr_length = bufsize;
2569 cr->cr_flags = CISS_REQ_DATAIN;
2572 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2573 cc->header.address.physical.bus = 0;
2574 cc->header.address.physical.target = 0;
2575 cc->cdb.cdb_length = sizeof(*cbc);
2576 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2577 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2578 cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
2579 cc->cdb.timeout = 0;
2581 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
2582 bzero(cbc, sizeof(*cbc));
2583 cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
2584 cbc->bmic_opcode = opcode;
2585 cbc->size = htons((u_int16_t)bufsize);
2590 ciss_release_request(cr);
2593 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
2599 /************************************************************************
2600 * Handle a command passed in from userspace.
2603 ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
2605 struct ciss_request *cr;
2606 struct ciss_command *cc;
2607 struct ciss_error_info *ce;
2617 while (ciss_get_request(sc, &cr) != 0)
2618 msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
2622 * Allocate an in-kernel databuffer if required, copy in user data.
2624 mtx_unlock(&sc->ciss_mtx);
2625 cr->cr_length = ioc->buf_size;
2626 if (ioc->buf_size > 0) {
2627 if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
2631 if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
2632 debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2638 * Build the request based on the user command.
2640 bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
2641 bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
2643 /* XXX anything else to populate here? */
2644 mtx_lock(&sc->ciss_mtx);
2649 if ((error = ciss_synch_request(cr, 60 * 1000))) {
2650 debug(0, "request failed - %d", error);
2655 * Check to see if the command succeeded.
2657 ce = (struct ciss_error_info *)&(cc->sg[0]);
2658 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
2659 bzero(ce, sizeof(*ce));
2662 * Copy the results back to the user.
2664 bcopy(ce, &ioc->error_info, sizeof(*ce));
2665 mtx_unlock(&sc->ciss_mtx);
2666 if ((ioc->buf_size > 0) &&
2667 (error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
2668 debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2676 mtx_lock(&sc->ciss_mtx);
2679 if ((cr != NULL) && (cr->cr_data != NULL))
2680 free(cr->cr_data, CISS_MALLOC_CLASS);
2682 ciss_release_request(cr);
2686 /************************************************************************
2687 * Map a request into bus-visible space, initialise the scatter/gather
2691 ciss_map_request(struct ciss_request *cr)
2693 struct ciss_softc *sc;
2700 /* check that mapping is necessary */
2701 if (cr->cr_flags & CISS_REQ_MAPPED)
2704 cr->cr_flags |= CISS_REQ_MAPPED;
2706 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2707 BUS_DMASYNC_PREWRITE);
2709 if (cr->cr_data != NULL) {
2710 if (cr->cr_flags & CISS_REQ_CCB)
2711 error = bus_dmamap_load_ccb(sc->ciss_buffer_dmat,
2712 cr->cr_datamap, cr->cr_data,
2713 ciss_request_map_helper, cr, 0);
2715 error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2716 cr->cr_data, cr->cr_length,
2717 ciss_request_map_helper, cr, 0);
2722 * Post the command to the adapter.
2724 cr->cr_sg_tag = CISS_SG_NONE;
2725 cr->cr_flags |= CISS_REQ_BUSY;
2727 CISS_TL_PERF_POST_CMD(sc, cr);
2729 CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2736 ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2738 struct ciss_command *cc;
2739 struct ciss_request *cr;
2740 struct ciss_softc *sc;
2745 cr = (struct ciss_request *)arg;
2749 for (i = 0; i < nseg; i++) {
2750 cc->sg[i].address = segs[i].ds_addr;
2751 cc->sg[i].length = segs[i].ds_len;
2752 cc->sg[i].extension = 0;
2754 /* we leave the s/g table entirely within the command */
2755 cc->header.sg_in_list = nseg;
2756 cc->header.sg_total = nseg;
2758 if (cr->cr_flags & CISS_REQ_DATAIN)
2759 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2760 if (cr->cr_flags & CISS_REQ_DATAOUT)
2761 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2764 cr->cr_sg_tag = CISS_SG_NONE;
2766 cr->cr_sg_tag = CISS_SG_1;
2768 cr->cr_sg_tag = CISS_SG_2;
2770 cr->cr_sg_tag = CISS_SG_4;
2772 cr->cr_sg_tag = CISS_SG_8;
2773 else if (nseg <= 16)
2774 cr->cr_sg_tag = CISS_SG_16;
2775 else if (nseg <= 32)
2776 cr->cr_sg_tag = CISS_SG_32;
2778 cr->cr_sg_tag = CISS_SG_MAX;
2781 * Post the command to the adapter.
2783 cr->cr_flags |= CISS_REQ_BUSY;
2785 CISS_TL_PERF_POST_CMD(sc, cr);
2787 CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2790 /************************************************************************
2791 * Unmap a request from bus-visible space.
2794 ciss_unmap_request(struct ciss_request *cr)
2796 struct ciss_softc *sc;
2802 /* check that unmapping is necessary */
2803 if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
2806 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2807 BUS_DMASYNC_POSTWRITE);
2809 if (cr->cr_data == NULL)
2812 if (cr->cr_flags & CISS_REQ_DATAIN)
2813 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
2814 if (cr->cr_flags & CISS_REQ_DATAOUT)
2815 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
2817 bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2819 cr->cr_flags &= ~CISS_REQ_MAPPED;
2822 /************************************************************************
2823 * Attach the driver to CAM.
2825 * We put all the logical drives on a single SCSI bus.
2828 ciss_cam_init(struct ciss_softc *sc)
2835 * Allocate a devq. We can reuse this for the masked physical
2836 * devices if we decide to export these as well.
2838 if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests - 2)) == NULL) {
2839 ciss_printf(sc, "can't allocate CAM SIM queue\n");
2846 * This naturally wastes a bit of memory. The alternative is to allocate
2847 * and register each bus as it is found, and then track them on a linked
2848 * list. Unfortunately, the driver has a few places where it needs to
2849 * look up the SIM based solely on bus number, and it's unclear whether
2850 * a list traversal would work for these situations.
2852 maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
2853 CISS_PHYSICAL_BASE);
2854 sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
2855 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
2856 if (sc->ciss_cam_sim == NULL) {
2857 ciss_printf(sc, "can't allocate memory for controller SIM\n");
2861 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2862 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2864 device_get_unit(sc->ciss_dev),
2867 sc->ciss_max_requests - 2,
2868 sc->ciss_cam_devq)) == NULL) {
2869 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2874 * Register bus with this SIM.
2876 mtx_lock(&sc->ciss_mtx);
2877 if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) {
2878 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2879 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2880 mtx_unlock(&sc->ciss_mtx);
2884 mtx_unlock(&sc->ciss_mtx);
2887 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
2888 CISS_PHYSICAL_BASE; i++) {
2889 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2891 device_get_unit(sc->ciss_dev),
2893 sc->ciss_max_requests - 2,
2894 sc->ciss_cam_devq)) == NULL) {
2895 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2899 mtx_lock(&sc->ciss_mtx);
2900 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2901 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2902 mtx_unlock(&sc->ciss_mtx);
2905 mtx_unlock(&sc->ciss_mtx);
2911 /************************************************************************
2912 * Initiate a rescan of the 'logical devices' SIM
2915 ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
2921 if ((ccb = xpt_alloc_ccb_nowait()) == NULL) {
2922 ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
2926 if (xpt_create_path(&ccb->ccb_h.path, NULL,
2927 cam_sim_path(sc->ciss_cam_sim[bus]),
2928 target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2929 ciss_printf(sc, "rescan failed (can't create path)\n");
2934 /* scan is now in progress */
2937 /************************************************************************
2938 * Handle requests coming from CAM
2941 ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
2943 struct ciss_softc *sc;
2944 struct ccb_scsiio *csio;
2948 sc = cam_sim_softc(sim);
2949 bus = cam_sim_bus(sim);
2950 csio = (struct ccb_scsiio *)&ccb->csio;
2951 target = csio->ccb_h.target_id;
2952 physical = CISS_IS_PHYSICAL(bus);
2954 switch (ccb->ccb_h.func_code) {
2956 /* perform SCSI I/O */
2958 if (!ciss_cam_action_io(sim, csio))
2962 /* perform geometry calculations */
2963 case XPT_CALC_GEOMETRY:
2965 struct ccb_calc_geometry *ccg = &ccb->ccg;
2966 struct ciss_ldrive *ld;
2968 debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2972 ld = &sc->ciss_logical[bus][target];
2975 * Use the cached geometry settings unless the fault tolerance
2978 if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
2979 u_int32_t secs_per_cylinder;
2982 ccg->secs_per_track = 32;
2983 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2984 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2986 ccg->heads = ld->cl_geometry.heads;
2987 ccg->secs_per_track = ld->cl_geometry.sectors;
2988 ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2990 ccb->ccb_h.status = CAM_REQ_CMP;
2994 /* handle path attribute inquiry */
2997 struct ccb_pathinq *cpi = &ccb->cpi;
3000 debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
3002 cpi->version_num = 1;
3003 cpi->hba_inquiry = PI_TAG_ABLE; /* XXX is this correct? */
3004 cpi->target_sprt = 0;
3006 cpi->max_target = sc->ciss_cfg->max_logical_supported;
3007 cpi->max_lun = 0; /* 'logical drive' channel only */
3008 cpi->initiator_id = sc->ciss_cfg->max_logical_supported;
3009 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3010 strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
3011 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3012 cpi->unit_number = cam_sim_unit(sim);
3013 cpi->bus_id = cam_sim_bus(sim);
3014 cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */
3015 cpi->transport = XPORT_SPI;
3016 cpi->transport_version = 2;
3017 cpi->protocol = PROTO_SCSI;
3018 cpi->protocol_version = SCSI_REV_2;
3019 if (sc->ciss_cfg->max_sg_length == 0) {
3022 /* XXX Fix for ZMR cards that advertise max_sg_length == 32
3023 * Confusing bit here. max_sg_length is usually a power of 2. We always
3024 * need to subtract 1 to account for partial pages. Then we need to
3025 * align on a valid PAGE_SIZE so we round down to the nearest power of 2.
3026 * Add 1 so we can then subtract it out in the assignment to maxio.
3027 * The reason for all these shenanigans is to create a maxio value that
3028 * creates IO operations to volumes that yield consistent operations
3029 * with good performance.
3031 sg_length = sc->ciss_cfg->max_sg_length - 1;
3032 sg_length = (1 << (fls(sg_length) - 1)) + 1;
3034 cpi->maxio = (min(CISS_MAX_SG_ELEMENTS, sg_length) - 1) * PAGE_SIZE;
3035 ccb->ccb_h.status = CAM_REQ_CMP;
3039 case XPT_GET_TRAN_SETTINGS:
3041 struct ccb_trans_settings *cts = &ccb->cts;
3043 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3044 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3046 bus = cam_sim_bus(sim);
3047 target = cts->ccb_h.target_id;
3049 debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
3050 /* disconnect always OK */
3051 cts->protocol = PROTO_SCSI;
3052 cts->protocol_version = SCSI_REV_2;
3053 cts->transport = XPORT_SPI;
3054 cts->transport_version = 2;
3056 spi->valid = CTS_SPI_VALID_DISC;
3057 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
3059 scsi->valid = CTS_SCSI_VALID_TQ;
3060 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3062 cts->ccb_h.status = CAM_REQ_CMP;
3066 default: /* we can't do this */
3067 debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
3068 ccb->ccb_h.status = CAM_REQ_INVALID;
3075 /************************************************************************
3076 * Handle a CAM SCSI I/O request.
3079 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
3081 struct ciss_softc *sc;
3083 struct ciss_request *cr;
3084 struct ciss_command *cc;
3087 sc = cam_sim_softc(sim);
3088 bus = cam_sim_bus(sim);
3089 target = csio->ccb_h.target_id;
3091 debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
3093 /* check that the CDB pointer is not to a physical address */
3094 if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
3095 debug(3, " CDB pointer is to physical address");
3096 csio->ccb_h.status = CAM_REQ_CMP_ERR;
3099 /* abandon aborted ccbs or those that have failed validation */
3100 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
3101 debug(3, "abandoning CCB due to abort/validation failure");
3105 /* handle emulation of some SCSI commands ourself */
3106 if (ciss_cam_emulate(sc, csio))
3110 * Get a request to manage this command. If we can't, return the
3111 * ccb, freeze the queue and flag so that we unfreeze it when a
3112 * request completes.
3114 if ((error = ciss_get_request(sc, &cr)) != 0) {
3115 xpt_freeze_simq(sim, 1);
3116 sc->ciss_flags |= CISS_FLAG_BUSY;
3117 csio->ccb_h.status |= CAM_REQUEUE_REQ;
3122 * Build the command.
3126 cr->cr_length = csio->dxfer_len;
3127 cr->cr_complete = ciss_cam_complete;
3128 cr->cr_private = csio;
3131 * Target the right logical volume.
3133 if (CISS_IS_PHYSICAL(bus))
3134 cc->header.address =
3135 sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
3137 cc->header.address =
3138 sc->ciss_logical[bus][target].cl_address;
3139 cc->cdb.cdb_length = csio->cdb_len;
3140 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3141 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; /* XXX ordered tags? */
3142 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
3143 cr->cr_flags = CISS_REQ_DATAOUT | CISS_REQ_CCB;
3144 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3145 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
3146 cr->cr_flags = CISS_REQ_DATAIN | CISS_REQ_CCB;
3147 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3151 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
3153 cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
3154 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
3155 bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
3157 bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
3161 * Submit the request to the adapter.
3163 * Note that this may fail if we're unable to map the request (and
3164 * if we ever learn a transport layer other than simple, may fail
3165 * if the adapter rejects the command).
3167 if ((error = ciss_start(cr)) != 0) {
3168 xpt_freeze_simq(sim, 1);
3169 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3170 if (error == EINPROGRESS) {
3173 csio->ccb_h.status |= CAM_REQUEUE_REQ;
3174 ciss_release_request(cr);
3182 /************************************************************************
3183 * Emulate SCSI commands the adapter doesn't handle as we might like.
3186 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
3191 target = csio->ccb_h.target_id;
3192 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3193 opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3194 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
3196 if (CISS_IS_PHYSICAL(bus)) {
3197 if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
3198 csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3199 xpt_done((union ccb *)csio);
3206 * Handle requests for volumes that don't exist or are not online.
3207 * A selection timeout is slightly better than an illegal request.
3208 * Other errors might be better.
3210 if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
3211 csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3212 xpt_done((union ccb *)csio);
3216 /* if we have to fake Synchronise Cache */
3217 if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
3219 * If this is a Synchronise Cache command, typically issued when
3220 * a device is closed, flush the adapter and complete now.
3222 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
3223 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
3224 ciss_flush_adapter(sc);
3225 csio->ccb_h.status |= CAM_REQ_CMP;
3226 xpt_done((union ccb *)csio);
3232 * A CISS target can only ever have one lun per target. REPORT_LUNS requires
3233 * at least one LUN field to be pre created for us, so snag it and fill in
3234 * the least significant byte indicating 1 LUN here. Emulate the command
3235 * return to shut up warning on console of a CDB error. swb
3237 if (opcode == REPORT_LUNS && csio->dxfer_len > 0) {
3238 csio->data_ptr[3] = 8;
3239 csio->ccb_h.status |= CAM_REQ_CMP;
3240 xpt_done((union ccb *)csio);
3247 /************************************************************************
3248 * Check for possibly-completed commands.
3251 ciss_cam_poll(struct cam_sim *sim)
3254 struct ciss_softc *sc = cam_sim_softc(sim);
3260 ciss_perf_done(sc, &qh);
3263 ciss_complete(sc, &qh);
3266 /************************************************************************
3267 * Handle completion of a command - pass results back through the CCB
3270 ciss_cam_complete(struct ciss_request *cr)
3272 struct ciss_softc *sc;
3273 struct ciss_command *cc;
3274 struct ciss_error_info *ce;
3275 struct ccb_scsiio *csio;
3283 ce = (struct ciss_error_info *)&(cc->sg[0]);
3284 csio = (struct ccb_scsiio *)cr->cr_private;
3287 * Extract status values from request.
3289 ciss_report_request(cr, &command_status, &scsi_status);
3290 csio->scsi_status = scsi_status;
3293 * Handle specific SCSI status values.
3295 switch(scsi_status) {
3296 /* no status due to adapter error */
3298 debug(0, "adapter error");
3299 csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3302 /* no status due to command completed OK */
3303 case SCSI_STATUS_OK: /* CISS_SCSI_STATUS_GOOD */
3304 debug(2, "SCSI_STATUS_OK");
3305 csio->ccb_h.status |= CAM_REQ_CMP;
3308 /* check condition, sense data included */
3309 case SCSI_STATUS_CHECK_COND: /* CISS_SCSI_STATUS_CHECK_CONDITION */
3310 debug(0, "SCSI_STATUS_CHECK_COND sense size %d resid %d\n",
3311 ce->sense_length, ce->residual_count);
3312 bzero(&csio->sense_data, SSD_FULL_SIZE);
3313 bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
3314 if (csio->sense_len > ce->sense_length)
3315 csio->sense_resid = csio->sense_len - ce->sense_length;
3317 csio->sense_resid = 0;
3318 csio->resid = ce->residual_count;
3319 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
3322 struct scsi_sense_data *sns = (struct scsi_sense_data *)&ce->sense_info[0];
3323 debug(0, "sense key %x", scsi_get_sense_key(sns, csio->sense_len -
3324 csio->sense_resid, /*show_errors*/ 1));
3329 case SCSI_STATUS_BUSY: /* CISS_SCSI_STATUS_BUSY */
3330 debug(0, "SCSI_STATUS_BUSY");
3331 csio->ccb_h.status |= CAM_SCSI_BUSY;
3335 debug(0, "unknown status 0x%x", csio->scsi_status);
3336 csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3340 /* handle post-command fixup */
3341 ciss_cam_complete_fixup(sc, csio);
3343 ciss_release_request(cr);
3344 if (sc->ciss_flags & CISS_FLAG_BUSY) {
3345 sc->ciss_flags &= ~CISS_FLAG_BUSY;
3346 if (csio->ccb_h.status & CAM_RELEASE_SIMQ)
3347 xpt_release_simq(xpt_path_sim(csio->ccb_h.path), 0);
3349 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3351 xpt_done((union ccb *)csio);
3354 /********************************************************************************
3355 * Fix up the result of some commands here.
3358 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
3360 struct scsi_inquiry_data *inq;
3361 struct ciss_ldrive *cl;
3365 cdb = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3366 (uint8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes;
3367 if (cdb[0] == INQUIRY &&
3368 (cdb[1] & SI_EVPD) == 0 &&
3369 (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
3370 csio->dxfer_len >= SHORT_INQUIRY_LENGTH) {
3372 inq = (struct scsi_inquiry_data *)csio->data_ptr;
3373 target = csio->ccb_h.target_id;
3374 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3377 * Don't let hard drives be seen by the DA driver. They will still be
3378 * attached by the PASS driver.
3380 if (CISS_IS_PHYSICAL(bus)) {
3381 if (SID_TYPE(inq) == T_DIRECT)
3382 inq->device = (inq->device & 0xe0) | T_NODEVICE;
3386 cl = &sc->ciss_logical[bus][target];
3388 padstr(inq->vendor, "HP",
3390 padstr(inq->product,
3391 ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance),
3393 padstr(inq->revision,
3394 ciss_name_ldrive_status(cl->cl_lstatus->status),
3400 /********************************************************************************
3401 * Find a peripheral attached at (target)
3403 static struct cam_periph *
3404 ciss_find_periph(struct ciss_softc *sc, int bus, int target)
3406 struct cam_periph *periph;
3407 struct cam_path *path;
3410 status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
3412 if (status == CAM_REQ_CMP) {
3413 periph = cam_periph_find(path, NULL);
3414 xpt_free_path(path);
3421 /********************************************************************************
3422 * Name the device at (target)
3424 * XXX is this strictly correct?
3427 ciss_name_device(struct ciss_softc *sc, int bus, int target)
3429 struct cam_periph *periph;
3431 if (CISS_IS_PHYSICAL(bus))
3433 if ((periph = ciss_find_periph(sc, bus, target)) != NULL) {
3434 sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
3435 periph->periph_name, periph->unit_number);
3438 sc->ciss_logical[bus][target].cl_name[0] = 0;
3442 /************************************************************************
3443 * Periodic status monitoring.
3446 ciss_periodic(void *arg)
3448 struct ciss_softc *sc;
3449 struct ciss_request *cr = NULL;
3450 struct ciss_command *cc = NULL;
3455 sc = (struct ciss_softc *)arg;
3458 * Check the adapter heartbeat.
3460 if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
3461 sc->ciss_heart_attack++;
3462 debug(0, "adapter heart attack in progress 0x%x/%d",
3463 sc->ciss_heartbeat, sc->ciss_heart_attack);
3464 if (sc->ciss_heart_attack == 3) {
3465 ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
3466 ciss_disable_adapter(sc);
3470 sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
3471 sc->ciss_heart_attack = 0;
3472 debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
3476 * Send the NOP message and wait for a response.
3478 if (ciss_nop_message_heartbeat != 0 && (error = ciss_get_request(sc, &cr)) == 0) {
3480 cr->cr_complete = ciss_nop_complete;
3481 cc->cdb.cdb_length = 1;
3482 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
3483 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3484 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3485 cc->cdb.timeout = 0;
3486 cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
3488 if ((error = ciss_start(cr)) != 0) {
3489 ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3494 * If the notify event request has died for some reason, or has
3495 * not started yet, restart it.
3497 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
3498 debug(0, "(re)starting Event Notify chain");
3499 ciss_notify_event(sc);
3505 callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
3509 ciss_nop_complete(struct ciss_request *cr)
3511 struct ciss_softc *sc;
3512 static int first_time = 1;
3515 if (ciss_report_request(cr, NULL, NULL) != 0) {
3516 if (first_time == 1) {
3518 ciss_printf(sc, "SENDING NOP MESSAGE FAILED (not logging anymore)\n");
3522 ciss_release_request(cr);
3525 /************************************************************************
3526 * Disable the adapter.
3528 * The all requests in completed queue is failed with hardware error.
3529 * This will cause failover in a multipath configuration.
3532 ciss_disable_adapter(struct ciss_softc *sc)
3535 struct ciss_request *cr;
3536 struct ciss_command *cc;
3537 struct ciss_error_info *ce;
3540 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
3541 pci_disable_busmaster(sc->ciss_dev);
3542 sc->ciss_flags &= ~CISS_FLAG_RUNNING;
3544 for (i = 1; i < sc->ciss_max_requests; i++) {
3545 cr = &sc->ciss_request[i];
3546 if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
3550 ce = (struct ciss_error_info *)&(cc->sg[0]);
3551 ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
3552 ciss_enqueue_complete(cr, &qh);
3556 if ((cr = ciss_dequeue_complete(sc, &qh)) == NULL)
3560 * If the request has a callback, invoke it.
3562 if (cr->cr_complete != NULL) {
3563 cr->cr_complete(cr);
3568 * If someone is sleeping on this request, wake them up.
3570 if (cr->cr_flags & CISS_REQ_SLEEP) {
3571 cr->cr_flags &= ~CISS_REQ_SLEEP;
3578 /************************************************************************
3579 * Request a notification response from the adapter.
3581 * If (cr) is NULL, this is the first request of the adapter, so
3582 * reset the adapter's message pointer and start with the oldest
3583 * message available.
3586 ciss_notify_event(struct ciss_softc *sc)
3588 struct ciss_request *cr;
3589 struct ciss_command *cc;
3590 struct ciss_notify_cdb *cnc;
3595 cr = sc->ciss_periodic_notify;
3597 /* get a request if we don't already have one */
3599 if ((error = ciss_get_request(sc, &cr)) != 0) {
3600 debug(0, "can't get notify event request");
3603 sc->ciss_periodic_notify = cr;
3604 cr->cr_complete = ciss_notify_complete;
3605 debug(1, "acquired request %d", cr->cr_tag);
3609 * Get a databuffer if we don't already have one, note that the
3610 * adapter command wants a larger buffer than the actual
3613 if (cr->cr_data == NULL) {
3614 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3615 debug(0, "can't get notify event request buffer");
3619 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3622 /* re-setup the request's command (since we never release it) XXX overkill*/
3623 ciss_preen_command(cr);
3625 /* (re)build the notify event command */
3627 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3628 cc->header.address.physical.bus = 0;
3629 cc->header.address.physical.target = 0;
3631 cc->cdb.cdb_length = sizeof(*cnc);
3632 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3633 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3634 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3635 cc->cdb.timeout = 0; /* no timeout, we hope */
3637 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3638 bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
3639 cnc->opcode = CISS_OPCODE_READ;
3640 cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
3641 cnc->timeout = 0; /* no timeout, we hope */
3642 cnc->synchronous = 0;
3644 cnc->seek_to_oldest = 0;
3645 if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
3649 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3651 /* submit the request */
3652 error = ciss_start(cr);
3657 if (cr->cr_data != NULL)
3658 free(cr->cr_data, CISS_MALLOC_CLASS);
3659 ciss_release_request(cr);
3661 sc->ciss_periodic_notify = NULL;
3662 debug(0, "can't submit notify event request");
3663 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3665 debug(1, "notify event submitted");
3666 sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
3671 ciss_notify_complete(struct ciss_request *cr)
3673 struct ciss_command *cc;
3674 struct ciss_notify *cn;
3675 struct ciss_softc *sc;
3681 cn = (struct ciss_notify *)cr->cr_data;
3685 * Report request results, decode status.
3687 ciss_report_request(cr, &command_status, &scsi_status);
3690 * Abort the chain on a fatal error.
3692 * XXX which of these are actually errors?
3694 if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
3695 (command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
3696 (command_status != CISS_CMD_STATUS_TIMEOUT)) { /* XXX timeout? */
3697 ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
3698 ciss_name_command_status(command_status));
3699 ciss_release_request(cr);
3700 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3705 * If the adapter gave us a text message, print it.
3707 if (cn->message[0] != 0)
3708 ciss_printf(sc, "*** %.80s\n", cn->message);
3710 debug(0, "notify event class %d subclass %d detail %d",
3711 cn->class, cn->subclass, cn->detail);
3714 * If the response indicates that the notifier has been aborted,
3715 * release the notifier command.
3717 if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
3718 (cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
3719 (cn->detail == 1)) {
3720 debug(0, "notifier exiting");
3721 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3722 ciss_release_request(cr);
3723 sc->ciss_periodic_notify = NULL;
3724 wakeup(&sc->ciss_periodic_notify);
3726 /* Handle notify events in a kernel thread */
3727 ciss_enqueue_notify(cr);
3728 sc->ciss_periodic_notify = NULL;
3729 wakeup(&sc->ciss_periodic_notify);
3730 wakeup(&sc->ciss_notify);
3733 * Send a new notify event command, if we're not aborting.
3735 if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
3736 ciss_notify_event(sc);
3740 /************************************************************************
3741 * Abort the Notify Event chain.
3743 * Note that we can't just abort the command in progress; we have to
3744 * explicitly issue an Abort Notify Event command in order for the
3745 * adapter to clean up correctly.
3747 * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
3748 * the chain will not restart itself.
3751 ciss_notify_abort(struct ciss_softc *sc)
3753 struct ciss_request *cr;
3754 struct ciss_command *cc;
3755 struct ciss_notify_cdb *cnc;
3756 int error, command_status, scsi_status;
3763 /* verify that there's an outstanding command */
3764 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3767 /* get a command to issue the abort with */
3768 if ((error = ciss_get_request(sc, &cr)))
3771 /* get a buffer for the result */
3772 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3773 debug(0, "can't get notify event request buffer");
3777 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3781 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3782 cc->header.address.physical.bus = 0;
3783 cc->header.address.physical.target = 0;
3784 cc->cdb.cdb_length = sizeof(*cnc);
3785 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3786 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3787 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3788 cc->cdb.timeout = 0; /* no timeout, we hope */
3790 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3791 bzero(cnc, sizeof(*cnc));
3792 cnc->opcode = CISS_OPCODE_WRITE;
3793 cnc->command = CISS_COMMAND_ABORT_NOTIFY;
3794 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3796 ciss_print_request(cr);
3799 * Submit the request and wait for it to complete.
3801 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3802 ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
3809 ciss_report_request(cr, &command_status, &scsi_status);
3810 switch(command_status) {
3811 case CISS_CMD_STATUS_SUCCESS:
3813 case CISS_CMD_STATUS_INVALID_COMMAND:
3815 * Some older adapters don't support the CISS version of this
3816 * command. Fall back to using the BMIC version.
3818 error = ciss_notify_abort_bmic(sc);
3823 case CISS_CMD_STATUS_TARGET_STATUS:
3825 * This can happen if the adapter thinks there wasn't an outstanding
3826 * Notify Event command but we did. We clean up here.
3828 if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
3829 if (sc->ciss_periodic_notify != NULL)
3830 ciss_release_request(sc->ciss_periodic_notify);
3837 ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
3838 ciss_name_command_status(command_status));
3844 * Sleep waiting for the notifier command to complete. Note
3845 * that if it doesn't, we may end up in a bad situation, since
3846 * the adapter may deliver it later. Also note that the adapter
3847 * requires the Notify Event command to be cancelled in order to
3848 * maintain internal bookkeeping.
3850 while (sc->ciss_periodic_notify != NULL) {
3851 error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
3852 if (error == EWOULDBLOCK) {
3853 ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
3859 /* release the cancel request */
3861 if (cr->cr_data != NULL)
3862 free(cr->cr_data, CISS_MALLOC_CLASS);
3863 ciss_release_request(cr);
3866 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3870 /************************************************************************
3871 * Abort the Notify Event chain using a BMIC command.
3874 ciss_notify_abort_bmic(struct ciss_softc *sc)
3876 struct ciss_request *cr;
3877 int error, command_status;
3884 /* verify that there's an outstanding command */
3885 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3889 * Build a BMIC command to cancel the Notify on Event command.
3891 * Note that we are sending a CISS opcode here. Odd.
3893 if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
3898 * Submit the request and wait for it to complete.
3900 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3901 ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
3908 ciss_report_request(cr, &command_status, NULL);
3909 switch(command_status) {
3910 case CISS_CMD_STATUS_SUCCESS:
3913 ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
3914 ciss_name_command_status(command_status));
3921 ciss_release_request(cr);
3925 /************************************************************************
3926 * Handle rescanning all the logical volumes when a notify event
3927 * causes the drives to come online or offline.
3930 ciss_notify_rescan_logical(struct ciss_softc *sc)
3932 struct ciss_lun_report *cll;
3933 struct ciss_ldrive *ld;
3937 * We must rescan all logical volumes to get the right logical
3940 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
3941 sc->ciss_cfg->max_logical_supported);
3945 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
3948 * Delete any of the drives which were destroyed by the
3951 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
3952 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
3953 ld = &sc->ciss_logical[i][j];
3955 if (ld->cl_update == 0)
3958 if (ld->cl_status != CISS_LD_ONLINE) {
3959 ciss_cam_rescan_target(sc, i, j);
3962 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
3964 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
3966 ld->cl_ldrive = NULL;
3967 ld->cl_lstatus = NULL;
3973 * Scan for new drives.
3975 for (i = 0; i < ndrives; i++) {
3978 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
3979 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
3980 ld = &sc->ciss_logical[bus][target];
3982 if (ld->cl_update == 0)
3986 ld->cl_address = cll->lun[i];
3987 ld->cl_controller = &sc->ciss_controllers[bus];
3988 if (ciss_identify_logical(sc, ld) == 0) {
3989 ciss_cam_rescan_target(sc, bus, target);
3992 free(cll, CISS_MALLOC_CLASS);
3995 /************************************************************************
3996 * Handle a notify event relating to the status of a logical drive.
3998 * XXX need to be able to defer some of these to properly handle
3999 * calling the "ID Physical drive" command, unless the 'extended'
4000 * drive IDs are always in BIG_MAP format.
4003 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
4005 struct ciss_ldrive *ld;
4011 bus = cn->device.physical.bus;
4012 target = cn->data.logical_status.logical_drive;
4013 ld = &sc->ciss_logical[bus][target];
4015 switch (cn->subclass) {
4016 case CISS_NOTIFY_LOGICAL_STATUS:
4017 switch (cn->detail) {
4019 ciss_name_device(sc, bus, target);
4020 ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
4021 cn->data.logical_status.logical_drive, ld->cl_name,
4022 ciss_name_ldrive_status(cn->data.logical_status.previous_state),
4023 ciss_name_ldrive_status(cn->data.logical_status.new_state),
4024 cn->data.logical_status.spare_state,
4025 "\20\1configured\2rebuilding\3failed\4in use\5available\n");
4028 * Update our idea of the drive's status.
4030 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4031 if (ld->cl_lstatus != NULL)
4032 ld->cl_lstatus->status = cn->data.logical_status.new_state;
4035 * Have CAM rescan the drive if its status has changed.
4037 rescan_ld = (cn->data.logical_status.previous_state !=
4038 cn->data.logical_status.new_state) ? 1 : 0;
4041 ciss_notify_rescan_logical(sc);
4046 case 1: /* logical drive has recognised new media, needs Accept Media Exchange */
4047 ciss_name_device(sc, bus, target);
4048 ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
4049 cn->data.logical_status.logical_drive, ld->cl_name);
4050 ciss_accept_media(sc, ld);
4053 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4054 ciss_notify_rescan_logical(sc);
4059 ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
4060 cn->data.rebuild_aborted.logical_drive,
4062 (cn->detail == 2) ? "read" : "write");
4067 case CISS_NOTIFY_LOGICAL_ERROR:
4068 if (cn->detail == 0) {
4069 ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
4070 cn->data.io_error.logical_drive,
4072 cn->data.io_error.failure_bus,
4073 cn->data.io_error.failure_drive);
4074 /* XXX should we take the drive down at this point, or will we be told? */
4078 case CISS_NOTIFY_LOGICAL_SURFACE:
4079 if (cn->detail == 0)
4080 ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
4081 cn->data.consistency_completed.logical_drive,
4087 /************************************************************************
4088 * Handle a notify event relating to the status of a physical drive.
4091 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
4095 /************************************************************************
4096 * Handle a notify event relating to the status of a physical drive.
4099 ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
4101 struct ciss_lun_report *cll = NULL;
4104 switch (cn->subclass) {
4105 case CISS_NOTIFY_HOTPLUG_PHYSICAL:
4106 case CISS_NOTIFY_HOTPLUG_NONDISK:
4107 bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
4109 CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
4111 if (cn->detail == 0) {
4113 * Mark the device offline so that it'll start producing selection
4114 * timeouts to the upper layer.
4116 if ((bus >= 0) && (target >= 0))
4117 sc->ciss_physical[bus][target].cp_online = 0;
4120 * Rescan the physical lun list for new items
4122 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
4123 sc->ciss_cfg->max_physical_supported);
4125 ciss_printf(sc, "Warning, cannot get physical lun list\n");
4128 ciss_filter_physical(sc, cll);
4133 ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
4138 free(cll, CISS_MALLOC_CLASS);
4141 /************************************************************************
4142 * Handle deferred processing of notify events. Notify events may need
4143 * sleep which is unsafe during an interrupt.
4146 ciss_notify_thread(void *arg)
4148 struct ciss_softc *sc;
4149 struct ciss_request *cr;
4150 struct ciss_notify *cn;
4152 sc = (struct ciss_softc *)arg;
4153 #if __FreeBSD_version >= 500000
4154 mtx_lock(&sc->ciss_mtx);
4158 if (STAILQ_EMPTY(&sc->ciss_notify) != 0 &&
4159 (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
4160 msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
4163 if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
4166 cr = ciss_dequeue_notify(sc);
4170 cn = (struct ciss_notify *)cr->cr_data;
4172 switch (cn->class) {
4173 case CISS_NOTIFY_HOTPLUG:
4174 ciss_notify_hotplug(sc, cn);
4176 case CISS_NOTIFY_LOGICAL:
4177 ciss_notify_logical(sc, cn);
4179 case CISS_NOTIFY_PHYSICAL:
4180 ciss_notify_physical(sc, cn);
4184 ciss_release_request(cr);
4187 sc->ciss_notify_thread = NULL;
4188 wakeup(&sc->ciss_notify_thread);
4190 #if __FreeBSD_version >= 500000
4191 mtx_unlock(&sc->ciss_mtx);
4196 /************************************************************************
4197 * Start the notification kernel thread.
4200 ciss_spawn_notify_thread(struct ciss_softc *sc)
4203 #if __FreeBSD_version > 500005
4204 if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4205 &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
4206 device_get_unit(sc->ciss_dev)))
4208 if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4209 &sc->ciss_notify_thread, "ciss_notify%d",
4210 device_get_unit(sc->ciss_dev)))
4212 panic("Could not create notify thread\n");
4215 /************************************************************************
4216 * Kill the notification kernel thread.
4219 ciss_kill_notify_thread(struct ciss_softc *sc)
4222 if (sc->ciss_notify_thread == NULL)
4225 sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
4226 wakeup(&sc->ciss_notify);
4227 msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
4230 /************************************************************************
4234 ciss_print_request(struct ciss_request *cr)
4236 struct ciss_softc *sc;
4237 struct ciss_command *cc;
4243 ciss_printf(sc, "REQUEST @ %p\n", cr);
4244 ciss_printf(sc, " data %p/%d tag %d flags %b\n",
4245 cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
4246 "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
4247 ciss_printf(sc, " sg list/total %d/%d host tag 0x%x\n",
4248 cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
4249 switch(cc->header.address.mode.mode) {
4250 case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
4251 case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
4252 ciss_printf(sc, " physical bus %d target %d\n",
4253 cc->header.address.physical.bus, cc->header.address.physical.target);
4255 case CISS_HDR_ADDRESS_MODE_LOGICAL:
4256 ciss_printf(sc, " logical unit %d\n", cc->header.address.logical.lun);
4259 ciss_printf(sc, " %s cdb length %d type %s attribute %s\n",
4260 (cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
4261 (cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
4262 (cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
4264 (cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
4265 (cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
4266 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
4267 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
4268 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
4269 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
4270 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
4271 ciss_printf(sc, " %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
4273 if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
4274 /* XXX print error info */
4276 /* since we don't use chained s/g, don't support it here */
4277 for (i = 0; i < cc->header.sg_in_list; i++) {
4279 ciss_printf(sc, " ");
4280 printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
4281 if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
4287 /************************************************************************
4288 * Print information about the status of a logical drive.
4291 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
4295 if (ld->cl_lstatus == NULL) {
4296 printf("does not exist\n");
4300 /* print drive status */
4301 switch(ld->cl_lstatus->status) {
4302 case CISS_LSTATUS_OK:
4305 case CISS_LSTATUS_INTERIM_RECOVERY:
4306 printf("in interim recovery mode\n");
4308 case CISS_LSTATUS_READY_RECOVERY:
4309 printf("ready to begin recovery\n");
4311 case CISS_LSTATUS_RECOVERING:
4312 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4313 target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4314 printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
4315 bus, target, ld->cl_lstatus->blocks_to_recover);
4317 case CISS_LSTATUS_EXPANDING:
4318 printf("being expanded, %u blocks remaining\n",
4319 ld->cl_lstatus->blocks_to_recover);
4321 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4322 printf("queued for expansion\n");
4324 case CISS_LSTATUS_FAILED:
4325 printf("queued for expansion\n");
4327 case CISS_LSTATUS_WRONG_PDRIVE:
4328 printf("wrong physical drive inserted\n");
4330 case CISS_LSTATUS_MISSING_PDRIVE:
4331 printf("missing a needed physical drive\n");
4333 case CISS_LSTATUS_BECOMING_READY:
4334 printf("becoming ready\n");
4338 /* print failed physical drives */
4339 for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
4340 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
4341 target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
4344 ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
4345 ld->cl_lstatus->drive_failure_map[i]);
4350 #include "opt_ddb.h"
4352 #include <ddb/ddb.h>
4353 /************************************************************************
4354 * Print information about the controller/driver.
4357 ciss_print_adapter(struct ciss_softc *sc)
4361 ciss_printf(sc, "ADAPTER:\n");
4362 for (i = 0; i < CISSQ_COUNT; i++) {
4363 ciss_printf(sc, "%s %d/%d\n",
4365 i == 1 ? "busy" : "complete",
4366 sc->ciss_qstat[i].q_length,
4367 sc->ciss_qstat[i].q_max);
4369 ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
4370 ciss_printf(sc, "flags %b\n", sc->ciss_flags,
4371 "\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
4373 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
4374 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
4375 ciss_printf(sc, "LOGICAL DRIVE %d: ", i);
4376 ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
4380 /* XXX Should physical drives be printed out here? */
4382 for (i = 1; i < sc->ciss_max_requests; i++)
4383 ciss_print_request(sc->ciss_request + i);
4387 DB_COMMAND(ciss_prt, db_ciss_prt)
4389 struct ciss_softc *sc;
4393 dc = devclass_find("ciss");
4395 printf("%s: can't find devclass!\n", __func__);
4398 maxciss = devclass_get_maxunit(dc);
4399 for (i = 0; i < maxciss; i++) {
4400 sc = devclass_get_softc(dc, i);
4401 ciss_print_adapter(sc);
4407 /************************************************************************
4408 * Return a name for a logical drive status value.
4411 ciss_name_ldrive_status(int status)
4414 case CISS_LSTATUS_OK:
4416 case CISS_LSTATUS_FAILED:
4418 case CISS_LSTATUS_NOT_CONFIGURED:
4419 return("not configured");
4420 case CISS_LSTATUS_INTERIM_RECOVERY:
4421 return("interim recovery");
4422 case CISS_LSTATUS_READY_RECOVERY:
4423 return("ready for recovery");
4424 case CISS_LSTATUS_RECOVERING:
4425 return("recovering");
4426 case CISS_LSTATUS_WRONG_PDRIVE:
4427 return("wrong physical drive inserted");
4428 case CISS_LSTATUS_MISSING_PDRIVE:
4429 return("missing physical drive");
4430 case CISS_LSTATUS_EXPANDING:
4431 return("expanding");
4432 case CISS_LSTATUS_BECOMING_READY:
4433 return("becoming ready");
4434 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4435 return("queued for expansion");
4437 return("unknown status");
4440 /************************************************************************
4441 * Return an online/offline/nonexistent value for a logical drive
4445 ciss_decode_ldrive_status(int status)
4448 case CISS_LSTATUS_NOT_CONFIGURED:
4449 return(CISS_LD_NONEXISTENT);
4451 case CISS_LSTATUS_OK:
4452 case CISS_LSTATUS_INTERIM_RECOVERY:
4453 case CISS_LSTATUS_READY_RECOVERY:
4454 case CISS_LSTATUS_RECOVERING:
4455 case CISS_LSTATUS_EXPANDING:
4456 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4457 return(CISS_LD_ONLINE);
4459 case CISS_LSTATUS_FAILED:
4460 case CISS_LSTATUS_WRONG_PDRIVE:
4461 case CISS_LSTATUS_MISSING_PDRIVE:
4462 case CISS_LSTATUS_BECOMING_READY:
4464 return(CISS_LD_OFFLINE);
4469 /************************************************************************
4470 * Return a name for a logical drive's organisation.
4473 ciss_name_ldrive_org(int org)
4476 case CISS_LDRIVE_RAID0:
4478 case CISS_LDRIVE_RAID1:
4479 return("RAID 1(1+0)");
4480 case CISS_LDRIVE_RAID4:
4482 case CISS_LDRIVE_RAID5:
4484 case CISS_LDRIVE_RAID51:
4486 case CISS_LDRIVE_RAIDADG:
4492 /************************************************************************
4493 * Return a name for a command status value.
4496 ciss_name_command_status(int status)
4499 case CISS_CMD_STATUS_SUCCESS:
4501 case CISS_CMD_STATUS_TARGET_STATUS:
4502 return("target status");
4503 case CISS_CMD_STATUS_DATA_UNDERRUN:
4504 return("data underrun");
4505 case CISS_CMD_STATUS_DATA_OVERRUN:
4506 return("data overrun");
4507 case CISS_CMD_STATUS_INVALID_COMMAND:
4508 return("invalid command");
4509 case CISS_CMD_STATUS_PROTOCOL_ERROR:
4510 return("protocol error");
4511 case CISS_CMD_STATUS_HARDWARE_ERROR:
4512 return("hardware error");
4513 case CISS_CMD_STATUS_CONNECTION_LOST:
4514 return("connection lost");
4515 case CISS_CMD_STATUS_ABORTED:
4517 case CISS_CMD_STATUS_ABORT_FAILED:
4518 return("abort failed");
4519 case CISS_CMD_STATUS_UNSOLICITED_ABORT:
4520 return("unsolicited abort");
4521 case CISS_CMD_STATUS_TIMEOUT:
4523 case CISS_CMD_STATUS_UNABORTABLE:
4524 return("unabortable");
4526 return("unknown status");
4529 /************************************************************************
4530 * Handle an open on the control device.
4533 ciss_open(struct cdev *dev, int flags, int fmt, struct thread *p)
4535 struct ciss_softc *sc;
4539 sc = (struct ciss_softc *)dev->si_drv1;
4541 /* we might want to veto if someone already has us open */
4543 mtx_lock(&sc->ciss_mtx);
4544 sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
4545 mtx_unlock(&sc->ciss_mtx);
4549 /************************************************************************
4550 * Handle the last close on the control device.
4553 ciss_close(struct cdev *dev, int flags, int fmt, struct thread *p)
4555 struct ciss_softc *sc;
4559 sc = (struct ciss_softc *)dev->si_drv1;
4561 mtx_lock(&sc->ciss_mtx);
4562 sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
4563 mtx_unlock(&sc->ciss_mtx);
4567 /********************************************************************************
4568 * Handle adapter-specific control operations.
4570 * Note that the API here is compatible with the Linux driver, in order to
4571 * simplify the porting of Compaq's userland tools.
4574 ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *p)
4576 struct ciss_softc *sc;
4577 IOCTL_Command_struct *ioc = (IOCTL_Command_struct *)addr;
4579 IOCTL_Command_struct32 *ioc32 = (IOCTL_Command_struct32 *)addr;
4580 IOCTL_Command_struct ioc_swab;
4586 sc = (struct ciss_softc *)dev->si_drv1;
4588 mtx_lock(&sc->ciss_mtx);
4591 case CCISS_GETQSTATS:
4593 union ciss_statrequest *cr = (union ciss_statrequest *)addr;
4595 switch (cr->cs_item) {
4598 bcopy(&sc->ciss_qstat[cr->cs_item], &cr->cs_qstat,
4599 sizeof(struct ciss_qstat));
4609 case CCISS_GETPCIINFO:
4611 cciss_pci_info_struct *pis = (cciss_pci_info_struct *)addr;
4613 pis->bus = pci_get_bus(sc->ciss_dev);
4614 pis->dev_fn = pci_get_slot(sc->ciss_dev);
4615 pis->board_id = (pci_get_subvendor(sc->ciss_dev) << 16) |
4616 pci_get_subdevice(sc->ciss_dev);
4621 case CCISS_GETINTINFO:
4623 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4625 cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
4626 cis->count = sc->ciss_cfg->interrupt_coalesce_count;
4631 case CCISS_SETINTINFO:
4633 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4635 if ((cis->delay == 0) && (cis->count == 0)) {
4641 * XXX apparently this is only safe if the controller is idle,
4642 * we should suspend it before doing this.
4644 sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
4645 sc->ciss_cfg->interrupt_coalesce_count = cis->count;
4647 if (ciss_update_config(sc))
4650 /* XXX resume the controller here */
4654 case CCISS_GETNODENAME:
4655 bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
4656 sizeof(NodeName_type));
4659 case CCISS_SETNODENAME:
4660 bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
4661 sizeof(NodeName_type));
4662 if (ciss_update_config(sc))
4666 case CCISS_GETHEARTBEAT:
4667 *(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
4670 case CCISS_GETBUSTYPES:
4671 *(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
4674 case CCISS_GETFIRMVER:
4675 bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
4676 sizeof(FirmwareVer_type));
4679 case CCISS_GETDRIVERVER:
4680 *(DriverVer_type *)addr = CISS_DRIVER_VERSION;
4683 case CCISS_REVALIDVOLS:
4685 * This is a bit ugly; to do it "right" we really need
4686 * to find any disks that have changed, kick CAM off them,
4687 * then rescan only these disks. It'd be nice if they
4688 * a) told us which disk(s) they were going to play with,
4689 * and b) which ones had arrived. 8(
4694 case CCISS_PASSTHRU32:
4695 ioc_swab.LUN_info = ioc32->LUN_info;
4696 ioc_swab.Request = ioc32->Request;
4697 ioc_swab.error_info = ioc32->error_info;
4698 ioc_swab.buf_size = ioc32->buf_size;
4699 ioc_swab.buf = (u_int8_t *)(uintptr_t)ioc32->buf;
4704 case CCISS_PASSTHRU:
4705 error = ciss_user_command(sc, ioc);
4709 debug(0, "unknown ioctl 0x%lx", cmd);
4711 debug(1, "CCISS_GETPCIINFO: 0x%lx", CCISS_GETPCIINFO);
4712 debug(1, "CCISS_GETINTINFO: 0x%lx", CCISS_GETINTINFO);
4713 debug(1, "CCISS_SETINTINFO: 0x%lx", CCISS_SETINTINFO);
4714 debug(1, "CCISS_GETNODENAME: 0x%lx", CCISS_GETNODENAME);
4715 debug(1, "CCISS_SETNODENAME: 0x%lx", CCISS_SETNODENAME);
4716 debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
4717 debug(1, "CCISS_GETBUSTYPES: 0x%lx", CCISS_GETBUSTYPES);
4718 debug(1, "CCISS_GETFIRMVER: 0x%lx", CCISS_GETFIRMVER);
4719 debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
4720 debug(1, "CCISS_REVALIDVOLS: 0x%lx", CCISS_REVALIDVOLS);
4721 debug(1, "CCISS_PASSTHRU: 0x%lx", CCISS_PASSTHRU);
4727 mtx_unlock(&sc->ciss_mtx);