1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 1999 Michael Smith
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 /*
30 * Driver for the Mylex DAC960 family of RAID controllers.
31 */
32
33 #include <sys/param.h>
34 #include <sys/systm.h>
35 #include <sys/bio.h>
36 #include <sys/lock.h>
37 #include <sys/malloc.h>
38 #include <sys/mutex.h>
39 #include <sys/kernel.h>
40 #include <sys/sx.h>
41
42 #include <sys/bus.h>
43 #include <sys/conf.h>
44 #include <sys/stat.h>
45
46 #include <machine/resource.h>
47 #include <machine/bus.h>
48 #include <machine/clock.h>
49 #include <sys/rman.h>
50
51 #include <geom/geom_disk.h>
52
53 #include <dev/mlx/mlxio.h>
54 #include <dev/mlx/mlxvar.h>
55 #include <dev/mlx/mlxreg.h>
56
57 static struct cdevsw mlx_cdevsw = {
58 .d_version = D_VERSION,
59 .d_open = mlx_open,
60 .d_close = mlx_close,
61 .d_ioctl = mlx_ioctl,
62 .d_name = "mlx",
63 };
64
65 /*
66 * Per-interface accessor methods
67 */
68 static int mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
69 static int mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
70 static void mlx_v3_intaction(struct mlx_softc *sc, int action);
71 static int mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, int first);
72
73 static int mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
74 static int mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
75 static void mlx_v4_intaction(struct mlx_softc *sc, int action);
76 static int mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, int first);
77
78 static int mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc);
79 static int mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status);
80 static void mlx_v5_intaction(struct mlx_softc *sc, int action);
81 static int mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2, int first);
82
83 /*
84 * Status monitoring
85 */
86 static void mlx_periodic(void *data);
87 static void mlx_periodic_enquiry(struct mlx_command *mc);
88 static void mlx_periodic_eventlog_poll(struct mlx_softc *sc);
89 static void mlx_periodic_eventlog_respond(struct mlx_command *mc);
90 static void mlx_periodic_rebuild(struct mlx_command *mc);
91
92 /*
93 * Channel Pause
94 */
95 static void mlx_pause_action(struct mlx_softc *sc);
96 static void mlx_pause_done(struct mlx_command *mc);
97
98 /*
99 * Command submission.
100 */
101 static void *mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize,
102 void (*complete)(struct mlx_command *mc));
103 static int mlx_flush(struct mlx_softc *sc);
104 static int mlx_check(struct mlx_softc *sc, int drive);
105 static int mlx_rebuild(struct mlx_softc *sc, int channel, int target);
106 static int mlx_wait_command(struct mlx_command *mc);
107 static int mlx_poll_command(struct mlx_command *mc);
108 void mlx_startio_cb(void *arg,
109 bus_dma_segment_t *segs,
110 int nsegments, int error);
111 static void mlx_startio(struct mlx_softc *sc);
112 static void mlx_completeio(struct mlx_command *mc);
113 static int mlx_user_command(struct mlx_softc *sc,
114 struct mlx_usercommand *mu);
115 void mlx_user_cb(void *arg, bus_dma_segment_t *segs,
116 int nsegments, int error);
117
118 /*
119 * Command buffer allocation.
120 */
121 static struct mlx_command *mlx_alloccmd(struct mlx_softc *sc);
122 static void mlx_releasecmd(struct mlx_command *mc);
123 static void mlx_freecmd(struct mlx_command *mc);
124
125 /*
126 * Command management.
127 */
128 static int mlx_getslot(struct mlx_command *mc);
129 static void mlx_setup_dmamap(struct mlx_command *mc,
130 bus_dma_segment_t *segs,
131 int nsegments, int error);
132 static void mlx_unmapcmd(struct mlx_command *mc);
133 static int mlx_shutdown_locked(struct mlx_softc *sc);
134 static int mlx_start(struct mlx_command *mc);
135 static int mlx_done(struct mlx_softc *sc, int startio);
136 static void mlx_complete(struct mlx_softc *sc);
137
138 /*
139 * Debugging.
140 */
141 static char *mlx_diagnose_command(struct mlx_command *mc);
142 static void mlx_describe_controller(struct mlx_softc *sc);
143 static int mlx_fw_message(struct mlx_softc *sc, int status, int param1, int param2);
144
145 /*
146 * Utility functions.
147 */
148 static struct mlx_sysdrive *mlx_findunit(struct mlx_softc *sc, int unit);
149
150 /********************************************************************************
151 ********************************************************************************
152 Public Interfaces
153 ********************************************************************************
154 ********************************************************************************/
155
156 /********************************************************************************
157 * Free all of the resources associated with (sc)
158 *
159 * Should not be called if the controller is active.
160 */
161 void
mlx_free(struct mlx_softc * sc)162 mlx_free(struct mlx_softc *sc)
163 {
164 struct mlx_command *mc;
165
166 debug_called(1);
167
168 /* destroy control device */
169 if (sc->mlx_dev_t != NULL)
170 destroy_dev(sc->mlx_dev_t);
171
172 if (sc->mlx_intr)
173 bus_teardown_intr(sc->mlx_dev, sc->mlx_irq, sc->mlx_intr);
174
175 /* cancel status timeout */
176 MLX_IO_LOCK(sc);
177 callout_stop(&sc->mlx_timeout);
178
179 /* throw away any command buffers */
180 while ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL) {
181 TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link);
182 mlx_freecmd(mc);
183 }
184 MLX_IO_UNLOCK(sc);
185 callout_drain(&sc->mlx_timeout);
186
187 /* destroy data-transfer DMA tag */
188 if (sc->mlx_buffer_dmat)
189 bus_dma_tag_destroy(sc->mlx_buffer_dmat);
190
191 /* free and destroy DMA memory and tag for s/g lists */
192 if (sc->mlx_sgbusaddr)
193 bus_dmamap_unload(sc->mlx_sg_dmat, sc->mlx_sg_dmamap);
194 if (sc->mlx_sgtable)
195 bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
196 if (sc->mlx_sg_dmat)
197 bus_dma_tag_destroy(sc->mlx_sg_dmat);
198
199 /* disconnect the interrupt handler */
200 if (sc->mlx_irq != NULL)
201 bus_release_resource(sc->mlx_dev, SYS_RES_IRQ, 0, sc->mlx_irq);
202
203 /* destroy the parent DMA tag */
204 if (sc->mlx_parent_dmat)
205 bus_dma_tag_destroy(sc->mlx_parent_dmat);
206
207 /* release the register window mapping */
208 if (sc->mlx_mem != NULL)
209 bus_release_resource(sc->mlx_dev, sc->mlx_mem_type, sc->mlx_mem_rid, sc->mlx_mem);
210
211 /* free controller enquiry data */
212 if (sc->mlx_enq2 != NULL)
213 free(sc->mlx_enq2, M_DEVBUF);
214
215 sx_destroy(&sc->mlx_config_lock);
216 mtx_destroy(&sc->mlx_io_lock);
217 }
218
219 /********************************************************************************
220 * Map the scatter/gather table into bus space
221 */
222 static void
mlx_dma_map_sg(void * arg,bus_dma_segment_t * segs,int nseg,int error)223 mlx_dma_map_sg(void *arg, bus_dma_segment_t *segs, int nseg, int error)
224 {
225 struct mlx_softc *sc = (struct mlx_softc *)arg;
226
227 debug_called(1);
228
229 /* save base of s/g table's address in bus space */
230 sc->mlx_sgbusaddr = segs->ds_addr;
231 }
232
233 static int
mlx_sglist_map(struct mlx_softc * sc)234 mlx_sglist_map(struct mlx_softc *sc)
235 {
236 size_t segsize;
237 int error, ncmd;
238
239 debug_called(1);
240
241 /* destroy any existing mappings */
242 if (sc->mlx_sgbusaddr)
243 bus_dmamap_unload(sc->mlx_sg_dmat, sc->mlx_sg_dmamap);
244 if (sc->mlx_sgtable)
245 bus_dmamem_free(sc->mlx_sg_dmat, sc->mlx_sgtable, sc->mlx_sg_dmamap);
246 if (sc->mlx_sg_dmat)
247 bus_dma_tag_destroy(sc->mlx_sg_dmat);
248 sc->mlx_sgbusaddr = 0;
249 sc->mlx_sgtable = NULL;
250 sc->mlx_sg_dmat = NULL;
251
252 /*
253 * Create a single tag describing a region large enough to hold all of
254 * the s/g lists we will need. If we're called early on, we don't know how
255 * many commands we're going to be asked to support, so only allocate enough
256 * for a couple.
257 */
258 if (sc->mlx_enq2 == NULL) {
259 ncmd = 2;
260 } else {
261 ncmd = sc->mlx_enq2->me_max_commands;
262 }
263 segsize = sizeof(struct mlx_sgentry) * MLX_NSEG * ncmd;
264 error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */
265 1, 0, /* alignment,boundary */
266 BUS_SPACE_MAXADDR, /* lowaddr */
267 BUS_SPACE_MAXADDR, /* highaddr */
268 NULL, NULL, /* filter, filterarg */
269 segsize, 1, /* maxsize, nsegments */
270 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
271 0, /* flags */
272 NULL, NULL, /* lockfunc, lockarg */
273 &sc->mlx_sg_dmat);
274 if (error != 0) {
275 device_printf(sc->mlx_dev, "can't allocate scatter/gather DMA tag\n");
276 return(ENOMEM);
277 }
278
279 /*
280 * Allocate enough s/g maps for all commands and permanently map them into
281 * controller-visible space.
282 *
283 * XXX this assumes we can get enough space for all the s/g maps in one
284 * contiguous slab. We may need to switch to a more complex arrangement
285 * where we allocate in smaller chunks and keep a lookup table from slot
286 * to bus address.
287 */
288 error = bus_dmamem_alloc(sc->mlx_sg_dmat, (void **)&sc->mlx_sgtable,
289 BUS_DMA_NOWAIT, &sc->mlx_sg_dmamap);
290 if (error) {
291 device_printf(sc->mlx_dev, "can't allocate s/g table\n");
292 return(ENOMEM);
293 }
294 (void)bus_dmamap_load(sc->mlx_sg_dmat, sc->mlx_sg_dmamap, sc->mlx_sgtable,
295 segsize, mlx_dma_map_sg, sc, 0);
296 return(0);
297 }
298
299 /********************************************************************************
300 * Initialise the controller and softc
301 */
302 int
mlx_attach(struct mlx_softc * sc)303 mlx_attach(struct mlx_softc *sc)
304 {
305 struct mlx_enquiry_old *meo;
306 int rid, error, fwminor, hscode, hserror, hsparam1, hsparam2, hsmsg;
307
308 debug_called(1);
309
310 /*
311 * Initialise per-controller queues.
312 */
313 TAILQ_INIT(&sc->mlx_work);
314 TAILQ_INIT(&sc->mlx_freecmds);
315 bioq_init(&sc->mlx_bioq);
316
317 /*
318 * Select accessor methods based on controller interface type.
319 */
320 switch(sc->mlx_iftype) {
321 case MLX_IFTYPE_2:
322 case MLX_IFTYPE_3:
323 sc->mlx_tryqueue = mlx_v3_tryqueue;
324 sc->mlx_findcomplete = mlx_v3_findcomplete;
325 sc->mlx_intaction = mlx_v3_intaction;
326 sc->mlx_fw_handshake = mlx_v3_fw_handshake;
327 break;
328 case MLX_IFTYPE_4:
329 sc->mlx_tryqueue = mlx_v4_tryqueue;
330 sc->mlx_findcomplete = mlx_v4_findcomplete;
331 sc->mlx_intaction = mlx_v4_intaction;
332 sc->mlx_fw_handshake = mlx_v4_fw_handshake;
333 break;
334 case MLX_IFTYPE_5:
335 sc->mlx_tryqueue = mlx_v5_tryqueue;
336 sc->mlx_findcomplete = mlx_v5_findcomplete;
337 sc->mlx_intaction = mlx_v5_intaction;
338 sc->mlx_fw_handshake = mlx_v5_fw_handshake;
339 break;
340 default:
341 return(ENXIO); /* should never happen */
342 }
343
344 /* disable interrupts before we start talking to the controller */
345 MLX_IO_LOCK(sc);
346 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
347 MLX_IO_UNLOCK(sc);
348
349 /*
350 * Wait for the controller to come ready, handshake with the firmware if required.
351 * This is typically only necessary on platforms where the controller BIOS does not
352 * run.
353 */
354 hsmsg = 0;
355 DELAY(1000);
356 while ((hscode = sc->mlx_fw_handshake(sc, &hserror, &hsparam1, &hsparam2,
357 hsmsg == 0)) != 0) {
358 /* report first time around... */
359 if (hsmsg == 0) {
360 device_printf(sc->mlx_dev, "controller initialisation in progress...\n");
361 hsmsg = 1;
362 }
363 /* did we get a real message? */
364 if (hscode == 2) {
365 hscode = mlx_fw_message(sc, hserror, hsparam1, hsparam2);
366 /* fatal initialisation error? */
367 if (hscode != 0) {
368 return(ENXIO);
369 }
370 }
371 }
372 if (hsmsg == 1)
373 device_printf(sc->mlx_dev, "initialisation complete.\n");
374
375 /*
376 * Allocate and connect our interrupt.
377 */
378 rid = 0;
379 sc->mlx_irq = bus_alloc_resource_any(sc->mlx_dev, SYS_RES_IRQ, &rid,
380 RF_SHAREABLE | RF_ACTIVE);
381 if (sc->mlx_irq == NULL) {
382 device_printf(sc->mlx_dev, "can't allocate interrupt\n");
383 return(ENXIO);
384 }
385 error = bus_setup_intr(sc->mlx_dev, sc->mlx_irq, INTR_TYPE_BIO |
386 INTR_ENTROPY | INTR_MPSAFE, NULL, mlx_intr, sc, &sc->mlx_intr);
387 if (error) {
388 device_printf(sc->mlx_dev, "can't set up interrupt\n");
389 return(ENXIO);
390 }
391
392 /*
393 * Create DMA tag for mapping buffers into controller-addressable space.
394 */
395 error = bus_dma_tag_create(sc->mlx_parent_dmat, /* parent */
396 1, 0, /* align, boundary */
397 BUS_SPACE_MAXADDR, /* lowaddr */
398 BUS_SPACE_MAXADDR, /* highaddr */
399 NULL, NULL, /* filter, filterarg */
400 MLX_MAXPHYS, /* maxsize */
401 MLX_NSEG, /* nsegments */
402 BUS_SPACE_MAXSIZE_32BIT, /* maxsegsize */
403 0, /* flags */
404 busdma_lock_mutex, /* lockfunc */
405 &sc->mlx_io_lock, /* lockarg */
406 &sc->mlx_buffer_dmat);
407 if (error != 0) {
408 device_printf(sc->mlx_dev, "can't allocate buffer DMA tag\n");
409 return(ENOMEM);
410 }
411
412 /*
413 * Create some initial scatter/gather mappings so we can run the probe
414 * commands.
415 */
416 error = mlx_sglist_map(sc);
417 if (error != 0) {
418 device_printf(sc->mlx_dev, "can't make initial s/g list mapping\n");
419 return(error);
420 }
421
422 /*
423 * We don't (yet) know where the event log is up to.
424 */
425 sc->mlx_currevent = -1;
426
427 /*
428 * Obtain controller feature information
429 */
430 MLX_IO_LOCK(sc);
431 if ((sc->mlx_enq2 = mlx_enquire(sc, MLX_CMD_ENQUIRY2, sizeof(struct mlx_enquiry2), NULL)) == NULL) {
432 MLX_IO_UNLOCK(sc);
433 device_printf(sc->mlx_dev, "ENQUIRY2 failed\n");
434 return(ENXIO);
435 }
436
437 /*
438 * Do quirk/feature related things.
439 */
440 fwminor = (sc->mlx_enq2->me_firmware_id >> 8) & 0xff;
441 switch(sc->mlx_iftype) {
442 case MLX_IFTYPE_2:
443 /* These controllers don't report the firmware version in the ENQUIRY2 response */
444 if ((meo = mlx_enquire(sc, MLX_CMD_ENQUIRY_OLD, sizeof(struct mlx_enquiry_old), NULL)) == NULL) {
445 MLX_IO_UNLOCK(sc);
446 device_printf(sc->mlx_dev, "ENQUIRY_OLD failed\n");
447 return(ENXIO);
448 }
449 sc->mlx_enq2->me_firmware_id = ('0' << 24) | (0 << 16) | (meo->me_fwminor << 8) | meo->me_fwmajor;
450
451 /* XXX require 2.42 or better (PCI) */
452 if (meo->me_fwminor < 42) {
453 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
454 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 2.42 or later\n");
455 }
456 free(meo, M_DEVBUF);
457 break;
458 case MLX_IFTYPE_3:
459 /* XXX certify 3.52? */
460 if (fwminor < 51) {
461 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
462 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 3.51 or later\n");
463 }
464 break;
465 case MLX_IFTYPE_4:
466 /* XXX certify firmware versions? */
467 if (fwminor < 6) {
468 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
469 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 4.06 or later\n");
470 }
471 break;
472 case MLX_IFTYPE_5:
473 if (fwminor < 7) {
474 device_printf(sc->mlx_dev, " *** WARNING *** This firmware revision is not recommended\n");
475 device_printf(sc->mlx_dev, " *** WARNING *** Use revision 5.07 or later\n");
476 }
477 break;
478 default:
479 MLX_IO_UNLOCK(sc);
480 return(ENXIO); /* should never happen */
481 }
482 MLX_IO_UNLOCK(sc);
483
484 /*
485 * Create the final scatter/gather mappings now that we have characterised the controller.
486 */
487 error = mlx_sglist_map(sc);
488 if (error != 0) {
489 device_printf(sc->mlx_dev, "can't make final s/g list mapping\n");
490 return(error);
491 }
492
493 /*
494 * No user-requested background operation is in progress.
495 */
496 sc->mlx_background = 0;
497 sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
498
499 /*
500 * Create the control device.
501 */
502 sc->mlx_dev_t = make_dev(&mlx_cdevsw, 0, UID_ROOT, GID_OPERATOR,
503 S_IRUSR | S_IWUSR, "mlx%d", device_get_unit(sc->mlx_dev));
504 sc->mlx_dev_t->si_drv1 = sc;
505
506 /*
507 * Start the timeout routine.
508 */
509 callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc);
510
511 /* print a little information about the controller */
512 mlx_describe_controller(sc);
513
514 return(0);
515 }
516
517 /********************************************************************************
518 * Locate disk resources and attach children to them.
519 */
520 void
mlx_startup(struct mlx_softc * sc)521 mlx_startup(struct mlx_softc *sc)
522 {
523 struct mlx_enq_sys_drive *mes;
524 struct mlx_sysdrive *dr;
525 int i;
526
527 debug_called(1);
528
529 /*
530 * Scan all the system drives and attach children for those that
531 * don't currently have them.
532 */
533 MLX_IO_LOCK(sc);
534 mes = mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(*mes) * MLX_MAXDRIVES, NULL);
535 MLX_IO_UNLOCK(sc);
536 if (mes == NULL) {
537 device_printf(sc->mlx_dev, "error fetching drive status\n");
538 return;
539 }
540
541 /* iterate over drives returned */
542 MLX_CONFIG_LOCK(sc);
543 for (i = 0, dr = &sc->mlx_sysdrive[0];
544 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
545 i++, dr++) {
546 /* are we already attached to this drive? */
547 if (dr->ms_disk == 0) {
548 /* pick up drive information */
549 dr->ms_size = mes[i].sd_size;
550 dr->ms_raidlevel = mes[i].sd_raidlevel & 0xf;
551 dr->ms_state = mes[i].sd_state;
552
553 /* generate geometry information */
554 if (sc->mlx_geom == MLX_GEOM_128_32) {
555 dr->ms_heads = 128;
556 dr->ms_sectors = 32;
557 dr->ms_cylinders = dr->ms_size / (128 * 32);
558 } else { /* MLX_GEOM_255/63 */
559 dr->ms_heads = 255;
560 dr->ms_sectors = 63;
561 dr->ms_cylinders = dr->ms_size / (255 * 63);
562 }
563 dr->ms_disk = device_add_child(sc->mlx_dev, /*"mlxd"*/NULL, DEVICE_UNIT_ANY);
564 if (dr->ms_disk == 0)
565 device_printf(sc->mlx_dev, "device_add_child failed\n");
566 device_set_ivars(dr->ms_disk, dr);
567 }
568 }
569 free(mes, M_DEVBUF);
570 bus_attach_children(sc->mlx_dev);
571
572 /* mark controller back up */
573 MLX_IO_LOCK(sc);
574 sc->mlx_state &= ~MLX_STATE_SHUTDOWN;
575
576 /* enable interrupts */
577 sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
578 MLX_IO_UNLOCK(sc);
579 MLX_CONFIG_UNLOCK(sc);
580 }
581
582 /********************************************************************************
583 * Disconnect from the controller completely, in preparation for unload.
584 */
585 int
mlx_detach(device_t dev)586 mlx_detach(device_t dev)
587 {
588 struct mlx_softc *sc = device_get_softc(dev);
589 struct mlxd_softc *mlxd;
590 int i, error;
591
592 debug_called(1);
593
594 error = EBUSY;
595 MLX_CONFIG_LOCK(sc);
596 if (sc->mlx_state & MLX_STATE_OPEN)
597 goto out;
598
599 for (i = 0; i < MLX_MAXDRIVES; i++) {
600 if (sc->mlx_sysdrive[i].ms_disk != 0) {
601 mlxd = device_get_softc(sc->mlx_sysdrive[i].ms_disk);
602 if (mlxd->mlxd_flags & MLXD_OPEN) { /* drive is mounted, abort detach */
603 device_printf(sc->mlx_sysdrive[i].ms_disk, "still open, can't detach\n");
604 goto out;
605 }
606 }
607 }
608 if ((error = mlx_shutdown(dev)))
609 goto out;
610 MLX_CONFIG_UNLOCK(sc);
611
612 mlx_free(sc);
613
614 return (0);
615 out:
616 MLX_CONFIG_UNLOCK(sc);
617 return(error);
618 }
619
620 /********************************************************************************
621 * Bring the controller down to a dormant state and detach all child devices.
622 *
623 * This function is called before detach, system shutdown, or before performing
624 * an operation which may add or delete system disks. (Call mlx_startup to
625 * resume normal operation.)
626 *
627 * Note that we can assume that the bioq on the controller is empty, as we won't
628 * allow shutdown if any device is open.
629 */
630 int
mlx_shutdown(device_t dev)631 mlx_shutdown(device_t dev)
632 {
633 struct mlx_softc *sc = device_get_softc(dev);
634 int error;
635
636 MLX_CONFIG_LOCK(sc);
637 error = mlx_shutdown_locked(sc);
638 MLX_CONFIG_UNLOCK(sc);
639 return (error);
640 }
641
642 static int
mlx_shutdown_locked(struct mlx_softc * sc)643 mlx_shutdown_locked(struct mlx_softc *sc)
644 {
645 int error;
646
647 debug_called(1);
648
649 MLX_CONFIG_ASSERT_LOCKED(sc);
650
651 MLX_IO_LOCK(sc);
652 sc->mlx_state |= MLX_STATE_SHUTDOWN;
653 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
654
655 /* flush controller */
656 device_printf(sc->mlx_dev, "flushing cache...");
657 if (mlx_flush(sc)) {
658 printf("failed\n");
659 } else {
660 printf("done\n");
661 }
662 MLX_IO_UNLOCK(sc);
663
664 /* delete all our child devices */
665 error = bus_generic_detach(sc->mlx_dev);
666
667 return (error);
668 }
669
670 /********************************************************************************
671 * Bring the controller to a quiescent state, ready for system suspend.
672 */
673 int
mlx_suspend(device_t dev)674 mlx_suspend(device_t dev)
675 {
676 struct mlx_softc *sc = device_get_softc(dev);
677
678 debug_called(1);
679
680 MLX_IO_LOCK(sc);
681 sc->mlx_state |= MLX_STATE_SUSPEND;
682
683 /* flush controller */
684 device_printf(sc->mlx_dev, "flushing cache...");
685 printf("%s\n", mlx_flush(sc) ? "failed" : "done");
686
687 sc->mlx_intaction(sc, MLX_INTACTION_DISABLE);
688 MLX_IO_UNLOCK(sc);
689
690 return(0);
691 }
692
693 /********************************************************************************
694 * Bring the controller back to a state ready for operation.
695 */
696 int
mlx_resume(device_t dev)697 mlx_resume(device_t dev)
698 {
699 struct mlx_softc *sc = device_get_softc(dev);
700
701 debug_called(1);
702
703 MLX_IO_LOCK(sc);
704 sc->mlx_state &= ~MLX_STATE_SUSPEND;
705 sc->mlx_intaction(sc, MLX_INTACTION_ENABLE);
706 MLX_IO_UNLOCK(sc);
707
708 return(0);
709 }
710
711 /*******************************************************************************
712 * Take an interrupt, or be poked by other code to look for interrupt-worthy
713 * status.
714 */
715 void
mlx_intr(void * arg)716 mlx_intr(void *arg)
717 {
718 struct mlx_softc *sc = (struct mlx_softc *)arg;
719
720 debug_called(1);
721
722 /* collect finished commands, queue anything waiting */
723 MLX_IO_LOCK(sc);
724 mlx_done(sc, 1);
725 MLX_IO_UNLOCK(sc);
726 };
727
728 /*******************************************************************************
729 * Receive a buf structure from a child device and queue it on a particular
730 * disk resource, then poke the disk resource to start as much work as it can.
731 */
732 int
mlx_submit_buf(struct mlx_softc * sc,struct bio * bp)733 mlx_submit_buf(struct mlx_softc *sc, struct bio *bp)
734 {
735
736 debug_called(1);
737
738 MLX_IO_ASSERT_LOCKED(sc);
739 bioq_insert_tail(&sc->mlx_bioq, bp);
740 sc->mlx_waitbufs++;
741 mlx_startio(sc);
742 return(0);
743 }
744
745 /********************************************************************************
746 * Accept an open operation on the control device.
747 */
748 int
mlx_open(struct cdev * dev,int flags,int fmt,struct thread * td)749 mlx_open(struct cdev *dev, int flags, int fmt, struct thread *td)
750 {
751 struct mlx_softc *sc = dev->si_drv1;
752
753 MLX_CONFIG_LOCK(sc);
754 MLX_IO_LOCK(sc);
755 sc->mlx_state |= MLX_STATE_OPEN;
756 MLX_IO_UNLOCK(sc);
757 MLX_CONFIG_UNLOCK(sc);
758 return(0);
759 }
760
761 /********************************************************************************
762 * Accept the last close on the control device.
763 */
764 int
mlx_close(struct cdev * dev,int flags,int fmt,struct thread * td)765 mlx_close(struct cdev *dev, int flags, int fmt, struct thread *td)
766 {
767 struct mlx_softc *sc = dev->si_drv1;
768
769 MLX_CONFIG_LOCK(sc);
770 MLX_IO_LOCK(sc);
771 sc->mlx_state &= ~MLX_STATE_OPEN;
772 MLX_IO_UNLOCK(sc);
773 MLX_CONFIG_UNLOCK(sc);
774 return (0);
775 }
776
777 /********************************************************************************
778 * Handle controller-specific control operations.
779 */
780 int
mlx_ioctl(struct cdev * dev,u_long cmd,caddr_t addr,int32_t flag,struct thread * td)781 mlx_ioctl(struct cdev *dev, u_long cmd, caddr_t addr, int32_t flag, struct thread *td)
782 {
783 struct mlx_softc *sc = dev->si_drv1;
784 struct mlx_rebuild_request *rb = (struct mlx_rebuild_request *)addr;
785 struct mlx_rebuild_status *rs = (struct mlx_rebuild_status *)addr;
786 int *arg = (int *)addr;
787 struct mlx_pause *mp;
788 struct mlx_sysdrive *dr;
789 struct mlxd_softc *mlxd;
790 int i, error;
791
792 switch(cmd) {
793 /*
794 * Enumerate connected system drives; returns the first system drive's
795 * unit number if *arg is -1, or the next unit after *arg if it's
796 * a valid unit on this controller.
797 */
798 case MLX_NEXT_CHILD:
799 /* search system drives */
800 MLX_CONFIG_LOCK(sc);
801 for (i = 0; i < MLX_MAXDRIVES; i++) {
802 /* is this one attached? */
803 if (sc->mlx_sysdrive[i].ms_disk != 0) {
804 /* looking for the next one we come across? */
805 if (*arg == -1) {
806 *arg = device_get_unit(sc->mlx_sysdrive[i].ms_disk);
807 MLX_CONFIG_UNLOCK(sc);
808 return(0);
809 }
810 /* we want the one after this one */
811 if (*arg == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
812 *arg = -1;
813 }
814 }
815 MLX_CONFIG_UNLOCK(sc);
816 return(ENOENT);
817
818 /*
819 * Scan the controller to see whether new drives have appeared.
820 */
821 case MLX_RESCAN_DRIVES:
822 bus_topo_lock();
823 mlx_startup(sc);
824 bus_topo_unlock();
825 return(0);
826
827 /*
828 * Disconnect from the specified drive; it may be about to go
829 * away.
830 */
831 case MLX_DETACH_DRIVE: /* detach one drive */
832 MLX_CONFIG_LOCK(sc);
833 if (((dr = mlx_findunit(sc, *arg)) == NULL) ||
834 ((mlxd = device_get_softc(dr->ms_disk)) == NULL)) {
835 MLX_CONFIG_UNLOCK(sc);
836 return(ENOENT);
837 }
838
839 device_printf(dr->ms_disk, "detaching...");
840 error = 0;
841 if (mlxd->mlxd_flags & MLXD_OPEN) {
842 error = EBUSY;
843 goto detach_out;
844 }
845
846 /* flush controller */
847 MLX_IO_LOCK(sc);
848 if (mlx_flush(sc)) {
849 MLX_IO_UNLOCK(sc);
850 error = EBUSY;
851 goto detach_out;
852 }
853 MLX_IO_UNLOCK(sc);
854
855 /* nuke drive */
856 if ((error = device_delete_child(sc->mlx_dev, dr->ms_disk)) != 0)
857 goto detach_out;
858 dr->ms_disk = 0;
859
860 detach_out:
861 MLX_CONFIG_UNLOCK(sc);
862 if (error) {
863 printf("failed\n");
864 } else {
865 printf("done\n");
866 }
867 return(error);
868
869 /*
870 * Pause one or more SCSI channels for a period of time, to assist
871 * in the process of hot-swapping devices.
872 *
873 * Note that at least the 3.51 firmware on the DAC960PL doesn't seem
874 * to do this right.
875 */
876 case MLX_PAUSE_CHANNEL: /* schedule a channel pause */
877 /* Does this command work on this firmware? */
878 if (!(sc->mlx_feature & MLX_FEAT_PAUSEWORKS))
879 return(EOPNOTSUPP);
880
881 /* check time values */
882 mp = (struct mlx_pause *)addr;
883 if ((mp->mp_when < 0) || (mp->mp_when > 3600))
884 return(EINVAL);
885 if ((mp->mp_howlong < 1) || (mp->mp_howlong > (0xf * 30)))
886 return(EINVAL);
887
888 MLX_IO_LOCK(sc);
889 if ((mp->mp_which == MLX_PAUSE_CANCEL) && (sc->mlx_pause.mp_when != 0)) {
890 /* cancel a pending pause operation */
891 sc->mlx_pause.mp_which = 0;
892 } else {
893 /* fix for legal channels */
894 mp->mp_which &= ((1 << sc->mlx_enq2->me_actual_channels) -1);
895
896 /* check for a pause currently running */
897 if ((sc->mlx_pause.mp_which != 0) && (sc->mlx_pause.mp_when == 0)) {
898 MLX_IO_UNLOCK(sc);
899 return(EBUSY);
900 }
901
902 /* looks ok, go with it */
903 sc->mlx_pause.mp_which = mp->mp_which;
904 sc->mlx_pause.mp_when = time_second + mp->mp_when;
905 sc->mlx_pause.mp_howlong = sc->mlx_pause.mp_when + mp->mp_howlong;
906 }
907 MLX_IO_UNLOCK(sc);
908 return(0);
909
910 /*
911 * Accept a command passthrough-style.
912 */
913 case MLX_COMMAND:
914 return(mlx_user_command(sc, (struct mlx_usercommand *)addr));
915
916 /*
917 * Start a rebuild on a given SCSI disk
918 */
919 case MLX_REBUILDASYNC:
920 MLX_IO_LOCK(sc);
921 if (sc->mlx_background != 0) {
922 MLX_IO_UNLOCK(sc);
923 rb->rr_status = 0x0106;
924 return(EBUSY);
925 }
926 rb->rr_status = mlx_rebuild(sc, rb->rr_channel, rb->rr_target);
927 switch (rb->rr_status) {
928 case 0:
929 error = 0;
930 break;
931 case 0x10000:
932 error = ENOMEM; /* couldn't set up the command */
933 break;
934 case 0x0002:
935 error = EBUSY;
936 break;
937 case 0x0104:
938 error = EIO;
939 break;
940 case 0x0105:
941 error = ERANGE;
942 break;
943 case 0x0106:
944 error = EBUSY;
945 break;
946 default:
947 error = EINVAL;
948 break;
949 }
950 if (error == 0)
951 sc->mlx_background = MLX_BACKGROUND_REBUILD;
952 MLX_IO_UNLOCK(sc);
953 return(error);
954
955 /*
956 * Get the status of the current rebuild or consistency check.
957 */
958 case MLX_REBUILDSTAT:
959 MLX_IO_LOCK(sc);
960 *rs = sc->mlx_rebuildstat;
961 MLX_IO_UNLOCK(sc);
962 return(0);
963
964 /*
965 * Return the per-controller system drive number matching the
966 * disk device number in (arg), if it happens to belong to us.
967 */
968 case MLX_GET_SYSDRIVE:
969 error = ENOENT;
970 MLX_CONFIG_LOCK(sc);
971 bus_topo_lock();
972 mlxd = devclass_get_softc(devclass_find("mlxd"), *arg);
973 bus_topo_unlock();
974 if ((mlxd != NULL) && (mlxd->mlxd_drive >= sc->mlx_sysdrive) &&
975 (mlxd->mlxd_drive < (sc->mlx_sysdrive + MLX_MAXDRIVES))) {
976 error = 0;
977 *arg = mlxd->mlxd_drive - sc->mlx_sysdrive;
978 }
979 MLX_CONFIG_UNLOCK(sc);
980 return(error);
981
982 default:
983 return(ENOTTY);
984 }
985 }
986
987 /********************************************************************************
988 * Handle operations requested by a System Drive connected to this controller.
989 */
990 int
mlx_submit_ioctl(struct mlx_softc * sc,struct mlx_sysdrive * drive,u_long cmd,caddr_t addr,int32_t flag,struct thread * td)991 mlx_submit_ioctl(struct mlx_softc *sc, struct mlx_sysdrive *drive, u_long cmd,
992 caddr_t addr, int32_t flag, struct thread *td)
993 {
994 int *arg = (int *)addr;
995 int error, result;
996
997 switch(cmd) {
998 /*
999 * Return the current status of this drive.
1000 */
1001 case MLXD_STATUS:
1002 MLX_IO_LOCK(sc);
1003 *arg = drive->ms_state;
1004 MLX_IO_UNLOCK(sc);
1005 return(0);
1006
1007 /*
1008 * Start a background consistency check on this drive.
1009 */
1010 case MLXD_CHECKASYNC: /* start a background consistency check */
1011 MLX_IO_LOCK(sc);
1012 if (sc->mlx_background != 0) {
1013 MLX_IO_UNLOCK(sc);
1014 *arg = 0x0106;
1015 return(EBUSY);
1016 }
1017 result = mlx_check(sc, drive - &sc->mlx_sysdrive[0]);
1018 switch (result) {
1019 case 0:
1020 error = 0;
1021 break;
1022 case 0x10000:
1023 error = ENOMEM; /* couldn't set up the command */
1024 break;
1025 case 0x0002:
1026 error = EIO;
1027 break;
1028 case 0x0105:
1029 error = ERANGE;
1030 break;
1031 case 0x0106:
1032 error = EBUSY;
1033 break;
1034 default:
1035 error = EINVAL;
1036 break;
1037 }
1038 if (error == 0)
1039 sc->mlx_background = MLX_BACKGROUND_CHECK;
1040 MLX_IO_UNLOCK(sc);
1041 *arg = result;
1042 return(error);
1043
1044 }
1045 return(ENOIOCTL);
1046 }
1047
1048
1049 /********************************************************************************
1050 ********************************************************************************
1051 Status Monitoring
1052 ********************************************************************************
1053 ********************************************************************************/
1054
1055 /********************************************************************************
1056 * Fire off commands to periodically check the status of connected drives.
1057 */
1058 static void
mlx_periodic(void * data)1059 mlx_periodic(void *data)
1060 {
1061 struct mlx_softc *sc = (struct mlx_softc *)data;
1062
1063 debug_called(1);
1064 MLX_IO_ASSERT_LOCKED(sc);
1065
1066 /*
1067 * Run a bus pause?
1068 */
1069 if ((sc->mlx_pause.mp_which != 0) &&
1070 (sc->mlx_pause.mp_when > 0) &&
1071 (time_second >= sc->mlx_pause.mp_when)){
1072
1073 mlx_pause_action(sc); /* pause is running */
1074 sc->mlx_pause.mp_when = 0;
1075 sysbeep(500, SBT_1S);
1076
1077 /*
1078 * Bus pause still running?
1079 */
1080 } else if ((sc->mlx_pause.mp_which != 0) &&
1081 (sc->mlx_pause.mp_when == 0)) {
1082
1083 /* time to stop bus pause? */
1084 if (time_second >= sc->mlx_pause.mp_howlong) {
1085 mlx_pause_action(sc);
1086 sc->mlx_pause.mp_which = 0; /* pause is complete */
1087 sysbeep(500, SBT_1S);
1088 } else {
1089 sysbeep((time_second % 5) * 100 + 500, SBT_1S / 8);
1090 }
1091
1092 /*
1093 * Run normal periodic activities?
1094 */
1095 } else if (time_second > (sc->mlx_lastpoll + 10)) {
1096 sc->mlx_lastpoll = time_second;
1097
1098 /*
1099 * Check controller status.
1100 *
1101 * XXX Note that this may not actually launch a command in situations of high load.
1102 */
1103 mlx_enquire(sc, (sc->mlx_iftype == MLX_IFTYPE_2) ? MLX_CMD_ENQUIRY_OLD : MLX_CMD_ENQUIRY,
1104 imax(sizeof(struct mlx_enquiry), sizeof(struct mlx_enquiry_old)), mlx_periodic_enquiry);
1105
1106 /*
1107 * Check system drive status.
1108 *
1109 * XXX This might be better left to event-driven detection, eg. I/O to an offline
1110 * drive will detect it's offline, rebuilds etc. should detect the drive is back
1111 * online.
1112 */
1113 mlx_enquire(sc, MLX_CMD_ENQSYSDRIVE, sizeof(struct mlx_enq_sys_drive) * MLX_MAXDRIVES,
1114 mlx_periodic_enquiry);
1115
1116 }
1117
1118 /* get drive rebuild/check status */
1119 /* XXX should check sc->mlx_background if this is only valid while in progress */
1120 mlx_enquire(sc, MLX_CMD_REBUILDSTAT, sizeof(struct mlx_rebuild_stat), mlx_periodic_rebuild);
1121
1122 /* deal with possibly-missed interrupts and timed-out commands */
1123 mlx_done(sc, 1);
1124
1125 /* reschedule another poll next second or so */
1126 callout_reset(&sc->mlx_timeout, hz, mlx_periodic, sc);
1127 }
1128
1129 /********************************************************************************
1130 * Handle the result of an ENQUIRY command instigated by periodic status polling.
1131 */
1132 static void
mlx_periodic_enquiry(struct mlx_command * mc)1133 mlx_periodic_enquiry(struct mlx_command *mc)
1134 {
1135 struct mlx_softc *sc = mc->mc_sc;
1136
1137 debug_called(1);
1138 MLX_IO_ASSERT_LOCKED(sc);
1139
1140 /* Command completed OK? */
1141 if (mc->mc_status != 0) {
1142 device_printf(sc->mlx_dev, "periodic enquiry failed - %s\n", mlx_diagnose_command(mc));
1143 goto out;
1144 }
1145
1146 /* respond to command */
1147 switch(mc->mc_mailbox[0]) {
1148 /*
1149 * This is currently a bit fruitless, as we don't know how to extract the eventlog
1150 * pointer yet.
1151 */
1152 case MLX_CMD_ENQUIRY_OLD:
1153 {
1154 struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data;
1155 struct mlx_enquiry_old *meo = (struct mlx_enquiry_old *)mc->mc_data;
1156 int i;
1157
1158 /* convert data in-place to new format */
1159 for (i = (sizeof(me->me_dead) / sizeof(me->me_dead[0])) - 1; i >= 0; i--) {
1160 me->me_dead[i].dd_chan = meo->me_dead[i].dd_chan;
1161 me->me_dead[i].dd_targ = meo->me_dead[i].dd_targ;
1162 }
1163 me->me_misc_flags = 0;
1164 me->me_rebuild_count = meo->me_rebuild_count;
1165 me->me_dead_count = meo->me_dead_count;
1166 me->me_critical_sd_count = meo->me_critical_sd_count;
1167 me->me_event_log_seq_num = 0;
1168 me->me_offline_sd_count = meo->me_offline_sd_count;
1169 me->me_max_commands = meo->me_max_commands;
1170 me->me_rebuild_flag = meo->me_rebuild_flag;
1171 me->me_fwmajor = meo->me_fwmajor;
1172 me->me_fwminor = meo->me_fwminor;
1173 me->me_status_flags = meo->me_status_flags;
1174 me->me_flash_age = meo->me_flash_age;
1175 for (i = (sizeof(me->me_drvsize) / sizeof(me->me_drvsize[0])) - 1; i >= 0; i--) {
1176 if (i > ((sizeof(meo->me_drvsize) / sizeof(meo->me_drvsize[0])) - 1)) {
1177 me->me_drvsize[i] = 0; /* drive beyond supported range */
1178 } else {
1179 me->me_drvsize[i] = meo->me_drvsize[i];
1180 }
1181 }
1182 me->me_num_sys_drvs = meo->me_num_sys_drvs;
1183 }
1184 /* FALLTHROUGH */
1185
1186 /*
1187 * Generic controller status update. We could do more with this than just
1188 * checking the event log.
1189 */
1190 case MLX_CMD_ENQUIRY:
1191 {
1192 struct mlx_enquiry *me = (struct mlx_enquiry *)mc->mc_data;
1193
1194 if (sc->mlx_currevent == -1) {
1195 /* initialise our view of the event log */
1196 sc->mlx_currevent = sc->mlx_lastevent = me->me_event_log_seq_num;
1197 } else if ((me->me_event_log_seq_num != sc->mlx_lastevent) && !(sc->mlx_flags & MLX_EVENTLOG_BUSY)) {
1198 /* record where current events are up to */
1199 sc->mlx_currevent = me->me_event_log_seq_num;
1200 debug(1, "event log pointer was %d, now %d\n", sc->mlx_lastevent, sc->mlx_currevent);
1201
1202 /* mark the event log as busy */
1203 sc->mlx_flags |= MLX_EVENTLOG_BUSY;
1204
1205 /* drain new eventlog entries */
1206 mlx_periodic_eventlog_poll(sc);
1207 }
1208 break;
1209 }
1210 case MLX_CMD_ENQSYSDRIVE:
1211 {
1212 struct mlx_enq_sys_drive *mes = (struct mlx_enq_sys_drive *)mc->mc_data;
1213 struct mlx_sysdrive *dr;
1214 int i;
1215
1216 for (i = 0, dr = &sc->mlx_sysdrive[0];
1217 (i < MLX_MAXDRIVES) && (mes[i].sd_size != 0xffffffff);
1218 i++) {
1219
1220 /* has state been changed by controller? */
1221 if (dr->ms_state != mes[i].sd_state) {
1222 switch(mes[i].sd_state) {
1223 case MLX_SYSD_OFFLINE:
1224 device_printf(dr->ms_disk, "drive offline\n");
1225 break;
1226 case MLX_SYSD_ONLINE:
1227 device_printf(dr->ms_disk, "drive online\n");
1228 break;
1229 case MLX_SYSD_CRITICAL:
1230 device_printf(dr->ms_disk, "drive critical\n");
1231 break;
1232 }
1233 /* save new state */
1234 dr->ms_state = mes[i].sd_state;
1235 }
1236 }
1237 break;
1238 }
1239 default:
1240 device_printf(sc->mlx_dev, "%s: unknown command 0x%x", __func__, mc->mc_mailbox[0]);
1241 break;
1242 }
1243
1244 out:
1245 free(mc->mc_data, M_DEVBUF);
1246 mlx_releasecmd(mc);
1247 }
1248
1249 static void
mlx_eventlog_cb(void * arg,bus_dma_segment_t * segs,int nsegments,int error)1250 mlx_eventlog_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1251 {
1252 struct mlx_command *mc;
1253
1254 mc = (struct mlx_command *)arg;
1255 mlx_setup_dmamap(mc, segs, nsegments, error);
1256
1257 /* build the command to get one entry */
1258 mlx_make_type3(mc, MLX_CMD_LOGOP, MLX_LOGOP_GET, 1,
1259 mc->mc_sc->mlx_lastevent, 0, 0, mc->mc_dataphys, 0);
1260 mc->mc_complete = mlx_periodic_eventlog_respond;
1261 mc->mc_private = mc;
1262
1263 /* start the command */
1264 if (mlx_start(mc) != 0) {
1265 mlx_releasecmd(mc);
1266 free(mc->mc_data, M_DEVBUF);
1267 mc->mc_data = NULL;
1268 }
1269
1270 }
1271
1272 /********************************************************************************
1273 * Instigate a poll for one event log message on (sc).
1274 * We only poll for one message at a time, to keep our command usage down.
1275 */
1276 static void
mlx_periodic_eventlog_poll(struct mlx_softc * sc)1277 mlx_periodic_eventlog_poll(struct mlx_softc *sc)
1278 {
1279 struct mlx_command *mc;
1280 void *result = NULL;
1281 int error = 0;
1282
1283 debug_called(1);
1284 MLX_IO_ASSERT_LOCKED(sc);
1285
1286 /* get ourselves a command buffer */
1287 error = 1;
1288 if ((mc = mlx_alloccmd(sc)) == NULL)
1289 goto out;
1290
1291 /* allocate the response structure */
1292 if ((result = malloc(/*sizeof(struct mlx_eventlog_entry)*/1024, M_DEVBUF,
1293 M_NOWAIT)) == NULL)
1294 goto out;
1295
1296 /* get a command slot */
1297 if (mlx_getslot(mc))
1298 goto out;
1299
1300 /* map the command so the controller can see it */
1301 mc->mc_data = result;
1302 mc->mc_length = /*sizeof(struct mlx_eventlog_entry)*/1024;
1303 error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
1304 mc->mc_length, mlx_eventlog_cb, mc, BUS_DMA_NOWAIT);
1305
1306 out:
1307 if (error != 0) {
1308 if (mc != NULL)
1309 mlx_releasecmd(mc);
1310 if ((result != NULL) && (mc->mc_data != NULL))
1311 free(result, M_DEVBUF);
1312 }
1313 }
1314
1315 /********************************************************************************
1316 * Handle the result of polling for a log message, generate diagnostic output.
1317 * If this wasn't the last message waiting for us, we'll go collect another.
1318 */
1319 static char *mlx_sense_messages[] = {
1320 "because write recovery failed",
1321 "because of SCSI bus reset failure",
1322 "because of double check condition",
1323 "because it was removed",
1324 "because of gross error on SCSI chip",
1325 "because of bad tag returned from drive",
1326 "because of timeout on SCSI command",
1327 "because of reset SCSI command issued from system",
1328 "because busy or parity error count exceeded limit",
1329 "because of 'kill drive' command from system",
1330 "because of selection timeout",
1331 "due to SCSI phase sequence error",
1332 "due to unknown status"
1333 };
1334
1335 static void
mlx_periodic_eventlog_respond(struct mlx_command * mc)1336 mlx_periodic_eventlog_respond(struct mlx_command *mc)
1337 {
1338 struct mlx_softc *sc = mc->mc_sc;
1339 struct mlx_eventlog_entry *el = (struct mlx_eventlog_entry *)mc->mc_data;
1340 char *reason;
1341
1342 debug_called(1);
1343 MLX_IO_ASSERT_LOCKED(sc);
1344
1345 sc->mlx_lastevent++; /* next message... */
1346 if (mc->mc_status == 0) {
1347
1348 /* handle event log message */
1349 switch(el->el_type) {
1350 /*
1351 * This is the only sort of message we understand at the moment.
1352 * The tests here are probably incomplete.
1353 */
1354 case MLX_LOGMSG_SENSE: /* sense data */
1355 /* Mylex vendor-specific message indicating a drive was killed? */
1356 if ((el->el_sensekey == 9) &&
1357 (el->el_asc == 0x80)) {
1358 if (el->el_asq < nitems(mlx_sense_messages)) {
1359 reason = mlx_sense_messages[el->el_asq];
1360 } else {
1361 reason = "for unknown reason";
1362 }
1363 device_printf(sc->mlx_dev, "physical drive %d:%d killed %s\n",
1364 el->el_channel, el->el_target, reason);
1365 }
1366 /* SCSI drive was reset? */
1367 if ((el->el_sensekey == 6) && (el->el_asc == 0x29)) {
1368 device_printf(sc->mlx_dev, "physical drive %d:%d reset\n",
1369 el->el_channel, el->el_target);
1370 }
1371 /* SCSI drive error? */
1372 if (!((el->el_sensekey == 0) ||
1373 ((el->el_sensekey == 2) &&
1374 (el->el_asc == 0x04) &&
1375 ((el->el_asq == 0x01) ||
1376 (el->el_asq == 0x02))))) {
1377 device_printf(sc->mlx_dev, "physical drive %d:%d error log: sense = %d asc = %x asq = %x\n",
1378 el->el_channel, el->el_target, el->el_sensekey, el->el_asc, el->el_asq);
1379 device_printf(sc->mlx_dev, " info %4D csi %4D\n", el->el_information, ":", el->el_csi, ":");
1380 }
1381 break;
1382
1383 default:
1384 device_printf(sc->mlx_dev, "unknown log message type 0x%x\n", el->el_type);
1385 break;
1386 }
1387 } else {
1388 device_printf(sc->mlx_dev, "error reading message log - %s\n", mlx_diagnose_command(mc));
1389 /* give up on all the outstanding messages, as we may have come unsynched */
1390 sc->mlx_lastevent = sc->mlx_currevent;
1391 }
1392
1393 /* dispose of command and data */
1394 free(mc->mc_data, M_DEVBUF);
1395 mlx_releasecmd(mc);
1396
1397 /* is there another message to obtain? */
1398 if (sc->mlx_lastevent != sc->mlx_currevent) {
1399 mlx_periodic_eventlog_poll(sc);
1400 } else {
1401 /* clear log-busy status */
1402 sc->mlx_flags &= ~MLX_EVENTLOG_BUSY;
1403 }
1404 }
1405
1406 /********************************************************************************
1407 * Handle check/rebuild operations in progress.
1408 */
1409 static void
mlx_periodic_rebuild(struct mlx_command * mc)1410 mlx_periodic_rebuild(struct mlx_command *mc)
1411 {
1412 struct mlx_softc *sc = mc->mc_sc;
1413 struct mlx_rebuild_status *mr = (struct mlx_rebuild_status *)mc->mc_data;
1414
1415 MLX_IO_ASSERT_LOCKED(sc);
1416 switch(mc->mc_status) {
1417 case 0: /* operation running, update stats */
1418 sc->mlx_rebuildstat = *mr;
1419
1420 /* spontaneous rebuild/check? */
1421 if (sc->mlx_background == 0) {
1422 sc->mlx_background = MLX_BACKGROUND_SPONTANEOUS;
1423 device_printf(sc->mlx_dev, "background check/rebuild operation started\n");
1424 }
1425 break;
1426
1427 case 0x0105: /* nothing running, finalise stats and report */
1428 switch(sc->mlx_background) {
1429 case MLX_BACKGROUND_CHECK:
1430 device_printf(sc->mlx_dev, "consistency check completed\n"); /* XXX print drive? */
1431 break;
1432 case MLX_BACKGROUND_REBUILD:
1433 device_printf(sc->mlx_dev, "drive rebuild completed\n"); /* XXX print channel/target? */
1434 break;
1435 case MLX_BACKGROUND_SPONTANEOUS:
1436 default:
1437 /* if we have previously been non-idle, report the transition */
1438 if (sc->mlx_rebuildstat.rs_code != MLX_REBUILDSTAT_IDLE) {
1439 device_printf(sc->mlx_dev, "background check/rebuild operation completed\n");
1440 }
1441 }
1442 sc->mlx_background = 0;
1443 sc->mlx_rebuildstat.rs_code = MLX_REBUILDSTAT_IDLE;
1444 break;
1445 }
1446 free(mc->mc_data, M_DEVBUF);
1447 mlx_releasecmd(mc);
1448 }
1449
1450 /********************************************************************************
1451 ********************************************************************************
1452 Channel Pause
1453 ********************************************************************************
1454 ********************************************************************************/
1455
1456 /********************************************************************************
1457 * It's time to perform a channel pause action for (sc), either start or stop
1458 * the pause.
1459 */
1460 static void
mlx_pause_action(struct mlx_softc * sc)1461 mlx_pause_action(struct mlx_softc *sc)
1462 {
1463 struct mlx_command *mc;
1464 int failsafe, i, command;
1465
1466 MLX_IO_ASSERT_LOCKED(sc);
1467
1468 /* What are we doing here? */
1469 if (sc->mlx_pause.mp_when == 0) {
1470 command = MLX_CMD_STARTCHANNEL;
1471 failsafe = 0;
1472
1473 } else {
1474 command = MLX_CMD_STOPCHANNEL;
1475
1476 /*
1477 * Channels will always start again after the failsafe period,
1478 * which is specified in multiples of 30 seconds.
1479 * This constrains us to a maximum pause of 450 seconds.
1480 */
1481 failsafe = ((sc->mlx_pause.mp_howlong - time_second) + 5) / 30;
1482 if (failsafe > 0xf) {
1483 failsafe = 0xf;
1484 sc->mlx_pause.mp_howlong = time_second + (0xf * 30) - 5;
1485 }
1486 }
1487
1488 /* build commands for every channel requested */
1489 for (i = 0; i < sc->mlx_enq2->me_actual_channels; i++) {
1490 if ((1 << i) & sc->mlx_pause.mp_which) {
1491
1492 /* get ourselves a command buffer */
1493 if ((mc = mlx_alloccmd(sc)) == NULL)
1494 goto fail;
1495 /* get a command slot */
1496 mc->mc_flags |= MLX_CMD_PRIORITY;
1497 if (mlx_getslot(mc))
1498 goto fail;
1499
1500 /* build the command */
1501 mlx_make_type2(mc, command, (failsafe << 4) | i, 0, 0, 0, 0, 0, 0, 0);
1502 mc->mc_complete = mlx_pause_done;
1503 mc->mc_private = sc; /* XXX not needed */
1504 if (mlx_start(mc))
1505 goto fail;
1506 /* command submitted OK */
1507 return;
1508
1509 fail:
1510 device_printf(sc->mlx_dev, "%s failed for channel %d\n",
1511 command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", i);
1512 if (mc != NULL)
1513 mlx_releasecmd(mc);
1514 }
1515 }
1516 }
1517
1518 static void
mlx_pause_done(struct mlx_command * mc)1519 mlx_pause_done(struct mlx_command *mc)
1520 {
1521 struct mlx_softc *sc = mc->mc_sc;
1522 int command = mc->mc_mailbox[0];
1523 int channel = mc->mc_mailbox[2] & 0xf;
1524
1525 MLX_IO_ASSERT_LOCKED(sc);
1526 if (mc->mc_status != 0) {
1527 device_printf(sc->mlx_dev, "%s command failed - %s\n",
1528 command == MLX_CMD_STOPCHANNEL ? "pause" : "resume", mlx_diagnose_command(mc));
1529 } else if (command == MLX_CMD_STOPCHANNEL) {
1530 device_printf(sc->mlx_dev, "channel %d pausing for %ld seconds\n",
1531 channel, (long)(sc->mlx_pause.mp_howlong - time_second));
1532 } else {
1533 device_printf(sc->mlx_dev, "channel %d resuming\n", channel);
1534 }
1535 mlx_releasecmd(mc);
1536 }
1537
1538 /********************************************************************************
1539 ********************************************************************************
1540 Command Submission
1541 ********************************************************************************
1542 ********************************************************************************/
1543
1544 static void
mlx_enquire_cb(void * arg,bus_dma_segment_t * segs,int nsegments,int error)1545 mlx_enquire_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1546 {
1547 struct mlx_softc *sc;
1548 struct mlx_command *mc;
1549
1550 mc = (struct mlx_command *)arg;
1551 if (error)
1552 return;
1553
1554 mlx_setup_dmamap(mc, segs, nsegments, error);
1555
1556 /* build an enquiry command */
1557 sc = mc->mc_sc;
1558 mlx_make_type2(mc, mc->mc_command, 0, 0, 0, 0, 0, 0, mc->mc_dataphys, 0);
1559
1560 /* do we want a completion callback? */
1561 if (mc->mc_complete != NULL) {
1562 if ((error = mlx_start(mc)) != 0)
1563 return;
1564 } else {
1565 /* run the command in either polled or wait mode */
1566 if ((sc->mlx_state & MLX_STATE_INTEN) ? mlx_wait_command(mc) :
1567 mlx_poll_command(mc))
1568 return;
1569
1570 /* command completed OK? */
1571 if (mc->mc_status != 0) {
1572 device_printf(sc->mlx_dev, "ENQUIRY failed - %s\n",
1573 mlx_diagnose_command(mc));
1574 return;
1575 }
1576 }
1577 }
1578
1579 /********************************************************************************
1580 * Perform an Enquiry command using a type-3 command buffer and a return a single
1581 * linear result buffer. If the completion function is specified, it will
1582 * be called with the completed command (and the result response will not be
1583 * valid until that point). Otherwise, the command will either be busy-waited
1584 * for (interrupts not enabled), or slept for.
1585 */
1586 static void *
mlx_enquire(struct mlx_softc * sc,int command,size_t bufsize,void (* complete)(struct mlx_command * mc))1587 mlx_enquire(struct mlx_softc *sc, int command, size_t bufsize, void (* complete)(struct mlx_command *mc))
1588 {
1589 struct mlx_command *mc;
1590 void *result;
1591 int error;
1592
1593 debug_called(1);
1594 MLX_IO_ASSERT_LOCKED(sc);
1595
1596 /* get ourselves a command buffer */
1597 error = 1;
1598 result = NULL;
1599 if ((mc = mlx_alloccmd(sc)) == NULL)
1600 goto out;
1601 /* allocate the response structure */
1602 if ((result = malloc(bufsize, M_DEVBUF, M_NOWAIT)) == NULL)
1603 goto out;
1604 /* get a command slot */
1605 mc->mc_flags |= MLX_CMD_PRIORITY | MLX_CMD_DATAOUT;
1606 if (mlx_getslot(mc))
1607 goto out;
1608
1609 /* map the command so the controller can see it */
1610 mc->mc_data = result;
1611 mc->mc_length = bufsize;
1612 mc->mc_command = command;
1613
1614 if (complete != NULL) {
1615 mc->mc_complete = complete;
1616 mc->mc_private = mc;
1617 }
1618
1619 error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
1620 mc->mc_length, mlx_enquire_cb, mc, BUS_DMA_NOWAIT);
1621
1622 out:
1623 /* we got a command, but nobody else will free it */
1624 if ((mc != NULL) && (mc->mc_complete == NULL))
1625 mlx_releasecmd(mc);
1626 /* we got an error, and we allocated a result */
1627 if ((error != 0) && (result != NULL)) {
1628 free(result, M_DEVBUF);
1629 result = NULL;
1630 }
1631 return(result);
1632 }
1633
1634
1635 /********************************************************************************
1636 * Perform a Flush command on the nominated controller.
1637 *
1638 * May be called with interrupts enabled or disabled; will not return until
1639 * the flush operation completes or fails.
1640 */
1641 static int
mlx_flush(struct mlx_softc * sc)1642 mlx_flush(struct mlx_softc *sc)
1643 {
1644 struct mlx_command *mc;
1645 int error;
1646
1647 debug_called(1);
1648 MLX_IO_ASSERT_LOCKED(sc);
1649
1650 /* get ourselves a command buffer */
1651 error = 1;
1652 if ((mc = mlx_alloccmd(sc)) == NULL)
1653 goto out;
1654 /* get a command slot */
1655 if (mlx_getslot(mc))
1656 goto out;
1657
1658 /* build a flush command */
1659 mlx_make_type2(mc, MLX_CMD_FLUSH, 0, 0, 0, 0, 0, 0, 0, 0);
1660
1661 /* can't assume that interrupts are going to work here, so play it safe */
1662 if (mlx_poll_command(mc))
1663 goto out;
1664
1665 /* command completed OK? */
1666 if (mc->mc_status != 0) {
1667 device_printf(sc->mlx_dev, "FLUSH failed - %s\n", mlx_diagnose_command(mc));
1668 goto out;
1669 }
1670
1671 error = 0; /* success */
1672 out:
1673 if (mc != NULL)
1674 mlx_releasecmd(mc);
1675 return(error);
1676 }
1677
1678 /********************************************************************************
1679 * Start a background consistency check on (drive).
1680 *
1681 * May be called with interrupts enabled or disabled; will return as soon as the
1682 * operation has started or been refused.
1683 */
1684 static int
mlx_check(struct mlx_softc * sc,int drive)1685 mlx_check(struct mlx_softc *sc, int drive)
1686 {
1687 struct mlx_command *mc;
1688 int error;
1689
1690 debug_called(1);
1691 MLX_IO_ASSERT_LOCKED(sc);
1692
1693 /* get ourselves a command buffer */
1694 error = 0x10000;
1695 if ((mc = mlx_alloccmd(sc)) == NULL)
1696 goto out;
1697 /* get a command slot */
1698 if (mlx_getslot(mc))
1699 goto out;
1700
1701 /* build a checkasync command, set the "fix it" flag */
1702 mlx_make_type2(mc, MLX_CMD_CHECKASYNC, 0, 0, 0, 0, 0, drive | 0x80, 0, 0);
1703
1704 /* start the command and wait for it to be returned */
1705 if (mlx_wait_command(mc))
1706 goto out;
1707
1708 /* command completed OK? */
1709 if (mc->mc_status != 0) {
1710 device_printf(sc->mlx_dev, "CHECK ASYNC failed - %s\n", mlx_diagnose_command(mc));
1711 } else {
1712 device_printf(sc->mlx_sysdrive[drive].ms_disk, "consistency check started");
1713 }
1714 error = mc->mc_status;
1715
1716 out:
1717 if (mc != NULL)
1718 mlx_releasecmd(mc);
1719 return(error);
1720 }
1721
1722 /********************************************************************************
1723 * Start a background rebuild of the physical drive at (channel),(target).
1724 *
1725 * May be called with interrupts enabled or disabled; will return as soon as the
1726 * operation has started or been refused.
1727 */
1728 static int
mlx_rebuild(struct mlx_softc * sc,int channel,int target)1729 mlx_rebuild(struct mlx_softc *sc, int channel, int target)
1730 {
1731 struct mlx_command *mc;
1732 int error;
1733
1734 debug_called(1);
1735 MLX_IO_ASSERT_LOCKED(sc);
1736
1737 /* get ourselves a command buffer */
1738 error = 0x10000;
1739 if ((mc = mlx_alloccmd(sc)) == NULL)
1740 goto out;
1741 /* get a command slot */
1742 if (mlx_getslot(mc))
1743 goto out;
1744
1745 /* build a checkasync command, set the "fix it" flag */
1746 mlx_make_type2(mc, MLX_CMD_REBUILDASYNC, channel, target, 0, 0, 0, 0, 0, 0);
1747
1748 /* start the command and wait for it to be returned */
1749 if (mlx_wait_command(mc))
1750 goto out;
1751
1752 /* command completed OK? */
1753 if (mc->mc_status != 0) {
1754 device_printf(sc->mlx_dev, "REBUILD ASYNC failed - %s\n", mlx_diagnose_command(mc));
1755 } else {
1756 device_printf(sc->mlx_dev, "drive rebuild started for %d:%d\n", channel, target);
1757 }
1758 error = mc->mc_status;
1759
1760 out:
1761 if (mc != NULL)
1762 mlx_releasecmd(mc);
1763 return(error);
1764 }
1765
1766 /********************************************************************************
1767 * Run the command (mc) and return when it completes.
1768 *
1769 * Interrupts need to be enabled; returns nonzero on error.
1770 */
1771 static int
mlx_wait_command(struct mlx_command * mc)1772 mlx_wait_command(struct mlx_command *mc)
1773 {
1774 struct mlx_softc *sc = mc->mc_sc;
1775 int error, count;
1776
1777 debug_called(1);
1778 MLX_IO_ASSERT_LOCKED(sc);
1779
1780 mc->mc_complete = NULL;
1781 mc->mc_private = mc; /* wake us when you're done */
1782 if ((error = mlx_start(mc)) != 0)
1783 return(error);
1784
1785 count = 0;
1786 /* XXX better timeout? */
1787 while ((mc->mc_status == MLX_STATUS_BUSY) && (count < 30)) {
1788 mtx_sleep(mc->mc_private, &sc->mlx_io_lock, PRIBIO | PCATCH, "mlxwcmd", hz);
1789 }
1790
1791 if (mc->mc_status != 0) {
1792 device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
1793 return(EIO);
1794 }
1795 return(0);
1796 }
1797
1798
1799 /********************************************************************************
1800 * Start the command (mc) and busy-wait for it to complete.
1801 *
1802 * Should only be used when interrupts can't be relied upon. Returns 0 on
1803 * success, nonzero on error.
1804 * Successfully completed commands are dequeued.
1805 */
1806 static int
mlx_poll_command(struct mlx_command * mc)1807 mlx_poll_command(struct mlx_command *mc)
1808 {
1809 struct mlx_softc *sc = mc->mc_sc;
1810 int error, count;
1811
1812 debug_called(1);
1813 MLX_IO_ASSERT_LOCKED(sc);
1814
1815 mc->mc_complete = NULL;
1816 mc->mc_private = NULL; /* we will poll for it */
1817 if ((error = mlx_start(mc)) != 0)
1818 return(error);
1819
1820 count = 0;
1821 do {
1822 /* poll for completion */
1823 mlx_done(mc->mc_sc, 1);
1824
1825 } while ((mc->mc_status == MLX_STATUS_BUSY) && (count++ < 15000000));
1826 if (mc->mc_status != MLX_STATUS_BUSY) {
1827 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
1828 return(0);
1829 }
1830 device_printf(sc->mlx_dev, "command failed - %s\n", mlx_diagnose_command(mc));
1831 return(EIO);
1832 }
1833
1834 void
mlx_startio_cb(void * arg,bus_dma_segment_t * segs,int nsegments,int error)1835 mlx_startio_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1836 {
1837 struct mlx_command *mc;
1838 struct mlxd_softc *mlxd;
1839 struct mlx_softc *sc;
1840 struct bio *bp;
1841 int blkcount;
1842 int driveno;
1843 int cmd;
1844
1845 mc = (struct mlx_command *)arg;
1846 mlx_setup_dmamap(mc, segs, nsegments, error);
1847
1848 sc = mc->mc_sc;
1849 bp = mc->mc_private;
1850
1851 if (bp->bio_cmd == BIO_READ) {
1852 mc->mc_flags |= MLX_CMD_DATAIN;
1853 cmd = MLX_CMD_READSG;
1854 } else {
1855 mc->mc_flags |= MLX_CMD_DATAOUT;
1856 cmd = MLX_CMD_WRITESG;
1857 }
1858
1859 /* build a suitable I/O command (assumes 512-byte rounded transfers) */
1860 mlxd = bp->bio_disk->d_drv1;
1861 driveno = mlxd->mlxd_drive - sc->mlx_sysdrive;
1862 blkcount = howmany(bp->bio_bcount, MLX_BLKSIZE);
1863
1864 if ((bp->bio_pblkno + blkcount) > sc->mlx_sysdrive[driveno].ms_size)
1865 device_printf(sc->mlx_dev,
1866 "I/O beyond end of unit (%lld,%d > %lu)\n",
1867 (long long)bp->bio_pblkno, blkcount,
1868 (u_long)sc->mlx_sysdrive[driveno].ms_size);
1869
1870 /*
1871 * Build the I/O command. Note that the SG list type bits are set to zero,
1872 * denoting the format of SG list that we are using.
1873 */
1874 if (sc->mlx_iftype == MLX_IFTYPE_2) {
1875 mlx_make_type1(mc, (cmd == MLX_CMD_WRITESG) ? MLX_CMD_WRITESG_OLD :
1876 MLX_CMD_READSG_OLD,
1877 blkcount & 0xff, /* xfer length low byte */
1878 bp->bio_pblkno, /* physical block number */
1879 driveno, /* target drive number */
1880 mc->mc_sgphys, /* location of SG list */
1881 mc->mc_nsgent & 0x3f); /* size of SG list */
1882 } else {
1883 mlx_make_type5(mc, cmd,
1884 blkcount & 0xff, /* xfer length low byte */
1885 (driveno << 3) | ((blkcount >> 8) & 0x07),
1886 /* target+length high 3 bits */
1887 bp->bio_pblkno, /* physical block number */
1888 mc->mc_sgphys, /* location of SG list */
1889 mc->mc_nsgent & 0x3f); /* size of SG list */
1890 }
1891
1892 /* try to give command to controller */
1893 if (mlx_start(mc) != 0) {
1894 /* fail the command */
1895 mc->mc_status = MLX_STATUS_WEDGED;
1896 mlx_completeio(mc);
1897 }
1898
1899 sc->mlx_state &= ~MLX_STATE_QFROZEN;
1900 }
1901
1902 /********************************************************************************
1903 * Pull as much work off the softc's work queue as possible and give it to the
1904 * controller. Leave a couple of slots free for emergencies.
1905 */
1906 static void
mlx_startio(struct mlx_softc * sc)1907 mlx_startio(struct mlx_softc *sc)
1908 {
1909 struct mlx_command *mc;
1910 struct bio *bp;
1911 int error;
1912
1913 MLX_IO_ASSERT_LOCKED(sc);
1914
1915 /* spin until something prevents us from doing any work */
1916 for (;;) {
1917 if (sc->mlx_state & MLX_STATE_QFROZEN)
1918 break;
1919
1920 /* see if there's work to be done */
1921 if ((bp = bioq_first(&sc->mlx_bioq)) == NULL)
1922 break;
1923 /* get a command */
1924 if ((mc = mlx_alloccmd(sc)) == NULL)
1925 break;
1926 /* get a slot for the command */
1927 if (mlx_getslot(mc) != 0) {
1928 mlx_releasecmd(mc);
1929 break;
1930 }
1931 /* get the buf containing our work */
1932 bioq_remove(&sc->mlx_bioq, bp);
1933 sc->mlx_waitbufs--;
1934
1935 /* connect the buf to the command */
1936 mc->mc_complete = mlx_completeio;
1937 mc->mc_private = bp;
1938 mc->mc_data = bp->bio_data;
1939 mc->mc_length = bp->bio_bcount;
1940
1941 /* map the command so the controller can work with it */
1942 error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
1943 mc->mc_length, mlx_startio_cb, mc, 0);
1944 if (error == EINPROGRESS) {
1945 sc->mlx_state |= MLX_STATE_QFROZEN;
1946 break;
1947 }
1948 }
1949 }
1950
1951 /********************************************************************************
1952 * Handle completion of an I/O command.
1953 */
1954 static void
mlx_completeio(struct mlx_command * mc)1955 mlx_completeio(struct mlx_command *mc)
1956 {
1957 struct mlx_softc *sc = mc->mc_sc;
1958 struct bio *bp = mc->mc_private;
1959 struct mlxd_softc *mlxd = bp->bio_disk->d_drv1;
1960
1961 MLX_IO_ASSERT_LOCKED(sc);
1962 if (mc->mc_status != MLX_STATUS_OK) { /* could be more verbose here? */
1963 bp->bio_error = EIO;
1964 bp->bio_flags |= BIO_ERROR;
1965
1966 switch(mc->mc_status) {
1967 case MLX_STATUS_RDWROFFLINE: /* system drive has gone offline */
1968 device_printf(mlxd->mlxd_dev, "drive offline\n");
1969 /* should signal this with a return code */
1970 mlxd->mlxd_drive->ms_state = MLX_SYSD_OFFLINE;
1971 break;
1972
1973 default: /* other I/O error */
1974 device_printf(sc->mlx_dev, "I/O error - %s\n", mlx_diagnose_command(mc));
1975 #if 0
1976 device_printf(sc->mlx_dev, " b_bcount %ld blkcount %ld b_pblkno %d\n",
1977 bp->bio_bcount, bp->bio_bcount / MLX_BLKSIZE, bp->bio_pblkno);
1978 device_printf(sc->mlx_dev, " %13D\n", mc->mc_mailbox, " ");
1979 #endif
1980 break;
1981 }
1982 }
1983 mlx_releasecmd(mc);
1984 mlxd_intr(bp);
1985 }
1986
1987 void
mlx_user_cb(void * arg,bus_dma_segment_t * segs,int nsegments,int error)1988 mlx_user_cb(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1989 {
1990 struct mlx_usercommand *mu;
1991 struct mlx_command *mc;
1992 struct mlx_dcdb *dcdb;
1993
1994 mc = (struct mlx_command *)arg;
1995 if (error)
1996 return;
1997
1998 mlx_setup_dmamap(mc, segs, nsegments, error);
1999
2000 mu = (struct mlx_usercommand *)mc->mc_private;
2001 dcdb = NULL;
2002
2003 /*
2004 * If this is a passthrough SCSI command, the DCDB is packed at the
2005 * beginning of the data area. Fix up the DCDB to point to the correct
2006 * physical address and override any bufptr supplied by the caller since
2007 * we know what it's meant to be.
2008 */
2009 if (mc->mc_mailbox[0] == MLX_CMD_DIRECT_CDB) {
2010 dcdb = (struct mlx_dcdb *)mc->mc_data;
2011 dcdb->dcdb_physaddr = mc->mc_dataphys + sizeof(*dcdb);
2012 mu->mu_bufptr = 8;
2013 }
2014
2015 /*
2016 * If there's a data buffer, fix up the command's buffer pointer.
2017 */
2018 if (mu->mu_datasize > 0) {
2019 mc->mc_mailbox[mu->mu_bufptr ] = mc->mc_dataphys & 0xff;
2020 mc->mc_mailbox[mu->mu_bufptr + 1] = (mc->mc_dataphys >> 8) & 0xff;
2021 mc->mc_mailbox[mu->mu_bufptr + 2] = (mc->mc_dataphys >> 16) & 0xff;
2022 mc->mc_mailbox[mu->mu_bufptr + 3] = (mc->mc_dataphys >> 24) & 0xff;
2023 }
2024 debug(0, "command fixup");
2025
2026 /* submit the command and wait */
2027 if (mlx_wait_command(mc) != 0)
2028 return;
2029
2030 }
2031
2032 /********************************************************************************
2033 * Take a command from user-space and try to run it.
2034 *
2035 * XXX Note that this can't perform very much in the way of error checking, and
2036 * as such, applications _must_ be considered trustworthy.
2037 * XXX Commands using S/G for data are not supported.
2038 */
2039 static int
mlx_user_command(struct mlx_softc * sc,struct mlx_usercommand * mu)2040 mlx_user_command(struct mlx_softc *sc, struct mlx_usercommand *mu)
2041 {
2042 struct mlx_command *mc;
2043 void *kbuf;
2044 int error;
2045
2046 debug_called(0);
2047
2048 kbuf = NULL;
2049 mc = NULL;
2050 error = ENOMEM;
2051
2052 /* get ourselves a command and copy in from user space */
2053 MLX_IO_LOCK(sc);
2054 if ((mc = mlx_alloccmd(sc)) == NULL) {
2055 MLX_IO_UNLOCK(sc);
2056 return(error);
2057 }
2058 bcopy(mu->mu_command, mc->mc_mailbox, sizeof(mc->mc_mailbox));
2059 debug(0, "got command buffer");
2060
2061 /*
2062 * if we need a buffer for data transfer, allocate one and copy in its
2063 * initial contents
2064 */
2065 if (mu->mu_datasize > 0) {
2066 if (mu->mu_datasize > MLX_MAXPHYS) {
2067 error = EINVAL;
2068 goto out;
2069 }
2070 MLX_IO_UNLOCK(sc);
2071 kbuf = malloc(mu->mu_datasize, M_DEVBUF, M_WAITOK);
2072 if ((error = copyin(mu->mu_buf, kbuf, mu->mu_datasize))) {
2073 MLX_IO_LOCK(sc);
2074 goto out;
2075 }
2076 MLX_IO_LOCK(sc);
2077 debug(0, "got kernel buffer");
2078 }
2079
2080 /* get a command slot */
2081 if (mlx_getslot(mc))
2082 goto out;
2083 debug(0, "got a slot");
2084
2085 if (mu->mu_datasize > 0) {
2086
2087 /* range check the pointer to physical buffer address */
2088 if ((mu->mu_bufptr < 0) || (mu->mu_bufptr > (sizeof(mu->mu_command) -
2089 sizeof(u_int32_t)))) {
2090 error = EINVAL;
2091 goto out;
2092 }
2093 }
2094
2095 /* map the command so the controller can see it */
2096 mc->mc_data = kbuf;
2097 mc->mc_length = mu->mu_datasize;
2098 mc->mc_private = mu;
2099 error = bus_dmamap_load(sc->mlx_buffer_dmat, mc->mc_dmamap, mc->mc_data,
2100 mc->mc_length, mlx_user_cb, mc, BUS_DMA_NOWAIT);
2101 if (error)
2102 goto out;
2103
2104 /* copy out status and data */
2105 mu->mu_status = mc->mc_status;
2106 if (mu->mu_datasize > 0) {
2107 MLX_IO_UNLOCK(sc);
2108 error = copyout(kbuf, mu->mu_buf, mu->mu_datasize);
2109 MLX_IO_LOCK(sc);
2110 }
2111
2112 out:
2113 mlx_releasecmd(mc);
2114 MLX_IO_UNLOCK(sc);
2115 if (kbuf != NULL)
2116 free(kbuf, M_DEVBUF);
2117 return(error);
2118 }
2119
2120 /********************************************************************************
2121 ********************************************************************************
2122 Command I/O to Controller
2123 ********************************************************************************
2124 ********************************************************************************/
2125
2126 /********************************************************************************
2127 * Find a free command slot for (mc).
2128 *
2129 * Don't hand out a slot to a normal-priority command unless there are at least
2130 * 4 slots free for priority commands.
2131 */
2132 static int
mlx_getslot(struct mlx_command * mc)2133 mlx_getslot(struct mlx_command *mc)
2134 {
2135 struct mlx_softc *sc = mc->mc_sc;
2136 int slot, limit;
2137
2138 debug_called(1);
2139
2140 MLX_IO_ASSERT_LOCKED(sc);
2141
2142 /*
2143 * Enforce slot-usage limit, if we have the required information.
2144 */
2145 if (sc->mlx_enq2 != NULL) {
2146 limit = sc->mlx_enq2->me_max_commands;
2147 } else {
2148 limit = 2;
2149 }
2150 if (sc->mlx_busycmds >= ((mc->mc_flags & MLX_CMD_PRIORITY) ? limit : limit - 4))
2151 return(EBUSY);
2152
2153 /*
2154 * Allocate an outstanding command slot
2155 *
2156 * XXX linear search is slow
2157 */
2158 for (slot = 0; slot < limit; slot++) {
2159 debug(2, "try slot %d", slot);
2160 if (sc->mlx_busycmd[slot] == NULL)
2161 break;
2162 }
2163 if (slot < limit) {
2164 sc->mlx_busycmd[slot] = mc;
2165 sc->mlx_busycmds++;
2166 }
2167
2168 /* out of slots? */
2169 if (slot >= limit)
2170 return(EBUSY);
2171
2172 debug(2, "got slot %d", slot);
2173 mc->mc_slot = slot;
2174 return(0);
2175 }
2176
2177 /********************************************************************************
2178 * Map/unmap (mc)'s data in the controller's addressable space.
2179 */
2180 static void
mlx_setup_dmamap(struct mlx_command * mc,bus_dma_segment_t * segs,int nsegments,int error)2181 mlx_setup_dmamap(struct mlx_command *mc, bus_dma_segment_t *segs, int nsegments,
2182 int error)
2183 {
2184 struct mlx_softc *sc = mc->mc_sc;
2185 struct mlx_sgentry *sg;
2186 int i;
2187
2188 debug_called(1);
2189
2190 /* XXX should be unnecessary */
2191 if (sc->mlx_enq2 && (nsegments > sc->mlx_enq2->me_max_sg))
2192 panic("MLX: too many s/g segments (%d, max %d)", nsegments,
2193 sc->mlx_enq2->me_max_sg);
2194
2195 /* get base address of s/g table */
2196 sg = sc->mlx_sgtable + (mc->mc_slot * MLX_NSEG);
2197
2198 /* save s/g table information in command */
2199 mc->mc_nsgent = nsegments;
2200 mc->mc_sgphys = sc->mlx_sgbusaddr +
2201 (mc->mc_slot * MLX_NSEG * sizeof(struct mlx_sgentry));
2202 mc->mc_dataphys = segs[0].ds_addr;
2203
2204 /* populate s/g table */
2205 for (i = 0; i < nsegments; i++, sg++) {
2206 sg->sg_addr = segs[i].ds_addr;
2207 sg->sg_count = segs[i].ds_len;
2208 }
2209
2210 /* Make sure the buffers are visible on the bus. */
2211 if (mc->mc_flags & MLX_CMD_DATAIN)
2212 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap,
2213 BUS_DMASYNC_PREREAD);
2214 if (mc->mc_flags & MLX_CMD_DATAOUT)
2215 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap,
2216 BUS_DMASYNC_PREWRITE);
2217 }
2218
2219 static void
mlx_unmapcmd(struct mlx_command * mc)2220 mlx_unmapcmd(struct mlx_command *mc)
2221 {
2222 struct mlx_softc *sc = mc->mc_sc;
2223
2224 debug_called(1);
2225
2226 /* if the command involved data at all */
2227 if (mc->mc_data != NULL) {
2228
2229 if (mc->mc_flags & MLX_CMD_DATAIN)
2230 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTREAD);
2231 if (mc->mc_flags & MLX_CMD_DATAOUT)
2232 bus_dmamap_sync(sc->mlx_buffer_dmat, mc->mc_dmamap, BUS_DMASYNC_POSTWRITE);
2233
2234 bus_dmamap_unload(sc->mlx_buffer_dmat, mc->mc_dmamap);
2235 }
2236 }
2237
2238 /********************************************************************************
2239 * Try to deliver (mc) to the controller.
2240 *
2241 * Can be called at any interrupt level, with or without interrupts enabled.
2242 */
2243 static int
mlx_start(struct mlx_command * mc)2244 mlx_start(struct mlx_command *mc)
2245 {
2246 struct mlx_softc *sc = mc->mc_sc;
2247 int i;
2248
2249 debug_called(1);
2250
2251 /* save the slot number as ident so we can handle this command when complete */
2252 mc->mc_mailbox[0x1] = mc->mc_slot;
2253
2254 /* mark the command as currently being processed */
2255 mc->mc_status = MLX_STATUS_BUSY;
2256
2257 /* set a default 60-second timeout XXX tunable? XXX not currently used */
2258 mc->mc_timeout = time_second + 60;
2259
2260 /* spin waiting for the mailbox */
2261 for (i = 100000; i > 0; i--) {
2262 if (sc->mlx_tryqueue(sc, mc)) {
2263 /* move command to work queue */
2264 TAILQ_INSERT_TAIL(&sc->mlx_work, mc, mc_link);
2265 return (0);
2266 } else if (i > 1)
2267 mlx_done(sc, 0);
2268 }
2269
2270 /*
2271 * We couldn't get the controller to take the command. Revoke the slot
2272 * that the command was given and return it with a bad status.
2273 */
2274 sc->mlx_busycmd[mc->mc_slot] = NULL;
2275 device_printf(sc->mlx_dev, "controller wedged (not taking commands)\n");
2276 mc->mc_status = MLX_STATUS_WEDGED;
2277 mlx_complete(sc);
2278 return(EIO);
2279 }
2280
2281 /********************************************************************************
2282 * Poll the controller (sc) for completed commands.
2283 * Update command status and free slots for reuse. If any slots were freed,
2284 * new commands may be posted.
2285 *
2286 * Returns nonzero if one or more commands were completed.
2287 */
2288 static int
mlx_done(struct mlx_softc * sc,int startio)2289 mlx_done(struct mlx_softc *sc, int startio)
2290 {
2291 struct mlx_command *mc;
2292 int result;
2293 u_int8_t slot;
2294 u_int16_t status;
2295
2296 debug_called(2);
2297 MLX_IO_ASSERT_LOCKED(sc);
2298
2299 result = 0;
2300
2301 /* loop collecting completed commands */
2302 for (;;) {
2303 /* poll for a completed command's identifier and status */
2304 if (sc->mlx_findcomplete(sc, &slot, &status)) {
2305 result = 1;
2306 mc = sc->mlx_busycmd[slot]; /* find command */
2307 if (mc != NULL) { /* paranoia */
2308 if (mc->mc_status == MLX_STATUS_BUSY) {
2309 mc->mc_status = status; /* save status */
2310
2311 /* free slot for reuse */
2312 sc->mlx_busycmd[slot] = NULL;
2313 sc->mlx_busycmds--;
2314 } else {
2315 device_printf(sc->mlx_dev, "duplicate done event for slot %d\n", slot);
2316 }
2317 } else {
2318 device_printf(sc->mlx_dev, "done event for nonbusy slot %d\n", slot);
2319 }
2320 } else {
2321 break;
2322 }
2323 }
2324
2325 /* if we've completed any commands, try posting some more */
2326 if (result && startio)
2327 mlx_startio(sc);
2328
2329 /* handle completion and timeouts */
2330 mlx_complete(sc);
2331
2332 return(result);
2333 }
2334
2335 /********************************************************************************
2336 * Perform post-completion processing for commands on (sc).
2337 */
2338 static void
mlx_complete(struct mlx_softc * sc)2339 mlx_complete(struct mlx_softc *sc)
2340 {
2341 struct mlx_command *mc, *nc;
2342
2343 debug_called(2);
2344 MLX_IO_ASSERT_LOCKED(sc);
2345
2346 /* scan the list of busy/done commands */
2347 mc = TAILQ_FIRST(&sc->mlx_work);
2348 while (mc != NULL) {
2349 nc = TAILQ_NEXT(mc, mc_link);
2350
2351 /* Command has been completed in some fashion */
2352 if (mc->mc_status != MLX_STATUS_BUSY) {
2353
2354 /* unmap the command's data buffer */
2355 mlx_unmapcmd(mc);
2356 /*
2357 * Does the command have a completion handler?
2358 */
2359 if (mc->mc_complete != NULL) {
2360 /* remove from list and give to handler */
2361 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
2362 mc->mc_complete(mc);
2363
2364 /*
2365 * Is there a sleeper waiting on this command?
2366 */
2367 } else if (mc->mc_private != NULL) { /* sleeping caller wants to know about it */
2368
2369 /* remove from list and wake up sleeper */
2370 TAILQ_REMOVE(&sc->mlx_work, mc, mc_link);
2371 wakeup_one(mc->mc_private);
2372
2373 /*
2374 * Leave the command for a caller that's polling for it.
2375 */
2376 } else {
2377 }
2378 }
2379 mc = nc;
2380 }
2381 }
2382
2383 /********************************************************************************
2384 ********************************************************************************
2385 Command Buffer Management
2386 ********************************************************************************
2387 ********************************************************************************/
2388
2389 /********************************************************************************
2390 * Get a new command buffer.
2391 *
2392 * This may return NULL in low-memory cases.
2393 *
2394 * Note that using malloc() is expensive (the command buffer is << 1 page) but
2395 * necessary if we are to be a loadable module before the zone allocator is fixed.
2396 *
2397 * If possible, we recycle a command buffer that's been used before.
2398 *
2399 * XXX Note that command buffers are not cleaned out - it is the caller's
2400 * responsibility to ensure that all required fields are filled in before
2401 * using a buffer.
2402 */
2403 static struct mlx_command *
mlx_alloccmd(struct mlx_softc * sc)2404 mlx_alloccmd(struct mlx_softc *sc)
2405 {
2406 struct mlx_command *mc;
2407 int error;
2408
2409 debug_called(1);
2410
2411 MLX_IO_ASSERT_LOCKED(sc);
2412 if ((mc = TAILQ_FIRST(&sc->mlx_freecmds)) != NULL)
2413 TAILQ_REMOVE(&sc->mlx_freecmds, mc, mc_link);
2414
2415 /* allocate a new command buffer? */
2416 if (mc == NULL) {
2417 mc = (struct mlx_command *)malloc(sizeof(*mc), M_DEVBUF, M_NOWAIT | M_ZERO);
2418 if (mc != NULL) {
2419 mc->mc_sc = sc;
2420 error = bus_dmamap_create(sc->mlx_buffer_dmat, 0, &mc->mc_dmamap);
2421 if (error) {
2422 free(mc, M_DEVBUF);
2423 return(NULL);
2424 }
2425 }
2426 }
2427 return(mc);
2428 }
2429
2430 /********************************************************************************
2431 * Release a command buffer for recycling.
2432 *
2433 * XXX It might be a good idea to limit the number of commands we save for reuse
2434 * if it's shown that this list bloats out massively.
2435 */
2436 static void
mlx_releasecmd(struct mlx_command * mc)2437 mlx_releasecmd(struct mlx_command *mc)
2438 {
2439
2440 debug_called(1);
2441
2442 MLX_IO_ASSERT_LOCKED(mc->mc_sc);
2443 TAILQ_INSERT_HEAD(&mc->mc_sc->mlx_freecmds, mc, mc_link);
2444 }
2445
2446 /********************************************************************************
2447 * Permanently discard a command buffer.
2448 */
2449 static void
mlx_freecmd(struct mlx_command * mc)2450 mlx_freecmd(struct mlx_command *mc)
2451 {
2452 struct mlx_softc *sc = mc->mc_sc;
2453
2454 debug_called(1);
2455 bus_dmamap_destroy(sc->mlx_buffer_dmat, mc->mc_dmamap);
2456 free(mc, M_DEVBUF);
2457 }
2458
2459
2460 /********************************************************************************
2461 ********************************************************************************
2462 Type 3 interface accessor methods
2463 ********************************************************************************
2464 ********************************************************************************/
2465
2466 /********************************************************************************
2467 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2468 * (the controller is not ready to take a command).
2469 */
2470 static int
mlx_v3_tryqueue(struct mlx_softc * sc,struct mlx_command * mc)2471 mlx_v3_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2472 {
2473 int i;
2474
2475 debug_called(2);
2476 MLX_IO_ASSERT_LOCKED(sc);
2477
2478 /* ready for our command? */
2479 if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_FULL)) {
2480 /* copy mailbox data to window */
2481 for (i = 0; i < 13; i++)
2482 MLX_V3_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2483
2484 /* post command */
2485 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_FULL);
2486 return(1);
2487 }
2488 return(0);
2489 }
2490
2491 /********************************************************************************
2492 * See if a command has been completed, if so acknowledge its completion
2493 * and recover the slot number and status code.
2494 */
2495 static int
mlx_v3_findcomplete(struct mlx_softc * sc,u_int8_t * slot,u_int16_t * status)2496 mlx_v3_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2497 {
2498
2499 debug_called(2);
2500 MLX_IO_ASSERT_LOCKED(sc);
2501
2502 /* status available? */
2503 if (MLX_V3_GET_ODBR(sc) & MLX_V3_ODB_SAVAIL) {
2504 *slot = MLX_V3_GET_STATUS_IDENT(sc); /* get command identifier */
2505 *status = MLX_V3_GET_STATUS(sc); /* get status */
2506
2507 /* acknowledge completion */
2508 MLX_V3_PUT_ODBR(sc, MLX_V3_ODB_SAVAIL);
2509 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
2510 return(1);
2511 }
2512 return(0);
2513 }
2514
2515 /********************************************************************************
2516 * Enable/disable interrupts as requested. (No acknowledge required)
2517 */
2518 static void
mlx_v3_intaction(struct mlx_softc * sc,int action)2519 mlx_v3_intaction(struct mlx_softc *sc, int action)
2520 {
2521 debug_called(1);
2522 MLX_IO_ASSERT_LOCKED(sc);
2523
2524 switch(action) {
2525 case MLX_INTACTION_DISABLE:
2526 MLX_V3_PUT_IER(sc, 0);
2527 sc->mlx_state &= ~MLX_STATE_INTEN;
2528 break;
2529 case MLX_INTACTION_ENABLE:
2530 MLX_V3_PUT_IER(sc, 1);
2531 sc->mlx_state |= MLX_STATE_INTEN;
2532 break;
2533 }
2534 }
2535
2536 /********************************************************************************
2537 * Poll for firmware error codes during controller initialisation.
2538 * Returns 0 if initialisation is complete, 1 if still in progress but no
2539 * error has been fetched, 2 if an error has been retrieved.
2540 */
2541 static int
mlx_v3_fw_handshake(struct mlx_softc * sc,int * error,int * param1,int * param2,int first)2542 mlx_v3_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2,
2543 int first)
2544 {
2545 u_int8_t fwerror;
2546
2547 debug_called(2);
2548
2549 /* first time around, clear any hardware completion status */
2550 if (first) {
2551 MLX_V3_PUT_IDBR(sc, MLX_V3_IDB_SACK);
2552 DELAY(1000);
2553 }
2554
2555 /* init in progress? */
2556 if (!(MLX_V3_GET_IDBR(sc) & MLX_V3_IDB_INIT_BUSY))
2557 return(0);
2558
2559 /* test error value */
2560 fwerror = MLX_V3_GET_FWERROR(sc);
2561 if (!(fwerror & MLX_V3_FWERROR_PEND))
2562 return(1);
2563
2564 /* mask status pending bit, fetch status */
2565 *error = fwerror & ~MLX_V3_FWERROR_PEND;
2566 *param1 = MLX_V3_GET_FWERROR_PARAM1(sc);
2567 *param2 = MLX_V3_GET_FWERROR_PARAM2(sc);
2568
2569 /* acknowledge */
2570 MLX_V3_PUT_FWERROR(sc, 0);
2571
2572 return(2);
2573 }
2574
2575 /********************************************************************************
2576 ********************************************************************************
2577 Type 4 interface accessor methods
2578 ********************************************************************************
2579 ********************************************************************************/
2580
2581 /********************************************************************************
2582 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2583 * (the controller is not ready to take a command).
2584 */
2585 static int
mlx_v4_tryqueue(struct mlx_softc * sc,struct mlx_command * mc)2586 mlx_v4_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2587 {
2588 int i;
2589
2590 debug_called(2);
2591 MLX_IO_ASSERT_LOCKED(sc);
2592
2593 /* ready for our command? */
2594 if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_FULL)) {
2595 /* copy mailbox data to window */
2596 for (i = 0; i < 13; i++)
2597 MLX_V4_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2598
2599 /* memory-mapped controller, so issue a write barrier to ensure the mailbox is filled */
2600 bus_barrier(sc->mlx_mem, MLX_V4_MAILBOX, MLX_V4_MAILBOX_LENGTH,
2601 BUS_SPACE_BARRIER_WRITE);
2602
2603 /* post command */
2604 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_HWMBOX_CMD);
2605 return(1);
2606 }
2607 return(0);
2608 }
2609
2610 /********************************************************************************
2611 * See if a command has been completed, if so acknowledge its completion
2612 * and recover the slot number and status code.
2613 */
2614 static int
mlx_v4_findcomplete(struct mlx_softc * sc,u_int8_t * slot,u_int16_t * status)2615 mlx_v4_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2616 {
2617
2618 debug_called(2);
2619 MLX_IO_ASSERT_LOCKED(sc);
2620
2621 /* status available? */
2622 if (MLX_V4_GET_ODBR(sc) & MLX_V4_ODB_HWSAVAIL) {
2623 *slot = MLX_V4_GET_STATUS_IDENT(sc); /* get command identifier */
2624 *status = MLX_V4_GET_STATUS(sc); /* get status */
2625
2626 /* acknowledge completion */
2627 MLX_V4_PUT_ODBR(sc, MLX_V4_ODB_HWMBOX_ACK);
2628 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2629 return(1);
2630 }
2631 return(0);
2632 }
2633
2634 /********************************************************************************
2635 * Enable/disable interrupts as requested.
2636 */
2637 static void
mlx_v4_intaction(struct mlx_softc * sc,int action)2638 mlx_v4_intaction(struct mlx_softc *sc, int action)
2639 {
2640 debug_called(1);
2641 MLX_IO_ASSERT_LOCKED(sc);
2642
2643 switch(action) {
2644 case MLX_INTACTION_DISABLE:
2645 MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK | MLX_V4_IER_DISINT);
2646 sc->mlx_state &= ~MLX_STATE_INTEN;
2647 break;
2648 case MLX_INTACTION_ENABLE:
2649 MLX_V4_PUT_IER(sc, MLX_V4_IER_MASK & ~MLX_V4_IER_DISINT);
2650 sc->mlx_state |= MLX_STATE_INTEN;
2651 break;
2652 }
2653 }
2654
2655 /********************************************************************************
2656 * Poll for firmware error codes during controller initialisation.
2657 * Returns 0 if initialisation is complete, 1 if still in progress but no
2658 * error has been fetched, 2 if an error has been retrieved.
2659 */
2660 static int
mlx_v4_fw_handshake(struct mlx_softc * sc,int * error,int * param1,int * param2,int first)2661 mlx_v4_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2,
2662 int first)
2663 {
2664 u_int8_t fwerror;
2665
2666 debug_called(2);
2667
2668 /* first time around, clear any hardware completion status */
2669 if (first) {
2670 MLX_V4_PUT_IDBR(sc, MLX_V4_IDB_SACK);
2671 DELAY(1000);
2672 }
2673
2674 /* init in progress? */
2675 if (!(MLX_V4_GET_IDBR(sc) & MLX_V4_IDB_INIT_BUSY))
2676 return(0);
2677
2678 /* test error value */
2679 fwerror = MLX_V4_GET_FWERROR(sc);
2680 if (!(fwerror & MLX_V4_FWERROR_PEND))
2681 return(1);
2682
2683 /* mask status pending bit, fetch status */
2684 *error = fwerror & ~MLX_V4_FWERROR_PEND;
2685 *param1 = MLX_V4_GET_FWERROR_PARAM1(sc);
2686 *param2 = MLX_V4_GET_FWERROR_PARAM2(sc);
2687
2688 /* acknowledge */
2689 MLX_V4_PUT_FWERROR(sc, 0);
2690
2691 return(2);
2692 }
2693
2694 /********************************************************************************
2695 ********************************************************************************
2696 Type 5 interface accessor methods
2697 ********************************************************************************
2698 ********************************************************************************/
2699
2700 /********************************************************************************
2701 * Try to give (mc) to the controller. Returns 1 if successful, 0 on failure
2702 * (the controller is not ready to take a command).
2703 */
2704 static int
mlx_v5_tryqueue(struct mlx_softc * sc,struct mlx_command * mc)2705 mlx_v5_tryqueue(struct mlx_softc *sc, struct mlx_command *mc)
2706 {
2707 int i;
2708
2709 debug_called(2);
2710 MLX_IO_ASSERT_LOCKED(sc);
2711
2712 /* ready for our command? */
2713 if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_EMPTY) {
2714 /* copy mailbox data to window */
2715 for (i = 0; i < 13; i++)
2716 MLX_V5_PUT_MAILBOX(sc, i, mc->mc_mailbox[i]);
2717
2718 /* post command */
2719 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_HWMBOX_CMD);
2720 return(1);
2721 }
2722 return(0);
2723 }
2724
2725 /********************************************************************************
2726 * See if a command has been completed, if so acknowledge its completion
2727 * and recover the slot number and status code.
2728 */
2729 static int
mlx_v5_findcomplete(struct mlx_softc * sc,u_int8_t * slot,u_int16_t * status)2730 mlx_v5_findcomplete(struct mlx_softc *sc, u_int8_t *slot, u_int16_t *status)
2731 {
2732
2733 debug_called(2);
2734 MLX_IO_ASSERT_LOCKED(sc);
2735
2736 /* status available? */
2737 if (MLX_V5_GET_ODBR(sc) & MLX_V5_ODB_HWSAVAIL) {
2738 *slot = MLX_V5_GET_STATUS_IDENT(sc); /* get command identifier */
2739 *status = MLX_V5_GET_STATUS(sc); /* get status */
2740
2741 /* acknowledge completion */
2742 MLX_V5_PUT_ODBR(sc, MLX_V5_ODB_HWMBOX_ACK);
2743 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
2744 return(1);
2745 }
2746 return(0);
2747 }
2748
2749 /********************************************************************************
2750 * Enable/disable interrupts as requested.
2751 */
2752 static void
mlx_v5_intaction(struct mlx_softc * sc,int action)2753 mlx_v5_intaction(struct mlx_softc *sc, int action)
2754 {
2755 debug_called(1);
2756 MLX_IO_ASSERT_LOCKED(sc);
2757
2758 switch(action) {
2759 case MLX_INTACTION_DISABLE:
2760 MLX_V5_PUT_IER(sc, 0xff & MLX_V5_IER_DISINT);
2761 sc->mlx_state &= ~MLX_STATE_INTEN;
2762 break;
2763 case MLX_INTACTION_ENABLE:
2764 MLX_V5_PUT_IER(sc, 0xff & ~MLX_V5_IER_DISINT);
2765 sc->mlx_state |= MLX_STATE_INTEN;
2766 break;
2767 }
2768 }
2769
2770 /********************************************************************************
2771 * Poll for firmware error codes during controller initialisation.
2772 * Returns 0 if initialisation is complete, 1 if still in progress but no
2773 * error has been fetched, 2 if an error has been retrieved.
2774 */
2775 static int
mlx_v5_fw_handshake(struct mlx_softc * sc,int * error,int * param1,int * param2,int first)2776 mlx_v5_fw_handshake(struct mlx_softc *sc, int *error, int *param1, int *param2,
2777 int first)
2778 {
2779 u_int8_t fwerror;
2780
2781 debug_called(2);
2782
2783 /* first time around, clear any hardware completion status */
2784 if (first) {
2785 MLX_V5_PUT_IDBR(sc, MLX_V5_IDB_SACK);
2786 DELAY(1000);
2787 }
2788
2789 /* init in progress? */
2790 if (MLX_V5_GET_IDBR(sc) & MLX_V5_IDB_INIT_DONE)
2791 return(0);
2792
2793 /* test for error value */
2794 fwerror = MLX_V5_GET_FWERROR(sc);
2795 if (!(fwerror & MLX_V5_FWERROR_PEND))
2796 return(1);
2797
2798 /* mask status pending bit, fetch status */
2799 *error = fwerror & ~MLX_V5_FWERROR_PEND;
2800 *param1 = MLX_V5_GET_FWERROR_PARAM1(sc);
2801 *param2 = MLX_V5_GET_FWERROR_PARAM2(sc);
2802
2803 /* acknowledge */
2804 MLX_V5_PUT_FWERROR(sc, 0xff);
2805
2806 return(2);
2807 }
2808
2809 /********************************************************************************
2810 ********************************************************************************
2811 Debugging
2812 ********************************************************************************
2813 ********************************************************************************/
2814
2815 /********************************************************************************
2816 * Return a status message describing (mc)
2817 */
2818 static char *mlx_status_messages[] = {
2819 "normal completion", /* 00 */
2820 "irrecoverable data error", /* 01 */
2821 "drive does not exist, or is offline", /* 02 */
2822 "attempt to write beyond end of drive", /* 03 */
2823 "bad data encountered", /* 04 */
2824 "invalid log entry request", /* 05 */
2825 "attempt to rebuild online drive", /* 06 */
2826 "new disk failed during rebuild", /* 07 */
2827 "invalid channel/target", /* 08 */
2828 "rebuild/check already in progress", /* 09 */
2829 "one or more disks are dead", /* 10 */
2830 "invalid or non-redundant drive", /* 11 */
2831 "channel is busy", /* 12 */
2832 "channel is not stopped", /* 13 */
2833 "rebuild successfully terminated", /* 14 */
2834 "unsupported command", /* 15 */
2835 "check condition received", /* 16 */
2836 "device is busy", /* 17 */
2837 "selection or command timeout", /* 18 */
2838 "command terminated abnormally", /* 19 */
2839 ""
2840 };
2841
2842 static struct
2843 {
2844 int command;
2845 u_int16_t status;
2846 int msg;
2847 } mlx_messages[] = {
2848 {MLX_CMD_READSG, 0x0001, 1},
2849 {MLX_CMD_READSG, 0x0002, 1},
2850 {MLX_CMD_READSG, 0x0105, 3},
2851 {MLX_CMD_READSG, 0x010c, 4},
2852 {MLX_CMD_WRITESG, 0x0001, 1},
2853 {MLX_CMD_WRITESG, 0x0002, 1},
2854 {MLX_CMD_WRITESG, 0x0105, 3},
2855 {MLX_CMD_READSG_OLD, 0x0001, 1},
2856 {MLX_CMD_READSG_OLD, 0x0002, 1},
2857 {MLX_CMD_READSG_OLD, 0x0105, 3},
2858 {MLX_CMD_WRITESG_OLD, 0x0001, 1},
2859 {MLX_CMD_WRITESG_OLD, 0x0002, 1},
2860 {MLX_CMD_WRITESG_OLD, 0x0105, 3},
2861 {MLX_CMD_LOGOP, 0x0105, 5},
2862 {MLX_CMD_REBUILDASYNC, 0x0002, 6},
2863 {MLX_CMD_REBUILDASYNC, 0x0004, 7},
2864 {MLX_CMD_REBUILDASYNC, 0x0105, 8},
2865 {MLX_CMD_REBUILDASYNC, 0x0106, 9},
2866 {MLX_CMD_REBUILDASYNC, 0x0107, 14},
2867 {MLX_CMD_CHECKASYNC, 0x0002, 10},
2868 {MLX_CMD_CHECKASYNC, 0x0105, 11},
2869 {MLX_CMD_CHECKASYNC, 0x0106, 9},
2870 {MLX_CMD_STOPCHANNEL, 0x0106, 12},
2871 {MLX_CMD_STOPCHANNEL, 0x0105, 8},
2872 {MLX_CMD_STARTCHANNEL, 0x0005, 13},
2873 {MLX_CMD_STARTCHANNEL, 0x0105, 8},
2874 {MLX_CMD_DIRECT_CDB, 0x0002, 16},
2875 {MLX_CMD_DIRECT_CDB, 0x0008, 17},
2876 {MLX_CMD_DIRECT_CDB, 0x000e, 18},
2877 {MLX_CMD_DIRECT_CDB, 0x000f, 19},
2878 {MLX_CMD_DIRECT_CDB, 0x0105, 8},
2879
2880 {0, 0x0104, 14},
2881 {-1, 0, 0}
2882 };
2883
2884 static char *
mlx_diagnose_command(struct mlx_command * mc)2885 mlx_diagnose_command(struct mlx_command *mc)
2886 {
2887 static char unkmsg[80];
2888 int i;
2889
2890 /* look up message in table */
2891 for (i = 0; mlx_messages[i].command != -1; i++)
2892 if (((mc->mc_mailbox[0] == mlx_messages[i].command) || (mlx_messages[i].command == 0)) &&
2893 (mc->mc_status == mlx_messages[i].status))
2894 return(mlx_status_messages[mlx_messages[i].msg]);
2895
2896 sprintf(unkmsg, "unknown response 0x%x for command 0x%x", (int)mc->mc_status, (int)mc->mc_mailbox[0]);
2897 return(unkmsg);
2898 }
2899
2900 /*******************************************************************************
2901 * Print a string describing the controller (sc)
2902 */
2903 static struct
2904 {
2905 int hwid;
2906 char *name;
2907 } mlx_controller_names[] = {
2908 {0x01, "960P/PD"},
2909 {0x02, "960PL"},
2910 {0x10, "960PG"},
2911 {0x11, "960PJ"},
2912 {0x12, "960PR"},
2913 {0x13, "960PT"},
2914 {0x14, "960PTL0"},
2915 {0x15, "960PRL"},
2916 {0x16, "960PTL1"},
2917 {0x20, "1164PVX"},
2918 {-1, NULL}
2919 };
2920
2921 static void
mlx_describe_controller(struct mlx_softc * sc)2922 mlx_describe_controller(struct mlx_softc *sc)
2923 {
2924 static char buf[80];
2925 char *model;
2926 int i;
2927
2928 for (i = 0, model = NULL; mlx_controller_names[i].name != NULL; i++) {
2929 if ((sc->mlx_enq2->me_hardware_id & 0xff) == mlx_controller_names[i].hwid) {
2930 model = mlx_controller_names[i].name;
2931 break;
2932 }
2933 }
2934 if (model == NULL) {
2935 sprintf(buf, " model 0x%x", sc->mlx_enq2->me_hardware_id & 0xff);
2936 model = buf;
2937 }
2938 device_printf(sc->mlx_dev, "DAC%s, %d channel%s, firmware %d.%02d-%c-%02d, %dMB RAM\n",
2939 model,
2940 sc->mlx_enq2->me_actual_channels,
2941 sc->mlx_enq2->me_actual_channels > 1 ? "s" : "",
2942 sc->mlx_enq2->me_firmware_id & 0xff,
2943 (sc->mlx_enq2->me_firmware_id >> 8) & 0xff,
2944 (sc->mlx_enq2->me_firmware_id >> 24) & 0xff,
2945 (sc->mlx_enq2->me_firmware_id >> 16) & 0xff,
2946 sc->mlx_enq2->me_mem_size / (1024 * 1024));
2947
2948 if (bootverbose) {
2949 device_printf(sc->mlx_dev, " Hardware ID 0x%08x\n", sc->mlx_enq2->me_hardware_id);
2950 device_printf(sc->mlx_dev, " Firmware ID 0x%08x\n", sc->mlx_enq2->me_firmware_id);
2951 device_printf(sc->mlx_dev, " Configured/Actual channels %d/%d\n", sc->mlx_enq2->me_configured_channels,
2952 sc->mlx_enq2->me_actual_channels);
2953 device_printf(sc->mlx_dev, " Max Targets %d\n", sc->mlx_enq2->me_max_targets);
2954 device_printf(sc->mlx_dev, " Max Tags %d\n", sc->mlx_enq2->me_max_tags);
2955 device_printf(sc->mlx_dev, " Max System Drives %d\n", sc->mlx_enq2->me_max_sys_drives);
2956 device_printf(sc->mlx_dev, " Max Arms %d\n", sc->mlx_enq2->me_max_arms);
2957 device_printf(sc->mlx_dev, " Max Spans %d\n", sc->mlx_enq2->me_max_spans);
2958 device_printf(sc->mlx_dev, " DRAM/cache/flash/NVRAM size %d/%d/%d/%d\n", sc->mlx_enq2->me_mem_size,
2959 sc->mlx_enq2->me_cache_size, sc->mlx_enq2->me_flash_size, sc->mlx_enq2->me_nvram_size);
2960 device_printf(sc->mlx_dev, " DRAM type %d\n", sc->mlx_enq2->me_mem_type);
2961 device_printf(sc->mlx_dev, " Clock Speed %dns\n", sc->mlx_enq2->me_clock_speed);
2962 device_printf(sc->mlx_dev, " Hardware Speed %dns\n", sc->mlx_enq2->me_hardware_speed);
2963 device_printf(sc->mlx_dev, " Max Commands %d\n", sc->mlx_enq2->me_max_commands);
2964 device_printf(sc->mlx_dev, " Max SG Entries %d\n", sc->mlx_enq2->me_max_sg);
2965 device_printf(sc->mlx_dev, " Max DP %d\n", sc->mlx_enq2->me_max_dp);
2966 device_printf(sc->mlx_dev, " Max IOD %d\n", sc->mlx_enq2->me_max_iod);
2967 device_printf(sc->mlx_dev, " Max Comb %d\n", sc->mlx_enq2->me_max_comb);
2968 device_printf(sc->mlx_dev, " Latency %ds\n", sc->mlx_enq2->me_latency);
2969 device_printf(sc->mlx_dev, " SCSI Timeout %ds\n", sc->mlx_enq2->me_scsi_timeout);
2970 device_printf(sc->mlx_dev, " Min Free Lines %d\n", sc->mlx_enq2->me_min_freelines);
2971 device_printf(sc->mlx_dev, " Rate Constant %d\n", sc->mlx_enq2->me_rate_const);
2972 device_printf(sc->mlx_dev, " MAXBLK %d\n", sc->mlx_enq2->me_maxblk);
2973 device_printf(sc->mlx_dev, " Blocking Factor %d sectors\n", sc->mlx_enq2->me_blocking_factor);
2974 device_printf(sc->mlx_dev, " Cache Line Size %d blocks\n", sc->mlx_enq2->me_cacheline);
2975 device_printf(sc->mlx_dev, " SCSI Capability %s%dMHz, %d bit\n",
2976 sc->mlx_enq2->me_scsi_cap & (1<<4) ? "differential " : "",
2977 (1 << ((sc->mlx_enq2->me_scsi_cap >> 2) & 3)) * 10,
2978 8 << (sc->mlx_enq2->me_scsi_cap & 0x3));
2979 device_printf(sc->mlx_dev, " Firmware Build Number %d\n", sc->mlx_enq2->me_firmware_build);
2980 device_printf(sc->mlx_dev, " Fault Management Type %d\n", sc->mlx_enq2->me_fault_mgmt_type);
2981 device_printf(sc->mlx_dev, " Features %b\n", sc->mlx_enq2->me_firmware_features,
2982 "\20\4Background Init\3Read Ahead\2MORE\1Cluster\n");
2983
2984 }
2985 }
2986
2987 /*******************************************************************************
2988 * Emit a string describing the firmware handshake status code, and return a flag
2989 * indicating whether the code represents a fatal error.
2990 *
2991 * Error code interpretations are from the Linux driver, and don't directly match
2992 * the messages printed by Mylex's BIOS. This may change if documentation on the
2993 * codes is forthcoming.
2994 */
2995 static int
mlx_fw_message(struct mlx_softc * sc,int error,int param1,int param2)2996 mlx_fw_message(struct mlx_softc *sc, int error, int param1, int param2)
2997 {
2998 switch(error) {
2999 case 0x00:
3000 device_printf(sc->mlx_dev, "physical drive %d:%d not responding\n", param2, param1);
3001 break;
3002 case 0x08:
3003 /* we could be neater about this and give some indication when we receive more of them */
3004 if (!(sc->mlx_flags & MLX_SPINUP_REPORTED)) {
3005 device_printf(sc->mlx_dev, "spinning up drives...\n");
3006 sc->mlx_flags |= MLX_SPINUP_REPORTED;
3007 }
3008 break;
3009 case 0x30:
3010 device_printf(sc->mlx_dev, "configuration checksum error\n");
3011 break;
3012 case 0x60:
3013 device_printf(sc->mlx_dev, "mirror race recovery failed\n");
3014 break;
3015 case 0x70:
3016 device_printf(sc->mlx_dev, "mirror race recovery in progress\n");
3017 break;
3018 case 0x90:
3019 device_printf(sc->mlx_dev, "physical drive %d:%d COD mismatch\n", param2, param1);
3020 break;
3021 case 0xa0:
3022 device_printf(sc->mlx_dev, "logical drive installation aborted\n");
3023 break;
3024 case 0xb0:
3025 device_printf(sc->mlx_dev, "mirror race on a critical system drive\n");
3026 break;
3027 case 0xd0:
3028 device_printf(sc->mlx_dev, "new controller configuration found\n");
3029 break;
3030 case 0xf0:
3031 device_printf(sc->mlx_dev, "FATAL MEMORY PARITY ERROR\n");
3032 return(1);
3033 default:
3034 device_printf(sc->mlx_dev, "unknown firmware initialisation error %02x:%02x:%02x\n", error, param1, param2);
3035 break;
3036 }
3037 return(0);
3038 }
3039
3040 /********************************************************************************
3041 ********************************************************************************
3042 Utility Functions
3043 ********************************************************************************
3044 ********************************************************************************/
3045
3046 /********************************************************************************
3047 * Find the disk whose unit number is (unit) on this controller
3048 */
3049 static struct mlx_sysdrive *
mlx_findunit(struct mlx_softc * sc,int unit)3050 mlx_findunit(struct mlx_softc *sc, int unit)
3051 {
3052 int i;
3053
3054 /* search system drives */
3055 MLX_CONFIG_ASSERT_LOCKED(sc);
3056 for (i = 0; i < MLX_MAXDRIVES; i++) {
3057 /* is this one attached? */
3058 if (sc->mlx_sysdrive[i].ms_disk != 0) {
3059 /* is this the one? */
3060 if (unit == device_get_unit(sc->mlx_sysdrive[i].ms_disk))
3061 return(&sc->mlx_sysdrive[i]);
3062 }
3063 }
3064 return(NULL);
3065 }
3066