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