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