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