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 int ciss_name_device(struct ciss_softc *sc, int bus, int target);
185 /* periodic status monitoring */
186 static void ciss_periodic(void *arg);
187 static void ciss_nop_complete(struct ciss_request *cr);
188 static void ciss_disable_adapter(struct ciss_softc *sc);
189 static void ciss_notify_event(struct ciss_softc *sc);
190 static void ciss_notify_complete(struct ciss_request *cr);
191 static int ciss_notify_abort(struct ciss_softc *sc);
192 static int ciss_notify_abort_bmic(struct ciss_softc *sc);
193 static void ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
194 static void ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
195 static void ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
197 /* debugging output */
198 static void ciss_print_request(struct ciss_request *cr);
199 static void ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
200 static const char *ciss_name_ldrive_status(int status);
201 static int ciss_decode_ldrive_status(int status);
202 static const char *ciss_name_ldrive_org(int org);
203 static const char *ciss_name_command_status(int status);
208 static device_method_t ciss_methods[] = {
209 /* Device interface */
210 DEVMETHOD(device_probe, ciss_probe),
211 DEVMETHOD(device_attach, ciss_attach),
212 DEVMETHOD(device_detach, ciss_detach),
213 DEVMETHOD(device_shutdown, ciss_shutdown),
217 static driver_t ciss_pci_driver = {
220 sizeof(struct ciss_softc)
223 static devclass_t ciss_devclass;
224 DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
225 MODULE_DEPEND(ciss, cam, 1, 1, 1);
226 MODULE_DEPEND(ciss, pci, 1, 1, 1);
229 * Control device interface.
231 static d_open_t ciss_open;
232 static d_close_t ciss_close;
233 static d_ioctl_t ciss_ioctl;
235 static struct cdevsw ciss_cdevsw = {
236 .d_version = D_VERSION,
239 .d_close = ciss_close,
240 .d_ioctl = ciss_ioctl,
245 * This tunable can be set at boot time and controls whether physical devices
246 * that are marked hidden by the firmware should be exposed anyways.
248 static unsigned int ciss_expose_hidden_physical = 0;
249 TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
251 static unsigned int ciss_nop_message_heartbeat = 0;
252 TUNABLE_INT("hw.ciss.nop_message_heartbeat", &ciss_nop_message_heartbeat);
255 * This tunable can force a particular transport to be used:
258 * 2 : force performant
260 static int ciss_force_transport = 0;
261 TUNABLE_INT("hw.ciss.force_transport", &ciss_force_transport);
264 * This tunable can force a particular interrupt delivery method to be used:
269 static int ciss_force_interrupt = 0;
270 TUNABLE_INT("hw.ciss.force_interrupt", &ciss_force_interrupt);
272 /************************************************************************
273 * CISS adapters amazingly don't have a defined programming interface
274 * value. (One could say some very despairing things about PCI and
275 * people just not getting the general idea.) So we are forced to
276 * stick with matching against subvendor/subdevice, and thus have to
277 * be updated for every new CISS adapter that appears.
279 #define CISS_BOARD_UNKNWON 0
280 #define CISS_BOARD_SA5 1
281 #define CISS_BOARD_SA5B 2
282 #define CISS_BOARD_NOMSI (1<<4)
283 #define CISS_BOARD_SIMPLE (1<<5)
291 } ciss_vendor_data[] = {
292 { 0x0e11, 0x4070, CISS_BOARD_SA5|CISS_BOARD_NOMSI|CISS_BOARD_SIMPLE,
293 "Compaq Smart Array 5300" },
294 { 0x0e11, 0x4080, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "Compaq Smart Array 5i" },
295 { 0x0e11, 0x4082, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "Compaq Smart Array 532" },
296 { 0x0e11, 0x4083, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "HP Smart Array 5312" },
297 { 0x0e11, 0x4091, CISS_BOARD_SA5, "HP Smart Array 6i" },
298 { 0x0e11, 0x409A, CISS_BOARD_SA5, "HP Smart Array 641" },
299 { 0x0e11, 0x409B, CISS_BOARD_SA5, "HP Smart Array 642" },
300 { 0x0e11, 0x409C, CISS_BOARD_SA5, "HP Smart Array 6400" },
301 { 0x0e11, 0x409D, CISS_BOARD_SA5, "HP Smart Array 6400 EM" },
302 { 0x103C, 0x3211, CISS_BOARD_SA5, "HP Smart Array E200i" },
303 { 0x103C, 0x3212, CISS_BOARD_SA5, "HP Smart Array E200" },
304 { 0x103C, 0x3213, CISS_BOARD_SA5, "HP Smart Array E200i" },
305 { 0x103C, 0x3214, CISS_BOARD_SA5, "HP Smart Array E200i" },
306 { 0x103C, 0x3215, CISS_BOARD_SA5, "HP Smart Array E200i" },
307 { 0x103C, 0x3220, CISS_BOARD_SA5, "HP Smart Array" },
308 { 0x103C, 0x3222, CISS_BOARD_SA5, "HP Smart Array" },
309 { 0x103C, 0x3223, CISS_BOARD_SA5, "HP Smart Array P800" },
310 { 0x103C, 0x3225, CISS_BOARD_SA5, "HP Smart Array P600" },
311 { 0x103C, 0x3230, CISS_BOARD_SA5, "HP Smart Array" },
312 { 0x103C, 0x3231, CISS_BOARD_SA5, "HP Smart Array" },
313 { 0x103C, 0x3232, CISS_BOARD_SA5, "HP Smart Array" },
314 { 0x103C, 0x3233, CISS_BOARD_SA5, "HP Smart Array" },
315 { 0x103C, 0x3234, CISS_BOARD_SA5, "HP Smart Array P400" },
316 { 0x103C, 0x3235, CISS_BOARD_SA5, "HP Smart Array P400i" },
317 { 0x103C, 0x3236, CISS_BOARD_SA5, "HP Smart Array" },
318 { 0x103C, 0x3237, CISS_BOARD_SA5, "HP Smart Array E500" },
319 { 0x103C, 0x3238, CISS_BOARD_SA5, "HP Smart Array" },
320 { 0x103C, 0x3239, CISS_BOARD_SA5, "HP Smart Array" },
321 { 0x103C, 0x323A, CISS_BOARD_SA5, "HP Smart Array" },
322 { 0x103C, 0x323B, CISS_BOARD_SA5, "HP Smart Array" },
323 { 0x103C, 0x323C, CISS_BOARD_SA5, "HP Smart Array" },
324 { 0x103C, 0x323D, CISS_BOARD_SA5, "HP Smart Array P700m" },
325 { 0x103C, 0x3241, CISS_BOARD_SA5, "HP Smart Array P212" },
326 { 0x103C, 0x3243, CISS_BOARD_SA5, "HP Smart Array P410" },
327 { 0x103C, 0x3245, CISS_BOARD_SA5, "HP Smart Array P410i" },
328 { 0x103C, 0x3247, CISS_BOARD_SA5, "HP Smart Array P411" },
329 { 0x103C, 0x3249, CISS_BOARD_SA5, "HP Smart Array P812" },
330 { 0x103C, 0x324A, CISS_BOARD_SA5, "HP Smart Array P712m" },
331 { 0x103C, 0x324B, CISS_BOARD_SA5, "HP Smart Array" },
332 { 0x103C, 0x3350, CISS_BOARD_SA5, "HP Smart Array P222" },
333 { 0x103C, 0x3351, CISS_BOARD_SA5, "HP Smart Array P420" },
334 { 0x103C, 0x3352, CISS_BOARD_SA5, "HP Smart Array P421" },
335 { 0x103C, 0x3353, CISS_BOARD_SA5, "HP Smart Array P822" },
336 { 0x103C, 0x3354, CISS_BOARD_SA5, "HP Smart Array P420i" },
337 { 0x103C, 0x3355, CISS_BOARD_SA5, "HP Smart Array P220i" },
338 { 0x103C, 0x3356, CISS_BOARD_SA5, "HP Smart Array P721m" },
339 { 0x103C, 0x1920, CISS_BOARD_SA5, "HP Smart Array P430i" },
340 { 0x103C, 0x1921, CISS_BOARD_SA5, "HP Smart Array P830i" },
341 { 0x103C, 0x1922, CISS_BOARD_SA5, "HP Smart Array P430" },
342 { 0x103C, 0x1923, CISS_BOARD_SA5, "HP Smart Array P431" },
343 { 0x103C, 0x1924, CISS_BOARD_SA5, "HP Smart Array P830" },
344 { 0x103C, 0x1926, CISS_BOARD_SA5, "HP Smart Array P731m" },
345 { 0x103C, 0x1928, CISS_BOARD_SA5, "HP Smart Array P230i" },
346 { 0x103C, 0x1929, CISS_BOARD_SA5, "HP Smart Array P530" },
347 { 0x103C, 0x192A, CISS_BOARD_SA5, "HP Smart Array P531" },
351 /************************************************************************
352 * Find a match for the device in our list of known adapters.
355 ciss_lookup(device_t dev)
359 for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
360 if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
361 (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
367 /************************************************************************
368 * Match a known CISS adapter.
371 ciss_probe(device_t dev)
375 i = ciss_lookup(dev);
377 device_set_desc(dev, ciss_vendor_data[i].desc);
378 return(BUS_PROBE_DEFAULT);
383 /************************************************************************
384 * Attach the driver to this adapter.
387 ciss_attach(device_t dev)
389 struct ciss_softc *sc;
395 /* print structure/union sizes */
396 debug_struct(ciss_command);
397 debug_struct(ciss_header);
398 debug_union(ciss_device_address);
399 debug_struct(ciss_cdb);
400 debug_struct(ciss_report_cdb);
401 debug_struct(ciss_notify_cdb);
402 debug_struct(ciss_notify);
403 debug_struct(ciss_message_cdb);
404 debug_struct(ciss_error_info_pointer);
405 debug_struct(ciss_error_info);
406 debug_struct(ciss_sg_entry);
407 debug_struct(ciss_config_table);
408 debug_struct(ciss_bmic_cdb);
409 debug_struct(ciss_bmic_id_ldrive);
410 debug_struct(ciss_bmic_id_lstatus);
411 debug_struct(ciss_bmic_id_table);
412 debug_struct(ciss_bmic_id_pdrive);
413 debug_struct(ciss_bmic_blink_pdrive);
414 debug_struct(ciss_bmic_flush_cache);
415 debug_const(CISS_MAX_REQUESTS);
416 debug_const(CISS_MAX_LOGICAL);
417 debug_const(CISS_INTERRUPT_COALESCE_DELAY);
418 debug_const(CISS_INTERRUPT_COALESCE_COUNT);
419 debug_const(CISS_COMMAND_ALLOC_SIZE);
420 debug_const(CISS_COMMAND_SG_LENGTH);
422 debug_type(cciss_pci_info_struct);
423 debug_type(cciss_coalint_struct);
424 debug_type(cciss_coalint_struct);
425 debug_type(NodeName_type);
426 debug_type(NodeName_type);
427 debug_type(Heartbeat_type);
428 debug_type(BusTypes_type);
429 debug_type(FirmwareVer_type);
430 debug_type(DriverVer_type);
431 debug_type(IOCTL_Command_struct);
434 sc = device_get_softc(dev);
436 mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
437 callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);
440 * Do PCI-specific init.
442 if ((error = ciss_init_pci(sc)) != 0)
446 * Initialise driver queues.
449 ciss_initq_notify(sc);
452 * Initalize device sysctls.
454 ciss_init_sysctl(sc);
457 * Initialise command/request pool.
459 if ((error = ciss_init_requests(sc)) != 0)
463 * Get adapter information.
465 if ((error = ciss_identify_adapter(sc)) != 0)
469 * Find all the physical devices.
471 if ((error = ciss_init_physical(sc)) != 0)
475 * Build our private table of logical devices.
477 if ((error = ciss_init_logical(sc)) != 0)
481 * Enable interrupts so that the CAM scan can complete.
483 CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
486 * Initialise the CAM interface.
488 if ((error = ciss_cam_init(sc)) != 0)
492 * Start the heartbeat routine and event chain.
497 * Create the control device.
499 sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
500 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
501 "ciss%d", device_get_unit(sc->ciss_dev));
502 sc->ciss_dev_t->si_drv1 = sc;
505 * The adapter is running; synchronous commands can now sleep
506 * waiting for an interrupt to signal completion.
508 sc->ciss_flags |= CISS_FLAG_RUNNING;
510 ciss_spawn_notify_thread(sc);
515 /* ciss_free() expects the mutex to be held */
516 mtx_lock(&sc->ciss_mtx);
522 /************************************************************************
523 * Detach the driver from this adapter.
526 ciss_detach(device_t dev)
528 struct ciss_softc *sc = device_get_softc(dev);
532 mtx_lock(&sc->ciss_mtx);
533 if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
534 mtx_unlock(&sc->ciss_mtx);
538 /* flush adapter cache */
539 ciss_flush_adapter(sc);
541 /* release all resources. The mutex is released and freed here too. */
547 /************************************************************************
548 * Prepare adapter for system shutdown.
551 ciss_shutdown(device_t dev)
553 struct ciss_softc *sc = device_get_softc(dev);
557 mtx_lock(&sc->ciss_mtx);
558 /* flush adapter cache */
559 ciss_flush_adapter(sc);
561 if (sc->ciss_soft_reset)
563 mtx_unlock(&sc->ciss_mtx);
569 ciss_init_sysctl(struct ciss_softc *sc)
572 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
573 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
574 OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
577 /************************************************************************
578 * Perform PCI-specific attachment actions.
581 ciss_init_pci(struct ciss_softc *sc)
583 uintptr_t cbase, csize, cofs;
584 uint32_t method, supported_methods;
585 int error, sqmask, i;
591 * Work out adapter type.
593 i = ciss_lookup(sc->ciss_dev);
595 ciss_printf(sc, "unknown adapter type\n");
599 if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
600 sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
601 } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
602 sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
605 * XXX Big hammer, masks/unmasks all possible interrupts. This should
606 * work on all hardware variants. Need to add code to handle the
607 * "controller crashed" interupt bit that this unmasks.
613 * Allocate register window first (we need this to find the config
617 sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
618 if ((sc->ciss_regs_resource =
619 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
620 &sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
621 ciss_printf(sc, "can't allocate register window\n");
624 sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
625 sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
628 * Find the BAR holding the config structure. If it's not the one
629 * we already mapped for registers, map it too.
631 sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
632 if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
633 if ((sc->ciss_cfg_resource =
634 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
635 &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
636 ciss_printf(sc, "can't allocate config window\n");
639 cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
640 csize = rman_get_end(sc->ciss_cfg_resource) -
641 rman_get_start(sc->ciss_cfg_resource) + 1;
643 cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
644 csize = rman_get_end(sc->ciss_regs_resource) -
645 rman_get_start(sc->ciss_regs_resource) + 1;
647 cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
650 * Use the base/size/offset values we just calculated to
651 * sanity-check the config structure. If it's OK, point to it.
653 if ((cofs + sizeof(struct ciss_config_table)) > csize) {
654 ciss_printf(sc, "config table outside window\n");
657 sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
658 debug(1, "config struct at %p", sc->ciss_cfg);
661 * Calculate the number of request structures/commands we are
662 * going to provide for this adapter.
664 sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
667 * Validate the config structure. If we supported other transport
668 * methods, we could select amongst them at this point in time.
670 if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
671 ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
672 sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
673 sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
678 * Select the mode of operation, prefer Performant.
680 if (!(sc->ciss_cfg->supported_methods &
681 (CISS_TRANSPORT_METHOD_SIMPLE | CISS_TRANSPORT_METHOD_PERF))) {
682 ciss_printf(sc, "No supported transport layers: 0x%x\n",
683 sc->ciss_cfg->supported_methods);
686 switch (ciss_force_transport) {
688 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
691 supported_methods = CISS_TRANSPORT_METHOD_PERF;
695 * Override the capabilities of the BOARD and specify SIMPLE
698 if (ciss_vendor_data[i].flags & CISS_BOARD_SIMPLE)
699 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
701 supported_methods = sc->ciss_cfg->supported_methods;
706 if ((supported_methods & CISS_TRANSPORT_METHOD_PERF) != 0) {
707 method = CISS_TRANSPORT_METHOD_PERF;
708 sc->ciss_perf = (struct ciss_perf_config *)(cbase + cofs +
709 sc->ciss_cfg->transport_offset);
710 if (ciss_init_perf(sc)) {
711 supported_methods &= ~method;
714 } else if (supported_methods & CISS_TRANSPORT_METHOD_SIMPLE) {
715 method = CISS_TRANSPORT_METHOD_SIMPLE;
717 ciss_printf(sc, "No supported transport methods: 0x%x\n",
718 sc->ciss_cfg->supported_methods);
723 * Tell it we're using the low 4GB of RAM. Set the default interrupt
724 * coalescing options.
726 sc->ciss_cfg->requested_method = method;
727 sc->ciss_cfg->command_physlimit = 0;
728 sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
729 sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
732 sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
735 if (ciss_update_config(sc)) {
736 ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
737 CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
740 if ((sc->ciss_cfg->active_method & method) == 0) {
741 supported_methods &= ~method;
742 if (supported_methods == 0) {
743 ciss_printf(sc, "adapter refuses to go into available transports "
744 "mode (0x%x, 0x%x)\n", supported_methods,
745 sc->ciss_cfg->active_method);
752 * Wait for the adapter to come ready.
754 if ((error = ciss_wait_adapter(sc)) != 0)
757 /* Prepare to possibly use MSIX and/or PERFORMANT interrupts. Normal
758 * interrupts have a rid of 0, this will be overridden if MSIX is used.
760 sc->ciss_irq_rid[0] = 0;
761 if (method == CISS_TRANSPORT_METHOD_PERF) {
762 ciss_printf(sc, "PERFORMANT Transport\n");
763 if ((ciss_force_interrupt != 1) && (ciss_setup_msix(sc) == 0)) {
764 intr = ciss_perf_msi_intr;
766 intr = ciss_perf_intr;
768 /* XXX The docs say that the 0x01 bit is only for SAS controllers.
769 * Unfortunately, there is no good way to know if this is a SAS
770 * controller. Hopefully enabling this bit universally will work OK.
771 * It seems to work fine for SA6i controllers.
773 sc->ciss_interrupt_mask = CISS_TL_PERF_INTR_OPQ | CISS_TL_PERF_INTR_MSI;
776 ciss_printf(sc, "SIMPLE Transport\n");
777 /* MSIX doesn't seem to work in SIMPLE mode, only enable if it forced */
778 if (ciss_force_interrupt == 2)
779 /* If this fails, we automatically revert to INTx */
781 sc->ciss_perf = NULL;
783 sc->ciss_interrupt_mask = sqmask;
787 * Turn off interrupts before we go routing anything.
789 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
792 * Allocate and set up our interrupt.
794 if ((sc->ciss_irq_resource =
795 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid[0],
796 RF_ACTIVE | RF_SHAREABLE)) == NULL) {
797 ciss_printf(sc, "can't allocate interrupt\n");
801 if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
802 INTR_TYPE_CAM|INTR_MPSAFE, NULL, intr, sc,
804 ciss_printf(sc, "can't set up interrupt\n");
809 * Allocate the parent bus DMA tag appropriate for our PCI
812 * Note that "simple" adapters can only address within a 32-bit
815 if (bus_dma_tag_create(bus_get_dma_tag(sc->ciss_dev),/* PCI parent */
816 1, 0, /* alignment, boundary */
817 BUS_SPACE_MAXADDR, /* lowaddr */
818 BUS_SPACE_MAXADDR, /* highaddr */
819 NULL, NULL, /* filter, filterarg */
820 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
821 CISS_MAX_SG_ELEMENTS, /* nsegments */
822 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
824 NULL, NULL, /* lockfunc, lockarg */
825 &sc->ciss_parent_dmat)) {
826 ciss_printf(sc, "can't allocate parent DMA tag\n");
831 * Create DMA tag for mapping buffers into adapter-addressable
834 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
835 1, 0, /* alignment, boundary */
836 BUS_SPACE_MAXADDR, /* lowaddr */
837 BUS_SPACE_MAXADDR, /* highaddr */
838 NULL, NULL, /* filter, filterarg */
839 MAXBSIZE, CISS_MAX_SG_ELEMENTS, /* maxsize, nsegments */
840 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
841 BUS_DMA_ALLOCNOW, /* flags */
842 busdma_lock_mutex, &sc->ciss_mtx, /* lockfunc, lockarg */
843 &sc->ciss_buffer_dmat)) {
844 ciss_printf(sc, "can't allocate buffer DMA tag\n");
850 /************************************************************************
851 * Setup MSI/MSIX operation (Performant only)
852 * Four interrupts are available, but we only use 1 right now. If MSI-X
853 * isn't avaialble, try using MSI instead.
856 ciss_setup_msix(struct ciss_softc *sc)
860 /* Weed out devices that don't actually support MSI */
861 i = ciss_lookup(sc->ciss_dev);
862 if (ciss_vendor_data[i].flags & CISS_BOARD_NOMSI)
866 * Only need to use the minimum number of MSI vectors, as the driver
867 * doesn't support directed MSIX interrupts.
869 val = pci_msix_count(sc->ciss_dev);
870 if (val < CISS_MSI_COUNT) {
871 val = pci_msi_count(sc->ciss_dev);
872 device_printf(sc->ciss_dev, "got %d MSI messages]\n", val);
873 if (val < CISS_MSI_COUNT)
876 val = MIN(val, CISS_MSI_COUNT);
877 if (pci_alloc_msix(sc->ciss_dev, &val) != 0) {
878 if (pci_alloc_msi(sc->ciss_dev, &val) != 0)
884 ciss_printf(sc, "Using %d MSIX interrupt%s\n", val,
885 (val != 1) ? "s" : "");
887 for (i = 0; i < val; i++)
888 sc->ciss_irq_rid[i] = i + 1;
894 /************************************************************************
895 * Setup the Performant structures.
898 ciss_init_perf(struct ciss_softc *sc)
900 struct ciss_perf_config *pc = sc->ciss_perf;
904 * Create the DMA tag for the reply queue.
906 reply_size = sizeof(uint64_t) * sc->ciss_max_requests;
907 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
908 1, 0, /* alignment, boundary */
909 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
910 BUS_SPACE_MAXADDR, /* highaddr */
911 NULL, NULL, /* filter, filterarg */
912 reply_size, 1, /* maxsize, nsegments */
913 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
915 NULL, NULL, /* lockfunc, lockarg */
916 &sc->ciss_reply_dmat)) {
917 ciss_printf(sc, "can't allocate reply DMA tag\n");
921 * Allocate memory and make it available for DMA.
923 if (bus_dmamem_alloc(sc->ciss_reply_dmat, (void **)&sc->ciss_reply,
924 BUS_DMA_NOWAIT, &sc->ciss_reply_map)) {
925 ciss_printf(sc, "can't allocate reply memory\n");
928 bus_dmamap_load(sc->ciss_reply_dmat, sc->ciss_reply_map, sc->ciss_reply,
929 reply_size, ciss_command_map_helper, &sc->ciss_reply_phys, 0);
930 bzero(sc->ciss_reply, reply_size);
932 sc->ciss_cycle = 0x1;
936 * Preload the fetch table with common command sizes. This allows the
937 * hardware to not waste bus cycles for typical i/o commands, but also not
938 * tax the driver to be too exact in choosing sizes. The table is optimized
939 * for page-aligned i/o's, but since most i/o comes from the various pagers,
940 * it's a reasonable assumption to make.
942 pc->fetch_count[CISS_SG_FETCH_NONE] = (sizeof(struct ciss_command) + 15) / 16;
943 pc->fetch_count[CISS_SG_FETCH_1] =
944 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 1 + 15) / 16;
945 pc->fetch_count[CISS_SG_FETCH_2] =
946 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 2 + 15) / 16;
947 pc->fetch_count[CISS_SG_FETCH_4] =
948 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 4 + 15) / 16;
949 pc->fetch_count[CISS_SG_FETCH_8] =
950 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 8 + 15) / 16;
951 pc->fetch_count[CISS_SG_FETCH_16] =
952 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 16 + 15) / 16;
953 pc->fetch_count[CISS_SG_FETCH_32] =
954 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 32 + 15) / 16;
955 pc->fetch_count[CISS_SG_FETCH_MAX] = (CISS_COMMAND_ALLOC_SIZE + 15) / 16;
957 pc->rq_size = sc->ciss_max_requests; /* XXX less than the card supports? */
958 pc->rq_count = 1; /* XXX Hardcode for a single queue */
961 pc->rq[0].rq_addr_hi = 0x0;
962 pc->rq[0].rq_addr_lo = sc->ciss_reply_phys;
967 /************************************************************************
968 * Wait for the adapter to come ready.
971 ciss_wait_adapter(struct ciss_softc *sc)
978 * Wait for the adapter to come ready.
980 if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
981 ciss_printf(sc, "waiting for adapter to come ready...\n");
982 for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
983 DELAY(1000000); /* one second */
985 ciss_printf(sc, "timed out waiting for adapter to come ready\n");
993 /************************************************************************
994 * Flush the adapter cache.
997 ciss_flush_adapter(struct ciss_softc *sc)
999 struct ciss_request *cr;
1000 struct ciss_bmic_flush_cache *cbfc;
1001 int error, command_status;
1009 * Build a BMIC request to flush the cache. We don't disable
1010 * it, as we may be going to do more I/O (eg. we are emulating
1011 * the Synchronise Cache command).
1013 if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1017 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
1018 (void **)&cbfc, sizeof(*cbfc))) != 0)
1022 * Submit the request and wait for it to complete.
1024 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1025 ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
1032 ciss_report_request(cr, &command_status, NULL);
1033 switch(command_status) {
1034 case CISS_CMD_STATUS_SUCCESS:
1037 ciss_printf(sc, "error flushing cache (%s)\n",
1038 ciss_name_command_status(command_status));
1045 free(cbfc, CISS_MALLOC_CLASS);
1047 ciss_release_request(cr);
1052 ciss_soft_reset(struct ciss_softc *sc)
1054 struct ciss_request *cr = NULL;
1055 struct ciss_command *cc;
1058 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1059 /* only reset proxy controllers */
1060 if (sc->ciss_controllers[i].physical.bus == 0)
1063 if ((error = ciss_get_request(sc, &cr)) != 0)
1066 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
1071 cc->header.address = sc->ciss_controllers[i];
1073 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
1076 ciss_release_request(cr);
1080 ciss_printf(sc, "error resetting controller (%d)\n", error);
1083 ciss_release_request(cr);
1086 /************************************************************************
1087 * Allocate memory for the adapter command structures, initialise
1088 * the request structures.
1090 * Note that the entire set of commands are allocated in a single
1094 ciss_init_requests(struct ciss_softc *sc)
1096 struct ciss_request *cr;
1102 ciss_printf(sc, "using %d of %d available commands\n",
1103 sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
1106 * Create the DMA tag for commands.
1108 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
1109 32, 0, /* alignment, boundary */
1110 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1111 BUS_SPACE_MAXADDR, /* highaddr */
1112 NULL, NULL, /* filter, filterarg */
1113 CISS_COMMAND_ALLOC_SIZE *
1114 sc->ciss_max_requests, 1, /* maxsize, nsegments */
1115 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1117 NULL, NULL, /* lockfunc, lockarg */
1118 &sc->ciss_command_dmat)) {
1119 ciss_printf(sc, "can't allocate command DMA tag\n");
1123 * Allocate memory and make it available for DMA.
1125 if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
1126 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
1127 ciss_printf(sc, "can't allocate command memory\n");
1130 bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map,sc->ciss_command,
1131 CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
1132 ciss_command_map_helper, &sc->ciss_command_phys, 0);
1133 bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
1136 * Set up the request and command structures, push requests onto
1139 for (i = 1; i < sc->ciss_max_requests; i++) {
1140 cr = &sc->ciss_request[i];
1143 cr->cr_cc = (struct ciss_command *)((uintptr_t)sc->ciss_command +
1144 CISS_COMMAND_ALLOC_SIZE * i);
1145 cr->cr_ccphys = sc->ciss_command_phys + CISS_COMMAND_ALLOC_SIZE * i;
1146 bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
1147 ciss_enqueue_free(cr);
1153 ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1158 *addr = segs[0].ds_addr;
1161 /************************************************************************
1162 * Identify the adapter, print some information about it.
1165 ciss_identify_adapter(struct ciss_softc *sc)
1167 struct ciss_request *cr;
1168 int error, command_status;
1175 * Get a request, allocate storage for the adapter data.
1177 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
1178 (void **)&sc->ciss_id,
1179 sizeof(*sc->ciss_id))) != 0)
1183 * Submit the request and wait for it to complete.
1185 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1186 ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
1193 ciss_report_request(cr, &command_status, NULL);
1194 switch(command_status) {
1195 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1197 case CISS_CMD_STATUS_DATA_UNDERRUN:
1198 case CISS_CMD_STATUS_DATA_OVERRUN:
1199 ciss_printf(sc, "data over/underrun reading adapter information\n");
1201 ciss_printf(sc, "error reading adapter information (%s)\n",
1202 ciss_name_command_status(command_status));
1207 /* sanity-check reply */
1208 if (!sc->ciss_id->big_map_supported) {
1209 ciss_printf(sc, "adapter does not support BIG_MAP\n");
1215 /* XXX later revisions may not need this */
1216 sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
1219 /* XXX only really required for old 5300 adapters? */
1220 sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
1223 * Earlier controller specs do not contain these config
1224 * entries, so assume that a 0 means its old and assign
1225 * these values to the defaults that were established
1226 * when this driver was developed for them
1228 if (sc->ciss_cfg->max_logical_supported == 0)
1229 sc->ciss_cfg->max_logical_supported = CISS_MAX_LOGICAL;
1230 if (sc->ciss_cfg->max_physical_supported == 0)
1231 sc->ciss_cfg->max_physical_supported = CISS_MAX_PHYSICAL;
1232 /* print information */
1234 ciss_printf(sc, " %d logical drive%s configured\n",
1235 sc->ciss_id->configured_logical_drives,
1236 (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
1237 ciss_printf(sc, " firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
1238 ciss_printf(sc, " %d SCSI channels\n", sc->ciss_id->scsi_bus_count);
1240 ciss_printf(sc, " signature '%.4s'\n", sc->ciss_cfg->signature);
1241 ciss_printf(sc, " valence %d\n", sc->ciss_cfg->valence);
1242 ciss_printf(sc, " supported I/O methods 0x%b\n",
1243 sc->ciss_cfg->supported_methods,
1244 "\20\1READY\2simple\3performant\4MEMQ\n");
1245 ciss_printf(sc, " active I/O method 0x%b\n",
1246 sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
1247 ciss_printf(sc, " 4G page base 0x%08x\n",
1248 sc->ciss_cfg->command_physlimit);
1249 ciss_printf(sc, " interrupt coalesce delay %dus\n",
1250 sc->ciss_cfg->interrupt_coalesce_delay);
1251 ciss_printf(sc, " interrupt coalesce count %d\n",
1252 sc->ciss_cfg->interrupt_coalesce_count);
1253 ciss_printf(sc, " max outstanding commands %d\n",
1254 sc->ciss_cfg->max_outstanding_commands);
1255 ciss_printf(sc, " bus types 0x%b\n", sc->ciss_cfg->bus_types,
1256 "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
1257 ciss_printf(sc, " server name '%.16s'\n", sc->ciss_cfg->server_name);
1258 ciss_printf(sc, " heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
1259 ciss_printf(sc, " max logical logical volumes: %d\n", sc->ciss_cfg->max_logical_supported);
1260 ciss_printf(sc, " max physical disks supported: %d\n", sc->ciss_cfg->max_physical_supported);
1261 ciss_printf(sc, " max physical disks per logical volume: %d\n", sc->ciss_cfg->max_physical_per_logical);
1266 if (sc->ciss_id != NULL) {
1267 free(sc->ciss_id, CISS_MALLOC_CLASS);
1272 ciss_release_request(cr);
1276 /************************************************************************
1277 * Helper routine for generating a list of logical and physical luns.
1279 static struct ciss_lun_report *
1280 ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
1282 struct ciss_request *cr;
1283 struct ciss_command *cc;
1284 struct ciss_report_cdb *crc;
1285 struct ciss_lun_report *cll;
1296 * Get a request, allocate storage for the address list.
1298 if ((error = ciss_get_request(sc, &cr)) != 0)
1300 report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
1301 if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1302 ciss_printf(sc, "can't allocate memory for lun report\n");
1308 * Build the Report Logical/Physical LUNs command.
1312 cr->cr_length = report_size;
1313 cr->cr_flags = CISS_REQ_DATAIN;
1315 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
1316 cc->header.address.physical.bus = 0;
1317 cc->header.address.physical.target = 0;
1318 cc->cdb.cdb_length = sizeof(*crc);
1319 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1320 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1321 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1322 cc->cdb.timeout = 30; /* XXX better suggestions? */
1324 crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
1325 bzero(crc, sizeof(*crc));
1326 crc->opcode = opcode;
1327 crc->length = htonl(report_size); /* big-endian field */
1328 cll->list_size = htonl(report_size - sizeof(*cll)); /* big-endian field */
1331 * Submit the request and wait for it to complete. (timeout
1332 * here should be much greater than above)
1334 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1335 ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
1340 * Check response. Note that data over/underrun is OK.
1342 ciss_report_request(cr, &command_status, NULL);
1343 switch(command_status) {
1344 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1345 case CISS_CMD_STATUS_DATA_UNDERRUN: /* buffer too large, not bad */
1347 case CISS_CMD_STATUS_DATA_OVERRUN:
1348 ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
1349 sc->ciss_cfg->max_logical_supported);
1352 ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
1353 ciss_name_command_status(command_status));
1357 ciss_release_request(cr);
1362 ciss_release_request(cr);
1363 if (error && cll != NULL) {
1364 free(cll, CISS_MALLOC_CLASS);
1370 /************************************************************************
1371 * Find logical drives on the adapter.
1374 ciss_init_logical(struct ciss_softc *sc)
1376 struct ciss_lun_report *cll;
1377 int error = 0, i, j;
1382 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
1383 sc->ciss_cfg->max_logical_supported);
1389 /* sanity-check reply */
1390 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1391 if ((ndrives < 0) || (ndrives > sc->ciss_cfg->max_logical_supported)) {
1392 ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
1393 ndrives, sc->ciss_cfg->max_logical_supported);
1399 * Save logical drive information.
1402 ciss_printf(sc, "%d logical drive%s\n",
1403 ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
1407 malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
1408 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1409 if (sc->ciss_logical == NULL) {
1414 for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1415 sc->ciss_logical[i] =
1416 malloc(sc->ciss_cfg->max_logical_supported *
1417 sizeof(struct ciss_ldrive),
1418 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1419 if (sc->ciss_logical[i] == NULL) {
1424 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++)
1425 sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
1429 for (i = 0; i < sc->ciss_cfg->max_logical_supported; i++) {
1431 struct ciss_ldrive *ld;
1434 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
1435 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
1436 ld = &sc->ciss_logical[bus][target];
1438 ld->cl_address = cll->lun[i];
1439 ld->cl_controller = &sc->ciss_controllers[bus];
1440 if (ciss_identify_logical(sc, ld) != 0)
1443 * If the drive has had media exchanged, we should bring it online.
1445 if (ld->cl_lstatus->media_exchanged)
1446 ciss_accept_media(sc, ld);
1453 free(cll, CISS_MALLOC_CLASS);
1458 ciss_init_physical(struct ciss_softc *sc)
1460 struct ciss_lun_report *cll;
1470 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
1471 sc->ciss_cfg->max_physical_supported);
1477 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1480 ciss_printf(sc, "%d physical device%s\n",
1481 nphys, (nphys > 1 || nphys == 0) ? "s" : "");
1485 * Figure out the bus mapping.
1486 * Logical buses include both the local logical bus for local arrays and
1487 * proxy buses for remote arrays. Physical buses are numbered by the
1488 * controller and represent physical buses that hold physical devices.
1489 * We shift these bus numbers so that everything fits into a single flat
1490 * numbering space for CAM. Logical buses occupy the first 32 CAM bus
1491 * numbers, and the physical bus numbers are shifted to be above that.
1492 * This results in the various driver arrays being indexed as follows:
1494 * ciss_controllers[] - indexed by logical bus
1495 * ciss_cam_sim[] - indexed by both logical and physical, with physical
1496 * being shifted by 32.
1497 * ciss_logical[][] - indexed by logical bus
1498 * ciss_physical[][] - indexed by physical bus
1500 * XXX This is getting more and more hackish. CISS really doesn't play
1501 * well with a standard SCSI model; devices are addressed via magic
1502 * cookies, not via b/t/l addresses. Since there is no way to store
1503 * the cookie in the CAM device object, we have to keep these lookup
1504 * tables handy so that the devices can be found quickly at the cost
1505 * of wasting memory and having a convoluted lookup scheme. This
1506 * driver should probably be converted to block interface.
1509 * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
1510 * controller. A proxy controller is another physical controller
1511 * behind the primary PCI controller. We need to know about this
1512 * so that BMIC commands can be properly targeted. There can be
1513 * proxy controllers attached to a single PCI controller, so
1514 * find the highest numbered one so the array can be properly
1517 sc->ciss_max_logical_bus = 1;
1518 for (i = 0; i < nphys; i++) {
1519 if (cll->lun[i].physical.extra_address == 0) {
1520 bus = cll->lun[i].physical.bus;
1521 sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
1523 bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
1524 sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
1528 sc->ciss_controllers =
1529 malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
1530 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1532 if (sc->ciss_controllers == NULL) {
1533 ciss_printf(sc, "Could not allocate memory for controller map\n");
1538 /* setup a map of controller addresses */
1539 for (i = 0; i < nphys; i++) {
1540 if (cll->lun[i].physical.extra_address == 0) {
1541 sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
1546 malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
1547 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1548 if (sc->ciss_physical == NULL) {
1549 ciss_printf(sc, "Could not allocate memory for physical device map\n");
1554 for (i = 0; i < sc->ciss_max_physical_bus; i++) {
1555 sc->ciss_physical[i] =
1556 malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
1557 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1558 if (sc->ciss_physical[i] == NULL) {
1559 ciss_printf(sc, "Could not allocate memory for target map\n");
1565 ciss_filter_physical(sc, cll);
1569 free(cll, CISS_MALLOC_CLASS);
1575 ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
1581 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1582 for (i = 0; i < nphys; i++) {
1583 if (cll->lun[i].physical.extra_address == 0)
1587 * Filter out devices that we don't want. Level 3 LUNs could
1588 * probably be supported, but the docs don't give enough of a
1591 * The mode field of the physical address is likely set to have
1592 * hard disks masked out. Honor it unless the user has overridden
1593 * us with the tunable. We also munge the inquiry data for these
1594 * disks so that they only show up as passthrough devices. Keeping
1595 * them visible in this fashion is useful for doing things like
1596 * flashing firmware.
1598 ea = cll->lun[i].physical.extra_address;
1599 if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
1600 (CISS_EXTRA_MODE2(ea) == 0x3))
1602 if ((ciss_expose_hidden_physical == 0) &&
1603 (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
1607 * Note: CISS firmware numbers physical busses starting at '1', not
1608 * '0'. This numbering is internal to the firmware and is only
1609 * used as a hint here.
1611 bus = CISS_EXTRA_BUS2(ea) - 1;
1612 target = CISS_EXTRA_TARGET2(ea);
1613 sc->ciss_physical[bus][target].cp_address = cll->lun[i];
1614 sc->ciss_physical[bus][target].cp_online = 1;
1621 ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1623 struct ciss_request *cr;
1624 struct ciss_command *cc;
1625 struct scsi_inquiry *inq;
1631 bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1633 if ((error = ciss_get_request(sc, &cr)) != 0)
1637 cr->cr_data = &ld->cl_geometry;
1638 cr->cr_length = sizeof(ld->cl_geometry);
1639 cr->cr_flags = CISS_REQ_DATAIN;
1641 cc->header.address = ld->cl_address;
1642 cc->cdb.cdb_length = 6;
1643 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1644 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1645 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1646 cc->cdb.timeout = 30;
1648 inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1649 inq->opcode = INQUIRY;
1650 inq->byte2 = SI_EVPD;
1651 inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1652 scsi_ulto2b(sizeof(ld->cl_geometry), inq->length);
1654 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1655 ciss_printf(sc, "error getting geometry (%d)\n", error);
1659 ciss_report_request(cr, &command_status, NULL);
1660 switch(command_status) {
1661 case CISS_CMD_STATUS_SUCCESS:
1662 case CISS_CMD_STATUS_DATA_UNDERRUN:
1664 case CISS_CMD_STATUS_DATA_OVERRUN:
1665 ciss_printf(sc, "WARNING: Data overrun\n");
1668 ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1669 ciss_name_command_status(command_status));
1675 ciss_release_request(cr);
1678 /************************************************************************
1679 * Identify a logical drive, initialise state related to it.
1682 ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1684 struct ciss_request *cr;
1685 struct ciss_command *cc;
1686 struct ciss_bmic_cdb *cbc;
1687 int error, command_status;
1694 * Build a BMIC request to fetch the drive ID.
1696 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
1697 (void **)&ld->cl_ldrive,
1698 sizeof(*ld->cl_ldrive))) != 0)
1701 cc->header.address = *ld->cl_controller; /* target controller */
1702 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1703 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1706 * Submit the request and wait for it to complete.
1708 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1709 ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
1716 ciss_report_request(cr, &command_status, NULL);
1717 switch(command_status) {
1718 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1720 case CISS_CMD_STATUS_DATA_UNDERRUN:
1721 case CISS_CMD_STATUS_DATA_OVERRUN:
1722 ciss_printf(sc, "data over/underrun reading logical drive ID\n");
1724 ciss_printf(sc, "error reading logical drive ID (%s)\n",
1725 ciss_name_command_status(command_status));
1729 ciss_release_request(cr);
1733 * Build a CISS BMIC command to get the logical drive status.
1735 if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
1739 * Get the logical drive geometry.
1741 if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1745 * Print the drive's basic characteristics.
1748 ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
1749 CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
1750 CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
1751 ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
1752 ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
1753 ld->cl_ldrive->block_size));
1755 ciss_print_ldrive(sc, ld);
1759 /* make the drive not-exist */
1760 ld->cl_status = CISS_LD_NONEXISTENT;
1761 if (ld->cl_ldrive != NULL) {
1762 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
1763 ld->cl_ldrive = NULL;
1765 if (ld->cl_lstatus != NULL) {
1766 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
1767 ld->cl_lstatus = NULL;
1771 ciss_release_request(cr);
1776 /************************************************************************
1777 * Get status for a logical drive.
1779 * XXX should we also do this in response to Test Unit Ready?
1782 ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld)
1784 struct ciss_request *cr;
1785 struct ciss_command *cc;
1786 struct ciss_bmic_cdb *cbc;
1787 int error, command_status;
1790 * Build a CISS BMIC command to get the logical drive status.
1792 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
1793 (void **)&ld->cl_lstatus,
1794 sizeof(*ld->cl_lstatus))) != 0)
1797 cc->header.address = *ld->cl_controller; /* target controller */
1798 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1799 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1802 * Submit the request and wait for it to complete.
1804 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1805 ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1812 ciss_report_request(cr, &command_status, NULL);
1813 switch(command_status) {
1814 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1816 case CISS_CMD_STATUS_DATA_UNDERRUN:
1817 case CISS_CMD_STATUS_DATA_OVERRUN:
1818 ciss_printf(sc, "data over/underrun reading logical drive status\n");
1820 ciss_printf(sc, "error reading logical drive status (%s)\n",
1821 ciss_name_command_status(command_status));
1827 * Set the drive's summary status based on the returned status.
1829 * XXX testing shows that a failed JBOD drive comes back at next
1830 * boot in "queued for expansion" mode. WTF?
1832 ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
1836 ciss_release_request(cr);
1840 /************************************************************************
1841 * Notify the adapter of a config update.
1844 ciss_update_config(struct ciss_softc *sc)
1850 CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
1851 for (i = 0; i < 1000; i++) {
1852 if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
1853 CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
1861 /************************************************************************
1862 * Accept new media into a logical drive.
1864 * XXX The drive has previously been offline; it would be good if we
1865 * could make sure it's not open right now.
1868 ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
1870 struct ciss_request *cr;
1871 struct ciss_command *cc;
1872 struct ciss_bmic_cdb *cbc;
1874 int error = 0, ldrive;
1876 ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1878 debug(0, "bringing logical drive %d back online", ldrive);
1881 * Build a CISS BMIC command to bring the drive back online.
1883 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
1887 cc->header.address = *ld->cl_controller; /* target controller */
1888 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1889 cbc->log_drive = ldrive;
1892 * Submit the request and wait for it to complete.
1894 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1895 ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
1902 ciss_report_request(cr, &command_status, NULL);
1903 switch(command_status) {
1904 case CISS_CMD_STATUS_SUCCESS: /* all OK */
1905 /* we should get a logical drive status changed event here */
1908 ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
1909 ciss_name_command_status(command_status));
1915 ciss_release_request(cr);
1919 /************************************************************************
1920 * Release adapter resources.
1923 ciss_free(struct ciss_softc *sc)
1925 struct ciss_request *cr;
1930 /* we're going away */
1931 sc->ciss_flags |= CISS_FLAG_ABORTING;
1933 /* terminate the periodic heartbeat routine */
1934 callout_stop(&sc->ciss_periodic);
1936 /* cancel the Event Notify chain */
1937 ciss_notify_abort(sc);
1939 ciss_kill_notify_thread(sc);
1941 /* disconnect from CAM */
1942 if (sc->ciss_cam_sim) {
1943 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1944 if (sc->ciss_cam_sim[i]) {
1945 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1946 cam_sim_free(sc->ciss_cam_sim[i], 0);
1949 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
1950 CISS_PHYSICAL_BASE; i++) {
1951 if (sc->ciss_cam_sim[i]) {
1952 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1953 cam_sim_free(sc->ciss_cam_sim[i], 0);
1956 free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
1958 if (sc->ciss_cam_devq)
1959 cam_simq_free(sc->ciss_cam_devq);
1961 /* remove the control device */
1962 mtx_unlock(&sc->ciss_mtx);
1963 if (sc->ciss_dev_t != NULL)
1964 destroy_dev(sc->ciss_dev_t);
1966 /* Final cleanup of the callout. */
1967 callout_drain(&sc->ciss_periodic);
1968 mtx_destroy(&sc->ciss_mtx);
1970 /* free the controller data */
1971 if (sc->ciss_id != NULL)
1972 free(sc->ciss_id, CISS_MALLOC_CLASS);
1974 /* release I/O resources */
1975 if (sc->ciss_regs_resource != NULL)
1976 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1977 sc->ciss_regs_rid, sc->ciss_regs_resource);
1978 if (sc->ciss_cfg_resource != NULL)
1979 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1980 sc->ciss_cfg_rid, sc->ciss_cfg_resource);
1981 if (sc->ciss_intr != NULL)
1982 bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
1983 if (sc->ciss_irq_resource != NULL)
1984 bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
1985 sc->ciss_irq_rid[0], sc->ciss_irq_resource);
1987 pci_release_msi(sc->ciss_dev);
1989 while ((cr = ciss_dequeue_free(sc)) != NULL)
1990 bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
1991 if (sc->ciss_buffer_dmat)
1992 bus_dma_tag_destroy(sc->ciss_buffer_dmat);
1994 /* destroy command memory and DMA tag */
1995 if (sc->ciss_command != NULL) {
1996 bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
1997 bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
1999 if (sc->ciss_command_dmat)
2000 bus_dma_tag_destroy(sc->ciss_command_dmat);
2002 if (sc->ciss_reply) {
2003 bus_dmamap_unload(sc->ciss_reply_dmat, sc->ciss_reply_map);
2004 bus_dmamem_free(sc->ciss_reply_dmat, sc->ciss_reply, sc->ciss_reply_map);
2006 if (sc->ciss_reply_dmat)
2007 bus_dma_tag_destroy(sc->ciss_reply_dmat);
2009 /* destroy DMA tags */
2010 if (sc->ciss_parent_dmat)
2011 bus_dma_tag_destroy(sc->ciss_parent_dmat);
2012 if (sc->ciss_logical) {
2013 for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
2014 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
2015 if (sc->ciss_logical[i][j].cl_ldrive)
2016 free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
2017 if (sc->ciss_logical[i][j].cl_lstatus)
2018 free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
2020 free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
2022 free(sc->ciss_logical, CISS_MALLOC_CLASS);
2025 if (sc->ciss_physical) {
2026 for (i = 0; i < sc->ciss_max_physical_bus; i++)
2027 free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
2028 free(sc->ciss_physical, CISS_MALLOC_CLASS);
2031 if (sc->ciss_controllers)
2032 free(sc->ciss_controllers, CISS_MALLOC_CLASS);
2036 /************************************************************************
2037 * Give a command to the adapter.
2039 * Note that this uses the simple transport layer directly. If we
2040 * want to add support for other layers, we'll need a switch of some
2043 * Note that the simple transport layer has no way of refusing a
2044 * command; we only have as many request structures as the adapter
2045 * supports commands, so we don't have to check (this presumes that
2046 * the adapter can handle commands as fast as we throw them at it).
2049 ciss_start(struct ciss_request *cr)
2051 struct ciss_command *cc; /* XXX debugging only */
2055 debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
2058 * Map the request's data.
2060 if ((error = ciss_map_request(cr)))
2064 ciss_print_request(cr);
2070 /************************************************************************
2071 * Fetch completed request(s) from the adapter, queue them for
2072 * completion handling.
2074 * Note that this uses the simple transport layer directly. If we
2075 * want to add support for other layers, we'll need a switch of some
2078 * Note that the simple transport mechanism does not require any
2079 * reentrancy protection; the OPQ read is atomic. If there is a
2080 * chance of a race with something else that might move the request
2081 * off the busy list, then we will have to lock against that
2082 * (eg. timeouts, etc.)
2085 ciss_done(struct ciss_softc *sc, cr_qhead_t *qh)
2087 struct ciss_request *cr;
2088 struct ciss_command *cc;
2089 u_int32_t tag, index;
2094 * Loop quickly taking requests from the adapter and moving them
2095 * to the completed queue.
2099 tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
2100 if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
2103 debug(2, "completed command %d%s", index,
2104 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2105 if (index >= sc->ciss_max_requests) {
2106 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2109 cr = &(sc->ciss_request[index]);
2111 cc->header.host_tag = tag; /* not updated by adapter */
2112 ciss_enqueue_complete(cr, qh);
2118 ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh)
2120 struct ciss_request *cr;
2121 struct ciss_command *cc;
2122 u_int32_t tag, index;
2127 * Loop quickly taking requests from the adapter and moving them
2128 * to the completed queue.
2131 tag = sc->ciss_reply[sc->ciss_rqidx];
2132 if ((tag & CISS_CYCLE_MASK) != sc->ciss_cycle)
2135 debug(2, "completed command %d%s\n", index,
2136 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2137 if (index < sc->ciss_max_requests) {
2138 cr = &(sc->ciss_request[index]);
2140 cc->header.host_tag = tag; /* not updated by adapter */
2141 ciss_enqueue_complete(cr, qh);
2143 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2145 if (++sc->ciss_rqidx == sc->ciss_max_requests) {
2147 sc->ciss_cycle ^= 1;
2153 /************************************************************************
2154 * Take an interrupt from the adapter.
2157 ciss_intr(void *arg)
2160 struct ciss_softc *sc = (struct ciss_softc *)arg;
2163 * The only interrupt we recognise indicates that there are
2164 * entries in the outbound post queue.
2168 mtx_lock(&sc->ciss_mtx);
2169 ciss_complete(sc, &qh);
2170 mtx_unlock(&sc->ciss_mtx);
2174 ciss_perf_intr(void *arg)
2176 struct ciss_softc *sc = (struct ciss_softc *)arg;
2178 /* Clear the interrupt and flush the bridges. Docs say that the flush
2179 * needs to be done twice, which doesn't seem right.
2181 CISS_TL_PERF_CLEAR_INT(sc);
2182 CISS_TL_PERF_FLUSH_INT(sc);
2184 ciss_perf_msi_intr(sc);
2188 ciss_perf_msi_intr(void *arg)
2191 struct ciss_softc *sc = (struct ciss_softc *)arg;
2194 ciss_perf_done(sc, &qh);
2195 mtx_lock(&sc->ciss_mtx);
2196 ciss_complete(sc, &qh);
2197 mtx_unlock(&sc->ciss_mtx);
2201 /************************************************************************
2202 * Process completed requests.
2204 * Requests can be completed in three fashions:
2206 * - by invoking a callback function (cr_complete is non-null)
2207 * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
2208 * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
2211 ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh)
2213 struct ciss_request *cr;
2218 * Loop taking requests off the completed queue and performing
2219 * completion processing on them.
2222 if ((cr = ciss_dequeue_complete(sc, qh)) == NULL)
2224 ciss_unmap_request(cr);
2226 if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
2227 ciss_printf(sc, "WARNING: completing non-busy request\n");
2228 cr->cr_flags &= ~CISS_REQ_BUSY;
2231 * If the request has a callback, invoke it.
2233 if (cr->cr_complete != NULL) {
2234 cr->cr_complete(cr);
2239 * If someone is sleeping on this request, wake them up.
2241 if (cr->cr_flags & CISS_REQ_SLEEP) {
2242 cr->cr_flags &= ~CISS_REQ_SLEEP;
2248 * If someone is polling this request for completion, signal.
2250 if (cr->cr_flags & CISS_REQ_POLL) {
2251 cr->cr_flags &= ~CISS_REQ_POLL;
2256 * Give up and throw the request back on the free queue. This
2257 * should never happen; resources will probably be lost.
2259 ciss_printf(sc, "WARNING: completed command with no submitter\n");
2260 ciss_enqueue_free(cr);
2264 /************************************************************************
2265 * Report on the completion status of a request, and pass back SCSI
2266 * and command status values.
2269 _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func)
2271 struct ciss_command *cc;
2272 struct ciss_error_info *ce;
2277 ce = (struct ciss_error_info *)&(cc->sg[0]);
2280 * We don't consider data under/overrun an error for the Report
2281 * Logical/Physical LUNs commands.
2283 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
2284 ((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
2285 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
2286 ((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
2287 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
2288 (cc->cdb.cdb[0] == INQUIRY))) {
2289 cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
2290 debug(2, "ignoring irrelevant under/overrun error");
2294 * Check the command's error bit, if clear, there's no status and
2297 if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
2298 if (scsi_status != NULL)
2299 *scsi_status = SCSI_STATUS_OK;
2300 if (command_status != NULL)
2301 *command_status = CISS_CMD_STATUS_SUCCESS;
2304 if (command_status != NULL)
2305 *command_status = ce->command_status;
2306 if (scsi_status != NULL) {
2307 if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
2308 *scsi_status = ce->scsi_status;
2314 ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
2315 ce->command_status, ciss_name_command_status(ce->command_status),
2317 if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
2318 ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x, function %s\n",
2319 ce->additional_error_info.invalid_command.offense_size,
2320 ce->additional_error_info.invalid_command.offense_offset,
2321 ce->additional_error_info.invalid_command.offense_value,
2326 ciss_print_request(cr);
2331 /************************************************************************
2332 * Issue a request and don't return until it's completed.
2334 * Depending on adapter status, we may poll or sleep waiting for
2338 ciss_synch_request(struct ciss_request *cr, int timeout)
2340 if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
2341 return(ciss_wait_request(cr, timeout));
2343 return(ciss_poll_request(cr, timeout));
2347 /************************************************************************
2348 * Issue a request and poll for completion.
2350 * Timeout in milliseconds.
2353 ciss_poll_request(struct ciss_request *cr, int timeout)
2356 struct ciss_softc *sc;
2363 cr->cr_flags |= CISS_REQ_POLL;
2364 if ((error = ciss_start(cr)) != 0)
2369 ciss_perf_done(sc, &qh);
2372 ciss_complete(sc, &qh);
2373 if (!(cr->cr_flags & CISS_REQ_POLL))
2376 } while (timeout-- >= 0);
2377 return(EWOULDBLOCK);
2380 /************************************************************************
2381 * Issue a request and sleep waiting for completion.
2383 * Timeout in milliseconds. Note that a spurious wakeup will reset
2387 ciss_wait_request(struct ciss_request *cr, int timeout)
2393 cr->cr_flags |= CISS_REQ_SLEEP;
2394 if ((error = ciss_start(cr)) != 0)
2397 while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
2398 error = msleep(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ", (timeout * hz) / 1000);
2404 /************************************************************************
2405 * Abort a request. Note that a potential exists here to race the
2406 * request being completed; the caller must deal with this.
2409 ciss_abort_request(struct ciss_request *ar)
2411 struct ciss_request *cr;
2412 struct ciss_command *cc;
2413 struct ciss_message_cdb *cmc;
2419 if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
2422 /* build the abort command */
2424 cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL; /* addressing? */
2425 cc->header.address.physical.target = 0;
2426 cc->header.address.physical.bus = 0;
2427 cc->cdb.cdb_length = sizeof(*cmc);
2428 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
2429 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2430 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2431 cc->cdb.timeout = 30;
2433 cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
2434 cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
2435 cmc->type = CISS_MESSAGE_ABORT_TASK;
2436 cmc->abort_tag = ar->cr_tag; /* endianness?? */
2439 * Send the request and wait for a response. If we believe we
2440 * aborted the request OK, clear the flag that indicates it's
2443 error = ciss_synch_request(cr, 35 * 1000);
2445 error = ciss_report_request(cr, NULL, NULL);
2446 ciss_release_request(cr);
2453 /************************************************************************
2454 * Fetch and initialise a request
2457 ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
2459 struct ciss_request *cr;
2464 * Get a request and clean it up.
2466 if ((cr = ciss_dequeue_free(sc)) == NULL)
2471 cr->cr_complete = NULL;
2472 cr->cr_private = NULL;
2473 cr->cr_sg_tag = CISS_SG_MAX; /* Backstop to prevent accidents */
2475 ciss_preen_command(cr);
2481 ciss_preen_command(struct ciss_request *cr)
2483 struct ciss_command *cc;
2487 * Clean up the command structure.
2489 * Note that we set up the error_info structure here, since the
2490 * length can be overwritten by any command.
2493 cc->header.sg_in_list = 0; /* kinda inefficient this way */
2494 cc->header.sg_total = 0;
2495 cc->header.host_tag = cr->cr_tag << 2;
2496 cc->header.host_tag_zeroes = 0;
2497 bzero(&(cc->sg[0]), CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command));
2498 cmdphys = cr->cr_ccphys;
2499 cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
2500 cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
2503 /************************************************************************
2504 * Release a request to the free list.
2507 ciss_release_request(struct ciss_request *cr)
2509 struct ciss_softc *sc;
2515 /* release the request to the free queue */
2516 ciss_requeue_free(cr);
2519 /************************************************************************
2520 * Allocate a request that will be used to send a BMIC command. Do some
2521 * of the common setup here to avoid duplicating it everywhere else.
2524 ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
2525 int opcode, void **bufp, size_t bufsize)
2527 struct ciss_request *cr;
2528 struct ciss_command *cc;
2529 struct ciss_bmic_cdb *cbc;
2542 if ((error = ciss_get_request(sc, &cr)) != 0)
2546 * Allocate data storage if requested, determine the data direction.
2549 if ((bufsize > 0) && (bufp != NULL)) {
2550 if (*bufp == NULL) {
2551 if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
2557 dataout = 1; /* we are given a buffer, so we are writing */
2562 * Build a CISS BMIC command to get the logical drive ID.
2565 cr->cr_length = bufsize;
2567 cr->cr_flags = CISS_REQ_DATAIN;
2570 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2571 cc->header.address.physical.bus = 0;
2572 cc->header.address.physical.target = 0;
2573 cc->cdb.cdb_length = sizeof(*cbc);
2574 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2575 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2576 cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
2577 cc->cdb.timeout = 0;
2579 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
2580 bzero(cbc, sizeof(*cbc));
2581 cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
2582 cbc->bmic_opcode = opcode;
2583 cbc->size = htons((u_int16_t)bufsize);
2588 ciss_release_request(cr);
2591 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
2597 /************************************************************************
2598 * Handle a command passed in from userspace.
2601 ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
2603 struct ciss_request *cr;
2604 struct ciss_command *cc;
2605 struct ciss_error_info *ce;
2615 while (ciss_get_request(sc, &cr) != 0)
2616 msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
2620 * Allocate an in-kernel databuffer if required, copy in user data.
2622 mtx_unlock(&sc->ciss_mtx);
2623 cr->cr_length = ioc->buf_size;
2624 if (ioc->buf_size > 0) {
2625 if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
2629 if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
2630 debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2636 * Build the request based on the user command.
2638 bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
2639 bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
2641 /* XXX anything else to populate here? */
2642 mtx_lock(&sc->ciss_mtx);
2647 if ((error = ciss_synch_request(cr, 60 * 1000))) {
2648 debug(0, "request failed - %d", error);
2653 * Check to see if the command succeeded.
2655 ce = (struct ciss_error_info *)&(cc->sg[0]);
2656 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
2657 bzero(ce, sizeof(*ce));
2660 * Copy the results back to the user.
2662 bcopy(ce, &ioc->error_info, sizeof(*ce));
2663 mtx_unlock(&sc->ciss_mtx);
2664 if ((ioc->buf_size > 0) &&
2665 (error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
2666 debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2674 mtx_lock(&sc->ciss_mtx);
2677 if ((cr != NULL) && (cr->cr_data != NULL))
2678 free(cr->cr_data, CISS_MALLOC_CLASS);
2680 ciss_release_request(cr);
2684 /************************************************************************
2685 * Map a request into bus-visible space, initialise the scatter/gather
2689 ciss_map_request(struct ciss_request *cr)
2691 struct ciss_softc *sc;
2698 /* check that mapping is necessary */
2699 if (cr->cr_flags & CISS_REQ_MAPPED)
2702 cr->cr_flags |= CISS_REQ_MAPPED;
2704 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2705 BUS_DMASYNC_PREWRITE);
2707 if (cr->cr_data != NULL) {
2708 if (cr->cr_flags & CISS_REQ_CCB)
2709 error = bus_dmamap_load_ccb(sc->ciss_buffer_dmat,
2710 cr->cr_datamap, cr->cr_data,
2711 ciss_request_map_helper, cr, 0);
2713 error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2714 cr->cr_data, cr->cr_length,
2715 ciss_request_map_helper, cr, 0);
2720 * Post the command to the adapter.
2722 cr->cr_sg_tag = CISS_SG_NONE;
2723 cr->cr_flags |= CISS_REQ_BUSY;
2725 CISS_TL_PERF_POST_CMD(sc, cr);
2727 CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2734 ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2736 struct ciss_command *cc;
2737 struct ciss_request *cr;
2738 struct ciss_softc *sc;
2743 cr = (struct ciss_request *)arg;
2747 for (i = 0; i < nseg; i++) {
2748 cc->sg[i].address = segs[i].ds_addr;
2749 cc->sg[i].length = segs[i].ds_len;
2750 cc->sg[i].extension = 0;
2752 /* we leave the s/g table entirely within the command */
2753 cc->header.sg_in_list = nseg;
2754 cc->header.sg_total = nseg;
2756 if (cr->cr_flags & CISS_REQ_DATAIN)
2757 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2758 if (cr->cr_flags & CISS_REQ_DATAOUT)
2759 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2762 cr->cr_sg_tag = CISS_SG_NONE;
2764 cr->cr_sg_tag = CISS_SG_1;
2766 cr->cr_sg_tag = CISS_SG_2;
2768 cr->cr_sg_tag = CISS_SG_4;
2770 cr->cr_sg_tag = CISS_SG_8;
2771 else if (nseg <= 16)
2772 cr->cr_sg_tag = CISS_SG_16;
2773 else if (nseg <= 32)
2774 cr->cr_sg_tag = CISS_SG_32;
2776 cr->cr_sg_tag = CISS_SG_MAX;
2779 * Post the command to the adapter.
2781 cr->cr_flags |= CISS_REQ_BUSY;
2783 CISS_TL_PERF_POST_CMD(sc, cr);
2785 CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2788 /************************************************************************
2789 * Unmap a request from bus-visible space.
2792 ciss_unmap_request(struct ciss_request *cr)
2794 struct ciss_softc *sc;
2800 /* check that unmapping is necessary */
2801 if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
2804 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2805 BUS_DMASYNC_POSTWRITE);
2807 if (cr->cr_data == NULL)
2810 if (cr->cr_flags & CISS_REQ_DATAIN)
2811 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
2812 if (cr->cr_flags & CISS_REQ_DATAOUT)
2813 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
2815 bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2817 cr->cr_flags &= ~CISS_REQ_MAPPED;
2820 /************************************************************************
2821 * Attach the driver to CAM.
2823 * We put all the logical drives on a single SCSI bus.
2826 ciss_cam_init(struct ciss_softc *sc)
2833 * Allocate a devq. We can reuse this for the masked physical
2834 * devices if we decide to export these as well.
2836 if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests - 2)) == NULL) {
2837 ciss_printf(sc, "can't allocate CAM SIM queue\n");
2844 * This naturally wastes a bit of memory. The alternative is to allocate
2845 * and register each bus as it is found, and then track them on a linked
2846 * list. Unfortunately, the driver has a few places where it needs to
2847 * look up the SIM based solely on bus number, and it's unclear whether
2848 * a list traversal would work for these situations.
2850 maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
2851 CISS_PHYSICAL_BASE);
2852 sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
2853 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
2854 if (sc->ciss_cam_sim == NULL) {
2855 ciss_printf(sc, "can't allocate memory for controller SIM\n");
2859 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2860 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2862 device_get_unit(sc->ciss_dev),
2865 sc->ciss_max_requests - 2,
2866 sc->ciss_cam_devq)) == NULL) {
2867 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2872 * Register bus with this SIM.
2874 mtx_lock(&sc->ciss_mtx);
2875 if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) {
2876 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2877 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2878 mtx_unlock(&sc->ciss_mtx);
2882 mtx_unlock(&sc->ciss_mtx);
2885 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
2886 CISS_PHYSICAL_BASE; i++) {
2887 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2889 device_get_unit(sc->ciss_dev),
2891 sc->ciss_max_requests - 2,
2892 sc->ciss_cam_devq)) == NULL) {
2893 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2897 mtx_lock(&sc->ciss_mtx);
2898 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2899 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2900 mtx_unlock(&sc->ciss_mtx);
2903 mtx_unlock(&sc->ciss_mtx);
2909 /************************************************************************
2910 * Initiate a rescan of the 'logical devices' SIM
2913 ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
2919 if ((ccb = xpt_alloc_ccb_nowait()) == NULL) {
2920 ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
2924 if (xpt_create_path(&ccb->ccb_h.path, NULL,
2925 cam_sim_path(sc->ciss_cam_sim[bus]),
2926 target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2927 ciss_printf(sc, "rescan failed (can't create path)\n");
2932 /* scan is now in progress */
2935 /************************************************************************
2936 * Handle requests coming from CAM
2939 ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
2941 struct ciss_softc *sc;
2942 struct ccb_scsiio *csio;
2946 sc = cam_sim_softc(sim);
2947 bus = cam_sim_bus(sim);
2948 csio = (struct ccb_scsiio *)&ccb->csio;
2949 target = csio->ccb_h.target_id;
2950 physical = CISS_IS_PHYSICAL(bus);
2952 switch (ccb->ccb_h.func_code) {
2954 /* perform SCSI I/O */
2956 if (!ciss_cam_action_io(sim, csio))
2960 /* perform geometry calculations */
2961 case XPT_CALC_GEOMETRY:
2963 struct ccb_calc_geometry *ccg = &ccb->ccg;
2964 struct ciss_ldrive *ld;
2966 debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2970 ld = &sc->ciss_logical[bus][target];
2973 * Use the cached geometry settings unless the fault tolerance
2976 if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
2977 u_int32_t secs_per_cylinder;
2980 ccg->secs_per_track = 32;
2981 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2982 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2984 ccg->heads = ld->cl_geometry.heads;
2985 ccg->secs_per_track = ld->cl_geometry.sectors;
2986 ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2988 ccb->ccb_h.status = CAM_REQ_CMP;
2992 /* handle path attribute inquiry */
2995 struct ccb_pathinq *cpi = &ccb->cpi;
2998 debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
3000 cpi->version_num = 1;
3001 cpi->hba_inquiry = PI_TAG_ABLE; /* XXX is this correct? */
3002 cpi->target_sprt = 0;
3004 cpi->max_target = sc->ciss_cfg->max_logical_supported;
3005 cpi->max_lun = 0; /* 'logical drive' channel only */
3006 cpi->initiator_id = sc->ciss_cfg->max_logical_supported;
3007 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3008 strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
3009 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3010 cpi->unit_number = cam_sim_unit(sim);
3011 cpi->bus_id = cam_sim_bus(sim);
3012 cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */
3013 cpi->transport = XPORT_SPI;
3014 cpi->transport_version = 2;
3015 cpi->protocol = PROTO_SCSI;
3016 cpi->protocol_version = SCSI_REV_2;
3017 if (sc->ciss_cfg->max_sg_length == 0) {
3020 /* XXX Fix for ZMR cards that advertise max_sg_length == 32
3021 * Confusing bit here. max_sg_length is usually a power of 2. We always
3022 * need to subtract 1 to account for partial pages. Then we need to
3023 * align on a valid PAGE_SIZE so we round down to the nearest power of 2.
3024 * Add 1 so we can then subtract it out in the assignment to maxio.
3025 * The reason for all these shenanigans is to create a maxio value that
3026 * creates IO operations to volumes that yield consistent operations
3027 * with good performance.
3029 sg_length = sc->ciss_cfg->max_sg_length - 1;
3030 sg_length = (1 << (fls(sg_length) - 1)) + 1;
3032 cpi->maxio = (min(CISS_MAX_SG_ELEMENTS, sg_length) - 1) * PAGE_SIZE;
3033 ccb->ccb_h.status = CAM_REQ_CMP;
3037 case XPT_GET_TRAN_SETTINGS:
3039 struct ccb_trans_settings *cts = &ccb->cts;
3041 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3042 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3044 bus = cam_sim_bus(sim);
3045 target = cts->ccb_h.target_id;
3047 debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
3048 /* disconnect always OK */
3049 cts->protocol = PROTO_SCSI;
3050 cts->protocol_version = SCSI_REV_2;
3051 cts->transport = XPORT_SPI;
3052 cts->transport_version = 2;
3054 spi->valid = CTS_SPI_VALID_DISC;
3055 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
3057 scsi->valid = CTS_SCSI_VALID_TQ;
3058 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3060 cts->ccb_h.status = CAM_REQ_CMP;
3064 default: /* we can't do this */
3065 debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
3066 ccb->ccb_h.status = CAM_REQ_INVALID;
3073 /************************************************************************
3074 * Handle a CAM SCSI I/O request.
3077 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
3079 struct ciss_softc *sc;
3081 struct ciss_request *cr;
3082 struct ciss_command *cc;
3085 sc = cam_sim_softc(sim);
3086 bus = cam_sim_bus(sim);
3087 target = csio->ccb_h.target_id;
3089 debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
3091 /* check that the CDB pointer is not to a physical address */
3092 if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
3093 debug(3, " CDB pointer is to physical address");
3094 csio->ccb_h.status = CAM_REQ_CMP_ERR;
3097 /* abandon aborted ccbs or those that have failed validation */
3098 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
3099 debug(3, "abandoning CCB due to abort/validation failure");
3103 /* handle emulation of some SCSI commands ourself */
3104 if (ciss_cam_emulate(sc, csio))
3108 * Get a request to manage this command. If we can't, return the
3109 * ccb, freeze the queue and flag so that we unfreeze it when a
3110 * request completes.
3112 if ((error = ciss_get_request(sc, &cr)) != 0) {
3113 xpt_freeze_simq(sim, 1);
3114 sc->ciss_flags |= CISS_FLAG_BUSY;
3115 csio->ccb_h.status |= CAM_REQUEUE_REQ;
3120 * Build the command.
3124 cr->cr_length = csio->dxfer_len;
3125 cr->cr_complete = ciss_cam_complete;
3126 cr->cr_private = csio;
3129 * Target the right logical volume.
3131 if (CISS_IS_PHYSICAL(bus))
3132 cc->header.address =
3133 sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
3135 cc->header.address =
3136 sc->ciss_logical[bus][target].cl_address;
3137 cc->cdb.cdb_length = csio->cdb_len;
3138 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3139 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; /* XXX ordered tags? */
3140 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
3141 cr->cr_flags = CISS_REQ_DATAOUT | CISS_REQ_CCB;
3142 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3143 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
3144 cr->cr_flags = CISS_REQ_DATAIN | CISS_REQ_CCB;
3145 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3149 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
3151 cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
3152 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
3153 bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
3155 bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
3159 * Submit the request to the adapter.
3161 * Note that this may fail if we're unable to map the request (and
3162 * if we ever learn a transport layer other than simple, may fail
3163 * if the adapter rejects the command).
3165 if ((error = ciss_start(cr)) != 0) {
3166 xpt_freeze_simq(sim, 1);
3167 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3168 if (error == EINPROGRESS) {
3171 csio->ccb_h.status |= CAM_REQUEUE_REQ;
3172 ciss_release_request(cr);
3180 /************************************************************************
3181 * Emulate SCSI commands the adapter doesn't handle as we might like.
3184 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
3189 target = csio->ccb_h.target_id;
3190 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3191 opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3192 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
3194 if (CISS_IS_PHYSICAL(bus)) {
3195 if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
3196 csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3197 xpt_done((union ccb *)csio);
3204 * Handle requests for volumes that don't exist or are not online.
3205 * A selection timeout is slightly better than an illegal request.
3206 * Other errors might be better.
3208 if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
3209 csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3210 xpt_done((union ccb *)csio);
3214 /* if we have to fake Synchronise Cache */
3215 if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
3217 * If this is a Synchronise Cache command, typically issued when
3218 * a device is closed, flush the adapter and complete now.
3220 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
3221 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
3222 ciss_flush_adapter(sc);
3223 csio->ccb_h.status |= CAM_REQ_CMP;
3224 xpt_done((union ccb *)csio);
3230 * A CISS target can only ever have one lun per target. REPORT_LUNS requires
3231 * at least one LUN field to be pre created for us, so snag it and fill in
3232 * the least significant byte indicating 1 LUN here. Emulate the command
3233 * return to shut up warning on console of a CDB error. swb
3235 if (opcode == REPORT_LUNS && csio->dxfer_len > 0) {
3236 csio->data_ptr[3] = 8;
3237 csio->ccb_h.status |= CAM_REQ_CMP;
3238 xpt_done((union ccb *)csio);
3245 /************************************************************************
3246 * Check for possibly-completed commands.
3249 ciss_cam_poll(struct cam_sim *sim)
3252 struct ciss_softc *sc = cam_sim_softc(sim);
3258 ciss_perf_done(sc, &qh);
3261 ciss_complete(sc, &qh);
3264 /************************************************************************
3265 * Handle completion of a command - pass results back through the CCB
3268 ciss_cam_complete(struct ciss_request *cr)
3270 struct ciss_softc *sc;
3271 struct ciss_command *cc;
3272 struct ciss_error_info *ce;
3273 struct ccb_scsiio *csio;
3281 ce = (struct ciss_error_info *)&(cc->sg[0]);
3282 csio = (struct ccb_scsiio *)cr->cr_private;
3285 * Extract status values from request.
3287 ciss_report_request(cr, &command_status, &scsi_status);
3288 csio->scsi_status = scsi_status;
3291 * Handle specific SCSI status values.
3293 switch(scsi_status) {
3294 /* no status due to adapter error */
3296 debug(0, "adapter error");
3297 csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3300 /* no status due to command completed OK */
3301 case SCSI_STATUS_OK: /* CISS_SCSI_STATUS_GOOD */
3302 debug(2, "SCSI_STATUS_OK");
3303 csio->ccb_h.status |= CAM_REQ_CMP;
3306 /* check condition, sense data included */
3307 case SCSI_STATUS_CHECK_COND: /* CISS_SCSI_STATUS_CHECK_CONDITION */
3308 debug(0, "SCSI_STATUS_CHECK_COND sense size %d resid %d\n",
3309 ce->sense_length, ce->residual_count);
3310 bzero(&csio->sense_data, SSD_FULL_SIZE);
3311 bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
3312 if (csio->sense_len > ce->sense_length)
3313 csio->sense_resid = csio->sense_len - ce->sense_length;
3315 csio->sense_resid = 0;
3316 csio->resid = ce->residual_count;
3317 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
3320 struct scsi_sense_data *sns = (struct scsi_sense_data *)&ce->sense_info[0];
3321 debug(0, "sense key %x", scsi_get_sense_key(sns, csio->sense_len -
3322 csio->sense_resid, /*show_errors*/ 1));
3327 case SCSI_STATUS_BUSY: /* CISS_SCSI_STATUS_BUSY */
3328 debug(0, "SCSI_STATUS_BUSY");
3329 csio->ccb_h.status |= CAM_SCSI_BUSY;
3333 debug(0, "unknown status 0x%x", csio->scsi_status);
3334 csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3338 /* handle post-command fixup */
3339 ciss_cam_complete_fixup(sc, csio);
3341 ciss_release_request(cr);
3342 if (sc->ciss_flags & CISS_FLAG_BUSY) {
3343 sc->ciss_flags &= ~CISS_FLAG_BUSY;
3344 if (csio->ccb_h.status & CAM_RELEASE_SIMQ)
3345 xpt_release_simq(xpt_path_sim(csio->ccb_h.path), 0);
3347 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3349 xpt_done((union ccb *)csio);
3352 /********************************************************************************
3353 * Fix up the result of some commands here.
3356 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
3358 struct scsi_inquiry_data *inq;
3359 struct ciss_ldrive *cl;
3363 cdb = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3364 (uint8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes;
3365 if (cdb[0] == INQUIRY &&
3366 (cdb[1] & SI_EVPD) == 0 &&
3367 (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
3368 csio->dxfer_len >= SHORT_INQUIRY_LENGTH) {
3370 inq = (struct scsi_inquiry_data *)csio->data_ptr;
3371 target = csio->ccb_h.target_id;
3372 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3375 * Don't let hard drives be seen by the DA driver. They will still be
3376 * attached by the PASS driver.
3378 if (CISS_IS_PHYSICAL(bus)) {
3379 if (SID_TYPE(inq) == T_DIRECT)
3380 inq->device = (inq->device & 0xe0) | T_NODEVICE;
3384 cl = &sc->ciss_logical[bus][target];
3386 padstr(inq->vendor, "HP",
3388 padstr(inq->product,
3389 ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance),
3391 padstr(inq->revision,
3392 ciss_name_ldrive_status(cl->cl_lstatus->status),
3398 /********************************************************************************
3399 * Name the device at (target)
3401 * XXX is this strictly correct?
3404 ciss_name_device(struct ciss_softc *sc, int bus, int target)
3406 struct cam_periph *periph;
3407 struct cam_path *path;
3410 if (CISS_IS_PHYSICAL(bus))
3413 status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
3416 if (status == CAM_REQ_CMP) {
3417 mtx_lock(&sc->ciss_mtx);
3418 periph = cam_periph_find(path, NULL);
3419 sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
3420 periph->periph_name, periph->unit_number);
3421 mtx_unlock(&sc->ciss_mtx);
3422 xpt_free_path(path);
3425 sc->ciss_logical[bus][target].cl_name[0] = 0;
3429 /************************************************************************
3430 * Periodic status monitoring.
3433 ciss_periodic(void *arg)
3435 struct ciss_softc *sc;
3436 struct ciss_request *cr = NULL;
3437 struct ciss_command *cc = NULL;
3442 sc = (struct ciss_softc *)arg;
3445 * Check the adapter heartbeat.
3447 if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
3448 sc->ciss_heart_attack++;
3449 debug(0, "adapter heart attack in progress 0x%x/%d",
3450 sc->ciss_heartbeat, sc->ciss_heart_attack);
3451 if (sc->ciss_heart_attack == 3) {
3452 ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
3453 ciss_disable_adapter(sc);
3457 sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
3458 sc->ciss_heart_attack = 0;
3459 debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
3463 * Send the NOP message and wait for a response.
3465 if (ciss_nop_message_heartbeat != 0 && (error = ciss_get_request(sc, &cr)) == 0) {
3467 cr->cr_complete = ciss_nop_complete;
3468 cc->cdb.cdb_length = 1;
3469 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
3470 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3471 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3472 cc->cdb.timeout = 0;
3473 cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
3475 if ((error = ciss_start(cr)) != 0) {
3476 ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3481 * If the notify event request has died for some reason, or has
3482 * not started yet, restart it.
3484 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
3485 debug(0, "(re)starting Event Notify chain");
3486 ciss_notify_event(sc);
3492 callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
3496 ciss_nop_complete(struct ciss_request *cr)
3498 struct ciss_softc *sc;
3499 static int first_time = 1;
3502 if (ciss_report_request(cr, NULL, NULL) != 0) {
3503 if (first_time == 1) {
3505 ciss_printf(sc, "SENDING NOP MESSAGE FAILED (not logging anymore)\n");
3509 ciss_release_request(cr);
3512 /************************************************************************
3513 * Disable the adapter.
3515 * The all requests in completed queue is failed with hardware error.
3516 * This will cause failover in a multipath configuration.
3519 ciss_disable_adapter(struct ciss_softc *sc)
3522 struct ciss_request *cr;
3523 struct ciss_command *cc;
3524 struct ciss_error_info *ce;
3527 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
3528 pci_disable_busmaster(sc->ciss_dev);
3529 sc->ciss_flags &= ~CISS_FLAG_RUNNING;
3531 for (i = 1; i < sc->ciss_max_requests; i++) {
3532 cr = &sc->ciss_request[i];
3533 if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
3537 ce = (struct ciss_error_info *)&(cc->sg[0]);
3538 ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
3539 ciss_enqueue_complete(cr, &qh);
3543 if ((cr = ciss_dequeue_complete(sc, &qh)) == NULL)
3547 * If the request has a callback, invoke it.
3549 if (cr->cr_complete != NULL) {
3550 cr->cr_complete(cr);
3555 * If someone is sleeping on this request, wake them up.
3557 if (cr->cr_flags & CISS_REQ_SLEEP) {
3558 cr->cr_flags &= ~CISS_REQ_SLEEP;
3565 /************************************************************************
3566 * Request a notification response from the adapter.
3568 * If (cr) is NULL, this is the first request of the adapter, so
3569 * reset the adapter's message pointer and start with the oldest
3570 * message available.
3573 ciss_notify_event(struct ciss_softc *sc)
3575 struct ciss_request *cr;
3576 struct ciss_command *cc;
3577 struct ciss_notify_cdb *cnc;
3582 cr = sc->ciss_periodic_notify;
3584 /* get a request if we don't already have one */
3586 if ((error = ciss_get_request(sc, &cr)) != 0) {
3587 debug(0, "can't get notify event request");
3590 sc->ciss_periodic_notify = cr;
3591 cr->cr_complete = ciss_notify_complete;
3592 debug(1, "acquired request %d", cr->cr_tag);
3596 * Get a databuffer if we don't already have one, note that the
3597 * adapter command wants a larger buffer than the actual
3600 if (cr->cr_data == NULL) {
3601 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3602 debug(0, "can't get notify event request buffer");
3606 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3609 /* re-setup the request's command (since we never release it) XXX overkill*/
3610 ciss_preen_command(cr);
3612 /* (re)build the notify event command */
3614 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3615 cc->header.address.physical.bus = 0;
3616 cc->header.address.physical.target = 0;
3618 cc->cdb.cdb_length = sizeof(*cnc);
3619 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3620 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3621 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3622 cc->cdb.timeout = 0; /* no timeout, we hope */
3624 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3625 bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
3626 cnc->opcode = CISS_OPCODE_READ;
3627 cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
3628 cnc->timeout = 0; /* no timeout, we hope */
3629 cnc->synchronous = 0;
3631 cnc->seek_to_oldest = 0;
3632 if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
3636 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3638 /* submit the request */
3639 error = ciss_start(cr);
3644 if (cr->cr_data != NULL)
3645 free(cr->cr_data, CISS_MALLOC_CLASS);
3646 ciss_release_request(cr);
3648 sc->ciss_periodic_notify = NULL;
3649 debug(0, "can't submit notify event request");
3650 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3652 debug(1, "notify event submitted");
3653 sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
3658 ciss_notify_complete(struct ciss_request *cr)
3660 struct ciss_command *cc;
3661 struct ciss_notify *cn;
3662 struct ciss_softc *sc;
3668 cn = (struct ciss_notify *)cr->cr_data;
3672 * Report request results, decode status.
3674 ciss_report_request(cr, &command_status, &scsi_status);
3677 * Abort the chain on a fatal error.
3679 * XXX which of these are actually errors?
3681 if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
3682 (command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
3683 (command_status != CISS_CMD_STATUS_TIMEOUT)) { /* XXX timeout? */
3684 ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
3685 ciss_name_command_status(command_status));
3686 ciss_release_request(cr);
3687 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3692 * If the adapter gave us a text message, print it.
3694 if (cn->message[0] != 0)
3695 ciss_printf(sc, "*** %.80s\n", cn->message);
3697 debug(0, "notify event class %d subclass %d detail %d",
3698 cn->class, cn->subclass, cn->detail);
3701 * If the response indicates that the notifier has been aborted,
3702 * release the notifier command.
3704 if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
3705 (cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
3706 (cn->detail == 1)) {
3707 debug(0, "notifier exiting");
3708 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3709 ciss_release_request(cr);
3710 sc->ciss_periodic_notify = NULL;
3711 wakeup(&sc->ciss_periodic_notify);
3713 /* Handle notify events in a kernel thread */
3714 ciss_enqueue_notify(cr);
3715 sc->ciss_periodic_notify = NULL;
3716 wakeup(&sc->ciss_periodic_notify);
3717 wakeup(&sc->ciss_notify);
3720 * Send a new notify event command, if we're not aborting.
3722 if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
3723 ciss_notify_event(sc);
3727 /************************************************************************
3728 * Abort the Notify Event chain.
3730 * Note that we can't just abort the command in progress; we have to
3731 * explicitly issue an Abort Notify Event command in order for the
3732 * adapter to clean up correctly.
3734 * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
3735 * the chain will not restart itself.
3738 ciss_notify_abort(struct ciss_softc *sc)
3740 struct ciss_request *cr;
3741 struct ciss_command *cc;
3742 struct ciss_notify_cdb *cnc;
3743 int error, command_status, scsi_status;
3750 /* verify that there's an outstanding command */
3751 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3754 /* get a command to issue the abort with */
3755 if ((error = ciss_get_request(sc, &cr)))
3758 /* get a buffer for the result */
3759 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3760 debug(0, "can't get notify event request buffer");
3764 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3768 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3769 cc->header.address.physical.bus = 0;
3770 cc->header.address.physical.target = 0;
3771 cc->cdb.cdb_length = sizeof(*cnc);
3772 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3773 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3774 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3775 cc->cdb.timeout = 0; /* no timeout, we hope */
3777 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3778 bzero(cnc, sizeof(*cnc));
3779 cnc->opcode = CISS_OPCODE_WRITE;
3780 cnc->command = CISS_COMMAND_ABORT_NOTIFY;
3781 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3783 ciss_print_request(cr);
3786 * Submit the request and wait for it to complete.
3788 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3789 ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
3796 ciss_report_request(cr, &command_status, &scsi_status);
3797 switch(command_status) {
3798 case CISS_CMD_STATUS_SUCCESS:
3800 case CISS_CMD_STATUS_INVALID_COMMAND:
3802 * Some older adapters don't support the CISS version of this
3803 * command. Fall back to using the BMIC version.
3805 error = ciss_notify_abort_bmic(sc);
3810 case CISS_CMD_STATUS_TARGET_STATUS:
3812 * This can happen if the adapter thinks there wasn't an outstanding
3813 * Notify Event command but we did. We clean up here.
3815 if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
3816 if (sc->ciss_periodic_notify != NULL)
3817 ciss_release_request(sc->ciss_periodic_notify);
3824 ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
3825 ciss_name_command_status(command_status));
3831 * Sleep waiting for the notifier command to complete. Note
3832 * that if it doesn't, we may end up in a bad situation, since
3833 * the adapter may deliver it later. Also note that the adapter
3834 * requires the Notify Event command to be cancelled in order to
3835 * maintain internal bookkeeping.
3837 while (sc->ciss_periodic_notify != NULL) {
3838 error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
3839 if (error == EWOULDBLOCK) {
3840 ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
3846 /* release the cancel request */
3848 if (cr->cr_data != NULL)
3849 free(cr->cr_data, CISS_MALLOC_CLASS);
3850 ciss_release_request(cr);
3853 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3857 /************************************************************************
3858 * Abort the Notify Event chain using a BMIC command.
3861 ciss_notify_abort_bmic(struct ciss_softc *sc)
3863 struct ciss_request *cr;
3864 int error, command_status;
3871 /* verify that there's an outstanding command */
3872 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3876 * Build a BMIC command to cancel the Notify on Event command.
3878 * Note that we are sending a CISS opcode here. Odd.
3880 if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
3885 * Submit the request and wait for it to complete.
3887 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3888 ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
3895 ciss_report_request(cr, &command_status, NULL);
3896 switch(command_status) {
3897 case CISS_CMD_STATUS_SUCCESS:
3900 ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
3901 ciss_name_command_status(command_status));
3908 ciss_release_request(cr);
3912 /************************************************************************
3913 * Handle rescanning all the logical volumes when a notify event
3914 * causes the drives to come online or offline.
3917 ciss_notify_rescan_logical(struct ciss_softc *sc)
3919 struct ciss_lun_report *cll;
3920 struct ciss_ldrive *ld;
3924 * We must rescan all logical volumes to get the right logical
3927 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
3928 sc->ciss_cfg->max_logical_supported);
3932 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
3935 * Delete any of the drives which were destroyed by the
3938 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
3939 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
3940 ld = &sc->ciss_logical[i][j];
3942 if (ld->cl_update == 0)
3945 if (ld->cl_status != CISS_LD_ONLINE) {
3946 ciss_cam_rescan_target(sc, i, j);
3949 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
3951 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
3953 ld->cl_ldrive = NULL;
3954 ld->cl_lstatus = NULL;
3960 * Scan for new drives.
3962 for (i = 0; i < ndrives; i++) {
3965 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
3966 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
3967 ld = &sc->ciss_logical[bus][target];
3969 if (ld->cl_update == 0)
3973 ld->cl_address = cll->lun[i];
3974 ld->cl_controller = &sc->ciss_controllers[bus];
3975 if (ciss_identify_logical(sc, ld) == 0) {
3976 ciss_cam_rescan_target(sc, bus, target);
3979 free(cll, CISS_MALLOC_CLASS);
3982 /************************************************************************
3983 * Handle a notify event relating to the status of a logical drive.
3985 * XXX need to be able to defer some of these to properly handle
3986 * calling the "ID Physical drive" command, unless the 'extended'
3987 * drive IDs are always in BIG_MAP format.
3990 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
3992 struct ciss_ldrive *ld;
3998 bus = cn->device.physical.bus;
3999 target = cn->data.logical_status.logical_drive;
4000 ld = &sc->ciss_logical[bus][target];
4002 switch (cn->subclass) {
4003 case CISS_NOTIFY_LOGICAL_STATUS:
4004 switch (cn->detail) {
4006 ciss_name_device(sc, bus, target);
4007 ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
4008 cn->data.logical_status.logical_drive, ld->cl_name,
4009 ciss_name_ldrive_status(cn->data.logical_status.previous_state),
4010 ciss_name_ldrive_status(cn->data.logical_status.new_state),
4011 cn->data.logical_status.spare_state,
4012 "\20\1configured\2rebuilding\3failed\4in use\5available\n");
4015 * Update our idea of the drive's status.
4017 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4018 if (ld->cl_lstatus != NULL)
4019 ld->cl_lstatus->status = cn->data.logical_status.new_state;
4022 * Have CAM rescan the drive if its status has changed.
4024 rescan_ld = (cn->data.logical_status.previous_state !=
4025 cn->data.logical_status.new_state) ? 1 : 0;
4028 ciss_notify_rescan_logical(sc);
4033 case 1: /* logical drive has recognised new media, needs Accept Media Exchange */
4034 ciss_name_device(sc, bus, target);
4035 ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
4036 cn->data.logical_status.logical_drive, ld->cl_name);
4037 ciss_accept_media(sc, ld);
4040 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4041 ciss_notify_rescan_logical(sc);
4046 ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
4047 cn->data.rebuild_aborted.logical_drive,
4049 (cn->detail == 2) ? "read" : "write");
4054 case CISS_NOTIFY_LOGICAL_ERROR:
4055 if (cn->detail == 0) {
4056 ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
4057 cn->data.io_error.logical_drive,
4059 cn->data.io_error.failure_bus,
4060 cn->data.io_error.failure_drive);
4061 /* XXX should we take the drive down at this point, or will we be told? */
4065 case CISS_NOTIFY_LOGICAL_SURFACE:
4066 if (cn->detail == 0)
4067 ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
4068 cn->data.consistency_completed.logical_drive,
4074 /************************************************************************
4075 * Handle a notify event relating to the status of a physical drive.
4078 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
4082 /************************************************************************
4083 * Handle a notify event relating to the status of a physical drive.
4086 ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
4088 struct ciss_lun_report *cll = NULL;
4091 switch (cn->subclass) {
4092 case CISS_NOTIFY_HOTPLUG_PHYSICAL:
4093 case CISS_NOTIFY_HOTPLUG_NONDISK:
4094 bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
4096 CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
4098 if (cn->detail == 0) {
4100 * Mark the device offline so that it'll start producing selection
4101 * timeouts to the upper layer.
4103 if ((bus >= 0) && (target >= 0))
4104 sc->ciss_physical[bus][target].cp_online = 0;
4107 * Rescan the physical lun list for new items
4109 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
4110 sc->ciss_cfg->max_physical_supported);
4112 ciss_printf(sc, "Warning, cannot get physical lun list\n");
4115 ciss_filter_physical(sc, cll);
4120 ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
4125 free(cll, CISS_MALLOC_CLASS);
4128 /************************************************************************
4129 * Handle deferred processing of notify events. Notify events may need
4130 * sleep which is unsafe during an interrupt.
4133 ciss_notify_thread(void *arg)
4135 struct ciss_softc *sc;
4136 struct ciss_request *cr;
4137 struct ciss_notify *cn;
4139 sc = (struct ciss_softc *)arg;
4140 #if __FreeBSD_version >= 500000
4141 mtx_lock(&sc->ciss_mtx);
4145 if (STAILQ_EMPTY(&sc->ciss_notify) != 0 &&
4146 (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
4147 msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
4150 if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
4153 cr = ciss_dequeue_notify(sc);
4157 cn = (struct ciss_notify *)cr->cr_data;
4159 switch (cn->class) {
4160 case CISS_NOTIFY_HOTPLUG:
4161 ciss_notify_hotplug(sc, cn);
4163 case CISS_NOTIFY_LOGICAL:
4164 ciss_notify_logical(sc, cn);
4166 case CISS_NOTIFY_PHYSICAL:
4167 ciss_notify_physical(sc, cn);
4171 ciss_release_request(cr);
4174 sc->ciss_notify_thread = NULL;
4175 wakeup(&sc->ciss_notify_thread);
4177 #if __FreeBSD_version >= 500000
4178 mtx_unlock(&sc->ciss_mtx);
4183 /************************************************************************
4184 * Start the notification kernel thread.
4187 ciss_spawn_notify_thread(struct ciss_softc *sc)
4190 #if __FreeBSD_version > 500005
4191 if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4192 &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
4193 device_get_unit(sc->ciss_dev)))
4195 if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4196 &sc->ciss_notify_thread, "ciss_notify%d",
4197 device_get_unit(sc->ciss_dev)))
4199 panic("Could not create notify thread\n");
4202 /************************************************************************
4203 * Kill the notification kernel thread.
4206 ciss_kill_notify_thread(struct ciss_softc *sc)
4209 if (sc->ciss_notify_thread == NULL)
4212 sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
4213 wakeup(&sc->ciss_notify);
4214 msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
4217 /************************************************************************
4221 ciss_print_request(struct ciss_request *cr)
4223 struct ciss_softc *sc;
4224 struct ciss_command *cc;
4230 ciss_printf(sc, "REQUEST @ %p\n", cr);
4231 ciss_printf(sc, " data %p/%d tag %d flags %b\n",
4232 cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
4233 "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
4234 ciss_printf(sc, " sg list/total %d/%d host tag 0x%x\n",
4235 cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
4236 switch(cc->header.address.mode.mode) {
4237 case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
4238 case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
4239 ciss_printf(sc, " physical bus %d target %d\n",
4240 cc->header.address.physical.bus, cc->header.address.physical.target);
4242 case CISS_HDR_ADDRESS_MODE_LOGICAL:
4243 ciss_printf(sc, " logical unit %d\n", cc->header.address.logical.lun);
4246 ciss_printf(sc, " %s cdb length %d type %s attribute %s\n",
4247 (cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
4248 (cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
4249 (cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
4251 (cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
4252 (cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
4253 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
4254 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
4255 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
4256 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
4257 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
4258 ciss_printf(sc, " %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
4260 if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
4261 /* XXX print error info */
4263 /* since we don't use chained s/g, don't support it here */
4264 for (i = 0; i < cc->header.sg_in_list; i++) {
4266 ciss_printf(sc, " ");
4267 printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
4268 if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
4274 /************************************************************************
4275 * Print information about the status of a logical drive.
4278 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
4282 if (ld->cl_lstatus == NULL) {
4283 printf("does not exist\n");
4287 /* print drive status */
4288 switch(ld->cl_lstatus->status) {
4289 case CISS_LSTATUS_OK:
4292 case CISS_LSTATUS_INTERIM_RECOVERY:
4293 printf("in interim recovery mode\n");
4295 case CISS_LSTATUS_READY_RECOVERY:
4296 printf("ready to begin recovery\n");
4298 case CISS_LSTATUS_RECOVERING:
4299 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4300 target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4301 printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
4302 bus, target, ld->cl_lstatus->blocks_to_recover);
4304 case CISS_LSTATUS_EXPANDING:
4305 printf("being expanded, %u blocks remaining\n",
4306 ld->cl_lstatus->blocks_to_recover);
4308 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4309 printf("queued for expansion\n");
4311 case CISS_LSTATUS_FAILED:
4312 printf("queued for expansion\n");
4314 case CISS_LSTATUS_WRONG_PDRIVE:
4315 printf("wrong physical drive inserted\n");
4317 case CISS_LSTATUS_MISSING_PDRIVE:
4318 printf("missing a needed physical drive\n");
4320 case CISS_LSTATUS_BECOMING_READY:
4321 printf("becoming ready\n");
4325 /* print failed physical drives */
4326 for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
4327 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
4328 target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
4331 ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
4332 ld->cl_lstatus->drive_failure_map[i]);
4337 #include "opt_ddb.h"
4339 #include <ddb/ddb.h>
4340 /************************************************************************
4341 * Print information about the controller/driver.
4344 ciss_print_adapter(struct ciss_softc *sc)
4348 ciss_printf(sc, "ADAPTER:\n");
4349 for (i = 0; i < CISSQ_COUNT; i++) {
4350 ciss_printf(sc, "%s %d/%d\n",
4352 i == 1 ? "busy" : "complete",
4353 sc->ciss_qstat[i].q_length,
4354 sc->ciss_qstat[i].q_max);
4356 ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
4357 ciss_printf(sc, "flags %b\n", sc->ciss_flags,
4358 "\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
4360 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
4361 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
4362 ciss_printf(sc, "LOGICAL DRIVE %d: ", i);
4363 ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
4367 /* XXX Should physical drives be printed out here? */
4369 for (i = 1; i < sc->ciss_max_requests; i++)
4370 ciss_print_request(sc->ciss_request + i);
4374 DB_COMMAND(ciss_prt, db_ciss_prt)
4376 struct ciss_softc *sc;
4380 dc = devclass_find("ciss");
4382 printf("%s: can't find devclass!\n", __func__);
4385 maxciss = devclass_get_maxunit(dc);
4386 for (i = 0; i < maxciss; i++) {
4387 sc = devclass_get_softc(dc, i);
4388 ciss_print_adapter(sc);
4394 /************************************************************************
4395 * Return a name for a logical drive status value.
4398 ciss_name_ldrive_status(int status)
4401 case CISS_LSTATUS_OK:
4403 case CISS_LSTATUS_FAILED:
4405 case CISS_LSTATUS_NOT_CONFIGURED:
4406 return("not configured");
4407 case CISS_LSTATUS_INTERIM_RECOVERY:
4408 return("interim recovery");
4409 case CISS_LSTATUS_READY_RECOVERY:
4410 return("ready for recovery");
4411 case CISS_LSTATUS_RECOVERING:
4412 return("recovering");
4413 case CISS_LSTATUS_WRONG_PDRIVE:
4414 return("wrong physical drive inserted");
4415 case CISS_LSTATUS_MISSING_PDRIVE:
4416 return("missing physical drive");
4417 case CISS_LSTATUS_EXPANDING:
4418 return("expanding");
4419 case CISS_LSTATUS_BECOMING_READY:
4420 return("becoming ready");
4421 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4422 return("queued for expansion");
4424 return("unknown status");
4427 /************************************************************************
4428 * Return an online/offline/nonexistent value for a logical drive
4432 ciss_decode_ldrive_status(int status)
4435 case CISS_LSTATUS_NOT_CONFIGURED:
4436 return(CISS_LD_NONEXISTENT);
4438 case CISS_LSTATUS_OK:
4439 case CISS_LSTATUS_INTERIM_RECOVERY:
4440 case CISS_LSTATUS_READY_RECOVERY:
4441 case CISS_LSTATUS_RECOVERING:
4442 case CISS_LSTATUS_EXPANDING:
4443 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4444 return(CISS_LD_ONLINE);
4446 case CISS_LSTATUS_FAILED:
4447 case CISS_LSTATUS_WRONG_PDRIVE:
4448 case CISS_LSTATUS_MISSING_PDRIVE:
4449 case CISS_LSTATUS_BECOMING_READY:
4451 return(CISS_LD_OFFLINE);
4456 /************************************************************************
4457 * Return a name for a logical drive's organisation.
4460 ciss_name_ldrive_org(int org)
4463 case CISS_LDRIVE_RAID0:
4465 case CISS_LDRIVE_RAID1:
4466 return("RAID 1(1+0)");
4467 case CISS_LDRIVE_RAID4:
4469 case CISS_LDRIVE_RAID5:
4471 case CISS_LDRIVE_RAID51:
4473 case CISS_LDRIVE_RAIDADG:
4479 /************************************************************************
4480 * Return a name for a command status value.
4483 ciss_name_command_status(int status)
4486 case CISS_CMD_STATUS_SUCCESS:
4488 case CISS_CMD_STATUS_TARGET_STATUS:
4489 return("target status");
4490 case CISS_CMD_STATUS_DATA_UNDERRUN:
4491 return("data underrun");
4492 case CISS_CMD_STATUS_DATA_OVERRUN:
4493 return("data overrun");
4494 case CISS_CMD_STATUS_INVALID_COMMAND:
4495 return("invalid command");
4496 case CISS_CMD_STATUS_PROTOCOL_ERROR:
4497 return("protocol error");
4498 case CISS_CMD_STATUS_HARDWARE_ERROR:
4499 return("hardware error");
4500 case CISS_CMD_STATUS_CONNECTION_LOST:
4501 return("connection lost");
4502 case CISS_CMD_STATUS_ABORTED:
4504 case CISS_CMD_STATUS_ABORT_FAILED:
4505 return("abort failed");
4506 case CISS_CMD_STATUS_UNSOLICITED_ABORT:
4507 return("unsolicited abort");
4508 case CISS_CMD_STATUS_TIMEOUT:
4510 case CISS_CMD_STATUS_UNABORTABLE:
4511 return("unabortable");
4513 return("unknown status");
4516 /************************************************************************
4517 * Handle an open on the control device.
4520 ciss_open(struct cdev *dev, int flags, int fmt, struct thread *p)
4522 struct ciss_softc *sc;
4526 sc = (struct ciss_softc *)dev->si_drv1;
4528 /* we might want to veto if someone already has us open */
4530 mtx_lock(&sc->ciss_mtx);
4531 sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
4532 mtx_unlock(&sc->ciss_mtx);
4536 /************************************************************************
4537 * Handle the last close on the control device.
4540 ciss_close(struct cdev *dev, int flags, int fmt, struct thread *p)
4542 struct ciss_softc *sc;
4546 sc = (struct ciss_softc *)dev->si_drv1;
4548 mtx_lock(&sc->ciss_mtx);
4549 sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
4550 mtx_unlock(&sc->ciss_mtx);
4554 /********************************************************************************
4555 * Handle adapter-specific control operations.
4557 * Note that the API here is compatible with the Linux driver, in order to
4558 * simplify the porting of Compaq's userland tools.
4561 ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *p)
4563 struct ciss_softc *sc;
4564 IOCTL_Command_struct *ioc = (IOCTL_Command_struct *)addr;
4566 IOCTL_Command_struct32 *ioc32 = (IOCTL_Command_struct32 *)addr;
4567 IOCTL_Command_struct ioc_swab;
4573 sc = (struct ciss_softc *)dev->si_drv1;
4575 mtx_lock(&sc->ciss_mtx);
4578 case CCISS_GETQSTATS:
4580 union ciss_statrequest *cr = (union ciss_statrequest *)addr;
4582 switch (cr->cs_item) {
4585 bcopy(&sc->ciss_qstat[cr->cs_item], &cr->cs_qstat,
4586 sizeof(struct ciss_qstat));
4596 case CCISS_GETPCIINFO:
4598 cciss_pci_info_struct *pis = (cciss_pci_info_struct *)addr;
4600 pis->bus = pci_get_bus(sc->ciss_dev);
4601 pis->dev_fn = pci_get_slot(sc->ciss_dev);
4602 pis->board_id = (pci_get_subvendor(sc->ciss_dev) << 16) |
4603 pci_get_subdevice(sc->ciss_dev);
4608 case CCISS_GETINTINFO:
4610 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4612 cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
4613 cis->count = sc->ciss_cfg->interrupt_coalesce_count;
4618 case CCISS_SETINTINFO:
4620 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4622 if ((cis->delay == 0) && (cis->count == 0)) {
4628 * XXX apparently this is only safe if the controller is idle,
4629 * we should suspend it before doing this.
4631 sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
4632 sc->ciss_cfg->interrupt_coalesce_count = cis->count;
4634 if (ciss_update_config(sc))
4637 /* XXX resume the controller here */
4641 case CCISS_GETNODENAME:
4642 bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
4643 sizeof(NodeName_type));
4646 case CCISS_SETNODENAME:
4647 bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
4648 sizeof(NodeName_type));
4649 if (ciss_update_config(sc))
4653 case CCISS_GETHEARTBEAT:
4654 *(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
4657 case CCISS_GETBUSTYPES:
4658 *(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
4661 case CCISS_GETFIRMVER:
4662 bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
4663 sizeof(FirmwareVer_type));
4666 case CCISS_GETDRIVERVER:
4667 *(DriverVer_type *)addr = CISS_DRIVER_VERSION;
4670 case CCISS_REVALIDVOLS:
4672 * This is a bit ugly; to do it "right" we really need
4673 * to find any disks that have changed, kick CAM off them,
4674 * then rescan only these disks. It'd be nice if they
4675 * a) told us which disk(s) they were going to play with,
4676 * and b) which ones had arrived. 8(
4681 case CCISS_PASSTHRU32:
4682 ioc_swab.LUN_info = ioc32->LUN_info;
4683 ioc_swab.Request = ioc32->Request;
4684 ioc_swab.error_info = ioc32->error_info;
4685 ioc_swab.buf_size = ioc32->buf_size;
4686 ioc_swab.buf = (u_int8_t *)(uintptr_t)ioc32->buf;
4691 case CCISS_PASSTHRU:
4692 error = ciss_user_command(sc, ioc);
4696 debug(0, "unknown ioctl 0x%lx", cmd);
4698 debug(1, "CCISS_GETPCIINFO: 0x%lx", CCISS_GETPCIINFO);
4699 debug(1, "CCISS_GETINTINFO: 0x%lx", CCISS_GETINTINFO);
4700 debug(1, "CCISS_SETINTINFO: 0x%lx", CCISS_SETINTINFO);
4701 debug(1, "CCISS_GETNODENAME: 0x%lx", CCISS_GETNODENAME);
4702 debug(1, "CCISS_SETNODENAME: 0x%lx", CCISS_SETNODENAME);
4703 debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
4704 debug(1, "CCISS_GETBUSTYPES: 0x%lx", CCISS_GETBUSTYPES);
4705 debug(1, "CCISS_GETFIRMVER: 0x%lx", CCISS_GETFIRMVER);
4706 debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
4707 debug(1, "CCISS_REVALIDVOLS: 0x%lx", CCISS_REVALIDVOLS);
4708 debug(1, "CCISS_PASSTHRU: 0x%lx", CCISS_PASSTHRU);
4714 mtx_unlock(&sc->ciss_mtx);