Deleted Added
full compact
twe.c (73104) twe.c (76340)
1/*-
2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
1/*-
2 * Copyright (c) 2000 Michael Smith
3 * Copyright (c) 2000 BSDi
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 *
27 * $FreeBSD: head/sys/dev/twe/twe.c 73104 2001-02-26 20:13:19Z msmith $
27 * $FreeBSD: head/sys/dev/twe/twe.c 76340 2001-05-07 21:46:44Z msmith $
28 */
29
30/*
31 * Driver for the 3ware Escalade family of IDE RAID controllers.
32 */
33
34#include <dev/twe/twe_compat.h>
35#include <dev/twe/twereg.h>
36#include <dev/twe/tweio.h>
37#include <dev/twe/twevar.h>
38#define TWE_DEFINE_TABLES
39#include <dev/twe/twe_tables.h>
40
41/*
42 * Command submission.
43 */
44static int twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result);
45static int twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result);
46static int twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result);
47static void *twe_get_param(struct twe_softc *sc, int table_id, int parameter_id, size_t size,
48 void (* func)(struct twe_request *tr));
49static int twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value);
50static int twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value);
51static int twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value);
52static int twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size,
53 void *data);
54static int twe_init_connection(struct twe_softc *sc, int mode);
55static int twe_wait_request(struct twe_request *tr);
56static int twe_immediate_request(struct twe_request *tr);
57static void twe_completeio(struct twe_request *tr);
58static void twe_reset(struct twe_softc *sc);
59
60/*
61 * Command I/O to controller.
62 */
63static int twe_start(struct twe_request *tr);
64static void twe_done(struct twe_softc *sc);
65static void twe_complete(struct twe_softc *sc);
66static int twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout);
67static int twe_drain_response_queue(struct twe_softc *sc);
68static int twe_check_bits(struct twe_softc *sc, u_int32_t status_reg);
69static int twe_soft_reset(struct twe_softc *sc);
70
71/*
72 * Interrupt handling.
73 */
74static void twe_host_intr(struct twe_softc *sc);
75static void twe_attention_intr(struct twe_softc *sc);
76static void twe_command_intr(struct twe_softc *sc);
77
78/*
79 * Asynchronous event handling.
80 */
81static int twe_fetch_aen(struct twe_softc *sc);
82static void twe_handle_aen(struct twe_request *tr);
83static void twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen);
84static int twe_dequeue_aen(struct twe_softc *sc);
85static int twe_drain_aen_queue(struct twe_softc *sc);
86static int twe_find_aen(struct twe_softc *sc, u_int16_t aen);
87
88/*
89 * Command buffer management.
90 */
91static int twe_get_request(struct twe_softc *sc, struct twe_request **tr);
92static void twe_release_request(struct twe_request *tr);
93
94/*
95 * Debugging.
96 */
97static char *twe_format_aen(struct twe_softc *sc, u_int16_t aen);
98static int twe_report_request(struct twe_request *tr);
99static void twe_panic(struct twe_softc *sc, char *reason);
100
101/********************************************************************************
102 ********************************************************************************
103 Public Interfaces
104 ********************************************************************************
105 ********************************************************************************/
106
107/********************************************************************************
108 * Initialise the controller, set up driver data structures.
109 */
110int
111twe_setup(struct twe_softc *sc)
112{
113 struct twe_request *tr;
114 int i;
115
116 debug_called(4);
117
118 /*
119 * Initialise request queues.
120 */
121 twe_initq_free(sc);
122 twe_initq_bio(sc);
123 twe_initq_ready(sc);
124 twe_initq_busy(sc);
125 twe_initq_complete(sc);
126 sc->twe_wait_aen = -1;
127
128 /*
129 * Allocate request structures up front.
130 */
131 for (i = 0; i < TWE_Q_LENGTH; i++) {
132 if ((tr = twe_allocate_request(sc)) == NULL)
133 return(ENOMEM);
134 /*
135 * Set global defaults that won't change.
136 */
137 tr->tr_command.generic.host_id = sc->twe_host_id; /* controller-assigned host ID */
138 tr->tr_command.generic.request_id = i; /* our index number */
139 sc->twe_lookup[i] = tr;
140
141 /*
142 * Put command onto the freelist.
143 */
144 twe_release_request(tr);
145 }
146
147 /*
148 * Wait for the controller to come ready.
149 */
150 if (twe_wait_status(sc, TWE_STATUS_MICROCONTROLLER_READY, 60)) {
151 twe_printf(sc, "microcontroller not ready\n");
152 return(ENXIO);
153 }
154
155 /*
156 * Disable interrupts from the card.
157 */
158 twe_disable_interrupts(sc);
159
160 /*
161 * Soft reset the controller, look for the AEN acknowledging the reset,
162 * check for errors, drain the response queue.
163 */
164 for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
165
166 if (i > 0)
167 twe_printf(sc, "reset %d failed, trying again\n", i);
168
169 if (!twe_soft_reset(sc))
170 break; /* reset process complete */
171 }
172 /* did we give up? */
173 if (i >= TWE_MAX_RESET_TRIES) {
174 twe_printf(sc, "can't initialise controller, giving up\n");
175 return(ENXIO);
176 }
177
178 return(0);
179}
180
181/********************************************************************************
182 * Locate disk devices and attach children to them.
183 */
184void
185twe_init(struct twe_softc *sc)
186{
187 struct twe_drive *dr;
188 int i, table;
189 u_int16_t dsize;
190 TWE_Param *drives, *param;
191 TWE_Unit_Descriptor *ud;
192
193
194 /*
195 * The controller is in a safe state, so try to find drives attached to it.
196 */
197 if ((drives = twe_get_param(sc, TWE_PARAM_UNITSUMMARY, TWE_PARAM_UNITSUMMARY_Status,
198 TWE_MAX_UNITS, NULL)) == NULL) {
199 twe_printf(sc, "can't detect attached units\n");
200 return;
201 }
202
203 /*
204 * For each detected unit, create a child device.
205 */
206 for (i = 0, dr = &sc->twe_drive[0]; i < TWE_MAX_UNITS; i++, dr++) {
207
208 /* check that the drive is online */
209 if (!(drives->data[i] & TWE_PARAM_UNITSTATUS_Online))
210 continue;
211
212 table = TWE_PARAM_UNITINFO + i;
213
214 if (twe_get_param_4(sc, table, TWE_PARAM_UNITINFO_Capacity, &dr->td_size)) {
215 twe_printf(sc, "error fetching capacity for unit %d\n", i);
216 continue;
217 }
218 if (twe_get_param_1(sc, table, TWE_PARAM_UNITINFO_Status, &dr->td_state)) {
219 twe_printf(sc, "error fetching state for unit %d\n", i);
220 continue;
221 }
222 if (twe_get_param_2(sc, table, TWE_PARAM_UNITINFO_DescriptorSize, &dsize)) {
223 twe_printf(sc, "error fetching descriptor size for unit %d\n", i);
224 continue;
225 }
226 if ((param = twe_get_param(sc, table, TWE_PARAM_UNITINFO_Descriptor, dsize - 3, NULL)) == NULL) {
227 twe_printf(sc, "error fetching descriptor for unit %d\n", i);
228 continue;
229 }
230 ud = (TWE_Unit_Descriptor *)param->data;
231 dr->td_type = ud->configuration;
232 free(param, M_DEVBUF);
233
234 /* build synthetic geometry as per controller internal rules */
235 if (dr->td_size > 0x200000) {
236 dr->td_heads = 255;
237 dr->td_sectors = 63;
238 } else {
239 dr->td_heads = 64;
240 dr->td_sectors = 32;
241 }
242 dr->td_cylinders = dr->td_size / (dr->td_heads * dr->td_sectors);
243 dr->td_unit = i;
244
245 twe_attach_drive(sc, dr);
246 }
247 free(drives, M_DEVBUF);
248
249 /*
250 * Initialise connection with controller.
251 */
252 twe_init_connection(sc, TWE_INIT_MESSAGE_CREDITS);
253
254#ifdef TWE_SHUTDOWN_NOTIFICATION
255 /*
256 * Tell the controller we support shutdown notification.
257 */
258 twe_set_param_1(sc, TWE_PARAM_FEATURES, TWE_PARAM_FEATURES_DriverShutdown, 1);
259#endif
260
261 /*
262 * Mark controller up and ready to run.
263 */
264 sc->twe_state &= ~TWE_STATE_SHUTDOWN;
265
266 /*
267 * Finally enable interrupts.
268 */
269 twe_enable_interrupts(sc);
270}
271
272/********************************************************************************
273 * Stop the controller
274 */
275void
276twe_deinit(struct twe_softc *sc)
277{
278 /*
279 * Mark the controller as shutting down, and disable any further interrupts.
280 */
281 sc->twe_state |= TWE_STATE_SHUTDOWN;
282 twe_disable_interrupts(sc);
283
284#ifdef TWE_SHUTDOWN_NOTIFICATION
285 /*
286 * Disconnect from the controller
287 */
288 twe_init_connection(sc, TWE_SHUTDOWN_MESSAGE_CREDITS);
289#endif
290}
291
292/*******************************************************************************
293 * Take an interrupt, or be poked by other code to look for interrupt-worthy
294 * status.
295 */
296void
297twe_intr(struct twe_softc *sc)
298{
299 u_int32_t status_reg;
300
301 debug_called(4);
302
303 /*
304 * Collect current interrupt status.
305 */
306 status_reg = TWE_STATUS(sc);
307 twe_check_bits(sc, status_reg);
308
309 /*
310 * Dispatch based on interrupt status
311 */
312 if (status_reg & TWE_STATUS_HOST_INTERRUPT)
313 twe_host_intr(sc);
314 if (status_reg & TWE_STATUS_ATTENTION_INTERRUPT)
315 twe_attention_intr(sc);
316 if (status_reg & TWE_STATUS_COMMAND_INTERRUPT)
317 twe_command_intr(sc);
318 if (status_reg & TWE_STATUS_RESPONSE_INTERRUPT)
319 twe_done(sc);
320};
321
322/********************************************************************************
323 * Pull as much work off the softc's work queue as possible and give it to the
324 * controller.
325 */
326void
327twe_startio(struct twe_softc *sc)
328{
329 struct twe_request *tr;
330 TWE_Command *cmd;
331 twe_bio *bp;
332 int error;
333
334 debug_called(4);
335
336 /* spin until something prevents us from doing any work */
337 for (;;) {
338
339 /* try to get a command that's already ready to go */
340 tr = twe_dequeue_ready(sc);
341
342 /* build a command from an outstanding bio */
343 if (tr == NULL) {
344
345 /* see if there's work to be done */
346 if ((bp = twe_dequeue_bio(sc)) == NULL)
347 break;
348
349 /* get a command to handle the bio with */
350 if (twe_get_request(sc, &tr)) {
351 twe_enqueue_bio(sc, bp); /* failed, put the bio back */
352 break;
353 }
354
355 /* connect the bio to the command */
356 tr->tr_complete = twe_completeio;
357 tr->tr_private = bp;
358 tr->tr_data = TWE_BIO_DATA(bp);
359 tr->tr_length = TWE_BIO_LENGTH(bp);
360 cmd = &tr->tr_command;
361 if (TWE_BIO_IS_READ(bp)) {
362 tr->tr_flags |= TWE_CMD_DATAIN;
363 cmd->io.opcode = TWE_OP_READ;
364 } else {
365 tr->tr_flags |= TWE_CMD_DATAOUT;
366 cmd->io.opcode = TWE_OP_WRITE;
367 }
368
369 /* build a suitable I/O command (assumes 512-byte rounded transfers) */
370 cmd->io.size = 3;
371 cmd->io.unit = TWE_BIO_UNIT(bp);
372 cmd->io.block_count = (tr->tr_length + TWE_BLOCK_SIZE - 1) / TWE_BLOCK_SIZE;
373 cmd->io.lba = TWE_BIO_LBA(bp);
374
375 /* map the command so the controller can work with it */
376 twe_map_request(tr);
377 }
378
379 /* did we find something to do? */
380 if (tr == NULL)
381 break;
382
383 /* try to give command to controller */
384 error = twe_start(tr);
385
386 if (error != 0) {
387 if (error == EBUSY) {
388 twe_requeue_ready(tr); /* try it again later */
389 break; /* don't try anything more for now */
390 }
391 /* we don't support any other return from twe_start */
392 twe_panic(sc, "twe_start returned nonsense");
393 }
394 }
395}
396
397/********************************************************************************
398 * Write blocks from memory to disk, for system crash dumps.
399 */
400int
401twe_dump_blocks(struct twe_softc *sc, int unit, u_int32_t lba, void *data, int nblks)
402{
403 struct twe_request *tr;
404 TWE_Command *cmd;
405 int error;
406
407 if (twe_get_request(sc, &tr))
408 return(ENOMEM);
409
410 tr->tr_data = data;
411 tr->tr_status = TWE_CMD_SETUP;
412 tr->tr_length = nblks * TWE_BLOCK_SIZE;
413 tr->tr_flags = TWE_CMD_DATAOUT;
414
415 cmd = &tr->tr_command;
416 cmd->io.opcode = TWE_OP_WRITE;
417 cmd->io.size = 3;
418 cmd->io.unit = unit;
419 cmd->io.block_count = nblks;
420 cmd->io.lba = lba;
421
422 twe_map_request(tr);
423 error = twe_immediate_request(tr);
424 if (error == 0)
425 if (twe_report_request(tr))
426 error = EIO;
427 twe_release_request(tr);
428 return(error);
429}
430
431/********************************************************************************
432 * Handle controller-specific control operations.
433 */
434int
435twe_ioctl(struct twe_softc *sc, int cmd, void *addr)
436{
437 struct twe_usercommand *tu = (struct twe_usercommand *)addr;
438 struct twe_paramcommand *tp = (struct twe_paramcommand *)addr;
439 union twe_statrequest *ts = (union twe_statrequest *)addr;
440 TWE_Param *param;
441 void *data;
442 int *arg = (int *)addr;
443 struct twe_request *tr;
444 u_int8_t srid;
445 int s, error;
446
447 error = 0;
448 switch(cmd) {
449 /* handle a command from userspace */
450 case TWEIO_COMMAND:
451 /* get a request */
452 if (twe_get_request(sc, &tr)) {
453 error = EBUSY;
454 goto cmd_done;
455 }
456
457 /*
458 * Save the command's request ID, copy the user-supplied command in,
459 * restore the request ID.
460 */
461 srid = tr->tr_command.generic.request_id;
462 bcopy(&tu->tu_command, &tr->tr_command, sizeof(TWE_Command));
463 tr->tr_command.generic.request_id = srid;
464
465 /* if there's a data buffer, allocate and copy it in */
466 tr->tr_length = tu->tu_size;
467 if (tr->tr_length > 0) {
468 if ((tr->tr_data = malloc(tr->tr_length, M_DEVBUF, M_WAITOK)) == NULL) {
469 error = ENOMEM;
470 goto cmd_done;
471 }
472 if ((error = copyin(tu->tu_data, tr->tr_data, tr->tr_length)) != 0)
473 goto cmd_done;
474 tr->tr_flags |= TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
475 }
476
477 /* run the command */
478 twe_map_request(tr);
479 twe_wait_request(tr);
480
481 /* copy the command out again */
482 bcopy(&tr->tr_command, &tu->tu_command, sizeof(TWE_Command));
483
484 /* if there was a data buffer, copy it out */
485 if (tr->tr_length > 0)
486 error = copyout(tr->tr_data, tu->tu_data, tr->tr_length);
487
488 cmd_done:
489 /* free resources */
490 if (tr->tr_data != NULL)
491 free(tr->tr_data, M_DEVBUF);
492 if (tr != NULL)
493 twe_release_request(tr);
494
495 break;
496
497 /* fetch statistics counter */
498 case TWEIO_STATS:
499 switch (ts->ts_item) {
500#ifdef TWE_PERFORMANCE_MONITOR
501 case TWEQ_FREE:
502 case TWEQ_BIO:
503 case TWEQ_READY:
504 case TWEQ_BUSY:
505 case TWEQ_COMPLETE:
506 bcopy(&sc->twe_qstat[ts->ts_item], &ts->ts_qstat, sizeof(struct twe_qstat));
507 break;
508#endif
509 default:
510 error = ENOENT;
511 break;
512 }
513 break;
514
515 /* poll for an AEN */
516 case TWEIO_AEN_POLL:
517 *arg = twe_dequeue_aen(sc);
518 if (*arg == -1)
519 error = ENOENT;
520 break;
521
522 /* wait for another AEN to show up */
523 case TWEIO_AEN_WAIT:
524 s = splbio();
525 while ((*arg = twe_dequeue_aen(sc)) == -1) {
526 error = tsleep(&sc->twe_aen_queue, PRIBIO | PCATCH, "tweaen", 0);
527 if (error == EINTR)
528 break;
529 }
530 splx(s);
531 break;
532
533 case TWEIO_GET_PARAM:
534 if ((param = twe_get_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, NULL)) == NULL) {
535 twe_printf(sc, "TWEIO_GET_PARAM failed for 0x%x/0x%x/%d\n",
536 tp->tp_table_id, tp->tp_param_id, tp->tp_size);
537 error = EINVAL;
538 } else {
539 if (param->parameter_size_bytes > tp->tp_size) {
540 twe_printf(sc, "TWEIO_GET_PARAM parameter too large (%d > %d)\n",
541 param->parameter_size_bytes, tp->tp_size);
542 error = EFAULT;
543 } else {
544 error = copyout(param->data, tp->tp_data, param->parameter_size_bytes);
545 }
546 free(param, M_DEVBUF);
547 }
548 break;
549
550 case TWEIO_SET_PARAM:
551 if ((data = malloc(tp->tp_size, M_DEVBUF, M_WAITOK)) == NULL) {
552 error = ENOMEM;
553 } else {
554 error = copyin(tp->tp_data, data, tp->tp_size);
555 if (error == 0)
556 error = twe_set_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, data);
557 free(data, M_DEVBUF);
558 }
559 break;
560
561 case TWEIO_RESET:
562 twe_reset(sc);
563 break;
564
565 /* nothing we understand */
566 default:
567 error = ENOTTY;
568 }
569
570 return(error);
571}
572
573/********************************************************************************
574 * Enable the useful interrupts from the controller.
575 */
576void
577twe_enable_interrupts(struct twe_softc *sc)
578{
579 sc->twe_state |= TWE_STATE_INTEN;
580 TWE_CONTROL(sc,
581 TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT |
582 TWE_CONTROL_UNMASK_RESPONSE_INTERRUPT |
583 TWE_CONTROL_ENABLE_INTERRUPTS);
584}
585
586/********************************************************************************
587 * Disable interrupts from the controller.
588 */
589void
590twe_disable_interrupts(struct twe_softc *sc)
591{
592 TWE_CONTROL(sc, TWE_CONTROL_DISABLE_INTERRUPTS);
593 sc->twe_state &= ~TWE_STATE_INTEN;
594}
595
596/********************************************************************************
597 ********************************************************************************
598 Command Submission
599 ********************************************************************************
600 ********************************************************************************/
601
602/********************************************************************************
603 * Read integer parameter table entries.
604 */
605static int
606twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result)
607{
608 TWE_Param *param;
609
610 if ((param = twe_get_param(sc, table_id, param_id, 1, NULL)) == NULL)
611 return(ENOENT);
612 *result = *(u_int8_t *)param->data;
613 free(param, M_DEVBUF);
614 return(0);
615}
616
617static int
618twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result)
619{
620 TWE_Param *param;
621
622 if ((param = twe_get_param(sc, table_id, param_id, 2, NULL)) == NULL)
623 return(ENOENT);
624 *result = *(u_int16_t *)param->data;
625 free(param, M_DEVBUF);
626 return(0);
627}
628
629static int
630twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result)
631{
632 TWE_Param *param;
633
634 if ((param = twe_get_param(sc, table_id, param_id, 4, NULL)) == NULL)
635 return(ENOENT);
636 *result = *(u_int32_t *)param->data;
637 free(param, M_DEVBUF);
638 return(0);
639}
640
641/********************************************************************************
642 * Perform a TWE_OP_GET_PARAM command. If a callback function is provided, it
643 * will be called with the command when it's completed. If no callback is
644 * provided, we will wait for the command to complete and then return just the data.
645 * The caller is responsible for freeing the data when done with it.
646 */
647static void *
648twe_get_param(struct twe_softc *sc, int table_id, int param_id, size_t param_size,
649 void (* func)(struct twe_request *tr))
650{
651 struct twe_request *tr;
652 TWE_Command *cmd;
653 TWE_Param *param;
654 int error;
655
656 debug_called(4);
657
658 tr = NULL;
659 param = NULL;
660
661 /* get a command */
662 if (twe_get_request(sc, &tr))
663 goto err;
664
665 /* get a buffer */
666 if ((param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
667 goto err;
668 tr->tr_data = param;
669 tr->tr_length = TWE_SECTOR_SIZE;
670 tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
671
672 /* build the command for the controller */
673 cmd = &tr->tr_command;
674 cmd->param.opcode = TWE_OP_GET_PARAM;
675 cmd->param.size = 2;
676 cmd->param.unit = 0;
677 cmd->param.param_count = 1;
678
679 /* map the command/data into controller-visible space */
680 twe_map_request(tr);
681
682 /* fill in the outbound parameter data */
683 param->table_id = table_id;
684 param->parameter_id = param_id;
685 param->parameter_size_bytes = param_size;
686
687 /* submit the command and either wait or let the callback handle it */
688 if (func == NULL) {
689 /* XXX could use twe_wait_request here if interrupts were enabled? */
690 error = twe_immediate_request(tr);
691 if (error == 0) {
692 if (twe_report_request(tr))
693 goto err;
694 }
695 twe_release_request(tr);
696 return(param);
697 } else {
698 tr->tr_complete = func;
699 error = twe_start(tr);
700 if (error == 0)
701 return(func);
702 }
703
704 /* something failed */
705err:
706 debug(1, "failed");
707 if (tr != NULL)
708 twe_release_request(tr);
709 if (param != NULL)
710 free(param, M_DEVBUF);
711 return(NULL);
712}
713
714/********************************************************************************
715 * Set integer parameter table entries.
716 */
717static int
718twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value)
719{
720 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
721}
722
723static int
724twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value)
725{
726 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
727}
728
729static int
730twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value)
731{
732 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
733}
734
735/********************************************************************************
736 * Perform a TWE_OP_SET_PARAM command, returns nonzero on error.
737 */
738static int
739twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size, void *data)
740{
741 struct twe_request *tr;
742 TWE_Command *cmd;
743 TWE_Param *param;
744 int error;
745
746 debug_called(4);
747
748 tr = NULL;
749 param = NULL;
750 error = ENOMEM;
751
752 /* get a command */
753 if (twe_get_request(sc, &tr))
754 goto out;
755
756 /* get a buffer */
757 if ((param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
758 goto out;
759 tr->tr_data = param;
760 tr->tr_length = TWE_SECTOR_SIZE;
761 tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
762
763 /* build the command for the controller */
764 cmd = &tr->tr_command;
765 cmd->param.opcode = TWE_OP_SET_PARAM;
766 cmd->param.size = 2;
767 cmd->param.unit = 0;
768 cmd->param.param_count = 1;
769
770 /* map the command/data into controller-visible space */
771 twe_map_request(tr);
772
773 /* fill in the outbound parameter data */
774 param->table_id = table_id;
775 param->parameter_id = param_id;
776 param->parameter_size_bytes = param_size;
777 bcopy(data, param->data, param_size);
778
779 /* XXX could use twe_wait_request here if interrupts were enabled? */
780 error = twe_immediate_request(tr);
781 if (error == 0) {
782 if (twe_report_request(tr))
783 error = EIO;
784 }
785
786out:
787 if (tr != NULL)
788 twe_release_request(tr);
789 if (param != NULL)
790 free(param, M_DEVBUF);
791 return(error);
792}
793
794/********************************************************************************
795 * Perform a TWE_OP_INIT_CONNECTION command, returns nonzero on error.
796 *
797 * Typically called with interrupts disabled.
798 */
799static int
800twe_init_connection(struct twe_softc *sc, int mode)
801{
802 struct twe_request *tr;
803 TWE_Command *cmd;
804 int error;
805
806 debug_called(4);
807
808 /* get a command */
809 if (twe_get_request(sc, &tr))
810 return(NULL);
811
812 /* build the command */
813 cmd = &tr->tr_command;
814 cmd->initconnection.opcode = TWE_OP_INIT_CONNECTION;
815 cmd->initconnection.size = 3;
816 cmd->initconnection.host_id = 0;
817 cmd->initconnection.message_credits = mode;
818 cmd->initconnection.response_queue_pointer = 0;
819
820 /* map the command into controller-visible space */
821 twe_map_request(tr);
822
823 /* submit the command */
824 error = twe_immediate_request(tr);
825 /* XXX check command result? */
826 twe_unmap_request(tr);
827 twe_release_request(tr);
828
829 if (mode == TWE_INIT_MESSAGE_CREDITS)
830 sc->twe_host_id = cmd->initconnection.host_id;
831 return(error);
832}
833
834/********************************************************************************
835 * Start the command (tr) and sleep waiting for it to complete.
836 *
837 * Successfully completed commands are dequeued.
838 */
839static int
840twe_wait_request(struct twe_request *tr)
841{
842 int s;
843
844 debug_called(4);
845
846 tr->tr_flags |= TWE_CMD_SLEEPER;
847 tr->tr_status = TWE_CMD_BUSY;
848 twe_enqueue_ready(tr);
849 twe_startio(tr->tr_sc);
850 s = splbio();
851 while (tr->tr_status == TWE_CMD_BUSY)
852 tsleep(tr, PRIBIO, "twewait", 0);
853 splx(s);
854
855 return(0);
856}
857
858/********************************************************************************
859 * Start the command (tr) and busy-wait for it to complete.
860 * This should only be used when interrupts are actually disabled (although it
861 * will work if they are not).
862 */
863static int
864twe_immediate_request(struct twe_request *tr)
865{
866 int error;
867
868 debug_called(4);
869
870 error = 0;
871
872 if ((error = twe_start(tr)) != 0)
873 return(error);
874 while (tr->tr_status == TWE_CMD_BUSY){
875 twe_done(tr->tr_sc);
876 }
877 return(tr->tr_status != TWE_CMD_COMPLETE);
878}
879
880/********************************************************************************
881 * Handle completion of an I/O command.
882 */
883static void
884twe_completeio(struct twe_request *tr)
885{
886 struct twe_softc *sc = tr->tr_sc;
887 twe_bio *bp = (twe_bio *)tr->tr_private;
888
889 debug_called(4);
890
891 if (tr->tr_status == TWE_CMD_COMPLETE) {
892
893 if (twe_report_request(tr))
894 TWE_BIO_SET_ERROR(bp, EIO);
895
896 } else {
897 twe_panic(sc, "twe_completeio on incomplete command");
898 }
899 tr->tr_private = NULL;
900 twed_intr(bp);
901 twe_release_request(tr);
902}
903
904/********************************************************************************
905 * Reset the controller and pull all the active commands back onto the ready
906 * queue. Used to restart a controller that's exhibiting bad behaviour.
907 */
908static void
909twe_reset(struct twe_softc *sc)
910{
911 struct twe_request *tr;
912 int i, s;
913
914 twe_printf(sc, "controller reset in progress...\n");
915
916 /*
917 * Disable interrupts from the controller, and mask any accidental entry
918 * into our interrupt handler.
919 */
920 twe_disable_interrupts(sc);
921 s = splbio();
922
923 /*
924 * Try to soft-reset the controller.
925 */
926 for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
927
928 if (i > 0)
929 twe_printf(sc, "reset %d failed, trying again\n", i);
930
931 if (!twe_soft_reset(sc))
932 break; /* reset process complete */
933 }
934 /* did we give up? */
935 if (i >= TWE_MAX_RESET_TRIES) {
936 twe_printf(sc, "can't reset controller, giving up\n");
937 goto out;
938 }
939
940 /*
941 * Move all of the commands that were busy back to the ready queue.
942 */
943 i = 0;
944 while ((tr = twe_dequeue_busy(sc)) != NULL) {
945 twe_enqueue_ready(tr);
946 i++;
947 }
948
949 /*
950 * Kick the controller to start things going again, then re-enable interrupts.
951 */
952 twe_startio(sc);
953 twe_enable_interrupts(sc);
954 twe_printf(sc, "controller reset done, %d commands restarted\n", i);
955
956out:
957 splx(s);
958 twe_enable_interrupts(sc);
959}
960
961/********************************************************************************
962 ********************************************************************************
963 Command I/O to Controller
964 ********************************************************************************
965 ********************************************************************************/
966
967/********************************************************************************
968 * Try to deliver (tr) to the controller.
969 *
970 * Can be called at any interrupt level, with or without interrupts enabled.
971 */
972static int
973twe_start(struct twe_request *tr)
974{
975 struct twe_softc *sc = tr->tr_sc;
976 int i, s, done;
977 u_int32_t status_reg;
978
979 debug_called(4);
980
981 /* mark the command as currently being processed */
982 tr->tr_status = TWE_CMD_BUSY;
983
984 /*
985 * Spin briefly waiting for the controller to come ready
986 *
987 * XXX it might be more efficient to return EBUSY immediately
988 * and let the command be rescheduled.
989 */
990 for (i = 100000, done = 0; (i > 0) && !done; i--) {
991 s = splbio();
992
993 /* check to see if we can post a command */
994 status_reg = TWE_STATUS(sc);
995 twe_check_bits(sc, status_reg);
996
997 if (!(status_reg & TWE_STATUS_COMMAND_QUEUE_FULL)) {
998 TWE_COMMAND_QUEUE(sc, tr->tr_cmdphys);
999 done = 1;
1000 /* move command to work queue */
1001 twe_enqueue_busy(tr);
1002#ifdef TWE_DEBUG
1003 if (tr->tr_complete != NULL) {
1004 debug(3, "queued request %d with callback %p", tr->tr_command.generic.request_id, tr->tr_complete);
1005 } else if (tr->tr_flags & TWE_CMD_SLEEPER) {
1006 debug(3, "queued request %d with wait channel %p", tr->tr_command.generic.request_id, tr);
1007 } else {
1008 debug(3, "queued request %d for polling caller", tr->tr_command.generic.request_id);
1009 }
1010#endif
1011 }
1012 splx(s); /* drop spl to allow completion interrupts */
1013 }
1014
1015 /* command is enqueued */
1016 if (done)
1017 return(0);
1018
1019 /*
1020 * We couldn't get the controller to take the command; try submitting it again later.
1021 * This should only happen if something is wrong with the controller, or if we have
1022 * overestimated the number of commands it can accept. (Should we actually reject
1023 * the command at this point?)
1024 */
1025 return(EBUSY);
1026}
1027
1028/********************************************************************************
1029 * Poll the controller (sc) for completed commands.
1030 *
1031 * Can be called at any interrupt level, with or without interrupts enabled.
1032 */
1033static void
1034twe_done(struct twe_softc *sc)
1035{
1036 TWE_Response_Queue rq;
1037 struct twe_request *tr;
1038 int s, found;
1039 u_int32_t status_reg;
1040
1041 debug_called(5);
1042
1043 /* loop collecting completed commands */
1044 found = 0;
1045 s = splbio();
1046 for (;;) {
1047 status_reg = TWE_STATUS(sc);
1048 twe_check_bits(sc, status_reg); /* XXX should this fail? */
1049
1050 if (!(status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)) {
1051 found = 1;
1052 rq = TWE_RESPONSE_QUEUE(sc);
1053 tr = sc->twe_lookup[rq.u.response_id]; /* find command */
1054 if (tr->tr_status != TWE_CMD_BUSY)
1055 twe_printf(sc, "completion event for nonbusy command\n");
1056 tr->tr_status = TWE_CMD_COMPLETE;
1057 debug(3, "completed request id %d with status %d",
1058 tr->tr_command.generic.request_id, tr->tr_command.generic.status);
1059 /* move to completed queue */
1060 twe_remove_busy(tr);
1061 twe_enqueue_complete(tr);
1062 } else {
1063 break; /* no response ready */
1064 }
1065 }
1066 splx(s);
1067
1068 /* if we've completed any commands, try posting some more */
1069 if (found)
1070 twe_startio(sc);
1071
1072 /* handle completion and timeouts */
1073 twe_complete(sc); /* XXX use deferred completion? */
1074}
1075
1076/********************************************************************************
1077 * Perform post-completion processing for commands on (sc).
1078 *
1079 * This is split from twe_done as it can be safely deferred and run at a lower
1080 * priority level should facilities for such a thing become available.
1081 */
1082static void
1083twe_complete(struct twe_softc *sc)
1084{
1085 struct twe_request *tr;
1086
1087 debug_called(5);
1088
1089 /*
1090 * Pull commands off the completed list, dispatch them appropriately
1091 */
1092 while ((tr = twe_dequeue_complete(sc)) != NULL) {
1093
1094 /* unmap the command's data buffer */
1095 twe_unmap_request(tr);
1096
1097 /* dispatch to suit command originator */
1098 if (tr->tr_complete != NULL) { /* completion callback */
1099 debug(2, "call completion handler %p", tr->tr_complete);
1100 tr->tr_complete(tr);
1101
1102 } else if (tr->tr_flags & TWE_CMD_SLEEPER) { /* caller is asleep waiting */
1103 debug(2, "wake up command owner on %p", tr);
1104 wakeup_one(tr);
1105
1106 } else { /* caller is polling command */
1107 debug(2, "command left for owner");
1108 }
1109 }
1110}
1111
1112/********************************************************************************
1113 * Wait for (status) to be set in the controller status register for up to
1114 * (timeout) seconds. Returns 0 if found, nonzero if we time out.
1115 *
1116 * Note: this busy-waits, rather than sleeping, since we may be called with
1117 * eg. clock interrupts masked.
1118 */
1119static int
1120twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout)
1121{
1122 time_t expiry;
1123 u_int32_t status_reg;
1124
1125 debug_called(4);
1126
1127 expiry = time_second + timeout;
1128
1129 do {
1130 status_reg = TWE_STATUS(sc);
1131 if (status_reg & status) /* got the required bit(s)? */
1132 return(0);
1133 DELAY(100000);
1134 } while (time_second <= expiry);
1135
1136 return(1);
1137}
1138
1139/********************************************************************************
1140 * Drain the response queue, which may contain responses to commands we know
1141 * nothing about.
1142 */
1143static int
1144twe_drain_response_queue(struct twe_softc *sc)
1145{
1146 TWE_Response_Queue rq;
1147 u_int32_t status_reg;
1148
1149 debug_called(4);
1150
1151 for (;;) { /* XXX give up eventually? */
1152 status_reg = TWE_STATUS(sc);
1153 if (twe_check_bits(sc, status_reg))
1154 return(1);
1155 if (status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)
1156 return(0);
1157 rq = TWE_RESPONSE_QUEUE(sc);
1158 }
1159}
1160
1161/********************************************************************************
1162 * Soft-reset the controller
1163 */
1164static int
1165twe_soft_reset(struct twe_softc *sc)
1166{
1167 u_int32_t status_reg;
1168
1169 debug_called(2);
1170
1171 TWE_SOFT_RESET(sc);
1172
1173 if (twe_wait_status(sc, TWE_STATUS_ATTENTION_INTERRUPT, 15)) {
1174 twe_printf(sc, "no attention interrupt\n");
1175 return(1);
1176 }
1177 if (twe_drain_aen_queue(sc)) {
1178 twe_printf(sc, "can't drain AEN queue\n");
1179 return(1);
1180 }
1181 if (twe_find_aen(sc, TWE_AEN_SOFT_RESET)) {
1182 twe_printf(sc, "reset not reported\n");
1183 return(1);
1184 }
1185 status_reg = TWE_STATUS(sc);
1186 if (TWE_STATUS_ERRORS(status_reg) || twe_check_bits(sc, status_reg)) {
1187 twe_printf(sc, "controller errors detected\n");
1188 return(1);
1189 }
1190 if (twe_drain_response_queue(sc)) {
1191 twe_printf(sc, "can't drain response queue\n");
1192 return(1);
1193 }
1194 return(0);
1195}
1196
1197/********************************************************************************
1198 ********************************************************************************
1199 Interrupt Handling
1200 ********************************************************************************
1201 ********************************************************************************/
1202
1203/********************************************************************************
1204 * Host interrupt.
1205 *
1206 * XXX what does this mean?
1207 */
1208static void
1209twe_host_intr(struct twe_softc *sc)
1210{
1211 debug_called(4);
1212
1213 twe_printf(sc, "host interrupt\n");
1214 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_HOST_INTERRUPT);
1215}
1216
1217/********************************************************************************
1218 * Attention interrupt.
1219 *
1220 * Signalled when the controller has one or more AENs for us.
1221 */
1222static void
1223twe_attention_intr(struct twe_softc *sc)
1224{
1225 debug_called(4);
1226
1227 /* instigate a poll for AENs */
1228 if (twe_fetch_aen(sc)) {
1229 twe_printf(sc, "error polling for signalled AEN\n");
1230 } else {
1231 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1232 }
1233}
1234
1235/********************************************************************************
1236 * Command interrupt.
1237 *
1238 * Signalled when the controller can handle more commands.
1239 */
1240static void
1241twe_command_intr(struct twe_softc *sc)
1242{
1243 debug_called(4);
1244
1245 /*
1246 * We don't use this, rather we try to submit commands when we receive
1247 * them, and when other commands have completed. Mask it so we don't get
1248 * another one.
1249 */
1250 twe_printf(sc, "command interrupt\n");
1251 TWE_CONTROL(sc, TWE_CONTROL_MASK_COMMAND_INTERRUPT);
1252}
1253
1254/********************************************************************************
1255 ********************************************************************************
1256 Asynchronous Event Handling
1257 ********************************************************************************
1258 ********************************************************************************/
1259
1260/********************************************************************************
1261 * Request an AEN from the controller.
1262 */
1263static int
1264twe_fetch_aen(struct twe_softc *sc)
1265{
1266
1267 debug_called(4);
1268
1269 if ((twe_get_param(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, 2, twe_handle_aen)) == NULL)
1270 return(EIO);
1271 return(0);
1272}
1273
1274/********************************************************************************
1275 * Handle an AEN returned by the controller.
1276 */
1277static void
1278twe_handle_aen(struct twe_request *tr)
1279{
1280 struct twe_softc *sc = tr->tr_sc;
1281 TWE_Param *param;
1282 u_int16_t aen;
1283
1284 debug_called(4);
1285
1286 /* XXX check for command success somehow? */
1287
1288 param = (TWE_Param *)tr->tr_data;
1289 aen = *(u_int16_t *)(param->data);
1290
1291 free(tr->tr_data, M_DEVBUF);
1292 twe_release_request(tr);
1293 twe_enqueue_aen(sc, aen);
1294
1295 /* XXX poll for more AENs? */
1296}
1297
1298/********************************************************************************
1299 * Pull AENs out of the controller and park them in the queue, in a context where
1300 * interrupts aren't active. Return nonzero if we encounter any errors in the
1301 * process of obtaining all the available AENs.
1302 */
1303static int
1304twe_drain_aen_queue(struct twe_softc *sc)
1305{
1306 u_int16_t aen;
1307
1308 for (;;) {
1309 if (twe_get_param_2(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, &aen))
1310 return(1);
1311 if (aen == TWE_AEN_QUEUE_EMPTY)
1312 return(0);
1313 twe_enqueue_aen(sc, aen);
1314 }
1315}
1316
1317/********************************************************************************
1318 * Push an AEN that we've received onto the queue.
1319 *
1320 * Note that we have to lock this against reentrance, since it may be called
1321 * from both interrupt and non-interrupt context.
1322 *
1323 * If someone is waiting for the AEN we have, wake them up.
1324 */
1325static void
1326twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen)
1327{
1328 char *msg;
1329 int s, next, nextnext;
1330
1331 debug_called(4);
1332
1333 if ((msg = twe_format_aen(sc, aen)) != NULL)
1334 twe_printf(sc, "AEN: <%s>\n", msg);
1335
1336 s = splbio();
1337 /* enqueue the AEN */
1338 next = ((sc->twe_aen_head + 1) % TWE_Q_LENGTH);
1339 nextnext = ((sc->twe_aen_head + 2) % TWE_Q_LENGTH);
1340
1341 /* check to see if this is the last free slot, and subvert the AEN if it is */
1342 if (nextnext == sc->twe_aen_tail)
1343 aen = TWE_AEN_QUEUE_FULL;
1344
1345 /* look to see if there's room for this AEN */
1346 if (next != sc->twe_aen_tail) {
1347 sc->twe_aen_queue[sc->twe_aen_head] = aen;
1348 sc->twe_aen_head = next;
1349 }
1350
1351 /* wake up anyone asleep on the queue */
1352 wakeup(&sc->twe_aen_queue);
1353
1354 /* anyone looking for this AEN? */
1355 if (sc->twe_wait_aen == aen) {
1356 sc->twe_wait_aen = -1;
1357 wakeup(&sc->twe_wait_aen);
1358 }
1359 splx(s);
1360}
1361
1362/********************************************************************************
1363 * Pop an AEN off the queue, or return -1 if there are none left.
1364 *
1365 * We are more or less interrupt-safe, so don't block interrupts.
1366 */
1367static int
1368twe_dequeue_aen(struct twe_softc *sc)
1369{
1370 int result;
1371
1372 debug_called(4);
1373
1374 if (sc->twe_aen_tail == sc->twe_aen_head) {
1375 result = -1;
1376 } else {
1377 result = sc->twe_aen_queue[sc->twe_aen_tail];
1378 sc->twe_aen_tail = ((sc->twe_aen_tail + 1) % TWE_Q_LENGTH);
1379 }
1380 return(result);
1381}
1382
1383/********************************************************************************
1384 * Check to see if the requested AEN is in the queue.
1385 *
1386 * XXX we could probably avoid masking interrupts here
1387 */
1388static int
1389twe_find_aen(struct twe_softc *sc, u_int16_t aen)
1390{
1391 int i, s, missing;
1392
1393 missing = 1;
1394 s = splbio();
1395 for (i = sc->twe_aen_tail; (i != sc->twe_aen_head) && missing; i = (i + 1) % TWE_Q_LENGTH) {
1396 if (sc->twe_aen_queue[i] == aen)
1397 missing = 0;
1398 }
1399 splx(s);
1400 return(missing);
1401}
1402
1403
1404#if 0 /* currently unused */
1405/********************************************************************************
1406 * Sleep waiting for at least (timeout) seconds until we see (aen) as
1407 * requested. Returns nonzero on timeout or failure.
1408 *
1409 * XXX: this should not be used in cases where there may be more than one sleeper
1410 * without a mechanism for registering multiple sleepers.
1411 */
1412static int
1413twe_wait_aen(struct twe_softc *sc, int aen, int timeout)
1414{
1415 time_t expiry;
1416 int found, s;
1417
1418 debug_called(4);
1419
1420 expiry = time_second + timeout;
1421 found = 0;
1422
1423 s = splbio();
1424 sc->twe_wait_aen = aen;
1425 do {
1426 twe_fetch_aen(sc);
1427 tsleep(&sc->twe_wait_aen, PZERO, "twewaen", hz);
1428 if (sc->twe_wait_aen == -1)
1429 found = 1;
1430 } while ((time_second <= expiry) && !found);
1431 splx(s);
1432 return(!found);
1433}
1434#endif
1435
1436/********************************************************************************
1437 ********************************************************************************
1438 Command Buffer Management
1439 ********************************************************************************
1440 ********************************************************************************/
1441
1442/********************************************************************************
1443 * Get a new command buffer.
1444 *
1445 * This will return NULL if all command buffers are in use.
1446 */
1447static int
1448twe_get_request(struct twe_softc *sc, struct twe_request **tr)
1449{
1450 debug_called(4);
1451
1452 /* try to reuse an old buffer */
1453 *tr = twe_dequeue_free(sc);
1454
1455 /* initialise some fields to their defaults */
1456 if (*tr != NULL) {
1457 (*tr)->tr_data = NULL;
1458 (*tr)->tr_private = NULL;
1459 (*tr)->tr_status = TWE_CMD_SETUP; /* command is in setup phase */
1460 (*tr)->tr_flags = 0;
1461 (*tr)->tr_complete = NULL;
1462 (*tr)->tr_command.generic.status = 0; /* before submission to controller */
1463 (*tr)->tr_command.generic.flags = 0; /* not used */
1464 }
1465 return(*tr == NULL);
1466}
1467
1468/********************************************************************************
1469 * Release a command buffer for reuse.
1470 *
1471 */
1472static void
1473twe_release_request(struct twe_request *tr)
1474{
1475 debug_called(4);
1476
1477 if (tr->tr_private != NULL)
1478 twe_panic(tr->tr_sc, "tr_private != NULL");
1479 twe_enqueue_free(tr);
1480}
1481
1482/********************************************************************************
1483 ********************************************************************************
1484 Debugging
1485 ********************************************************************************
1486 ********************************************************************************/
1487
1488/********************************************************************************
1489 * Print some information about the controller
1490 */
1491void
1492twe_describe_controller(struct twe_softc *sc)
1493{
1494 TWE_Param *p[6];
1495 u_int8_t ports;
1496 u_int32_t size;
1497 int i;
1498
1499 debug_called(2);
1500
1501 /* get the port count */
1502 twe_get_param_1(sc, TWE_PARAM_CONTROLLER, TWE_PARAM_CONTROLLER_PortCount, &ports);
1503
1504 /* get version strings */
1505 p[0] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_Mon, 16, NULL);
1506 p[1] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_FW, 16, NULL);
1507 p[2] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_BIOS, 16, NULL);
1508 p[3] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCB, 8, NULL);
1509 p[4] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_ATA, 8, NULL);
1510 p[5] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCI, 8, NULL);
1511
1512 twe_printf(sc, "%d ports, Firmware %.16s, BIOS %.16s\n", ports, p[1]->data, p[2]->data);
1513 if (bootverbose)
1514 twe_printf(sc, "Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n", p[0]->data, p[3]->data,
1515 p[4]->data, p[5]->data);
1516 free(p[0], M_DEVBUF);
1517 free(p[1], M_DEVBUF);
1518 free(p[2], M_DEVBUF);
1519 free(p[3], M_DEVBUF);
1520 free(p[4], M_DEVBUF);
1521 free(p[5], M_DEVBUF);
1522
1523 /* print attached drives */
1524 if (bootverbose) {
1525 p[0] = twe_get_param(sc, TWE_PARAM_DRIVESUMMARY, TWE_PARAM_DRIVESUMMARY_Status, 16, NULL);
1526 for (i = 0; i < ports; i++) {
1527 if (p[0]->data[i] != TWE_PARAM_DRIVESTATUS_Present)
1528 continue;
1529 twe_get_param_4(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Size, &size);
1530 p[1] = twe_get_param(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Model, 40, NULL);
1531 if (p[1] != NULL) {
1532 twe_printf(sc, "port %d: %.40s %dMB\n", i, p[1]->data, size / 2048);
1533 free(p[1], M_DEVBUF);
1534 } else {
1535 twe_printf(sc, "port %d, drive status unavailable\n", i);
1536 }
1537 }
1538 free(p[0], M_DEVBUF);
1539 }
1540}
1541
1542/********************************************************************************
1543 * Complain if the status bits aren't what we're expecting.
1544 *
1545 * Rate-limit the complaints to at most one of each every five seconds, but
1546 * always return the correct status.
1547 */
1548static int
1549twe_check_bits(struct twe_softc *sc, u_int32_t status_reg)
1550{
1551 int result;
1552 static time_t lastwarn[2] = {0, 0};
1553
1554 /*
1555 * This can be a little problematic, as twe_panic may call twe_reset if
1556 * TWE_DEBUG is not set, which will call us again as part of the soft reset.
1557 */
1558 if ((status_reg & TWE_STATUS_PANIC_BITS) != 0) {
1559 twe_printf(sc, "FATAL STATUS BIT(S) %b\n", status_reg & TWE_STATUS_PANIC_BITS,
1560 TWE_STATUS_BITS_DESCRIPTION);
1561 twe_panic(sc, "fatal status bits");
1562 }
1563
1564 result = 0;
1565 if ((status_reg & TWE_STATUS_EXPECTED_BITS) != TWE_STATUS_EXPECTED_BITS) {
1566 if (time_second > (lastwarn[0] + 5)) {
1567 twe_printf(sc, "missing expected status bit(s) %b\n", ~status_reg & TWE_STATUS_EXPECTED_BITS,
1568 TWE_STATUS_BITS_DESCRIPTION);
1569 lastwarn[0] = time_second;
1570 }
1571 result = 1;
1572 }
1573
1574 if ((status_reg & TWE_STATUS_UNEXPECTED_BITS) != 0) {
1575 if (time_second > (lastwarn[1] + 5)) {
1576 twe_printf(sc, "unexpected status bit(s) %b\n", status_reg & TWE_STATUS_UNEXPECTED_BITS,
1577 TWE_STATUS_BITS_DESCRIPTION);
1578 lastwarn[1] = time_second;
1579 }
1580 result = 1;
1581 }
1582
1583 return(result);
1584}
1585
1586/********************************************************************************
1587 * Return a string describing (aen).
1588 *
1589 * The low 8 bits of the aen are the code, the high 8 bits give the unit number
1590 * where an AEN is specific to a unit.
1591 *
1592 * Note that we could expand this routine to handle eg. up/downgrading the status
1593 * of a drive if we had some idea of what the drive's initial status was.
1594 */
1595
1596static char *
1597twe_format_aen(struct twe_softc *sc, u_int16_t aen)
1598{
1599 static char buf[80];
1600 device_t child;
1601 char *code, *msg;
1602
1603 code = twe_describe_code(twe_table_aen, TWE_AEN_CODE(aen));
1604 msg = code + 2;
1605
1606 switch (*code) {
1607 case 'q':
1608 if (!bootverbose)
1609 return(NULL);
1610 /* FALLTHROUGH */
1611 case 'p':
1612 return(msg);
1613
1614 case 'c':
1615 if ((child = sc->twe_drive[TWE_AEN_UNIT(aen)].td_disk) != NULL) {
1616 sprintf(buf, "twed%d: %s", device_get_unit(child), msg);
1617 } else {
1618 sprintf(buf, "twe%d: %s for unknown unit %d", device_get_unit(sc->twe_dev),
1619 msg, TWE_AEN_UNIT(aen));
1620 }
1621 return(buf);
1622
1623 case 'x':
1624 default:
1625 break;
1626 }
1627 sprintf(buf, "unknown AEN 0x%x", aen);
1628 return(buf);
1629}
1630
1631/********************************************************************************
1632 * Print a diagnostic if the status of the command warrants it, and return
1633 * either zero (command was ok) or nonzero (command failed).
1634 */
1635static int
1636twe_report_request(struct twe_request *tr)
1637{
1638 struct twe_softc *sc = tr->tr_sc;
1639 TWE_Command *cmd = &tr->tr_command;
28 */
29
30/*
31 * Driver for the 3ware Escalade family of IDE RAID controllers.
32 */
33
34#include <dev/twe/twe_compat.h>
35#include <dev/twe/twereg.h>
36#include <dev/twe/tweio.h>
37#include <dev/twe/twevar.h>
38#define TWE_DEFINE_TABLES
39#include <dev/twe/twe_tables.h>
40
41/*
42 * Command submission.
43 */
44static int twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result);
45static int twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result);
46static int twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result);
47static void *twe_get_param(struct twe_softc *sc, int table_id, int parameter_id, size_t size,
48 void (* func)(struct twe_request *tr));
49static int twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value);
50static int twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value);
51static int twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value);
52static int twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size,
53 void *data);
54static int twe_init_connection(struct twe_softc *sc, int mode);
55static int twe_wait_request(struct twe_request *tr);
56static int twe_immediate_request(struct twe_request *tr);
57static void twe_completeio(struct twe_request *tr);
58static void twe_reset(struct twe_softc *sc);
59
60/*
61 * Command I/O to controller.
62 */
63static int twe_start(struct twe_request *tr);
64static void twe_done(struct twe_softc *sc);
65static void twe_complete(struct twe_softc *sc);
66static int twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout);
67static int twe_drain_response_queue(struct twe_softc *sc);
68static int twe_check_bits(struct twe_softc *sc, u_int32_t status_reg);
69static int twe_soft_reset(struct twe_softc *sc);
70
71/*
72 * Interrupt handling.
73 */
74static void twe_host_intr(struct twe_softc *sc);
75static void twe_attention_intr(struct twe_softc *sc);
76static void twe_command_intr(struct twe_softc *sc);
77
78/*
79 * Asynchronous event handling.
80 */
81static int twe_fetch_aen(struct twe_softc *sc);
82static void twe_handle_aen(struct twe_request *tr);
83static void twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen);
84static int twe_dequeue_aen(struct twe_softc *sc);
85static int twe_drain_aen_queue(struct twe_softc *sc);
86static int twe_find_aen(struct twe_softc *sc, u_int16_t aen);
87
88/*
89 * Command buffer management.
90 */
91static int twe_get_request(struct twe_softc *sc, struct twe_request **tr);
92static void twe_release_request(struct twe_request *tr);
93
94/*
95 * Debugging.
96 */
97static char *twe_format_aen(struct twe_softc *sc, u_int16_t aen);
98static int twe_report_request(struct twe_request *tr);
99static void twe_panic(struct twe_softc *sc, char *reason);
100
101/********************************************************************************
102 ********************************************************************************
103 Public Interfaces
104 ********************************************************************************
105 ********************************************************************************/
106
107/********************************************************************************
108 * Initialise the controller, set up driver data structures.
109 */
110int
111twe_setup(struct twe_softc *sc)
112{
113 struct twe_request *tr;
114 int i;
115
116 debug_called(4);
117
118 /*
119 * Initialise request queues.
120 */
121 twe_initq_free(sc);
122 twe_initq_bio(sc);
123 twe_initq_ready(sc);
124 twe_initq_busy(sc);
125 twe_initq_complete(sc);
126 sc->twe_wait_aen = -1;
127
128 /*
129 * Allocate request structures up front.
130 */
131 for (i = 0; i < TWE_Q_LENGTH; i++) {
132 if ((tr = twe_allocate_request(sc)) == NULL)
133 return(ENOMEM);
134 /*
135 * Set global defaults that won't change.
136 */
137 tr->tr_command.generic.host_id = sc->twe_host_id; /* controller-assigned host ID */
138 tr->tr_command.generic.request_id = i; /* our index number */
139 sc->twe_lookup[i] = tr;
140
141 /*
142 * Put command onto the freelist.
143 */
144 twe_release_request(tr);
145 }
146
147 /*
148 * Wait for the controller to come ready.
149 */
150 if (twe_wait_status(sc, TWE_STATUS_MICROCONTROLLER_READY, 60)) {
151 twe_printf(sc, "microcontroller not ready\n");
152 return(ENXIO);
153 }
154
155 /*
156 * Disable interrupts from the card.
157 */
158 twe_disable_interrupts(sc);
159
160 /*
161 * Soft reset the controller, look for the AEN acknowledging the reset,
162 * check for errors, drain the response queue.
163 */
164 for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
165
166 if (i > 0)
167 twe_printf(sc, "reset %d failed, trying again\n", i);
168
169 if (!twe_soft_reset(sc))
170 break; /* reset process complete */
171 }
172 /* did we give up? */
173 if (i >= TWE_MAX_RESET_TRIES) {
174 twe_printf(sc, "can't initialise controller, giving up\n");
175 return(ENXIO);
176 }
177
178 return(0);
179}
180
181/********************************************************************************
182 * Locate disk devices and attach children to them.
183 */
184void
185twe_init(struct twe_softc *sc)
186{
187 struct twe_drive *dr;
188 int i, table;
189 u_int16_t dsize;
190 TWE_Param *drives, *param;
191 TWE_Unit_Descriptor *ud;
192
193
194 /*
195 * The controller is in a safe state, so try to find drives attached to it.
196 */
197 if ((drives = twe_get_param(sc, TWE_PARAM_UNITSUMMARY, TWE_PARAM_UNITSUMMARY_Status,
198 TWE_MAX_UNITS, NULL)) == NULL) {
199 twe_printf(sc, "can't detect attached units\n");
200 return;
201 }
202
203 /*
204 * For each detected unit, create a child device.
205 */
206 for (i = 0, dr = &sc->twe_drive[0]; i < TWE_MAX_UNITS; i++, dr++) {
207
208 /* check that the drive is online */
209 if (!(drives->data[i] & TWE_PARAM_UNITSTATUS_Online))
210 continue;
211
212 table = TWE_PARAM_UNITINFO + i;
213
214 if (twe_get_param_4(sc, table, TWE_PARAM_UNITINFO_Capacity, &dr->td_size)) {
215 twe_printf(sc, "error fetching capacity for unit %d\n", i);
216 continue;
217 }
218 if (twe_get_param_1(sc, table, TWE_PARAM_UNITINFO_Status, &dr->td_state)) {
219 twe_printf(sc, "error fetching state for unit %d\n", i);
220 continue;
221 }
222 if (twe_get_param_2(sc, table, TWE_PARAM_UNITINFO_DescriptorSize, &dsize)) {
223 twe_printf(sc, "error fetching descriptor size for unit %d\n", i);
224 continue;
225 }
226 if ((param = twe_get_param(sc, table, TWE_PARAM_UNITINFO_Descriptor, dsize - 3, NULL)) == NULL) {
227 twe_printf(sc, "error fetching descriptor for unit %d\n", i);
228 continue;
229 }
230 ud = (TWE_Unit_Descriptor *)param->data;
231 dr->td_type = ud->configuration;
232 free(param, M_DEVBUF);
233
234 /* build synthetic geometry as per controller internal rules */
235 if (dr->td_size > 0x200000) {
236 dr->td_heads = 255;
237 dr->td_sectors = 63;
238 } else {
239 dr->td_heads = 64;
240 dr->td_sectors = 32;
241 }
242 dr->td_cylinders = dr->td_size / (dr->td_heads * dr->td_sectors);
243 dr->td_unit = i;
244
245 twe_attach_drive(sc, dr);
246 }
247 free(drives, M_DEVBUF);
248
249 /*
250 * Initialise connection with controller.
251 */
252 twe_init_connection(sc, TWE_INIT_MESSAGE_CREDITS);
253
254#ifdef TWE_SHUTDOWN_NOTIFICATION
255 /*
256 * Tell the controller we support shutdown notification.
257 */
258 twe_set_param_1(sc, TWE_PARAM_FEATURES, TWE_PARAM_FEATURES_DriverShutdown, 1);
259#endif
260
261 /*
262 * Mark controller up and ready to run.
263 */
264 sc->twe_state &= ~TWE_STATE_SHUTDOWN;
265
266 /*
267 * Finally enable interrupts.
268 */
269 twe_enable_interrupts(sc);
270}
271
272/********************************************************************************
273 * Stop the controller
274 */
275void
276twe_deinit(struct twe_softc *sc)
277{
278 /*
279 * Mark the controller as shutting down, and disable any further interrupts.
280 */
281 sc->twe_state |= TWE_STATE_SHUTDOWN;
282 twe_disable_interrupts(sc);
283
284#ifdef TWE_SHUTDOWN_NOTIFICATION
285 /*
286 * Disconnect from the controller
287 */
288 twe_init_connection(sc, TWE_SHUTDOWN_MESSAGE_CREDITS);
289#endif
290}
291
292/*******************************************************************************
293 * Take an interrupt, or be poked by other code to look for interrupt-worthy
294 * status.
295 */
296void
297twe_intr(struct twe_softc *sc)
298{
299 u_int32_t status_reg;
300
301 debug_called(4);
302
303 /*
304 * Collect current interrupt status.
305 */
306 status_reg = TWE_STATUS(sc);
307 twe_check_bits(sc, status_reg);
308
309 /*
310 * Dispatch based on interrupt status
311 */
312 if (status_reg & TWE_STATUS_HOST_INTERRUPT)
313 twe_host_intr(sc);
314 if (status_reg & TWE_STATUS_ATTENTION_INTERRUPT)
315 twe_attention_intr(sc);
316 if (status_reg & TWE_STATUS_COMMAND_INTERRUPT)
317 twe_command_intr(sc);
318 if (status_reg & TWE_STATUS_RESPONSE_INTERRUPT)
319 twe_done(sc);
320};
321
322/********************************************************************************
323 * Pull as much work off the softc's work queue as possible and give it to the
324 * controller.
325 */
326void
327twe_startio(struct twe_softc *sc)
328{
329 struct twe_request *tr;
330 TWE_Command *cmd;
331 twe_bio *bp;
332 int error;
333
334 debug_called(4);
335
336 /* spin until something prevents us from doing any work */
337 for (;;) {
338
339 /* try to get a command that's already ready to go */
340 tr = twe_dequeue_ready(sc);
341
342 /* build a command from an outstanding bio */
343 if (tr == NULL) {
344
345 /* see if there's work to be done */
346 if ((bp = twe_dequeue_bio(sc)) == NULL)
347 break;
348
349 /* get a command to handle the bio with */
350 if (twe_get_request(sc, &tr)) {
351 twe_enqueue_bio(sc, bp); /* failed, put the bio back */
352 break;
353 }
354
355 /* connect the bio to the command */
356 tr->tr_complete = twe_completeio;
357 tr->tr_private = bp;
358 tr->tr_data = TWE_BIO_DATA(bp);
359 tr->tr_length = TWE_BIO_LENGTH(bp);
360 cmd = &tr->tr_command;
361 if (TWE_BIO_IS_READ(bp)) {
362 tr->tr_flags |= TWE_CMD_DATAIN;
363 cmd->io.opcode = TWE_OP_READ;
364 } else {
365 tr->tr_flags |= TWE_CMD_DATAOUT;
366 cmd->io.opcode = TWE_OP_WRITE;
367 }
368
369 /* build a suitable I/O command (assumes 512-byte rounded transfers) */
370 cmd->io.size = 3;
371 cmd->io.unit = TWE_BIO_UNIT(bp);
372 cmd->io.block_count = (tr->tr_length + TWE_BLOCK_SIZE - 1) / TWE_BLOCK_SIZE;
373 cmd->io.lba = TWE_BIO_LBA(bp);
374
375 /* map the command so the controller can work with it */
376 twe_map_request(tr);
377 }
378
379 /* did we find something to do? */
380 if (tr == NULL)
381 break;
382
383 /* try to give command to controller */
384 error = twe_start(tr);
385
386 if (error != 0) {
387 if (error == EBUSY) {
388 twe_requeue_ready(tr); /* try it again later */
389 break; /* don't try anything more for now */
390 }
391 /* we don't support any other return from twe_start */
392 twe_panic(sc, "twe_start returned nonsense");
393 }
394 }
395}
396
397/********************************************************************************
398 * Write blocks from memory to disk, for system crash dumps.
399 */
400int
401twe_dump_blocks(struct twe_softc *sc, int unit, u_int32_t lba, void *data, int nblks)
402{
403 struct twe_request *tr;
404 TWE_Command *cmd;
405 int error;
406
407 if (twe_get_request(sc, &tr))
408 return(ENOMEM);
409
410 tr->tr_data = data;
411 tr->tr_status = TWE_CMD_SETUP;
412 tr->tr_length = nblks * TWE_BLOCK_SIZE;
413 tr->tr_flags = TWE_CMD_DATAOUT;
414
415 cmd = &tr->tr_command;
416 cmd->io.opcode = TWE_OP_WRITE;
417 cmd->io.size = 3;
418 cmd->io.unit = unit;
419 cmd->io.block_count = nblks;
420 cmd->io.lba = lba;
421
422 twe_map_request(tr);
423 error = twe_immediate_request(tr);
424 if (error == 0)
425 if (twe_report_request(tr))
426 error = EIO;
427 twe_release_request(tr);
428 return(error);
429}
430
431/********************************************************************************
432 * Handle controller-specific control operations.
433 */
434int
435twe_ioctl(struct twe_softc *sc, int cmd, void *addr)
436{
437 struct twe_usercommand *tu = (struct twe_usercommand *)addr;
438 struct twe_paramcommand *tp = (struct twe_paramcommand *)addr;
439 union twe_statrequest *ts = (union twe_statrequest *)addr;
440 TWE_Param *param;
441 void *data;
442 int *arg = (int *)addr;
443 struct twe_request *tr;
444 u_int8_t srid;
445 int s, error;
446
447 error = 0;
448 switch(cmd) {
449 /* handle a command from userspace */
450 case TWEIO_COMMAND:
451 /* get a request */
452 if (twe_get_request(sc, &tr)) {
453 error = EBUSY;
454 goto cmd_done;
455 }
456
457 /*
458 * Save the command's request ID, copy the user-supplied command in,
459 * restore the request ID.
460 */
461 srid = tr->tr_command.generic.request_id;
462 bcopy(&tu->tu_command, &tr->tr_command, sizeof(TWE_Command));
463 tr->tr_command.generic.request_id = srid;
464
465 /* if there's a data buffer, allocate and copy it in */
466 tr->tr_length = tu->tu_size;
467 if (tr->tr_length > 0) {
468 if ((tr->tr_data = malloc(tr->tr_length, M_DEVBUF, M_WAITOK)) == NULL) {
469 error = ENOMEM;
470 goto cmd_done;
471 }
472 if ((error = copyin(tu->tu_data, tr->tr_data, tr->tr_length)) != 0)
473 goto cmd_done;
474 tr->tr_flags |= TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
475 }
476
477 /* run the command */
478 twe_map_request(tr);
479 twe_wait_request(tr);
480
481 /* copy the command out again */
482 bcopy(&tr->tr_command, &tu->tu_command, sizeof(TWE_Command));
483
484 /* if there was a data buffer, copy it out */
485 if (tr->tr_length > 0)
486 error = copyout(tr->tr_data, tu->tu_data, tr->tr_length);
487
488 cmd_done:
489 /* free resources */
490 if (tr->tr_data != NULL)
491 free(tr->tr_data, M_DEVBUF);
492 if (tr != NULL)
493 twe_release_request(tr);
494
495 break;
496
497 /* fetch statistics counter */
498 case TWEIO_STATS:
499 switch (ts->ts_item) {
500#ifdef TWE_PERFORMANCE_MONITOR
501 case TWEQ_FREE:
502 case TWEQ_BIO:
503 case TWEQ_READY:
504 case TWEQ_BUSY:
505 case TWEQ_COMPLETE:
506 bcopy(&sc->twe_qstat[ts->ts_item], &ts->ts_qstat, sizeof(struct twe_qstat));
507 break;
508#endif
509 default:
510 error = ENOENT;
511 break;
512 }
513 break;
514
515 /* poll for an AEN */
516 case TWEIO_AEN_POLL:
517 *arg = twe_dequeue_aen(sc);
518 if (*arg == -1)
519 error = ENOENT;
520 break;
521
522 /* wait for another AEN to show up */
523 case TWEIO_AEN_WAIT:
524 s = splbio();
525 while ((*arg = twe_dequeue_aen(sc)) == -1) {
526 error = tsleep(&sc->twe_aen_queue, PRIBIO | PCATCH, "tweaen", 0);
527 if (error == EINTR)
528 break;
529 }
530 splx(s);
531 break;
532
533 case TWEIO_GET_PARAM:
534 if ((param = twe_get_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, NULL)) == NULL) {
535 twe_printf(sc, "TWEIO_GET_PARAM failed for 0x%x/0x%x/%d\n",
536 tp->tp_table_id, tp->tp_param_id, tp->tp_size);
537 error = EINVAL;
538 } else {
539 if (param->parameter_size_bytes > tp->tp_size) {
540 twe_printf(sc, "TWEIO_GET_PARAM parameter too large (%d > %d)\n",
541 param->parameter_size_bytes, tp->tp_size);
542 error = EFAULT;
543 } else {
544 error = copyout(param->data, tp->tp_data, param->parameter_size_bytes);
545 }
546 free(param, M_DEVBUF);
547 }
548 break;
549
550 case TWEIO_SET_PARAM:
551 if ((data = malloc(tp->tp_size, M_DEVBUF, M_WAITOK)) == NULL) {
552 error = ENOMEM;
553 } else {
554 error = copyin(tp->tp_data, data, tp->tp_size);
555 if (error == 0)
556 error = twe_set_param(sc, tp->tp_table_id, tp->tp_param_id, tp->tp_size, data);
557 free(data, M_DEVBUF);
558 }
559 break;
560
561 case TWEIO_RESET:
562 twe_reset(sc);
563 break;
564
565 /* nothing we understand */
566 default:
567 error = ENOTTY;
568 }
569
570 return(error);
571}
572
573/********************************************************************************
574 * Enable the useful interrupts from the controller.
575 */
576void
577twe_enable_interrupts(struct twe_softc *sc)
578{
579 sc->twe_state |= TWE_STATE_INTEN;
580 TWE_CONTROL(sc,
581 TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT |
582 TWE_CONTROL_UNMASK_RESPONSE_INTERRUPT |
583 TWE_CONTROL_ENABLE_INTERRUPTS);
584}
585
586/********************************************************************************
587 * Disable interrupts from the controller.
588 */
589void
590twe_disable_interrupts(struct twe_softc *sc)
591{
592 TWE_CONTROL(sc, TWE_CONTROL_DISABLE_INTERRUPTS);
593 sc->twe_state &= ~TWE_STATE_INTEN;
594}
595
596/********************************************************************************
597 ********************************************************************************
598 Command Submission
599 ********************************************************************************
600 ********************************************************************************/
601
602/********************************************************************************
603 * Read integer parameter table entries.
604 */
605static int
606twe_get_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t *result)
607{
608 TWE_Param *param;
609
610 if ((param = twe_get_param(sc, table_id, param_id, 1, NULL)) == NULL)
611 return(ENOENT);
612 *result = *(u_int8_t *)param->data;
613 free(param, M_DEVBUF);
614 return(0);
615}
616
617static int
618twe_get_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t *result)
619{
620 TWE_Param *param;
621
622 if ((param = twe_get_param(sc, table_id, param_id, 2, NULL)) == NULL)
623 return(ENOENT);
624 *result = *(u_int16_t *)param->data;
625 free(param, M_DEVBUF);
626 return(0);
627}
628
629static int
630twe_get_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t *result)
631{
632 TWE_Param *param;
633
634 if ((param = twe_get_param(sc, table_id, param_id, 4, NULL)) == NULL)
635 return(ENOENT);
636 *result = *(u_int32_t *)param->data;
637 free(param, M_DEVBUF);
638 return(0);
639}
640
641/********************************************************************************
642 * Perform a TWE_OP_GET_PARAM command. If a callback function is provided, it
643 * will be called with the command when it's completed. If no callback is
644 * provided, we will wait for the command to complete and then return just the data.
645 * The caller is responsible for freeing the data when done with it.
646 */
647static void *
648twe_get_param(struct twe_softc *sc, int table_id, int param_id, size_t param_size,
649 void (* func)(struct twe_request *tr))
650{
651 struct twe_request *tr;
652 TWE_Command *cmd;
653 TWE_Param *param;
654 int error;
655
656 debug_called(4);
657
658 tr = NULL;
659 param = NULL;
660
661 /* get a command */
662 if (twe_get_request(sc, &tr))
663 goto err;
664
665 /* get a buffer */
666 if ((param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
667 goto err;
668 tr->tr_data = param;
669 tr->tr_length = TWE_SECTOR_SIZE;
670 tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
671
672 /* build the command for the controller */
673 cmd = &tr->tr_command;
674 cmd->param.opcode = TWE_OP_GET_PARAM;
675 cmd->param.size = 2;
676 cmd->param.unit = 0;
677 cmd->param.param_count = 1;
678
679 /* map the command/data into controller-visible space */
680 twe_map_request(tr);
681
682 /* fill in the outbound parameter data */
683 param->table_id = table_id;
684 param->parameter_id = param_id;
685 param->parameter_size_bytes = param_size;
686
687 /* submit the command and either wait or let the callback handle it */
688 if (func == NULL) {
689 /* XXX could use twe_wait_request here if interrupts were enabled? */
690 error = twe_immediate_request(tr);
691 if (error == 0) {
692 if (twe_report_request(tr))
693 goto err;
694 }
695 twe_release_request(tr);
696 return(param);
697 } else {
698 tr->tr_complete = func;
699 error = twe_start(tr);
700 if (error == 0)
701 return(func);
702 }
703
704 /* something failed */
705err:
706 debug(1, "failed");
707 if (tr != NULL)
708 twe_release_request(tr);
709 if (param != NULL)
710 free(param, M_DEVBUF);
711 return(NULL);
712}
713
714/********************************************************************************
715 * Set integer parameter table entries.
716 */
717static int
718twe_set_param_1(struct twe_softc *sc, int table_id, int param_id, u_int8_t value)
719{
720 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
721}
722
723static int
724twe_set_param_2(struct twe_softc *sc, int table_id, int param_id, u_int16_t value)
725{
726 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
727}
728
729static int
730twe_set_param_4(struct twe_softc *sc, int table_id, int param_id, u_int32_t value)
731{
732 return(twe_set_param(sc, table_id, param_id, sizeof(value), &value));
733}
734
735/********************************************************************************
736 * Perform a TWE_OP_SET_PARAM command, returns nonzero on error.
737 */
738static int
739twe_set_param(struct twe_softc *sc, int table_id, int param_id, int param_size, void *data)
740{
741 struct twe_request *tr;
742 TWE_Command *cmd;
743 TWE_Param *param;
744 int error;
745
746 debug_called(4);
747
748 tr = NULL;
749 param = NULL;
750 error = ENOMEM;
751
752 /* get a command */
753 if (twe_get_request(sc, &tr))
754 goto out;
755
756 /* get a buffer */
757 if ((param = (TWE_Param *)malloc(TWE_SECTOR_SIZE, M_DEVBUF, M_NOWAIT)) == NULL)
758 goto out;
759 tr->tr_data = param;
760 tr->tr_length = TWE_SECTOR_SIZE;
761 tr->tr_flags = TWE_CMD_DATAIN | TWE_CMD_DATAOUT;
762
763 /* build the command for the controller */
764 cmd = &tr->tr_command;
765 cmd->param.opcode = TWE_OP_SET_PARAM;
766 cmd->param.size = 2;
767 cmd->param.unit = 0;
768 cmd->param.param_count = 1;
769
770 /* map the command/data into controller-visible space */
771 twe_map_request(tr);
772
773 /* fill in the outbound parameter data */
774 param->table_id = table_id;
775 param->parameter_id = param_id;
776 param->parameter_size_bytes = param_size;
777 bcopy(data, param->data, param_size);
778
779 /* XXX could use twe_wait_request here if interrupts were enabled? */
780 error = twe_immediate_request(tr);
781 if (error == 0) {
782 if (twe_report_request(tr))
783 error = EIO;
784 }
785
786out:
787 if (tr != NULL)
788 twe_release_request(tr);
789 if (param != NULL)
790 free(param, M_DEVBUF);
791 return(error);
792}
793
794/********************************************************************************
795 * Perform a TWE_OP_INIT_CONNECTION command, returns nonzero on error.
796 *
797 * Typically called with interrupts disabled.
798 */
799static int
800twe_init_connection(struct twe_softc *sc, int mode)
801{
802 struct twe_request *tr;
803 TWE_Command *cmd;
804 int error;
805
806 debug_called(4);
807
808 /* get a command */
809 if (twe_get_request(sc, &tr))
810 return(NULL);
811
812 /* build the command */
813 cmd = &tr->tr_command;
814 cmd->initconnection.opcode = TWE_OP_INIT_CONNECTION;
815 cmd->initconnection.size = 3;
816 cmd->initconnection.host_id = 0;
817 cmd->initconnection.message_credits = mode;
818 cmd->initconnection.response_queue_pointer = 0;
819
820 /* map the command into controller-visible space */
821 twe_map_request(tr);
822
823 /* submit the command */
824 error = twe_immediate_request(tr);
825 /* XXX check command result? */
826 twe_unmap_request(tr);
827 twe_release_request(tr);
828
829 if (mode == TWE_INIT_MESSAGE_CREDITS)
830 sc->twe_host_id = cmd->initconnection.host_id;
831 return(error);
832}
833
834/********************************************************************************
835 * Start the command (tr) and sleep waiting for it to complete.
836 *
837 * Successfully completed commands are dequeued.
838 */
839static int
840twe_wait_request(struct twe_request *tr)
841{
842 int s;
843
844 debug_called(4);
845
846 tr->tr_flags |= TWE_CMD_SLEEPER;
847 tr->tr_status = TWE_CMD_BUSY;
848 twe_enqueue_ready(tr);
849 twe_startio(tr->tr_sc);
850 s = splbio();
851 while (tr->tr_status == TWE_CMD_BUSY)
852 tsleep(tr, PRIBIO, "twewait", 0);
853 splx(s);
854
855 return(0);
856}
857
858/********************************************************************************
859 * Start the command (tr) and busy-wait for it to complete.
860 * This should only be used when interrupts are actually disabled (although it
861 * will work if they are not).
862 */
863static int
864twe_immediate_request(struct twe_request *tr)
865{
866 int error;
867
868 debug_called(4);
869
870 error = 0;
871
872 if ((error = twe_start(tr)) != 0)
873 return(error);
874 while (tr->tr_status == TWE_CMD_BUSY){
875 twe_done(tr->tr_sc);
876 }
877 return(tr->tr_status != TWE_CMD_COMPLETE);
878}
879
880/********************************************************************************
881 * Handle completion of an I/O command.
882 */
883static void
884twe_completeio(struct twe_request *tr)
885{
886 struct twe_softc *sc = tr->tr_sc;
887 twe_bio *bp = (twe_bio *)tr->tr_private;
888
889 debug_called(4);
890
891 if (tr->tr_status == TWE_CMD_COMPLETE) {
892
893 if (twe_report_request(tr))
894 TWE_BIO_SET_ERROR(bp, EIO);
895
896 } else {
897 twe_panic(sc, "twe_completeio on incomplete command");
898 }
899 tr->tr_private = NULL;
900 twed_intr(bp);
901 twe_release_request(tr);
902}
903
904/********************************************************************************
905 * Reset the controller and pull all the active commands back onto the ready
906 * queue. Used to restart a controller that's exhibiting bad behaviour.
907 */
908static void
909twe_reset(struct twe_softc *sc)
910{
911 struct twe_request *tr;
912 int i, s;
913
914 twe_printf(sc, "controller reset in progress...\n");
915
916 /*
917 * Disable interrupts from the controller, and mask any accidental entry
918 * into our interrupt handler.
919 */
920 twe_disable_interrupts(sc);
921 s = splbio();
922
923 /*
924 * Try to soft-reset the controller.
925 */
926 for (i = 0; i < TWE_MAX_RESET_TRIES; i++) {
927
928 if (i > 0)
929 twe_printf(sc, "reset %d failed, trying again\n", i);
930
931 if (!twe_soft_reset(sc))
932 break; /* reset process complete */
933 }
934 /* did we give up? */
935 if (i >= TWE_MAX_RESET_TRIES) {
936 twe_printf(sc, "can't reset controller, giving up\n");
937 goto out;
938 }
939
940 /*
941 * Move all of the commands that were busy back to the ready queue.
942 */
943 i = 0;
944 while ((tr = twe_dequeue_busy(sc)) != NULL) {
945 twe_enqueue_ready(tr);
946 i++;
947 }
948
949 /*
950 * Kick the controller to start things going again, then re-enable interrupts.
951 */
952 twe_startio(sc);
953 twe_enable_interrupts(sc);
954 twe_printf(sc, "controller reset done, %d commands restarted\n", i);
955
956out:
957 splx(s);
958 twe_enable_interrupts(sc);
959}
960
961/********************************************************************************
962 ********************************************************************************
963 Command I/O to Controller
964 ********************************************************************************
965 ********************************************************************************/
966
967/********************************************************************************
968 * Try to deliver (tr) to the controller.
969 *
970 * Can be called at any interrupt level, with or without interrupts enabled.
971 */
972static int
973twe_start(struct twe_request *tr)
974{
975 struct twe_softc *sc = tr->tr_sc;
976 int i, s, done;
977 u_int32_t status_reg;
978
979 debug_called(4);
980
981 /* mark the command as currently being processed */
982 tr->tr_status = TWE_CMD_BUSY;
983
984 /*
985 * Spin briefly waiting for the controller to come ready
986 *
987 * XXX it might be more efficient to return EBUSY immediately
988 * and let the command be rescheduled.
989 */
990 for (i = 100000, done = 0; (i > 0) && !done; i--) {
991 s = splbio();
992
993 /* check to see if we can post a command */
994 status_reg = TWE_STATUS(sc);
995 twe_check_bits(sc, status_reg);
996
997 if (!(status_reg & TWE_STATUS_COMMAND_QUEUE_FULL)) {
998 TWE_COMMAND_QUEUE(sc, tr->tr_cmdphys);
999 done = 1;
1000 /* move command to work queue */
1001 twe_enqueue_busy(tr);
1002#ifdef TWE_DEBUG
1003 if (tr->tr_complete != NULL) {
1004 debug(3, "queued request %d with callback %p", tr->tr_command.generic.request_id, tr->tr_complete);
1005 } else if (tr->tr_flags & TWE_CMD_SLEEPER) {
1006 debug(3, "queued request %d with wait channel %p", tr->tr_command.generic.request_id, tr);
1007 } else {
1008 debug(3, "queued request %d for polling caller", tr->tr_command.generic.request_id);
1009 }
1010#endif
1011 }
1012 splx(s); /* drop spl to allow completion interrupts */
1013 }
1014
1015 /* command is enqueued */
1016 if (done)
1017 return(0);
1018
1019 /*
1020 * We couldn't get the controller to take the command; try submitting it again later.
1021 * This should only happen if something is wrong with the controller, or if we have
1022 * overestimated the number of commands it can accept. (Should we actually reject
1023 * the command at this point?)
1024 */
1025 return(EBUSY);
1026}
1027
1028/********************************************************************************
1029 * Poll the controller (sc) for completed commands.
1030 *
1031 * Can be called at any interrupt level, with or without interrupts enabled.
1032 */
1033static void
1034twe_done(struct twe_softc *sc)
1035{
1036 TWE_Response_Queue rq;
1037 struct twe_request *tr;
1038 int s, found;
1039 u_int32_t status_reg;
1040
1041 debug_called(5);
1042
1043 /* loop collecting completed commands */
1044 found = 0;
1045 s = splbio();
1046 for (;;) {
1047 status_reg = TWE_STATUS(sc);
1048 twe_check_bits(sc, status_reg); /* XXX should this fail? */
1049
1050 if (!(status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)) {
1051 found = 1;
1052 rq = TWE_RESPONSE_QUEUE(sc);
1053 tr = sc->twe_lookup[rq.u.response_id]; /* find command */
1054 if (tr->tr_status != TWE_CMD_BUSY)
1055 twe_printf(sc, "completion event for nonbusy command\n");
1056 tr->tr_status = TWE_CMD_COMPLETE;
1057 debug(3, "completed request id %d with status %d",
1058 tr->tr_command.generic.request_id, tr->tr_command.generic.status);
1059 /* move to completed queue */
1060 twe_remove_busy(tr);
1061 twe_enqueue_complete(tr);
1062 } else {
1063 break; /* no response ready */
1064 }
1065 }
1066 splx(s);
1067
1068 /* if we've completed any commands, try posting some more */
1069 if (found)
1070 twe_startio(sc);
1071
1072 /* handle completion and timeouts */
1073 twe_complete(sc); /* XXX use deferred completion? */
1074}
1075
1076/********************************************************************************
1077 * Perform post-completion processing for commands on (sc).
1078 *
1079 * This is split from twe_done as it can be safely deferred and run at a lower
1080 * priority level should facilities for such a thing become available.
1081 */
1082static void
1083twe_complete(struct twe_softc *sc)
1084{
1085 struct twe_request *tr;
1086
1087 debug_called(5);
1088
1089 /*
1090 * Pull commands off the completed list, dispatch them appropriately
1091 */
1092 while ((tr = twe_dequeue_complete(sc)) != NULL) {
1093
1094 /* unmap the command's data buffer */
1095 twe_unmap_request(tr);
1096
1097 /* dispatch to suit command originator */
1098 if (tr->tr_complete != NULL) { /* completion callback */
1099 debug(2, "call completion handler %p", tr->tr_complete);
1100 tr->tr_complete(tr);
1101
1102 } else if (tr->tr_flags & TWE_CMD_SLEEPER) { /* caller is asleep waiting */
1103 debug(2, "wake up command owner on %p", tr);
1104 wakeup_one(tr);
1105
1106 } else { /* caller is polling command */
1107 debug(2, "command left for owner");
1108 }
1109 }
1110}
1111
1112/********************************************************************************
1113 * Wait for (status) to be set in the controller status register for up to
1114 * (timeout) seconds. Returns 0 if found, nonzero if we time out.
1115 *
1116 * Note: this busy-waits, rather than sleeping, since we may be called with
1117 * eg. clock interrupts masked.
1118 */
1119static int
1120twe_wait_status(struct twe_softc *sc, u_int32_t status, int timeout)
1121{
1122 time_t expiry;
1123 u_int32_t status_reg;
1124
1125 debug_called(4);
1126
1127 expiry = time_second + timeout;
1128
1129 do {
1130 status_reg = TWE_STATUS(sc);
1131 if (status_reg & status) /* got the required bit(s)? */
1132 return(0);
1133 DELAY(100000);
1134 } while (time_second <= expiry);
1135
1136 return(1);
1137}
1138
1139/********************************************************************************
1140 * Drain the response queue, which may contain responses to commands we know
1141 * nothing about.
1142 */
1143static int
1144twe_drain_response_queue(struct twe_softc *sc)
1145{
1146 TWE_Response_Queue rq;
1147 u_int32_t status_reg;
1148
1149 debug_called(4);
1150
1151 for (;;) { /* XXX give up eventually? */
1152 status_reg = TWE_STATUS(sc);
1153 if (twe_check_bits(sc, status_reg))
1154 return(1);
1155 if (status_reg & TWE_STATUS_RESPONSE_QUEUE_EMPTY)
1156 return(0);
1157 rq = TWE_RESPONSE_QUEUE(sc);
1158 }
1159}
1160
1161/********************************************************************************
1162 * Soft-reset the controller
1163 */
1164static int
1165twe_soft_reset(struct twe_softc *sc)
1166{
1167 u_int32_t status_reg;
1168
1169 debug_called(2);
1170
1171 TWE_SOFT_RESET(sc);
1172
1173 if (twe_wait_status(sc, TWE_STATUS_ATTENTION_INTERRUPT, 15)) {
1174 twe_printf(sc, "no attention interrupt\n");
1175 return(1);
1176 }
1177 if (twe_drain_aen_queue(sc)) {
1178 twe_printf(sc, "can't drain AEN queue\n");
1179 return(1);
1180 }
1181 if (twe_find_aen(sc, TWE_AEN_SOFT_RESET)) {
1182 twe_printf(sc, "reset not reported\n");
1183 return(1);
1184 }
1185 status_reg = TWE_STATUS(sc);
1186 if (TWE_STATUS_ERRORS(status_reg) || twe_check_bits(sc, status_reg)) {
1187 twe_printf(sc, "controller errors detected\n");
1188 return(1);
1189 }
1190 if (twe_drain_response_queue(sc)) {
1191 twe_printf(sc, "can't drain response queue\n");
1192 return(1);
1193 }
1194 return(0);
1195}
1196
1197/********************************************************************************
1198 ********************************************************************************
1199 Interrupt Handling
1200 ********************************************************************************
1201 ********************************************************************************/
1202
1203/********************************************************************************
1204 * Host interrupt.
1205 *
1206 * XXX what does this mean?
1207 */
1208static void
1209twe_host_intr(struct twe_softc *sc)
1210{
1211 debug_called(4);
1212
1213 twe_printf(sc, "host interrupt\n");
1214 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_HOST_INTERRUPT);
1215}
1216
1217/********************************************************************************
1218 * Attention interrupt.
1219 *
1220 * Signalled when the controller has one or more AENs for us.
1221 */
1222static void
1223twe_attention_intr(struct twe_softc *sc)
1224{
1225 debug_called(4);
1226
1227 /* instigate a poll for AENs */
1228 if (twe_fetch_aen(sc)) {
1229 twe_printf(sc, "error polling for signalled AEN\n");
1230 } else {
1231 TWE_CONTROL(sc, TWE_CONTROL_CLEAR_ATTENTION_INTERRUPT);
1232 }
1233}
1234
1235/********************************************************************************
1236 * Command interrupt.
1237 *
1238 * Signalled when the controller can handle more commands.
1239 */
1240static void
1241twe_command_intr(struct twe_softc *sc)
1242{
1243 debug_called(4);
1244
1245 /*
1246 * We don't use this, rather we try to submit commands when we receive
1247 * them, and when other commands have completed. Mask it so we don't get
1248 * another one.
1249 */
1250 twe_printf(sc, "command interrupt\n");
1251 TWE_CONTROL(sc, TWE_CONTROL_MASK_COMMAND_INTERRUPT);
1252}
1253
1254/********************************************************************************
1255 ********************************************************************************
1256 Asynchronous Event Handling
1257 ********************************************************************************
1258 ********************************************************************************/
1259
1260/********************************************************************************
1261 * Request an AEN from the controller.
1262 */
1263static int
1264twe_fetch_aen(struct twe_softc *sc)
1265{
1266
1267 debug_called(4);
1268
1269 if ((twe_get_param(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, 2, twe_handle_aen)) == NULL)
1270 return(EIO);
1271 return(0);
1272}
1273
1274/********************************************************************************
1275 * Handle an AEN returned by the controller.
1276 */
1277static void
1278twe_handle_aen(struct twe_request *tr)
1279{
1280 struct twe_softc *sc = tr->tr_sc;
1281 TWE_Param *param;
1282 u_int16_t aen;
1283
1284 debug_called(4);
1285
1286 /* XXX check for command success somehow? */
1287
1288 param = (TWE_Param *)tr->tr_data;
1289 aen = *(u_int16_t *)(param->data);
1290
1291 free(tr->tr_data, M_DEVBUF);
1292 twe_release_request(tr);
1293 twe_enqueue_aen(sc, aen);
1294
1295 /* XXX poll for more AENs? */
1296}
1297
1298/********************************************************************************
1299 * Pull AENs out of the controller and park them in the queue, in a context where
1300 * interrupts aren't active. Return nonzero if we encounter any errors in the
1301 * process of obtaining all the available AENs.
1302 */
1303static int
1304twe_drain_aen_queue(struct twe_softc *sc)
1305{
1306 u_int16_t aen;
1307
1308 for (;;) {
1309 if (twe_get_param_2(sc, TWE_PARAM_AEN, TWE_PARAM_AEN_UnitCode, &aen))
1310 return(1);
1311 if (aen == TWE_AEN_QUEUE_EMPTY)
1312 return(0);
1313 twe_enqueue_aen(sc, aen);
1314 }
1315}
1316
1317/********************************************************************************
1318 * Push an AEN that we've received onto the queue.
1319 *
1320 * Note that we have to lock this against reentrance, since it may be called
1321 * from both interrupt and non-interrupt context.
1322 *
1323 * If someone is waiting for the AEN we have, wake them up.
1324 */
1325static void
1326twe_enqueue_aen(struct twe_softc *sc, u_int16_t aen)
1327{
1328 char *msg;
1329 int s, next, nextnext;
1330
1331 debug_called(4);
1332
1333 if ((msg = twe_format_aen(sc, aen)) != NULL)
1334 twe_printf(sc, "AEN: <%s>\n", msg);
1335
1336 s = splbio();
1337 /* enqueue the AEN */
1338 next = ((sc->twe_aen_head + 1) % TWE_Q_LENGTH);
1339 nextnext = ((sc->twe_aen_head + 2) % TWE_Q_LENGTH);
1340
1341 /* check to see if this is the last free slot, and subvert the AEN if it is */
1342 if (nextnext == sc->twe_aen_tail)
1343 aen = TWE_AEN_QUEUE_FULL;
1344
1345 /* look to see if there's room for this AEN */
1346 if (next != sc->twe_aen_tail) {
1347 sc->twe_aen_queue[sc->twe_aen_head] = aen;
1348 sc->twe_aen_head = next;
1349 }
1350
1351 /* wake up anyone asleep on the queue */
1352 wakeup(&sc->twe_aen_queue);
1353
1354 /* anyone looking for this AEN? */
1355 if (sc->twe_wait_aen == aen) {
1356 sc->twe_wait_aen = -1;
1357 wakeup(&sc->twe_wait_aen);
1358 }
1359 splx(s);
1360}
1361
1362/********************************************************************************
1363 * Pop an AEN off the queue, or return -1 if there are none left.
1364 *
1365 * We are more or less interrupt-safe, so don't block interrupts.
1366 */
1367static int
1368twe_dequeue_aen(struct twe_softc *sc)
1369{
1370 int result;
1371
1372 debug_called(4);
1373
1374 if (sc->twe_aen_tail == sc->twe_aen_head) {
1375 result = -1;
1376 } else {
1377 result = sc->twe_aen_queue[sc->twe_aen_tail];
1378 sc->twe_aen_tail = ((sc->twe_aen_tail + 1) % TWE_Q_LENGTH);
1379 }
1380 return(result);
1381}
1382
1383/********************************************************************************
1384 * Check to see if the requested AEN is in the queue.
1385 *
1386 * XXX we could probably avoid masking interrupts here
1387 */
1388static int
1389twe_find_aen(struct twe_softc *sc, u_int16_t aen)
1390{
1391 int i, s, missing;
1392
1393 missing = 1;
1394 s = splbio();
1395 for (i = sc->twe_aen_tail; (i != sc->twe_aen_head) && missing; i = (i + 1) % TWE_Q_LENGTH) {
1396 if (sc->twe_aen_queue[i] == aen)
1397 missing = 0;
1398 }
1399 splx(s);
1400 return(missing);
1401}
1402
1403
1404#if 0 /* currently unused */
1405/********************************************************************************
1406 * Sleep waiting for at least (timeout) seconds until we see (aen) as
1407 * requested. Returns nonzero on timeout or failure.
1408 *
1409 * XXX: this should not be used in cases where there may be more than one sleeper
1410 * without a mechanism for registering multiple sleepers.
1411 */
1412static int
1413twe_wait_aen(struct twe_softc *sc, int aen, int timeout)
1414{
1415 time_t expiry;
1416 int found, s;
1417
1418 debug_called(4);
1419
1420 expiry = time_second + timeout;
1421 found = 0;
1422
1423 s = splbio();
1424 sc->twe_wait_aen = aen;
1425 do {
1426 twe_fetch_aen(sc);
1427 tsleep(&sc->twe_wait_aen, PZERO, "twewaen", hz);
1428 if (sc->twe_wait_aen == -1)
1429 found = 1;
1430 } while ((time_second <= expiry) && !found);
1431 splx(s);
1432 return(!found);
1433}
1434#endif
1435
1436/********************************************************************************
1437 ********************************************************************************
1438 Command Buffer Management
1439 ********************************************************************************
1440 ********************************************************************************/
1441
1442/********************************************************************************
1443 * Get a new command buffer.
1444 *
1445 * This will return NULL if all command buffers are in use.
1446 */
1447static int
1448twe_get_request(struct twe_softc *sc, struct twe_request **tr)
1449{
1450 debug_called(4);
1451
1452 /* try to reuse an old buffer */
1453 *tr = twe_dequeue_free(sc);
1454
1455 /* initialise some fields to their defaults */
1456 if (*tr != NULL) {
1457 (*tr)->tr_data = NULL;
1458 (*tr)->tr_private = NULL;
1459 (*tr)->tr_status = TWE_CMD_SETUP; /* command is in setup phase */
1460 (*tr)->tr_flags = 0;
1461 (*tr)->tr_complete = NULL;
1462 (*tr)->tr_command.generic.status = 0; /* before submission to controller */
1463 (*tr)->tr_command.generic.flags = 0; /* not used */
1464 }
1465 return(*tr == NULL);
1466}
1467
1468/********************************************************************************
1469 * Release a command buffer for reuse.
1470 *
1471 */
1472static void
1473twe_release_request(struct twe_request *tr)
1474{
1475 debug_called(4);
1476
1477 if (tr->tr_private != NULL)
1478 twe_panic(tr->tr_sc, "tr_private != NULL");
1479 twe_enqueue_free(tr);
1480}
1481
1482/********************************************************************************
1483 ********************************************************************************
1484 Debugging
1485 ********************************************************************************
1486 ********************************************************************************/
1487
1488/********************************************************************************
1489 * Print some information about the controller
1490 */
1491void
1492twe_describe_controller(struct twe_softc *sc)
1493{
1494 TWE_Param *p[6];
1495 u_int8_t ports;
1496 u_int32_t size;
1497 int i;
1498
1499 debug_called(2);
1500
1501 /* get the port count */
1502 twe_get_param_1(sc, TWE_PARAM_CONTROLLER, TWE_PARAM_CONTROLLER_PortCount, &ports);
1503
1504 /* get version strings */
1505 p[0] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_Mon, 16, NULL);
1506 p[1] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_FW, 16, NULL);
1507 p[2] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_BIOS, 16, NULL);
1508 p[3] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCB, 8, NULL);
1509 p[4] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_ATA, 8, NULL);
1510 p[5] = twe_get_param(sc, TWE_PARAM_VERSION, TWE_PARAM_VERSION_PCI, 8, NULL);
1511
1512 twe_printf(sc, "%d ports, Firmware %.16s, BIOS %.16s\n", ports, p[1]->data, p[2]->data);
1513 if (bootverbose)
1514 twe_printf(sc, "Monitor %.16s, PCB %.8s, Achip %.8s, Pchip %.8s\n", p[0]->data, p[3]->data,
1515 p[4]->data, p[5]->data);
1516 free(p[0], M_DEVBUF);
1517 free(p[1], M_DEVBUF);
1518 free(p[2], M_DEVBUF);
1519 free(p[3], M_DEVBUF);
1520 free(p[4], M_DEVBUF);
1521 free(p[5], M_DEVBUF);
1522
1523 /* print attached drives */
1524 if (bootverbose) {
1525 p[0] = twe_get_param(sc, TWE_PARAM_DRIVESUMMARY, TWE_PARAM_DRIVESUMMARY_Status, 16, NULL);
1526 for (i = 0; i < ports; i++) {
1527 if (p[0]->data[i] != TWE_PARAM_DRIVESTATUS_Present)
1528 continue;
1529 twe_get_param_4(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Size, &size);
1530 p[1] = twe_get_param(sc, TWE_PARAM_DRIVEINFO + i, TWE_PARAM_DRIVEINFO_Model, 40, NULL);
1531 if (p[1] != NULL) {
1532 twe_printf(sc, "port %d: %.40s %dMB\n", i, p[1]->data, size / 2048);
1533 free(p[1], M_DEVBUF);
1534 } else {
1535 twe_printf(sc, "port %d, drive status unavailable\n", i);
1536 }
1537 }
1538 free(p[0], M_DEVBUF);
1539 }
1540}
1541
1542/********************************************************************************
1543 * Complain if the status bits aren't what we're expecting.
1544 *
1545 * Rate-limit the complaints to at most one of each every five seconds, but
1546 * always return the correct status.
1547 */
1548static int
1549twe_check_bits(struct twe_softc *sc, u_int32_t status_reg)
1550{
1551 int result;
1552 static time_t lastwarn[2] = {0, 0};
1553
1554 /*
1555 * This can be a little problematic, as twe_panic may call twe_reset if
1556 * TWE_DEBUG is not set, which will call us again as part of the soft reset.
1557 */
1558 if ((status_reg & TWE_STATUS_PANIC_BITS) != 0) {
1559 twe_printf(sc, "FATAL STATUS BIT(S) %b\n", status_reg & TWE_STATUS_PANIC_BITS,
1560 TWE_STATUS_BITS_DESCRIPTION);
1561 twe_panic(sc, "fatal status bits");
1562 }
1563
1564 result = 0;
1565 if ((status_reg & TWE_STATUS_EXPECTED_BITS) != TWE_STATUS_EXPECTED_BITS) {
1566 if (time_second > (lastwarn[0] + 5)) {
1567 twe_printf(sc, "missing expected status bit(s) %b\n", ~status_reg & TWE_STATUS_EXPECTED_BITS,
1568 TWE_STATUS_BITS_DESCRIPTION);
1569 lastwarn[0] = time_second;
1570 }
1571 result = 1;
1572 }
1573
1574 if ((status_reg & TWE_STATUS_UNEXPECTED_BITS) != 0) {
1575 if (time_second > (lastwarn[1] + 5)) {
1576 twe_printf(sc, "unexpected status bit(s) %b\n", status_reg & TWE_STATUS_UNEXPECTED_BITS,
1577 TWE_STATUS_BITS_DESCRIPTION);
1578 lastwarn[1] = time_second;
1579 }
1580 result = 1;
1581 }
1582
1583 return(result);
1584}
1585
1586/********************************************************************************
1587 * Return a string describing (aen).
1588 *
1589 * The low 8 bits of the aen are the code, the high 8 bits give the unit number
1590 * where an AEN is specific to a unit.
1591 *
1592 * Note that we could expand this routine to handle eg. up/downgrading the status
1593 * of a drive if we had some idea of what the drive's initial status was.
1594 */
1595
1596static char *
1597twe_format_aen(struct twe_softc *sc, u_int16_t aen)
1598{
1599 static char buf[80];
1600 device_t child;
1601 char *code, *msg;
1602
1603 code = twe_describe_code(twe_table_aen, TWE_AEN_CODE(aen));
1604 msg = code + 2;
1605
1606 switch (*code) {
1607 case 'q':
1608 if (!bootverbose)
1609 return(NULL);
1610 /* FALLTHROUGH */
1611 case 'p':
1612 return(msg);
1613
1614 case 'c':
1615 if ((child = sc->twe_drive[TWE_AEN_UNIT(aen)].td_disk) != NULL) {
1616 sprintf(buf, "twed%d: %s", device_get_unit(child), msg);
1617 } else {
1618 sprintf(buf, "twe%d: %s for unknown unit %d", device_get_unit(sc->twe_dev),
1619 msg, TWE_AEN_UNIT(aen));
1620 }
1621 return(buf);
1622
1623 case 'x':
1624 default:
1625 break;
1626 }
1627 sprintf(buf, "unknown AEN 0x%x", aen);
1628 return(buf);
1629}
1630
1631/********************************************************************************
1632 * Print a diagnostic if the status of the command warrants it, and return
1633 * either zero (command was ok) or nonzero (command failed).
1634 */
1635static int
1636twe_report_request(struct twe_request *tr)
1637{
1638 struct twe_softc *sc = tr->tr_sc;
1639 TWE_Command *cmd = &tr->tr_command;
1640 int result;
1640 int result = 0;
1641
1641
1642 switch (cmd->generic.flags) {
1643 case TWE_FLAGS_SUCCESS:
1644 result = 0;
1645 break;
1646 case TWE_FLAGS_INFORMATIONAL:
1647 case TWE_FLAGS_WARNING:
1648 twe_printf(sc, "command completed - %s\n",
1649 twe_describe_code(twe_table_status, cmd->generic.status));
1650 result = 0;
1651 break;
1652
1653 case TWE_FLAGS_FATAL:
1654 default:
1655 twe_printf(sc, "command failed - %s\n",
1656 twe_describe_code(twe_table_status, cmd->generic.status));
1642 /*
1643 * Check the command status value and handle accordingly.
1644 */
1645 if (cmd->generic.status == TWE_STATUS_RESET) {
1646 /*
1647 * The status code 0xff requests a controller reset.
1648 */
1649 twe_printf(sc, "command returned with controller rest request\n");
1650 twe_reset(sc);
1657 result = 1;
1651 result = 1;
1658
1652 } else if (cmd->generic.status > TWE_STATUS_FATAL) {
1659 /*
1653 /*
1660 * The status code 0xff requests a controller reset
1654 * Fatal errors that don't require controller reset.
1661 */
1655 */
1662 if (cmd->generic.status == 0xff)
1663 twe_reset(sc);
1664 break;
1656 twe_printf(sc, "command returned fatal status - %s (flags = 0x%x)\n",
1657 twe_describe_code(twe_table_status, cmd->generic.status),
1658 cmd->generic.flags);
1659 result = 1;
1660 } else if (cmd->generic.status > TWE_STATUS_WARNING) {
1661 /*
1662 * Warning level status.
1663 */
1664 twe_printf(sc, "command returned warning status - %s (flags = 0x%x)\n",
1665 twe_describe_code(twe_table_status, cmd->generic.status),
1666 cmd->generic.flags);
1667 } else if (cmd->generic.status > 0x40) {
1668 /*
1669 * Info level status.
1670 */
1671 twe_printf(sc, "command returned info status: %s (flags = 0x%x)\n",
1672 twe_describe_code(twe_table_status, cmd->generic.status),
1673 cmd->generic.flags);
1665 }
1674 }
1675
1666 return(result);
1667}
1668
1669/********************************************************************************
1670 * Print some controller state to aid in debugging error/panic conditions
1671 */
1672void
1673twe_print_controller(struct twe_softc *sc)
1674{
1675 u_int32_t status_reg;
1676
1677 status_reg = TWE_STATUS(sc);
1678 twe_printf(sc, "status %b\n", status_reg, TWE_STATUS_BITS_DESCRIPTION);
1679 twe_printf(sc, " current max\n");
1680 twe_printf(sc, "free %04d %04d\n", sc->twe_qstat[TWEQ_FREE].q_length, sc->twe_qstat[TWEQ_FREE].q_max);
1681 twe_printf(sc, "ready %04d %04d\n", sc->twe_qstat[TWEQ_READY].q_length, sc->twe_qstat[TWEQ_READY].q_max);
1682 twe_printf(sc, "busy %04d %04d\n", sc->twe_qstat[TWEQ_BUSY].q_length, sc->twe_qstat[TWEQ_BUSY].q_max);
1683 twe_printf(sc, "complete %04d %04d\n", sc->twe_qstat[TWEQ_COMPLETE].q_length, sc->twe_qstat[TWEQ_COMPLETE].q_max);
1684 twe_printf(sc, "bioq %04d %04d\n", sc->twe_qstat[TWEQ_BIO].q_length, sc->twe_qstat[TWEQ_BIO].q_max);
1685 twe_printf(sc, "AEN queue head %d tail %d\n", sc->twe_aen_head, sc->twe_aen_tail);
1686}
1687
1688static void
1689twe_panic(struct twe_softc *sc, char *reason)
1690{
1691 twe_print_controller(sc);
1692#ifdef TWE_DEBUG
1693 panic(reason);
1694#else
1695 twe_reset(sc);
1696#endif
1697}
1698
1699#if 0
1700/********************************************************************************
1701 * Print a request/command in human-readable format.
1702 */
1703static void
1704twe_print_request(struct twe_request *tr)
1705{
1706 struct twe_softc *sc = tr->tr_sc;
1707 TWE_Command *cmd = &tr->tr_command;
1708 int i;
1709
1710 twe_printf(sc, "CMD: request_id %d opcode <%s> size %d unit %d host_id %d\n",
1711 cmd->generic.request_id, twe_describe_code(twe_table_opcode, cmd->generic.opcode), cmd->generic.size,
1712 cmd->generic.unit, cmd->generic.host_id);
1713 twe_printf(sc, " status %d flags 0x%x count %d sgl_offset %d\n",
1714 cmd->generic.status, cmd->generic.flags, cmd->generic.count, cmd->generic.sgl_offset);
1715
1716 switch(cmd->generic.opcode) { /* XXX add more opcodes? */
1717 case TWE_OP_READ:
1718 case TWE_OP_WRITE:
1719 twe_printf(sc, " lba %d\n", cmd->io.lba);
1720 for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->io.sgl[i].length != 0); i++)
1721 twe_printf(sc, " %d: 0x%x/%d\n",
1722 i, cmd->io.sgl[i].address, cmd->io.sgl[i].length);
1723 break;
1724
1725 case TWE_OP_GET_PARAM:
1726 case TWE_OP_SET_PARAM:
1727 for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->param.sgl[i].length != 0); i++)
1728 twe_printf(sc, " %d: 0x%x/%d\n",
1729 i, cmd->param.sgl[i].address, cmd->param.sgl[i].length);
1730 break;
1731
1732 case TWE_OP_INIT_CONNECTION:
1733 twe_printf(sc, " response queue pointer 0x%x\n",
1734 cmd->initconnection.response_queue_pointer);
1735 break;
1736
1737 default:
1738 break;
1739 }
1740 twe_printf(sc, " tr_command %p/0x%x tr_data %p/0x%x,%d\n",
1741 tr, tr->tr_cmdphys, tr->tr_data, tr->tr_dataphys, tr->tr_length);
1742 twe_printf(sc, " tr_status %d tr_flags 0x%x tr_complete %p tr_private %p\n",
1743 tr->tr_status, tr->tr_flags, tr->tr_complete, tr->tr_private);
1744}
1745
1746#endif
1676 return(result);
1677}
1678
1679/********************************************************************************
1680 * Print some controller state to aid in debugging error/panic conditions
1681 */
1682void
1683twe_print_controller(struct twe_softc *sc)
1684{
1685 u_int32_t status_reg;
1686
1687 status_reg = TWE_STATUS(sc);
1688 twe_printf(sc, "status %b\n", status_reg, TWE_STATUS_BITS_DESCRIPTION);
1689 twe_printf(sc, " current max\n");
1690 twe_printf(sc, "free %04d %04d\n", sc->twe_qstat[TWEQ_FREE].q_length, sc->twe_qstat[TWEQ_FREE].q_max);
1691 twe_printf(sc, "ready %04d %04d\n", sc->twe_qstat[TWEQ_READY].q_length, sc->twe_qstat[TWEQ_READY].q_max);
1692 twe_printf(sc, "busy %04d %04d\n", sc->twe_qstat[TWEQ_BUSY].q_length, sc->twe_qstat[TWEQ_BUSY].q_max);
1693 twe_printf(sc, "complete %04d %04d\n", sc->twe_qstat[TWEQ_COMPLETE].q_length, sc->twe_qstat[TWEQ_COMPLETE].q_max);
1694 twe_printf(sc, "bioq %04d %04d\n", sc->twe_qstat[TWEQ_BIO].q_length, sc->twe_qstat[TWEQ_BIO].q_max);
1695 twe_printf(sc, "AEN queue head %d tail %d\n", sc->twe_aen_head, sc->twe_aen_tail);
1696}
1697
1698static void
1699twe_panic(struct twe_softc *sc, char *reason)
1700{
1701 twe_print_controller(sc);
1702#ifdef TWE_DEBUG
1703 panic(reason);
1704#else
1705 twe_reset(sc);
1706#endif
1707}
1708
1709#if 0
1710/********************************************************************************
1711 * Print a request/command in human-readable format.
1712 */
1713static void
1714twe_print_request(struct twe_request *tr)
1715{
1716 struct twe_softc *sc = tr->tr_sc;
1717 TWE_Command *cmd = &tr->tr_command;
1718 int i;
1719
1720 twe_printf(sc, "CMD: request_id %d opcode <%s> size %d unit %d host_id %d\n",
1721 cmd->generic.request_id, twe_describe_code(twe_table_opcode, cmd->generic.opcode), cmd->generic.size,
1722 cmd->generic.unit, cmd->generic.host_id);
1723 twe_printf(sc, " status %d flags 0x%x count %d sgl_offset %d\n",
1724 cmd->generic.status, cmd->generic.flags, cmd->generic.count, cmd->generic.sgl_offset);
1725
1726 switch(cmd->generic.opcode) { /* XXX add more opcodes? */
1727 case TWE_OP_READ:
1728 case TWE_OP_WRITE:
1729 twe_printf(sc, " lba %d\n", cmd->io.lba);
1730 for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->io.sgl[i].length != 0); i++)
1731 twe_printf(sc, " %d: 0x%x/%d\n",
1732 i, cmd->io.sgl[i].address, cmd->io.sgl[i].length);
1733 break;
1734
1735 case TWE_OP_GET_PARAM:
1736 case TWE_OP_SET_PARAM:
1737 for (i = 0; (i < TWE_MAX_SGL_LENGTH) && (cmd->param.sgl[i].length != 0); i++)
1738 twe_printf(sc, " %d: 0x%x/%d\n",
1739 i, cmd->param.sgl[i].address, cmd->param.sgl[i].length);
1740 break;
1741
1742 case TWE_OP_INIT_CONNECTION:
1743 twe_printf(sc, " response queue pointer 0x%x\n",
1744 cmd->initconnection.response_queue_pointer);
1745 break;
1746
1747 default:
1748 break;
1749 }
1750 twe_printf(sc, " tr_command %p/0x%x tr_data %p/0x%x,%d\n",
1751 tr, tr->tr_cmdphys, tr->tr_data, tr->tr_dataphys, tr->tr_length);
1752 twe_printf(sc, " tr_status %d tr_flags 0x%x tr_complete %p tr_private %p\n",
1753 tr->tr_status, tr->tr_flags, tr->tr_complete, tr->tr_private);
1754}
1755
1756#endif