2 * SPDX-License-Identifier: BSD-2-Clause
4 * Copyright (c) 2001 Michael Smith
5 * Copyright (c) 2004 Paul Saab
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * 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>
112 static MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data",
113 "ciss internal data buffers");
116 static int ciss_lookup(device_t dev);
117 static int ciss_probe(device_t dev);
118 static int ciss_attach(device_t dev);
119 static int ciss_detach(device_t dev);
120 static int ciss_shutdown(device_t dev);
122 /* (de)initialisation functions, control wrappers */
123 static int ciss_init_pci(struct ciss_softc *sc);
124 static int ciss_setup_msix(struct ciss_softc *sc);
125 static int ciss_init_perf(struct ciss_softc *sc);
126 static int ciss_wait_adapter(struct ciss_softc *sc);
127 static int ciss_flush_adapter(struct ciss_softc *sc);
128 static int ciss_init_requests(struct ciss_softc *sc);
129 static void ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
130 int nseg, int error);
131 static int ciss_identify_adapter(struct ciss_softc *sc);
132 static int ciss_init_logical(struct ciss_softc *sc);
133 static int ciss_init_physical(struct ciss_softc *sc);
134 static int ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll);
135 static int ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
136 static int ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld);
137 static int ciss_update_config(struct ciss_softc *sc);
138 static int ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld);
139 static void ciss_init_sysctl(struct ciss_softc *sc);
140 static void ciss_soft_reset(struct ciss_softc *sc);
141 static void ciss_free(struct ciss_softc *sc);
142 static void ciss_spawn_notify_thread(struct ciss_softc *sc);
143 static void ciss_kill_notify_thread(struct ciss_softc *sc);
145 /* request submission/completion */
146 static int ciss_start(struct ciss_request *cr);
147 static void ciss_done(struct ciss_softc *sc, cr_qhead_t *qh);
148 static void ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh);
149 static void ciss_intr(void *arg);
150 static void ciss_perf_intr(void *arg);
151 static void ciss_perf_msi_intr(void *arg);
152 static void ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh);
153 static int _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func);
154 static int ciss_synch_request(struct ciss_request *cr, int timeout);
155 static int ciss_poll_request(struct ciss_request *cr, int timeout);
156 static int ciss_wait_request(struct ciss_request *cr, int timeout);
158 static int ciss_abort_request(struct ciss_request *cr);
161 /* request queueing */
162 static int ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
163 static void ciss_preen_command(struct ciss_request *cr);
164 static void ciss_release_request(struct ciss_request *cr);
166 /* request helpers */
167 static int ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
168 int opcode, void **bufp, size_t bufsize);
169 static int ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
172 static int ciss_map_request(struct ciss_request *cr);
173 static void ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
174 int nseg, int error);
175 static void ciss_unmap_request(struct ciss_request *cr);
178 static int ciss_cam_init(struct ciss_softc *sc);
179 static void ciss_cam_rescan_target(struct ciss_softc *sc,
180 int bus, int target);
181 static void ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
182 static int ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
183 static int ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
184 static void ciss_cam_poll(struct cam_sim *sim);
185 static void ciss_cam_complete(struct ciss_request *cr);
186 static void ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
187 static int ciss_name_device(struct ciss_softc *sc, int bus, int target);
189 /* periodic status monitoring */
190 static void ciss_periodic(void *arg);
191 static void ciss_nop_complete(struct ciss_request *cr);
192 static void ciss_disable_adapter(struct ciss_softc *sc);
193 static void ciss_notify_event(struct ciss_softc *sc);
194 static void ciss_notify_complete(struct ciss_request *cr);
195 static int ciss_notify_abort(struct ciss_softc *sc);
196 static int ciss_notify_abort_bmic(struct ciss_softc *sc);
197 static void ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
198 static void ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
199 static void ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
201 /* debugging output */
203 static void ciss_print_request(struct ciss_request *cr);
205 static void ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
206 static const char *ciss_name_ldrive_status(int status);
207 static int ciss_decode_ldrive_status(int status);
208 static const char *ciss_name_ldrive_org(int org);
209 static const char *ciss_name_command_status(int status);
214 static device_method_t ciss_methods[] = {
215 /* Device interface */
216 DEVMETHOD(device_probe, ciss_probe),
217 DEVMETHOD(device_attach, ciss_attach),
218 DEVMETHOD(device_detach, ciss_detach),
219 DEVMETHOD(device_shutdown, ciss_shutdown),
223 static driver_t ciss_pci_driver = {
226 sizeof(struct ciss_softc)
230 * Control device interface.
232 static d_open_t ciss_open;
233 static d_close_t ciss_close;
234 static d_ioctl_t ciss_ioctl;
236 static struct cdevsw ciss_cdevsw = {
237 .d_version = D_VERSION,
240 .d_close = ciss_close,
241 .d_ioctl = ciss_ioctl,
246 * This tunable can be set at boot time and controls whether physical devices
247 * that are marked hidden by the firmware should be exposed anyways.
249 static unsigned int ciss_expose_hidden_physical = 0;
250 TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
252 static unsigned int ciss_nop_message_heartbeat = 0;
253 TUNABLE_INT("hw.ciss.nop_message_heartbeat", &ciss_nop_message_heartbeat);
256 * This tunable can force a particular transport to be used:
259 * 2 : force performant
261 static int ciss_force_transport = 0;
262 TUNABLE_INT("hw.ciss.force_transport", &ciss_force_transport);
265 * This tunable can force a particular interrupt delivery method to be used:
270 static int ciss_force_interrupt = 0;
271 TUNABLE_INT("hw.ciss.force_interrupt", &ciss_force_interrupt);
273 /************************************************************************
274 * CISS adapters amazingly don't have a defined programming interface
275 * value. (One could say some very despairing things about PCI and
276 * people just not getting the general idea.) So we are forced to
277 * stick with matching against subvendor/subdevice, and thus have to
278 * be updated for every new CISS adapter that appears.
280 #define CISS_BOARD_UNKNOWN 0
281 #define CISS_BOARD_SA5 1
282 #define CISS_BOARD_SA5B 2
283 #define CISS_BOARD_NOMSI (1<<4)
284 #define CISS_BOARD_SIMPLE (1<<5)
292 } ciss_vendor_data[] = {
293 { 0x0e11, 0x4070, CISS_BOARD_SA5|CISS_BOARD_NOMSI|CISS_BOARD_SIMPLE,
294 "Compaq Smart Array 5300" },
295 { 0x0e11, 0x4080, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "Compaq Smart Array 5i" },
296 { 0x0e11, 0x4082, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "Compaq Smart Array 532" },
297 { 0x0e11, 0x4083, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "HP Smart Array 5312" },
298 { 0x0e11, 0x4091, CISS_BOARD_SA5, "HP Smart Array 6i" },
299 { 0x0e11, 0x409A, CISS_BOARD_SA5, "HP Smart Array 641" },
300 { 0x0e11, 0x409B, CISS_BOARD_SA5, "HP Smart Array 642" },
301 { 0x0e11, 0x409C, CISS_BOARD_SA5, "HP Smart Array 6400" },
302 { 0x0e11, 0x409D, CISS_BOARD_SA5, "HP Smart Array 6400 EM" },
303 { 0x103C, 0x3211, CISS_BOARD_SA5, "HP Smart Array E200i" },
304 { 0x103C, 0x3212, CISS_BOARD_SA5, "HP Smart Array E200" },
305 { 0x103C, 0x3213, CISS_BOARD_SA5, "HP Smart Array E200i" },
306 { 0x103C, 0x3214, CISS_BOARD_SA5, "HP Smart Array E200i" },
307 { 0x103C, 0x3215, CISS_BOARD_SA5, "HP Smart Array E200i" },
308 { 0x103C, 0x3220, CISS_BOARD_SA5, "HP Smart Array" },
309 { 0x103C, 0x3222, CISS_BOARD_SA5, "HP Smart Array" },
310 { 0x103C, 0x3223, CISS_BOARD_SA5, "HP Smart Array P800" },
311 { 0x103C, 0x3225, CISS_BOARD_SA5, "HP Smart Array P600" },
312 { 0x103C, 0x3230, CISS_BOARD_SA5, "HP Smart Array" },
313 { 0x103C, 0x3231, CISS_BOARD_SA5, "HP Smart Array" },
314 { 0x103C, 0x3232, CISS_BOARD_SA5, "HP Smart Array" },
315 { 0x103C, 0x3233, CISS_BOARD_SA5, "HP Smart Array" },
316 { 0x103C, 0x3234, CISS_BOARD_SA5, "HP Smart Array P400" },
317 { 0x103C, 0x3235, CISS_BOARD_SA5, "HP Smart Array P400i" },
318 { 0x103C, 0x3236, CISS_BOARD_SA5, "HP Smart Array" },
319 { 0x103C, 0x3237, CISS_BOARD_SA5, "HP Smart Array E500" },
320 { 0x103C, 0x3238, CISS_BOARD_SA5, "HP Smart Array" },
321 { 0x103C, 0x3239, CISS_BOARD_SA5, "HP Smart Array" },
322 { 0x103C, 0x323A, CISS_BOARD_SA5, "HP Smart Array" },
323 { 0x103C, 0x323B, CISS_BOARD_SA5, "HP Smart Array" },
324 { 0x103C, 0x323C, CISS_BOARD_SA5, "HP Smart Array" },
325 { 0x103C, 0x323D, CISS_BOARD_SA5, "HP Smart Array P700m" },
326 { 0x103C, 0x3241, CISS_BOARD_SA5, "HP Smart Array P212" },
327 { 0x103C, 0x3243, CISS_BOARD_SA5, "HP Smart Array P410" },
328 { 0x103C, 0x3245, CISS_BOARD_SA5, "HP Smart Array P410i" },
329 { 0x103C, 0x3247, CISS_BOARD_SA5, "HP Smart Array P411" },
330 { 0x103C, 0x3249, CISS_BOARD_SA5, "HP Smart Array P812" },
331 { 0x103C, 0x324A, CISS_BOARD_SA5, "HP Smart Array P712m" },
332 { 0x103C, 0x324B, CISS_BOARD_SA5, "HP Smart Array" },
333 { 0x103C, 0x3350, CISS_BOARD_SA5, "HP Smart Array P222" },
334 { 0x103C, 0x3351, CISS_BOARD_SA5, "HP Smart Array P420" },
335 { 0x103C, 0x3352, CISS_BOARD_SA5, "HP Smart Array P421" },
336 { 0x103C, 0x3353, CISS_BOARD_SA5, "HP Smart Array P822" },
337 { 0x103C, 0x3354, CISS_BOARD_SA5, "HP Smart Array P420i" },
338 { 0x103C, 0x3355, CISS_BOARD_SA5, "HP Smart Array P220i" },
339 { 0x103C, 0x3356, CISS_BOARD_SA5, "HP Smart Array P721m" },
340 { 0x103C, 0x1920, CISS_BOARD_SA5, "HP Smart Array P430i" },
341 { 0x103C, 0x1921, CISS_BOARD_SA5, "HP Smart Array P830i" },
342 { 0x103C, 0x1922, CISS_BOARD_SA5, "HP Smart Array P430" },
343 { 0x103C, 0x1923, CISS_BOARD_SA5, "HP Smart Array P431" },
344 { 0x103C, 0x1924, CISS_BOARD_SA5, "HP Smart Array P830" },
345 { 0x103C, 0x1926, CISS_BOARD_SA5, "HP Smart Array P731m" },
346 { 0x103C, 0x1928, CISS_BOARD_SA5, "HP Smart Array P230i" },
347 { 0x103C, 0x1929, CISS_BOARD_SA5, "HP Smart Array P530" },
348 { 0x103C, 0x192A, CISS_BOARD_SA5, "HP Smart Array P531" },
349 { 0x103C, 0x21BD, CISS_BOARD_SA5, "HP Smart Array P244br" },
350 { 0x103C, 0x21BE, CISS_BOARD_SA5, "HP Smart Array P741m" },
351 { 0x103C, 0x21BF, CISS_BOARD_SA5, "HP Smart Array H240ar" },
352 { 0x103C, 0x21C0, CISS_BOARD_SA5, "HP Smart Array P440ar" },
353 { 0x103C, 0x21C1, CISS_BOARD_SA5, "HP Smart Array P840ar" },
354 { 0x103C, 0x21C2, CISS_BOARD_SA5, "HP Smart Array P440" },
355 { 0x103C, 0x21C3, CISS_BOARD_SA5, "HP Smart Array P441" },
356 { 0x103C, 0x21C5, CISS_BOARD_SA5, "HP Smart Array P841" },
357 { 0x103C, 0x21C6, CISS_BOARD_SA5, "HP Smart Array H244br" },
358 { 0x103C, 0x21C7, CISS_BOARD_SA5, "HP Smart Array H240" },
359 { 0x103C, 0x21C8, CISS_BOARD_SA5, "HP Smart Array H241" },
360 { 0x103C, 0x21CA, CISS_BOARD_SA5, "HP Smart Array P246br" },
361 { 0x103C, 0x21CB, CISS_BOARD_SA5, "HP Smart Array P840" },
362 { 0x103C, 0x21CC, CISS_BOARD_SA5, "HP Smart Array P542d" },
363 { 0x103C, 0x21CD, CISS_BOARD_SA5, "HP Smart Array P240nr" },
364 { 0x103C, 0x21CE, CISS_BOARD_SA5, "HP Smart Array H240nr" },
368 DRIVER_MODULE(ciss, pci, ciss_pci_driver, 0, 0);
369 MODULE_PNP_INFO("U16:vendor;U16:device;", pci, ciss, ciss_vendor_data,
370 nitems(ciss_vendor_data) - 1);
371 MODULE_DEPEND(ciss, cam, 1, 1, 1);
372 MODULE_DEPEND(ciss, pci, 1, 1, 1);
374 /************************************************************************
375 * Find a match for the device in our list of known adapters.
378 ciss_lookup(device_t dev)
382 for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
383 if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
384 (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
390 /************************************************************************
391 * Match a known CISS adapter.
394 ciss_probe(device_t dev)
398 i = ciss_lookup(dev);
400 device_set_desc(dev, ciss_vendor_data[i].desc);
401 return(BUS_PROBE_DEFAULT);
406 /************************************************************************
407 * Attach the driver to this adapter.
410 ciss_attach(device_t dev)
412 struct ciss_softc *sc;
418 /* print structure/union sizes */
419 debug_struct(ciss_command);
420 debug_struct(ciss_header);
421 debug_union(ciss_device_address);
422 debug_struct(ciss_cdb);
423 debug_struct(ciss_report_cdb);
424 debug_struct(ciss_notify_cdb);
425 debug_struct(ciss_notify);
426 debug_struct(ciss_message_cdb);
427 debug_struct(ciss_error_info_pointer);
428 debug_struct(ciss_error_info);
429 debug_struct(ciss_sg_entry);
430 debug_struct(ciss_config_table);
431 debug_struct(ciss_bmic_cdb);
432 debug_struct(ciss_bmic_id_ldrive);
433 debug_struct(ciss_bmic_id_lstatus);
434 debug_struct(ciss_bmic_id_table);
435 debug_struct(ciss_bmic_id_pdrive);
436 debug_struct(ciss_bmic_blink_pdrive);
437 debug_struct(ciss_bmic_flush_cache);
438 debug_const(CISS_MAX_REQUESTS);
439 debug_const(CISS_MAX_LOGICAL);
440 debug_const(CISS_INTERRUPT_COALESCE_DELAY);
441 debug_const(CISS_INTERRUPT_COALESCE_COUNT);
442 debug_const(CISS_COMMAND_ALLOC_SIZE);
443 debug_const(CISS_COMMAND_SG_LENGTH);
445 debug_type(cciss_pci_info_struct);
446 debug_type(cciss_coalint_struct);
447 debug_type(cciss_coalint_struct);
448 debug_type(NodeName_type);
449 debug_type(NodeName_type);
450 debug_type(Heartbeat_type);
451 debug_type(BusTypes_type);
452 debug_type(FirmwareVer_type);
453 debug_type(DriverVer_type);
454 debug_type(IOCTL_Command_struct);
457 sc = device_get_softc(dev);
459 mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
460 callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);
463 * Do PCI-specific init.
465 if ((error = ciss_init_pci(sc)) != 0)
469 * Initialise driver queues.
472 ciss_initq_notify(sc);
475 * Initialize device sysctls.
477 ciss_init_sysctl(sc);
480 * Initialise command/request pool.
482 if ((error = ciss_init_requests(sc)) != 0)
486 * Get adapter information.
488 if ((error = ciss_identify_adapter(sc)) != 0)
492 * Find all the physical devices.
494 if ((error = ciss_init_physical(sc)) != 0)
498 * Build our private table of logical devices.
500 if ((error = ciss_init_logical(sc)) != 0)
504 * Enable interrupts so that the CAM scan can complete.
506 CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
509 * Initialise the CAM interface.
511 if ((error = ciss_cam_init(sc)) != 0)
515 * Start the heartbeat routine and event chain.
520 * Create the control device.
522 sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
523 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
524 "ciss%d", device_get_unit(sc->ciss_dev));
525 sc->ciss_dev_t->si_drv1 = sc;
528 * The adapter is running; synchronous commands can now sleep
529 * waiting for an interrupt to signal completion.
531 sc->ciss_flags |= CISS_FLAG_RUNNING;
533 ciss_spawn_notify_thread(sc);
538 /* ciss_free() expects the mutex to be held */
539 mtx_lock(&sc->ciss_mtx);
545 /************************************************************************
546 * Detach the driver from this adapter.
549 ciss_detach(device_t dev)
551 struct ciss_softc *sc = device_get_softc(dev);
555 mtx_lock(&sc->ciss_mtx);
556 if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
557 mtx_unlock(&sc->ciss_mtx);
561 /* flush adapter cache */
562 ciss_flush_adapter(sc);
564 /* release all resources. The mutex is released and freed here too. */
570 /************************************************************************
571 * Prepare adapter for system shutdown.
574 ciss_shutdown(device_t dev)
576 struct ciss_softc *sc = device_get_softc(dev);
580 mtx_lock(&sc->ciss_mtx);
581 /* flush adapter cache */
582 ciss_flush_adapter(sc);
584 if (sc->ciss_soft_reset)
586 mtx_unlock(&sc->ciss_mtx);
592 ciss_init_sysctl(struct ciss_softc *sc)
595 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
596 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
597 OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
600 /************************************************************************
601 * Perform PCI-specific attachment actions.
604 ciss_init_pci(struct ciss_softc *sc)
606 uintptr_t cbase, csize, cofs;
607 uint32_t method, supported_methods;
608 int error, sqmask, i;
614 * Work out adapter type.
616 i = ciss_lookup(sc->ciss_dev);
618 ciss_printf(sc, "unknown adapter type\n");
622 if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
623 sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
624 } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
625 sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
628 * XXX Big hammer, masks/unmasks all possible interrupts. This should
629 * work on all hardware variants. Need to add code to handle the
630 * "controller crashed" interrupt bit that this unmasks.
636 * Allocate register window first (we need this to find the config
640 sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
641 if ((sc->ciss_regs_resource =
642 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
643 &sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
644 ciss_printf(sc, "can't allocate register window\n");
647 sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
648 sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
651 * Find the BAR holding the config structure. If it's not the one
652 * we already mapped for registers, map it too.
654 sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
655 if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
656 if ((sc->ciss_cfg_resource =
657 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
658 &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
659 ciss_printf(sc, "can't allocate config window\n");
662 cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
663 csize = rman_get_end(sc->ciss_cfg_resource) -
664 rman_get_start(sc->ciss_cfg_resource) + 1;
666 cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
667 csize = rman_get_end(sc->ciss_regs_resource) -
668 rman_get_start(sc->ciss_regs_resource) + 1;
670 cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
673 * Use the base/size/offset values we just calculated to
674 * sanity-check the config structure. If it's OK, point to it.
676 if ((cofs + sizeof(struct ciss_config_table)) > csize) {
677 ciss_printf(sc, "config table outside window\n");
680 sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
681 debug(1, "config struct at %p", sc->ciss_cfg);
684 * Calculate the number of request structures/commands we are
685 * going to provide for this adapter.
687 sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
690 * Validate the config structure. If we supported other transport
691 * methods, we could select amongst them at this point in time.
693 if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
694 ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
695 sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
696 sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
701 * Select the mode of operation, prefer Performant.
703 if (!(sc->ciss_cfg->supported_methods &
704 (CISS_TRANSPORT_METHOD_SIMPLE | CISS_TRANSPORT_METHOD_PERF))) {
705 ciss_printf(sc, "No supported transport layers: 0x%x\n",
706 sc->ciss_cfg->supported_methods);
709 switch (ciss_force_transport) {
711 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
714 supported_methods = CISS_TRANSPORT_METHOD_PERF;
718 * Override the capabilities of the BOARD and specify SIMPLE
721 if (ciss_vendor_data[i].flags & CISS_BOARD_SIMPLE)
722 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
724 supported_methods = sc->ciss_cfg->supported_methods;
729 if ((supported_methods & CISS_TRANSPORT_METHOD_PERF) != 0) {
730 method = CISS_TRANSPORT_METHOD_PERF;
731 sc->ciss_perf = (struct ciss_perf_config *)(cbase + cofs +
732 sc->ciss_cfg->transport_offset);
733 if (ciss_init_perf(sc)) {
734 supported_methods &= ~method;
737 } else if (supported_methods & CISS_TRANSPORT_METHOD_SIMPLE) {
738 method = CISS_TRANSPORT_METHOD_SIMPLE;
740 ciss_printf(sc, "No supported transport methods: 0x%x\n",
741 sc->ciss_cfg->supported_methods);
746 * Tell it we're using the low 4GB of RAM. Set the default interrupt
747 * coalescing options.
749 sc->ciss_cfg->requested_method = method;
750 sc->ciss_cfg->command_physlimit = 0;
751 sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
752 sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
755 sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
758 if (ciss_update_config(sc)) {
759 ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
760 CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
763 if ((sc->ciss_cfg->active_method & method) == 0) {
764 supported_methods &= ~method;
765 if (supported_methods == 0) {
766 ciss_printf(sc, "adapter refuses to go into available transports "
767 "mode (0x%x, 0x%x)\n", supported_methods,
768 sc->ciss_cfg->active_method);
775 * Wait for the adapter to come ready.
777 if ((error = ciss_wait_adapter(sc)) != 0)
780 /* Prepare to possibly use MSIX and/or PERFORMANT interrupts. Normal
781 * interrupts have a rid of 0, this will be overridden if MSIX is used.
783 sc->ciss_irq_rid[0] = 0;
784 if (method == CISS_TRANSPORT_METHOD_PERF) {
785 ciss_printf(sc, "PERFORMANT Transport\n");
786 if ((ciss_force_interrupt != 1) && (ciss_setup_msix(sc) == 0)) {
787 intr = ciss_perf_msi_intr;
789 intr = ciss_perf_intr;
791 /* XXX The docs say that the 0x01 bit is only for SAS controllers.
792 * Unfortunately, there is no good way to know if this is a SAS
793 * controller. Hopefully enabling this bit universally will work OK.
794 * It seems to work fine for SA6i controllers.
796 sc->ciss_interrupt_mask = CISS_TL_PERF_INTR_OPQ | CISS_TL_PERF_INTR_MSI;
799 ciss_printf(sc, "SIMPLE Transport\n");
800 /* MSIX doesn't seem to work in SIMPLE mode, only enable if it forced */
801 if (ciss_force_interrupt == 2)
802 /* If this fails, we automatically revert to INTx */
804 sc->ciss_perf = NULL;
806 sc->ciss_interrupt_mask = sqmask;
810 * Turn off interrupts before we go routing anything.
812 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
815 * Allocate and set up our interrupt.
817 if ((sc->ciss_irq_resource =
818 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid[0],
819 RF_ACTIVE | RF_SHAREABLE)) == NULL) {
820 ciss_printf(sc, "can't allocate interrupt\n");
824 if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
825 INTR_TYPE_CAM|INTR_MPSAFE, NULL, intr, sc,
827 ciss_printf(sc, "can't set up interrupt\n");
832 * Allocate the parent bus DMA tag appropriate for our PCI
835 * Note that "simple" adapters can only address within a 32-bit
838 if (bus_dma_tag_create(bus_get_dma_tag(sc->ciss_dev),/* PCI parent */
839 1, 0, /* alignment, boundary */
840 BUS_SPACE_MAXADDR, /* lowaddr */
841 BUS_SPACE_MAXADDR, /* highaddr */
842 NULL, NULL, /* filter, filterarg */
843 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
844 BUS_SPACE_UNRESTRICTED, /* nsegments */
845 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
847 NULL, NULL, /* lockfunc, lockarg */
848 &sc->ciss_parent_dmat)) {
849 ciss_printf(sc, "can't allocate parent DMA tag\n");
854 * Create DMA tag for mapping buffers into adapter-addressable
857 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
858 1, 0, /* alignment, boundary */
859 BUS_SPACE_MAXADDR, /* lowaddr */
860 BUS_SPACE_MAXADDR, /* highaddr */
861 NULL, NULL, /* filter, filterarg */
862 (CISS_MAX_SG_ELEMENTS - 1) * PAGE_SIZE, /* maxsize */
863 CISS_MAX_SG_ELEMENTS, /* nsegments */
864 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
865 BUS_DMA_ALLOCNOW, /* flags */
866 busdma_lock_mutex, &sc->ciss_mtx, /* lockfunc, lockarg */
867 &sc->ciss_buffer_dmat)) {
868 ciss_printf(sc, "can't allocate buffer DMA tag\n");
874 /************************************************************************
875 * Setup MSI/MSIX operation (Performant only)
876 * Four interrupts are available, but we only use 1 right now. If MSI-X
877 * isn't avaialble, try using MSI instead.
880 ciss_setup_msix(struct ciss_softc *sc)
884 /* Weed out devices that don't actually support MSI */
885 i = ciss_lookup(sc->ciss_dev);
886 if (ciss_vendor_data[i].flags & CISS_BOARD_NOMSI)
890 * Only need to use the minimum number of MSI vectors, as the driver
891 * doesn't support directed MSIX interrupts.
893 val = pci_msix_count(sc->ciss_dev);
894 if (val < CISS_MSI_COUNT) {
895 val = pci_msi_count(sc->ciss_dev);
896 device_printf(sc->ciss_dev, "got %d MSI messages]\n", val);
897 if (val < CISS_MSI_COUNT)
900 val = MIN(val, CISS_MSI_COUNT);
901 if (pci_alloc_msix(sc->ciss_dev, &val) != 0) {
902 if (pci_alloc_msi(sc->ciss_dev, &val) != 0)
908 ciss_printf(sc, "Using %d MSIX interrupt%s\n", val,
909 (val != 1) ? "s" : "");
911 for (i = 0; i < val; i++)
912 sc->ciss_irq_rid[i] = i + 1;
918 /************************************************************************
919 * Setup the Performant structures.
922 ciss_init_perf(struct ciss_softc *sc)
924 struct ciss_perf_config *pc = sc->ciss_perf;
928 * Create the DMA tag for the reply queue.
930 reply_size = sizeof(uint64_t) * sc->ciss_max_requests;
931 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
932 1, 0, /* alignment, boundary */
933 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
934 BUS_SPACE_MAXADDR, /* highaddr */
935 NULL, NULL, /* filter, filterarg */
936 reply_size, 1, /* maxsize, nsegments */
937 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
939 NULL, NULL, /* lockfunc, lockarg */
940 &sc->ciss_reply_dmat)) {
941 ciss_printf(sc, "can't allocate reply DMA tag\n");
945 * Allocate memory and make it available for DMA.
947 if (bus_dmamem_alloc(sc->ciss_reply_dmat, (void **)&sc->ciss_reply,
948 BUS_DMA_NOWAIT, &sc->ciss_reply_map)) {
949 ciss_printf(sc, "can't allocate reply memory\n");
952 bus_dmamap_load(sc->ciss_reply_dmat, sc->ciss_reply_map, sc->ciss_reply,
953 reply_size, ciss_command_map_helper, &sc->ciss_reply_phys, 0);
954 bzero(sc->ciss_reply, reply_size);
956 sc->ciss_cycle = 0x1;
960 * Preload the fetch table with common command sizes. This allows the
961 * hardware to not waste bus cycles for typical i/o commands, but also not
962 * tax the driver to be too exact in choosing sizes. The table is optimized
963 * for page-aligned i/o's, but since most i/o comes from the various pagers,
964 * it's a reasonable assumption to make.
966 pc->fetch_count[CISS_SG_FETCH_NONE] = (sizeof(struct ciss_command) + 15) / 16;
967 pc->fetch_count[CISS_SG_FETCH_1] =
968 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 1 + 15) / 16;
969 pc->fetch_count[CISS_SG_FETCH_2] =
970 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 2 + 15) / 16;
971 pc->fetch_count[CISS_SG_FETCH_4] =
972 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 4 + 15) / 16;
973 pc->fetch_count[CISS_SG_FETCH_8] =
974 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 8 + 15) / 16;
975 pc->fetch_count[CISS_SG_FETCH_16] =
976 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 16 + 15) / 16;
977 pc->fetch_count[CISS_SG_FETCH_32] =
978 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 32 + 15) / 16;
979 pc->fetch_count[CISS_SG_FETCH_MAX] = (CISS_COMMAND_ALLOC_SIZE + 15) / 16;
981 pc->rq_size = sc->ciss_max_requests; /* XXX less than the card supports? */
982 pc->rq_count = 1; /* XXX Hardcode for a single queue */
985 pc->rq[0].rq_addr_hi = 0x0;
986 pc->rq[0].rq_addr_lo = sc->ciss_reply_phys;
991 /************************************************************************
992 * Wait for the adapter to come ready.
995 ciss_wait_adapter(struct ciss_softc *sc)
1002 * Wait for the adapter to come ready.
1004 if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
1005 ciss_printf(sc, "waiting for adapter to come ready...\n");
1006 for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
1007 DELAY(1000000); /* one second */
1009 ciss_printf(sc, "timed out waiting for adapter to come ready\n");
1017 /************************************************************************
1018 * Flush the adapter cache.
1021 ciss_flush_adapter(struct ciss_softc *sc)
1023 struct ciss_request *cr;
1024 struct ciss_bmic_flush_cache *cbfc;
1025 int error, command_status;
1033 * Build a BMIC request to flush the cache. We don't disable
1034 * it, as we may be going to do more I/O (eg. we are emulating
1035 * the Synchronise Cache command).
1037 if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1041 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
1042 (void **)&cbfc, sizeof(*cbfc))) != 0)
1046 * Submit the request and wait for it to complete.
1048 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1049 ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
1056 ciss_report_request(cr, &command_status, NULL);
1057 switch(command_status) {
1058 case CISS_CMD_STATUS_SUCCESS:
1061 ciss_printf(sc, "error flushing cache (%s)\n",
1062 ciss_name_command_status(command_status));
1069 free(cbfc, CISS_MALLOC_CLASS);
1071 ciss_release_request(cr);
1076 ciss_soft_reset(struct ciss_softc *sc)
1078 struct ciss_request *cr = NULL;
1079 struct ciss_command *cc;
1082 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1083 /* only reset proxy controllers */
1084 if (sc->ciss_controllers[i].physical.bus == 0)
1087 if ((error = ciss_get_request(sc, &cr)) != 0)
1090 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
1095 cc->header.address = sc->ciss_controllers[i];
1097 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
1100 ciss_release_request(cr);
1104 ciss_printf(sc, "error resetting controller (%d)\n", error);
1107 ciss_release_request(cr);
1110 /************************************************************************
1111 * Allocate memory for the adapter command structures, initialise
1112 * the request structures.
1114 * Note that the entire set of commands are allocated in a single
1118 ciss_init_requests(struct ciss_softc *sc)
1120 struct ciss_request *cr;
1126 ciss_printf(sc, "using %d of %d available commands\n",
1127 sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
1130 * Create the DMA tag for commands.
1132 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
1133 32, 0, /* alignment, boundary */
1134 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1135 BUS_SPACE_MAXADDR, /* highaddr */
1136 NULL, NULL, /* filter, filterarg */
1137 CISS_COMMAND_ALLOC_SIZE *
1138 sc->ciss_max_requests, 1, /* maxsize, nsegments */
1139 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1141 NULL, NULL, /* lockfunc, lockarg */
1142 &sc->ciss_command_dmat)) {
1143 ciss_printf(sc, "can't allocate command DMA tag\n");
1147 * Allocate memory and make it available for DMA.
1149 if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
1150 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
1151 ciss_printf(sc, "can't allocate command memory\n");
1154 bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map,sc->ciss_command,
1155 CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
1156 ciss_command_map_helper, &sc->ciss_command_phys, 0);
1157 bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
1160 * Set up the request and command structures, push requests onto
1163 for (i = 1; i < sc->ciss_max_requests; i++) {
1164 cr = &sc->ciss_request[i];
1167 cr->cr_cc = (struct ciss_command *)((uintptr_t)sc->ciss_command +
1168 CISS_COMMAND_ALLOC_SIZE * i);
1169 cr->cr_ccphys = sc->ciss_command_phys + CISS_COMMAND_ALLOC_SIZE * i;
1170 bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
1171 ciss_enqueue_free(cr);
1177 ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1182 *addr = segs[0].ds_addr;
1185 /************************************************************************
1186 * Identify the adapter, print some information about it.
1189 ciss_identify_adapter(struct ciss_softc *sc)
1191 struct ciss_request *cr;
1192 int error, command_status;
1199 * Get a request, allocate storage for the adapter data.
1201 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
1202 (void **)&sc->ciss_id,
1203 sizeof(*sc->ciss_id))) != 0)
1207 * Submit the request and wait for it to complete.
1209 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1210 ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
1217 ciss_report_request(cr, &command_status, NULL);
1218 switch(command_status) {
1219 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1221 case CISS_CMD_STATUS_DATA_UNDERRUN:
1222 case CISS_CMD_STATUS_DATA_OVERRUN:
1223 ciss_printf(sc, "data over/underrun reading adapter information\n");
1225 ciss_printf(sc, "error reading adapter information (%s)\n",
1226 ciss_name_command_status(command_status));
1231 /* sanity-check reply */
1232 if (!(sc->ciss_id->controller_flags & CONTROLLER_FLAGS_BIG_MAP_SUPPORT)) {
1233 ciss_printf(sc, "adapter does not support BIG_MAP\n");
1239 /* XXX later revisions may not need this */
1240 sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
1243 /* XXX only really required for old 5300 adapters? */
1244 sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
1247 * Earlier controller specs do not contain these config
1248 * entries, so assume that a 0 means its old and assign
1249 * these values to the defaults that were established
1250 * when this driver was developed for them
1252 if (sc->ciss_cfg->max_logical_supported == 0)
1253 sc->ciss_cfg->max_logical_supported = CISS_MAX_LOGICAL;
1254 if (sc->ciss_cfg->max_physical_supported == 0)
1255 sc->ciss_cfg->max_physical_supported = CISS_MAX_PHYSICAL;
1256 /* print information */
1258 ciss_printf(sc, " %d logical drive%s configured\n",
1259 sc->ciss_id->configured_logical_drives,
1260 (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
1261 ciss_printf(sc, " firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
1262 ciss_printf(sc, " %d SCSI channels\n", sc->ciss_id->scsi_chip_count);
1264 ciss_printf(sc, " signature '%.4s'\n", sc->ciss_cfg->signature);
1265 ciss_printf(sc, " valence %d\n", sc->ciss_cfg->valence);
1266 ciss_printf(sc, " supported I/O methods 0x%b\n",
1267 sc->ciss_cfg->supported_methods,
1268 "\20\1READY\2simple\3performant\4MEMQ\n");
1269 ciss_printf(sc, " active I/O method 0x%b\n",
1270 sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
1271 ciss_printf(sc, " 4G page base 0x%08x\n",
1272 sc->ciss_cfg->command_physlimit);
1273 ciss_printf(sc, " interrupt coalesce delay %dus\n",
1274 sc->ciss_cfg->interrupt_coalesce_delay);
1275 ciss_printf(sc, " interrupt coalesce count %d\n",
1276 sc->ciss_cfg->interrupt_coalesce_count);
1277 ciss_printf(sc, " max outstanding commands %d\n",
1278 sc->ciss_cfg->max_outstanding_commands);
1279 ciss_printf(sc, " bus types 0x%b\n", sc->ciss_cfg->bus_types,
1280 "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
1281 ciss_printf(sc, " server name '%.16s'\n", sc->ciss_cfg->server_name);
1282 ciss_printf(sc, " heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
1283 ciss_printf(sc, " max logical volumes: %d\n", sc->ciss_cfg->max_logical_supported);
1284 ciss_printf(sc, " max physical disks supported: %d\n", sc->ciss_cfg->max_physical_supported);
1285 ciss_printf(sc, " max physical disks per logical volume: %d\n", sc->ciss_cfg->max_physical_per_logical);
1286 ciss_printf(sc, " JBOD Support is %s\n", (sc->ciss_id->uiYetMoreControllerFlags & YMORE_CONTROLLER_FLAGS_JBOD_SUPPORTED) ?
1287 "Available" : "Unavailable");
1288 ciss_printf(sc, " JBOD Mode is %s\n", (sc->ciss_id->PowerUPNvramFlags & PWR_UP_FLAG_JBOD_ENABLED) ?
1289 "Enabled" : "Disabled");
1294 if (sc->ciss_id != NULL) {
1295 free(sc->ciss_id, CISS_MALLOC_CLASS);
1300 ciss_release_request(cr);
1304 /************************************************************************
1305 * Helper routine for generating a list of logical and physical luns.
1307 static struct ciss_lun_report *
1308 ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
1310 struct ciss_request *cr;
1311 struct ciss_command *cc;
1312 struct ciss_report_cdb *crc;
1313 struct ciss_lun_report *cll;
1324 * Get a request, allocate storage for the address list.
1326 if ((error = ciss_get_request(sc, &cr)) != 0)
1328 report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
1329 if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1330 ciss_printf(sc, "can't allocate memory for lun report\n");
1336 * Build the Report Logical/Physical LUNs command.
1340 cr->cr_length = report_size;
1341 cr->cr_flags = CISS_REQ_DATAIN;
1343 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
1344 cc->header.address.physical.bus = 0;
1345 cc->header.address.physical.target = 0;
1346 cc->cdb.cdb_length = sizeof(*crc);
1347 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1348 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1349 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1350 cc->cdb.timeout = 30; /* XXX better suggestions? */
1352 crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
1353 bzero(crc, sizeof(*crc));
1354 crc->opcode = opcode;
1355 crc->length = htonl(report_size); /* big-endian field */
1356 cll->list_size = htonl(report_size - sizeof(*cll)); /* big-endian field */
1359 * Submit the request and wait for it to complete. (timeout
1360 * here should be much greater than above)
1362 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1363 ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
1368 * Check response. Note that data over/underrun is OK.
1370 ciss_report_request(cr, &command_status, NULL);
1371 switch(command_status) {
1372 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1373 case CISS_CMD_STATUS_DATA_UNDERRUN: /* buffer too large, not bad */
1375 case CISS_CMD_STATUS_DATA_OVERRUN:
1376 ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
1377 sc->ciss_cfg->max_logical_supported);
1380 ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
1381 ciss_name_command_status(command_status));
1385 ciss_release_request(cr);
1390 ciss_release_request(cr);
1391 if (error && cll != NULL) {
1392 free(cll, CISS_MALLOC_CLASS);
1398 /************************************************************************
1399 * Find logical drives on the adapter.
1402 ciss_init_logical(struct ciss_softc *sc)
1404 struct ciss_lun_report *cll;
1405 int error = 0, i, j;
1410 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
1411 sc->ciss_cfg->max_logical_supported);
1417 /* sanity-check reply */
1418 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1419 if ((ndrives < 0) || (ndrives > sc->ciss_cfg->max_logical_supported)) {
1420 ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
1421 ndrives, sc->ciss_cfg->max_logical_supported);
1427 * Save logical drive information.
1430 ciss_printf(sc, "%d logical drive%s\n",
1431 ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
1435 malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
1436 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1437 if (sc->ciss_logical == NULL) {
1442 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1443 sc->ciss_logical[i] =
1444 malloc(sc->ciss_cfg->max_logical_supported *
1445 sizeof(struct ciss_ldrive),
1446 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1447 if (sc->ciss_logical[i] == NULL) {
1452 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++)
1453 sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
1456 for (i = 0; i < sc->ciss_cfg->max_logical_supported; i++) {
1458 struct ciss_ldrive *ld;
1461 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
1462 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
1463 ld = &sc->ciss_logical[bus][target];
1465 ld->cl_address = cll->lun[i];
1466 ld->cl_controller = &sc->ciss_controllers[bus];
1467 if (ciss_identify_logical(sc, ld) != 0)
1470 * If the drive has had media exchanged, we should bring it online.
1472 if (ld->cl_lstatus->media_exchanged)
1473 ciss_accept_media(sc, ld);
1479 free(cll, CISS_MALLOC_CLASS);
1484 ciss_init_physical(struct ciss_softc *sc)
1486 struct ciss_lun_report *cll;
1495 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
1496 sc->ciss_cfg->max_physical_supported);
1502 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1505 ciss_printf(sc, "%d physical device%s\n",
1506 nphys, (nphys > 1 || nphys == 0) ? "s" : "");
1510 * Figure out the bus mapping.
1511 * Logical buses include both the local logical bus for local arrays and
1512 * proxy buses for remote arrays. Physical buses are numbered by the
1513 * controller and represent physical buses that hold physical devices.
1514 * We shift these bus numbers so that everything fits into a single flat
1515 * numbering space for CAM. Logical buses occupy the first 32 CAM bus
1516 * numbers, and the physical bus numbers are shifted to be above that.
1517 * This results in the various driver arrays being indexed as follows:
1519 * ciss_controllers[] - indexed by logical bus
1520 * ciss_cam_sim[] - indexed by both logical and physical, with physical
1521 * being shifted by 32.
1522 * ciss_logical[][] - indexed by logical bus
1523 * ciss_physical[][] - indexed by physical bus
1525 * XXX This is getting more and more hackish. CISS really doesn't play
1526 * well with a standard SCSI model; devices are addressed via magic
1527 * cookies, not via b/t/l addresses. Since there is no way to store
1528 * the cookie in the CAM device object, we have to keep these lookup
1529 * tables handy so that the devices can be found quickly at the cost
1530 * of wasting memory and having a convoluted lookup scheme. This
1531 * driver should probably be converted to block interface.
1534 * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
1535 * controller. A proxy controller is another physical controller
1536 * behind the primary PCI controller. We need to know about this
1537 * so that BMIC commands can be properly targeted. There can be
1538 * proxy controllers attached to a single PCI controller, so
1539 * find the highest numbered one so the array can be properly
1542 sc->ciss_max_logical_bus = 1;
1543 for (i = 0; i < nphys; i++) {
1544 if (cll->lun[i].physical.extra_address == 0) {
1545 bus = cll->lun[i].physical.bus;
1546 sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
1548 bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
1549 sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
1553 sc->ciss_controllers =
1554 malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
1555 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1557 if (sc->ciss_controllers == NULL) {
1558 ciss_printf(sc, "Could not allocate memory for controller map\n");
1563 /* setup a map of controller addresses */
1564 for (i = 0; i < nphys; i++) {
1565 if (cll->lun[i].physical.extra_address == 0) {
1566 sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
1571 malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
1572 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1573 if (sc->ciss_physical == NULL) {
1574 ciss_printf(sc, "Could not allocate memory for physical device map\n");
1579 for (i = 0; i < sc->ciss_max_physical_bus; i++) {
1580 sc->ciss_physical[i] =
1581 malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
1582 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1583 if (sc->ciss_physical[i] == NULL) {
1584 ciss_printf(sc, "Could not allocate memory for target map\n");
1590 ciss_filter_physical(sc, cll);
1594 free(cll, CISS_MALLOC_CLASS);
1600 ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
1606 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1607 for (i = 0; i < nphys; i++) {
1608 if (cll->lun[i].physical.extra_address == 0)
1612 * Filter out devices that we don't want. Level 3 LUNs could
1613 * probably be supported, but the docs don't give enough of a
1616 * The mode field of the physical address is likely set to have
1617 * hard disks masked out. Honor it unless the user has overridden
1618 * us with the tunable. We also munge the inquiry data for these
1619 * disks so that they only show up as passthrough devices. Keeping
1620 * them visible in this fashion is useful for doing things like
1621 * flashing firmware.
1623 ea = cll->lun[i].physical.extra_address;
1624 if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
1625 (CISS_EXTRA_MODE2(ea) == 0x3))
1627 if ((ciss_expose_hidden_physical == 0) &&
1628 (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
1632 * Note: CISS firmware numbers physical busses starting at '1', not
1633 * '0'. This numbering is internal to the firmware and is only
1634 * used as a hint here.
1636 bus = CISS_EXTRA_BUS2(ea) - 1;
1637 target = CISS_EXTRA_TARGET2(ea);
1638 sc->ciss_physical[bus][target].cp_address = cll->lun[i];
1639 sc->ciss_physical[bus][target].cp_online = 1;
1646 ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1648 struct ciss_request *cr;
1649 struct ciss_command *cc;
1650 struct scsi_inquiry *inq;
1656 bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1658 if ((error = ciss_get_request(sc, &cr)) != 0)
1662 cr->cr_data = &ld->cl_geometry;
1663 cr->cr_length = sizeof(ld->cl_geometry);
1664 cr->cr_flags = CISS_REQ_DATAIN;
1666 cc->header.address = ld->cl_address;
1667 cc->cdb.cdb_length = 6;
1668 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1669 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1670 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1671 cc->cdb.timeout = 30;
1673 inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1674 inq->opcode = INQUIRY;
1675 inq->byte2 = SI_EVPD;
1676 inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1677 scsi_ulto2b(sizeof(ld->cl_geometry), inq->length);
1679 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1680 ciss_printf(sc, "error getting geometry (%d)\n", error);
1684 ciss_report_request(cr, &command_status, NULL);
1685 switch(command_status) {
1686 case CISS_CMD_STATUS_SUCCESS:
1687 case CISS_CMD_STATUS_DATA_UNDERRUN:
1689 case CISS_CMD_STATUS_DATA_OVERRUN:
1690 ciss_printf(sc, "WARNING: Data overrun\n");
1693 ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1694 ciss_name_command_status(command_status));
1700 ciss_release_request(cr);
1703 /************************************************************************
1704 * Identify a logical drive, initialise state related to it.
1707 ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1709 struct ciss_request *cr;
1710 struct ciss_command *cc;
1711 struct ciss_bmic_cdb *cbc;
1712 int error, command_status;
1719 * Build a BMIC request to fetch the drive ID.
1721 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
1722 (void **)&ld->cl_ldrive,
1723 sizeof(*ld->cl_ldrive))) != 0)
1726 cc->header.address = *ld->cl_controller; /* target controller */
1727 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1728 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1731 * Submit the request and wait for it to complete.
1733 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1734 ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
1741 ciss_report_request(cr, &command_status, NULL);
1742 switch(command_status) {
1743 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1745 case CISS_CMD_STATUS_DATA_UNDERRUN:
1746 case CISS_CMD_STATUS_DATA_OVERRUN:
1747 ciss_printf(sc, "data over/underrun reading logical drive ID\n");
1749 ciss_printf(sc, "error reading logical drive ID (%s)\n",
1750 ciss_name_command_status(command_status));
1754 ciss_release_request(cr);
1758 * Build a CISS BMIC command to get the logical drive status.
1760 if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
1764 * Get the logical drive geometry.
1766 if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1770 * Print the drive's basic characteristics.
1773 ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
1774 CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
1775 CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
1776 ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
1777 ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
1778 ld->cl_ldrive->block_size));
1780 ciss_print_ldrive(sc, ld);
1784 /* make the drive not-exist */
1785 ld->cl_status = CISS_LD_NONEXISTENT;
1786 if (ld->cl_ldrive != NULL) {
1787 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
1788 ld->cl_ldrive = NULL;
1790 if (ld->cl_lstatus != NULL) {
1791 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
1792 ld->cl_lstatus = NULL;
1796 ciss_release_request(cr);
1801 /************************************************************************
1802 * Get status for a logical drive.
1804 * XXX should we also do this in response to Test Unit Ready?
1807 ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld)
1809 struct ciss_request *cr;
1810 struct ciss_command *cc;
1811 struct ciss_bmic_cdb *cbc;
1812 int error, command_status;
1815 * Build a CISS BMIC command to get the logical drive status.
1817 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
1818 (void **)&ld->cl_lstatus,
1819 sizeof(*ld->cl_lstatus))) != 0)
1822 cc->header.address = *ld->cl_controller; /* target controller */
1823 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1824 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1827 * Submit the request and wait for it to complete.
1829 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1830 ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1837 ciss_report_request(cr, &command_status, NULL);
1838 switch(command_status) {
1839 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1841 case CISS_CMD_STATUS_DATA_UNDERRUN:
1842 case CISS_CMD_STATUS_DATA_OVERRUN:
1843 ciss_printf(sc, "data over/underrun reading logical drive status\n");
1845 ciss_printf(sc, "error reading logical drive status (%s)\n",
1846 ciss_name_command_status(command_status));
1852 * Set the drive's summary status based on the returned status.
1854 * XXX testing shows that a failed JBOD drive comes back at next
1855 * boot in "queued for expansion" mode. WTF?
1857 ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
1861 ciss_release_request(cr);
1865 /************************************************************************
1866 * Notify the adapter of a config update.
1869 ciss_update_config(struct ciss_softc *sc)
1875 CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
1876 for (i = 0; i < 1000; i++) {
1877 if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
1878 CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
1886 /************************************************************************
1887 * Accept new media into a logical drive.
1889 * XXX The drive has previously been offline; it would be good if we
1890 * could make sure it's not open right now.
1893 ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
1895 struct ciss_request *cr;
1896 struct ciss_command *cc;
1897 struct ciss_bmic_cdb *cbc;
1899 int error = 0, ldrive;
1901 ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1903 debug(0, "bringing logical drive %d back online", ldrive);
1906 * Build a CISS BMIC command to bring the drive back online.
1908 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
1912 cc->header.address = *ld->cl_controller; /* target controller */
1913 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1914 cbc->log_drive = ldrive;
1917 * Submit the request and wait for it to complete.
1919 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1920 ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
1927 ciss_report_request(cr, &command_status, NULL);
1928 switch(command_status) {
1929 case CISS_CMD_STATUS_SUCCESS: /* all OK */
1930 /* we should get a logical drive status changed event here */
1933 ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
1934 ciss_name_command_status(command_status));
1940 ciss_release_request(cr);
1944 /************************************************************************
1945 * Release adapter resources.
1948 ciss_free(struct ciss_softc *sc)
1950 struct ciss_request *cr;
1955 /* we're going away */
1956 sc->ciss_flags |= CISS_FLAG_ABORTING;
1958 /* terminate the periodic heartbeat routine */
1959 callout_stop(&sc->ciss_periodic);
1961 /* cancel the Event Notify chain */
1962 ciss_notify_abort(sc);
1964 ciss_kill_notify_thread(sc);
1966 /* disconnect from CAM */
1967 if (sc->ciss_cam_sim) {
1968 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1969 if (sc->ciss_cam_sim[i]) {
1970 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1971 cam_sim_free(sc->ciss_cam_sim[i], 0);
1974 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
1975 CISS_PHYSICAL_BASE; i++) {
1976 if (sc->ciss_cam_sim[i]) {
1977 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1978 cam_sim_free(sc->ciss_cam_sim[i], 0);
1981 free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
1983 if (sc->ciss_cam_devq)
1984 cam_simq_free(sc->ciss_cam_devq);
1986 /* remove the control device */
1987 mtx_unlock(&sc->ciss_mtx);
1988 if (sc->ciss_dev_t != NULL)
1989 destroy_dev(sc->ciss_dev_t);
1991 /* Final cleanup of the callout. */
1992 callout_drain(&sc->ciss_periodic);
1993 mtx_destroy(&sc->ciss_mtx);
1995 /* free the controller data */
1996 if (sc->ciss_id != NULL)
1997 free(sc->ciss_id, CISS_MALLOC_CLASS);
1999 /* release I/O resources */
2000 if (sc->ciss_regs_resource != NULL)
2001 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
2002 sc->ciss_regs_rid, sc->ciss_regs_resource);
2003 if (sc->ciss_cfg_resource != NULL)
2004 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
2005 sc->ciss_cfg_rid, sc->ciss_cfg_resource);
2006 if (sc->ciss_intr != NULL)
2007 bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
2008 if (sc->ciss_irq_resource != NULL)
2009 bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
2010 sc->ciss_irq_rid[0], sc->ciss_irq_resource);
2012 pci_release_msi(sc->ciss_dev);
2014 while ((cr = ciss_dequeue_free(sc)) != NULL)
2015 bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
2016 if (sc->ciss_buffer_dmat)
2017 bus_dma_tag_destroy(sc->ciss_buffer_dmat);
2019 /* destroy command memory and DMA tag */
2020 if (sc->ciss_command != NULL) {
2021 bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
2022 bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
2024 if (sc->ciss_command_dmat)
2025 bus_dma_tag_destroy(sc->ciss_command_dmat);
2027 if (sc->ciss_reply) {
2028 bus_dmamap_unload(sc->ciss_reply_dmat, sc->ciss_reply_map);
2029 bus_dmamem_free(sc->ciss_reply_dmat, sc->ciss_reply, sc->ciss_reply_map);
2031 if (sc->ciss_reply_dmat)
2032 bus_dma_tag_destroy(sc->ciss_reply_dmat);
2034 /* destroy DMA tags */
2035 if (sc->ciss_parent_dmat)
2036 bus_dma_tag_destroy(sc->ciss_parent_dmat);
2037 if (sc->ciss_logical) {
2038 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2039 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
2040 if (sc->ciss_logical[i][j].cl_ldrive)
2041 free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
2042 if (sc->ciss_logical[i][j].cl_lstatus)
2043 free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
2045 free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
2047 free(sc->ciss_logical, CISS_MALLOC_CLASS);
2050 if (sc->ciss_physical) {
2051 for (i = 0; i < sc->ciss_max_physical_bus; i++)
2052 free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
2053 free(sc->ciss_physical, CISS_MALLOC_CLASS);
2056 if (sc->ciss_controllers)
2057 free(sc->ciss_controllers, CISS_MALLOC_CLASS);
2061 /************************************************************************
2062 * Give a command to the adapter.
2064 * Note that this uses the simple transport layer directly. If we
2065 * want to add support for other layers, we'll need a switch of some
2068 * Note that the simple transport layer has no way of refusing a
2069 * command; we only have as many request structures as the adapter
2070 * supports commands, so we don't have to check (this presumes that
2071 * the adapter can handle commands as fast as we throw them at it).
2074 ciss_start(struct ciss_request *cr)
2078 debug(2, "post command %d tag %d ", cr->cr_tag, cr->cr_cc->header.host_tag);
2081 * Map the request's data.
2083 if ((error = ciss_map_request(cr)))
2087 ciss_print_request(cr);
2093 /************************************************************************
2094 * Fetch completed request(s) from the adapter, queue them for
2095 * completion handling.
2097 * Note that this uses the simple transport layer directly. If we
2098 * want to add support for other layers, we'll need a switch of some
2101 * Note that the simple transport mechanism does not require any
2102 * reentrancy protection; the OPQ read is atomic. If there is a
2103 * chance of a race with something else that might move the request
2104 * off the busy list, then we will have to lock against that
2105 * (eg. timeouts, etc.)
2108 ciss_done(struct ciss_softc *sc, cr_qhead_t *qh)
2110 struct ciss_request *cr;
2111 struct ciss_command *cc;
2112 u_int32_t tag, index;
2117 * Loop quickly taking requests from the adapter and moving them
2118 * to the completed queue.
2121 tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
2122 if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
2125 debug(2, "completed command %d%s", index,
2126 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2127 if (index >= sc->ciss_max_requests) {
2128 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2131 cr = &(sc->ciss_request[index]);
2133 cc->header.host_tag = tag; /* not updated by adapter */
2134 ciss_enqueue_complete(cr, qh);
2140 ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh)
2142 struct ciss_request *cr;
2143 struct ciss_command *cc;
2144 u_int32_t tag, index;
2149 * Loop quickly taking requests from the adapter and moving them
2150 * to the completed queue.
2153 tag = sc->ciss_reply[sc->ciss_rqidx];
2154 if ((tag & CISS_CYCLE_MASK) != sc->ciss_cycle)
2157 debug(2, "completed command %d%s\n", index,
2158 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2159 if (index < sc->ciss_max_requests) {
2160 cr = &(sc->ciss_request[index]);
2162 cc->header.host_tag = tag; /* not updated by adapter */
2163 ciss_enqueue_complete(cr, qh);
2165 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2167 if (++sc->ciss_rqidx == sc->ciss_max_requests) {
2169 sc->ciss_cycle ^= 1;
2175 /************************************************************************
2176 * Take an interrupt from the adapter.
2179 ciss_intr(void *arg)
2182 struct ciss_softc *sc = (struct ciss_softc *)arg;
2185 * The only interrupt we recognise indicates that there are
2186 * entries in the outbound post queue.
2190 mtx_lock(&sc->ciss_mtx);
2191 ciss_complete(sc, &qh);
2192 mtx_unlock(&sc->ciss_mtx);
2196 ciss_perf_intr(void *arg)
2198 struct ciss_softc *sc = (struct ciss_softc *)arg;
2200 /* Clear the interrupt and flush the bridges. Docs say that the flush
2201 * needs to be done twice, which doesn't seem right.
2203 CISS_TL_PERF_CLEAR_INT(sc);
2204 CISS_TL_PERF_FLUSH_INT(sc);
2206 ciss_perf_msi_intr(sc);
2210 ciss_perf_msi_intr(void *arg)
2213 struct ciss_softc *sc = (struct ciss_softc *)arg;
2216 ciss_perf_done(sc, &qh);
2217 mtx_lock(&sc->ciss_mtx);
2218 ciss_complete(sc, &qh);
2219 mtx_unlock(&sc->ciss_mtx);
2222 /************************************************************************
2223 * Process completed requests.
2225 * Requests can be completed in three fashions:
2227 * - by invoking a callback function (cr_complete is non-null)
2228 * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
2229 * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
2232 ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh)
2234 struct ciss_request *cr;
2239 * Loop taking requests off the completed queue and performing
2240 * completion processing on them.
2243 if ((cr = ciss_dequeue_complete(sc, qh)) == NULL)
2245 ciss_unmap_request(cr);
2247 if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
2248 ciss_printf(sc, "WARNING: completing non-busy request\n");
2249 cr->cr_flags &= ~CISS_REQ_BUSY;
2252 * If the request has a callback, invoke it.
2254 if (cr->cr_complete != NULL) {
2255 cr->cr_complete(cr);
2260 * If someone is sleeping on this request, wake them up.
2262 if (cr->cr_flags & CISS_REQ_SLEEP) {
2263 cr->cr_flags &= ~CISS_REQ_SLEEP;
2269 * If someone is polling this request for completion, signal.
2271 if (cr->cr_flags & CISS_REQ_POLL) {
2272 cr->cr_flags &= ~CISS_REQ_POLL;
2277 * Give up and throw the request back on the free queue. This
2278 * should never happen; resources will probably be lost.
2280 ciss_printf(sc, "WARNING: completed command with no submitter\n");
2281 ciss_enqueue_free(cr);
2285 /************************************************************************
2286 * Report on the completion status of a request, and pass back SCSI
2287 * and command status values.
2290 _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func)
2292 struct ciss_command *cc;
2293 struct ciss_error_info *ce;
2298 ce = (struct ciss_error_info *)&(cc->sg[0]);
2301 * We don't consider data under/overrun an error for the Report
2302 * Logical/Physical LUNs commands.
2304 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
2305 ((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
2306 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
2307 ((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
2308 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
2309 (cc->cdb.cdb[0] == INQUIRY))) {
2310 cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
2311 debug(2, "ignoring irrelevant under/overrun error");
2315 * Check the command's error bit, if clear, there's no status and
2318 if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
2319 if (scsi_status != NULL)
2320 *scsi_status = SCSI_STATUS_OK;
2321 if (command_status != NULL)
2322 *command_status = CISS_CMD_STATUS_SUCCESS;
2325 if (command_status != NULL)
2326 *command_status = ce->command_status;
2327 if (scsi_status != NULL) {
2328 if (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN) {
2329 *scsi_status = SCSI_STATUS_OK;
2330 } else if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
2331 *scsi_status = ce->scsi_status;
2336 if (bootverbose && ce->command_status != CISS_CMD_STATUS_DATA_UNDERRUN)
2337 ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
2338 ce->command_status, ciss_name_command_status(ce->command_status),
2340 if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
2341 ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x, function %s\n",
2342 ce->additional_error_info.invalid_command.offense_size,
2343 ce->additional_error_info.invalid_command.offense_offset,
2344 ce->additional_error_info.invalid_command.offense_value,
2349 ciss_print_request(cr);
2354 /************************************************************************
2355 * Issue a request and don't return until it's completed.
2357 * Depending on adapter status, we may poll or sleep waiting for
2361 ciss_synch_request(struct ciss_request *cr, int timeout)
2363 if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
2364 return(ciss_wait_request(cr, timeout));
2366 return(ciss_poll_request(cr, timeout));
2370 /************************************************************************
2371 * Issue a request and poll for completion.
2373 * Timeout in milliseconds.
2376 ciss_poll_request(struct ciss_request *cr, int timeout)
2379 struct ciss_softc *sc;
2386 cr->cr_flags |= CISS_REQ_POLL;
2387 if ((error = ciss_start(cr)) != 0)
2392 ciss_perf_done(sc, &qh);
2395 ciss_complete(sc, &qh);
2396 if (!(cr->cr_flags & CISS_REQ_POLL))
2399 } while (timeout-- >= 0);
2400 return(EWOULDBLOCK);
2403 /************************************************************************
2404 * Issue a request and sleep waiting for completion.
2406 * Timeout in milliseconds. Note that a spurious wakeup will reset
2410 ciss_wait_request(struct ciss_request *cr, int timeout)
2416 cr->cr_flags |= CISS_REQ_SLEEP;
2417 if ((error = ciss_start(cr)) != 0)
2420 while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
2421 error = msleep_sbt(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ",
2422 SBT_1MS * timeout, 0, 0);
2428 /************************************************************************
2429 * Abort a request. Note that a potential exists here to race the
2430 * request being completed; the caller must deal with this.
2433 ciss_abort_request(struct ciss_request *ar)
2435 struct ciss_request *cr;
2436 struct ciss_command *cc;
2437 struct ciss_message_cdb *cmc;
2443 if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
2446 /* build the abort command */
2448 cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL; /* addressing? */
2449 cc->header.address.physical.target = 0;
2450 cc->header.address.physical.bus = 0;
2451 cc->cdb.cdb_length = sizeof(*cmc);
2452 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
2453 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2454 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2455 cc->cdb.timeout = 30;
2457 cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
2458 cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
2459 cmc->type = CISS_MESSAGE_ABORT_TASK;
2460 cmc->abort_tag = ar->cr_tag; /* endianness?? */
2463 * Send the request and wait for a response. If we believe we
2464 * aborted the request OK, clear the flag that indicates it's
2467 error = ciss_synch_request(cr, 35 * 1000);
2469 error = ciss_report_request(cr, NULL, NULL);
2470 ciss_release_request(cr);
2476 /************************************************************************
2477 * Fetch and initialise a request
2480 ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
2482 struct ciss_request *cr;
2487 * Get a request and clean it up.
2489 if ((cr = ciss_dequeue_free(sc)) == NULL)
2494 cr->cr_complete = NULL;
2495 cr->cr_private = NULL;
2496 cr->cr_sg_tag = CISS_SG_MAX; /* Backstop to prevent accidents */
2498 ciss_preen_command(cr);
2504 ciss_preen_command(struct ciss_request *cr)
2506 struct ciss_command *cc;
2510 * Clean up the command structure.
2512 * Note that we set up the error_info structure here, since the
2513 * length can be overwritten by any command.
2516 cc->header.sg_in_list = 0; /* kinda inefficient this way */
2517 cc->header.sg_total = 0;
2518 cc->header.host_tag = cr->cr_tag << 2;
2519 cc->header.host_tag_zeroes = 0;
2520 bzero(&(cc->sg[0]), CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command));
2521 cmdphys = cr->cr_ccphys;
2522 cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
2523 cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
2526 /************************************************************************
2527 * Release a request to the free list.
2530 ciss_release_request(struct ciss_request *cr)
2535 /* release the request to the free queue */
2536 ciss_requeue_free(cr);
2539 /************************************************************************
2540 * Allocate a request that will be used to send a BMIC command. Do some
2541 * of the common setup here to avoid duplicating it everywhere else.
2544 ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
2545 int opcode, void **bufp, size_t bufsize)
2547 struct ciss_request *cr;
2548 struct ciss_command *cc;
2549 struct ciss_bmic_cdb *cbc;
2562 if ((error = ciss_get_request(sc, &cr)) != 0)
2566 * Allocate data storage if requested, determine the data direction.
2569 if ((bufsize > 0) && (bufp != NULL)) {
2570 if (*bufp == NULL) {
2571 if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
2577 dataout = 1; /* we are given a buffer, so we are writing */
2582 * Build a CISS BMIC command to get the logical drive ID.
2585 cr->cr_length = bufsize;
2587 cr->cr_flags = CISS_REQ_DATAIN;
2590 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2591 cc->header.address.physical.bus = 0;
2592 cc->header.address.physical.target = 0;
2593 cc->cdb.cdb_length = sizeof(*cbc);
2594 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2595 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2596 cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
2597 cc->cdb.timeout = 0;
2599 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
2600 bzero(cbc, sizeof(*cbc));
2601 cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
2602 cbc->bmic_opcode = opcode;
2603 cbc->size = htons((u_int16_t)bufsize);
2608 ciss_release_request(cr);
2611 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
2617 /************************************************************************
2618 * Handle a command passed in from userspace.
2621 ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
2623 struct ciss_request *cr;
2624 struct ciss_command *cc;
2625 struct ciss_error_info *ce;
2635 while (ciss_get_request(sc, &cr) != 0)
2636 msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
2640 * Allocate an in-kernel databuffer if required, copy in user data.
2642 mtx_unlock(&sc->ciss_mtx);
2643 cr->cr_length = ioc->buf_size;
2644 if (ioc->buf_size > 0) {
2645 if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
2649 if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
2650 debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2656 * Build the request based on the user command.
2658 bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
2659 bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
2661 /* XXX anything else to populate here? */
2662 mtx_lock(&sc->ciss_mtx);
2667 if ((error = ciss_synch_request(cr, 60 * 1000))) {
2668 debug(0, "request failed - %d", error);
2673 * Check to see if the command succeeded.
2675 ce = (struct ciss_error_info *)&(cc->sg[0]);
2676 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
2677 bzero(ce, sizeof(*ce));
2680 * Copy the results back to the user.
2682 bcopy(ce, &ioc->error_info, sizeof(*ce));
2683 mtx_unlock(&sc->ciss_mtx);
2684 if ((ioc->buf_size > 0) &&
2685 (error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
2686 debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2694 mtx_lock(&sc->ciss_mtx);
2697 if ((cr != NULL) && (cr->cr_data != NULL))
2698 free(cr->cr_data, CISS_MALLOC_CLASS);
2700 ciss_release_request(cr);
2704 /************************************************************************
2705 * Map a request into bus-visible space, initialise the scatter/gather
2709 ciss_map_request(struct ciss_request *cr)
2711 struct ciss_softc *sc;
2718 /* check that mapping is necessary */
2719 if (cr->cr_flags & CISS_REQ_MAPPED)
2722 cr->cr_flags |= CISS_REQ_MAPPED;
2724 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2725 BUS_DMASYNC_PREWRITE);
2727 if (cr->cr_data != NULL) {
2728 if (cr->cr_flags & CISS_REQ_CCB)
2729 error = bus_dmamap_load_ccb(sc->ciss_buffer_dmat,
2730 cr->cr_datamap, cr->cr_data,
2731 ciss_request_map_helper, cr, 0);
2733 error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2734 cr->cr_data, cr->cr_length,
2735 ciss_request_map_helper, cr, 0);
2740 * Post the command to the adapter.
2742 cr->cr_sg_tag = CISS_SG_NONE;
2743 cr->cr_flags |= CISS_REQ_BUSY;
2745 CISS_TL_PERF_POST_CMD(sc, cr);
2747 CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2754 ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2756 struct ciss_command *cc;
2757 struct ciss_request *cr;
2758 struct ciss_softc *sc;
2763 cr = (struct ciss_request *)arg;
2767 for (i = 0; i < nseg; i++) {
2768 cc->sg[i].address = segs[i].ds_addr;
2769 cc->sg[i].length = segs[i].ds_len;
2770 cc->sg[i].extension = 0;
2772 /* we leave the s/g table entirely within the command */
2773 cc->header.sg_in_list = nseg;
2774 cc->header.sg_total = nseg;
2776 if (cr->cr_flags & CISS_REQ_DATAIN)
2777 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2778 if (cr->cr_flags & CISS_REQ_DATAOUT)
2779 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2782 cr->cr_sg_tag = CISS_SG_NONE;
2784 cr->cr_sg_tag = CISS_SG_1;
2786 cr->cr_sg_tag = CISS_SG_2;
2788 cr->cr_sg_tag = CISS_SG_4;
2790 cr->cr_sg_tag = CISS_SG_8;
2791 else if (nseg <= 16)
2792 cr->cr_sg_tag = CISS_SG_16;
2793 else if (nseg <= 32)
2794 cr->cr_sg_tag = CISS_SG_32;
2796 cr->cr_sg_tag = CISS_SG_MAX;
2799 * Post the command to the adapter.
2801 cr->cr_flags |= CISS_REQ_BUSY;
2803 CISS_TL_PERF_POST_CMD(sc, cr);
2805 CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2808 /************************************************************************
2809 * Unmap a request from bus-visible space.
2812 ciss_unmap_request(struct ciss_request *cr)
2814 struct ciss_softc *sc;
2820 /* check that unmapping is necessary */
2821 if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
2824 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2825 BUS_DMASYNC_POSTWRITE);
2827 if (cr->cr_data == NULL)
2830 if (cr->cr_flags & CISS_REQ_DATAIN)
2831 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
2832 if (cr->cr_flags & CISS_REQ_DATAOUT)
2833 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
2835 bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2837 cr->cr_flags &= ~CISS_REQ_MAPPED;
2840 /************************************************************************
2841 * Attach the driver to CAM.
2843 * We put all the logical drives on a single SCSI bus.
2846 ciss_cam_init(struct ciss_softc *sc)
2853 * Allocate a devq. We can reuse this for the masked physical
2854 * devices if we decide to export these as well.
2856 if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests - 2)) == NULL) {
2857 ciss_printf(sc, "can't allocate CAM SIM queue\n");
2864 * This naturally wastes a bit of memory. The alternative is to allocate
2865 * and register each bus as it is found, and then track them on a linked
2866 * list. Unfortunately, the driver has a few places where it needs to
2867 * look up the SIM based solely on bus number, and it's unclear whether
2868 * a list traversal would work for these situations.
2870 maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
2871 CISS_PHYSICAL_BASE);
2872 sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
2873 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
2874 if (sc->ciss_cam_sim == NULL) {
2875 ciss_printf(sc, "can't allocate memory for controller SIM\n");
2879 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2880 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2882 device_get_unit(sc->ciss_dev),
2885 sc->ciss_max_requests - 2,
2886 sc->ciss_cam_devq)) == NULL) {
2887 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2892 * Register bus with this SIM.
2894 mtx_lock(&sc->ciss_mtx);
2895 if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) {
2896 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2897 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2898 mtx_unlock(&sc->ciss_mtx);
2902 mtx_unlock(&sc->ciss_mtx);
2905 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
2906 CISS_PHYSICAL_BASE; i++) {
2907 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2909 device_get_unit(sc->ciss_dev),
2911 sc->ciss_max_requests - 2,
2912 sc->ciss_cam_devq)) == NULL) {
2913 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2917 mtx_lock(&sc->ciss_mtx);
2918 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2919 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2920 mtx_unlock(&sc->ciss_mtx);
2923 mtx_unlock(&sc->ciss_mtx);
2929 /************************************************************************
2930 * Initiate a rescan of the 'logical devices' SIM
2933 ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
2939 if ((ccb = xpt_alloc_ccb_nowait()) == NULL) {
2940 ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
2944 if (xpt_create_path(&ccb->ccb_h.path, NULL,
2945 cam_sim_path(sc->ciss_cam_sim[bus]),
2946 target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2947 ciss_printf(sc, "rescan failed (can't create path)\n");
2952 /* scan is now in progress */
2955 /************************************************************************
2956 * Handle requests coming from CAM
2959 ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
2961 struct ciss_softc *sc;
2962 struct ccb_scsiio *csio;
2966 sc = cam_sim_softc(sim);
2967 bus = cam_sim_bus(sim);
2968 csio = (struct ccb_scsiio *)&ccb->csio;
2969 target = csio->ccb_h.target_id;
2970 physical = CISS_IS_PHYSICAL(bus);
2972 switch (ccb->ccb_h.func_code) {
2973 /* perform SCSI I/O */
2975 if (!ciss_cam_action_io(sim, csio))
2979 /* perform geometry calculations */
2980 case XPT_CALC_GEOMETRY:
2982 struct ccb_calc_geometry *ccg = &ccb->ccg;
2983 struct ciss_ldrive *ld;
2985 debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2989 ld = &sc->ciss_logical[bus][target];
2992 * Use the cached geometry settings unless the fault tolerance
2995 if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
2996 u_int32_t secs_per_cylinder;
2999 ccg->secs_per_track = 32;
3000 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
3001 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
3003 ccg->heads = ld->cl_geometry.heads;
3004 ccg->secs_per_track = ld->cl_geometry.sectors;
3005 ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
3007 ccb->ccb_h.status = CAM_REQ_CMP;
3011 /* handle path attribute inquiry */
3014 struct ccb_pathinq *cpi = &ccb->cpi;
3017 debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
3019 cpi->version_num = 1;
3020 cpi->hba_inquiry = PI_TAG_ABLE; /* XXX is this correct? */
3021 cpi->target_sprt = 0;
3023 cpi->max_target = sc->ciss_cfg->max_logical_supported;
3024 cpi->max_lun = 0; /* 'logical drive' channel only */
3025 cpi->initiator_id = sc->ciss_cfg->max_logical_supported;
3026 strlcpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3027 strlcpy(cpi->hba_vid, "CISS", HBA_IDLEN);
3028 strlcpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3029 cpi->unit_number = cam_sim_unit(sim);
3030 cpi->bus_id = cam_sim_bus(sim);
3031 cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */
3032 cpi->transport = XPORT_SPI;
3033 cpi->transport_version = 2;
3034 cpi->protocol = PROTO_SCSI;
3035 cpi->protocol_version = SCSI_REV_2;
3036 if (sc->ciss_cfg->max_sg_length == 0) {
3039 /* XXX Fix for ZMR cards that advertise max_sg_length == 32
3040 * Confusing bit here. max_sg_length is usually a power of 2. We always
3041 * need to subtract 1 to account for partial pages. Then we need to
3042 * align on a valid PAGE_SIZE so we round down to the nearest power of 2.
3043 * Add 1 so we can then subtract it out in the assignment to maxio.
3044 * The reason for all these shenanigans is to create a maxio value that
3045 * creates IO operations to volumes that yield consistent operations
3046 * with good performance.
3048 sg_length = sc->ciss_cfg->max_sg_length - 1;
3049 sg_length = (1 << (fls(sg_length) - 1)) + 1;
3051 cpi->maxio = (min(CISS_MAX_SG_ELEMENTS, sg_length) - 1) * PAGE_SIZE;
3052 ccb->ccb_h.status = CAM_REQ_CMP;
3056 case XPT_GET_TRAN_SETTINGS:
3058 struct ccb_trans_settings *cts = &ccb->cts;
3059 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3060 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3062 debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", cam_sim_bus(sim),
3063 ctl->ccb_h.target_id);
3064 /* disconnect always OK */
3065 cts->protocol = PROTO_SCSI;
3066 cts->protocol_version = SCSI_REV_2;
3067 cts->transport = XPORT_SPI;
3068 cts->transport_version = 2;
3070 spi->valid = CTS_SPI_VALID_DISC;
3071 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
3073 scsi->valid = CTS_SCSI_VALID_TQ;
3074 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3076 cts->ccb_h.status = CAM_REQ_CMP;
3080 default: /* we can't do this */
3081 debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
3082 ccb->ccb_h.status = CAM_REQ_INVALID;
3089 /************************************************************************
3090 * Handle a CAM SCSI I/O request.
3093 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
3095 struct ciss_softc *sc;
3097 struct ciss_request *cr;
3098 struct ciss_command *cc;
3101 sc = cam_sim_softc(sim);
3102 bus = cam_sim_bus(sim);
3103 target = csio->ccb_h.target_id;
3105 debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
3107 /* check that the CDB pointer is not to a physical address */
3108 if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
3109 debug(3, " CDB pointer is to physical address");
3110 csio->ccb_h.status = CAM_REQ_CMP_ERR;
3113 /* abandon aborted ccbs or those that have failed validation */
3114 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
3115 debug(3, "abandoning CCB due to abort/validation failure");
3119 /* handle emulation of some SCSI commands ourself */
3120 if (ciss_cam_emulate(sc, csio))
3124 * Get a request to manage this command. If we can't, return the
3125 * ccb, freeze the queue and flag so that we unfreeze it when a
3126 * request completes.
3128 if ((error = ciss_get_request(sc, &cr)) != 0) {
3129 xpt_freeze_simq(sim, 1);
3130 sc->ciss_flags |= CISS_FLAG_BUSY;
3131 csio->ccb_h.status |= CAM_REQUEUE_REQ;
3136 * Build the command.
3140 cr->cr_length = csio->dxfer_len;
3141 cr->cr_complete = ciss_cam_complete;
3142 cr->cr_private = csio;
3145 * Target the right logical volume.
3147 if (CISS_IS_PHYSICAL(bus))
3148 cc->header.address =
3149 sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
3151 cc->header.address =
3152 sc->ciss_logical[bus][target].cl_address;
3153 cc->cdb.cdb_length = csio->cdb_len;
3154 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3155 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; /* XXX ordered tags? */
3156 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
3157 cr->cr_flags = CISS_REQ_DATAOUT | CISS_REQ_CCB;
3158 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3159 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
3160 cr->cr_flags = CISS_REQ_DATAIN | CISS_REQ_CCB;
3161 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3165 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
3167 cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
3168 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
3169 bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
3171 bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
3175 * Submit the request to the adapter.
3177 * Note that this may fail if we're unable to map the request (and
3178 * if we ever learn a transport layer other than simple, may fail
3179 * if the adapter rejects the command).
3181 if ((error = ciss_start(cr)) != 0) {
3182 xpt_freeze_simq(sim, 1);
3183 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3184 if (error == EINPROGRESS) {
3187 csio->ccb_h.status |= CAM_REQUEUE_REQ;
3188 ciss_release_request(cr);
3196 /************************************************************************
3197 * Emulate SCSI commands the adapter doesn't handle as we might like.
3200 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
3205 target = csio->ccb_h.target_id;
3206 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3207 opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3208 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
3210 if (CISS_IS_PHYSICAL(bus)) {
3211 if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
3212 csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3213 xpt_done((union ccb *)csio);
3220 * Handle requests for volumes that don't exist or are not online.
3221 * A selection timeout is slightly better than an illegal request.
3222 * Other errors might be better.
3224 if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
3225 csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3226 xpt_done((union ccb *)csio);
3230 /* if we have to fake Synchronise Cache */
3231 if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
3233 * If this is a Synchronise Cache command, typically issued when
3234 * a device is closed, flush the adapter and complete now.
3236 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
3237 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
3238 ciss_flush_adapter(sc);
3239 csio->ccb_h.status |= CAM_REQ_CMP;
3240 xpt_done((union ccb *)csio);
3246 * A CISS target can only ever have one lun per target. REPORT_LUNS requires
3247 * at least one LUN field to be pre created for us, so snag it and fill in
3248 * the least significant byte indicating 1 LUN here. Emulate the command
3249 * return to shut up warning on console of a CDB error. swb
3251 if (opcode == REPORT_LUNS && csio->dxfer_len > 0) {
3252 csio->data_ptr[3] = 8;
3253 csio->ccb_h.status |= CAM_REQ_CMP;
3254 xpt_done((union ccb *)csio);
3261 /************************************************************************
3262 * Check for possibly-completed commands.
3265 ciss_cam_poll(struct cam_sim *sim)
3268 struct ciss_softc *sc = cam_sim_softc(sim);
3274 ciss_perf_done(sc, &qh);
3277 ciss_complete(sc, &qh);
3280 /************************************************************************
3281 * Handle completion of a command - pass results back through the CCB
3284 ciss_cam_complete(struct ciss_request *cr)
3286 struct ciss_softc *sc;
3287 struct ciss_command *cc;
3288 struct ciss_error_info *ce;
3289 struct ccb_scsiio *csio;
3297 ce = (struct ciss_error_info *)&(cc->sg[0]);
3298 csio = (struct ccb_scsiio *)cr->cr_private;
3301 * Extract status values from request.
3303 ciss_report_request(cr, &command_status, &scsi_status);
3304 switch(command_status) {
3305 case CISS_CMD_STATUS_DATA_UNDERRUN:
3306 csio->resid = ce->residual_count;
3308 case CISS_CMD_STATUS_SUCCESS:
3309 csio->scsi_status = scsi_status;
3310 debug(2, "SCSI_STATUS_OK");
3311 csio->ccb_h.status |= CAM_REQ_CMP;
3313 case CISS_CMD_STATUS_TARGET_STATUS:
3314 csio->scsi_status = scsi_status;
3315 bzero(&csio->sense_data, SSD_FULL_SIZE);
3316 bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
3317 if (csio->sense_len > ce->sense_length)
3318 csio->sense_resid = csio->sense_len - ce->sense_length;
3320 csio->sense_resid = 0;
3321 csio->resid = ce->residual_count;
3322 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
3324 case CISS_CMD_STATUS_DATA_OVERRUN:
3325 csio->ccb_h.status |= CAM_DATA_RUN_ERR;
3328 csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3332 /* handle post-command fixup */
3333 ciss_cam_complete_fixup(sc, csio);
3335 ciss_release_request(cr);
3336 if (sc->ciss_flags & CISS_FLAG_BUSY) {
3337 sc->ciss_flags &= ~CISS_FLAG_BUSY;
3338 if (csio->ccb_h.status & CAM_RELEASE_SIMQ)
3339 xpt_release_simq(xpt_path_sim(csio->ccb_h.path), 0);
3341 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3343 xpt_done((union ccb *)csio);
3346 /********************************************************************************
3347 * Fix up the result of some commands here.
3350 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
3352 struct scsi_inquiry_data *inq;
3353 struct ciss_ldrive *cl;
3357 cdb = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3358 (uint8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes;
3359 if (cdb[0] == INQUIRY &&
3360 (cdb[1] & SI_EVPD) == 0 &&
3361 (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
3362 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 * If the controller is in JBOD mode, there are no logical volumes.
3369 * Let the disks be probed and dealt with via CAM. Else, mask off
3370 * the physical disks and setup the parts of the inq structure for
3371 * the logical volume. swb
3373 if( !(sc->ciss_id->PowerUPNvramFlags & PWR_UP_FLAG_JBOD_ENABLED)){
3374 if (CISS_IS_PHYSICAL(bus)) {
3375 if (SID_TYPE(inq) == T_DIRECT)
3376 inq->device = (inq->device & 0xe0) | T_NODEVICE;
3379 cl = &sc->ciss_logical[bus][target];
3381 padstr(inq->vendor, "HP",
3383 padstr(inq->product,
3384 ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance),
3386 padstr(inq->revision,
3387 ciss_name_ldrive_status(cl->cl_lstatus->status),
3393 /********************************************************************************
3394 * Name the device at (target)
3396 * XXX is this strictly correct?
3399 ciss_name_device(struct ciss_softc *sc, int bus, int target)
3401 struct cam_periph *periph;
3402 struct cam_path *path;
3405 if (CISS_IS_PHYSICAL(bus))
3408 status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
3411 if (status == CAM_REQ_CMP) {
3412 xpt_path_lock(path);
3413 periph = cam_periph_find(path, NULL);
3414 xpt_path_unlock(path);
3415 xpt_free_path(path);
3416 if (periph != NULL) {
3417 sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
3418 periph->periph_name, periph->unit_number);
3422 sc->ciss_logical[bus][target].cl_name[0] = 0;
3426 /************************************************************************
3427 * Periodic status monitoring.
3430 ciss_periodic(void *arg)
3432 struct ciss_softc *sc;
3433 struct ciss_request *cr = NULL;
3434 struct ciss_command *cc = NULL;
3439 sc = (struct ciss_softc *)arg;
3442 * Check the adapter heartbeat.
3444 if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
3445 sc->ciss_heart_attack++;
3446 debug(0, "adapter heart attack in progress 0x%x/%d",
3447 sc->ciss_heartbeat, sc->ciss_heart_attack);
3448 if (sc->ciss_heart_attack == 3) {
3449 ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
3450 ciss_disable_adapter(sc);
3454 sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
3455 sc->ciss_heart_attack = 0;
3456 debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
3460 * Send the NOP message and wait for a response.
3462 if (ciss_nop_message_heartbeat != 0 && (error = ciss_get_request(sc, &cr)) == 0) {
3464 cr->cr_complete = ciss_nop_complete;
3465 cc->cdb.cdb_length = 1;
3466 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
3467 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3468 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3469 cc->cdb.timeout = 0;
3470 cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
3472 if ((error = ciss_start(cr)) != 0) {
3473 ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3478 * If the notify event request has died for some reason, or has
3479 * not started yet, restart it.
3481 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
3482 debug(0, "(re)starting Event Notify chain");
3483 ciss_notify_event(sc);
3489 callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
3493 ciss_nop_complete(struct ciss_request *cr)
3495 struct ciss_softc *sc;
3496 static int first_time = 1;
3499 if (ciss_report_request(cr, NULL, NULL) != 0) {
3500 if (first_time == 1) {
3502 ciss_printf(sc, "SENDING NOP MESSAGE FAILED (not logging anymore)\n");
3506 ciss_release_request(cr);
3509 /************************************************************************
3510 * Disable the adapter.
3512 * The all requests in completed queue is failed with hardware error.
3513 * This will cause failover in a multipath configuration.
3516 ciss_disable_adapter(struct ciss_softc *sc)
3519 struct ciss_request *cr;
3520 struct ciss_command *cc;
3521 struct ciss_error_info *ce;
3524 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
3525 pci_disable_busmaster(sc->ciss_dev);
3526 sc->ciss_flags &= ~CISS_FLAG_RUNNING;
3529 for (i = 1; i < sc->ciss_max_requests; i++) {
3530 cr = &sc->ciss_request[i];
3531 if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
3535 ce = (struct ciss_error_info *)&(cc->sg[0]);
3536 ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
3537 ciss_enqueue_complete(cr, &qh);
3541 if ((cr = ciss_dequeue_complete(sc, &qh)) == NULL)
3545 * If the request has a callback, invoke it.
3547 if (cr->cr_complete != NULL) {
3548 cr->cr_complete(cr);
3553 * If someone is sleeping on this request, wake them up.
3555 if (cr->cr_flags & CISS_REQ_SLEEP) {
3556 cr->cr_flags &= ~CISS_REQ_SLEEP;
3563 /************************************************************************
3564 * Request a notification response from the adapter.
3566 * If (cr) is NULL, this is the first request of the adapter, so
3567 * reset the adapter's message pointer and start with the oldest
3568 * message available.
3571 ciss_notify_event(struct ciss_softc *sc)
3573 struct ciss_request *cr;
3574 struct ciss_command *cc;
3575 struct ciss_notify_cdb *cnc;
3580 cr = sc->ciss_periodic_notify;
3582 /* get a request if we don't already have one */
3584 if ((error = ciss_get_request(sc, &cr)) != 0) {
3585 debug(0, "can't get notify event request");
3588 sc->ciss_periodic_notify = cr;
3589 cr->cr_complete = ciss_notify_complete;
3590 debug(1, "acquired request %d", cr->cr_tag);
3594 * Get a databuffer if we don't already have one, note that the
3595 * adapter command wants a larger buffer than the actual
3598 if (cr->cr_data == NULL) {
3599 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3600 debug(0, "can't get notify event request buffer");
3604 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3607 /* re-setup the request's command (since we never release it) XXX overkill*/
3608 ciss_preen_command(cr);
3610 /* (re)build the notify event command */
3612 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3613 cc->header.address.physical.bus = 0;
3614 cc->header.address.physical.target = 0;
3616 cc->cdb.cdb_length = sizeof(*cnc);
3617 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3618 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3619 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3620 cc->cdb.timeout = 0; /* no timeout, we hope */
3622 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3623 bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
3624 cnc->opcode = CISS_OPCODE_READ;
3625 cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
3626 cnc->timeout = 0; /* no timeout, we hope */
3627 cnc->synchronous = 0;
3629 cnc->seek_to_oldest = 0;
3630 if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
3634 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3636 /* submit the request */
3637 error = ciss_start(cr);
3642 if (cr->cr_data != NULL)
3643 free(cr->cr_data, CISS_MALLOC_CLASS);
3644 ciss_release_request(cr);
3646 sc->ciss_periodic_notify = NULL;
3647 debug(0, "can't submit notify event request");
3648 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3650 debug(1, "notify event submitted");
3651 sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
3656 ciss_notify_complete(struct ciss_request *cr)
3658 struct ciss_notify *cn;
3659 struct ciss_softc *sc;
3664 cn = (struct ciss_notify *)cr->cr_data;
3668 * Report request results, decode status.
3670 ciss_report_request(cr, &command_status, &scsi_status);
3673 * Abort the chain on a fatal error.
3675 * XXX which of these are actually errors?
3677 if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
3678 (command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
3679 (command_status != CISS_CMD_STATUS_TIMEOUT)) { /* XXX timeout? */
3680 ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
3681 ciss_name_command_status(command_status));
3682 ciss_release_request(cr);
3683 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3688 * If the adapter gave us a text message, print it.
3690 if (cn->message[0] != 0)
3691 ciss_printf(sc, "*** %.80s\n", cn->message);
3693 debug(0, "notify event class %d subclass %d detail %d",
3694 cn->class, cn->subclass, cn->detail);
3697 * If the response indicates that the notifier has been aborted,
3698 * release the notifier command.
3700 if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
3701 (cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
3702 (cn->detail == 1)) {
3703 debug(0, "notifier exiting");
3704 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3705 ciss_release_request(cr);
3706 sc->ciss_periodic_notify = NULL;
3707 wakeup(&sc->ciss_periodic_notify);
3709 /* Handle notify events in a kernel thread */
3710 ciss_enqueue_notify(cr);
3711 sc->ciss_periodic_notify = NULL;
3712 wakeup(&sc->ciss_periodic_notify);
3713 wakeup(&sc->ciss_notify);
3716 * Send a new notify event command, if we're not aborting.
3718 if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
3719 ciss_notify_event(sc);
3723 /************************************************************************
3724 * Abort the Notify Event chain.
3726 * Note that we can't just abort the command in progress; we have to
3727 * explicitly issue an Abort Notify Event command in order for the
3728 * adapter to clean up correctly.
3730 * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
3731 * the chain will not restart itself.
3734 ciss_notify_abort(struct ciss_softc *sc)
3736 struct ciss_request *cr;
3737 struct ciss_command *cc;
3738 struct ciss_notify_cdb *cnc;
3739 int error, command_status, scsi_status;
3746 /* verify that there's an outstanding command */
3747 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3750 /* get a command to issue the abort with */
3751 if ((error = ciss_get_request(sc, &cr)))
3754 /* get a buffer for the result */
3755 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3756 debug(0, "can't get notify event request buffer");
3760 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3764 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3765 cc->header.address.physical.bus = 0;
3766 cc->header.address.physical.target = 0;
3767 cc->cdb.cdb_length = sizeof(*cnc);
3768 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3769 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3770 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3771 cc->cdb.timeout = 0; /* no timeout, we hope */
3773 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3774 bzero(cnc, sizeof(*cnc));
3775 cnc->opcode = CISS_OPCODE_WRITE;
3776 cnc->command = CISS_COMMAND_ABORT_NOTIFY;
3777 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3779 ciss_print_request(cr);
3783 * Submit the request and wait for it to complete.
3785 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3786 ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
3793 ciss_report_request(cr, &command_status, &scsi_status);
3794 switch(command_status) {
3795 case CISS_CMD_STATUS_SUCCESS:
3797 case CISS_CMD_STATUS_INVALID_COMMAND:
3799 * Some older adapters don't support the CISS version of this
3800 * command. Fall back to using the BMIC version.
3802 error = ciss_notify_abort_bmic(sc);
3807 case CISS_CMD_STATUS_TARGET_STATUS:
3809 * This can happen if the adapter thinks there wasn't an outstanding
3810 * Notify Event command but we did. We clean up here.
3812 if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
3813 if (sc->ciss_periodic_notify != NULL)
3814 ciss_release_request(sc->ciss_periodic_notify);
3821 ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
3822 ciss_name_command_status(command_status));
3828 * Sleep waiting for the notifier command to complete. Note
3829 * that if it doesn't, we may end up in a bad situation, since
3830 * the adapter may deliver it later. Also note that the adapter
3831 * requires the Notify Event command to be cancelled in order to
3832 * maintain internal bookkeeping.
3834 while (sc->ciss_periodic_notify != NULL) {
3835 error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
3836 if (error == EWOULDBLOCK) {
3837 ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
3843 /* release the cancel request */
3845 if (cr->cr_data != NULL)
3846 free(cr->cr_data, CISS_MALLOC_CLASS);
3847 ciss_release_request(cr);
3850 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3854 /************************************************************************
3855 * Abort the Notify Event chain using a BMIC command.
3858 ciss_notify_abort_bmic(struct ciss_softc *sc)
3860 struct ciss_request *cr;
3861 int error, command_status;
3868 /* verify that there's an outstanding command */
3869 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3873 * Build a BMIC command to cancel the Notify on Event command.
3875 * Note that we are sending a CISS opcode here. Odd.
3877 if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
3882 * Submit the request and wait for it to complete.
3884 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3885 ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
3892 ciss_report_request(cr, &command_status, NULL);
3893 switch(command_status) {
3894 case CISS_CMD_STATUS_SUCCESS:
3897 ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
3898 ciss_name_command_status(command_status));
3905 ciss_release_request(cr);
3909 /************************************************************************
3910 * Handle rescanning all the logical volumes when a notify event
3911 * causes the drives to come online or offline.
3914 ciss_notify_rescan_logical(struct ciss_softc *sc)
3916 struct ciss_lun_report *cll;
3917 struct ciss_ldrive *ld;
3921 * We must rescan all logical volumes to get the right logical
3924 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
3925 sc->ciss_cfg->max_logical_supported);
3929 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
3932 * Delete any of the drives which were destroyed by the
3935 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
3936 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
3937 ld = &sc->ciss_logical[i][j];
3939 if (ld->cl_update == 0)
3942 if (ld->cl_status != CISS_LD_ONLINE) {
3943 ciss_cam_rescan_target(sc, i, j);
3946 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
3948 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
3950 ld->cl_ldrive = NULL;
3951 ld->cl_lstatus = NULL;
3957 * Scan for new drives.
3959 for (i = 0; i < ndrives; i++) {
3962 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
3963 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
3964 ld = &sc->ciss_logical[bus][target];
3966 if (ld->cl_update == 0)
3970 ld->cl_address = cll->lun[i];
3971 ld->cl_controller = &sc->ciss_controllers[bus];
3972 if (ciss_identify_logical(sc, ld) == 0) {
3973 ciss_cam_rescan_target(sc, bus, target);
3976 free(cll, CISS_MALLOC_CLASS);
3979 /************************************************************************
3980 * Handle a notify event relating to the status of a logical drive.
3982 * XXX need to be able to defer some of these to properly handle
3983 * calling the "ID Physical drive" command, unless the 'extended'
3984 * drive IDs are always in BIG_MAP format.
3987 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
3989 struct ciss_ldrive *ld;
3990 int ostatus, bus, target;
3994 bus = cn->device.physical.bus;
3995 target = cn->data.logical_status.logical_drive;
3996 ld = &sc->ciss_logical[bus][target];
3998 switch (cn->subclass) {
3999 case CISS_NOTIFY_LOGICAL_STATUS:
4000 switch (cn->detail) {
4002 ciss_name_device(sc, bus, target);
4003 ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
4004 cn->data.logical_status.logical_drive, ld->cl_name,
4005 ciss_name_ldrive_status(cn->data.logical_status.previous_state),
4006 ciss_name_ldrive_status(cn->data.logical_status.new_state),
4007 cn->data.logical_status.spare_state,
4008 "\20\1configured\2rebuilding\3failed\4in use\5available\n");
4011 * Update our idea of the drive's status.
4013 ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
4014 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4015 if (ld->cl_lstatus != NULL)
4016 ld->cl_lstatus->status = cn->data.logical_status.new_state;
4019 * Have CAM rescan the drive if its status has changed.
4021 if (ostatus != ld->cl_status) {
4023 ciss_notify_rescan_logical(sc);
4028 case 1: /* logical drive has recognised new media, needs Accept Media Exchange */
4029 ciss_name_device(sc, bus, target);
4030 ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
4031 cn->data.logical_status.logical_drive, ld->cl_name);
4032 ciss_accept_media(sc, ld);
4035 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4036 ciss_notify_rescan_logical(sc);
4041 ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
4042 cn->data.rebuild_aborted.logical_drive,
4044 (cn->detail == 2) ? "read" : "write");
4049 case CISS_NOTIFY_LOGICAL_ERROR:
4050 if (cn->detail == 0) {
4051 ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
4052 cn->data.io_error.logical_drive,
4054 cn->data.io_error.failure_bus,
4055 cn->data.io_error.failure_drive);
4056 /* XXX should we take the drive down at this point, or will we be told? */
4060 case CISS_NOTIFY_LOGICAL_SURFACE:
4061 if (cn->detail == 0)
4062 ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
4063 cn->data.consistency_completed.logical_drive,
4069 /************************************************************************
4070 * Handle a notify event relating to the status of a physical drive.
4073 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
4077 /************************************************************************
4078 * Handle a notify event relating to the status of a physical drive.
4081 ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
4083 struct ciss_lun_report *cll = NULL;
4086 switch (cn->subclass) {
4087 case CISS_NOTIFY_HOTPLUG_PHYSICAL:
4088 case CISS_NOTIFY_HOTPLUG_NONDISK:
4089 bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
4091 CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
4093 if (cn->detail == 0) {
4095 * Mark the device offline so that it'll start producing selection
4096 * timeouts to the upper layer.
4098 if ((bus >= 0) && (target >= 0))
4099 sc->ciss_physical[bus][target].cp_online = 0;
4102 * Rescan the physical lun list for new items
4104 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
4105 sc->ciss_cfg->max_physical_supported);
4107 ciss_printf(sc, "Warning, cannot get physical lun list\n");
4110 ciss_filter_physical(sc, cll);
4115 ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
4120 free(cll, CISS_MALLOC_CLASS);
4123 /************************************************************************
4124 * Handle deferred processing of notify events. Notify events may need
4125 * sleep which is unsafe during an interrupt.
4128 ciss_notify_thread(void *arg)
4130 struct ciss_softc *sc;
4131 struct ciss_request *cr;
4132 struct ciss_notify *cn;
4134 sc = (struct ciss_softc *)arg;
4135 mtx_lock(&sc->ciss_mtx);
4138 if (STAILQ_EMPTY(&sc->ciss_notify) != 0 &&
4139 (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
4140 msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
4143 if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
4146 cr = ciss_dequeue_notify(sc);
4150 cn = (struct ciss_notify *)cr->cr_data;
4152 switch (cn->class) {
4153 case CISS_NOTIFY_HOTPLUG:
4154 ciss_notify_hotplug(sc, cn);
4156 case CISS_NOTIFY_LOGICAL:
4157 ciss_notify_logical(sc, cn);
4159 case CISS_NOTIFY_PHYSICAL:
4160 ciss_notify_physical(sc, cn);
4164 ciss_release_request(cr);
4166 sc->ciss_notify_thread = NULL;
4167 wakeup(&sc->ciss_notify_thread);
4169 mtx_unlock(&sc->ciss_mtx);
4173 /************************************************************************
4174 * Start the notification kernel thread.
4177 ciss_spawn_notify_thread(struct ciss_softc *sc)
4180 if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4181 &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
4182 device_get_unit(sc->ciss_dev)))
4183 panic("Could not create notify thread\n");
4186 /************************************************************************
4187 * Kill the notification kernel thread.
4190 ciss_kill_notify_thread(struct ciss_softc *sc)
4193 if (sc->ciss_notify_thread == NULL)
4196 sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
4197 wakeup(&sc->ciss_notify);
4198 msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
4201 /************************************************************************
4206 ciss_print_request(struct ciss_request *cr)
4208 struct ciss_softc *sc;
4209 struct ciss_command *cc;
4215 ciss_printf(sc, "REQUEST @ %p\n", cr);
4216 ciss_printf(sc, " data %p/%d tag %d flags %b\n",
4217 cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
4218 "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
4219 ciss_printf(sc, " sg list/total %d/%d host tag 0x%x\n",
4220 cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
4221 switch(cc->header.address.mode.mode) {
4222 case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
4223 case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
4224 ciss_printf(sc, " physical bus %d target %d\n",
4225 cc->header.address.physical.bus, cc->header.address.physical.target);
4227 case CISS_HDR_ADDRESS_MODE_LOGICAL:
4228 ciss_printf(sc, " logical unit %d\n", cc->header.address.logical.lun);
4231 ciss_printf(sc, " %s cdb length %d type %s attribute %s\n",
4232 (cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
4233 (cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
4234 (cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
4236 (cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
4237 (cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
4238 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
4239 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
4240 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
4241 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
4242 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
4243 ciss_printf(sc, " %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
4245 if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
4246 /* XXX print error info */
4248 /* since we don't use chained s/g, don't support it here */
4249 for (i = 0; i < cc->header.sg_in_list; i++) {
4251 ciss_printf(sc, " ");
4252 printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
4253 if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
4260 /************************************************************************
4261 * Print information about the status of a logical drive.
4264 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
4268 if (ld->cl_lstatus == NULL) {
4269 printf("does not exist\n");
4273 /* print drive status */
4274 switch(ld->cl_lstatus->status) {
4275 case CISS_LSTATUS_OK:
4278 case CISS_LSTATUS_INTERIM_RECOVERY:
4279 printf("in interim recovery mode\n");
4281 case CISS_LSTATUS_READY_RECOVERY:
4282 printf("ready to begin recovery\n");
4284 case CISS_LSTATUS_RECOVERING:
4285 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4286 target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4287 printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
4288 bus, target, ld->cl_lstatus->blocks_to_recover);
4290 case CISS_LSTATUS_EXPANDING:
4291 printf("being expanded, %u blocks remaining\n",
4292 ld->cl_lstatus->blocks_to_recover);
4294 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4295 printf("queued for expansion\n");
4297 case CISS_LSTATUS_FAILED:
4298 printf("queued for expansion\n");
4300 case CISS_LSTATUS_WRONG_PDRIVE:
4301 printf("wrong physical drive inserted\n");
4303 case CISS_LSTATUS_MISSING_PDRIVE:
4304 printf("missing a needed physical drive\n");
4306 case CISS_LSTATUS_BECOMING_READY:
4307 printf("becoming ready\n");
4311 /* print failed physical drives */
4312 for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
4313 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
4314 target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
4317 ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
4318 ld->cl_lstatus->drive_failure_map[i]);
4323 #include <ddb/ddb.h>
4324 /************************************************************************
4325 * Print information about the controller/driver.
4328 ciss_print_adapter(struct ciss_softc *sc)
4332 ciss_printf(sc, "ADAPTER:\n");
4333 for (i = 0; i < CISSQ_COUNT; i++) {
4334 ciss_printf(sc, "%s %d/%d\n",
4336 i == 1 ? "busy" : "complete",
4337 sc->ciss_qstat[i].q_length,
4338 sc->ciss_qstat[i].q_max);
4340 ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
4341 ciss_printf(sc, "flags %b\n", sc->ciss_flags,
4342 "\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
4344 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
4345 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
4346 ciss_printf(sc, "LOGICAL DRIVE %d: ", i);
4347 ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
4351 /* XXX Should physical drives be printed out here? */
4353 for (i = 1; i < sc->ciss_max_requests; i++)
4354 ciss_print_request(sc->ciss_request + i);
4358 DB_COMMAND(ciss_prt, db_ciss_prt)
4360 struct ciss_softc *sc;
4364 dc = devclass_find("ciss");
4366 printf("%s: can't find devclass!\n", __func__);
4369 maxciss = devclass_get_maxunit(dc);
4370 for (i = 0; i < maxciss; i++) {
4371 sc = devclass_get_softc(dc, i);
4372 ciss_print_adapter(sc);
4377 /************************************************************************
4378 * Return a name for a logical drive status value.
4381 ciss_name_ldrive_status(int status)
4384 case CISS_LSTATUS_OK:
4386 case CISS_LSTATUS_FAILED:
4388 case CISS_LSTATUS_NOT_CONFIGURED:
4389 return("not configured");
4390 case CISS_LSTATUS_INTERIM_RECOVERY:
4391 return("interim recovery");
4392 case CISS_LSTATUS_READY_RECOVERY:
4393 return("ready for recovery");
4394 case CISS_LSTATUS_RECOVERING:
4395 return("recovering");
4396 case CISS_LSTATUS_WRONG_PDRIVE:
4397 return("wrong physical drive inserted");
4398 case CISS_LSTATUS_MISSING_PDRIVE:
4399 return("missing physical drive");
4400 case CISS_LSTATUS_EXPANDING:
4401 return("expanding");
4402 case CISS_LSTATUS_BECOMING_READY:
4403 return("becoming ready");
4404 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4405 return("queued for expansion");
4407 return("unknown status");
4410 /************************************************************************
4411 * Return an online/offline/nonexistent value for a logical drive
4415 ciss_decode_ldrive_status(int status)
4418 case CISS_LSTATUS_NOT_CONFIGURED:
4419 return(CISS_LD_NONEXISTENT);
4421 case CISS_LSTATUS_OK:
4422 case CISS_LSTATUS_INTERIM_RECOVERY:
4423 case CISS_LSTATUS_READY_RECOVERY:
4424 case CISS_LSTATUS_RECOVERING:
4425 case CISS_LSTATUS_EXPANDING:
4426 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4427 return(CISS_LD_ONLINE);
4429 case CISS_LSTATUS_FAILED:
4430 case CISS_LSTATUS_WRONG_PDRIVE:
4431 case CISS_LSTATUS_MISSING_PDRIVE:
4432 case CISS_LSTATUS_BECOMING_READY:
4434 return(CISS_LD_OFFLINE);
4438 /************************************************************************
4439 * Return a name for a logical drive's organisation.
4442 ciss_name_ldrive_org(int org)
4445 case CISS_LDRIVE_RAID0:
4447 case CISS_LDRIVE_RAID1:
4448 return("RAID 1(1+0)");
4449 case CISS_LDRIVE_RAID4:
4451 case CISS_LDRIVE_RAID5:
4453 case CISS_LDRIVE_RAID51:
4455 case CISS_LDRIVE_RAIDADG:
4461 /************************************************************************
4462 * Return a name for a command status value.
4465 ciss_name_command_status(int status)
4468 case CISS_CMD_STATUS_SUCCESS:
4470 case CISS_CMD_STATUS_TARGET_STATUS:
4471 return("target status");
4472 case CISS_CMD_STATUS_DATA_UNDERRUN:
4473 return("data underrun");
4474 case CISS_CMD_STATUS_DATA_OVERRUN:
4475 return("data overrun");
4476 case CISS_CMD_STATUS_INVALID_COMMAND:
4477 return("invalid command");
4478 case CISS_CMD_STATUS_PROTOCOL_ERROR:
4479 return("protocol error");
4480 case CISS_CMD_STATUS_HARDWARE_ERROR:
4481 return("hardware error");
4482 case CISS_CMD_STATUS_CONNECTION_LOST:
4483 return("connection lost");
4484 case CISS_CMD_STATUS_ABORTED:
4486 case CISS_CMD_STATUS_ABORT_FAILED:
4487 return("abort failed");
4488 case CISS_CMD_STATUS_UNSOLICITED_ABORT:
4489 return("unsolicited abort");
4490 case CISS_CMD_STATUS_TIMEOUT:
4492 case CISS_CMD_STATUS_UNABORTABLE:
4493 return("unabortable");
4495 return("unknown status");
4498 /************************************************************************
4499 * Handle an open on the control device.
4502 ciss_open(struct cdev *dev, int flags, int fmt, struct thread *p)
4504 struct ciss_softc *sc;
4508 sc = (struct ciss_softc *)dev->si_drv1;
4510 /* we might want to veto if someone already has us open */
4512 mtx_lock(&sc->ciss_mtx);
4513 sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
4514 mtx_unlock(&sc->ciss_mtx);
4518 /************************************************************************
4519 * Handle the last close on the control device.
4522 ciss_close(struct cdev *dev, int flags, int fmt, struct thread *p)
4524 struct ciss_softc *sc;
4528 sc = (struct ciss_softc *)dev->si_drv1;
4530 mtx_lock(&sc->ciss_mtx);
4531 sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
4532 mtx_unlock(&sc->ciss_mtx);
4536 /********************************************************************************
4537 * Handle adapter-specific control operations.
4539 * Note that the API here is compatible with the Linux driver, in order to
4540 * simplify the porting of Compaq's userland tools.
4543 ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *p)
4545 struct ciss_softc *sc;
4546 IOCTL_Command_struct *ioc = (IOCTL_Command_struct *)addr;
4548 IOCTL_Command_struct32 *ioc32 = (IOCTL_Command_struct32 *)addr;
4549 IOCTL_Command_struct ioc_swab;
4555 sc = (struct ciss_softc *)dev->si_drv1;
4557 mtx_lock(&sc->ciss_mtx);
4560 case CCISS_GETQSTATS:
4562 union ciss_statrequest *cr = (union ciss_statrequest *)addr;
4564 switch (cr->cs_item) {
4567 bcopy(&sc->ciss_qstat[cr->cs_item], &cr->cs_qstat,
4568 sizeof(struct ciss_qstat));
4578 case CCISS_GETPCIINFO:
4580 cciss_pci_info_struct *pis = (cciss_pci_info_struct *)addr;
4582 pis->bus = pci_get_bus(sc->ciss_dev);
4583 pis->dev_fn = pci_get_slot(sc->ciss_dev);
4584 pis->board_id = (pci_get_subvendor(sc->ciss_dev) << 16) |
4585 pci_get_subdevice(sc->ciss_dev);
4590 case CCISS_GETINTINFO:
4592 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4594 cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
4595 cis->count = sc->ciss_cfg->interrupt_coalesce_count;
4600 case CCISS_SETINTINFO:
4602 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4604 if ((cis->delay == 0) && (cis->count == 0)) {
4610 * XXX apparently this is only safe if the controller is idle,
4611 * we should suspend it before doing this.
4613 sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
4614 sc->ciss_cfg->interrupt_coalesce_count = cis->count;
4616 if (ciss_update_config(sc))
4619 /* XXX resume the controller here */
4623 case CCISS_GETNODENAME:
4624 bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
4625 sizeof(NodeName_type));
4628 case CCISS_SETNODENAME:
4629 bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
4630 sizeof(NodeName_type));
4631 if (ciss_update_config(sc))
4635 case CCISS_GETHEARTBEAT:
4636 *(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
4639 case CCISS_GETBUSTYPES:
4640 *(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
4643 case CCISS_GETFIRMVER:
4644 bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
4645 sizeof(FirmwareVer_type));
4648 case CCISS_GETDRIVERVER:
4649 *(DriverVer_type *)addr = CISS_DRIVER_VERSION;
4652 case CCISS_REVALIDVOLS:
4654 * This is a bit ugly; to do it "right" we really need
4655 * to find any disks that have changed, kick CAM off them,
4656 * then rescan only these disks. It'd be nice if they
4657 * a) told us which disk(s) they were going to play with,
4658 * and b) which ones had arrived. 8(
4663 case CCISS_PASSTHRU32:
4664 ioc_swab.LUN_info = ioc32->LUN_info;
4665 ioc_swab.Request = ioc32->Request;
4666 ioc_swab.error_info = ioc32->error_info;
4667 ioc_swab.buf_size = ioc32->buf_size;
4668 ioc_swab.buf = (u_int8_t *)(uintptr_t)ioc32->buf;
4673 case CCISS_PASSTHRU:
4674 error = ciss_user_command(sc, ioc);
4678 debug(0, "unknown ioctl 0x%lx", cmd);
4680 debug(1, "CCISS_GETPCIINFO: 0x%lx", CCISS_GETPCIINFO);
4681 debug(1, "CCISS_GETINTINFO: 0x%lx", CCISS_GETINTINFO);
4682 debug(1, "CCISS_SETINTINFO: 0x%lx", CCISS_SETINTINFO);
4683 debug(1, "CCISS_GETNODENAME: 0x%lx", CCISS_GETNODENAME);
4684 debug(1, "CCISS_SETNODENAME: 0x%lx", CCISS_SETNODENAME);
4685 debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
4686 debug(1, "CCISS_GETBUSTYPES: 0x%lx", CCISS_GETBUSTYPES);
4687 debug(1, "CCISS_GETFIRMVER: 0x%lx", CCISS_GETFIRMVER);
4688 debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
4689 debug(1, "CCISS_REVALIDVOLS: 0x%lx", CCISS_REVALIDVOLS);
4690 debug(1, "CCISS_PASSTHRU: 0x%lx", CCISS_PASSTHRU);
4696 mtx_unlock(&sc->ciss_mtx);