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