twe.c revision 1.9 1 /* $NetBSD: twe.c,v 1.9 2001/01/23 20:47:02 ad Exp $ */
2
3 /*-
4 * Copyright (c) 2000 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.
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 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: twe.c,v 1.1 2000/05/24 23:35:23 msmith Exp
66 */
67
68 /*
69 * Driver for the 3ware Escalade family of RAID controllers.
70 */
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/kernel.h>
75 #include <sys/device.h>
76 #include <sys/queue.h>
77 #include <sys/proc.h>
78 #include <sys/buf.h>
79 #include <sys/endian.h>
80 #include <sys/malloc.h>
81 #include <sys/disk.h>
82
83 #include <uvm/uvm_extern.h>
84
85 #include <machine/bswap.h>
86 #include <machine/bus.h>
87
88 #include <dev/pci/pcireg.h>
89 #include <dev/pci/pcivar.h>
90 #include <dev/pci/pcidevs.h>
91 #include <dev/pci/twereg.h>
92 #include <dev/pci/twevar.h>
93
94 #define TWE_INL(sc, port) \
95 bus_space_read_4((sc)->sc_iot, (sc)->sc_ioh, port)
96 #define TWE_OUTL(sc, port, val) \
97 bus_space_write_4((sc)->sc_iot, (sc)->sc_ioh, port, val)
98
99 #define PCI_CBIO 0x10
100
101 static void twe_aen_handler(struct twe_ccb *, int);
102 static void twe_attach(struct device *, struct device *, void *);
103 static int twe_init_connection(struct twe_softc *);
104 static int twe_intr(void *);
105 static int twe_match(struct device *, struct cfdata *, void *);
106 static int twe_param_get(struct twe_softc *, int, int, size_t,
107 void (*)(struct twe_ccb *, int), void **);
108 static void twe_poll(struct twe_softc *);
109 static int twe_print(void *, const char *);
110 static int twe_reset(struct twe_softc *);
111 static int twe_submatch(struct device *, struct cfdata *, void *);
112 static int twe_status_check(struct twe_softc *, u_int);
113 static int twe_status_wait(struct twe_softc *, u_int, int);
114
115 struct cfattach twe_ca = {
116 sizeof(struct twe_softc), twe_match, twe_attach
117 };
118
119 struct {
120 const u_int aen; /* High byte non-zero if w/unit */
121 const char *desc;
122 } static const twe_aen_names[] = {
123 { 0x0000, "queue empty" },
124 { 0x0001, "soft reset" },
125 { 0x0102, "degraded mirror" },
126 { 0x0003, "controller error" },
127 { 0x0104, "rebuild fail" },
128 { 0x0105, "rebuild done" },
129 { 0x0106, "incompatible unit" },
130 { 0x0107, "init done" },
131 { 0x0108, "unclean shutdown" },
132 { 0x0109, "aport timeout" },
133 { 0x010a, "drive error" },
134 { 0x010b, "rebuild started" },
135 { 0x0015, "table undefined" },
136 { 0x00ff, "aen queue full" },
137 };
138
139 /*
140 * Match a supported board.
141 */
142 static int
143 twe_match(struct device *parent, struct cfdata *cfdata, void *aux)
144 {
145 struct pci_attach_args *pa;
146
147 pa = aux;
148
149 return (PCI_VENDOR(pa->pa_id) == PCI_VENDOR_3WARE &&
150 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_3WARE_ESCALADE);
151 }
152
153 /*
154 * Attach a supported board.
155 *
156 * XXX This doesn't fail gracefully.
157 */
158 static void
159 twe_attach(struct device *parent, struct device *self, void *aux)
160 {
161 struct pci_attach_args *pa;
162 struct twe_softc *sc;
163 pci_chipset_tag_t pc;
164 pci_intr_handle_t ih;
165 pcireg_t csr;
166 const char *intrstr;
167 int size, i, rv, rseg;
168 struct twe_param *dtp, *ctp;
169 bus_dma_segment_t seg;
170 struct twe_cmd *tc;
171 struct twe_attach_args twea;
172 struct twe_ccb *ccb;
173
174 sc = (struct twe_softc *)self;
175 pa = aux;
176 pc = pa->pa_pc;
177 sc->sc_dmat = pa->pa_dmat;
178 SIMPLEQ_INIT(&sc->sc_ccb_queue);
179 SLIST_INIT(&sc->sc_ccb_freelist);
180
181 printf(": 3ware Escalade\n");
182
183 if (pci_mapreg_map(pa, PCI_CBIO, PCI_MAPREG_TYPE_IO, 0,
184 &sc->sc_iot, &sc->sc_ioh, NULL, NULL)) {
185 printf("%s: can't map i/o space\n", sc->sc_dv.dv_xname);
186 return;
187 }
188
189 /* Enable the device. */
190 csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
191 pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
192 csr | PCI_COMMAND_MASTER_ENABLE);
193
194 /* Map and establish the interrupt. */
195 if (pci_intr_map(pa, &ih)) {
196 printf("%s: can't map interrupt\n", sc->sc_dv.dv_xname);
197 return;
198 }
199 intrstr = pci_intr_string(pc, ih);
200 sc->sc_ih = pci_intr_establish(pc, ih, IPL_BIO, twe_intr, sc);
201 if (sc->sc_ih == NULL) {
202 printf("%s: can't establish interrupt", sc->sc_dv.dv_xname);
203 if (intrstr != NULL)
204 printf(" at %s", intrstr);
205 printf("\n");
206 return;
207 }
208 if (intrstr != NULL)
209 printf("%s: interrupting at %s\n", sc->sc_dv.dv_xname, intrstr);
210
211 /*
212 * Allocate and initialise the command blocks and CCBs.
213 */
214 size = sizeof(struct twe_cmd) * TWE_MAX_QUEUECNT;
215
216 if ((rv = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1,
217 &rseg, BUS_DMA_NOWAIT)) != 0) {
218 printf("%s: unable to allocate commands, rv = %d\n",
219 sc->sc_dv.dv_xname, rv);
220 return;
221 }
222
223 if ((rv = bus_dmamem_map(sc->sc_dmat, &seg, rseg, size,
224 (caddr_t *)&sc->sc_cmds,
225 BUS_DMA_NOWAIT | BUS_DMA_COHERENT)) != 0) {
226 printf("%s: unable to map commands, rv = %d\n",
227 sc->sc_dv.dv_xname, rv);
228 return;
229 }
230
231 if ((rv = bus_dmamap_create(sc->sc_dmat, size, size, 1, 0,
232 BUS_DMA_NOWAIT, &sc->sc_dmamap)) != 0) {
233 printf("%s: unable to create command DMA map, rv = %d\n",
234 sc->sc_dv.dv_xname, rv);
235 return;
236 }
237
238 if ((rv = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap, sc->sc_cmds,
239 size, NULL, BUS_DMA_NOWAIT)) != 0) {
240 printf("%s: unable to load command DMA map, rv = %d\n",
241 sc->sc_dv.dv_xname, rv);
242 return;
243 }
244
245 sc->sc_cmds_paddr = sc->sc_dmamap->dm_segs[0].ds_addr;
246 memset(sc->sc_cmds, 0, size);
247
248 ccb = malloc(sizeof(*ccb) * TWE_MAX_QUEUECNT, M_DEVBUF, M_NOWAIT);
249 sc->sc_ccbs = ccb;
250 tc = (struct twe_cmd *)sc->sc_cmds;
251
252 for (i = 0; i < TWE_MAX_QUEUECNT; i++, tc++, ccb++) {
253 ccb->ccb_cmd = tc;
254 ccb->ccb_cmdid = i;
255 ccb->ccb_flags = 0;
256 rv = bus_dmamap_create(sc->sc_dmat, TWE_MAX_XFER,
257 TWE_MAX_SEGS, PAGE_SIZE, 0,
258 BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW,
259 &ccb->ccb_dmamap_xfer);
260 if (rv != 0) {
261 printf("%s: can't create dmamap, rv = %d\n",
262 sc->sc_dv.dv_xname, rv);
263 return;
264 }
265 /* Save one CCB for parameter retrieval. */
266 if (i != 0)
267 SLIST_INSERT_HEAD(&sc->sc_ccb_freelist, ccb,
268 ccb_chain.slist);
269 }
270
271 /* Wait for the controller to become ready. */
272 if (twe_status_wait(sc, TWE_STS_MICROCONTROLLER_READY, 6)) {
273 printf("%s: microcontroller not ready\n", sc->sc_dv.dv_xname);
274 return;
275 }
276
277 TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_DISABLE_INTRS);
278
279 /* Reset the controller. */
280 if (twe_reset(sc)) {
281 printf("%s: reset failed\n", sc->sc_dv.dv_xname);
282 return;
283 }
284
285 /* Find attached units. */
286 rv = twe_param_get(sc, TWE_PARAM_UNITSUMMARY,
287 TWE_PARAM_UNITSUMMARY_Status, TWE_MAX_UNITS, NULL, (void **)&dtp);
288 if (rv != 0) {
289 printf("%s: can't detect attached units (%d)\n",
290 sc->sc_dv.dv_xname, rv);
291 return;
292 }
293
294 /* For each detected unit, collect size and store in an array. */
295 for (i = 0, sc->sc_nunits = 0; i < TWE_MAX_UNITS; i++) {
296 /* Unit present? */
297 if ((dtp->tp_data[i] & TWE_PARAM_UNITSTATUS_Online) == 0) {
298 sc->sc_dsize[i] = 0;
299 continue;
300 }
301
302 rv = twe_param_get(sc, TWE_PARAM_UNITINFO + i,
303 TWE_PARAM_UNITINFO_Capacity, 4, NULL, (void **)&ctp);
304 if (rv != 0) {
305 printf("%s: error %d fetching capacity for unit %d\n",
306 sc->sc_dv.dv_xname, rv, i);
307 continue;
308 }
309
310 sc->sc_dsize[i] = le32toh(*(u_int32_t *)ctp->tp_data);
311 free(ctp, M_DEVBUF);
312 sc->sc_nunits++;
313 }
314 free(dtp, M_DEVBUF);
315
316 /* Initialise connection with controller and enable interrupts. */
317 twe_init_connection(sc);
318 TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR |
319 TWE_CTL_UNMASK_RESP_INTR |
320 TWE_CTL_ENABLE_INTRS);
321
322 /* Attach sub-devices. */
323 for (i = 0; i < TWE_MAX_UNITS; i++) {
324 if (sc->sc_dsize[i] == 0)
325 continue;
326 twea.twea_unit = i;
327 config_found_sm(&sc->sc_dv, &twea, twe_print, twe_submatch);
328 }
329 }
330
331 /*
332 * Reset the controller. Currently only useful at attach time; must be
333 * called with interrupts blocked.
334 */
335 static int
336 twe_reset(struct twe_softc *sc)
337 {
338 struct twe_param *tp;
339 u_int aen, status;
340 volatile u_int32_t junk;
341 int got, rv;
342
343 /* Issue a soft reset. */
344 TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_ISSUE_SOFT_RESET |
345 TWE_CTL_CLEAR_HOST_INTR |
346 TWE_CTL_CLEAR_ATTN_INTR |
347 TWE_CTL_MASK_CMD_INTR |
348 TWE_CTL_MASK_RESP_INTR |
349 TWE_CTL_CLEAR_ERROR_STS |
350 TWE_CTL_DISABLE_INTRS);
351
352 if (twe_status_wait(sc, TWE_STS_ATTN_INTR, 15)) {
353 printf("%s: no attention interrupt\n",
354 sc->sc_dv.dv_xname);
355 return (-1);
356 }
357
358 /* Pull AENs out of the controller; look for a soft reset AEN. */
359 for (got = 0;;) {
360 rv = twe_param_get(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode,
361 2, NULL, (void **)&tp);
362 if (rv != 0)
363 printf("%s: error %d while draining response queue\n",
364 sc->sc_dv.dv_xname, rv);
365 aen = TWE_AEN_CODE(le16toh(*(u_int16_t *)tp->tp_data));
366 free(tp, M_DEVBUF);
367 if (aen == TWE_AEN_QUEUE_EMPTY)
368 break;
369 if (aen == TWE_AEN_SOFT_RESET)
370 got = 1;
371 }
372 if (!got) {
373 printf("%s: reset not reported\n", sc->sc_dv.dv_xname);
374 return (-1);
375 }
376
377 /* Check controller status. */
378 status = TWE_INL(sc, TWE_REG_STS);
379 if (twe_status_check(sc, status)) {
380 printf("%s: controller errors detected\n",
381 sc->sc_dv.dv_xname);
382 return (-1);
383 }
384
385 /* Drain the response queue. */
386 for (;;) {
387 status = TWE_INL(sc, TWE_REG_STS);
388 if (twe_status_check(sc, status) != 0) {
389 printf("%s: can't drain response queue\n",
390 sc->sc_dv.dv_xname);
391 return (-1);
392 }
393 if ((status & TWE_STS_RESP_QUEUE_EMPTY) != 0)
394 break;
395 junk = TWE_INL(sc, TWE_REG_RESP_QUEUE);
396 }
397
398 return (0);
399 }
400
401 /*
402 * Print autoconfiguration message for a sub-device.
403 */
404 static int
405 twe_print(void *aux, const char *pnp)
406 {
407 struct twe_attach_args *twea;
408
409 twea = aux;
410
411 if (pnp != NULL)
412 printf("block device at %s", pnp);
413 printf(" unit %d", twea->twea_unit);
414 return (UNCONF);
415 }
416
417 /*
418 * Match a sub-device.
419 */
420 static int
421 twe_submatch(struct device *parent, struct cfdata *cf, void *aux)
422 {
423 struct twe_attach_args *twea;
424
425 twea = aux;
426
427 if (cf->tweacf_unit != TWECF_UNIT_DEFAULT &&
428 cf->tweacf_unit != twea->twea_unit)
429 return (0);
430
431 return ((*cf->cf_attach->ca_match)(parent, cf, aux));
432 }
433
434 /*
435 * Interrupt service routine.
436 */
437 static int
438 twe_intr(void *arg)
439 {
440 struct twe_softc *sc;
441 u_int status;
442 int caught, rv;
443
444 sc = arg;
445 caught = 0;
446 status = TWE_INL(sc, TWE_REG_STS);
447 twe_status_check(sc, status);
448
449 /* Host interrupts - purpose unknown. */
450 if ((status & TWE_STS_HOST_INTR) != 0) {
451 #ifdef DIAGNOSTIC
452 printf("%s: host interrupt\n", sc->sc_dv.dv_xname);
453 #endif
454 TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_CLEAR_HOST_INTR);
455 caught = 1;
456 }
457
458 /*
459 * Attention interrupts, signalled when a controller or child device
460 * state change has occured.
461 */
462 if ((status & TWE_STS_ATTN_INTR) != 0) {
463 rv = twe_param_get(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode,
464 2, twe_aen_handler, NULL);
465 if (rv != 0) {
466 printf("%s: unable to retrieve AEN (%d)\n",
467 sc->sc_dv.dv_xname, rv);
468 TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR);
469 }
470 caught = 1;
471 }
472
473 /*
474 * Command interrupts, signalled when the controller can accept more
475 * commands. We don't use this; instead, we try to submit commands
476 * when we receive them, and when other commands have completed.
477 * Mask it so we don't get another one.
478 */
479 if ((status & TWE_STS_CMD_INTR) != 0) {
480 #ifdef DIAGNOSTIC
481 printf("%s: command interrupt\n", sc->sc_dv.dv_xname);
482 #endif
483 TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_MASK_CMD_INTR);
484 caught = 1;
485 }
486
487 if ((status & TWE_STS_RESP_INTR) != 0) {
488 twe_poll(sc);
489 caught = 1;
490 }
491
492 return (caught);
493 }
494
495 /*
496 * Handle an AEN returned by the controller.
497 */
498 static void
499 twe_aen_handler(struct twe_ccb *ccb, int error)
500 {
501 struct twe_softc *sc;
502 struct twe_param *tp;
503 const char *str;
504 u_int aen;
505 int i, hu, rv;
506
507 sc = (struct twe_softc *)ccb->ccb_tx.tx_dv;
508 tp = ccb->ccb_tx.tx_context;
509 twe_ccb_unmap(sc, ccb);
510
511 if (error) {
512 printf("%s: error retrieving AEN\n", sc->sc_dv.dv_xname);
513 aen = TWE_AEN_QUEUE_EMPTY;
514 } else
515 aen = le16toh(*(u_int16_t *)tp->tp_data);
516 free(tp, M_DEVBUF);
517 twe_ccb_free(sc, ccb);
518
519 if (TWE_AEN_CODE(aen) == TWE_AEN_QUEUE_EMPTY) {
520 TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR);
521 return;
522 }
523
524 str = "<unknown>";
525 i = 0;
526 hu = 0;
527
528 while (i < sizeof(twe_aen_names) / sizeof(twe_aen_names[0])) {
529 if (TWE_AEN_CODE(twe_aen_names[i].aen) == TWE_AEN_CODE(aen)) {
530 str = twe_aen_names[i].desc;
531 hu = (TWE_AEN_UNIT(twe_aen_names[i].aen) != 0);
532 break;
533 }
534 i++;
535 }
536 printf("%s: AEN 0x%04x (%s) received", sc->sc_dv.dv_xname,
537 TWE_AEN_CODE(aen), str);
538 if (hu != 0)
539 printf(" for unit %d", TWE_AEN_UNIT(aen));
540 printf("\n");
541
542 /*
543 * Chain another retrieval in case interrupts have been
544 * coalesced.
545 */
546 rv = twe_param_get(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, 2,
547 twe_aen_handler, NULL);
548 if (rv != 0)
549 printf("%s: unable to retrieve AEN (%d)\n",
550 sc->sc_dv.dv_xname, rv);
551 }
552
553 /*
554 * Execute a TWE_OP_GET_PARAM command. If a callback function is provided,
555 * it will be called with generated context when the command has completed.
556 * If no callback is provided, the command will be executed synchronously
557 * and a pointer to a buffer containing the data returned.
558 *
559 * The caller or callback is responsible for freeing the buffer.
560 */
561 static int
562 twe_param_get(struct twe_softc *sc, int table_id, int param_id, size_t size,
563 void (*func)(struct twe_ccb *, int), void **pbuf)
564 {
565 struct twe_ccb *ccb;
566 struct twe_cmd *tc;
567 struct twe_param *tp;
568 int rv, s;
569
570 rv = twe_ccb_alloc(sc, &ccb,
571 TWE_CCB_PARAM | TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
572 if (rv != 0)
573 return (rv);
574
575 tp = malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
576 if (pbuf != NULL)
577 *pbuf = tp;
578
579 ccb->ccb_data = tp;
580 ccb->ccb_datasize = TWE_SECTOR_SIZE;
581 ccb->ccb_tx.tx_handler = func;
582 ccb->ccb_tx.tx_context = tp;
583 ccb->ccb_tx.tx_dv = &sc->sc_dv;
584
585 tc = ccb->ccb_cmd;
586 tc->tc_size = 2;
587 tc->tc_opcode = TWE_OP_GET_PARAM | (tc->tc_size << 5);
588 tc->tc_unit = 0;
589 tc->tc_count = htole16(1);
590
591 /* Fill in the outbound parameter data. */
592 tp->tp_table_id = htole16(table_id);
593 tp->tp_param_id = param_id;
594 tp->tp_param_size = size;
595
596 /* Map the transfer. */
597 if ((rv = twe_ccb_map(sc, ccb)) != 0) {
598 twe_ccb_free(sc, ccb);
599 free(tp, M_DEVBUF);
600 return (rv);
601 }
602
603 /* Submit the command and either wait or let the callback handle it. */
604 if (func == NULL) {
605 s = splbio();
606 rv = twe_ccb_poll(sc, ccb, 5);
607 twe_ccb_unmap(sc, ccb);
608 twe_ccb_free(sc, ccb);
609 splx(s);
610 if (rv != 0)
611 free(tp, M_DEVBUF);
612 } else {
613 twe_ccb_enqueue(sc, ccb);
614 rv = 0;
615 }
616
617 return (rv);
618 }
619
620 /*
621 * Execute a TWE_OP_INIT_CONNECTION command. Return non-zero on error.
622 * Must be called with interrupts blocked.
623 */
624 static int
625 twe_init_connection(struct twe_softc *sc)
626 {
627 struct twe_ccb *ccb;
628 struct twe_cmd *tc;
629 int rv;
630
631 if ((rv = twe_ccb_alloc(sc, &ccb, 0)) != 0)
632 return (rv);
633
634 /* Build the command. */
635 tc = ccb->ccb_cmd;
636 tc->tc_size = 3;
637 tc->tc_opcode = TWE_OP_INIT_CONNECTION;
638 tc->tc_unit = 0;
639 tc->tc_count = htole16(TWE_MAX_CMDS);
640 tc->tc_args.init_connection.response_queue_pointer = 0;
641
642 /* Submit the command for immediate execution. */
643 rv = twe_ccb_poll(sc, ccb, 5);
644 twe_ccb_free(sc, ccb);
645 return (rv);
646 }
647
648 /*
649 * Poll the controller for completed commands. Must be called with
650 * interrupts blocked.
651 */
652 static void
653 twe_poll(struct twe_softc *sc)
654 {
655 struct twe_ccb *ccb;
656 int found;
657 u_int status, cmdid;
658
659 found = 0;
660
661 for (;;) {
662 status = TWE_INL(sc, TWE_REG_STS);
663 twe_status_check(sc, status);
664
665 if ((status & TWE_STS_RESP_QUEUE_EMPTY))
666 break;
667
668 found = 1;
669 cmdid = TWE_INL(sc, TWE_REG_RESP_QUEUE);
670 cmdid = (cmdid & TWE_RESP_MASK) >> TWE_RESP_SHIFT;
671 if (cmdid >= TWE_MAX_QUEUECNT) {
672 printf("%s: bad completion\n", sc->sc_dv.dv_xname);
673 continue;
674 }
675
676 ccb = sc->sc_ccbs + cmdid;
677 if ((ccb->ccb_flags & TWE_CCB_ACTIVE) == 0) {
678 printf("%s: bad completion (not active)\n",
679 sc->sc_dv.dv_xname);
680 continue;
681 }
682 ccb->ccb_flags ^= TWE_CCB_COMPLETE | TWE_CCB_ACTIVE;
683
684 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
685 (caddr_t)ccb->ccb_cmd - sc->sc_cmds,
686 sizeof(struct twe_cmd),
687 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
688
689 /* Pass notification to upper layers. */
690 if (ccb->ccb_tx.tx_handler != NULL)
691 (*ccb->ccb_tx.tx_handler)(ccb,
692 ccb->ccb_cmd->tc_status != 0 ? EIO : 0);
693 }
694
695 /* If any commands have completed, run the software queue. */
696 if (found)
697 twe_ccb_enqueue(sc, NULL);
698 }
699
700 /*
701 * Wait for `status' to be set in the controller status register. Return
702 * zero if found, non-zero if the operation timed out.
703 */
704 static int
705 twe_status_wait(struct twe_softc *sc, u_int32_t status, int timo)
706 {
707
708 for (; timo != 0; timo--) {
709 if ((TWE_INL(sc, TWE_REG_STS) & status) == status)
710 break;
711 delay(100000);
712 }
713
714 return (timo == 0);
715 }
716
717 /*
718 * Complain if the status bits aren't what we expect.
719 */
720 static int
721 twe_status_check(struct twe_softc *sc, u_int status)
722 {
723 int rv;
724
725 rv = 0;
726
727 if ((status & TWE_STS_EXPECTED_BITS) != TWE_STS_EXPECTED_BITS) {
728 printf("%s: missing status bits: 0x%08x\n", sc->sc_dv.dv_xname,
729 status & ~TWE_STS_EXPECTED_BITS);
730 rv = -1;
731 }
732
733 if ((status & TWE_STS_UNEXPECTED_BITS) != 0) {
734 printf("%s: unexpected status bits: 0x%08x\n",
735 sc->sc_dv.dv_xname, status & TWE_STS_UNEXPECTED_BITS);
736 rv = -1;
737 }
738
739 return (rv);
740 }
741
742 /*
743 * Allocate and initialise a CCB.
744 */
745 int
746 twe_ccb_alloc(struct twe_softc *sc, struct twe_ccb **ccbp, int flags)
747 {
748 struct twe_cmd *tc;
749 struct twe_ccb *ccb;
750 int s;
751
752 s = splbio();
753 if ((flags & TWE_CCB_PARAM) != 0)
754 ccb = sc->sc_ccbs;
755 else {
756 /* Allocate a CCB and command block. */
757 if (SLIST_FIRST(&sc->sc_ccb_freelist) == NULL) {
758 splx(s);
759 return (EAGAIN);
760 }
761 ccb = SLIST_FIRST(&sc->sc_ccb_freelist);
762 SLIST_REMOVE_HEAD(&sc->sc_ccb_freelist, ccb_chain.slist);
763 }
764 #ifdef DIAGNOSTIC
765 if ((ccb->ccb_flags & TWE_CCB_ALLOCED) != 0)
766 panic("twe_ccb_alloc: CCB already allocated");
767 flags |= TWE_CCB_ALLOCED;
768 #endif
769 splx(s);
770
771 /* Initialise some fields and return. */
772 ccb->ccb_tx.tx_handler = NULL;
773 ccb->ccb_flags = flags;
774 tc = ccb->ccb_cmd;
775 tc->tc_status = 0;
776 tc->tc_flags = 0;
777 tc->tc_cmdid = ccb->ccb_cmdid;
778 *ccbp = ccb;
779
780 return (0);
781 }
782
783 /*
784 * Free a CCB.
785 */
786 void
787 twe_ccb_free(struct twe_softc *sc, struct twe_ccb *ccb)
788 {
789 int s;
790
791 s = splbio();
792 if ((ccb->ccb_flags & TWE_CCB_PARAM) == 0)
793 SLIST_INSERT_HEAD(&sc->sc_ccb_freelist, ccb, ccb_chain.slist);
794 ccb->ccb_flags = 0;
795 splx(s);
796 }
797
798 /*
799 * Map the specified CCB's command block and data buffer (if any) into
800 * controller visible space. Perform DMA synchronisation.
801 */
802 int
803 twe_ccb_map(struct twe_softc *sc, struct twe_ccb *ccb)
804 {
805 struct twe_cmd *tc;
806 int flags, nsegs, i, s, rv;
807 void *data;
808
809 /*
810 * The data as a whole must be 512-byte aligned.
811 */
812 if (((u_long)ccb->ccb_data & (TWE_ALIGNMENT - 1)) != 0) {
813 s = splvm();
814 /* XXX */
815 ccb->ccb_abuf = uvm_km_kmemalloc(kmem_map, uvmexp.kmem_object,
816 ccb->ccb_datasize, UVM_KMF_NOWAIT);
817 splx(s);
818 data = (void *)ccb->ccb_abuf;
819 if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
820 memcpy(data, ccb->ccb_data, ccb->ccb_datasize);
821 } else {
822 ccb->ccb_abuf = (vaddr_t)0;
823 data = ccb->ccb_data;
824 }
825
826 /*
827 * Map the data buffer into bus space and build the S/G list.
828 */
829 rv = bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap_xfer, data,
830 ccb->ccb_datasize, NULL, BUS_DMA_NOWAIT);
831 if (rv != 0) {
832 if (ccb->ccb_abuf != (vaddr_t)0) {
833 s = splvm();
834 /* XXX */
835 uvm_km_free(kmem_map, ccb->ccb_abuf,
836 ccb->ccb_datasize);
837 splx(s);
838 }
839 return (rv);
840 }
841
842 nsegs = ccb->ccb_dmamap_xfer->dm_nsegs;
843 tc = ccb->ccb_cmd;
844 tc->tc_size += 2 * nsegs;
845
846 /* The location of the S/G list is dependant upon command type. */
847 switch (tc->tc_opcode >> 5) {
848 case 2:
849 for (i = 0; i < nsegs; i++) {
850 tc->tc_args.param.sgl[i].tsg_address =
851 htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
852 tc->tc_args.param.sgl[i].tsg_length =
853 htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
854 }
855 /* XXX Needed? */
856 for (; i < TWE_SG_SIZE; i++) {
857 tc->tc_args.param.sgl[i].tsg_address = 0;
858 tc->tc_args.param.sgl[i].tsg_length = 0;
859 }
860 break;
861 case 3:
862 for (i = 0; i < nsegs; i++) {
863 tc->tc_args.io.sgl[i].tsg_address =
864 htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
865 tc->tc_args.io.sgl[i].tsg_length =
866 htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
867 }
868 /* XXX Needed? */
869 for (; i < TWE_SG_SIZE; i++) {
870 tc->tc_args.io.sgl[i].tsg_address = 0;
871 tc->tc_args.io.sgl[i].tsg_length = 0;
872 }
873 break;
874 #ifdef DEBUG
875 default:
876 panic("twe_ccb_map: oops");
877 #endif
878 }
879
880 if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
881 flags = BUS_DMASYNC_PREREAD;
882 else
883 flags = 0;
884 if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
885 flags |= BUS_DMASYNC_PREWRITE;
886
887 bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
888 ccb->ccb_datasize, flags);
889 return (0);
890 }
891
892 /*
893 * Unmap the specified CCB's command block and data buffer (if any) and
894 * perform DMA synchronisation.
895 */
896 void
897 twe_ccb_unmap(struct twe_softc *sc, struct twe_ccb *ccb)
898 {
899 int flags, s;
900
901 if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
902 flags = BUS_DMASYNC_POSTREAD;
903 else
904 flags = 0;
905 if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
906 flags |= BUS_DMASYNC_POSTWRITE;
907
908 bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
909 ccb->ccb_datasize, flags);
910 bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap_xfer);
911
912 if (ccb->ccb_abuf != (vaddr_t)0) {
913 if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
914 memcpy(ccb->ccb_data, (void *)ccb->ccb_abuf,
915 ccb->ccb_datasize);
916 s = splvm();
917 /* XXX */
918 uvm_km_free(kmem_map, ccb->ccb_abuf, ccb->ccb_datasize);
919 splx(s);
920 }
921 }
922
923 /*
924 * Submit a command to the controller and poll on completion. Return
925 * non-zero on timeout (but don't check status, as some command types don't
926 * return status). Must be called with interrupts blocked.
927 */
928 int
929 twe_ccb_poll(struct twe_softc *sc, struct twe_ccb *ccb, int timo)
930 {
931 int rv;
932
933 if ((rv = twe_ccb_submit(sc, ccb)) != 0)
934 return (rv);
935
936 for (; timo != 0; timo--) {
937 twe_poll(sc);
938 if ((ccb->ccb_flags & TWE_CCB_COMPLETE) != 0)
939 break;
940 DELAY(100000);
941 }
942
943 return (timo == 0);
944 }
945
946 /*
947 * If a CCB is specified, enqueue it. Pull CCBs off the software queue in
948 * the order that they were enqueued and try to submit their command blocks
949 * to the controller for execution.
950 */
951 void
952 twe_ccb_enqueue(struct twe_softc *sc, struct twe_ccb *ccb)
953 {
954 int s;
955
956 s = splbio();
957
958 if (ccb != NULL)
959 SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_queue, ccb, ccb_chain.simpleq);
960
961 while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_queue)) != NULL) {
962 if (twe_ccb_submit(sc, ccb))
963 break;
964 SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_queue, ccb, ccb_chain.simpleq);
965 }
966
967 splx(s);
968 }
969
970 /*
971 * Submit the command block associated with the specified CCB to the
972 * controller for execution. Must be called with interrupts blocked.
973 */
974 int
975 twe_ccb_submit(struct twe_softc *sc, struct twe_ccb *ccb)
976 {
977 bus_addr_t pa;
978 int rv;
979 u_int status;
980
981 /* Check to see if we can post a command. */
982 status = TWE_INL(sc, TWE_REG_STS);
983 twe_status_check(sc, status);
984
985 if ((status & TWE_STS_CMD_QUEUE_FULL) == 0) {
986 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
987 (caddr_t)ccb->ccb_cmd - sc->sc_cmds, sizeof(struct twe_cmd),
988 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
989 ccb->ccb_flags |= TWE_CCB_ACTIVE;
990 pa = sc->sc_cmds_paddr +
991 ccb->ccb_cmdid * sizeof(struct twe_cmd);
992 TWE_OUTL(sc, TWE_REG_CMD_QUEUE, (u_int32_t)pa);
993 rv = 0;
994 } else
995 rv = EBUSY;
996
997 return (rv);
998 }
999