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 MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data", "ciss internal data buffers");
111 static int ciss_lookup(device_t dev);
112 static int ciss_probe(device_t dev);
113 static int ciss_attach(device_t dev);
114 static int ciss_detach(device_t dev);
115 static int ciss_shutdown(device_t dev);
117 /* (de)initialisation functions, control wrappers */
118 static int ciss_init_pci(struct ciss_softc *sc);
119 static int ciss_setup_msix(struct ciss_softc *sc);
120 static int ciss_init_perf(struct ciss_softc *sc);
121 static int ciss_wait_adapter(struct ciss_softc *sc);
122 static int ciss_flush_adapter(struct ciss_softc *sc);
123 static int ciss_init_requests(struct ciss_softc *sc);
124 static void ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
125 int nseg, int error);
126 static int ciss_identify_adapter(struct ciss_softc *sc);
127 static int ciss_init_logical(struct ciss_softc *sc);
128 static int ciss_init_physical(struct ciss_softc *sc);
129 static int ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll);
130 static int ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
131 static int ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld);
132 static int ciss_update_config(struct ciss_softc *sc);
133 static int ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld);
134 static void ciss_init_sysctl(struct ciss_softc *sc);
135 static void ciss_soft_reset(struct ciss_softc *sc);
136 static void ciss_free(struct ciss_softc *sc);
137 static void ciss_spawn_notify_thread(struct ciss_softc *sc);
138 static void ciss_kill_notify_thread(struct ciss_softc *sc);
140 /* request submission/completion */
141 static int ciss_start(struct ciss_request *cr);
142 static void ciss_done(struct ciss_softc *sc, cr_qhead_t *qh);
143 static void ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh);
144 static void ciss_intr(void *arg);
145 static void ciss_perf_intr(void *arg);
146 static void ciss_perf_msi_intr(void *arg);
147 static void ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh);
148 static int _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func);
149 static int ciss_synch_request(struct ciss_request *cr, int timeout);
150 static int ciss_poll_request(struct ciss_request *cr, int timeout);
151 static int ciss_wait_request(struct ciss_request *cr, int timeout);
153 static int ciss_abort_request(struct ciss_request *cr);
156 /* request queueing */
157 static int ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
158 static void ciss_preen_command(struct ciss_request *cr);
159 static void ciss_release_request(struct ciss_request *cr);
161 /* request helpers */
162 static int ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
163 int opcode, void **bufp, size_t bufsize);
164 static int ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
167 static int ciss_map_request(struct ciss_request *cr);
168 static void ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
169 int nseg, int error);
170 static void ciss_unmap_request(struct ciss_request *cr);
173 static int ciss_cam_init(struct ciss_softc *sc);
174 static void ciss_cam_rescan_target(struct ciss_softc *sc,
175 int bus, int target);
176 static void ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
177 static int ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
178 static int ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
179 static void ciss_cam_poll(struct cam_sim *sim);
180 static void ciss_cam_complete(struct ciss_request *cr);
181 static void ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
182 static struct cam_periph *ciss_find_periph(struct ciss_softc *sc,
183 int bus, int target);
184 static int ciss_name_device(struct ciss_softc *sc, int bus, int target);
186 /* periodic status monitoring */
187 static void ciss_periodic(void *arg);
188 static void ciss_nop_complete(struct ciss_request *cr);
189 static void ciss_disable_adapter(struct ciss_softc *sc);
190 static void ciss_notify_event(struct ciss_softc *sc);
191 static void ciss_notify_complete(struct ciss_request *cr);
192 static int ciss_notify_abort(struct ciss_softc *sc);
193 static int ciss_notify_abort_bmic(struct ciss_softc *sc);
194 static void ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
195 static void ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
196 static void ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
198 /* debugging output */
199 static void ciss_print_request(struct ciss_request *cr);
200 static void ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
201 static const char *ciss_name_ldrive_status(int status);
202 static int ciss_decode_ldrive_status(int status);
203 static const char *ciss_name_ldrive_org(int org);
204 static const char *ciss_name_command_status(int status);
209 static device_method_t ciss_methods[] = {
210 /* Device interface */
211 DEVMETHOD(device_probe, ciss_probe),
212 DEVMETHOD(device_attach, ciss_attach),
213 DEVMETHOD(device_detach, ciss_detach),
214 DEVMETHOD(device_shutdown, ciss_shutdown),
218 static driver_t ciss_pci_driver = {
221 sizeof(struct ciss_softc)
224 static devclass_t ciss_devclass;
225 DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
226 MODULE_DEPEND(ciss, cam, 1, 1, 1);
227 MODULE_DEPEND(ciss, pci, 1, 1, 1);
230 * Control device interface.
232 static d_open_t ciss_open;
233 static d_close_t ciss_close;
234 static d_ioctl_t ciss_ioctl;
236 static struct cdevsw ciss_cdevsw = {
237 .d_version = D_VERSION,
240 .d_close = ciss_close,
241 .d_ioctl = ciss_ioctl,
246 * This tunable can be set at boot time and controls whether physical devices
247 * that are marked hidden by the firmware should be exposed anyways.
249 static unsigned int ciss_expose_hidden_physical = 0;
250 TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
252 static unsigned int ciss_nop_message_heartbeat = 0;
253 TUNABLE_INT("hw.ciss.nop_message_heartbeat", &ciss_nop_message_heartbeat);
256 * This tunable can force a particular transport to be used:
259 * 2 : force performant
261 static int ciss_force_transport = 0;
262 TUNABLE_INT("hw.ciss.force_transport", &ciss_force_transport);
265 * This tunable can force a particular interrupt delivery method to be used:
270 static int ciss_force_interrupt = 0;
271 TUNABLE_INT("hw.ciss.force_interrupt", &ciss_force_interrupt);
273 /************************************************************************
274 * CISS adapters amazingly don't have a defined programming interface
275 * value. (One could say some very despairing things about PCI and
276 * people just not getting the general idea.) So we are forced to
277 * stick with matching against subvendor/subdevice, and thus have to
278 * be updated for every new CISS adapter that appears.
280 #define CISS_BOARD_UNKNWON 0
281 #define CISS_BOARD_SA5 1
282 #define CISS_BOARD_SA5B 2
283 #define CISS_BOARD_NOMSI (1<<4)
291 } ciss_vendor_data[] = {
292 { 0x0e11, 0x4070, CISS_BOARD_SA5|CISS_BOARD_NOMSI, "Compaq Smart Array 5300" },
293 { 0x0e11, 0x4080, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "Compaq Smart Array 5i" },
294 { 0x0e11, 0x4082, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "Compaq Smart Array 532" },
295 { 0x0e11, 0x4083, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "HP Smart Array 5312" },
296 { 0x0e11, 0x4091, CISS_BOARD_SA5, "HP Smart Array 6i" },
297 { 0x0e11, 0x409A, CISS_BOARD_SA5, "HP Smart Array 641" },
298 { 0x0e11, 0x409B, CISS_BOARD_SA5, "HP Smart Array 642" },
299 { 0x0e11, 0x409C, CISS_BOARD_SA5, "HP Smart Array 6400" },
300 { 0x0e11, 0x409D, CISS_BOARD_SA5, "HP Smart Array 6400 EM" },
301 { 0x103C, 0x3211, CISS_BOARD_SA5, "HP Smart Array E200i" },
302 { 0x103C, 0x3212, CISS_BOARD_SA5, "HP Smart Array E200" },
303 { 0x103C, 0x3213, CISS_BOARD_SA5, "HP Smart Array E200i" },
304 { 0x103C, 0x3214, CISS_BOARD_SA5, "HP Smart Array E200i" },
305 { 0x103C, 0x3215, CISS_BOARD_SA5, "HP Smart Array E200i" },
306 { 0x103C, 0x3220, CISS_BOARD_SA5, "HP Smart Array" },
307 { 0x103C, 0x3222, CISS_BOARD_SA5, "HP Smart Array" },
308 { 0x103C, 0x3223, CISS_BOARD_SA5, "HP Smart Array P800" },
309 { 0x103C, 0x3225, CISS_BOARD_SA5, "HP Smart Array P600" },
310 { 0x103C, 0x3230, CISS_BOARD_SA5, "HP Smart Array" },
311 { 0x103C, 0x3231, CISS_BOARD_SA5, "HP Smart Array" },
312 { 0x103C, 0x3232, CISS_BOARD_SA5, "HP Smart Array" },
313 { 0x103C, 0x3233, CISS_BOARD_SA5, "HP Smart Array" },
314 { 0x103C, 0x3234, CISS_BOARD_SA5, "HP Smart Array P400" },
315 { 0x103C, 0x3235, CISS_BOARD_SA5, "HP Smart Array P400i" },
316 { 0x103C, 0x3236, CISS_BOARD_SA5, "HP Smart Array" },
317 { 0x103C, 0x3237, CISS_BOARD_SA5, "HP Smart Array E500" },
318 { 0x103C, 0x3238, CISS_BOARD_SA5, "HP Smart Array" },
319 { 0x103C, 0x3239, CISS_BOARD_SA5, "HP Smart Array" },
320 { 0x103C, 0x323A, CISS_BOARD_SA5, "HP Smart Array" },
321 { 0x103C, 0x323B, CISS_BOARD_SA5, "HP Smart Array" },
322 { 0x103C, 0x323C, CISS_BOARD_SA5, "HP Smart Array" },
323 { 0x103C, 0x323D, CISS_BOARD_SA5, "HP Smart Array P700m" },
324 { 0x103C, 0x3241, CISS_BOARD_SA5, "HP Smart Array P212" },
325 { 0x103C, 0x3243, CISS_BOARD_SA5, "HP Smart Array P410" },
326 { 0x103C, 0x3245, CISS_BOARD_SA5, "HP Smart Array P410i" },
327 { 0x103C, 0x3247, CISS_BOARD_SA5, "HP Smart Array P411" },
328 { 0x103C, 0x3249, CISS_BOARD_SA5, "HP Smart Array P812" },
329 { 0x103C, 0x324A, CISS_BOARD_SA5, "HP Smart Array P712m" },
330 { 0x103C, 0x324B, CISS_BOARD_SA5, "HP Smart Array" },
331 { 0x103C, 0x3350, CISS_BOARD_SA5, "HP Smart Array P222" },
332 { 0x103C, 0x3351, CISS_BOARD_SA5, "HP Smart Array P420" },
333 { 0x103C, 0x3352, CISS_BOARD_SA5, "HP Smart Array P421" },
334 { 0x103C, 0x3353, CISS_BOARD_SA5, "HP Smart Array P822" },
335 { 0x103C, 0x3354, CISS_BOARD_SA5, "HP Smart Array P420i" },
336 { 0x103C, 0x3355, CISS_BOARD_SA5, "HP Smart Array P220i" },
337 { 0x103C, 0x3356, CISS_BOARD_SA5, "HP Smart Array P721m" },
341 /************************************************************************
342 * Find a match for the device in our list of known adapters.
345 ciss_lookup(device_t dev)
349 for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
350 if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
351 (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
357 /************************************************************************
358 * Match a known CISS adapter.
361 ciss_probe(device_t dev)
365 i = ciss_lookup(dev);
367 device_set_desc(dev, ciss_vendor_data[i].desc);
368 return(BUS_PROBE_DEFAULT);
373 /************************************************************************
374 * Attach the driver to this adapter.
377 ciss_attach(device_t dev)
379 struct ciss_softc *sc;
385 /* print structure/union sizes */
386 debug_struct(ciss_command);
387 debug_struct(ciss_header);
388 debug_union(ciss_device_address);
389 debug_struct(ciss_cdb);
390 debug_struct(ciss_report_cdb);
391 debug_struct(ciss_notify_cdb);
392 debug_struct(ciss_notify);
393 debug_struct(ciss_message_cdb);
394 debug_struct(ciss_error_info_pointer);
395 debug_struct(ciss_error_info);
396 debug_struct(ciss_sg_entry);
397 debug_struct(ciss_config_table);
398 debug_struct(ciss_bmic_cdb);
399 debug_struct(ciss_bmic_id_ldrive);
400 debug_struct(ciss_bmic_id_lstatus);
401 debug_struct(ciss_bmic_id_table);
402 debug_struct(ciss_bmic_id_pdrive);
403 debug_struct(ciss_bmic_blink_pdrive);
404 debug_struct(ciss_bmic_flush_cache);
405 debug_const(CISS_MAX_REQUESTS);
406 debug_const(CISS_MAX_LOGICAL);
407 debug_const(CISS_INTERRUPT_COALESCE_DELAY);
408 debug_const(CISS_INTERRUPT_COALESCE_COUNT);
409 debug_const(CISS_COMMAND_ALLOC_SIZE);
410 debug_const(CISS_COMMAND_SG_LENGTH);
412 debug_type(cciss_pci_info_struct);
413 debug_type(cciss_coalint_struct);
414 debug_type(cciss_coalint_struct);
415 debug_type(NodeName_type);
416 debug_type(NodeName_type);
417 debug_type(Heartbeat_type);
418 debug_type(BusTypes_type);
419 debug_type(FirmwareVer_type);
420 debug_type(DriverVer_type);
421 debug_type(IOCTL_Command_struct);
424 sc = device_get_softc(dev);
426 mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
427 callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);
430 * Do PCI-specific init.
432 if ((error = ciss_init_pci(sc)) != 0)
436 * Initialise driver queues.
439 ciss_initq_notify(sc);
442 * Initalize device sysctls.
444 ciss_init_sysctl(sc);
447 * Initialise command/request pool.
449 if ((error = ciss_init_requests(sc)) != 0)
453 * Get adapter information.
455 if ((error = ciss_identify_adapter(sc)) != 0)
459 * Find all the physical devices.
461 if ((error = ciss_init_physical(sc)) != 0)
465 * Build our private table of logical devices.
467 if ((error = ciss_init_logical(sc)) != 0)
471 * Enable interrupts so that the CAM scan can complete.
473 CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
476 * Initialise the CAM interface.
478 if ((error = ciss_cam_init(sc)) != 0)
482 * Start the heartbeat routine and event chain.
487 * Create the control device.
489 sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
490 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
491 "ciss%d", device_get_unit(sc->ciss_dev));
492 sc->ciss_dev_t->si_drv1 = sc;
495 * The adapter is running; synchronous commands can now sleep
496 * waiting for an interrupt to signal completion.
498 sc->ciss_flags |= CISS_FLAG_RUNNING;
500 ciss_spawn_notify_thread(sc);
505 /* ciss_free() expects the mutex to be held */
506 mtx_lock(&sc->ciss_mtx);
512 /************************************************************************
513 * Detach the driver from this adapter.
516 ciss_detach(device_t dev)
518 struct ciss_softc *sc = device_get_softc(dev);
522 mtx_lock(&sc->ciss_mtx);
523 if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
524 mtx_unlock(&sc->ciss_mtx);
528 /* flush adapter cache */
529 ciss_flush_adapter(sc);
531 /* release all resources. The mutex is released and freed here too. */
537 /************************************************************************
538 * Prepare adapter for system shutdown.
541 ciss_shutdown(device_t dev)
543 struct ciss_softc *sc = device_get_softc(dev);
547 mtx_lock(&sc->ciss_mtx);
548 /* flush adapter cache */
549 ciss_flush_adapter(sc);
551 if (sc->ciss_soft_reset)
553 mtx_unlock(&sc->ciss_mtx);
559 ciss_init_sysctl(struct ciss_softc *sc)
562 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
563 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
564 OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
567 /************************************************************************
568 * Perform PCI-specific attachment actions.
571 ciss_init_pci(struct ciss_softc *sc)
573 uintptr_t cbase, csize, cofs;
574 uint32_t method, supported_methods;
575 int error, sqmask, i;
581 * Work out adapter type.
583 i = ciss_lookup(sc->ciss_dev);
585 ciss_printf(sc, "unknown adapter type\n");
589 if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
590 sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
591 } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
592 sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
595 * XXX Big hammer, masks/unmasks all possible interrupts. This should
596 * work on all hardware variants. Need to add code to handle the
597 * "controller crashed" interupt bit that this unmasks.
603 * Allocate register window first (we need this to find the config
607 sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
608 if ((sc->ciss_regs_resource =
609 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
610 &sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
611 ciss_printf(sc, "can't allocate register window\n");
614 sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
615 sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
618 * Find the BAR holding the config structure. If it's not the one
619 * we already mapped for registers, map it too.
621 sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
622 if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
623 if ((sc->ciss_cfg_resource =
624 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
625 &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
626 ciss_printf(sc, "can't allocate config window\n");
629 cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
630 csize = rman_get_end(sc->ciss_cfg_resource) -
631 rman_get_start(sc->ciss_cfg_resource) + 1;
633 cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
634 csize = rman_get_end(sc->ciss_regs_resource) -
635 rman_get_start(sc->ciss_regs_resource) + 1;
637 cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
640 * Use the base/size/offset values we just calculated to
641 * sanity-check the config structure. If it's OK, point to it.
643 if ((cofs + sizeof(struct ciss_config_table)) > csize) {
644 ciss_printf(sc, "config table outside window\n");
647 sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
648 debug(1, "config struct at %p", sc->ciss_cfg);
651 * Calculate the number of request structures/commands we are
652 * going to provide for this adapter.
654 sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
657 * Validate the config structure. If we supported other transport
658 * methods, we could select amongst them at this point in time.
660 if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
661 ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
662 sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
663 sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
668 * Select the mode of operation, prefer Performant.
670 if (!(sc->ciss_cfg->supported_methods &
671 (CISS_TRANSPORT_METHOD_SIMPLE | CISS_TRANSPORT_METHOD_PERF))) {
672 ciss_printf(sc, "No supported transport layers: 0x%x\n",
673 sc->ciss_cfg->supported_methods);
676 switch (ciss_force_transport) {
678 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
681 supported_methods = CISS_TRANSPORT_METHOD_PERF;
684 supported_methods = sc->ciss_cfg->supported_methods;
689 if ((supported_methods & CISS_TRANSPORT_METHOD_PERF) != 0) {
690 method = CISS_TRANSPORT_METHOD_PERF;
691 sc->ciss_perf = (struct ciss_perf_config *)(cbase + cofs +
692 sc->ciss_cfg->transport_offset);
693 if (ciss_init_perf(sc)) {
694 supported_methods &= ~method;
697 } else if (supported_methods & CISS_TRANSPORT_METHOD_SIMPLE) {
698 method = CISS_TRANSPORT_METHOD_SIMPLE;
700 ciss_printf(sc, "No supported transport methods: 0x%x\n",
701 sc->ciss_cfg->supported_methods);
706 * Tell it we're using the low 4GB of RAM. Set the default interrupt
707 * coalescing options.
709 sc->ciss_cfg->requested_method = method;
710 sc->ciss_cfg->command_physlimit = 0;
711 sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
712 sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
715 sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
718 if (ciss_update_config(sc)) {
719 ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
720 CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
723 if ((sc->ciss_cfg->active_method & method) == 0) {
724 supported_methods &= ~method;
725 if (supported_methods == 0) {
726 ciss_printf(sc, "adapter refuses to go into available transports "
727 "mode (0x%x, 0x%x)\n", supported_methods,
728 sc->ciss_cfg->active_method);
735 * Wait for the adapter to come ready.
737 if ((error = ciss_wait_adapter(sc)) != 0)
740 /* Prepare to possibly use MSIX and/or PERFORMANT interrupts. Normal
741 * interrupts have a rid of 0, this will be overridden if MSIX is used.
743 sc->ciss_irq_rid[0] = 0;
744 if (method == CISS_TRANSPORT_METHOD_PERF) {
745 ciss_printf(sc, "PERFORMANT Transport\n");
746 if ((ciss_force_interrupt != 1) && (ciss_setup_msix(sc) == 0)) {
747 intr = ciss_perf_msi_intr;
749 intr = ciss_perf_intr;
751 /* XXX The docs say that the 0x01 bit is only for SAS controllers.
752 * Unfortunately, there is no good way to know if this is a SAS
753 * controller. Hopefully enabling this bit universally will work OK.
754 * It seems to work fine for SA6i controllers.
756 sc->ciss_interrupt_mask = CISS_TL_PERF_INTR_OPQ | CISS_TL_PERF_INTR_MSI;
759 ciss_printf(sc, "SIMPLE Transport\n");
760 /* MSIX doesn't seem to work in SIMPLE mode, only enable if it forced */
761 if (ciss_force_interrupt == 2)
762 /* If this fails, we automatically revert to INTx */
764 sc->ciss_perf = NULL;
766 sc->ciss_interrupt_mask = sqmask;
770 * Turn off interrupts before we go routing anything.
772 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
775 * Allocate and set up our interrupt.
777 if ((sc->ciss_irq_resource =
778 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid[0],
779 RF_ACTIVE | RF_SHAREABLE)) == NULL) {
780 ciss_printf(sc, "can't allocate interrupt\n");
784 if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
785 INTR_TYPE_CAM|INTR_MPSAFE, NULL, intr, sc,
787 ciss_printf(sc, "can't set up interrupt\n");
792 * Allocate the parent bus DMA tag appropriate for our PCI
795 * Note that "simple" adapters can only address within a 32-bit
798 if (bus_dma_tag_create(bus_get_dma_tag(sc->ciss_dev),/* PCI parent */
799 1, 0, /* alignment, boundary */
800 BUS_SPACE_MAXADDR, /* lowaddr */
801 BUS_SPACE_MAXADDR, /* highaddr */
802 NULL, NULL, /* filter, filterarg */
803 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
804 CISS_MAX_SG_ELEMENTS, /* nsegments */
805 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
807 NULL, NULL, /* lockfunc, lockarg */
808 &sc->ciss_parent_dmat)) {
809 ciss_printf(sc, "can't allocate parent DMA tag\n");
814 * Create DMA tag for mapping buffers into adapter-addressable
817 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
818 1, 0, /* alignment, boundary */
819 BUS_SPACE_MAXADDR, /* lowaddr */
820 BUS_SPACE_MAXADDR, /* highaddr */
821 NULL, NULL, /* filter, filterarg */
822 MAXBSIZE, CISS_MAX_SG_ELEMENTS, /* maxsize, nsegments */
823 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
824 BUS_DMA_ALLOCNOW, /* flags */
825 busdma_lock_mutex, &sc->ciss_mtx, /* lockfunc, lockarg */
826 &sc->ciss_buffer_dmat)) {
827 ciss_printf(sc, "can't allocate buffer DMA tag\n");
833 /************************************************************************
834 * Setup MSI/MSIX operation (Performant only)
835 * Four interrupts are available, but we only use 1 right now. If MSI-X
836 * isn't avaialble, try using MSI instead.
839 ciss_setup_msix(struct ciss_softc *sc)
843 /* Weed out devices that don't actually support MSI */
844 i = ciss_lookup(sc->ciss_dev);
845 if (ciss_vendor_data[i].flags & CISS_BOARD_NOMSI)
849 * Only need to use the minimum number of MSI vectors, as the driver
850 * doesn't support directed MSIX interrupts.
852 val = pci_msix_count(sc->ciss_dev);
853 if (val < CISS_MSI_COUNT) {
854 val = pci_msi_count(sc->ciss_dev);
855 device_printf(sc->ciss_dev, "got %d MSI messages]\n", val);
856 if (val < CISS_MSI_COUNT)
859 val = MIN(val, CISS_MSI_COUNT);
860 if (pci_alloc_msix(sc->ciss_dev, &val) != 0) {
861 if (pci_alloc_msi(sc->ciss_dev, &val) != 0)
867 ciss_printf(sc, "Using %d MSIX interrupt%s\n", val,
868 (val != 1) ? "s" : "");
870 for (i = 0; i < val; i++)
871 sc->ciss_irq_rid[i] = i + 1;
877 /************************************************************************
878 * Setup the Performant structures.
881 ciss_init_perf(struct ciss_softc *sc)
883 struct ciss_perf_config *pc = sc->ciss_perf;
887 * Create the DMA tag for the reply queue.
889 reply_size = sizeof(uint64_t) * sc->ciss_max_requests;
890 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
891 1, 0, /* alignment, boundary */
892 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
893 BUS_SPACE_MAXADDR, /* highaddr */
894 NULL, NULL, /* filter, filterarg */
895 reply_size, 1, /* maxsize, nsegments */
896 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
898 NULL, NULL, /* lockfunc, lockarg */
899 &sc->ciss_reply_dmat)) {
900 ciss_printf(sc, "can't allocate reply DMA tag\n");
904 * Allocate memory and make it available for DMA.
906 if (bus_dmamem_alloc(sc->ciss_reply_dmat, (void **)&sc->ciss_reply,
907 BUS_DMA_NOWAIT, &sc->ciss_reply_map)) {
908 ciss_printf(sc, "can't allocate reply memory\n");
911 bus_dmamap_load(sc->ciss_reply_dmat, sc->ciss_reply_map, sc->ciss_reply,
912 reply_size, ciss_command_map_helper, &sc->ciss_reply_phys, 0);
913 bzero(sc->ciss_reply, reply_size);
915 sc->ciss_cycle = 0x1;
919 * Preload the fetch table with common command sizes. This allows the
920 * hardware to not waste bus cycles for typical i/o commands, but also not
921 * tax the driver to be too exact in choosing sizes. The table is optimized
922 * for page-aligned i/o's, but since most i/o comes from the various pagers,
923 * it's a reasonable assumption to make.
925 pc->fetch_count[CISS_SG_FETCH_NONE] = (sizeof(struct ciss_command) + 15) / 16;
926 pc->fetch_count[CISS_SG_FETCH_1] =
927 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 1 + 15) / 16;
928 pc->fetch_count[CISS_SG_FETCH_2] =
929 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 2 + 15) / 16;
930 pc->fetch_count[CISS_SG_FETCH_4] =
931 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 4 + 15) / 16;
932 pc->fetch_count[CISS_SG_FETCH_8] =
933 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 8 + 15) / 16;
934 pc->fetch_count[CISS_SG_FETCH_16] =
935 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 16 + 15) / 16;
936 pc->fetch_count[CISS_SG_FETCH_32] =
937 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 32 + 15) / 16;
938 pc->fetch_count[CISS_SG_FETCH_MAX] = (CISS_COMMAND_ALLOC_SIZE + 15) / 16;
940 pc->rq_size = sc->ciss_max_requests; /* XXX less than the card supports? */
941 pc->rq_count = 1; /* XXX Hardcode for a single queue */
944 pc->rq[0].rq_addr_hi = 0x0;
945 pc->rq[0].rq_addr_lo = sc->ciss_reply_phys;
950 /************************************************************************
951 * Wait for the adapter to come ready.
954 ciss_wait_adapter(struct ciss_softc *sc)
961 * Wait for the adapter to come ready.
963 if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
964 ciss_printf(sc, "waiting for adapter to come ready...\n");
965 for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
966 DELAY(1000000); /* one second */
968 ciss_printf(sc, "timed out waiting for adapter to come ready\n");
976 /************************************************************************
977 * Flush the adapter cache.
980 ciss_flush_adapter(struct ciss_softc *sc)
982 struct ciss_request *cr;
983 struct ciss_bmic_flush_cache *cbfc;
984 int error, command_status;
992 * Build a BMIC request to flush the cache. We don't disable
993 * it, as we may be going to do more I/O (eg. we are emulating
994 * the Synchronise Cache command).
996 if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1000 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
1001 (void **)&cbfc, sizeof(*cbfc))) != 0)
1005 * Submit the request and wait for it to complete.
1007 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1008 ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
1015 ciss_report_request(cr, &command_status, NULL);
1016 switch(command_status) {
1017 case CISS_CMD_STATUS_SUCCESS:
1020 ciss_printf(sc, "error flushing cache (%s)\n",
1021 ciss_name_command_status(command_status));
1028 free(cbfc, CISS_MALLOC_CLASS);
1030 ciss_release_request(cr);
1035 ciss_soft_reset(struct ciss_softc *sc)
1037 struct ciss_request *cr = NULL;
1038 struct ciss_command *cc;
1041 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1042 /* only reset proxy controllers */
1043 if (sc->ciss_controllers[i].physical.bus == 0)
1046 if ((error = ciss_get_request(sc, &cr)) != 0)
1049 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
1054 cc->header.address = sc->ciss_controllers[i];
1056 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
1059 ciss_release_request(cr);
1063 ciss_printf(sc, "error resetting controller (%d)\n", error);
1066 ciss_release_request(cr);
1069 /************************************************************************
1070 * Allocate memory for the adapter command structures, initialise
1071 * the request structures.
1073 * Note that the entire set of commands are allocated in a single
1077 ciss_init_requests(struct ciss_softc *sc)
1079 struct ciss_request *cr;
1085 ciss_printf(sc, "using %d of %d available commands\n",
1086 sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
1089 * Create the DMA tag for commands.
1091 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
1092 32, 0, /* alignment, boundary */
1093 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1094 BUS_SPACE_MAXADDR, /* highaddr */
1095 NULL, NULL, /* filter, filterarg */
1096 CISS_COMMAND_ALLOC_SIZE *
1097 sc->ciss_max_requests, 1, /* maxsize, nsegments */
1098 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1100 NULL, NULL, /* lockfunc, lockarg */
1101 &sc->ciss_command_dmat)) {
1102 ciss_printf(sc, "can't allocate command DMA tag\n");
1106 * Allocate memory and make it available for DMA.
1108 if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
1109 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
1110 ciss_printf(sc, "can't allocate command memory\n");
1113 bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map,sc->ciss_command,
1114 CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
1115 ciss_command_map_helper, &sc->ciss_command_phys, 0);
1116 bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
1119 * Set up the request and command structures, push requests onto
1122 for (i = 1; i < sc->ciss_max_requests; i++) {
1123 cr = &sc->ciss_request[i];
1126 cr->cr_cc = (struct ciss_command *)((uintptr_t)sc->ciss_command +
1127 CISS_COMMAND_ALLOC_SIZE * i);
1128 cr->cr_ccphys = sc->ciss_command_phys + CISS_COMMAND_ALLOC_SIZE * i;
1129 bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
1130 ciss_enqueue_free(cr);
1136 ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1141 *addr = segs[0].ds_addr;
1144 /************************************************************************
1145 * Identify the adapter, print some information about it.
1148 ciss_identify_adapter(struct ciss_softc *sc)
1150 struct ciss_request *cr;
1151 int error, command_status;
1158 * Get a request, allocate storage for the adapter data.
1160 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
1161 (void **)&sc->ciss_id,
1162 sizeof(*sc->ciss_id))) != 0)
1166 * Submit the request and wait for it to complete.
1168 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1169 ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
1176 ciss_report_request(cr, &command_status, NULL);
1177 switch(command_status) {
1178 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1180 case CISS_CMD_STATUS_DATA_UNDERRUN:
1181 case CISS_CMD_STATUS_DATA_OVERRUN:
1182 ciss_printf(sc, "data over/underrun reading adapter information\n");
1184 ciss_printf(sc, "error reading adapter information (%s)\n",
1185 ciss_name_command_status(command_status));
1190 /* sanity-check reply */
1191 if (!sc->ciss_id->big_map_supported) {
1192 ciss_printf(sc, "adapter does not support BIG_MAP\n");
1198 /* XXX later revisions may not need this */
1199 sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
1202 /* XXX only really required for old 5300 adapters? */
1203 sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
1205 /* print information */
1207 #if 0 /* XXX proxy volumes??? */
1208 ciss_printf(sc, " %d logical drive%s configured\n",
1209 sc->ciss_id->configured_logical_drives,
1210 (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
1212 ciss_printf(sc, " firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
1213 ciss_printf(sc, " %d SCSI channels\n", sc->ciss_id->scsi_bus_count);
1215 ciss_printf(sc, " signature '%.4s'\n", sc->ciss_cfg->signature);
1216 ciss_printf(sc, " valence %d\n", sc->ciss_cfg->valence);
1217 ciss_printf(sc, " supported I/O methods 0x%b\n",
1218 sc->ciss_cfg->supported_methods,
1219 "\20\1READY\2simple\3performant\4MEMQ\n");
1220 ciss_printf(sc, " active I/O method 0x%b\n",
1221 sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
1222 ciss_printf(sc, " 4G page base 0x%08x\n",
1223 sc->ciss_cfg->command_physlimit);
1224 ciss_printf(sc, " interrupt coalesce delay %dus\n",
1225 sc->ciss_cfg->interrupt_coalesce_delay);
1226 ciss_printf(sc, " interrupt coalesce count %d\n",
1227 sc->ciss_cfg->interrupt_coalesce_count);
1228 ciss_printf(sc, " max outstanding commands %d\n",
1229 sc->ciss_cfg->max_outstanding_commands);
1230 ciss_printf(sc, " bus types 0x%b\n", sc->ciss_cfg->bus_types,
1231 "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
1232 ciss_printf(sc, " server name '%.16s'\n", sc->ciss_cfg->server_name);
1233 ciss_printf(sc, " heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
1238 if (sc->ciss_id != NULL) {
1239 free(sc->ciss_id, CISS_MALLOC_CLASS);
1244 ciss_release_request(cr);
1248 /************************************************************************
1249 * Helper routine for generating a list of logical and physical luns.
1251 static struct ciss_lun_report *
1252 ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
1254 struct ciss_request *cr;
1255 struct ciss_command *cc;
1256 struct ciss_report_cdb *crc;
1257 struct ciss_lun_report *cll;
1268 * Get a request, allocate storage for the address list.
1270 if ((error = ciss_get_request(sc, &cr)) != 0)
1272 report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
1273 if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1274 ciss_printf(sc, "can't allocate memory for lun report\n");
1280 * Build the Report Logical/Physical LUNs command.
1284 cr->cr_length = report_size;
1285 cr->cr_flags = CISS_REQ_DATAIN;
1287 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
1288 cc->header.address.physical.bus = 0;
1289 cc->header.address.physical.target = 0;
1290 cc->cdb.cdb_length = sizeof(*crc);
1291 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1292 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1293 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1294 cc->cdb.timeout = 30; /* XXX better suggestions? */
1296 crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
1297 bzero(crc, sizeof(*crc));
1298 crc->opcode = opcode;
1299 crc->length = htonl(report_size); /* big-endian field */
1300 cll->list_size = htonl(report_size - sizeof(*cll)); /* big-endian field */
1303 * Submit the request and wait for it to complete. (timeout
1304 * here should be much greater than above)
1306 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1307 ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
1312 * Check response. Note that data over/underrun is OK.
1314 ciss_report_request(cr, &command_status, NULL);
1315 switch(command_status) {
1316 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1317 case CISS_CMD_STATUS_DATA_UNDERRUN: /* buffer too large, not bad */
1319 case CISS_CMD_STATUS_DATA_OVERRUN:
1320 ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
1324 ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
1325 ciss_name_command_status(command_status));
1329 ciss_release_request(cr);
1334 ciss_release_request(cr);
1335 if (error && cll != NULL) {
1336 free(cll, CISS_MALLOC_CLASS);
1342 /************************************************************************
1343 * Find logical drives on the adapter.
1346 ciss_init_logical(struct ciss_softc *sc)
1348 struct ciss_lun_report *cll;
1349 int error = 0, i, j;
1354 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
1361 /* sanity-check reply */
1362 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1363 if ((ndrives < 0) || (ndrives > CISS_MAX_LOGICAL)) {
1364 ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
1365 ndrives, CISS_MAX_LOGICAL);
1371 * Save logical drive information.
1374 ciss_printf(sc, "%d logical drive%s\n",
1375 ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
1379 malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
1380 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1381 if (sc->ciss_logical == NULL) {
1386 for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1387 sc->ciss_logical[i] =
1388 malloc(CISS_MAX_LOGICAL * sizeof(struct ciss_ldrive),
1389 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1390 if (sc->ciss_logical[i] == NULL) {
1395 for (j = 0; j < CISS_MAX_LOGICAL; j++)
1396 sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
1400 for (i = 0; i < CISS_MAX_LOGICAL; i++) {
1402 struct ciss_ldrive *ld;
1405 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
1406 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
1407 ld = &sc->ciss_logical[bus][target];
1409 ld->cl_address = cll->lun[i];
1410 ld->cl_controller = &sc->ciss_controllers[bus];
1411 if (ciss_identify_logical(sc, ld) != 0)
1414 * If the drive has had media exchanged, we should bring it online.
1416 if (ld->cl_lstatus->media_exchanged)
1417 ciss_accept_media(sc, ld);
1424 free(cll, CISS_MALLOC_CLASS);
1429 ciss_init_physical(struct ciss_softc *sc)
1431 struct ciss_lun_report *cll;
1441 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
1448 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1451 ciss_printf(sc, "%d physical device%s\n",
1452 nphys, (nphys > 1 || nphys == 0) ? "s" : "");
1456 * Figure out the bus mapping.
1457 * Logical buses include both the local logical bus for local arrays and
1458 * proxy buses for remote arrays. Physical buses are numbered by the
1459 * controller and represent physical buses that hold physical devices.
1460 * We shift these bus numbers so that everything fits into a single flat
1461 * numbering space for CAM. Logical buses occupy the first 32 CAM bus
1462 * numbers, and the physical bus numbers are shifted to be above that.
1463 * This results in the various driver arrays being indexed as follows:
1465 * ciss_controllers[] - indexed by logical bus
1466 * ciss_cam_sim[] - indexed by both logical and physical, with physical
1467 * being shifted by 32.
1468 * ciss_logical[][] - indexed by logical bus
1469 * ciss_physical[][] - indexed by physical bus
1471 * XXX This is getting more and more hackish. CISS really doesn't play
1472 * well with a standard SCSI model; devices are addressed via magic
1473 * cookies, not via b/t/l addresses. Since there is no way to store
1474 * the cookie in the CAM device object, we have to keep these lookup
1475 * tables handy so that the devices can be found quickly at the cost
1476 * of wasting memory and having a convoluted lookup scheme. This
1477 * driver should probably be converted to block interface.
1480 * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
1481 * controller. A proxy controller is another physical controller
1482 * behind the primary PCI controller. We need to know about this
1483 * so that BMIC commands can be properly targeted. There can be
1484 * proxy controllers attached to a single PCI controller, so
1485 * find the highest numbered one so the array can be properly
1488 sc->ciss_max_logical_bus = 1;
1489 for (i = 0; i < nphys; i++) {
1490 if (cll->lun[i].physical.extra_address == 0) {
1491 bus = cll->lun[i].physical.bus;
1492 sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
1494 bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
1495 sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
1499 sc->ciss_controllers =
1500 malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
1501 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1503 if (sc->ciss_controllers == NULL) {
1504 ciss_printf(sc, "Could not allocate memory for controller map\n");
1509 /* setup a map of controller addresses */
1510 for (i = 0; i < nphys; i++) {
1511 if (cll->lun[i].physical.extra_address == 0) {
1512 sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
1517 malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
1518 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1519 if (sc->ciss_physical == NULL) {
1520 ciss_printf(sc, "Could not allocate memory for physical device map\n");
1525 for (i = 0; i < sc->ciss_max_physical_bus; i++) {
1526 sc->ciss_physical[i] =
1527 malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
1528 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1529 if (sc->ciss_physical[i] == NULL) {
1530 ciss_printf(sc, "Could not allocate memory for target map\n");
1536 ciss_filter_physical(sc, cll);
1540 free(cll, CISS_MALLOC_CLASS);
1546 ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
1552 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1553 for (i = 0; i < nphys; i++) {
1554 if (cll->lun[i].physical.extra_address == 0)
1558 * Filter out devices that we don't want. Level 3 LUNs could
1559 * probably be supported, but the docs don't give enough of a
1562 * The mode field of the physical address is likely set to have
1563 * hard disks masked out. Honor it unless the user has overridden
1564 * us with the tunable. We also munge the inquiry data for these
1565 * disks so that they only show up as passthrough devices. Keeping
1566 * them visible in this fashion is useful for doing things like
1567 * flashing firmware.
1569 ea = cll->lun[i].physical.extra_address;
1570 if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
1571 (CISS_EXTRA_MODE2(ea) == 0x3))
1573 if ((ciss_expose_hidden_physical == 0) &&
1574 (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
1578 * Note: CISS firmware numbers physical busses starting at '1', not
1579 * '0'. This numbering is internal to the firmware and is only
1580 * used as a hint here.
1582 bus = CISS_EXTRA_BUS2(ea) - 1;
1583 target = CISS_EXTRA_TARGET2(ea);
1584 sc->ciss_physical[bus][target].cp_address = cll->lun[i];
1585 sc->ciss_physical[bus][target].cp_online = 1;
1592 ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1594 struct ciss_request *cr;
1595 struct ciss_command *cc;
1596 struct scsi_inquiry *inq;
1602 bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1604 if ((error = ciss_get_request(sc, &cr)) != 0)
1608 cr->cr_data = &ld->cl_geometry;
1609 cr->cr_length = sizeof(ld->cl_geometry);
1610 cr->cr_flags = CISS_REQ_DATAIN;
1612 cc->header.address = ld->cl_address;
1613 cc->cdb.cdb_length = 6;
1614 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1615 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1616 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1617 cc->cdb.timeout = 30;
1619 inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1620 inq->opcode = INQUIRY;
1621 inq->byte2 = SI_EVPD;
1622 inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1623 scsi_ulto2b(sizeof(ld->cl_geometry), inq->length);
1625 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1626 ciss_printf(sc, "error getting geometry (%d)\n", error);
1630 ciss_report_request(cr, &command_status, NULL);
1631 switch(command_status) {
1632 case CISS_CMD_STATUS_SUCCESS:
1633 case CISS_CMD_STATUS_DATA_UNDERRUN:
1635 case CISS_CMD_STATUS_DATA_OVERRUN:
1636 ciss_printf(sc, "WARNING: Data overrun\n");
1639 ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1640 ciss_name_command_status(command_status));
1646 ciss_release_request(cr);
1649 /************************************************************************
1650 * Identify a logical drive, initialise state related to it.
1653 ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1655 struct ciss_request *cr;
1656 struct ciss_command *cc;
1657 struct ciss_bmic_cdb *cbc;
1658 int error, command_status;
1665 * Build a BMIC request to fetch the drive ID.
1667 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
1668 (void **)&ld->cl_ldrive,
1669 sizeof(*ld->cl_ldrive))) != 0)
1672 cc->header.address = *ld->cl_controller; /* target controller */
1673 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1674 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1677 * Submit the request and wait for it to complete.
1679 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1680 ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
1687 ciss_report_request(cr, &command_status, NULL);
1688 switch(command_status) {
1689 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1691 case CISS_CMD_STATUS_DATA_UNDERRUN:
1692 case CISS_CMD_STATUS_DATA_OVERRUN:
1693 ciss_printf(sc, "data over/underrun reading logical drive ID\n");
1695 ciss_printf(sc, "error reading logical drive ID (%s)\n",
1696 ciss_name_command_status(command_status));
1700 ciss_release_request(cr);
1704 * Build a CISS BMIC command to get the logical drive status.
1706 if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
1710 * Get the logical drive geometry.
1712 if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1716 * Print the drive's basic characteristics.
1719 ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
1720 CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
1721 CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
1722 ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
1723 ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
1724 ld->cl_ldrive->block_size));
1726 ciss_print_ldrive(sc, ld);
1730 /* make the drive not-exist */
1731 ld->cl_status = CISS_LD_NONEXISTENT;
1732 if (ld->cl_ldrive != NULL) {
1733 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
1734 ld->cl_ldrive = NULL;
1736 if (ld->cl_lstatus != NULL) {
1737 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
1738 ld->cl_lstatus = NULL;
1742 ciss_release_request(cr);
1747 /************************************************************************
1748 * Get status for a logical drive.
1750 * XXX should we also do this in response to Test Unit Ready?
1753 ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld)
1755 struct ciss_request *cr;
1756 struct ciss_command *cc;
1757 struct ciss_bmic_cdb *cbc;
1758 int error, command_status;
1761 * Build a CISS BMIC command to get the logical drive status.
1763 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
1764 (void **)&ld->cl_lstatus,
1765 sizeof(*ld->cl_lstatus))) != 0)
1768 cc->header.address = *ld->cl_controller; /* target controller */
1769 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1770 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1773 * Submit the request and wait for it to complete.
1775 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1776 ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1783 ciss_report_request(cr, &command_status, NULL);
1784 switch(command_status) {
1785 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1787 case CISS_CMD_STATUS_DATA_UNDERRUN:
1788 case CISS_CMD_STATUS_DATA_OVERRUN:
1789 ciss_printf(sc, "data over/underrun reading logical drive status\n");
1791 ciss_printf(sc, "error reading logical drive status (%s)\n",
1792 ciss_name_command_status(command_status));
1798 * Set the drive's summary status based on the returned status.
1800 * XXX testing shows that a failed JBOD drive comes back at next
1801 * boot in "queued for expansion" mode. WTF?
1803 ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
1807 ciss_release_request(cr);
1811 /************************************************************************
1812 * Notify the adapter of a config update.
1815 ciss_update_config(struct ciss_softc *sc)
1821 CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
1822 for (i = 0; i < 1000; i++) {
1823 if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
1824 CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
1832 /************************************************************************
1833 * Accept new media into a logical drive.
1835 * XXX The drive has previously been offline; it would be good if we
1836 * could make sure it's not open right now.
1839 ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
1841 struct ciss_request *cr;
1842 struct ciss_command *cc;
1843 struct ciss_bmic_cdb *cbc;
1845 int error = 0, ldrive;
1847 ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1849 debug(0, "bringing logical drive %d back online");
1852 * Build a CISS BMIC command to bring the drive back online.
1854 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
1858 cc->header.address = *ld->cl_controller; /* target controller */
1859 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1860 cbc->log_drive = ldrive;
1863 * Submit the request and wait for it to complete.
1865 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1866 ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
1873 ciss_report_request(cr, &command_status, NULL);
1874 switch(command_status) {
1875 case CISS_CMD_STATUS_SUCCESS: /* all OK */
1876 /* we should get a logical drive status changed event here */
1879 ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
1880 ciss_name_command_status(command_status));
1886 ciss_release_request(cr);
1890 /************************************************************************
1891 * Release adapter resources.
1894 ciss_free(struct ciss_softc *sc)
1896 struct ciss_request *cr;
1901 /* we're going away */
1902 sc->ciss_flags |= CISS_FLAG_ABORTING;
1904 /* terminate the periodic heartbeat routine */
1905 callout_stop(&sc->ciss_periodic);
1907 /* cancel the Event Notify chain */
1908 ciss_notify_abort(sc);
1910 ciss_kill_notify_thread(sc);
1912 /* disconnect from CAM */
1913 if (sc->ciss_cam_sim) {
1914 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1915 if (sc->ciss_cam_sim[i]) {
1916 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1917 cam_sim_free(sc->ciss_cam_sim[i], 0);
1920 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
1921 CISS_PHYSICAL_BASE; i++) {
1922 if (sc->ciss_cam_sim[i]) {
1923 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1924 cam_sim_free(sc->ciss_cam_sim[i], 0);
1927 free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
1929 if (sc->ciss_cam_devq)
1930 cam_simq_free(sc->ciss_cam_devq);
1932 /* remove the control device */
1933 mtx_unlock(&sc->ciss_mtx);
1934 if (sc->ciss_dev_t != NULL)
1935 destroy_dev(sc->ciss_dev_t);
1937 /* Final cleanup of the callout. */
1938 callout_drain(&sc->ciss_periodic);
1939 mtx_destroy(&sc->ciss_mtx);
1941 /* free the controller data */
1942 if (sc->ciss_id != NULL)
1943 free(sc->ciss_id, CISS_MALLOC_CLASS);
1945 /* release I/O resources */
1946 if (sc->ciss_regs_resource != NULL)
1947 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1948 sc->ciss_regs_rid, sc->ciss_regs_resource);
1949 if (sc->ciss_cfg_resource != NULL)
1950 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1951 sc->ciss_cfg_rid, sc->ciss_cfg_resource);
1952 if (sc->ciss_intr != NULL)
1953 bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
1954 if (sc->ciss_irq_resource != NULL)
1955 bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
1956 sc->ciss_irq_rid[0], sc->ciss_irq_resource);
1958 pci_release_msi(sc->ciss_dev);
1960 while ((cr = ciss_dequeue_free(sc)) != NULL)
1961 bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
1962 if (sc->ciss_buffer_dmat)
1963 bus_dma_tag_destroy(sc->ciss_buffer_dmat);
1965 /* destroy command memory and DMA tag */
1966 if (sc->ciss_command != NULL) {
1967 bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
1968 bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
1970 if (sc->ciss_command_dmat)
1971 bus_dma_tag_destroy(sc->ciss_command_dmat);
1973 if (sc->ciss_reply) {
1974 bus_dmamap_unload(sc->ciss_reply_dmat, sc->ciss_reply_map);
1975 bus_dmamem_free(sc->ciss_reply_dmat, sc->ciss_reply, sc->ciss_reply_map);
1977 if (sc->ciss_reply_dmat)
1978 bus_dma_tag_destroy(sc->ciss_reply_dmat);
1980 /* destroy DMA tags */
1981 if (sc->ciss_parent_dmat)
1982 bus_dma_tag_destroy(sc->ciss_parent_dmat);
1983 if (sc->ciss_logical) {
1984 for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1985 for (j = 0; j < CISS_MAX_LOGICAL; j++) {
1986 if (sc->ciss_logical[i][j].cl_ldrive)
1987 free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
1988 if (sc->ciss_logical[i][j].cl_lstatus)
1989 free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
1991 free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
1993 free(sc->ciss_logical, CISS_MALLOC_CLASS);
1996 if (sc->ciss_physical) {
1997 for (i = 0; i < sc->ciss_max_physical_bus; i++)
1998 free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
1999 free(sc->ciss_physical, CISS_MALLOC_CLASS);
2002 if (sc->ciss_controllers)
2003 free(sc->ciss_controllers, CISS_MALLOC_CLASS);
2007 /************************************************************************
2008 * Give a command to the adapter.
2010 * Note that this uses the simple transport layer directly. If we
2011 * want to add support for other layers, we'll need a switch of some
2014 * Note that the simple transport layer has no way of refusing a
2015 * command; we only have as many request structures as the adapter
2016 * supports commands, so we don't have to check (this presumes that
2017 * the adapter can handle commands as fast as we throw them at it).
2020 ciss_start(struct ciss_request *cr)
2022 struct ciss_command *cc; /* XXX debugging only */
2026 debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
2029 * Map the request's data.
2031 if ((error = ciss_map_request(cr)))
2035 ciss_print_request(cr);
2041 /************************************************************************
2042 * Fetch completed request(s) from the adapter, queue them for
2043 * completion handling.
2045 * Note that this uses the simple transport layer directly. If we
2046 * want to add support for other layers, we'll need a switch of some
2049 * Note that the simple transport mechanism does not require any
2050 * reentrancy protection; the OPQ read is atomic. If there is a
2051 * chance of a race with something else that might move the request
2052 * off the busy list, then we will have to lock against that
2053 * (eg. timeouts, etc.)
2056 ciss_done(struct ciss_softc *sc, cr_qhead_t *qh)
2058 struct ciss_request *cr;
2059 struct ciss_command *cc;
2060 u_int32_t tag, index;
2065 * Loop quickly taking requests from the adapter and moving them
2066 * to the completed queue.
2070 tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
2071 if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
2074 debug(2, "completed command %d%s", index,
2075 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2076 if (index >= sc->ciss_max_requests) {
2077 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2080 cr = &(sc->ciss_request[index]);
2082 cc->header.host_tag = tag; /* not updated by adapter */
2083 ciss_enqueue_complete(cr, qh);
2089 ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh)
2091 struct ciss_request *cr;
2092 struct ciss_command *cc;
2093 u_int32_t tag, index;
2098 * Loop quickly taking requests from the adapter and moving them
2099 * to the completed queue.
2102 tag = sc->ciss_reply[sc->ciss_rqidx];
2103 if ((tag & CISS_CYCLE_MASK) != sc->ciss_cycle)
2106 debug(2, "completed command %d%s\n", index,
2107 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2108 if (index < sc->ciss_max_requests) {
2109 cr = &(sc->ciss_request[index]);
2111 cc->header.host_tag = tag; /* not updated by adapter */
2112 ciss_enqueue_complete(cr, qh);
2114 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2116 if (++sc->ciss_rqidx == sc->ciss_max_requests) {
2118 sc->ciss_cycle ^= 1;
2124 /************************************************************************
2125 * Take an interrupt from the adapter.
2128 ciss_intr(void *arg)
2131 struct ciss_softc *sc = (struct ciss_softc *)arg;
2134 * The only interrupt we recognise indicates that there are
2135 * entries in the outbound post queue.
2139 mtx_lock(&sc->ciss_mtx);
2140 ciss_complete(sc, &qh);
2141 mtx_unlock(&sc->ciss_mtx);
2145 ciss_perf_intr(void *arg)
2147 struct ciss_softc *sc = (struct ciss_softc *)arg;
2149 /* Clear the interrupt and flush the bridges. Docs say that the flush
2150 * needs to be done twice, which doesn't seem right.
2152 CISS_TL_PERF_CLEAR_INT(sc);
2153 CISS_TL_PERF_FLUSH_INT(sc);
2155 ciss_perf_msi_intr(sc);
2159 ciss_perf_msi_intr(void *arg)
2162 struct ciss_softc *sc = (struct ciss_softc *)arg;
2165 ciss_perf_done(sc, &qh);
2166 mtx_lock(&sc->ciss_mtx);
2167 ciss_complete(sc, &qh);
2168 mtx_unlock(&sc->ciss_mtx);
2172 /************************************************************************
2173 * Process completed requests.
2175 * Requests can be completed in three fashions:
2177 * - by invoking a callback function (cr_complete is non-null)
2178 * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
2179 * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
2182 ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh)
2184 struct ciss_request *cr;
2189 * Loop taking requests off the completed queue and performing
2190 * completion processing on them.
2193 if ((cr = ciss_dequeue_complete(sc, qh)) == NULL)
2195 ciss_unmap_request(cr);
2197 if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
2198 ciss_printf(sc, "WARNING: completing non-busy request\n");
2199 cr->cr_flags &= ~CISS_REQ_BUSY;
2202 * If the request has a callback, invoke it.
2204 if (cr->cr_complete != NULL) {
2205 cr->cr_complete(cr);
2210 * If someone is sleeping on this request, wake them up.
2212 if (cr->cr_flags & CISS_REQ_SLEEP) {
2213 cr->cr_flags &= ~CISS_REQ_SLEEP;
2219 * If someone is polling this request for completion, signal.
2221 if (cr->cr_flags & CISS_REQ_POLL) {
2222 cr->cr_flags &= ~CISS_REQ_POLL;
2227 * Give up and throw the request back on the free queue. This
2228 * should never happen; resources will probably be lost.
2230 ciss_printf(sc, "WARNING: completed command with no submitter\n");
2231 ciss_enqueue_free(cr);
2235 /************************************************************************
2236 * Report on the completion status of a request, and pass back SCSI
2237 * and command status values.
2240 _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func)
2242 struct ciss_command *cc;
2243 struct ciss_error_info *ce;
2248 ce = (struct ciss_error_info *)&(cc->sg[0]);
2251 * We don't consider data under/overrun an error for the Report
2252 * Logical/Physical LUNs commands.
2254 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
2255 ((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
2256 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
2257 ((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
2258 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
2259 (cc->cdb.cdb[0] == INQUIRY))) {
2260 cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
2261 debug(2, "ignoring irrelevant under/overrun error");
2265 * Check the command's error bit, if clear, there's no status and
2268 if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
2269 if (scsi_status != NULL)
2270 *scsi_status = SCSI_STATUS_OK;
2271 if (command_status != NULL)
2272 *command_status = CISS_CMD_STATUS_SUCCESS;
2275 if (command_status != NULL)
2276 *command_status = ce->command_status;
2277 if (scsi_status != NULL) {
2278 if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
2279 *scsi_status = ce->scsi_status;
2285 ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
2286 ce->command_status, ciss_name_command_status(ce->command_status),
2288 if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
2289 ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x, function %s\n",
2290 ce->additional_error_info.invalid_command.offense_size,
2291 ce->additional_error_info.invalid_command.offense_offset,
2292 ce->additional_error_info.invalid_command.offense_value,
2297 ciss_print_request(cr);
2302 /************************************************************************
2303 * Issue a request and don't return until it's completed.
2305 * Depending on adapter status, we may poll or sleep waiting for
2309 ciss_synch_request(struct ciss_request *cr, int timeout)
2311 if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
2312 return(ciss_wait_request(cr, timeout));
2314 return(ciss_poll_request(cr, timeout));
2318 /************************************************************************
2319 * Issue a request and poll for completion.
2321 * Timeout in milliseconds.
2324 ciss_poll_request(struct ciss_request *cr, int timeout)
2327 struct ciss_softc *sc;
2334 cr->cr_flags |= CISS_REQ_POLL;
2335 if ((error = ciss_start(cr)) != 0)
2340 ciss_perf_done(sc, &qh);
2343 ciss_complete(sc, &qh);
2344 if (!(cr->cr_flags & CISS_REQ_POLL))
2347 } while (timeout-- >= 0);
2348 return(EWOULDBLOCK);
2351 /************************************************************************
2352 * Issue a request and sleep waiting for completion.
2354 * Timeout in milliseconds. Note that a spurious wakeup will reset
2358 ciss_wait_request(struct ciss_request *cr, int timeout)
2364 cr->cr_flags |= CISS_REQ_SLEEP;
2365 if ((error = ciss_start(cr)) != 0)
2368 while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
2369 error = msleep(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ", (timeout * hz) / 1000);
2375 /************************************************************************
2376 * Abort a request. Note that a potential exists here to race the
2377 * request being completed; the caller must deal with this.
2380 ciss_abort_request(struct ciss_request *ar)
2382 struct ciss_request *cr;
2383 struct ciss_command *cc;
2384 struct ciss_message_cdb *cmc;
2390 if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
2393 /* build the abort command */
2395 cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL; /* addressing? */
2396 cc->header.address.physical.target = 0;
2397 cc->header.address.physical.bus = 0;
2398 cc->cdb.cdb_length = sizeof(*cmc);
2399 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
2400 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2401 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2402 cc->cdb.timeout = 30;
2404 cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
2405 cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
2406 cmc->type = CISS_MESSAGE_ABORT_TASK;
2407 cmc->abort_tag = ar->cr_tag; /* endianness?? */
2410 * Send the request and wait for a response. If we believe we
2411 * aborted the request OK, clear the flag that indicates it's
2414 error = ciss_synch_request(cr, 35 * 1000);
2416 error = ciss_report_request(cr, NULL, NULL);
2417 ciss_release_request(cr);
2424 /************************************************************************
2425 * Fetch and initialise a request
2428 ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
2430 struct ciss_request *cr;
2435 * Get a request and clean it up.
2437 if ((cr = ciss_dequeue_free(sc)) == NULL)
2442 cr->cr_complete = NULL;
2443 cr->cr_private = NULL;
2444 cr->cr_sg_tag = CISS_SG_MAX; /* Backstop to prevent accidents */
2446 ciss_preen_command(cr);
2452 ciss_preen_command(struct ciss_request *cr)
2454 struct ciss_command *cc;
2458 * Clean up the command structure.
2460 * Note that we set up the error_info structure here, since the
2461 * length can be overwritten by any command.
2464 cc->header.sg_in_list = 0; /* kinda inefficient this way */
2465 cc->header.sg_total = 0;
2466 cc->header.host_tag = cr->cr_tag << 2;
2467 cc->header.host_tag_zeroes = 0;
2468 cmdphys = cr->cr_ccphys;
2469 cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
2470 cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
2473 /************************************************************************
2474 * Release a request to the free list.
2477 ciss_release_request(struct ciss_request *cr)
2479 struct ciss_softc *sc;
2485 /* release the request to the free queue */
2486 ciss_requeue_free(cr);
2489 /************************************************************************
2490 * Allocate a request that will be used to send a BMIC command. Do some
2491 * of the common setup here to avoid duplicating it everywhere else.
2494 ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
2495 int opcode, void **bufp, size_t bufsize)
2497 struct ciss_request *cr;
2498 struct ciss_command *cc;
2499 struct ciss_bmic_cdb *cbc;
2512 if ((error = ciss_get_request(sc, &cr)) != 0)
2516 * Allocate data storage if requested, determine the data direction.
2519 if ((bufsize > 0) && (bufp != NULL)) {
2520 if (*bufp == NULL) {
2521 if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
2527 dataout = 1; /* we are given a buffer, so we are writing */
2532 * Build a CISS BMIC command to get the logical drive ID.
2535 cr->cr_length = bufsize;
2537 cr->cr_flags = CISS_REQ_DATAIN;
2540 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2541 cc->header.address.physical.bus = 0;
2542 cc->header.address.physical.target = 0;
2543 cc->cdb.cdb_length = sizeof(*cbc);
2544 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2545 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2546 cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
2547 cc->cdb.timeout = 0;
2549 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
2550 bzero(cbc, sizeof(*cbc));
2551 cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
2552 cbc->bmic_opcode = opcode;
2553 cbc->size = htons((u_int16_t)bufsize);
2558 ciss_release_request(cr);
2561 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
2567 /************************************************************************
2568 * Handle a command passed in from userspace.
2571 ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
2573 struct ciss_request *cr;
2574 struct ciss_command *cc;
2575 struct ciss_error_info *ce;
2585 while (ciss_get_request(sc, &cr) != 0)
2586 msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
2590 * Allocate an in-kernel databuffer if required, copy in user data.
2592 mtx_unlock(&sc->ciss_mtx);
2593 cr->cr_length = ioc->buf_size;
2594 if (ioc->buf_size > 0) {
2595 if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
2599 if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
2600 debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2606 * Build the request based on the user command.
2608 bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
2609 bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
2611 /* XXX anything else to populate here? */
2612 mtx_lock(&sc->ciss_mtx);
2617 if ((error = ciss_synch_request(cr, 60 * 1000))) {
2618 debug(0, "request failed - %d", error);
2623 * Check to see if the command succeeded.
2625 ce = (struct ciss_error_info *)&(cc->sg[0]);
2626 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
2627 bzero(ce, sizeof(*ce));
2630 * Copy the results back to the user.
2632 bcopy(ce, &ioc->error_info, sizeof(*ce));
2633 mtx_unlock(&sc->ciss_mtx);
2634 if ((ioc->buf_size > 0) &&
2635 (error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
2636 debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2644 mtx_lock(&sc->ciss_mtx);
2647 if ((cr != NULL) && (cr->cr_data != NULL))
2648 free(cr->cr_data, CISS_MALLOC_CLASS);
2650 ciss_release_request(cr);
2654 /************************************************************************
2655 * Map a request into bus-visible space, initialise the scatter/gather
2659 ciss_map_request(struct ciss_request *cr)
2661 struct ciss_softc *sc;
2668 /* check that mapping is necessary */
2669 if (cr->cr_flags & CISS_REQ_MAPPED)
2672 cr->cr_flags |= CISS_REQ_MAPPED;
2674 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2675 BUS_DMASYNC_PREWRITE);
2677 if (cr->cr_data != NULL) {
2678 error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2679 cr->cr_data, cr->cr_length,
2680 ciss_request_map_helper, cr, 0);
2685 * Post the command to the adapter.
2687 cr->cr_sg_tag = CISS_SG_NONE;
2688 cr->cr_flags |= CISS_REQ_BUSY;
2690 CISS_TL_PERF_POST_CMD(sc, cr);
2692 CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2699 ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2701 struct ciss_command *cc;
2702 struct ciss_request *cr;
2703 struct ciss_softc *sc;
2708 cr = (struct ciss_request *)arg;
2712 for (i = 0; i < nseg; i++) {
2713 cc->sg[i].address = segs[i].ds_addr;
2714 cc->sg[i].length = segs[i].ds_len;
2715 cc->sg[i].extension = 0;
2717 /* we leave the s/g table entirely within the command */
2718 cc->header.sg_in_list = nseg;
2719 cc->header.sg_total = nseg;
2721 if (cr->cr_flags & CISS_REQ_DATAIN)
2722 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2723 if (cr->cr_flags & CISS_REQ_DATAOUT)
2724 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2727 cr->cr_sg_tag = CISS_SG_NONE;
2729 cr->cr_sg_tag = CISS_SG_1;
2731 cr->cr_sg_tag = CISS_SG_2;
2733 cr->cr_sg_tag = CISS_SG_4;
2735 cr->cr_sg_tag = CISS_SG_8;
2736 else if (nseg <= 16)
2737 cr->cr_sg_tag = CISS_SG_16;
2738 else if (nseg <= 32)
2739 cr->cr_sg_tag = CISS_SG_32;
2741 cr->cr_sg_tag = CISS_SG_MAX;
2744 * Post the command to the adapter.
2746 cr->cr_flags |= CISS_REQ_BUSY;
2748 CISS_TL_PERF_POST_CMD(sc, cr);
2750 CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2753 /************************************************************************
2754 * Unmap a request from bus-visible space.
2757 ciss_unmap_request(struct ciss_request *cr)
2759 struct ciss_softc *sc;
2765 /* check that unmapping is necessary */
2766 if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
2769 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2770 BUS_DMASYNC_POSTWRITE);
2772 if (cr->cr_data == NULL)
2775 if (cr->cr_flags & CISS_REQ_DATAIN)
2776 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
2777 if (cr->cr_flags & CISS_REQ_DATAOUT)
2778 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
2780 bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2782 cr->cr_flags &= ~CISS_REQ_MAPPED;
2785 /************************************************************************
2786 * Attach the driver to CAM.
2788 * We put all the logical drives on a single SCSI bus.
2791 ciss_cam_init(struct ciss_softc *sc)
2798 * Allocate a devq. We can reuse this for the masked physical
2799 * devices if we decide to export these as well.
2801 if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests - 2)) == NULL) {
2802 ciss_printf(sc, "can't allocate CAM SIM queue\n");
2809 * This naturally wastes a bit of memory. The alternative is to allocate
2810 * and register each bus as it is found, and then track them on a linked
2811 * list. Unfortunately, the driver has a few places where it needs to
2812 * look up the SIM based solely on bus number, and it's unclear whether
2813 * a list traversal would work for these situations.
2815 maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
2816 CISS_PHYSICAL_BASE);
2817 sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
2818 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
2819 if (sc->ciss_cam_sim == NULL) {
2820 ciss_printf(sc, "can't allocate memory for controller SIM\n");
2824 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2825 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2827 device_get_unit(sc->ciss_dev),
2830 sc->ciss_max_requests - 2,
2831 sc->ciss_cam_devq)) == NULL) {
2832 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2837 * Register bus with this SIM.
2839 mtx_lock(&sc->ciss_mtx);
2840 if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) {
2841 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2842 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2843 mtx_unlock(&sc->ciss_mtx);
2847 mtx_unlock(&sc->ciss_mtx);
2850 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
2851 CISS_PHYSICAL_BASE; i++) {
2852 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2854 device_get_unit(sc->ciss_dev),
2856 sc->ciss_max_requests - 2,
2857 sc->ciss_cam_devq)) == NULL) {
2858 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2862 mtx_lock(&sc->ciss_mtx);
2863 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2864 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2865 mtx_unlock(&sc->ciss_mtx);
2868 mtx_unlock(&sc->ciss_mtx);
2874 /************************************************************************
2875 * Initiate a rescan of the 'logical devices' SIM
2878 ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
2884 if ((ccb = xpt_alloc_ccb_nowait()) == NULL) {
2885 ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
2889 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
2890 cam_sim_path(sc->ciss_cam_sim[bus]),
2891 target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2892 ciss_printf(sc, "rescan failed (can't create path)\n");
2897 /* scan is now in progress */
2900 /************************************************************************
2901 * Handle requests coming from CAM
2904 ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
2906 struct ciss_softc *sc;
2907 struct ccb_scsiio *csio;
2911 sc = cam_sim_softc(sim);
2912 bus = cam_sim_bus(sim);
2913 csio = (struct ccb_scsiio *)&ccb->csio;
2914 target = csio->ccb_h.target_id;
2915 physical = CISS_IS_PHYSICAL(bus);
2917 switch (ccb->ccb_h.func_code) {
2919 /* perform SCSI I/O */
2921 if (!ciss_cam_action_io(sim, csio))
2925 /* perform geometry calculations */
2926 case XPT_CALC_GEOMETRY:
2928 struct ccb_calc_geometry *ccg = &ccb->ccg;
2929 struct ciss_ldrive *ld;
2931 debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2935 ld = &sc->ciss_logical[bus][target];
2938 * Use the cached geometry settings unless the fault tolerance
2941 if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
2942 u_int32_t secs_per_cylinder;
2945 ccg->secs_per_track = 32;
2946 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2947 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2949 ccg->heads = ld->cl_geometry.heads;
2950 ccg->secs_per_track = ld->cl_geometry.sectors;
2951 ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2953 ccb->ccb_h.status = CAM_REQ_CMP;
2957 /* handle path attribute inquiry */
2960 struct ccb_pathinq *cpi = &ccb->cpi;
2962 debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2964 cpi->version_num = 1;
2965 cpi->hba_inquiry = PI_TAG_ABLE; /* XXX is this correct? */
2966 cpi->target_sprt = 0;
2968 cpi->max_target = CISS_MAX_LOGICAL;
2969 cpi->max_lun = 0; /* 'logical drive' channel only */
2970 cpi->initiator_id = CISS_MAX_LOGICAL;
2971 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2972 strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
2973 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2974 cpi->unit_number = cam_sim_unit(sim);
2975 cpi->bus_id = cam_sim_bus(sim);
2976 cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */
2977 cpi->transport = XPORT_SPI;
2978 cpi->transport_version = 2;
2979 cpi->protocol = PROTO_SCSI;
2980 cpi->protocol_version = SCSI_REV_2;
2981 cpi->maxio = (CISS_MAX_SG_ELEMENTS - 1) * PAGE_SIZE;
2982 ccb->ccb_h.status = CAM_REQ_CMP;
2986 case XPT_GET_TRAN_SETTINGS:
2988 struct ccb_trans_settings *cts = &ccb->cts;
2990 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
2991 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
2993 bus = cam_sim_bus(sim);
2994 target = cts->ccb_h.target_id;
2996 debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
2997 /* disconnect always OK */
2998 cts->protocol = PROTO_SCSI;
2999 cts->protocol_version = SCSI_REV_2;
3000 cts->transport = XPORT_SPI;
3001 cts->transport_version = 2;
3003 spi->valid = CTS_SPI_VALID_DISC;
3004 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
3006 scsi->valid = CTS_SCSI_VALID_TQ;
3007 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3009 cts->ccb_h.status = CAM_REQ_CMP;
3013 default: /* we can't do this */
3014 debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
3015 ccb->ccb_h.status = CAM_REQ_INVALID;
3022 /************************************************************************
3023 * Handle a CAM SCSI I/O request.
3026 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
3028 struct ciss_softc *sc;
3030 struct ciss_request *cr;
3031 struct ciss_command *cc;
3034 sc = cam_sim_softc(sim);
3035 bus = cam_sim_bus(sim);
3036 target = csio->ccb_h.target_id;
3038 debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
3040 /* check that the CDB pointer is not to a physical address */
3041 if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
3042 debug(3, " CDB pointer is to physical address");
3043 csio->ccb_h.status = CAM_REQ_CMP_ERR;
3046 /* if there is data transfer, it must be to/from a virtual address */
3047 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
3048 if (csio->ccb_h.flags & CAM_DATA_PHYS) { /* we can't map it */
3049 debug(3, " data pointer is to physical address");
3050 csio->ccb_h.status = CAM_REQ_CMP_ERR;
3052 if (csio->ccb_h.flags & CAM_SCATTER_VALID) { /* we want to do the s/g setup */
3053 debug(3, " data has premature s/g setup");
3054 csio->ccb_h.status = CAM_REQ_CMP_ERR;
3058 /* abandon aborted ccbs or those that have failed validation */
3059 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
3060 debug(3, "abandoning CCB due to abort/validation failure");
3064 /* handle emulation of some SCSI commands ourself */
3065 if (ciss_cam_emulate(sc, csio))
3069 * Get a request to manage this command. If we can't, return the
3070 * ccb, freeze the queue and flag so that we unfreeze it when a
3071 * request completes.
3073 if ((error = ciss_get_request(sc, &cr)) != 0) {
3074 xpt_freeze_simq(sim, 1);
3075 sc->ciss_flags |= CISS_FLAG_BUSY;
3076 csio->ccb_h.status |= CAM_REQUEUE_REQ;
3081 * Build the command.
3084 cr->cr_data = csio->data_ptr;
3085 cr->cr_length = csio->dxfer_len;
3086 cr->cr_complete = ciss_cam_complete;
3087 cr->cr_private = csio;
3090 * Target the right logical volume.
3092 if (CISS_IS_PHYSICAL(bus))
3093 cc->header.address =
3094 sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
3096 cc->header.address =
3097 sc->ciss_logical[bus][target].cl_address;
3098 cc->cdb.cdb_length = csio->cdb_len;
3099 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3100 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; /* XXX ordered tags? */
3101 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
3102 cr->cr_flags = CISS_REQ_DATAOUT;
3103 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3104 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
3105 cr->cr_flags = CISS_REQ_DATAIN;
3106 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3109 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
3111 cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
3112 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
3113 bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
3115 bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
3119 * Submit the request to the adapter.
3121 * Note that this may fail if we're unable to map the request (and
3122 * if we ever learn a transport layer other than simple, may fail
3123 * if the adapter rejects the command).
3125 if ((error = ciss_start(cr)) != 0) {
3126 xpt_freeze_simq(sim, 1);
3127 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3128 if (error == EINPROGRESS) {
3131 csio->ccb_h.status |= CAM_REQUEUE_REQ;
3132 ciss_release_request(cr);
3140 /************************************************************************
3141 * Emulate SCSI commands the adapter doesn't handle as we might like.
3144 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
3149 target = csio->ccb_h.target_id;
3150 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3151 opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3152 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
3154 if (CISS_IS_PHYSICAL(bus)) {
3155 if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
3156 csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3157 xpt_done((union ccb *)csio);
3164 * Handle requests for volumes that don't exist or are not online.
3165 * A selection timeout is slightly better than an illegal request.
3166 * Other errors might be better.
3168 if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
3169 csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3170 xpt_done((union ccb *)csio);
3174 /* if we have to fake Synchronise Cache */
3175 if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
3177 * If this is a Synchronise Cache command, typically issued when
3178 * a device is closed, flush the adapter and complete now.
3180 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
3181 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
3182 ciss_flush_adapter(sc);
3183 csio->ccb_h.status |= CAM_REQ_CMP;
3184 xpt_done((union ccb *)csio);
3192 /************************************************************************
3193 * Check for possibly-completed commands.
3196 ciss_cam_poll(struct cam_sim *sim)
3199 struct ciss_softc *sc = cam_sim_softc(sim);
3205 ciss_perf_done(sc, &qh);
3208 ciss_complete(sc, &qh);
3211 /************************************************************************
3212 * Handle completion of a command - pass results back through the CCB
3215 ciss_cam_complete(struct ciss_request *cr)
3217 struct ciss_softc *sc;
3218 struct ciss_command *cc;
3219 struct ciss_error_info *ce;
3220 struct ccb_scsiio *csio;
3228 ce = (struct ciss_error_info *)&(cc->sg[0]);
3229 csio = (struct ccb_scsiio *)cr->cr_private;
3232 * Extract status values from request.
3234 ciss_report_request(cr, &command_status, &scsi_status);
3235 csio->scsi_status = scsi_status;
3238 * Handle specific SCSI status values.
3240 switch(scsi_status) {
3241 /* no status due to adapter error */
3243 debug(0, "adapter error");
3244 csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3247 /* no status due to command completed OK */
3248 case SCSI_STATUS_OK: /* CISS_SCSI_STATUS_GOOD */
3249 debug(2, "SCSI_STATUS_OK");
3250 csio->ccb_h.status |= CAM_REQ_CMP;
3253 /* check condition, sense data included */
3254 case SCSI_STATUS_CHECK_COND: /* CISS_SCSI_STATUS_CHECK_CONDITION */
3255 debug(0, "SCSI_STATUS_CHECK_COND sense size %d resid %d\n",
3256 ce->sense_length, ce->residual_count);
3257 bzero(&csio->sense_data, SSD_FULL_SIZE);
3258 bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
3259 if (csio->sense_len > ce->sense_length)
3260 csio->sense_resid = csio->sense_len - ce->sense_length;
3262 csio->sense_resid = 0;
3263 csio->resid = ce->residual_count;
3264 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
3267 struct scsi_sense_data *sns = (struct scsi_sense_data *)&ce->sense_info[0];
3268 debug(0, "sense key %x", scsi_get_sense_key(sns, csio->sense_len -
3269 csio->sense_resid, /*show_errors*/ 1));
3274 case SCSI_STATUS_BUSY: /* CISS_SCSI_STATUS_BUSY */
3275 debug(0, "SCSI_STATUS_BUSY");
3276 csio->ccb_h.status |= CAM_SCSI_BUSY;
3280 debug(0, "unknown status 0x%x", csio->scsi_status);
3281 csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3285 /* handle post-command fixup */
3286 ciss_cam_complete_fixup(sc, csio);
3288 ciss_release_request(cr);
3289 if (sc->ciss_flags & CISS_FLAG_BUSY) {
3290 sc->ciss_flags &= ~CISS_FLAG_BUSY;
3291 if (csio->ccb_h.status & CAM_RELEASE_SIMQ)
3292 xpt_release_simq(xpt_path_sim(csio->ccb_h.path), 0);
3294 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3296 xpt_done((union ccb *)csio);
3299 /********************************************************************************
3300 * Fix up the result of some commands here.
3303 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
3305 struct scsi_inquiry_data *inq;
3306 struct ciss_ldrive *cl;
3310 cdb = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3311 (uint8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes;
3312 if (cdb[0] == INQUIRY &&
3313 (cdb[1] & SI_EVPD) == 0 &&
3314 (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
3315 csio->dxfer_len >= SHORT_INQUIRY_LENGTH) {
3317 inq = (struct scsi_inquiry_data *)csio->data_ptr;
3318 target = csio->ccb_h.target_id;
3319 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3322 * Don't let hard drives be seen by the DA driver. They will still be
3323 * attached by the PASS driver.
3325 if (CISS_IS_PHYSICAL(bus)) {
3326 if (SID_TYPE(inq) == T_DIRECT)
3327 inq->device = (inq->device & 0xe0) | T_NODEVICE;
3331 cl = &sc->ciss_logical[bus][target];
3333 padstr(inq->vendor, "COMPAQ", 8);
3334 padstr(inq->product, ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance), 8);
3335 padstr(inq->revision, ciss_name_ldrive_status(cl->cl_lstatus->status), 16);
3340 /********************************************************************************
3341 * Find a peripheral attached at (target)
3343 static struct cam_periph *
3344 ciss_find_periph(struct ciss_softc *sc, int bus, int target)
3346 struct cam_periph *periph;
3347 struct cam_path *path;
3350 status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
3352 if (status == CAM_REQ_CMP) {
3353 periph = cam_periph_find(path, NULL);
3354 xpt_free_path(path);
3361 /********************************************************************************
3362 * Name the device at (target)
3364 * XXX is this strictly correct?
3367 ciss_name_device(struct ciss_softc *sc, int bus, int target)
3369 struct cam_periph *periph;
3371 if (CISS_IS_PHYSICAL(bus))
3373 if ((periph = ciss_find_periph(sc, bus, target)) != NULL) {
3374 sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
3375 periph->periph_name, periph->unit_number);
3378 sc->ciss_logical[bus][target].cl_name[0] = 0;
3382 /************************************************************************
3383 * Periodic status monitoring.
3386 ciss_periodic(void *arg)
3388 struct ciss_softc *sc;
3389 struct ciss_request *cr = NULL;
3390 struct ciss_command *cc = NULL;
3395 sc = (struct ciss_softc *)arg;
3398 * Check the adapter heartbeat.
3400 if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
3401 sc->ciss_heart_attack++;
3402 debug(0, "adapter heart attack in progress 0x%x/%d",
3403 sc->ciss_heartbeat, sc->ciss_heart_attack);
3404 if (sc->ciss_heart_attack == 3) {
3405 ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
3406 ciss_disable_adapter(sc);
3410 sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
3411 sc->ciss_heart_attack = 0;
3412 debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
3416 * Send the NOP message and wait for a response.
3418 if (ciss_nop_message_heartbeat != 0 && (error = ciss_get_request(sc, &cr)) == 0) {
3420 cr->cr_complete = ciss_nop_complete;
3421 cc->cdb.cdb_length = 1;
3422 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
3423 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3424 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3425 cc->cdb.timeout = 0;
3426 cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
3428 if ((error = ciss_start(cr)) != 0) {
3429 ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3434 * If the notify event request has died for some reason, or has
3435 * not started yet, restart it.
3437 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
3438 debug(0, "(re)starting Event Notify chain");
3439 ciss_notify_event(sc);
3445 callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
3449 ciss_nop_complete(struct ciss_request *cr)
3451 struct ciss_softc *sc;
3452 static int first_time = 1;
3455 if (ciss_report_request(cr, NULL, NULL) != 0) {
3456 if (first_time == 1) {
3458 ciss_printf(sc, "SENDING NOP MESSAGE FAILED (not logging anymore)\n");
3462 ciss_release_request(cr);
3465 /************************************************************************
3466 * Disable the adapter.
3468 * The all requests in completed queue is failed with hardware error.
3469 * This will cause failover in a multipath configuration.
3472 ciss_disable_adapter(struct ciss_softc *sc)
3475 struct ciss_request *cr;
3476 struct ciss_command *cc;
3477 struct ciss_error_info *ce;
3480 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
3481 pci_disable_busmaster(sc->ciss_dev);
3482 sc->ciss_flags &= ~CISS_FLAG_RUNNING;
3484 for (i = 1; i < sc->ciss_max_requests; i++) {
3485 cr = &sc->ciss_request[i];
3486 if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
3490 ce = (struct ciss_error_info *)&(cc->sg[0]);
3491 ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
3492 ciss_enqueue_complete(cr, &qh);
3496 if ((cr = ciss_dequeue_complete(sc, &qh)) == NULL)
3500 * If the request has a callback, invoke it.
3502 if (cr->cr_complete != NULL) {
3503 cr->cr_complete(cr);
3508 * If someone is sleeping on this request, wake them up.
3510 if (cr->cr_flags & CISS_REQ_SLEEP) {
3511 cr->cr_flags &= ~CISS_REQ_SLEEP;
3518 /************************************************************************
3519 * Request a notification response from the adapter.
3521 * If (cr) is NULL, this is the first request of the adapter, so
3522 * reset the adapter's message pointer and start with the oldest
3523 * message available.
3526 ciss_notify_event(struct ciss_softc *sc)
3528 struct ciss_request *cr;
3529 struct ciss_command *cc;
3530 struct ciss_notify_cdb *cnc;
3535 cr = sc->ciss_periodic_notify;
3537 /* get a request if we don't already have one */
3539 if ((error = ciss_get_request(sc, &cr)) != 0) {
3540 debug(0, "can't get notify event request");
3543 sc->ciss_periodic_notify = cr;
3544 cr->cr_complete = ciss_notify_complete;
3545 debug(1, "acquired request %d", cr->cr_tag);
3549 * Get a databuffer if we don't already have one, note that the
3550 * adapter command wants a larger buffer than the actual
3553 if (cr->cr_data == NULL) {
3554 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3555 debug(0, "can't get notify event request buffer");
3559 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3562 /* re-setup the request's command (since we never release it) XXX overkill*/
3563 ciss_preen_command(cr);
3565 /* (re)build the notify event command */
3567 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3568 cc->header.address.physical.bus = 0;
3569 cc->header.address.physical.target = 0;
3571 cc->cdb.cdb_length = sizeof(*cnc);
3572 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3573 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3574 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3575 cc->cdb.timeout = 0; /* no timeout, we hope */
3577 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3578 bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
3579 cnc->opcode = CISS_OPCODE_READ;
3580 cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
3581 cnc->timeout = 0; /* no timeout, we hope */
3582 cnc->synchronous = 0;
3584 cnc->seek_to_oldest = 0;
3585 if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
3589 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3591 /* submit the request */
3592 error = ciss_start(cr);
3597 if (cr->cr_data != NULL)
3598 free(cr->cr_data, CISS_MALLOC_CLASS);
3599 ciss_release_request(cr);
3601 sc->ciss_periodic_notify = NULL;
3602 debug(0, "can't submit notify event request");
3603 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3605 debug(1, "notify event submitted");
3606 sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
3611 ciss_notify_complete(struct ciss_request *cr)
3613 struct ciss_command *cc;
3614 struct ciss_notify *cn;
3615 struct ciss_softc *sc;
3621 cn = (struct ciss_notify *)cr->cr_data;
3625 * Report request results, decode status.
3627 ciss_report_request(cr, &command_status, &scsi_status);
3630 * Abort the chain on a fatal error.
3632 * XXX which of these are actually errors?
3634 if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
3635 (command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
3636 (command_status != CISS_CMD_STATUS_TIMEOUT)) { /* XXX timeout? */
3637 ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
3638 ciss_name_command_status(command_status));
3639 ciss_release_request(cr);
3640 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3645 * If the adapter gave us a text message, print it.
3647 if (cn->message[0] != 0)
3648 ciss_printf(sc, "*** %.80s\n", cn->message);
3650 debug(0, "notify event class %d subclass %d detail %d",
3651 cn->class, cn->subclass, cn->detail);
3654 * If the response indicates that the notifier has been aborted,
3655 * release the notifier command.
3657 if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
3658 (cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
3659 (cn->detail == 1)) {
3660 debug(0, "notifier exiting");
3661 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3662 ciss_release_request(cr);
3663 sc->ciss_periodic_notify = NULL;
3664 wakeup(&sc->ciss_periodic_notify);
3666 /* Handle notify events in a kernel thread */
3667 ciss_enqueue_notify(cr);
3668 sc->ciss_periodic_notify = NULL;
3669 wakeup(&sc->ciss_periodic_notify);
3670 wakeup(&sc->ciss_notify);
3673 * Send a new notify event command, if we're not aborting.
3675 if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
3676 ciss_notify_event(sc);
3680 /************************************************************************
3681 * Abort the Notify Event chain.
3683 * Note that we can't just abort the command in progress; we have to
3684 * explicitly issue an Abort Notify Event command in order for the
3685 * adapter to clean up correctly.
3687 * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
3688 * the chain will not restart itself.
3691 ciss_notify_abort(struct ciss_softc *sc)
3693 struct ciss_request *cr;
3694 struct ciss_command *cc;
3695 struct ciss_notify_cdb *cnc;
3696 int error, command_status, scsi_status;
3703 /* verify that there's an outstanding command */
3704 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3707 /* get a command to issue the abort with */
3708 if ((error = ciss_get_request(sc, &cr)))
3711 /* get a buffer for the result */
3712 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3713 debug(0, "can't get notify event request buffer");
3717 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3721 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3722 cc->header.address.physical.bus = 0;
3723 cc->header.address.physical.target = 0;
3724 cc->cdb.cdb_length = sizeof(*cnc);
3725 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3726 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3727 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3728 cc->cdb.timeout = 0; /* no timeout, we hope */
3730 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3731 bzero(cnc, sizeof(*cnc));
3732 cnc->opcode = CISS_OPCODE_WRITE;
3733 cnc->command = CISS_COMMAND_ABORT_NOTIFY;
3734 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3736 ciss_print_request(cr);
3739 * Submit the request and wait for it to complete.
3741 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3742 ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
3749 ciss_report_request(cr, &command_status, &scsi_status);
3750 switch(command_status) {
3751 case CISS_CMD_STATUS_SUCCESS:
3753 case CISS_CMD_STATUS_INVALID_COMMAND:
3755 * Some older adapters don't support the CISS version of this
3756 * command. Fall back to using the BMIC version.
3758 error = ciss_notify_abort_bmic(sc);
3763 case CISS_CMD_STATUS_TARGET_STATUS:
3765 * This can happen if the adapter thinks there wasn't an outstanding
3766 * Notify Event command but we did. We clean up here.
3768 if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
3769 if (sc->ciss_periodic_notify != NULL)
3770 ciss_release_request(sc->ciss_periodic_notify);
3777 ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
3778 ciss_name_command_status(command_status));
3784 * Sleep waiting for the notifier command to complete. Note
3785 * that if it doesn't, we may end up in a bad situation, since
3786 * the adapter may deliver it later. Also note that the adapter
3787 * requires the Notify Event command to be cancelled in order to
3788 * maintain internal bookkeeping.
3790 while (sc->ciss_periodic_notify != NULL) {
3791 error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
3792 if (error == EWOULDBLOCK) {
3793 ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
3799 /* release the cancel request */
3801 if (cr->cr_data != NULL)
3802 free(cr->cr_data, CISS_MALLOC_CLASS);
3803 ciss_release_request(cr);
3806 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3810 /************************************************************************
3811 * Abort the Notify Event chain using a BMIC command.
3814 ciss_notify_abort_bmic(struct ciss_softc *sc)
3816 struct ciss_request *cr;
3817 int error, command_status;
3824 /* verify that there's an outstanding command */
3825 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3829 * Build a BMIC command to cancel the Notify on Event command.
3831 * Note that we are sending a CISS opcode here. Odd.
3833 if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
3838 * Submit the request and wait for it to complete.
3840 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3841 ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
3848 ciss_report_request(cr, &command_status, NULL);
3849 switch(command_status) {
3850 case CISS_CMD_STATUS_SUCCESS:
3853 ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
3854 ciss_name_command_status(command_status));
3861 ciss_release_request(cr);
3865 /************************************************************************
3866 * Handle rescanning all the logical volumes when a notify event
3867 * causes the drives to come online or offline.
3870 ciss_notify_rescan_logical(struct ciss_softc *sc)
3872 struct ciss_lun_report *cll;
3873 struct ciss_ldrive *ld;
3877 * We must rescan all logical volumes to get the right logical
3880 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
3885 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
3888 * Delete any of the drives which were destroyed by the
3891 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
3892 for (j = 0; j < CISS_MAX_LOGICAL; j++) {
3893 ld = &sc->ciss_logical[i][j];
3895 if (ld->cl_update == 0)
3898 if (ld->cl_status != CISS_LD_ONLINE) {
3899 ciss_cam_rescan_target(sc, i, j);
3902 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
3904 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
3906 ld->cl_ldrive = NULL;
3907 ld->cl_lstatus = NULL;
3913 * Scan for new drives.
3915 for (i = 0; i < ndrives; i++) {
3918 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
3919 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
3920 ld = &sc->ciss_logical[bus][target];
3922 if (ld->cl_update == 0)
3926 ld->cl_address = cll->lun[i];
3927 ld->cl_controller = &sc->ciss_controllers[bus];
3928 if (ciss_identify_logical(sc, ld) == 0) {
3929 ciss_cam_rescan_target(sc, bus, target);
3932 free(cll, CISS_MALLOC_CLASS);
3935 /************************************************************************
3936 * Handle a notify event relating to the status of a logical drive.
3938 * XXX need to be able to defer some of these to properly handle
3939 * calling the "ID Physical drive" command, unless the 'extended'
3940 * drive IDs are always in BIG_MAP format.
3943 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
3945 struct ciss_ldrive *ld;
3946 int ostatus, bus, target;
3950 bus = cn->device.physical.bus;
3951 target = cn->data.logical_status.logical_drive;
3952 ld = &sc->ciss_logical[bus][target];
3954 switch (cn->subclass) {
3955 case CISS_NOTIFY_LOGICAL_STATUS:
3956 switch (cn->detail) {
3958 ciss_name_device(sc, bus, target);
3959 ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
3960 cn->data.logical_status.logical_drive, ld->cl_name,
3961 ciss_name_ldrive_status(cn->data.logical_status.previous_state),
3962 ciss_name_ldrive_status(cn->data.logical_status.new_state),
3963 cn->data.logical_status.spare_state,
3964 "\20\1configured\2rebuilding\3failed\4in use\5available\n");
3967 * Update our idea of the drive's status.
3969 ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
3970 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3971 if (ld->cl_lstatus != NULL)
3972 ld->cl_lstatus->status = cn->data.logical_status.new_state;
3975 * Have CAM rescan the drive if its status has changed.
3977 if (ostatus != ld->cl_status) {
3979 ciss_notify_rescan_logical(sc);
3984 case 1: /* logical drive has recognised new media, needs Accept Media Exchange */
3985 ciss_name_device(sc, bus, target);
3986 ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
3987 cn->data.logical_status.logical_drive, ld->cl_name);
3988 ciss_accept_media(sc, ld);
3991 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3992 ciss_notify_rescan_logical(sc);
3997 ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
3998 cn->data.rebuild_aborted.logical_drive,
4000 (cn->detail == 2) ? "read" : "write");
4005 case CISS_NOTIFY_LOGICAL_ERROR:
4006 if (cn->detail == 0) {
4007 ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
4008 cn->data.io_error.logical_drive,
4010 cn->data.io_error.failure_bus,
4011 cn->data.io_error.failure_drive);
4012 /* XXX should we take the drive down at this point, or will we be told? */
4016 case CISS_NOTIFY_LOGICAL_SURFACE:
4017 if (cn->detail == 0)
4018 ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
4019 cn->data.consistency_completed.logical_drive,
4025 /************************************************************************
4026 * Handle a notify event relating to the status of a physical drive.
4029 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
4033 /************************************************************************
4034 * Handle a notify event relating to the status of a physical drive.
4037 ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
4039 struct ciss_lun_report *cll = NULL;
4042 switch (cn->subclass) {
4043 case CISS_NOTIFY_HOTPLUG_PHYSICAL:
4044 case CISS_NOTIFY_HOTPLUG_NONDISK:
4045 bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
4047 CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
4049 if (cn->detail == 0) {
4051 * Mark the device offline so that it'll start producing selection
4052 * timeouts to the upper layer.
4054 if ((bus >= 0) && (target >= 0))
4055 sc->ciss_physical[bus][target].cp_online = 0;
4058 * Rescan the physical lun list for new items
4060 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
4063 ciss_printf(sc, "Warning, cannot get physical lun list\n");
4066 ciss_filter_physical(sc, cll);
4071 ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
4076 free(cll, CISS_MALLOC_CLASS);
4079 /************************************************************************
4080 * Handle deferred processing of notify events. Notify events may need
4081 * sleep which is unsafe during an interrupt.
4084 ciss_notify_thread(void *arg)
4086 struct ciss_softc *sc;
4087 struct ciss_request *cr;
4088 struct ciss_notify *cn;
4090 sc = (struct ciss_softc *)arg;
4091 #if __FreeBSD_version >= 500000
4092 mtx_lock(&sc->ciss_mtx);
4096 if (STAILQ_EMPTY(&sc->ciss_notify) != 0 &&
4097 (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
4098 msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
4101 if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
4104 cr = ciss_dequeue_notify(sc);
4108 cn = (struct ciss_notify *)cr->cr_data;
4110 switch (cn->class) {
4111 case CISS_NOTIFY_HOTPLUG:
4112 ciss_notify_hotplug(sc, cn);
4114 case CISS_NOTIFY_LOGICAL:
4115 ciss_notify_logical(sc, cn);
4117 case CISS_NOTIFY_PHYSICAL:
4118 ciss_notify_physical(sc, cn);
4122 ciss_release_request(cr);
4125 sc->ciss_notify_thread = NULL;
4126 wakeup(&sc->ciss_notify_thread);
4128 #if __FreeBSD_version >= 500000
4129 mtx_unlock(&sc->ciss_mtx);
4134 /************************************************************************
4135 * Start the notification kernel thread.
4138 ciss_spawn_notify_thread(struct ciss_softc *sc)
4141 #if __FreeBSD_version > 500005
4142 if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4143 &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
4144 device_get_unit(sc->ciss_dev)))
4146 if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4147 &sc->ciss_notify_thread, "ciss_notify%d",
4148 device_get_unit(sc->ciss_dev)))
4150 panic("Could not create notify thread\n");
4153 /************************************************************************
4154 * Kill the notification kernel thread.
4157 ciss_kill_notify_thread(struct ciss_softc *sc)
4160 if (sc->ciss_notify_thread == NULL)
4163 sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
4164 wakeup(&sc->ciss_notify);
4165 msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
4168 /************************************************************************
4172 ciss_print_request(struct ciss_request *cr)
4174 struct ciss_softc *sc;
4175 struct ciss_command *cc;
4181 ciss_printf(sc, "REQUEST @ %p\n", cr);
4182 ciss_printf(sc, " data %p/%d tag %d flags %b\n",
4183 cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
4184 "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
4185 ciss_printf(sc, " sg list/total %d/%d host tag 0x%x\n",
4186 cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
4187 switch(cc->header.address.mode.mode) {
4188 case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
4189 case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
4190 ciss_printf(sc, " physical bus %d target %d\n",
4191 cc->header.address.physical.bus, cc->header.address.physical.target);
4193 case CISS_HDR_ADDRESS_MODE_LOGICAL:
4194 ciss_printf(sc, " logical unit %d\n", cc->header.address.logical.lun);
4197 ciss_printf(sc, " %s cdb length %d type %s attribute %s\n",
4198 (cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
4199 (cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
4200 (cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
4202 (cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
4203 (cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
4204 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
4205 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
4206 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
4207 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
4208 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
4209 ciss_printf(sc, " %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
4211 if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
4212 /* XXX print error info */
4214 /* since we don't use chained s/g, don't support it here */
4215 for (i = 0; i < cc->header.sg_in_list; i++) {
4217 ciss_printf(sc, " ");
4218 printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
4219 if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
4225 /************************************************************************
4226 * Print information about the status of a logical drive.
4229 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
4233 if (ld->cl_lstatus == NULL) {
4234 printf("does not exist\n");
4238 /* print drive status */
4239 switch(ld->cl_lstatus->status) {
4240 case CISS_LSTATUS_OK:
4243 case CISS_LSTATUS_INTERIM_RECOVERY:
4244 printf("in interim recovery mode\n");
4246 case CISS_LSTATUS_READY_RECOVERY:
4247 printf("ready to begin recovery\n");
4249 case CISS_LSTATUS_RECOVERING:
4250 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4251 target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4252 printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
4253 bus, target, ld->cl_lstatus->blocks_to_recover);
4255 case CISS_LSTATUS_EXPANDING:
4256 printf("being expanded, %u blocks remaining\n",
4257 ld->cl_lstatus->blocks_to_recover);
4259 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4260 printf("queued for expansion\n");
4262 case CISS_LSTATUS_FAILED:
4263 printf("queued for expansion\n");
4265 case CISS_LSTATUS_WRONG_PDRIVE:
4266 printf("wrong physical drive inserted\n");
4268 case CISS_LSTATUS_MISSING_PDRIVE:
4269 printf("missing a needed physical drive\n");
4271 case CISS_LSTATUS_BECOMING_READY:
4272 printf("becoming ready\n");
4276 /* print failed physical drives */
4277 for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
4278 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
4279 target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
4282 ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
4283 ld->cl_lstatus->drive_failure_map[i]);
4288 /************************************************************************
4289 * Print information about the controller/driver.
4292 ciss_print_adapter(struct ciss_softc *sc)
4296 ciss_printf(sc, "ADAPTER:\n");
4297 for (i = 0; i < CISSQ_COUNT; i++) {
4298 ciss_printf(sc, "%s %d/%d\n",
4300 i == 1 ? "busy" : "complete",
4301 sc->ciss_qstat[i].q_length,
4302 sc->ciss_qstat[i].q_max);
4304 ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
4305 ciss_printf(sc, "flags %b\n", sc->ciss_flags,
4306 "\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
4308 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
4309 for (j = 0; j < CISS_MAX_LOGICAL; j++) {
4310 ciss_printf(sc, "LOGICAL DRIVE %d: ", i);
4311 ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
4315 /* XXX Should physical drives be printed out here? */
4317 for (i = 1; i < sc->ciss_max_requests; i++)
4318 ciss_print_request(sc->ciss_request + i);
4325 struct ciss_softc *sc;
4327 sc = devclass_get_softc(devclass_find("ciss"), 0);
4329 printf("no ciss controllers\n");
4331 ciss_print_adapter(sc);
4336 /************************************************************************
4337 * Return a name for a logical drive status value.
4340 ciss_name_ldrive_status(int status)
4343 case CISS_LSTATUS_OK:
4345 case CISS_LSTATUS_FAILED:
4347 case CISS_LSTATUS_NOT_CONFIGURED:
4348 return("not configured");
4349 case CISS_LSTATUS_INTERIM_RECOVERY:
4350 return("interim recovery");
4351 case CISS_LSTATUS_READY_RECOVERY:
4352 return("ready for recovery");
4353 case CISS_LSTATUS_RECOVERING:
4354 return("recovering");
4355 case CISS_LSTATUS_WRONG_PDRIVE:
4356 return("wrong physical drive inserted");
4357 case CISS_LSTATUS_MISSING_PDRIVE:
4358 return("missing physical drive");
4359 case CISS_LSTATUS_EXPANDING:
4360 return("expanding");
4361 case CISS_LSTATUS_BECOMING_READY:
4362 return("becoming ready");
4363 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4364 return("queued for expansion");
4366 return("unknown status");
4369 /************************************************************************
4370 * Return an online/offline/nonexistent value for a logical drive
4374 ciss_decode_ldrive_status(int status)
4377 case CISS_LSTATUS_NOT_CONFIGURED:
4378 return(CISS_LD_NONEXISTENT);
4380 case CISS_LSTATUS_OK:
4381 case CISS_LSTATUS_INTERIM_RECOVERY:
4382 case CISS_LSTATUS_READY_RECOVERY:
4383 case CISS_LSTATUS_RECOVERING:
4384 case CISS_LSTATUS_EXPANDING:
4385 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4386 return(CISS_LD_ONLINE);
4388 case CISS_LSTATUS_FAILED:
4389 case CISS_LSTATUS_WRONG_PDRIVE:
4390 case CISS_LSTATUS_MISSING_PDRIVE:
4391 case CISS_LSTATUS_BECOMING_READY:
4393 return(CISS_LD_OFFLINE);
4398 /************************************************************************
4399 * Return a name for a logical drive's organisation.
4402 ciss_name_ldrive_org(int org)
4405 case CISS_LDRIVE_RAID0:
4407 case CISS_LDRIVE_RAID1:
4408 return("RAID 1(1+0)");
4409 case CISS_LDRIVE_RAID4:
4411 case CISS_LDRIVE_RAID5:
4413 case CISS_LDRIVE_RAID51:
4415 case CISS_LDRIVE_RAIDADG:
4421 /************************************************************************
4422 * Return a name for a command status value.
4425 ciss_name_command_status(int status)
4428 case CISS_CMD_STATUS_SUCCESS:
4430 case CISS_CMD_STATUS_TARGET_STATUS:
4431 return("target status");
4432 case CISS_CMD_STATUS_DATA_UNDERRUN:
4433 return("data underrun");
4434 case CISS_CMD_STATUS_DATA_OVERRUN:
4435 return("data overrun");
4436 case CISS_CMD_STATUS_INVALID_COMMAND:
4437 return("invalid command");
4438 case CISS_CMD_STATUS_PROTOCOL_ERROR:
4439 return("protocol error");
4440 case CISS_CMD_STATUS_HARDWARE_ERROR:
4441 return("hardware error");
4442 case CISS_CMD_STATUS_CONNECTION_LOST:
4443 return("connection lost");
4444 case CISS_CMD_STATUS_ABORTED:
4446 case CISS_CMD_STATUS_ABORT_FAILED:
4447 return("abort failed");
4448 case CISS_CMD_STATUS_UNSOLICITED_ABORT:
4449 return("unsolicited abort");
4450 case CISS_CMD_STATUS_TIMEOUT:
4452 case CISS_CMD_STATUS_UNABORTABLE:
4453 return("unabortable");
4455 return("unknown status");
4458 /************************************************************************
4459 * Handle an open on the control device.
4462 ciss_open(struct cdev *dev, int flags, int fmt, struct thread *p)
4464 struct ciss_softc *sc;
4468 sc = (struct ciss_softc *)dev->si_drv1;
4470 /* we might want to veto if someone already has us open */
4472 mtx_lock(&sc->ciss_mtx);
4473 sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
4474 mtx_unlock(&sc->ciss_mtx);
4478 /************************************************************************
4479 * Handle the last close on the control device.
4482 ciss_close(struct cdev *dev, int flags, int fmt, struct thread *p)
4484 struct ciss_softc *sc;
4488 sc = (struct ciss_softc *)dev->si_drv1;
4490 mtx_lock(&sc->ciss_mtx);
4491 sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
4492 mtx_unlock(&sc->ciss_mtx);
4496 /********************************************************************************
4497 * Handle adapter-specific control operations.
4499 * Note that the API here is compatible with the Linux driver, in order to
4500 * simplify the porting of Compaq's userland tools.
4503 ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *p)
4505 struct ciss_softc *sc;
4506 IOCTL_Command_struct *ioc = (IOCTL_Command_struct *)addr;
4508 IOCTL_Command_struct32 *ioc32 = (IOCTL_Command_struct32 *)addr;
4509 IOCTL_Command_struct ioc_swab;
4515 sc = (struct ciss_softc *)dev->si_drv1;
4517 mtx_lock(&sc->ciss_mtx);
4520 case CCISS_GETQSTATS:
4522 union ciss_statrequest *cr = (union ciss_statrequest *)addr;
4524 switch (cr->cs_item) {
4527 bcopy(&sc->ciss_qstat[cr->cs_item], &cr->cs_qstat,
4528 sizeof(struct ciss_qstat));
4538 case CCISS_GETPCIINFO:
4540 cciss_pci_info_struct *pis = (cciss_pci_info_struct *)addr;
4542 pis->bus = pci_get_bus(sc->ciss_dev);
4543 pis->dev_fn = pci_get_slot(sc->ciss_dev);
4544 pis->board_id = (pci_get_subvendor(sc->ciss_dev) << 16) |
4545 pci_get_subdevice(sc->ciss_dev);
4550 case CCISS_GETINTINFO:
4552 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4554 cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
4555 cis->count = sc->ciss_cfg->interrupt_coalesce_count;
4560 case CCISS_SETINTINFO:
4562 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4564 if ((cis->delay == 0) && (cis->count == 0)) {
4570 * XXX apparently this is only safe if the controller is idle,
4571 * we should suspend it before doing this.
4573 sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
4574 sc->ciss_cfg->interrupt_coalesce_count = cis->count;
4576 if (ciss_update_config(sc))
4579 /* XXX resume the controller here */
4583 case CCISS_GETNODENAME:
4584 bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
4585 sizeof(NodeName_type));
4588 case CCISS_SETNODENAME:
4589 bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
4590 sizeof(NodeName_type));
4591 if (ciss_update_config(sc))
4595 case CCISS_GETHEARTBEAT:
4596 *(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
4599 case CCISS_GETBUSTYPES:
4600 *(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
4603 case CCISS_GETFIRMVER:
4604 bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
4605 sizeof(FirmwareVer_type));
4608 case CCISS_GETDRIVERVER:
4609 *(DriverVer_type *)addr = CISS_DRIVER_VERSION;
4612 case CCISS_REVALIDVOLS:
4614 * This is a bit ugly; to do it "right" we really need
4615 * to find any disks that have changed, kick CAM off them,
4616 * then rescan only these disks. It'd be nice if they
4617 * a) told us which disk(s) they were going to play with,
4618 * and b) which ones had arrived. 8(
4623 case CCISS_PASSTHRU32:
4624 ioc_swab.LUN_info = ioc32->LUN_info;
4625 ioc_swab.Request = ioc32->Request;
4626 ioc_swab.error_info = ioc32->error_info;
4627 ioc_swab.buf_size = ioc32->buf_size;
4628 ioc_swab.buf = (u_int8_t *)(uintptr_t)ioc32->buf;
4633 case CCISS_PASSTHRU:
4634 error = ciss_user_command(sc, ioc);
4638 debug(0, "unknown ioctl 0x%lx", cmd);
4640 debug(1, "CCISS_GETPCIINFO: 0x%lx", CCISS_GETPCIINFO);
4641 debug(1, "CCISS_GETINTINFO: 0x%lx", CCISS_GETINTINFO);
4642 debug(1, "CCISS_SETINTINFO: 0x%lx", CCISS_SETINTINFO);
4643 debug(1, "CCISS_GETNODENAME: 0x%lx", CCISS_GETNODENAME);
4644 debug(1, "CCISS_SETNODENAME: 0x%lx", CCISS_SETNODENAME);
4645 debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
4646 debug(1, "CCISS_GETBUSTYPES: 0x%lx", CCISS_GETBUSTYPES);
4647 debug(1, "CCISS_GETFIRMVER: 0x%lx", CCISS_GETFIRMVER);
4648 debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
4649 debug(1, "CCISS_REVALIDVOLS: 0x%lx", CCISS_REVALIDVOLS);
4650 debug(1, "CCISS_PASSTHRU: 0x%lx", CCISS_PASSTHRU);
4656 mtx_unlock(&sc->ciss_mtx);