xref: /freebsd/sys/dev/ciss/ciss.c (revision 52ec752989b2e6d4e9a59a8ff25d8ff596d85e62)
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 	free(ccb, M_TEMP);
2171 	return;
2172     }
2173 
2174     xpt_setup_ccb(&ccb->ccb_h, sc->ciss_cam_path, 5/*priority (low)*/);
2175     ccb->ccb_h.func_code = XPT_SCAN_BUS;
2176     ccb->ccb_h.cbfcnp = ciss_cam_rescan_callback;
2177     ccb->crcn.flags = CAM_FLAG_NONE;
2178     xpt_action(ccb);
2179 
2180     /* scan is now in progress */
2181 }
2182 
2183 static void
2184 ciss_cam_rescan_all(struct ciss_softc *sc)
2185 {
2186     ciss_cam_rescan_target(sc, 0);
2187 }
2188 
2189 static void
2190 ciss_cam_rescan_callback(struct cam_periph *periph, union ccb *ccb)
2191 {
2192     xpt_free_path(ccb->ccb_h.path);
2193     free(ccb, M_TEMP);
2194 }
2195 
2196 /************************************************************************
2197  * Handle requests coming from CAM
2198  */
2199 static void
2200 ciss_cam_action(struct cam_sim *sim, union ccb *ccb)
2201 {
2202     struct ciss_softc	*sc;
2203     struct ccb_scsiio	*csio;
2204     int			target;
2205 
2206     sc = cam_sim_softc(sim);
2207     csio = (struct ccb_scsiio *)&ccb->csio;
2208     target = csio->ccb_h.target_id;
2209 
2210     switch (ccb->ccb_h.func_code) {
2211 
2212 	/* perform SCSI I/O */
2213     case XPT_SCSI_IO:
2214 	if (!ciss_cam_action_io(sim, csio))
2215 	    return;
2216 	break;
2217 
2218 	/* perform geometry calculations */
2219     case XPT_CALC_GEOMETRY:
2220     {
2221 	struct ccb_calc_geometry	*ccg = &ccb->ccg;
2222 	struct ciss_ldrive		*ld = &sc->ciss_logical[target];
2223 
2224 	debug(1, "XPT_CALC_GEOMETRY %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2225 
2226 	/*
2227 	 * Use the cached geometry settings unless the fault tolerance
2228 	 * is invalid.
2229 	 */
2230 	if (ld->cl_geometry.fault_tolerance == 0xFF) {
2231 	    u_int32_t			secs_per_cylinder;
2232 
2233 	    ccg->heads = 255;
2234 	    ccg->secs_per_track = 32;
2235 	    secs_per_cylinder = ccg->heads * ccg->secs_per_track;
2236 	    ccg->cylinders = ccg->volume_size / secs_per_cylinder;
2237 	} else {
2238 	    ccg->heads = ld->cl_geometry.heads;
2239 	    ccg->secs_per_track = ld->cl_geometry.sectors;
2240 	    ccg->cylinders = ntohs(ld->cl_geometry.cylinders);
2241 	}
2242 	ccb->ccb_h.status = CAM_REQ_CMP;
2243         break;
2244     }
2245 
2246 	/* handle path attribute inquiry */
2247     case XPT_PATH_INQ:
2248     {
2249 	struct ccb_pathinq	*cpi = &ccb->cpi;
2250 
2251 	debug(1, "XPT_PATH_INQ %d:%d:%d", cam_sim_bus(sim), ccb->ccb_h.target_id, ccb->ccb_h.target_lun);
2252 
2253 	cpi->version_num = 1;
2254 	cpi->hba_inquiry = PI_TAG_ABLE;	/* XXX is this correct? */
2255 	cpi->target_sprt = 0;
2256 	cpi->hba_misc = 0;
2257 	cpi->max_target = CISS_MAX_LOGICAL;
2258 	cpi->max_lun = 0;		/* 'logical drive' channel only */
2259 	cpi->initiator_id = CISS_MAX_LOGICAL;
2260 	strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
2261         strncpy(cpi->hba_vid, "msmith@freebsd.org", HBA_IDLEN);
2262         strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
2263         cpi->unit_number = cam_sim_unit(sim);
2264         cpi->bus_id = cam_sim_bus(sim);
2265 	cpi->base_transfer_speed = 132 * 1024;	/* XXX what to set this to? */
2266 	ccb->ccb_h.status = CAM_REQ_CMP;
2267 	break;
2268     }
2269 
2270     case XPT_GET_TRAN_SETTINGS:
2271     {
2272 	struct ccb_trans_settings	*cts = &ccb->cts;
2273 	int				bus, target;
2274 
2275 	bus = cam_sim_bus(sim);
2276 	target = cts->ccb_h.target_id;
2277 
2278 	debug(1, "XPT_GET_TRAN_SETTINGS %d:%d", bus, target);
2279 	cts->valid = 0;
2280 
2281 	/* disconnect always OK */
2282 	cts->flags |= CCB_TRANS_DISC_ENB;
2283 	cts->valid |= CCB_TRANS_DISC_VALID;
2284 
2285 	cts->ccb_h.status = CAM_REQ_CMP;
2286 	break;
2287     }
2288 
2289     default:		/* we can't do this */
2290 	debug(1, "unspported func_code = 0x%x", ccb->ccb_h.func_code);
2291 	ccb->ccb_h.status = CAM_REQ_INVALID;
2292 	break;
2293     }
2294 
2295     xpt_done(ccb);
2296 }
2297 
2298 /************************************************************************
2299  * Handle a CAM SCSI I/O request.
2300  */
2301 static int
2302 ciss_cam_action_io(struct cam_sim *sim, struct ccb_scsiio *csio)
2303 {
2304     struct ciss_softc	*sc;
2305     int			bus, target;
2306     struct ciss_request	*cr;
2307     struct ciss_command	*cc;
2308     int			error;
2309 
2310     sc = cam_sim_softc(sim);
2311     bus = cam_sim_bus(sim);
2312     target = csio->ccb_h.target_id;
2313 
2314     debug(2, "XPT_SCSI_IO %d:%d:%d", bus, target, csio->ccb_h.target_lun);
2315 
2316     /* check for I/O attempt to nonexistent device */
2317     if ((bus != 0) ||
2318 	(target >= CISS_MAX_LOGICAL)) {
2319 	debug(3, "  device does not exist");
2320 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2321     }
2322 
2323     /* firmware does not support commands > 10 bytes */
2324     if (csio->cdb_len > 12/*CISS_CDB_BUFFER_SIZE*/) {
2325 	debug(3, "  command too large (%d > %d)", csio->cdb_len, CISS_CDB_BUFFER_SIZE);
2326 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2327     }
2328 
2329     /* check that the CDB pointer is not to a physical address */
2330     if ((csio->ccb_h.flags & CAM_CDB_POINTER) && (csio->ccb_h.flags & CAM_CDB_PHYS)) {
2331 	debug(3, "  CDB pointer is to physical address");
2332 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2333     }
2334 
2335     /* if there is data transfer, it must be to/from a virtual address */
2336     if ((csio->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
2337 	if (csio->ccb_h.flags & CAM_DATA_PHYS) {		/* we can't map it */
2338 	    debug(3, "  data pointer is to physical address");
2339 	    csio->ccb_h.status = CAM_REQ_CMP_ERR;
2340 	}
2341 	if (csio->ccb_h.flags & CAM_SCATTER_VALID) {	/* we want to do the s/g setup */
2342 	    debug(3, "  data has premature s/g setup");
2343 	    csio->ccb_h.status = CAM_REQ_CMP_ERR;
2344 	}
2345     }
2346 
2347     /* abandon aborted ccbs or those that have failed validation */
2348     if ((csio->ccb_h.status & CAM_STATUS_MASK) != CAM_REQ_INPROG) {
2349 	debug(3, "abandoning CCB due to abort/validation failure");
2350 	return(EINVAL);
2351     }
2352 
2353     /* handle emulation of some SCSI commands ourself */
2354     if (ciss_cam_emulate(sc, csio))
2355 	return(0);
2356 
2357     /*
2358      * Get a request to manage this command.  If we can't, return the
2359      * ccb, freeze the queue and flag so that we unfreeze it when a
2360      * request completes.
2361      */
2362     if ((error = ciss_get_request(sc, &cr)) != 0) {
2363 	xpt_freeze_simq(sc->ciss_cam_sim, 1);
2364 	csio->ccb_h.status |= CAM_REQUEUE_REQ;
2365 	return(error);
2366     }
2367 
2368     /*
2369      * Build the command.
2370      */
2371     cc = CISS_FIND_COMMAND(cr);
2372     cr->cr_data = csio->data_ptr;
2373     cr->cr_length = csio->dxfer_len;
2374     cr->cr_complete = ciss_cam_complete;
2375     cr->cr_private = csio;
2376 
2377     cc->header.address.logical.mode = CISS_HDR_ADDRESS_MODE_LOGICAL;
2378     cc->header.address.logical.lun = target;
2379     cc->cdb.cdb_length = csio->cdb_len;
2380     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2381     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;	/* XXX ordered tags? */
2382     if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_OUT) {
2383 	cr->cr_flags = CISS_REQ_DATAOUT;
2384 	cc->cdb.direction = CISS_CDB_DIRECTION_WRITE;
2385     } else if ((csio->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN) {
2386 	cr->cr_flags = CISS_REQ_DATAIN;
2387 	cc->cdb.direction = CISS_CDB_DIRECTION_READ;
2388     } else {
2389 	cr->cr_flags = 0;
2390 	cc->cdb.direction = CISS_CDB_DIRECTION_NONE;
2391     }
2392     cc->cdb.timeout = (csio->ccb_h.timeout / 1000) + 1;
2393     if (csio->ccb_h.flags & CAM_CDB_POINTER) {
2394 	bcopy(csio->cdb_io.cdb_ptr, &cc->cdb.cdb[0], csio->cdb_len);
2395     } else {
2396 	bcopy(csio->cdb_io.cdb_bytes, &cc->cdb.cdb[0], csio->cdb_len);
2397     }
2398 
2399     /*
2400      * Submit the request to the adapter.
2401      *
2402      * Note that this may fail if we're unable to map the request (and
2403      * if we ever learn a transport layer other than simple, may fail
2404      * if the adapter rejects the command).
2405      */
2406     if ((error = ciss_start(cr)) != 0) {
2407 	xpt_freeze_simq(sc->ciss_cam_sim, 1);
2408 	if (error == EINPROGRESS) {
2409 	    csio->ccb_h.status |= CAM_RELEASE_SIMQ;
2410 	    error = 0;
2411 	} else {
2412 	    csio->ccb_h.status |= CAM_REQUEUE_REQ;
2413 	    ciss_release_request(cr);
2414 	}
2415 	return(error);
2416     }
2417 
2418     return(0);
2419 }
2420 
2421 /************************************************************************
2422  * Emulate SCSI commands the adapter doesn't handle as we might like.
2423  */
2424 static int
2425 ciss_cam_emulate(struct ciss_softc *sc, struct ccb_scsiio *csio)
2426 {
2427     int		target;
2428     u_int8_t	opcode;
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     /* if we have to fake Synchronise Cache */
2465     if (sc->ciss_flags & CISS_FLAG_FAKE_SYNCH) {
2466 	/*
2467 	 * If this is a Synchronise Cache command, typically issued when
2468 	 * a device is closed, flush the adapter and complete now.
2469 	 */
2470 	if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
2471 	     *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == SYNCHRONIZE_CACHE) {
2472 	    ciss_flush_adapter(sc);
2473 	    csio->ccb_h.status = CAM_REQ_CMP;
2474 	    xpt_done((union ccb *)csio);
2475 	    return(1);
2476 	}
2477     }
2478 
2479     return(0);
2480 }
2481 
2482 /************************************************************************
2483  * Check for possibly-completed commands.
2484  */
2485 static void
2486 ciss_cam_poll(struct cam_sim *sim)
2487 {
2488     struct ciss_softc	*sc = cam_sim_softc(sim);
2489 
2490     debug_called(2);
2491 
2492     ciss_done(sc);
2493 }
2494 
2495 /************************************************************************
2496  * Handle completion of a command - pass results back through the CCB
2497  */
2498 static void
2499 ciss_cam_complete(struct ciss_request *cr)
2500 {
2501     struct ciss_softc		*sc;
2502     struct ciss_command		*cc;
2503     struct ciss_error_info	*ce;
2504     struct ccb_scsiio		*csio;
2505     int				scsi_status;
2506     int				command_status;
2507 
2508     debug_called(2);
2509 
2510     sc = cr->cr_sc;
2511     cc = CISS_FIND_COMMAND(cr);
2512     ce = (struct ciss_error_info *)&(cc->sg[0]);
2513     csio = (struct ccb_scsiio *)cr->cr_private;
2514 
2515     /*
2516      * Extract status values from request.
2517      */
2518     ciss_report_request(cr, &command_status, &scsi_status);
2519     csio->scsi_status = scsi_status;
2520 
2521     /*
2522      * Handle specific SCSI status values.
2523      */
2524     switch(scsi_status) {
2525 	/* no status due to adapter error */
2526     case -1:
2527 	debug(0, "adapter error");
2528 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2529 	break;
2530 
2531 	/* no status due to command completed OK */
2532     case SCSI_STATUS_OK:		/* CISS_SCSI_STATUS_GOOD */
2533 	debug(2, "SCSI_STATUS_OK");
2534 	csio->ccb_h.status = CAM_REQ_CMP;
2535 	break;
2536 
2537 	/* check condition, sense data included */
2538     case SCSI_STATUS_CHECK_COND:	/* CISS_SCSI_STATUS_CHECK_CONDITION */
2539 	debug(0, "SCSI_STATUS_CHECK_COND  sense size %d  resid %d",
2540 	      ce->sense_length, ce->residual_count);
2541 	bzero(&csio->sense_data, SSD_FULL_SIZE);
2542 	bcopy(&ce->sense_info[0], &csio->sense_data, ce->sense_length);
2543 	csio->sense_len = ce->sense_length;
2544 	csio->resid = ce->residual_count;
2545 	csio->ccb_h.status = CAM_SCSI_STATUS_ERROR | CAM_AUTOSNS_VALID;
2546 #ifdef CISS_DEBUG
2547 	{
2548 	    struct scsi_sense_data	*sns = (struct scsi_sense_data *)&ce->sense_info[0];
2549 	    debug(0, "sense key %x", sns->flags & SSD_KEY);
2550 	}
2551 #endif
2552 	break;
2553 
2554     case SCSI_STATUS_BUSY:		/* CISS_SCSI_STATUS_BUSY */
2555 	debug(0, "SCSI_STATUS_BUSY");
2556 	csio->ccb_h.status = CAM_SCSI_BUSY;
2557 	break;
2558 
2559     default:
2560 	debug(0, "unknown status 0x%x", csio->scsi_status);
2561 	csio->ccb_h.status = CAM_REQ_CMP_ERR;
2562 	break;
2563     }
2564 
2565     /* handle post-command fixup */
2566     ciss_cam_complete_fixup(sc, csio);
2567 
2568     /* tell CAM we're ready for more commands */
2569     csio->ccb_h.status |= CAM_RELEASE_SIMQ;
2570 
2571     xpt_done((union ccb *)csio);
2572     ciss_release_request(cr);
2573 }
2574 
2575 /********************************************************************************
2576  * Fix up the result of some commands here.
2577  */
2578 static void
2579 ciss_cam_complete_fixup(struct ciss_softc *sc, struct ccb_scsiio *csio)
2580 {
2581     struct scsi_inquiry_data	*inq;
2582     struct ciss_ldrive		*cl;
2583     int				target;
2584 
2585     if (((csio->ccb_h.flags & CAM_CDB_POINTER) ?
2586 	 *(u_int8_t *)csio->cdb_io.cdb_ptr : csio->cdb_io.cdb_bytes[0]) == INQUIRY) {
2587 
2588 	inq = (struct scsi_inquiry_data *)csio->data_ptr;
2589 	target = csio->ccb_h.target_id;
2590 	cl = &sc->ciss_logical[target];
2591 
2592 	padstr(inq->vendor, "COMPAQ", 8);
2593 	padstr(inq->product, ciss_name_ldrive_org(cl->cl_ldrive->fault_tolerance), 8);
2594 	padstr(inq->revision, ciss_name_ldrive_status(cl->cl_lstatus->status), 16);
2595     }
2596 }
2597 
2598 
2599 /********************************************************************************
2600  * Find a peripheral attached at (target)
2601  */
2602 static struct cam_periph *
2603 ciss_find_periph(struct ciss_softc *sc, int target)
2604 {
2605     struct cam_periph	*periph;
2606     struct cam_path	*path;
2607     int			status;
2608 
2609     status = xpt_create_path(&path, NULL, cam_sim_path(sc->ciss_cam_sim), target, 0);
2610     if (status == CAM_REQ_CMP) {
2611 	periph = cam_periph_find(path, NULL);
2612 	xpt_free_path(path);
2613     } else {
2614 	periph = NULL;
2615     }
2616     return(periph);
2617 }
2618 
2619 /********************************************************************************
2620  * Name the device at (target)
2621  *
2622  * XXX is this strictly correct?
2623  */
2624 static int
2625 ciss_name_device(struct ciss_softc *sc, int target)
2626 {
2627     struct cam_periph	*periph;
2628 
2629     if ((periph = ciss_find_periph(sc, target)) != NULL) {
2630 	sprintf(sc->ciss_logical[target].cl_name, "%s%d", periph->periph_name, periph->unit_number);
2631 	return(0);
2632     }
2633     sc->ciss_logical[target].cl_name[0] = 0;
2634     return(ENOENT);
2635 }
2636 
2637 /************************************************************************
2638  * Periodic status monitoring.
2639  */
2640 static void
2641 ciss_periodic(void *arg)
2642 {
2643     struct ciss_softc	*sc;
2644 
2645     debug_called(1);
2646 
2647     sc = (struct ciss_softc *)arg;
2648 
2649     /*
2650      * Check the adapter heartbeat.
2651      */
2652     if (sc->ciss_cfg->heartbeat == sc->ciss_heartbeat) {
2653 	sc->ciss_heart_attack++;
2654 	debug(0, "adapter heart attack in progress 0x%x/%d",
2655 	      sc->ciss_heartbeat, sc->ciss_heart_attack);
2656 	if (sc->ciss_heart_attack == 3) {
2657 	    ciss_printf(sc, "ADAPTER HEARTBEAT FAILED\n");
2658 	    /* XXX should reset adapter here */
2659 	}
2660     } else {
2661 	sc->ciss_heartbeat = sc->ciss_cfg->heartbeat;
2662 	sc->ciss_heart_attack = 0;
2663 	debug(3, "new heartbeat 0x%x", sc->ciss_heartbeat);
2664     }
2665 
2666     /*
2667      * If the notify event request has died for some reason, or has
2668      * not started yet, restart it.
2669      */
2670     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK)) {
2671 	debug(0, "(re)starting Event Notify chain");
2672 	ciss_notify_event(sc);
2673     }
2674 
2675     /*
2676      * Reschedule.
2677      */
2678     if (!(sc->ciss_flags & CISS_FLAG_ABORTING))
2679 	sc->ciss_periodic = timeout(ciss_periodic, sc, CISS_HEARTBEAT_RATE * hz);
2680 }
2681 
2682 /************************************************************************
2683  * Request a notification response from the adapter.
2684  *
2685  * If (cr) is NULL, this is the first request of the adapter, so
2686  * reset the adapter's message pointer and start with the oldest
2687  * message available.
2688  */
2689 static void
2690 ciss_notify_event(struct ciss_softc *sc)
2691 {
2692     struct ciss_request		*cr;
2693     struct ciss_command		*cc;
2694     struct ciss_notify_cdb	*cnc;
2695     int				error;
2696 
2697     debug_called(1);
2698 
2699     cr = sc->ciss_periodic_notify;
2700 
2701     /* get a request if we don't already have one */
2702     if (cr == NULL) {
2703 	if ((error = ciss_get_request(sc, &cr)) != 0) {
2704 	    debug(0, "can't get notify event request");
2705 	    goto out;
2706 	}
2707 	sc->ciss_periodic_notify = cr;
2708 	cr->cr_complete = ciss_notify_complete;
2709 	debug(1, "acquired request %d", cr->cr_tag);
2710     }
2711 
2712     /*
2713      * Get a databuffer if we don't already have one, note that the
2714      * adapter command wants a larger buffer than the actual
2715      * structure.
2716      */
2717     if (cr->cr_data == NULL) {
2718 	if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
2719 	    debug(0, "can't get notify event request buffer");
2720 	    error = ENOMEM;
2721 	    goto out;
2722 	}
2723 	cr->cr_length = CISS_NOTIFY_DATA_SIZE;
2724     }
2725 
2726     /* re-setup the request's command (since we never release it) XXX overkill*/
2727     ciss_preen_command(cr);
2728 
2729     /* (re)build the notify event command */
2730     cc = CISS_FIND_COMMAND(cr);
2731     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2732     cc->header.address.physical.bus = 0;
2733     cc->header.address.physical.target = 0;
2734 
2735     cc->cdb.cdb_length = sizeof(*cnc);
2736     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2737     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2738     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
2739     cc->cdb.timeout = 0;	/* no timeout, we hope */
2740 
2741     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
2742     bzero(cr->cr_data, CISS_NOTIFY_DATA_SIZE);
2743     cnc->opcode = CISS_OPCODE_READ;
2744     cnc->command = CISS_COMMAND_NOTIFY_ON_EVENT;
2745     cnc->timeout = 0;		/* no timeout, we hope */
2746     cnc->synchronous = 0;
2747     cnc->ordered = 0;
2748     cnc->seek_to_oldest = 0;
2749     cnc->new_only = 0;
2750     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
2751 
2752     /* submit the request */
2753     error = ciss_start(cr);
2754 
2755  out:
2756     if (error) {
2757 	if (cr != NULL) {
2758 	    if (cr->cr_data != NULL)
2759 		free(cr->cr_data, CISS_MALLOC_CLASS);
2760 	    ciss_release_request(cr);
2761 	}
2762 	sc->ciss_periodic_notify = NULL;
2763 	debug(0, "can't submit notify event request");
2764 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2765     } else {
2766 	debug(1, "notify event submitted");
2767 	sc->ciss_flags |= CISS_FLAG_NOTIFY_OK;
2768     }
2769 }
2770 
2771 static void
2772 ciss_notify_complete(struct ciss_request *cr)
2773 {
2774     struct ciss_command	*cc;
2775     struct ciss_notify	*cn;
2776     struct ciss_softc	*sc;
2777     int			scsi_status;
2778     int			command_status;
2779 
2780     debug_called(1);
2781 
2782     cc = CISS_FIND_COMMAND(cr);
2783     cn = (struct ciss_notify *)cr->cr_data;
2784     sc = cr->cr_sc;
2785 
2786     /*
2787      * Report request results, decode status.
2788      */
2789     ciss_report_request(cr, &command_status, &scsi_status);
2790 
2791     /*
2792      * Abort the chain on a fatal error.
2793      *
2794      * XXX which of these are actually errors?
2795      */
2796     if ((command_status != CISS_CMD_STATUS_SUCCESS) &&
2797 	(command_status != CISS_CMD_STATUS_TARGET_STATUS) &&
2798 	(command_status != CISS_CMD_STATUS_TIMEOUT)) {	/* XXX timeout? */
2799 	ciss_printf(sc, "fatal error in Notify Event request (%s)\n",
2800 		    ciss_name_command_status(command_status));
2801 	ciss_release_request(cr);
2802 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2803 	return;
2804     }
2805 
2806     /*
2807      * If the adapter gave us a text message, print it.
2808      */
2809     if (cn->message[0] != 0)
2810 	ciss_printf(sc, "*** %.80s\n", cn->message);
2811 
2812     debug(0, "notify event class %d subclass %d detail %d",
2813 		cn->class, cn->subclass, cn->detail);
2814 
2815     /*
2816      * If there's room, save the event for a user-level tool.
2817      */
2818     if (((sc->ciss_notify_head + 1) % CISS_MAX_EVENTS) != sc->ciss_notify_tail) {
2819 	sc->ciss_notify[sc->ciss_notify_head] = *cn;
2820 	sc->ciss_notify_head = (sc->ciss_notify_head + 1) % CISS_MAX_EVENTS;
2821     }
2822 
2823     /*
2824      * Some events are directly of interest to us.
2825      */
2826     switch (cn->class) {
2827     case CISS_NOTIFY_LOGICAL:
2828 	ciss_notify_logical(sc, cn);
2829 	break;
2830     case CISS_NOTIFY_PHYSICAL:
2831 	ciss_notify_physical(sc, cn);
2832 	break;
2833     }
2834 
2835     /*
2836      * If the response indicates that the notifier has been aborted,
2837      * release the notifier command.
2838      */
2839     if ((cn->class == CISS_NOTIFY_NOTIFIER) &&
2840 	(cn->subclass == CISS_NOTIFY_NOTIFIER_STATUS) &&
2841 	(cn->detail == 1)) {
2842 	debug(0, "notifier exiting");
2843 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2844 	ciss_release_request(cr);
2845 	sc->ciss_periodic_notify = NULL;
2846 	wakeup(&sc->ciss_periodic_notify);
2847     }
2848 
2849     /*
2850      * Send a new notify event command, if we're not aborting.
2851      */
2852     if (!(sc->ciss_flags & CISS_FLAG_ABORTING)) {
2853 	ciss_notify_event(sc);
2854     }
2855 }
2856 
2857 /************************************************************************
2858  * Abort the Notify Event chain.
2859  *
2860  * Note that we can't just abort the command in progress; we have to
2861  * explicitly issue an Abort Notify Event command in order for the
2862  * adapter to clean up correctly.
2863  *
2864  * If we are called with CISS_FLAG_ABORTING set in the adapter softc,
2865  * the chain will not restart itself.
2866  */
2867 static int
2868 ciss_notify_abort(struct ciss_softc *sc)
2869 {
2870     struct ciss_request		*cr;
2871     struct ciss_command		*cc;
2872     struct ciss_notify_cdb	*cnc;
2873     int				error, s, command_status, scsi_status;
2874 
2875     debug_called(1);
2876 
2877     cr = NULL;
2878     error = 0;
2879 
2880     /* verify that there's an outstanding command */
2881     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
2882 	goto out;
2883 
2884     /* get a command to issue the abort with */
2885     if ((error = ciss_get_request(sc, &cr)))
2886 	goto out;
2887 
2888     /* get a buffer for the result */
2889     if ((cr->cr_data = malloc(CISS_NOTIFY_DATA_SIZE, CISS_MALLOC_CLASS, M_NOWAIT)) == NULL) {
2890 	debug(0, "can't get notify event request buffer");
2891 	error = ENOMEM;
2892 	goto out;
2893     }
2894     cr->cr_length = CISS_NOTIFY_DATA_SIZE;
2895 
2896     /* build the CDB */
2897     cc = CISS_FIND_COMMAND(cr);
2898     cc->header.address.physical.mode = CISS_HDR_ADDRESS_MODE_PERIPHERAL;
2899     cc->header.address.physical.bus = 0;
2900     cc->header.address.physical.target = 0;
2901     cc->cdb.cdb_length = sizeof(*cnc);
2902     cc->cdb.type = CISS_CDB_TYPE_COMMAND;
2903     cc->cdb.attribute = CISS_CDB_ATTRIBUTE_SIMPLE;
2904     cc->cdb.direction = CISS_CDB_DIRECTION_READ;
2905     cc->cdb.timeout = 0;	/* no timeout, we hope */
2906 
2907     cnc = (struct ciss_notify_cdb *)&(cc->cdb.cdb[0]);
2908     bzero(cnc, sizeof(*cnc));
2909     cnc->opcode = CISS_OPCODE_WRITE;
2910     cnc->command = CISS_COMMAND_ABORT_NOTIFY;
2911     cnc->length = htonl(CISS_NOTIFY_DATA_SIZE);
2912 
2913     ciss_print_request(cr);
2914 
2915     /*
2916      * Submit the request and wait for it to complete.
2917      */
2918     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
2919 	ciss_printf(sc, "Abort Notify Event command failed (%d)\n", error);
2920 	goto out;
2921     }
2922 
2923     /*
2924      * Check response.
2925      */
2926     ciss_report_request(cr, &command_status, &scsi_status);
2927     switch(command_status) {
2928     case CISS_CMD_STATUS_SUCCESS:
2929 	break;
2930     case CISS_CMD_STATUS_INVALID_COMMAND:
2931 	/*
2932 	 * Some older adapters don't support the CISS version of this
2933 	 * command.  Fall back to using the BMIC version.
2934 	 */
2935 	error = ciss_notify_abort_bmic(sc);
2936 	if (error != 0)
2937 	    goto out;
2938 	break;
2939 
2940     case CISS_CMD_STATUS_TARGET_STATUS:
2941 	/*
2942 	 * This can happen if the adapter thinks there wasn't an outstanding
2943 	 * Notify Event command but we did.  We clean up here.
2944 	 */
2945 	if (scsi_status == CISS_SCSI_STATUS_CHECK_CONDITION) {
2946 	    if (sc->ciss_periodic_notify != NULL)
2947 		ciss_release_request(sc->ciss_periodic_notify);
2948 	    error = 0;
2949 	    goto out;
2950 	}
2951 	/* FALLTHROUGH */
2952 
2953     default:
2954 	ciss_printf(sc, "Abort Notify Event command failed (%s)\n",
2955 		    ciss_name_command_status(command_status));
2956 	error = EIO;
2957 	goto out;
2958     }
2959 
2960     /*
2961      * Sleep waiting for the notifier command to complete.  Note
2962      * that if it doesn't, we may end up in a bad situation, since
2963      * the adapter may deliver it later.  Also note that the adapter
2964      * requires the Notify Event command to be cancelled in order to
2965      * maintain internal bookkeeping.
2966      */
2967     s = splcam();
2968     while (sc->ciss_periodic_notify != NULL) {
2969 	error = tsleep(&sc->ciss_periodic_notify, 0, "cissNEA", hz * 5);
2970 	if (error == EWOULDBLOCK) {
2971 	    ciss_printf(sc, "Notify Event command failed to abort, adapter may wedge.\n");
2972 	    break;
2973 	}
2974     }
2975     splx(s);
2976 
2977  out:
2978     /* release the cancel request */
2979     if (cr != NULL) {
2980 	if (cr->cr_data != NULL)
2981 	    free(cr->cr_data, CISS_MALLOC_CLASS);
2982 	ciss_release_request(cr);
2983     }
2984     if (error == 0)
2985 	sc->ciss_flags &= ~CISS_FLAG_NOTIFY_OK;
2986     return(error);
2987 }
2988 
2989 /************************************************************************
2990  * Abort the Notify Event chain using a BMIC command.
2991  */
2992 static int
2993 ciss_notify_abort_bmic(struct ciss_softc *sc)
2994 {
2995     struct ciss_request			*cr;
2996     int					error, command_status;
2997 
2998     debug_called(1);
2999 
3000     cr = NULL;
3001     error = 0;
3002 
3003     /* verify that there's an outstanding command */
3004     if (!(sc->ciss_flags & CISS_FLAG_NOTIFY_OK))
3005 	goto out;
3006 
3007     /*
3008      * Build a BMIC command to cancel the Notify on Event command.
3009      *
3010      * Note that we are sending a CISS opcode here.  Odd.
3011      */
3012     if ((error = ciss_get_bmic_request(sc, &cr, CISS_COMMAND_ABORT_NOTIFY,
3013 				       NULL, 0)) != 0)
3014 	goto out;
3015 
3016     /*
3017      * Submit the request and wait for it to complete.
3018      */
3019     if ((error = ciss_synch_request(cr, 60 * 1000)) != 0) {
3020 	ciss_printf(sc, "error sending BMIC Cancel Notify on Event command (%d)\n", error);
3021 	goto out;
3022     }
3023 
3024     /*
3025      * Check response.
3026      */
3027     ciss_report_request(cr, &command_status, NULL);
3028     switch(command_status) {
3029     case CISS_CMD_STATUS_SUCCESS:
3030 	break;
3031     default:
3032 	ciss_printf(sc, "error cancelling Notify on Event (%s)\n",
3033 		    ciss_name_command_status(command_status));
3034 	error = EIO;
3035 	goto out;
3036     }
3037 
3038 out:
3039     if (cr != NULL)
3040 	ciss_release_request(cr);
3041     return(error);
3042 }
3043 
3044 /************************************************************************
3045  * Handle a notify event relating to the status of a logical drive.
3046  *
3047  * XXX need to be able to defer some of these to properly handle
3048  *     calling the "ID Physical drive" command, unless the 'extended'
3049  *     drive IDs are always in BIG_MAP format.
3050  */
3051 static void
3052 ciss_notify_logical(struct ciss_softc *sc, struct ciss_notify *cn)
3053 {
3054     struct ciss_ldrive	*ld;
3055     int			ostatus;
3056 
3057     debug_called(2);
3058 
3059     ld = &sc->ciss_logical[cn->data.logical_status.logical_drive];
3060 
3061     switch (cn->subclass) {
3062     case CISS_NOTIFY_LOGICAL_STATUS:
3063 	switch (cn->detail) {
3064 	case 0:
3065 	    ciss_name_device(sc, cn->data.logical_status.logical_drive);
3066 	    ciss_printf(sc, "logical drive %d (%s) changed status %s->%s, spare status 0x%b\n",
3067 			cn->data.logical_status.logical_drive, ld->cl_name,
3068 			ciss_name_ldrive_status(cn->data.logical_status.previous_state),
3069 			ciss_name_ldrive_status(cn->data.logical_status.new_state),
3070 			cn->data.logical_status.spare_state,
3071 			"\20\1configured\2rebuilding\3failed\4in use\5available\n");
3072 
3073 	    /*
3074 	     * Update our idea of the drive's status.
3075 	     */
3076 	    ostatus = ciss_decode_ldrive_status(cn->data.logical_status.previous_state);
3077 	    ld->cl_status = ciss_decode_ldrive_status(cn->data.logical_status.new_state);
3078 	    if (ld->cl_lstatus != NULL)
3079 		ld->cl_lstatus->status = cn->data.logical_status.new_state;
3080 
3081 #if 0
3082 	    /*
3083 	     * Have CAM rescan the drive if its status has changed.
3084 	     */
3085 	    if (ostatus != ld->cl_status)
3086 		ciss_cam_rescan_target(sc, cn->data.logical_status.logical_drive);
3087 #endif
3088 
3089 	    break;
3090 
3091 	case 1:	/* logical drive has recognised new media, needs Accept Media Exchange */
3092 	    ciss_name_device(sc, cn->data.logical_status.logical_drive);
3093 	    ciss_printf(sc, "logical drive %d (%s) media exchanged, ready to go online\n",
3094 			cn->data.logical_status.logical_drive, ld->cl_name);
3095 	    ciss_accept_media(sc, cn->data.logical_status.logical_drive, 1);
3096 	    break;
3097 
3098 	case 2:
3099 	case 3:
3100 	    ciss_printf(sc, "rebuild of logical drive %d (%s) failed due to %s error\n",
3101 			cn->data.rebuild_aborted.logical_drive,
3102 			sc->ciss_logical[cn->data.rebuild_aborted.logical_drive].cl_name,
3103 			(cn->detail == 2) ? "read" : "write");
3104 	    break;
3105 	}
3106 	break;
3107 
3108     case CISS_NOTIFY_LOGICAL_ERROR:
3109 	if (cn->detail == 0) {
3110 	    ciss_printf(sc, "FATAL I/O ERROR on logical drive %d (%s), SCSI port %d ID %d\n",
3111 			cn->data.io_error.logical_drive,
3112 			sc->ciss_logical[cn->data.io_error.logical_drive].cl_name,
3113 			cn->data.io_error.failure_bus,
3114 			cn->data.io_error.failure_drive);
3115 	    /* XXX should we take the drive down at this point, or will we be told? */
3116 	}
3117 	break;
3118 
3119     case CISS_NOTIFY_LOGICAL_SURFACE:
3120 	if (cn->detail == 0)
3121 	    ciss_printf(sc, "logical drive %d (%s) completed consistency initialisation\n",
3122 			cn->data.consistency_completed.logical_drive,
3123 			sc->ciss_logical[cn->data.consistency_completed.logical_drive].cl_name);
3124 	break;
3125     }
3126 }
3127 
3128 /************************************************************************
3129  * Handle a notify event relating to the status of a physical drive.
3130  */
3131 static void
3132 ciss_notify_physical(struct ciss_softc *sc, struct ciss_notify *cn)
3133 {
3134 
3135 }
3136 
3137 /************************************************************************
3138  * Print a request.
3139  */
3140 static void
3141 ciss_print_request(struct ciss_request *cr)
3142 {
3143     struct ciss_softc	*sc;
3144     struct ciss_command	*cc;
3145     int			i;
3146 
3147     sc = cr->cr_sc;
3148     cc = CISS_FIND_COMMAND(cr);
3149 
3150     ciss_printf(sc, "REQUEST @ %p\n", cr);
3151     ciss_printf(sc, "  data %p/%d  tag %d  flags %b\n",
3152 	      cr->cr_data, cr->cr_length, cr->cr_tag, cr->cr_flags,
3153 	      "\20\1mapped\2sleep\3poll\4dataout\5datain\n");
3154     ciss_printf(sc, "  sg list/total %d/%d  host tag 0x%x\n",
3155 		cc->header.sg_in_list, cc->header.sg_total, cc->header.host_tag);
3156     switch(cc->header.address.mode.mode) {
3157     case CISS_HDR_ADDRESS_MODE_PERIPHERAL:
3158     case CISS_HDR_ADDRESS_MODE_MASK_PERIPHERAL:
3159 	ciss_printf(sc, "  physical bus %d target %d\n",
3160 		    cc->header.address.physical.bus, cc->header.address.physical.target);
3161 	break;
3162     case CISS_HDR_ADDRESS_MODE_LOGICAL:
3163 	ciss_printf(sc, "  logical unit %d\n", cc->header.address.logical.lun);
3164 	break;
3165     }
3166     ciss_printf(sc, "  %s cdb length %d type %s attribute %s\n",
3167 		(cc->cdb.direction == CISS_CDB_DIRECTION_NONE) ? "no-I/O" :
3168 		(cc->cdb.direction == CISS_CDB_DIRECTION_READ) ? "READ" :
3169 		(cc->cdb.direction == CISS_CDB_DIRECTION_WRITE) ? "WRITE" : "??",
3170 		cc->cdb.cdb_length,
3171 		(cc->cdb.type == CISS_CDB_TYPE_COMMAND) ? "command" :
3172 		(cc->cdb.type == CISS_CDB_TYPE_MESSAGE) ? "message" : "??",
3173 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_UNTAGGED) ? "untagged" :
3174 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_SIMPLE) ? "simple" :
3175 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_HEAD_OF_QUEUE) ? "head-of-queue" :
3176 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_ORDERED) ? "ordered" :
3177 		(cc->cdb.attribute == CISS_CDB_ATTRIBUTE_AUTO_CONTINGENT) ? "auto-contingent" : "??");
3178     ciss_printf(sc, "  %*D\n", cc->cdb.cdb_length, &cc->cdb.cdb[0], " ");
3179 
3180     if (cc->header.host_tag & CISS_HDR_HOST_TAG_ERROR) {
3181 	/* XXX print error info */
3182     } else {
3183 	/* since we don't use chained s/g, don't support it here */
3184 	for (i = 0; i < cc->header.sg_in_list; i++) {
3185 	    if ((i % 4) == 0)
3186 		ciss_printf(sc, "   ");
3187 	    printf("0x%08x/%d ", (u_int32_t)cc->sg[i].address, cc->sg[i].length);
3188 	    if ((((i + 1) % 4) == 0) || (i == (cc->header.sg_in_list - 1)))
3189 		printf("\n");
3190 	}
3191     }
3192 }
3193 
3194 /************************************************************************
3195  * Print information about the status of a logical drive.
3196  */
3197 static void
3198 ciss_print_ldrive(struct ciss_softc *sc, struct ciss_ldrive *ld)
3199 {
3200     int		bus, target, i;
3201 
3202     if (ld->cl_lstatus == NULL) {
3203 	printf("does not exist\n");
3204 	return;
3205     }
3206 
3207     /* print drive status */
3208     switch(ld->cl_lstatus->status) {
3209     case CISS_LSTATUS_OK:
3210 	printf("online\n");
3211 	break;
3212     case CISS_LSTATUS_INTERIM_RECOVERY:
3213 	printf("in interim recovery mode\n");
3214 	break;
3215     case CISS_LSTATUS_READY_RECOVERY:
3216 	printf("ready to begin recovery\n");
3217 	break;
3218     case CISS_LSTATUS_RECOVERING:
3219 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
3220 	target = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_rebuilding);
3221 	printf("being recovered, working on physical drive %d.%d, %u blocks remaining\n",
3222 	       bus, target, ld->cl_lstatus->blocks_to_recover);
3223 	break;
3224     case CISS_LSTATUS_EXPANDING:
3225 	printf("being expanded, %u blocks remaining\n",
3226 	       ld->cl_lstatus->blocks_to_recover);
3227 	break;
3228     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
3229 	printf("queued for expansion\n");
3230 	break;
3231     case CISS_LSTATUS_FAILED:
3232 	printf("queued for expansion\n");
3233 	break;
3234     case CISS_LSTATUS_WRONG_PDRIVE:
3235 	printf("wrong physical drive inserted\n");
3236 	break;
3237     case CISS_LSTATUS_MISSING_PDRIVE:
3238 	printf("missing a needed physical drive\n");
3239 	break;
3240     case CISS_LSTATUS_BECOMING_READY:
3241 	printf("becoming ready\n");
3242 	break;
3243     }
3244 
3245     /* print failed physical drives */
3246     for (i = 0; i < CISS_BIG_MAP_ENTRIES / 8; i++) {
3247 	bus = CISS_BIG_MAP_BUS(sc, ld->cl_lstatus->drive_failure_map[i]);
3248 	target = CISS_BIG_MAP_TARGET(sc, ld->cl_lstatus->drive_failure_map[i]);
3249 	if (bus == -1)
3250 	    continue;
3251 	ciss_printf(sc, "physical drive %d:%d (%x) failed\n", bus, target,
3252 		    ld->cl_lstatus->drive_failure_map[i]);
3253     }
3254 }
3255 
3256 #ifdef CISS_DEBUG
3257 /************************************************************************
3258  * Print information about the controller/driver.
3259  */
3260 static void
3261 ciss_print_adapter(struct ciss_softc *sc)
3262 {
3263     int		i;
3264 
3265     ciss_printf(sc, "ADAPTER:\n");
3266     for (i = 0; i < CISSQ_COUNT; i++) {
3267 	ciss_printf(sc, "%s     %d/%d\n",
3268 	    i == 0 ? "free" :
3269 	    i == 1 ? "busy" : "complete",
3270 	    sc->ciss_qstat[i].q_length,
3271 	    sc->ciss_qstat[i].q_max);
3272     }
3273     ciss_printf(sc, "max_requests %d\n", sc->ciss_max_requests);
3274     ciss_printf(sc, "notify_head/tail %d/%d\n",
3275 	sc->ciss_notify_head, sc->ciss_notify_tail);
3276     ciss_printf(sc, "flags %b\n", sc->ciss_flags,
3277 	"\20\1notify_ok\2control_open\3aborting\4running\21fake_synch\22bmic_abort\n");
3278 
3279     for (i = 0; i < CISS_MAX_LOGICAL; i++) {
3280 	ciss_printf(sc, "LOGICAL DRIVE %d:  ", i);
3281 	ciss_print_ldrive(sc, sc->ciss_logical + i);
3282     }
3283 
3284     for (i = 1; i < sc->ciss_max_requests; i++)
3285 	ciss_print_request(sc->ciss_request + i);
3286 }
3287 
3288 /* DDB hook */
3289 static void
3290 ciss_print0(void)
3291 {
3292     struct ciss_softc	*sc;
3293 
3294     sc = devclass_get_softc(devclass_find("ciss"), 0);
3295     if (sc == NULL) {
3296 	printf("no ciss controllers\n");
3297     } else {
3298 	ciss_print_adapter(sc);
3299     }
3300 }
3301 #endif
3302 
3303 /************************************************************************
3304  * Return a name for a logical drive status value.
3305  */
3306 static const char *
3307 ciss_name_ldrive_status(int status)
3308 {
3309     switch (status) {
3310     case CISS_LSTATUS_OK:
3311 	return("OK");
3312     case CISS_LSTATUS_FAILED:
3313 	return("failed");
3314     case CISS_LSTATUS_NOT_CONFIGURED:
3315 	return("not configured");
3316     case CISS_LSTATUS_INTERIM_RECOVERY:
3317 	return("interim recovery");
3318     case CISS_LSTATUS_READY_RECOVERY:
3319 	return("ready for recovery");
3320     case CISS_LSTATUS_RECOVERING:
3321 	return("recovering");
3322     case CISS_LSTATUS_WRONG_PDRIVE:
3323 	return("wrong physical drive inserted");
3324     case CISS_LSTATUS_MISSING_PDRIVE:
3325 	return("missing physical drive");
3326     case CISS_LSTATUS_EXPANDING:
3327 	return("expanding");
3328     case CISS_LSTATUS_BECOMING_READY:
3329 	return("becoming ready");
3330     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
3331 	return("queued for expansion");
3332     }
3333     return("unknown status");
3334 }
3335 
3336 /************************************************************************
3337  * Return an online/offline/nonexistent value for a logical drive
3338  * status value.
3339  */
3340 static int
3341 ciss_decode_ldrive_status(int status)
3342 {
3343     switch(status) {
3344     case CISS_LSTATUS_NOT_CONFIGURED:
3345 	return(CISS_LD_NONEXISTENT);
3346 
3347     case CISS_LSTATUS_OK:
3348     case CISS_LSTATUS_INTERIM_RECOVERY:
3349     case CISS_LSTATUS_READY_RECOVERY:
3350     case CISS_LSTATUS_RECOVERING:
3351     case CISS_LSTATUS_EXPANDING:
3352     case CISS_LSTATUS_QUEUED_FOR_EXPANSION:
3353 	return(CISS_LD_ONLINE);
3354 
3355     case CISS_LSTATUS_FAILED:
3356     case CISS_LSTATUS_WRONG_PDRIVE:
3357     case CISS_LSTATUS_MISSING_PDRIVE:
3358     case CISS_LSTATUS_BECOMING_READY:
3359     default:
3360 	return(CISS_LD_OFFLINE);
3361     }
3362 }
3363 
3364 
3365 /************************************************************************
3366  * Return a name for a logical drive's organisation.
3367  */
3368 static const char *
3369 ciss_name_ldrive_org(int org)
3370 {
3371     switch(org) {
3372     case CISS_LDRIVE_RAID0:
3373 	return("RAID 0");
3374     case CISS_LDRIVE_RAID1:
3375 	return("RAID 1");
3376     case CISS_LDRIVE_RAID4:
3377 	return("RAID 4");
3378     case CISS_LDRIVE_RAID5:
3379 	return("RAID 5");
3380     case CISS_LDRIVE_RAID51:
3381 	return("RAID 5+1");
3382     case CISS_LDRIVE_RAIDADG:
3383 	return("RAID ADG");
3384     }
3385     return("unkown");
3386 }
3387 
3388 /************************************************************************
3389  * Return a name for a command status value.
3390  */
3391 static const char *
3392 ciss_name_command_status(int status)
3393 {
3394     switch(status) {
3395     case CISS_CMD_STATUS_SUCCESS:
3396 	return("success");
3397     case CISS_CMD_STATUS_TARGET_STATUS:
3398 	return("target status");
3399     case CISS_CMD_STATUS_DATA_UNDERRUN:
3400 	return("data underrun");
3401     case CISS_CMD_STATUS_DATA_OVERRUN:
3402 	return("data overrun");
3403     case CISS_CMD_STATUS_INVALID_COMMAND:
3404 	return("invalid command");
3405     case CISS_CMD_STATUS_PROTOCOL_ERROR:
3406 	return("protocol error");
3407     case CISS_CMD_STATUS_HARDWARE_ERROR:
3408 	return("hardware error");
3409     case CISS_CMD_STATUS_CONNECTION_LOST:
3410 	return("connection lost");
3411     case CISS_CMD_STATUS_ABORTED:
3412 	return("aborted");
3413     case CISS_CMD_STATUS_ABORT_FAILED:
3414 	return("abort failed");
3415     case CISS_CMD_STATUS_UNSOLICITED_ABORT:
3416 	return("unsolicited abort");
3417     case CISS_CMD_STATUS_TIMEOUT:
3418 	return("timeout");
3419     case CISS_CMD_STATUS_UNABORTABLE:
3420 	return("unabortable");
3421     }
3422     return("unknown status");
3423 }
3424 
3425 /************************************************************************
3426  * Handle an open on the control device.
3427  */
3428 static int
3429 ciss_open(dev_t dev, int flags, int fmt, d_thread_t *p)
3430 {
3431     struct ciss_softc	*sc;
3432 
3433     debug_called(1);
3434 
3435     sc = (struct ciss_softc *)dev->si_drv1;
3436 
3437     /* we might want to veto if someone already has us open */
3438 
3439     sc->ciss_flags |= CISS_FLAG_CONTROL_OPEN;
3440     return(0);
3441 }
3442 
3443 /************************************************************************
3444  * Handle the last close on the control device.
3445  */
3446 static int
3447 ciss_close(dev_t dev, int flags, int fmt, d_thread_t *p)
3448 {
3449     struct ciss_softc	*sc;
3450 
3451     debug_called(1);
3452 
3453     sc = (struct ciss_softc *)dev->si_drv1;
3454 
3455     sc->ciss_flags &= ~CISS_FLAG_CONTROL_OPEN;
3456     return (0);
3457 }
3458 
3459 /********************************************************************************
3460  * Handle adapter-specific control operations.
3461  *
3462  * Note that the API here is compatible with the Linux driver, in order to
3463  * simplify the porting of Compaq's userland tools.
3464  */
3465 static int
3466 ciss_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *p)
3467 {
3468     struct ciss_softc		*sc;
3469     int				error;
3470 
3471     debug_called(1);
3472 
3473     sc = (struct ciss_softc *)dev->si_drv1;
3474     error = 0;
3475 
3476     switch(cmd) {
3477     case CCISS_GETPCIINFO:
3478     {
3479 	cciss_pci_info_struct	*pis = (cciss_pci_info_struct *)addr;
3480 
3481 	pis->bus = pci_get_bus(sc->ciss_dev);
3482 	pis->dev_fn = pci_get_slot(sc->ciss_dev);
3483 	pis->board_id = pci_get_devid(sc->ciss_dev);
3484 
3485 	break;
3486     }
3487 
3488     case CCISS_GETINTINFO:
3489     {
3490 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
3491 
3492 	cis->delay = sc->ciss_cfg->interrupt_coalesce_delay;
3493 	cis->count = sc->ciss_cfg->interrupt_coalesce_count;
3494 
3495 	break;
3496     }
3497 
3498     case CCISS_SETINTINFO:
3499     {
3500 	cciss_coalint_struct	*cis = (cciss_coalint_struct *)addr;
3501 
3502 	if ((cis->delay == 0) && (cis->count == 0)) {
3503 	    error = EINVAL;
3504 	    break;
3505 	}
3506 
3507 	/*
3508 	 * XXX apparently this is only safe if the controller is idle,
3509 	 *     we should suspend it before doing this.
3510 	 */
3511 	sc->ciss_cfg->interrupt_coalesce_delay = cis->delay;
3512 	sc->ciss_cfg->interrupt_coalesce_count = cis->count;
3513 
3514 	if (ciss_update_config(sc))
3515 	    error = EIO;
3516 
3517 	/* XXX resume the controller here */
3518 	break;
3519     }
3520 
3521     case CCISS_GETNODENAME:
3522 	bcopy(sc->ciss_cfg->server_name, (NodeName_type *)addr,
3523 	      sizeof(NodeName_type));
3524 	break;
3525 
3526     case CCISS_SETNODENAME:
3527 	bcopy((NodeName_type *)addr, sc->ciss_cfg->server_name,
3528 	      sizeof(NodeName_type));
3529 	if (ciss_update_config(sc))
3530 	    error = EIO;
3531 	break;
3532 
3533     case CCISS_GETHEARTBEAT:
3534 	*(Heartbeat_type *)addr = sc->ciss_cfg->heartbeat;
3535 	break;
3536 
3537     case CCISS_GETBUSTYPES:
3538 	*(BusTypes_type *)addr = sc->ciss_cfg->bus_types;
3539 	break;
3540 
3541     case CCISS_GETFIRMVER:
3542 	bcopy(sc->ciss_id->running_firmware_revision, (FirmwareVer_type *)addr,
3543 	      sizeof(FirmwareVer_type));
3544 	break;
3545 
3546     case CCISS_GETDRIVERVER:
3547 	*(DriverVer_type *)addr = CISS_DRIVER_VERSION;
3548 	break;
3549 
3550     case CCISS_REVALIDVOLS:
3551 	/*
3552 	 * This is a bit ugly; to do it "right" we really need
3553 	 * to find any disks that have changed, kick CAM off them,
3554 	 * then rescan only these disks.  It'd be nice if they
3555 	 * a) told us which disk(s) they were going to play with,
3556 	 * and b) which ones had arrived. 8(
3557 	 */
3558 	break;
3559 
3560     case CCISS_PASSTHRU:
3561 	error = ciss_user_command(sc, (IOCTL_Command_struct *)addr);
3562 	break;
3563 
3564     default:
3565 	debug(0, "unknown ioctl 0x%lx", cmd);
3566 
3567 	debug(1, "CCISS_GETPCIINFO:   0x%lx", CCISS_GETPCIINFO);
3568 	debug(1, "CCISS_GETINTINFO:   0x%lx", CCISS_GETINTINFO);
3569 	debug(1, "CCISS_SETINTINFO:   0x%lx", CCISS_SETINTINFO);
3570 	debug(1, "CCISS_GETNODENAME:  0x%lx", CCISS_GETNODENAME);
3571 	debug(1, "CCISS_SETNODENAME:  0x%lx", CCISS_SETNODENAME);
3572 	debug(1, "CCISS_GETHEARTBEAT: 0x%lx", CCISS_GETHEARTBEAT);
3573 	debug(1, "CCISS_GETBUSTYPES:  0x%lx", CCISS_GETBUSTYPES);
3574 	debug(1, "CCISS_GETFIRMVER:   0x%lx", CCISS_GETFIRMVER);
3575 	debug(1, "CCISS_GETDRIVERVER: 0x%lx", CCISS_GETDRIVERVER);
3576 	debug(1, "CCISS_REVALIDVOLS:  0x%lx", CCISS_REVALIDVOLS);
3577 	debug(1, "CCISS_PASSTHRU:     0x%lx", CCISS_PASSTHRU);
3578 
3579 	error = ENOIOCTL;
3580 	break;
3581     }
3582 
3583     return(error);
3584 }
3585