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
27 * $FreeBSD: src/sys/dev/ciss/ciss.c,v 1.113 2012/03/12 08:03:51 scottl Exp $
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>
87 #include <sys/device.h>
89 #include <bus/cam/cam.h>
90 #include <bus/cam/cam_ccb.h>
91 #include <bus/cam/cam_periph.h>
92 #include <bus/cam/cam_sim.h>
93 #include <bus/cam/cam_xpt_sim.h>
94 #include <bus/cam/scsi/scsi_all.h>
95 #include <bus/cam/scsi/scsi_message.h>
97 #include <machine/endian.h>
100 #include <bus/pci/pcireg.h>
101 #include <bus/pci/pcivar.h>
103 #include <dev/raid/ciss/cissreg.h>
104 #include <dev/raid/ciss/cissio.h>
105 #include <dev/raid/ciss/cissvar.h>
107 static MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data",
108 "ciss internal data buffers");
111 static int ciss_lookup(device_t dev);
112 static int ciss_probe(device_t dev);
113 static int ciss_attach(device_t dev);
114 static int ciss_detach(device_t dev);
115 static int ciss_shutdown(device_t dev);
117 /* (de)initialisation functions, control wrappers */
118 static int ciss_init_pci(struct ciss_softc *sc);
119 static int ciss_setup_msix(struct ciss_softc *sc);
120 static int ciss_init_perf(struct ciss_softc *sc);
121 static int ciss_wait_adapter(struct ciss_softc *sc);
122 static int ciss_flush_adapter(struct ciss_softc *sc);
123 static int ciss_init_requests(struct ciss_softc *sc);
124 static void ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
125 int nseg, int error);
126 static int ciss_identify_adapter(struct ciss_softc *sc);
127 static int ciss_init_logical(struct ciss_softc *sc);
128 static int ciss_init_physical(struct ciss_softc *sc);
129 static int ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll);
130 static int ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
131 static int ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld);
132 static int ciss_update_config(struct ciss_softc *sc);
133 static int ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld);
134 static void ciss_init_sysctl(struct ciss_softc *sc);
135 static void ciss_soft_reset(struct ciss_softc *sc);
136 static void ciss_free(struct ciss_softc *sc);
137 static void ciss_spawn_notify_thread(struct ciss_softc *sc);
138 static void ciss_kill_notify_thread(struct ciss_softc *sc);
140 /* request submission/completion */
141 static int ciss_start(struct ciss_request *cr);
142 static void ciss_done(struct ciss_softc *sc, cr_qhead_t *qh);
143 static void ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh);
144 static void ciss_intr(void *arg);
145 static void ciss_perf_intr(void *arg);
146 static void ciss_perf_msi_intr(void *arg);
147 static void ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh);
148 static int _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func);
149 static int ciss_synch_request(struct ciss_request *cr, int timeout);
150 static int ciss_poll_request(struct ciss_request *cr, int timeout);
151 static int ciss_wait_request(struct ciss_request *cr, int timeout);
153 static int ciss_abort_request(struct ciss_request *cr);
156 /* request queueing */
157 static int ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
158 static void ciss_preen_command(struct ciss_request *cr);
159 static void ciss_release_request(struct ciss_request *cr);
161 /* request helpers */
162 static int ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
163 int opcode, void **bufp, size_t bufsize);
164 static int ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
167 static int ciss_map_request(struct ciss_request *cr);
168 static void ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
169 int nseg, int error);
170 static void ciss_unmap_request(struct ciss_request *cr);
173 static int ciss_cam_init(struct ciss_softc *sc);
174 static void ciss_cam_rescan_target(struct ciss_softc *sc,
175 int bus, int target);
176 static void ciss_cam_rescan_all(struct ciss_softc *sc);
177 static void ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb);
178 static void ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
179 static int ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
180 static int ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
181 static void ciss_cam_poll(struct cam_sim *sim);
182 static void ciss_cam_complete(struct ciss_request *cr);
183 static void ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
184 static struct cam_periph *ciss_find_periph(struct ciss_softc *sc,
185 int bus, int target);
186 static int ciss_name_device(struct ciss_softc *sc, int bus, int target);
188 /* periodic status monitoring */
189 static void ciss_periodic(void *arg);
190 static void ciss_nop_complete(struct ciss_request *cr);
191 static void ciss_disable_adapter(struct ciss_softc *sc);
192 static void ciss_notify_event(struct ciss_softc *sc);
193 static void ciss_notify_complete(struct ciss_request *cr);
194 static int ciss_notify_abort(struct ciss_softc *sc);
195 static int ciss_notify_abort_bmic(struct ciss_softc *sc);
196 static void ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
197 static void ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
198 static void ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
200 /* debugging output */
201 static void ciss_print_request(struct ciss_request *cr);
202 static void ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
203 static const char *ciss_name_ldrive_status(int status);
204 static int ciss_decode_ldrive_status(int status);
205 static const char *ciss_name_ldrive_org(int org);
206 static const char *ciss_name_command_status(int status);
211 static device_method_t ciss_methods[] = {
212 /* Device interface */
213 DEVMETHOD(device_probe, ciss_probe),
214 DEVMETHOD(device_attach, ciss_attach),
215 DEVMETHOD(device_detach, ciss_detach),
216 DEVMETHOD(device_shutdown, ciss_shutdown),
220 static driver_t ciss_pci_driver = {
223 sizeof(struct ciss_softc)
226 static devclass_t ciss_devclass;
227 DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, NULL, NULL);
228 MODULE_VERSION(ciss, 1);
229 MODULE_DEPEND(ciss, cam, 1, 1, 1);
230 MODULE_DEPEND(ciss, pci, 1, 1, 1);
233 * Control device interface.
235 static d_open_t ciss_open;
236 static d_close_t ciss_close;
237 static d_ioctl_t ciss_ioctl;
239 static struct dev_ops ciss_ops = {
242 .d_close = ciss_close,
243 .d_ioctl = ciss_ioctl,
247 * This tunable can be set at boot time and controls whether physical devices
248 * that are marked hidden by the firmware should be exposed anyways.
250 static unsigned int ciss_expose_hidden_physical = 0;
251 TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
253 static unsigned int ciss_nop_message_heartbeat = 0;
254 TUNABLE_INT("hw.ciss.nop_message_heartbeat", &ciss_nop_message_heartbeat);
257 * This tunable can force a particular transport to be used:
260 * 2 : force performant
262 static int ciss_force_transport = 0;
263 TUNABLE_INT("hw.ciss.force_transport", &ciss_force_transport);
266 * This tunable can force a particular interrupt delivery method to be used:
271 static int ciss_force_interrupt = 0;
272 TUNABLE_INT("hw.ciss.force_interrupt", &ciss_force_interrupt);
274 /************************************************************************
275 * CISS adapters amazingly don't have a defined programming interface
276 * value. (One could say some very despairing things about PCI and
277 * people just not getting the general idea.) So we are forced to
278 * stick with matching against subvendor/subdevice, and thus have to
279 * be updated for every new CISS adapter that appears.
281 #define CISS_BOARD_UNKNWON 0
282 #define CISS_BOARD_SA5 1
283 #define CISS_BOARD_SA5B 2
284 #define CISS_BOARD_NOMSI (1<<4)
292 } ciss_vendor_data[] = {
293 { 0x0e11, 0x4070, CISS_BOARD_SA5|CISS_BOARD_NOMSI, "Compaq Smart Array 5300" },
294 { 0x0e11, 0x4080, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "Compaq Smart Array 5i" },
295 { 0x0e11, 0x4082, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "Compaq Smart Array 532" },
296 { 0x0e11, 0x4083, CISS_BOARD_SA5B|CISS_BOARD_NOMSI, "HP Smart Array 5312" },
297 { 0x0e11, 0x4091, CISS_BOARD_SA5, "HP Smart Array 6i" },
298 { 0x0e11, 0x409A, CISS_BOARD_SA5, "HP Smart Array 641" },
299 { 0x0e11, 0x409B, CISS_BOARD_SA5, "HP Smart Array 642" },
300 { 0x0e11, 0x409C, CISS_BOARD_SA5, "HP Smart Array 6400" },
301 { 0x0e11, 0x409D, CISS_BOARD_SA5, "HP Smart Array 6400 EM" },
302 { 0x103C, 0x3211, CISS_BOARD_SA5, "HP Smart Array E200i" },
303 { 0x103C, 0x3212, CISS_BOARD_SA5, "HP Smart Array E200" },
304 { 0x103C, 0x3213, CISS_BOARD_SA5, "HP Smart Array E200i" },
305 { 0x103C, 0x3214, CISS_BOARD_SA5, "HP Smart Array E200i" },
306 { 0x103C, 0x3215, CISS_BOARD_SA5, "HP Smart Array E200i" },
307 { 0x103C, 0x3220, CISS_BOARD_SA5, "HP Smart Array" },
308 { 0x103C, 0x3222, CISS_BOARD_SA5, "HP Smart Array" },
309 { 0x103C, 0x3223, CISS_BOARD_SA5, "HP Smart Array P800" },
310 { 0x103C, 0x3225, CISS_BOARD_SA5, "HP Smart Array P600" },
311 { 0x103C, 0x3230, CISS_BOARD_SA5, "HP Smart Array" },
312 { 0x103C, 0x3231, CISS_BOARD_SA5, "HP Smart Array" },
313 { 0x103C, 0x3232, CISS_BOARD_SA5, "HP Smart Array" },
314 { 0x103C, 0x3233, CISS_BOARD_SA5, "HP Smart Array" },
315 { 0x103C, 0x3234, CISS_BOARD_SA5, "HP Smart Array P400" },
316 { 0x103C, 0x3235, CISS_BOARD_SA5, "HP Smart Array P400i" },
317 { 0x103C, 0x3236, CISS_BOARD_SA5, "HP Smart Array" },
318 { 0x103C, 0x3237, CISS_BOARD_SA5, "HP Smart Array E500" },
319 { 0x103C, 0x3238, CISS_BOARD_SA5, "HP Smart Array" },
320 { 0x103C, 0x3239, CISS_BOARD_SA5, "HP Smart Array" },
321 { 0x103C, 0x323A, CISS_BOARD_SA5, "HP Smart Array" },
322 { 0x103C, 0x323B, CISS_BOARD_SA5, "HP Smart Array" },
323 { 0x103C, 0x323C, CISS_BOARD_SA5, "HP Smart Array" },
324 { 0x103C, 0x323D, CISS_BOARD_SA5, "HP Smart Array P700m" },
325 { 0x103C, 0x3241, CISS_BOARD_SA5, "HP Smart Array P212" },
326 { 0x103C, 0x3243, CISS_BOARD_SA5, "HP Smart Array P410" },
327 { 0x103C, 0x3245, CISS_BOARD_SA5, "HP Smart Array P410i" },
328 { 0x103C, 0x3247, CISS_BOARD_SA5, "HP Smart Array P411" },
329 { 0x103C, 0x3249, CISS_BOARD_SA5, "HP Smart Array P812" },
330 { 0x103C, 0x324A, CISS_BOARD_SA5, "HP Smart Array P712m" },
331 { 0x103C, 0x324B, CISS_BOARD_SA5, "HP Smart Array" },
332 { 0x103C, 0x3350, CISS_BOARD_SA5, "HP Smart Array P222" },
333 { 0x103C, 0x3351, CISS_BOARD_SA5, "HP Smart Array P420" },
334 { 0x103C, 0x3352, CISS_BOARD_SA5, "HP Smart Array P421" },
335 { 0x103C, 0x3353, CISS_BOARD_SA5, "HP Smart Array P822" },
336 { 0x103C, 0x3354, CISS_BOARD_SA5, "HP Smart Array P420i" },
337 { 0x103C, 0x3355, CISS_BOARD_SA5, "HP Smart Array P220i" },
338 { 0x103C, 0x3356, CISS_BOARD_SA5, "HP Smart Array P721m" },
342 /************************************************************************
343 * Find a match for the device in our list of known adapters.
346 ciss_lookup(device_t dev)
350 for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
351 if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
352 (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
358 /************************************************************************
359 * Match a known CISS adapter.
362 ciss_probe(device_t dev)
366 i = ciss_lookup(dev);
368 device_set_desc(dev, ciss_vendor_data[i].desc);
369 return(BUS_PROBE_DEFAULT);
374 /************************************************************************
375 * Attach the driver to this adapter.
378 ciss_attach(device_t dev)
380 struct ciss_softc *sc;
386 /* print structure/union sizes */
387 debug_struct(ciss_command);
388 debug_struct(ciss_header);
389 debug_union(ciss_device_address);
390 debug_struct(ciss_cdb);
391 debug_struct(ciss_report_cdb);
392 debug_struct(ciss_notify_cdb);
393 debug_struct(ciss_notify);
394 debug_struct(ciss_message_cdb);
395 debug_struct(ciss_error_info_pointer);
396 debug_struct(ciss_error_info);
397 debug_struct(ciss_sg_entry);
398 debug_struct(ciss_config_table);
399 debug_struct(ciss_bmic_cdb);
400 debug_struct(ciss_bmic_id_ldrive);
401 debug_struct(ciss_bmic_id_lstatus);
402 debug_struct(ciss_bmic_id_table);
403 debug_struct(ciss_bmic_id_pdrive);
404 debug_struct(ciss_bmic_blink_pdrive);
405 debug_struct(ciss_bmic_flush_cache);
406 debug_const(CISS_MAX_REQUESTS);
407 debug_const(CISS_MAX_LOGICAL);
408 debug_const(CISS_INTERRUPT_COALESCE_DELAY);
409 debug_const(CISS_INTERRUPT_COALESCE_COUNT);
410 debug_const(CISS_COMMAND_ALLOC_SIZE);
411 debug_const(CISS_COMMAND_SG_LENGTH);
413 debug_type(cciss_pci_info_struct);
414 debug_type(cciss_coalint_struct);
415 debug_type(cciss_coalint_struct);
416 debug_type(NodeName_type);
417 debug_type(NodeName_type);
418 debug_type(Heartbeat_type);
419 debug_type(BusTypes_type);
420 debug_type(FirmwareVer_type);
421 debug_type(DriverVer_type);
422 debug_type(IOCTL_Command_struct);
425 sc = device_get_softc(dev);
427 lockinit(&sc->ciss_lock, "cissmtx", 0, LK_CANRECURSE);
428 callout_init(&sc->ciss_periodic);
431 * Do PCI-specific init.
433 if ((error = ciss_init_pci(sc)) != 0)
437 * Initialise driver queues.
440 ciss_initq_notify(sc);
443 * Initalize device sysctls.
445 ciss_init_sysctl(sc);
448 * Initialise command/request pool.
450 if ((error = ciss_init_requests(sc)) != 0)
454 * Get adapter information.
456 if ((error = ciss_identify_adapter(sc)) != 0)
460 * Find all the physical devices.
462 if ((error = ciss_init_physical(sc)) != 0)
466 * Build our private table of logical devices.
468 if ((error = ciss_init_logical(sc)) != 0)
472 * Enable interrupts so that the CAM scan can complete.
474 CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
477 * Initialise the CAM interface.
479 if ((error = ciss_cam_init(sc)) != 0)
483 * Start the heartbeat routine and event chain.
488 * Create the control device.
490 sc->ciss_dev_t = make_dev(&ciss_ops, device_get_unit(sc->ciss_dev),
491 UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
492 "ciss%d", device_get_unit(sc->ciss_dev));
493 sc->ciss_dev_t->si_drv1 = sc;
496 * The adapter is running; synchronous commands can now sleep
497 * waiting for an interrupt to signal completion.
499 sc->ciss_flags |= CISS_FLAG_RUNNING;
501 ciss_spawn_notify_thread(sc);
506 /* ciss_free() expects the mutex to be held */
507 lockmgr(&sc->ciss_lock, LK_EXCLUSIVE);
513 /************************************************************************
514 * Detach the driver from this adapter.
517 ciss_detach(device_t dev)
519 struct ciss_softc *sc = device_get_softc(dev);
523 lockmgr(&sc->ciss_lock, LK_EXCLUSIVE);
524 if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
525 lockmgr(&sc->ciss_lock, LK_RELEASE);
529 /* flush adapter cache */
530 ciss_flush_adapter(sc);
532 /* release all resources. The mutex is released and freed here too. */
538 /************************************************************************
539 * Prepare adapter for system shutdown.
542 ciss_shutdown(device_t dev)
544 struct ciss_softc *sc = device_get_softc(dev);
548 lockmgr(&sc->ciss_lock, LK_EXCLUSIVE);
549 /* flush adapter cache */
550 ciss_flush_adapter(sc);
552 if (sc->ciss_soft_reset)
554 lockmgr(&sc->ciss_lock, LK_RELEASE);
560 ciss_init_sysctl(struct ciss_softc *sc)
562 sysctl_ctx_init(&sc->ciss_sysctl_ctx);
563 sc->ciss_sysctl_tree = SYSCTL_ADD_NODE(&sc->ciss_sysctl_ctx,
564 SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO,
565 device_get_nameunit(sc->ciss_dev), CTLFLAG_RD, 0, "");
566 SYSCTL_ADD_INT(&sc->ciss_sysctl_ctx,
567 SYSCTL_CHILDREN(sc->ciss_sysctl_tree),
568 OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
571 /************************************************************************
572 * Perform PCI-specific attachment actions.
575 ciss_init_pci(struct ciss_softc *sc)
577 uintptr_t cbase, csize, cofs;
578 uint32_t method, supported_methods;
579 int error, sqmask, i;
587 * Work out adapter type.
589 i = ciss_lookup(sc->ciss_dev);
591 ciss_printf(sc, "unknown adapter type\n");
595 if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
596 sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
597 } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
598 sqmask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
601 * XXX Big hammer, masks/unmasks all possible interrupts. This should
602 * work on all hardware variants. Need to add code to handle the
603 * "controller crashed" interupt bit that this unmasks.
609 * Allocate register window first (we need this to find the config
613 sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
614 if ((sc->ciss_regs_resource =
615 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
616 &sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
617 ciss_printf(sc, "can't allocate register window\n");
620 sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
621 sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
624 * Find the BAR holding the config structure. If it's not the one
625 * we already mapped for registers, map it too.
627 sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
628 if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
629 if ((sc->ciss_cfg_resource =
630 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
631 &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
632 ciss_printf(sc, "can't allocate config window\n");
635 cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
636 csize = rman_get_end(sc->ciss_cfg_resource) -
637 rman_get_start(sc->ciss_cfg_resource) + 1;
639 cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
640 csize = rman_get_end(sc->ciss_regs_resource) -
641 rman_get_start(sc->ciss_regs_resource) + 1;
643 cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
646 * Use the base/size/offset values we just calculated to
647 * sanity-check the config structure. If it's OK, point to it.
649 if ((cofs + sizeof(struct ciss_config_table)) > csize) {
650 ciss_printf(sc, "config table outside window\n");
653 sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
654 debug(1, "config struct at %p", sc->ciss_cfg);
657 * Calculate the number of request structures/commands we are
658 * going to provide for this adapter.
660 sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
663 * Validate the config structure. If we supported other transport
664 * methods, we could select amongst them at this point in time.
666 if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
667 ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
668 sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
669 sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
674 * Select the mode of operation, prefer Performant.
676 if (!(sc->ciss_cfg->supported_methods &
677 (CISS_TRANSPORT_METHOD_SIMPLE | CISS_TRANSPORT_METHOD_PERF))) {
678 ciss_printf(sc, "No supported transport layers: 0x%x\n",
679 sc->ciss_cfg->supported_methods);
682 switch (ciss_force_transport) {
684 supported_methods = CISS_TRANSPORT_METHOD_SIMPLE;
687 supported_methods = CISS_TRANSPORT_METHOD_PERF;
690 supported_methods = sc->ciss_cfg->supported_methods;
695 if ((supported_methods & CISS_TRANSPORT_METHOD_PERF) != 0) {
696 method = CISS_TRANSPORT_METHOD_PERF;
697 sc->ciss_perf = (struct ciss_perf_config *)(cbase + cofs +
698 sc->ciss_cfg->transport_offset);
699 if (ciss_init_perf(sc)) {
700 supported_methods &= ~method;
703 } else if (supported_methods & CISS_TRANSPORT_METHOD_SIMPLE) {
704 method = CISS_TRANSPORT_METHOD_SIMPLE;
706 ciss_printf(sc, "No supported transport methods: 0x%x\n",
707 sc->ciss_cfg->supported_methods);
712 * Tell it we're using the low 4GB of RAM. Set the default interrupt
713 * coalescing options.
715 sc->ciss_cfg->requested_method = method;
716 sc->ciss_cfg->command_physlimit = 0;
717 sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
718 sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
721 sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
724 if (ciss_update_config(sc)) {
725 ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
726 CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
729 if ((sc->ciss_cfg->active_method & method) == 0) {
730 supported_methods &= ~method;
731 if (supported_methods == 0) {
732 ciss_printf(sc, "adapter refuses to go into available transports "
733 "mode (0x%x, 0x%x)\n", supported_methods,
734 sc->ciss_cfg->active_method);
741 * Wait for the adapter to come ready.
743 if ((error = ciss_wait_adapter(sc)) != 0)
746 /* Prepare to possibly use MSIX and/or PERFORMANT interrupts. Normal
747 * interrupts have a rid of 0, this will be overridden if MSIX is used.
749 sc->ciss_irq_rid[0] = 0;
750 if (method == CISS_TRANSPORT_METHOD_PERF) {
751 ciss_printf(sc, "PERFORMANT Transport\n");
752 if ((ciss_force_interrupt != 1) && (ciss_setup_msix(sc) == 0)) {
753 intr = ciss_perf_msi_intr;
755 intr = ciss_perf_intr;
757 /* XXX The docs say that the 0x01 bit is only for SAS controllers.
758 * Unfortunately, there is no good way to know if this is a SAS
759 * controller. Hopefully enabling this bit universally will work OK.
760 * It seems to work fine for SA6i controllers.
762 sc->ciss_interrupt_mask = CISS_TL_PERF_INTR_OPQ | CISS_TL_PERF_INTR_MSI;
765 ciss_printf(sc, "SIMPLE Transport\n");
766 /* MSIX doesn't seem to work in SIMPLE mode, only enable if it forced */
767 if (ciss_force_interrupt == 2)
768 /* If this fails, we automatically revert to INTx */
770 sc->ciss_perf = NULL;
772 sc->ciss_interrupt_mask = sqmask;
775 * Turn off interrupts before we go routing anything.
777 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
780 * Allocate and set up our interrupt.
782 #ifdef __DragonFly__ /* DragonFly specific MSI setup */
783 use_msi = (intr == ciss_perf_msi_intr);
785 sc->ciss_irq_rid[0] = 0;
786 sc->ciss_irq_type = pci_alloc_1intr(sc->ciss_dev, use_msi,
787 &sc->ciss_irq_rid[0], &irq_flags);
788 if ((sc->ciss_irq_resource =
789 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid[0],
790 irq_flags)) == NULL) {
791 ciss_printf(sc, "can't allocate interrupt\n");
795 if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
796 INTR_MPSAFE, intr, sc,
797 &sc->ciss_intr, NULL)) {
798 ciss_printf(sc, "can't set up interrupt\n");
803 * Allocate the parent bus DMA tag appropriate for our PCI
806 * Note that "simple" adapters can only address within a 32-bit
809 if (bus_dma_tag_create(NULL, /* PCI parent */
810 1, 0, /* alignment, boundary */
811 BUS_SPACE_MAXADDR, /* lowaddr */
812 BUS_SPACE_MAXADDR, /* highaddr */
813 NULL, NULL, /* filter, filterarg */
814 BUS_SPACE_MAXSIZE_32BIT, /* maxsize */
815 CISS_MAX_SG_ELEMENTS, /* nsegments */
816 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
818 &sc->ciss_parent_dmat)) {
819 ciss_printf(sc, "can't allocate parent DMA tag\n");
824 * Create DMA tag for mapping buffers into adapter-addressable
827 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
828 1, 0, /* alignment, boundary */
829 BUS_SPACE_MAXADDR, /* lowaddr */
830 BUS_SPACE_MAXADDR, /* highaddr */
831 NULL, NULL, /* filter, filterarg */
832 MAXBSIZE, CISS_MAX_SG_ELEMENTS, /* maxsize, nsegments */
833 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
834 BUS_DMA_ALLOCNOW, /* flags */
835 &sc->ciss_buffer_dmat)) {
836 ciss_printf(sc, "can't allocate buffer DMA tag\n");
842 /************************************************************************
843 * Setup MSI/MSIX operation (Performant only)
844 * Four interrupts are available, but we only use 1 right now. If MSI-X
845 * isn't avaialble, try using MSI instead.
848 ciss_setup_msix(struct ciss_softc *sc)
852 /* Weed out devices that don't actually support MSI */
853 i = ciss_lookup(sc->ciss_dev);
854 if (ciss_vendor_data[i].flags & CISS_BOARD_NOMSI)
857 #if 0 /* XXX swildner */
859 * Only need to use the minimum number of MSI vectors, as the driver
860 * doesn't support directed MSIX interrupts.
862 val = pci_msix_count(sc->ciss_dev);
863 if (val < CISS_MSI_COUNT) {
864 val = pci_msi_count(sc->ciss_dev);
865 device_printf(sc->ciss_dev, "got %d MSI messages]\n", val);
866 if (val < CISS_MSI_COUNT)
869 val = MIN(val, CISS_MSI_COUNT);
870 if (pci_alloc_msix(sc->ciss_dev, &val) != 0) {
871 if (pci_alloc_msi(sc->ciss_dev, &val) != 0)
879 ciss_printf(sc, "Using %d MSIX interrupt%s\n", val,
880 (val != 1) ? "s" : "");
882 for (i = 0; i < val; i++)
883 sc->ciss_irq_rid[i] = i + 1;
889 /************************************************************************
890 * Setup the Performant structures.
893 ciss_init_perf(struct ciss_softc *sc)
895 struct ciss_perf_config *pc = sc->ciss_perf;
899 * Create the DMA tag for the reply queue.
901 reply_size = sizeof(uint64_t) * sc->ciss_max_requests;
902 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
903 1, 0, /* alignment, boundary */
904 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
905 BUS_SPACE_MAXADDR, /* highaddr */
906 NULL, NULL, /* filter, filterarg */
907 reply_size, 1, /* maxsize, nsegments */
908 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
910 &sc->ciss_reply_dmat)) {
911 ciss_printf(sc, "can't allocate reply DMA tag\n");
915 * Allocate memory and make it available for DMA.
917 if (bus_dmamem_alloc(sc->ciss_reply_dmat, (void **)&sc->ciss_reply,
918 BUS_DMA_NOWAIT, &sc->ciss_reply_map)) {
919 ciss_printf(sc, "can't allocate reply memory\n");
922 bus_dmamap_load(sc->ciss_reply_dmat, sc->ciss_reply_map, sc->ciss_reply,
923 reply_size, ciss_command_map_helper, &sc->ciss_reply_phys, 0);
924 bzero(sc->ciss_reply, reply_size);
926 sc->ciss_cycle = 0x1;
930 * Preload the fetch table with common command sizes. This allows the
931 * hardware to not waste bus cycles for typical i/o commands, but also not
932 * tax the driver to be too exact in choosing sizes. The table is optimized
933 * for page-aligned i/o's, but since most i/o comes from the various pagers,
934 * it's a reasonable assumption to make.
936 pc->fetch_count[CISS_SG_FETCH_NONE] = (sizeof(struct ciss_command) + 15) / 16;
937 pc->fetch_count[CISS_SG_FETCH_1] =
938 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 1 + 15) / 16;
939 pc->fetch_count[CISS_SG_FETCH_2] =
940 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 2 + 15) / 16;
941 pc->fetch_count[CISS_SG_FETCH_4] =
942 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 4 + 15) / 16;
943 pc->fetch_count[CISS_SG_FETCH_8] =
944 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 8 + 15) / 16;
945 pc->fetch_count[CISS_SG_FETCH_16] =
946 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 16 + 15) / 16;
947 pc->fetch_count[CISS_SG_FETCH_32] =
948 (sizeof(struct ciss_command) + sizeof(struct ciss_sg_entry) * 32 + 15) / 16;
949 pc->fetch_count[CISS_SG_FETCH_MAX] = (CISS_COMMAND_ALLOC_SIZE + 15) / 16;
951 pc->rq_size = sc->ciss_max_requests; /* XXX less than the card supports? */
952 pc->rq_count = 1; /* XXX Hardcode for a single queue */
955 pc->rq[0].rq_addr_hi = 0x0;
956 pc->rq[0].rq_addr_lo = sc->ciss_reply_phys;
961 /************************************************************************
962 * Wait for the adapter to come ready.
965 ciss_wait_adapter(struct ciss_softc *sc)
972 * Wait for the adapter to come ready.
974 if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
975 ciss_printf(sc, "waiting for adapter to come ready...\n");
976 for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
977 DELAY(1000000); /* one second */
979 ciss_printf(sc, "timed out waiting for adapter to come ready\n");
987 /************************************************************************
988 * Flush the adapter cache.
991 ciss_flush_adapter(struct ciss_softc *sc)
993 struct ciss_request *cr;
994 struct ciss_bmic_flush_cache *cbfc;
995 int error, command_status;
1003 * Build a BMIC request to flush the cache. We don't disable
1004 * it, as we may be going to do more I/O (eg. we are emulating
1005 * the Synchronise Cache command).
1007 cbfc = kmalloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_INTWAIT | M_ZERO);
1008 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
1009 (void **)&cbfc, sizeof(*cbfc))) != 0)
1013 * Submit the request and wait for it to complete.
1015 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1016 ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
1023 ciss_report_request(cr, &command_status, NULL);
1024 switch(command_status) {
1025 case CISS_CMD_STATUS_SUCCESS:
1028 ciss_printf(sc, "error flushing cache (%s)\n",
1029 ciss_name_command_status(command_status));
1036 kfree(cbfc, CISS_MALLOC_CLASS);
1038 ciss_release_request(cr);
1043 ciss_soft_reset(struct ciss_softc *sc)
1045 struct ciss_request *cr = NULL;
1046 struct ciss_command *cc;
1049 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1050 /* only reset proxy controllers */
1051 if (sc->ciss_controllers[i].physical.bus == 0)
1054 if ((error = ciss_get_request(sc, &cr)) != 0)
1057 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
1062 cc->header.address = sc->ciss_controllers[i];
1064 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
1067 ciss_release_request(cr);
1071 ciss_printf(sc, "error resetting controller (%d)\n", error);
1074 ciss_release_request(cr);
1077 /************************************************************************
1078 * Allocate memory for the adapter command structures, initialise
1079 * the request structures.
1081 * Note that the entire set of commands are allocated in a single
1085 ciss_init_requests(struct ciss_softc *sc)
1087 struct ciss_request *cr;
1093 ciss_printf(sc, "using %d of %d available commands\n",
1094 sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
1097 * Create the DMA tag for commands.
1099 if (bus_dma_tag_create(sc->ciss_parent_dmat, /* parent */
1100 32, 0, /* alignment, boundary */
1101 BUS_SPACE_MAXADDR_32BIT, /* lowaddr */
1102 BUS_SPACE_MAXADDR, /* highaddr */
1103 NULL, NULL, /* filter, filterarg */
1104 CISS_COMMAND_ALLOC_SIZE *
1105 sc->ciss_max_requests, 1, /* maxsize, nsegments */
1106 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
1108 &sc->ciss_command_dmat)) {
1109 ciss_printf(sc, "can't allocate command DMA tag\n");
1113 * Allocate memory and make it available for DMA.
1115 if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
1116 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
1117 ciss_printf(sc, "can't allocate command memory\n");
1120 bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map,sc->ciss_command,
1121 CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
1122 ciss_command_map_helper, &sc->ciss_command_phys, 0);
1123 bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
1126 * Set up the request and command structures, push requests onto
1129 for (i = 1; i < sc->ciss_max_requests; i++) {
1130 cr = &sc->ciss_request[i];
1133 cr->cr_cc = (struct ciss_command *)((uintptr_t)sc->ciss_command +
1134 CISS_COMMAND_ALLOC_SIZE * i);
1135 cr->cr_ccphys = sc->ciss_command_phys + CISS_COMMAND_ALLOC_SIZE * i;
1136 bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
1137 ciss_enqueue_free(cr);
1143 ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
1148 *addr = segs[0].ds_addr;
1151 /************************************************************************
1152 * Identify the adapter, print some information about it.
1155 ciss_identify_adapter(struct ciss_softc *sc)
1157 struct ciss_request *cr;
1158 int error, command_status;
1165 * Get a request, allocate storage for the adapter data.
1167 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
1168 (void **)&sc->ciss_id,
1169 sizeof(*sc->ciss_id))) != 0)
1173 * Submit the request and wait for it to complete.
1175 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1176 ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
1183 ciss_report_request(cr, &command_status, NULL);
1184 switch(command_status) {
1185 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1187 case CISS_CMD_STATUS_DATA_UNDERRUN:
1188 case CISS_CMD_STATUS_DATA_OVERRUN:
1189 ciss_printf(sc, "data over/underrun reading adapter information\n");
1191 ciss_printf(sc, "error reading adapter information (%s)\n",
1192 ciss_name_command_status(command_status));
1197 /* sanity-check reply */
1198 if (!sc->ciss_id->big_map_supported) {
1199 ciss_printf(sc, "adapter does not support BIG_MAP\n");
1205 /* XXX later revisions may not need this */
1206 sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
1209 /* XXX only really required for old 5300 adapters? */
1210 sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
1212 /* print information */
1214 #if 0 /* XXX proxy volumes??? */
1215 ciss_printf(sc, " %d logical drive%s configured\n",
1216 sc->ciss_id->configured_logical_drives,
1217 (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
1219 ciss_printf(sc, " firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
1220 ciss_printf(sc, " %d SCSI channels\n", sc->ciss_id->scsi_bus_count);
1222 ciss_printf(sc, " signature '%.4s'\n", sc->ciss_cfg->signature);
1223 ciss_printf(sc, " valence %d\n", sc->ciss_cfg->valence);
1224 ciss_printf(sc, " supported I/O methods 0x%b\n",
1225 sc->ciss_cfg->supported_methods,
1226 "\20\1READY\2simple\3performant\4MEMQ\n");
1227 ciss_printf(sc, " active I/O method 0x%b\n",
1228 sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
1229 ciss_printf(sc, " 4G page base 0x%08x\n",
1230 sc->ciss_cfg->command_physlimit);
1231 ciss_printf(sc, " interrupt coalesce delay %dus\n",
1232 sc->ciss_cfg->interrupt_coalesce_delay);
1233 ciss_printf(sc, " interrupt coalesce count %d\n",
1234 sc->ciss_cfg->interrupt_coalesce_count);
1235 ciss_printf(sc, " max outstanding commands %d\n",
1236 sc->ciss_cfg->max_outstanding_commands);
1237 ciss_printf(sc, " bus types 0x%b\n", sc->ciss_cfg->bus_types,
1238 "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
1239 ciss_printf(sc, " server name '%.16s'\n", sc->ciss_cfg->server_name);
1240 ciss_printf(sc, " heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
1245 if (sc->ciss_id != NULL) {
1246 kfree(sc->ciss_id, CISS_MALLOC_CLASS);
1251 ciss_release_request(cr);
1255 /************************************************************************
1256 * Helper routine for generating a list of logical and physical luns.
1258 static struct ciss_lun_report *
1259 ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
1261 struct ciss_request *cr;
1262 struct ciss_command *cc;
1263 struct ciss_report_cdb *crc;
1264 struct ciss_lun_report *cll;
1275 * Get a request, allocate storage for the address list.
1277 if ((error = ciss_get_request(sc, &cr)) != 0)
1279 report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
1280 cll = kmalloc(report_size, CISS_MALLOC_CLASS, M_INTWAIT | M_ZERO);
1283 * Build the Report Logical/Physical LUNs command.
1287 cr->cr_length = report_size;
1288 cr->cr_flags = CISS_REQ_DATAIN;
1290 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
1291 cc->header.address.physical.bus = 0;
1292 cc->header.address.physical.target = 0;
1293 cc->cdb.cdb_length = sizeof(*crc);
1294 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1295 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1296 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1297 cc->cdb.timeout = 30; /* XXX better suggestions? */
1299 crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
1300 bzero(crc, sizeof(*crc));
1301 crc->opcode = opcode;
1302 crc->length = htonl(report_size); /* big-endian field */
1303 cll->list_size = htonl(report_size - sizeof(*cll)); /* big-endian field */
1306 * Submit the request and wait for it to complete. (timeout
1307 * here should be much greater than above)
1309 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1310 ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
1315 * Check response. Note that data over/underrun is OK.
1317 ciss_report_request(cr, &command_status, NULL);
1318 switch(command_status) {
1319 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1320 case CISS_CMD_STATUS_DATA_UNDERRUN: /* buffer too large, not bad */
1322 case CISS_CMD_STATUS_DATA_OVERRUN:
1323 ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
1327 ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
1328 ciss_name_command_status(command_status));
1332 ciss_release_request(cr);
1337 ciss_release_request(cr);
1338 if (error && cll != NULL) {
1339 kfree(cll, CISS_MALLOC_CLASS);
1345 /************************************************************************
1346 * Find logical drives on the adapter.
1349 ciss_init_logical(struct ciss_softc *sc)
1351 struct ciss_lun_report *cll;
1352 int error = 0, i, j;
1357 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
1364 /* sanity-check reply */
1365 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1366 if ((ndrives < 0) || (ndrives > CISS_MAX_LOGICAL)) {
1367 ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
1368 ndrives, CISS_MAX_LOGICAL);
1374 * Save logical drive information.
1377 ciss_printf(sc, "%d logical drive%s\n",
1378 ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
1382 kmalloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
1383 CISS_MALLOC_CLASS, M_INTWAIT | M_ZERO);
1385 for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1386 sc->ciss_logical[i] =
1387 kmalloc(CISS_MAX_LOGICAL * sizeof(struct ciss_ldrive),
1388 CISS_MALLOC_CLASS, M_INTWAIT | M_ZERO);
1390 for (j = 0; j < CISS_MAX_LOGICAL; j++)
1391 sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
1395 for (i = 0; i < CISS_MAX_LOGICAL; i++) {
1397 struct ciss_ldrive *ld;
1400 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
1401 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
1402 ld = &sc->ciss_logical[bus][target];
1404 ld->cl_address = cll->lun[i];
1405 ld->cl_controller = &sc->ciss_controllers[bus];
1406 if (ciss_identify_logical(sc, ld) != 0)
1409 * If the drive has had media exchanged, we should bring it online.
1411 if (ld->cl_lstatus->media_exchanged)
1412 ciss_accept_media(sc, ld);
1419 kfree(cll, CISS_MALLOC_CLASS);
1424 ciss_init_physical(struct ciss_softc *sc)
1426 struct ciss_lun_report *cll;
1435 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
1442 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1445 ciss_printf(sc, "%d physical device%s\n",
1446 nphys, (nphys > 1 || nphys == 0) ? "s" : "");
1450 * Figure out the bus mapping.
1451 * Logical buses include both the local logical bus for local arrays and
1452 * proxy buses for remote arrays. Physical buses are numbered by the
1453 * controller and represent physical buses that hold physical devices.
1454 * We shift these bus numbers so that everything fits into a single flat
1455 * numbering space for CAM. Logical buses occupy the first 32 CAM bus
1456 * numbers, and the physical bus numbers are shifted to be above that.
1457 * This results in the various driver arrays being indexed as follows:
1459 * ciss_controllers[] - indexed by logical bus
1460 * ciss_cam_sim[] - indexed by both logical and physical, with physical
1461 * being shifted by 32.
1462 * ciss_logical[][] - indexed by logical bus
1463 * ciss_physical[][] - indexed by physical bus
1465 * XXX This is getting more and more hackish. CISS really doesn't play
1466 * well with a standard SCSI model; devices are addressed via magic
1467 * cookies, not via b/t/l addresses. Since there is no way to store
1468 * the cookie in the CAM device object, we have to keep these lookup
1469 * tables handy so that the devices can be found quickly at the cost
1470 * of wasting memory and having a convoluted lookup scheme. This
1471 * driver should probably be converted to block interface.
1474 * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
1475 * controller. A proxy controller is another physical controller
1476 * behind the primary PCI controller. We need to know about this
1477 * so that BMIC commands can be properly targeted. There can be
1478 * proxy controllers attached to a single PCI controller, so
1479 * find the highest numbered one so the array can be properly
1482 sc->ciss_max_logical_bus = 1;
1483 for (i = 0; i < nphys; i++) {
1484 if (cll->lun[i].physical.extra_address == 0) {
1485 bus = cll->lun[i].physical.bus;
1486 sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
1488 bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
1489 sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
1493 sc->ciss_controllers =
1494 kmalloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
1495 CISS_MALLOC_CLASS, M_INTWAIT | M_ZERO);
1497 /* setup a map of controller addresses */
1498 for (i = 0; i < nphys; i++) {
1499 if (cll->lun[i].physical.extra_address == 0) {
1500 sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
1505 kmalloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
1506 CISS_MALLOC_CLASS, M_INTWAIT | M_ZERO);
1508 for (i = 0; i < sc->ciss_max_physical_bus; i++) {
1509 sc->ciss_physical[i] =
1510 kmalloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
1511 CISS_MALLOC_CLASS, M_INTWAIT | M_ZERO);
1514 ciss_filter_physical(sc, cll);
1518 kfree(cll, CISS_MALLOC_CLASS);
1524 ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
1530 nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1531 for (i = 0; i < nphys; i++) {
1532 if (cll->lun[i].physical.extra_address == 0)
1536 * Filter out devices that we don't want. Level 3 LUNs could
1537 * probably be supported, but the docs don't give enough of a
1540 * The mode field of the physical address is likely set to have
1541 * hard disks masked out. Honor it unless the user has overridden
1542 * us with the tunable. We also munge the inquiry data for these
1543 * disks so that they only show up as passthrough devices. Keeping
1544 * them visible in this fashion is useful for doing things like
1545 * flashing firmware.
1547 ea = cll->lun[i].physical.extra_address;
1548 if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
1549 (CISS_EXTRA_MODE2(ea) == 0x3))
1551 if ((ciss_expose_hidden_physical == 0) &&
1552 (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
1556 * Note: CISS firmware numbers physical busses starting at '1', not
1557 * '0'. This numbering is internal to the firmware and is only
1558 * used as a hint here.
1560 bus = CISS_EXTRA_BUS2(ea) - 1;
1561 target = CISS_EXTRA_TARGET2(ea);
1562 sc->ciss_physical[bus][target].cp_address = cll->lun[i];
1563 sc->ciss_physical[bus][target].cp_online = 1;
1570 ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1572 struct ciss_request *cr;
1573 struct ciss_command *cc;
1574 struct scsi_inquiry *inq;
1580 bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1582 if ((error = ciss_get_request(sc, &cr)) != 0)
1586 cr->cr_data = &ld->cl_geometry;
1587 cr->cr_length = sizeof(ld->cl_geometry);
1588 cr->cr_flags = CISS_REQ_DATAIN;
1590 cc->header.address = ld->cl_address;
1591 cc->cdb.cdb_length = 6;
1592 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1593 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1594 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1595 cc->cdb.timeout = 30;
1597 inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1598 inq->opcode = INQUIRY;
1599 inq->byte2 = SI_EVPD;
1600 inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1601 inq->length = sizeof(ld->cl_geometry);
1603 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1604 ciss_printf(sc, "error getting geometry (%d)\n", error);
1608 ciss_report_request(cr, &command_status, NULL);
1609 switch(command_status) {
1610 case CISS_CMD_STATUS_SUCCESS:
1611 case CISS_CMD_STATUS_DATA_UNDERRUN:
1613 case CISS_CMD_STATUS_DATA_OVERRUN:
1614 ciss_printf(sc, "WARNING: Data overrun\n");
1617 ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1618 ciss_name_command_status(command_status));
1624 ciss_release_request(cr);
1627 /************************************************************************
1628 * Identify a logical drive, initialise state related to it.
1631 ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1633 struct ciss_request *cr;
1634 struct ciss_command *cc;
1635 struct ciss_bmic_cdb *cbc;
1636 int error, command_status;
1643 * Build a BMIC request to fetch the drive ID.
1645 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
1646 (void **)&ld->cl_ldrive,
1647 sizeof(*ld->cl_ldrive))) != 0)
1650 cc->header.address = *ld->cl_controller; /* target controller */
1651 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1652 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1655 * Submit the request and wait for it to complete.
1657 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1658 ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
1665 ciss_report_request(cr, &command_status, NULL);
1666 switch(command_status) {
1667 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1669 case CISS_CMD_STATUS_DATA_UNDERRUN:
1670 case CISS_CMD_STATUS_DATA_OVERRUN:
1671 ciss_printf(sc, "data over/underrun reading logical drive ID\n");
1673 ciss_printf(sc, "error reading logical drive ID (%s)\n",
1674 ciss_name_command_status(command_status));
1678 ciss_release_request(cr);
1682 * Build a CISS BMIC command to get the logical drive status.
1684 if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
1688 * Get the logical drive geometry.
1690 if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1694 * Print the drive's basic characteristics.
1697 ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
1698 CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
1699 CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
1700 ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
1701 ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
1702 ld->cl_ldrive->block_size));
1704 ciss_print_ldrive(sc, ld);
1708 /* make the drive not-exist */
1709 ld->cl_status = CISS_LD_NONEXISTENT;
1710 if (ld->cl_ldrive != NULL) {
1711 kfree(ld->cl_ldrive, CISS_MALLOC_CLASS);
1712 ld->cl_ldrive = NULL;
1714 if (ld->cl_lstatus != NULL) {
1715 kfree(ld->cl_lstatus, CISS_MALLOC_CLASS);
1716 ld->cl_lstatus = NULL;
1720 ciss_release_request(cr);
1725 /************************************************************************
1726 * Get status for a logical drive.
1728 * XXX should we also do this in response to Test Unit Ready?
1731 ciss_get_ldrive_status(struct ciss_softc *sc, struct ciss_ldrive *ld)
1733 struct ciss_request *cr;
1734 struct ciss_command *cc;
1735 struct ciss_bmic_cdb *cbc;
1736 int error, command_status;
1739 * Build a CISS BMIC command to get the logical drive status.
1741 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
1742 (void **)&ld->cl_lstatus,
1743 sizeof(*ld->cl_lstatus))) != 0)
1746 cc->header.address = *ld->cl_controller; /* target controller */
1747 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1748 cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1751 * Submit the request and wait for it to complete.
1753 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1754 ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1761 ciss_report_request(cr, &command_status, NULL);
1762 switch(command_status) {
1763 case CISS_CMD_STATUS_SUCCESS: /* buffer right size */
1765 case CISS_CMD_STATUS_DATA_UNDERRUN:
1766 case CISS_CMD_STATUS_DATA_OVERRUN:
1767 ciss_printf(sc, "data over/underrun reading logical drive status\n");
1769 ciss_printf(sc, "error reading logical drive status (%s)\n",
1770 ciss_name_command_status(command_status));
1776 * Set the drive's summary status based on the returned status.
1778 * XXX testing shows that a failed JBOD drive comes back at next
1779 * boot in "queued for expansion" mode. WTF?
1781 ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
1785 ciss_release_request(cr);
1789 /************************************************************************
1790 * Notify the adapter of a config update.
1793 ciss_update_config(struct ciss_softc *sc)
1799 CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
1800 for (i = 0; i < 1000; i++) {
1801 if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
1802 CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
1810 /************************************************************************
1811 * Accept new media into a logical drive.
1813 * XXX The drive has previously been offline; it would be good if we
1814 * could make sure it's not open right now.
1817 ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
1819 struct ciss_request *cr;
1820 struct ciss_command *cc;
1821 struct ciss_bmic_cdb *cbc;
1823 int error = 0, ldrive;
1825 ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1827 debug(0, "bringing logical drive %d back online", ldrive);
1830 * Build a CISS BMIC command to bring the drive back online.
1832 if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
1836 cc->header.address = *ld->cl_controller; /* target controller */
1837 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1838 cbc->log_drive = ldrive;
1841 * Submit the request and wait for it to complete.
1843 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1844 ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
1851 ciss_report_request(cr, &command_status, NULL);
1852 switch(command_status) {
1853 case CISS_CMD_STATUS_SUCCESS: /* all OK */
1854 /* we should get a logical drive status changed event here */
1857 ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
1858 ciss_name_command_status(command_status));
1864 ciss_release_request(cr);
1868 /************************************************************************
1869 * Release adapter resources.
1872 ciss_free(struct ciss_softc *sc)
1874 struct ciss_request *cr;
1879 /* we're going away */
1880 sc->ciss_flags |= CISS_FLAG_ABORTING;
1882 /* terminate the periodic heartbeat routine */
1883 callout_stop(&sc->ciss_periodic);
1885 /* cancel the Event Notify chain */
1886 ciss_notify_abort(sc);
1888 ciss_kill_notify_thread(sc);
1890 /* disconnect from CAM */
1891 if (sc->ciss_cam_sim) {
1892 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1893 if (sc->ciss_cam_sim[i]) {
1894 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1895 cam_sim_free(sc->ciss_cam_sim[i]);
1898 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
1899 CISS_PHYSICAL_BASE; i++) {
1900 if (sc->ciss_cam_sim[i]) {
1901 xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1902 cam_sim_free(sc->ciss_cam_sim[i]);
1905 kfree(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
1907 if (sc->ciss_cam_devq)
1908 cam_simq_release(sc->ciss_cam_devq);
1910 /* remove the control device */
1911 lockmgr(&sc->ciss_lock, LK_RELEASE);
1912 if (sc->ciss_dev_t != NULL)
1913 destroy_dev(sc->ciss_dev_t);
1915 /* Final cleanup of the callout. */
1916 #if 0 /* XXX swildner callout_drain */
1917 callout_drain(&sc->ciss_periodic);
1919 callout_stop(&sc->ciss_periodic);
1921 lockuninit(&sc->ciss_lock);
1923 /* free the controller data */
1924 if (sc->ciss_id != NULL)
1925 kfree(sc->ciss_id, CISS_MALLOC_CLASS);
1927 /* release I/O resources */
1928 if (sc->ciss_regs_resource != NULL)
1929 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1930 sc->ciss_regs_rid, sc->ciss_regs_resource);
1931 if (sc->ciss_cfg_resource != NULL)
1932 bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1933 sc->ciss_cfg_rid, sc->ciss_cfg_resource);
1934 if (sc->ciss_intr != NULL)
1935 bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
1936 if (sc->ciss_irq_resource != NULL)
1937 bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
1938 sc->ciss_irq_rid[0], sc->ciss_irq_resource);
1939 if (sc->ciss_irq_type == PCI_INTR_TYPE_MSI)
1940 pci_release_msi(sc->ciss_dev);
1942 while ((cr = ciss_dequeue_free(sc)) != NULL)
1943 bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
1944 if (sc->ciss_buffer_dmat)
1945 bus_dma_tag_destroy(sc->ciss_buffer_dmat);
1947 /* destroy command memory and DMA tag */
1948 if (sc->ciss_command != NULL) {
1949 bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
1950 bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
1952 if (sc->ciss_command_dmat)
1953 bus_dma_tag_destroy(sc->ciss_command_dmat);
1955 if (sc->ciss_reply) {
1956 bus_dmamap_unload(sc->ciss_reply_dmat, sc->ciss_reply_map);
1957 bus_dmamem_free(sc->ciss_reply_dmat, sc->ciss_reply, sc->ciss_reply_map);
1959 if (sc->ciss_reply_dmat)
1960 bus_dma_tag_destroy(sc->ciss_reply_dmat);
1962 /* destroy DMA tags */
1963 if (sc->ciss_parent_dmat)
1964 bus_dma_tag_destroy(sc->ciss_parent_dmat);
1965 if (sc->ciss_logical) {
1966 for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1967 for (j = 0; j < CISS_MAX_LOGICAL; j++) {
1968 if (sc->ciss_logical[i][j].cl_ldrive)
1969 kfree(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
1970 if (sc->ciss_logical[i][j].cl_lstatus)
1971 kfree(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
1973 kfree(sc->ciss_logical[i], CISS_MALLOC_CLASS);
1975 kfree(sc->ciss_logical, CISS_MALLOC_CLASS);
1978 if (sc->ciss_physical) {
1979 for (i = 0; i < sc->ciss_max_physical_bus; i++)
1980 kfree(sc->ciss_physical[i], CISS_MALLOC_CLASS);
1981 kfree(sc->ciss_physical, CISS_MALLOC_CLASS);
1984 if (sc->ciss_controllers)
1985 kfree(sc->ciss_controllers, CISS_MALLOC_CLASS);
1987 sysctl_ctx_free(&sc->ciss_sysctl_ctx);
1990 /************************************************************************
1991 * Give a command to the adapter.
1993 * Note that this uses the simple transport layer directly. If we
1994 * want to add support for other layers, we'll need a switch of some
1997 * Note that the simple transport layer has no way of refusing a
1998 * command; we only have as many request structures as the adapter
1999 * supports commands, so we don't have to check (this presumes that
2000 * the adapter can handle commands as fast as we throw them at it).
2003 ciss_start(struct ciss_request *cr)
2006 struct ciss_command *cc;
2013 debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
2016 * Map the request's data.
2018 if ((error = ciss_map_request(cr)))
2022 ciss_print_request(cr);
2028 /************************************************************************
2029 * Fetch completed request(s) from the adapter, queue them for
2030 * completion handling.
2032 * Note that this uses the simple transport layer directly. If we
2033 * want to add support for other layers, we'll need a switch of some
2036 * Note that the simple transport mechanism does not require any
2037 * reentrancy protection; the OPQ read is atomic. If there is a
2038 * chance of a race with something else that might move the request
2039 * off the busy list, then we will have to lock against that
2040 * (eg. timeouts, etc.)
2043 ciss_done(struct ciss_softc *sc, cr_qhead_t *qh)
2045 struct ciss_request *cr;
2046 struct ciss_command *cc;
2047 u_int32_t tag, index;
2052 * Loop quickly taking requests from the adapter and moving them
2053 * to the completed queue.
2057 tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
2058 if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
2061 debug(2, "completed command %d%s", index,
2062 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2063 if (index >= sc->ciss_max_requests) {
2064 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2067 cr = &(sc->ciss_request[index]);
2069 cc->header.host_tag = tag; /* not updated by adapter */
2070 ciss_enqueue_complete(cr, qh);
2076 ciss_perf_done(struct ciss_softc *sc, cr_qhead_t *qh)
2078 struct ciss_request *cr;
2079 struct ciss_command *cc;
2080 u_int32_t tag, index;
2085 * Loop quickly taking requests from the adapter and moving them
2086 * to the completed queue.
2089 tag = sc->ciss_reply[sc->ciss_rqidx];
2090 if ((tag & CISS_CYCLE_MASK) != sc->ciss_cycle)
2093 debug(2, "completed command %d%s\n", index,
2094 (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
2095 if (index < sc->ciss_max_requests) {
2096 cr = &(sc->ciss_request[index]);
2098 cc->header.host_tag = tag; /* not updated by adapter */
2099 ciss_enqueue_complete(cr, qh);
2101 ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
2103 if (++sc->ciss_rqidx == sc->ciss_max_requests) {
2105 sc->ciss_cycle ^= 1;
2111 /************************************************************************
2112 * Take an interrupt from the adapter.
2115 ciss_intr(void *arg)
2118 struct ciss_softc *sc = (struct ciss_softc *)arg;
2121 * The only interrupt we recognise indicates that there are
2122 * entries in the outbound post queue.
2126 lockmgr(&sc->ciss_lock, LK_EXCLUSIVE);
2127 ciss_complete(sc, &qh);
2128 lockmgr(&sc->ciss_lock, LK_RELEASE);
2132 ciss_perf_intr(void *arg)
2134 struct ciss_softc *sc = (struct ciss_softc *)arg;
2136 /* Clear the interrupt and flush the bridges. Docs say that the flush
2137 * needs to be done twice, which doesn't seem right.
2139 CISS_TL_PERF_CLEAR_INT(sc);
2140 CISS_TL_PERF_FLUSH_INT(sc);
2142 ciss_perf_msi_intr(sc);
2146 ciss_perf_msi_intr(void *arg)
2149 struct ciss_softc *sc = (struct ciss_softc *)arg;
2152 ciss_perf_done(sc, &qh);
2153 lockmgr(&sc->ciss_lock, LK_EXCLUSIVE);
2154 ciss_complete(sc, &qh);
2155 lockmgr(&sc->ciss_lock, LK_RELEASE);
2159 /************************************************************************
2160 * Process completed requests.
2162 * Requests can be completed in three fashions:
2164 * - by invoking a callback function (cr_complete is non-null)
2165 * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
2166 * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
2169 ciss_complete(struct ciss_softc *sc, cr_qhead_t *qh)
2171 struct ciss_request *cr;
2176 * Loop taking requests off the completed queue and performing
2177 * completion processing on them.
2180 if ((cr = ciss_dequeue_complete(sc, qh)) == NULL)
2182 ciss_unmap_request(cr);
2184 if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
2185 ciss_printf(sc, "WARNING: completing non-busy request\n");
2186 cr->cr_flags &= ~CISS_REQ_BUSY;
2189 * If the request has a callback, invoke it.
2191 if (cr->cr_complete != NULL) {
2192 cr->cr_complete(cr);
2197 * If someone is sleeping on this request, wake them up.
2199 if (cr->cr_flags & CISS_REQ_SLEEP) {
2200 cr->cr_flags &= ~CISS_REQ_SLEEP;
2206 * If someone is polling this request for completion, signal.
2208 if (cr->cr_flags & CISS_REQ_POLL) {
2209 cr->cr_flags &= ~CISS_REQ_POLL;
2214 * Give up and throw the request back on the free queue. This
2215 * should never happen; resources will probably be lost.
2217 ciss_printf(sc, "WARNING: completed command with no submitter\n");
2218 ciss_enqueue_free(cr);
2222 /************************************************************************
2223 * Report on the completion status of a request, and pass back SCSI
2224 * and command status values.
2227 _ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status, const char *func)
2229 struct ciss_command *cc;
2230 struct ciss_error_info *ce;
2235 ce = (struct ciss_error_info *)&(cc->sg[0]);
2238 * We don't consider data under/overrun an error for the Report
2239 * Logical/Physical LUNs commands.
2241 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
2242 ((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
2243 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
2244 ((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
2245 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
2246 (cc->cdb.cdb[0] == INQUIRY))) {
2247 cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
2248 debug(2, "ignoring irrelevant under/overrun error");
2252 * Check the command's error bit, if clear, there's no status and
2255 if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
2256 if (scsi_status != NULL)
2257 *scsi_status = SCSI_STATUS_OK;
2258 if (command_status != NULL)
2259 *command_status = CISS_CMD_STATUS_SUCCESS;
2262 if (command_status != NULL)
2263 *command_status = ce->command_status;
2264 if (scsi_status != NULL) {
2265 if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
2266 *scsi_status = ce->scsi_status;
2272 ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
2273 ce->command_status, ciss_name_command_status(ce->command_status),
2275 if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
2276 ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x, function %s\n",
2277 ce->additional_error_info.invalid_command.offense_size,
2278 ce->additional_error_info.invalid_command.offense_offset,
2279 ce->additional_error_info.invalid_command.offense_value,
2284 ciss_print_request(cr);
2289 /************************************************************************
2290 * Issue a request and don't return until it's completed.
2292 * Depending on adapter status, we may poll or sleep waiting for
2296 ciss_synch_request(struct ciss_request *cr, int timeout)
2298 if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
2299 return(ciss_wait_request(cr, timeout));
2301 return(ciss_poll_request(cr, timeout));
2305 /************************************************************************
2306 * Issue a request and poll for completion.
2308 * Timeout in milliseconds.
2311 ciss_poll_request(struct ciss_request *cr, int timeout)
2314 struct ciss_softc *sc;
2321 cr->cr_flags |= CISS_REQ_POLL;
2322 if ((error = ciss_start(cr)) != 0)
2327 ciss_perf_done(sc, &qh);
2330 ciss_complete(sc, &qh);
2331 if (!(cr->cr_flags & CISS_REQ_POLL))
2334 } while (timeout-- >= 0);
2335 return(EWOULDBLOCK);
2338 /************************************************************************
2339 * Issue a request and sleep waiting for completion.
2341 * Timeout in milliseconds. Note that a spurious wakeup will reset
2345 ciss_wait_request(struct ciss_request *cr, int timeout)
2351 cr->cr_flags |= CISS_REQ_SLEEP;
2352 if ((error = ciss_start(cr)) != 0)
2355 while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
2356 error = lksleep(cr, &cr->cr_sc->ciss_lock, 0, "cissREQ", (timeout * hz) / 1000);
2362 /************************************************************************
2363 * Abort a request. Note that a potential exists here to race the
2364 * request being completed; the caller must deal with this.
2367 ciss_abort_request(struct ciss_request *ar)
2369 struct ciss_request *cr;
2370 struct ciss_command *cc;
2371 struct ciss_message_cdb *cmc;
2377 if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
2380 /* build the abort command */
2382 cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL; /* addressing? */
2383 cc->header.address.physical.target = 0;
2384 cc->header.address.physical.bus = 0;
2385 cc->cdb.cdb_length = sizeof(*cmc);
2386 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
2387 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2388 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2389 cc->cdb.timeout = 30;
2391 cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
2392 cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
2393 cmc->type = CISS_MESSAGE_ABORT_TASK;
2394 cmc->abort_tag = ar->cr_tag; /* endianness?? */
2397 * Send the request and wait for a response. If we believe we
2398 * aborted the request OK, clear the flag that indicates it's
2401 error = ciss_synch_request(cr, 35 * 1000);
2403 error = ciss_report_request(cr, NULL, NULL);
2404 ciss_release_request(cr);
2411 /************************************************************************
2412 * Fetch and initialise a request
2415 ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
2417 struct ciss_request *cr;
2422 * Get a request and clean it up.
2424 if ((cr = ciss_dequeue_free(sc)) == NULL)
2429 cr->cr_complete = NULL;
2430 cr->cr_private = NULL;
2431 cr->cr_sg_tag = CISS_SG_MAX; /* Backstop to prevent accidents */
2433 ciss_preen_command(cr);
2439 ciss_preen_command(struct ciss_request *cr)
2441 struct ciss_command *cc;
2445 * Clean up the command structure.
2447 * Note that we set up the error_info structure here, since the
2448 * length can be overwritten by any command.
2451 cc->header.sg_in_list = 0; /* kinda inefficient this way */
2452 cc->header.sg_total = 0;
2453 cc->header.host_tag = cr->cr_tag << 2;
2454 cc->header.host_tag_zeroes = 0;
2455 cmdphys = cr->cr_ccphys;
2456 cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
2457 cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
2460 /************************************************************************
2461 * Release a request to the free list.
2464 ciss_release_request(struct ciss_request *cr)
2468 /* release the request to the free queue */
2469 ciss_requeue_free(cr);
2472 /************************************************************************
2473 * Allocate a request that will be used to send a BMIC command. Do some
2474 * of the common setup here to avoid duplicating it everywhere else.
2477 ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
2478 int opcode, void **bufp, size_t bufsize)
2480 struct ciss_request *cr;
2481 struct ciss_command *cc;
2482 struct ciss_bmic_cdb *cbc;
2495 if ((error = ciss_get_request(sc, &cr)) != 0)
2499 * Allocate data storage if requested, determine the data direction.
2502 if ((bufsize > 0) && (bufp != NULL)) {
2503 if (*bufp == NULL) {
2504 buf = kmalloc(bufsize, CISS_MALLOC_CLASS, M_INTWAIT | M_ZERO);
2507 dataout = 1; /* we are given a buffer, so we are writing */
2512 * Build a CISS BMIC command to get the logical drive ID.
2515 cr->cr_length = bufsize;
2517 cr->cr_flags = CISS_REQ_DATAIN;
2520 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2521 cc->header.address.physical.bus = 0;
2522 cc->header.address.physical.target = 0;
2523 cc->cdb.cdb_length = sizeof(*cbc);
2524 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2525 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2526 cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
2527 cc->cdb.timeout = 0;
2529 cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
2530 bzero(cbc, sizeof(*cbc));
2531 cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
2532 cbc->bmic_opcode = opcode;
2533 cbc->size = htons((u_int16_t)bufsize);
2538 ciss_release_request(cr);
2541 if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
2547 /************************************************************************
2548 * Handle a command passed in from userspace.
2551 ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
2553 struct ciss_request *cr;
2554 struct ciss_command *cc;
2555 struct ciss_error_info *ce;
2565 while (ciss_get_request(sc, &cr) != 0)
2566 lksleep(sc, &sc->ciss_lock, 0, "cissREQ", hz);
2570 * Allocate an in-kernel databuffer if required, copy in user data.
2572 lockmgr(&sc->ciss_lock, LK_RELEASE);
2573 cr->cr_length = ioc->buf_size;
2574 if (ioc->buf_size > 0) {
2575 cr->cr_data = kmalloc(ioc->buf_size, CISS_MALLOC_CLASS, M_WAITOK);
2576 if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
2577 debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2583 * Build the request based on the user command.
2585 bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
2586 bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
2588 /* XXX anything else to populate here? */
2589 lockmgr(&sc->ciss_lock, LK_EXCLUSIVE);
2594 if ((error = ciss_synch_request(cr, 60 * 1000))) {
2595 debug(0, "request failed - %d", error);
2600 * Check to see if the command succeeded.
2602 ce = (struct ciss_error_info *)&(cc->sg[0]);
2603 if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
2604 bzero(ce, sizeof(*ce));
2607 * Copy the results back to the user.
2609 bcopy(ce, &ioc->error_info, sizeof(*ce));
2610 lockmgr(&sc->ciss_lock, LK_RELEASE);
2611 if ((ioc->buf_size > 0) &&
2612 (error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
2613 debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2621 lockmgr(&sc->ciss_lock, LK_EXCLUSIVE);
2624 if ((cr != NULL) && (cr->cr_data != NULL))
2625 kfree(cr->cr_data, CISS_MALLOC_CLASS);
2627 ciss_release_request(cr);
2631 /************************************************************************
2632 * Map a request into bus-visible space, initialise the scatter/gather
2636 ciss_map_request(struct ciss_request *cr)
2638 struct ciss_softc *sc;
2645 /* check that mapping is necessary */
2646 if (cr->cr_flags & CISS_REQ_MAPPED)
2649 cr->cr_flags |= CISS_REQ_MAPPED;
2651 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2652 BUS_DMASYNC_PREWRITE);
2654 if (cr->cr_data != NULL) {
2655 error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2656 cr->cr_data, cr->cr_length,
2657 ciss_request_map_helper, cr, 0);
2662 * Post the command to the adapter.
2664 cr->cr_sg_tag = CISS_SG_NONE;
2665 cr->cr_flags |= CISS_REQ_BUSY;
2667 CISS_TL_PERF_POST_CMD(sc, cr);
2669 CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2676 ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2678 struct ciss_command *cc;
2679 struct ciss_request *cr;
2680 struct ciss_softc *sc;
2685 cr = (struct ciss_request *)arg;
2689 for (i = 0; i < nseg; i++) {
2690 cc->sg[i].address = segs[i].ds_addr;
2691 cc->sg[i].length = segs[i].ds_len;
2692 cc->sg[i].extension = 0;
2694 /* we leave the s/g table entirely within the command */
2695 cc->header.sg_in_list = nseg;
2696 cc->header.sg_total = nseg;
2698 if (cr->cr_flags & CISS_REQ_DATAIN)
2699 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2700 if (cr->cr_flags & CISS_REQ_DATAOUT)
2701 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2704 cr->cr_sg_tag = CISS_SG_NONE;
2706 cr->cr_sg_tag = CISS_SG_1;
2708 cr->cr_sg_tag = CISS_SG_2;
2710 cr->cr_sg_tag = CISS_SG_4;
2712 cr->cr_sg_tag = CISS_SG_8;
2713 else if (nseg <= 16)
2714 cr->cr_sg_tag = CISS_SG_16;
2715 else if (nseg <= 32)
2716 cr->cr_sg_tag = CISS_SG_32;
2718 cr->cr_sg_tag = CISS_SG_MAX;
2721 * Post the command to the adapter.
2723 cr->cr_flags |= CISS_REQ_BUSY;
2725 CISS_TL_PERF_POST_CMD(sc, cr);
2727 CISS_TL_SIMPLE_POST_CMD(sc, cr->cr_ccphys);
2730 /************************************************************************
2731 * Unmap a request from bus-visible space.
2734 ciss_unmap_request(struct ciss_request *cr)
2736 struct ciss_softc *sc;
2742 /* check that unmapping is necessary */
2743 if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
2746 bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2747 BUS_DMASYNC_POSTWRITE);
2749 if (cr->cr_data == NULL)
2752 if (cr->cr_flags & CISS_REQ_DATAIN)
2753 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
2754 if (cr->cr_flags & CISS_REQ_DATAOUT)
2755 bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
2757 bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2759 cr->cr_flags &= ~CISS_REQ_MAPPED;
2762 /************************************************************************
2763 * Attach the driver to CAM.
2765 * We put all the logical drives on a single SCSI bus.
2768 ciss_cam_init(struct ciss_softc *sc)
2775 * Allocate a devq. We can reuse this for the masked physical
2776 * devices if we decide to export these as well.
2778 if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests - 2)) == NULL) {
2779 ciss_printf(sc, "can't allocate CAM SIM queue\n");
2786 * This naturally wastes a bit of memory. The alternative is to allocate
2787 * and register each bus as it is found, and then track them on a linked
2788 * list. Unfortunately, the driver has a few places where it needs to
2789 * look up the SIM based solely on bus number, and it's unclear whether
2790 * a list traversal would work for these situations.
2792 maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
2793 CISS_PHYSICAL_BASE);
2794 sc->ciss_cam_sim = kmalloc(maxbus * sizeof(struct cam_sim*),
2795 CISS_MALLOC_CLASS, M_INTWAIT | M_ZERO);
2797 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2798 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2800 device_get_unit(sc->ciss_dev),
2803 sc->ciss_max_requests - 2,
2804 sc->ciss_cam_devq)) == NULL) {
2805 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2810 * Register bus with this SIM.
2812 lockmgr(&sc->ciss_lock, LK_EXCLUSIVE);
2813 if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) {
2814 if (xpt_bus_register(sc->ciss_cam_sim[i], i) != 0) {
2815 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2816 lockmgr(&sc->ciss_lock, LK_RELEASE);
2820 lockmgr(&sc->ciss_lock, LK_RELEASE);
2823 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
2824 CISS_PHYSICAL_BASE; i++) {
2825 if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2827 device_get_unit(sc->ciss_dev),
2829 sc->ciss_max_requests - 2,
2830 sc->ciss_cam_devq)) == NULL) {
2831 ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2835 lockmgr(&sc->ciss_lock, LK_EXCLUSIVE);
2836 if (xpt_bus_register(sc->ciss_cam_sim[i], i) != 0) {
2837 ciss_printf(sc, "can't register SCSI bus %d\n", i);
2838 lockmgr(&sc->ciss_lock, LK_RELEASE);
2841 lockmgr(&sc->ciss_lock, LK_RELEASE);
2845 * Initiate a rescan of the bus.
2847 ciss_cam_rescan_all(sc);
2852 /************************************************************************
2853 * Initiate a rescan of the 'logical devices' SIM
2856 ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
2862 ccb = kmalloc(sizeof(union ccb), M_TEMP, M_WAITOK | M_ZERO);
2864 if (xpt_create_path(&ccb->ccb_h.path, xpt_periph,
2865 cam_sim_path(sc->ciss_cam_sim[bus]),
2866 target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2867 ciss_printf(sc, "rescan failed (can't create path)\n");
2872 xpt_setup_ccb(&ccb->ccb_h, ccb->ccb_h.path, 5/*priority (low)*/);
2873 ccb->ccb_h.func_code = XPT_SCAN_BUS;
2874 ccb->ccb_h.cbfcnp = ciss_cam_rescan_callback;
2875 ccb->crcn.flags = CAM_FLAG_NONE;
2878 /* scan is now in progress */
2882 ciss_cam_rescan_all(struct ciss_softc *sc)
2886 /* Rescan the logical buses */
2887 for (i = 0; i < sc->ciss_max_logical_bus; i++)
2888 ciss_cam_rescan_target(sc, i, CAM_TARGET_WILDCARD);
2889 /* Rescan the physical buses */
2890 for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
2891 CISS_PHYSICAL_BASE; i++)
2892 ciss_cam_rescan_target(sc, i, CAM_TARGET_WILDCARD);
2896 ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2898 xpt_free_path(ccb->ccb_h.path);
2902 /************************************************************************
2903 * Handle requests coming from CAM
2906 ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
2908 struct ciss_softc *sc;
2909 struct ccb_scsiio *csio;
2913 sc = cam_sim_softc(sim);
2914 bus = cam_sim_bus(sim);
2915 csio = (struct ccb_scsiio *)&ccb->csio;
2916 target = csio->ccb_h.target_id;
2917 physical = CISS_IS_PHYSICAL(bus);
2919 switch (ccb->ccb_h.func_code) {
2921 /* perform SCSI I/O */
2923 if (!ciss_cam_action_io(sim, csio))
2927 /* perform geometry calculations */
2928 case XPT_CALC_GEOMETRY:
2930 struct ccb_calc_geometry *ccg = &ccb->ccg;
2931 struct ciss_ldrive *ld;
2933 debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2937 ld = &sc->ciss_logical[bus][target];
2940 * Use the cached geometry settings unless the fault tolerance
2943 if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
2944 u_int32_t secs_per_cylinder;
2947 ccg->secs_per_track = 32;
2948 secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2949 ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2951 ccg->heads = ld->cl_geometry.heads;
2952 ccg->secs_per_track = ld->cl_geometry.sectors;
2953 ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2955 ccb->ccb_h.status = CAM_REQ_CMP;
2959 /* handle path attribute inquiry */
2962 struct ccb_pathinq *cpi = &ccb->cpi;
2964 debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2966 cpi->version_num = 1;
2967 cpi->hba_inquiry = PI_TAG_ABLE; /* XXX is this correct? */
2968 cpi->target_sprt = 0;
2970 cpi->max_target = CISS_MAX_LOGICAL;
2971 cpi->max_lun = 0; /* 'logical drive' channel only */
2972 cpi->initiator_id = CISS_MAX_LOGICAL;
2973 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2974 strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
2975 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2976 cpi->unit_number = cam_sim_unit(sim);
2977 cpi->bus_id = cam_sim_bus(sim);
2978 cpi->base_transfer_speed = 132 * 1024; /* XXX what to set this to? */
2979 cpi->transport = XPORT_SPI;
2980 cpi->transport_version = 2;
2981 cpi->protocol = PROTO_SCSI;
2982 cpi->protocol_version = SCSI_REV_2;
2983 #if 0 /* XXX swildner */
2984 cpi->maxio = (CISS_MAX_SG_ELEMENTS - 1) * PAGE_SIZE;
2986 ccb->ccb_h.status = CAM_REQ_CMP;
2990 case XPT_GET_TRAN_SETTINGS:
2992 struct ccb_trans_settings *cts = &ccb->cts;
2996 struct ccb_trans_settings_spi *spi = &cts->xport_specific.spi;
2997 struct ccb_trans_settings_scsi *scsi = &cts->proto_specific.scsi;
3000 bus = cam_sim_bus(sim);
3001 target = cts->ccb_h.target_id;
3004 debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
3005 /* disconnect always OK */
3006 cts->protocol = PROTO_SCSI;
3007 cts->protocol_version = SCSI_REV_2;
3008 cts->transport = XPORT_SPI;
3009 cts->transport_version = 2;
3011 spi->valid = CTS_SPI_VALID_DISC;
3012 spi->flags = CTS_SPI_FLAGS_DISC_ENB;
3014 scsi->valid = CTS_SCSI_VALID_TQ;
3015 scsi->flags = CTS_SCSI_FLAGS_TAG_ENB;
3017 cts->ccb_h.status = CAM_REQ_CMP;
3021 default: /* we can't do this */
3022 debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
3023 ccb->ccb_h.status = CAM_REQ_INVALID;
3030 /************************************************************************
3031 * Handle a CAM SCSI I/O request.
3034 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
3036 struct ciss_softc *sc;
3038 struct ciss_request *cr;
3039 struct ciss_command *cc;
3042 sc = cam_sim_softc(sim);
3043 bus = cam_sim_bus(sim);
3044 target = csio->ccb_h.target_id;
3046 debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
3048 /* check that the CDB pointer is not to a physical address */
3049 if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
3050 debug(3, " CDB pointer is to physical address");
3051 csio->ccb_h.status = CAM_REQ_CMP_ERR;
3054 /* if there is data transfer, it must be to/from a virtual address */
3055 if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
3056 if (csio->ccb_h.flags & CAM_DATA_PHYS) { /* we can't map it */
3057 debug(3, " data pointer is to physical address");
3058 csio->ccb_h.status = CAM_REQ_CMP_ERR;
3060 if (csio->ccb_h.flags & CAM_SCATTER_VALID) { /* we want to do the s/g setup */
3061 debug(3, " data has premature s/g setup");
3062 csio->ccb_h.status = CAM_REQ_CMP_ERR;
3066 /* abandon aborted ccbs or those that have failed validation */
3067 if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
3068 debug(3, "abandoning CCB due to abort/validation failure");
3072 /* handle emulation of some SCSI commands ourself */
3073 if (ciss_cam_emulate(sc, csio))
3077 * Get a request to manage this command. If we can't, return the
3078 * ccb, freeze the queue and flag so that we unfreeze it when a
3079 * request completes.
3081 if ((error = ciss_get_request(sc, &cr)) != 0) {
3082 xpt_freeze_simq(sim, 1);
3083 sc->ciss_flags |= CISS_FLAG_BUSY;
3084 csio->ccb_h.status |= CAM_REQUEUE_REQ;
3089 * Build the command.
3092 cr->cr_data = csio->data_ptr;
3093 cr->cr_length = csio->dxfer_len;
3094 cr->cr_complete = ciss_cam_complete;
3095 cr->cr_private = csio;
3098 * Target the right logical volume.
3100 if (CISS_IS_PHYSICAL(bus))
3101 cc->header.address =
3102 sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
3104 cc->header.address =
3105 sc->ciss_logical[bus][target].cl_address;
3106 cc->cdb.cdb_length = csio->cdb_len;
3107 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3108 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE; /* XXX ordered tags? */
3109 if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
3110 cr->cr_flags = CISS_REQ_DATAOUT;
3111 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3112 } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
3113 cr->cr_flags = CISS_REQ_DATAIN;
3114 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3117 cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
3119 cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
3120 if (csio->ccb_h.flags & CAM_CDB_POINTER) {
3121 bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
3123 bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
3127 * Submit the request to the adapter.
3129 * Note that this may fail if we're unable to map the request (and
3130 * if we ever learn a transport layer other than simple, may fail
3131 * if the adapter rejects the command).
3133 if ((error = ciss_start(cr)) != 0) {
3134 xpt_freeze_simq(sim, 1);
3135 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3136 if (error == EINPROGRESS) {
3139 csio->ccb_h.status |= CAM_REQUEUE_REQ;
3140 ciss_release_request(cr);
3148 /************************************************************************
3149 * Emulate SCSI commands the adapter doesn't handle as we might like.
3152 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
3156 target = csio->ccb_h.target_id;
3157 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3159 if (CISS_IS_PHYSICAL(bus)) {
3160 if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
3161 csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3162 xpt_done((union ccb *)csio);
3169 * Handle requests for volumes that don't exist or are not online.
3170 * A selection timeout is slightly better than an illegal request.
3171 * Other errors might be better.
3173 if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
3174 csio->ccb_h.status |= CAM_SEL_TIMEOUT;
3175 xpt_done((union ccb *)csio);
3179 /* if we have to fake Synchronise Cache */
3180 if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
3182 * If this is a Synchronise Cache command, typically issued when
3183 * a device is closed, flush the adapter and complete now.
3185 if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
3186 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
3187 ciss_flush_adapter(sc);
3188 csio->ccb_h.status |= CAM_REQ_CMP;
3189 xpt_done((union ccb *)csio);
3197 /************************************************************************
3198 * Check for possibly-completed commands.
3201 ciss_cam_poll(struct cam_sim *sim)
3204 struct ciss_softc *sc = cam_sim_softc(sim);
3210 ciss_perf_done(sc, &qh);
3213 ciss_complete(sc, &qh);
3216 /************************************************************************
3217 * Handle completion of a command - pass results back through the CCB
3220 ciss_cam_complete(struct ciss_request *cr)
3222 struct ciss_softc *sc;
3223 struct ciss_command *cc;
3224 struct ciss_error_info *ce;
3225 struct ccb_scsiio *csio;
3233 ce = (struct ciss_error_info *)&(cc->sg[0]);
3234 csio = (struct ccb_scsiio *)cr->cr_private;
3237 * Extract status values from request.
3239 ciss_report_request(cr, &command_status, &scsi_status);
3240 csio->scsi_status = scsi_status;
3243 * Handle specific SCSI status values.
3245 switch(scsi_status) {
3246 /* no status due to adapter error */
3248 debug(0, "adapter error");
3249 csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3252 /* no status due to command completed OK */
3253 case SCSI_STATUS_OK: /* CISS_SCSI_STATUS_GOOD */
3254 debug(2, "SCSI_STATUS_OK");
3255 csio->ccb_h.status |= CAM_REQ_CMP;
3258 /* check condition, sense data included */
3259 case SCSI_STATUS_CHECK_COND: /* CISS_SCSI_STATUS_CHECK_CONDITION */
3260 debug(0, "SCSI_STATUS_CHECK_COND sense size %d resid %d\n",
3261 ce->sense_length, ce->residual_count);
3262 bzero(&csio->sense_data, SSD_FULL_SIZE);
3263 bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
3264 if (csio->sense_len > ce->sense_length)
3265 csio->sense_resid = csio->sense_len - ce->sense_length;
3267 csio->sense_resid = 0;
3268 csio->resid = ce->residual_count;
3269 csio->ccb_h.status |= CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
3272 struct scsi_sense_data *sns = (struct scsi_sense_data *)&ce->sense_info[0];
3273 debug(0, "sense key %x", sns->flags & SSD_KEY);
3278 case SCSI_STATUS_BUSY: /* CISS_SCSI_STATUS_BUSY */
3279 debug(0, "SCSI_STATUS_BUSY");
3280 csio->ccb_h.status |= CAM_SCSI_BUSY;
3284 debug(0, "unknown status 0x%x", csio->scsi_status);
3285 csio->ccb_h.status |= CAM_REQ_CMP_ERR;
3289 /* handle post-command fixup */
3290 ciss_cam_complete_fixup(sc, csio);
3292 ciss_release_request(cr);
3293 if (sc->ciss_flags & CISS_FLAG_BUSY) {
3294 sc->ciss_flags &= ~CISS_FLAG_BUSY;
3295 if (csio->ccb_h.status & CAM_RELEASE_SIMQ)
3296 xpt_release_simq(xpt_path_sim(csio->ccb_h.path), 0);
3298 csio->ccb_h.status |= CAM_RELEASE_SIMQ;
3300 xpt_done((union ccb *)csio);
3303 /********************************************************************************
3304 * Fix up the result of some commands here.
3307 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
3309 struct scsi_inquiry_data *inq;
3310 struct ciss_ldrive *cl;
3314 cdb = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
3315 (uint8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes;
3316 if (cdb[0] == INQUIRY &&
3317 (cdb[1] & SI_EVPD) == 0 &&
3318 (csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN &&
3319 csio->dxfer_len >= SHORT_INQUIRY_LENGTH) {
3321 inq = (struct scsi_inquiry_data *)csio->data_ptr;
3322 target = csio->ccb_h.target_id;
3323 bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3326 * Don't let hard drives be seen by the DA driver. They will still be
3327 * attached by the PASS driver.
3329 if (CISS_IS_PHYSICAL(bus)) {
3330 if (SID_TYPE(inq) == T_DIRECT)
3331 inq->device = (inq->device & 0xe0) | T_NODEVICE;
3335 cl = &sc->ciss_logical[bus][target];
3337 padstr(inq->vendor, "COMPAQ", 8);
3338 padstr(inq->product, ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance), 8);
3339 padstr(inq->revision, ciss_name_ldrive_status(cl->cl_lstatus->status), 16);
3344 /********************************************************************************
3345 * Find a peripheral attached at (target)
3347 static struct cam_periph *
3348 ciss_find_periph(struct ciss_softc *sc, int bus, int target)
3350 struct cam_periph *periph;
3351 struct cam_path *path;
3354 status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
3356 if (status == CAM_REQ_CMP) {
3357 periph = cam_periph_find(path, NULL);
3358 xpt_free_path(path);
3365 /********************************************************************************
3366 * Name the device at (target)
3368 * XXX is this strictly correct?
3371 ciss_name_device(struct ciss_softc *sc, int bus, int target)
3373 struct cam_periph *periph;
3375 if (CISS_IS_PHYSICAL(bus))
3377 if ((periph = ciss_find_periph(sc, bus, target)) != NULL) {
3378 ksprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
3379 periph->periph_name, periph->unit_number);
3382 sc->ciss_logical[bus][target].cl_name[0] = 0;
3386 /************************************************************************
3387 * Periodic status monitoring.
3390 ciss_periodic(void *arg)
3392 struct ciss_softc *sc = (struct ciss_softc *)arg;
3393 struct ciss_request *cr = NULL;
3394 struct ciss_command *cc = NULL;
3397 lockmgr(&sc->ciss_lock, LK_EXCLUSIVE);
3401 * Check the adapter heartbeat.
3403 if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
3404 sc->ciss_heart_attack++;
3405 debug(0, "adapter heart attack in progress 0x%x/%d",
3406 sc->ciss_heartbeat, sc->ciss_heart_attack);
3407 if (sc->ciss_heart_attack == 3) {
3408 ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
3409 ciss_disable_adapter(sc);
3410 lockmgr(&sc->ciss_lock, LK_RELEASE);
3414 sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
3415 sc->ciss_heart_attack = 0;
3416 debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
3420 * Send the NOP message and wait for a response.
3422 if (ciss_nop_message_heartbeat != 0 && (error = ciss_get_request(sc, &cr)) == 0) {
3424 cr->cr_complete = ciss_nop_complete;
3425 cc->cdb.cdb_length = 1;
3426 cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
3427 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3428 cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3429 cc->cdb.timeout = 0;
3430 cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
3432 if ((error = ciss_start(cr)) != 0) {
3433 ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3438 * If the notify event request has died for some reason, or has
3439 * not started yet, restart it.
3441 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
3442 debug(0, "(re)starting Event Notify chain");
3443 ciss_notify_event(sc);
3445 lockmgr(&sc->ciss_lock, LK_RELEASE);
3450 callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
3454 ciss_nop_complete(struct ciss_request *cr)
3456 struct ciss_softc *sc;
3457 static int first_time = 1;
3460 if (ciss_report_request(cr, NULL, NULL) != 0) {
3461 if (first_time == 1) {
3463 ciss_printf(sc, "SENDING NOP MESSAGE FAILED (not logging anymore)\n");
3467 ciss_release_request(cr);
3470 /************************************************************************
3471 * Disable the adapter.
3473 * The all requests in completed queue is failed with hardware error.
3474 * This will cause failover in a multipath configuration.
3477 ciss_disable_adapter(struct ciss_softc *sc)
3480 struct ciss_request *cr;
3481 struct ciss_command *cc;
3482 struct ciss_error_info *ce;
3485 CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
3486 pci_disable_busmaster(sc->ciss_dev);
3487 sc->ciss_flags &= ~CISS_FLAG_RUNNING;
3489 for (i = 1; i < sc->ciss_max_requests; i++) {
3490 cr = &sc->ciss_request[i];
3491 if ((cr->cr_flags & CISS_REQ_BUSY) == 0)
3495 ce = (struct ciss_error_info *)&(cc->sg[0]);
3496 ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
3497 ciss_enqueue_complete(cr, &qh);
3501 if ((cr = ciss_dequeue_complete(sc, &qh)) == NULL)
3505 * If the request has a callback, invoke it.
3507 if (cr->cr_complete != NULL) {
3508 cr->cr_complete(cr);
3513 * If someone is sleeping on this request, wake them up.
3515 if (cr->cr_flags & CISS_REQ_SLEEP) {
3516 cr->cr_flags &= ~CISS_REQ_SLEEP;
3523 /************************************************************************
3524 * Request a notification response from the adapter.
3526 * If (cr) is NULL, this is the first request of the adapter, so
3527 * reset the adapter's message pointer and start with the oldest
3528 * message available.
3531 ciss_notify_event(struct ciss_softc *sc)
3533 struct ciss_request *cr;
3534 struct ciss_command *cc;
3535 struct ciss_notify_cdb *cnc;
3540 cr = sc->ciss_periodic_notify;
3542 /* get a request if we don't already have one */
3544 if ((error = ciss_get_request(sc, &cr)) != 0) {
3545 debug(0, "can't get notify event request");
3548 sc->ciss_periodic_notify = cr;
3549 cr->cr_complete = ciss_notify_complete;
3550 debug(1, "acquired request %d", cr->cr_tag);
3554 * Get a databuffer if we don't already have one, note that the
3555 * adapter command wants a larger buffer than the actual
3558 if (cr->cr_data == NULL) {
3559 cr->cr_data = kmalloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_INTWAIT);
3560 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3563 /* re-setup the request's command (since we never release it) XXX overkill*/
3564 ciss_preen_command(cr);
3566 /* (re)build the notify event command */
3568 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3569 cc->header.address.physical.bus = 0;
3570 cc->header.address.physical.target = 0;
3572 cc->cdb.cdb_length = sizeof(*cnc);
3573 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3574 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3575 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3576 cc->cdb.timeout = 0; /* no timeout, we hope */
3578 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3579 bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
3580 cnc->opcode = CISS_OPCODE_READ;
3581 cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
3582 cnc->timeout = 0; /* no timeout, we hope */
3583 cnc->synchronous = 0;
3585 cnc->seek_to_oldest = 0;
3586 if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
3590 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3592 /* submit the request */
3593 error = ciss_start(cr);
3598 if (cr->cr_data != NULL)
3599 kfree(cr->cr_data, CISS_MALLOC_CLASS);
3600 ciss_release_request(cr);
3602 sc->ciss_periodic_notify = NULL;
3603 debug(0, "can't submit notify event request");
3604 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3606 debug(1, "notify event submitted");
3607 sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
3612 ciss_notify_complete(struct ciss_request *cr)
3614 struct ciss_notify *cn;
3615 struct ciss_softc *sc;
3620 cn = (struct ciss_notify *)cr->cr_data;
3624 * Report request results, decode status.
3626 ciss_report_request(cr, &command_status, &scsi_status);
3629 * Abort the chain on a fatal error.
3631 * XXX which of these are actually errors?
3633 if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
3634 (command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
3635 (command_status != CISS_CMD_STATUS_TIMEOUT)) { /* XXX timeout? */
3636 ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
3637 ciss_name_command_status(command_status));
3638 ciss_release_request(cr);
3639 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3644 * If the adapter gave us a text message, print it.
3646 if (cn->message[0] != 0)
3647 ciss_printf(sc, "*** %.80s\n", cn->message);
3649 debug(0, "notify event class %d subclass %d detail %d",
3650 cn->class, cn->subclass, cn->detail);
3653 * If the response indicates that the notifier has been aborted,
3654 * release the notifier command.
3656 if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
3657 (cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
3658 (cn->detail == 1)) {
3659 debug(0, "notifier exiting");
3660 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3661 ciss_release_request(cr);
3662 sc->ciss_periodic_notify = NULL;
3663 wakeup(&sc->ciss_periodic_notify);
3665 /* Handle notify events in a kernel thread */
3666 ciss_enqueue_notify(cr);
3667 sc->ciss_periodic_notify = NULL;
3668 wakeup(&sc->ciss_periodic_notify);
3669 wakeup(&sc->ciss_notify);
3672 * Send a new notify event command, if we're not aborting.
3674 if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
3675 ciss_notify_event(sc);
3679 /************************************************************************
3680 * Abort the Notify Event chain.
3682 * Note that we can't just abort the command in progress; we have to
3683 * explicitly issue an Abort Notify Event command in order for the
3684 * adapter to clean up correctly.
3686 * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
3687 * the chain will not restart itself.
3690 ciss_notify_abort(struct ciss_softc *sc)
3692 struct ciss_request *cr;
3693 struct ciss_command *cc;
3694 struct ciss_notify_cdb *cnc;
3695 int error, command_status, scsi_status;
3702 /* verify that there's an outstanding command */
3703 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3706 /* get a command to issue the abort with */
3707 if ((error = ciss_get_request(sc, &cr)))
3710 /* get a buffer for the result */
3711 cr->cr_data = kmalloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_INTWAIT);
3712 cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3716 cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3717 cc->header.address.physical.bus = 0;
3718 cc->header.address.physical.target = 0;
3719 cc->cdb.cdb_length = sizeof(*cnc);
3720 cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3721 cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3722 cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3723 cc->cdb.timeout = 0; /* no timeout, we hope */
3725 cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3726 bzero(cnc, sizeof(*cnc));
3727 cnc->opcode = CISS_OPCODE_WRITE;
3728 cnc->command = CISS_COMMAND_ABORT_NOTIFY;
3729 cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3731 ciss_print_request(cr);
3734 * Submit the request and wait for it to complete.
3736 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3737 ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
3744 ciss_report_request(cr, &command_status, &scsi_status);
3745 switch(command_status) {
3746 case CISS_CMD_STATUS_SUCCESS:
3748 case CISS_CMD_STATUS_INVALID_COMMAND:
3750 * Some older adapters don't support the CISS version of this
3751 * command. Fall back to using the BMIC version.
3753 error = ciss_notify_abort_bmic(sc);
3758 case CISS_CMD_STATUS_TARGET_STATUS:
3760 * This can happen if the adapter thinks there wasn't an outstanding
3761 * Notify Event command but we did. We clean up here.
3763 if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
3764 if (sc->ciss_periodic_notify != NULL)
3765 ciss_release_request(sc->ciss_periodic_notify);
3772 ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
3773 ciss_name_command_status(command_status));
3779 * Sleep waiting for the notifier command to complete. Note
3780 * that if it doesn't, we may end up in a bad situation, since
3781 * the adapter may deliver it later. Also note that the adapter
3782 * requires the Notify Event command to be cancelled in order to
3783 * maintain internal bookkeeping.
3785 while (sc->ciss_periodic_notify != NULL) {
3786 error = lksleep(&sc->ciss_periodic_notify, &sc->ciss_lock, 0, "cissNEA", hz * 5);
3787 if (error == EWOULDBLOCK) {
3788 ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
3794 /* release the cancel request */
3796 if (cr->cr_data != NULL)
3797 kfree(cr->cr_data, CISS_MALLOC_CLASS);
3798 ciss_release_request(cr);
3801 sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3805 /************************************************************************
3806 * Abort the Notify Event chain using a BMIC command.
3809 ciss_notify_abort_bmic(struct ciss_softc *sc)
3811 struct ciss_request *cr;
3812 int error, command_status;
3819 /* verify that there's an outstanding command */
3820 if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3824 * Build a BMIC command to cancel the Notify on Event command.
3826 * Note that we are sending a CISS opcode here. Odd.
3828 if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
3833 * Submit the request and wait for it to complete.
3835 if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3836 ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
3843 ciss_report_request(cr, &command_status, NULL);
3844 switch(command_status) {
3845 case CISS_CMD_STATUS_SUCCESS:
3848 ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
3849 ciss_name_command_status(command_status));
3856 ciss_release_request(cr);
3860 /************************************************************************
3861 * Handle rescanning all the logical volumes when a notify event
3862 * causes the drives to come online or offline.
3865 ciss_notify_rescan_logical(struct ciss_softc *sc)
3867 struct ciss_lun_report *cll;
3868 struct ciss_ldrive *ld;
3872 * We must rescan all logical volumes to get the right logical
3875 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
3880 ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
3883 * Delete any of the drives which were destroyed by the
3886 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
3887 for (j = 0; j < CISS_MAX_LOGICAL; j++) {
3888 ld = &sc->ciss_logical[i][j];
3890 if (ld->cl_update == 0)
3893 if (ld->cl_status != CISS_LD_ONLINE) {
3894 ciss_cam_rescan_target(sc, i, j);
3897 kfree(ld->cl_ldrive, CISS_MALLOC_CLASS);
3899 kfree(ld->cl_lstatus, CISS_MALLOC_CLASS);
3901 ld->cl_ldrive = NULL;
3902 ld->cl_lstatus = NULL;
3908 * Scan for new drives.
3910 for (i = 0; i < ndrives; i++) {
3913 bus = CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
3914 target = CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
3915 ld = &sc->ciss_logical[bus][target];
3917 if (ld->cl_update == 0)
3921 ld->cl_address = cll->lun[i];
3922 ld->cl_controller = &sc->ciss_controllers[bus];
3923 if (ciss_identify_logical(sc, ld) == 0) {
3924 ciss_cam_rescan_target(sc, bus, target);
3927 kfree(cll, CISS_MALLOC_CLASS);
3930 /************************************************************************
3931 * Handle a notify event relating to the status of a logical drive.
3933 * XXX need to be able to defer some of these to properly handle
3934 * calling the "ID Physical drive" command, unless the 'extended'
3935 * drive IDs are always in BIG_MAP format.
3938 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
3940 struct ciss_ldrive *ld;
3941 int ostatus, bus, target;
3945 bus = cn->device.physical.bus;
3946 target = cn->data.logical_status.logical_drive;
3947 ld = &sc->ciss_logical[bus][target];
3949 switch (cn->subclass) {
3950 case CISS_NOTIFY_LOGICAL_STATUS:
3951 switch (cn->detail) {
3953 ciss_name_device(sc, bus, target);
3954 ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
3955 cn->data.logical_status.logical_drive, ld->cl_name,
3956 ciss_name_ldrive_status(cn->data.logical_status.previous_state),
3957 ciss_name_ldrive_status(cn->data.logical_status.new_state),
3958 cn->data.logical_status.spare_state,
3959 "\20\1configured\2rebuilding\3failed\4in use\5available\n");
3962 * Update our idea of the drive's status.
3964 ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
3965 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3966 if (ld->cl_lstatus != NULL)
3967 ld->cl_lstatus->status = cn->data.logical_status.new_state;
3970 * Have CAM rescan the drive if its status has changed.
3972 if (ostatus != ld->cl_status) {
3974 ciss_notify_rescan_logical(sc);
3979 case 1: /* logical drive has recognised new media, needs Accept Media Exchange */
3980 ciss_name_device(sc, bus, target);
3981 ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
3982 cn->data.logical_status.logical_drive, ld->cl_name);
3983 ciss_accept_media(sc, ld);
3986 ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3987 ciss_notify_rescan_logical(sc);
3992 ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
3993 cn->data.rebuild_aborted.logical_drive,
3995 (cn->detail == 2) ? "read" : "write");
4000 case CISS_NOTIFY_LOGICAL_ERROR:
4001 if (cn->detail == 0) {
4002 ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
4003 cn->data.io_error.logical_drive,
4005 cn->data.io_error.failure_bus,
4006 cn->data.io_error.failure_drive);
4007 /* XXX should we take the drive down at this point, or will we be told? */
4011 case CISS_NOTIFY_LOGICAL_SURFACE:
4012 if (cn->detail == 0)
4013 ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
4014 cn->data.consistency_completed.logical_drive,
4020 /************************************************************************
4021 * Handle a notify event relating to the status of a physical drive.
4024 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
4028 /************************************************************************
4029 * Handle a notify event relating to the status of a physical drive.
4032 ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
4034 struct ciss_lun_report *cll = NULL;
4037 switch (cn->subclass) {
4038 case CISS_NOTIFY_HOTPLUG_PHYSICAL:
4039 case CISS_NOTIFY_HOTPLUG_NONDISK:
4040 bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
4042 CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
4044 if (cn->detail == 0) {
4046 * Mark the device offline so that it'll start producing selection
4047 * timeouts to the upper layer.
4049 if ((bus >= 0) && (target >= 0))
4050 sc->ciss_physical[bus][target].cp_online = 0;
4053 * Rescan the physical lun list for new items
4055 cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
4058 ciss_printf(sc, "Warning, cannot get physical lun list\n");
4061 ciss_filter_physical(sc, cll);
4066 ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
4071 kfree(cll, CISS_MALLOC_CLASS);
4074 /************************************************************************
4075 * Handle deferred processing of notify events. Notify events may need
4076 * sleep which is unsafe during an interrupt.
4079 ciss_notify_thread(void *arg)
4081 struct ciss_softc *sc;
4082 struct ciss_request *cr;
4083 struct ciss_notify *cn;
4085 sc = (struct ciss_softc *)arg;
4086 lockmgr(&sc->ciss_lock, LK_EXCLUSIVE);
4089 if (STAILQ_EMPTY(&sc->ciss_notify) != 0 &&
4090 (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
4091 lksleep(&sc->ciss_notify, &sc->ciss_lock, 0, "idle", 0);
4094 if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
4097 cr = ciss_dequeue_notify(sc);
4101 cn = (struct ciss_notify *)cr->cr_data;
4103 switch (cn->class) {
4104 case CISS_NOTIFY_HOTPLUG:
4105 ciss_notify_hotplug(sc, cn);
4107 case CISS_NOTIFY_LOGICAL:
4108 ciss_notify_logical(sc, cn);
4110 case CISS_NOTIFY_PHYSICAL:
4111 ciss_notify_physical(sc, cn);
4115 ciss_release_request(cr);
4118 sc->ciss_notify_thread = NULL;
4119 wakeup(&sc->ciss_notify_thread);
4121 lockmgr(&sc->ciss_lock, LK_RELEASE);
4125 /************************************************************************
4126 * Start the notification kernel thread.
4129 ciss_spawn_notify_thread(struct ciss_softc *sc)
4132 if (kthread_create((void(*)(void *))ciss_notify_thread, sc,
4133 &sc->ciss_notify_thread, "ciss_notify%d",
4134 device_get_unit(sc->ciss_dev)))
4135 panic("Could not create notify thread\n");
4138 /************************************************************************
4139 * Kill the notification kernel thread.
4142 ciss_kill_notify_thread(struct ciss_softc *sc)
4145 if (sc->ciss_notify_thread == NULL)
4148 sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
4149 wakeup(&sc->ciss_notify);
4150 lksleep(&sc->ciss_notify_thread, &sc->ciss_lock, 0, "thtrm", 0);
4153 /************************************************************************
4157 ciss_print_request(struct ciss_request *cr)
4159 struct ciss_softc *sc;
4160 struct ciss_command *cc;
4162 char hexstr[HEX_NCPYLEN(CISS_CDB_BUFFER_SIZE)];
4167 ciss_printf(sc, "REQUEST @ %p\n", cr);
4168 ciss_printf(sc, " data %p/%d tag %d flags %b\n",
4169 cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
4170 "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
4171 ciss_printf(sc, " sg list/total %d/%d host tag 0x%x\n",
4172 cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
4173 switch(cc->header.address.mode.mode) {
4174 case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
4175 case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
4176 ciss_printf(sc, " physical bus %d target %d\n",
4177 cc->header.address.physical.bus, cc->header.address.physical.target);
4179 case CISS_HDR_ADDRESS_MODE_LOGICAL:
4180 ciss_printf(sc, " logical unit %d\n", cc->header.address.logical.lun);
4183 ciss_printf(sc, " %s cdb length %d type %s attribute %s\n",
4184 (cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
4185 (cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
4186 (cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
4188 (cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
4189 (cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
4190 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
4191 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
4192 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
4193 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
4194 (cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
4195 ciss_printf(sc, " %s\n", hexncpy(&cc->cdb.cdb[0], cc->cdb.cdb_length,
4196 hexstr, HEX_NCPYLEN(cc->cdb.cdb_length), " "));
4198 if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
4199 /* XXX print error info */
4201 /* since we don't use chained s/g, don't support it here */
4202 for (i = 0; i < cc->header.sg_in_list; i++) {
4204 ciss_printf(sc, " ");
4205 kprintf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
4206 if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
4212 /************************************************************************
4213 * Print information about the status of a logical drive.
4216 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
4220 if (ld->cl_lstatus == NULL) {
4221 kprintf("does not exist\n");
4225 /* print drive status */
4226 switch(ld->cl_lstatus->status) {
4227 case CISS_LSTATUS_OK:
4228 kprintf("online\n");
4230 case CISS_LSTATUS_INTERIM_RECOVERY:
4231 kprintf("in interim recovery mode\n");
4233 case CISS_LSTATUS_READY_RECOVERY:
4234 kprintf("ready to begin recovery\n");
4236 case CISS_LSTATUS_RECOVERING:
4237 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4238 target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
4239 kprintf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
4240 bus, target, ld->cl_lstatus->blocks_to_recover);
4242 case CISS_LSTATUS_EXPANDING:
4243 kprintf("being expanded, %u blocks remaining\n",
4244 ld->cl_lstatus->blocks_to_recover);
4246 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4247 kprintf("queued for expansion\n");
4249 case CISS_LSTATUS_FAILED:
4250 kprintf("queued for expansion\n");
4252 case CISS_LSTATUS_WRONG_PDRIVE:
4253 kprintf("wrong physical drive inserted\n");
4255 case CISS_LSTATUS_MISSING_PDRIVE:
4256 kprintf("missing a needed physical drive\n");
4258 case CISS_LSTATUS_BECOMING_READY:
4259 kprintf("becoming ready\n");
4263 /* print failed physical drives */
4264 for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
4265 bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
4266 target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
4269 ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
4270 ld->cl_lstatus->drive_failure_map[i]);
4275 #include "opt_ddb.h"
4277 #include <ddb/ddb.h>
4278 /************************************************************************
4279 * Print information about the controller/driver.
4282 ciss_print_adapter(struct ciss_softc *sc)
4286 ciss_printf(sc, "ADAPTER:\n");
4287 for (i = 0; i < CISSQ_COUNT; i++) {
4288 ciss_printf(sc, "%s %d/%d\n",
4290 i == 1 ? "busy" : "complete",
4291 sc->ciss_qstat[i].q_length,
4292 sc->ciss_qstat[i].q_max);
4294 ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
4295 ciss_printf(sc, "flags %b\n", sc->ciss_flags,
4296 "\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
4298 for (i = 0; i < sc->ciss_max_logical_bus; i++) {
4299 for (j = 0; j < CISS_MAX_LOGICAL; j++) {
4300 ciss_printf(sc, "LOGICAL DRIVE %d: ", i);
4301 ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
4305 /* XXX Should physical drives be printed out here? */
4307 for (i = 1; i < sc->ciss_max_requests; i++)
4308 ciss_print_request(sc->ciss_request + i);
4312 DB_COMMAND(ciss_prt, db_ciss_prt)
4314 struct ciss_softc *sc;
4316 sc = devclass_get_softc(devclass_find("ciss"), 0);
4318 kprintf("no ciss controllers\n");
4320 ciss_print_adapter(sc);
4326 /************************************************************************
4327 * Return a name for a logical drive status value.
4330 ciss_name_ldrive_status(int status)
4333 case CISS_LSTATUS_OK:
4335 case CISS_LSTATUS_FAILED:
4337 case CISS_LSTATUS_NOT_CONFIGURED:
4338 return("not configured");
4339 case CISS_LSTATUS_INTERIM_RECOVERY:
4340 return("interim recovery");
4341 case CISS_LSTATUS_READY_RECOVERY:
4342 return("ready for recovery");
4343 case CISS_LSTATUS_RECOVERING:
4344 return("recovering");
4345 case CISS_LSTATUS_WRONG_PDRIVE:
4346 return("wrong physical drive inserted");
4347 case CISS_LSTATUS_MISSING_PDRIVE:
4348 return("missing physical drive");
4349 case CISS_LSTATUS_EXPANDING:
4350 return("expanding");
4351 case CISS_LSTATUS_BECOMING_READY:
4352 return("becoming ready");
4353 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4354 return("queued for expansion");
4356 return("unknown status");
4359 /************************************************************************
4360 * Return an online/offline/nonexistent value for a logical drive
4364 ciss_decode_ldrive_status(int status)
4367 case CISS_LSTATUS_NOT_CONFIGURED:
4368 return(CISS_LD_NONEXISTENT);
4370 case CISS_LSTATUS_OK:
4371 case CISS_LSTATUS_INTERIM_RECOVERY:
4372 case CISS_LSTATUS_READY_RECOVERY:
4373 case CISS_LSTATUS_RECOVERING:
4374 case CISS_LSTATUS_EXPANDING:
4375 case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4376 return(CISS_LD_ONLINE);
4378 case CISS_LSTATUS_FAILED:
4379 case CISS_LSTATUS_WRONG_PDRIVE:
4380 case CISS_LSTATUS_MISSING_PDRIVE:
4381 case CISS_LSTATUS_BECOMING_READY:
4383 return(CISS_LD_OFFLINE);
4388 /************************************************************************
4389 * Return a name for a logical drive's organisation.
4392 ciss_name_ldrive_org(int org)
4395 case CISS_LDRIVE_RAID0:
4397 case CISS_LDRIVE_RAID1:
4398 return("RAID 1(1+0)");
4399 case CISS_LDRIVE_RAID4:
4401 case CISS_LDRIVE_RAID5:
4403 case CISS_LDRIVE_RAID51:
4405 case CISS_LDRIVE_RAIDADG:
4411 /************************************************************************
4412 * Return a name for a command status value.
4415 ciss_name_command_status(int status)
4418 case CISS_CMD_STATUS_SUCCESS:
4420 case CISS_CMD_STATUS_TARGET_STATUS:
4421 return("target status");
4422 case CISS_CMD_STATUS_DATA_UNDERRUN:
4423 return("data underrun");
4424 case CISS_CMD_STATUS_DATA_OVERRUN:
4425 return("data overrun");
4426 case CISS_CMD_STATUS_INVALID_COMMAND:
4427 return("invalid command");
4428 case CISS_CMD_STATUS_PROTOCOL_ERROR:
4429 return("protocol error");
4430 case CISS_CMD_STATUS_HARDWARE_ERROR:
4431 return("hardware error");
4432 case CISS_CMD_STATUS_CONNECTION_LOST:
4433 return("connection lost");
4434 case CISS_CMD_STATUS_ABORTED:
4436 case CISS_CMD_STATUS_ABORT_FAILED:
4437 return("abort failed");
4438 case CISS_CMD_STATUS_UNSOLICITED_ABORT:
4439 return("unsolicited abort");
4440 case CISS_CMD_STATUS_TIMEOUT:
4442 case CISS_CMD_STATUS_UNABORTABLE:
4443 return("unabortable");
4445 return("unknown status");
4448 /************************************************************************
4449 * Handle an open on the control device.
4452 ciss_open(struct dev_open_args *ap)
4454 cdev_t dev = ap->a_head.a_dev;
4455 struct ciss_softc *sc;
4459 sc = (struct ciss_softc *)dev->si_drv1;
4461 /* we might want to veto if someone already has us open */
4463 lockmgr(&sc->ciss_lock, LK_EXCLUSIVE);
4464 sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
4465 lockmgr(&sc->ciss_lock, LK_RELEASE);
4469 /************************************************************************
4470 * Handle the last close on the control device.
4473 ciss_close(struct dev_close_args *ap)
4475 cdev_t dev = ap->a_head.a_dev;
4476 struct ciss_softc *sc;
4480 sc = (struct ciss_softc *)dev->si_drv1;
4482 lockmgr(&sc->ciss_lock, LK_EXCLUSIVE);
4483 sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
4484 lockmgr(&sc->ciss_lock, LK_RELEASE);
4488 /********************************************************************************
4489 * Handle adapter-specific control operations.
4491 * Note that the API here is compatible with the Linux driver, in order to
4492 * simplify the porting of Compaq's userland tools.
4495 ciss_ioctl(struct dev_ioctl_args *ap)
4497 caddr_t addr = ap->a_data;
4498 cdev_t dev = ap->a_head.a_dev;
4499 u_long cmd = ap->a_cmd;
4500 struct ciss_softc *sc;
4501 IOCTL_Command_struct *ioc = (IOCTL_Command_struct *)addr;
4503 IOCTL_Command_struct32 *ioc32 = (IOCTL_Command_struct32 *)addr;
4504 IOCTL_Command_struct ioc_swab;
4510 sc = (struct ciss_softc *)dev->si_drv1;
4512 lockmgr(&sc->ciss_lock, LK_EXCLUSIVE);
4515 case CCISS_GETQSTATS:
4517 union ciss_statrequest *cr = (union ciss_statrequest *)addr;
4519 switch (cr->cs_item) {
4522 bcopy(&sc->ciss_qstat[cr->cs_item], &cr->cs_qstat,
4523 sizeof(struct ciss_qstat));
4533 case CCISS_GETPCIINFO:
4535 cciss_pci_info_struct *pis = (cciss_pci_info_struct *)addr;
4537 pis->bus = pci_get_bus(sc->ciss_dev);
4538 pis->dev_fn = pci_get_slot(sc->ciss_dev);
4539 pis->board_id = (pci_get_subvendor(sc->ciss_dev) << 16) |
4540 pci_get_subdevice(sc->ciss_dev);
4545 case CCISS_GETINTINFO:
4547 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4549 cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
4550 cis->count = sc->ciss_cfg->interrupt_coalesce_count;
4555 case CCISS_SETINTINFO:
4557 cciss_coalint_struct *cis = (cciss_coalint_struct *)addr;
4559 if ((cis->delay == 0) && (cis->count == 0)) {
4565 * XXX apparently this is only safe if the controller is idle,
4566 * we should suspend it before doing this.
4568 sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
4569 sc->ciss_cfg->interrupt_coalesce_count = cis->count;
4571 if (ciss_update_config(sc))
4574 /* XXX resume the controller here */
4578 case CCISS_GETNODENAME:
4579 bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
4580 sizeof(NodeName_type));
4583 case CCISS_SETNODENAME:
4584 bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
4585 sizeof(NodeName_type));
4586 if (ciss_update_config(sc))
4590 case CCISS_GETHEARTBEAT:
4591 *(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
4594 case CCISS_GETBUSTYPES:
4595 *(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
4598 case CCISS_GETFIRMVER:
4599 bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
4600 sizeof(FirmwareVer_type));
4603 case CCISS_GETDRIVERVER:
4604 *(DriverVer_type *)addr = CISS_DRIVER_VERSION;
4607 case CCISS_REVALIDVOLS:
4609 * This is a bit ugly; to do it "right" we really need
4610 * to find any disks that have changed, kick CAM off them,
4611 * then rescan only these disks. It'd be nice if they
4612 * a) told us which disk(s) they were going to play with,
4613 * and b) which ones had arrived. 8(
4618 case CCISS_PASSTHRU32:
4619 ioc_swab.LUN_info = ioc32->LUN_info;
4620 ioc_swab.Request = ioc32->Request;
4621 ioc_swab.error_info = ioc32->error_info;
4622 ioc_swab.buf_size = ioc32->buf_size;
4623 ioc_swab.buf = (u_int8_t *)(uintptr_t)ioc32->buf;
4628 case CCISS_PASSTHRU:
4629 error = ciss_user_command(sc, ioc);
4633 debug(0, "unknown ioctl 0x%lx", cmd);
4635 debug(1, "CCISS_GETPCIINFO: 0x%lx", CCISS_GETPCIINFO);
4636 debug(1, "CCISS_GETINTINFO: 0x%lx", CCISS_GETINTINFO);
4637 debug(1, "CCISS_SETINTINFO: 0x%lx", CCISS_SETINTINFO);
4638 debug(1, "CCISS_GETNODENAME: 0x%lx", CCISS_GETNODENAME);
4639 debug(1, "CCISS_SETNODENAME: 0x%lx", CCISS_SETNODENAME);
4640 debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
4641 debug(1, "CCISS_GETBUSTYPES: 0x%lx", CCISS_GETBUSTYPES);
4642 debug(1, "CCISS_GETFIRMVER: 0x%lx", CCISS_GETFIRMVER);
4643 debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
4644 debug(1, "CCISS_REVALIDVOLS: 0x%lx", CCISS_REVALIDVOLS);
4645 debug(1, "CCISS_PASSTHRU: 0x%lx", CCISS_PASSTHRU);
4651 lockmgr(&sc->ciss_lock, LK_RELEASE);