2 * Copyright (c) 2001 Michael Smith
3 * Copyright (c) 2004 Paul Saab
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Common Interface for SCSI-3 Support driver.
33 * CISS claims to provide a common interface between a generic SCSI
34 * transport and an intelligent host adapter.
36 * This driver supports CISS as defined in the document "CISS Command
37 * Interface for SCSI-3 Support Open Specification", Version 1.04,
38 * Valence Number 1, dated 20001127, produced by Compaq Computer
39 * Corporation. This document appears to be a hastily and somewhat
40 * arbitrarlily cut-down version of a larger (and probably even more
41 * chaotic and inconsistent) Compaq internal document. Various
42 * details were also gleaned from Compaq's "cciss" driver for Linux.
44 * We provide a shim layer between the CISS interface and CAM,
45 * offloading most of the queueing and being-a-disk chores onto CAM.
46 * Entry to the driver is via the PCI bus attachment (ciss_probe,
47 * ciss_attach, etc) and via the CAM interface (ciss_cam_action,
48 * ciss_cam_poll). The Compaq CISS adapters are, however, poor SCSI
49 * citizens and we have to fake up some responses to get reasonable
50 * behaviour out of them. In addition, the CISS command set is by no
51 * means adequate to support the functionality of a RAID controller,
52 * and thus the supported Compaq adapters utilise portions of the
53 * control protocol from earlier Compaq adapter families.
55 * Note that we only support the "simple" transport layer over PCI.
56 * This interface (ab)uses the I2O register set (specifically the post
57 * queues) to exchange commands with the adapter. Other interfaces
58 * are available, but we aren't supposed to know about them, and it is
59 * dubious whether they would provide major performance improvements
60 * except under extreme load.
62 * Currently the only supported CISS adapters are the Compaq Smart
63 * Array 5* series (5300, 5i, 532). Even with only three adapters,
64 * Compaq still manage to have interface variations.
67 * Thanks must go to Fred Harris and Darryl DeVinney at Compaq, as
68 * well as Paul Saab at Yahoo! for their assistance in making this
71 * More thanks must go to John Cagle at HP for the countless hours
72 * spent making this driver "work" with the MSA* series storage
73 * enclosures. Without his help (and nagging), this driver could not
74 * be used with these enclosures.
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/malloc.h>
80 #include <sys/kernel.h>
84 #include <sys/kthread.h>
85 #include <sys/queue.h>
86 #include <sys/sysctl.h>
89 #include <cam/cam_ccb.h>
90 #include <cam/cam_periph.h>
91 #include <cam/cam_sim.h>
92 #include <cam/cam_xpt_sim.h>
93 #include <cam/scsi/scsi_all.h>
94 #include <cam/scsi/scsi_message.h>
96 #include <machine/bus.h>
97 #include <machine/endian.h>
98 #include <machine/resource.h>
101 #include <dev/pci/pcireg.h>
102 #include <dev/pci/pcivar.h>
104 #include <dev/ciss/cissreg.h>
105 #include <dev/ciss/cissio.h>
106 #include <dev/ciss/cissvar.h>
108 static MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data",
109 "ciss internal data buffers");
112 static int ciss_lookup(device_t dev);
113 static int ciss_probe(device_t dev);
114 static int ciss_attach(device_t dev);
115 static int ciss_detach(device_t dev);
116 static int ciss_shutdown(device_t dev);
118 /* (de)initialisation functions, control wrappers */
119 static int ciss_init_pci(struct ciss_softc *sc);
120 static int ciss_setup_msix(struct ciss_softc *sc);
121 static int ciss_init_perf(struct ciss_softc *sc);
122 static int ciss_wait_adapter(struct ciss_softc *sc);
123 static int ciss_flush_adapter(struct ciss_softc *sc);
124 static int ciss_init_requests(struct ciss_softc *sc);
125 static void ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
126 int nseg, int error);
127 static int ciss_identify_adapter(struct ciss_softc *sc);
128 static int ciss_init_logical(struct ciss_softc *sc);
129 static int ciss_init_physical(struct ciss_softc *sc);
130 static int ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll);
131 static int ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
132 static int ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld);
133 static int ciss_update_config(struct ciss_softc *sc);
134 static int ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld);
135 static void ciss_init_sysctl(struct ciss_softc *sc);
136 static void ciss_soft_reset(struct ciss_softc *sc);
137 static void ciss_free(struct ciss_softc *sc);
138 static void ciss_spawn_notify_thread(struct ciss_softc *sc);
139 static void ciss_kill_notify_thread(struct ciss_softc *sc);
141 /* request submission/completion */
142 static int ciss_start(struct ciss_request *cr);
143 static void ciss_done(struct ciss_softc *sc, cr_qhead_t *qh);
144 static void ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh);
145 static void ciss_intr(void *arg);
146 static void ciss_perf_intr(void *arg);
147 static void ciss_perf_msi_intr(void *arg);
148 static void ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh);
149 static int _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func);
150 static int ciss_synch_request(struct ciss_request *cr, int timeout);
151 static int ciss_poll_request(struct ciss_request *cr, int timeout);
152 static int ciss_wait_request(struct ciss_request *cr, int timeout);
154 static int ciss_abort_request(struct ciss_request *cr);
157 /* request queueing */
158 static int ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
159 static void ciss_preen_command(struct ciss_request *cr);
160 static void ciss_release_request(struct ciss_request *cr);
162 /* request helpers */
163 static int ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
164 int opcode, void **bufp, size_t bufsize);
165 static int ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
168 static int ciss_map_request(struct ciss_request *cr);
169 static void ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
170 int nseg, int error);
171 static void ciss_unmap_request(struct ciss_request *cr);
174 static int ciss_cam_init(struct ciss_softc *sc);
175 static void ciss_cam_rescan_target(struct ciss_softc *sc,
176 int bus, int target);
177 static void ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
178 static int ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
179 static int ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
180 static void ciss_cam_poll(struct cam_sim *sim);
181 static void ciss_cam_complete(struct ciss_request *cr);
182 static void ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
183 static struct cam_periph *ciss_find_periph(struct ciss_softc *sc,
184 int bus, int target);
185 static int ciss_name_device(struct ciss_softc *sc, int bus, int target);
187 /* periodic status monitoring */
188 static void ciss_periodic(void *arg);
189 static void ciss_nop_complete(struct ciss_request *cr);
190 static void ciss_disable_adapter(struct ciss_softc *sc);
191 static void ciss_notify_event(struct ciss_softc *sc);
192 static void ciss_notify_complete(struct ciss_request *cr);
193 static int ciss_notify_abort(struct ciss_softc *sc);
194 static int ciss_notify_abort_bmic(struct ciss_softc *sc);
195 static void ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
196 static void ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
197 static void ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
199 /* debugging output */
200 static void ciss_print_request(struct ciss_request *cr);
201 static void ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
202 static const char *ciss_name_ldrive_status(int status);
203 static int ciss_decode_ldrive_status(int status);
204 static const char *ciss_name_ldrive_org(int org);
205 static const char *ciss_name_command_status(int status);
210 static device_method_t ciss_methods[] = {
211 /* Device interface */
212 DEVMETHOD(device_probe, ciss_probe),
213 DEVMETHOD(device_attach, ciss_attach),
214 DEVMETHOD(device_detach, ciss_detach),
215 DEVMETHOD(device_shutdown, ciss_shutdown),
219 static driver_t ciss_pci_driver = {
222 sizeof(struct ciss_softc)
225 static devclass_t ciss_devclass;
226 DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
227 MODULE_DEPEND(ciss, cam, 1, 1, 1);
228 MODULE_DEPEND(ciss, pci, 1, 1, 1);
231 * Control device interface.
233 static d_open_t ciss_open;
234 static d_close_t ciss_close;
235 static d_ioctl_t ciss_ioctl;
237 static struct cdevsw ciss_cdevsw = {
238 .d_version = D_VERSION,
241 .d_close = ciss_close,
242 .d_ioctl = ciss_ioctl,
247 * This tunable can be set at boot time and controls whether physical devices
248 * that are marked hidden by the firmware should be exposed anyways.
250 static unsigned int ciss_expose_hidden_physical = 0;
251 TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
253 static unsigned int ciss_nop_message_heartbeat = 0;
254 TUNABLE_INT("hw.ciss.nop_message_heartbeat", &ciss_nop_message_heartbeat);
257 * This tunable can force a particular transport to be used:
260 * 2 : force performant
262 static int ciss_force_transport = 0;
263 TUNABLE_INT("hw.ciss.force_transport", &ciss_force_transport);
266 * This tunable can force a particular interrupt delivery method to be used:
271 static int ciss_force_interrupt = 0;
272 TUNABLE_INT("hw.ciss.force_interrupt", &ciss_force_interrupt);
274 /************************************************************************
275 * CISS adapters amazingly don't have a defined programming interface
276 * value. (One could say some very despairing things about PCI and
277 * people just not getting the general idea.) So we are forced to
278 * stick with matching against subvendor/subdevice, and thus have to
279 * be updated for every new CISS adapter that appears.
281 #define CISS_BOARD_UNKNWON 0
282 #define CISS_BOARD_SA5 1
283 #define CISS_BOARD_SA5B 2
284 #define CISS_BOARD_NOMSI (1<<4)
292 } ciss_vendor_data[] = {
293 { 0x0e11, 0x4070, CISS_BOARD_SA5|CISS_BOARD_NOMSI, "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" },
342 /************************************************************************
343 * Find a match for the device in our list of known adapters.
346 ciss_lookup(device_t dev)
350 for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
351 if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
352 (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
358 /************************************************************************
359 * Match a known CISS adapter.
362 ciss_probe(device_t dev)
366 i = ciss_lookup(dev);
368 device_set_desc(dev, ciss_vendor_data[i].desc);
369 return(BUS_PROBE_DEFAULT);
374 /************************************************************************
375 * Attach the driver to this adapter.
378 ciss_attach(device_t dev)
380 struct ciss_softc *sc;
386 /* print structure/union sizes */
387 debug_struct(ciss_command);
388 debug_struct(ciss_header);
389 debug_union(ciss_device_address);
390 debug_struct(ciss_cdb);
391 debug_struct(ciss_report_cdb);
392 debug_struct(ciss_notify_cdb);
393 debug_struct(ciss_notify);
394 debug_struct(ciss_message_cdb);
395 debug_struct(ciss_error_info_pointer);
396 debug_struct(ciss_error_info);
397 debug_struct(ciss_sg_entry);
398 debug_struct(ciss_config_table);
399 debug_struct(ciss_bmic_cdb);
400 debug_struct(ciss_bmic_id_ldrive);
401 debug_struct(ciss_bmic_id_lstatus);
402 debug_struct(ciss_bmic_id_table);
403 debug_struct(ciss_bmic_id_pdrive);
404 debug_struct(ciss_bmic_blink_pdrive);
405 debug_struct(ciss_bmic_flush_cache);
406 debug_const(CISS_MAX_REQUESTS);
407 debug_const(CISS_MAX_LOGICAL);
408 debug_const(CISS_INTERRUPT_COALESCE_DELAY);
409 debug_const(CISS_INTERRUPT_COALESCE_COUNT);
410 debug_const(CISS_COMMAND_ALLOC_SIZE);
411 debug_const(CISS_COMMAND_SG_LENGTH);
413 debug_type(cciss_pci_info_struct);
414 debug_type(cciss_coalint_struct);
415 debug_type(cciss_coalint_struct);
416 debug_type(NodeName_type);
417 debug_type(NodeName_type);
418 debug_type(Heartbeat_type);
419 debug_type(BusTypes_type);
420 debug_type(FirmwareVer_type);
421 debug_type(DriverVer_type);
422 debug_type(IOCTL_Command_struct);
425 sc = device_get_softc(dev);
427 mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
428 callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);
431 * Do PCI-specific init.
433 if ((error = ciss_init_pci(sc)) != 0)
437 * Initialise driver queues.
440 ciss_initq_notify(sc);
443 * Initalize device sysctls.
445 ciss_init_sysctl(sc);
448 * Initialise command/request pool.
450 if ((error = ciss_init_requests(sc)) != 0)
454 * Get adapter information.
456 if ((error = ciss_identify_adapter(sc)) != 0)
460 * Find all the physical devices.
462 if ((error = ciss_init_physical(sc)) != 0)
466 * Build our private table of logical devices.
468 if ((error = ciss_init_logical(sc)) != 0)
472 * Enable interrupts so that the CAM scan can complete.
474 CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
477 * Initialise the CAM interface.
479 if ((error = ciss_cam_init(sc)) != 0)
483 * Start the heartbeat routine and event chain.
488 * Create the control device.
490 sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
491 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
492 "ciss%d", device_get_unit(sc->ciss_dev));
493 sc->ciss_dev_t->si_drv1 = sc;
496 * The adapter is running; synchronous commands can now sleep
497 * waiting for an interrupt to signal completion.
499 sc->ciss_flags |= CISS_FLAG_RUNNING;
501 ciss_spawn_notify_thread(sc);
506 /* ciss_free() expects the mutex to be held */
507 mtx_lock(&sc->ciss_mtx);
513 /************************************************************************
514 * Detach the driver from this adapter.
517 ciss_detach(device_t dev)
519 struct ciss_softc *sc = device_get_softc(dev);
523 mtx_lock(&sc->ciss_mtx);
524 if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
525 mtx_unlock(&sc->ciss_mtx);
529 /* flush adapter cache */
530 ciss_flush_adapter(sc);
532 /* release all resources. The mutex is released and freed here too. */
538 /************************************************************************
539 * Prepare adapter for system shutdown.
542 ciss_shutdown(device_t dev)
544 struct ciss_softc *sc = device_get_softc(dev);
548 mtx_lock(&sc->ciss_mtx);
549 /* flush adapter cache */
550 ciss_flush_adapter(sc);
552 if (sc->ciss_soft_reset)
554 mtx_unlock(&sc->ciss_mtx);
560 ciss_init_sysctl(struct ciss_softc *sc)
563 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
564 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
565 OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
568 /************************************************************************
569 * Perform PCI-specific attachment actions.
572 ciss_init_pci(struct ciss_softc *sc)
574 uintptr_t cbase, csize, cofs;
575 uint32_t method, supported_methods;
576 int error, sqmask, i;
582 * Work out adapter type.
584 i = ciss_lookup(sc->ciss_dev);
586 ciss_printf(sc, "unknown adapter type\n");
590 if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
591 sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
592 } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
593 sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
596 * XXX Big hammer, masks/unmasks all possible interrupts. This should
597 * work on all hardware variants. Need to add code to handle the
598 * "controller crashed" interupt bit that this unmasks.
604 * Allocate register window first (we need this to find the config
608 sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
609 if ((sc->ciss_regs_resource =
610 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
611 &sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
612 ciss_printf(sc, "can't allocate register window\n");
615 sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
616 sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
619 * Find the BAR holding the config structure. If it's not the one
620 * we already mapped for registers, map it too.
622 sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
623 if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
624 if ((sc->ciss_cfg_resource =
625 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
626 &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
627 ciss_printf(sc, "can't allocate config window\n");
630 cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
631 csize = rman_get_end(sc->ciss_cfg_resource) -
632 rman_get_start(sc->ciss_cfg_resource) + 1;
634 cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
635 csize = rman_get_end(sc->ciss_regs_resource) -
636 rman_get_start(sc->ciss_regs_resource) + 1;
638 cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
641 * Use the base/size/offset values we just calculated to
642 * sanity-check the config structure. If it's OK, point to it.
644 if ((cofs + sizeof(struct ciss_config_table)) > csize) {
645 ciss_printf(sc, "config table outside window\n");
648 sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
649 debug(1, "config struct at %p", sc->ciss_cfg);
652 * Calculate the number of request structures/commands we are
653 * going to provide for this adapter.
655 sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
658 * Validate the config structure. If we supported other transport
659 * methods, we could select amongst them at this point in time.
661 if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
662 ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
663 sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
664 sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
669 * Select the mode of operation, prefer Performant.
671 if (!(sc->ciss_cfg->supported_methods &
672 (CISS_TRANSPORT_METHOD_SIMPLE | CISS_TRANSPORT_METHOD_PERF))) {
673 ciss_printf(sc, "No supported transport layers: 0x%x\n",
674 sc->ciss_cfg->supported_methods);
677 switch (ciss_force_transport) {
679 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
682 supported_methods = CISS_TRANSPORT_METHOD_PERF;
685 supported_methods = sc->ciss_cfg->supported_methods;
690 if ((supported_methods & CISS_TRANSPORT_METHOD_PERF) != 0) {
691 method = CISS_TRANSPORT_METHOD_PERF;
692 sc->ciss_perf = (struct ciss_perf_config *)(cbase + cofs +
693 sc->ciss_cfg->transport_offset);
694 if (ciss_init_perf(sc)) {
695 supported_methods &= ~method;
698 } else if (supported_methods & CISS_TRANSPORT_METHOD_SIMPLE) {
699 method = CISS_TRANSPORT_METHOD_SIMPLE;
701 ciss_printf(sc, "No supported transport methods: 0x%x\n",
702 sc->ciss_cfg->supported_methods);
707 * Tell it we're using the low 4GB of RAM. Set the default interrupt
708 * coalescing options.
710 sc->ciss_cfg->requested_method = method;
711 sc->ciss_cfg->command_physlimit = 0;
712 sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
713 sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
716 sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
719 if (ciss_update_config(sc)) {
720 ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
721 CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
724 if ((sc->ciss_cfg->active_method & method) == 0) {
725 supported_methods &= ~method;
726 if (supported_methods == 0) {
727 ciss_printf(sc, "adapter refuses to go into available transports "
728 "mode (0x%x, 0x%x)\n", supported_methods,
729 sc->ciss_cfg->active_method);
736 * Wait for the adapter to come ready.
738 if ((error = ciss_wait_adapter(sc)) != 0)
741 /* Prepare to possibly use MSIX and/or PERFORMANT interrupts. Normal
742 * interrupts have a rid of 0, this will be overridden if MSIX is used.
744 sc->ciss_irq_rid[0] = 0;
745 if (method == CISS_TRANSPORT_METHOD_PERF) {
746 ciss_printf(sc, "PERFORMANT Transport\n");
747 if ((ciss_force_interrupt != 1) && (ciss_setup_msix(sc) == 0)) {
748 intr = ciss_perf_msi_intr;
750 intr = ciss_perf_intr;
752 /* XXX The docs say that the 0x01 bit is only for SAS controllers.
753 * Unfortunately, there is no good way to know if this is a SAS
754 * controller. Hopefully enabling this bit universally will work OK.
755 * It seems to work fine for SA6i controllers.
757 sc->ciss_interrupt_mask = CISS_TL_PERF_INTR_OPQ | CISS_TL_PERF_INTR_MSI;
760 ciss_printf(sc, "SIMPLE Transport\n");
761 /* MSIX doesn't seem to work in SIMPLE mode, only enable if it forced */
762 if (ciss_force_interrupt == 2)
763 /* If this fails, we automatically revert to INTx */
765 sc->ciss_perf = NULL;
767 sc->ciss_interrupt_mask = sqmask;
771 * Turn off interrupts before we go routing anything.
773 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
776 * Allocate and set up our interrupt.
778 if ((sc->ciss_irq_resource =
779 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid[0],
780 RF_ACTIVE | RF_SHAREABLE)) == NULL) {
781 ciss_printf(sc, "can't allocate interrupt\n");
785 if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
786 INTR_TYPE_CAM|INTR_MPSAFE, NULL, intr, sc,
788 ciss_printf(sc, "can't set up interrupt\n");
793 * Allocate the parent bus DMA tag appropriate for our PCI
796 * Note that "simple" adapters can only address within a 32-bit
799 if (bus_dma_tag_create(NULL, /* parent */
800 1, 0, /* alignment, boundary */
801 BUS_SPACE_MAXADDR, /* lowaddr */
802 BUS_SPACE_MAXADDR, /* highaddr */
803 NULL, NULL, /* filter, filterarg */
804 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
805 CISS_MAX_SG_ELEMENTS, /* nsegments */
806 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
808 NULL, NULL, /* lockfunc, lockarg */
809 &sc->ciss_parent_dmat)) {
810 ciss_printf(sc, "can't allocate parent DMA tag\n");
815 * Create DMA tag for mapping buffers into adapter-addressable
818 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
819 1, 0, /* alignment, boundary */
820 BUS_SPACE_MAXADDR, /* lowaddr */
821 BUS_SPACE_MAXADDR, /* highaddr */
822 NULL, NULL, /* filter, filterarg */
823 MAXBSIZE, CISS_MAX_SG_ELEMENTS, /* maxsize, nsegments */
824 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
825 BUS_DMA_ALLOCNOW, /* flags */
826 busdma_lock_mutex, &sc->ciss_mtx, /* lockfunc, lockarg */
827 &sc->ciss_buffer_dmat)) {
828 ciss_printf(sc, "can't allocate buffer DMA tag\n");
834 /************************************************************************
835 * Setup MSI/MSIX operation (Performant only)
836 * Four interrupts are available, but we only use 1 right now. If MSI-X
837 * isn't avaialble, try using MSI instead.
840 ciss_setup_msix(struct ciss_softc *sc)
844 /* Weed out devices that don't actually support MSI */
845 i = ciss_lookup(sc->ciss_dev);
846 if (ciss_vendor_data[i].flags & CISS_BOARD_NOMSI)
850 * Only need to use the minimum number of MSI vectors, as the driver
851 * doesn't support directed MSIX interrupts.
853 val = pci_msix_count(sc->ciss_dev);
854 if (val < CISS_MSI_COUNT) {
855 val = pci_msi_count(sc->ciss_dev);
856 device_printf(sc->ciss_dev, "got %d MSI messages]\n", val);
857 if (val < CISS_MSI_COUNT)
860 val = MIN(val, CISS_MSI_COUNT);
861 if (pci_alloc_msix(sc->ciss_dev, &val) != 0) {
862 if (pci_alloc_msi(sc->ciss_dev, &val) != 0)
868 ciss_printf(sc, "Using %d MSIX interrupt%s\n", val,
869 (val != 1) ? "s" : "");
871 for (i = 0; i < val; i++)
872 sc->ciss_irq_rid[i] = i + 1;
878 /************************************************************************
879 * Setup the Performant structures.
882 ciss_init_perf(struct ciss_softc *sc)
884 struct ciss_perf_config *pc = sc->ciss_perf;
888 * Create the DMA tag for the reply queue.
890 reply_size = sizeof(uint64_t) * sc->ciss_max_requests;
891 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
892 1, 0, /* alignment, boundary */
893 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
894 BUS_SPACE_MAXADDR, /* highaddr */
895 NULL, NULL, /* filter, filterarg */
896 reply_size, 1, /* maxsize, nsegments */
897 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
899 NULL, NULL, /* lockfunc, lockarg */
900 &sc->ciss_reply_dmat)) {
901 ciss_printf(sc, "can't allocate reply DMA tag\n");
905 * Allocate memory and make it available for DMA.
907 if (bus_dmamem_alloc(sc->ciss_reply_dmat, (void **)&sc->ciss_reply,
908 BUS_DMA_NOWAIT, &sc->ciss_reply_map)) {
909 ciss_printf(sc, "can't allocate reply memory\n");
912 bus_dmamap_load(sc->ciss_reply_dmat, sc->ciss_reply_map, sc->ciss_reply,
913 reply_size, ciss_command_map_helper, &sc->ciss_reply_phys, 0);
914 bzero(sc->ciss_reply, reply_size);
916 sc->ciss_cycle = 0x1;
920 * Preload the fetch table with common command sizes. This allows the
921 * hardware to not waste bus cycles for typical i/o commands, but also not
922 * tax the driver to be too exact in choosing sizes. The table is optimized
923 * for page-aligned i/o's, but since most i/o comes from the various pagers,
924 * it's a reasonable assumption to make.
926 pc->fetch_count[CISS_SG_FETCH_NONE] = (sizeof(struct ciss_command) + 15) / 16;
927 pc->fetch_count[CISS_SG_FETCH_1] =
928 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 1 + 15) / 16;
929 pc->fetch_count[CISS_SG_FETCH_2] =
930 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 2 + 15) / 16;
931 pc->fetch_count[CISS_SG_FETCH_4] =
932 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 4 + 15) / 16;
933 pc->fetch_count[CISS_SG_FETCH_8] =
934 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 8 + 15) / 16;
935 pc->fetch_count[CISS_SG_FETCH_16] =
936 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 16 + 15) / 16;
937 pc->fetch_count[CISS_SG_FETCH_32] =
938 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 32 + 15) / 16;
939 pc->fetch_count[CISS_SG_FETCH_MAX] = (CISS_COMMAND_ALLOC_SIZE + 15) / 16;
941 pc->rq_size = sc->ciss_max_requests; /* XXX less than the card supports? */
942 pc->rq_count = 1; /* XXX Hardcode for a single queue */
945 pc->rq[0].rq_addr_hi = 0x0;
946 pc->rq[0].rq_addr_lo = sc->ciss_reply_phys;
951 /************************************************************************
952 * Wait for the adapter to come ready.
955 ciss_wait_adapter(struct ciss_softc *sc)
962 * Wait for the adapter to come ready.
964 if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
965 ciss_printf(sc, "waiting for adapter to come ready...\n");
966 for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
967 DELAY(1000000); /* one second */
969 ciss_printf(sc, "timed out waiting for adapter to come ready\n");
977 /************************************************************************
978 * Flush the adapter cache.
981 ciss_flush_adapter(struct ciss_softc *sc)
983 struct ciss_request *cr;
984 struct ciss_bmic_flush_cache *cbfc;
985 int error, command_status;
993 * Build a BMIC request to flush the cache. We don't disable
994 * it, as we may be going to do more I/O (eg. we are emulating
995 * the Synchronise Cache command).
997 if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1001 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
1002 (void **)&cbfc, sizeof(*cbfc))) != 0)
1006 * Submit the request and wait for it to complete.
1008 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1009 ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
1016 ciss_report_request(cr, &command_status, NULL);
1017 switch(command_status) {
1018 case CISS_CMD_STATUS_SUCCESS:
1021 ciss_printf(sc, "error flushing cache (%s)\n",
1022 ciss_name_command_status(command_status));
1029 free(cbfc, CISS_MALLOC_CLASS);
1031 ciss_release_request(cr);
1036 ciss_soft_reset(struct ciss_softc *sc)
1038 struct ciss_request *cr = NULL;
1039 struct ciss_command *cc;
1042 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1043 /* only reset proxy controllers */
1044 if (sc->ciss_controllers[i].physical.bus == 0)
1047 if ((error = ciss_get_request(sc, &cr)) != 0)
1050 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
1055 cc->header.address = sc->ciss_controllers[i];
1057 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
1060 ciss_release_request(cr);
1064 ciss_printf(sc, "error resetting controller (%d)\n", error);
1067 ciss_release_request(cr);
1070 /************************************************************************
1071 * Allocate memory for the adapter command structures, initialise
1072 * the request structures.
1074 * Note that the entire set of commands are allocated in a single
1078 ciss_init_requests(struct ciss_softc *sc)
1080 struct ciss_request *cr;
1086 ciss_printf(sc, "using %d of %d available commands\n",
1087 sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
1090 * Create the DMA tag for commands.
1092 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
1093 32, 0, /* alignment, boundary */
1094 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1095 BUS_SPACE_MAXADDR, /* highaddr */
1096 NULL, NULL, /* filter, filterarg */
1097 CISS_COMMAND_ALLOC_SIZE *
1098 sc->ciss_max_requests, 1, /* maxsize, nsegments */
1099 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1101 NULL, NULL, /* lockfunc, lockarg */
1102 &sc->ciss_command_dmat)) {
1103 ciss_printf(sc, "can't allocate command DMA tag\n");
1107 * Allocate memory and make it available for DMA.
1109 if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
1110 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
1111 ciss_printf(sc, "can't allocate command memory\n");
1114 bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map,sc->ciss_command,
1115 CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
1116 ciss_command_map_helper, &sc->ciss_command_phys, 0);
1117 bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
1120 * Set up the request and command structures, push requests onto
1123 for (i = 1; i < sc->ciss_max_requests; i++) {
1124 cr = &sc->ciss_request[i];
1127 cr->cr_cc = (struct ciss_command *)((uintptr_t)sc->ciss_command +
1128 CISS_COMMAND_ALLOC_SIZE * i);
1129 cr->cr_ccphys = sc->ciss_command_phys + CISS_COMMAND_ALLOC_SIZE * i;
1130 bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
1131 ciss_enqueue_free(cr);
1137 ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1142 *addr = segs[0].ds_addr;
1145 /************************************************************************
1146 * Identify the adapter, print some information about it.
1149 ciss_identify_adapter(struct ciss_softc *sc)
1151 struct ciss_request *cr;
1152 int error, command_status;
1159 * Get a request, allocate storage for the adapter data.
1161 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
1162 (void **)&sc->ciss_id,
1163 sizeof(*sc->ciss_id))) != 0)
1167 * Submit the request and wait for it to complete.
1169 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1170 ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
1177 ciss_report_request(cr, &command_status, NULL);
1178 switch(command_status) {
1179 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1181 case CISS_CMD_STATUS_DATA_UNDERRUN:
1182 case CISS_CMD_STATUS_DATA_OVERRUN:
1183 ciss_printf(sc, "data over/underrun reading adapter information\n");
1185 ciss_printf(sc, "error reading adapter information (%s)\n",
1186 ciss_name_command_status(command_status));
1191 /* sanity-check reply */
1192 if (!sc->ciss_id->big_map_supported) {
1193 ciss_printf(sc, "adapter does not support BIG_MAP\n");
1199 /* XXX later revisions may not need this */
1200 sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
1203 /* XXX only really required for old 5300 adapters? */
1204 sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
1206 /* print information */
1208 #if 0 /* XXX proxy volumes??? */
1209 ciss_printf(sc, " %d logical drive%s configured\n",
1210 sc->ciss_id->configured_logical_drives,
1211 (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
1213 ciss_printf(sc, " firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
1214 ciss_printf(sc, " %d SCSI channels\n", sc->ciss_id->scsi_bus_count);
1216 ciss_printf(sc, " signature '%.4s'\n", sc->ciss_cfg->signature);
1217 ciss_printf(sc, " valence %d\n", sc->ciss_cfg->valence);
1218 ciss_printf(sc, " supported I/O methods 0x%b\n",
1219 sc->ciss_cfg->supported_methods,
1220 "\20\1READY\2simple\3performant\4MEMQ\n");
1221 ciss_printf(sc, " active I/O method 0x%b\n",
1222 sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
1223 ciss_printf(sc, " 4G page base 0x%08x\n",
1224 sc->ciss_cfg->command_physlimit);
1225 ciss_printf(sc, " interrupt coalesce delay %dus\n",
1226 sc->ciss_cfg->interrupt_coalesce_delay);
1227 ciss_printf(sc, " interrupt coalesce count %d\n",
1228 sc->ciss_cfg->interrupt_coalesce_count);
1229 ciss_printf(sc, " max outstanding commands %d\n",
1230 sc->ciss_cfg->max_outstanding_commands);
1231 ciss_printf(sc, " bus types 0x%b\n", sc->ciss_cfg->bus_types,
1232 "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
1233 ciss_printf(sc, " server name '%.16s'\n", sc->ciss_cfg->server_name);
1234 ciss_printf(sc, " heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
1239 if (sc->ciss_id != NULL) {
1240 free(sc->ciss_id, CISS_MALLOC_CLASS);
1245 ciss_release_request(cr);
1249 /************************************************************************
1250 * Helper routine for generating a list of logical and physical luns.
1252 static struct ciss_lun_report *
1253 ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
1255 struct ciss_request *cr;
1256 struct ciss_command *cc;
1257 struct ciss_report_cdb *crc;
1258 struct ciss_lun_report *cll;
1269 * Get a request, allocate storage for the address list.
1271 if ((error = ciss_get_request(sc, &cr)) != 0)
1273 report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
1274 if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1275 ciss_printf(sc, "can't allocate memory for lun report\n");
1281 * Build the Report Logical/Physical LUNs command.
1285 cr->cr_length = report_size;
1286 cr->cr_flags = CISS_REQ_DATAIN;
1288 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
1289 cc->header.address.physical.bus = 0;
1290 cc->header.address.physical.target = 0;
1291 cc->cdb.cdb_length = sizeof(*crc);
1292 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1293 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1294 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1295 cc->cdb.timeout = 30; /* XXX better suggestions? */
1297 crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
1298 bzero(crc, sizeof(*crc));
1299 crc->opcode = opcode;
1300 crc->length = htonl(report_size); /* big-endian field */
1301 cll->list_size = htonl(report_size - sizeof(*cll)); /* big-endian field */
1304 * Submit the request and wait for it to complete. (timeout
1305 * here should be much greater than above)
1307 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1308 ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
1313 * Check response. Note that data over/underrun is OK.
1315 ciss_report_request(cr, &command_status, NULL);
1316 switch(command_status) {
1317 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1318 case CISS_CMD_STATUS_DATA_UNDERRUN: /* buffer too large, not bad */
1320 case CISS_CMD_STATUS_DATA_OVERRUN:
1321 ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
1325 ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
1326 ciss_name_command_status(command_status));
1330 ciss_release_request(cr);
1335 ciss_release_request(cr);
1336 if (error && cll != NULL) {
1337 free(cll, CISS_MALLOC_CLASS);
1343 /************************************************************************
1344 * Find logical drives on the adapter.
1347 ciss_init_logical(struct ciss_softc *sc)
1349 struct ciss_lun_report *cll;
1350 int error = 0, i, j;
1355 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
1362 /* sanity-check reply */
1363 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1364 if ((ndrives < 0) || (ndrives > CISS_MAX_LOGICAL)) {
1365 ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
1366 ndrives, CISS_MAX_LOGICAL);
1372 * Save logical drive information.
1375 ciss_printf(sc, "%d logical drive%s\n",
1376 ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
1380 malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
1381 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1382 if (sc->ciss_logical == NULL) {
1387 for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1388 sc->ciss_logical[i] =
1389 malloc(CISS_MAX_LOGICAL * sizeof(struct ciss_ldrive),
1390 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1391 if (sc->ciss_logical[i] == NULL) {
1396 for (j = 0; j < CISS_MAX_LOGICAL; j++)
1397 sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
1401 for (i = 0; i < CISS_MAX_LOGICAL; i++) {
1403 struct ciss_ldrive *ld;
1406 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
1407 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
1408 ld = &sc->ciss_logical[bus][target];
1410 ld->cl_address = cll->lun[i];
1411 ld->cl_controller = &sc->ciss_controllers[bus];
1412 if (ciss_identify_logical(sc, ld) != 0)
1415 * If the drive has had media exchanged, we should bring it online.
1417 if (ld->cl_lstatus->media_exchanged)
1418 ciss_accept_media(sc, ld);
1425 free(cll, CISS_MALLOC_CLASS);
1430 ciss_init_physical(struct ciss_softc *sc)
1432 struct ciss_lun_report *cll;
1442 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
1449 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1452 ciss_printf(sc, "%d physical device%s\n",
1453 nphys, (nphys > 1 || nphys == 0) ? "s" : "");
1457 * Figure out the bus mapping.
1458 * Logical buses include both the local logical bus for local arrays and
1459 * proxy buses for remote arrays. Physical buses are numbered by the
1460 * controller and represent physical buses that hold physical devices.
1461 * We shift these bus numbers so that everything fits into a single flat
1462 * numbering space for CAM. Logical buses occupy the first 32 CAM bus
1463 * numbers, and the physical bus numbers are shifted to be above that.
1464 * This results in the various driver arrays being indexed as follows:
1466 * ciss_controllers[] - indexed by logical bus
1467 * ciss_cam_sim[] - indexed by both logical and physical, with physical
1468 * being shifted by 32.
1469 * ciss_logical[][] - indexed by logical bus
1470 * ciss_physical[][] - indexed by physical bus
1472 * XXX This is getting more and more hackish. CISS really doesn't play
1473 * well with a standard SCSI model; devices are addressed via magic
1474 * cookies, not via b/t/l addresses. Since there is no way to store
1475 * the cookie in the CAM device object, we have to keep these lookup
1476 * tables handy so that the devices can be found quickly at the cost
1477 * of wasting memory and having a convoluted lookup scheme. This
1478 * driver should probably be converted to block interface.
1481 * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
1482 * controller. A proxy controller is another physical controller
1483 * behind the primary PCI controller. We need to know about this
1484 * so that BMIC commands can be properly targeted. There can be
1485 * proxy controllers attached to a single PCI controller, so
1486 * find the highest numbered one so the array can be properly
1489 sc->ciss_max_logical_bus = 1;
1490 for (i = 0; i < nphys; i++) {
1491 if (cll->lun[i].physical.extra_address == 0) {
1492 bus = cll->lun[i].physical.bus;
1493 sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
1495 bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
1496 sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
1500 sc->ciss_controllers =
1501 malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
1502 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1504 if (sc->ciss_controllers == NULL) {
1505 ciss_printf(sc, "Could not allocate memory for controller map\n");
1510 /* setup a map of controller addresses */
1511 for (i = 0; i < nphys; i++) {
1512 if (cll->lun[i].physical.extra_address == 0) {
1513 sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
1518 malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
1519 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1520 if (sc->ciss_physical == NULL) {
1521 ciss_printf(sc, "Could not allocate memory for physical device map\n");
1526 for (i = 0; i < sc->ciss_max_physical_bus; i++) {
1527 sc->ciss_physical[i] =
1528 malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
1529 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1530 if (sc->ciss_physical[i] == NULL) {
1531 ciss_printf(sc, "Could not allocate memory for target map\n");
1537 ciss_filter_physical(sc, cll);
1541 free(cll, CISS_MALLOC_CLASS);
1547 ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
1553 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1554 for (i = 0; i < nphys; i++) {
1555 if (cll->lun[i].physical.extra_address == 0)
1559 * Filter out devices that we don't want. Level 3 LUNs could
1560 * probably be supported, but the docs don't give enough of a
1563 * The mode field of the physical address is likely set to have
1564 * hard disks masked out. Honor it unless the user has overridden
1565 * us with the tunable. We also munge the inquiry data for these
1566 * disks so that they only show up as passthrough devices. Keeping
1567 * them visible in this fashion is useful for doing things like
1568 * flashing firmware.
1570 ea = cll->lun[i].physical.extra_address;
1571 if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
1572 (CISS_EXTRA_MODE2(ea) == 0x3))
1574 if ((ciss_expose_hidden_physical == 0) &&
1575 (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
1579 * Note: CISS firmware numbers physical busses starting at '1', not
1580 * '0'. This numbering is internal to the firmware and is only
1581 * used as a hint here.
1583 bus = CISS_EXTRA_BUS2(ea) - 1;
1584 target = CISS_EXTRA_TARGET2(ea);
1585 sc->ciss_physical[bus][target].cp_address = cll->lun[i];
1586 sc->ciss_physical[bus][target].cp_online = 1;
1593 ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1595 struct ciss_request *cr;
1596 struct ciss_command *cc;
1597 struct scsi_inquiry *inq;
1603 bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1605 if ((error = ciss_get_request(sc, &cr)) != 0)
1609 cr->cr_data = &ld->cl_geometry;
1610 cr->cr_length = sizeof(ld->cl_geometry);
1611 cr->cr_flags = CISS_REQ_DATAIN;
1613 cc->header.address = ld->cl_address;
1614 cc->cdb.cdb_length = 6;
1615 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1616 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1617 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1618 cc->cdb.timeout = 30;
1620 inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1621 inq->opcode = INQUIRY;
1622 inq->byte2 = SI_EVPD;
1623 inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1624 scsi_ulto2b(sizeof(ld->cl_geometry), inq->length);
1626 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1627 ciss_printf(sc, "error getting geometry (%d)\n", error);
1631 ciss_report_request(cr, &command_status, NULL);
1632 switch(command_status) {
1633 case CISS_CMD_STATUS_SUCCESS:
1634 case CISS_CMD_STATUS_DATA_UNDERRUN:
1636 case CISS_CMD_STATUS_DATA_OVERRUN:
1637 ciss_printf(sc, "WARNING: Data overrun\n");
1640 ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1641 ciss_name_command_status(command_status));
1647 ciss_release_request(cr);
1650 /************************************************************************
1651 * Identify a logical drive, initialise state related to it.
1654 ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1656 struct ciss_request *cr;
1657 struct ciss_command *cc;
1658 struct ciss_bmic_cdb *cbc;
1659 int error, command_status;
1666 * Build a BMIC request to fetch the drive ID.
1668 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
1669 (void **)&ld->cl_ldrive,
1670 sizeof(*ld->cl_ldrive))) != 0)
1673 cc->header.address = *ld->cl_controller; /* target controller */
1674 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1675 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1678 * Submit the request and wait for it to complete.
1680 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1681 ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
1688 ciss_report_request(cr, &command_status, NULL);
1689 switch(command_status) {
1690 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1692 case CISS_CMD_STATUS_DATA_UNDERRUN:
1693 case CISS_CMD_STATUS_DATA_OVERRUN:
1694 ciss_printf(sc, "data over/underrun reading logical drive ID\n");
1696 ciss_printf(sc, "error reading logical drive ID (%s)\n",
1697 ciss_name_command_status(command_status));
1701 ciss_release_request(cr);
1705 * Build a CISS BMIC command to get the logical drive status.
1707 if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
1711 * Get the logical drive geometry.
1713 if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1717 * Print the drive's basic characteristics.
1720 ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
1721 CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
1722 CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
1723 ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
1724 ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
1725 ld->cl_ldrive->block_size));
1727 ciss_print_ldrive(sc, ld);
1731 /* make the drive not-exist */
1732 ld->cl_status = CISS_LD_NONEXISTENT;
1733 if (ld->cl_ldrive != NULL) {
1734 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
1735 ld->cl_ldrive = NULL;
1737 if (ld->cl_lstatus != NULL) {
1738 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
1739 ld->cl_lstatus = NULL;
1743 ciss_release_request(cr);
1748 /************************************************************************
1749 * Get status for a logical drive.
1751 * XXX should we also do this in response to Test Unit Ready?
1754 ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld)
1756 struct ciss_request *cr;
1757 struct ciss_command *cc;
1758 struct ciss_bmic_cdb *cbc;
1759 int error, command_status;
1762 * Build a CISS BMIC command to get the logical drive status.
1764 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
1765 (void **)&ld->cl_lstatus,
1766 sizeof(*ld->cl_lstatus))) != 0)
1769 cc->header.address = *ld->cl_controller; /* target controller */
1770 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1771 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1774 * Submit the request and wait for it to complete.
1776 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1777 ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1784 ciss_report_request(cr, &command_status, NULL);
1785 switch(command_status) {
1786 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1788 case CISS_CMD_STATUS_DATA_UNDERRUN:
1789 case CISS_CMD_STATUS_DATA_OVERRUN:
1790 ciss_printf(sc, "data over/underrun reading logical drive status\n");
1792 ciss_printf(sc, "error reading logical drive status (%s)\n",
1793 ciss_name_command_status(command_status));
1799 * Set the drive's summary status based on the returned status.
1801 * XXX testing shows that a failed JBOD drive comes back at next
1802 * boot in "queued for expansion" mode. WTF?
1804 ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
1808 ciss_release_request(cr);
1812 /************************************************************************
1813 * Notify the adapter of a config update.
1816 ciss_update_config(struct ciss_softc *sc)
1822 CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
1823 for (i = 0; i < 1000; i++) {
1824 if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
1825 CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
1833 /************************************************************************
1834 * Accept new media into a logical drive.
1836 * XXX The drive has previously been offline; it would be good if we
1837 * could make sure it's not open right now.
1840 ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
1842 struct ciss_request *cr;
1843 struct ciss_command *cc;
1844 struct ciss_bmic_cdb *cbc;
1846 int error = 0, ldrive;
1848 ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1850 debug(0, "bringing logical drive %d back online");
1853 * Build a CISS BMIC command to bring the drive back online.
1855 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
1859 cc->header.address = *ld->cl_controller; /* target controller */
1860 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1861 cbc->log_drive = ldrive;
1864 * Submit the request and wait for it to complete.
1866 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1867 ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
1874 ciss_report_request(cr, &command_status, NULL);
1875 switch(command_status) {
1876 case CISS_CMD_STATUS_SUCCESS: /* all OK */
1877 /* we should get a logical drive status changed event here */
1880 ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
1881 ciss_name_command_status(command_status));
1887 ciss_release_request(cr);
1891 /************************************************************************
1892 * Release adapter resources.
1895 ciss_free(struct ciss_softc *sc)
1897 struct ciss_request *cr;
1902 /* we're going away */
1903 sc->ciss_flags |= CISS_FLAG_ABORTING;
1905 /* terminate the periodic heartbeat routine */
1906 callout_stop(&sc->ciss_periodic);
1908 /* cancel the Event Notify chain */
1909 ciss_notify_abort(sc);
1911 ciss_kill_notify_thread(sc);
1913 /* disconnect from CAM */
1914 if (sc->ciss_cam_sim) {
1915 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1916 if (sc->ciss_cam_sim[i]) {
1917 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1918 cam_sim_free(sc->ciss_cam_sim[i], 0);
1921 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
1922 CISS_PHYSICAL_BASE; i++) {
1923 if (sc->ciss_cam_sim[i]) {
1924 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1925 cam_sim_free(sc->ciss_cam_sim[i], 0);
1928 free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
1930 if (sc->ciss_cam_devq)
1931 cam_simq_free(sc->ciss_cam_devq);
1933 /* remove the control device */
1934 mtx_unlock(&sc->ciss_mtx);
1935 if (sc->ciss_dev_t != NULL)
1936 destroy_dev(sc->ciss_dev_t);
1938 /* Final cleanup of the callout. */
1939 callout_drain(&sc->ciss_periodic);
1940 mtx_destroy(&sc->ciss_mtx);
1942 /* free the controller data */
1943 if (sc->ciss_id != NULL)
1944 free(sc->ciss_id, CISS_MALLOC_CLASS);
1946 /* release I/O resources */
1947 if (sc->ciss_regs_resource != NULL)
1948 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1949 sc->ciss_regs_rid, sc->ciss_regs_resource);
1950 if (sc->ciss_cfg_resource != NULL)
1951 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1952 sc->ciss_cfg_rid, sc->ciss_cfg_resource);
1953 if (sc->ciss_intr != NULL)
1954 bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
1955 if (sc->ciss_irq_resource != NULL)
1956 bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
1957 sc->ciss_irq_rid[0], sc->ciss_irq_resource);
1959 pci_release_msi(sc->ciss_dev);
1961 while ((cr = ciss_dequeue_free(sc)) != NULL)
1962 bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
1963 if (sc->ciss_buffer_dmat)
1964 bus_dma_tag_destroy(sc->ciss_buffer_dmat);
1966 /* destroy command memory and DMA tag */
1967 if (sc->ciss_command != NULL) {
1968 bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
1969 bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
1971 if (sc->ciss_command_dmat)
1972 bus_dma_tag_destroy(sc->ciss_command_dmat);
1974 if (sc->ciss_reply) {
1975 bus_dmamap_unload(sc->ciss_reply_dmat, sc->ciss_reply_map);
1976 bus_dmamem_free(sc->ciss_reply_dmat, sc->ciss_reply, sc->ciss_reply_map);
1978 if (sc->ciss_reply_dmat)
1979 bus_dma_tag_destroy(sc->ciss_reply_dmat);
1981 /* destroy DMA tags */
1982 if (sc->ciss_parent_dmat)
1983 bus_dma_tag_destroy(sc->ciss_parent_dmat);
1984 if (sc->ciss_logical) {
1985 for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1986 for (j = 0; j < CISS_MAX_LOGICAL; j++) {
1987 if (sc->ciss_logical[i][j].cl_ldrive)
1988 free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
1989 if (sc->ciss_logical[i][j].cl_lstatus)
1990 free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
1992 free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
1994 free(sc->ciss_logical, CISS_MALLOC_CLASS);
1997 if (sc->ciss_physical) {
1998 for (i = 0; i < sc->ciss_max_physical_bus; i++)
1999 free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
2000 free(sc->ciss_physical, CISS_MALLOC_CLASS);
2003 if (sc->ciss_controllers)
2004 free(sc->ciss_controllers, CISS_MALLOC_CLASS);
2008 /************************************************************************
2009 * Give a command to the adapter.
2011 * Note that this uses the simple transport layer directly. If we
2012 * want to add support for other layers, we'll need a switch of some
2015 * Note that the simple transport layer has no way of refusing a
2016 * command; we only have as many request structures as the adapter
2017 * supports commands, so we don't have to check (this presumes that
2018 * the adapter can handle commands as fast as we throw them at it).
2021 ciss_start(struct ciss_request *cr)
2023 struct ciss_command *cc; /* XXX debugging only */
2027 debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
2030 * Map the request's data.
2032 if ((error = ciss_map_request(cr)))
2036 ciss_print_request(cr);
2042 /************************************************************************
2043 * Fetch completed request(s) from the adapter, queue them for
2044 * completion handling.
2046 * Note that this uses the simple transport layer directly. If we
2047 * want to add support for other layers, we'll need a switch of some
2050 * Note that the simple transport mechanism does not require any
2051 * reentrancy protection; the OPQ read is atomic. If there is a
2052 * chance of a race with something else that might move the request
2053 * off the busy list, then we will have to lock against that
2054 * (eg. timeouts, etc.)
2057 ciss_done(struct ciss_softc *sc, cr_qhead_t *qh)
2059 struct ciss_request *cr;
2060 struct ciss_command *cc;
2061 u_int32_t tag, index;
2066 * Loop quickly taking requests from the adapter and moving them
2067 * to the completed queue.
2071 tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
2072 if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
2075 debug(2, "completed command %d%s", index,
2076 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2077 if (index >= sc->ciss_max_requests) {
2078 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2081 cr = &(sc->ciss_request[index]);
2083 cc->header.host_tag = tag; /* not updated by adapter */
2084 ciss_enqueue_complete(cr, qh);
2090 ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh)
2092 struct ciss_request *cr;
2093 struct ciss_command *cc;
2094 u_int32_t tag, index;
2099 * Loop quickly taking requests from the adapter and moving them
2100 * to the completed queue.
2103 tag = sc->ciss_reply[sc->ciss_rqidx];
2104 if ((tag & CISS_CYCLE_MASK) != sc->ciss_cycle)
2107 debug(2, "completed command %d%s\n", index,
2108 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2109 if (index < sc->ciss_max_requests) {
2110 cr = &(sc->ciss_request[index]);
2112 cc->header.host_tag = tag; /* not updated by adapter */
2113 ciss_enqueue_complete(cr, qh);
2115 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2117 if (++sc->ciss_rqidx == sc->ciss_max_requests) {
2119 sc->ciss_cycle ^= 1;
2125 /************************************************************************
2126 * Take an interrupt from the adapter.
2129 ciss_intr(void *arg)
2132 struct ciss_softc *sc = (struct ciss_softc *)arg;
2135 * The only interrupt we recognise indicates that there are
2136 * entries in the outbound post queue.
2140 mtx_lock(&sc->ciss_mtx);
2141 ciss_complete(sc, &qh);
2142 mtx_unlock(&sc->ciss_mtx);
2146 ciss_perf_intr(void *arg)
2148 struct ciss_softc *sc = (struct ciss_softc *)arg;
2150 /* Clear the interrupt and flush the bridges. Docs say that the flush
2151 * needs to be done twice, which doesn't seem right.
2153 CISS_TL_PERF_CLEAR_INT(sc);
2154 CISS_TL_PERF_FLUSH_INT(sc);
2156 ciss_perf_msi_intr(sc);
2160 ciss_perf_msi_intr(void *arg)
2163 struct ciss_softc *sc = (struct ciss_softc *)arg;
2166 ciss_perf_done(sc, &qh);
2167 mtx_lock(&sc->ciss_mtx);
2168 ciss_complete(sc, &qh);
2169 mtx_unlock(&sc->ciss_mtx);
2173 /************************************************************************
2174 * Process completed requests.
2176 * Requests can be completed in three fashions:
2178 * - by invoking a callback function (cr_complete is non-null)
2179 * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
2180 * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
2183 ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh)
2185 struct ciss_request *cr;
2190 * Loop taking requests off the completed queue and performing
2191 * completion processing on them.
2194 if ((cr = ciss_dequeue_complete(sc, qh)) == NULL)
2196 ciss_unmap_request(cr);
2198 if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
2199 ciss_printf(sc, "WARNING: completing non-busy request\n");
2200 cr->cr_flags &= ~CISS_REQ_BUSY;
2203 * If the request has a callback, invoke it.
2205 if (cr->cr_complete != NULL) {
2206 cr->cr_complete(cr);
2211 * If someone is sleeping on this request, wake them up.
2213 if (cr->cr_flags & CISS_REQ_SLEEP) {
2214 cr->cr_flags &= ~CISS_REQ_SLEEP;
2220 * If someone is polling this request for completion, signal.
2222 if (cr->cr_flags & CISS_REQ_POLL) {
2223 cr->cr_flags &= ~CISS_REQ_POLL;
2228 * Give up and throw the request back on the free queue. This
2229 * should never happen; resources will probably be lost.
2231 ciss_printf(sc, "WARNING: completed command with no submitter\n");
2232 ciss_enqueue_free(cr);
2236 /************************************************************************
2237 * Report on the completion status of a request, and pass back SCSI
2238 * and command status values.
2241 _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func)
2243 struct ciss_command *cc;
2244 struct ciss_error_info *ce;
2249 ce = (struct ciss_error_info *)&(cc->sg[0]);
2252 * We don't consider data under/overrun an error for the Report
2253 * Logical/Physical LUNs commands.
2255 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
2256 ((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
2257 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
2258 ((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
2259 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
2260 (cc->cdb.cdb[0] == INQUIRY))) {
2261 cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
2262 debug(2, "ignoring irrelevant under/overrun error");
2266 * Check the command's error bit, if clear, there's no status and
2269 if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
2270 if (scsi_status != NULL)
2271 *scsi_status = SCSI_STATUS_OK;
2272 if (command_status != NULL)
2273 *command_status = CISS_CMD_STATUS_SUCCESS;
2276 if (command_status != NULL)
2277 *command_status = ce->command_status;
2278 if (scsi_status != NULL) {
2279 if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
2280 *scsi_status = ce->scsi_status;
2286 ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
2287 ce->command_status, ciss_name_command_status(ce->command_status),
2289 if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
2290 ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x, function %s\n",
2291 ce->additional_error_info.invalid_command.offense_size,
2292 ce->additional_error_info.invalid_command.offense_offset,
2293 ce->additional_error_info.invalid_command.offense_value,
2298 ciss_print_request(cr);
2303 /************************************************************************
2304 * Issue a request and don't return until it's completed.
2306 * Depending on adapter status, we may poll or sleep waiting for
2310 ciss_synch_request(struct ciss_request *cr, int timeout)
2312 if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
2313 return(ciss_wait_request(cr, timeout));
2315 return(ciss_poll_request(cr, timeout));
2319 /************************************************************************
2320 * Issue a request and poll for completion.
2322 * Timeout in milliseconds.
2325 ciss_poll_request(struct ciss_request *cr, int timeout)
2328 struct ciss_softc *sc;
2335 cr->cr_flags |= CISS_REQ_POLL;
2336 if ((error = ciss_start(cr)) != 0)
2341 ciss_perf_done(sc, &qh);
2344 ciss_complete(sc, &qh);
2345 if (!(cr->cr_flags & CISS_REQ_POLL))
2348 } while (timeout-- >= 0);
2349 return(EWOULDBLOCK);
2352 /************************************************************************
2353 * Issue a request and sleep waiting for completion.
2355 * Timeout in milliseconds. Note that a spurious wakeup will reset
2359 ciss_wait_request(struct ciss_request *cr, int timeout)
2365 cr->cr_flags |= CISS_REQ_SLEEP;
2366 if ((error = ciss_start(cr)) != 0)
2369 while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
2370 error = msleep(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ", (timeout * hz) / 1000);
2376 /************************************************************************
2377 * Abort a request. Note that a potential exists here to race the
2378 * request being completed; the caller must deal with this.
2381 ciss_abort_request(struct ciss_request *ar)
2383 struct ciss_request *cr;
2384 struct ciss_command *cc;
2385 struct ciss_message_cdb *cmc;
2391 if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
2394 /* build the abort command */
2396 cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL; /* addressing? */
2397 cc->header.address.physical.target = 0;
2398 cc->header.address.physical.bus = 0;
2399 cc->cdb.cdb_length = sizeof(*cmc);
2400 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
2401 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2402 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2403 cc->cdb.timeout = 30;
2405 cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
2406 cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
2407 cmc->type = CISS_MESSAGE_ABORT_TASK;
2408 cmc->abort_tag = ar->cr_tag; /* endianness?? */
2411 * Send the request and wait for a response. If we believe we
2412 * aborted the request OK, clear the flag that indicates it's
2415 error = ciss_synch_request(cr, 35 * 1000);
2417 error = ciss_report_request(cr, NULL, NULL);
2418 ciss_release_request(cr);
2425 /************************************************************************
2426 * Fetch and initialise a request
2429 ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
2431 struct ciss_request *cr;
2436 * Get a request and clean it up.
2438 if ((cr = ciss_dequeue_free(sc)) == NULL)
2443 cr->cr_complete = NULL;
2444 cr->cr_private = NULL;
2445 cr->cr_sg_tag = CISS_SG_MAX; /* Backstop to prevent accidents */
2447 ciss_preen_command(cr);
2453 ciss_preen_command(struct ciss_request *cr)
2455 struct ciss_command *cc;
2459 * Clean up the command structure.
2461 * Note that we set up the error_info structure here, since the
2462 * length can be overwritten by any command.
2465 cc->header.sg_in_list = 0; /* kinda inefficient this way */
2466 cc->header.sg_total = 0;
2467 cc->header.host_tag = cr->cr_tag << 2;
2468 cc->header.host_tag_zeroes = 0;
2469 cmdphys = cr->cr_ccphys;
2470 cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
2471 cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
2474 /************************************************************************
2475 * Release a request to the free list.
2478 ciss_release_request(struct ciss_request *cr)
2480 struct ciss_softc *sc;
2486 /* release the request to the free queue */
2487 ciss_requeue_free(cr);
2490 /************************************************************************
2491 * Allocate a request that will be used to send a BMIC command. Do some
2492 * of the common setup here to avoid duplicating it everywhere else.
2495 ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
2496 int opcode, void **bufp, size_t bufsize)
2498 struct ciss_request *cr;
2499 struct ciss_command *cc;
2500 struct ciss_bmic_cdb *cbc;
2513 if ((error = ciss_get_request(sc, &cr)) != 0)
2517 * Allocate data storage if requested, determine the data direction.
2520 if ((bufsize > 0) && (bufp != NULL)) {
2521 if (*bufp == NULL) {
2522 if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
2528 dataout = 1; /* we are given a buffer, so we are writing */
2533 * Build a CISS BMIC command to get the logical drive ID.
2536 cr->cr_length = bufsize;
2538 cr->cr_flags = CISS_REQ_DATAIN;
2541 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2542 cc->header.address.physical.bus = 0;
2543 cc->header.address.physical.target = 0;
2544 cc->cdb.cdb_length = sizeof(*cbc);
2545 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2546 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2547 cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
2548 cc->cdb.timeout = 0;
2550 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
2551 bzero(cbc, sizeof(*cbc));
2552 cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
2553 cbc->bmic_opcode = opcode;
2554 cbc->size = htons((u_int16_t)bufsize);
2559 ciss_release_request(cr);
2562 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
2568 /************************************************************************
2569 * Handle a command passed in from userspace.
2572 ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
2574 struct ciss_request *cr;
2575 struct ciss_command *cc;
2576 struct ciss_error_info *ce;
2586 while (ciss_get_request(sc, &cr) != 0)
2587 msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
2591 * Allocate an in-kernel databuffer if required, copy in user data.
2593 mtx_unlock(&sc->ciss_mtx);
2594 cr->cr_length = ioc->buf_size;
2595 if (ioc->buf_size > 0) {
2596 if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
2600 if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
2601 debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2607 * Build the request based on the user command.
2609 bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
2610 bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
2612 /* XXX anything else to populate here? */
2613 mtx_lock(&sc->ciss_mtx);
2618 if ((error = ciss_synch_request(cr, 60 * 1000))) {
2619 debug(0, "request failed - %d", error);
2624 * Check to see if the command succeeded.
2626 ce = (struct ciss_error_info *)&(cc->sg[0]);
2627 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
2628 bzero(ce, sizeof(*ce));
2631 * Copy the results back to the user.
2633 bcopy(ce, &ioc->error_info, sizeof(*ce));
2634 mtx_unlock(&sc->ciss_mtx);
2635 if ((ioc->buf_size > 0) &&
2636 (error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
2637 debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2645 mtx_lock(&sc->ciss_mtx);
2648 if ((cr != NULL) && (cr->cr_data != NULL))
2649 free(cr->cr_data, CISS_MALLOC_CLASS);
2651 ciss_release_request(cr);
2655 /************************************************************************
2656 * Map a request into bus-visible space, initialise the scatter/gather
2660 ciss_map_request(struct ciss_request *cr)
2662 struct ciss_softc *sc;
2669 /* check that mapping is necessary */
2670 if (cr->cr_flags & CISS_REQ_MAPPED)
2673 cr->cr_flags |= CISS_REQ_MAPPED;
2675 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2676 BUS_DMASYNC_PREWRITE);
2678 if (cr->cr_data != NULL) {
2679 error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2680 cr->cr_data, cr->cr_length,
2681 ciss_request_map_helper, cr, 0);
2686 * Post the command to the adapter.
2688 cr->cr_sg_tag = CISS_SG_NONE;
2689 cr->cr_flags |= CISS_REQ_BUSY;
2691 CISS_TL_PERF_POST_CMD(sc, cr);
2693 CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2700 ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2702 struct ciss_command *cc;
2703 struct ciss_request *cr;
2704 struct ciss_softc *sc;
2709 cr = (struct ciss_request *)arg;
2713 for (i = 0; i < nseg; i++) {
2714 cc->sg[i].address = segs[i].ds_addr;
2715 cc->sg[i].length = segs[i].ds_len;
2716 cc->sg[i].extension = 0;
2718 /* we leave the s/g table entirely within the command */
2719 cc->header.sg_in_list = nseg;
2720 cc->header.sg_total = nseg;
2722 if (cr->cr_flags & CISS_REQ_DATAIN)
2723 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2724 if (cr->cr_flags & CISS_REQ_DATAOUT)
2725 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2728 cr->cr_sg_tag = CISS_SG_NONE;
2730 cr->cr_sg_tag = CISS_SG_1;
2732 cr->cr_sg_tag = CISS_SG_2;
2734 cr->cr_sg_tag = CISS_SG_4;
2736 cr->cr_sg_tag = CISS_SG_8;
2737 else if (nseg <= 16)
2738 cr->cr_sg_tag = CISS_SG_16;
2739 else if (nseg <= 32)
2740 cr->cr_sg_tag = CISS_SG_32;
2742 cr->cr_sg_tag = CISS_SG_MAX;
2745 * Post the command to the adapter.
2747 cr->cr_flags |= CISS_REQ_BUSY;
2749 CISS_TL_PERF_POST_CMD(sc, cr);
2751 CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2754 /************************************************************************
2755 * Unmap a request from bus-visible space.
2758 ciss_unmap_request(struct ciss_request *cr)
2760 struct ciss_softc *sc;
2766 /* check that unmapping is necessary */
2767 if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
2770 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2771 BUS_DMASYNC_POSTWRITE);
2773 if (cr->cr_data == NULL)
2776 if (cr->cr_flags & CISS_REQ_DATAIN)
2777 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
2778 if (cr->cr_flags & CISS_REQ_DATAOUT)
2779 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
2781 bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2783 cr->cr_flags &= ~CISS_REQ_MAPPED;
2786 /************************************************************************
2787 * Attach the driver to CAM.
2789 * We put all the logical drives on a single SCSI bus.
2792 ciss_cam_init(struct ciss_softc *sc)
2799 * Allocate a devq. We can reuse this for the masked physical
2800 * devices if we decide to export these as well.
2802 if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests - 2)) == NULL) {
2803 ciss_printf(sc, "can't allocate CAM SIM queue\n");
2810 * This naturally wastes a bit of memory. The alternative is to allocate
2811 * and register each bus as it is found, and then track them on a linked
2812 * list. Unfortunately, the driver has a few places where it needs to
2813 * look up the SIM based solely on bus number, and it's unclear whether
2814 * a list traversal would work for these situations.
2816 maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
2817 CISS_PHYSICAL_BASE);
2818 sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
2819 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
2820 if (sc->ciss_cam_sim == NULL) {
2821 ciss_printf(sc, "can't allocate memory for controller SIM\n");
2825 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2826 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2828 device_get_unit(sc->ciss_dev),
2831 sc->ciss_max_requests - 2,
2832 sc->ciss_cam_devq)) == NULL) {
2833 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2838 * Register bus with this SIM.
2840 mtx_lock(&sc->ciss_mtx);
2841 if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) {
2842 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2843 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2844 mtx_unlock(&sc->ciss_mtx);
2848 mtx_unlock(&sc->ciss_mtx);
2851 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
2852 CISS_PHYSICAL_BASE; i++) {
2853 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2855 device_get_unit(sc->ciss_dev),
2857 sc->ciss_max_requests - 2,
2858 sc->ciss_cam_devq)) == NULL) {
2859 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2863 mtx_lock(&sc->ciss_mtx);
2864 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2865 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2866 mtx_unlock(&sc->ciss_mtx);
2869 mtx_unlock(&sc->ciss_mtx);
2875 /************************************************************************
2876 * Initiate a rescan of the 'logical devices' SIM
2879 ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
2885 if ((ccb = xpt_alloc_ccb_nowait()) == NULL) {
2886 ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
2890 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
2891 cam_sim_path(sc->ciss_cam_sim[bus]),
2892 target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2893 ciss_printf(sc, "rescan failed (can't create path)\n");
2898 /* scan is now in progress */
2901 /************************************************************************
2902 * Handle requests coming from CAM
2905 ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
2907 struct ciss_softc *sc;
2908 struct ccb_scsiio *csio;
2912 sc = cam_sim_softc(sim);
2913 bus = cam_sim_bus(sim);
2914 csio = (struct ccb_scsiio *)&ccb->csio;
2915 target = csio->ccb_h.target_id;
2916 physical = CISS_IS_PHYSICAL(bus);
2918 switch (ccb->ccb_h.func_code) {
2920 /* perform SCSI I/O */
2922 if (!ciss_cam_action_io(sim, csio))
2926 /* perform geometry calculations */
2927 case XPT_CALC_GEOMETRY:
2929 struct ccb_calc_geometry *ccg = &ccb->ccg;
2930 struct ciss_ldrive *ld;
2932 debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2936 ld = &sc->ciss_logical[bus][target];
2939 * Use the cached geometry settings unless the fault tolerance
2942 if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
2943 u_int32_t secs_per_cylinder;
2946 ccg->secs_per_track = 32;
2947 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2948 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2950 ccg->heads = ld->cl_geometry.heads;
2951 ccg->secs_per_track = ld->cl_geometry.sectors;
2952 ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2954 ccb->ccb_h.status = CAM_REQ_CMP;
2958 /* handle path attribute inquiry */
2961 struct ccb_pathinq *cpi = &ccb->cpi;
2963 debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2965 cpi->version_num = 1;
2966 cpi->hba_inquiry = PI_TAG_ABLE; /* XXX is this correct? */
2967 cpi->target_sprt = 0;
2969 cpi->max_target = CISS_MAX_LOGICAL;
2970 cpi->max_lun = 0; /* 'logical drive' channel only */
2971 cpi->initiator_id = CISS_MAX_LOGICAL;
2972 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2973 strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
2974 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2975 cpi->unit_number = cam_sim_unit(sim);
2976 cpi->bus_id = cam_sim_bus(sim);
2977 cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */
2978 cpi->transport = XPORT_SPI;
2979 cpi->transport_version = 2;
2980 cpi->protocol = PROTO_SCSI;
2981 cpi->protocol_version = SCSI_REV_2;
2982 cpi->maxio = (CISS_MAX_SG_ELEMENTS - 1) * PAGE_SIZE;
2983 ccb->ccb_h.status = CAM_REQ_CMP;
2987 case XPT_GET_TRAN_SETTINGS:
2989 struct ccb_trans_settings *cts = &ccb->cts;
2991 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
2992 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
2994 bus = cam_sim_bus(sim);
2995 target = cts->ccb_h.target_id;
2997 debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
2998 /* disconnect always OK */
2999 cts->protocol = PROTO_SCSI;
3000 cts->protocol_version = SCSI_REV_2;
3001 cts->transport = XPORT_SPI;
3002 cts->transport_version = 2;
3004 spi->valid = CTS_SPI_VALID_DISC;
3005 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
3007 scsi->valid = CTS_SCSI_VALID_TQ;
3008 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3010 cts->ccb_h.status = CAM_REQ_CMP;
3014 default: /* we can't do this */
3015 debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
3016 ccb->ccb_h.status = CAM_REQ_INVALID;
3023 /************************************************************************
3024 * Handle a CAM SCSI I/O request.
3027 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
3029 struct ciss_softc *sc;
3031 struct ciss_request *cr;
3032 struct ciss_command *cc;
3035 sc = cam_sim_softc(sim);
3036 bus = cam_sim_bus(sim);
3037 target = csio->ccb_h.target_id;
3039 debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
3041 /* check that the CDB pointer is not to a physical address */
3042 if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
3043 debug(3, " CDB pointer is to physical address");
3044 csio->ccb_h.status = CAM_REQ_CMP_ERR;
3047 /* if there is data transfer, it must be to/from a virtual address */
3048 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
3049 if (csio->ccb_h.flags & CAM_DATA_PHYS) { /* we can't map it */
3050 debug(3, " data pointer is to physical address");
3051 csio->ccb_h.status = CAM_REQ_CMP_ERR;
3053 if (csio->ccb_h.flags & CAM_SCATTER_VALID) { /* we want to do the s/g setup */
3054 debug(3, " data has premature s/g setup");
3055 csio->ccb_h.status = CAM_REQ_CMP_ERR;
3059 /* abandon aborted ccbs or those that have failed validation */
3060 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
3061 debug(3, "abandoning CCB due to abort/validation failure");
3065 /* handle emulation of some SCSI commands ourself */
3066 if (ciss_cam_emulate(sc, csio))
3070 * Get a request to manage this command. If we can't, return the
3071 * ccb, freeze the queue and flag so that we unfreeze it when a
3072 * request completes.
3074 if ((error = ciss_get_request(sc, &cr)) != 0) {
3075 xpt_freeze_simq(sim, 1);
3076 sc->ciss_flags |= CISS_FLAG_BUSY;
3077 csio->ccb_h.status |= CAM_REQUEUE_REQ;
3082 * Build the command.
3085 cr->cr_data = csio->data_ptr;
3086 cr->cr_length = csio->dxfer_len;
3087 cr->cr_complete = ciss_cam_complete;
3088 cr->cr_private = csio;
3091 * Target the right logical volume.
3093 if (CISS_IS_PHYSICAL(bus))
3094 cc->header.address =
3095 sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
3097 cc->header.address =
3098 sc->ciss_logical[bus][target].cl_address;
3099 cc->cdb.cdb_length = csio->cdb_len;
3100 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3101 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; /* XXX ordered tags? */
3102 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
3103 cr->cr_flags = CISS_REQ_DATAOUT;
3104 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3105 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
3106 cr->cr_flags = CISS_REQ_DATAIN;
3107 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3110 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
3112 cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
3113 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
3114 bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
3116 bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
3120 * Submit the request to the adapter.
3122 * Note that this may fail if we're unable to map the request (and
3123 * if we ever learn a transport layer other than simple, may fail
3124 * if the adapter rejects the command).
3126 if ((error = ciss_start(cr)) != 0) {
3127 xpt_freeze_simq(sim, 1);
3128 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3129 if (error == EINPROGRESS) {
3132 csio->ccb_h.status |= CAM_REQUEUE_REQ;
3133 ciss_release_request(cr);
3141 /************************************************************************
3142 * Emulate SCSI commands the adapter doesn't handle as we might like.
3145 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
3150 target = csio->ccb_h.target_id;
3151 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3152 opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3153 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
3155 if (CISS_IS_PHYSICAL(bus)) {
3156 if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
3157 csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3158 xpt_done((union ccb *)csio);
3165 * Handle requests for volumes that don't exist or are not online.
3166 * A selection timeout is slightly better than an illegal request.
3167 * Other errors might be better.
3169 if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
3170 csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3171 xpt_done((union ccb *)csio);
3175 /* if we have to fake Synchronise Cache */
3176 if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
3178 * If this is a Synchronise Cache command, typically issued when
3179 * a device is closed, flush the adapter and complete now.
3181 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
3182 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
3183 ciss_flush_adapter(sc);
3184 csio->ccb_h.status |= CAM_REQ_CMP;
3185 xpt_done((union ccb *)csio);
3193 /************************************************************************
3194 * Check for possibly-completed commands.
3197 ciss_cam_poll(struct cam_sim *sim)
3200 struct ciss_softc *sc = cam_sim_softc(sim);
3206 ciss_perf_done(sc, &qh);
3209 ciss_complete(sc, &qh);
3212 /************************************************************************
3213 * Handle completion of a command - pass results back through the CCB
3216 ciss_cam_complete(struct ciss_request *cr)
3218 struct ciss_softc *sc;
3219 struct ciss_command *cc;
3220 struct ciss_error_info *ce;
3221 struct ccb_scsiio *csio;
3229 ce = (struct ciss_error_info *)&(cc->sg[0]);
3230 csio = (struct ccb_scsiio *)cr->cr_private;
3233 * Extract status values from request.
3235 ciss_report_request(cr, &command_status, &scsi_status);
3236 csio->scsi_status = scsi_status;
3239 * Handle specific SCSI status values.
3241 switch(scsi_status) {
3242 /* no status due to adapter error */
3244 debug(0, "adapter error");
3245 csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3248 /* no status due to command completed OK */
3249 case SCSI_STATUS_OK: /* CISS_SCSI_STATUS_GOOD */
3250 debug(2, "SCSI_STATUS_OK");
3251 csio->ccb_h.status |= CAM_REQ_CMP;
3254 /* check condition, sense data included */
3255 case SCSI_STATUS_CHECK_COND: /* CISS_SCSI_STATUS_CHECK_CONDITION */
3256 debug(0, "SCSI_STATUS_CHECK_COND sense size %d resid %d\n",
3257 ce->sense_length, ce->residual_count);
3258 bzero(&csio->sense_data, SSD_FULL_SIZE);
3259 bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
3260 if (csio->sense_len > ce->sense_length)
3261 csio->sense_resid = csio->sense_len - ce->sense_length;
3263 csio->sense_resid = 0;
3264 csio->resid = ce->residual_count;
3265 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
3268 struct scsi_sense_data *sns = (struct scsi_sense_data *)&ce->sense_info[0];
3269 debug(0, "sense key %x", scsi_get_sense_key(sns, csio->sense_len -
3270 csio->sense_resid, /*show_errors*/ 1));
3275 case SCSI_STATUS_BUSY: /* CISS_SCSI_STATUS_BUSY */
3276 debug(0, "SCSI_STATUS_BUSY");
3277 csio->ccb_h.status |= CAM_SCSI_BUSY;
3281 debug(0, "unknown status 0x%x", csio->scsi_status);
3282 csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3286 /* handle post-command fixup */
3287 ciss_cam_complete_fixup(sc, csio);
3289 ciss_release_request(cr);
3290 if (sc->ciss_flags & CISS_FLAG_BUSY) {
3291 sc->ciss_flags &= ~CISS_FLAG_BUSY;
3292 if (csio->ccb_h.status & CAM_RELEASE_SIMQ)
3293 xpt_release_simq(xpt_path_sim(csio->ccb_h.path), 0);
3295 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3297 xpt_done((union ccb *)csio);
3300 /********************************************************************************
3301 * Fix up the result of some commands here.
3304 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
3306 struct scsi_inquiry_data *inq;
3307 struct ciss_ldrive *cl;
3311 cdb = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3312 (uint8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes;
3313 if (cdb[0] == INQUIRY &&
3314 (cdb[1] & SI_EVPD) == 0 &&
3315 (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
3316 csio->dxfer_len >= SHORT_INQUIRY_LENGTH) {
3318 inq = (struct scsi_inquiry_data *)csio->data_ptr;
3319 target = csio->ccb_h.target_id;
3320 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3323 * Don't let hard drives be seen by the DA driver. They will still be
3324 * attached by the PASS driver.
3326 if (CISS_IS_PHYSICAL(bus)) {
3327 if (SID_TYPE(inq) == T_DIRECT)
3328 inq->device = (inq->device & 0xe0) | T_NODEVICE;
3332 cl = &sc->ciss_logical[bus][target];
3334 padstr(inq->vendor, "COMPAQ", 8);
3335 padstr(inq->product, ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance), 8);
3336 padstr(inq->revision, ciss_name_ldrive_status(cl->cl_lstatus->status), 16);
3341 /********************************************************************************
3342 * Find a peripheral attached at (target)
3344 static struct cam_periph *
3345 ciss_find_periph(struct ciss_softc *sc, int bus, int target)
3347 struct cam_periph *periph;
3348 struct cam_path *path;
3351 status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
3353 if (status == CAM_REQ_CMP) {
3354 periph = cam_periph_find(path, NULL);
3355 xpt_free_path(path);
3362 /********************************************************************************
3363 * Name the device at (target)
3365 * XXX is this strictly correct?
3368 ciss_name_device(struct ciss_softc *sc, int bus, int target)
3370 struct cam_periph *periph;
3372 if (CISS_IS_PHYSICAL(bus))
3374 if ((periph = ciss_find_periph(sc, bus, target)) != NULL) {
3375 sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
3376 periph->periph_name, periph->unit_number);
3379 sc->ciss_logical[bus][target].cl_name[0] = 0;
3383 /************************************************************************
3384 * Periodic status monitoring.
3387 ciss_periodic(void *arg)
3389 struct ciss_softc *sc;
3390 struct ciss_request *cr = NULL;
3391 struct ciss_command *cc = NULL;
3396 sc = (struct ciss_softc *)arg;
3399 * Check the adapter heartbeat.
3401 if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
3402 sc->ciss_heart_attack++;
3403 debug(0, "adapter heart attack in progress 0x%x/%d",
3404 sc->ciss_heartbeat, sc->ciss_heart_attack);
3405 if (sc->ciss_heart_attack == 3) {
3406 ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
3407 ciss_disable_adapter(sc);
3411 sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
3412 sc->ciss_heart_attack = 0;
3413 debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
3417 * Send the NOP message and wait for a response.
3419 if (ciss_nop_message_heartbeat != 0 && (error = ciss_get_request(sc, &cr)) == 0) {
3421 cr->cr_complete = ciss_nop_complete;
3422 cc->cdb.cdb_length = 1;
3423 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
3424 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3425 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3426 cc->cdb.timeout = 0;
3427 cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
3429 if ((error = ciss_start(cr)) != 0) {
3430 ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3435 * If the notify event request has died for some reason, or has
3436 * not started yet, restart it.
3438 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
3439 debug(0, "(re)starting Event Notify chain");
3440 ciss_notify_event(sc);
3446 callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
3450 ciss_nop_complete(struct ciss_request *cr)
3452 struct ciss_softc *sc;
3453 static int first_time = 1;
3456 if (ciss_report_request(cr, NULL, NULL) != 0) {
3457 if (first_time == 1) {
3459 ciss_printf(sc, "SENDING NOP MESSAGE FAILED (not logging anymore)\n");
3463 ciss_release_request(cr);
3466 /************************************************************************
3467 * Disable the adapter.
3469 * The all requests in completed queue is failed with hardware error.
3470 * This will cause failover in a multipath configuration.
3473 ciss_disable_adapter(struct ciss_softc *sc)
3476 struct ciss_request *cr;
3477 struct ciss_command *cc;
3478 struct ciss_error_info *ce;
3481 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
3482 pci_disable_busmaster(sc->ciss_dev);
3483 sc->ciss_flags &= ~CISS_FLAG_RUNNING;
3485 for (i = 1; i < sc->ciss_max_requests; i++) {
3486 cr = &sc->ciss_request[i];
3487 if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
3491 ce = (struct ciss_error_info *)&(cc->sg[0]);
3492 ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
3493 ciss_enqueue_complete(cr, &qh);
3497 if ((cr = ciss_dequeue_complete(sc, &qh)) == NULL)
3501 * If the request has a callback, invoke it.
3503 if (cr->cr_complete != NULL) {
3504 cr->cr_complete(cr);
3509 * If someone is sleeping on this request, wake them up.
3511 if (cr->cr_flags & CISS_REQ_SLEEP) {
3512 cr->cr_flags &= ~CISS_REQ_SLEEP;
3519 /************************************************************************
3520 * Request a notification response from the adapter.
3522 * If (cr) is NULL, this is the first request of the adapter, so
3523 * reset the adapter's message pointer and start with the oldest
3524 * message available.
3527 ciss_notify_event(struct ciss_softc *sc)
3529 struct ciss_request *cr;
3530 struct ciss_command *cc;
3531 struct ciss_notify_cdb *cnc;
3536 cr = sc->ciss_periodic_notify;
3538 /* get a request if we don't already have one */
3540 if ((error = ciss_get_request(sc, &cr)) != 0) {
3541 debug(0, "can't get notify event request");
3544 sc->ciss_periodic_notify = cr;
3545 cr->cr_complete = ciss_notify_complete;
3546 debug(1, "acquired request %d", cr->cr_tag);
3550 * Get a databuffer if we don't already have one, note that the
3551 * adapter command wants a larger buffer than the actual
3554 if (cr->cr_data == NULL) {
3555 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3556 debug(0, "can't get notify event request buffer");
3560 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3563 /* re-setup the request's command (since we never release it) XXX overkill*/
3564 ciss_preen_command(cr);
3566 /* (re)build the notify event command */
3568 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3569 cc->header.address.physical.bus = 0;
3570 cc->header.address.physical.target = 0;
3572 cc->cdb.cdb_length = sizeof(*cnc);
3573 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3574 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3575 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3576 cc->cdb.timeout = 0; /* no timeout, we hope */
3578 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3579 bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
3580 cnc->opcode = CISS_OPCODE_READ;
3581 cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
3582 cnc->timeout = 0; /* no timeout, we hope */
3583 cnc->synchronous = 0;
3585 cnc->seek_to_oldest = 0;
3586 if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
3590 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3592 /* submit the request */
3593 error = ciss_start(cr);
3598 if (cr->cr_data != NULL)
3599 free(cr->cr_data, CISS_MALLOC_CLASS);
3600 ciss_release_request(cr);
3602 sc->ciss_periodic_notify = NULL;
3603 debug(0, "can't submit notify event request");
3604 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3606 debug(1, "notify event submitted");
3607 sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
3612 ciss_notify_complete(struct ciss_request *cr)
3614 struct ciss_command *cc;
3615 struct ciss_notify *cn;
3616 struct ciss_softc *sc;
3622 cn = (struct ciss_notify *)cr->cr_data;
3626 * Report request results, decode status.
3628 ciss_report_request(cr, &command_status, &scsi_status);
3631 * Abort the chain on a fatal error.
3633 * XXX which of these are actually errors?
3635 if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
3636 (command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
3637 (command_status != CISS_CMD_STATUS_TIMEOUT)) { /* XXX timeout? */
3638 ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
3639 ciss_name_command_status(command_status));
3640 ciss_release_request(cr);
3641 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3646 * If the adapter gave us a text message, print it.
3648 if (cn->message[0] != 0)
3649 ciss_printf(sc, "*** %.80s\n", cn->message);
3651 debug(0, "notify event class %d subclass %d detail %d",
3652 cn->class, cn->subclass, cn->detail);
3655 * If the response indicates that the notifier has been aborted,
3656 * release the notifier command.
3658 if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
3659 (cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
3660 (cn->detail == 1)) {
3661 debug(0, "notifier exiting");
3662 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3663 ciss_release_request(cr);
3664 sc->ciss_periodic_notify = NULL;
3665 wakeup(&sc->ciss_periodic_notify);
3667 /* Handle notify events in a kernel thread */
3668 ciss_enqueue_notify(cr);
3669 sc->ciss_periodic_notify = NULL;
3670 wakeup(&sc->ciss_periodic_notify);
3671 wakeup(&sc->ciss_notify);
3674 * Send a new notify event command, if we're not aborting.
3676 if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
3677 ciss_notify_event(sc);
3681 /************************************************************************
3682 * Abort the Notify Event chain.
3684 * Note that we can't just abort the command in progress; we have to
3685 * explicitly issue an Abort Notify Event command in order for the
3686 * adapter to clean up correctly.
3688 * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
3689 * the chain will not restart itself.
3692 ciss_notify_abort(struct ciss_softc *sc)
3694 struct ciss_request *cr;
3695 struct ciss_command *cc;
3696 struct ciss_notify_cdb *cnc;
3697 int error, command_status, scsi_status;
3704 /* verify that there's an outstanding command */
3705 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3708 /* get a command to issue the abort with */
3709 if ((error = ciss_get_request(sc, &cr)))
3712 /* get a buffer for the result */
3713 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3714 debug(0, "can't get notify event request buffer");
3718 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3722 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3723 cc->header.address.physical.bus = 0;
3724 cc->header.address.physical.target = 0;
3725 cc->cdb.cdb_length = sizeof(*cnc);
3726 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3727 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3728 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3729 cc->cdb.timeout = 0; /* no timeout, we hope */
3731 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3732 bzero(cnc, sizeof(*cnc));
3733 cnc->opcode = CISS_OPCODE_WRITE;
3734 cnc->command = CISS_COMMAND_ABORT_NOTIFY;
3735 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3737 ciss_print_request(cr);
3740 * Submit the request and wait for it to complete.
3742 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3743 ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
3750 ciss_report_request(cr, &command_status, &scsi_status);
3751 switch(command_status) {
3752 case CISS_CMD_STATUS_SUCCESS:
3754 case CISS_CMD_STATUS_INVALID_COMMAND:
3756 * Some older adapters don't support the CISS version of this
3757 * command. Fall back to using the BMIC version.
3759 error = ciss_notify_abort_bmic(sc);
3764 case CISS_CMD_STATUS_TARGET_STATUS:
3766 * This can happen if the adapter thinks there wasn't an outstanding
3767 * Notify Event command but we did. We clean up here.
3769 if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
3770 if (sc->ciss_periodic_notify != NULL)
3771 ciss_release_request(sc->ciss_periodic_notify);
3778 ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
3779 ciss_name_command_status(command_status));
3785 * Sleep waiting for the notifier command to complete. Note
3786 * that if it doesn't, we may end up in a bad situation, since
3787 * the adapter may deliver it later. Also note that the adapter
3788 * requires the Notify Event command to be cancelled in order to
3789 * maintain internal bookkeeping.
3791 while (sc->ciss_periodic_notify != NULL) {
3792 error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
3793 if (error == EWOULDBLOCK) {
3794 ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
3800 /* release the cancel request */
3802 if (cr->cr_data != NULL)
3803 free(cr->cr_data, CISS_MALLOC_CLASS);
3804 ciss_release_request(cr);
3807 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3811 /************************************************************************
3812 * Abort the Notify Event chain using a BMIC command.
3815 ciss_notify_abort_bmic(struct ciss_softc *sc)
3817 struct ciss_request *cr;
3818 int error, command_status;
3825 /* verify that there's an outstanding command */
3826 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3830 * Build a BMIC command to cancel the Notify on Event command.
3832 * Note that we are sending a CISS opcode here. Odd.
3834 if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
3839 * Submit the request and wait for it to complete.
3841 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3842 ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
3849 ciss_report_request(cr, &command_status, NULL);
3850 switch(command_status) {
3851 case CISS_CMD_STATUS_SUCCESS:
3854 ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
3855 ciss_name_command_status(command_status));
3862 ciss_release_request(cr);
3866 /************************************************************************
3867 * Handle rescanning all the logical volumes when a notify event
3868 * causes the drives to come online or offline.
3871 ciss_notify_rescan_logical(struct ciss_softc *sc)
3873 struct ciss_lun_report *cll;
3874 struct ciss_ldrive *ld;
3878 * We must rescan all logical volumes to get the right logical
3881 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
3886 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
3889 * Delete any of the drives which were destroyed by the
3892 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
3893 for (j = 0; j < CISS_MAX_LOGICAL; j++) {
3894 ld = &sc->ciss_logical[i][j];
3896 if (ld->cl_update == 0)
3899 if (ld->cl_status != CISS_LD_ONLINE) {
3900 ciss_cam_rescan_target(sc, i, j);
3903 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
3905 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
3907 ld->cl_ldrive = NULL;
3908 ld->cl_lstatus = NULL;
3914 * Scan for new drives.
3916 for (i = 0; i < ndrives; i++) {
3919 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
3920 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
3921 ld = &sc->ciss_logical[bus][target];
3923 if (ld->cl_update == 0)
3927 ld->cl_address = cll->lun[i];
3928 ld->cl_controller = &sc->ciss_controllers[bus];
3929 if (ciss_identify_logical(sc, ld) == 0) {
3930 ciss_cam_rescan_target(sc, bus, target);
3933 free(cll, CISS_MALLOC_CLASS);
3936 /************************************************************************
3937 * Handle a notify event relating to the status of a logical drive.
3939 * XXX need to be able to defer some of these to properly handle
3940 * calling the "ID Physical drive" command, unless the 'extended'
3941 * drive IDs are always in BIG_MAP format.
3944 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
3946 struct ciss_ldrive *ld;
3947 int ostatus, bus, target;
3951 bus = cn->device.physical.bus;
3952 target = cn->data.logical_status.logical_drive;
3953 ld = &sc->ciss_logical[bus][target];
3955 switch (cn->subclass) {
3956 case CISS_NOTIFY_LOGICAL_STATUS:
3957 switch (cn->detail) {
3959 ciss_name_device(sc, bus, target);
3960 ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
3961 cn->data.logical_status.logical_drive, ld->cl_name,
3962 ciss_name_ldrive_status(cn->data.logical_status.previous_state),
3963 ciss_name_ldrive_status(cn->data.logical_status.new_state),
3964 cn->data.logical_status.spare_state,
3965 "\20\1configured\2rebuilding\3failed\4in use\5available\n");
3968 * Update our idea of the drive's status.
3970 ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
3971 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3972 if (ld->cl_lstatus != NULL)
3973 ld->cl_lstatus->status = cn->data.logical_status.new_state;
3976 * Have CAM rescan the drive if its status has changed.
3978 if (ostatus != ld->cl_status) {
3980 ciss_notify_rescan_logical(sc);
3985 case 1: /* logical drive has recognised new media, needs Accept Media Exchange */
3986 ciss_name_device(sc, bus, target);
3987 ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
3988 cn->data.logical_status.logical_drive, ld->cl_name);
3989 ciss_accept_media(sc, ld);
3992 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3993 ciss_notify_rescan_logical(sc);
3998 ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
3999 cn->data.rebuild_aborted.logical_drive,
4001 (cn->detail == 2) ? "read" : "write");
4006 case CISS_NOTIFY_LOGICAL_ERROR:
4007 if (cn->detail == 0) {
4008 ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
4009 cn->data.io_error.logical_drive,
4011 cn->data.io_error.failure_bus,
4012 cn->data.io_error.failure_drive);
4013 /* XXX should we take the drive down at this point, or will we be told? */
4017 case CISS_NOTIFY_LOGICAL_SURFACE:
4018 if (cn->detail == 0)
4019 ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
4020 cn->data.consistency_completed.logical_drive,
4026 /************************************************************************
4027 * Handle a notify event relating to the status of a physical drive.
4030 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
4034 /************************************************************************
4035 * Handle a notify event relating to the status of a physical drive.
4038 ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
4040 struct ciss_lun_report *cll = NULL;
4043 switch (cn->subclass) {
4044 case CISS_NOTIFY_HOTPLUG_PHYSICAL:
4045 case CISS_NOTIFY_HOTPLUG_NONDISK:
4046 bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
4048 CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
4050 if (cn->detail == 0) {
4052 * Mark the device offline so that it'll start producing selection
4053 * timeouts to the upper layer.
4055 if ((bus >= 0) && (target >= 0))
4056 sc->ciss_physical[bus][target].cp_online = 0;
4059 * Rescan the physical lun list for new items
4061 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
4064 ciss_printf(sc, "Warning, cannot get physical lun list\n");
4067 ciss_filter_physical(sc, cll);
4072 ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
4077 free(cll, CISS_MALLOC_CLASS);
4080 /************************************************************************
4081 * Handle deferred processing of notify events. Notify events may need
4082 * sleep which is unsafe during an interrupt.
4085 ciss_notify_thread(void *arg)
4087 struct ciss_softc *sc;
4088 struct ciss_request *cr;
4089 struct ciss_notify *cn;
4091 sc = (struct ciss_softc *)arg;
4092 #if __FreeBSD_version >= 500000
4093 mtx_lock(&sc->ciss_mtx);
4097 if (STAILQ_EMPTY(&sc->ciss_notify) != 0 &&
4098 (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
4099 msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
4102 if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
4105 cr = ciss_dequeue_notify(sc);
4109 cn = (struct ciss_notify *)cr->cr_data;
4111 switch (cn->class) {
4112 case CISS_NOTIFY_HOTPLUG:
4113 ciss_notify_hotplug(sc, cn);
4115 case CISS_NOTIFY_LOGICAL:
4116 ciss_notify_logical(sc, cn);
4118 case CISS_NOTIFY_PHYSICAL:
4119 ciss_notify_physical(sc, cn);
4123 ciss_release_request(cr);
4126 sc->ciss_notify_thread = NULL;
4127 wakeup(&sc->ciss_notify_thread);
4129 #if __FreeBSD_version >= 500000
4130 mtx_unlock(&sc->ciss_mtx);
4135 /************************************************************************
4136 * Start the notification kernel thread.
4139 ciss_spawn_notify_thread(struct ciss_softc *sc)
4142 #if __FreeBSD_version > 500005
4143 if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4144 &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
4145 device_get_unit(sc->ciss_dev)))
4147 if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4148 &sc->ciss_notify_thread, "ciss_notify%d",
4149 device_get_unit(sc->ciss_dev)))
4151 panic("Could not create notify thread\n");
4154 /************************************************************************
4155 * Kill the notification kernel thread.
4158 ciss_kill_notify_thread(struct ciss_softc *sc)
4161 if (sc->ciss_notify_thread == NULL)
4164 sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
4165 wakeup(&sc->ciss_notify);
4166 msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
4169 /************************************************************************
4173 ciss_print_request(struct ciss_request *cr)
4175 struct ciss_softc *sc;
4176 struct ciss_command *cc;
4182 ciss_printf(sc, "REQUEST @ %p\n", cr);
4183 ciss_printf(sc, " data %p/%d tag %d flags %b\n",
4184 cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
4185 "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
4186 ciss_printf(sc, " sg list/total %d/%d host tag 0x%x\n",
4187 cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
4188 switch(cc->header.address.mode.mode) {
4189 case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
4190 case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
4191 ciss_printf(sc, " physical bus %d target %d\n",
4192 cc->header.address.physical.bus, cc->header.address.physical.target);
4194 case CISS_HDR_ADDRESS_MODE_LOGICAL:
4195 ciss_printf(sc, " logical unit %d\n", cc->header.address.logical.lun);
4198 ciss_printf(sc, " %s cdb length %d type %s attribute %s\n",
4199 (cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
4200 (cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
4201 (cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
4203 (cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
4204 (cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
4205 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
4206 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
4207 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
4208 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
4209 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
4210 ciss_printf(sc, " %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
4212 if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
4213 /* XXX print error info */
4215 /* since we don't use chained s/g, don't support it here */
4216 for (i = 0; i < cc->header.sg_in_list; i++) {
4218 ciss_printf(sc, " ");
4219 printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
4220 if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
4226 /************************************************************************
4227 * Print information about the status of a logical drive.
4230 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
4234 if (ld->cl_lstatus == NULL) {
4235 printf("does not exist\n");
4239 /* print drive status */
4240 switch(ld->cl_lstatus->status) {
4241 case CISS_LSTATUS_OK:
4244 case CISS_LSTATUS_INTERIM_RECOVERY:
4245 printf("in interim recovery mode\n");
4247 case CISS_LSTATUS_READY_RECOVERY:
4248 printf("ready to begin recovery\n");
4250 case CISS_LSTATUS_RECOVERING:
4251 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4252 target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4253 printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
4254 bus, target, ld->cl_lstatus->blocks_to_recover);
4256 case CISS_LSTATUS_EXPANDING:
4257 printf("being expanded, %u blocks remaining\n",
4258 ld->cl_lstatus->blocks_to_recover);
4260 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4261 printf("queued for expansion\n");
4263 case CISS_LSTATUS_FAILED:
4264 printf("queued for expansion\n");
4266 case CISS_LSTATUS_WRONG_PDRIVE:
4267 printf("wrong physical drive inserted\n");
4269 case CISS_LSTATUS_MISSING_PDRIVE:
4270 printf("missing a needed physical drive\n");
4272 case CISS_LSTATUS_BECOMING_READY:
4273 printf("becoming ready\n");
4277 /* print failed physical drives */
4278 for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
4279 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
4280 target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
4283 ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
4284 ld->cl_lstatus->drive_failure_map[i]);
4289 /************************************************************************
4290 * Print information about the controller/driver.
4293 ciss_print_adapter(struct ciss_softc *sc)
4297 ciss_printf(sc, "ADAPTER:\n");
4298 for (i = 0; i < CISSQ_COUNT; i++) {
4299 ciss_printf(sc, "%s %d/%d\n",
4301 i == 1 ? "busy" : "complete",
4302 sc->ciss_qstat[i].q_length,
4303 sc->ciss_qstat[i].q_max);
4305 ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
4306 ciss_printf(sc, "flags %b\n", sc->ciss_flags,
4307 "\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
4309 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
4310 for (j = 0; j < CISS_MAX_LOGICAL; j++) {
4311 ciss_printf(sc, "LOGICAL DRIVE %d: ", i);
4312 ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
4316 /* XXX Should physical drives be printed out here? */
4318 for (i = 1; i < sc->ciss_max_requests; i++)
4319 ciss_print_request(sc->ciss_request + i);
4326 struct ciss_softc *sc;
4328 sc = devclass_get_softc(devclass_find("ciss"), 0);
4330 printf("no ciss controllers\n");
4332 ciss_print_adapter(sc);
4337 /************************************************************************
4338 * Return a name for a logical drive status value.
4341 ciss_name_ldrive_status(int status)
4344 case CISS_LSTATUS_OK:
4346 case CISS_LSTATUS_FAILED:
4348 case CISS_LSTATUS_NOT_CONFIGURED:
4349 return("not configured");
4350 case CISS_LSTATUS_INTERIM_RECOVERY:
4351 return("interim recovery");
4352 case CISS_LSTATUS_READY_RECOVERY:
4353 return("ready for recovery");
4354 case CISS_LSTATUS_RECOVERING:
4355 return("recovering");
4356 case CISS_LSTATUS_WRONG_PDRIVE:
4357 return("wrong physical drive inserted");
4358 case CISS_LSTATUS_MISSING_PDRIVE:
4359 return("missing physical drive");
4360 case CISS_LSTATUS_EXPANDING:
4361 return("expanding");
4362 case CISS_LSTATUS_BECOMING_READY:
4363 return("becoming ready");
4364 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4365 return("queued for expansion");
4367 return("unknown status");
4370 /************************************************************************
4371 * Return an online/offline/nonexistent value for a logical drive
4375 ciss_decode_ldrive_status(int status)
4378 case CISS_LSTATUS_NOT_CONFIGURED:
4379 return(CISS_LD_NONEXISTENT);
4381 case CISS_LSTATUS_OK:
4382 case CISS_LSTATUS_INTERIM_RECOVERY:
4383 case CISS_LSTATUS_READY_RECOVERY:
4384 case CISS_LSTATUS_RECOVERING:
4385 case CISS_LSTATUS_EXPANDING:
4386 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4387 return(CISS_LD_ONLINE);
4389 case CISS_LSTATUS_FAILED:
4390 case CISS_LSTATUS_WRONG_PDRIVE:
4391 case CISS_LSTATUS_MISSING_PDRIVE:
4392 case CISS_LSTATUS_BECOMING_READY:
4394 return(CISS_LD_OFFLINE);
4399 /************************************************************************
4400 * Return a name for a logical drive's organisation.
4403 ciss_name_ldrive_org(int org)
4406 case CISS_LDRIVE_RAID0:
4408 case CISS_LDRIVE_RAID1:
4409 return("RAID 1(1+0)");
4410 case CISS_LDRIVE_RAID4:
4412 case CISS_LDRIVE_RAID5:
4414 case CISS_LDRIVE_RAID51:
4416 case CISS_LDRIVE_RAIDADG:
4422 /************************************************************************
4423 * Return a name for a command status value.
4426 ciss_name_command_status(int status)
4429 case CISS_CMD_STATUS_SUCCESS:
4431 case CISS_CMD_STATUS_TARGET_STATUS:
4432 return("target status");
4433 case CISS_CMD_STATUS_DATA_UNDERRUN:
4434 return("data underrun");
4435 case CISS_CMD_STATUS_DATA_OVERRUN:
4436 return("data overrun");
4437 case CISS_CMD_STATUS_INVALID_COMMAND:
4438 return("invalid command");
4439 case CISS_CMD_STATUS_PROTOCOL_ERROR:
4440 return("protocol error");
4441 case CISS_CMD_STATUS_HARDWARE_ERROR:
4442 return("hardware error");
4443 case CISS_CMD_STATUS_CONNECTION_LOST:
4444 return("connection lost");
4445 case CISS_CMD_STATUS_ABORTED:
4447 case CISS_CMD_STATUS_ABORT_FAILED:
4448 return("abort failed");
4449 case CISS_CMD_STATUS_UNSOLICITED_ABORT:
4450 return("unsolicited abort");
4451 case CISS_CMD_STATUS_TIMEOUT:
4453 case CISS_CMD_STATUS_UNABORTABLE:
4454 return("unabortable");
4456 return("unknown status");
4459 /************************************************************************
4460 * Handle an open on the control device.
4463 ciss_open(struct cdev *dev, int flags, int fmt, struct thread *p)
4465 struct ciss_softc *sc;
4469 sc = (struct ciss_softc *)dev->si_drv1;
4471 /* we might want to veto if someone already has us open */
4473 mtx_lock(&sc->ciss_mtx);
4474 sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
4475 mtx_unlock(&sc->ciss_mtx);
4479 /************************************************************************
4480 * Handle the last close on the control device.
4483 ciss_close(struct cdev *dev, int flags, int fmt, struct thread *p)
4485 struct ciss_softc *sc;
4489 sc = (struct ciss_softc *)dev->si_drv1;
4491 mtx_lock(&sc->ciss_mtx);
4492 sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
4493 mtx_unlock(&sc->ciss_mtx);
4497 /********************************************************************************
4498 * Handle adapter-specific control operations.
4500 * Note that the API here is compatible with the Linux driver, in order to
4501 * simplify the porting of Compaq's userland tools.
4504 ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *p)
4506 struct ciss_softc *sc;
4507 IOCTL_Command_struct *ioc = (IOCTL_Command_struct *)addr;
4509 IOCTL_Command_struct32 *ioc32 = (IOCTL_Command_struct32 *)addr;
4510 IOCTL_Command_struct ioc_swab;
4516 sc = (struct ciss_softc *)dev->si_drv1;
4518 mtx_lock(&sc->ciss_mtx);
4521 case CCISS_GETQSTATS:
4523 union ciss_statrequest *cr = (union ciss_statrequest *)addr;
4525 switch (cr->cs_item) {
4528 bcopy(&sc->ciss_qstat[cr->cs_item], &cr->cs_qstat,
4529 sizeof(struct ciss_qstat));
4539 case CCISS_GETPCIINFO:
4541 cciss_pci_info_struct *pis = (cciss_pci_info_struct *)addr;
4543 pis->bus = pci_get_bus(sc->ciss_dev);
4544 pis->dev_fn = pci_get_slot(sc->ciss_dev);
4545 pis->board_id = (pci_get_subvendor(sc->ciss_dev) << 16) |
4546 pci_get_subdevice(sc->ciss_dev);
4551 case CCISS_GETINTINFO:
4553 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4555 cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
4556 cis->count = sc->ciss_cfg->interrupt_coalesce_count;
4561 case CCISS_SETINTINFO:
4563 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4565 if ((cis->delay == 0) && (cis->count == 0)) {
4571 * XXX apparently this is only safe if the controller is idle,
4572 * we should suspend it before doing this.
4574 sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
4575 sc->ciss_cfg->interrupt_coalesce_count = cis->count;
4577 if (ciss_update_config(sc))
4580 /* XXX resume the controller here */
4584 case CCISS_GETNODENAME:
4585 bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
4586 sizeof(NodeName_type));
4589 case CCISS_SETNODENAME:
4590 bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
4591 sizeof(NodeName_type));
4592 if (ciss_update_config(sc))
4596 case CCISS_GETHEARTBEAT:
4597 *(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
4600 case CCISS_GETBUSTYPES:
4601 *(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
4604 case CCISS_GETFIRMVER:
4605 bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
4606 sizeof(FirmwareVer_type));
4609 case CCISS_GETDRIVERVER:
4610 *(DriverVer_type *)addr = CISS_DRIVER_VERSION;
4613 case CCISS_REVALIDVOLS:
4615 * This is a bit ugly; to do it "right" we really need
4616 * to find any disks that have changed, kick CAM off them,
4617 * then rescan only these disks. It'd be nice if they
4618 * a) told us which disk(s) they were going to play with,
4619 * and b) which ones had arrived. 8(
4624 case CCISS_PASSTHRU32:
4625 ioc_swab.LUN_info = ioc32->LUN_info;
4626 ioc_swab.Request = ioc32->Request;
4627 ioc_swab.error_info = ioc32->error_info;
4628 ioc_swab.buf_size = ioc32->buf_size;
4629 ioc_swab.buf = (u_int8_t *)(uintptr_t)ioc32->buf;
4634 case CCISS_PASSTHRU:
4635 error = ciss_user_command(sc, ioc);
4639 debug(0, "unknown ioctl 0x%lx", cmd);
4641 debug(1, "CCISS_GETPCIINFO: 0x%lx", CCISS_GETPCIINFO);
4642 debug(1, "CCISS_GETINTINFO: 0x%lx", CCISS_GETINTINFO);
4643 debug(1, "CCISS_SETINTINFO: 0x%lx", CCISS_SETINTINFO);
4644 debug(1, "CCISS_GETNODENAME: 0x%lx", CCISS_GETNODENAME);
4645 debug(1, "CCISS_SETNODENAME: 0x%lx", CCISS_SETNODENAME);
4646 debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
4647 debug(1, "CCISS_GETBUSTYPES: 0x%lx", CCISS_GETBUSTYPES);
4648 debug(1, "CCISS_GETFIRMVER: 0x%lx", CCISS_GETFIRMVER);
4649 debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
4650 debug(1, "CCISS_REVALIDVOLS: 0x%lx", CCISS_REVALIDVOLS);
4651 debug(1, "CCISS_PASSTHRU: 0x%lx", CCISS_PASSTHRU);
4657 mtx_unlock(&sc->ciss_mtx);