2 * Copyright (c) 2001 Michael Smith
3 * Copyright (c) 2004 Paul Saab
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * Common Interface for SCSI-3 Support driver.
33 * CISS claims to provide a common interface between a generic SCSI
34 * transport and an intelligent host adapter.
36 * This driver supports CISS as defined in the document "CISS Command
37 * Interface for SCSI-3 Support Open Specification", Version 1.04,
38 * Valence Number 1, dated 20001127, produced by Compaq Computer
39 * Corporation. This document appears to be a hastily and somewhat
40 * arbitrarlily cut-down version of a larger (and probably even more
41 * chaotic and inconsistent) Compaq internal document. Various
42 * details were also gleaned from Compaq's "cciss" driver for Linux.
44 * We provide a shim layer between the CISS interface and CAM,
45 * offloading most of the queueing and being-a-disk chores onto CAM.
46 * Entry to the driver is via the PCI bus attachment (ciss_probe,
47 * ciss_attach, etc) and via the CAM interface (ciss_cam_action,
48 * ciss_cam_poll). The Compaq CISS adapters are, however, poor SCSI
49 * citizens and we have to fake up some responses to get reasonable
50 * behaviour out of them. In addition, the CISS command set is by no
51 * means adequate to support the functionality of a RAID controller,
52 * and thus the supported Compaq adapters utilise portions of the
53 * control protocol from earlier Compaq adapter families.
55 * Note that we only support the "simple" transport layer over PCI.
56 * This interface (ab)uses the I2O register set (specifically the post
57 * queues) to exchange commands with the adapter. Other interfaces
58 * are available, but we aren't supposed to know about them, and it is
59 * dubious whether they would provide major performance improvements
60 * except under extreme load.
62 * Currently the only supported CISS adapters are the Compaq Smart
63 * Array 5* series (5300, 5i, 532). Even with only three adapters,
64 * Compaq still manage to have interface variations.
67 * Thanks must go to Fred Harris and Darryl DeVinney at Compaq, as
68 * well as Paul Saab at Yahoo! for their assistance in making this
71 * More thanks must go to John Cagle at HP for the countless hours
72 * spent making this driver "work" with the MSA* series storage
73 * enclosures. Without his help (and nagging), this driver could not
74 * be used with these enclosures.
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/malloc.h>
80 #include <sys/kernel.h>
84 #include <sys/kthread.h>
85 #include <sys/queue.h>
86 #include <sys/sysctl.h>
89 #include <cam/cam_ccb.h>
90 #include <cam/cam_periph.h>
91 #include <cam/cam_sim.h>
92 #include <cam/cam_xpt_sim.h>
93 #include <cam/scsi/scsi_all.h>
94 #include <cam/scsi/scsi_message.h>
96 #include <machine/bus.h>
97 #include <machine/endian.h>
98 #include <machine/resource.h>
101 #include <dev/pci/pcireg.h>
102 #include <dev/pci/pcivar.h>
104 #include <dev/ciss/cissreg.h>
105 #include <dev/ciss/cissio.h>
106 #include <dev/ciss/cissvar.h>
108 static MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data",
109 "ciss internal data buffers");
112 static int ciss_lookup(device_t dev);
113 static int ciss_probe(device_t dev);
114 static int ciss_attach(device_t dev);
115 static int ciss_detach(device_t dev);
116 static int ciss_shutdown(device_t dev);
118 /* (de)initialisation functions, control wrappers */
119 static int ciss_init_pci(struct ciss_softc *sc);
120 static int ciss_setup_msix(struct ciss_softc *sc);
121 static int ciss_init_perf(struct ciss_softc *sc);
122 static int ciss_wait_adapter(struct ciss_softc *sc);
123 static int ciss_flush_adapter(struct ciss_softc *sc);
124 static int ciss_init_requests(struct ciss_softc *sc);
125 static void ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
126 int nseg, int error);
127 static int ciss_identify_adapter(struct ciss_softc *sc);
128 static int ciss_init_logical(struct ciss_softc *sc);
129 static int ciss_init_physical(struct ciss_softc *sc);
130 static int ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll);
131 static int ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
132 static int ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld);
133 static int ciss_update_config(struct ciss_softc *sc);
134 static int ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld);
135 static void ciss_init_sysctl(struct ciss_softc *sc);
136 static void ciss_soft_reset(struct ciss_softc *sc);
137 static void ciss_free(struct ciss_softc *sc);
138 static void ciss_spawn_notify_thread(struct ciss_softc *sc);
139 static void ciss_kill_notify_thread(struct ciss_softc *sc);
141 /* request submission/completion */
142 static int ciss_start(struct ciss_request *cr);
143 static void ciss_done(struct ciss_softc *sc, cr_qhead_t *qh);
144 static void ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh);
145 static void ciss_intr(void *arg);
146 static void ciss_perf_intr(void *arg);
147 static void ciss_perf_msi_intr(void *arg);
148 static void ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh);
149 static int _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func);
150 static int ciss_synch_request(struct ciss_request *cr, int timeout);
151 static int ciss_poll_request(struct ciss_request *cr, int timeout);
152 static int ciss_wait_request(struct ciss_request *cr, int timeout);
154 static int ciss_abort_request(struct ciss_request *cr);
157 /* request queueing */
158 static int ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
159 static void ciss_preen_command(struct ciss_request *cr);
160 static void ciss_release_request(struct ciss_request *cr);
162 /* request helpers */
163 static int ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
164 int opcode, void **bufp, size_t bufsize);
165 static int ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
168 static int ciss_map_request(struct ciss_request *cr);
169 static void ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
170 int nseg, int error);
171 static void ciss_unmap_request(struct ciss_request *cr);
174 static int ciss_cam_init(struct ciss_softc *sc);
175 static void ciss_cam_rescan_target(struct ciss_softc *sc,
176 int bus, int target);
177 static void ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
178 static int ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
179 static int ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
180 static void ciss_cam_poll(struct cam_sim *sim);
181 static void ciss_cam_complete(struct ciss_request *cr);
182 static void ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
183 static int ciss_name_device(struct ciss_softc *sc, int bus, int target);
185 /* periodic status monitoring */
186 static void ciss_periodic(void *arg);
187 static void ciss_nop_complete(struct ciss_request *cr);
188 static void ciss_disable_adapter(struct ciss_softc *sc);
189 static void ciss_notify_event(struct ciss_softc *sc);
190 static void ciss_notify_complete(struct ciss_request *cr);
191 static int ciss_notify_abort(struct ciss_softc *sc);
192 static int ciss_notify_abort_bmic(struct ciss_softc *sc);
193 static void ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
194 static void ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
195 static void ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
197 /* debugging output */
198 static void ciss_print_request(struct ciss_request *cr);
199 static void ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
200 static const char *ciss_name_ldrive_status(int status);
201 static int ciss_decode_ldrive_status(int status);
202 static const char *ciss_name_ldrive_org(int org);
203 static const char *ciss_name_command_status(int status);
208 static device_method_t ciss_methods[] = {
209 /* Device interface */
210 DEVMETHOD(device_probe, ciss_probe),
211 DEVMETHOD(device_attach, ciss_attach),
212 DEVMETHOD(device_detach, ciss_detach),
213 DEVMETHOD(device_shutdown, ciss_shutdown),
217 static driver_t ciss_pci_driver = {
220 sizeof(struct ciss_softc)
223 static devclass_t ciss_devclass;
224 DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
225 MODULE_DEPEND(ciss, cam, 1, 1, 1);
226 MODULE_DEPEND(ciss, pci, 1, 1, 1);
229 * Control device interface.
231 static d_open_t ciss_open;
232 static d_close_t ciss_close;
233 static d_ioctl_t ciss_ioctl;
235 static struct cdevsw ciss_cdevsw = {
236 .d_version = D_VERSION,
239 .d_close = ciss_close,
240 .d_ioctl = ciss_ioctl,
245 * This tunable can be set at boot time and controls whether physical devices
246 * that are marked hidden by the firmware should be exposed anyways.
248 static unsigned int ciss_expose_hidden_physical = 0;
249 TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
251 static unsigned int ciss_nop_message_heartbeat = 0;
252 TUNABLE_INT("hw.ciss.nop_message_heartbeat", &ciss_nop_message_heartbeat);
255 * This tunable can force a particular transport to be used:
258 * 2 : force performant
260 static int ciss_force_transport = 0;
261 TUNABLE_INT("hw.ciss.force_transport", &ciss_force_transport);
264 * This tunable can force a particular interrupt delivery method to be used:
269 static int ciss_force_interrupt = 0;
270 TUNABLE_INT("hw.ciss.force_interrupt", &ciss_force_interrupt);
272 /************************************************************************
273 * CISS adapters amazingly don't have a defined programming interface
274 * value. (One could say some very despairing things about PCI and
275 * people just not getting the general idea.) So we are forced to
276 * stick with matching against subvendor/subdevice, and thus have to
277 * be updated for every new CISS adapter that appears.
279 #define CISS_BOARD_UNKNWON 0
280 #define CISS_BOARD_SA5 1
281 #define CISS_BOARD_SA5B 2
282 #define CISS_BOARD_NOMSI (1<<4)
283 #define CISS_BOARD_SIMPLE (1<<5)
291 } ciss_vendor_data[] = {
292 { 0x0e11, 0x4070, CISS_BOARD_SA5|CISS_BOARD_NOMSI|CISS_BOARD_SIMPLE,
293 "Compaq Smart Array 5300" },
294 { 0x0e11, 0x4080, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "Compaq Smart Array 5i" },
295 { 0x0e11, 0x4082, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "Compaq Smart Array 532" },
296 { 0x0e11, 0x4083, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "HP Smart Array 5312" },
297 { 0x0e11, 0x4091, CISS_BOARD_SA5, "HP Smart Array 6i" },
298 { 0x0e11, 0x409A, CISS_BOARD_SA5, "HP Smart Array 641" },
299 { 0x0e11, 0x409B, CISS_BOARD_SA5, "HP Smart Array 642" },
300 { 0x0e11, 0x409C, CISS_BOARD_SA5, "HP Smart Array 6400" },
301 { 0x0e11, 0x409D, CISS_BOARD_SA5, "HP Smart Array 6400 EM" },
302 { 0x103C, 0x3211, CISS_BOARD_SA5, "HP Smart Array E200i" },
303 { 0x103C, 0x3212, CISS_BOARD_SA5, "HP Smart Array E200" },
304 { 0x103C, 0x3213, CISS_BOARD_SA5, "HP Smart Array E200i" },
305 { 0x103C, 0x3214, CISS_BOARD_SA5, "HP Smart Array E200i" },
306 { 0x103C, 0x3215, CISS_BOARD_SA5, "HP Smart Array E200i" },
307 { 0x103C, 0x3220, CISS_BOARD_SA5, "HP Smart Array" },
308 { 0x103C, 0x3222, CISS_BOARD_SA5, "HP Smart Array" },
309 { 0x103C, 0x3223, CISS_BOARD_SA5, "HP Smart Array P800" },
310 { 0x103C, 0x3225, CISS_BOARD_SA5, "HP Smart Array P600" },
311 { 0x103C, 0x3230, CISS_BOARD_SA5, "HP Smart Array" },
312 { 0x103C, 0x3231, CISS_BOARD_SA5, "HP Smart Array" },
313 { 0x103C, 0x3232, CISS_BOARD_SA5, "HP Smart Array" },
314 { 0x103C, 0x3233, CISS_BOARD_SA5, "HP Smart Array" },
315 { 0x103C, 0x3234, CISS_BOARD_SA5, "HP Smart Array P400" },
316 { 0x103C, 0x3235, CISS_BOARD_SA5, "HP Smart Array P400i" },
317 { 0x103C, 0x3236, CISS_BOARD_SA5, "HP Smart Array" },
318 { 0x103C, 0x3237, CISS_BOARD_SA5, "HP Smart Array E500" },
319 { 0x103C, 0x3238, CISS_BOARD_SA5, "HP Smart Array" },
320 { 0x103C, 0x3239, CISS_BOARD_SA5, "HP Smart Array" },
321 { 0x103C, 0x323A, CISS_BOARD_SA5, "HP Smart Array" },
322 { 0x103C, 0x323B, CISS_BOARD_SA5, "HP Smart Array" },
323 { 0x103C, 0x323C, CISS_BOARD_SA5, "HP Smart Array" },
324 { 0x103C, 0x323D, CISS_BOARD_SA5, "HP Smart Array P700m" },
325 { 0x103C, 0x3241, CISS_BOARD_SA5, "HP Smart Array P212" },
326 { 0x103C, 0x3243, CISS_BOARD_SA5, "HP Smart Array P410" },
327 { 0x103C, 0x3245, CISS_BOARD_SA5, "HP Smart Array P410i" },
328 { 0x103C, 0x3247, CISS_BOARD_SA5, "HP Smart Array P411" },
329 { 0x103C, 0x3249, CISS_BOARD_SA5, "HP Smart Array P812" },
330 { 0x103C, 0x324A, CISS_BOARD_SA5, "HP Smart Array P712m" },
331 { 0x103C, 0x324B, CISS_BOARD_SA5, "HP Smart Array" },
332 { 0x103C, 0x3350, CISS_BOARD_SA5, "HP Smart Array P222" },
333 { 0x103C, 0x3351, CISS_BOARD_SA5, "HP Smart Array P420" },
334 { 0x103C, 0x3352, CISS_BOARD_SA5, "HP Smart Array P421" },
335 { 0x103C, 0x3353, CISS_BOARD_SA5, "HP Smart Array P822" },
336 { 0x103C, 0x3354, CISS_BOARD_SA5, "HP Smart Array P420i" },
337 { 0x103C, 0x3355, CISS_BOARD_SA5, "HP Smart Array P220i" },
338 { 0x103C, 0x3356, CISS_BOARD_SA5, "HP Smart Array P721m" },
339 { 0x103C, 0x1920, CISS_BOARD_SA5, "HP Smart Array P430i" },
340 { 0x103C, 0x1921, CISS_BOARD_SA5, "HP Smart Array P830i" },
341 { 0x103C, 0x1922, CISS_BOARD_SA5, "HP Smart Array P430" },
342 { 0x103C, 0x1923, CISS_BOARD_SA5, "HP Smart Array P431" },
343 { 0x103C, 0x1924, CISS_BOARD_SA5, "HP Smart Array P830" },
344 { 0x103C, 0x1926, CISS_BOARD_SA5, "HP Smart Array P731m" },
345 { 0x103C, 0x1928, CISS_BOARD_SA5, "HP Smart Array P230i" },
346 { 0x103C, 0x1929, CISS_BOARD_SA5, "HP Smart Array P530" },
347 { 0x103C, 0x192A, CISS_BOARD_SA5, "HP Smart Array P531" },
348 { 0x103C, 0x21BD, CISS_BOARD_SA5, "HP Smart Array TBD" },
349 { 0x103C, 0x21BE, CISS_BOARD_SA5, "HP Smart Array TBD" },
350 { 0x103C, 0x21BF, CISS_BOARD_SA5, "HP Smart Array TBD" },
351 { 0x103C, 0x21C0, CISS_BOARD_SA5, "HP Smart Array TBD" },
352 { 0x103C, 0x21C2, CISS_BOARD_SA5, "HP Smart Array TBD" },
353 { 0x103C, 0x21C3, CISS_BOARD_SA5, "HP Smart Array TBD" },
354 { 0x103C, 0x21C5, CISS_BOARD_SA5, "HP Smart Array TBD" },
355 { 0x103C, 0x21C6, CISS_BOARD_SA5, "HP Smart Array TBD" },
356 { 0x103C, 0x21C7, CISS_BOARD_SA5, "HP Smart Array TBD" },
357 { 0x103C, 0x21C8, CISS_BOARD_SA5, "HP Smart Array TBD" },
358 { 0x103C, 0x21CA, CISS_BOARD_SA5, "HP Smart Array TBD" },
359 { 0x103C, 0x21CB, CISS_BOARD_SA5, "HP Smart Array TBD" },
360 { 0x103C, 0x21CC, CISS_BOARD_SA5, "HP Smart Array TBD" },
361 { 0x103C, 0x21CD, CISS_BOARD_SA5, "HP Smart Array TBD" },
362 { 0x103C, 0x21CE, CISS_BOARD_SA5, "HP Smart Array TBD" },
366 /************************************************************************
367 * Find a match for the device in our list of known adapters.
370 ciss_lookup(device_t dev)
374 for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
375 if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
376 (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
382 /************************************************************************
383 * Match a known CISS adapter.
386 ciss_probe(device_t dev)
390 i = ciss_lookup(dev);
392 device_set_desc(dev, ciss_vendor_data[i].desc);
393 return(BUS_PROBE_DEFAULT);
398 /************************************************************************
399 * Attach the driver to this adapter.
402 ciss_attach(device_t dev)
404 struct ciss_softc *sc;
410 /* print structure/union sizes */
411 debug_struct(ciss_command);
412 debug_struct(ciss_header);
413 debug_union(ciss_device_address);
414 debug_struct(ciss_cdb);
415 debug_struct(ciss_report_cdb);
416 debug_struct(ciss_notify_cdb);
417 debug_struct(ciss_notify);
418 debug_struct(ciss_message_cdb);
419 debug_struct(ciss_error_info_pointer);
420 debug_struct(ciss_error_info);
421 debug_struct(ciss_sg_entry);
422 debug_struct(ciss_config_table);
423 debug_struct(ciss_bmic_cdb);
424 debug_struct(ciss_bmic_id_ldrive);
425 debug_struct(ciss_bmic_id_lstatus);
426 debug_struct(ciss_bmic_id_table);
427 debug_struct(ciss_bmic_id_pdrive);
428 debug_struct(ciss_bmic_blink_pdrive);
429 debug_struct(ciss_bmic_flush_cache);
430 debug_const(CISS_MAX_REQUESTS);
431 debug_const(CISS_MAX_LOGICAL);
432 debug_const(CISS_INTERRUPT_COALESCE_DELAY);
433 debug_const(CISS_INTERRUPT_COALESCE_COUNT);
434 debug_const(CISS_COMMAND_ALLOC_SIZE);
435 debug_const(CISS_COMMAND_SG_LENGTH);
437 debug_type(cciss_pci_info_struct);
438 debug_type(cciss_coalint_struct);
439 debug_type(cciss_coalint_struct);
440 debug_type(NodeName_type);
441 debug_type(NodeName_type);
442 debug_type(Heartbeat_type);
443 debug_type(BusTypes_type);
444 debug_type(FirmwareVer_type);
445 debug_type(DriverVer_type);
446 debug_type(IOCTL_Command_struct);
449 sc = device_get_softc(dev);
451 mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
452 callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);
455 * Do PCI-specific init.
457 if ((error = ciss_init_pci(sc)) != 0)
461 * Initialise driver queues.
464 ciss_initq_notify(sc);
467 * Initalize device sysctls.
469 ciss_init_sysctl(sc);
472 * Initialise command/request pool.
474 if ((error = ciss_init_requests(sc)) != 0)
478 * Get adapter information.
480 if ((error = ciss_identify_adapter(sc)) != 0)
484 * Find all the physical devices.
486 if ((error = ciss_init_physical(sc)) != 0)
490 * Build our private table of logical devices.
492 if ((error = ciss_init_logical(sc)) != 0)
496 * Enable interrupts so that the CAM scan can complete.
498 CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
501 * Initialise the CAM interface.
503 if ((error = ciss_cam_init(sc)) != 0)
507 * Start the heartbeat routine and event chain.
512 * Create the control device.
514 sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
515 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
516 "ciss%d", device_get_unit(sc->ciss_dev));
517 sc->ciss_dev_t->si_drv1 = sc;
520 * The adapter is running; synchronous commands can now sleep
521 * waiting for an interrupt to signal completion.
523 sc->ciss_flags |= CISS_FLAG_RUNNING;
525 ciss_spawn_notify_thread(sc);
530 /* ciss_free() expects the mutex to be held */
531 mtx_lock(&sc->ciss_mtx);
537 /************************************************************************
538 * Detach the driver from this adapter.
541 ciss_detach(device_t dev)
543 struct ciss_softc *sc = device_get_softc(dev);
547 mtx_lock(&sc->ciss_mtx);
548 if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
549 mtx_unlock(&sc->ciss_mtx);
553 /* flush adapter cache */
554 ciss_flush_adapter(sc);
556 /* release all resources. The mutex is released and freed here too. */
562 /************************************************************************
563 * Prepare adapter for system shutdown.
566 ciss_shutdown(device_t dev)
568 struct ciss_softc *sc = device_get_softc(dev);
572 mtx_lock(&sc->ciss_mtx);
573 /* flush adapter cache */
574 ciss_flush_adapter(sc);
576 if (sc->ciss_soft_reset)
578 mtx_unlock(&sc->ciss_mtx);
584 ciss_init_sysctl(struct ciss_softc *sc)
587 SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
588 SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
589 OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
592 /************************************************************************
593 * Perform PCI-specific attachment actions.
596 ciss_init_pci(struct ciss_softc *sc)
598 uintptr_t cbase, csize, cofs;
599 uint32_t method, supported_methods;
600 int error, sqmask, i;
606 * Work out adapter type.
608 i = ciss_lookup(sc->ciss_dev);
610 ciss_printf(sc, "unknown adapter type\n");
614 if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
615 sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
616 } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
617 sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
620 * XXX Big hammer, masks/unmasks all possible interrupts. This should
621 * work on all hardware variants. Need to add code to handle the
622 * "controller crashed" interupt bit that this unmasks.
628 * Allocate register window first (we need this to find the config
632 sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
633 if ((sc->ciss_regs_resource =
634 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
635 &sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
636 ciss_printf(sc, "can't allocate register window\n");
639 sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
640 sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
643 * Find the BAR holding the config structure. If it's not the one
644 * we already mapped for registers, map it too.
646 sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
647 if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
648 if ((sc->ciss_cfg_resource =
649 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
650 &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
651 ciss_printf(sc, "can't allocate config window\n");
654 cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
655 csize = rman_get_end(sc->ciss_cfg_resource) -
656 rman_get_start(sc->ciss_cfg_resource) + 1;
658 cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
659 csize = rman_get_end(sc->ciss_regs_resource) -
660 rman_get_start(sc->ciss_regs_resource) + 1;
662 cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
665 * Use the base/size/offset values we just calculated to
666 * sanity-check the config structure. If it's OK, point to it.
668 if ((cofs + sizeof(struct ciss_config_table)) > csize) {
669 ciss_printf(sc, "config table outside window\n");
672 sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
673 debug(1, "config struct at %p", sc->ciss_cfg);
676 * Calculate the number of request structures/commands we are
677 * going to provide for this adapter.
679 sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
682 * Validate the config structure. If we supported other transport
683 * methods, we could select amongst them at this point in time.
685 if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
686 ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
687 sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
688 sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
693 * Select the mode of operation, prefer Performant.
695 if (!(sc->ciss_cfg->supported_methods &
696 (CISS_TRANSPORT_METHOD_SIMPLE | CISS_TRANSPORT_METHOD_PERF))) {
697 ciss_printf(sc, "No supported transport layers: 0x%x\n",
698 sc->ciss_cfg->supported_methods);
701 switch (ciss_force_transport) {
703 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
706 supported_methods = CISS_TRANSPORT_METHOD_PERF;
710 * Override the capabilities of the BOARD and specify SIMPLE
713 if (ciss_vendor_data[i].flags & CISS_BOARD_SIMPLE)
714 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
716 supported_methods = sc->ciss_cfg->supported_methods;
721 if ((supported_methods & CISS_TRANSPORT_METHOD_PERF) != 0) {
722 method = CISS_TRANSPORT_METHOD_PERF;
723 sc->ciss_perf = (struct ciss_perf_config *)(cbase + cofs +
724 sc->ciss_cfg->transport_offset);
725 if (ciss_init_perf(sc)) {
726 supported_methods &= ~method;
729 } else if (supported_methods & CISS_TRANSPORT_METHOD_SIMPLE) {
730 method = CISS_TRANSPORT_METHOD_SIMPLE;
732 ciss_printf(sc, "No supported transport methods: 0x%x\n",
733 sc->ciss_cfg->supported_methods);
738 * Tell it we're using the low 4GB of RAM. Set the default interrupt
739 * coalescing options.
741 sc->ciss_cfg->requested_method = method;
742 sc->ciss_cfg->command_physlimit = 0;
743 sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
744 sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
747 sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
750 if (ciss_update_config(sc)) {
751 ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
752 CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
755 if ((sc->ciss_cfg->active_method & method) == 0) {
756 supported_methods &= ~method;
757 if (supported_methods == 0) {
758 ciss_printf(sc, "adapter refuses to go into available transports "
759 "mode (0x%x, 0x%x)\n", supported_methods,
760 sc->ciss_cfg->active_method);
767 * Wait for the adapter to come ready.
769 if ((error = ciss_wait_adapter(sc)) != 0)
772 /* Prepare to possibly use MSIX and/or PERFORMANT interrupts. Normal
773 * interrupts have a rid of 0, this will be overridden if MSIX is used.
775 sc->ciss_irq_rid[0] = 0;
776 if (method == CISS_TRANSPORT_METHOD_PERF) {
777 ciss_printf(sc, "PERFORMANT Transport\n");
778 if ((ciss_force_interrupt != 1) && (ciss_setup_msix(sc) == 0)) {
779 intr = ciss_perf_msi_intr;
781 intr = ciss_perf_intr;
783 /* XXX The docs say that the 0x01 bit is only for SAS controllers.
784 * Unfortunately, there is no good way to know if this is a SAS
785 * controller. Hopefully enabling this bit universally will work OK.
786 * It seems to work fine for SA6i controllers.
788 sc->ciss_interrupt_mask = CISS_TL_PERF_INTR_OPQ | CISS_TL_PERF_INTR_MSI;
791 ciss_printf(sc, "SIMPLE Transport\n");
792 /* MSIX doesn't seem to work in SIMPLE mode, only enable if it forced */
793 if (ciss_force_interrupt == 2)
794 /* If this fails, we automatically revert to INTx */
796 sc->ciss_perf = NULL;
798 sc->ciss_interrupt_mask = sqmask;
802 * Turn off interrupts before we go routing anything.
804 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
807 * Allocate and set up our interrupt.
809 if ((sc->ciss_irq_resource =
810 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid[0],
811 RF_ACTIVE | RF_SHAREABLE)) == NULL) {
812 ciss_printf(sc, "can't allocate interrupt\n");
816 if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
817 INTR_TYPE_CAM|INTR_MPSAFE, NULL, intr, sc,
819 ciss_printf(sc, "can't set up interrupt\n");
824 * Allocate the parent bus DMA tag appropriate for our PCI
827 * Note that "simple" adapters can only address within a 32-bit
830 if (bus_dma_tag_create(bus_get_dma_tag(sc->ciss_dev),/* PCI parent */
831 1, 0, /* alignment, boundary */
832 BUS_SPACE_MAXADDR, /* lowaddr */
833 BUS_SPACE_MAXADDR, /* highaddr */
834 NULL, NULL, /* filter, filterarg */
835 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
836 CISS_MAX_SG_ELEMENTS, /* nsegments */
837 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
839 NULL, NULL, /* lockfunc, lockarg */
840 &sc->ciss_parent_dmat)) {
841 ciss_printf(sc, "can't allocate parent DMA tag\n");
846 * Create DMA tag for mapping buffers into adapter-addressable
849 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
850 1, 0, /* alignment, boundary */
851 BUS_SPACE_MAXADDR, /* lowaddr */
852 BUS_SPACE_MAXADDR, /* highaddr */
853 NULL, NULL, /* filter, filterarg */
854 MAXBSIZE, CISS_MAX_SG_ELEMENTS, /* maxsize, nsegments */
855 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
856 BUS_DMA_ALLOCNOW, /* flags */
857 busdma_lock_mutex, &sc->ciss_mtx, /* lockfunc, lockarg */
858 &sc->ciss_buffer_dmat)) {
859 ciss_printf(sc, "can't allocate buffer DMA tag\n");
865 /************************************************************************
866 * Setup MSI/MSIX operation (Performant only)
867 * Four interrupts are available, but we only use 1 right now. If MSI-X
868 * isn't avaialble, try using MSI instead.
871 ciss_setup_msix(struct ciss_softc *sc)
875 /* Weed out devices that don't actually support MSI */
876 i = ciss_lookup(sc->ciss_dev);
877 if (ciss_vendor_data[i].flags & CISS_BOARD_NOMSI)
881 * Only need to use the minimum number of MSI vectors, as the driver
882 * doesn't support directed MSIX interrupts.
884 val = pci_msix_count(sc->ciss_dev);
885 if (val < CISS_MSI_COUNT) {
886 val = pci_msi_count(sc->ciss_dev);
887 device_printf(sc->ciss_dev, "got %d MSI messages]\n", val);
888 if (val < CISS_MSI_COUNT)
891 val = MIN(val, CISS_MSI_COUNT);
892 if (pci_alloc_msix(sc->ciss_dev, &val) != 0) {
893 if (pci_alloc_msi(sc->ciss_dev, &val) != 0)
899 ciss_printf(sc, "Using %d MSIX interrupt%s\n", val,
900 (val != 1) ? "s" : "");
902 for (i = 0; i < val; i++)
903 sc->ciss_irq_rid[i] = i + 1;
909 /************************************************************************
910 * Setup the Performant structures.
913 ciss_init_perf(struct ciss_softc *sc)
915 struct ciss_perf_config *pc = sc->ciss_perf;
919 * Create the DMA tag for the reply queue.
921 reply_size = sizeof(uint64_t) * sc->ciss_max_requests;
922 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
923 1, 0, /* alignment, boundary */
924 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
925 BUS_SPACE_MAXADDR, /* highaddr */
926 NULL, NULL, /* filter, filterarg */
927 reply_size, 1, /* maxsize, nsegments */
928 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
930 NULL, NULL, /* lockfunc, lockarg */
931 &sc->ciss_reply_dmat)) {
932 ciss_printf(sc, "can't allocate reply DMA tag\n");
936 * Allocate memory and make it available for DMA.
938 if (bus_dmamem_alloc(sc->ciss_reply_dmat, (void **)&sc->ciss_reply,
939 BUS_DMA_NOWAIT, &sc->ciss_reply_map)) {
940 ciss_printf(sc, "can't allocate reply memory\n");
943 bus_dmamap_load(sc->ciss_reply_dmat, sc->ciss_reply_map, sc->ciss_reply,
944 reply_size, ciss_command_map_helper, &sc->ciss_reply_phys, 0);
945 bzero(sc->ciss_reply, reply_size);
947 sc->ciss_cycle = 0x1;
951 * Preload the fetch table with common command sizes. This allows the
952 * hardware to not waste bus cycles for typical i/o commands, but also not
953 * tax the driver to be too exact in choosing sizes. The table is optimized
954 * for page-aligned i/o's, but since most i/o comes from the various pagers,
955 * it's a reasonable assumption to make.
957 pc->fetch_count[CISS_SG_FETCH_NONE] = (sizeof(struct ciss_command) + 15) / 16;
958 pc->fetch_count[CISS_SG_FETCH_1] =
959 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 1 + 15) / 16;
960 pc->fetch_count[CISS_SG_FETCH_2] =
961 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 2 + 15) / 16;
962 pc->fetch_count[CISS_SG_FETCH_4] =
963 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 4 + 15) / 16;
964 pc->fetch_count[CISS_SG_FETCH_8] =
965 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 8 + 15) / 16;
966 pc->fetch_count[CISS_SG_FETCH_16] =
967 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 16 + 15) / 16;
968 pc->fetch_count[CISS_SG_FETCH_32] =
969 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 32 + 15) / 16;
970 pc->fetch_count[CISS_SG_FETCH_MAX] = (CISS_COMMAND_ALLOC_SIZE + 15) / 16;
972 pc->rq_size = sc->ciss_max_requests; /* XXX less than the card supports? */
973 pc->rq_count = 1; /* XXX Hardcode for a single queue */
976 pc->rq[0].rq_addr_hi = 0x0;
977 pc->rq[0].rq_addr_lo = sc->ciss_reply_phys;
982 /************************************************************************
983 * Wait for the adapter to come ready.
986 ciss_wait_adapter(struct ciss_softc *sc)
993 * Wait for the adapter to come ready.
995 if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
996 ciss_printf(sc, "waiting for adapter to come ready...\n");
997 for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
998 DELAY(1000000); /* one second */
1000 ciss_printf(sc, "timed out waiting for adapter to come ready\n");
1008 /************************************************************************
1009 * Flush the adapter cache.
1012 ciss_flush_adapter(struct ciss_softc *sc)
1014 struct ciss_request *cr;
1015 struct ciss_bmic_flush_cache *cbfc;
1016 int error, command_status;
1024 * Build a BMIC request to flush the cache. We don't disable
1025 * it, as we may be going to do more I/O (eg. we are emulating
1026 * the Synchronise Cache command).
1028 if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1032 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
1033 (void **)&cbfc, sizeof(*cbfc))) != 0)
1037 * Submit the request and wait for it to complete.
1039 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1040 ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
1047 ciss_report_request(cr, &command_status, NULL);
1048 switch(command_status) {
1049 case CISS_CMD_STATUS_SUCCESS:
1052 ciss_printf(sc, "error flushing cache (%s)\n",
1053 ciss_name_command_status(command_status));
1060 free(cbfc, CISS_MALLOC_CLASS);
1062 ciss_release_request(cr);
1067 ciss_soft_reset(struct ciss_softc *sc)
1069 struct ciss_request *cr = NULL;
1070 struct ciss_command *cc;
1073 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1074 /* only reset proxy controllers */
1075 if (sc->ciss_controllers[i].physical.bus == 0)
1078 if ((error = ciss_get_request(sc, &cr)) != 0)
1081 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
1086 cc->header.address = sc->ciss_controllers[i];
1088 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
1091 ciss_release_request(cr);
1095 ciss_printf(sc, "error resetting controller (%d)\n", error);
1098 ciss_release_request(cr);
1101 /************************************************************************
1102 * Allocate memory for the adapter command structures, initialise
1103 * the request structures.
1105 * Note that the entire set of commands are allocated in a single
1109 ciss_init_requests(struct ciss_softc *sc)
1111 struct ciss_request *cr;
1117 ciss_printf(sc, "using %d of %d available commands\n",
1118 sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
1121 * Create the DMA tag for commands.
1123 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
1124 32, 0, /* alignment, boundary */
1125 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1126 BUS_SPACE_MAXADDR, /* highaddr */
1127 NULL, NULL, /* filter, filterarg */
1128 CISS_COMMAND_ALLOC_SIZE *
1129 sc->ciss_max_requests, 1, /* maxsize, nsegments */
1130 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1132 NULL, NULL, /* lockfunc, lockarg */
1133 &sc->ciss_command_dmat)) {
1134 ciss_printf(sc, "can't allocate command DMA tag\n");
1138 * Allocate memory and make it available for DMA.
1140 if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
1141 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
1142 ciss_printf(sc, "can't allocate command memory\n");
1145 bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map,sc->ciss_command,
1146 CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
1147 ciss_command_map_helper, &sc->ciss_command_phys, 0);
1148 bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
1151 * Set up the request and command structures, push requests onto
1154 for (i = 1; i < sc->ciss_max_requests; i++) {
1155 cr = &sc->ciss_request[i];
1158 cr->cr_cc = (struct ciss_command *)((uintptr_t)sc->ciss_command +
1159 CISS_COMMAND_ALLOC_SIZE * i);
1160 cr->cr_ccphys = sc->ciss_command_phys + CISS_COMMAND_ALLOC_SIZE * i;
1161 bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
1162 ciss_enqueue_free(cr);
1168 ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1173 *addr = segs[0].ds_addr;
1176 /************************************************************************
1177 * Identify the adapter, print some information about it.
1180 ciss_identify_adapter(struct ciss_softc *sc)
1182 struct ciss_request *cr;
1183 int error, command_status;
1190 * Get a request, allocate storage for the adapter data.
1192 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
1193 (void **)&sc->ciss_id,
1194 sizeof(*sc->ciss_id))) != 0)
1198 * Submit the request and wait for it to complete.
1200 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1201 ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
1208 ciss_report_request(cr, &command_status, NULL);
1209 switch(command_status) {
1210 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1212 case CISS_CMD_STATUS_DATA_UNDERRUN:
1213 case CISS_CMD_STATUS_DATA_OVERRUN:
1214 ciss_printf(sc, "data over/underrun reading adapter information\n");
1216 ciss_printf(sc, "error reading adapter information (%s)\n",
1217 ciss_name_command_status(command_status));
1222 /* sanity-check reply */
1223 if (!sc->ciss_id->big_map_supported) {
1224 ciss_printf(sc, "adapter does not support BIG_MAP\n");
1230 /* XXX later revisions may not need this */
1231 sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
1234 /* XXX only really required for old 5300 adapters? */
1235 sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
1238 * Earlier controller specs do not contain these config
1239 * entries, so assume that a 0 means its old and assign
1240 * these values to the defaults that were established
1241 * when this driver was developed for them
1243 if (sc->ciss_cfg->max_logical_supported == 0)
1244 sc->ciss_cfg->max_logical_supported = CISS_MAX_LOGICAL;
1245 if (sc->ciss_cfg->max_physical_supported == 0)
1246 sc->ciss_cfg->max_physical_supported = CISS_MAX_PHYSICAL;
1247 /* print information */
1249 ciss_printf(sc, " %d logical drive%s configured\n",
1250 sc->ciss_id->configured_logical_drives,
1251 (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
1252 ciss_printf(sc, " firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
1253 ciss_printf(sc, " %d SCSI channels\n", sc->ciss_id->scsi_bus_count);
1255 ciss_printf(sc, " signature '%.4s'\n", sc->ciss_cfg->signature);
1256 ciss_printf(sc, " valence %d\n", sc->ciss_cfg->valence);
1257 ciss_printf(sc, " supported I/O methods 0x%b\n",
1258 sc->ciss_cfg->supported_methods,
1259 "\20\1READY\2simple\3performant\4MEMQ\n");
1260 ciss_printf(sc, " active I/O method 0x%b\n",
1261 sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
1262 ciss_printf(sc, " 4G page base 0x%08x\n",
1263 sc->ciss_cfg->command_physlimit);
1264 ciss_printf(sc, " interrupt coalesce delay %dus\n",
1265 sc->ciss_cfg->interrupt_coalesce_delay);
1266 ciss_printf(sc, " interrupt coalesce count %d\n",
1267 sc->ciss_cfg->interrupt_coalesce_count);
1268 ciss_printf(sc, " max outstanding commands %d\n",
1269 sc->ciss_cfg->max_outstanding_commands);
1270 ciss_printf(sc, " bus types 0x%b\n", sc->ciss_cfg->bus_types,
1271 "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
1272 ciss_printf(sc, " server name '%.16s'\n", sc->ciss_cfg->server_name);
1273 ciss_printf(sc, " heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
1274 ciss_printf(sc, " max logical logical volumes: %d\n", sc->ciss_cfg->max_logical_supported);
1275 ciss_printf(sc, " max physical disks supported: %d\n", sc->ciss_cfg->max_physical_supported);
1276 ciss_printf(sc, " max physical disks per logical volume: %d\n", sc->ciss_cfg->max_physical_per_logical);
1281 if (sc->ciss_id != NULL) {
1282 free(sc->ciss_id, CISS_MALLOC_CLASS);
1287 ciss_release_request(cr);
1291 /************************************************************************
1292 * Helper routine for generating a list of logical and physical luns.
1294 static struct ciss_lun_report *
1295 ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
1297 struct ciss_request *cr;
1298 struct ciss_command *cc;
1299 struct ciss_report_cdb *crc;
1300 struct ciss_lun_report *cll;
1311 * Get a request, allocate storage for the address list.
1313 if ((error = ciss_get_request(sc, &cr)) != 0)
1315 report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
1316 if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1317 ciss_printf(sc, "can't allocate memory for lun report\n");
1323 * Build the Report Logical/Physical LUNs command.
1327 cr->cr_length = report_size;
1328 cr->cr_flags = CISS_REQ_DATAIN;
1330 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
1331 cc->header.address.physical.bus = 0;
1332 cc->header.address.physical.target = 0;
1333 cc->cdb.cdb_length = sizeof(*crc);
1334 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1335 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1336 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1337 cc->cdb.timeout = 30; /* XXX better suggestions? */
1339 crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
1340 bzero(crc, sizeof(*crc));
1341 crc->opcode = opcode;
1342 crc->length = htonl(report_size); /* big-endian field */
1343 cll->list_size = htonl(report_size - sizeof(*cll)); /* big-endian field */
1346 * Submit the request and wait for it to complete. (timeout
1347 * here should be much greater than above)
1349 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1350 ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
1355 * Check response. Note that data over/underrun is OK.
1357 ciss_report_request(cr, &command_status, NULL);
1358 switch(command_status) {
1359 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1360 case CISS_CMD_STATUS_DATA_UNDERRUN: /* buffer too large, not bad */
1362 case CISS_CMD_STATUS_DATA_OVERRUN:
1363 ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
1364 sc->ciss_cfg->max_logical_supported);
1367 ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
1368 ciss_name_command_status(command_status));
1372 ciss_release_request(cr);
1377 ciss_release_request(cr);
1378 if (error && cll != NULL) {
1379 free(cll, CISS_MALLOC_CLASS);
1385 /************************************************************************
1386 * Find logical drives on the adapter.
1389 ciss_init_logical(struct ciss_softc *sc)
1391 struct ciss_lun_report *cll;
1392 int error = 0, i, j;
1397 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
1398 sc->ciss_cfg->max_logical_supported);
1404 /* sanity-check reply */
1405 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1406 if ((ndrives < 0) || (ndrives > sc->ciss_cfg->max_logical_supported)) {
1407 ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
1408 ndrives, sc->ciss_cfg->max_logical_supported);
1414 * Save logical drive information.
1417 ciss_printf(sc, "%d logical drive%s\n",
1418 ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
1422 malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
1423 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1424 if (sc->ciss_logical == NULL) {
1429 for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1430 sc->ciss_logical[i] =
1431 malloc(sc->ciss_cfg->max_logical_supported *
1432 sizeof(struct ciss_ldrive),
1433 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1434 if (sc->ciss_logical[i] == NULL) {
1439 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++)
1440 sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
1444 for (i = 0; i < sc->ciss_cfg->max_logical_supported; i++) {
1446 struct ciss_ldrive *ld;
1449 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
1450 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
1451 ld = &sc->ciss_logical[bus][target];
1453 ld->cl_address = cll->lun[i];
1454 ld->cl_controller = &sc->ciss_controllers[bus];
1455 if (ciss_identify_logical(sc, ld) != 0)
1458 * If the drive has had media exchanged, we should bring it online.
1460 if (ld->cl_lstatus->media_exchanged)
1461 ciss_accept_media(sc, ld);
1468 free(cll, CISS_MALLOC_CLASS);
1473 ciss_init_physical(struct ciss_softc *sc)
1475 struct ciss_lun_report *cll;
1485 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
1486 sc->ciss_cfg->max_physical_supported);
1492 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1495 ciss_printf(sc, "%d physical device%s\n",
1496 nphys, (nphys > 1 || nphys == 0) ? "s" : "");
1500 * Figure out the bus mapping.
1501 * Logical buses include both the local logical bus for local arrays and
1502 * proxy buses for remote arrays. Physical buses are numbered by the
1503 * controller and represent physical buses that hold physical devices.
1504 * We shift these bus numbers so that everything fits into a single flat
1505 * numbering space for CAM. Logical buses occupy the first 32 CAM bus
1506 * numbers, and the physical bus numbers are shifted to be above that.
1507 * This results in the various driver arrays being indexed as follows:
1509 * ciss_controllers[] - indexed by logical bus
1510 * ciss_cam_sim[] - indexed by both logical and physical, with physical
1511 * being shifted by 32.
1512 * ciss_logical[][] - indexed by logical bus
1513 * ciss_physical[][] - indexed by physical bus
1515 * XXX This is getting more and more hackish. CISS really doesn't play
1516 * well with a standard SCSI model; devices are addressed via magic
1517 * cookies, not via b/t/l addresses. Since there is no way to store
1518 * the cookie in the CAM device object, we have to keep these lookup
1519 * tables handy so that the devices can be found quickly at the cost
1520 * of wasting memory and having a convoluted lookup scheme. This
1521 * driver should probably be converted to block interface.
1524 * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
1525 * controller. A proxy controller is another physical controller
1526 * behind the primary PCI controller. We need to know about this
1527 * so that BMIC commands can be properly targeted. There can be
1528 * proxy controllers attached to a single PCI controller, so
1529 * find the highest numbered one so the array can be properly
1532 sc->ciss_max_logical_bus = 1;
1533 for (i = 0; i < nphys; i++) {
1534 if (cll->lun[i].physical.extra_address == 0) {
1535 bus = cll->lun[i].physical.bus;
1536 sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
1538 bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
1539 sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
1543 sc->ciss_controllers =
1544 malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
1545 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1547 if (sc->ciss_controllers == NULL) {
1548 ciss_printf(sc, "Could not allocate memory for controller map\n");
1553 /* setup a map of controller addresses */
1554 for (i = 0; i < nphys; i++) {
1555 if (cll->lun[i].physical.extra_address == 0) {
1556 sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
1561 malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
1562 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1563 if (sc->ciss_physical == NULL) {
1564 ciss_printf(sc, "Could not allocate memory for physical device map\n");
1569 for (i = 0; i < sc->ciss_max_physical_bus; i++) {
1570 sc->ciss_physical[i] =
1571 malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
1572 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1573 if (sc->ciss_physical[i] == NULL) {
1574 ciss_printf(sc, "Could not allocate memory for target map\n");
1580 ciss_filter_physical(sc, cll);
1584 free(cll, CISS_MALLOC_CLASS);
1590 ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
1596 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1597 for (i = 0; i < nphys; i++) {
1598 if (cll->lun[i].physical.extra_address == 0)
1602 * Filter out devices that we don't want. Level 3 LUNs could
1603 * probably be supported, but the docs don't give enough of a
1606 * The mode field of the physical address is likely set to have
1607 * hard disks masked out. Honor it unless the user has overridden
1608 * us with the tunable. We also munge the inquiry data for these
1609 * disks so that they only show up as passthrough devices. Keeping
1610 * them visible in this fashion is useful for doing things like
1611 * flashing firmware.
1613 ea = cll->lun[i].physical.extra_address;
1614 if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
1615 (CISS_EXTRA_MODE2(ea) == 0x3))
1617 if ((ciss_expose_hidden_physical == 0) &&
1618 (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
1622 * Note: CISS firmware numbers physical busses starting at '1', not
1623 * '0'. This numbering is internal to the firmware and is only
1624 * used as a hint here.
1626 bus = CISS_EXTRA_BUS2(ea) - 1;
1627 target = CISS_EXTRA_TARGET2(ea);
1628 sc->ciss_physical[bus][target].cp_address = cll->lun[i];
1629 sc->ciss_physical[bus][target].cp_online = 1;
1636 ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1638 struct ciss_request *cr;
1639 struct ciss_command *cc;
1640 struct scsi_inquiry *inq;
1646 bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1648 if ((error = ciss_get_request(sc, &cr)) != 0)
1652 cr->cr_data = &ld->cl_geometry;
1653 cr->cr_length = sizeof(ld->cl_geometry);
1654 cr->cr_flags = CISS_REQ_DATAIN;
1656 cc->header.address = ld->cl_address;
1657 cc->cdb.cdb_length = 6;
1658 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1659 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1660 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1661 cc->cdb.timeout = 30;
1663 inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1664 inq->opcode = INQUIRY;
1665 inq->byte2 = SI_EVPD;
1666 inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1667 scsi_ulto2b(sizeof(ld->cl_geometry), inq->length);
1669 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1670 ciss_printf(sc, "error getting geometry (%d)\n", error);
1674 ciss_report_request(cr, &command_status, NULL);
1675 switch(command_status) {
1676 case CISS_CMD_STATUS_SUCCESS:
1677 case CISS_CMD_STATUS_DATA_UNDERRUN:
1679 case CISS_CMD_STATUS_DATA_OVERRUN:
1680 ciss_printf(sc, "WARNING: Data overrun\n");
1683 ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1684 ciss_name_command_status(command_status));
1690 ciss_release_request(cr);
1693 /************************************************************************
1694 * Identify a logical drive, initialise state related to it.
1697 ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1699 struct ciss_request *cr;
1700 struct ciss_command *cc;
1701 struct ciss_bmic_cdb *cbc;
1702 int error, command_status;
1709 * Build a BMIC request to fetch the drive ID.
1711 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
1712 (void **)&ld->cl_ldrive,
1713 sizeof(*ld->cl_ldrive))) != 0)
1716 cc->header.address = *ld->cl_controller; /* target controller */
1717 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1718 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1721 * Submit the request and wait for it to complete.
1723 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1724 ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
1731 ciss_report_request(cr, &command_status, NULL);
1732 switch(command_status) {
1733 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1735 case CISS_CMD_STATUS_DATA_UNDERRUN:
1736 case CISS_CMD_STATUS_DATA_OVERRUN:
1737 ciss_printf(sc, "data over/underrun reading logical drive ID\n");
1739 ciss_printf(sc, "error reading logical drive ID (%s)\n",
1740 ciss_name_command_status(command_status));
1744 ciss_release_request(cr);
1748 * Build a CISS BMIC command to get the logical drive status.
1750 if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
1754 * Get the logical drive geometry.
1756 if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1760 * Print the drive's basic characteristics.
1763 ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
1764 CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
1765 CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
1766 ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
1767 ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
1768 ld->cl_ldrive->block_size));
1770 ciss_print_ldrive(sc, ld);
1774 /* make the drive not-exist */
1775 ld->cl_status = CISS_LD_NONEXISTENT;
1776 if (ld->cl_ldrive != NULL) {
1777 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
1778 ld->cl_ldrive = NULL;
1780 if (ld->cl_lstatus != NULL) {
1781 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
1782 ld->cl_lstatus = NULL;
1786 ciss_release_request(cr);
1791 /************************************************************************
1792 * Get status for a logical drive.
1794 * XXX should we also do this in response to Test Unit Ready?
1797 ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld)
1799 struct ciss_request *cr;
1800 struct ciss_command *cc;
1801 struct ciss_bmic_cdb *cbc;
1802 int error, command_status;
1805 * Build a CISS BMIC command to get the logical drive status.
1807 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
1808 (void **)&ld->cl_lstatus,
1809 sizeof(*ld->cl_lstatus))) != 0)
1812 cc->header.address = *ld->cl_controller; /* target controller */
1813 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1814 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1817 * Submit the request and wait for it to complete.
1819 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1820 ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1827 ciss_report_request(cr, &command_status, NULL);
1828 switch(command_status) {
1829 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1831 case CISS_CMD_STATUS_DATA_UNDERRUN:
1832 case CISS_CMD_STATUS_DATA_OVERRUN:
1833 ciss_printf(sc, "data over/underrun reading logical drive status\n");
1835 ciss_printf(sc, "error reading logical drive status (%s)\n",
1836 ciss_name_command_status(command_status));
1842 * Set the drive's summary status based on the returned status.
1844 * XXX testing shows that a failed JBOD drive comes back at next
1845 * boot in "queued for expansion" mode. WTF?
1847 ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
1851 ciss_release_request(cr);
1855 /************************************************************************
1856 * Notify the adapter of a config update.
1859 ciss_update_config(struct ciss_softc *sc)
1865 CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
1866 for (i = 0; i < 1000; i++) {
1867 if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
1868 CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
1876 /************************************************************************
1877 * Accept new media into a logical drive.
1879 * XXX The drive has previously been offline; it would be good if we
1880 * could make sure it's not open right now.
1883 ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
1885 struct ciss_request *cr;
1886 struct ciss_command *cc;
1887 struct ciss_bmic_cdb *cbc;
1889 int error = 0, ldrive;
1891 ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1893 debug(0, "bringing logical drive %d back online", ldrive);
1896 * Build a CISS BMIC command to bring the drive back online.
1898 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
1902 cc->header.address = *ld->cl_controller; /* target controller */
1903 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1904 cbc->log_drive = ldrive;
1907 * Submit the request and wait for it to complete.
1909 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1910 ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
1917 ciss_report_request(cr, &command_status, NULL);
1918 switch(command_status) {
1919 case CISS_CMD_STATUS_SUCCESS: /* all OK */
1920 /* we should get a logical drive status changed event here */
1923 ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
1924 ciss_name_command_status(command_status));
1930 ciss_release_request(cr);
1934 /************************************************************************
1935 * Release adapter resources.
1938 ciss_free(struct ciss_softc *sc)
1940 struct ciss_request *cr;
1945 /* we're going away */
1946 sc->ciss_flags |= CISS_FLAG_ABORTING;
1948 /* terminate the periodic heartbeat routine */
1949 callout_stop(&sc->ciss_periodic);
1951 /* cancel the Event Notify chain */
1952 ciss_notify_abort(sc);
1954 ciss_kill_notify_thread(sc);
1956 /* disconnect from CAM */
1957 if (sc->ciss_cam_sim) {
1958 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1959 if (sc->ciss_cam_sim[i]) {
1960 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1961 cam_sim_free(sc->ciss_cam_sim[i], 0);
1964 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
1965 CISS_PHYSICAL_BASE; i++) {
1966 if (sc->ciss_cam_sim[i]) {
1967 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1968 cam_sim_free(sc->ciss_cam_sim[i], 0);
1971 free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
1973 if (sc->ciss_cam_devq)
1974 cam_simq_free(sc->ciss_cam_devq);
1976 /* remove the control device */
1977 mtx_unlock(&sc->ciss_mtx);
1978 if (sc->ciss_dev_t != NULL)
1979 destroy_dev(sc->ciss_dev_t);
1981 /* Final cleanup of the callout. */
1982 callout_drain(&sc->ciss_periodic);
1983 mtx_destroy(&sc->ciss_mtx);
1985 /* free the controller data */
1986 if (sc->ciss_id != NULL)
1987 free(sc->ciss_id, CISS_MALLOC_CLASS);
1989 /* release I/O resources */
1990 if (sc->ciss_regs_resource != NULL)
1991 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1992 sc->ciss_regs_rid, sc->ciss_regs_resource);
1993 if (sc->ciss_cfg_resource != NULL)
1994 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1995 sc->ciss_cfg_rid, sc->ciss_cfg_resource);
1996 if (sc->ciss_intr != NULL)
1997 bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
1998 if (sc->ciss_irq_resource != NULL)
1999 bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
2000 sc->ciss_irq_rid[0], sc->ciss_irq_resource);
2002 pci_release_msi(sc->ciss_dev);
2004 while ((cr = ciss_dequeue_free(sc)) != NULL)
2005 bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
2006 if (sc->ciss_buffer_dmat)
2007 bus_dma_tag_destroy(sc->ciss_buffer_dmat);
2009 /* destroy command memory and DMA tag */
2010 if (sc->ciss_command != NULL) {
2011 bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
2012 bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
2014 if (sc->ciss_command_dmat)
2015 bus_dma_tag_destroy(sc->ciss_command_dmat);
2017 if (sc->ciss_reply) {
2018 bus_dmamap_unload(sc->ciss_reply_dmat, sc->ciss_reply_map);
2019 bus_dmamem_free(sc->ciss_reply_dmat, sc->ciss_reply, sc->ciss_reply_map);
2021 if (sc->ciss_reply_dmat)
2022 bus_dma_tag_destroy(sc->ciss_reply_dmat);
2024 /* destroy DMA tags */
2025 if (sc->ciss_parent_dmat)
2026 bus_dma_tag_destroy(sc->ciss_parent_dmat);
2027 if (sc->ciss_logical) {
2028 for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
2029 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
2030 if (sc->ciss_logical[i][j].cl_ldrive)
2031 free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
2032 if (sc->ciss_logical[i][j].cl_lstatus)
2033 free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
2035 free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
2037 free(sc->ciss_logical, CISS_MALLOC_CLASS);
2040 if (sc->ciss_physical) {
2041 for (i = 0; i < sc->ciss_max_physical_bus; i++)
2042 free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
2043 free(sc->ciss_physical, CISS_MALLOC_CLASS);
2046 if (sc->ciss_controllers)
2047 free(sc->ciss_controllers, CISS_MALLOC_CLASS);
2051 /************************************************************************
2052 * Give a command to the adapter.
2054 * Note that this uses the simple transport layer directly. If we
2055 * want to add support for other layers, we'll need a switch of some
2058 * Note that the simple transport layer has no way of refusing a
2059 * command; we only have as many request structures as the adapter
2060 * supports commands, so we don't have to check (this presumes that
2061 * the adapter can handle commands as fast as we throw them at it).
2064 ciss_start(struct ciss_request *cr)
2066 struct ciss_command *cc; /* XXX debugging only */
2070 debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
2073 * Map the request's data.
2075 if ((error = ciss_map_request(cr)))
2079 ciss_print_request(cr);
2085 /************************************************************************
2086 * Fetch completed request(s) from the adapter, queue them for
2087 * completion handling.
2089 * Note that this uses the simple transport layer directly. If we
2090 * want to add support for other layers, we'll need a switch of some
2093 * Note that the simple transport mechanism does not require any
2094 * reentrancy protection; the OPQ read is atomic. If there is a
2095 * chance of a race with something else that might move the request
2096 * off the busy list, then we will have to lock against that
2097 * (eg. timeouts, etc.)
2100 ciss_done(struct ciss_softc *sc, cr_qhead_t *qh)
2102 struct ciss_request *cr;
2103 struct ciss_command *cc;
2104 u_int32_t tag, index;
2109 * Loop quickly taking requests from the adapter and moving them
2110 * to the completed queue.
2114 tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
2115 if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
2118 debug(2, "completed command %d%s", index,
2119 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2120 if (index >= sc->ciss_max_requests) {
2121 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2124 cr = &(sc->ciss_request[index]);
2126 cc->header.host_tag = tag; /* not updated by adapter */
2127 ciss_enqueue_complete(cr, qh);
2133 ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh)
2135 struct ciss_request *cr;
2136 struct ciss_command *cc;
2137 u_int32_t tag, index;
2142 * Loop quickly taking requests from the adapter and moving them
2143 * to the completed queue.
2146 tag = sc->ciss_reply[sc->ciss_rqidx];
2147 if ((tag & CISS_CYCLE_MASK) != sc->ciss_cycle)
2150 debug(2, "completed command %d%s\n", index,
2151 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2152 if (index < sc->ciss_max_requests) {
2153 cr = &(sc->ciss_request[index]);
2155 cc->header.host_tag = tag; /* not updated by adapter */
2156 ciss_enqueue_complete(cr, qh);
2158 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2160 if (++sc->ciss_rqidx == sc->ciss_max_requests) {
2162 sc->ciss_cycle ^= 1;
2168 /************************************************************************
2169 * Take an interrupt from the adapter.
2172 ciss_intr(void *arg)
2175 struct ciss_softc *sc = (struct ciss_softc *)arg;
2178 * The only interrupt we recognise indicates that there are
2179 * entries in the outbound post queue.
2183 mtx_lock(&sc->ciss_mtx);
2184 ciss_complete(sc, &qh);
2185 mtx_unlock(&sc->ciss_mtx);
2189 ciss_perf_intr(void *arg)
2191 struct ciss_softc *sc = (struct ciss_softc *)arg;
2193 /* Clear the interrupt and flush the bridges. Docs say that the flush
2194 * needs to be done twice, which doesn't seem right.
2196 CISS_TL_PERF_CLEAR_INT(sc);
2197 CISS_TL_PERF_FLUSH_INT(sc);
2199 ciss_perf_msi_intr(sc);
2203 ciss_perf_msi_intr(void *arg)
2206 struct ciss_softc *sc = (struct ciss_softc *)arg;
2209 ciss_perf_done(sc, &qh);
2210 mtx_lock(&sc->ciss_mtx);
2211 ciss_complete(sc, &qh);
2212 mtx_unlock(&sc->ciss_mtx);
2216 /************************************************************************
2217 * Process completed requests.
2219 * Requests can be completed in three fashions:
2221 * - by invoking a callback function (cr_complete is non-null)
2222 * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
2223 * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
2226 ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh)
2228 struct ciss_request *cr;
2233 * Loop taking requests off the completed queue and performing
2234 * completion processing on them.
2237 if ((cr = ciss_dequeue_complete(sc, qh)) == NULL)
2239 ciss_unmap_request(cr);
2241 if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
2242 ciss_printf(sc, "WARNING: completing non-busy request\n");
2243 cr->cr_flags &= ~CISS_REQ_BUSY;
2246 * If the request has a callback, invoke it.
2248 if (cr->cr_complete != NULL) {
2249 cr->cr_complete(cr);
2254 * If someone is sleeping on this request, wake them up.
2256 if (cr->cr_flags & CISS_REQ_SLEEP) {
2257 cr->cr_flags &= ~CISS_REQ_SLEEP;
2263 * If someone is polling this request for completion, signal.
2265 if (cr->cr_flags & CISS_REQ_POLL) {
2266 cr->cr_flags &= ~CISS_REQ_POLL;
2271 * Give up and throw the request back on the free queue. This
2272 * should never happen; resources will probably be lost.
2274 ciss_printf(sc, "WARNING: completed command with no submitter\n");
2275 ciss_enqueue_free(cr);
2279 /************************************************************************
2280 * Report on the completion status of a request, and pass back SCSI
2281 * and command status values.
2284 _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func)
2286 struct ciss_command *cc;
2287 struct ciss_error_info *ce;
2292 ce = (struct ciss_error_info *)&(cc->sg[0]);
2295 * We don't consider data under/overrun an error for the Report
2296 * Logical/Physical LUNs commands.
2298 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
2299 ((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
2300 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
2301 ((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
2302 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
2303 (cc->cdb.cdb[0] == INQUIRY))) {
2304 cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
2305 debug(2, "ignoring irrelevant under/overrun error");
2309 * Check the command's error bit, if clear, there's no status and
2312 if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
2313 if (scsi_status != NULL)
2314 *scsi_status = SCSI_STATUS_OK;
2315 if (command_status != NULL)
2316 *command_status = CISS_CMD_STATUS_SUCCESS;
2319 if (command_status != NULL)
2320 *command_status = ce->command_status;
2321 if (scsi_status != NULL) {
2322 if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
2323 *scsi_status = ce->scsi_status;
2329 ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
2330 ce->command_status, ciss_name_command_status(ce->command_status),
2332 if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
2333 ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x, function %s\n",
2334 ce->additional_error_info.invalid_command.offense_size,
2335 ce->additional_error_info.invalid_command.offense_offset,
2336 ce->additional_error_info.invalid_command.offense_value,
2341 ciss_print_request(cr);
2346 /************************************************************************
2347 * Issue a request and don't return until it's completed.
2349 * Depending on adapter status, we may poll or sleep waiting for
2353 ciss_synch_request(struct ciss_request *cr, int timeout)
2355 if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
2356 return(ciss_wait_request(cr, timeout));
2358 return(ciss_poll_request(cr, timeout));
2362 /************************************************************************
2363 * Issue a request and poll for completion.
2365 * Timeout in milliseconds.
2368 ciss_poll_request(struct ciss_request *cr, int timeout)
2371 struct ciss_softc *sc;
2378 cr->cr_flags |= CISS_REQ_POLL;
2379 if ((error = ciss_start(cr)) != 0)
2384 ciss_perf_done(sc, &qh);
2387 ciss_complete(sc, &qh);
2388 if (!(cr->cr_flags & CISS_REQ_POLL))
2391 } while (timeout-- >= 0);
2392 return(EWOULDBLOCK);
2395 /************************************************************************
2396 * Issue a request and sleep waiting for completion.
2398 * Timeout in milliseconds. Note that a spurious wakeup will reset
2402 ciss_wait_request(struct ciss_request *cr, int timeout)
2408 cr->cr_flags |= CISS_REQ_SLEEP;
2409 if ((error = ciss_start(cr)) != 0)
2412 while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
2413 error = msleep(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ", (timeout * hz) / 1000);
2419 /************************************************************************
2420 * Abort a request. Note that a potential exists here to race the
2421 * request being completed; the caller must deal with this.
2424 ciss_abort_request(struct ciss_request *ar)
2426 struct ciss_request *cr;
2427 struct ciss_command *cc;
2428 struct ciss_message_cdb *cmc;
2434 if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
2437 /* build the abort command */
2439 cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL; /* addressing? */
2440 cc->header.address.physical.target = 0;
2441 cc->header.address.physical.bus = 0;
2442 cc->cdb.cdb_length = sizeof(*cmc);
2443 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
2444 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2445 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2446 cc->cdb.timeout = 30;
2448 cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
2449 cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
2450 cmc->type = CISS_MESSAGE_ABORT_TASK;
2451 cmc->abort_tag = ar->cr_tag; /* endianness?? */
2454 * Send the request and wait for a response. If we believe we
2455 * aborted the request OK, clear the flag that indicates it's
2458 error = ciss_synch_request(cr, 35 * 1000);
2460 error = ciss_report_request(cr, NULL, NULL);
2461 ciss_release_request(cr);
2468 /************************************************************************
2469 * Fetch and initialise a request
2472 ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
2474 struct ciss_request *cr;
2479 * Get a request and clean it up.
2481 if ((cr = ciss_dequeue_free(sc)) == NULL)
2486 cr->cr_complete = NULL;
2487 cr->cr_private = NULL;
2488 cr->cr_sg_tag = CISS_SG_MAX; /* Backstop to prevent accidents */
2490 ciss_preen_command(cr);
2496 ciss_preen_command(struct ciss_request *cr)
2498 struct ciss_command *cc;
2502 * Clean up the command structure.
2504 * Note that we set up the error_info structure here, since the
2505 * length can be overwritten by any command.
2508 cc->header.sg_in_list = 0; /* kinda inefficient this way */
2509 cc->header.sg_total = 0;
2510 cc->header.host_tag = cr->cr_tag << 2;
2511 cc->header.host_tag_zeroes = 0;
2512 bzero(&(cc->sg[0]), CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command));
2513 cmdphys = cr->cr_ccphys;
2514 cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
2515 cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
2518 /************************************************************************
2519 * Release a request to the free list.
2522 ciss_release_request(struct ciss_request *cr)
2524 struct ciss_softc *sc;
2530 /* release the request to the free queue */
2531 ciss_requeue_free(cr);
2534 /************************************************************************
2535 * Allocate a request that will be used to send a BMIC command. Do some
2536 * of the common setup here to avoid duplicating it everywhere else.
2539 ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
2540 int opcode, void **bufp, size_t bufsize)
2542 struct ciss_request *cr;
2543 struct ciss_command *cc;
2544 struct ciss_bmic_cdb *cbc;
2557 if ((error = ciss_get_request(sc, &cr)) != 0)
2561 * Allocate data storage if requested, determine the data direction.
2564 if ((bufsize > 0) && (bufp != NULL)) {
2565 if (*bufp == NULL) {
2566 if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
2572 dataout = 1; /* we are given a buffer, so we are writing */
2577 * Build a CISS BMIC command to get the logical drive ID.
2580 cr->cr_length = bufsize;
2582 cr->cr_flags = CISS_REQ_DATAIN;
2585 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2586 cc->header.address.physical.bus = 0;
2587 cc->header.address.physical.target = 0;
2588 cc->cdb.cdb_length = sizeof(*cbc);
2589 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2590 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2591 cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
2592 cc->cdb.timeout = 0;
2594 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
2595 bzero(cbc, sizeof(*cbc));
2596 cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
2597 cbc->bmic_opcode = opcode;
2598 cbc->size = htons((u_int16_t)bufsize);
2603 ciss_release_request(cr);
2606 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
2612 /************************************************************************
2613 * Handle a command passed in from userspace.
2616 ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
2618 struct ciss_request *cr;
2619 struct ciss_command *cc;
2620 struct ciss_error_info *ce;
2630 while (ciss_get_request(sc, &cr) != 0)
2631 msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
2635 * Allocate an in-kernel databuffer if required, copy in user data.
2637 mtx_unlock(&sc->ciss_mtx);
2638 cr->cr_length = ioc->buf_size;
2639 if (ioc->buf_size > 0) {
2640 if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
2644 if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
2645 debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2651 * Build the request based on the user command.
2653 bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
2654 bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
2656 /* XXX anything else to populate here? */
2657 mtx_lock(&sc->ciss_mtx);
2662 if ((error = ciss_synch_request(cr, 60 * 1000))) {
2663 debug(0, "request failed - %d", error);
2668 * Check to see if the command succeeded.
2670 ce = (struct ciss_error_info *)&(cc->sg[0]);
2671 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
2672 bzero(ce, sizeof(*ce));
2675 * Copy the results back to the user.
2677 bcopy(ce, &ioc->error_info, sizeof(*ce));
2678 mtx_unlock(&sc->ciss_mtx);
2679 if ((ioc->buf_size > 0) &&
2680 (error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
2681 debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2689 mtx_lock(&sc->ciss_mtx);
2692 if ((cr != NULL) && (cr->cr_data != NULL))
2693 free(cr->cr_data, CISS_MALLOC_CLASS);
2695 ciss_release_request(cr);
2699 /************************************************************************
2700 * Map a request into bus-visible space, initialise the scatter/gather
2704 ciss_map_request(struct ciss_request *cr)
2706 struct ciss_softc *sc;
2713 /* check that mapping is necessary */
2714 if (cr->cr_flags & CISS_REQ_MAPPED)
2717 cr->cr_flags |= CISS_REQ_MAPPED;
2719 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2720 BUS_DMASYNC_PREWRITE);
2722 if (cr->cr_data != NULL) {
2723 if (cr->cr_flags & CISS_REQ_CCB)
2724 error = bus_dmamap_load_ccb(sc->ciss_buffer_dmat,
2725 cr->cr_datamap, cr->cr_data,
2726 ciss_request_map_helper, cr, 0);
2728 error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2729 cr->cr_data, cr->cr_length,
2730 ciss_request_map_helper, cr, 0);
2735 * Post the command to the adapter.
2737 cr->cr_sg_tag = CISS_SG_NONE;
2738 cr->cr_flags |= CISS_REQ_BUSY;
2740 CISS_TL_PERF_POST_CMD(sc, cr);
2742 CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2749 ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2751 struct ciss_command *cc;
2752 struct ciss_request *cr;
2753 struct ciss_softc *sc;
2758 cr = (struct ciss_request *)arg;
2762 for (i = 0; i < nseg; i++) {
2763 cc->sg[i].address = segs[i].ds_addr;
2764 cc->sg[i].length = segs[i].ds_len;
2765 cc->sg[i].extension = 0;
2767 /* we leave the s/g table entirely within the command */
2768 cc->header.sg_in_list = nseg;
2769 cc->header.sg_total = nseg;
2771 if (cr->cr_flags & CISS_REQ_DATAIN)
2772 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2773 if (cr->cr_flags & CISS_REQ_DATAOUT)
2774 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2777 cr->cr_sg_tag = CISS_SG_NONE;
2779 cr->cr_sg_tag = CISS_SG_1;
2781 cr->cr_sg_tag = CISS_SG_2;
2783 cr->cr_sg_tag = CISS_SG_4;
2785 cr->cr_sg_tag = CISS_SG_8;
2786 else if (nseg <= 16)
2787 cr->cr_sg_tag = CISS_SG_16;
2788 else if (nseg <= 32)
2789 cr->cr_sg_tag = CISS_SG_32;
2791 cr->cr_sg_tag = CISS_SG_MAX;
2794 * Post the command to the adapter.
2796 cr->cr_flags |= CISS_REQ_BUSY;
2798 CISS_TL_PERF_POST_CMD(sc, cr);
2800 CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2803 /************************************************************************
2804 * Unmap a request from bus-visible space.
2807 ciss_unmap_request(struct ciss_request *cr)
2809 struct ciss_softc *sc;
2815 /* check that unmapping is necessary */
2816 if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
2819 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2820 BUS_DMASYNC_POSTWRITE);
2822 if (cr->cr_data == NULL)
2825 if (cr->cr_flags & CISS_REQ_DATAIN)
2826 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
2827 if (cr->cr_flags & CISS_REQ_DATAOUT)
2828 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
2830 bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2832 cr->cr_flags &= ~CISS_REQ_MAPPED;
2835 /************************************************************************
2836 * Attach the driver to CAM.
2838 * We put all the logical drives on a single SCSI bus.
2841 ciss_cam_init(struct ciss_softc *sc)
2848 * Allocate a devq. We can reuse this for the masked physical
2849 * devices if we decide to export these as well.
2851 if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests - 2)) == NULL) {
2852 ciss_printf(sc, "can't allocate CAM SIM queue\n");
2859 * This naturally wastes a bit of memory. The alternative is to allocate
2860 * and register each bus as it is found, and then track them on a linked
2861 * list. Unfortunately, the driver has a few places where it needs to
2862 * look up the SIM based solely on bus number, and it's unclear whether
2863 * a list traversal would work for these situations.
2865 maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
2866 CISS_PHYSICAL_BASE);
2867 sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
2868 CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
2869 if (sc->ciss_cam_sim == NULL) {
2870 ciss_printf(sc, "can't allocate memory for controller SIM\n");
2874 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2875 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2877 device_get_unit(sc->ciss_dev),
2880 sc->ciss_max_requests - 2,
2881 sc->ciss_cam_devq)) == NULL) {
2882 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2887 * Register bus with this SIM.
2889 mtx_lock(&sc->ciss_mtx);
2890 if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) {
2891 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2892 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2893 mtx_unlock(&sc->ciss_mtx);
2897 mtx_unlock(&sc->ciss_mtx);
2900 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
2901 CISS_PHYSICAL_BASE; i++) {
2902 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2904 device_get_unit(sc->ciss_dev),
2906 sc->ciss_max_requests - 2,
2907 sc->ciss_cam_devq)) == NULL) {
2908 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2912 mtx_lock(&sc->ciss_mtx);
2913 if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2914 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2915 mtx_unlock(&sc->ciss_mtx);
2918 mtx_unlock(&sc->ciss_mtx);
2924 /************************************************************************
2925 * Initiate a rescan of the 'logical devices' SIM
2928 ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
2934 if ((ccb = xpt_alloc_ccb_nowait()) == NULL) {
2935 ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
2939 if (xpt_create_path(&ccb->ccb_h.path, NULL,
2940 cam_sim_path(sc->ciss_cam_sim[bus]),
2941 target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2942 ciss_printf(sc, "rescan failed (can't create path)\n");
2947 /* scan is now in progress */
2950 /************************************************************************
2951 * Handle requests coming from CAM
2954 ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
2956 struct ciss_softc *sc;
2957 struct ccb_scsiio *csio;
2961 sc = cam_sim_softc(sim);
2962 bus = cam_sim_bus(sim);
2963 csio = (struct ccb_scsiio *)&ccb->csio;
2964 target = csio->ccb_h.target_id;
2965 physical = CISS_IS_PHYSICAL(bus);
2967 switch (ccb->ccb_h.func_code) {
2969 /* perform SCSI I/O */
2971 if (!ciss_cam_action_io(sim, csio))
2975 /* perform geometry calculations */
2976 case XPT_CALC_GEOMETRY:
2978 struct ccb_calc_geometry *ccg = &ccb->ccg;
2979 struct ciss_ldrive *ld;
2981 debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2985 ld = &sc->ciss_logical[bus][target];
2988 * Use the cached geometry settings unless the fault tolerance
2991 if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
2992 u_int32_t secs_per_cylinder;
2995 ccg->secs_per_track = 32;
2996 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2997 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2999 ccg->heads = ld->cl_geometry.heads;
3000 ccg->secs_per_track = ld->cl_geometry.sectors;
3001 ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
3003 ccb->ccb_h.status = CAM_REQ_CMP;
3007 /* handle path attribute inquiry */
3010 struct ccb_pathinq *cpi = &ccb->cpi;
3013 debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
3015 cpi->version_num = 1;
3016 cpi->hba_inquiry = PI_TAG_ABLE; /* XXX is this correct? */
3017 cpi->target_sprt = 0;
3019 cpi->max_target = sc->ciss_cfg->max_logical_supported;
3020 cpi->max_lun = 0; /* 'logical drive' channel only */
3021 cpi->initiator_id = sc->ciss_cfg->max_logical_supported;
3022 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
3023 strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
3024 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
3025 cpi->unit_number = cam_sim_unit(sim);
3026 cpi->bus_id = cam_sim_bus(sim);
3027 cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */
3028 cpi->transport = XPORT_SPI;
3029 cpi->transport_version = 2;
3030 cpi->protocol = PROTO_SCSI;
3031 cpi->protocol_version = SCSI_REV_2;
3032 if (sc->ciss_cfg->max_sg_length == 0) {
3035 /* XXX Fix for ZMR cards that advertise max_sg_length == 32
3036 * Confusing bit here. max_sg_length is usually a power of 2. We always
3037 * need to subtract 1 to account for partial pages. Then we need to
3038 * align on a valid PAGE_SIZE so we round down to the nearest power of 2.
3039 * Add 1 so we can then subtract it out in the assignment to maxio.
3040 * The reason for all these shenanigans is to create a maxio value that
3041 * creates IO operations to volumes that yield consistent operations
3042 * with good performance.
3044 sg_length = sc->ciss_cfg->max_sg_length - 1;
3045 sg_length = (1 << (fls(sg_length) - 1)) + 1;
3047 cpi->maxio = (min(CISS_MAX_SG_ELEMENTS, sg_length) - 1) * PAGE_SIZE;
3048 ccb->ccb_h.status = CAM_REQ_CMP;
3052 case XPT_GET_TRAN_SETTINGS:
3054 struct ccb_trans_settings *cts = &ccb->cts;
3056 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
3057 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3059 bus = cam_sim_bus(sim);
3060 target = cts->ccb_h.target_id;
3062 debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
3063 /* disconnect always OK */
3064 cts->protocol = PROTO_SCSI;
3065 cts->protocol_version = SCSI_REV_2;
3066 cts->transport = XPORT_SPI;
3067 cts->transport_version = 2;
3069 spi->valid = CTS_SPI_VALID_DISC;
3070 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
3072 scsi->valid = CTS_SCSI_VALID_TQ;
3073 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3075 cts->ccb_h.status = CAM_REQ_CMP;
3079 default: /* we can't do this */
3080 debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
3081 ccb->ccb_h.status = CAM_REQ_INVALID;
3088 /************************************************************************
3089 * Handle a CAM SCSI I/O request.
3092 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
3094 struct ciss_softc *sc;
3096 struct ciss_request *cr;
3097 struct ciss_command *cc;
3100 sc = cam_sim_softc(sim);
3101 bus = cam_sim_bus(sim);
3102 target = csio->ccb_h.target_id;
3104 debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
3106 /* check that the CDB pointer is not to a physical address */
3107 if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
3108 debug(3, " CDB pointer is to physical address");
3109 csio->ccb_h.status = CAM_REQ_CMP_ERR;
3112 /* abandon aborted ccbs or those that have failed validation */
3113 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
3114 debug(3, "abandoning CCB due to abort/validation failure");
3118 /* handle emulation of some SCSI commands ourself */
3119 if (ciss_cam_emulate(sc, csio))
3123 * Get a request to manage this command. If we can't, return the
3124 * ccb, freeze the queue and flag so that we unfreeze it when a
3125 * request completes.
3127 if ((error = ciss_get_request(sc, &cr)) != 0) {
3128 xpt_freeze_simq(sim, 1);
3129 sc->ciss_flags |= CISS_FLAG_BUSY;
3130 csio->ccb_h.status |= CAM_REQUEUE_REQ;
3135 * Build the command.
3139 cr->cr_length = csio->dxfer_len;
3140 cr->cr_complete = ciss_cam_complete;
3141 cr->cr_private = csio;
3144 * Target the right logical volume.
3146 if (CISS_IS_PHYSICAL(bus))
3147 cc->header.address =
3148 sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
3150 cc->header.address =
3151 sc->ciss_logical[bus][target].cl_address;
3152 cc->cdb.cdb_length = csio->cdb_len;
3153 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3154 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; /* XXX ordered tags? */
3155 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
3156 cr->cr_flags = CISS_REQ_DATAOUT | CISS_REQ_CCB;
3157 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3158 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
3159 cr->cr_flags = CISS_REQ_DATAIN | CISS_REQ_CCB;
3160 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3164 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
3166 cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
3167 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
3168 bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
3170 bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
3174 * Submit the request to the adapter.
3176 * Note that this may fail if we're unable to map the request (and
3177 * if we ever learn a transport layer other than simple, may fail
3178 * if the adapter rejects the command).
3180 if ((error = ciss_start(cr)) != 0) {
3181 xpt_freeze_simq(sim, 1);
3182 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3183 if (error == EINPROGRESS) {
3186 csio->ccb_h.status |= CAM_REQUEUE_REQ;
3187 ciss_release_request(cr);
3195 /************************************************************************
3196 * Emulate SCSI commands the adapter doesn't handle as we might like.
3199 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
3204 target = csio->ccb_h.target_id;
3205 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3206 opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3207 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
3209 if (CISS_IS_PHYSICAL(bus)) {
3210 if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
3211 csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3212 xpt_done((union ccb *)csio);
3219 * Handle requests for volumes that don't exist or are not online.
3220 * A selection timeout is slightly better than an illegal request.
3221 * Other errors might be better.
3223 if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
3224 csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3225 xpt_done((union ccb *)csio);
3229 /* if we have to fake Synchronise Cache */
3230 if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
3232 * If this is a Synchronise Cache command, typically issued when
3233 * a device is closed, flush the adapter and complete now.
3235 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
3236 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
3237 ciss_flush_adapter(sc);
3238 csio->ccb_h.status |= CAM_REQ_CMP;
3239 xpt_done((union ccb *)csio);
3245 * A CISS target can only ever have one lun per target. REPORT_LUNS requires
3246 * at least one LUN field to be pre created for us, so snag it and fill in
3247 * the least significant byte indicating 1 LUN here. Emulate the command
3248 * return to shut up warning on console of a CDB error. swb
3250 if (opcode == REPORT_LUNS && csio->dxfer_len > 0) {
3251 csio->data_ptr[3] = 8;
3252 csio->ccb_h.status |= CAM_REQ_CMP;
3253 xpt_done((union ccb *)csio);
3260 /************************************************************************
3261 * Check for possibly-completed commands.
3264 ciss_cam_poll(struct cam_sim *sim)
3267 struct ciss_softc *sc = cam_sim_softc(sim);
3273 ciss_perf_done(sc, &qh);
3276 ciss_complete(sc, &qh);
3279 /************************************************************************
3280 * Handle completion of a command - pass results back through the CCB
3283 ciss_cam_complete(struct ciss_request *cr)
3285 struct ciss_softc *sc;
3286 struct ciss_command *cc;
3287 struct ciss_error_info *ce;
3288 struct ccb_scsiio *csio;
3296 ce = (struct ciss_error_info *)&(cc->sg[0]);
3297 csio = (struct ccb_scsiio *)cr->cr_private;
3300 * Extract status values from request.
3302 ciss_report_request(cr, &command_status, &scsi_status);
3303 csio->scsi_status = scsi_status;
3306 * Handle specific SCSI status values.
3308 switch(scsi_status) {
3309 /* no status due to adapter error */
3311 debug(0, "adapter error");
3312 csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3315 /* no status due to command completed OK */
3316 case SCSI_STATUS_OK: /* CISS_SCSI_STATUS_GOOD */
3317 debug(2, "SCSI_STATUS_OK");
3318 csio->ccb_h.status |= CAM_REQ_CMP;
3321 /* check condition, sense data included */
3322 case SCSI_STATUS_CHECK_COND: /* CISS_SCSI_STATUS_CHECK_CONDITION */
3323 debug(0, "SCSI_STATUS_CHECK_COND sense size %d resid %d\n",
3324 ce->sense_length, ce->residual_count);
3325 bzero(&csio->sense_data, SSD_FULL_SIZE);
3326 bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
3327 if (csio->sense_len > ce->sense_length)
3328 csio->sense_resid = csio->sense_len - ce->sense_length;
3330 csio->sense_resid = 0;
3331 csio->resid = ce->residual_count;
3332 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
3335 struct scsi_sense_data *sns = (struct scsi_sense_data *)&ce->sense_info[0];
3336 debug(0, "sense key %x", scsi_get_sense_key(sns, csio->sense_len -
3337 csio->sense_resid, /*show_errors*/ 1));
3342 case SCSI_STATUS_BUSY: /* CISS_SCSI_STATUS_BUSY */
3343 debug(0, "SCSI_STATUS_BUSY");
3344 csio->ccb_h.status |= CAM_SCSI_BUSY;
3348 debug(0, "unknown status 0x%x", csio->scsi_status);
3349 csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3353 /* handle post-command fixup */
3354 ciss_cam_complete_fixup(sc, csio);
3356 ciss_release_request(cr);
3357 if (sc->ciss_flags & CISS_FLAG_BUSY) {
3358 sc->ciss_flags &= ~CISS_FLAG_BUSY;
3359 if (csio->ccb_h.status & CAM_RELEASE_SIMQ)
3360 xpt_release_simq(xpt_path_sim(csio->ccb_h.path), 0);
3362 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3364 xpt_done((union ccb *)csio);
3367 /********************************************************************************
3368 * Fix up the result of some commands here.
3371 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
3373 struct scsi_inquiry_data *inq;
3374 struct ciss_ldrive *cl;
3378 cdb = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3379 (uint8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes;
3380 if (cdb[0] == INQUIRY &&
3381 (cdb[1] & SI_EVPD) == 0 &&
3382 (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
3383 csio->dxfer_len >= SHORT_INQUIRY_LENGTH) {
3385 inq = (struct scsi_inquiry_data *)csio->data_ptr;
3386 target = csio->ccb_h.target_id;
3387 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3390 * Don't let hard drives be seen by the DA driver. They will still be
3391 * attached by the PASS driver.
3393 if (CISS_IS_PHYSICAL(bus)) {
3394 if (SID_TYPE(inq) == T_DIRECT)
3395 inq->device = (inq->device & 0xe0) | T_NODEVICE;
3399 cl = &sc->ciss_logical[bus][target];
3401 padstr(inq->vendor, "HP",
3403 padstr(inq->product,
3404 ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance),
3406 padstr(inq->revision,
3407 ciss_name_ldrive_status(cl->cl_lstatus->status),
3413 /********************************************************************************
3414 * Name the device at (target)
3416 * XXX is this strictly correct?
3419 ciss_name_device(struct ciss_softc *sc, int bus, int target)
3421 struct cam_periph *periph;
3422 struct cam_path *path;
3425 if (CISS_IS_PHYSICAL(bus))
3428 status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
3431 if (status == CAM_REQ_CMP) {
3432 xpt_path_lock(path);
3433 periph = cam_periph_find(path, NULL);
3434 sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
3435 periph->periph_name, periph->unit_number);
3436 xpt_path_unlock(path);
3437 xpt_free_path(path);
3440 sc->ciss_logical[bus][target].cl_name[0] = 0;
3444 /************************************************************************
3445 * Periodic status monitoring.
3448 ciss_periodic(void *arg)
3450 struct ciss_softc *sc;
3451 struct ciss_request *cr = NULL;
3452 struct ciss_command *cc = NULL;
3457 sc = (struct ciss_softc *)arg;
3460 * Check the adapter heartbeat.
3462 if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
3463 sc->ciss_heart_attack++;
3464 debug(0, "adapter heart attack in progress 0x%x/%d",
3465 sc->ciss_heartbeat, sc->ciss_heart_attack);
3466 if (sc->ciss_heart_attack == 3) {
3467 ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
3468 ciss_disable_adapter(sc);
3472 sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
3473 sc->ciss_heart_attack = 0;
3474 debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
3478 * Send the NOP message and wait for a response.
3480 if (ciss_nop_message_heartbeat != 0 && (error = ciss_get_request(sc, &cr)) == 0) {
3482 cr->cr_complete = ciss_nop_complete;
3483 cc->cdb.cdb_length = 1;
3484 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
3485 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3486 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3487 cc->cdb.timeout = 0;
3488 cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
3490 if ((error = ciss_start(cr)) != 0) {
3491 ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3496 * If the notify event request has died for some reason, or has
3497 * not started yet, restart it.
3499 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
3500 debug(0, "(re)starting Event Notify chain");
3501 ciss_notify_event(sc);
3507 callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
3511 ciss_nop_complete(struct ciss_request *cr)
3513 struct ciss_softc *sc;
3514 static int first_time = 1;
3517 if (ciss_report_request(cr, NULL, NULL) != 0) {
3518 if (first_time == 1) {
3520 ciss_printf(sc, "SENDING NOP MESSAGE FAILED (not logging anymore)\n");
3524 ciss_release_request(cr);
3527 /************************************************************************
3528 * Disable the adapter.
3530 * The all requests in completed queue is failed with hardware error.
3531 * This will cause failover in a multipath configuration.
3534 ciss_disable_adapter(struct ciss_softc *sc)
3537 struct ciss_request *cr;
3538 struct ciss_command *cc;
3539 struct ciss_error_info *ce;
3542 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
3543 pci_disable_busmaster(sc->ciss_dev);
3544 sc->ciss_flags &= ~CISS_FLAG_RUNNING;
3546 for (i = 1; i < sc->ciss_max_requests; i++) {
3547 cr = &sc->ciss_request[i];
3548 if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
3552 ce = (struct ciss_error_info *)&(cc->sg[0]);
3553 ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
3554 ciss_enqueue_complete(cr, &qh);
3558 if ((cr = ciss_dequeue_complete(sc, &qh)) == NULL)
3562 * If the request has a callback, invoke it.
3564 if (cr->cr_complete != NULL) {
3565 cr->cr_complete(cr);
3570 * If someone is sleeping on this request, wake them up.
3572 if (cr->cr_flags & CISS_REQ_SLEEP) {
3573 cr->cr_flags &= ~CISS_REQ_SLEEP;
3580 /************************************************************************
3581 * Request a notification response from the adapter.
3583 * If (cr) is NULL, this is the first request of the adapter, so
3584 * reset the adapter's message pointer and start with the oldest
3585 * message available.
3588 ciss_notify_event(struct ciss_softc *sc)
3590 struct ciss_request *cr;
3591 struct ciss_command *cc;
3592 struct ciss_notify_cdb *cnc;
3597 cr = sc->ciss_periodic_notify;
3599 /* get a request if we don't already have one */
3601 if ((error = ciss_get_request(sc, &cr)) != 0) {
3602 debug(0, "can't get notify event request");
3605 sc->ciss_periodic_notify = cr;
3606 cr->cr_complete = ciss_notify_complete;
3607 debug(1, "acquired request %d", cr->cr_tag);
3611 * Get a databuffer if we don't already have one, note that the
3612 * adapter command wants a larger buffer than the actual
3615 if (cr->cr_data == NULL) {
3616 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3617 debug(0, "can't get notify event request buffer");
3621 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3624 /* re-setup the request's command (since we never release it) XXX overkill*/
3625 ciss_preen_command(cr);
3627 /* (re)build the notify event command */
3629 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3630 cc->header.address.physical.bus = 0;
3631 cc->header.address.physical.target = 0;
3633 cc->cdb.cdb_length = sizeof(*cnc);
3634 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3635 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3636 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3637 cc->cdb.timeout = 0; /* no timeout, we hope */
3639 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3640 bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
3641 cnc->opcode = CISS_OPCODE_READ;
3642 cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
3643 cnc->timeout = 0; /* no timeout, we hope */
3644 cnc->synchronous = 0;
3646 cnc->seek_to_oldest = 0;
3647 if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
3651 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3653 /* submit the request */
3654 error = ciss_start(cr);
3659 if (cr->cr_data != NULL)
3660 free(cr->cr_data, CISS_MALLOC_CLASS);
3661 ciss_release_request(cr);
3663 sc->ciss_periodic_notify = NULL;
3664 debug(0, "can't submit notify event request");
3665 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3667 debug(1, "notify event submitted");
3668 sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
3673 ciss_notify_complete(struct ciss_request *cr)
3675 struct ciss_command *cc;
3676 struct ciss_notify *cn;
3677 struct ciss_softc *sc;
3683 cn = (struct ciss_notify *)cr->cr_data;
3687 * Report request results, decode status.
3689 ciss_report_request(cr, &command_status, &scsi_status);
3692 * Abort the chain on a fatal error.
3694 * XXX which of these are actually errors?
3696 if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
3697 (command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
3698 (command_status != CISS_CMD_STATUS_TIMEOUT)) { /* XXX timeout? */
3699 ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
3700 ciss_name_command_status(command_status));
3701 ciss_release_request(cr);
3702 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3707 * If the adapter gave us a text message, print it.
3709 if (cn->message[0] != 0)
3710 ciss_printf(sc, "*** %.80s\n", cn->message);
3712 debug(0, "notify event class %d subclass %d detail %d",
3713 cn->class, cn->subclass, cn->detail);
3716 * If the response indicates that the notifier has been aborted,
3717 * release the notifier command.
3719 if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
3720 (cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
3721 (cn->detail == 1)) {
3722 debug(0, "notifier exiting");
3723 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3724 ciss_release_request(cr);
3725 sc->ciss_periodic_notify = NULL;
3726 wakeup(&sc->ciss_periodic_notify);
3728 /* Handle notify events in a kernel thread */
3729 ciss_enqueue_notify(cr);
3730 sc->ciss_periodic_notify = NULL;
3731 wakeup(&sc->ciss_periodic_notify);
3732 wakeup(&sc->ciss_notify);
3735 * Send a new notify event command, if we're not aborting.
3737 if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
3738 ciss_notify_event(sc);
3742 /************************************************************************
3743 * Abort the Notify Event chain.
3745 * Note that we can't just abort the command in progress; we have to
3746 * explicitly issue an Abort Notify Event command in order for the
3747 * adapter to clean up correctly.
3749 * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
3750 * the chain will not restart itself.
3753 ciss_notify_abort(struct ciss_softc *sc)
3755 struct ciss_request *cr;
3756 struct ciss_command *cc;
3757 struct ciss_notify_cdb *cnc;
3758 int error, command_status, scsi_status;
3765 /* verify that there's an outstanding command */
3766 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3769 /* get a command to issue the abort with */
3770 if ((error = ciss_get_request(sc, &cr)))
3773 /* get a buffer for the result */
3774 if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3775 debug(0, "can't get notify event request buffer");
3779 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3783 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3784 cc->header.address.physical.bus = 0;
3785 cc->header.address.physical.target = 0;
3786 cc->cdb.cdb_length = sizeof(*cnc);
3787 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3788 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3789 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3790 cc->cdb.timeout = 0; /* no timeout, we hope */
3792 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3793 bzero(cnc, sizeof(*cnc));
3794 cnc->opcode = CISS_OPCODE_WRITE;
3795 cnc->command = CISS_COMMAND_ABORT_NOTIFY;
3796 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3798 ciss_print_request(cr);
3801 * Submit the request and wait for it to complete.
3803 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3804 ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
3811 ciss_report_request(cr, &command_status, &scsi_status);
3812 switch(command_status) {
3813 case CISS_CMD_STATUS_SUCCESS:
3815 case CISS_CMD_STATUS_INVALID_COMMAND:
3817 * Some older adapters don't support the CISS version of this
3818 * command. Fall back to using the BMIC version.
3820 error = ciss_notify_abort_bmic(sc);
3825 case CISS_CMD_STATUS_TARGET_STATUS:
3827 * This can happen if the adapter thinks there wasn't an outstanding
3828 * Notify Event command but we did. We clean up here.
3830 if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
3831 if (sc->ciss_periodic_notify != NULL)
3832 ciss_release_request(sc->ciss_periodic_notify);
3839 ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
3840 ciss_name_command_status(command_status));
3846 * Sleep waiting for the notifier command to complete. Note
3847 * that if it doesn't, we may end up in a bad situation, since
3848 * the adapter may deliver it later. Also note that the adapter
3849 * requires the Notify Event command to be cancelled in order to
3850 * maintain internal bookkeeping.
3852 while (sc->ciss_periodic_notify != NULL) {
3853 error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
3854 if (error == EWOULDBLOCK) {
3855 ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
3861 /* release the cancel request */
3863 if (cr->cr_data != NULL)
3864 free(cr->cr_data, CISS_MALLOC_CLASS);
3865 ciss_release_request(cr);
3868 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3872 /************************************************************************
3873 * Abort the Notify Event chain using a BMIC command.
3876 ciss_notify_abort_bmic(struct ciss_softc *sc)
3878 struct ciss_request *cr;
3879 int error, command_status;
3886 /* verify that there's an outstanding command */
3887 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3891 * Build a BMIC command to cancel the Notify on Event command.
3893 * Note that we are sending a CISS opcode here. Odd.
3895 if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
3900 * Submit the request and wait for it to complete.
3902 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3903 ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
3910 ciss_report_request(cr, &command_status, NULL);
3911 switch(command_status) {
3912 case CISS_CMD_STATUS_SUCCESS:
3915 ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
3916 ciss_name_command_status(command_status));
3923 ciss_release_request(cr);
3927 /************************************************************************
3928 * Handle rescanning all the logical volumes when a notify event
3929 * causes the drives to come online or offline.
3932 ciss_notify_rescan_logical(struct ciss_softc *sc)
3934 struct ciss_lun_report *cll;
3935 struct ciss_ldrive *ld;
3939 * We must rescan all logical volumes to get the right logical
3942 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
3943 sc->ciss_cfg->max_logical_supported);
3947 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
3950 * Delete any of the drives which were destroyed by the
3953 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
3954 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
3955 ld = &sc->ciss_logical[i][j];
3957 if (ld->cl_update == 0)
3960 if (ld->cl_status != CISS_LD_ONLINE) {
3961 ciss_cam_rescan_target(sc, i, j);
3964 free(ld->cl_ldrive, CISS_MALLOC_CLASS);
3966 free(ld->cl_lstatus, CISS_MALLOC_CLASS);
3968 ld->cl_ldrive = NULL;
3969 ld->cl_lstatus = NULL;
3975 * Scan for new drives.
3977 for (i = 0; i < ndrives; i++) {
3980 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
3981 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
3982 ld = &sc->ciss_logical[bus][target];
3984 if (ld->cl_update == 0)
3988 ld->cl_address = cll->lun[i];
3989 ld->cl_controller = &sc->ciss_controllers[bus];
3990 if (ciss_identify_logical(sc, ld) == 0) {
3991 ciss_cam_rescan_target(sc, bus, target);
3994 free(cll, CISS_MALLOC_CLASS);
3997 /************************************************************************
3998 * Handle a notify event relating to the status of a logical drive.
4000 * XXX need to be able to defer some of these to properly handle
4001 * calling the "ID Physical drive" command, unless the 'extended'
4002 * drive IDs are always in BIG_MAP format.
4005 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
4007 struct ciss_ldrive *ld;
4013 bus = cn->device.physical.bus;
4014 target = cn->data.logical_status.logical_drive;
4015 ld = &sc->ciss_logical[bus][target];
4017 switch (cn->subclass) {
4018 case CISS_NOTIFY_LOGICAL_STATUS:
4019 switch (cn->detail) {
4021 ciss_name_device(sc, bus, target);
4022 ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
4023 cn->data.logical_status.logical_drive, ld->cl_name,
4024 ciss_name_ldrive_status(cn->data.logical_status.previous_state),
4025 ciss_name_ldrive_status(cn->data.logical_status.new_state),
4026 cn->data.logical_status.spare_state,
4027 "\20\1configured\2rebuilding\3failed\4in use\5available\n");
4030 * Update our idea of the drive's status.
4032 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4033 if (ld->cl_lstatus != NULL)
4034 ld->cl_lstatus->status = cn->data.logical_status.new_state;
4037 * Have CAM rescan the drive if its status has changed.
4039 rescan_ld = (cn->data.logical_status.previous_state !=
4040 cn->data.logical_status.new_state) ? 1 : 0;
4043 ciss_notify_rescan_logical(sc);
4048 case 1: /* logical drive has recognised new media, needs Accept Media Exchange */
4049 ciss_name_device(sc, bus, target);
4050 ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
4051 cn->data.logical_status.logical_drive, ld->cl_name);
4052 ciss_accept_media(sc, ld);
4055 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
4056 ciss_notify_rescan_logical(sc);
4061 ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
4062 cn->data.rebuild_aborted.logical_drive,
4064 (cn->detail == 2) ? "read" : "write");
4069 case CISS_NOTIFY_LOGICAL_ERROR:
4070 if (cn->detail == 0) {
4071 ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
4072 cn->data.io_error.logical_drive,
4074 cn->data.io_error.failure_bus,
4075 cn->data.io_error.failure_drive);
4076 /* XXX should we take the drive down at this point, or will we be told? */
4080 case CISS_NOTIFY_LOGICAL_SURFACE:
4081 if (cn->detail == 0)
4082 ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
4083 cn->data.consistency_completed.logical_drive,
4089 /************************************************************************
4090 * Handle a notify event relating to the status of a physical drive.
4093 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
4097 /************************************************************************
4098 * Handle a notify event relating to the status of a physical drive.
4101 ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
4103 struct ciss_lun_report *cll = NULL;
4106 switch (cn->subclass) {
4107 case CISS_NOTIFY_HOTPLUG_PHYSICAL:
4108 case CISS_NOTIFY_HOTPLUG_NONDISK:
4109 bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
4111 CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
4113 if (cn->detail == 0) {
4115 * Mark the device offline so that it'll start producing selection
4116 * timeouts to the upper layer.
4118 if ((bus >= 0) && (target >= 0))
4119 sc->ciss_physical[bus][target].cp_online = 0;
4122 * Rescan the physical lun list for new items
4124 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
4125 sc->ciss_cfg->max_physical_supported);
4127 ciss_printf(sc, "Warning, cannot get physical lun list\n");
4130 ciss_filter_physical(sc, cll);
4135 ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
4140 free(cll, CISS_MALLOC_CLASS);
4143 /************************************************************************
4144 * Handle deferred processing of notify events. Notify events may need
4145 * sleep which is unsafe during an interrupt.
4148 ciss_notify_thread(void *arg)
4150 struct ciss_softc *sc;
4151 struct ciss_request *cr;
4152 struct ciss_notify *cn;
4154 sc = (struct ciss_softc *)arg;
4155 #if __FreeBSD_version >= 500000
4156 mtx_lock(&sc->ciss_mtx);
4160 if (STAILQ_EMPTY(&sc->ciss_notify) != 0 &&
4161 (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
4162 msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
4165 if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
4168 cr = ciss_dequeue_notify(sc);
4172 cn = (struct ciss_notify *)cr->cr_data;
4174 switch (cn->class) {
4175 case CISS_NOTIFY_HOTPLUG:
4176 ciss_notify_hotplug(sc, cn);
4178 case CISS_NOTIFY_LOGICAL:
4179 ciss_notify_logical(sc, cn);
4181 case CISS_NOTIFY_PHYSICAL:
4182 ciss_notify_physical(sc, cn);
4186 ciss_release_request(cr);
4189 sc->ciss_notify_thread = NULL;
4190 wakeup(&sc->ciss_notify_thread);
4192 #if __FreeBSD_version >= 500000
4193 mtx_unlock(&sc->ciss_mtx);
4198 /************************************************************************
4199 * Start the notification kernel thread.
4202 ciss_spawn_notify_thread(struct ciss_softc *sc)
4205 #if __FreeBSD_version > 500005
4206 if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4207 &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
4208 device_get_unit(sc->ciss_dev)))
4210 if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
4211 &sc->ciss_notify_thread, "ciss_notify%d",
4212 device_get_unit(sc->ciss_dev)))
4214 panic("Could not create notify thread\n");
4217 /************************************************************************
4218 * Kill the notification kernel thread.
4221 ciss_kill_notify_thread(struct ciss_softc *sc)
4224 if (sc->ciss_notify_thread == NULL)
4227 sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
4228 wakeup(&sc->ciss_notify);
4229 msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
4232 /************************************************************************
4236 ciss_print_request(struct ciss_request *cr)
4238 struct ciss_softc *sc;
4239 struct ciss_command *cc;
4245 ciss_printf(sc, "REQUEST @ %p\n", cr);
4246 ciss_printf(sc, " data %p/%d tag %d flags %b\n",
4247 cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
4248 "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
4249 ciss_printf(sc, " sg list/total %d/%d host tag 0x%x\n",
4250 cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
4251 switch(cc->header.address.mode.mode) {
4252 case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
4253 case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
4254 ciss_printf(sc, " physical bus %d target %d\n",
4255 cc->header.address.physical.bus, cc->header.address.physical.target);
4257 case CISS_HDR_ADDRESS_MODE_LOGICAL:
4258 ciss_printf(sc, " logical unit %d\n", cc->header.address.logical.lun);
4261 ciss_printf(sc, " %s cdb length %d type %s attribute %s\n",
4262 (cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
4263 (cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
4264 (cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
4266 (cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
4267 (cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
4268 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
4269 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
4270 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
4271 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
4272 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
4273 ciss_printf(sc, " %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
4275 if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
4276 /* XXX print error info */
4278 /* since we don't use chained s/g, don't support it here */
4279 for (i = 0; i < cc->header.sg_in_list; i++) {
4281 ciss_printf(sc, " ");
4282 printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
4283 if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
4289 /************************************************************************
4290 * Print information about the status of a logical drive.
4293 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
4297 if (ld->cl_lstatus == NULL) {
4298 printf("does not exist\n");
4302 /* print drive status */
4303 switch(ld->cl_lstatus->status) {
4304 case CISS_LSTATUS_OK:
4307 case CISS_LSTATUS_INTERIM_RECOVERY:
4308 printf("in interim recovery mode\n");
4310 case CISS_LSTATUS_READY_RECOVERY:
4311 printf("ready to begin recovery\n");
4313 case CISS_LSTATUS_RECOVERING:
4314 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4315 target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4316 printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
4317 bus, target, ld->cl_lstatus->blocks_to_recover);
4319 case CISS_LSTATUS_EXPANDING:
4320 printf("being expanded, %u blocks remaining\n",
4321 ld->cl_lstatus->blocks_to_recover);
4323 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4324 printf("queued for expansion\n");
4326 case CISS_LSTATUS_FAILED:
4327 printf("queued for expansion\n");
4329 case CISS_LSTATUS_WRONG_PDRIVE:
4330 printf("wrong physical drive inserted\n");
4332 case CISS_LSTATUS_MISSING_PDRIVE:
4333 printf("missing a needed physical drive\n");
4335 case CISS_LSTATUS_BECOMING_READY:
4336 printf("becoming ready\n");
4340 /* print failed physical drives */
4341 for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
4342 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
4343 target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
4346 ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
4347 ld->cl_lstatus->drive_failure_map[i]);
4352 #include "opt_ddb.h"
4354 #include <ddb/ddb.h>
4355 /************************************************************************
4356 * Print information about the controller/driver.
4359 ciss_print_adapter(struct ciss_softc *sc)
4363 ciss_printf(sc, "ADAPTER:\n");
4364 for (i = 0; i < CISSQ_COUNT; i++) {
4365 ciss_printf(sc, "%s %d/%d\n",
4367 i == 1 ? "busy" : "complete",
4368 sc->ciss_qstat[i].q_length,
4369 sc->ciss_qstat[i].q_max);
4371 ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
4372 ciss_printf(sc, "flags %b\n", sc->ciss_flags,
4373 "\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
4375 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
4376 for (j = 0; j < sc->ciss_cfg->max_logical_supported; j++) {
4377 ciss_printf(sc, "LOGICAL DRIVE %d: ", i);
4378 ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
4382 /* XXX Should physical drives be printed out here? */
4384 for (i = 1; i < sc->ciss_max_requests; i++)
4385 ciss_print_request(sc->ciss_request + i);
4389 DB_COMMAND(ciss_prt, db_ciss_prt)
4391 struct ciss_softc *sc;
4395 dc = devclass_find("ciss");
4397 printf("%s: can't find devclass!\n", __func__);
4400 maxciss = devclass_get_maxunit(dc);
4401 for (i = 0; i < maxciss; i++) {
4402 sc = devclass_get_softc(dc, i);
4403 ciss_print_adapter(sc);
4409 /************************************************************************
4410 * Return a name for a logical drive status value.
4413 ciss_name_ldrive_status(int status)
4416 case CISS_LSTATUS_OK:
4418 case CISS_LSTATUS_FAILED:
4420 case CISS_LSTATUS_NOT_CONFIGURED:
4421 return("not configured");
4422 case CISS_LSTATUS_INTERIM_RECOVERY:
4423 return("interim recovery");
4424 case CISS_LSTATUS_READY_RECOVERY:
4425 return("ready for recovery");
4426 case CISS_LSTATUS_RECOVERING:
4427 return("recovering");
4428 case CISS_LSTATUS_WRONG_PDRIVE:
4429 return("wrong physical drive inserted");
4430 case CISS_LSTATUS_MISSING_PDRIVE:
4431 return("missing physical drive");
4432 case CISS_LSTATUS_EXPANDING:
4433 return("expanding");
4434 case CISS_LSTATUS_BECOMING_READY:
4435 return("becoming ready");
4436 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4437 return("queued for expansion");
4439 return("unknown status");
4442 /************************************************************************
4443 * Return an online/offline/nonexistent value for a logical drive
4447 ciss_decode_ldrive_status(int status)
4450 case CISS_LSTATUS_NOT_CONFIGURED:
4451 return(CISS_LD_NONEXISTENT);
4453 case CISS_LSTATUS_OK:
4454 case CISS_LSTATUS_INTERIM_RECOVERY:
4455 case CISS_LSTATUS_READY_RECOVERY:
4456 case CISS_LSTATUS_RECOVERING:
4457 case CISS_LSTATUS_EXPANDING:
4458 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4459 return(CISS_LD_ONLINE);
4461 case CISS_LSTATUS_FAILED:
4462 case CISS_LSTATUS_WRONG_PDRIVE:
4463 case CISS_LSTATUS_MISSING_PDRIVE:
4464 case CISS_LSTATUS_BECOMING_READY:
4466 return(CISS_LD_OFFLINE);
4471 /************************************************************************
4472 * Return a name for a logical drive's organisation.
4475 ciss_name_ldrive_org(int org)
4478 case CISS_LDRIVE_RAID0:
4480 case CISS_LDRIVE_RAID1:
4481 return("RAID 1(1+0)");
4482 case CISS_LDRIVE_RAID4:
4484 case CISS_LDRIVE_RAID5:
4486 case CISS_LDRIVE_RAID51:
4488 case CISS_LDRIVE_RAIDADG:
4494 /************************************************************************
4495 * Return a name for a command status value.
4498 ciss_name_command_status(int status)
4501 case CISS_CMD_STATUS_SUCCESS:
4503 case CISS_CMD_STATUS_TARGET_STATUS:
4504 return("target status");
4505 case CISS_CMD_STATUS_DATA_UNDERRUN:
4506 return("data underrun");
4507 case CISS_CMD_STATUS_DATA_OVERRUN:
4508 return("data overrun");
4509 case CISS_CMD_STATUS_INVALID_COMMAND:
4510 return("invalid command");
4511 case CISS_CMD_STATUS_PROTOCOL_ERROR:
4512 return("protocol error");
4513 case CISS_CMD_STATUS_HARDWARE_ERROR:
4514 return("hardware error");
4515 case CISS_CMD_STATUS_CONNECTION_LOST:
4516 return("connection lost");
4517 case CISS_CMD_STATUS_ABORTED:
4519 case CISS_CMD_STATUS_ABORT_FAILED:
4520 return("abort failed");
4521 case CISS_CMD_STATUS_UNSOLICITED_ABORT:
4522 return("unsolicited abort");
4523 case CISS_CMD_STATUS_TIMEOUT:
4525 case CISS_CMD_STATUS_UNABORTABLE:
4526 return("unabortable");
4528 return("unknown status");
4531 /************************************************************************
4532 * Handle an open on the control device.
4535 ciss_open(struct cdev *dev, int flags, int fmt, struct thread *p)
4537 struct ciss_softc *sc;
4541 sc = (struct ciss_softc *)dev->si_drv1;
4543 /* we might want to veto if someone already has us open */
4545 mtx_lock(&sc->ciss_mtx);
4546 sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
4547 mtx_unlock(&sc->ciss_mtx);
4551 /************************************************************************
4552 * Handle the last close on the control device.
4555 ciss_close(struct cdev *dev, int flags, int fmt, struct thread *p)
4557 struct ciss_softc *sc;
4561 sc = (struct ciss_softc *)dev->si_drv1;
4563 mtx_lock(&sc->ciss_mtx);
4564 sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
4565 mtx_unlock(&sc->ciss_mtx);
4569 /********************************************************************************
4570 * Handle adapter-specific control operations.
4572 * Note that the API here is compatible with the Linux driver, in order to
4573 * simplify the porting of Compaq's userland tools.
4576 ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *p)
4578 struct ciss_softc *sc;
4579 IOCTL_Command_struct *ioc = (IOCTL_Command_struct *)addr;
4581 IOCTL_Command_struct32 *ioc32 = (IOCTL_Command_struct32 *)addr;
4582 IOCTL_Command_struct ioc_swab;
4588 sc = (struct ciss_softc *)dev->si_drv1;
4590 mtx_lock(&sc->ciss_mtx);
4593 case CCISS_GETQSTATS:
4595 union ciss_statrequest *cr = (union ciss_statrequest *)addr;
4597 switch (cr->cs_item) {
4600 bcopy(&sc->ciss_qstat[cr->cs_item], &cr->cs_qstat,
4601 sizeof(struct ciss_qstat));
4611 case CCISS_GETPCIINFO:
4613 cciss_pci_info_struct *pis = (cciss_pci_info_struct *)addr;
4615 pis->bus = pci_get_bus(sc->ciss_dev);
4616 pis->dev_fn = pci_get_slot(sc->ciss_dev);
4617 pis->board_id = (pci_get_subvendor(sc->ciss_dev) << 16) |
4618 pci_get_subdevice(sc->ciss_dev);
4623 case CCISS_GETINTINFO:
4625 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4627 cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
4628 cis->count = sc->ciss_cfg->interrupt_coalesce_count;
4633 case CCISS_SETINTINFO:
4635 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4637 if ((cis->delay == 0) && (cis->count == 0)) {
4643 * XXX apparently this is only safe if the controller is idle,
4644 * we should suspend it before doing this.
4646 sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
4647 sc->ciss_cfg->interrupt_coalesce_count = cis->count;
4649 if (ciss_update_config(sc))
4652 /* XXX resume the controller here */
4656 case CCISS_GETNODENAME:
4657 bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
4658 sizeof(NodeName_type));
4661 case CCISS_SETNODENAME:
4662 bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
4663 sizeof(NodeName_type));
4664 if (ciss_update_config(sc))
4668 case CCISS_GETHEARTBEAT:
4669 *(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
4672 case CCISS_GETBUSTYPES:
4673 *(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
4676 case CCISS_GETFIRMVER:
4677 bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
4678 sizeof(FirmwareVer_type));
4681 case CCISS_GETDRIVERVER:
4682 *(DriverVer_type *)addr = CISS_DRIVER_VERSION;
4685 case CCISS_REVALIDVOLS:
4687 * This is a bit ugly; to do it "right" we really need
4688 * to find any disks that have changed, kick CAM off them,
4689 * then rescan only these disks. It'd be nice if they
4690 * a) told us which disk(s) they were going to play with,
4691 * and b) which ones had arrived. 8(
4696 case CCISS_PASSTHRU32:
4697 ioc_swab.LUN_info = ioc32->LUN_info;
4698 ioc_swab.Request = ioc32->Request;
4699 ioc_swab.error_info = ioc32->error_info;
4700 ioc_swab.buf_size = ioc32->buf_size;
4701 ioc_swab.buf = (u_int8_t *)(uintptr_t)ioc32->buf;
4706 case CCISS_PASSTHRU:
4707 error = ciss_user_command(sc, ioc);
4711 debug(0, "unknown ioctl 0x%lx", cmd);
4713 debug(1, "CCISS_GETPCIINFO: 0x%lx", CCISS_GETPCIINFO);
4714 debug(1, "CCISS_GETINTINFO: 0x%lx", CCISS_GETINTINFO);
4715 debug(1, "CCISS_SETINTINFO: 0x%lx", CCISS_SETINTINFO);
4716 debug(1, "CCISS_GETNODENAME: 0x%lx", CCISS_GETNODENAME);
4717 debug(1, "CCISS_SETNODENAME: 0x%lx", CCISS_SETNODENAME);
4718 debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
4719 debug(1, "CCISS_GETBUSTYPES: 0x%lx", CCISS_GETBUSTYPES);
4720 debug(1, "CCISS_GETFIRMVER: 0x%lx", CCISS_GETFIRMVER);
4721 debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
4722 debug(1, "CCISS_REVALIDVOLS: 0x%lx", CCISS_REVALIDVOLS);
4723 debug(1, "CCISS_PASSTHRU: 0x%lx", CCISS_PASSTHRU);
4729 mtx_unlock(&sc->ciss_mtx);