mly.c revision 1.36.16.2 1 /* $NetBSD: mly.c,v 1.36.16.2 2008/06/02 13:23:41 mjf Exp $ */
2
3 /*-
4 * Copyright (c) 2001 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Andrew Doran, Thor Lancelot Simon, and Eric Haszlakiewicz.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*-
33 * Copyright (c) 2000, 2001 Michael Smith
34 * Copyright (c) 2000 BSDi
35 * All rights reserved.
36 *
37 * Redistribution and use in source and binary forms, with or without
38 * modification, are permitted provided that the following conditions
39 * are met:
40 * 1. Redistributions of source code must retain the above copyright
41 * notice, this list of conditions and the following disclaimer.
42 * 2. Redistributions in binary form must reproduce the above copyright
43 * notice, this list of conditions and the following disclaimer in the
44 * documentation and/or other materials provided with the distribution.
45 *
46 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
47 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
48 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
49 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
50 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
51 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
52 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
53 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
54 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
55 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * SUCH DAMAGE.
57 *
58 * from FreeBSD: mly.c,v 1.8 2001/07/14 00:12:22 msmith Exp
59 */
60
61 /*
62 * Driver for the Mylex AcceleRAID and eXtremeRAID family with v6 firmware.
63 *
64 * TODO:
65 *
66 * o Make mly->mly_btl a hash, then MLY_BTL_RESCAN becomes a SIMPLEQ.
67 * o Handle FC and multiple LUNs.
68 * o Fix mmbox usage.
69 * o Fix transfer speed fudge.
70 */
71
72 #include <sys/cdefs.h>
73 __KERNEL_RCSID(0, "$NetBSD: mly.c,v 1.36.16.2 2008/06/02 13:23:41 mjf Exp $");
74
75 #include <sys/param.h>
76 #include <sys/systm.h>
77 #include <sys/device.h>
78 #include <sys/kernel.h>
79 #include <sys/queue.h>
80 #include <sys/buf.h>
81 #include <sys/endian.h>
82 #include <sys/conf.h>
83 #include <sys/malloc.h>
84 #include <sys/ioctl.h>
85 #include <sys/scsiio.h>
86 #include <sys/kthread.h>
87 #include <sys/kauth.h>
88
89 #include <uvm/uvm_extern.h>
90
91 #include <sys/bus.h>
92
93 #include <dev/scsipi/scsi_all.h>
94 #include <dev/scsipi/scsipi_all.h>
95 #include <dev/scsipi/scsiconf.h>
96
97 #include <dev/pci/pcireg.h>
98 #include <dev/pci/pcivar.h>
99 #include <dev/pci/pcidevs.h>
100
101 #include <dev/pci/mlyreg.h>
102 #include <dev/pci/mlyio.h>
103 #include <dev/pci/mlyvar.h>
104 #include <dev/pci/mly_tables.h>
105
106 static void mly_attach(struct device *, struct device *, void *);
107 static int mly_match(struct device *, struct cfdata *, void *);
108 static const struct mly_ident *mly_find_ident(struct pci_attach_args *);
109 static int mly_fwhandshake(struct mly_softc *);
110 static int mly_flush(struct mly_softc *);
111 static int mly_intr(void *);
112 static void mly_shutdown(void *);
113
114 static int mly_alloc_ccbs(struct mly_softc *);
115 static void mly_check_event(struct mly_softc *);
116 static void mly_complete_event(struct mly_softc *, struct mly_ccb *);
117 static void mly_complete_rescan(struct mly_softc *, struct mly_ccb *);
118 static int mly_dmamem_alloc(struct mly_softc *, int, bus_dmamap_t *,
119 void **, bus_addr_t *, bus_dma_segment_t *);
120 static void mly_dmamem_free(struct mly_softc *, int, bus_dmamap_t,
121 void *, bus_dma_segment_t *);
122 static int mly_enable_mmbox(struct mly_softc *);
123 static void mly_fetch_event(struct mly_softc *);
124 static int mly_get_controllerinfo(struct mly_softc *);
125 static int mly_get_eventstatus(struct mly_softc *);
126 static int mly_ioctl(struct mly_softc *, struct mly_cmd_ioctl *,
127 void **, size_t, void *, size_t *);
128 static void mly_padstr(char *, const char *, int);
129 static void mly_process_event(struct mly_softc *, struct mly_event *);
130 static void mly_release_ccbs(struct mly_softc *);
131 static int mly_scan_btl(struct mly_softc *, int, int);
132 static void mly_scan_channel(struct mly_softc *, int);
133 static void mly_thread(void *);
134
135 static int mly_ccb_alloc(struct mly_softc *, struct mly_ccb **);
136 static void mly_ccb_complete(struct mly_softc *, struct mly_ccb *);
137 static void mly_ccb_enqueue(struct mly_softc *, struct mly_ccb *);
138 static void mly_ccb_free(struct mly_softc *, struct mly_ccb *);
139 static int mly_ccb_map(struct mly_softc *, struct mly_ccb *);
140 static int mly_ccb_poll(struct mly_softc *, struct mly_ccb *, int);
141 static int mly_ccb_submit(struct mly_softc *, struct mly_ccb *);
142 static void mly_ccb_unmap(struct mly_softc *, struct mly_ccb *);
143 static int mly_ccb_wait(struct mly_softc *, struct mly_ccb *, int);
144
145 static void mly_get_xfer_mode(struct mly_softc *, int,
146 struct scsipi_xfer_mode *);
147 static void mly_scsipi_complete(struct mly_softc *, struct mly_ccb *);
148 static int mly_scsipi_ioctl(struct scsipi_channel *, u_long, void *,
149 int, struct proc *);
150 static void mly_scsipi_minphys(struct buf *);
151 static void mly_scsipi_request(struct scsipi_channel *,
152 scsipi_adapter_req_t, void *);
153
154 static int mly_user_command(struct mly_softc *, struct mly_user_command *);
155 static int mly_user_health(struct mly_softc *, struct mly_user_health *);
156
157 extern struct cfdriver mly_cd;
158
159 CFATTACH_DECL(mly, sizeof(struct mly_softc),
160 mly_match, mly_attach, NULL, NULL);
161
162 dev_type_open(mlyopen);
163 dev_type_close(mlyclose);
164 dev_type_ioctl(mlyioctl);
165
166 const struct cdevsw mly_cdevsw = {
167 mlyopen, mlyclose, noread, nowrite, mlyioctl,
168 nostop, notty, nopoll, nommap, nokqfilter, D_OTHER,
169 };
170
171 static struct mly_ident {
172 u_short vendor;
173 u_short product;
174 u_short subvendor;
175 u_short subproduct;
176 int hwif;
177 const char *desc;
178 } const mly_ident[] = {
179 {
180 PCI_VENDOR_MYLEX,
181 PCI_PRODUCT_MYLEX_EXTREMERAID,
182 PCI_VENDOR_MYLEX,
183 0x0040,
184 MLY_HWIF_STRONGARM,
185 "eXtremeRAID 2000"
186 },
187 {
188 PCI_VENDOR_MYLEX,
189 PCI_PRODUCT_MYLEX_EXTREMERAID,
190 PCI_VENDOR_MYLEX,
191 0x0030,
192 MLY_HWIF_STRONGARM,
193 "eXtremeRAID 3000"
194 },
195 {
196 PCI_VENDOR_MYLEX,
197 PCI_PRODUCT_MYLEX_ACCELERAID,
198 PCI_VENDOR_MYLEX,
199 0x0050,
200 MLY_HWIF_I960RX,
201 "AcceleRAID 352"
202 },
203 {
204 PCI_VENDOR_MYLEX,
205 PCI_PRODUCT_MYLEX_ACCELERAID,
206 PCI_VENDOR_MYLEX,
207 0x0052,
208 MLY_HWIF_I960RX,
209 "AcceleRAID 170"
210 },
211 {
212 PCI_VENDOR_MYLEX,
213 PCI_PRODUCT_MYLEX_ACCELERAID,
214 PCI_VENDOR_MYLEX,
215 0x0054,
216 MLY_HWIF_I960RX,
217 "AcceleRAID 160"
218 },
219 };
220
221 static void *mly_sdh;
222
223 /*
224 * Try to find a `mly_ident' entry corresponding to this board.
225 */
226 static const struct mly_ident *
227 mly_find_ident(struct pci_attach_args *pa)
228 {
229 const struct mly_ident *mpi, *maxmpi;
230 pcireg_t reg;
231
232 mpi = mly_ident;
233 maxmpi = mpi + sizeof(mly_ident) / sizeof(mly_ident[0]);
234
235 if (PCI_CLASS(pa->pa_class) == PCI_CLASS_I2O)
236 return (NULL);
237
238 for (; mpi < maxmpi; mpi++) {
239 if (PCI_VENDOR(pa->pa_id) != mpi->vendor ||
240 PCI_PRODUCT(pa->pa_id) != mpi->product)
241 continue;
242
243 if (mpi->subvendor == 0x0000)
244 return (mpi);
245
246 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_SUBSYS_ID_REG);
247
248 if (PCI_VENDOR(reg) == mpi->subvendor &&
249 PCI_PRODUCT(reg) == mpi->subproduct)
250 return (mpi);
251 }
252
253 return (NULL);
254 }
255
256 /*
257 * Match a supported board.
258 */
259 static int
260 mly_match(struct device *parent, struct cfdata *cfdata,
261 void *aux)
262 {
263
264 return (mly_find_ident(aux) != NULL);
265 }
266
267 /*
268 * Attach a supported board.
269 */
270 static void
271 mly_attach(struct device *parent, struct device *self, void *aux)
272 {
273 struct pci_attach_args *pa;
274 struct mly_softc *mly;
275 struct mly_ioctl_getcontrollerinfo *mi;
276 const struct mly_ident *ident;
277 pci_chipset_tag_t pc;
278 pci_intr_handle_t ih;
279 bus_space_handle_t memh, ioh;
280 bus_space_tag_t memt, iot;
281 pcireg_t reg;
282 const char *intrstr;
283 int ior, memr, i, rv, state;
284 struct scsipi_adapter *adapt;
285 struct scsipi_channel *chan;
286 int maj;
287
288 mly = (struct mly_softc *)self;
289 pa = aux;
290 pc = pa->pa_pc;
291 ident = mly_find_ident(pa);
292 state = 0;
293
294 mly->mly_dmat = pa->pa_dmat;
295 mly->mly_hwif = ident->hwif;
296
297 printf(": Mylex %s\n", ident->desc);
298
299 /*
300 * Map the PCI register window.
301 */
302 memr = -1;
303 ior = -1;
304
305 for (i = 0x10; i <= 0x14; i += 4) {
306 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, i);
307
308 if (PCI_MAPREG_TYPE(reg) == PCI_MAPREG_TYPE_IO) {
309 if (ior == -1 && PCI_MAPREG_IO_SIZE(reg) != 0)
310 ior = i;
311 } else {
312 if (memr == -1 && PCI_MAPREG_MEM_SIZE(reg) != 0)
313 memr = i;
314 }
315 }
316
317 if (memr != -1)
318 if (pci_mapreg_map(pa, memr, PCI_MAPREG_TYPE_MEM, 0,
319 &memt, &memh, NULL, NULL))
320 memr = -1;
321 if (ior != -1)
322 if (pci_mapreg_map(pa, ior, PCI_MAPREG_TYPE_IO, 0,
323 &iot, &ioh, NULL, NULL))
324 ior = -1;
325
326 if (memr != -1) {
327 mly->mly_iot = memt;
328 mly->mly_ioh = memh;
329 } else if (ior != -1) {
330 mly->mly_iot = iot;
331 mly->mly_ioh = ioh;
332 } else {
333 aprint_error_dev(self, "can't map i/o or memory space\n");
334 return;
335 }
336
337 /*
338 * Enable the device.
339 */
340 reg = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
341 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
342 reg | PCI_COMMAND_MASTER_ENABLE);
343
344 /*
345 * Map and establish the interrupt.
346 */
347 if (pci_intr_map(pa, &ih)) {
348 aprint_error_dev(self, "can't map interrupt\n");
349 return;
350 }
351 intrstr = pci_intr_string(pc, ih);
352 mly->mly_ih = pci_intr_establish(pc, ih, IPL_BIO, mly_intr, mly);
353 if (mly->mly_ih == NULL) {
354 aprint_error_dev(self, "can't establish interrupt");
355 if (intrstr != NULL)
356 printf(" at %s", intrstr);
357 printf("\n");
358 return;
359 }
360
361 if (intrstr != NULL)
362 printf("%s: interrupting at %s\n", device_xname(&mly->mly_dv),
363 intrstr);
364
365 /*
366 * Take care of interface-specific tasks.
367 */
368 switch (mly->mly_hwif) {
369 case MLY_HWIF_I960RX:
370 mly->mly_doorbell_true = 0x00;
371 mly->mly_cmd_mailbox = MLY_I960RX_COMMAND_MAILBOX;
372 mly->mly_status_mailbox = MLY_I960RX_STATUS_MAILBOX;
373 mly->mly_idbr = MLY_I960RX_IDBR;
374 mly->mly_odbr = MLY_I960RX_ODBR;
375 mly->mly_error_status = MLY_I960RX_ERROR_STATUS;
376 mly->mly_interrupt_status = MLY_I960RX_INTERRUPT_STATUS;
377 mly->mly_interrupt_mask = MLY_I960RX_INTERRUPT_MASK;
378 break;
379
380 case MLY_HWIF_STRONGARM:
381 mly->mly_doorbell_true = 0xff;
382 mly->mly_cmd_mailbox = MLY_STRONGARM_COMMAND_MAILBOX;
383 mly->mly_status_mailbox = MLY_STRONGARM_STATUS_MAILBOX;
384 mly->mly_idbr = MLY_STRONGARM_IDBR;
385 mly->mly_odbr = MLY_STRONGARM_ODBR;
386 mly->mly_error_status = MLY_STRONGARM_ERROR_STATUS;
387 mly->mly_interrupt_status = MLY_STRONGARM_INTERRUPT_STATUS;
388 mly->mly_interrupt_mask = MLY_STRONGARM_INTERRUPT_MASK;
389 break;
390 }
391
392 /*
393 * Allocate and map the scatter/gather lists.
394 */
395 rv = mly_dmamem_alloc(mly, MLY_SGL_SIZE * MLY_MAX_CCBS,
396 &mly->mly_sg_dmamap, (void **)&mly->mly_sg,
397 &mly->mly_sg_busaddr, &mly->mly_sg_seg);
398 if (rv) {
399 printf("%s: unable to allocate S/G maps\n",
400 device_xname(&mly->mly_dv));
401 goto bad;
402 }
403 state++;
404
405 /*
406 * Allocate and map the memory mailbox.
407 */
408 rv = mly_dmamem_alloc(mly, sizeof(struct mly_mmbox),
409 &mly->mly_mmbox_dmamap, (void **)&mly->mly_mmbox,
410 &mly->mly_mmbox_busaddr, &mly->mly_mmbox_seg);
411 if (rv) {
412 aprint_error_dev(&mly->mly_dv, "unable to allocate mailboxes\n");
413 goto bad;
414 }
415 state++;
416
417 /*
418 * Initialise per-controller queues.
419 */
420 SLIST_INIT(&mly->mly_ccb_free);
421 SIMPLEQ_INIT(&mly->mly_ccb_queue);
422
423 /*
424 * Disable interrupts before we start talking to the controller.
425 */
426 mly_outb(mly, mly->mly_interrupt_mask, MLY_INTERRUPT_MASK_DISABLE);
427
428 /*
429 * Wait for the controller to come ready, handshaking with the
430 * firmware if required. This is typically only necessary on
431 * platforms where the controller BIOS does not run.
432 */
433 if (mly_fwhandshake(mly)) {
434 aprint_error_dev(&mly->mly_dv, "unable to bring controller online\n");
435 goto bad;
436 }
437
438 /*
439 * Allocate initial command buffers, obtain controller feature
440 * information, and then reallocate command buffers, since we'll
441 * know how many we want.
442 */
443 if (mly_alloc_ccbs(mly)) {
444 aprint_error_dev(&mly->mly_dv, "unable to allocate CCBs\n");
445 goto bad;
446 }
447 state++;
448 if (mly_get_controllerinfo(mly)) {
449 aprint_error_dev(&mly->mly_dv, "unable to retrieve controller info\n");
450 goto bad;
451 }
452 mly_release_ccbs(mly);
453 if (mly_alloc_ccbs(mly)) {
454 aprint_error_dev(&mly->mly_dv, "unable to allocate CCBs\n");
455 state--;
456 goto bad;
457 }
458
459 /*
460 * Get the current event counter for health purposes, populate the
461 * initial health status buffer.
462 */
463 if (mly_get_eventstatus(mly)) {
464 aprint_error_dev(&mly->mly_dv, "unable to retrieve event status\n");
465 goto bad;
466 }
467
468 /*
469 * Enable memory-mailbox mode.
470 */
471 if (mly_enable_mmbox(mly)) {
472 aprint_error_dev(&mly->mly_dv, "unable to enable memory mailbox\n");
473 goto bad;
474 }
475
476 /*
477 * Print a little information about the controller.
478 */
479 mi = mly->mly_controllerinfo;
480
481 printf("%s: %d physical channel%s, firmware %d.%02d-%d-%02d "
482 "(%02d%02d%02d%02d), %dMB RAM\n", device_xname(&mly->mly_dv),
483 mi->physical_channels_present,
484 (mi->physical_channels_present) > 1 ? "s" : "",
485 mi->fw_major, mi->fw_minor, mi->fw_turn, mi->fw_build,
486 mi->fw_century, mi->fw_year, mi->fw_month, mi->fw_day,
487 le16toh(mi->memory_size));
488
489 /*
490 * Register our `shutdownhook'.
491 */
492 if (mly_sdh == NULL)
493 shutdownhook_establish(mly_shutdown, NULL);
494
495 /*
496 * Clear any previous BTL information. For each bus that scsipi
497 * wants to scan, we'll receive the SCBUSIOLLSCAN ioctl and retrieve
498 * all BTL info at that point.
499 */
500 memset(&mly->mly_btl, 0, sizeof(mly->mly_btl));
501
502 mly->mly_nchans = mly->mly_controllerinfo->physical_channels_present +
503 mly->mly_controllerinfo->virtual_channels_present;
504
505 /*
506 * Attach to scsipi.
507 */
508 adapt = &mly->mly_adapt;
509 memset(adapt, 0, sizeof(*adapt));
510 adapt->adapt_dev = &mly->mly_dv;
511 adapt->adapt_nchannels = mly->mly_nchans;
512 adapt->adapt_openings = mly->mly_ncmds - MLY_CCBS_RESV;
513 adapt->adapt_max_periph = mly->mly_ncmds - MLY_CCBS_RESV;
514 adapt->adapt_request = mly_scsipi_request;
515 adapt->adapt_minphys = mly_scsipi_minphys;
516 adapt->adapt_ioctl = mly_scsipi_ioctl;
517
518 for (i = 0; i < mly->mly_nchans; i++) {
519 chan = &mly->mly_chans[i];
520 memset(chan, 0, sizeof(*chan));
521 chan->chan_adapter = adapt;
522 chan->chan_bustype = &scsi_bustype;
523 chan->chan_channel = i;
524 chan->chan_ntargets = MLY_MAX_TARGETS;
525 chan->chan_nluns = MLY_MAX_LUNS;
526 chan->chan_id = mly->mly_controllerparam->initiator_id;
527 chan->chan_flags = SCSIPI_CHAN_NOSETTLE;
528 config_found(&mly->mly_dv, chan, scsiprint);
529 }
530
531 /*
532 * Now enable interrupts...
533 */
534 mly_outb(mly, mly->mly_interrupt_mask, MLY_INTERRUPT_MASK_ENABLE);
535
536 /*
537 * Finally, create our monitoring thread.
538 */
539 mly->mly_state |= MLY_STATE_INITOK;
540 rv = kthread_create(PRI_NONE, 0, NULL, mly_thread, mly,
541 &mly->mly_thread, "%s", device_xname(&mly->mly_dv));
542 if (rv != 0)
543 aprint_error_dev(&mly->mly_dv, "unable to create thread (%d)\n",
544 rv);
545 maj = cdevsw_lookup_major(&mly_cdevsw);
546 device_register_name(makedev(maj, device_unit(self)), self, true,
547 DEV_OTHER, device_xname(self));
548
549 return;
550
551 bad:
552 if (state > 2)
553 mly_release_ccbs(mly);
554 if (state > 1)
555 mly_dmamem_free(mly, sizeof(struct mly_mmbox),
556 mly->mly_mmbox_dmamap, (void *)mly->mly_mmbox,
557 &mly->mly_mmbox_seg);
558 if (state > 0)
559 mly_dmamem_free(mly, MLY_SGL_SIZE * MLY_MAX_CCBS,
560 mly->mly_sg_dmamap, (void *)mly->mly_sg,
561 &mly->mly_sg_seg);
562 }
563
564 /*
565 * Scan all possible devices on the specified channel.
566 */
567 static void
568 mly_scan_channel(struct mly_softc *mly, int bus)
569 {
570 int s, target;
571
572 for (target = 0; target < MLY_MAX_TARGETS; target++) {
573 s = splbio();
574 if (!mly_scan_btl(mly, bus, target)) {
575 tsleep(&mly->mly_btl[bus][target], PRIBIO, "mlyscan",
576 0);
577 }
578 splx(s);
579 }
580 }
581
582 /*
583 * Shut down all configured `mly' devices.
584 */
585 static void
586 mly_shutdown(void *cookie)
587 {
588 struct mly_softc *mly;
589 int i;
590
591 for (i = 0; i < mly_cd.cd_ndevs; i++) {
592 if ((mly = device_lookup(&mly_cd, i)) == NULL)
593 continue;
594
595 if (mly_flush(mly))
596 aprint_error_dev(&mly->mly_dv, "unable to flush cache\n");
597 }
598 }
599
600 /*
601 * Fill in the mly_controllerinfo and mly_controllerparam fields in the
602 * softc.
603 */
604 static int
605 mly_get_controllerinfo(struct mly_softc *mly)
606 {
607 struct mly_cmd_ioctl mci;
608 int rv;
609
610 /*
611 * Build the getcontrollerinfo ioctl and send it.
612 */
613 memset(&mci, 0, sizeof(mci));
614 mci.sub_ioctl = MDACIOCTL_GETCONTROLLERINFO;
615 rv = mly_ioctl(mly, &mci, (void **)&mly->mly_controllerinfo,
616 sizeof(*mly->mly_controllerinfo), NULL, NULL);
617 if (rv != 0)
618 return (rv);
619
620 /*
621 * Build the getcontrollerparameter ioctl and send it.
622 */
623 memset(&mci, 0, sizeof(mci));
624 mci.sub_ioctl = MDACIOCTL_GETCONTROLLERPARAMETER;
625 rv = mly_ioctl(mly, &mci, (void **)&mly->mly_controllerparam,
626 sizeof(*mly->mly_controllerparam), NULL, NULL);
627
628 return (rv);
629 }
630
631 /*
632 * Rescan a device, possibly as a consequence of getting an event which
633 * suggests that it may have changed. Must be called with interrupts
634 * blocked.
635 */
636 static int
637 mly_scan_btl(struct mly_softc *mly, int bus, int target)
638 {
639 struct mly_ccb *mc;
640 struct mly_cmd_ioctl *mci;
641 int rv;
642
643 if (target == mly->mly_controllerparam->initiator_id) {
644 mly->mly_btl[bus][target].mb_flags = MLY_BTL_PROTECTED;
645 return (EIO);
646 }
647
648 /* Don't re-scan if a scan is already in progress. */
649 if ((mly->mly_btl[bus][target].mb_flags & MLY_BTL_SCANNING) != 0)
650 return (EBUSY);
651
652 /* Get a command. */
653 if ((rv = mly_ccb_alloc(mly, &mc)) != 0)
654 return (rv);
655
656 /* Set up the data buffer. */
657 mc->mc_data = malloc(sizeof(union mly_devinfo),
658 M_DEVBUF, M_NOWAIT|M_ZERO);
659
660 mc->mc_flags |= MLY_CCB_DATAIN;
661 mc->mc_complete = mly_complete_rescan;
662
663 /*
664 * Build the ioctl.
665 */
666 mci = (struct mly_cmd_ioctl *)&mc->mc_packet->ioctl;
667 mci->opcode = MDACMD_IOCTL;
668 mci->timeout = 30 | MLY_TIMEOUT_SECONDS;
669 memset(&mci->param, 0, sizeof(mci->param));
670
671 if (MLY_BUS_IS_VIRTUAL(mly, bus)) {
672 mc->mc_length = sizeof(struct mly_ioctl_getlogdevinfovalid);
673 mci->data_size = htole32(mc->mc_length);
674 mci->sub_ioctl = MDACIOCTL_GETLOGDEVINFOVALID;
675 _lto3l(MLY_LOGADDR(0, MLY_LOGDEV_ID(mly, bus, target)),
676 mci->addr);
677 } else {
678 mc->mc_length = sizeof(struct mly_ioctl_getphysdevinfovalid);
679 mci->data_size = htole32(mc->mc_length);
680 mci->sub_ioctl = MDACIOCTL_GETPHYSDEVINFOVALID;
681 _lto3l(MLY_PHYADDR(0, bus, target, 0), mci->addr);
682 }
683
684 /*
685 * Dispatch the command.
686 */
687 if ((rv = mly_ccb_map(mly, mc)) != 0) {
688 free(mc->mc_data, M_DEVBUF);
689 mly_ccb_free(mly, mc);
690 return(rv);
691 }
692
693 mly->mly_btl[bus][target].mb_flags |= MLY_BTL_SCANNING;
694 mly_ccb_enqueue(mly, mc);
695 return (0);
696 }
697
698 /*
699 * Handle the completion of a rescan operation.
700 */
701 static void
702 mly_complete_rescan(struct mly_softc *mly, struct mly_ccb *mc)
703 {
704 struct mly_ioctl_getlogdevinfovalid *ldi;
705 struct mly_ioctl_getphysdevinfovalid *pdi;
706 struct mly_cmd_ioctl *mci;
707 struct mly_btl btl, *btlp;
708 struct scsipi_xfer_mode xm;
709 int bus, target, rescan;
710 u_int tmp;
711
712 mly_ccb_unmap(mly, mc);
713
714 /*
715 * Recover the bus and target from the command. We need these even
716 * in the case where we don't have a useful response.
717 */
718 mci = (struct mly_cmd_ioctl *)&mc->mc_packet->ioctl;
719 tmp = _3ltol(mci->addr);
720 rescan = 0;
721
722 if (mci->sub_ioctl == MDACIOCTL_GETLOGDEVINFOVALID) {
723 bus = MLY_LOGDEV_BUS(mly, MLY_LOGADDR_DEV(tmp));
724 target = MLY_LOGDEV_TARGET(mly, MLY_LOGADDR_DEV(tmp));
725 } else {
726 bus = MLY_PHYADDR_CHANNEL(tmp);
727 target = MLY_PHYADDR_TARGET(tmp);
728 }
729
730 btlp = &mly->mly_btl[bus][target];
731
732 /* The default result is 'no device'. */
733 memset(&btl, 0, sizeof(btl));
734 btl.mb_flags = MLY_BTL_PROTECTED;
735
736 /* If the rescan completed OK, we have possibly-new BTL data. */
737 if (mc->mc_status != 0)
738 goto out;
739
740 if (mc->mc_length == sizeof(*ldi)) {
741 ldi = (struct mly_ioctl_getlogdevinfovalid *)mc->mc_data;
742 tmp = le32toh(ldi->logical_device_number);
743
744 if (MLY_LOGDEV_BUS(mly, tmp) != bus ||
745 MLY_LOGDEV_TARGET(mly, tmp) != target) {
746 #ifdef MLYDEBUG
747 printf("%s: WARNING: BTL rescan (logical) for %d:%d "
748 "returned data for %d:%d instead\n",
749 device_xname(&mly->mly_dv), bus, target,
750 MLY_LOGDEV_BUS(mly, tmp),
751 MLY_LOGDEV_TARGET(mly, tmp));
752 #endif
753 goto out;
754 }
755
756 btl.mb_flags = MLY_BTL_LOGICAL | MLY_BTL_TQING;
757 btl.mb_type = ldi->raid_level;
758 btl.mb_state = ldi->state;
759 } else if (mc->mc_length == sizeof(*pdi)) {
760 pdi = (struct mly_ioctl_getphysdevinfovalid *)mc->mc_data;
761
762 if (pdi->channel != bus || pdi->target != target) {
763 #ifdef MLYDEBUG
764 printf("%s: WARNING: BTL rescan (physical) for %d:%d "
765 " returned data for %d:%d instead\n",
766 device_xname(&mly->mly_dv),
767 bus, target, pdi->channel, pdi->target);
768 #endif
769 goto out;
770 }
771
772 btl.mb_flags = MLY_BTL_PHYSICAL;
773 btl.mb_type = MLY_DEVICE_TYPE_PHYSICAL;
774 btl.mb_state = pdi->state;
775 btl.mb_speed = pdi->speed;
776 btl.mb_width = pdi->width;
777
778 if (pdi->state != MLY_DEVICE_STATE_UNCONFIGURED)
779 btl.mb_flags |= MLY_BTL_PROTECTED;
780 if (pdi->command_tags != 0)
781 btl.mb_flags |= MLY_BTL_TQING;
782 } else {
783 printf("%s: BTL rescan result invalid\n", device_xname(&mly->mly_dv));
784 goto out;
785 }
786
787 /* Decide whether we need to rescan the device. */
788 if (btl.mb_flags != btlp->mb_flags ||
789 btl.mb_speed != btlp->mb_speed ||
790 btl.mb_width != btlp->mb_width)
791 rescan = 1;
792
793 out:
794 *btlp = btl;
795
796 if (rescan && (btl.mb_flags & MLY_BTL_PROTECTED) == 0) {
797 xm.xm_target = target;
798 mly_get_xfer_mode(mly, bus, &xm);
799 /* XXX SCSI mid-layer rescan goes here. */
800 }
801
802 /* Wake anybody waiting on the device to be rescanned. */
803 wakeup(btlp);
804
805 free(mc->mc_data, M_DEVBUF);
806 mly_ccb_free(mly, mc);
807 }
808
809 /*
810 * Get the current health status and set the 'next event' counter to suit.
811 */
812 static int
813 mly_get_eventstatus(struct mly_softc *mly)
814 {
815 struct mly_cmd_ioctl mci;
816 struct mly_health_status *mh;
817 int rv;
818
819 /* Build the gethealthstatus ioctl and send it. */
820 memset(&mci, 0, sizeof(mci));
821 mh = NULL;
822 mci.sub_ioctl = MDACIOCTL_GETHEALTHSTATUS;
823
824 rv = mly_ioctl(mly, &mci, (void *)&mh, sizeof(*mh), NULL, NULL);
825 if (rv)
826 return (rv);
827
828 /* Get the event counter. */
829 mly->mly_event_change = le32toh(mh->change_counter);
830 mly->mly_event_waiting = le32toh(mh->next_event);
831 mly->mly_event_counter = le32toh(mh->next_event);
832
833 /* Save the health status into the memory mailbox */
834 memcpy(&mly->mly_mmbox->mmm_health.status, mh, sizeof(*mh));
835
836 bus_dmamap_sync(mly->mly_dmat, mly->mly_mmbox_dmamap,
837 offsetof(struct mly_mmbox, mmm_health),
838 sizeof(mly->mly_mmbox->mmm_health),
839 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
840
841 free(mh, M_DEVBUF);
842 return (0);
843 }
844
845 /*
846 * Enable memory mailbox mode.
847 */
848 static int
849 mly_enable_mmbox(struct mly_softc *mly)
850 {
851 struct mly_cmd_ioctl mci;
852 u_int8_t *sp;
853 u_int64_t tmp;
854 int rv;
855
856 /* Build the ioctl and send it. */
857 memset(&mci, 0, sizeof(mci));
858 mci.sub_ioctl = MDACIOCTL_SETMEMORYMAILBOX;
859
860 /* Set buffer addresses. */
861 tmp = mly->mly_mmbox_busaddr + offsetof(struct mly_mmbox, mmm_command);
862 mci.param.setmemorymailbox.command_mailbox_physaddr = htole64(tmp);
863
864 tmp = mly->mly_mmbox_busaddr + offsetof(struct mly_mmbox, mmm_status);
865 mci.param.setmemorymailbox.status_mailbox_physaddr = htole64(tmp);
866
867 tmp = mly->mly_mmbox_busaddr + offsetof(struct mly_mmbox, mmm_health);
868 mci.param.setmemorymailbox.health_buffer_physaddr = htole64(tmp);
869
870 /* Set buffer sizes - abuse of data_size field is revolting. */
871 sp = (u_int8_t *)&mci.data_size;
872 sp[0] = (sizeof(union mly_cmd_packet) * MLY_MMBOX_COMMANDS) >> 10;
873 sp[1] = (sizeof(union mly_status_packet) * MLY_MMBOX_STATUS) >> 10;
874 mci.param.setmemorymailbox.health_buffer_size =
875 sizeof(union mly_health_region) >> 10;
876
877 rv = mly_ioctl(mly, &mci, NULL, 0, NULL, NULL);
878 if (rv)
879 return (rv);
880
881 mly->mly_state |= MLY_STATE_MMBOX_ACTIVE;
882 return (0);
883 }
884
885 /*
886 * Flush all pending I/O from the controller.
887 */
888 static int
889 mly_flush(struct mly_softc *mly)
890 {
891 struct mly_cmd_ioctl mci;
892
893 /* Build the ioctl */
894 memset(&mci, 0, sizeof(mci));
895 mci.sub_ioctl = MDACIOCTL_FLUSHDEVICEDATA;
896 mci.param.deviceoperation.operation_device =
897 MLY_OPDEVICE_PHYSICAL_CONTROLLER;
898
899 /* Pass it off to the controller */
900 return (mly_ioctl(mly, &mci, NULL, 0, NULL, NULL));
901 }
902
903 /*
904 * Perform an ioctl command.
905 *
906 * If (data) is not NULL, the command requires data transfer to the
907 * controller. If (*data) is NULL the command requires data transfer from
908 * the controller, and we will allocate a buffer for it.
909 */
910 static int
911 mly_ioctl(struct mly_softc *mly, struct mly_cmd_ioctl *ioctl, void **data,
912 size_t datasize, void *sense_buffer,
913 size_t *sense_length)
914 {
915 struct mly_ccb *mc;
916 struct mly_cmd_ioctl *mci;
917 u_int8_t status;
918 int rv;
919
920 mc = NULL;
921 if ((rv = mly_ccb_alloc(mly, &mc)) != 0)
922 goto bad;
923
924 /*
925 * Copy the ioctl structure, but save some important fields and then
926 * fixup.
927 */
928 mci = &mc->mc_packet->ioctl;
929 ioctl->sense_buffer_address = htole64(mci->sense_buffer_address);
930 ioctl->maximum_sense_size = mci->maximum_sense_size;
931 *mci = *ioctl;
932 mci->opcode = MDACMD_IOCTL;
933 mci->timeout = 30 | MLY_TIMEOUT_SECONDS;
934
935 /* Handle the data buffer. */
936 if (data != NULL) {
937 if (*data == NULL) {
938 /* Allocate data buffer */
939 mc->mc_data = malloc(datasize, M_DEVBUF, M_NOWAIT);
940 mc->mc_flags |= MLY_CCB_DATAIN;
941 } else {
942 mc->mc_data = *data;
943 mc->mc_flags |= MLY_CCB_DATAOUT;
944 }
945 mc->mc_length = datasize;
946 mc->mc_packet->generic.data_size = htole32(datasize);
947 }
948
949 /* Run the command. */
950 if (datasize > 0)
951 if ((rv = mly_ccb_map(mly, mc)) != 0)
952 goto bad;
953 rv = mly_ccb_poll(mly, mc, 30000);
954 if (datasize > 0)
955 mly_ccb_unmap(mly, mc);
956 if (rv != 0)
957 goto bad;
958
959 /* Clean up and return any data. */
960 status = mc->mc_status;
961
962 if (status != 0)
963 printf("mly_ioctl: command status %d\n", status);
964
965 if (mc->mc_sense > 0 && sense_buffer != NULL) {
966 memcpy(sense_buffer, mc->mc_packet, mc->mc_sense);
967 *sense_length = mc->mc_sense;
968 goto bad;
969 }
970
971 /* Should we return a data pointer? */
972 if (data != NULL && *data == NULL)
973 *data = mc->mc_data;
974
975 /* Command completed OK. */
976 rv = (status != 0 ? EIO : 0);
977
978 bad:
979 if (mc != NULL) {
980 /* Do we need to free a data buffer we allocated? */
981 if (rv != 0 && mc->mc_data != NULL &&
982 (data == NULL || *data == NULL))
983 free(mc->mc_data, M_DEVBUF);
984 mly_ccb_free(mly, mc);
985 }
986
987 return (rv);
988 }
989
990 /*
991 * Check for event(s) outstanding in the controller.
992 */
993 static void
994 mly_check_event(struct mly_softc *mly)
995 {
996
997 bus_dmamap_sync(mly->mly_dmat, mly->mly_mmbox_dmamap,
998 offsetof(struct mly_mmbox, mmm_health),
999 sizeof(mly->mly_mmbox->mmm_health),
1000 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1001
1002 /*
1003 * The controller may have updated the health status information, so
1004 * check for it here. Note that the counters are all in host
1005 * memory, so this check is very cheap. Also note that we depend on
1006 * checking on completion
1007 */
1008 if (le32toh(mly->mly_mmbox->mmm_health.status.change_counter) !=
1009 mly->mly_event_change) {
1010 mly->mly_event_change =
1011 le32toh(mly->mly_mmbox->mmm_health.status.change_counter);
1012 mly->mly_event_waiting =
1013 le32toh(mly->mly_mmbox->mmm_health.status.next_event);
1014
1015 /* Wake up anyone that might be interested in this. */
1016 wakeup(&mly->mly_event_change);
1017 }
1018
1019 bus_dmamap_sync(mly->mly_dmat, mly->mly_mmbox_dmamap,
1020 offsetof(struct mly_mmbox, mmm_health),
1021 sizeof(mly->mly_mmbox->mmm_health),
1022 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
1023
1024 if (mly->mly_event_counter != mly->mly_event_waiting)
1025 mly_fetch_event(mly);
1026 }
1027
1028 /*
1029 * Fetch one event from the controller. If we fail due to resource
1030 * starvation, we'll be retried the next time a command completes.
1031 */
1032 static void
1033 mly_fetch_event(struct mly_softc *mly)
1034 {
1035 struct mly_ccb *mc;
1036 struct mly_cmd_ioctl *mci;
1037 int s;
1038 u_int32_t event;
1039
1040 /* Get a command. */
1041 if (mly_ccb_alloc(mly, &mc))
1042 return;
1043
1044 /* Set up the data buffer. */
1045 mc->mc_data = malloc(sizeof(struct mly_event), M_DEVBUF,
1046 M_NOWAIT|M_ZERO);
1047
1048 mc->mc_length = sizeof(struct mly_event);
1049 mc->mc_flags |= MLY_CCB_DATAIN;
1050 mc->mc_complete = mly_complete_event;
1051
1052 /*
1053 * Get an event number to fetch. It's possible that we've raced
1054 * with another context for the last event, in which case there will
1055 * be no more events.
1056 */
1057 s = splbio();
1058 if (mly->mly_event_counter == mly->mly_event_waiting) {
1059 splx(s);
1060 free(mc->mc_data, M_DEVBUF);
1061 mly_ccb_free(mly, mc);
1062 return;
1063 }
1064 event = mly->mly_event_counter++;
1065 splx(s);
1066
1067 /*
1068 * Build the ioctl.
1069 *
1070 * At this point we are committed to sending this request, as it
1071 * will be the only one constructed for this particular event
1072 * number.
1073 */
1074 mci = (struct mly_cmd_ioctl *)&mc->mc_packet->ioctl;
1075 mci->opcode = MDACMD_IOCTL;
1076 mci->data_size = htole32(sizeof(struct mly_event));
1077 _lto3l(MLY_PHYADDR(0, 0, (event >> 16) & 0xff, (event >> 24) & 0xff),
1078 mci->addr);
1079 mci->timeout = 30 | MLY_TIMEOUT_SECONDS;
1080 mci->sub_ioctl = MDACIOCTL_GETEVENT;
1081 mci->param.getevent.sequence_number_low = htole16(event & 0xffff);
1082
1083 /*
1084 * Submit the command.
1085 */
1086 if (mly_ccb_map(mly, mc) != 0)
1087 goto bad;
1088 mly_ccb_enqueue(mly, mc);
1089 return;
1090
1091 bad:
1092 printf("%s: couldn't fetch event %u\n", device_xname(&mly->mly_dv), event);
1093 free(mc->mc_data, M_DEVBUF);
1094 mly_ccb_free(mly, mc);
1095 }
1096
1097 /*
1098 * Handle the completion of an event poll.
1099 */
1100 static void
1101 mly_complete_event(struct mly_softc *mly, struct mly_ccb *mc)
1102 {
1103 struct mly_event *me;
1104
1105 me = (struct mly_event *)mc->mc_data;
1106 mly_ccb_unmap(mly, mc);
1107 mly_ccb_free(mly, mc);
1108
1109 /* If the event was successfully fetched, process it. */
1110 if (mc->mc_status == SCSI_OK)
1111 mly_process_event(mly, me);
1112 else
1113 aprint_error_dev(&mly->mly_dv, "unable to fetch event; status = 0x%x\n",
1114 mc->mc_status);
1115
1116 free(me, M_DEVBUF);
1117
1118 /* Check for another event. */
1119 mly_check_event(mly);
1120 }
1121
1122 /*
1123 * Process a controller event. Called with interrupts blocked (i.e., at
1124 * interrupt time).
1125 */
1126 static void
1127 mly_process_event(struct mly_softc *mly, struct mly_event *me)
1128 {
1129 struct scsi_sense_data *ssd;
1130 int bus, target, event, class, action;
1131 const char *fp, *tp;
1132
1133 ssd = (struct scsi_sense_data *)&me->sense[0];
1134
1135 /*
1136 * Errors can be reported using vendor-unique sense data. In this
1137 * case, the event code will be 0x1c (Request sense data present),
1138 * the sense key will be 0x09 (vendor specific), the MSB of the ASC
1139 * will be set, and the actual event code will be a 16-bit value
1140 * comprised of the ASCQ (low byte) and low seven bits of the ASC
1141 * (low seven bits of the high byte).
1142 */
1143 if (le32toh(me->code) == 0x1c &&
1144 SSD_SENSE_KEY(ssd->flags) == SKEY_VENDOR_SPECIFIC &&
1145 (ssd->asc & 0x80) != 0) {
1146 event = ((int)(ssd->asc & ~0x80) << 8) +
1147 ssd->ascq;
1148 } else
1149 event = le32toh(me->code);
1150
1151 /* Look up event, get codes. */
1152 fp = mly_describe_code(mly_table_event, event);
1153
1154 /* Quiet event? */
1155 class = fp[0];
1156 #ifdef notyet
1157 if (isupper(class) && bootverbose)
1158 class = tolower(class);
1159 #endif
1160
1161 /* Get action code, text string. */
1162 action = fp[1];
1163 tp = fp + 3;
1164
1165 /*
1166 * Print some information about the event.
1167 *
1168 * This code uses a table derived from the corresponding portion of
1169 * the Linux driver, and thus the parser is very similar.
1170 */
1171 switch (class) {
1172 case 'p':
1173 /*
1174 * Error on physical drive.
1175 */
1176 printf("%s: physical device %d:%d %s\n", device_xname(&mly->mly_dv),
1177 me->channel, me->target, tp);
1178 if (action == 'r')
1179 mly->mly_btl[me->channel][me->target].mb_flags |=
1180 MLY_BTL_RESCAN;
1181 break;
1182
1183 case 'l':
1184 case 'm':
1185 /*
1186 * Error on logical unit, or message about logical unit.
1187 */
1188 bus = MLY_LOGDEV_BUS(mly, me->lun);
1189 target = MLY_LOGDEV_TARGET(mly, me->lun);
1190 printf("%s: logical device %d:%d %s\n", device_xname(&mly->mly_dv),
1191 bus, target, tp);
1192 if (action == 'r')
1193 mly->mly_btl[bus][target].mb_flags |= MLY_BTL_RESCAN;
1194 break;
1195
1196 case 's':
1197 /*
1198 * Report of sense data.
1199 */
1200 if ((SSD_SENSE_KEY(ssd->flags) == SKEY_NO_SENSE ||
1201 SSD_SENSE_KEY(ssd->flags) == SKEY_NOT_READY) &&
1202 ssd->asc == 0x04 &&
1203 (ssd->ascq == 0x01 ||
1204 ssd->ascq == 0x02)) {
1205 /* Ignore NO_SENSE or NOT_READY in one case */
1206 break;
1207 }
1208
1209 /*
1210 * XXX Should translate this if SCSIVERBOSE.
1211 */
1212 printf("%s: physical device %d:%d %s\n", device_xname(&mly->mly_dv),
1213 me->channel, me->target, tp);
1214 printf("%s: sense key %d asc %02x ascq %02x\n",
1215 device_xname(&mly->mly_dv), SSD_SENSE_KEY(ssd->flags),
1216 ssd->asc, ssd->ascq);
1217 printf("%s: info %x%x%x%x csi %x%x%x%x\n",
1218 device_xname(&mly->mly_dv), ssd->info[0], ssd->info[1],
1219 ssd->info[2], ssd->info[3], ssd->csi[0],
1220 ssd->csi[1], ssd->csi[2],
1221 ssd->csi[3]);
1222 if (action == 'r')
1223 mly->mly_btl[me->channel][me->target].mb_flags |=
1224 MLY_BTL_RESCAN;
1225 break;
1226
1227 case 'e':
1228 printf("%s: ", device_xname(&mly->mly_dv));
1229 printf(tp, me->target, me->lun);
1230 break;
1231
1232 case 'c':
1233 printf("%s: controller %s\n", device_xname(&mly->mly_dv), tp);
1234 break;
1235
1236 case '?':
1237 printf("%s: %s - %d\n", device_xname(&mly->mly_dv), tp, event);
1238 break;
1239
1240 default:
1241 /* Probably a 'noisy' event being ignored. */
1242 break;
1243 }
1244 }
1245
1246 /*
1247 * Perform periodic activities.
1248 */
1249 static void
1250 mly_thread(void *cookie)
1251 {
1252 struct mly_softc *mly;
1253 struct mly_btl *btl;
1254 int s, bus, target, done;
1255
1256 mly = (struct mly_softc *)cookie;
1257
1258 for (;;) {
1259 /* Check for new events. */
1260 mly_check_event(mly);
1261
1262 /* Re-scan up to 1 device. */
1263 s = splbio();
1264 done = 0;
1265 for (bus = 0; bus < mly->mly_nchans && !done; bus++) {
1266 for (target = 0; target < MLY_MAX_TARGETS; target++) {
1267 /* Perform device rescan? */
1268 btl = &mly->mly_btl[bus][target];
1269 if ((btl->mb_flags & MLY_BTL_RESCAN) != 0) {
1270 btl->mb_flags ^= MLY_BTL_RESCAN;
1271 mly_scan_btl(mly, bus, target);
1272 done = 1;
1273 break;
1274 }
1275 }
1276 }
1277 splx(s);
1278
1279 /* Sleep for N seconds. */
1280 tsleep(mly_thread, PWAIT, "mlyzzz",
1281 hz * MLY_PERIODIC_INTERVAL);
1282 }
1283 }
1284
1285 /*
1286 * Submit a command to the controller and poll on completion. Return
1287 * non-zero on timeout.
1288 */
1289 static int
1290 mly_ccb_poll(struct mly_softc *mly, struct mly_ccb *mc, int timo)
1291 {
1292 int rv;
1293
1294 if ((rv = mly_ccb_submit(mly, mc)) != 0)
1295 return (rv);
1296
1297 for (timo *= 10; timo != 0; timo--) {
1298 if ((mc->mc_flags & MLY_CCB_COMPLETE) != 0)
1299 break;
1300 mly_intr(mly);
1301 DELAY(100);
1302 }
1303
1304 return (timo == 0);
1305 }
1306
1307 /*
1308 * Submit a command to the controller and sleep on completion. Return
1309 * non-zero on timeout.
1310 */
1311 static int
1312 mly_ccb_wait(struct mly_softc *mly, struct mly_ccb *mc, int timo)
1313 {
1314 int rv, s;
1315
1316 mly_ccb_enqueue(mly, mc);
1317
1318 s = splbio();
1319 if ((mc->mc_flags & MLY_CCB_COMPLETE) != 0) {
1320 splx(s);
1321 return (0);
1322 }
1323 rv = tsleep(mc, PRIBIO, "mlywccb", timo * hz / 1000);
1324 splx(s);
1325
1326 return (rv);
1327 }
1328
1329 /*
1330 * If a CCB is specified, enqueue it. Pull CCBs off the software queue in
1331 * the order that they were enqueued and try to submit their command blocks
1332 * to the controller for execution.
1333 */
1334 void
1335 mly_ccb_enqueue(struct mly_softc *mly, struct mly_ccb *mc)
1336 {
1337 int s;
1338
1339 s = splbio();
1340
1341 if (mc != NULL)
1342 SIMPLEQ_INSERT_TAIL(&mly->mly_ccb_queue, mc, mc_link.simpleq);
1343
1344 while ((mc = SIMPLEQ_FIRST(&mly->mly_ccb_queue)) != NULL) {
1345 if (mly_ccb_submit(mly, mc))
1346 break;
1347 SIMPLEQ_REMOVE_HEAD(&mly->mly_ccb_queue, mc_link.simpleq);
1348 }
1349
1350 splx(s);
1351 }
1352
1353 /*
1354 * Deliver a command to the controller.
1355 */
1356 static int
1357 mly_ccb_submit(struct mly_softc *mly, struct mly_ccb *mc)
1358 {
1359 union mly_cmd_packet *pkt;
1360 int s, off;
1361
1362 mc->mc_packet->generic.command_id = htole16(mc->mc_slot);
1363
1364 bus_dmamap_sync(mly->mly_dmat, mly->mly_pkt_dmamap,
1365 mc->mc_packetphys - mly->mly_pkt_busaddr,
1366 sizeof(union mly_cmd_packet),
1367 BUS_DMASYNC_PREREAD | BUS_DMASYNC_PREWRITE);
1368
1369 s = splbio();
1370
1371 /*
1372 * Do we have to use the hardware mailbox?
1373 */
1374 if ((mly->mly_state & MLY_STATE_MMBOX_ACTIVE) == 0) {
1375 /*
1376 * Check to see if the controller is ready for us.
1377 */
1378 if (mly_idbr_true(mly, MLY_HM_CMDSENT)) {
1379 splx(s);
1380 return (EBUSY);
1381 }
1382
1383 /*
1384 * It's ready, send the command.
1385 */
1386 mly_outl(mly, mly->mly_cmd_mailbox,
1387 (u_int64_t)mc->mc_packetphys & 0xffffffff);
1388 mly_outl(mly, mly->mly_cmd_mailbox + 4,
1389 (u_int64_t)mc->mc_packetphys >> 32);
1390 mly_outb(mly, mly->mly_idbr, MLY_HM_CMDSENT);
1391 } else {
1392 pkt = &mly->mly_mmbox->mmm_command[mly->mly_mmbox_cmd_idx];
1393 off = (char *)pkt - (char *)mly->mly_mmbox;
1394
1395 bus_dmamap_sync(mly->mly_dmat, mly->mly_mmbox_dmamap,
1396 off, sizeof(mly->mly_mmbox->mmm_command[0]),
1397 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1398
1399 /* Check to see if the next index is free yet. */
1400 if (pkt->mmbox.flag != 0) {
1401 splx(s);
1402 return (EBUSY);
1403 }
1404
1405 /* Copy in new command */
1406 memcpy(pkt->mmbox.data, mc->mc_packet->mmbox.data,
1407 sizeof(pkt->mmbox.data));
1408
1409 /* Copy flag last. */
1410 pkt->mmbox.flag = mc->mc_packet->mmbox.flag;
1411
1412 bus_dmamap_sync(mly->mly_dmat, mly->mly_mmbox_dmamap,
1413 off, sizeof(mly->mly_mmbox->mmm_command[0]),
1414 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
1415
1416 /* Signal controller and update index. */
1417 mly_outb(mly, mly->mly_idbr, MLY_AM_CMDSENT);
1418 mly->mly_mmbox_cmd_idx =
1419 (mly->mly_mmbox_cmd_idx + 1) % MLY_MMBOX_COMMANDS;
1420 }
1421
1422 splx(s);
1423 return (0);
1424 }
1425
1426 /*
1427 * Pick up completed commands from the controller and handle accordingly.
1428 */
1429 int
1430 mly_intr(void *cookie)
1431 {
1432 struct mly_ccb *mc;
1433 union mly_status_packet *sp;
1434 u_int16_t slot;
1435 int forus, off;
1436 struct mly_softc *mly;
1437
1438 mly = cookie;
1439 forus = 0;
1440
1441 /*
1442 * Pick up hardware-mailbox commands.
1443 */
1444 if (mly_odbr_true(mly, MLY_HM_STSREADY)) {
1445 slot = mly_inw(mly, mly->mly_status_mailbox);
1446
1447 if (slot < MLY_SLOT_MAX) {
1448 mc = mly->mly_ccbs + (slot - MLY_SLOT_START);
1449 mc->mc_status =
1450 mly_inb(mly, mly->mly_status_mailbox + 2);
1451 mc->mc_sense =
1452 mly_inb(mly, mly->mly_status_mailbox + 3);
1453 mc->mc_resid =
1454 mly_inl(mly, mly->mly_status_mailbox + 4);
1455
1456 mly_ccb_complete(mly, mc);
1457 } else {
1458 /* Slot 0xffff may mean "extremely bogus command". */
1459 printf("%s: got HM completion for illegal slot %u\n",
1460 device_xname(&mly->mly_dv), slot);
1461 }
1462
1463 /* Unconditionally acknowledge status. */
1464 mly_outb(mly, mly->mly_odbr, MLY_HM_STSREADY);
1465 mly_outb(mly, mly->mly_idbr, MLY_HM_STSACK);
1466 forus = 1;
1467 }
1468
1469 /*
1470 * Pick up memory-mailbox commands.
1471 */
1472 if (mly_odbr_true(mly, MLY_AM_STSREADY)) {
1473 for (;;) {
1474 sp = &mly->mly_mmbox->mmm_status[mly->mly_mmbox_sts_idx];
1475 off = (char *)sp - (char *)mly->mly_mmbox;
1476
1477 bus_dmamap_sync(mly->mly_dmat, mly->mly_mmbox_dmamap,
1478 off, sizeof(mly->mly_mmbox->mmm_command[0]),
1479 BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
1480
1481 /* Check for more status. */
1482 if (sp->mmbox.flag == 0)
1483 break;
1484
1485 /* Get slot number. */
1486 slot = le16toh(sp->status.command_id);
1487 if (slot < MLY_SLOT_MAX) {
1488 mc = mly->mly_ccbs + (slot - MLY_SLOT_START);
1489 mc->mc_status = sp->status.status;
1490 mc->mc_sense = sp->status.sense_length;
1491 mc->mc_resid = le32toh(sp->status.residue);
1492 mly_ccb_complete(mly, mc);
1493 } else {
1494 /*
1495 * Slot 0xffff may mean "extremely bogus
1496 * command".
1497 */
1498 printf("%s: got AM completion for illegal "
1499 "slot %u at %d\n", device_xname(&mly->mly_dv),
1500 slot, mly->mly_mmbox_sts_idx);
1501 }
1502
1503 /* Clear and move to next index. */
1504 sp->mmbox.flag = 0;
1505 mly->mly_mmbox_sts_idx =
1506 (mly->mly_mmbox_sts_idx + 1) % MLY_MMBOX_STATUS;
1507 }
1508
1509 /* Acknowledge that we have collected status value(s). */
1510 mly_outb(mly, mly->mly_odbr, MLY_AM_STSREADY);
1511 forus = 1;
1512 }
1513
1514 /*
1515 * Run the queue.
1516 */
1517 if (forus && ! SIMPLEQ_EMPTY(&mly->mly_ccb_queue))
1518 mly_ccb_enqueue(mly, NULL);
1519
1520 return (forus);
1521 }
1522
1523 /*
1524 * Process completed commands
1525 */
1526 static void
1527 mly_ccb_complete(struct mly_softc *mly, struct mly_ccb *mc)
1528 {
1529 void (*complete)(struct mly_softc *, struct mly_ccb *);
1530
1531 bus_dmamap_sync(mly->mly_dmat, mly->mly_pkt_dmamap,
1532 mc->mc_packetphys - mly->mly_pkt_busaddr,
1533 sizeof(union mly_cmd_packet),
1534 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
1535
1536 complete = mc->mc_complete;
1537 mc->mc_flags |= MLY_CCB_COMPLETE;
1538
1539 /*
1540 * Call completion handler or wake up sleeping consumer.
1541 */
1542 if (complete != NULL)
1543 (*complete)(mly, mc);
1544 else
1545 wakeup(mc);
1546 }
1547
1548 /*
1549 * Allocate a command.
1550 */
1551 int
1552 mly_ccb_alloc(struct mly_softc *mly, struct mly_ccb **mcp)
1553 {
1554 struct mly_ccb *mc;
1555 int s;
1556
1557 s = splbio();
1558 mc = SLIST_FIRST(&mly->mly_ccb_free);
1559 if (mc != NULL)
1560 SLIST_REMOVE_HEAD(&mly->mly_ccb_free, mc_link.slist);
1561 splx(s);
1562
1563 *mcp = mc;
1564 return (mc == NULL ? EAGAIN : 0);
1565 }
1566
1567 /*
1568 * Release a command back to the freelist.
1569 */
1570 void
1571 mly_ccb_free(struct mly_softc *mly, struct mly_ccb *mc)
1572 {
1573 int s;
1574
1575 /*
1576 * Fill in parts of the command that may cause confusion if a
1577 * consumer doesn't when we are later allocated.
1578 */
1579 mc->mc_data = NULL;
1580 mc->mc_flags = 0;
1581 mc->mc_complete = NULL;
1582 mc->mc_private = NULL;
1583 mc->mc_packet->generic.command_control = 0;
1584
1585 /*
1586 * By default, we set up to overwrite the command packet with sense
1587 * information.
1588 */
1589 mc->mc_packet->generic.sense_buffer_address =
1590 htole64(mc->mc_packetphys);
1591 mc->mc_packet->generic.maximum_sense_size =
1592 sizeof(union mly_cmd_packet);
1593
1594 s = splbio();
1595 SLIST_INSERT_HEAD(&mly->mly_ccb_free, mc, mc_link.slist);
1596 splx(s);
1597 }
1598
1599 /*
1600 * Allocate and initialize command and packet structures.
1601 *
1602 * If the controller supports fewer than MLY_MAX_CCBS commands, limit our
1603 * allocation to that number. If we don't yet know how many commands the
1604 * controller supports, allocate a very small set (suitable for initialization
1605 * purposes only).
1606 */
1607 static int
1608 mly_alloc_ccbs(struct mly_softc *mly)
1609 {
1610 struct mly_ccb *mc;
1611 int i, rv;
1612
1613 if (mly->mly_controllerinfo == NULL)
1614 mly->mly_ncmds = MLY_CCBS_RESV;
1615 else {
1616 i = le16toh(mly->mly_controllerinfo->maximum_parallel_commands);
1617 mly->mly_ncmds = min(MLY_MAX_CCBS, i);
1618 }
1619
1620 /*
1621 * Allocate enough space for all the command packets in one chunk
1622 * and map them permanently into controller-visible space.
1623 */
1624 rv = mly_dmamem_alloc(mly,
1625 mly->mly_ncmds * sizeof(union mly_cmd_packet),
1626 &mly->mly_pkt_dmamap, (void **)&mly->mly_pkt,
1627 &mly->mly_pkt_busaddr, &mly->mly_pkt_seg);
1628 if (rv)
1629 return (rv);
1630
1631 mly->mly_ccbs = malloc(sizeof(struct mly_ccb) * mly->mly_ncmds,
1632 M_DEVBUF, M_NOWAIT|M_ZERO);
1633
1634 for (i = 0; i < mly->mly_ncmds; i++) {
1635 mc = mly->mly_ccbs + i;
1636 mc->mc_slot = MLY_SLOT_START + i;
1637 mc->mc_packet = mly->mly_pkt + i;
1638 mc->mc_packetphys = mly->mly_pkt_busaddr +
1639 (i * sizeof(union mly_cmd_packet));
1640
1641 rv = bus_dmamap_create(mly->mly_dmat, MLY_MAX_XFER,
1642 MLY_MAX_SEGS, MLY_MAX_XFER, 0,
1643 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
1644 &mc->mc_datamap);
1645 if (rv) {
1646 mly_release_ccbs(mly);
1647 return (rv);
1648 }
1649
1650 mly_ccb_free(mly, mc);
1651 }
1652
1653 return (0);
1654 }
1655
1656 /*
1657 * Free all the storage held by commands.
1658 *
1659 * Must be called with all commands on the free list.
1660 */
1661 static void
1662 mly_release_ccbs(struct mly_softc *mly)
1663 {
1664 struct mly_ccb *mc;
1665
1666 /* Throw away command buffer DMA maps. */
1667 while (mly_ccb_alloc(mly, &mc) == 0)
1668 bus_dmamap_destroy(mly->mly_dmat, mc->mc_datamap);
1669
1670 /* Release CCB storage. */
1671 free(mly->mly_ccbs, M_DEVBUF);
1672
1673 /* Release the packet storage. */
1674 mly_dmamem_free(mly, mly->mly_ncmds * sizeof(union mly_cmd_packet),
1675 mly->mly_pkt_dmamap, (void *)mly->mly_pkt, &mly->mly_pkt_seg);
1676 }
1677
1678 /*
1679 * Map a command into controller-visible space.
1680 */
1681 static int
1682 mly_ccb_map(struct mly_softc *mly, struct mly_ccb *mc)
1683 {
1684 struct mly_cmd_generic *gen;
1685 struct mly_sg_entry *sg;
1686 bus_dma_segment_t *ds;
1687 int flg, nseg, rv;
1688
1689 #ifdef DIAGNOSTIC
1690 /* Don't map more than once. */
1691 if ((mc->mc_flags & MLY_CCB_MAPPED) != 0)
1692 panic("mly_ccb_map: already mapped");
1693 mc->mc_flags |= MLY_CCB_MAPPED;
1694
1695 /* Does the command have a data buffer? */
1696 if (mc->mc_data == NULL)
1697 panic("mly_ccb_map: no data buffer");
1698 #endif
1699
1700 rv = bus_dmamap_load(mly->mly_dmat, mc->mc_datamap, mc->mc_data,
1701 mc->mc_length, NULL, BUS_DMA_NOWAIT | BUS_DMA_STREAMING |
1702 ((mc->mc_flags & MLY_CCB_DATAIN) != 0 ?
1703 BUS_DMA_READ : BUS_DMA_WRITE));
1704 if (rv != 0)
1705 return (rv);
1706
1707 gen = &mc->mc_packet->generic;
1708
1709 /*
1710 * Can we use the transfer structure directly?
1711 */
1712 if ((nseg = mc->mc_datamap->dm_nsegs) <= 2) {
1713 mc->mc_sgoff = -1;
1714 sg = &gen->transfer.direct.sg[0];
1715 } else {
1716 mc->mc_sgoff = (mc->mc_slot - MLY_SLOT_START) *
1717 MLY_MAX_SEGS;
1718 sg = mly->mly_sg + mc->mc_sgoff;
1719 gen->command_control |= MLY_CMDCTL_EXTENDED_SG_TABLE;
1720 gen->transfer.indirect.entries[0] = htole16(nseg);
1721 gen->transfer.indirect.table_physaddr[0] =
1722 htole64(mly->mly_sg_busaddr +
1723 (mc->mc_sgoff * sizeof(struct mly_sg_entry)));
1724 }
1725
1726 /*
1727 * Fill the S/G table.
1728 */
1729 for (ds = mc->mc_datamap->dm_segs; nseg != 0; nseg--, sg++, ds++) {
1730 sg->physaddr = htole64(ds->ds_addr);
1731 sg->length = htole64(ds->ds_len);
1732 }
1733
1734 /*
1735 * Sync up the data map.
1736 */
1737 if ((mc->mc_flags & MLY_CCB_DATAIN) != 0)
1738 flg = BUS_DMASYNC_PREREAD;
1739 else /* if ((mc->mc_flags & MLY_CCB_DATAOUT) != 0) */ {
1740 gen->command_control |= MLY_CMDCTL_DATA_DIRECTION;
1741 flg = BUS_DMASYNC_PREWRITE;
1742 }
1743
1744 bus_dmamap_sync(mly->mly_dmat, mc->mc_datamap, 0, mc->mc_length, flg);
1745
1746 /*
1747 * Sync up the chained S/G table, if we're using one.
1748 */
1749 if (mc->mc_sgoff == -1)
1750 return (0);
1751
1752 bus_dmamap_sync(mly->mly_dmat, mly->mly_sg_dmamap, mc->mc_sgoff,
1753 MLY_SGL_SIZE, BUS_DMASYNC_PREWRITE);
1754
1755 return (0);
1756 }
1757
1758 /*
1759 * Unmap a command from controller-visible space.
1760 */
1761 static void
1762 mly_ccb_unmap(struct mly_softc *mly, struct mly_ccb *mc)
1763 {
1764 int flg;
1765
1766 #ifdef DIAGNOSTIC
1767 if ((mc->mc_flags & MLY_CCB_MAPPED) == 0)
1768 panic("mly_ccb_unmap: not mapped");
1769 mc->mc_flags &= ~MLY_CCB_MAPPED;
1770 #endif
1771
1772 if ((mc->mc_flags & MLY_CCB_DATAIN) != 0)
1773 flg = BUS_DMASYNC_POSTREAD;
1774 else /* if ((mc->mc_flags & MLY_CCB_DATAOUT) != 0) */
1775 flg = BUS_DMASYNC_POSTWRITE;
1776
1777 bus_dmamap_sync(mly->mly_dmat, mc->mc_datamap, 0, mc->mc_length, flg);
1778 bus_dmamap_unload(mly->mly_dmat, mc->mc_datamap);
1779
1780 if (mc->mc_sgoff == -1)
1781 return;
1782
1783 bus_dmamap_sync(mly->mly_dmat, mly->mly_sg_dmamap, mc->mc_sgoff,
1784 MLY_SGL_SIZE, BUS_DMASYNC_POSTWRITE);
1785 }
1786
1787 /*
1788 * Adjust the size of each I/O before it passes to the SCSI layer.
1789 */
1790 static void
1791 mly_scsipi_minphys(struct buf *bp)
1792 {
1793
1794 if (bp->b_bcount > MLY_MAX_XFER)
1795 bp->b_bcount = MLY_MAX_XFER;
1796 minphys(bp);
1797 }
1798
1799 /*
1800 * Start a SCSI command.
1801 */
1802 static void
1803 mly_scsipi_request(struct scsipi_channel *chan, scsipi_adapter_req_t req,
1804 void *arg)
1805 {
1806 struct mly_ccb *mc;
1807 struct mly_cmd_scsi_small *ss;
1808 struct scsipi_xfer *xs;
1809 struct scsipi_periph *periph;
1810 struct mly_softc *mly;
1811 struct mly_btl *btl;
1812 int s, tmp;
1813
1814 mly = (void *)chan->chan_adapter->adapt_dev;
1815
1816 switch (req) {
1817 case ADAPTER_REQ_RUN_XFER:
1818 xs = arg;
1819 periph = xs->xs_periph;
1820 btl = &mly->mly_btl[chan->chan_channel][periph->periph_target];
1821 s = splbio();
1822 tmp = btl->mb_flags;
1823 splx(s);
1824
1825 /*
1826 * Check for I/O attempt to a protected or non-existant
1827 * device.
1828 */
1829 if ((tmp & MLY_BTL_PROTECTED) != 0) {
1830 xs->error = XS_SELTIMEOUT;
1831 scsipi_done(xs);
1832 break;
1833 }
1834
1835 #ifdef DIAGNOSTIC
1836 /* XXX Increase if/when we support large SCSI commands. */
1837 if (xs->cmdlen > MLY_CMD_SCSI_SMALL_CDB) {
1838 printf("%s: cmd too large\n", device_xname(&mly->mly_dv));
1839 xs->error = XS_DRIVER_STUFFUP;
1840 scsipi_done(xs);
1841 break;
1842 }
1843 #endif
1844
1845 if (mly_ccb_alloc(mly, &mc)) {
1846 xs->error = XS_RESOURCE_SHORTAGE;
1847 scsipi_done(xs);
1848 break;
1849 }
1850
1851 /* Build the command. */
1852 mc->mc_data = xs->data;
1853 mc->mc_length = xs->datalen;
1854 mc->mc_complete = mly_scsipi_complete;
1855 mc->mc_private = xs;
1856
1857 /* Build the packet for the controller. */
1858 ss = &mc->mc_packet->scsi_small;
1859 ss->opcode = MDACMD_SCSI;
1860 #ifdef notdef
1861 /*
1862 * XXX FreeBSD does this, but it doesn't fix anything,
1863 * XXX and appears potentially harmful.
1864 */
1865 ss->command_control |= MLY_CMDCTL_DISABLE_DISCONNECT;
1866 #endif
1867
1868 ss->data_size = htole32(xs->datalen);
1869 _lto3l(MLY_PHYADDR(0, chan->chan_channel,
1870 periph->periph_target, periph->periph_lun), ss->addr);
1871
1872 if (xs->timeout < 60 * 1000)
1873 ss->timeout = xs->timeout / 1000 |
1874 MLY_TIMEOUT_SECONDS;
1875 else if (xs->timeout < 60 * 60 * 1000)
1876 ss->timeout = xs->timeout / (60 * 1000) |
1877 MLY_TIMEOUT_MINUTES;
1878 else
1879 ss->timeout = xs->timeout / (60 * 60 * 1000) |
1880 MLY_TIMEOUT_HOURS;
1881
1882 ss->maximum_sense_size = sizeof(xs->sense);
1883 ss->cdb_length = xs->cmdlen;
1884 memcpy(ss->cdb, xs->cmd, xs->cmdlen);
1885
1886 if (mc->mc_length != 0) {
1887 if ((xs->xs_control & XS_CTL_DATA_OUT) != 0)
1888 mc->mc_flags |= MLY_CCB_DATAOUT;
1889 else /* if ((xs->xs_control & XS_CTL_DATA_IN) != 0) */
1890 mc->mc_flags |= MLY_CCB_DATAIN;
1891
1892 if (mly_ccb_map(mly, mc) != 0) {
1893 xs->error = XS_DRIVER_STUFFUP;
1894 mly_ccb_free(mly, mc);
1895 scsipi_done(xs);
1896 break;
1897 }
1898 }
1899
1900 /*
1901 * Give the command to the controller.
1902 */
1903 if ((xs->xs_control & XS_CTL_POLL) != 0) {
1904 if (mly_ccb_poll(mly, mc, xs->timeout + 5000)) {
1905 xs->error = XS_REQUEUE;
1906 if (mc->mc_length != 0)
1907 mly_ccb_unmap(mly, mc);
1908 mly_ccb_free(mly, mc);
1909 scsipi_done(xs);
1910 }
1911 } else
1912 mly_ccb_enqueue(mly, mc);
1913
1914 break;
1915
1916 case ADAPTER_REQ_GROW_RESOURCES:
1917 /*
1918 * Not supported.
1919 */
1920 break;
1921
1922 case ADAPTER_REQ_SET_XFER_MODE:
1923 /*
1924 * We can't change the transfer mode, but at least let
1925 * scsipi know what the adapter has negotiated.
1926 */
1927 mly_get_xfer_mode(mly, chan->chan_channel, arg);
1928 break;
1929 }
1930 }
1931
1932 /*
1933 * Handle completion of a SCSI command.
1934 */
1935 static void
1936 mly_scsipi_complete(struct mly_softc *mly, struct mly_ccb *mc)
1937 {
1938 struct scsipi_xfer *xs;
1939 struct scsipi_channel *chan;
1940 struct scsipi_inquiry_data *inq;
1941 struct mly_btl *btl;
1942 int target, sl, s;
1943 const char *p;
1944
1945 xs = mc->mc_private;
1946 xs->status = mc->mc_status;
1947
1948 /*
1949 * XXX The `resid' value as returned by the controller appears to be
1950 * bogus, so we always set it to zero. Is it perhaps the transfer
1951 * count?
1952 */
1953 xs->resid = 0; /* mc->mc_resid; */
1954
1955 if (mc->mc_length != 0)
1956 mly_ccb_unmap(mly, mc);
1957
1958 switch (mc->mc_status) {
1959 case SCSI_OK:
1960 /*
1961 * In order to report logical device type and status, we
1962 * overwrite the result of the INQUIRY command to logical
1963 * devices.
1964 */
1965 if (xs->cmd->opcode == INQUIRY) {
1966 chan = xs->xs_periph->periph_channel;
1967 target = xs->xs_periph->periph_target;
1968 btl = &mly->mly_btl[chan->chan_channel][target];
1969
1970 s = splbio();
1971 if ((btl->mb_flags & MLY_BTL_LOGICAL) != 0) {
1972 inq = (struct scsipi_inquiry_data *)xs->data;
1973 mly_padstr(inq->vendor, "MYLEX", 8);
1974 p = mly_describe_code(mly_table_device_type,
1975 btl->mb_type);
1976 mly_padstr(inq->product, p, 16);
1977 p = mly_describe_code(mly_table_device_state,
1978 btl->mb_state);
1979 mly_padstr(inq->revision, p, 4);
1980 }
1981 splx(s);
1982 }
1983
1984 xs->error = XS_NOERROR;
1985 break;
1986
1987 case SCSI_CHECK:
1988 sl = mc->mc_sense;
1989 if (sl > sizeof(xs->sense.scsi_sense))
1990 sl = sizeof(xs->sense.scsi_sense);
1991 memcpy(&xs->sense.scsi_sense, mc->mc_packet, sl);
1992 xs->error = XS_SENSE;
1993 break;
1994
1995 case SCSI_BUSY:
1996 case SCSI_QUEUE_FULL:
1997 xs->error = XS_BUSY;
1998 break;
1999
2000 default:
2001 printf("%s: unknown SCSI status 0x%x\n",
2002 device_xname(&mly->mly_dv), xs->status);
2003 xs->error = XS_DRIVER_STUFFUP;
2004 break;
2005 }
2006
2007 mly_ccb_free(mly, mc);
2008 scsipi_done(xs);
2009 }
2010
2011 /*
2012 * Notify scsipi about a target's transfer mode.
2013 */
2014 static void
2015 mly_get_xfer_mode(struct mly_softc *mly, int bus, struct scsipi_xfer_mode *xm)
2016 {
2017 struct mly_btl *btl;
2018 int s;
2019
2020 btl = &mly->mly_btl[bus][xm->xm_target];
2021 xm->xm_mode = 0;
2022
2023 s = splbio();
2024
2025 if ((btl->mb_flags & MLY_BTL_PHYSICAL) != 0) {
2026 if (btl->mb_speed == 0) {
2027 xm->xm_period = 0;
2028 xm->xm_offset = 0;
2029 } else {
2030 xm->xm_period = 12; /* XXX */
2031 xm->xm_offset = 8; /* XXX */
2032 xm->xm_mode |= PERIPH_CAP_SYNC; /* XXX */
2033 }
2034
2035 switch (btl->mb_width) {
2036 case 32:
2037 xm->xm_mode = PERIPH_CAP_WIDE32;
2038 break;
2039 case 16:
2040 xm->xm_mode = PERIPH_CAP_WIDE16;
2041 break;
2042 default:
2043 xm->xm_mode = 0;
2044 break;
2045 }
2046 } else /* ((btl->mb_flags & MLY_BTL_LOGICAL) != 0) */ {
2047 xm->xm_mode = PERIPH_CAP_WIDE16 | PERIPH_CAP_SYNC;
2048 xm->xm_period = 12;
2049 xm->xm_offset = 8;
2050 }
2051
2052 if ((btl->mb_flags & MLY_BTL_TQING) != 0)
2053 xm->xm_mode |= PERIPH_CAP_TQING;
2054
2055 splx(s);
2056
2057 scsipi_async_event(&mly->mly_chans[bus], ASYNC_EVENT_XFER_MODE, xm);
2058 }
2059
2060 /*
2061 * ioctl hook; used here only to initiate low-level rescans.
2062 */
2063 static int
2064 mly_scsipi_ioctl(struct scsipi_channel *chan, u_long cmd, void *data,
2065 int flag, struct proc *p)
2066 {
2067 struct mly_softc *mly;
2068 int rv;
2069
2070 mly = (struct mly_softc *)chan->chan_adapter->adapt_dev;
2071
2072 switch (cmd) {
2073 case SCBUSIOLLSCAN:
2074 mly_scan_channel(mly, chan->chan_channel);
2075 rv = 0;
2076 break;
2077 default:
2078 rv = ENOTTY;
2079 break;
2080 }
2081
2082 return (rv);
2083 }
2084
2085 /*
2086 * Handshake with the firmware while the card is being initialized.
2087 */
2088 static int
2089 mly_fwhandshake(struct mly_softc *mly)
2090 {
2091 u_int8_t error, param0, param1;
2092 int spinup;
2093
2094 spinup = 0;
2095
2096 /* Set HM_STSACK and let the firmware initialize. */
2097 mly_outb(mly, mly->mly_idbr, MLY_HM_STSACK);
2098 DELAY(1000); /* too short? */
2099
2100 /* If HM_STSACK is still true, the controller is initializing. */
2101 if (!mly_idbr_true(mly, MLY_HM_STSACK))
2102 return (0);
2103
2104 printf("%s: controller initialization started\n",
2105 device_xname(&mly->mly_dv));
2106
2107 /*
2108 * Spin waiting for initialization to finish, or for a message to be
2109 * delivered.
2110 */
2111 while (mly_idbr_true(mly, MLY_HM_STSACK)) {
2112 /* Check for a message */
2113 if (!mly_error_valid(mly))
2114 continue;
2115
2116 error = mly_inb(mly, mly->mly_error_status) & ~MLY_MSG_EMPTY;
2117 param0 = mly_inb(mly, mly->mly_cmd_mailbox);
2118 param1 = mly_inb(mly, mly->mly_cmd_mailbox + 1);
2119
2120 switch (error) {
2121 case MLY_MSG_SPINUP:
2122 if (!spinup) {
2123 printf("%s: drive spinup in progress\n",
2124 device_xname(&mly->mly_dv));
2125 spinup = 1;
2126 }
2127 break;
2128
2129 case MLY_MSG_RACE_RECOVERY_FAIL:
2130 printf("%s: mirror race recovery failed - \n",
2131 device_xname(&mly->mly_dv));
2132 printf("%s: one or more drives offline\n",
2133 device_xname(&mly->mly_dv));
2134 break;
2135
2136 case MLY_MSG_RACE_IN_PROGRESS:
2137 printf("%s: mirror race recovery in progress\n",
2138 device_xname(&mly->mly_dv));
2139 break;
2140
2141 case MLY_MSG_RACE_ON_CRITICAL:
2142 printf("%s: mirror race recovery on critical drive\n",
2143 device_xname(&mly->mly_dv));
2144 break;
2145
2146 case MLY_MSG_PARITY_ERROR:
2147 printf("%s: FATAL MEMORY PARITY ERROR\n",
2148 device_xname(&mly->mly_dv));
2149 return (ENXIO);
2150
2151 default:
2152 printf("%s: unknown initialization code 0x%x\n",
2153 device_xname(&mly->mly_dv), error);
2154 break;
2155 }
2156 }
2157
2158 return (0);
2159 }
2160
2161 /*
2162 * Space-fill a character string
2163 */
2164 static void
2165 mly_padstr(char *dst, const char *src, int len)
2166 {
2167
2168 while (len-- > 0) {
2169 if (*src != '\0')
2170 *dst++ = *src++;
2171 else
2172 *dst++ = ' ';
2173 }
2174 }
2175
2176 /*
2177 * Allocate DMA safe memory.
2178 */
2179 static int
2180 mly_dmamem_alloc(struct mly_softc *mly, int size, bus_dmamap_t *dmamap,
2181 void **kva, bus_addr_t *paddr, bus_dma_segment_t *seg)
2182 {
2183 int rseg, rv, state;
2184
2185 state = 0;
2186
2187 if ((rv = bus_dmamem_alloc(mly->mly_dmat, size, PAGE_SIZE, 0,
2188 seg, 1, &rseg, BUS_DMA_NOWAIT)) != 0) {
2189 aprint_error_dev(&mly->mly_dv, "dmamem_alloc = %d\n", rv);
2190 goto bad;
2191 }
2192
2193 state++;
2194
2195 if ((rv = bus_dmamem_map(mly->mly_dmat, seg, 1, size, kva,
2196 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
2197 aprint_error_dev(&mly->mly_dv, "dmamem_map = %d\n", rv);
2198 goto bad;
2199 }
2200
2201 state++;
2202
2203 if ((rv = bus_dmamap_create(mly->mly_dmat, size, size, 1, 0,
2204 BUS_DMA_NOWAIT, dmamap)) != 0) {
2205 aprint_error_dev(&mly->mly_dv, "dmamap_create = %d\n", rv);
2206 goto bad;
2207 }
2208
2209 state++;
2210
2211 if ((rv = bus_dmamap_load(mly->mly_dmat, *dmamap, *kva, size,
2212 NULL, BUS_DMA_NOWAIT)) != 0) {
2213 aprint_error_dev(&mly->mly_dv, "dmamap_load = %d\n", rv);
2214 goto bad;
2215 }
2216
2217 *paddr = (*dmamap)->dm_segs[0].ds_addr;
2218 memset(*kva, 0, size);
2219 return (0);
2220
2221 bad:
2222 if (state > 2)
2223 bus_dmamap_destroy(mly->mly_dmat, *dmamap);
2224 if (state > 1)
2225 bus_dmamem_unmap(mly->mly_dmat, *kva, size);
2226 if (state > 0)
2227 bus_dmamem_free(mly->mly_dmat, seg, 1);
2228
2229 return (rv);
2230 }
2231
2232 /*
2233 * Free DMA safe memory.
2234 */
2235 static void
2236 mly_dmamem_free(struct mly_softc *mly, int size, bus_dmamap_t dmamap,
2237 void *kva, bus_dma_segment_t *seg)
2238 {
2239
2240 bus_dmamap_unload(mly->mly_dmat, dmamap);
2241 bus_dmamap_destroy(mly->mly_dmat, dmamap);
2242 bus_dmamem_unmap(mly->mly_dmat, kva, size);
2243 bus_dmamem_free(mly->mly_dmat, seg, 1);
2244 }
2245
2246
2247 /*
2248 * Accept an open operation on the control device.
2249 */
2250 int
2251 mlyopen(dev_t dev, int flag, int mode, struct lwp *l)
2252 {
2253 struct mly_softc *mly;
2254
2255 if ((mly = device_lookup(&mly_cd, minor(dev))) == NULL)
2256 return (ENXIO);
2257 if ((mly->mly_state & MLY_STATE_INITOK) == 0)
2258 return (ENXIO);
2259 if ((mly->mly_state & MLY_STATE_OPEN) != 0)
2260 return (EBUSY);
2261
2262 mly->mly_state |= MLY_STATE_OPEN;
2263 return (0);
2264 }
2265
2266 /*
2267 * Accept the last close on the control device.
2268 */
2269 int
2270 mlyclose(dev_t dev, int flag, int mode,
2271 struct lwp *l)
2272 {
2273 struct mly_softc *mly;
2274
2275 mly = device_lookup(&mly_cd, minor(dev));
2276 mly->mly_state &= ~MLY_STATE_OPEN;
2277 return (0);
2278 }
2279
2280 /*
2281 * Handle control operations.
2282 */
2283 int
2284 mlyioctl(dev_t dev, u_long cmd, void *data, int flag,
2285 struct lwp *l)
2286 {
2287 struct mly_softc *mly;
2288 int rv;
2289
2290 mly = device_lookup(&mly_cd, minor(dev));
2291
2292 switch (cmd) {
2293 case MLYIO_COMMAND:
2294 rv = kauth_authorize_device_passthru(l->l_cred, dev,
2295 KAUTH_REQ_DEVICE_RAWIO_PASSTHRU_ALL, data);
2296 if (rv)
2297 break;
2298
2299 rv = mly_user_command(mly, (void *)data);
2300 break;
2301 case MLYIO_HEALTH:
2302 rv = mly_user_health(mly, (void *)data);
2303 break;
2304 default:
2305 rv = ENOTTY;
2306 break;
2307 }
2308
2309 return (rv);
2310 }
2311
2312 /*
2313 * Execute a command passed in from userspace.
2314 *
2315 * The control structure contains the actual command for the controller, as
2316 * well as the user-space data pointer and data size, and an optional sense
2317 * buffer size/pointer. On completion, the data size is adjusted to the
2318 * command residual, and the sense buffer size to the size of the returned
2319 * sense data.
2320 */
2321 static int
2322 mly_user_command(struct mly_softc *mly, struct mly_user_command *uc)
2323 {
2324 struct mly_ccb *mc;
2325 int rv, mapped;
2326
2327 if ((rv = mly_ccb_alloc(mly, &mc)) != 0)
2328 return (rv);
2329
2330 mapped = 0;
2331 mc->mc_data = NULL;
2332
2333 /*
2334 * Handle data size/direction.
2335 */
2336 if ((mc->mc_length = abs(uc->DataTransferLength)) != 0) {
2337 if (mc->mc_length > MAXPHYS) {
2338 rv = EINVAL;
2339 goto out;
2340 }
2341
2342 mc->mc_data = malloc(mc->mc_length, M_DEVBUF, M_WAITOK);
2343 if (mc->mc_data == NULL) {
2344 rv = ENOMEM;
2345 goto out;
2346 }
2347
2348 if (uc->DataTransferLength > 0) {
2349 mc->mc_flags |= MLY_CCB_DATAIN;
2350 memset(mc->mc_data, 0, mc->mc_length);
2351 }
2352
2353 if (uc->DataTransferLength < 0) {
2354 mc->mc_flags |= MLY_CCB_DATAOUT;
2355 rv = copyin(uc->DataTransferBuffer, mc->mc_data,
2356 mc->mc_length);
2357 if (rv != 0)
2358 goto out;
2359 }
2360
2361 if ((rv = mly_ccb_map(mly, mc)) != 0)
2362 goto out;
2363 mapped = 1;
2364 }
2365
2366 /* Copy in the command and execute it. */
2367 memcpy(mc->mc_packet, &uc->CommandMailbox, sizeof(uc->CommandMailbox));
2368
2369 if ((rv = mly_ccb_wait(mly, mc, 60000)) != 0)
2370 goto out;
2371
2372 /* Return the data to userspace. */
2373 if (uc->DataTransferLength > 0) {
2374 rv = copyout(mc->mc_data, uc->DataTransferBuffer,
2375 mc->mc_length);
2376 if (rv != 0)
2377 goto out;
2378 }
2379
2380 /* Return the sense buffer to userspace. */
2381 if (uc->RequestSenseLength > 0 && mc->mc_sense > 0) {
2382 rv = copyout(mc->mc_packet, uc->RequestSenseBuffer,
2383 min(uc->RequestSenseLength, mc->mc_sense));
2384 if (rv != 0)
2385 goto out;
2386 }
2387
2388 /* Return command results to userspace (caller will copy out). */
2389 uc->DataTransferLength = mc->mc_resid;
2390 uc->RequestSenseLength = min(uc->RequestSenseLength, mc->mc_sense);
2391 uc->CommandStatus = mc->mc_status;
2392 rv = 0;
2393
2394 out:
2395 if (mapped)
2396 mly_ccb_unmap(mly, mc);
2397 if (mc->mc_data != NULL)
2398 free(mc->mc_data, M_DEVBUF);
2399 mly_ccb_free(mly, mc);
2400
2401 return (rv);
2402 }
2403
2404 /*
2405 * Return health status to userspace. If the health change index in the
2406 * user structure does not match that currently exported by the controller,
2407 * we return the current status immediately. Otherwise, we block until
2408 * either interrupted or new status is delivered.
2409 */
2410 static int
2411 mly_user_health(struct mly_softc *mly, struct mly_user_health *uh)
2412 {
2413 struct mly_health_status mh;
2414 int rv, s;
2415
2416 /* Fetch the current health status from userspace. */
2417 rv = copyin(uh->HealthStatusBuffer, &mh, sizeof(mh));
2418 if (rv != 0)
2419 return (rv);
2420
2421 /* spin waiting for a status update */
2422 s = splbio();
2423 if (mly->mly_event_change == mh.change_counter)
2424 rv = tsleep(&mly->mly_event_change, PRIBIO | PCATCH,
2425 "mlyhealth", 0);
2426 splx(s);
2427
2428 if (rv == 0) {
2429 /*
2430 * Copy the controller's health status buffer out (there is
2431 * a race here if it changes again).
2432 */
2433 rv = copyout(&mly->mly_mmbox->mmm_health.status,
2434 uh->HealthStatusBuffer, sizeof(uh->HealthStatusBuffer));
2435 }
2436
2437 return (rv);
2438 }
2439