xref: /freebsd/sys/dev/ciss/ciss.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
1 /*-
2  * Copyright (c) 2001 Michael Smith
3  * Copyright (c) 2004 Paul Saab
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  *	$FreeBSD$
28  */
29 
30 /*
31  * Common Interface for SCSI-3 Support driver.
32  *
33  * CISS claims to provide a common interface between a generic SCSI
34  * transport and an intelligent host adapter.
35  *
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.
43  *
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.
54  *
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.
61  *
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.
65  *
66  *
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
69  * driver happen.
70  *
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.
75  */
76 
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/malloc.h>
80 #include <sys/kernel.h>
81 #include <sys/bus.h>
82 #include <sys/conf.h>
83 #include <sys/stat.h>
84 #include <sys/kthread.h>
85 #include <sys/queue.h>
86 #include <sys/sysctl.h>
87 
88 #include <cam/cam.h>
89 #include <cam/cam_ccb.h>
90 #include <cam/cam_periph.h>
91 #include <cam/cam_sim.h>
92 #include <cam/cam_xpt_sim.h>
93 #include <cam/scsi/scsi_all.h>
94 #include <cam/scsi/scsi_message.h>
95 
96 #include <machine/bus.h>
97 #include <machine/endian.h>
98 #include <machine/resource.h>
99 #include <sys/rman.h>
100 
101 #include <dev/pci/pcireg.h>
102 #include <dev/pci/pcivar.h>
103 
104 #include <dev/ciss/cissreg.h>
105 #include <dev/ciss/cissvar.h>
106 #include <dev/ciss/cissio.h>
107 
108 MALLOC_DEFINE(CISS_MALLOC_CLASS, "ciss_data", "ciss internal data buffers");
109 
110 /* pci interface */
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);
116 
117 /* (de)initialisation functions, control wrappers */
118 static int	ciss_init_pci(struct ciss_softc *sc);
119 static int	ciss_wait_adapter(struct ciss_softc *sc);
120 static int	ciss_flush_adapter(struct ciss_softc *sc);
121 static int	ciss_init_requests(struct ciss_softc *sc);
122 static void	ciss_command_map_helper(void *arg, bus_dma_segment_t *segs,
123 					int nseg, int error);
124 static int	ciss_identify_adapter(struct ciss_softc *sc);
125 static int	ciss_init_logical(struct ciss_softc *sc);
126 static int	ciss_init_physical(struct ciss_softc *sc);
127 static int	ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll);
128 static int	ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld);
129 static int	ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld);
130 static int	ciss_update_config(struct ciss_softc *sc);
131 static int	ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld);
132 static void	ciss_init_sysctl(struct ciss_softc *sc);
133 static void	ciss_soft_reset(struct ciss_softc *sc);
134 static void	ciss_free(struct ciss_softc *sc);
135 static void	ciss_spawn_notify_thread(struct ciss_softc *sc);
136 static void	ciss_kill_notify_thread(struct ciss_softc *sc);
137 
138 /* request submission/completion */
139 static int	ciss_start(struct ciss_request *cr);
140 static void	ciss_done(struct ciss_softc *sc);
141 static void	ciss_intr(void *arg);
142 static void	ciss_complete(struct ciss_softc *sc);
143 static int	ciss_report_request(struct ciss_request *cr, int *command_status,
144 				    int *scsi_status);
145 static int	ciss_synch_request(struct ciss_request *cr, int timeout);
146 static int	ciss_poll_request(struct ciss_request *cr, int timeout);
147 static int	ciss_wait_request(struct ciss_request *cr, int timeout);
148 #if 0
149 static int	ciss_abort_request(struct ciss_request *cr);
150 #endif
151 
152 /* request queueing */
153 static int	ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp);
154 static void	ciss_preen_command(struct ciss_request *cr);
155 static void 	ciss_release_request(struct ciss_request *cr);
156 
157 /* request helpers */
158 static int	ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
159 				      int opcode, void **bufp, size_t bufsize);
160 static int	ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc);
161 
162 /* DMA map/unmap */
163 static int	ciss_map_request(struct ciss_request *cr);
164 static void	ciss_request_map_helper(void *arg, bus_dma_segment_t *segs,
165 					int nseg, int error);
166 static void	ciss_unmap_request(struct ciss_request *cr);
167 
168 /* CAM interface */
169 static int	ciss_cam_init(struct ciss_softc *sc);
170 static void	ciss_cam_rescan_target(struct ciss_softc *sc,
171 				       int bus, int target);
172 static void	ciss_cam_rescan_all(struct ciss_softc *sc);
173 static void	ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb);
174 static void	ciss_cam_action(struct cam_sim *sim, union ccb *ccb);
175 static int	ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio);
176 static int	ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio);
177 static void	ciss_cam_poll(struct cam_sim *sim);
178 static void	ciss_cam_complete(struct ciss_request *cr);
179 static void	ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio);
180 static struct cam_periph *ciss_find_periph(struct ciss_softc *sc,
181 					   int bus, int target);
182 static int	ciss_name_device(struct ciss_softc *sc, int bus, int target);
183 
184 /* periodic status monitoring */
185 static void	ciss_periodic(void *arg);
186 static void	ciss_nop_complete(struct ciss_request *cr);
187 static void	ciss_disable_adapter(struct ciss_softc *sc);
188 static void	ciss_notify_event(struct ciss_softc *sc);
189 static void	ciss_notify_complete(struct ciss_request *cr);
190 static int	ciss_notify_abort(struct ciss_softc *sc);
191 static int	ciss_notify_abort_bmic(struct ciss_softc *sc);
192 static void	ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn);
193 static void	ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn);
194 static void	ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn);
195 
196 /* debugging output */
197 static void	ciss_print_request(struct ciss_request *cr);
198 static void	ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld);
199 static const char *ciss_name_ldrive_status(int status);
200 static int	ciss_decode_ldrive_status(int status);
201 static const char *ciss_name_ldrive_org(int org);
202 static const char *ciss_name_command_status(int status);
203 
204 /*
205  * PCI bus interface.
206  */
207 static device_method_t ciss_methods[] = {
208     /* Device interface */
209     DEVMETHOD(device_probe,	ciss_probe),
210     DEVMETHOD(device_attach,	ciss_attach),
211     DEVMETHOD(device_detach,	ciss_detach),
212     DEVMETHOD(device_shutdown,	ciss_shutdown),
213     { 0, 0 }
214 };
215 
216 static driver_t ciss_pci_driver = {
217     "ciss",
218     ciss_methods,
219     sizeof(struct ciss_softc)
220 };
221 
222 static devclass_t	ciss_devclass;
223 DRIVER_MODULE(ciss, pci, ciss_pci_driver, ciss_devclass, 0, 0);
224 MODULE_DEPEND(ciss, cam, 1, 1, 1);
225 MODULE_DEPEND(ciss, pci, 1, 1, 1);
226 
227 /*
228  * Control device interface.
229  */
230 static d_open_t		ciss_open;
231 static d_close_t	ciss_close;
232 static d_ioctl_t	ciss_ioctl;
233 
234 static struct cdevsw ciss_cdevsw = {
235 	.d_version =	D_VERSION,
236 	.d_flags =	0,
237 	.d_open =	ciss_open,
238 	.d_close =	ciss_close,
239 	.d_ioctl =	ciss_ioctl,
240 	.d_name =	"ciss",
241 };
242 
243 /*
244  * This tunable can be set at boot time and controls whether physical devices
245  * that are marked hidden by the firmware should be exposed anyways.
246  */
247 static unsigned int ciss_expose_hidden_physical = 0;
248 TUNABLE_INT("hw.ciss.expose_hidden_physical", &ciss_expose_hidden_physical);
249 
250 /************************************************************************
251  * CISS adapters amazingly don't have a defined programming interface
252  * value.  (One could say some very despairing things about PCI and
253  * people just not getting the general idea.)  So we are forced to
254  * stick with matching against subvendor/subdevice, and thus have to
255  * be updated for every new CISS adapter that appears.
256  */
257 #define CISS_BOARD_SA5	(1<<0)
258 #define CISS_BOARD_SA5B	(1<<1)
259 
260 static struct
261 {
262     u_int16_t	subvendor;
263     u_int16_t	subdevice;
264     int		flags;
265     char	*desc;
266 } ciss_vendor_data[] = {
267     { 0x0e11, 0x4070, CISS_BOARD_SA5,	"Compaq Smart Array 5300" },
268     { 0x0e11, 0x4080, CISS_BOARD_SA5B,	"Compaq Smart Array 5i" },
269     { 0x0e11, 0x4082, CISS_BOARD_SA5B,	"Compaq Smart Array 532" },
270     { 0x0e11, 0x4083, CISS_BOARD_SA5B,	"HP Smart Array 5312" },
271     { 0x0e11, 0x4091, CISS_BOARD_SA5,	"HP Smart Array 6i" },
272     { 0x0e11, 0x409A, CISS_BOARD_SA5,	"HP Smart Array 641" },
273     { 0x0e11, 0x409B, CISS_BOARD_SA5,	"HP Smart Array 642" },
274     { 0x0e11, 0x409C, CISS_BOARD_SA5,	"HP Smart Array 6400" },
275     { 0x0e11, 0x409D, CISS_BOARD_SA5,	"HP Smart Array 6400 EM" },
276     { 0x103C, 0x3211, CISS_BOARD_SA5,	"HP Smart Array E200i" },
277     { 0x103C, 0x3212, CISS_BOARD_SA5,	"HP Smart Array E200" },
278     { 0x103C, 0x3213, CISS_BOARD_SA5,	"HP Smart Array E200i" },
279     { 0x103C, 0x3214, CISS_BOARD_SA5,	"HP Smart Array E200i" },
280     { 0x103C, 0x3215, CISS_BOARD_SA5,	"HP Smart Array E200i" },
281     { 0x103C, 0x3220, CISS_BOARD_SA5,	"HP Smart Array" },
282     { 0x103C, 0x3222, CISS_BOARD_SA5,	"HP Smart Array" },
283     { 0x103C, 0x3223, CISS_BOARD_SA5,	"HP Smart Array P800" },
284     { 0x103C, 0x3225, CISS_BOARD_SA5,	"HP Smart Array P600" },
285     { 0x103C, 0x3230, CISS_BOARD_SA5,	"HP Smart Array" },
286     { 0x103C, 0x3231, CISS_BOARD_SA5,	"HP Smart Array" },
287     { 0x103C, 0x3232, CISS_BOARD_SA5,	"HP Smart Array" },
288     { 0x103C, 0x3233, CISS_BOARD_SA5,	"HP Smart Array" },
289     { 0x103C, 0x3234, CISS_BOARD_SA5,	"HP Smart Array P400" },
290     { 0x103C, 0x3235, CISS_BOARD_SA5,	"HP Smart Array P400i" },
291     { 0x103C, 0x3236, CISS_BOARD_SA5,	"HP Smart Array" },
292     { 0x103C, 0x3237, CISS_BOARD_SA5,	"HP Smart Array" },
293     { 0x103C, 0x3238, CISS_BOARD_SA5,	"HP Smart Array" },
294     { 0x103C, 0x3239, CISS_BOARD_SA5,	"HP Smart Array" },
295     { 0x103C, 0x323A, CISS_BOARD_SA5,	"HP Smart Array" },
296     { 0x103C, 0x323B, CISS_BOARD_SA5,	"HP Smart Array" },
297     { 0x103C, 0x323C, CISS_BOARD_SA5,	"HP Smart Array" },
298     { 0, 0, 0, NULL }
299 };
300 
301 /************************************************************************
302  * Find a match for the device in our list of known adapters.
303  */
304 static int
305 ciss_lookup(device_t dev)
306 {
307     int 	i;
308 
309     for (i = 0; ciss_vendor_data[i].desc != NULL; i++)
310 	if ((pci_get_subvendor(dev) == ciss_vendor_data[i].subvendor) &&
311 	    (pci_get_subdevice(dev) == ciss_vendor_data[i].subdevice)) {
312 	    return(i);
313 	}
314     return(-1);
315 }
316 
317 /************************************************************************
318  * Match a known CISS adapter.
319  */
320 static int
321 ciss_probe(device_t dev)
322 {
323     int		i;
324 
325     i = ciss_lookup(dev);
326     if (i != -1) {
327 	device_set_desc(dev, ciss_vendor_data[i].desc);
328 	return(BUS_PROBE_DEFAULT);
329     }
330     return(ENOENT);
331 }
332 
333 /************************************************************************
334  * Attach the driver to this adapter.
335  */
336 static int
337 ciss_attach(device_t dev)
338 {
339     struct ciss_softc	*sc;
340     int			i, error;
341 
342     debug_called(1);
343 
344 #ifdef CISS_DEBUG
345     /* print structure/union sizes */
346     debug_struct(ciss_command);
347     debug_struct(ciss_header);
348     debug_union(ciss_device_address);
349     debug_struct(ciss_cdb);
350     debug_struct(ciss_report_cdb);
351     debug_struct(ciss_notify_cdb);
352     debug_struct(ciss_notify);
353     debug_struct(ciss_message_cdb);
354     debug_struct(ciss_error_info_pointer);
355     debug_struct(ciss_error_info);
356     debug_struct(ciss_sg_entry);
357     debug_struct(ciss_config_table);
358     debug_struct(ciss_bmic_cdb);
359     debug_struct(ciss_bmic_id_ldrive);
360     debug_struct(ciss_bmic_id_lstatus);
361     debug_struct(ciss_bmic_id_table);
362     debug_struct(ciss_bmic_id_pdrive);
363     debug_struct(ciss_bmic_blink_pdrive);
364     debug_struct(ciss_bmic_flush_cache);
365     debug_const(CISS_MAX_REQUESTS);
366     debug_const(CISS_MAX_LOGICAL);
367     debug_const(CISS_INTERRUPT_COALESCE_DELAY);
368     debug_const(CISS_INTERRUPT_COALESCE_COUNT);
369     debug_const(CISS_COMMAND_ALLOC_SIZE);
370     debug_const(CISS_COMMAND_SG_LENGTH);
371 
372     debug_type(cciss_pci_info_struct);
373     debug_type(cciss_coalint_struct);
374     debug_type(cciss_coalint_struct);
375     debug_type(NodeName_type);
376     debug_type(NodeName_type);
377     debug_type(Heartbeat_type);
378     debug_type(BusTypes_type);
379     debug_type(FirmwareVer_type);
380     debug_type(DriverVer_type);
381     debug_type(IOCTL_Command_struct);
382 #endif
383 
384     sc = device_get_softc(dev);
385     sc->ciss_dev = dev;
386 
387     /*
388      * Work out adapter type.
389      */
390     i = ciss_lookup(dev);
391     if (i < 0) {
392 	ciss_printf(sc, "unknown adapter type\n");
393 	error = ENXIO;
394 	goto out;
395     }
396     if (ciss_vendor_data[i].flags & CISS_BOARD_SA5) {
397 	sc->ciss_interrupt_mask = CISS_TL_SIMPLE_INTR_OPQ_SA5;
398     } else if (ciss_vendor_data[i].flags & CISS_BOARD_SA5B) {
399 	sc->ciss_interrupt_mask = CISS_TL_SIMPLE_INTR_OPQ_SA5B;
400     } else {
401 	/* really an error on our part */
402 	ciss_printf(sc, "unable to determine hardware type\n");
403 	error = ENXIO;
404 	goto out;
405     }
406 
407     /*
408      * Do PCI-specific init.
409      */
410     if ((error = ciss_init_pci(sc)) != 0)
411 	goto out;
412 
413     /*
414      * Initialise driver queues.
415      */
416     ciss_initq_free(sc);
417     ciss_initq_busy(sc);
418     ciss_initq_complete(sc);
419     ciss_initq_notify(sc);
420     mtx_init(&sc->ciss_mtx, "cissmtx", NULL, MTX_DEF);
421     callout_init_mtx(&sc->ciss_periodic, &sc->ciss_mtx, 0);
422 
423     /*
424      * Initalize device sysctls.
425      */
426     ciss_init_sysctl(sc);
427 
428     /*
429      * Initialise command/request pool.
430      */
431     if ((error = ciss_init_requests(sc)) != 0)
432 	goto out;
433 
434     /*
435      * Get adapter information.
436      */
437     if ((error = ciss_identify_adapter(sc)) != 0)
438 	goto out;
439 
440     /*
441      * Find all the physical devices.
442      */
443     if ((error = ciss_init_physical(sc)) != 0)
444 	goto out;
445 
446     /*
447      * Build our private table of logical devices.
448      */
449     if ((error = ciss_init_logical(sc)) != 0)
450 	goto out;
451 
452     /*
453      * Enable interrupts so that the CAM scan can complete.
454      */
455     CISS_TL_SIMPLE_ENABLE_INTERRUPTS(sc);
456 
457     /*
458      * Initialise the CAM interface.
459      */
460     if ((error = ciss_cam_init(sc)) != 0)
461 	goto out;
462 
463     /*
464      * Start the heartbeat routine and event chain.
465      */
466     ciss_periodic(sc);
467 
468    /*
469      * Create the control device.
470      */
471     sc->ciss_dev_t = make_dev(&ciss_cdevsw, device_get_unit(sc->ciss_dev),
472 			      UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
473 			      "ciss%d", device_get_unit(sc->ciss_dev));
474     sc->ciss_dev_t->si_drv1 = sc;
475 
476     /*
477      * The adapter is running; synchronous commands can now sleep
478      * waiting for an interrupt to signal completion.
479      */
480     sc->ciss_flags |= CISS_FLAG_RUNNING;
481 
482     ciss_spawn_notify_thread(sc);
483 
484     error = 0;
485  out:
486     if (error != 0)
487 	ciss_free(sc);
488     return(error);
489 }
490 
491 /************************************************************************
492  * Detach the driver from this adapter.
493  */
494 static int
495 ciss_detach(device_t dev)
496 {
497     struct ciss_softc	*sc = device_get_softc(dev);
498 
499     debug_called(1);
500 
501     mtx_lock(&sc->ciss_mtx);
502     if (sc->ciss_flags & CISS_FLAG_CONTROL_OPEN) {
503 	mtx_unlock(&sc->ciss_mtx);
504 	return (EBUSY);
505     }
506 
507     /* flush adapter cache */
508     ciss_flush_adapter(sc);
509 
510     /* release all resources.  The mutex is released and freed here too. */
511     ciss_free(sc);
512 
513     return(0);
514 }
515 
516 /************************************************************************
517  * Prepare adapter for system shutdown.
518  */
519 static int
520 ciss_shutdown(device_t dev)
521 {
522     struct ciss_softc	*sc = device_get_softc(dev);
523 
524     debug_called(1);
525 
526     mtx_lock(&sc->ciss_mtx);
527     /* flush adapter cache */
528     ciss_flush_adapter(sc);
529 
530     if (sc->ciss_soft_reset)
531 	ciss_soft_reset(sc);
532     mtx_unlock(&sc->ciss_mtx);
533 
534     return(0);
535 }
536 
537 static void
538 ciss_init_sysctl(struct ciss_softc *sc)
539 {
540 
541     SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->ciss_dev),
542 	SYSCTL_CHILDREN(device_get_sysctl_tree(sc->ciss_dev)),
543 	OID_AUTO, "soft_reset", CTLFLAG_RW, &sc->ciss_soft_reset, 0, "");
544 }
545 
546 /************************************************************************
547  * Perform PCI-specific attachment actions.
548  */
549 static int
550 ciss_init_pci(struct ciss_softc *sc)
551 {
552     uintptr_t		cbase, csize, cofs;
553     int			error;
554 
555     debug_called(1);
556 
557     /*
558      * Allocate register window first (we need this to find the config
559      * struct).
560      */
561     error = ENXIO;
562     sc->ciss_regs_rid = CISS_TL_SIMPLE_BAR_REGS;
563     if ((sc->ciss_regs_resource =
564 	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
565 				&sc->ciss_regs_rid, RF_ACTIVE)) == NULL) {
566 	ciss_printf(sc, "can't allocate register window\n");
567 	return(ENXIO);
568     }
569     sc->ciss_regs_bhandle = rman_get_bushandle(sc->ciss_regs_resource);
570     sc->ciss_regs_btag = rman_get_bustag(sc->ciss_regs_resource);
571 
572     /*
573      * Find the BAR holding the config structure.  If it's not the one
574      * we already mapped for registers, map it too.
575      */
576     sc->ciss_cfg_rid = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_BAR) & 0xffff;
577     if (sc->ciss_cfg_rid != sc->ciss_regs_rid) {
578 	if ((sc->ciss_cfg_resource =
579 	     bus_alloc_resource_any(sc->ciss_dev, SYS_RES_MEMORY,
580 				    &sc->ciss_cfg_rid, RF_ACTIVE)) == NULL) {
581 	    ciss_printf(sc, "can't allocate config window\n");
582 	    return(ENXIO);
583 	}
584 	cbase = (uintptr_t)rman_get_virtual(sc->ciss_cfg_resource);
585 	csize = rman_get_end(sc->ciss_cfg_resource) -
586 	    rman_get_start(sc->ciss_cfg_resource) + 1;
587     } else {
588 	cbase = (uintptr_t)rman_get_virtual(sc->ciss_regs_resource);
589 	csize = rman_get_end(sc->ciss_regs_resource) -
590 	    rman_get_start(sc->ciss_regs_resource) + 1;
591     }
592     cofs = CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_CFG_OFF);
593 
594     /*
595      * Use the base/size/offset values we just calculated to
596      * sanity-check the config structure.  If it's OK, point to it.
597      */
598     if ((cofs + sizeof(struct ciss_config_table)) > csize) {
599 	ciss_printf(sc, "config table outside window\n");
600 	return(ENXIO);
601     }
602     sc->ciss_cfg = (struct ciss_config_table *)(cbase + cofs);
603     debug(1, "config struct at %p", sc->ciss_cfg);
604 
605     /*
606      * Validate the config structure.  If we supported other transport
607      * methods, we could select amongst them at this point in time.
608      */
609     if (strncmp(sc->ciss_cfg->signature, "CISS", 4)) {
610 	ciss_printf(sc, "config signature mismatch (got '%c%c%c%c')\n",
611 		    sc->ciss_cfg->signature[0], sc->ciss_cfg->signature[1],
612 		    sc->ciss_cfg->signature[2], sc->ciss_cfg->signature[3]);
613 	return(ENXIO);
614     }
615 
616     /*
617      * Put the board into simple mode, and tell it we're using the low
618      * 4GB of RAM.  Set the default interrupt coalescing options.
619      */
620     if (!(sc->ciss_cfg->supported_methods & CISS_TRANSPORT_METHOD_SIMPLE)) {
621 	ciss_printf(sc, "adapter does not support 'simple' transport layer\n");
622 	return(ENXIO);
623     }
624     sc->ciss_cfg->requested_method = CISS_TRANSPORT_METHOD_SIMPLE;
625     sc->ciss_cfg->command_physlimit = 0;
626     sc->ciss_cfg->interrupt_coalesce_delay = CISS_INTERRUPT_COALESCE_DELAY;
627     sc->ciss_cfg->interrupt_coalesce_count = CISS_INTERRUPT_COALESCE_COUNT;
628 
629 #ifdef __i386__
630     sc->ciss_cfg->host_driver |= CISS_DRIVER_SCSI_PREFETCH;
631 #endif
632 
633     if (ciss_update_config(sc)) {
634 	ciss_printf(sc, "adapter refuses to accept config update (IDBR 0x%x)\n",
635 		    CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR));
636 	return(ENXIO);
637     }
638     if (!(sc->ciss_cfg->active_method != CISS_TRANSPORT_METHOD_SIMPLE)) {
639 	ciss_printf(sc,
640 		    "adapter refuses to go into 'simple' transport mode (0x%x, 0x%x)\n",
641 		    sc->ciss_cfg->supported_methods, sc->ciss_cfg->active_method);
642 	return(ENXIO);
643     }
644 
645     /*
646      * Wait for the adapter to come ready.
647      */
648     if ((error = ciss_wait_adapter(sc)) != 0)
649 	return(error);
650 
651     /*
652      * Turn off interrupts before we go routing anything.
653      */
654     CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
655 
656     /*
657      * Allocate and set up our interrupt.
658      */
659     sc->ciss_irq_rid = 0;
660     if ((sc->ciss_irq_resource =
661 	 bus_alloc_resource_any(sc->ciss_dev, SYS_RES_IRQ, &sc->ciss_irq_rid,
662 				RF_ACTIVE | RF_SHAREABLE)) == NULL) {
663 	ciss_printf(sc, "can't allocate interrupt\n");
664 	return(ENXIO);
665     }
666     if (bus_setup_intr(sc->ciss_dev, sc->ciss_irq_resource,
667 		       INTR_TYPE_CAM|INTR_MPSAFE, NULL, ciss_intr, sc,
668 		       &sc->ciss_intr)) {
669 	ciss_printf(sc, "can't set up interrupt\n");
670 	return(ENXIO);
671     }
672 
673     /*
674      * Allocate the parent bus DMA tag appropriate for our PCI
675      * interface.
676      *
677      * Note that "simple" adapters can only address within a 32-bit
678      * span.
679      */
680     if (bus_dma_tag_create(NULL, 			/* parent */
681 			   1, 0, 			/* alignment, boundary */
682 			   BUS_SPACE_MAXADDR,		/* lowaddr */
683 			   BUS_SPACE_MAXADDR, 		/* highaddr */
684 			   NULL, NULL, 			/* filter, filterarg */
685 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsize */
686 			   CISS_COMMAND_SG_LENGTH,	/* nsegments */
687 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
688 			   BUS_DMA_ALLOCNOW,		/* flags */
689 			   NULL, NULL,			/* lockfunc, lockarg */
690 			   &sc->ciss_parent_dmat)) {
691 	ciss_printf(sc, "can't allocate parent DMA tag\n");
692 	return(ENOMEM);
693     }
694 
695     /*
696      * Create DMA tag for mapping buffers into adapter-addressable
697      * space.
698      */
699     if (bus_dma_tag_create(sc->ciss_parent_dmat, 	/* parent */
700 			   1, 0, 			/* alignment, boundary */
701 			   BUS_SPACE_MAXADDR,		/* lowaddr */
702 			   BUS_SPACE_MAXADDR, 		/* highaddr */
703 			   NULL, NULL, 			/* filter, filterarg */
704 			   MAXBSIZE, CISS_COMMAND_SG_LENGTH,	/* maxsize, nsegments */
705 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
706 			   0,				/* flags */
707 			   busdma_lock_mutex, &sc->ciss_mtx,	/* lockfunc, lockarg */
708 			   &sc->ciss_buffer_dmat)) {
709 	ciss_printf(sc, "can't allocate buffer DMA tag\n");
710 	return(ENOMEM);
711     }
712     return(0);
713 }
714 
715 /************************************************************************
716  * Wait for the adapter to come ready.
717  */
718 static int
719 ciss_wait_adapter(struct ciss_softc *sc)
720 {
721     int		i;
722 
723     debug_called(1);
724 
725     /*
726      * Wait for the adapter to come ready.
727      */
728     if (!(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY)) {
729 	ciss_printf(sc, "waiting for adapter to come ready...\n");
730 	for (i = 0; !(sc->ciss_cfg->active_method & CISS_TRANSPORT_METHOD_READY); i++) {
731 	    DELAY(1000000);	/* one second */
732 	    if (i > 30) {
733 		ciss_printf(sc, "timed out waiting for adapter to come ready\n");
734 		return(EIO);
735 	    }
736 	}
737     }
738     return(0);
739 }
740 
741 /************************************************************************
742  * Flush the adapter cache.
743  */
744 static int
745 ciss_flush_adapter(struct ciss_softc *sc)
746 {
747     struct ciss_request			*cr;
748     struct ciss_bmic_flush_cache	*cbfc;
749     int					error, command_status;
750 
751     debug_called(1);
752 
753     cr = NULL;
754     cbfc = NULL;
755 
756     /*
757      * Build a BMIC request to flush the cache.  We don't disable
758      * it, as we may be going to do more I/O (eg. we are emulating
759      * the Synchronise Cache command).
760      */
761     if ((cbfc = malloc(sizeof(*cbfc), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
762 	error = ENOMEM;
763 	goto out;
764     }
765     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_FLUSH_CACHE,
766 				       (void **)&cbfc, sizeof(*cbfc))) != 0)
767 	goto out;
768 
769     /*
770      * Submit the request and wait for it to complete.
771      */
772     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
773 	ciss_printf(sc, "error sending BMIC FLUSH_CACHE command (%d)\n", error);
774 	goto out;
775     }
776 
777     /*
778      * Check response.
779      */
780     ciss_report_request(cr, &command_status, NULL);
781     switch(command_status) {
782     case CISS_CMD_STATUS_SUCCESS:
783 	break;
784     default:
785 	ciss_printf(sc, "error flushing cache (%s)\n",
786 		    ciss_name_command_status(command_status));
787 	error = EIO;
788 	goto out;
789     }
790 
791 out:
792     if (cbfc != NULL)
793 	free(cbfc, CISS_MALLOC_CLASS);
794     if (cr != NULL)
795 	ciss_release_request(cr);
796     return(error);
797 }
798 
799 static void
800 ciss_soft_reset(struct ciss_softc *sc)
801 {
802     struct ciss_request		*cr = NULL;
803     struct ciss_command		*cc;
804     int				i, error = 0;
805 
806     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
807 	/* only reset proxy controllers */
808 	if (sc->ciss_controllers[i].physical.bus == 0)
809 	    continue;
810 
811 	if ((error = ciss_get_request(sc, &cr)) != 0)
812 	    break;
813 
814 	if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_SOFT_RESET,
815 					   NULL, 0)) != 0)
816 	    break;
817 
818 	cc = CISS_FIND_COMMAND(cr);
819 	cc->header.address = sc->ciss_controllers[i];
820 
821 	if ((error = ciss_synch_request(cr, 60 * 1000)) != 0)
822 	    break;
823 
824 	ciss_release_request(cr);
825     }
826 
827     if (error)
828 	ciss_printf(sc, "error resetting controller (%d)\n", error);
829 
830     if (cr != NULL)
831 	ciss_release_request(cr);
832 }
833 
834 /************************************************************************
835  * Allocate memory for the adapter command structures, initialise
836  * the request structures.
837  *
838  * Note that the entire set of commands are allocated in a single
839  * contiguous slab.
840  */
841 static int
842 ciss_init_requests(struct ciss_softc *sc)
843 {
844     struct ciss_request	*cr;
845     int			i;
846 
847     debug_called(1);
848 
849     /*
850      * Calculate the number of request structures/commands we are
851      * going to provide for this adapter.
852      */
853     sc->ciss_max_requests = min(CISS_MAX_REQUESTS, sc->ciss_cfg->max_outstanding_commands);
854 
855     if (bootverbose)
856 	ciss_printf(sc, "using %d of %d available commands\n",
857 		    sc->ciss_max_requests, sc->ciss_cfg->max_outstanding_commands);
858 
859     /*
860      * Create the DMA tag for commands.
861      */
862     if (bus_dma_tag_create(sc->ciss_parent_dmat,	/* parent */
863 			   1, 0, 			/* alignment, boundary */
864 			   BUS_SPACE_MAXADDR_32BIT,	/* lowaddr */
865 			   BUS_SPACE_MAXADDR, 		/* highaddr */
866 			   NULL, NULL, 			/* filter, filterarg */
867 			   CISS_COMMAND_ALLOC_SIZE *
868 			   sc->ciss_max_requests, 1,	/* maxsize, nsegments */
869 			   BUS_SPACE_MAXSIZE_32BIT,	/* maxsegsize */
870 			   BUS_DMA_ALLOCNOW,		/* flags */
871 			   NULL, NULL,			/* lockfunc, lockarg */
872 			   &sc->ciss_command_dmat)) {
873 	ciss_printf(sc, "can't allocate command DMA tag\n");
874 	return(ENOMEM);
875     }
876     /*
877      * Allocate memory and make it available for DMA.
878      */
879     if (bus_dmamem_alloc(sc->ciss_command_dmat, (void **)&sc->ciss_command,
880 			 BUS_DMA_NOWAIT, &sc->ciss_command_map)) {
881 	ciss_printf(sc, "can't allocate command memory\n");
882 	return(ENOMEM);
883     }
884     bus_dmamap_load(sc->ciss_command_dmat, sc->ciss_command_map, sc->ciss_command,
885 		    CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests,
886 		    ciss_command_map_helper, sc, 0);
887     bzero(sc->ciss_command, CISS_COMMAND_ALLOC_SIZE * sc->ciss_max_requests);
888 
889     /*
890      * Set up the request and command structures, push requests onto
891      * the free queue.
892      */
893     for (i = 1; i < sc->ciss_max_requests; i++) {
894 	cr = &sc->ciss_request[i];
895 	cr->cr_sc = sc;
896 	cr->cr_tag = i;
897 	bus_dmamap_create(sc->ciss_buffer_dmat, 0, &cr->cr_datamap);
898 	ciss_enqueue_free(cr);
899     }
900     return(0);
901 }
902 
903 static void
904 ciss_command_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
905 {
906     struct ciss_softc	*sc = (struct ciss_softc *)arg;
907 
908     sc->ciss_command_phys = segs->ds_addr;
909 }
910 
911 /************************************************************************
912  * Identify the adapter, print some information about it.
913  */
914 static int
915 ciss_identify_adapter(struct ciss_softc *sc)
916 {
917     struct ciss_request	*cr;
918     int			error, command_status;
919 
920     debug_called(1);
921 
922     cr = NULL;
923 
924     /*
925      * Get a request, allocate storage for the adapter data.
926      */
927     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_CTLR,
928 				       (void **)&sc->ciss_id,
929 				       sizeof(*sc->ciss_id))) != 0)
930 	goto out;
931 
932     /*
933      * Submit the request and wait for it to complete.
934      */
935     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
936 	ciss_printf(sc, "error sending BMIC ID_CTLR command (%d)\n", error);
937 	goto out;
938     }
939 
940     /*
941      * Check response.
942      */
943     ciss_report_request(cr, &command_status, NULL);
944     switch(command_status) {
945     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
946 	break;
947     case CISS_CMD_STATUS_DATA_UNDERRUN:
948     case CISS_CMD_STATUS_DATA_OVERRUN:
949 	ciss_printf(sc, "data over/underrun reading adapter information\n");
950     default:
951 	ciss_printf(sc, "error reading adapter information (%s)\n",
952 		    ciss_name_command_status(command_status));
953 	error = EIO;
954 	goto out;
955     }
956 
957     /* sanity-check reply */
958     if (!sc->ciss_id->big_map_supported) {
959 	ciss_printf(sc, "adapter does not support BIG_MAP\n");
960 	error = ENXIO;
961 	goto out;
962     }
963 
964 #if 0
965     /* XXX later revisions may not need this */
966     sc->ciss_flags |= CISS_FLAG_FAKE_SYNCH;
967 #endif
968 
969     /* XXX only really required for old 5300 adapters? */
970     sc->ciss_flags |= CISS_FLAG_BMIC_ABORT;
971 
972     /* print information */
973     if (bootverbose) {
974 #if 0	/* XXX proxy volumes??? */
975 	ciss_printf(sc, "  %d logical drive%s configured\n",
976 		    sc->ciss_id->configured_logical_drives,
977 		    (sc->ciss_id->configured_logical_drives == 1) ? "" : "s");
978 #endif
979 	ciss_printf(sc, "  firmware %4.4s\n", sc->ciss_id->running_firmware_revision);
980 	ciss_printf(sc, "  %d SCSI channels\n", sc->ciss_id->scsi_bus_count);
981 
982 	ciss_printf(sc, "  signature '%.4s'\n", sc->ciss_cfg->signature);
983 	ciss_printf(sc, "  valence %d\n", sc->ciss_cfg->valence);
984 	ciss_printf(sc, "  supported I/O methods 0x%b\n",
985 		    sc->ciss_cfg->supported_methods,
986 		    "\20\1READY\2simple\3performant\4MEMQ\n");
987 	ciss_printf(sc, "  active I/O method 0x%b\n",
988 		    sc->ciss_cfg->active_method, "\20\2simple\3performant\4MEMQ\n");
989 	ciss_printf(sc, "  4G page base 0x%08x\n",
990 		    sc->ciss_cfg->command_physlimit);
991 	ciss_printf(sc, "  interrupt coalesce delay %dus\n",
992 		    sc->ciss_cfg->interrupt_coalesce_delay);
993 	ciss_printf(sc, "  interrupt coalesce count %d\n",
994 		    sc->ciss_cfg->interrupt_coalesce_count);
995 	ciss_printf(sc, "  max outstanding commands %d\n",
996 		    sc->ciss_cfg->max_outstanding_commands);
997 	ciss_printf(sc, "  bus types 0x%b\n", sc->ciss_cfg->bus_types,
998 		    "\20\1ultra2\2ultra3\10fibre1\11fibre2\n");
999 	ciss_printf(sc, "  server name '%.16s'\n", sc->ciss_cfg->server_name);
1000 	ciss_printf(sc, "  heartbeat 0x%x\n", sc->ciss_cfg->heartbeat);
1001     }
1002 
1003 out:
1004     if (error) {
1005 	if (sc->ciss_id != NULL) {
1006 	    free(sc->ciss_id, CISS_MALLOC_CLASS);
1007 	    sc->ciss_id = NULL;
1008 	}
1009     }
1010     if (cr != NULL)
1011 	ciss_release_request(cr);
1012     return(error);
1013 }
1014 
1015 /************************************************************************
1016  * Helper routine for generating a list of logical and physical luns.
1017  */
1018 static struct ciss_lun_report *
1019 ciss_report_luns(struct ciss_softc *sc, int opcode, int nunits)
1020 {
1021     struct ciss_request		*cr;
1022     struct ciss_command		*cc;
1023     struct ciss_report_cdb	*crc;
1024     struct ciss_lun_report	*cll;
1025     int				command_status;
1026     int				report_size;
1027     int				error = 0;
1028 
1029     debug_called(1);
1030 
1031     cr = NULL;
1032     cll = NULL;
1033 
1034     /*
1035      * Get a request, allocate storage for the address list.
1036      */
1037     if ((error = ciss_get_request(sc, &cr)) != 0)
1038 	goto out;
1039     report_size = sizeof(*cll) + nunits * sizeof(union ciss_device_address);
1040     if ((cll = malloc(report_size, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
1041 	ciss_printf(sc, "can't allocate memory for lun report\n");
1042 	error = ENOMEM;
1043 	goto out;
1044     }
1045 
1046     /*
1047      * Build the Report Logical/Physical LUNs command.
1048      */
1049     cc = CISS_FIND_COMMAND(cr);
1050     cr->cr_data = cll;
1051     cr->cr_length = report_size;
1052     cr->cr_flags = CISS_REQ_DATAIN;
1053 
1054     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
1055     cc->header.address.physical.bus = 0;
1056     cc->header.address.physical.target = 0;
1057     cc->cdb.cdb_length = sizeof(*crc);
1058     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1059     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1060     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1061     cc->cdb.timeout = 30;	/* XXX better suggestions? */
1062 
1063     crc = (struct ciss_report_cdb *)&(cc->cdb.cdb[0]);
1064     bzero(crc, sizeof(*crc));
1065     crc->opcode = opcode;
1066     crc->length = htonl(report_size);			/* big-endian field */
1067     cll->list_size = htonl(report_size - sizeof(*cll));	/* big-endian field */
1068 
1069     /*
1070      * Submit the request and wait for it to complete.  (timeout
1071      * here should be much greater than above)
1072      */
1073     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1074 	ciss_printf(sc, "error sending %d LUN command (%d)\n", opcode, error);
1075 	goto out;
1076     }
1077 
1078     /*
1079      * Check response.  Note that data over/underrun is OK.
1080      */
1081     ciss_report_request(cr, &command_status, NULL);
1082     switch(command_status) {
1083     case CISS_CMD_STATUS_SUCCESS:	/* buffer right size */
1084     case CISS_CMD_STATUS_DATA_UNDERRUN:	/* buffer too large, not bad */
1085 	break;
1086     case CISS_CMD_STATUS_DATA_OVERRUN:
1087 	ciss_printf(sc, "WARNING: more units than driver limit (%d)\n",
1088 		    CISS_MAX_LOGICAL);
1089 	break;
1090     default:
1091 	ciss_printf(sc, "error detecting logical drive configuration (%s)\n",
1092 		    ciss_name_command_status(command_status));
1093 	error = EIO;
1094 	goto out;
1095     }
1096     ciss_release_request(cr);
1097     cr = NULL;
1098 
1099 out:
1100     if (cr != NULL)
1101 	ciss_release_request(cr);
1102     if (error && cll != NULL) {
1103 	free(cll, CISS_MALLOC_CLASS);
1104 	cll = NULL;
1105     }
1106     return(cll);
1107 }
1108 
1109 /************************************************************************
1110  * Find logical drives on the adapter.
1111  */
1112 static int
1113 ciss_init_logical(struct ciss_softc *sc)
1114 {
1115     struct ciss_lun_report	*cll;
1116     int				error = 0, i, j;
1117     int				ndrives;
1118 
1119     debug_called(1);
1120 
1121     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
1122 			   CISS_MAX_LOGICAL);
1123     if (cll == NULL) {
1124 	error = ENXIO;
1125 	goto out;
1126     }
1127 
1128     /* sanity-check reply */
1129     ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1130     if ((ndrives < 0) || (ndrives >= CISS_MAX_LOGICAL)) {
1131 	ciss_printf(sc, "adapter claims to report absurd number of logical drives (%d > %d)\n",
1132 		    ndrives, CISS_MAX_LOGICAL);
1133 	error = ENXIO;
1134 	goto out;
1135     }
1136 
1137     /*
1138      * Save logical drive information.
1139      */
1140     if (bootverbose) {
1141 	ciss_printf(sc, "%d logical drive%s\n",
1142 	    ndrives, (ndrives > 1 || ndrives == 0) ? "s" : "");
1143     }
1144 
1145     sc->ciss_logical =
1146 	malloc(sc->ciss_max_logical_bus * sizeof(struct ciss_ldrive *),
1147 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1148     if (sc->ciss_logical == NULL) {
1149 	error = ENXIO;
1150 	goto out;
1151     }
1152 
1153     for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1154 	sc->ciss_logical[i] =
1155 	    malloc(CISS_MAX_LOGICAL * sizeof(struct ciss_ldrive),
1156 		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1157 	if (sc->ciss_logical[i] == NULL) {
1158 	    error = ENXIO;
1159 	    goto out;
1160 	}
1161 
1162 	for (j = 0; j < CISS_MAX_LOGICAL; j++)
1163 	    sc->ciss_logical[i][j].cl_status = CISS_LD_NONEXISTENT;
1164     }
1165 
1166 
1167     for (i = 0; i < CISS_MAX_LOGICAL; i++) {
1168 	if (i < ndrives) {
1169 	    struct ciss_ldrive	*ld;
1170 	    int			bus, target;
1171 
1172 	    bus		= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
1173 	    target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
1174 	    ld		= &sc->ciss_logical[bus][target];
1175 
1176 	    ld->cl_address	= cll->lun[i];
1177 	    ld->cl_controller	= &sc->ciss_controllers[bus];
1178 	    if (ciss_identify_logical(sc, ld) != 0)
1179 		continue;
1180 	    /*
1181 	     * If the drive has had media exchanged, we should bring it online.
1182 	     */
1183 	    if (ld->cl_lstatus->media_exchanged)
1184 		ciss_accept_media(sc, ld);
1185 
1186 	}
1187     }
1188 
1189  out:
1190     if (cll != NULL)
1191 	free(cll, CISS_MALLOC_CLASS);
1192     return(error);
1193 }
1194 
1195 static int
1196 ciss_init_physical(struct ciss_softc *sc)
1197 {
1198     struct ciss_lun_report	*cll;
1199     int				error = 0, i;
1200     int				nphys;
1201     int				bus, target;
1202 
1203     debug_called(1);
1204 
1205     bus = 0;
1206     target = 0;
1207 
1208     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
1209 			   CISS_MAX_PHYSICAL);
1210     if (cll == NULL) {
1211 	error = ENXIO;
1212 	goto out;
1213     }
1214 
1215     nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1216 
1217     if (bootverbose) {
1218 	ciss_printf(sc, "%d physical device%s\n",
1219 	    nphys, (nphys > 1 || nphys == 0) ? "s" : "");
1220     }
1221 
1222     /*
1223      * Figure out the bus mapping.
1224      * Logical buses include both the local logical bus for local arrays and
1225      * proxy buses for remote arrays.  Physical buses are numbered by the
1226      * controller and represent physical buses that hold physical devices.
1227      * We shift these bus numbers so that everything fits into a single flat
1228      * numbering space for CAM.  Logical buses occupy the first 32 CAM bus
1229      * numbers, and the physical bus numbers are shifted to be above that.
1230      * This results in the various driver arrays being indexed as follows:
1231      *
1232      * ciss_controllers[] - indexed by logical bus
1233      * ciss_cam_sim[]     - indexed by both logical and physical, with physical
1234      *                      being shifted by 32.
1235      * ciss_logical[][]   - indexed by logical bus
1236      * ciss_physical[][]  - indexed by physical bus
1237      *
1238      * XXX This is getting more and more hackish.  CISS really doesn't play
1239      *     well with a standard SCSI model; devices are addressed via magic
1240      *     cookies, not via b/t/l addresses.  Since there is no way to store
1241      *     the cookie in the CAM device object, we have to keep these lookup
1242      *     tables handy so that the devices can be found quickly at the cost
1243      *     of wasting memory and having a convoluted lookup scheme.  This
1244      *     driver should probably be converted to block interface.
1245      */
1246     /*
1247      * If the L2 and L3 SCSI addresses are 0, this signifies a proxy
1248      * controller. A proxy controller is another physical controller
1249      * behind the primary PCI controller. We need to know about this
1250      * so that BMIC commands can be properly targeted.  There can be
1251      * proxy controllers attached to a single PCI controller, so
1252      * find the highest numbered one so the array can be properly
1253      * sized.
1254      */
1255     sc->ciss_max_logical_bus = 1;
1256     for (i = 0; i < nphys; i++) {
1257 	if (cll->lun[i].physical.extra_address == 0) {
1258 	    bus = cll->lun[i].physical.bus;
1259 	    sc->ciss_max_logical_bus = max(sc->ciss_max_logical_bus, bus) + 1;
1260 	} else {
1261 	    bus = CISS_EXTRA_BUS2(cll->lun[i].physical.extra_address);
1262 	    sc->ciss_max_physical_bus = max(sc->ciss_max_physical_bus, bus);
1263 	}
1264     }
1265 
1266     sc->ciss_controllers =
1267 	malloc(sc->ciss_max_logical_bus * sizeof (union ciss_device_address),
1268 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1269 
1270     if (sc->ciss_controllers == NULL) {
1271 	ciss_printf(sc, "Could not allocate memory for controller map\n");
1272 	error = ENOMEM;
1273 	goto out;
1274     }
1275 
1276     /* setup a map of controller addresses */
1277     for (i = 0; i < nphys; i++) {
1278 	if (cll->lun[i].physical.extra_address == 0) {
1279 	    sc->ciss_controllers[cll->lun[i].physical.bus] = cll->lun[i];
1280 	}
1281     }
1282 
1283     sc->ciss_physical =
1284 	malloc(sc->ciss_max_physical_bus * sizeof(struct ciss_pdrive *),
1285 	       CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1286     if (sc->ciss_physical == NULL) {
1287 	ciss_printf(sc, "Could not allocate memory for physical device map\n");
1288 	error = ENOMEM;
1289 	goto out;
1290     }
1291 
1292     for (i = 0; i < sc->ciss_max_physical_bus; i++) {
1293 	sc->ciss_physical[i] =
1294 	    malloc(sizeof(struct ciss_pdrive) * CISS_MAX_PHYSTGT,
1295 		   CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
1296 	if (sc->ciss_physical[i] == NULL) {
1297 	    ciss_printf(sc, "Could not allocate memory for target map\n");
1298 	    error = ENOMEM;
1299 	    goto out;
1300 	}
1301     }
1302 
1303     ciss_filter_physical(sc, cll);
1304 
1305 out:
1306     if (cll != NULL)
1307 	free(cll, CISS_MALLOC_CLASS);
1308 
1309     return(error);
1310 }
1311 
1312 static int
1313 ciss_filter_physical(struct ciss_softc *sc, struct ciss_lun_report *cll)
1314 {
1315     u_int32_t ea;
1316     int i, nphys;
1317     int	bus, target;
1318 
1319     nphys = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
1320     for (i = 0; i < nphys; i++) {
1321 	if (cll->lun[i].physical.extra_address == 0)
1322 	    continue;
1323 
1324 	/*
1325 	 * Filter out devices that we don't want.  Level 3 LUNs could
1326 	 * probably be supported, but the docs don't give enough of a
1327 	 * hint to know how.
1328 	 *
1329 	 * The mode field of the physical address is likely set to have
1330 	 * hard disks masked out.  Honor it unless the user has overridden
1331 	 * us with the tunable.  We also munge the inquiry data for these
1332 	 * disks so that they only show up as passthrough devices.  Keeping
1333 	 * them visible in this fashion is useful for doing things like
1334 	 * flashing firmware.
1335 	 */
1336 	ea = cll->lun[i].physical.extra_address;
1337 	if ((CISS_EXTRA_BUS3(ea) != 0) || (CISS_EXTRA_TARGET3(ea) != 0) ||
1338 	    (CISS_EXTRA_MODE2(ea) == 0x3))
1339 	    continue;
1340 	if ((ciss_expose_hidden_physical == 0) &&
1341 	   (cll->lun[i].physical.mode == CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL))
1342 	    continue;
1343 
1344 	/*
1345 	 * Note: CISS firmware numbers physical busses starting at '1', not
1346 	 *       '0'.  This numbering is internal to the firmware and is only
1347 	 *       used as a hint here.
1348 	 */
1349 	bus = CISS_EXTRA_BUS2(ea) - 1;
1350 	target = CISS_EXTRA_TARGET2(ea);
1351 	sc->ciss_physical[bus][target].cp_address = cll->lun[i];
1352 	sc->ciss_physical[bus][target].cp_online = 1;
1353     }
1354 
1355     return (0);
1356 }
1357 
1358 static int
1359 ciss_inquiry_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1360 {
1361     struct ciss_request			*cr;
1362     struct ciss_command			*cc;
1363     struct scsi_inquiry			*inq;
1364     int					error;
1365     int					command_status;
1366 
1367     cr = NULL;
1368 
1369     bzero(&ld->cl_geometry, sizeof(ld->cl_geometry));
1370 
1371     if ((error = ciss_get_request(sc, &cr)) != 0)
1372 	goto out;
1373 
1374     cc = CISS_FIND_COMMAND(cr);
1375     cr->cr_data = &ld->cl_geometry;
1376     cr->cr_length = sizeof(ld->cl_geometry);
1377     cr->cr_flags = CISS_REQ_DATAIN;
1378 
1379     cc->header.address = ld->cl_address;
1380     cc->cdb.cdb_length = 6;
1381     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
1382     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
1383     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
1384     cc->cdb.timeout = 30;
1385 
1386     inq = (struct scsi_inquiry *)&(cc->cdb.cdb[0]);
1387     inq->opcode = INQUIRY;
1388     inq->byte2 = SI_EVPD;
1389     inq->page_code = CISS_VPD_LOGICAL_DRIVE_GEOMETRY;
1390     inq->length = sizeof(ld->cl_geometry);
1391 
1392     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1393 	ciss_printf(sc, "error getting geometry (%d)\n", error);
1394 	goto out;
1395     }
1396 
1397     ciss_report_request(cr, &command_status, NULL);
1398     switch(command_status) {
1399     case CISS_CMD_STATUS_SUCCESS:
1400     case CISS_CMD_STATUS_DATA_UNDERRUN:
1401 	break;
1402     case CISS_CMD_STATUS_DATA_OVERRUN:
1403 	ciss_printf(sc, "WARNING: Data overrun\n");
1404 	break;
1405     default:
1406 	ciss_printf(sc, "Error detecting logical drive geometry (%s)\n",
1407 		    ciss_name_command_status(command_status));
1408 	break;
1409     }
1410 
1411 out:
1412     if (cr != NULL)
1413 	ciss_release_request(cr);
1414     return(error);
1415 }
1416 /************************************************************************
1417  * Identify a logical drive, initialise state related to it.
1418  */
1419 static int
1420 ciss_identify_logical(struct ciss_softc *sc, struct ciss_ldrive *ld)
1421 {
1422     struct ciss_request		*cr;
1423     struct ciss_command		*cc;
1424     struct ciss_bmic_cdb	*cbc;
1425     int				error, command_status;
1426 
1427     debug_called(1);
1428 
1429     cr = NULL;
1430 
1431     /*
1432      * Build a BMIC request to fetch the drive ID.
1433      */
1434     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LDRIVE,
1435 				       (void **)&ld->cl_ldrive,
1436 				       sizeof(*ld->cl_ldrive))) != 0)
1437 	goto out;
1438     cc = CISS_FIND_COMMAND(cr);
1439     cc->header.address = *ld->cl_controller;	/* target controller */
1440     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1441     cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1442 
1443     /*
1444      * Submit the request and wait for it to complete.
1445      */
1446     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1447 	ciss_printf(sc, "error sending BMIC LDRIVE command (%d)\n", error);
1448 	goto out;
1449     }
1450 
1451     /*
1452      * Check response.
1453      */
1454     ciss_report_request(cr, &command_status, NULL);
1455     switch(command_status) {
1456     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
1457 	break;
1458     case CISS_CMD_STATUS_DATA_UNDERRUN:
1459     case CISS_CMD_STATUS_DATA_OVERRUN:
1460 	ciss_printf(sc, "data over/underrun reading logical drive ID\n");
1461     default:
1462 	ciss_printf(sc, "error reading logical drive ID (%s)\n",
1463 		    ciss_name_command_status(command_status));
1464 	error = EIO;
1465 	goto out;
1466     }
1467     ciss_release_request(cr);
1468     cr = NULL;
1469 
1470     /*
1471      * Build a CISS BMIC command to get the logical drive status.
1472      */
1473     if ((error = ciss_get_ldrive_status(sc, ld)) != 0)
1474 	goto out;
1475 
1476     /*
1477      * Get the logical drive geometry.
1478      */
1479     if ((error = ciss_inquiry_logical(sc, ld)) != 0)
1480 	goto out;
1481 
1482     /*
1483      * Print the drive's basic characteristics.
1484      */
1485     if (bootverbose) {
1486 	ciss_printf(sc, "logical drive (b%dt%d): %s, %dMB ",
1487 		    CISS_LUN_TO_BUS(ld->cl_address.logical.lun),
1488 		    CISS_LUN_TO_TARGET(ld->cl_address.logical.lun),
1489 		    ciss_name_ldrive_org(ld->cl_ldrive->fault_tolerance),
1490 		    ((ld->cl_ldrive->blocks_available / (1024 * 1024)) *
1491 		     ld->cl_ldrive->block_size));
1492 
1493 	ciss_print_ldrive(sc, ld);
1494     }
1495 out:
1496     if (error != 0) {
1497 	/* make the drive not-exist */
1498 	ld->cl_status = CISS_LD_NONEXISTENT;
1499 	if (ld->cl_ldrive != NULL) {
1500 	    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
1501 	    ld->cl_ldrive = NULL;
1502 	}
1503 	if (ld->cl_lstatus != NULL) {
1504 	    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
1505 	    ld->cl_lstatus = NULL;
1506 	}
1507     }
1508     if (cr != NULL)
1509 	ciss_release_request(cr);
1510 
1511     return(error);
1512 }
1513 
1514 /************************************************************************
1515  * Get status for a logical drive.
1516  *
1517  * XXX should we also do this in response to Test Unit Ready?
1518  */
1519 static int
1520 ciss_get_ldrive_status(struct ciss_softc *sc,  struct ciss_ldrive *ld)
1521 {
1522     struct ciss_request		*cr;
1523     struct ciss_command		*cc;
1524     struct ciss_bmic_cdb	*cbc;
1525     int				error, command_status;
1526 
1527     /*
1528      * Build a CISS BMIC command to get the logical drive status.
1529      */
1530     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ID_LSTATUS,
1531 				       (void **)&ld->cl_lstatus,
1532 				       sizeof(*ld->cl_lstatus))) != 0)
1533 	goto out;
1534     cc = CISS_FIND_COMMAND(cr);
1535     cc->header.address = *ld->cl_controller;	/* target controller */
1536     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1537     cbc->log_drive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1538 
1539     /*
1540      * Submit the request and wait for it to complete.
1541      */
1542     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1543 	ciss_printf(sc, "error sending BMIC LSTATUS command (%d)\n", error);
1544 	goto out;
1545     }
1546 
1547     /*
1548      * Check response.
1549      */
1550     ciss_report_request(cr, &command_status, NULL);
1551     switch(command_status) {
1552     case CISS_CMD_STATUS_SUCCESS:		/* buffer right size */
1553 	break;
1554     case CISS_CMD_STATUS_DATA_UNDERRUN:
1555     case CISS_CMD_STATUS_DATA_OVERRUN:
1556 	ciss_printf(sc, "data over/underrun reading logical drive status\n");
1557     default:
1558 	ciss_printf(sc, "error reading logical drive status (%s)\n",
1559 		    ciss_name_command_status(command_status));
1560 	error = EIO;
1561 	goto out;
1562     }
1563 
1564     /*
1565      * Set the drive's summary status based on the returned status.
1566      *
1567      * XXX testing shows that a failed JBOD drive comes back at next
1568      * boot in "queued for expansion" mode.  WTF?
1569      */
1570     ld->cl_status = ciss_decode_ldrive_status(ld->cl_lstatus->status);
1571 
1572 out:
1573     if (cr != NULL)
1574 	ciss_release_request(cr);
1575     return(error);
1576 }
1577 
1578 /************************************************************************
1579  * Notify the adapter of a config update.
1580  */
1581 static int
1582 ciss_update_config(struct ciss_softc *sc)
1583 {
1584     int		i;
1585 
1586     debug_called(1);
1587 
1588     CISS_TL_SIMPLE_WRITE(sc, CISS_TL_SIMPLE_IDBR, CISS_TL_SIMPLE_IDBR_CFG_TABLE);
1589     for (i = 0; i < 1000; i++) {
1590 	if (!(CISS_TL_SIMPLE_READ(sc, CISS_TL_SIMPLE_IDBR) &
1591 	      CISS_TL_SIMPLE_IDBR_CFG_TABLE)) {
1592 	    return(0);
1593 	}
1594 	DELAY(1000);
1595     }
1596     return(1);
1597 }
1598 
1599 /************************************************************************
1600  * Accept new media into a logical drive.
1601  *
1602  * XXX The drive has previously been offline; it would be good if we
1603  *     could make sure it's not open right now.
1604  */
1605 static int
1606 ciss_accept_media(struct ciss_softc *sc, struct ciss_ldrive *ld)
1607 {
1608     struct ciss_request		*cr;
1609     struct ciss_command		*cc;
1610     struct ciss_bmic_cdb	*cbc;
1611     int				command_status;
1612     int				error = 0, ldrive;
1613 
1614     ldrive = CISS_LUN_TO_TARGET(ld->cl_address.logical.lun);
1615 
1616     debug(0, "bringing logical drive %d back online");
1617 
1618     /*
1619      * Build a CISS BMIC command to bring the drive back online.
1620      */
1621     if ((error = ciss_get_bmic_request(sc, &cr, CISS_BMIC_ACCEPT_MEDIA,
1622 				       NULL, 0)) != 0)
1623 	goto out;
1624     cc = CISS_FIND_COMMAND(cr);
1625     cc->header.address = *ld->cl_controller;	/* target controller */
1626     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
1627     cbc->log_drive = ldrive;
1628 
1629     /*
1630      * Submit the request and wait for it to complete.
1631      */
1632     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
1633 	ciss_printf(sc, "error sending BMIC ACCEPT MEDIA command (%d)\n", error);
1634 	goto out;
1635     }
1636 
1637     /*
1638      * Check response.
1639      */
1640     ciss_report_request(cr, &command_status, NULL);
1641     switch(command_status) {
1642     case CISS_CMD_STATUS_SUCCESS:		/* all OK */
1643 	/* we should get a logical drive status changed event here */
1644 	break;
1645     default:
1646 	ciss_printf(cr->cr_sc, "error accepting media into failed logical drive (%s)\n",
1647 		    ciss_name_command_status(command_status));
1648 	break;
1649     }
1650 
1651 out:
1652     if (cr != NULL)
1653 	ciss_release_request(cr);
1654     return(error);
1655 }
1656 
1657 /************************************************************************
1658  * Release adapter resources.
1659  */
1660 static void
1661 ciss_free(struct ciss_softc *sc)
1662 {
1663     struct ciss_request *cr;
1664     int			i, j;
1665 
1666     debug_called(1);
1667 
1668     /* we're going away */
1669     sc->ciss_flags |= CISS_FLAG_ABORTING;
1670 
1671     /* terminate the periodic heartbeat routine */
1672     callout_stop(&sc->ciss_periodic);
1673 
1674     /* cancel the Event Notify chain */
1675     ciss_notify_abort(sc);
1676 
1677     ciss_kill_notify_thread(sc);
1678 
1679     /* disconnect from CAM */
1680     if (sc->ciss_cam_sim) {
1681 	for (i = 0; i < sc->ciss_max_logical_bus; i++) {
1682 	    if (sc->ciss_cam_sim[i]) {
1683 		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1684 		cam_sim_free(sc->ciss_cam_sim[i], 0);
1685 	    }
1686 	}
1687 	for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
1688 	     CISS_PHYSICAL_BASE; i++) {
1689 	    if (sc->ciss_cam_sim[i]) {
1690 		xpt_bus_deregister(cam_sim_path(sc->ciss_cam_sim[i]));
1691 		cam_sim_free(sc->ciss_cam_sim[i], 0);
1692 	    }
1693 	}
1694 	free(sc->ciss_cam_sim, CISS_MALLOC_CLASS);
1695     }
1696     if (sc->ciss_cam_devq)
1697 	cam_simq_free(sc->ciss_cam_devq);
1698 
1699     /* remove the control device */
1700     mtx_unlock(&sc->ciss_mtx);
1701     if (sc->ciss_dev_t != NULL)
1702 	destroy_dev(sc->ciss_dev_t);
1703 
1704     /* Final cleanup of the callout. */
1705     callout_drain(&sc->ciss_periodic);
1706     mtx_destroy(&sc->ciss_mtx);
1707 
1708     /* free the controller data */
1709     if (sc->ciss_id != NULL)
1710 	free(sc->ciss_id, CISS_MALLOC_CLASS);
1711 
1712     /* release I/O resources */
1713     if (sc->ciss_regs_resource != NULL)
1714 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1715 			     sc->ciss_regs_rid, sc->ciss_regs_resource);
1716     if (sc->ciss_cfg_resource != NULL)
1717 	bus_release_resource(sc->ciss_dev, SYS_RES_MEMORY,
1718 			     sc->ciss_cfg_rid, sc->ciss_cfg_resource);
1719     if (sc->ciss_intr != NULL)
1720 	bus_teardown_intr(sc->ciss_dev, sc->ciss_irq_resource, sc->ciss_intr);
1721     if (sc->ciss_irq_resource != NULL)
1722 	bus_release_resource(sc->ciss_dev, SYS_RES_IRQ,
1723 			     sc->ciss_irq_rid, sc->ciss_irq_resource);
1724 
1725     /* destroy DMA tags */
1726     if (sc->ciss_parent_dmat)
1727 	bus_dma_tag_destroy(sc->ciss_parent_dmat);
1728 
1729     while ((cr = ciss_dequeue_free(sc)) != NULL)
1730 	bus_dmamap_destroy(sc->ciss_buffer_dmat, cr->cr_datamap);
1731     if (sc->ciss_buffer_dmat)
1732 	bus_dma_tag_destroy(sc->ciss_buffer_dmat);
1733 
1734     /* destroy command memory and DMA tag */
1735     if (sc->ciss_command != NULL) {
1736 	bus_dmamap_unload(sc->ciss_command_dmat, sc->ciss_command_map);
1737 	bus_dmamem_free(sc->ciss_command_dmat, sc->ciss_command, sc->ciss_command_map);
1738     }
1739     if (sc->ciss_command_dmat)
1740 	bus_dma_tag_destroy(sc->ciss_command_dmat);
1741 
1742     if (sc->ciss_logical) {
1743 	for (i = 0; i <= sc->ciss_max_logical_bus; i++) {
1744 	    for (j = 0; j < CISS_MAX_LOGICAL; j++) {
1745 		if (sc->ciss_logical[i][j].cl_ldrive)
1746 		    free(sc->ciss_logical[i][j].cl_ldrive, CISS_MALLOC_CLASS);
1747 		if (sc->ciss_logical[i][j].cl_lstatus)
1748 		    free(sc->ciss_logical[i][j].cl_lstatus, CISS_MALLOC_CLASS);
1749 	    }
1750 	    free(sc->ciss_logical[i], CISS_MALLOC_CLASS);
1751 	}
1752 	free(sc->ciss_logical, CISS_MALLOC_CLASS);
1753     }
1754 
1755     if (sc->ciss_physical) {
1756 	for (i = 0; i < sc->ciss_max_physical_bus; i++)
1757 	    free(sc->ciss_physical[i], CISS_MALLOC_CLASS);
1758 	free(sc->ciss_physical, CISS_MALLOC_CLASS);
1759     }
1760 
1761     if (sc->ciss_controllers)
1762 	free(sc->ciss_controllers, CISS_MALLOC_CLASS);
1763 
1764 }
1765 
1766 /************************************************************************
1767  * Give a command to the adapter.
1768  *
1769  * Note that this uses the simple transport layer directly.  If we
1770  * want to add support for other layers, we'll need a switch of some
1771  * sort.
1772  *
1773  * Note that the simple transport layer has no way of refusing a
1774  * command; we only have as many request structures as the adapter
1775  * supports commands, so we don't have to check (this presumes that
1776  * the adapter can handle commands as fast as we throw them at it).
1777  */
1778 static int
1779 ciss_start(struct ciss_request *cr)
1780 {
1781     struct ciss_command	*cc;	/* XXX debugging only */
1782     int			error;
1783 
1784     cc = CISS_FIND_COMMAND(cr);
1785     debug(2, "post command %d tag %d ", cr->cr_tag, cc->header.host_tag);
1786 
1787     /*
1788      * Map the request's data.
1789      */
1790     if ((error = ciss_map_request(cr)))
1791 	return(error);
1792 
1793 #if 0
1794     ciss_print_request(cr);
1795 #endif
1796 
1797     return(0);
1798 }
1799 
1800 /************************************************************************
1801  * Fetch completed request(s) from the adapter, queue them for
1802  * completion handling.
1803  *
1804  * Note that this uses the simple transport layer directly.  If we
1805  * want to add support for other layers, we'll need a switch of some
1806  * sort.
1807  *
1808  * Note that the simple transport mechanism does not require any
1809  * reentrancy protection; the OPQ read is atomic.  If there is a
1810  * chance of a race with something else that might move the request
1811  * off the busy list, then we will have to lock against that
1812  * (eg. timeouts, etc.)
1813  */
1814 static void
1815 ciss_done(struct ciss_softc *sc)
1816 {
1817     struct ciss_request	*cr;
1818     struct ciss_command	*cc;
1819     u_int32_t		tag, index;
1820     int			complete;
1821 
1822     debug_called(3);
1823 
1824     /*
1825      * Loop quickly taking requests from the adapter and moving them
1826      * from the busy queue to the completed queue.
1827      */
1828     complete = 0;
1829     for (;;) {
1830 
1831 	/* see if the OPQ contains anything */
1832 	if (!CISS_TL_SIMPLE_OPQ_INTERRUPT(sc))
1833 	    break;
1834 
1835 	tag = CISS_TL_SIMPLE_FETCH_CMD(sc);
1836 	if (tag == CISS_TL_SIMPLE_OPQ_EMPTY)
1837 	    break;
1838 	index = tag >> 2;
1839 	debug(2, "completed command %d%s", index,
1840 	      (tag & CISS_HDR_HOST_TAG_ERROR) ? " with error" : "");
1841 	if (index >= sc->ciss_max_requests) {
1842 	    ciss_printf(sc, "completed invalid request %d (0x%x)\n", index, tag);
1843 	    continue;
1844 	}
1845 	cr = &(sc->ciss_request[index]);
1846 	cc = CISS_FIND_COMMAND(cr);
1847 	cc->header.host_tag = tag;	/* not updated by adapter */
1848 	if (ciss_remove_busy(cr)) {
1849 	    /* assume this is garbage out of the adapter */
1850 	    ciss_printf(sc, "completed nonbusy request %d\n", index);
1851 	} else {
1852 	    ciss_enqueue_complete(cr);
1853 	}
1854 	complete = 1;
1855     }
1856 
1857     /*
1858      * Invoke completion processing.  If we can defer this out of
1859      * interrupt context, that'd be good.
1860      */
1861     if (complete)
1862 	ciss_complete(sc);
1863 }
1864 
1865 /************************************************************************
1866  * Take an interrupt from the adapter.
1867  */
1868 static void
1869 ciss_intr(void *arg)
1870 {
1871     struct ciss_softc	*sc = (struct ciss_softc *)arg;
1872 
1873     /*
1874      * The only interrupt we recognise indicates that there are
1875      * entries in the outbound post queue.
1876      */
1877     mtx_lock(&sc->ciss_mtx);
1878     ciss_done(sc);
1879     mtx_unlock(&sc->ciss_mtx);
1880 }
1881 
1882 /************************************************************************
1883  * Process completed requests.
1884  *
1885  * Requests can be completed in three fashions:
1886  *
1887  * - by invoking a callback function (cr_complete is non-null)
1888  * - by waking up a sleeper (cr_flags has CISS_REQ_SLEEP set)
1889  * - by clearing the CISS_REQ_POLL flag in interrupt/timeout context
1890  */
1891 static void
1892 ciss_complete(struct ciss_softc *sc)
1893 {
1894     struct ciss_request	*cr;
1895 
1896     debug_called(2);
1897 
1898     /*
1899      * Loop taking requests off the completed queue and performing
1900      * completion processing on them.
1901      */
1902     for (;;) {
1903 	if ((cr = ciss_dequeue_complete(sc)) == NULL)
1904 	    break;
1905 	ciss_unmap_request(cr);
1906 
1907 	/*
1908 	 * If the request has a callback, invoke it.
1909 	 */
1910 	if (cr->cr_complete != NULL) {
1911 	    cr->cr_complete(cr);
1912 	    continue;
1913 	}
1914 
1915 	/*
1916 	 * If someone is sleeping on this request, wake them up.
1917 	 */
1918 	if (cr->cr_flags & CISS_REQ_SLEEP) {
1919 	    cr->cr_flags &= ~CISS_REQ_SLEEP;
1920 	    wakeup(cr);
1921 	    continue;
1922 	}
1923 
1924 	/*
1925 	 * If someone is polling this request for completion, signal.
1926 	 */
1927 	if (cr->cr_flags & CISS_REQ_POLL) {
1928 	    cr->cr_flags &= ~CISS_REQ_POLL;
1929 	    continue;
1930 	}
1931 
1932 	/*
1933 	 * Give up and throw the request back on the free queue.  This
1934 	 * should never happen; resources will probably be lost.
1935 	 */
1936 	ciss_printf(sc, "WARNING: completed command with no submitter\n");
1937 	ciss_enqueue_free(cr);
1938     }
1939 }
1940 
1941 /************************************************************************
1942  * Report on the completion status of a request, and pass back SCSI
1943  * and command status values.
1944  */
1945 static int
1946 ciss_report_request(struct ciss_request *cr, int *command_status, int *scsi_status)
1947 {
1948     struct ciss_command		*cc;
1949     struct ciss_error_info	*ce;
1950 
1951     debug_called(2);
1952 
1953     cc = CISS_FIND_COMMAND(cr);
1954     ce = (struct ciss_error_info *)&(cc->sg[0]);
1955 
1956     /*
1957      * We don't consider data under/overrun an error for the Report
1958      * Logical/Physical LUNs commands.
1959      */
1960     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) &&
1961 	((ce->command_status == CISS_CMD_STATUS_DATA_OVERRUN) ||
1962 	 (ce->command_status == CISS_CMD_STATUS_DATA_UNDERRUN)) &&
1963 	((cc->cdb.cdb[0] == CISS_OPCODE_REPORT_LOGICAL_LUNS) ||
1964 	 (cc->cdb.cdb[0] == CISS_OPCODE_REPORT_PHYSICAL_LUNS) ||
1965 	 (cc->cdb.cdb[0] == INQUIRY))) {
1966 	cc->header.host_tag &= ~CISS_HDR_HOST_TAG_ERROR;
1967 	debug(2, "ignoring irrelevant under/overrun error");
1968     }
1969 
1970     /*
1971      * Check the command's error bit, if clear, there's no status and
1972      * everything is OK.
1973      */
1974     if (!(cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR)) {
1975 	if (scsi_status != NULL)
1976 	    *scsi_status = SCSI_STATUS_OK;
1977 	if (command_status != NULL)
1978 	    *command_status = CISS_CMD_STATUS_SUCCESS;
1979 	return(0);
1980     } else {
1981 	if (command_status != NULL)
1982 	    *command_status = ce->command_status;
1983 	if (scsi_status != NULL) {
1984 	    if (ce->command_status == CISS_CMD_STATUS_TARGET_STATUS) {
1985 		*scsi_status = ce->scsi_status;
1986 	    } else {
1987 		*scsi_status = -1;
1988 	    }
1989 	}
1990 	if (bootverbose)
1991 	    ciss_printf(cr->cr_sc, "command status 0x%x (%s) scsi status 0x%x\n",
1992 			ce->command_status, ciss_name_command_status(ce->command_status),
1993 			ce->scsi_status);
1994 	if (ce->command_status == CISS_CMD_STATUS_INVALID_COMMAND) {
1995 	    ciss_printf(cr->cr_sc, "invalid command, offense size %d at %d, value 0x%x\n",
1996 			ce->additional_error_info.invalid_command.offense_size,
1997 			ce->additional_error_info.invalid_command.offense_offset,
1998 			ce->additional_error_info.invalid_command.offense_value);
1999 	}
2000     }
2001 #if 0
2002     ciss_print_request(cr);
2003 #endif
2004     return(1);
2005 }
2006 
2007 /************************************************************************
2008  * Issue a request and don't return until it's completed.
2009  *
2010  * Depending on adapter status, we may poll or sleep waiting for
2011  * completion.
2012  */
2013 static int
2014 ciss_synch_request(struct ciss_request *cr, int timeout)
2015 {
2016     if (cr->cr_sc->ciss_flags & CISS_FLAG_RUNNING) {
2017 	return(ciss_wait_request(cr, timeout));
2018     } else {
2019 	return(ciss_poll_request(cr, timeout));
2020     }
2021 }
2022 
2023 /************************************************************************
2024  * Issue a request and poll for completion.
2025  *
2026  * Timeout in milliseconds.
2027  */
2028 static int
2029 ciss_poll_request(struct ciss_request *cr, int timeout)
2030 {
2031     int		error;
2032 
2033     debug_called(2);
2034 
2035     cr->cr_flags |= CISS_REQ_POLL;
2036     if ((error = ciss_start(cr)) != 0)
2037 	return(error);
2038 
2039     do {
2040 	ciss_done(cr->cr_sc);
2041 	if (!(cr->cr_flags & CISS_REQ_POLL))
2042 	    return(0);
2043 	DELAY(1000);
2044     } while (timeout-- >= 0);
2045     return(EWOULDBLOCK);
2046 }
2047 
2048 /************************************************************************
2049  * Issue a request and sleep waiting for completion.
2050  *
2051  * Timeout in milliseconds.  Note that a spurious wakeup will reset
2052  * the timeout.
2053  */
2054 static int
2055 ciss_wait_request(struct ciss_request *cr, int timeout)
2056 {
2057     int		s, error;
2058 
2059     debug_called(2);
2060 
2061     cr->cr_flags |= CISS_REQ_SLEEP;
2062     if ((error = ciss_start(cr)) != 0)
2063 	return(error);
2064 
2065     s = splcam();
2066     while ((cr->cr_flags & CISS_REQ_SLEEP) && (error != EWOULDBLOCK)) {
2067 	error = msleep(cr, &cr->cr_sc->ciss_mtx, PRIBIO, "cissREQ", (timeout * hz) / 1000);
2068     }
2069     splx(s);
2070     return(error);
2071 }
2072 
2073 #if 0
2074 /************************************************************************
2075  * Abort a request.  Note that a potential exists here to race the
2076  * request being completed; the caller must deal with this.
2077  */
2078 static int
2079 ciss_abort_request(struct ciss_request *ar)
2080 {
2081     struct ciss_request		*cr;
2082     struct ciss_command		*cc;
2083     struct ciss_message_cdb	*cmc;
2084     int				error;
2085 
2086     debug_called(1);
2087 
2088     /* get a request */
2089     if ((error = ciss_get_request(ar->cr_sc, &cr)) != 0)
2090 	return(error);
2091 
2092     /* build the abort command */
2093     cc = CISS_FIND_COMMAND(cr);
2094     cc->header.address.mode.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;	/* addressing? */
2095     cc->header.address.physical.target = 0;
2096     cc->header.address.physical.bus = 0;
2097     cc->cdb.cdb_length = sizeof(*cmc);
2098     cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
2099     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2100     cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2101     cc->cdb.timeout = 30;
2102 
2103     cmc = (struct ciss_message_cdb *)&(cc->cdb.cdb[0]);
2104     cmc->opcode = CISS_OPCODE_MESSAGE_ABORT;
2105     cmc->type = CISS_MESSAGE_ABORT_TASK;
2106     cmc->abort_tag = ar->cr_tag;	/* endianness?? */
2107 
2108     /*
2109      * Send the request and wait for a response.  If we believe we
2110      * aborted the request OK, clear the flag that indicates it's
2111      * running.
2112      */
2113     error = ciss_synch_request(cr, 35 * 1000);
2114     if (!error)
2115 	error = ciss_report_request(cr, NULL, NULL);
2116     ciss_release_request(cr);
2117 
2118     return(error);
2119 }
2120 #endif
2121 
2122 
2123 /************************************************************************
2124  * Fetch and initialise a request
2125  */
2126 static int
2127 ciss_get_request(struct ciss_softc *sc, struct ciss_request **crp)
2128 {
2129     struct ciss_request *cr;
2130 
2131     debug_called(2);
2132 
2133     /*
2134      * Get a request and clean it up.
2135      */
2136     if ((cr = ciss_dequeue_free(sc)) == NULL)
2137 	return(ENOMEM);
2138 
2139     cr->cr_data = NULL;
2140     cr->cr_flags = 0;
2141     cr->cr_complete = NULL;
2142     cr->cr_private = NULL;
2143 
2144     ciss_preen_command(cr);
2145     *crp = cr;
2146     return(0);
2147 }
2148 
2149 static void
2150 ciss_preen_command(struct ciss_request *cr)
2151 {
2152     struct ciss_command	*cc;
2153     u_int32_t		cmdphys;
2154 
2155     /*
2156      * Clean up the command structure.
2157      *
2158      * Note that we set up the error_info structure here, since the
2159      * length can be overwritten by any command.
2160      */
2161     cc = CISS_FIND_COMMAND(cr);
2162     cc->header.sg_in_list = 0;		/* kinda inefficient this way */
2163     cc->header.sg_total = 0;
2164     cc->header.host_tag = cr->cr_tag << 2;
2165     cc->header.host_tag_zeroes = 0;
2166     cmdphys = CISS_FIND_COMMANDPHYS(cr);
2167     cc->error_info.error_info_address = cmdphys + sizeof(struct ciss_command);
2168     cc->error_info.error_info_length = CISS_COMMAND_ALLOC_SIZE - sizeof(struct ciss_command);
2169 }
2170 
2171 /************************************************************************
2172  * Release a request to the free list.
2173  */
2174 static void
2175 ciss_release_request(struct ciss_request *cr)
2176 {
2177     struct ciss_softc	*sc;
2178 
2179     debug_called(2);
2180 
2181     sc = cr->cr_sc;
2182 
2183     /* release the request to the free queue */
2184     ciss_requeue_free(cr);
2185 }
2186 
2187 /************************************************************************
2188  * Allocate a request that will be used to send a BMIC command.  Do some
2189  * of the common setup here to avoid duplicating it everywhere else.
2190  */
2191 static int
2192 ciss_get_bmic_request(struct ciss_softc *sc, struct ciss_request **crp,
2193 		      int opcode, void **bufp, size_t bufsize)
2194 {
2195     struct ciss_request		*cr;
2196     struct ciss_command		*cc;
2197     struct ciss_bmic_cdb	*cbc;
2198     void			*buf;
2199     int				error;
2200     int				dataout;
2201 
2202     debug_called(2);
2203 
2204     cr = NULL;
2205     buf = NULL;
2206 
2207     /*
2208      * Get a request.
2209      */
2210     if ((error = ciss_get_request(sc, &cr)) != 0)
2211 	goto out;
2212 
2213     /*
2214      * Allocate data storage if requested, determine the data direction.
2215      */
2216     dataout = 0;
2217     if ((bufsize > 0) && (bufp != NULL)) {
2218 	if (*bufp == NULL) {
2219 	    if ((buf = malloc(bufsize, CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
2220 		error = ENOMEM;
2221 		goto out;
2222 	    }
2223 	} else {
2224 	    buf = *bufp;
2225 	    dataout = 1;	/* we are given a buffer, so we are writing */
2226 	}
2227     }
2228 
2229     /*
2230      * Build a CISS BMIC command to get the logical drive ID.
2231      */
2232     cr->cr_data = buf;
2233     cr->cr_length = bufsize;
2234     if (!dataout)
2235 	cr->cr_flags = CISS_REQ_DATAIN;
2236 
2237     cc = CISS_FIND_COMMAND(cr);
2238     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2239     cc->header.address.physical.bus = 0;
2240     cc->header.address.physical.target = 0;
2241     cc->cdb.cdb_length = sizeof(*cbc);
2242     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2243     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2244     cc->cdb.direction = dataout ? CISS_CDB_DIRECTION_WRITE : CISS_CDB_DIRECTION_READ;
2245     cc->cdb.timeout = 0;
2246 
2247     cbc = (struct ciss_bmic_cdb *)&(cc->cdb.cdb[0]);
2248     bzero(cbc, sizeof(*cbc));
2249     cbc->opcode = dataout ? CISS_ARRAY_CONTROLLER_WRITE : CISS_ARRAY_CONTROLLER_READ;
2250     cbc->bmic_opcode = opcode;
2251     cbc->size = htons((u_int16_t)bufsize);
2252 
2253 out:
2254     if (error) {
2255 	if (cr != NULL)
2256 	    ciss_release_request(cr);
2257     } else {
2258 	*crp = cr;
2259 	if ((bufp != NULL) && (*bufp == NULL) && (buf != NULL))
2260 	    *bufp = buf;
2261     }
2262     return(error);
2263 }
2264 
2265 /************************************************************************
2266  * Handle a command passed in from userspace.
2267  */
2268 static int
2269 ciss_user_command(struct ciss_softc *sc, IOCTL_Command_struct *ioc)
2270 {
2271     struct ciss_request		*cr;
2272     struct ciss_command		*cc;
2273     struct ciss_error_info	*ce;
2274     int				error = 0;
2275 
2276     debug_called(1);
2277 
2278     cr = NULL;
2279 
2280     /*
2281      * Get a request.
2282      */
2283     while (ciss_get_request(sc, &cr) != 0)
2284 	msleep(sc, &sc->ciss_mtx, PPAUSE, "cissREQ", hz);
2285     cc = CISS_FIND_COMMAND(cr);
2286 
2287     /*
2288      * Allocate an in-kernel databuffer if required, copy in user data.
2289      */
2290     cr->cr_length = ioc->buf_size;
2291     if (ioc->buf_size > 0) {
2292 	if ((cr->cr_data = malloc(ioc->buf_size, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
2293 	    error = ENOMEM;
2294 	    goto out;
2295 	}
2296 	if ((error = copyin(ioc->buf, cr->cr_data, ioc->buf_size))) {
2297 	    debug(0, "copyin: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2298 	    goto out;
2299 	}
2300     }
2301 
2302     /*
2303      * Build the request based on the user command.
2304      */
2305     bcopy(&ioc->LUN_info, &cc->header.address, sizeof(cc->header.address));
2306     bcopy(&ioc->Request, &cc->cdb, sizeof(cc->cdb));
2307 
2308     /* XXX anything else to populate here? */
2309 
2310     /*
2311      * Run the command.
2312      */
2313     if ((error = ciss_synch_request(cr, 60 * 1000))) {
2314 	debug(0, "request failed - %d", error);
2315 	goto out;
2316     }
2317 
2318     /*
2319      * Check to see if the command succeeded.
2320      */
2321     ce = (struct ciss_error_info *)&(cc->sg[0]);
2322     if ((cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) == 0)
2323 	bzero(ce, sizeof(*ce));
2324 
2325     /*
2326      * Copy the results back to the user.
2327      */
2328     bcopy(ce, &ioc->error_info, sizeof(*ce));
2329     if ((ioc->buf_size > 0) &&
2330 	(error = copyout(cr->cr_data, ioc->buf, ioc->buf_size))) {
2331 	debug(0, "copyout: bad data buffer %p/%d", ioc->buf, ioc->buf_size);
2332 	goto out;
2333     }
2334 
2335     /* done OK */
2336     error = 0;
2337 
2338 out:
2339     if ((cr != NULL) && (cr->cr_data != NULL))
2340 	free(cr->cr_data, CISS_MALLOC_CLASS);
2341     if (cr != NULL)
2342 	ciss_release_request(cr);
2343     return(error);
2344 }
2345 
2346 /************************************************************************
2347  * Map a request into bus-visible space, initialise the scatter/gather
2348  * list.
2349  */
2350 static int
2351 ciss_map_request(struct ciss_request *cr)
2352 {
2353     struct ciss_softc	*sc;
2354     int			error = 0;
2355 
2356     debug_called(2);
2357 
2358     sc = cr->cr_sc;
2359 
2360     /* check that mapping is necessary */
2361     if (cr->cr_flags & CISS_REQ_MAPPED)
2362 	return(0);
2363 
2364     cr->cr_flags |= CISS_REQ_MAPPED;
2365 
2366     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2367 		    BUS_DMASYNC_PREWRITE);
2368 
2369     if (cr->cr_data != NULL) {
2370 	error = bus_dmamap_load(sc->ciss_buffer_dmat, cr->cr_datamap,
2371 				cr->cr_data, cr->cr_length,
2372 				ciss_request_map_helper, cr, 0);
2373 	if (error != 0)
2374 	    return (error);
2375     } else {
2376 	/*
2377 	 * Post the command to the adapter.
2378 	 */
2379 	ciss_enqueue_busy(cr);
2380 	CISS_TL_SIMPLE_POST_CMD(cr->cr_sc, CISS_FIND_COMMANDPHYS(cr));
2381     }
2382 
2383     return(0);
2384 }
2385 
2386 static void
2387 ciss_request_map_helper(void *arg, bus_dma_segment_t *segs, int nseg, int error)
2388 {
2389     struct ciss_command	*cc;
2390     struct ciss_request *cr;
2391     struct ciss_softc	*sc;
2392     int			i;
2393 
2394     debug_called(2);
2395 
2396     cr = (struct ciss_request *)arg;
2397     sc = cr->cr_sc;
2398     cc = CISS_FIND_COMMAND(cr);
2399 
2400     for (i = 0; i < nseg; i++) {
2401 	cc->sg[i].address = segs[i].ds_addr;
2402 	cc->sg[i].length = segs[i].ds_len;
2403 	cc->sg[i].extension = 0;
2404     }
2405     /* we leave the s/g table entirely within the command */
2406     cc->header.sg_in_list = nseg;
2407     cc->header.sg_total = nseg;
2408 
2409     if (cr->cr_flags & CISS_REQ_DATAIN)
2410 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREREAD);
2411     if (cr->cr_flags & CISS_REQ_DATAOUT)
2412 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_PREWRITE);
2413 
2414     /*
2415      * Post the command to the adapter.
2416      */
2417     ciss_enqueue_busy(cr);
2418     CISS_TL_SIMPLE_POST_CMD(cr->cr_sc, CISS_FIND_COMMANDPHYS(cr));
2419 }
2420 
2421 /************************************************************************
2422  * Unmap a request from bus-visible space.
2423  */
2424 static void
2425 ciss_unmap_request(struct ciss_request *cr)
2426 {
2427     struct ciss_softc	*sc;
2428 
2429     debug_called(2);
2430 
2431     sc = cr->cr_sc;
2432 
2433     /* check that unmapping is necessary */
2434     if ((cr->cr_flags & CISS_REQ_MAPPED) == 0)
2435 	return;
2436 
2437     bus_dmamap_sync(sc->ciss_command_dmat, sc->ciss_command_map,
2438 		    BUS_DMASYNC_POSTWRITE);
2439 
2440     if (cr->cr_data == NULL)
2441 	goto out;
2442 
2443     if (cr->cr_flags & CISS_REQ_DATAIN)
2444 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTREAD);
2445     if (cr->cr_flags & CISS_REQ_DATAOUT)
2446 	bus_dmamap_sync(sc->ciss_buffer_dmat, cr->cr_datamap, BUS_DMASYNC_POSTWRITE);
2447 
2448     bus_dmamap_unload(sc->ciss_buffer_dmat, cr->cr_datamap);
2449 out:
2450     cr->cr_flags &= ~CISS_REQ_MAPPED;
2451 }
2452 
2453 /************************************************************************
2454  * Attach the driver to CAM.
2455  *
2456  * We put all the logical drives on a single SCSI bus.
2457  */
2458 static int
2459 ciss_cam_init(struct ciss_softc *sc)
2460 {
2461     int			i, maxbus;
2462 
2463     debug_called(1);
2464 
2465     /*
2466      * Allocate a devq.  We can reuse this for the masked physical
2467      * devices if we decide to export these as well.
2468      */
2469     if ((sc->ciss_cam_devq = cam_simq_alloc(sc->ciss_max_requests)) == NULL) {
2470 	ciss_printf(sc, "can't allocate CAM SIM queue\n");
2471 	return(ENOMEM);
2472     }
2473 
2474     /*
2475      * Create a SIM.
2476      *
2477      * This naturally wastes a bit of memory.  The alternative is to allocate
2478      * and register each bus as it is found, and then track them on a linked
2479      * list.  Unfortunately, the driver has a few places where it needs to
2480      * look up the SIM based solely on bus number, and it's unclear whether
2481      * a list traversal would work for these situations.
2482      */
2483     maxbus = max(sc->ciss_max_logical_bus, sc->ciss_max_physical_bus +
2484 		 CISS_PHYSICAL_BASE);
2485     sc->ciss_cam_sim = malloc(maxbus * sizeof(struct cam_sim*),
2486 			      CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO);
2487     if (sc->ciss_cam_sim == NULL) {
2488 	ciss_printf(sc, "can't allocate memory for controller SIM\n");
2489 	return(ENOMEM);
2490     }
2491 
2492     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
2493 	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2494 						 "ciss", sc,
2495 						 device_get_unit(sc->ciss_dev),
2496 						 &sc->ciss_mtx,
2497 						 sc->ciss_max_requests - 2,
2498 						 sc->ciss_max_requests - 2,
2499 						 sc->ciss_cam_devq)) == NULL) {
2500 	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2501 	    return(ENOMEM);
2502 	}
2503 
2504 	/*
2505 	 * Register bus with this SIM.
2506 	 */
2507 	mtx_lock(&sc->ciss_mtx);
2508 	if (i == 0 || sc->ciss_controllers[i].physical.bus != 0) {
2509 	    if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2510 		ciss_printf(sc, "can't register SCSI bus %d\n", i);
2511 		mtx_unlock(&sc->ciss_mtx);
2512 		return (ENXIO);
2513 	    }
2514 	}
2515 	mtx_unlock(&sc->ciss_mtx);
2516     }
2517 
2518     for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
2519 	 CISS_PHYSICAL_BASE; i++) {
2520 	if ((sc->ciss_cam_sim[i] = cam_sim_alloc(ciss_cam_action, ciss_cam_poll,
2521 						 "ciss", sc,
2522 						 device_get_unit(sc->ciss_dev),
2523 						 &sc->ciss_mtx, 1,
2524 						 sc->ciss_max_requests - 2,
2525 						 sc->ciss_cam_devq)) == NULL) {
2526 	    ciss_printf(sc, "can't allocate CAM SIM for controller %d\n", i);
2527 	    return (ENOMEM);
2528 	}
2529 
2530 	mtx_lock(&sc->ciss_mtx);
2531 	if (xpt_bus_register(sc->ciss_cam_sim[i], sc->ciss_dev, i) != 0) {
2532 	    ciss_printf(sc, "can't register SCSI bus %d\n", i);
2533 	    mtx_unlock(&sc->ciss_mtx);
2534 	    return (ENXIO);
2535 	}
2536 	mtx_unlock(&sc->ciss_mtx);
2537     }
2538 
2539     /*
2540      * Initiate a rescan of the bus.
2541      */
2542     mtx_lock(&sc->ciss_mtx);
2543     ciss_cam_rescan_all(sc);
2544     mtx_unlock(&sc->ciss_mtx);
2545 
2546     return(0);
2547 }
2548 
2549 /************************************************************************
2550  * Initiate a rescan of the 'logical devices' SIM
2551  */
2552 static void
2553 ciss_cam_rescan_target(struct ciss_softc *sc, int bus, int target)
2554 {
2555     struct cam_path	*path;
2556     union ccb		*ccb;
2557 
2558     debug_called(1);
2559 
2560     if ((ccb = malloc(sizeof(union ccb), CISS_MALLOC_CLASS, M_NOWAIT | M_ZERO)) == NULL) {
2561 	ciss_printf(sc, "rescan failed (can't allocate CCB)\n");
2562 	return;
2563     }
2564 
2565     if (xpt_create_path(&path, xpt_periph, cam_sim_path(sc->ciss_cam_sim[bus]),
2566 			target, CAM_LUN_WILDCARD) != CAM_REQ_CMP) {
2567 	ciss_printf(sc, "rescan failed (can't create path)\n");
2568 	free(ccb, CISS_MALLOC_CLASS);
2569 	return;
2570     }
2571 
2572     xpt_setup_ccb(&ccb->ccb_h, path, 5/*priority (low)*/);
2573     ccb->ccb_h.func_code = XPT_SCAN_BUS;
2574     ccb->ccb_h.cbfcnp = ciss_cam_rescan_callback;
2575     ccb->crcn.flags = CAM_FLAG_NONE;
2576     xpt_action(ccb);
2577 
2578     /* scan is now in progress */
2579 }
2580 
2581 static void
2582 ciss_cam_rescan_all(struct ciss_softc *sc)
2583 {
2584     int i;
2585 
2586     /* Rescan the logical buses */
2587     for (i = 0; i < sc->ciss_max_logical_bus; i++)
2588 	ciss_cam_rescan_target(sc, i, CAM_TARGET_WILDCARD);
2589     /* Rescan the physical buses */
2590     for (i = CISS_PHYSICAL_BASE; i < sc->ciss_max_physical_bus +
2591 	 CISS_PHYSICAL_BASE; i++)
2592 	ciss_cam_rescan_target(sc, i, CAM_TARGET_WILDCARD);
2593 }
2594 
2595 static void
2596 ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2597 {
2598     xpt_free_path(ccb->ccb_h.path);
2599     free(ccb, CISS_MALLOC_CLASS);
2600 }
2601 
2602 /************************************************************************
2603  * Handle requests coming from CAM
2604  */
2605 static void
2606 ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
2607 {
2608     struct ciss_softc	*sc;
2609     struct ccb_scsiio	*csio;
2610     int			bus, target;
2611     int			physical;
2612 
2613     sc = cam_sim_softc(sim);
2614     bus = cam_sim_bus(sim);
2615     csio = (struct ccb_scsiio *)&ccb->csio;
2616     target = csio->ccb_h.target_id;
2617     physical = CISS_IS_PHYSICAL(bus);
2618 
2619     switch (ccb->ccb_h.func_code) {
2620 
2621 	/* perform SCSI I/O */
2622     case XPT_SCSI_IO:
2623 	if (!ciss_cam_action_io(sim, csio))
2624 	    return;
2625 	break;
2626 
2627 	/* perform geometry calculations */
2628     case XPT_CALC_GEOMETRY:
2629     {
2630 	struct ccb_calc_geometry	*ccg = &ccb->ccg;
2631 	struct ciss_ldrive		*ld;
2632 
2633 	debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2634 
2635 	ld = NULL;
2636 	if (!physical)
2637 	    ld = &sc->ciss_logical[bus][target];
2638 
2639 	/*
2640 	 * Use the cached geometry settings unless the fault tolerance
2641 	 * is invalid.
2642 	 */
2643 	if (physical || ld->cl_geometry.fault_tolerance == 0xFF) {
2644 	    u_int32_t			secs_per_cylinder;
2645 
2646 	    ccg->heads = 255;
2647 	    ccg->secs_per_track = 32;
2648 	    secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2649 	    ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2650 	} else {
2651 	    ccg->heads = ld->cl_geometry.heads;
2652 	    ccg->secs_per_track = ld->cl_geometry.sectors;
2653 	    ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2654 	}
2655 	ccb->ccb_h.status = CAM_REQ_CMP;
2656         break;
2657     }
2658 
2659 	/* handle path attribute inquiry */
2660     case XPT_PATH_INQ:
2661     {
2662 	struct ccb_pathinq	*cpi = &ccb->cpi;
2663 
2664 	debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2665 
2666 	cpi->version_num = 1;
2667 	cpi->hba_inquiry = PI_TAG_ABLE;	/* XXX is this correct? */
2668 	cpi->target_sprt = 0;
2669 	cpi->hba_misc = 0;
2670 	cpi->max_target = CISS_MAX_LOGICAL;
2671 	cpi->max_lun = 0;		/* 'logical drive' channel only */
2672 	cpi->initiator_id = CISS_MAX_LOGICAL;
2673 	strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2674         strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
2675         strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2676         cpi->unit_number = cam_sim_unit(sim);
2677         cpi->bus_id = cam_sim_bus(sim);
2678 	cpi->base_transfer_speed = 132 * 1024;	/* XXX what to set this to? */
2679 	cpi->transport = XPORT_SPI;
2680 	cpi->transport_version = 2;
2681 	cpi->protocol = PROTO_SCSI;
2682 	cpi->protocol_version = SCSI_REV_2;
2683 	ccb->ccb_h.status = CAM_REQ_CMP;
2684 	break;
2685     }
2686 
2687     case XPT_GET_TRAN_SETTINGS:
2688     {
2689 	struct ccb_trans_settings	*cts = &ccb->cts;
2690 	int				bus, target;
2691 	struct ccb_trans_settings_spi *spi =
2692 	    &cts->xport_specific.spi;
2693 
2694 	bus = cam_sim_bus(sim);
2695 	target = cts->ccb_h.target_id;
2696 
2697 	debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
2698 	/* disconnect always OK */
2699 	cts->protocol = PROTO_SCSI;
2700 	cts->protocol_version = SCSI_REV_2;
2701 	cts->transport = XPORT_SPI;
2702 	cts->transport_version = 2;
2703 
2704 	spi->valid = CTS_SPI_VALID_DISC;
2705 	spi->flags = CTS_SPI_FLAGS_DISC_ENB;
2706 
2707 	cts->ccb_h.status = CAM_REQ_CMP;
2708 	break;
2709     }
2710 
2711     default:		/* we can't do this */
2712 	debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
2713 	ccb->ccb_h.status = CAM_REQ_INVALID;
2714 	break;
2715     }
2716 
2717     xpt_done(ccb);
2718 }
2719 
2720 /************************************************************************
2721  * Handle a CAM SCSI I/O request.
2722  */
2723 static int
2724 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
2725 {
2726     struct ciss_softc	*sc;
2727     int			bus, target;
2728     struct ciss_request	*cr;
2729     struct ciss_command	*cc;
2730     int			error;
2731 
2732     sc = cam_sim_softc(sim);
2733     bus = cam_sim_bus(sim);
2734     target = csio->ccb_h.target_id;
2735 
2736     debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
2737 
2738     /* firmware does not support commands > 10 bytes */
2739     if (csio->cdb_len > 12/*CISS_CDB_BUFFER_SIZE*/) {
2740 	debug(3, "  command too large (%d > %d)", csio->cdb_len, CISS_CDB_BUFFER_SIZE);
2741 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2742     }
2743 
2744     /* check that the CDB pointer is not to a physical address */
2745     if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
2746 	debug(3, "  CDB pointer is to physical address");
2747 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2748     }
2749 
2750     /* if there is data transfer, it must be to/from a virtual address */
2751     if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
2752 	if (csio->ccb_h.flags & CAM_DATA_PHYS) {		/* we can't map it */
2753 	    debug(3, "  data pointer is to physical address");
2754 	    csio->ccb_h.status = CAM_REQ_CMP_ERR;
2755 	}
2756 	if (csio->ccb_h.flags & CAM_SCATTER_VALID) {	/* we want to do the s/g setup */
2757 	    debug(3, "  data has premature s/g setup");
2758 	    csio->ccb_h.status = CAM_REQ_CMP_ERR;
2759 	}
2760     }
2761 
2762     /* abandon aborted ccbs or those that have failed validation */
2763     if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
2764 	debug(3, "abandoning CCB due to abort/validation failure");
2765 	return(EINVAL);
2766     }
2767 
2768     /* handle emulation of some SCSI commands ourself */
2769     if (ciss_cam_emulate(sc, csio))
2770 	return(0);
2771 
2772     /*
2773      * Get a request to manage this command.  If we can't, return the
2774      * ccb, freeze the queue and flag so that we unfreeze it when a
2775      * request completes.
2776      */
2777     if ((error = ciss_get_request(sc, &cr)) != 0) {
2778 	xpt_freeze_simq(sim, 1);
2779 	csio->ccb_h.status |= CAM_REQUEUE_REQ;
2780 	return(error);
2781     }
2782 
2783     /*
2784      * Build the command.
2785      */
2786     cc = CISS_FIND_COMMAND(cr);
2787     cr->cr_data = csio->data_ptr;
2788     cr->cr_length = csio->dxfer_len;
2789     cr->cr_complete = ciss_cam_complete;
2790     cr->cr_private = csio;
2791 
2792     /*
2793      * Target the right logical volume.
2794      */
2795     if (CISS_IS_PHYSICAL(bus))
2796 	cc->header.address =
2797 	    sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_address;
2798     else
2799 	cc->header.address =
2800 	    sc->ciss_logical[bus][target].cl_address;
2801     cc->cdb.cdb_length = csio->cdb_len;
2802     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2803     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;	/* XXX ordered tags? */
2804     if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2805 	cr->cr_flags = CISS_REQ_DATAOUT;
2806 	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
2807     } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2808 	cr->cr_flags = CISS_REQ_DATAIN;
2809 	cc->cdb.direction = CISS_CDB_DIRECTION_READ;
2810     } else {
2811 	cr->cr_flags = 0;
2812 	cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2813     }
2814     cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
2815     if (csio->ccb_h.flags & CAM_CDB_POINTER) {
2816 	bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
2817     } else {
2818 	bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
2819     }
2820 
2821     /*
2822      * Submit the request to the adapter.
2823      *
2824      * Note that this may fail if we're unable to map the request (and
2825      * if we ever learn a transport layer other than simple, may fail
2826      * if the adapter rejects the command).
2827      */
2828     if ((error = ciss_start(cr)) != 0) {
2829 	xpt_freeze_simq(sim, 1);
2830 	if (error == EINPROGRESS) {
2831 	    csio->ccb_h.status |= CAM_RELEASE_SIMQ;
2832 	    error = 0;
2833 	} else {
2834 	    csio->ccb_h.status |= CAM_REQUEUE_REQ;
2835 	    ciss_release_request(cr);
2836 	}
2837 	return(error);
2838     }
2839 
2840     return(0);
2841 }
2842 
2843 /************************************************************************
2844  * Emulate SCSI commands the adapter doesn't handle as we might like.
2845  */
2846 static int
2847 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
2848 {
2849     int		bus, target;
2850     u_int8_t	opcode;
2851 
2852     target = csio->ccb_h.target_id;
2853     bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
2854     opcode = (csio->ccb_h.flags & CAM_CDB_POINTER) ?
2855 	*(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0];
2856 
2857     if (CISS_IS_PHYSICAL(bus)) {
2858 	if (sc->ciss_physical[CISS_CAM_TO_PBUS(bus)][target].cp_online != 1) {
2859 	    csio->ccb_h.status = CAM_SEL_TIMEOUT;
2860 	    xpt_done((union ccb *)csio);
2861 	    return(1);
2862 	} else
2863 	    return(0);
2864     }
2865 
2866     /*
2867      * Handle requests for volumes that don't exist or are not online.
2868      * A selection timeout is slightly better than an illegal request.
2869      * Other errors might be better.
2870      */
2871     if (sc->ciss_logical[bus][target].cl_status != CISS_LD_ONLINE) {
2872 	csio->ccb_h.status = CAM_SEL_TIMEOUT;
2873 	xpt_done((union ccb *)csio);
2874 	return(1);
2875     }
2876 
2877     /* if we have to fake Synchronise Cache */
2878     if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
2879 	/*
2880 	 * If this is a Synchronise Cache command, typically issued when
2881 	 * a device is closed, flush the adapter and complete now.
2882 	 */
2883 	if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
2884 	     *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
2885 	    ciss_flush_adapter(sc);
2886 	    csio->ccb_h.status = CAM_REQ_CMP;
2887 	    xpt_done((union ccb *)csio);
2888 	    return(1);
2889 	}
2890     }
2891 
2892     return(0);
2893 }
2894 
2895 /************************************************************************
2896  * Check for possibly-completed commands.
2897  */
2898 static void
2899 ciss_cam_poll(struct cam_sim *sim)
2900 {
2901     struct ciss_softc	*sc = cam_sim_softc(sim);
2902 
2903     debug_called(2);
2904 
2905     ciss_done(sc);
2906 }
2907 
2908 /************************************************************************
2909  * Handle completion of a command - pass results back through the CCB
2910  */
2911 static void
2912 ciss_cam_complete(struct ciss_request *cr)
2913 {
2914     struct ciss_softc		*sc;
2915     struct ciss_command		*cc;
2916     struct ciss_error_info	*ce;
2917     struct ccb_scsiio		*csio;
2918     int				scsi_status;
2919     int				command_status;
2920 
2921     debug_called(2);
2922 
2923     sc = cr->cr_sc;
2924     cc = CISS_FIND_COMMAND(cr);
2925     ce = (struct ciss_error_info *)&(cc->sg[0]);
2926     csio = (struct ccb_scsiio *)cr->cr_private;
2927 
2928     /*
2929      * Extract status values from request.
2930      */
2931     ciss_report_request(cr, &command_status, &scsi_status);
2932     csio->scsi_status = scsi_status;
2933 
2934     /*
2935      * Handle specific SCSI status values.
2936      */
2937     switch(scsi_status) {
2938 	/* no status due to adapter error */
2939     case -1:
2940 	debug(0, "adapter error");
2941 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2942 	break;
2943 
2944 	/* no status due to command completed OK */
2945     case SCSI_STATUS_OK:		/* CISS_SCSI_STATUS_GOOD */
2946 	debug(2, "SCSI_STATUS_OK");
2947 	csio->ccb_h.status = CAM_REQ_CMP;
2948 	break;
2949 
2950 	/* check condition, sense data included */
2951     case SCSI_STATUS_CHECK_COND:	/* CISS_SCSI_STATUS_CHECK_CONDITION */
2952 	debug(0, "SCSI_STATUS_CHECK_COND  sense size %d  resid %d\n",
2953 	      ce->sense_length, ce->residual_count);
2954 	bzero(&csio->sense_data, SSD_FULL_SIZE);
2955 	bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
2956 	csio->sense_len = ce->sense_length;
2957 	csio->resid = ce->residual_count;
2958 	csio->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
2959 #ifdef CISS_DEBUG
2960 	{
2961 	    struct scsi_sense_data	*sns = (struct scsi_sense_data *)&ce->sense_info[0];
2962 	    debug(0, "sense key %x", sns->flags & SSD_KEY);
2963 	}
2964 #endif
2965 	break;
2966 
2967     case SCSI_STATUS_BUSY:		/* CISS_SCSI_STATUS_BUSY */
2968 	debug(0, "SCSI_STATUS_BUSY");
2969 	csio->ccb_h.status = CAM_SCSI_BUSY;
2970 	break;
2971 
2972     default:
2973 	debug(0, "unknown status 0x%x", csio->scsi_status);
2974 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2975 	break;
2976     }
2977 
2978     /* handle post-command fixup */
2979     ciss_cam_complete_fixup(sc, csio);
2980 
2981     /* tell CAM we're ready for more commands */
2982     csio->ccb_h.status |= CAM_RELEASE_SIMQ;
2983 
2984     xpt_done((union ccb *)csio);
2985     ciss_release_request(cr);
2986 }
2987 
2988 /********************************************************************************
2989  * Fix up the result of some commands here.
2990  */
2991 static void
2992 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
2993 {
2994     struct scsi_inquiry_data	*inq;
2995     struct ciss_ldrive		*cl;
2996     int				bus, target;
2997 
2998     if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
2999 	 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == INQUIRY) {
3000 
3001 	inq = (struct scsi_inquiry_data *)csio->data_ptr;
3002 	target = csio->ccb_h.target_id;
3003 	bus = cam_sim_bus(xpt_path_sim(csio->ccb_h.path));
3004 
3005 	/*
3006 	 * Don't let hard drives be seen by the DA driver.  They will still be
3007 	 * attached by the PASS driver.
3008 	 */
3009 	if (CISS_IS_PHYSICAL(bus)) {
3010 	    if (SID_TYPE(inq) == T_DIRECT)
3011 		inq->device = (inq->device & 0xe0) | T_NODEVICE;
3012 	    return;
3013 	}
3014 
3015 	cl = &sc->ciss_logical[bus][target];
3016 
3017 	padstr(inq->vendor, "COMPAQ", 8);
3018 	padstr(inq->product, ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance), 8);
3019 	padstr(inq->revision, ciss_name_ldrive_status(cl->cl_lstatus->status), 16);
3020     }
3021 }
3022 
3023 
3024 /********************************************************************************
3025  * Find a peripheral attached at (target)
3026  */
3027 static struct cam_periph *
3028 ciss_find_periph(struct ciss_softc *sc, int bus, int target)
3029 {
3030     struct cam_periph	*periph;
3031     struct cam_path	*path;
3032     int			status;
3033 
3034     status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim[bus]),
3035 			     target, 0);
3036     if (status == CAM_REQ_CMP) {
3037 	periph = cam_periph_find(path, NULL);
3038 	xpt_free_path(path);
3039     } else {
3040 	periph = NULL;
3041     }
3042     return(periph);
3043 }
3044 
3045 /********************************************************************************
3046  * Name the device at (target)
3047  *
3048  * XXX is this strictly correct?
3049  */
3050 static int
3051 ciss_name_device(struct ciss_softc *sc, int bus, int target)
3052 {
3053     struct cam_periph	*periph;
3054 
3055     if (CISS_IS_PHYSICAL(bus))
3056 	return (0);
3057     if ((periph = ciss_find_periph(sc, bus, target)) != NULL) {
3058 	sprintf(sc->ciss_logical[bus][target].cl_name, "%s%d",
3059 		periph->periph_name, periph->unit_number);
3060 	return(0);
3061     }
3062     sc->ciss_logical[bus][target].cl_name[0] = 0;
3063     return(ENOENT);
3064 }
3065 
3066 /************************************************************************
3067  * Periodic status monitoring.
3068  */
3069 static void
3070 ciss_periodic(void *arg)
3071 {
3072     struct ciss_softc	*sc;
3073     struct ciss_request	*cr = NULL;
3074     struct ciss_command	*cc = NULL;
3075     int			error = 0;
3076 
3077     debug_called(1);
3078 
3079     sc = (struct ciss_softc *)arg;
3080 
3081     /*
3082      * Check the adapter heartbeat.
3083      */
3084     if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
3085 	sc->ciss_heart_attack++;
3086 	debug(0, "adapter heart attack in progress 0x%x/%d",
3087 	      sc->ciss_heartbeat, sc->ciss_heart_attack);
3088 	if (sc->ciss_heart_attack == 3) {
3089 	    ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
3090 	    ciss_disable_adapter(sc);
3091 	    return;
3092 	}
3093     } else {
3094 	sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
3095 	sc->ciss_heart_attack = 0;
3096 	debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
3097     }
3098 
3099     /*
3100      * Send the NOP message and wait for a response.
3101      */
3102     if ((error = ciss_get_request(sc, &cr)) == 0) {
3103 	cc = CISS_FIND_COMMAND(cr);
3104 	cr->cr_complete = ciss_nop_complete;
3105 	cc->cdb.cdb_length = 1;
3106 	cc->cdb.type = CISS_CDB_TYPE_MESSAGE;
3107 	cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3108 	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
3109 	cc->cdb.timeout = 0;
3110 	cc->cdb.cdb[0] = CISS_OPCODE_MESSAGE_NOP;
3111 
3112 	if ((error = ciss_start(cr)) != 0) {
3113 	    ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3114 	}
3115     }
3116 
3117     /*
3118      * If the notify event request has died for some reason, or has
3119      * not started yet, restart it.
3120      */
3121     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
3122 	debug(0, "(re)starting Event Notify chain");
3123 	ciss_notify_event(sc);
3124     }
3125 
3126     /*
3127      * Reschedule.
3128      */
3129     callout_reset(&sc->ciss_periodic, CISS_HEARTBEAT_RATE * hz, ciss_periodic, sc);
3130 }
3131 
3132 static void
3133 ciss_nop_complete(struct ciss_request *cr)
3134 {
3135     struct ciss_softc		*sc;
3136 
3137     sc = cr->cr_sc;
3138     if (ciss_report_request(cr, NULL, NULL) != 0) {
3139 	ciss_printf(sc, "SENDING NOP MESSAGE FAILED\n");
3140     }
3141 
3142     ciss_release_request(cr);
3143 }
3144 
3145 /************************************************************************
3146  * Disable the adapter.
3147  *
3148  * The all requests in completed queue is failed with hardware error.
3149  * This will cause failover in a multipath configuration.
3150  */
3151 static void
3152 ciss_disable_adapter(struct ciss_softc *sc)
3153 {
3154     struct ciss_request		*cr;
3155     struct ciss_command		*cc;
3156     struct ciss_error_info	*ce;
3157     int				s;
3158 
3159     s = splcam();
3160 
3161     CISS_TL_SIMPLE_DISABLE_INTERRUPTS(sc);
3162     pci_disable_busmaster(sc->ciss_dev);
3163     sc->ciss_flags &= ~CISS_FLAG_RUNNING;
3164 
3165     for (;;) {
3166 	if ((cr = ciss_dequeue_busy(sc)) == NULL)
3167 	    break;
3168 
3169 	cc = CISS_FIND_COMMAND(cr);
3170 	ce = (struct ciss_error_info *)&(cc->sg[0]);
3171 	ce->command_status = CISS_CMD_STATUS_HARDWARE_ERROR;
3172 	ciss_enqueue_complete(cr);
3173     }
3174 
3175     for (;;) {
3176 	if ((cr = ciss_dequeue_complete(sc)) == NULL)
3177 	    break;
3178 
3179 	/*
3180 	 * If the request has a callback, invoke it.
3181 	 */
3182 	if (cr->cr_complete != NULL) {
3183 	    cr->cr_complete(cr);
3184 	    continue;
3185 	}
3186 
3187 	/*
3188 	 * If someone is sleeping on this request, wake them up.
3189 	 */
3190 	if (cr->cr_flags & CISS_REQ_SLEEP) {
3191 	    cr->cr_flags &= ~CISS_REQ_SLEEP;
3192 	    wakeup(cr);
3193 	    continue;
3194 	}
3195     }
3196 
3197     splx(s);
3198 }
3199 
3200 /************************************************************************
3201  * Request a notification response from the adapter.
3202  *
3203  * If (cr) is NULL, this is the first request of the adapter, so
3204  * reset the adapter's message pointer and start with the oldest
3205  * message available.
3206  */
3207 static void
3208 ciss_notify_event(struct ciss_softc *sc)
3209 {
3210     struct ciss_request		*cr;
3211     struct ciss_command		*cc;
3212     struct ciss_notify_cdb	*cnc;
3213     int				error;
3214 
3215     debug_called(1);
3216 
3217     cr = sc->ciss_periodic_notify;
3218 
3219     /* get a request if we don't already have one */
3220     if (cr == NULL) {
3221 	if ((error = ciss_get_request(sc, &cr)) != 0) {
3222 	    debug(0, "can't get notify event request");
3223 	    goto out;
3224 	}
3225 	sc->ciss_periodic_notify = cr;
3226 	cr->cr_complete = ciss_notify_complete;
3227 	debug(1, "acquired request %d", cr->cr_tag);
3228     }
3229 
3230     /*
3231      * Get a databuffer if we don't already have one, note that the
3232      * adapter command wants a larger buffer than the actual
3233      * structure.
3234      */
3235     if (cr->cr_data == NULL) {
3236 	if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3237 	    debug(0, "can't get notify event request buffer");
3238 	    error = ENOMEM;
3239 	    goto out;
3240 	}
3241 	cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3242     }
3243 
3244     /* re-setup the request's command (since we never release it) XXX overkill*/
3245     ciss_preen_command(cr);
3246 
3247     /* (re)build the notify event command */
3248     cc = CISS_FIND_COMMAND(cr);
3249     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3250     cc->header.address.physical.bus = 0;
3251     cc->header.address.physical.target = 0;
3252 
3253     cc->cdb.cdb_length = sizeof(*cnc);
3254     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3255     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3256     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3257     cc->cdb.timeout = 0;	/* no timeout, we hope */
3258 
3259     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3260     bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
3261     cnc->opcode = CISS_OPCODE_READ;
3262     cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
3263     cnc->timeout = 0;		/* no timeout, we hope */
3264     cnc->synchronous = 0;
3265     cnc->ordered = 0;
3266     cnc->seek_to_oldest = 0;
3267     if ((sc->ciss_flags & CISS_FLAG_RUNNING) == 0)
3268 	cnc->new_only = 1;
3269     else
3270 	cnc->new_only = 0;
3271     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3272 
3273     /* submit the request */
3274     error = ciss_start(cr);
3275 
3276  out:
3277     if (error) {
3278 	if (cr != NULL) {
3279 	    if (cr->cr_data != NULL)
3280 		free(cr->cr_data, CISS_MALLOC_CLASS);
3281 	    ciss_release_request(cr);
3282 	}
3283 	sc->ciss_periodic_notify = NULL;
3284 	debug(0, "can't submit notify event request");
3285 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3286     } else {
3287 	debug(1, "notify event submitted");
3288 	sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
3289     }
3290 }
3291 
3292 static void
3293 ciss_notify_complete(struct ciss_request *cr)
3294 {
3295     struct ciss_command	*cc;
3296     struct ciss_notify	*cn;
3297     struct ciss_softc	*sc;
3298     int			scsi_status;
3299     int			command_status;
3300     debug_called(1);
3301 
3302     cc = CISS_FIND_COMMAND(cr);
3303     cn = (struct ciss_notify *)cr->cr_data;
3304     sc = cr->cr_sc;
3305 
3306     /*
3307      * Report request results, decode status.
3308      */
3309     ciss_report_request(cr, &command_status, &scsi_status);
3310 
3311     /*
3312      * Abort the chain on a fatal error.
3313      *
3314      * XXX which of these are actually errors?
3315      */
3316     if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
3317 	(command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
3318 	(command_status != CISS_CMD_STATUS_TIMEOUT)) {	/* XXX timeout? */
3319 	ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
3320 		    ciss_name_command_status(command_status));
3321 	ciss_release_request(cr);
3322 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3323 	return;
3324     }
3325 
3326     /*
3327      * If the adapter gave us a text message, print it.
3328      */
3329     if (cn->message[0] != 0)
3330 	ciss_printf(sc, "*** %.80s\n", cn->message);
3331 
3332     debug(0, "notify event class %d subclass %d detail %d",
3333 		cn->class, cn->subclass, cn->detail);
3334 
3335     /*
3336      * If the response indicates that the notifier has been aborted,
3337      * release the notifier command.
3338      */
3339     if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
3340 	(cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
3341 	(cn->detail == 1)) {
3342 	debug(0, "notifier exiting");
3343 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3344 	ciss_release_request(cr);
3345 	sc->ciss_periodic_notify = NULL;
3346 	wakeup(&sc->ciss_periodic_notify);
3347     } else {
3348 	/* Handle notify events in a kernel thread */
3349 	ciss_enqueue_notify(cr);
3350 	sc->ciss_periodic_notify = NULL;
3351 	wakeup(&sc->ciss_periodic_notify);
3352 	wakeup(&sc->ciss_notify);
3353     }
3354     /*
3355      * Send a new notify event command, if we're not aborting.
3356      */
3357     if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
3358 	ciss_notify_event(sc);
3359     }
3360 }
3361 
3362 /************************************************************************
3363  * Abort the Notify Event chain.
3364  *
3365  * Note that we can't just abort the command in progress; we have to
3366  * explicitly issue an Abort Notify Event command in order for the
3367  * adapter to clean up correctly.
3368  *
3369  * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
3370  * the chain will not restart itself.
3371  */
3372 static int
3373 ciss_notify_abort(struct ciss_softc *sc)
3374 {
3375     struct ciss_request		*cr;
3376     struct ciss_command		*cc;
3377     struct ciss_notify_cdb	*cnc;
3378     int				error, s, command_status, scsi_status;
3379 
3380     debug_called(1);
3381 
3382     cr = NULL;
3383     error = 0;
3384 
3385     /* verify that there's an outstanding command */
3386     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3387 	goto out;
3388 
3389     /* get a command to issue the abort with */
3390     if ((error = ciss_get_request(sc, &cr)))
3391 	goto out;
3392 
3393     /* get a buffer for the result */
3394     if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
3395 	debug(0, "can't get notify event request buffer");
3396 	error = ENOMEM;
3397 	goto out;
3398     }
3399     cr->cr_length = CISS_NOTIFY_DATA_SIZE;
3400 
3401     /* build the CDB */
3402     cc = CISS_FIND_COMMAND(cr);
3403     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
3404     cc->header.address.physical.bus = 0;
3405     cc->header.address.physical.target = 0;
3406     cc->cdb.cdb_length = sizeof(*cnc);
3407     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
3408     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
3409     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
3410     cc->cdb.timeout = 0;	/* no timeout, we hope */
3411 
3412     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
3413     bzero(cnc, sizeof(*cnc));
3414     cnc->opcode = CISS_OPCODE_WRITE;
3415     cnc->command = CISS_COMMAND_ABORT_NOTIFY;
3416     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
3417 
3418     ciss_print_request(cr);
3419 
3420     /*
3421      * Submit the request and wait for it to complete.
3422      */
3423     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3424 	ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
3425 	goto out;
3426     }
3427 
3428     /*
3429      * Check response.
3430      */
3431     ciss_report_request(cr, &command_status, &scsi_status);
3432     switch(command_status) {
3433     case CISS_CMD_STATUS_SUCCESS:
3434 	break;
3435     case CISS_CMD_STATUS_INVALID_COMMAND:
3436 	/*
3437 	 * Some older adapters don't support the CISS version of this
3438 	 * command.  Fall back to using the BMIC version.
3439 	 */
3440 	error = ciss_notify_abort_bmic(sc);
3441 	if (error != 0)
3442 	    goto out;
3443 	break;
3444 
3445     case CISS_CMD_STATUS_TARGET_STATUS:
3446 	/*
3447 	 * This can happen if the adapter thinks there wasn't an outstanding
3448 	 * Notify Event command but we did.  We clean up here.
3449 	 */
3450 	if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
3451 	    if (sc->ciss_periodic_notify != NULL)
3452 		ciss_release_request(sc->ciss_periodic_notify);
3453 	    error = 0;
3454 	    goto out;
3455 	}
3456 	/* FALLTHROUGH */
3457 
3458     default:
3459 	ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
3460 		    ciss_name_command_status(command_status));
3461 	error = EIO;
3462 	goto out;
3463     }
3464 
3465     /*
3466      * Sleep waiting for the notifier command to complete.  Note
3467      * that if it doesn't, we may end up in a bad situation, since
3468      * the adapter may deliver it later.  Also note that the adapter
3469      * requires the Notify Event command to be cancelled in order to
3470      * maintain internal bookkeeping.
3471      */
3472     s = splcam();
3473     while (sc->ciss_periodic_notify != NULL) {
3474 	error = msleep(&sc->ciss_periodic_notify, &sc->ciss_mtx, PRIBIO, "cissNEA", hz * 5);
3475 	if (error == EWOULDBLOCK) {
3476 	    ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
3477 	    break;
3478 	}
3479     }
3480     splx(s);
3481 
3482  out:
3483     /* release the cancel request */
3484     if (cr != NULL) {
3485 	if (cr->cr_data != NULL)
3486 	    free(cr->cr_data, CISS_MALLOC_CLASS);
3487 	ciss_release_request(cr);
3488     }
3489     if (error == 0)
3490 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
3491     return(error);
3492 }
3493 
3494 /************************************************************************
3495  * Abort the Notify Event chain using a BMIC command.
3496  */
3497 static int
3498 ciss_notify_abort_bmic(struct ciss_softc *sc)
3499 {
3500     struct ciss_request			*cr;
3501     int					error, command_status;
3502 
3503     debug_called(1);
3504 
3505     cr = NULL;
3506     error = 0;
3507 
3508     /* verify that there's an outstanding command */
3509     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3510 	goto out;
3511 
3512     /*
3513      * Build a BMIC command to cancel the Notify on Event command.
3514      *
3515      * Note that we are sending a CISS opcode here.  Odd.
3516      */
3517     if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
3518 				       NULL, 0)) != 0)
3519 	goto out;
3520 
3521     /*
3522      * Submit the request and wait for it to complete.
3523      */
3524     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3525 	ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
3526 	goto out;
3527     }
3528 
3529     /*
3530      * Check response.
3531      */
3532     ciss_report_request(cr, &command_status, NULL);
3533     switch(command_status) {
3534     case CISS_CMD_STATUS_SUCCESS:
3535 	break;
3536     default:
3537 	ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
3538 		    ciss_name_command_status(command_status));
3539 	error = EIO;
3540 	goto out;
3541     }
3542 
3543 out:
3544     if (cr != NULL)
3545 	ciss_release_request(cr);
3546     return(error);
3547 }
3548 
3549 /************************************************************************
3550  * Handle rescanning all the logical volumes when a notify event
3551  * causes the drives to come online or offline.
3552  */
3553 static void
3554 ciss_notify_rescan_logical(struct ciss_softc *sc)
3555 {
3556     struct ciss_lun_report      *cll;
3557     struct ciss_ldrive		*ld;
3558     int                         i, j, ndrives;
3559 
3560     /*
3561      * We must rescan all logical volumes to get the right logical
3562      * drive address.
3563      */
3564     cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_LOGICAL_LUNS,
3565                            CISS_MAX_LOGICAL);
3566     if (cll == NULL)
3567         return;
3568 
3569     ndrives = (ntohl(cll->list_size) / sizeof(union ciss_device_address));
3570 
3571     /*
3572      * Delete any of the drives which were destroyed by the
3573      * firmware.
3574      */
3575     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
3576 	for (j = 0; j < CISS_MAX_LOGICAL; j++) {
3577 	    ld = &sc->ciss_logical[i][j];
3578 
3579 	    if (ld->cl_update == 0)
3580 		continue;
3581 
3582 	    if (ld->cl_status != CISS_LD_ONLINE) {
3583 		ciss_cam_rescan_target(sc, i, j);
3584 		ld->cl_update = 0;
3585 		if (ld->cl_ldrive)
3586 		    free(ld->cl_ldrive, CISS_MALLOC_CLASS);
3587 		if (ld->cl_lstatus)
3588 		    free(ld->cl_lstatus, CISS_MALLOC_CLASS);
3589 
3590 		ld->cl_ldrive = NULL;
3591 		ld->cl_lstatus = NULL;
3592 	    }
3593 	}
3594     }
3595 
3596     /*
3597      * Scan for new drives.
3598      */
3599     for (i = 0; i < ndrives; i++) {
3600 	int	bus, target;
3601 
3602 	bus 	= CISS_LUN_TO_BUS(cll->lun[i].logical.lun);
3603 	target	= CISS_LUN_TO_TARGET(cll->lun[i].logical.lun);
3604 	ld	= &sc->ciss_logical[bus][target];
3605 
3606 	if (ld->cl_update == 0)
3607 		continue;
3608 
3609 	ld->cl_update		= 0;
3610 	ld->cl_address		= cll->lun[i];
3611 	ld->cl_controller	= &sc->ciss_controllers[bus];
3612 	if (ciss_identify_logical(sc, ld) == 0) {
3613 	    ciss_cam_rescan_target(sc, bus, target);
3614 	}
3615     }
3616     free(cll, CISS_MALLOC_CLASS);
3617 }
3618 
3619 /************************************************************************
3620  * Handle a notify event relating to the status of a logical drive.
3621  *
3622  * XXX need to be able to defer some of these to properly handle
3623  *     calling the "ID Physical drive" command, unless the 'extended'
3624  *     drive IDs are always in BIG_MAP format.
3625  */
3626 static void
3627 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
3628 {
3629     struct ciss_ldrive	*ld;
3630     int			ostatus, bus, target;
3631 
3632     debug_called(2);
3633 
3634     bus		= cn->device.physical.bus;
3635     target	= cn->data.logical_status.logical_drive;
3636     ld		= &sc->ciss_logical[bus][target];
3637 
3638     switch (cn->subclass) {
3639     case CISS_NOTIFY_LOGICAL_STATUS:
3640 	switch (cn->detail) {
3641 	case 0:
3642 	    ciss_name_device(sc, bus, target);
3643 	    ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
3644 			cn->data.logical_status.logical_drive, ld->cl_name,
3645 			ciss_name_ldrive_status(cn->data.logical_status.previous_state),
3646 			ciss_name_ldrive_status(cn->data.logical_status.new_state),
3647 			cn->data.logical_status.spare_state,
3648 			"\20\1configured\2rebuilding\3failed\4in use\5available\n");
3649 
3650 	    /*
3651 	     * Update our idea of the drive's status.
3652 	     */
3653 	    ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
3654 	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3655 	    if (ld->cl_lstatus != NULL)
3656 		ld->cl_lstatus->status = cn->data.logical_status.new_state;
3657 
3658 	    /*
3659 	     * Have CAM rescan the drive if its status has changed.
3660 	     */
3661 	    if (ostatus != ld->cl_status) {
3662 		ld->cl_update = 1;
3663 		ciss_notify_rescan_logical(sc);
3664 	    }
3665 
3666 	    break;
3667 
3668 	case 1:	/* logical drive has recognised new media, needs Accept Media Exchange */
3669 	    ciss_name_device(sc, bus, target);
3670 	    ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
3671 			cn->data.logical_status.logical_drive, ld->cl_name);
3672 	    ciss_accept_media(sc, ld);
3673 
3674 	    ld->cl_update = 1;
3675 	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3676 	    ciss_notify_rescan_logical(sc);
3677 	    break;
3678 
3679 	case 2:
3680 	case 3:
3681 	    ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
3682 			cn->data.rebuild_aborted.logical_drive,
3683 			ld->cl_name,
3684 			(cn->detail == 2) ? "read" : "write");
3685 	    break;
3686 	}
3687 	break;
3688 
3689     case CISS_NOTIFY_LOGICAL_ERROR:
3690 	if (cn->detail == 0) {
3691 	    ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
3692 			cn->data.io_error.logical_drive,
3693 			ld->cl_name,
3694 			cn->data.io_error.failure_bus,
3695 			cn->data.io_error.failure_drive);
3696 	    /* XXX should we take the drive down at this point, or will we be told? */
3697 	}
3698 	break;
3699 
3700     case CISS_NOTIFY_LOGICAL_SURFACE:
3701 	if (cn->detail == 0)
3702 	    ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
3703 			cn->data.consistency_completed.logical_drive,
3704 			ld->cl_name);
3705 	break;
3706     }
3707 }
3708 
3709 /************************************************************************
3710  * Handle a notify event relating to the status of a physical drive.
3711  */
3712 static void
3713 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
3714 {
3715 }
3716 
3717 /************************************************************************
3718  * Handle a notify event relating to the status of a physical drive.
3719  */
3720 static void
3721 ciss_notify_hotplug(struct ciss_softc *sc, struct ciss_notify *cn)
3722 {
3723     struct ciss_lun_report *cll = NULL;
3724     int bus, target;
3725     int s;
3726 
3727     switch (cn->subclass) {
3728     case CISS_NOTIFY_HOTPLUG_PHYSICAL:
3729     case CISS_NOTIFY_HOTPLUG_NONDISK:
3730 	bus = CISS_BIG_MAP_BUS(sc, cn->data.drive.big_physical_drive_number);
3731 	target =
3732 	    CISS_BIG_MAP_TARGET(sc, cn->data.drive.big_physical_drive_number);
3733 
3734 	s = splcam();
3735 	if (cn->detail == 0) {
3736 	    /*
3737 	     * Mark the device offline so that it'll start producing selection
3738 	     * timeouts to the upper layer.
3739 	     */
3740 	    if ((bus >= 0) && (target >= 0))
3741 		sc->ciss_physical[bus][target].cp_online = 0;
3742 	} else {
3743 	    /*
3744 	     * Rescan the physical lun list for new items
3745 	     */
3746 	    cll = ciss_report_luns(sc, CISS_OPCODE_REPORT_PHYSICAL_LUNS,
3747 				   CISS_MAX_PHYSICAL);
3748 	    if (cll == NULL) {
3749 		ciss_printf(sc, "Warning, cannot get physical lun list\n");
3750 		break;
3751 	    }
3752 	    ciss_filter_physical(sc, cll);
3753 	}
3754 	splx(s);
3755 	break;
3756 
3757     default:
3758 	ciss_printf(sc, "Unknown hotplug event %d\n", cn->subclass);
3759 	return;
3760     }
3761 
3762     if (cll != NULL)
3763 	free(cll, CISS_MALLOC_CLASS);
3764 }
3765 
3766 /************************************************************************
3767  * Handle deferred processing of notify events.  Notify events may need
3768  * sleep which is unsafe during an interrupt.
3769  */
3770 static void
3771 ciss_notify_thread(void *arg)
3772 {
3773     struct ciss_softc		*sc;
3774     struct ciss_request		*cr;
3775     struct ciss_notify		*cn;
3776     int				s;
3777 
3778     sc = (struct ciss_softc *)arg;
3779 #if __FreeBSD_version >= 500000
3780     mtx_lock(&sc->ciss_mtx);
3781 #endif
3782 
3783     s = splcam();
3784     for (;;) {
3785 	if (TAILQ_EMPTY(&sc->ciss_notify) != 0 &&
3786 	    (sc->ciss_flags & CISS_FLAG_THREAD_SHUT) == 0) {
3787 	    msleep(&sc->ciss_notify, &sc->ciss_mtx, PUSER, "idle", 0);
3788 	}
3789 
3790 	if (sc->ciss_flags & CISS_FLAG_THREAD_SHUT)
3791 	    break;
3792 
3793 	cr = ciss_dequeue_notify(sc);
3794 	splx(s);
3795 
3796 	if (cr == NULL)
3797 		panic("cr null");
3798 	cn = (struct ciss_notify *)cr->cr_data;
3799 
3800 	switch (cn->class) {
3801 	case CISS_NOTIFY_HOTPLUG:
3802 	    ciss_notify_hotplug(sc, cn);
3803 	    break;
3804 	case CISS_NOTIFY_LOGICAL:
3805 	    ciss_notify_logical(sc, cn);
3806 	    break;
3807 	case CISS_NOTIFY_PHYSICAL:
3808 	    ciss_notify_physical(sc, cn);
3809 	    break;
3810 	}
3811 
3812 	ciss_release_request(cr);
3813 
3814 	s = splcam();
3815     }
3816     sc->ciss_notify_thread = NULL;
3817     wakeup(&sc->ciss_notify_thread);
3818     splx(s);
3819 
3820 #if __FreeBSD_version >= 500000
3821     mtx_unlock(&sc->ciss_mtx);
3822 #endif
3823     kproc_exit(0);
3824 }
3825 
3826 /************************************************************************
3827  * Start the notification kernel thread.
3828  */
3829 static void
3830 ciss_spawn_notify_thread(struct ciss_softc *sc)
3831 {
3832 
3833 #if __FreeBSD_version > 500005
3834     if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
3835 		       &sc->ciss_notify_thread, 0, 0, "ciss_notify%d",
3836 		       device_get_unit(sc->ciss_dev)))
3837 #else
3838     if (kproc_create((void(*)(void *))ciss_notify_thread, sc,
3839 		       &sc->ciss_notify_thread, "ciss_notify%d",
3840 		       device_get_unit(sc->ciss_dev)))
3841 #endif
3842 	panic("Could not create notify thread\n");
3843 }
3844 
3845 /************************************************************************
3846  * Kill the notification kernel thread.
3847  */
3848 static void
3849 ciss_kill_notify_thread(struct ciss_softc *sc)
3850 {
3851 
3852     if (sc->ciss_notify_thread == NULL)
3853 	return;
3854 
3855     sc->ciss_flags |= CISS_FLAG_THREAD_SHUT;
3856     wakeup(&sc->ciss_notify);
3857     msleep(&sc->ciss_notify_thread, &sc->ciss_mtx, PUSER, "thtrm", 0);
3858 }
3859 
3860 /************************************************************************
3861  * Print a request.
3862  */
3863 static void
3864 ciss_print_request(struct ciss_request *cr)
3865 {
3866     struct ciss_softc	*sc;
3867     struct ciss_command	*cc;
3868     int			i;
3869 
3870     sc = cr->cr_sc;
3871     cc = CISS_FIND_COMMAND(cr);
3872 
3873     ciss_printf(sc, "REQUEST @ %p\n", cr);
3874     ciss_printf(sc, "  data %p/%d  tag %d  flags %b\n",
3875 	      cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
3876 	      "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
3877     ciss_printf(sc, "  sg list/total %d/%d  host tag 0x%x\n",
3878 		cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
3879     switch(cc->header.address.mode.mode) {
3880     case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
3881     case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
3882 	ciss_printf(sc, "  physical bus %d target %d\n",
3883 		    cc->header.address.physical.bus, cc->header.address.physical.target);
3884 	break;
3885     case CISS_HDR_ADDRESS_MODE_LOGICAL:
3886 	ciss_printf(sc, "  logical unit %d\n", cc->header.address.logical.lun);
3887 	break;
3888     }
3889     ciss_printf(sc, "  %s cdb length %d type %s attribute %s\n",
3890 		(cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
3891 		(cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
3892 		(cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
3893 		cc->cdb.cdb_length,
3894 		(cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
3895 		(cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
3896 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
3897 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
3898 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
3899 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
3900 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
3901     ciss_printf(sc, "  %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
3902 
3903     if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
3904 	/* XXX print error info */
3905     } else {
3906 	/* since we don't use chained s/g, don't support it here */
3907 	for (i = 0; i < cc->header.sg_in_list; i++) {
3908 	    if ((i % 4) == 0)
3909 		ciss_printf(sc, "   ");
3910 	    printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
3911 	    if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
3912 		printf("\n");
3913 	}
3914     }
3915 }
3916 
3917 /************************************************************************
3918  * Print information about the status of a logical drive.
3919  */
3920 static void
3921 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
3922 {
3923     int		bus, target, i;
3924 
3925     if (ld->cl_lstatus == NULL) {
3926 	printf("does not exist\n");
3927 	return;
3928     }
3929 
3930     /* print drive status */
3931     switch(ld->cl_lstatus->status) {
3932     case CISS_LSTATUS_OK:
3933 	printf("online\n");
3934 	break;
3935     case CISS_LSTATUS_INTERIM_RECOVERY:
3936 	printf("in interim recovery mode\n");
3937 	break;
3938     case CISS_LSTATUS_READY_RECOVERY:
3939 	printf("ready to begin recovery\n");
3940 	break;
3941     case CISS_LSTATUS_RECOVERING:
3942 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
3943 	target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
3944 	printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
3945 	       bus, target, ld->cl_lstatus->blocks_to_recover);
3946 	break;
3947     case CISS_LSTATUS_EXPANDING:
3948 	printf("being expanded, %u blocks remaining\n",
3949 	       ld->cl_lstatus->blocks_to_recover);
3950 	break;
3951     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
3952 	printf("queued for expansion\n");
3953 	break;
3954     case CISS_LSTATUS_FAILED:
3955 	printf("queued for expansion\n");
3956 	break;
3957     case CISS_LSTATUS_WRONG_PDRIVE:
3958 	printf("wrong physical drive inserted\n");
3959 	break;
3960     case CISS_LSTATUS_MISSING_PDRIVE:
3961 	printf("missing a needed physical drive\n");
3962 	break;
3963     case CISS_LSTATUS_BECOMING_READY:
3964 	printf("becoming ready\n");
3965 	break;
3966     }
3967 
3968     /* print failed physical drives */
3969     for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
3970 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
3971 	target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
3972 	if (bus == -1)
3973 	    continue;
3974 	ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
3975 		    ld->cl_lstatus->drive_failure_map[i]);
3976     }
3977 }
3978 
3979 #ifdef CISS_DEBUG
3980 /************************************************************************
3981  * Print information about the controller/driver.
3982  */
3983 static void
3984 ciss_print_adapter(struct ciss_softc *sc)
3985 {
3986     int		i, j;
3987 
3988     ciss_printf(sc, "ADAPTER:\n");
3989     for (i = 0; i < CISSQ_COUNT; i++) {
3990 	ciss_printf(sc, "%s     %d/%d\n",
3991 	    i == 0 ? "free" :
3992 	    i == 1 ? "busy" : "complete",
3993 	    sc->ciss_qstat[i].q_length,
3994 	    sc->ciss_qstat[i].q_max);
3995     }
3996     ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
3997     ciss_printf(sc, "flags %b\n", sc->ciss_flags,
3998 	"\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
3999 
4000     for (i = 0; i < sc->ciss_max_logical_bus; i++) {
4001 	for (j = 0; j < CISS_MAX_LOGICAL; j++) {
4002 	    ciss_printf(sc, "LOGICAL DRIVE %d:  ", i);
4003 	    ciss_print_ldrive(sc, &sc->ciss_logical[i][j]);
4004 	}
4005     }
4006 
4007     /* XXX Should physical drives be printed out here? */
4008 
4009     for (i = 1; i < sc->ciss_max_requests; i++)
4010 	ciss_print_request(sc->ciss_request + i);
4011 }
4012 
4013 /* DDB hook */
4014 static void
4015 ciss_print0(void)
4016 {
4017     struct ciss_softc	*sc;
4018 
4019     sc = devclass_get_softc(devclass_find("ciss"), 0);
4020     if (sc == NULL) {
4021 	printf("no ciss controllers\n");
4022     } else {
4023 	ciss_print_adapter(sc);
4024     }
4025 }
4026 #endif
4027 
4028 /************************************************************************
4029  * Return a name for a logical drive status value.
4030  */
4031 static const char *
4032 ciss_name_ldrive_status(int status)
4033 {
4034     switch (status) {
4035     case CISS_LSTATUS_OK:
4036 	return("OK");
4037     case CISS_LSTATUS_FAILED:
4038 	return("failed");
4039     case CISS_LSTATUS_NOT_CONFIGURED:
4040 	return("not configured");
4041     case CISS_LSTATUS_INTERIM_RECOVERY:
4042 	return("interim recovery");
4043     case CISS_LSTATUS_READY_RECOVERY:
4044 	return("ready for recovery");
4045     case CISS_LSTATUS_RECOVERING:
4046 	return("recovering");
4047     case CISS_LSTATUS_WRONG_PDRIVE:
4048 	return("wrong physical drive inserted");
4049     case CISS_LSTATUS_MISSING_PDRIVE:
4050 	return("missing physical drive");
4051     case CISS_LSTATUS_EXPANDING:
4052 	return("expanding");
4053     case CISS_LSTATUS_BECOMING_READY:
4054 	return("becoming ready");
4055     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4056 	return("queued for expansion");
4057     }
4058     return("unknown status");
4059 }
4060 
4061 /************************************************************************
4062  * Return an online/offline/nonexistent value for a logical drive
4063  * status value.
4064  */
4065 static int
4066 ciss_decode_ldrive_status(int status)
4067 {
4068     switch(status) {
4069     case CISS_LSTATUS_NOT_CONFIGURED:
4070 	return(CISS_LD_NONEXISTENT);
4071 
4072     case CISS_LSTATUS_OK:
4073     case CISS_LSTATUS_INTERIM_RECOVERY:
4074     case CISS_LSTATUS_READY_RECOVERY:
4075     case CISS_LSTATUS_RECOVERING:
4076     case CISS_LSTATUS_EXPANDING:
4077     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
4078 	return(CISS_LD_ONLINE);
4079 
4080     case CISS_LSTATUS_FAILED:
4081     case CISS_LSTATUS_WRONG_PDRIVE:
4082     case CISS_LSTATUS_MISSING_PDRIVE:
4083     case CISS_LSTATUS_BECOMING_READY:
4084     default:
4085 	return(CISS_LD_OFFLINE);
4086     }
4087 }
4088 
4089 
4090 /************************************************************************
4091  * Return a name for a logical drive's organisation.
4092  */
4093 static const char *
4094 ciss_name_ldrive_org(int org)
4095 {
4096     switch(org) {
4097     case CISS_LDRIVE_RAID0:
4098 	return("RAID 0");
4099     case CISS_LDRIVE_RAID1:
4100 	return("RAID 1");
4101     case CISS_LDRIVE_RAID4:
4102 	return("RAID 4");
4103     case CISS_LDRIVE_RAID5:
4104 	return("RAID 5");
4105     case CISS_LDRIVE_RAID51:
4106 	return("RAID 5+1");
4107     case CISS_LDRIVE_RAIDADG:
4108 	return("RAID ADG");
4109     }
4110     return("unkown");
4111 }
4112 
4113 /************************************************************************
4114  * Return a name for a command status value.
4115  */
4116 static const char *
4117 ciss_name_command_status(int status)
4118 {
4119     switch(status) {
4120     case CISS_CMD_STATUS_SUCCESS:
4121 	return("success");
4122     case CISS_CMD_STATUS_TARGET_STATUS:
4123 	return("target status");
4124     case CISS_CMD_STATUS_DATA_UNDERRUN:
4125 	return("data underrun");
4126     case CISS_CMD_STATUS_DATA_OVERRUN:
4127 	return("data overrun");
4128     case CISS_CMD_STATUS_INVALID_COMMAND:
4129 	return("invalid command");
4130     case CISS_CMD_STATUS_PROTOCOL_ERROR:
4131 	return("protocol error");
4132     case CISS_CMD_STATUS_HARDWARE_ERROR:
4133 	return("hardware error");
4134     case CISS_CMD_STATUS_CONNECTION_LOST:
4135 	return("connection lost");
4136     case CISS_CMD_STATUS_ABORTED:
4137 	return("aborted");
4138     case CISS_CMD_STATUS_ABORT_FAILED:
4139 	return("abort failed");
4140     case CISS_CMD_STATUS_UNSOLICITED_ABORT:
4141 	return("unsolicited abort");
4142     case CISS_CMD_STATUS_TIMEOUT:
4143 	return("timeout");
4144     case CISS_CMD_STATUS_UNABORTABLE:
4145 	return("unabortable");
4146     }
4147     return("unknown status");
4148 }
4149 
4150 /************************************************************************
4151  * Handle an open on the control device.
4152  */
4153 static int
4154 ciss_open(struct cdev *dev, int flags, int fmt, d_thread_t *p)
4155 {
4156     struct ciss_softc	*sc;
4157 
4158     debug_called(1);
4159 
4160     sc = (struct ciss_softc *)dev->si_drv1;
4161 
4162     /* we might want to veto if someone already has us open */
4163 
4164     mtx_lock(&sc->ciss_mtx);
4165     sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
4166     mtx_unlock(&sc->ciss_mtx);
4167     return(0);
4168 }
4169 
4170 /************************************************************************
4171  * Handle the last close on the control device.
4172  */
4173 static int
4174 ciss_close(struct cdev *dev, int flags, int fmt, d_thread_t *p)
4175 {
4176     struct ciss_softc	*sc;
4177 
4178     debug_called(1);
4179 
4180     sc = (struct ciss_softc *)dev->si_drv1;
4181 
4182     mtx_lock(&sc->ciss_mtx);
4183     sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
4184     mtx_unlock(&sc->ciss_mtx);
4185     return (0);
4186 }
4187 
4188 /********************************************************************************
4189  * Handle adapter-specific control operations.
4190  *
4191  * Note that the API here is compatible with the Linux driver, in order to
4192  * simplify the porting of Compaq's userland tools.
4193  */
4194 static int
4195 ciss_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *p)
4196 {
4197     struct ciss_softc		*sc;
4198     IOCTL_Command_struct	*ioc	= (IOCTL_Command_struct *)addr;
4199 #ifdef __amd64__
4200     IOCTL_Command_struct32	*ioc32	= (IOCTL_Command_struct32 *)addr;
4201     IOCTL_Command_struct	ioc_swab;
4202 #endif
4203     int				error;
4204 
4205     debug_called(1);
4206 
4207     sc = (struct ciss_softc *)dev->si_drv1;
4208     error = 0;
4209     mtx_lock(&sc->ciss_mtx);
4210 
4211     switch(cmd) {
4212     case CCISS_GETPCIINFO:
4213     {
4214 	cciss_pci_info_struct	*pis = (cciss_pci_info_struct *)addr;
4215 
4216 	pis->bus = pci_get_bus(sc->ciss_dev);
4217 	pis->dev_fn = pci_get_slot(sc->ciss_dev);
4218 	pis->board_id = pci_get_devid(sc->ciss_dev);
4219 
4220 	break;
4221     }
4222 
4223     case CCISS_GETINTINFO:
4224     {
4225 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
4226 
4227 	cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
4228 	cis->count = sc->ciss_cfg->interrupt_coalesce_count;
4229 
4230 	break;
4231     }
4232 
4233     case CCISS_SETINTINFO:
4234     {
4235 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
4236 
4237 	if ((cis->delay == 0) && (cis->count == 0)) {
4238 	    error = EINVAL;
4239 	    break;
4240 	}
4241 
4242 	/*
4243 	 * XXX apparently this is only safe if the controller is idle,
4244 	 *     we should suspend it before doing this.
4245 	 */
4246 	sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
4247 	sc->ciss_cfg->interrupt_coalesce_count = cis->count;
4248 
4249 	if (ciss_update_config(sc))
4250 	    error = EIO;
4251 
4252 	/* XXX resume the controller here */
4253 	break;
4254     }
4255 
4256     case CCISS_GETNODENAME:
4257 	bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
4258 	      sizeof(NodeName_type));
4259 	break;
4260 
4261     case CCISS_SETNODENAME:
4262 	bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
4263 	      sizeof(NodeName_type));
4264 	if (ciss_update_config(sc))
4265 	    error = EIO;
4266 	break;
4267 
4268     case CCISS_GETHEARTBEAT:
4269 	*(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
4270 	break;
4271 
4272     case CCISS_GETBUSTYPES:
4273 	*(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
4274 	break;
4275 
4276     case CCISS_GETFIRMVER:
4277 	bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
4278 	      sizeof(FirmwareVer_type));
4279 	break;
4280 
4281     case CCISS_GETDRIVERVER:
4282 	*(DriverVer_type *)addr = CISS_DRIVER_VERSION;
4283 	break;
4284 
4285     case CCISS_REVALIDVOLS:
4286 	/*
4287 	 * This is a bit ugly; to do it "right" we really need
4288 	 * to find any disks that have changed, kick CAM off them,
4289 	 * then rescan only these disks.  It'd be nice if they
4290 	 * a) told us which disk(s) they were going to play with,
4291 	 * and b) which ones had arrived. 8(
4292 	 */
4293 	break;
4294 
4295 #ifdef __amd64__
4296     case CCISS_PASSTHRU32:
4297 	ioc_swab.LUN_info	= ioc32->LUN_info;
4298 	ioc_swab.Request	= ioc32->Request;
4299 	ioc_swab.error_info	= ioc32->error_info;
4300 	ioc_swab.buf_size	= ioc32->buf_size;
4301 	ioc_swab.buf		= (u_int8_t *)(uintptr_t)ioc32->buf;
4302 	ioc			= &ioc_swab;
4303 	/* FALLTHROUGH */
4304 #endif
4305 
4306     case CCISS_PASSTHRU:
4307 	error = ciss_user_command(sc, ioc);
4308 	break;
4309 
4310     default:
4311 	debug(0, "unknown ioctl 0x%lx", cmd);
4312 
4313 	debug(1, "CCISS_GETPCIINFO:   0x%lx", CCISS_GETPCIINFO);
4314 	debug(1, "CCISS_GETINTINFO:   0x%lx", CCISS_GETINTINFO);
4315 	debug(1, "CCISS_SETINTINFO:   0x%lx", CCISS_SETINTINFO);
4316 	debug(1, "CCISS_GETNODENAME:  0x%lx", CCISS_GETNODENAME);
4317 	debug(1, "CCISS_SETNODENAME:  0x%lx", CCISS_SETNODENAME);
4318 	debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
4319 	debug(1, "CCISS_GETBUSTYPES:  0x%lx", CCISS_GETBUSTYPES);
4320 	debug(1, "CCISS_GETFIRMVER:   0x%lx", CCISS_GETFIRMVER);
4321 	debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
4322 	debug(1, "CCISS_REVALIDVOLS:  0x%lx", CCISS_REVALIDVOLS);
4323 	debug(1, "CCISS_PASSTHRU:     0x%lx", CCISS_PASSTHRU);
4324 
4325 	error = ENOIOCTL;
4326 	break;
4327     }
4328 
4329     mtx_unlock(&sc->ciss_mtx);
4330     return(error);
4331 }
4332