2 * Copyright (c) 2001 Michael Smith
3 * Copyright (c) 2004 Paul Saab
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Common Interface for SCSI-3 Support driver.
33 * CISS claims to provide a common interface between a generic SCSI
34 * transport and an intelligent host adapter.
36 * This driver supports CISS as defined in the document "CISS Command
37 * Interface for SCSI-3 Support Open Specification", Version 1.04,
38 * Valence Number 1, dated 20001127, produced by Compaq Computer
39 * Corporation. This document appears to be a hastily and somewhat
40 * arbitrarlily cut-down version of a larger (and probably even more
41 * chaotic and inconsistent) Compaq internal document. Various
42 * details were also gleaned from Compaq's "cciss" driver for Linux.
44 * We provide a shim layer between the CISS interface and CAM,
45 * offloading most of the queueing and being-a-disk chores onto CAM.
46 * Entry to the driver is via the PCI bus attachment (ciss_probe,
47 * ciss_attach, etc) and via the CAM interface (ciss_cam_action,
48 * ciss_cam_poll). The Compaq CISS adapters are, however, poor SCSI
49 * citizens and we have to fake up some responses to get reasonable
50 * behaviour out of them. In addition, the CISS command set is by no
51 * means adequate to support the functionality of a RAID controller,
52 * and thus the supported Compaq adapters utilise portions of the
53 * control protocol from earlier Compaq adapter families.
55 * Note that we only support the "simple" transport layer over PCI.
56 * This interface (ab)uses the I2O register set (specifically the post
57 * queues) to exchange commands with the adapter. Other interfaces
58 * are available, but we aren't supposed to know about them, and it is
59 * dubious whether they would provide major performance improvements
60 * except under extreme load.
62 * Currently the only supported CISS adapters are the Compaq Smart
63 * Array 5* series (5300, 5i, 532). Even with only three adapters,
64 * Compaq still manage to have interface variations.
67 * Thanks must go to Fred Harris and Darryl DeVinney at Compaq, as
68 * well as Paul Saab at Yahoo! for their assistance in making this
71 * More thanks must go to John Cagle at HP for the countless hours
72 * spent making this driver "work" with the MSA* series storage
73 * enclosures. Without his help (and nagging), this driver could not
74 * be used with these enclosures.
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/malloc.h>
80 #include <sys/kernel.h>
84 #include <sys/kthread.h>
85 #include <sys/queue.h>
86 #include <sys/sysctl.h>
89 #include <cam/cam_ccb.h>
90 #include <cam/cam_periph.h>
91 #include <cam/cam_sim.h>
92 #include <cam/cam_xpt_sim.h>
93 #include <cam/scsi/scsi_all.h>
94 #include <cam/scsi/scsi_message.h>
96 #include <machine/bus.h>
97 #include <machine/endian.h>
98 #include <machine/resource.h>
101 #include <dev/pci/pcireg.h>
102 #include <dev/pci/pcivar.h>
104 #include <dev/ciss/cissreg.h>
105 #include <dev/ciss/cissio.h>
106 #include <dev/ciss/cissvar.h>
108 static MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data",
109 "ciss internal data buffers");
112 static int ciss_lookup(device_t dev);
113 static int ciss_probe(device_t dev);
114 static int ciss_attach(device_t dev);
115 static int ciss_detach(device_t dev);
116 static int ciss_shutdown(device_t dev);
118 /* (de)initialisation functions, control wrappers */
119 static int ciss_init_pci(struct ciss_softc *sc);
120 static int ciss_setup_msix(struct ciss_softc *sc);
121 static int ciss_init_perf(struct ciss_softc *sc);
122 static int ciss_wait_adapter(struct ciss_softc *sc);
123 static int ciss_flush_adapter(struct ciss_softc *sc);
124 static int ciss_init_requests(struct ciss_softc *sc);
125 static void ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
126 int nseg, int error);
127 static int ciss_identify_adapter(struct ciss_softc *sc);
128 static int ciss_init_logical(struct ciss_softc *sc);
129 static int ciss_init_physical(struct ciss_softc *sc);
130 static int ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll);
131 static int ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
132 static int ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld);
133 static int ciss_update_config(struct ciss_softc *sc);
134 static int ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld);
135 static void ciss_init_sysctl(struct ciss_softc *sc);
136 static void ciss_soft_reset(struct ciss_softc *sc);
137 static void ciss_free(struct ciss_softc *sc);
138 static void ciss_spawn_notify_thread(struct ciss_softc *sc);
139 static void ciss_kill_notify_thread(struct ciss_softc *sc);
141 /* request submission/completion */
142 static int ciss_start(struct ciss_request *cr);
143 static void ciss_done(struct ciss_softc *sc, cr_qhead_t *qh);
144 static void ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh);
145 static void ciss_intr(void *arg);
146 static void ciss_perf_intr(void *arg);
147 static void ciss_perf_msi_intr(void *arg);
148 static void ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh);
149 static int _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func);
150 static int ciss_synch_request(struct ciss_request *cr, int timeout);
151 static int ciss_poll_request(struct ciss_request *cr, int timeout);
152 static int ciss_wait_request(struct ciss_request *cr, int timeout);
154 static int ciss_abort_request(struct ciss_request *cr);
157 /* request queueing */
158 static int ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
159 static void ciss_preen_command(struct ciss_request *cr);
160 static void ciss_release_request(struct ciss_request *cr);
162 /* request helpers */
163 static int ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
164 int opcode, void **bufp, size_t bufsize);
165 static int ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
168 static int ciss_map_request(struct ciss_request *cr);
169 static void ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
170 int nseg, int error);
171 static void ciss_unmap_request(struct ciss_request *cr);
174 static int ciss_cam_init(struct ciss_softc *sc);
175 static void ciss_cam_rescan_target(struct ciss_softc *sc,
176 int bus, int target);
177 static void ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
178 static int ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
179 static int ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
180 static void ciss_cam_poll(struct cam_sim *sim);
181 static void ciss_cam_complete(struct ciss_request *cr);
182 static void ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
183 static struct cam_periph *ciss_find_periph(struct ciss_softc *sc,
184 int bus, int target);
185 static int ciss_name_device(struct ciss_softc *sc, int bus, int target);
187 /* periodic status monitoring */
188 static void ciss_periodic(void *arg);
189 static void ciss_nop_complete(struct ciss_request *cr);
190 static void ciss_disable_adapter(struct ciss_softc *sc);
191 static void ciss_notify_event(struct ciss_softc *sc);
192 static void ciss_notify_complete(struct ciss_request *cr);
193 static int ciss_notify_abort(struct ciss_softc *sc);
194 static int ciss_notify_abort_bmic(struct ciss_softc *sc);
195 static void ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
196 static void ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
197 static void ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
199 /* debugging output */
200 static void ciss_print_request(struct ciss_request *cr);
201 static void ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
202 static const char *ciss_name_ldrive_status(int status);
203 static int ciss_decode_ldrive_status(int status);
204 static const char *ciss_name_ldrive_org(int org);
205 static const char *ciss_name_command_status(int status);
210 static device_method_t ciss_methods[] = {
211 /* Device interface */
212 DEVMETHOD(device_probe, ciss_probe),
213 DEVMETHOD(device_attach, ciss_attach),
214 DEVMETHOD(device_detach, ciss_detach),
215 DEVMETHOD(device_shutdown, ciss_shutdown),
219 static driver_t ciss_pci_driver = {
222 sizeof(struct ciss_softc)
225 static devclass_t ciss_devclass;
226 DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
227 MODULE_DEPEND(ciss, cam, 1, 1, 1);
228 MODULE_DEPEND(ciss, pci, 1, 1, 1);
231 * Control device interface.
233 static d_open_t ciss_open;
234 static d_close_t ciss_close;
235 static d_ioctl_t ciss_ioctl;
237 static struct cdevsw ciss_cdevsw = {
238 .d_version = D_VERSION,
241 .d_close = ciss_close,
242 .d_ioctl = ciss_ioctl,
247 * This tunable can be set at boot time and controls whether physical devices
248 * that are marked hidden by the firmware should be exposed anyways.
250 static unsigned int ciss_expose_hidden_physical = 0;
251 TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
253 static unsigned int ciss_nop_message_heartbeat = 0;
254 TUNABLE_INT("hw.ciss.nop_message_heartbeat", &ciss_nop_message_heartbeat);
257 * This tunable can force a particular transport to be used:
260 * 2 : force performant
262 static int ciss_force_transport = 0;
263 TUNABLE_INT("hw.ciss.force_transport", &ciss_force_transport);
266 * This tunable can force a particular interrupt delivery method to be used:
271 static int ciss_force_interrupt = 0;
272 TUNABLE_INT("hw.ciss.force_interrupt", &ciss_force_interrupt);
274 /************************************************************************
275 * CISS adapters amazingly don't have a defined programming interface
276 * value. (One could say some very despairing things about PCI and
277 * people just not getting the general idea.) So we are forced to
278 * stick with matching against subvendor/subdevice, and thus have to
279 * be updated for every new CISS adapter that appears.
281 #define CISS_BOARD_UNKNWON 0
282 #define CISS_BOARD_SA5 1
283 #define CISS_BOARD_SA5B 2
284 #define CISS_BOARD_NOMSI (1<<4)
285 #define CISS_BOARD_SIMPLE (1<<5)
293 } ciss_vendor_data[] = {
294 { 0x0e11, 0x4070, CISS_BOARD_SA5|CISS_BOARD_NOMSI|CISS_BOARD_SIMPLE,
295 "Compaq Smart Array 5300" },
296 { 0x0e11, 0x4080, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "Compaq Smart Array 5i" },
297 { 0x0e11, 0x4082, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "Compaq Smart Array 532" },
298 { 0x0e11, 0x4083, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "HP Smart Array 5312" },
299 { 0x0e11, 0x4091, CISS_BOARD_SA5, "HP Smart Array 6i" },
300 { 0x0e11, 0x409A, CISS_BOARD_SA5, "HP Smart Array 641" },
301 { 0x0e11, 0x409B, CISS_BOARD_SA5, "HP Smart Array 642" },
302 { 0x0e11, 0x409C, CISS_BOARD_SA5, "HP Smart Array 6400" },
303 { 0x0e11, 0x409D, CISS_BOARD_SA5, "HP Smart Array 6400 EM" },
304 { 0x103C, 0x3211, CISS_BOARD_SA5, "HP Smart Array E200i" },
305 { 0x103C, 0x3212, CISS_BOARD_SA5, "HP Smart Array E200" },
306 { 0x103C, 0x3213, CISS_BOARD_SA5, "HP Smart Array E200i" },
307 { 0x103C, 0x3214, CISS_BOARD_SA5, "HP Smart Array E200i" },
308 { 0x103C, 0x3215, CISS_BOARD_SA5, "HP Smart Array E200i" },
309 { 0x103C, 0x3220, CISS_BOARD_SA5, "HP Smart Array" },
310 { 0x103C, 0x3222, CISS_BOARD_SA5, "HP Smart Array" },
311 { 0x103C, 0x3223, CISS_BOARD_SA5, "HP Smart Array P800" },
312 { 0x103C, 0x3225, CISS_BOARD_SA5, "HP Smart Array P600" },
313 { 0x103C, 0x3230, CISS_BOARD_SA5, "HP Smart Array" },
314 { 0x103C, 0x3231, CISS_BOARD_SA5, "HP Smart Array" },
315 { 0x103C, 0x3232, CISS_BOARD_SA5, "HP Smart Array" },
316 { 0x103C, 0x3233, CISS_BOARD_SA5, "HP Smart Array" },
317 { 0x103C, 0x3234, CISS_BOARD_SA5, "HP Smart Array P400" },
318 { 0x103C, 0x3235, CISS_BOARD_SA5, "HP Smart Array P400i" },
319 { 0x103C, 0x3236, CISS_BOARD_SA5, "HP Smart Array" },
320 { 0x103C, 0x3237, CISS_BOARD_SA5, "HP Smart Array E500" },
321 { 0x103C, 0x3238, CISS_BOARD_SA5, "HP Smart Array" },
322 { 0x103C, 0x3239, CISS_BOARD_SA5, "HP Smart Array" },
323 { 0x103C, 0x323A, CISS_BOARD_SA5, "HP Smart Array" },
324 { 0x103C, 0x323B, CISS_BOARD_SA5, "HP Smart Array" },
325 { 0x103C, 0x323C, CISS_BOARD_SA5, "HP Smart Array" },
326 { 0x103C, 0x323D, CISS_BOARD_SA5, "HP Smart Array P700m" },
327 { 0x103C, 0x3241, CISS_BOARD_SA5, "HP Smart Array P212" },
328 { 0x103C, 0x3243, CISS_BOARD_SA5, "HP Smart Array P410" },
329 { 0x103C, 0x3245, CISS_BOARD_SA5, "HP Smart Array P410i" },
330 { 0x103C, 0x3247, CISS_BOARD_SA5, "HP Smart Array P411" },
331 { 0x103C, 0x3249, CISS_BOARD_SA5, "HP Smart Array P812" },
332 { 0x103C, 0x324A, CISS_BOARD_SA5, "HP Smart Array P712m" },
333 { 0x103C, 0x324B, CISS_BOARD_SA5, "HP Smart Array" },
334 { 0x103C, 0x3350, CISS_BOARD_SA5, "HP Smart Array P222" },
335 { 0x103C, 0x3351, CISS_BOARD_SA5, "HP Smart Array P420" },
336 { 0x103C, 0x3352, CISS_BOARD_SA5, "HP Smart Array P421" },
337 { 0x103C, 0x3353, CISS_BOARD_SA5, "HP Smart Array P822" },
338 { 0x103C, 0x3354, CISS_BOARD_SA5, "HP Smart Array P420i" },
339 { 0x103C, 0x3355, CISS_BOARD_SA5, "HP Smart Array P220i" },
340 { 0x103C, 0x3356, CISS_BOARD_SA5, "HP Smart Array P721m" },
344 /************************************************************************
345 * Find a match for the device in our list of known adapters.
348 ciss_lookup(device_t dev)
352 for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
353 if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
354 (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
360 /************************************************************************
361 * Match a known CISS adapter.
364 ciss_probe(device_t dev)
368 i = ciss_lookup(dev);
370 device_set_desc(dev, ciss_vendor_data[i].desc);
371 return(BUS_PROBE_DEFAULT);
376 /************************************************************************
377 * Attach the driver to this adapter.
380 ciss_attach(device_t dev)
382 struct ciss_softc *sc;
388 /* print structure/union sizes */
389 debug_struct(ciss_command);
390 debug_struct(ciss_header);
391 debug_union(ciss_device_address);
392 debug_struct(ciss_cdb);
393 debug_struct(ciss_report_cdb);
394 debug_struct(ciss_notify_cdb);
395 debug_struct(ciss_notify);
396 debug_struct(ciss_message_cdb);
397 debug_struct(ciss_error_info_pointer);
398 debug_struct(ciss_error_info);
399 debug_struct(ciss_sg_entry);
400 debug_struct(ciss_config_table);
401 debug_struct(ciss_bmic_cdb);
402 debug_struct(ciss_bmic_id_ldrive);
403 debug_struct(ciss_bmic_id_lstatus);
404 debug_struct(ciss_bmic_id_table);
405 debug_struct(ciss_bmic_id_pdrive);
406 debug_struct(ciss_bmic_blink_pdrive);
407 debug_struct(ciss_bmic_flush_cache);
408 debug_const(CISS_MAX_REQUESTS);
409 debug_const(CISS_MAX_LOGICAL);
410 debug_const(CISS_INTERRUPT_COALESCE_DELAY);
411 debug_const(CISS_INTERRUPT_COALESCE_COUNT);
412 debug_const(CISS_COMMAND_ALLOC_SIZE);
413 debug_const(CISS_COMMAND_SG_LENGTH);
415 debug_type(cciss_pci_info_struct);
416 debug_type(cciss_coalint_struct);
417 debug_type(cciss_coalint_struct);
418 debug_type(NodeName_type);
419 debug_type(NodeName_type);
420 debug_type(Heartbeat_type);
421 debug_type(BusTypes_type);
422 debug_type(FirmwareVer_type);
423 debug_type(DriverVer_type);
424 debug_type(IOCTL_Command_struct);
427 sc = device_get_softc(dev);
429 mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
430 callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);
433 * Do PCI-specific init.
435 if ((error = ciss_init_pci(sc)) != 0)
439 * Initialise driver queues.
442 ciss_initq_notify(sc);
445 * Initalize device sysctls.
447 ciss_init_sysctl(sc);
450 * Initialise command/request pool.
452 if ((error = ciss_init_requests(sc)) != 0)
456 * Get adapter information.
458 if ((error = ciss_identify_adapter(sc)) != 0)
462 * Find all the physical devices.
464 if ((error = ciss_init_physical(sc)) != 0)
468 * Build our private table of logical devices.
470 if ((error = ciss_init_logical(sc)) != 0)
474 * Enable interrupts so that the CAM scan can complete.
476 CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
479 * Initialise the CAM interface.
481 if ((error = ciss_cam_init(sc)) != 0)
485 * Start the heartbeat routine and event chain.
490 * Create the control device.
492 sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
493 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
494 "ciss%d", device_get_unit(sc->ciss_dev));
495 sc->ciss_dev_t->si_drv1 = sc;
498 * The adapter is running; synchronous commands can now sleep
499 * waiting for an interrupt to signal completion.
501 sc->ciss_flags |= CISS_FLAG_RUNNING;
503 ciss_spawn_notify_thread(sc);
508 /* ciss_free() expects the mutex to be held */
509 mtx_lock(&sc->ciss_mtx);
515 /************************************************************************
516 * Detach the driver from this adapter.
519 ciss_detach(device_t dev)
521 struct ciss_softc *sc = device_get_softc(dev);
525 mtx_lock(&sc->ciss_mtx);
526 if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
527 mtx_unlock(&sc->ciss_mtx);
531 /* flush adapter cache */
532 ciss_flush_adapter(sc);
534 /* release all resources. The mutex is released and freed here too. */
540 /************************************************************************
541 * Prepare adapter for system shutdown.
544 ciss_shutdown(device_t dev)
546 struct ciss_softc *sc = device_get_softc(dev);
550 mtx_lock(&sc->ciss_mtx);
551 /* flush adapter cache */
552 ciss_flush_adapter(sc);
554 if (sc->ciss_soft_reset)
556 mtx_unlock(&sc->ciss_mtx);
562 ciss_init_sysctl(struct ciss_softc *sc)
565 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
566 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
567 OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
570 /************************************************************************
571 * Perform PCI-specific attachment actions.
574 ciss_init_pci(struct ciss_softc *sc)
576 uintptr_t cbase, csize, cofs;
577 uint32_t method, supported_methods;
578 int error, sqmask, i;
584 * Work out adapter type.
586 i = ciss_lookup(sc->ciss_dev);
588 ciss_printf(sc, "unknown adapter type\n");
592 if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
593 sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
594 } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
595 sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
598 * XXX Big hammer, masks/unmasks all possible interrupts. This should
599 * work on all hardware variants. Need to add code to handle the
600 * "controller crashed" interupt bit that this unmasks.
606 * Allocate register window first (we need this to find the config
610 sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
611 if ((sc->ciss_regs_resource =
612 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
613 &sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
614 ciss_printf(sc, "can't allocate register window\n");
617 sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
618 sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
621 * Find the BAR holding the config structure. If it's not the one
622 * we already mapped for registers, map it too.
624 sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
625 if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
626 if ((sc->ciss_cfg_resource =
627 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
628 &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
629 ciss_printf(sc, "can't allocate config window\n");
632 cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
633 csize = rman_get_end(sc->ciss_cfg_resource) -
634 rman_get_start(sc->ciss_cfg_resource) + 1;
636 cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
637 csize = rman_get_end(sc->ciss_regs_resource) -
638 rman_get_start(sc->ciss_regs_resource) + 1;
640 cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
643 * Use the base/size/offset values we just calculated to
644 * sanity-check the config structure. If it's OK, point to it.
646 if ((cofs + sizeof(struct ciss_config_table)) > csize) {
647 ciss_printf(sc, "config table outside window\n");
650 sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
651 debug(1, "config struct at %p", sc->ciss_cfg);
654 * Calculate the number of request structures/commands we are
655 * going to provide for this adapter.
657 sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
660 * Validate the config structure. If we supported other transport
661 * methods, we could select amongst them at this point in time.
663 if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
664 ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
665 sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
666 sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
671 * Select the mode of operation, prefer Performant.
673 if (!(sc->ciss_cfg->supported_methods &
674 (CISS_TRANSPORT_METHOD_SIMPLE | CISS_TRANSPORT_METHOD_PERF))) {
675 ciss_printf(sc, "No supported transport layers: 0x%x\n",
676 sc->ciss_cfg->supported_methods);
679 switch (ciss_force_transport) {
681 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
684 supported_methods = CISS_TRANSPORT_METHOD_PERF;
688 * Override the capabilities of the BOARD and specify SIMPLE
691 if (ciss_vendor_data[i].flags & CISS_BOARD_SIMPLE)
692 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
694 supported_methods = sc->ciss_cfg->supported_methods;
699 if ((supported_methods & CISS_TRANSPORT_METHOD_PERF) != 0) {
700 method = CISS_TRANSPORT_METHOD_PERF;
701 sc->ciss_perf = (struct ciss_perf_config *)(cbase + cofs +
702 sc->ciss_cfg->transport_offset);
703 if (ciss_init_perf(sc)) {
704 supported_methods &= ~method;
707 } else if (supported_methods & CISS_TRANSPORT_METHOD_SIMPLE) {
708 method = CISS_TRANSPORT_METHOD_SIMPLE;
710 ciss_printf(sc, "No supported transport methods: 0x%x\n",
711 sc->ciss_cfg->supported_methods);
716 * Tell it we're using the low 4GB of RAM. Set the default interrupt
717 * coalescing options.
719 sc->ciss_cfg->requested_method = method;
720 sc->ciss_cfg->command_physlimit = 0;
721 sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
722 sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
725 sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
728 if (ciss_update_config(sc)) {
729 ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
730 CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
733 if ((sc->ciss_cfg->active_method & method) == 0) {
734 supported_methods &= ~method;
735 if (supported_methods == 0) {
736 ciss_printf(sc, "adapter refuses to go into available transports "
737 "mode (0x%x, 0x%x)\n", supported_methods,
738 sc->ciss_cfg->active_method);
745 * Wait for the adapter to come ready.
747 if ((error = ciss_wait_adapter(sc)) != 0)
750 /* Prepare to possibly use MSIX and/or PERFORMANT interrupts. Normal
751 * interrupts have a rid of 0, this will be overridden if MSIX is used.
753 sc->ciss_irq_rid[0] = 0;
754 if (method == CISS_TRANSPORT_METHOD_PERF) {
755 ciss_printf(sc, "PERFORMANT Transport\n");
756 if ((ciss_force_interrupt != 1) && (ciss_setup_msix(sc) == 0)) {
757 intr = ciss_perf_msi_intr;
759 intr = ciss_perf_intr;
761 /* XXX The docs say that the 0x01 bit is only for SAS controllers.
762 * Unfortunately, there is no good way to know if this is a SAS
763 * controller. Hopefully enabling this bit universally will work OK.
764 * It seems to work fine for SA6i controllers.
766 sc->ciss_interrupt_mask = CISS_TL_PERF_INTR_OPQ | CISS_TL_PERF_INTR_MSI;
769 ciss_printf(sc, "SIMPLE Transport\n");
770 /* MSIX doesn't seem to work in SIMPLE mode, only enable if it forced */
771 if (ciss_force_interrupt == 2)
772 /* If this fails, we automatically revert to INTx */
774 sc->ciss_perf = NULL;
776 sc->ciss_interrupt_mask = sqmask;
780 * Turn off interrupts before we go routing anything.
782 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
785 * Allocate and set up our interrupt.
787 if ((sc->ciss_irq_resource =
788 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid[0],
789 RF_ACTIVE | RF_SHAREABLE)) == NULL) {
790 ciss_printf(sc, "can't allocate interrupt\n");
794 if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
795 INTR_TYPE_CAM|INTR_MPSAFE, NULL, intr, sc,
797 ciss_printf(sc, "can't set up interrupt\n");
802 * Allocate the parent bus DMA tag appropriate for our PCI
805 * Note that "simple" adapters can only address within a 32-bit
808 if (bus_dma_tag_create(bus_get_dma_tag(sc->ciss_dev),/* PCI parent */
809 1, 0, /* alignment, boundary */
810 BUS_SPACE_MAXADDR, /* lowaddr */
811 BUS_SPACE_MAXADDR, /* highaddr */
812 NULL, NULL, /* filter, filterarg */
813 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
814 CISS_MAX_SG_ELEMENTS, /* nsegments */
815 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
817 NULL, NULL, /* lockfunc, lockarg */
818 &sc->ciss_parent_dmat)) {
819 ciss_printf(sc, "can't allocate parent DMA tag\n");
824 * Create DMA tag for mapping buffers into adapter-addressable
827 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
828 1, 0, /* alignment, boundary */
829 BUS_SPACE_MAXADDR, /* lowaddr */
830 BUS_SPACE_MAXADDR, /* highaddr */
831 NULL, NULL, /* filter, filterarg */
832 MAXBSIZE, CISS_MAX_SG_ELEMENTS, /* maxsize, nsegments */
833 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
834 BUS_DMA_ALLOCNOW, /* flags */
835 busdma_lock_mutex, &sc->ciss_mtx, /* lockfunc, lockarg */
836 &sc->ciss_buffer_dmat)) {
837 ciss_printf(sc, "can't allocate buffer DMA tag\n");
843 /************************************************************************
844 * Setup MSI/MSIX operation (Performant only)
845 * Four interrupts are available, but we only use 1 right now. If MSI-X
846 * isn't avaialble, try using MSI instead.
849 ciss_setup_msix(struct ciss_softc *sc)
853 /* Weed out devices that don't actually support MSI */
854 i = ciss_lookup(sc->ciss_dev);
855 if (ciss_vendor_data[i].flags & CISS_BOARD_NOMSI)
859 * Only need to use the minimum number of MSI vectors, as the driver
860 * doesn't support directed MSIX interrupts.
862 val = pci_msix_count(sc->ciss_dev);
863 if (val < CISS_MSI_COUNT) {
864 val = pci_msi_count(sc->ciss_dev);
865 device_printf(sc->ciss_dev, "got %d MSI messages]\n", val);
866 if (val < CISS_MSI_COUNT)
869 val = MIN(val, CISS_MSI_COUNT);
870 if (pci_alloc_msix(sc->ciss_dev, &val) != 0) {
871 if (pci_alloc_msi(sc->ciss_dev, &val) != 0)
877 ciss_printf(sc, "Using %d MSIX interrupt%s\n", val,
878 (val != 1) ? "s" : "");
880 for (i = 0; i < val; i++)
881 sc->ciss_irq_rid[i] = i + 1;
887 /************************************************************************
888 * Setup the Performant structures.
891 ciss_init_perf(struct ciss_softc *sc)
893 struct ciss_perf_config *pc = sc->ciss_perf;
897 * Create the DMA tag for the reply queue.
899 reply_size = sizeof(uint64_t) * sc->ciss_max_requests;
900 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
901 1, 0, /* alignment, boundary */
902 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
903 BUS_SPACE_MAXADDR, /* highaddr */
904 NULL, NULL, /* filter, filterarg */
905 reply_size, 1, /* maxsize, nsegments */
906 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
908 NULL, NULL, /* lockfunc, lockarg */
909 &sc->ciss_reply_dmat)) {
910 ciss_printf(sc, "can't allocate reply DMA tag\n");
914 * Allocate memory and make it available for DMA.
916 if (bus_dmamem_alloc(sc->ciss_reply_dmat, (void **)&sc->ciss_reply,
917 BUS_DMA_NOWAIT, &sc->ciss_reply_map)) {
918 ciss_printf(sc, "can't allocate reply memory\n");
921 bus_dmamap_load(sc->ciss_reply_dmat, sc->ciss_reply_map, sc->ciss_reply,
922 reply_size, ciss_command_map_helper, &sc->ciss_reply_phys, 0);
923 bzero(sc->ciss_reply, reply_size);
925 sc->ciss_cycle = 0x1;
929 * Preload the fetch table with common command sizes. This allows the
930 * hardware to not waste bus cycles for typical i/o commands, but also not
931 * tax the driver to be too exact in choosing sizes. The table is optimized
932 * for page-aligned i/o's, but since most i/o comes from the various pagers,
933 * it's a reasonable assumption to make.
935 pc->fetch_count[CISS_SG_FETCH_NONE] = (sizeof(struct ciss_command) + 15) / 16;
936 pc->fetch_count[CISS_SG_FETCH_1] =
937 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 1 + 15) / 16;
938 pc->fetch_count[CISS_SG_FETCH_2] =
939 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 2 + 15) / 16;
940 pc->fetch_count[CISS_SG_FETCH_4] =
941 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 4 + 15) / 16;
942 pc->fetch_count[CISS_SG_FETCH_8] =
943 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 8 + 15) / 16;
944 pc->fetch_count[CISS_SG_FETCH_16] =
945 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 16 + 15) / 16;
946 pc->fetch_count[CISS_SG_FETCH_32] =
947 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 32 + 15) / 16;
948 pc->fetch_count[CISS_SG_FETCH_MAX] = (CISS_COMMAND_ALLOC_SIZE + 15) / 16;
950 pc->rq_size = sc->ciss_max_requests; /* XXX less than the card supports? */
951 pc->rq_count = 1; /* XXX Hardcode for a single queue */
954 pc->rq[0].rq_addr_hi = 0x0;
955 pc->rq[0].rq_addr_lo = sc->ciss_reply_phys;
960 /************************************************************************
961 * Wait for the adapter to come ready.
964 ciss_wait_adapter(struct ciss_softc *sc)
971 * Wait for the adapter to come ready.
973 if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
974 ciss_printf(sc, "waiting for adapter to come ready...\n");
975 for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
976 DELAY(1000000); /* one second */
978 ciss_printf(sc, "timed out waiting for adapter to come ready\n");
986 /************************************************************************
987 * Flush the adapter cache.
990 ciss_flush_adapter(struct ciss_softc *sc)
992 struct ciss_request *cr;
993 struct ciss_bmic_flush_cache *cbfc;
994 int error, command_status;
1002 * Build a BMIC request to flush the cache. We don't disable
1003 * it, as we may be going to do more I/O (eg. we are emulating
1004 * the Synchronise Cache command).
1006 if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1010 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
1011 (void **)&cbfc, sizeof(*cbfc))) != 0)
1015 * Submit the request and wait for it to complete.
1017 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1018 ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
1025 ciss_report_request(cr, &command_status, NULL);
1026 switch(command_status) {
1027 case CISS_CMD_STATUS_SUCCESS:
1030 ciss_printf(sc, "error flushing cache (%s)\n",
1031 ciss_name_command_status(command_status));
1038 free(cbfc, CISS_MALLOC_CLASS);
1040 ciss_release_request(cr);
1045 ciss_soft_reset(struct ciss_softc *sc)
1047 struct ciss_request *cr = NULL;
1048 struct ciss_command *cc;
1051 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1052 /* only reset proxy controllers */
1053 if (sc->ciss_controllers[i].physical.bus == 0)
1056 if ((error = ciss_get_request(sc, &cr)) != 0)
1059 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
1064 cc->header.address = sc->ciss_controllers[i];
1066 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
1069 ciss_release_request(cr);
1073 ciss_printf(sc, "error resetting controller (%d)\n", error);
1076 ciss_release_request(cr);
1079 /************************************************************************
1080 * Allocate memory for the adapter command structures, initialise
1081 * the request structures.
1083 * Note that the entire set of commands are allocated in a single
1087 ciss_init_requests(struct ciss_softc *sc)
1089 struct ciss_request *cr;
1095 ciss_printf(sc, "using %d of %d available commands\n",
1096 sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
1099 * Create the DMA tag for commands.
1101 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
1102 32, 0, /* alignment, boundary */
1103 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1104 BUS_SPACE_MAXADDR, /* highaddr */
1105 NULL, NULL, /* filter, filterarg */
1106 CISS_COMMAND_ALLOC_SIZE *
1107 sc->ciss_max_requests, 1, /* maxsize, nsegments */
1108 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1110 NULL, NULL, /* lockfunc, lockarg */
1111 &sc->ciss_command_dmat)) {
1112 ciss_printf(sc, "can't allocate command DMA tag\n");
1116 * Allocate memory and make it available for DMA.
1118 if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
1119 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
1120 ciss_printf(sc, "can't allocate command memory\n");
1123 bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map,sc->ciss_command,
1124 CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
1125 ciss_command_map_helper, &sc->ciss_command_phys, 0);
1126 bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
1129 * Set up the request and command structures, push requests onto
1132 for (i = 1; i < sc->ciss_max_requests; i++) {
1133 cr = &sc->ciss_request[i];
1136 cr->cr_cc = (struct ciss_command *)((uintptr_t)sc->ciss_command +
1137 CISS_COMMAND_ALLOC_SIZE * i);
1138 cr->cr_ccphys = sc->ciss_command_phys + CISS_COMMAND_ALLOC_SIZE * i;
1139 bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
1140 ciss_enqueue_free(cr);
1146 ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1151 *addr = segs[0].ds_addr;
1154 /************************************************************************
1155 * Identify the adapter, print some information about it.
1158 ciss_identify_adapter(struct ciss_softc *sc)
1160 struct ciss_request *cr;
1161 int error, command_status;
1168 * Get a request, allocate storage for the adapter data.
1170 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
1171 (void **)&sc->ciss_id,
1172 sizeof(*sc->ciss_id))) != 0)
1176 * Submit the request and wait for it to complete.
1178 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1179 ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
1186 ciss_report_request(cr, &command_status, NULL);
1187 switch(command_status) {
1188 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1190 case CISS_CMD_STATUS_DATA_UNDERRUN:
1191 case CISS_CMD_STATUS_DATA_OVERRUN:
1192 ciss_printf(sc, "data over/underrun reading adapter information\n");
1194 ciss_printf(sc, "error reading adapter information (%s)\n",
1195 ciss_name_command_status(command_status));
1200 /* sanity-check reply */
1201 if (!sc->ciss_id->big_map_supported) {
1202 ciss_printf(sc, "adapter does not support BIG_MAP\n");
1208 /* XXX later revisions may not need this */
1209 sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
1212 /* XXX only really required for old 5300 adapters? */
1213 sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
1216 * Earlier controller specs do not contain these config
1217 * entries, so assume that a 0 means its old and assign
1218 * these values to the defaults that were established
1219 * when this driver was developed for them
1221 if (sc->ciss_cfg->max_logical_supported == 0)
1222 sc->ciss_cfg->max_logical_supported = CISS_MAX_LOGICAL;
1223 if (sc->ciss_cfg->max_physical_supported == 0)
1224 sc->ciss_cfg->max_physical_supported = CISS_MAX_PHYSICAL;
1225 /* print information */
1227 ciss_printf(sc, " %d logical drive%s configured\n",
1228 sc->ciss_id->configured_logical_drives,
1229 (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
1230 ciss_printf(sc, " firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
1231 ciss_printf(sc, " %d SCSI channels\n", sc->ciss_id->scsi_bus_count);
1233 ciss_printf(sc, " signature '%.4s'\n", sc->ciss_cfg->signature);
1234 ciss_printf(sc, " valence %d\n", sc->ciss_cfg->valence);
1235 ciss_printf(sc, " supported I/O methods 0x%b\n",
1236 sc->ciss_cfg->supported_methods,
1237 "\20\1READY\2simple\3performant\4MEMQ\n");
1238 ciss_printf(sc, " active I/O method 0x%b\n",
1239 sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
1240 ciss_printf(sc, " 4G page base 0x%08x\n",
1241 sc->ciss_cfg->command_physlimit);
1242 ciss_printf(sc, " interrupt coalesce delay %dus\n",
1243 sc->ciss_cfg->interrupt_coalesce_delay);
1244 ciss_printf(sc, " interrupt coalesce count %d\n",
1245 sc->ciss_cfg->interrupt_coalesce_count);
1246 ciss_printf(sc, " max outstanding commands %d\n",
1247 sc->ciss_cfg->max_outstanding_commands);
1248 ciss_printf(sc, " bus types 0x%b\n", sc->ciss_cfg->bus_types,
1249 "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
1250 ciss_printf(sc, " server name '%.16s'\n", sc->ciss_cfg->server_name);
1251 ciss_printf(sc, " heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
1252 ciss_printf(sc, " max logical logical volumes: %d\n", sc->ciss_cfg->max_logical_supported);
1253 ciss_printf(sc, " max physical disks supported: %d\n", sc->ciss_cfg->max_physical_supported);
1254 ciss_printf(sc, " max physical disks per logical volume: %d\n", sc->ciss_cfg->max_physical_per_logical);
1259 if (sc->ciss_id != NULL) {
1260 free(sc->ciss_id, CISS_MALLOC_CLASS);
1265 ciss_release_request(cr);
1269 /************************************************************************
1270 * Helper routine for generating a list of logical and physical luns.
1272 static struct ciss_lun_report *
1273 ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
1275 struct ciss_request *cr;
1276 struct ciss_command *cc;
1277 struct ciss_report_cdb *crc;
1278 struct ciss_lun_report *cll;
1289 * Get a request, allocate storage for the address list.
1291 if ((error = ciss_get_request(sc, &cr)) != 0)
1293 report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
1294 if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1295 ciss_printf(sc, "can't allocate memory for lun report\n");
1301 * Build the Report Logical/Physical LUNs command.
1305 cr->cr_length = report_size;
1306 cr->cr_flags = CISS_REQ_DATAIN;
1308 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
1309 cc->header.address.physical.bus = 0;
1310 cc->header.address.physical.target = 0;
1311 cc->cdb.cdb_length = sizeof(*crc);
1312 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1313 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1314 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1315 cc->cdb.timeout = 30; /* XXX better suggestions? */
1317 crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
1318 bzero(crc, sizeof(*crc));
1319 crc->opcode = opcode;
1320 crc->length = htonl(report_size); /* big-endian field */
1321 cll->list_size = htonl(report_size - sizeof(*cll)); /* big-endian field */
1324 * Submit the request and wait for it to complete. (timeout
1325 * here should be much greater than above)
1327 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1328 ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
1333 * Check response. Note that data over/underrun is OK.
1335 ciss_report_request(cr, &command_status, NULL);
1336 switch(command_status) {
1337 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1338 case CISS_CMD_STATUS_DATA_UNDERRUN: /* buffer too large, not bad */
1340 case CISS_CMD_STATUS_DATA_OVERRUN:
1341 ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
1342 sc->ciss_cfg->max_logical_supported);
1345 ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
1346 ciss_name_command_status(command_status));
1350 ciss_release_request(cr);
1355 ciss_release_request(cr);
1356 if (error && cll != NULL) {
1357 free(cll, CISS_MALLOC_CLASS);
1363 /************************************************************************
1364 * Find logical drives on the adapter.
1367 ciss_init_logical(struct ciss_softc *sc)
1369 struct ciss_lun_report *cll;
1370 int error = 0, i, j;
1375 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
1376 sc->ciss_cfg->max_logical_supported);
1382 /* sanity-check reply */
1383 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1384 if ((ndrives < 0) || (ndrives > sc->ciss_cfg->max_logical_supported)) {
1385 ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
1386 ndrives, sc->ciss_cfg->max_logical_supported);
1392 * Save logical drive information.
1395 ciss_printf(sc, "%d logical drive%s\n",
1396 ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
1400 malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
1401 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1402 if (sc->ciss_logical == NULL) {
1407 for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1408 sc->ciss_logical[i] =
1409 malloc(sc->ciss_cfg->max_logical_supported *
1410 sizeof(struct ciss_ldrive),
1411 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1412 if (sc->ciss_logical[i] == NULL) {
1417 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++)
1418 sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
1422 for (i = 0; i < sc->ciss_cfg->max_logical_supported; i++) {
1424 struct ciss_ldrive *ld;
1427 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
1428 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
1429 ld = &sc->ciss_logical[bus][target];
1431 ld->cl_address = cll->lun[i];
1432 ld->cl_controller = &sc->ciss_controllers[bus];
1433 if (ciss_identify_logical(sc, ld) != 0)
1436 * If the drive has had media exchanged, we should bring it online.
1438 if (ld->cl_lstatus->media_exchanged)
1439 ciss_accept_media(sc, ld);
1446 free(cll, CISS_MALLOC_CLASS);
1451 ciss_init_physical(struct ciss_softc *sc)
1453 struct ciss_lun_report *cll;
1463 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
1464 sc->ciss_cfg->max_physical_supported);
1470 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1473 ciss_printf(sc, "%d physical device%s\n",
1474 nphys, (nphys > 1 || nphys == 0) ? "s" : "");
1478 * Figure out the bus mapping.
1479 * Logical buses include both the local logical bus for local arrays and
1480 * proxy buses for remote arrays. Physical buses are numbered by the
1481 * controller and represent physical buses that hold physical devices.
1482 * We shift these bus numbers so that everything fits into a single flat
1483 * numbering space for CAM. Logical buses occupy the first 32 CAM bus
1484 * numbers, and the physical bus numbers are shifted to be above that.
1485 * This results in the various driver arrays being indexed as follows:
1487 * ciss_controllers[] - indexed by logical bus
1488 * ciss_cam_sim[] - indexed by both logical and physical, with physical
1489 * being shifted by 32.
1490 * ciss_logical[][] - indexed by logical bus
1491 * ciss_physical[][] - indexed by physical bus
1493 * XXX This is getting more and more hackish. CISS really doesn't play
1494 * well with a standard SCSI model; devices are addressed via magic
1495 * cookies, not via b/t/l addresses. Since there is no way to store
1496 * the cookie in the CAM device object, we have to keep these lookup
1497 * tables handy so that the devices can be found quickly at the cost
1498 * of wasting memory and having a convoluted lookup scheme. This
1499 * driver should probably be converted to block interface.
1502 * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
1503 * controller. A proxy controller is another physical controller
1504 * behind the primary PCI controller. We need to know about this
1505 * so that BMIC commands can be properly targeted. There can be
1506 * proxy controllers attached to a single PCI controller, so
1507 * find the highest numbered one so the array can be properly
1510 sc->ciss_max_logical_bus = 1;
1511 for (i = 0; i < nphys; i++) {
1512 if (cll->lun[i].physical.extra_address == 0) {
1513 bus = cll->lun[i].physical.bus;
1514 sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
1516 bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
1517 sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
1521 sc->ciss_controllers =
1522 malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
1523 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1525 if (sc->ciss_controllers == NULL) {
1526 ciss_printf(sc, "Could not allocate memory for controller map\n");
1531 /* setup a map of controller addresses */
1532 for (i = 0; i < nphys; i++) {
1533 if (cll->lun[i].physical.extra_address == 0) {
1534 sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
1539 malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
1540 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1541 if (sc->ciss_physical == NULL) {
1542 ciss_printf(sc, "Could not allocate memory for physical device map\n");
1547 for (i = 0; i < sc->ciss_max_physical_bus; i++) {
1548 sc->ciss_physical[i] =
1549 malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
1550 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1551 if (sc->ciss_physical[i] == NULL) {
1552 ciss_printf(sc, "Could not allocate memory for target map\n");
1558 ciss_filter_physical(sc, cll);
1562 free(cll, CISS_MALLOC_CLASS);
1568 ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
1574 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1575 for (i = 0; i < nphys; i++) {
1576 if (cll->lun[i].physical.extra_address == 0)
1580 * Filter out devices that we don't want. Level 3 LUNs could
1581 * probably be supported, but the docs don't give enough of a
1584 * The mode field of the physical address is likely set to have
1585 * hard disks masked out. Honor it unless the user has overridden
1586 * us with the tunable. We also munge the inquiry data for these
1587 * disks so that they only show up as passthrough devices. Keeping
1588 * them visible in this fashion is useful for doing things like
1589 * flashing firmware.
1591 ea = cll->lun[i].physical.extra_address;
1592 if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
1593 (CISS_EXTRA_MODE2(ea) == 0x3))
1595 if ((ciss_expose_hidden_physical == 0) &&
1596 (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
1600 * Note: CISS firmware numbers physical busses starting at '1', not
1601 * '0'. This numbering is internal to the firmware and is only
1602 * used as a hint here.
1604 bus = CISS_EXTRA_BUS2(ea) - 1;
1605 target = CISS_EXTRA_TARGET2(ea);
1606 sc->ciss_physical[bus][target].cp_address = cll->lun[i];
1607 sc->ciss_physical[bus][target].cp_online = 1;
1614 ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1616 struct ciss_request *cr;
1617 struct ciss_command *cc;
1618 struct scsi_inquiry *inq;
1624 bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1626 if ((error = ciss_get_request(sc, &cr)) != 0)
1630 cr->cr_data = &ld->cl_geometry;
1631 cr->cr_length = sizeof(ld->cl_geometry);
1632 cr->cr_flags = CISS_REQ_DATAIN;
1634 cc->header.address = ld->cl_address;
1635 cc->cdb.cdb_length = 6;
1636 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1637 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1638 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1639 cc->cdb.timeout = 30;
1641 inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1642 inq->opcode = INQUIRY;
1643 inq->byte2 = SI_EVPD;
1644 inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1645 scsi_ulto2b(sizeof(ld->cl_geometry), inq->length);
1647 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1648 ciss_printf(sc, "error getting geometry (%d)\n", error);
1652 ciss_report_request(cr, &command_status, NULL);
1653 switch(command_status) {
1654 case CISS_CMD_STATUS_SUCCESS:
1655 case CISS_CMD_STATUS_DATA_UNDERRUN:
1657 case CISS_CMD_STATUS_DATA_OVERRUN:
1658 ciss_printf(sc, "WARNING: Data overrun\n");
1661 ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1662 ciss_name_command_status(command_status));
1668 ciss_release_request(cr);
1671 /************************************************************************
1672 * Identify a logical drive, initialise state related to it.
1675 ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1677 struct ciss_request *cr;
1678 struct ciss_command *cc;
1679 struct ciss_bmic_cdb *cbc;
1680 int error, command_status;
1687 * Build a BMIC request to fetch the drive ID.
1689 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
1690 (void **)&ld->cl_ldrive,
1691 sizeof(*ld->cl_ldrive))) != 0)
1694 cc->header.address = *ld->cl_controller; /* target controller */
1695 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1696 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1699 * Submit the request and wait for it to complete.
1701 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1702 ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
1709 ciss_report_request(cr, &command_status, NULL);
1710 switch(command_status) {
1711 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1713 case CISS_CMD_STATUS_DATA_UNDERRUN:
1714 case CISS_CMD_STATUS_DATA_OVERRUN:
1715 ciss_printf(sc, "data over/underrun reading logical drive ID\n");
1717 ciss_printf(sc, "error reading logical drive ID (%s)\n",
1718 ciss_name_command_status(command_status));
1722 ciss_release_request(cr);
1726 * Build a CISS BMIC command to get the logical drive status.
1728 if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
1732 * Get the logical drive geometry.
1734 if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1738 * Print the drive's basic characteristics.
1741 ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
1742 CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
1743 CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
1744 ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
1745 ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
1746 ld->cl_ldrive->block_size));
1748 ciss_print_ldrive(sc, ld);
1752 /* make the drive not-exist */
1753 ld->cl_status = CISS_LD_NONEXISTENT;
1754 if (ld->cl_ldrive != NULL) {
1755 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
1756 ld->cl_ldrive = NULL;
1758 if (ld->cl_lstatus != NULL) {
1759 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
1760 ld->cl_lstatus = NULL;
1764 ciss_release_request(cr);
1769 /************************************************************************
1770 * Get status for a logical drive.
1772 * XXX should we also do this in response to Test Unit Ready?
1775 ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld)
1777 struct ciss_request *cr;
1778 struct ciss_command *cc;
1779 struct ciss_bmic_cdb *cbc;
1780 int error, command_status;
1783 * Build a CISS BMIC command to get the logical drive status.
1785 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
1786 (void **)&ld->cl_lstatus,
1787 sizeof(*ld->cl_lstatus))) != 0)
1790 cc->header.address = *ld->cl_controller; /* target controller */
1791 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1792 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1795 * Submit the request and wait for it to complete.
1797 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1798 ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1805 ciss_report_request(cr, &command_status, NULL);
1806 switch(command_status) {
1807 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1809 case CISS_CMD_STATUS_DATA_UNDERRUN:
1810 case CISS_CMD_STATUS_DATA_OVERRUN:
1811 ciss_printf(sc, "data over/underrun reading logical drive status\n");
1813 ciss_printf(sc, "error reading logical drive status (%s)\n",
1814 ciss_name_command_status(command_status));
1820 * Set the drive's summary status based on the returned status.
1822 * XXX testing shows that a failed JBOD drive comes back at next
1823 * boot in "queued for expansion" mode. WTF?
1825 ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
1829 ciss_release_request(cr);
1833 /************************************************************************
1834 * Notify the adapter of a config update.
1837 ciss_update_config(struct ciss_softc *sc)
1843 CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
1844 for (i = 0; i < 1000; i++) {
1845 if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
1846 CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
1854 /************************************************************************
1855 * Accept new media into a logical drive.
1857 * XXX The drive has previously been offline; it would be good if we
1858 * could make sure it's not open right now.
1861 ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
1863 struct ciss_request *cr;
1864 struct ciss_command *cc;
1865 struct ciss_bmic_cdb *cbc;
1867 int error = 0, ldrive;
1869 ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1871 debug(0, "bringing logical drive %d back online", ldrive);
1874 * Build a CISS BMIC command to bring the drive back online.
1876 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
1880 cc->header.address = *ld->cl_controller; /* target controller */
1881 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1882 cbc->log_drive = ldrive;
1885 * Submit the request and wait for it to complete.
1887 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1888 ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
1895 ciss_report_request(cr, &command_status, NULL);
1896 switch(command_status) {
1897 case CISS_CMD_STATUS_SUCCESS: /* all OK */
1898 /* we should get a logical drive status changed event here */
1901 ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
1902 ciss_name_command_status(command_status));
1908 ciss_release_request(cr);
1912 /************************************************************************
1913 * Release adapter resources.
1916 ciss_free(struct ciss_softc *sc)
1918 struct ciss_request *cr;
1923 /* we're going away */
1924 sc->ciss_flags |= CISS_FLAG_ABORTING;
1926 /* terminate the periodic heartbeat routine */
1927 callout_stop(&sc->ciss_periodic);
1929 /* cancel the Event Notify chain */
1930 ciss_notify_abort(sc);
1932 ciss_kill_notify_thread(sc);
1934 /* disconnect from CAM */
1935 if (sc->ciss_cam_sim) {
1936 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1937 if (sc->ciss_cam_sim[i]) {
1938 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1939 cam_sim_free(sc->ciss_cam_sim[i], 0);
1942 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
1943 CISS_PHYSICAL_BASE; i++) {
1944 if (sc->ciss_cam_sim[i]) {
1945 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1946 cam_sim_free(sc->ciss_cam_sim[i], 0);
1949 free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
1951 if (sc->ciss_cam_devq)
1952 cam_simq_free(sc->ciss_cam_devq);
1954 /* remove the control device */
1955 mtx_unlock(&sc->ciss_mtx);
1956 if (sc->ciss_dev_t != NULL)
1957 destroy_dev(sc->ciss_dev_t);
1959 /* Final cleanup of the callout. */
1960 callout_drain(&sc->ciss_periodic);
1961 mtx_destroy(&sc->ciss_mtx);
1963 /* free the controller data */
1964 if (sc->ciss_id != NULL)
1965 free(sc->ciss_id, CISS_MALLOC_CLASS);
1967 /* release I/O resources */
1968 if (sc->ciss_regs_resource != NULL)
1969 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1970 sc->ciss_regs_rid, sc->ciss_regs_resource);
1971 if (sc->ciss_cfg_resource != NULL)
1972 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1973 sc->ciss_cfg_rid, sc->ciss_cfg_resource);
1974 if (sc->ciss_intr != NULL)
1975 bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
1976 if (sc->ciss_irq_resource != NULL)
1977 bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
1978 sc->ciss_irq_rid[0], sc->ciss_irq_resource);
1980 pci_release_msi(sc->ciss_dev);
1982 while ((cr = ciss_dequeue_free(sc)) != NULL)
1983 bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
1984 if (sc->ciss_buffer_dmat)
1985 bus_dma_tag_destroy(sc->ciss_buffer_dmat);
1987 /* destroy command memory and DMA tag */
1988 if (sc->ciss_command != NULL) {
1989 bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
1990 bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
1992 if (sc->ciss_command_dmat)
1993 bus_dma_tag_destroy(sc->ciss_command_dmat);
1995 if (sc->ciss_reply) {
1996 bus_dmamap_unload(sc->ciss_reply_dmat, sc->ciss_reply_map);
1997 bus_dmamem_free(sc->ciss_reply_dmat, sc->ciss_reply, sc->ciss_reply_map);
1999 if (sc->ciss_reply_dmat)
2000 bus_dma_tag_destroy(sc->ciss_reply_dmat);
2002 /* destroy DMA tags */
2003 if (sc->ciss_parent_dmat)
2004 bus_dma_tag_destroy(sc->ciss_parent_dmat);
2005 if (sc->ciss_logical) {
2006 for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
2007 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
2008 if (sc->ciss_logical[i][j].cl_ldrive)
2009 free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
2010 if (sc->ciss_logical[i][j].cl_lstatus)
2011 free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
2013 free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
2015 free(sc->ciss_logical, CISS_MALLOC_CLASS);
2018 if (sc->ciss_physical) {
2019 for (i = 0; i < sc->ciss_max_physical_bus; i++)
2020 free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
2021 free(sc->ciss_physical, CISS_MALLOC_CLASS);
2024 if (sc->ciss_controllers)
2025 free(sc->ciss_controllers, CISS_MALLOC_CLASS);
2029 /************************************************************************
2030 * Give a command to the adapter.
2032 * Note that this uses the simple transport layer directly. If we
2033 * want to add support for other layers, we'll need a switch of some
2036 * Note that the simple transport layer has no way of refusing a
2037 * command; we only have as many request structures as the adapter
2038 * supports commands, so we don't have to check (this presumes that
2039 * the adapter can handle commands as fast as we throw them at it).
2042 ciss_start(struct ciss_request *cr)
2044 struct ciss_command *cc; /* XXX debugging only */
2048 debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
2051 * Map the request's data.
2053 if ((error = ciss_map_request(cr)))
2057 ciss_print_request(cr);
2063 /************************************************************************
2064 * Fetch completed request(s) from the adapter, queue them for
2065 * completion handling.
2067 * Note that this uses the simple transport layer directly. If we
2068 * want to add support for other layers, we'll need a switch of some
2071 * Note that the simple transport mechanism does not require any
2072 * reentrancy protection; the OPQ read is atomic. If there is a
2073 * chance of a race with something else that might move the request
2074 * off the busy list, then we will have to lock against that
2075 * (eg. timeouts, etc.)
2078 ciss_done(struct ciss_softc *sc, cr_qhead_t *qh)
2080 struct ciss_request *cr;
2081 struct ciss_command *cc;
2082 u_int32_t tag, index;
2087 * Loop quickly taking requests from the adapter and moving them
2088 * to the completed queue.
2092 tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
2093 if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
2096 debug(2, "completed command %d%s", index,
2097 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2098 if (index >= sc->ciss_max_requests) {
2099 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2102 cr = &(sc->ciss_request[index]);
2104 cc->header.host_tag = tag; /* not updated by adapter */
2105 ciss_enqueue_complete(cr, qh);
2111 ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh)
2113 struct ciss_request *cr;
2114 struct ciss_command *cc;
2115 u_int32_t tag, index;
2120 * Loop quickly taking requests from the adapter and moving them
2121 * to the completed queue.
2124 tag = sc->ciss_reply[sc->ciss_rqidx];
2125 if ((tag & CISS_CYCLE_MASK) != sc->ciss_cycle)
2128 debug(2, "completed command %d%s\n", index,
2129 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2130 if (index < sc->ciss_max_requests) {
2131 cr = &(sc->ciss_request[index]);
2133 cc->header.host_tag = tag; /* not updated by adapter */
2134 ciss_enqueue_complete(cr, qh);
2136 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2138 if (++sc->ciss_rqidx == sc->ciss_max_requests) {
2140 sc->ciss_cycle ^= 1;
2146 /************************************************************************
2147 * Take an interrupt from the adapter.
2150 ciss_intr(void *arg)
2153 struct ciss_softc *sc = (struct ciss_softc *)arg;
2156 * The only interrupt we recognise indicates that there are
2157 * entries in the outbound post queue.
2161 mtx_lock(&sc->ciss_mtx);
2162 ciss_complete(sc, &qh);
2163 mtx_unlock(&sc->ciss_mtx);
2167 ciss_perf_intr(void *arg)
2169 struct ciss_softc *sc = (struct ciss_softc *)arg;
2171 /* Clear the interrupt and flush the bridges. Docs say that the flush
2172 * needs to be done twice, which doesn't seem right.
2174 CISS_TL_PERF_CLEAR_INT(sc);
2175 CISS_TL_PERF_FLUSH_INT(sc);
2177 ciss_perf_msi_intr(sc);
2181 ciss_perf_msi_intr(void *arg)
2184 struct ciss_softc *sc = (struct ciss_softc *)arg;
2187 ciss_perf_done(sc, &qh);
2188 mtx_lock(&sc->ciss_mtx);
2189 ciss_complete(sc, &qh);
2190 mtx_unlock(&sc->ciss_mtx);
2194 /************************************************************************
2195 * Process completed requests.
2197 * Requests can be completed in three fashions:
2199 * - by invoking a callback function (cr_complete is non-null)
2200 * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
2201 * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
2204 ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh)
2206 struct ciss_request *cr;
2211 * Loop taking requests off the completed queue and performing
2212 * completion processing on them.
2215 if ((cr = ciss_dequeue_complete(sc, qh)) == NULL)
2217 ciss_unmap_request(cr);
2219 if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
2220 ciss_printf(sc, "WARNING: completing non-busy request\n");
2221 cr->cr_flags &= ~CISS_REQ_BUSY;
2224 * If the request has a callback, invoke it.
2226 if (cr->cr_complete != NULL) {
2227 cr->cr_complete(cr);
2232 * If someone is sleeping on this request, wake them up.
2234 if (cr->cr_flags & CISS_REQ_SLEEP) {
2235 cr->cr_flags &= ~CISS_REQ_SLEEP;
2241 * If someone is polling this request for completion, signal.
2243 if (cr->cr_flags & CISS_REQ_POLL) {
2244 cr->cr_flags &= ~CISS_REQ_POLL;
2249 * Give up and throw the request back on the free queue. This
2250 * should never happen; resources will probably be lost.
2252 ciss_printf(sc, "WARNING: completed command with no submitter\n");
2253 ciss_enqueue_free(cr);
2257 /************************************************************************
2258 * Report on the completion status of a request, and pass back SCSI
2259 * and command status values.
2262 _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func)
2264 struct ciss_command *cc;
2265 struct ciss_error_info *ce;
2270 ce = (struct ciss_error_info *)&(cc->sg[0]);
2273 * We don't consider data under/overrun an error for the Report
2274 * Logical/Physical LUNs commands.
2276 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
2277 ((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
2278 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
2279 ((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
2280 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
2281 (cc->cdb.cdb[0] == INQUIRY))) {
2282 cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
2283 debug(2, "ignoring irrelevant under/overrun error");
2287 * Check the command's error bit, if clear, there's no status and
2290 if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
2291 if (scsi_status != NULL)
2292 *scsi_status = SCSI_STATUS_OK;
2293 if (command_status != NULL)
2294 *command_status = CISS_CMD_STATUS_SUCCESS;
2297 if (command_status != NULL)
2298 *command_status = ce->command_status;
2299 if (scsi_status != NULL) {
2300 if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
2301 *scsi_status = ce->scsi_status;
2307 ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
2308 ce->command_status, ciss_name_command_status(ce->command_status),
2310 if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
2311 ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x, function %s\n",
2312 ce->additional_error_info.invalid_command.offense_size,
2313 ce->additional_error_info.invalid_command.offense_offset,
2314 ce->additional_error_info.invalid_command.offense_value,
2319 ciss_print_request(cr);
2324 /************************************************************************
2325 * Issue a request and don't return until it's completed.
2327 * Depending on adapter status, we may poll or sleep waiting for
2331 ciss_synch_request(struct ciss_request *cr, int timeout)
2333 if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
2334 return(ciss_wait_request(cr, timeout));
2336 return(ciss_poll_request(cr, timeout));
2340 /************************************************************************
2341 * Issue a request and poll for completion.
2343 * Timeout in milliseconds.
2346 ciss_poll_request(struct ciss_request *cr, int timeout)
2349 struct ciss_softc *sc;
2356 cr->cr_flags |= CISS_REQ_POLL;
2357 if ((error = ciss_start(cr)) != 0)
2362 ciss_perf_done(sc, &qh);
2365 ciss_complete(sc, &qh);
2366 if (!(cr->cr_flags & CISS_REQ_POLL))
2369 } while (timeout-- >= 0);
2370 return(EWOULDBLOCK);
2373 /************************************************************************
2374 * Issue a request and sleep waiting for completion.
2376 * Timeout in milliseconds. Note that a spurious wakeup will reset
2380 ciss_wait_request(struct ciss_request *cr, int timeout)
2386 cr->cr_flags |= CISS_REQ_SLEEP;
2387 if ((error = ciss_start(cr)) != 0)
2390 while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
2391 error = msleep(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ", (timeout * hz) / 1000);
2397 /************************************************************************
2398 * Abort a request. Note that a potential exists here to race the
2399 * request being completed; the caller must deal with this.
2402 ciss_abort_request(struct ciss_request *ar)
2404 struct ciss_request *cr;
2405 struct ciss_command *cc;
2406 struct ciss_message_cdb *cmc;
2412 if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
2415 /* build the abort command */
2417 cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL; /* addressing? */
2418 cc->header.address.physical.target = 0;
2419 cc->header.address.physical.bus = 0;
2420 cc->cdb.cdb_length = sizeof(*cmc);
2421 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
2422 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2423 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2424 cc->cdb.timeout = 30;
2426 cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
2427 cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
2428 cmc->type = CISS_MESSAGE_ABORT_TASK;
2429 cmc->abort_tag = ar->cr_tag; /* endianness?? */
2432 * Send the request and wait for a response. If we believe we
2433 * aborted the request OK, clear the flag that indicates it's
2436 error = ciss_synch_request(cr, 35 * 1000);
2438 error = ciss_report_request(cr, NULL, NULL);
2439 ciss_release_request(cr);
2446 /************************************************************************
2447 * Fetch and initialise a request
2450 ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
2452 struct ciss_request *cr;
2457 * Get a request and clean it up.
2459 if ((cr = ciss_dequeue_free(sc)) == NULL)
2464 cr->cr_complete = NULL;
2465 cr->cr_private = NULL;
2466 cr->cr_sg_tag = CISS_SG_MAX; /* Backstop to prevent accidents */
2468 ciss_preen_command(cr);
2474 ciss_preen_command(struct ciss_request *cr)
2476 struct ciss_command *cc;
2480 * Clean up the command structure.
2482 * Note that we set up the error_info structure here, since the
2483 * length can be overwritten by any command.
2486 cc->header.sg_in_list = 0; /* kinda inefficient this way */
2487 cc->header.sg_total = 0;
2488 cc->header.host_tag = cr->cr_tag << 2;
2489 cc->header.host_tag_zeroes = 0;
2490 bzero(&(cc->sg[0]), CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command));
2491 cmdphys = cr->cr_ccphys;
2492 cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
2493 cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
2496 /************************************************************************
2497 * Release a request to the free list.
2500 ciss_release_request(struct ciss_request *cr)
2502 struct ciss_softc *sc;
2508 /* release the request to the free queue */
2509 ciss_requeue_free(cr);
2512 /************************************************************************
2513 * Allocate a request that will be used to send a BMIC command. Do some
2514 * of the common setup here to avoid duplicating it everywhere else.
2517 ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
2518 int opcode, void **bufp, size_t bufsize)
2520 struct ciss_request *cr;
2521 struct ciss_command *cc;
2522 struct ciss_bmic_cdb *cbc;
2535 if ((error = ciss_get_request(sc, &cr)) != 0)
2539 * Allocate data storage if requested, determine the data direction.
2542 if ((bufsize > 0) && (bufp != NULL)) {
2543 if (*bufp == NULL) {
2544 if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
2550 dataout = 1; /* we are given a buffer, so we are writing */
2555 * Build a CISS BMIC command to get the logical drive ID.
2558 cr->cr_length = bufsize;
2560 cr->cr_flags = CISS_REQ_DATAIN;
2563 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2564 cc->header.address.physical.bus = 0;
2565 cc->header.address.physical.target = 0;
2566 cc->cdb.cdb_length = sizeof(*cbc);
2567 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2568 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2569 cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
2570 cc->cdb.timeout = 0;
2572 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
2573 bzero(cbc, sizeof(*cbc));
2574 cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
2575 cbc->bmic_opcode = opcode;
2576 cbc->size = htons((u_int16_t)bufsize);
2581 ciss_release_request(cr);
2584 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
2590 /************************************************************************
2591 * Handle a command passed in from userspace.
2594 ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
2596 struct ciss_request *cr;
2597 struct ciss_command *cc;
2598 struct ciss_error_info *ce;
2608 while (ciss_get_request(sc, &cr) != 0)
2609 msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
2613 * Allocate an in-kernel databuffer if required, copy in user data.
2615 mtx_unlock(&sc->ciss_mtx);
2616 cr->cr_length = ioc->buf_size;
2617 if (ioc->buf_size > 0) {
2618 if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
2622 if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
2623 debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2629 * Build the request based on the user command.
2631 bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
2632 bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
2634 /* XXX anything else to populate here? */
2635 mtx_lock(&sc->ciss_mtx);
2640 if ((error = ciss_synch_request(cr, 60 * 1000))) {
2641 debug(0, "request failed - %d", error);
2646 * Check to see if the command succeeded.
2648 ce = (struct ciss_error_info *)&(cc->sg[0]);
2649 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
2650 bzero(ce, sizeof(*ce));
2653 * Copy the results back to the user.
2655 bcopy(ce, &ioc->error_info, sizeof(*ce));
2656 mtx_unlock(&sc->ciss_mtx);
2657 if ((ioc->buf_size > 0) &&
2658 (error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
2659 debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2667 mtx_lock(&sc->ciss_mtx);
2670 if ((cr != NULL) && (cr->cr_data != NULL))
2671 free(cr->cr_data, CISS_MALLOC_CLASS);
2673 ciss_release_request(cr);
2677 /************************************************************************
2678 * Map a request into bus-visible space, initialise the scatter/gather
2682 ciss_map_request(struct ciss_request *cr)
2684 struct ciss_softc *sc;
2691 /* check that mapping is necessary */
2692 if (cr->cr_flags & CISS_REQ_MAPPED)
2695 cr->cr_flags |= CISS_REQ_MAPPED;
2697 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2698 BUS_DMASYNC_PREWRITE);
2700 if (cr->cr_data != NULL) {
2701 if (cr->cr_flags & CISS_REQ_CCB)
2702 error = bus_dmamap_load_ccb(sc->ciss_buffer_dmat,
2703 cr->cr_datamap, cr->cr_data,
2704 ciss_request_map_helper, cr, 0);
2706 error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2707 cr->cr_data, cr->cr_length,
2708 ciss_request_map_helper, cr, 0);
2713 * Post the command to the adapter.
2715 cr->cr_sg_tag = CISS_SG_NONE;
2716 cr->cr_flags |= CISS_REQ_BUSY;
2718 CISS_TL_PERF_POST_CMD(sc, cr);
2720 CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2727 ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2729 struct ciss_command *cc;
2730 struct ciss_request *cr;
2731 struct ciss_softc *sc;
2736 cr = (struct ciss_request *)arg;
2740 for (i = 0; i < nseg; i++) {
2741 cc->sg[i].address = segs[i].ds_addr;
2742 cc->sg[i].length = segs[i].ds_len;
2743 cc->sg[i].extension = 0;
2745 /* we leave the s/g table entirely within the command */
2746 cc->header.sg_in_list = nseg;
2747 cc->header.sg_total = nseg;
2749 if (cr->cr_flags & CISS_REQ_DATAIN)
2750 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2751 if (cr->cr_flags & CISS_REQ_DATAOUT)
2752 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2755 cr->cr_sg_tag = CISS_SG_NONE;
2757 cr->cr_sg_tag = CISS_SG_1;
2759 cr->cr_sg_tag = CISS_SG_2;
2761 cr->cr_sg_tag = CISS_SG_4;
2763 cr->cr_sg_tag = CISS_SG_8;
2764 else if (nseg <= 16)
2765 cr->cr_sg_tag = CISS_SG_16;
2766 else if (nseg <= 32)
2767 cr->cr_sg_tag = CISS_SG_32;
2769 cr->cr_sg_tag = CISS_SG_MAX;
2772 * Post the command to the adapter.
2774 cr->cr_flags |= CISS_REQ_BUSY;
2776 CISS_TL_PERF_POST_CMD(sc, cr);
2778 CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2781 /************************************************************************
2782 * Unmap a request from bus-visible space.
2785 ciss_unmap_request(struct ciss_request *cr)
2787 struct ciss_softc *sc;
2793 /* check that unmapping is necessary */
2794 if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
2797 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2798 BUS_DMASYNC_POSTWRITE);
2800 if (cr->cr_data == NULL)
2803 if (cr->cr_flags & CISS_REQ_DATAIN)
2804 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
2805 if (cr->cr_flags & CISS_REQ_DATAOUT)
2806 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
2808 bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2810 cr->cr_flags &= ~CISS_REQ_MAPPED;
2813 /************************************************************************
2814 * Attach the driver to CAM.
2816 * We put all the logical drives on a single SCSI bus.
2819 ciss_cam_init(struct ciss_softc *sc)
2826 * Allocate a devq. We can reuse this for the masked physical
2827 * devices if we decide to export these as well.
2829 if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests - 2)) == NULL) {
2830 ciss_printf(sc, "can't allocate CAM SIM queue\n");
2837 * This naturally wastes a bit of memory. The alternative is to allocate
2838 * and register each bus as it is found, and then track them on a linked
2839 * list. Unfortunately, the driver has a few places where it needs to
2840 * look up the SIM based solely on bus number, and it's unclear whether
2841 * a list traversal would work for these situations.
2843 maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
2844 CISS_PHYSICAL_BASE);
2845 sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
2846 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
2847 if (sc->ciss_cam_sim == NULL) {
2848 ciss_printf(sc, "can't allocate memory for controller SIM\n");
2852 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2853 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2855 device_get_unit(sc->ciss_dev),
2858 sc->ciss_max_requests - 2,
2859 sc->ciss_cam_devq)) == NULL) {
2860 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2865 * Register bus with this SIM.
2867 mtx_lock(&sc->ciss_mtx);
2868 if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) {
2869 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2870 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2871 mtx_unlock(&sc->ciss_mtx);
2875 mtx_unlock(&sc->ciss_mtx);
2878 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
2879 CISS_PHYSICAL_BASE; i++) {
2880 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2882 device_get_unit(sc->ciss_dev),
2884 sc->ciss_max_requests - 2,
2885 sc->ciss_cam_devq)) == NULL) {
2886 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2890 mtx_lock(&sc->ciss_mtx);
2891 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2892 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2893 mtx_unlock(&sc->ciss_mtx);
2896 mtx_unlock(&sc->ciss_mtx);
2902 /************************************************************************
2903 * Initiate a rescan of the 'logical devices' SIM
2906 ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
2912 if ((ccb = xpt_alloc_ccb_nowait()) == NULL) {
2913 ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
2917 if (xpt_create_path(&ccb->ccb_h.path, NULL,
2918 cam_sim_path(sc->ciss_cam_sim[bus]),
2919 target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2920 ciss_printf(sc, "rescan failed (can't create path)\n");
2925 /* scan is now in progress */
2928 /************************************************************************
2929 * Handle requests coming from CAM
2932 ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
2934 struct ciss_softc *sc;
2935 struct ccb_scsiio *csio;
2939 sc = cam_sim_softc(sim);
2940 bus = cam_sim_bus(sim);
2941 csio = (struct ccb_scsiio *)&ccb->csio;
2942 target = csio->ccb_h.target_id;
2943 physical = CISS_IS_PHYSICAL(bus);
2945 switch (ccb->ccb_h.func_code) {
2947 /* perform SCSI I/O */
2949 if (!ciss_cam_action_io(sim, csio))
2953 /* perform geometry calculations */
2954 case XPT_CALC_GEOMETRY:
2956 struct ccb_calc_geometry *ccg = &ccb->ccg;
2957 struct ciss_ldrive *ld;
2959 debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2963 ld = &sc->ciss_logical[bus][target];
2966 * Use the cached geometry settings unless the fault tolerance
2969 if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
2970 u_int32_t secs_per_cylinder;
2973 ccg->secs_per_track = 32;
2974 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2975 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2977 ccg->heads = ld->cl_geometry.heads;
2978 ccg->secs_per_track = ld->cl_geometry.sectors;
2979 ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2981 ccb->ccb_h.status = CAM_REQ_CMP;
2985 /* handle path attribute inquiry */
2988 struct ccb_pathinq *cpi = &ccb->cpi;
2991 debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2993 cpi->version_num = 1;
2994 cpi->hba_inquiry = PI_TAG_ABLE; /* XXX is this correct? */
2995 cpi->target_sprt = 0;
2997 cpi->max_target = sc->ciss_cfg->max_logical_supported;
2998 cpi->max_lun = 0; /* 'logical drive' channel only */
2999 cpi->initiator_id = sc->ciss_cfg->max_logical_supported;
3000 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3001 strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
3002 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3003 cpi->unit_number = cam_sim_unit(sim);
3004 cpi->bus_id = cam_sim_bus(sim);
3005 cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */
3006 cpi->transport = XPORT_SPI;
3007 cpi->transport_version = 2;
3008 cpi->protocol = PROTO_SCSI;
3009 cpi->protocol_version = SCSI_REV_2;
3010 if (sc->ciss_cfg->max_sg_length == 0) {
3013 /* XXX Fix for ZMR cards that advertise max_sg_length == 32
3014 * Confusing bit here. max_sg_length is usually a power of 2. We always
3015 * need to subtract 1 to account for partial pages. Then we need to
3016 * align on a valid PAGE_SIZE so we round down to the nearest power of 2.
3017 * Add 1 so we can then subtract it out in the assignment to maxio.
3018 * The reason for all these shenanigans is to create a maxio value that
3019 * creates IO operations to volumes that yield consistent operations
3020 * with good performance.
3022 sg_length = sc->ciss_cfg->max_sg_length - 1;
3023 sg_length = (1 << (fls(sg_length) - 1)) + 1;
3025 cpi->maxio = (min(CISS_MAX_SG_ELEMENTS, sg_length) - 1) * PAGE_SIZE;
3026 ccb->ccb_h.status = CAM_REQ_CMP;
3030 case XPT_GET_TRAN_SETTINGS:
3032 struct ccb_trans_settings *cts = &ccb->cts;
3034 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3035 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3037 bus = cam_sim_bus(sim);
3038 target = cts->ccb_h.target_id;
3040 debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
3041 /* disconnect always OK */
3042 cts->protocol = PROTO_SCSI;
3043 cts->protocol_version = SCSI_REV_2;
3044 cts->transport = XPORT_SPI;
3045 cts->transport_version = 2;
3047 spi->valid = CTS_SPI_VALID_DISC;
3048 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
3050 scsi->valid = CTS_SCSI_VALID_TQ;
3051 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3053 cts->ccb_h.status = CAM_REQ_CMP;
3057 default: /* we can't do this */
3058 debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
3059 ccb->ccb_h.status = CAM_REQ_INVALID;
3066 /************************************************************************
3067 * Handle a CAM SCSI I/O request.
3070 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
3072 struct ciss_softc *sc;
3074 struct ciss_request *cr;
3075 struct ciss_command *cc;
3078 sc = cam_sim_softc(sim);
3079 bus = cam_sim_bus(sim);
3080 target = csio->ccb_h.target_id;
3082 debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
3084 /* check that the CDB pointer is not to a physical address */
3085 if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
3086 debug(3, " CDB pointer is to physical address");
3087 csio->ccb_h.status = CAM_REQ_CMP_ERR;
3090 /* abandon aborted ccbs or those that have failed validation */
3091 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
3092 debug(3, "abandoning CCB due to abort/validation failure");
3096 /* handle emulation of some SCSI commands ourself */
3097 if (ciss_cam_emulate(sc, csio))
3101 * Get a request to manage this command. If we can't, return the
3102 * ccb, freeze the queue and flag so that we unfreeze it when a
3103 * request completes.
3105 if ((error = ciss_get_request(sc, &cr)) != 0) {
3106 xpt_freeze_simq(sim, 1);
3107 sc->ciss_flags |= CISS_FLAG_BUSY;
3108 csio->ccb_h.status |= CAM_REQUEUE_REQ;
3113 * Build the command.
3117 cr->cr_length = csio->dxfer_len;
3118 cr->cr_complete = ciss_cam_complete;
3119 cr->cr_private = csio;
3122 * Target the right logical volume.
3124 if (CISS_IS_PHYSICAL(bus))
3125 cc->header.address =
3126 sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
3128 cc->header.address =
3129 sc->ciss_logical[bus][target].cl_address;
3130 cc->cdb.cdb_length = csio->cdb_len;
3131 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3132 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; /* XXX ordered tags? */
3133 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
3134 cr->cr_flags = CISS_REQ_DATAOUT | CISS_REQ_CCB;
3135 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3136 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
3137 cr->cr_flags = CISS_REQ_DATAIN | CISS_REQ_CCB;
3138 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3142 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
3144 cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
3145 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
3146 bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
3148 bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
3152 * Submit the request to the adapter.
3154 * Note that this may fail if we're unable to map the request (and
3155 * if we ever learn a transport layer other than simple, may fail
3156 * if the adapter rejects the command).
3158 if ((error = ciss_start(cr)) != 0) {
3159 xpt_freeze_simq(sim, 1);
3160 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3161 if (error == EINPROGRESS) {
3164 csio->ccb_h.status |= CAM_REQUEUE_REQ;
3165 ciss_release_request(cr);
3173 /************************************************************************
3174 * Emulate SCSI commands the adapter doesn't handle as we might like.
3177 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
3182 target = csio->ccb_h.target_id;
3183 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3184 opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3185 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
3187 if (CISS_IS_PHYSICAL(bus)) {
3188 if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
3189 csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3190 xpt_done((union ccb *)csio);
3197 * Handle requests for volumes that don't exist or are not online.
3198 * A selection timeout is slightly better than an illegal request.
3199 * Other errors might be better.
3201 if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
3202 csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3203 xpt_done((union ccb *)csio);
3207 /* if we have to fake Synchronise Cache */
3208 if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
3210 * If this is a Synchronise Cache command, typically issued when
3211 * a device is closed, flush the adapter and complete now.
3213 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
3214 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
3215 ciss_flush_adapter(sc);
3216 csio->ccb_h.status |= CAM_REQ_CMP;
3217 xpt_done((union ccb *)csio);
3223 * A CISS target can only ever have one lun per target. REPORT_LUNS requires
3224 * at least one LUN field to be pre created for us, so snag it and fill in
3225 * the least significant byte indicating 1 LUN here. Emulate the command
3226 * return to shut up warning on console of a CDB error. swb
3228 if (opcode == REPORT_LUNS && csio->dxfer_len > 0) {
3229 csio->data_ptr[3] = 8;
3230 csio->ccb_h.status |= CAM_REQ_CMP;
3231 xpt_done((union ccb *)csio);
3238 /************************************************************************
3239 * Check for possibly-completed commands.
3242 ciss_cam_poll(struct cam_sim *sim)
3245 struct ciss_softc *sc = cam_sim_softc(sim);
3251 ciss_perf_done(sc, &qh);
3254 ciss_complete(sc, &qh);
3257 /************************************************************************
3258 * Handle completion of a command - pass results back through the CCB
3261 ciss_cam_complete(struct ciss_request *cr)
3263 struct ciss_softc *sc;
3264 struct ciss_command *cc;
3265 struct ciss_error_info *ce;
3266 struct ccb_scsiio *csio;
3274 ce = (struct ciss_error_info *)&(cc->sg[0]);
3275 csio = (struct ccb_scsiio *)cr->cr_private;
3278 * Extract status values from request.
3280 ciss_report_request(cr, &command_status, &scsi_status);
3281 csio->scsi_status = scsi_status;
3284 * Handle specific SCSI status values.
3286 switch(scsi_status) {
3287 /* no status due to adapter error */
3289 debug(0, "adapter error");
3290 csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3293 /* no status due to command completed OK */
3294 case SCSI_STATUS_OK: /* CISS_SCSI_STATUS_GOOD */
3295 debug(2, "SCSI_STATUS_OK");
3296 csio->ccb_h.status |= CAM_REQ_CMP;
3299 /* check condition, sense data included */
3300 case SCSI_STATUS_CHECK_COND: /* CISS_SCSI_STATUS_CHECK_CONDITION */
3301 debug(0, "SCSI_STATUS_CHECK_COND sense size %d resid %d\n",
3302 ce->sense_length, ce->residual_count);
3303 bzero(&csio->sense_data, SSD_FULL_SIZE);
3304 bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
3305 if (csio->sense_len > ce->sense_length)
3306 csio->sense_resid = csio->sense_len - ce->sense_length;
3308 csio->sense_resid = 0;
3309 csio->resid = ce->residual_count;
3310 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
3313 struct scsi_sense_data *sns = (struct scsi_sense_data *)&ce->sense_info[0];
3314 debug(0, "sense key %x", scsi_get_sense_key(sns, csio->sense_len -
3315 csio->sense_resid, /*show_errors*/ 1));
3320 case SCSI_STATUS_BUSY: /* CISS_SCSI_STATUS_BUSY */
3321 debug(0, "SCSI_STATUS_BUSY");
3322 csio->ccb_h.status |= CAM_SCSI_BUSY;
3326 debug(0, "unknown status 0x%x", csio->scsi_status);
3327 csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3331 /* handle post-command fixup */
3332 ciss_cam_complete_fixup(sc, csio);
3334 ciss_release_request(cr);
3335 if (sc->ciss_flags & CISS_FLAG_BUSY) {
3336 sc->ciss_flags &= ~CISS_FLAG_BUSY;
3337 if (csio->ccb_h.status & CAM_RELEASE_SIMQ)
3338 xpt_release_simq(xpt_path_sim(csio->ccb_h.path), 0);
3340 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3342 xpt_done((union ccb *)csio);
3345 /********************************************************************************
3346 * Fix up the result of some commands here.
3349 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
3351 struct scsi_inquiry_data *inq;
3352 struct ciss_ldrive *cl;
3356 cdb = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3357 (uint8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes;
3358 if (cdb[0] == INQUIRY &&
3359 (cdb[1] & SI_EVPD) == 0 &&
3360 (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
3361 csio->dxfer_len >= SHORT_INQUIRY_LENGTH) {
3363 inq = (struct scsi_inquiry_data *)csio->data_ptr;
3364 target = csio->ccb_h.target_id;
3365 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3368 * Don't let hard drives be seen by the DA driver. They will still be
3369 * attached by the PASS driver.
3371 if (CISS_IS_PHYSICAL(bus)) {
3372 if (SID_TYPE(inq) == T_DIRECT)
3373 inq->device = (inq->device & 0xe0) | T_NODEVICE;
3377 cl = &sc->ciss_logical[bus][target];
3379 padstr(inq->vendor, "COMPAQ",
3381 padstr(inq->product,
3382 ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance),
3384 padstr(inq->revision,
3385 ciss_name_ldrive_status(cl->cl_lstatus->status),
3391 /********************************************************************************
3392 * Find a peripheral attached at (target)
3394 static struct cam_periph *
3395 ciss_find_periph(struct ciss_softc *sc, int bus, int target)
3397 struct cam_periph *periph;
3398 struct cam_path *path;
3401 status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
3403 if (status == CAM_REQ_CMP) {
3404 periph = cam_periph_find(path, NULL);
3405 xpt_free_path(path);
3412 /********************************************************************************
3413 * Name the device at (target)
3415 * XXX is this strictly correct?
3418 ciss_name_device(struct ciss_softc *sc, int bus, int target)
3420 struct cam_periph *periph;
3422 if (CISS_IS_PHYSICAL(bus))
3424 if ((periph = ciss_find_periph(sc, bus, target)) != NULL) {
3425 sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
3426 periph->periph_name, periph->unit_number);
3429 sc->ciss_logical[bus][target].cl_name[0] = 0;
3433 /************************************************************************
3434 * Periodic status monitoring.
3437 ciss_periodic(void *arg)
3439 struct ciss_softc *sc;
3440 struct ciss_request *cr = NULL;
3441 struct ciss_command *cc = NULL;
3446 sc = (struct ciss_softc *)arg;
3449 * Check the adapter heartbeat.
3451 if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
3452 sc->ciss_heart_attack++;
3453 debug(0, "adapter heart attack in progress 0x%x/%d",
3454 sc->ciss_heartbeat, sc->ciss_heart_attack);
3455 if (sc->ciss_heart_attack == 3) {
3456 ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
3457 ciss_disable_adapter(sc);
3461 sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
3462 sc->ciss_heart_attack = 0;
3463 debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
3467 * Send the NOP message and wait for a response.
3469 if (ciss_nop_message_heartbeat != 0 && (error = ciss_get_request(sc, &cr)) == 0) {
3471 cr->cr_complete = ciss_nop_complete;
3472 cc->cdb.cdb_length = 1;
3473 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
3474 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3475 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3476 cc->cdb.timeout = 0;
3477 cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
3479 if ((error = ciss_start(cr)) != 0) {
3480 ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3485 * If the notify event request has died for some reason, or has
3486 * not started yet, restart it.
3488 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
3489 debug(0, "(re)starting Event Notify chain");
3490 ciss_notify_event(sc);
3496 callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
3500 ciss_nop_complete(struct ciss_request *cr)
3502 struct ciss_softc *sc;
3503 static int first_time = 1;
3506 if (ciss_report_request(cr, NULL, NULL) != 0) {
3507 if (first_time == 1) {
3509 ciss_printf(sc, "SENDING NOP MESSAGE FAILED (not logging anymore)\n");
3513 ciss_release_request(cr);
3516 /************************************************************************
3517 * Disable the adapter.
3519 * The all requests in completed queue is failed with hardware error.
3520 * This will cause failover in a multipath configuration.
3523 ciss_disable_adapter(struct ciss_softc *sc)
3526 struct ciss_request *cr;
3527 struct ciss_command *cc;
3528 struct ciss_error_info *ce;
3531 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
3532 pci_disable_busmaster(sc->ciss_dev);
3533 sc->ciss_flags &= ~CISS_FLAG_RUNNING;
3535 for (i = 1; i < sc->ciss_max_requests; i++) {
3536 cr = &sc->ciss_request[i];
3537 if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
3541 ce = (struct ciss_error_info *)&(cc->sg[0]);
3542 ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
3543 ciss_enqueue_complete(cr, &qh);
3547 if ((cr = ciss_dequeue_complete(sc, &qh)) == NULL)
3551 * If the request has a callback, invoke it.
3553 if (cr->cr_complete != NULL) {
3554 cr->cr_complete(cr);
3559 * If someone is sleeping on this request, wake them up.
3561 if (cr->cr_flags & CISS_REQ_SLEEP) {
3562 cr->cr_flags &= ~CISS_REQ_SLEEP;
3569 /************************************************************************
3570 * Request a notification response from the adapter.
3572 * If (cr) is NULL, this is the first request of the adapter, so
3573 * reset the adapter's message pointer and start with the oldest
3574 * message available.
3577 ciss_notify_event(struct ciss_softc *sc)
3579 struct ciss_request *cr;
3580 struct ciss_command *cc;
3581 struct ciss_notify_cdb *cnc;
3586 cr = sc->ciss_periodic_notify;
3588 /* get a request if we don't already have one */
3590 if ((error = ciss_get_request(sc, &cr)) != 0) {
3591 debug(0, "can't get notify event request");
3594 sc->ciss_periodic_notify = cr;
3595 cr->cr_complete = ciss_notify_complete;
3596 debug(1, "acquired request %d", cr->cr_tag);
3600 * Get a databuffer if we don't already have one, note that the
3601 * adapter command wants a larger buffer than the actual
3604 if (cr->cr_data == NULL) {
3605 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3606 debug(0, "can't get notify event request buffer");
3610 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3613 /* re-setup the request's command (since we never release it) XXX overkill*/
3614 ciss_preen_command(cr);
3616 /* (re)build the notify event command */
3618 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3619 cc->header.address.physical.bus = 0;
3620 cc->header.address.physical.target = 0;
3622 cc->cdb.cdb_length = sizeof(*cnc);
3623 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3624 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3625 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3626 cc->cdb.timeout = 0; /* no timeout, we hope */
3628 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3629 bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
3630 cnc->opcode = CISS_OPCODE_READ;
3631 cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
3632 cnc->timeout = 0; /* no timeout, we hope */
3633 cnc->synchronous = 0;
3635 cnc->seek_to_oldest = 0;
3636 if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
3640 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3642 /* submit the request */
3643 error = ciss_start(cr);
3648 if (cr->cr_data != NULL)
3649 free(cr->cr_data, CISS_MALLOC_CLASS);
3650 ciss_release_request(cr);
3652 sc->ciss_periodic_notify = NULL;
3653 debug(0, "can't submit notify event request");
3654 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3656 debug(1, "notify event submitted");
3657 sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
3662 ciss_notify_complete(struct ciss_request *cr)
3664 struct ciss_command *cc;
3665 struct ciss_notify *cn;
3666 struct ciss_softc *sc;
3672 cn = (struct ciss_notify *)cr->cr_data;
3676 * Report request results, decode status.
3678 ciss_report_request(cr, &command_status, &scsi_status);
3681 * Abort the chain on a fatal error.
3683 * XXX which of these are actually errors?
3685 if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
3686 (command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
3687 (command_status != CISS_CMD_STATUS_TIMEOUT)) { /* XXX timeout? */
3688 ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
3689 ciss_name_command_status(command_status));
3690 ciss_release_request(cr);
3691 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3696 * If the adapter gave us a text message, print it.
3698 if (cn->message[0] != 0)
3699 ciss_printf(sc, "*** %.80s\n", cn->message);
3701 debug(0, "notify event class %d subclass %d detail %d",
3702 cn->class, cn->subclass, cn->detail);
3705 * If the response indicates that the notifier has been aborted,
3706 * release the notifier command.
3708 if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
3709 (cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
3710 (cn->detail == 1)) {
3711 debug(0, "notifier exiting");
3712 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3713 ciss_release_request(cr);
3714 sc->ciss_periodic_notify = NULL;
3715 wakeup(&sc->ciss_periodic_notify);
3717 /* Handle notify events in a kernel thread */
3718 ciss_enqueue_notify(cr);
3719 sc->ciss_periodic_notify = NULL;
3720 wakeup(&sc->ciss_periodic_notify);
3721 wakeup(&sc->ciss_notify);
3724 * Send a new notify event command, if we're not aborting.
3726 if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
3727 ciss_notify_event(sc);
3731 /************************************************************************
3732 * Abort the Notify Event chain.
3734 * Note that we can't just abort the command in progress; we have to
3735 * explicitly issue an Abort Notify Event command in order for the
3736 * adapter to clean up correctly.
3738 * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
3739 * the chain will not restart itself.
3742 ciss_notify_abort(struct ciss_softc *sc)
3744 struct ciss_request *cr;
3745 struct ciss_command *cc;
3746 struct ciss_notify_cdb *cnc;
3747 int error, command_status, scsi_status;
3754 /* verify that there's an outstanding command */
3755 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3758 /* get a command to issue the abort with */
3759 if ((error = ciss_get_request(sc, &cr)))
3762 /* get a buffer for the result */
3763 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3764 debug(0, "can't get notify event request buffer");
3768 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3772 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3773 cc->header.address.physical.bus = 0;
3774 cc->header.address.physical.target = 0;
3775 cc->cdb.cdb_length = sizeof(*cnc);
3776 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3777 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3778 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3779 cc->cdb.timeout = 0; /* no timeout, we hope */
3781 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3782 bzero(cnc, sizeof(*cnc));
3783 cnc->opcode = CISS_OPCODE_WRITE;
3784 cnc->command = CISS_COMMAND_ABORT_NOTIFY;
3785 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3787 ciss_print_request(cr);
3790 * Submit the request and wait for it to complete.
3792 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3793 ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
3800 ciss_report_request(cr, &command_status, &scsi_status);
3801 switch(command_status) {
3802 case CISS_CMD_STATUS_SUCCESS:
3804 case CISS_CMD_STATUS_INVALID_COMMAND:
3806 * Some older adapters don't support the CISS version of this
3807 * command. Fall back to using the BMIC version.
3809 error = ciss_notify_abort_bmic(sc);
3814 case CISS_CMD_STATUS_TARGET_STATUS:
3816 * This can happen if the adapter thinks there wasn't an outstanding
3817 * Notify Event command but we did. We clean up here.
3819 if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
3820 if (sc->ciss_periodic_notify != NULL)
3821 ciss_release_request(sc->ciss_periodic_notify);
3828 ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
3829 ciss_name_command_status(command_status));
3835 * Sleep waiting for the notifier command to complete. Note
3836 * that if it doesn't, we may end up in a bad situation, since
3837 * the adapter may deliver it later. Also note that the adapter
3838 * requires the Notify Event command to be cancelled in order to
3839 * maintain internal bookkeeping.
3841 while (sc->ciss_periodic_notify != NULL) {
3842 error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
3843 if (error == EWOULDBLOCK) {
3844 ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
3850 /* release the cancel request */
3852 if (cr->cr_data != NULL)
3853 free(cr->cr_data, CISS_MALLOC_CLASS);
3854 ciss_release_request(cr);
3857 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3861 /************************************************************************
3862 * Abort the Notify Event chain using a BMIC command.
3865 ciss_notify_abort_bmic(struct ciss_softc *sc)
3867 struct ciss_request *cr;
3868 int error, command_status;
3875 /* verify that there's an outstanding command */
3876 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3880 * Build a BMIC command to cancel the Notify on Event command.
3882 * Note that we are sending a CISS opcode here. Odd.
3884 if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
3889 * Submit the request and wait for it to complete.
3891 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3892 ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
3899 ciss_report_request(cr, &command_status, NULL);
3900 switch(command_status) {
3901 case CISS_CMD_STATUS_SUCCESS:
3904 ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
3905 ciss_name_command_status(command_status));
3912 ciss_release_request(cr);
3916 /************************************************************************
3917 * Handle rescanning all the logical volumes when a notify event
3918 * causes the drives to come online or offline.
3921 ciss_notify_rescan_logical(struct ciss_softc *sc)
3923 struct ciss_lun_report *cll;
3924 struct ciss_ldrive *ld;
3928 * We must rescan all logical volumes to get the right logical
3931 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
3932 sc->ciss_cfg->max_logical_supported);
3936 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
3939 * Delete any of the drives which were destroyed by the
3942 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
3943 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
3944 ld = &sc->ciss_logical[i][j];
3946 if (ld->cl_update == 0)
3949 if (ld->cl_status != CISS_LD_ONLINE) {
3950 ciss_cam_rescan_target(sc, i, j);
3953 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
3955 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
3957 ld->cl_ldrive = NULL;
3958 ld->cl_lstatus = NULL;
3964 * Scan for new drives.
3966 for (i = 0; i < ndrives; i++) {
3969 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
3970 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
3971 ld = &sc->ciss_logical[bus][target];
3973 if (ld->cl_update == 0)
3977 ld->cl_address = cll->lun[i];
3978 ld->cl_controller = &sc->ciss_controllers[bus];
3979 if (ciss_identify_logical(sc, ld) == 0) {
3980 ciss_cam_rescan_target(sc, bus, target);
3983 free(cll, CISS_MALLOC_CLASS);
3986 /************************************************************************
3987 * Handle a notify event relating to the status of a logical drive.
3989 * XXX need to be able to defer some of these to properly handle
3990 * calling the "ID Physical drive" command, unless the 'extended'
3991 * drive IDs are always in BIG_MAP format.
3994 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
3996 struct ciss_ldrive *ld;
4002 bus = cn->device.physical.bus;
4003 target = cn->data.logical_status.logical_drive;
4004 ld = &sc->ciss_logical[bus][target];
4006 switch (cn->subclass) {
4007 case CISS_NOTIFY_LOGICAL_STATUS:
4008 switch (cn->detail) {
4010 ciss_name_device(sc, bus, target);
4011 ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
4012 cn->data.logical_status.logical_drive, ld->cl_name,
4013 ciss_name_ldrive_status(cn->data.logical_status.previous_state),
4014 ciss_name_ldrive_status(cn->data.logical_status.new_state),
4015 cn->data.logical_status.spare_state,
4016 "\20\1configured\2rebuilding\3failed\4in use\5available\n");
4019 * Update our idea of the drive's status.
4021 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4022 if (ld->cl_lstatus != NULL)
4023 ld->cl_lstatus->status = cn->data.logical_status.new_state;
4026 * Have CAM rescan the drive if its status has changed.
4028 rescan_ld = (cn->data.logical_status.previous_state !=
4029 cn->data.logical_status.new_state) ? 1 : 0;
4032 ciss_notify_rescan_logical(sc);
4037 case 1: /* logical drive has recognised new media, needs Accept Media Exchange */
4038 ciss_name_device(sc, bus, target);
4039 ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
4040 cn->data.logical_status.logical_drive, ld->cl_name);
4041 ciss_accept_media(sc, ld);
4044 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4045 ciss_notify_rescan_logical(sc);
4050 ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
4051 cn->data.rebuild_aborted.logical_drive,
4053 (cn->detail == 2) ? "read" : "write");
4058 case CISS_NOTIFY_LOGICAL_ERROR:
4059 if (cn->detail == 0) {
4060 ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
4061 cn->data.io_error.logical_drive,
4063 cn->data.io_error.failure_bus,
4064 cn->data.io_error.failure_drive);
4065 /* XXX should we take the drive down at this point, or will we be told? */
4069 case CISS_NOTIFY_LOGICAL_SURFACE:
4070 if (cn->detail == 0)
4071 ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
4072 cn->data.consistency_completed.logical_drive,
4078 /************************************************************************
4079 * Handle a notify event relating to the status of a physical drive.
4082 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
4086 /************************************************************************
4087 * Handle a notify event relating to the status of a physical drive.
4090 ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
4092 struct ciss_lun_report *cll = NULL;
4095 switch (cn->subclass) {
4096 case CISS_NOTIFY_HOTPLUG_PHYSICAL:
4097 case CISS_NOTIFY_HOTPLUG_NONDISK:
4098 bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
4100 CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
4102 if (cn->detail == 0) {
4104 * Mark the device offline so that it'll start producing selection
4105 * timeouts to the upper layer.
4107 if ((bus >= 0) && (target >= 0))
4108 sc->ciss_physical[bus][target].cp_online = 0;
4111 * Rescan the physical lun list for new items
4113 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
4114 sc->ciss_cfg->max_physical_supported);
4116 ciss_printf(sc, "Warning, cannot get physical lun list\n");
4119 ciss_filter_physical(sc, cll);
4124 ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
4129 free(cll, CISS_MALLOC_CLASS);
4132 /************************************************************************
4133 * Handle deferred processing of notify events. Notify events may need
4134 * sleep which is unsafe during an interrupt.
4137 ciss_notify_thread(void *arg)
4139 struct ciss_softc *sc;
4140 struct ciss_request *cr;
4141 struct ciss_notify *cn;
4143 sc = (struct ciss_softc *)arg;
4144 #if __FreeBSD_version >= 500000
4145 mtx_lock(&sc->ciss_mtx);
4149 if (STAILQ_EMPTY(&sc->ciss_notify) != 0 &&
4150 (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
4151 msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
4154 if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
4157 cr = ciss_dequeue_notify(sc);
4161 cn = (struct ciss_notify *)cr->cr_data;
4163 switch (cn->class) {
4164 case CISS_NOTIFY_HOTPLUG:
4165 ciss_notify_hotplug(sc, cn);
4167 case CISS_NOTIFY_LOGICAL:
4168 ciss_notify_logical(sc, cn);
4170 case CISS_NOTIFY_PHYSICAL:
4171 ciss_notify_physical(sc, cn);
4175 ciss_release_request(cr);
4178 sc->ciss_notify_thread = NULL;
4179 wakeup(&sc->ciss_notify_thread);
4181 #if __FreeBSD_version >= 500000
4182 mtx_unlock(&sc->ciss_mtx);
4187 /************************************************************************
4188 * Start the notification kernel thread.
4191 ciss_spawn_notify_thread(struct ciss_softc *sc)
4194 #if __FreeBSD_version > 500005
4195 if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4196 &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
4197 device_get_unit(sc->ciss_dev)))
4199 if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4200 &sc->ciss_notify_thread, "ciss_notify%d",
4201 device_get_unit(sc->ciss_dev)))
4203 panic("Could not create notify thread\n");
4206 /************************************************************************
4207 * Kill the notification kernel thread.
4210 ciss_kill_notify_thread(struct ciss_softc *sc)
4213 if (sc->ciss_notify_thread == NULL)
4216 sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
4217 wakeup(&sc->ciss_notify);
4218 msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
4221 /************************************************************************
4225 ciss_print_request(struct ciss_request *cr)
4227 struct ciss_softc *sc;
4228 struct ciss_command *cc;
4234 ciss_printf(sc, "REQUEST @ %p\n", cr);
4235 ciss_printf(sc, " data %p/%d tag %d flags %b\n",
4236 cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
4237 "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
4238 ciss_printf(sc, " sg list/total %d/%d host tag 0x%x\n",
4239 cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
4240 switch(cc->header.address.mode.mode) {
4241 case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
4242 case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
4243 ciss_printf(sc, " physical bus %d target %d\n",
4244 cc->header.address.physical.bus, cc->header.address.physical.target);
4246 case CISS_HDR_ADDRESS_MODE_LOGICAL:
4247 ciss_printf(sc, " logical unit %d\n", cc->header.address.logical.lun);
4250 ciss_printf(sc, " %s cdb length %d type %s attribute %s\n",
4251 (cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
4252 (cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
4253 (cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
4255 (cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
4256 (cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
4257 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
4258 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
4259 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
4260 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
4261 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
4262 ciss_printf(sc, " %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
4264 if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
4265 /* XXX print error info */
4267 /* since we don't use chained s/g, don't support it here */
4268 for (i = 0; i < cc->header.sg_in_list; i++) {
4270 ciss_printf(sc, " ");
4271 printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
4272 if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
4278 /************************************************************************
4279 * Print information about the status of a logical drive.
4282 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
4286 if (ld->cl_lstatus == NULL) {
4287 printf("does not exist\n");
4291 /* print drive status */
4292 switch(ld->cl_lstatus->status) {
4293 case CISS_LSTATUS_OK:
4296 case CISS_LSTATUS_INTERIM_RECOVERY:
4297 printf("in interim recovery mode\n");
4299 case CISS_LSTATUS_READY_RECOVERY:
4300 printf("ready to begin recovery\n");
4302 case CISS_LSTATUS_RECOVERING:
4303 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4304 target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4305 printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
4306 bus, target, ld->cl_lstatus->blocks_to_recover);
4308 case CISS_LSTATUS_EXPANDING:
4309 printf("being expanded, %u blocks remaining\n",
4310 ld->cl_lstatus->blocks_to_recover);
4312 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4313 printf("queued for expansion\n");
4315 case CISS_LSTATUS_FAILED:
4316 printf("queued for expansion\n");
4318 case CISS_LSTATUS_WRONG_PDRIVE:
4319 printf("wrong physical drive inserted\n");
4321 case CISS_LSTATUS_MISSING_PDRIVE:
4322 printf("missing a needed physical drive\n");
4324 case CISS_LSTATUS_BECOMING_READY:
4325 printf("becoming ready\n");
4329 /* print failed physical drives */
4330 for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
4331 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
4332 target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
4335 ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
4336 ld->cl_lstatus->drive_failure_map[i]);
4341 #include "opt_ddb.h"
4343 #include <ddb/ddb.h>
4344 /************************************************************************
4345 * Print information about the controller/driver.
4348 ciss_print_adapter(struct ciss_softc *sc)
4352 ciss_printf(sc, "ADAPTER:\n");
4353 for (i = 0; i < CISSQ_COUNT; i++) {
4354 ciss_printf(sc, "%s %d/%d\n",
4356 i == 1 ? "busy" : "complete",
4357 sc->ciss_qstat[i].q_length,
4358 sc->ciss_qstat[i].q_max);
4360 ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
4361 ciss_printf(sc, "flags %b\n", sc->ciss_flags,
4362 "\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
4364 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
4365 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
4366 ciss_printf(sc, "LOGICAL DRIVE %d: ", i);
4367 ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
4371 /* XXX Should physical drives be printed out here? */
4373 for (i = 1; i < sc->ciss_max_requests; i++)
4374 ciss_print_request(sc->ciss_request + i);
4378 DB_COMMAND(ciss_prt, db_ciss_prt)
4380 struct ciss_softc *sc;
4382 sc = devclass_get_softc(devclass_find("ciss"), 0);
4384 printf("no ciss controllers\n");
4386 ciss_print_adapter(sc);
4392 /************************************************************************
4393 * Return a name for a logical drive status value.
4396 ciss_name_ldrive_status(int status)
4399 case CISS_LSTATUS_OK:
4401 case CISS_LSTATUS_FAILED:
4403 case CISS_LSTATUS_NOT_CONFIGURED:
4404 return("not configured");
4405 case CISS_LSTATUS_INTERIM_RECOVERY:
4406 return("interim recovery");
4407 case CISS_LSTATUS_READY_RECOVERY:
4408 return("ready for recovery");
4409 case CISS_LSTATUS_RECOVERING:
4410 return("recovering");
4411 case CISS_LSTATUS_WRONG_PDRIVE:
4412 return("wrong physical drive inserted");
4413 case CISS_LSTATUS_MISSING_PDRIVE:
4414 return("missing physical drive");
4415 case CISS_LSTATUS_EXPANDING:
4416 return("expanding");
4417 case CISS_LSTATUS_BECOMING_READY:
4418 return("becoming ready");
4419 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4420 return("queued for expansion");
4422 return("unknown status");
4425 /************************************************************************
4426 * Return an online/offline/nonexistent value for a logical drive
4430 ciss_decode_ldrive_status(int status)
4433 case CISS_LSTATUS_NOT_CONFIGURED:
4434 return(CISS_LD_NONEXISTENT);
4436 case CISS_LSTATUS_OK:
4437 case CISS_LSTATUS_INTERIM_RECOVERY:
4438 case CISS_LSTATUS_READY_RECOVERY:
4439 case CISS_LSTATUS_RECOVERING:
4440 case CISS_LSTATUS_EXPANDING:
4441 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4442 return(CISS_LD_ONLINE);
4444 case CISS_LSTATUS_FAILED:
4445 case CISS_LSTATUS_WRONG_PDRIVE:
4446 case CISS_LSTATUS_MISSING_PDRIVE:
4447 case CISS_LSTATUS_BECOMING_READY:
4449 return(CISS_LD_OFFLINE);
4454 /************************************************************************
4455 * Return a name for a logical drive's organisation.
4458 ciss_name_ldrive_org(int org)
4461 case CISS_LDRIVE_RAID0:
4463 case CISS_LDRIVE_RAID1:
4464 return("RAID 1(1+0)");
4465 case CISS_LDRIVE_RAID4:
4467 case CISS_LDRIVE_RAID5:
4469 case CISS_LDRIVE_RAID51:
4471 case CISS_LDRIVE_RAIDADG:
4477 /************************************************************************
4478 * Return a name for a command status value.
4481 ciss_name_command_status(int status)
4484 case CISS_CMD_STATUS_SUCCESS:
4486 case CISS_CMD_STATUS_TARGET_STATUS:
4487 return("target status");
4488 case CISS_CMD_STATUS_DATA_UNDERRUN:
4489 return("data underrun");
4490 case CISS_CMD_STATUS_DATA_OVERRUN:
4491 return("data overrun");
4492 case CISS_CMD_STATUS_INVALID_COMMAND:
4493 return("invalid command");
4494 case CISS_CMD_STATUS_PROTOCOL_ERROR:
4495 return("protocol error");
4496 case CISS_CMD_STATUS_HARDWARE_ERROR:
4497 return("hardware error");
4498 case CISS_CMD_STATUS_CONNECTION_LOST:
4499 return("connection lost");
4500 case CISS_CMD_STATUS_ABORTED:
4502 case CISS_CMD_STATUS_ABORT_FAILED:
4503 return("abort failed");
4504 case CISS_CMD_STATUS_UNSOLICITED_ABORT:
4505 return("unsolicited abort");
4506 case CISS_CMD_STATUS_TIMEOUT:
4508 case CISS_CMD_STATUS_UNABORTABLE:
4509 return("unabortable");
4511 return("unknown status");
4514 /************************************************************************
4515 * Handle an open on the control device.
4518 ciss_open(struct cdev *dev, int flags, int fmt, struct thread *p)
4520 struct ciss_softc *sc;
4524 sc = (struct ciss_softc *)dev->si_drv1;
4526 /* we might want to veto if someone already has us open */
4528 mtx_lock(&sc->ciss_mtx);
4529 sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
4530 mtx_unlock(&sc->ciss_mtx);
4534 /************************************************************************
4535 * Handle the last close on the control device.
4538 ciss_close(struct cdev *dev, int flags, int fmt, struct thread *p)
4540 struct ciss_softc *sc;
4544 sc = (struct ciss_softc *)dev->si_drv1;
4546 mtx_lock(&sc->ciss_mtx);
4547 sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
4548 mtx_unlock(&sc->ciss_mtx);
4552 /********************************************************************************
4553 * Handle adapter-specific control operations.
4555 * Note that the API here is compatible with the Linux driver, in order to
4556 * simplify the porting of Compaq's userland tools.
4559 ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *p)
4561 struct ciss_softc *sc;
4562 IOCTL_Command_struct *ioc = (IOCTL_Command_struct *)addr;
4564 IOCTL_Command_struct32 *ioc32 = (IOCTL_Command_struct32 *)addr;
4565 IOCTL_Command_struct ioc_swab;
4571 sc = (struct ciss_softc *)dev->si_drv1;
4573 mtx_lock(&sc->ciss_mtx);
4576 case CCISS_GETQSTATS:
4578 union ciss_statrequest *cr = (union ciss_statrequest *)addr;
4580 switch (cr->cs_item) {
4583 bcopy(&sc->ciss_qstat[cr->cs_item], &cr->cs_qstat,
4584 sizeof(struct ciss_qstat));
4594 case CCISS_GETPCIINFO:
4596 cciss_pci_info_struct *pis = (cciss_pci_info_struct *)addr;
4598 pis->bus = pci_get_bus(sc->ciss_dev);
4599 pis->dev_fn = pci_get_slot(sc->ciss_dev);
4600 pis->board_id = (pci_get_subvendor(sc->ciss_dev) << 16) |
4601 pci_get_subdevice(sc->ciss_dev);
4606 case CCISS_GETINTINFO:
4608 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4610 cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
4611 cis->count = sc->ciss_cfg->interrupt_coalesce_count;
4616 case CCISS_SETINTINFO:
4618 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4620 if ((cis->delay == 0) && (cis->count == 0)) {
4626 * XXX apparently this is only safe if the controller is idle,
4627 * we should suspend it before doing this.
4629 sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
4630 sc->ciss_cfg->interrupt_coalesce_count = cis->count;
4632 if (ciss_update_config(sc))
4635 /* XXX resume the controller here */
4639 case CCISS_GETNODENAME:
4640 bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
4641 sizeof(NodeName_type));
4644 case CCISS_SETNODENAME:
4645 bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
4646 sizeof(NodeName_type));
4647 if (ciss_update_config(sc))
4651 case CCISS_GETHEARTBEAT:
4652 *(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
4655 case CCISS_GETBUSTYPES:
4656 *(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
4659 case CCISS_GETFIRMVER:
4660 bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
4661 sizeof(FirmwareVer_type));
4664 case CCISS_GETDRIVERVER:
4665 *(DriverVer_type *)addr = CISS_DRIVER_VERSION;
4668 case CCISS_REVALIDVOLS:
4670 * This is a bit ugly; to do it "right" we really need
4671 * to find any disks that have changed, kick CAM off them,
4672 * then rescan only these disks. It'd be nice if they
4673 * a) told us which disk(s) they were going to play with,
4674 * and b) which ones had arrived. 8(
4679 case CCISS_PASSTHRU32:
4680 ioc_swab.LUN_info = ioc32->LUN_info;
4681 ioc_swab.Request = ioc32->Request;
4682 ioc_swab.error_info = ioc32->error_info;
4683 ioc_swab.buf_size = ioc32->buf_size;
4684 ioc_swab.buf = (u_int8_t *)(uintptr_t)ioc32->buf;
4689 case CCISS_PASSTHRU:
4690 error = ciss_user_command(sc, ioc);
4694 debug(0, "unknown ioctl 0x%lx", cmd);
4696 debug(1, "CCISS_GETPCIINFO: 0x%lx", CCISS_GETPCIINFO);
4697 debug(1, "CCISS_GETINTINFO: 0x%lx", CCISS_GETINTINFO);
4698 debug(1, "CCISS_SETINTINFO: 0x%lx", CCISS_SETINTINFO);
4699 debug(1, "CCISS_GETNODENAME: 0x%lx", CCISS_GETNODENAME);
4700 debug(1, "CCISS_SETNODENAME: 0x%lx", CCISS_SETNODENAME);
4701 debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
4702 debug(1, "CCISS_GETBUSTYPES: 0x%lx", CCISS_GETBUSTYPES);
4703 debug(1, "CCISS_GETFIRMVER: 0x%lx", CCISS_GETFIRMVER);
4704 debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
4705 debug(1, "CCISS_REVALIDVOLS: 0x%lx", CCISS_REVALIDVOLS);
4706 debug(1, "CCISS_PASSTHRU: 0x%lx", CCISS_PASSTHRU);
4712 mtx_unlock(&sc->ciss_mtx);