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