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