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