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