twe.c revision 1.8 1 /* $NetBSD: twe.c,v 1.8 2001/01/22 17:40:15 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 caught = 1;
469 }
470
471 /*
472 * Command interrupts, signalled when the controller can accept more
473 * commands. We don't use this; instead, we try to submit commands
474 * when we receive them, and when other commands have completed.
475 * Mask it so we don't get another one.
476 */
477 if ((status & TWE_STS_CMD_INTR) != 0) {
478 #ifdef DIAGNOSTIC
479 printf("%s: command interrupt\n", sc->sc_dv.dv_xname);
480 #endif
481 TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_MASK_CMD_INTR);
482 caught = 1;
483 }
484
485 if ((status & TWE_STS_RESP_INTR) != 0) {
486 twe_poll(sc);
487 caught = 1;
488 }
489
490 return (caught);
491 }
492
493 /*
494 * Handle an AEN returned by the controller.
495 */
496 static void
497 twe_aen_handler(struct twe_ccb *ccb, int error)
498 {
499 struct twe_softc *sc;
500 struct twe_param *tp;
501 const char *str;
502 u_int aen;
503 int i, hu, rv;
504
505 sc = (struct twe_softc *)ccb->ccb_tx.tx_dv;
506 tp = ccb->ccb_tx.tx_context;
507 twe_ccb_unmap(sc, ccb);
508
509 if (error) {
510 printf("%s: error retrieving AEN\n", sc->sc_dv.dv_xname);
511 aen = TWE_AEN_QUEUE_EMPTY;
512 } else
513 aen = le16toh(*(u_int16_t *)tp->tp_data);
514 free(tp, M_DEVBUF);
515 twe_ccb_free(sc, ccb);
516
517 if (TWE_AEN_CODE(aen) == TWE_AEN_QUEUE_EMPTY) {
518 TWE_OUTL(sc, TWE_REG_CTL, TWE_CTL_CLEAR_ATTN_INTR);
519 return;
520 }
521
522 str = "<unknown>";
523 i = 0;
524 hu = 0;
525
526 while (i < sizeof(twe_aen_names) / sizeof(twe_aen_names[0])) {
527 if (TWE_AEN_CODE(twe_aen_names[i].aen) == TWE_AEN_CODE(aen)) {
528 str = twe_aen_names[i].desc;
529 hu = (TWE_AEN_UNIT(twe_aen_names[i].aen) != 0);
530 break;
531 }
532 i++;
533 }
534 printf("%s: AEN 0x%04x (%s) received", sc->sc_dv.dv_xname,
535 TWE_AEN_CODE(aen), str);
536 if (hu != 0)
537 printf(" for unit %d", TWE_AEN_UNIT(aen));
538 printf("\n");
539
540 /*
541 * Chain another retrieval in case interrupts have been
542 * coalesced.
543 */
544 rv = twe_param_get(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, 2,
545 twe_aen_handler, NULL);
546 if (rv != 0)
547 printf("%s: unable to retrieve AEN (%d)\n",
548 sc->sc_dv.dv_xname, rv);
549 }
550
551 /*
552 * Execute a TWE_OP_GET_PARAM command. If a callback function is provided,
553 * it will be called with generated context when the command has completed.
554 * If no callback is provided, the command will be executed synchronously
555 * and a pointer to a buffer containing the data returned.
556 *
557 * The caller or callback is responsible for freeing the buffer.
558 */
559 static int
560 twe_param_get(struct twe_softc *sc, int table_id, int param_id, size_t size,
561 void (*func)(struct twe_ccb *, int), void **pbuf)
562 {
563 struct twe_ccb *ccb;
564 struct twe_cmd *tc;
565 struct twe_param *tp;
566 int rv, s;
567
568 rv = twe_ccb_alloc(sc, &ccb,
569 TWE_CCB_PARAM | TWE_CCB_DATA_IN | TWE_CCB_DATA_OUT);
570 if (rv != 0)
571 return (rv);
572
573 tp = malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT);
574 if (pbuf != NULL)
575 *pbuf = tp;
576
577 ccb->ccb_data = tp;
578 ccb->ccb_datasize = TWE_SECTOR_SIZE;
579 ccb->ccb_tx.tx_handler = func;
580 ccb->ccb_tx.tx_context = tp;
581 ccb->ccb_tx.tx_dv = &sc->sc_dv;
582
583 tc = ccb->ccb_cmd;
584 tc->tc_size = 2;
585 tc->tc_opcode = TWE_OP_GET_PARAM | (tc->tc_size << 5);
586 tc->tc_unit = 0;
587 tc->tc_count = htole16(1);
588
589 /* Fill in the outbound parameter data. */
590 tp->tp_table_id = htole16(table_id);
591 tp->tp_param_id = param_id;
592 tp->tp_param_size = size;
593
594 /* Map the transfer. */
595 if ((rv = twe_ccb_map(sc, ccb)) != 0) {
596 twe_ccb_free(sc, ccb);
597 free(tp, M_DEVBUF);
598 return (rv);
599 }
600
601 /* Submit the command and either wait or let the callback handle it. */
602 if (func == NULL) {
603 s = splbio();
604 rv = twe_ccb_poll(sc, ccb, 5);
605 twe_ccb_unmap(sc, ccb);
606 twe_ccb_free(sc, ccb);
607 splx(s);
608 if (rv != 0)
609 free(tp, M_DEVBUF);
610 } else {
611 twe_ccb_enqueue(sc, ccb);
612 rv = 0;
613 }
614
615 return (rv);
616 }
617
618 /*
619 * Execute a TWE_OP_INIT_CONNECTION command. Return non-zero on error.
620 * Must be called with interrupts blocked.
621 */
622 static int
623 twe_init_connection(struct twe_softc *sc)
624 {
625 struct twe_ccb *ccb;
626 struct twe_cmd *tc;
627 int rv;
628
629 if ((rv = twe_ccb_alloc(sc, &ccb, 0)) != 0)
630 return (rv);
631
632 /* Build the command. */
633 tc = ccb->ccb_cmd;
634 tc->tc_size = 3;
635 tc->tc_opcode = TWE_OP_INIT_CONNECTION;
636 tc->tc_unit = 0;
637 tc->tc_count = htole16(TWE_MAX_CMDS);
638 tc->tc_args.init_connection.response_queue_pointer = 0;
639
640 /* Submit the command for immediate execution. */
641 rv = twe_ccb_poll(sc, ccb, 5);
642 twe_ccb_free(sc, ccb);
643 return (rv);
644 }
645
646 /*
647 * Poll the controller for completed commands. Must be called with
648 * interrupts blocked.
649 */
650 static void
651 twe_poll(struct twe_softc *sc)
652 {
653 struct twe_ccb *ccb;
654 int found;
655 u_int status, cmdid;
656
657 found = 0;
658
659 for (;;) {
660 status = TWE_INL(sc, TWE_REG_STS);
661 twe_status_check(sc, status);
662
663 if ((status & TWE_STS_RESP_QUEUE_EMPTY))
664 break;
665
666 found = 1;
667 cmdid = TWE_INL(sc, TWE_REG_RESP_QUEUE);
668 cmdid = (cmdid & TWE_RESP_MASK) >> TWE_RESP_SHIFT;
669 if (cmdid >= TWE_MAX_QUEUECNT) {
670 printf("%s: bad completion\n", sc->sc_dv.dv_xname);
671 continue;
672 }
673
674 ccb = sc->sc_ccbs + cmdid;
675 if ((ccb->ccb_flags & TWE_CCB_ACTIVE) == 0) {
676 printf("%s: bad completion (not active)\n",
677 sc->sc_dv.dv_xname);
678 continue;
679 }
680 ccb->ccb_flags ^= TWE_CCB_COMPLETE | TWE_CCB_ACTIVE;
681
682 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
683 (caddr_t)ccb->ccb_cmd - sc->sc_cmds,
684 sizeof(struct twe_cmd),
685 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE);
686
687 /* Pass notification to upper layers. */
688 if (ccb->ccb_tx.tx_handler != NULL)
689 (*ccb->ccb_tx.tx_handler)(ccb,
690 ccb->ccb_cmd->tc_status != 0 ? EIO : 0);
691 }
692
693 /* If any commands have completed, run the software queue. */
694 if (found)
695 twe_ccb_enqueue(sc, NULL);
696 }
697
698 /*
699 * Wait for `status' to be set in the controller status register. Return
700 * zero if found, non-zero if the operation timed out.
701 */
702 static int
703 twe_status_wait(struct twe_softc *sc, u_int32_t status, int timo)
704 {
705
706 for (; timo != 0; timo--) {
707 if ((TWE_INL(sc, TWE_REG_STS) & status) == status)
708 break;
709 delay(100000);
710 }
711
712 return (timo == 0);
713 }
714
715 /*
716 * Complain if the status bits aren't what we expect.
717 */
718 static int
719 twe_status_check(struct twe_softc *sc, u_int status)
720 {
721 int rv;
722
723 rv = 0;
724
725 if ((status & TWE_STS_EXPECTED_BITS) != TWE_STS_EXPECTED_BITS) {
726 printf("%s: missing status bits: 0x%08x\n", sc->sc_dv.dv_xname,
727 status & ~TWE_STS_EXPECTED_BITS);
728 rv = -1;
729 }
730
731 if ((status & TWE_STS_UNEXPECTED_BITS) != 0) {
732 printf("%s: unexpected status bits: 0x%08x\n",
733 sc->sc_dv.dv_xname, status & TWE_STS_UNEXPECTED_BITS);
734 rv = -1;
735 }
736
737 return (rv);
738 }
739
740 /*
741 * Allocate and initialise a CCB.
742 */
743 int
744 twe_ccb_alloc(struct twe_softc *sc, struct twe_ccb **ccbp, int flags)
745 {
746 struct twe_cmd *tc;
747 struct twe_ccb *ccb;
748 int s;
749
750 s = splbio();
751 if ((flags & TWE_CCB_PARAM) != 0)
752 ccb = sc->sc_ccbs;
753 else {
754 /* Allocate a CCB and command block. */
755 if (SLIST_FIRST(&sc->sc_ccb_freelist) == NULL) {
756 splx(s);
757 return (EAGAIN);
758 }
759 ccb = SLIST_FIRST(&sc->sc_ccb_freelist);
760 SLIST_REMOVE_HEAD(&sc->sc_ccb_freelist, ccb_chain.slist);
761 }
762 #ifdef DIAGNOSTIC
763 if ((ccb->ccb_flags & TWE_CCB_ALLOCED) != 0)
764 panic("twe_ccb_alloc: CCB already allocated");
765 flags |= TWE_CCB_ALLOCED;
766 #endif
767 splx(s);
768
769 /* Initialise some fields and return. */
770 ccb->ccb_tx.tx_handler = NULL;
771 ccb->ccb_flags = flags;
772 tc = ccb->ccb_cmd;
773 tc->tc_status = 0;
774 tc->tc_flags = 0;
775 tc->tc_cmdid = ccb->ccb_cmdid;
776 *ccbp = ccb;
777
778 return (0);
779 }
780
781 /*
782 * Free a CCB.
783 */
784 void
785 twe_ccb_free(struct twe_softc *sc, struct twe_ccb *ccb)
786 {
787 int s;
788
789 s = splbio();
790 if ((ccb->ccb_flags & TWE_CCB_PARAM) == 0)
791 SLIST_INSERT_HEAD(&sc->sc_ccb_freelist, ccb, ccb_chain.slist);
792 ccb->ccb_flags = 0;
793 splx(s);
794 }
795
796 /*
797 * Map the specified CCB's command block and data buffer (if any) into
798 * controller visible space. Perform DMA synchronisation.
799 */
800 int
801 twe_ccb_map(struct twe_softc *sc, struct twe_ccb *ccb)
802 {
803 struct twe_cmd *tc;
804 int flags, nsegs, i, s, rv;
805 void *data;
806
807 /*
808 * The data as a whole must be 512-byte aligned.
809 */
810 if (((u_long)ccb->ccb_data & (TWE_ALIGNMENT - 1)) != 0) {
811 s = splvm();
812 /* XXX */
813 ccb->ccb_abuf = uvm_km_kmemalloc(kmem_map, uvmexp.kmem_object,
814 ccb->ccb_datasize, UVM_KMF_NOWAIT);
815 splx(s);
816 data = (void *)ccb->ccb_abuf;
817 if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
818 memcpy(data, ccb->ccb_data, ccb->ccb_datasize);
819 } else {
820 ccb->ccb_abuf = (vaddr_t)0;
821 data = ccb->ccb_data;
822 }
823
824 /*
825 * Map the data buffer into bus space and build the S/G list.
826 */
827 rv = bus_dmamap_load(sc->sc_dmat, ccb->ccb_dmamap_xfer, data,
828 ccb->ccb_datasize, NULL, BUS_DMA_NOWAIT);
829 if (rv != 0) {
830 if (ccb->ccb_abuf != (vaddr_t)0) {
831 s = splvm();
832 /* XXX */
833 uvm_km_free(kmem_map, ccb->ccb_abuf,
834 ccb->ccb_datasize);
835 splx(s);
836 }
837 return (rv);
838 }
839
840 nsegs = ccb->ccb_dmamap_xfer->dm_nsegs;
841 tc = ccb->ccb_cmd;
842 tc->tc_size += 2 * nsegs;
843
844 /* The location of the S/G list is dependant upon command type. */
845 switch (tc->tc_opcode >> 5) {
846 case 2:
847 for (i = 0; i < nsegs; i++) {
848 tc->tc_args.param.sgl[i].tsg_address =
849 htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
850 tc->tc_args.param.sgl[i].tsg_length =
851 htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
852 }
853 /* XXX Needed? */
854 for (; i < TWE_SG_SIZE; i++) {
855 tc->tc_args.param.sgl[i].tsg_address = 0;
856 tc->tc_args.param.sgl[i].tsg_length = 0;
857 }
858 break;
859 case 3:
860 for (i = 0; i < nsegs; i++) {
861 tc->tc_args.io.sgl[i].tsg_address =
862 htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_addr);
863 tc->tc_args.io.sgl[i].tsg_length =
864 htole32(ccb->ccb_dmamap_xfer->dm_segs[i].ds_len);
865 }
866 /* XXX Needed? */
867 for (; i < TWE_SG_SIZE; i++) {
868 tc->tc_args.io.sgl[i].tsg_address = 0;
869 tc->tc_args.io.sgl[i].tsg_length = 0;
870 }
871 break;
872 #ifdef DEBUG
873 default:
874 panic("twe_ccb_map: oops");
875 #endif
876 }
877
878 if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
879 flags = BUS_DMASYNC_PREREAD;
880 else
881 flags = 0;
882 if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
883 flags |= BUS_DMASYNC_PREWRITE;
884
885 bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
886 ccb->ccb_datasize, flags);
887 return (0);
888 }
889
890 /*
891 * Unmap the specified CCB's command block and data buffer (if any) and
892 * perform DMA synchronisation.
893 */
894 void
895 twe_ccb_unmap(struct twe_softc *sc, struct twe_ccb *ccb)
896 {
897 int flags, s;
898
899 if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
900 flags = BUS_DMASYNC_POSTREAD;
901 else
902 flags = 0;
903 if ((ccb->ccb_flags & TWE_CCB_DATA_OUT) != 0)
904 flags |= BUS_DMASYNC_POSTWRITE;
905
906 bus_dmamap_sync(sc->sc_dmat, ccb->ccb_dmamap_xfer, 0,
907 ccb->ccb_datasize, flags);
908 bus_dmamap_unload(sc->sc_dmat, ccb->ccb_dmamap_xfer);
909
910 if (ccb->ccb_abuf != (vaddr_t)0) {
911 if ((ccb->ccb_flags & TWE_CCB_DATA_IN) != 0)
912 memcpy(ccb->ccb_data, (void *)ccb->ccb_abuf,
913 ccb->ccb_datasize);
914 s = splvm();
915 /* XXX */
916 uvm_km_free(kmem_map, ccb->ccb_abuf, ccb->ccb_datasize);
917 splx(s);
918 }
919 }
920
921 /*
922 * Submit a command to the controller and poll on completion. Return
923 * non-zero on timeout (but don't check status, as some command types don't
924 * return status). Must be called with interrupts blocked.
925 */
926 int
927 twe_ccb_poll(struct twe_softc *sc, struct twe_ccb *ccb, int timo)
928 {
929 int rv;
930
931 if ((rv = twe_ccb_submit(sc, ccb)) != 0)
932 return (rv);
933
934 for (; timo != 0; timo--) {
935 twe_poll(sc);
936 if ((ccb->ccb_flags & TWE_CCB_COMPLETE) != 0)
937 break;
938 DELAY(100000);
939 }
940
941 return (timo == 0);
942 }
943
944 /*
945 * If a CCB is specified, enqueue it. Pull CCBs off the software queue in
946 * the order that they were enqueued and try to submit their command blocks
947 * to the controller for execution.
948 */
949 void
950 twe_ccb_enqueue(struct twe_softc *sc, struct twe_ccb *ccb)
951 {
952 int s;
953
954 s = splbio();
955
956 if (ccb != NULL)
957 SIMPLEQ_INSERT_TAIL(&sc->sc_ccb_queue, ccb, ccb_chain.simpleq);
958
959 while ((ccb = SIMPLEQ_FIRST(&sc->sc_ccb_queue)) != NULL) {
960 if (twe_ccb_submit(sc, ccb))
961 break;
962 SIMPLEQ_REMOVE_HEAD(&sc->sc_ccb_queue, ccb, ccb_chain.simpleq);
963 }
964
965 splx(s);
966 }
967
968 /*
969 * Submit the command block associated with the specified CCB to the
970 * controller for execution. Must be called with interrupts blocked.
971 */
972 int
973 twe_ccb_submit(struct twe_softc *sc, struct twe_ccb *ccb)
974 {
975 bus_addr_t pa;
976 int rv;
977 u_int status;
978
979 /* Check to see if we can post a command. */
980 status = TWE_INL(sc, TWE_REG_STS);
981 twe_status_check(sc, status);
982
983 if ((status & TWE_STS_CMD_QUEUE_FULL) == 0) {
984 bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap,
985 (caddr_t)ccb->ccb_cmd - sc->sc_cmds, sizeof(struct twe_cmd),
986 BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
987 ccb->ccb_flags |= TWE_CCB_ACTIVE;
988 pa = sc->sc_cmds_paddr +
989 ccb->ccb_cmdid * sizeof(struct twe_cmd);
990 TWE_OUTL(sc, TWE_REG_CMD_QUEUE, (u_int32_t)pa);
991 rv = 0;
992 } else
993 rv = EBUSY;
994
995 return (rv);
996 }
997