timeout/untimeout ==> callout_*
[dragonfly.git] / sys / dev / raid / amr / amr.c
1 /*-
2  * Copyright (c) 1999,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  * Copyright (c) 2002 Eric Moore
28  * Copyright (c) 2002 LSI Logic Corporation
29  * All rights reserved.
30  *
31  * Redistribution and use in source and binary forms, with or without
32  * modification, are permitted provided that the following conditions
33  * are met:
34  * 1. Redistributions of source code must retain the above copyright
35  *    notice, this list of conditions and the following disclaimer.
36  * 2. Redistributions in binary form must reproduce the above copyright
37  *    notice, this list of conditions and the following disclaimer in the
38  *    documentation and/or other materials provided with the distribution.
39  * 3. The party using or redistributing the source code and binary forms
40  *    agrees to the disclaimer below and the terms and conditions set forth
41  *    herein.
42  *
43  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
44  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
47  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53  * SUCH DAMAGE.
54  *
55  *      $FreeBSD: src/sys/dev/amr/amr.c,v 1.7.2.13 2003/01/15 13:41:18 emoore Exp $
56  *      $DragonFly: src/sys/dev/raid/amr/amr.c,v 1.13 2004/09/15 16:25:12 joerg Exp $
57  */
58
59 /*
60  * Driver for the AMI MegaRaid family of controllers.
61  */
62
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/malloc.h>
66 #include <sys/kernel.h>
67
68 #include "amr_compat.h"
69 #include <sys/bus.h>
70 #include <sys/conf.h>
71 #include <sys/devicestat.h>
72 #include <sys/disk.h>
73 #include <sys/stat.h>
74
75 #include <machine/bus_memio.h>
76 #include <machine/bus_pio.h>
77 #include <machine/bus.h>
78 #include <machine/resource.h>
79 #include <sys/rman.h>
80
81 #include <bus/pci/pcireg.h>
82 #include <bus/pci/pcivar.h>
83
84 #include "amrio.h"
85 #include "amrreg.h"
86 #include "amrvar.h"
87 #define AMR_DEFINE_TABLES
88 #include "amr_tables.h"
89
90 #define AMR_CDEV_MAJOR  132
91
92 static d_open_t         amr_open;
93 static d_close_t        amr_close;
94 static d_ioctl_t        amr_ioctl;
95
96 static struct cdevsw amr_cdevsw = {
97                 /* name */      "amr",
98                 /* maj */       AMR_CDEV_MAJOR,
99                 /* flags */     0,
100                 /* port */      NULL,
101                 /* clone */     NULL,
102
103                 /* open */      amr_open,
104                 /* close */     amr_close,
105                 /* read */      noread,
106                 /* write */     nowrite,
107                 /* ioctl */     amr_ioctl,
108                 /* poll */      nopoll,
109                 /* mmap */      nommap,
110                 /* strategy */  nostrategy,
111                 /* dump */      nodump,
112                 /* psize */     nopsize
113 };
114
115 /*
116  * Initialisation, bus interface.
117  */
118 static void     amr_startup(void *arg);
119
120 /*
121  * Command wrappers
122  */
123 static int      amr_query_controller(struct amr_softc *sc);
124 static void     *amr_enquiry(struct amr_softc *sc, size_t bufsize, 
125                              u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual);
126 static void     amr_completeio(struct amr_command *ac);
127 static int      amr_support_ext_cdb(struct amr_softc *sc);
128
129 /*
130  * Command buffer allocation.
131  */
132 static void     amr_alloccmd_cluster(struct amr_softc *sc);
133 static void     amr_freecmd_cluster(struct amr_command_cluster *acc);
134
135 /*
136  * Command processing.
137  */
138 static int      amr_bio_command(struct amr_softc *sc, struct amr_command **acp);
139 static int      amr_wait_command(struct amr_command *ac);
140 static int      amr_getslot(struct amr_command *ac);
141 static void     amr_mapcmd(struct amr_command *ac);
142 static void     amr_unmapcmd(struct amr_command *ac);
143 static int      amr_start(struct amr_command *ac);
144 static void     amr_complete(void *context, int pending);
145
146 /*
147  * Status monitoring
148  */
149 static void     amr_periodic(void *data);
150
151 /*
152  * Interface-specific shims
153  */
154 static int      amr_quartz_submit_command(struct amr_softc *sc);
155 static int      amr_quartz_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave);
156 static int      amr_quartz_poll_command(struct amr_command *ac);
157
158 static int      amr_std_submit_command(struct amr_softc *sc);
159 static int      amr_std_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave);
160 static int      amr_std_poll_command(struct amr_command *ac);
161 static void     amr_std_attach_mailbox(struct amr_softc *sc);
162
163 #ifdef AMR_BOARD_INIT
164 static int      amr_quartz_init(struct amr_softc *sc);
165 static int      amr_std_init(struct amr_softc *sc);
166 #endif
167
168 /*
169  * Debugging
170  */
171 static void     amr_describe_controller(struct amr_softc *sc);
172 #ifdef AMR_DEBUG
173 #if 0
174 static void     amr_printcommand(struct amr_command *ac);
175 #endif
176 #endif
177
178 DECLARE_DUMMY_MODULE(amr);
179
180 /********************************************************************************
181  ********************************************************************************
182                                                                       Inline Glue
183  ********************************************************************************
184  ********************************************************************************/
185
186 /********************************************************************************
187  ********************************************************************************
188                                                                 Public Interfaces
189  ********************************************************************************
190  ********************************************************************************/
191
192 /********************************************************************************
193  * Initialise the controller and softc.
194  */
195 int
196 amr_attach(struct amr_softc *sc)
197 {
198
199     debug_called(1);
200
201     /*
202      * Initialise per-controller queues.
203      */
204     TAILQ_INIT(&sc->amr_completed);
205     TAILQ_INIT(&sc->amr_freecmds);
206     TAILQ_INIT(&sc->amr_cmd_clusters);
207     TAILQ_INIT(&sc->amr_ready);
208     bioq_init(&sc->amr_bioq);
209
210 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005
211     /*
212      * Initialise command-completion task.
213      */
214     TASK_INIT(&sc->amr_task_complete, 0, amr_complete, sc);
215 #endif
216
217     debug(2, "queue init done");
218
219     /*
220      * Configure for this controller type.
221      */
222     if (AMR_IS_QUARTZ(sc)) {
223         sc->amr_submit_command = amr_quartz_submit_command;
224         sc->amr_get_work       = amr_quartz_get_work;
225         sc->amr_poll_command   = amr_quartz_poll_command;
226     } else {
227         sc->amr_submit_command = amr_std_submit_command;
228         sc->amr_get_work       = amr_std_get_work;
229         sc->amr_poll_command   = amr_std_poll_command;
230         amr_std_attach_mailbox(sc);;
231     }
232
233 #ifdef AMR_BOARD_INIT
234     if ((AMR_IS_QUARTZ(sc) ? amr_quartz_init(sc) : amr_std_init(sc))))
235         return(ENXIO);
236 #endif
237
238     /*
239      * Quiz controller for features and limits.
240      */
241     if (amr_query_controller(sc))
242         return(ENXIO);
243
244     debug(2, "controller query complete");
245
246     /*
247      * Attach our 'real' SCSI channels to CAM.
248      */
249     if (amr_cam_attach(sc))
250         return(ENXIO);
251     debug(2, "CAM attach done");
252
253     /*
254      * Create the control device.
255      */
256     cdevsw_add(&amr_cdevsw, -1, device_get_unit(sc->amr_dev));
257     sc->amr_dev_t = make_dev(&amr_cdevsw, device_get_unit(sc->amr_dev), 
258                             UID_ROOT, GID_OPERATOR, S_IRUSR | S_IWUSR,
259                             "amr%d", device_get_unit(sc->amr_dev));
260     sc->amr_dev_t->si_drv1 = sc;
261     reference_dev(sc->amr_dev_t);
262
263     /*
264      * Schedule ourselves to bring the controller up once interrupts are
265      * available.
266      */
267     bzero(&sc->amr_ich, sizeof(struct intr_config_hook));
268     sc->amr_ich.ich_func = amr_startup;
269     sc->amr_ich.ich_arg = sc;
270     if (config_intrhook_establish(&sc->amr_ich) != 0) {
271         device_printf(sc->amr_dev, "can't establish configuration hook\n");
272         return(ENOMEM);
273     }
274
275     /*
276      * Print a little information about the controller.
277      */
278     amr_describe_controller(sc);
279
280     debug(2, "attach complete");
281     return(0);
282 }
283
284 /********************************************************************************
285  * Locate disk resources and attach children to them.
286  */
287 static void
288 amr_startup(void *arg)
289 {
290     struct amr_softc    *sc = (struct amr_softc *)arg;
291     struct amr_logdrive *dr;
292     int                 i, error;
293     
294     debug_called(1);
295     callout_init(&sc->amr_timeout);
296
297     /* pull ourselves off the intrhook chain */
298     config_intrhook_disestablish(&sc->amr_ich);
299
300     /* get up-to-date drive information */
301     if (amr_query_controller(sc)) {
302         device_printf(sc->amr_dev, "can't scan controller for drives\n");
303         return;
304     }
305
306     /* iterate over available drives */
307     for (i = 0, dr = &sc->amr_drive[0]; (i < AMR_MAXLD) && (dr->al_size != 0xffffffff); i++, dr++) {
308         /* are we already attached to this drive? */
309         if (dr->al_disk == 0) {
310             /* generate geometry information */
311             if (dr->al_size > 0x200000) {       /* extended translation? */
312                 dr->al_heads = 255;
313                 dr->al_sectors = 63;
314             } else {
315                 dr->al_heads = 64;
316                 dr->al_sectors = 32;
317             }
318             dr->al_cylinders = dr->al_size / (dr->al_heads * dr->al_sectors);
319             
320             dr->al_disk = device_add_child(sc->amr_dev, NULL, -1);
321             if (dr->al_disk == 0)
322                 device_printf(sc->amr_dev, "device_add_child failed\n");
323             device_set_ivars(dr->al_disk, dr);
324         }
325     }
326     
327     if ((error = bus_generic_attach(sc->amr_dev)) != 0)
328         device_printf(sc->amr_dev, "bus_generic_attach returned %d\n", error);
329     
330     /* mark controller back up */
331     sc->amr_state &= ~AMR_STATE_SHUTDOWN;
332
333     /* interrupts will be enabled before we do anything more */
334     sc->amr_state |= AMR_STATE_INTEN;
335
336     /*
337      * Start the timeout routine.
338      */
339 /*    callout_reset(&sc->amr_timeout, hz, amr_periodic, sc); */
340
341     return;
342 }
343
344 /*******************************************************************************
345  * Free resources associated with a controller instance
346  */
347 void
348 amr_free(struct amr_softc *sc)
349 {
350     struct amr_command_cluster  *acc;
351
352     /* detach from CAM */
353     amr_cam_detach(sc);
354
355     /* cancel status timeout */
356     callout_stop(&sc->amr_timeout);
357     
358     /* throw away any command buffers */
359     while ((acc = TAILQ_FIRST(&sc->amr_cmd_clusters)) != NULL) {
360         TAILQ_REMOVE(&sc->amr_cmd_clusters, acc, acc_link);
361         amr_freecmd_cluster(acc);
362     }
363
364     /* destroy control device */
365     if( sc->amr_dev_t != (dev_t)NULL)
366             destroy_dev(sc->amr_dev_t);
367     cdevsw_remove(&amr_cdevsw, -1, device_get_unit(sc->amr_dev));
368 }
369
370 /*******************************************************************************
371  * Receive a bio structure from a child device and queue it on a particular
372  * disk resource, then poke the disk resource to start as much work as it can.
373  */
374 int
375 amr_submit_bio(struct amr_softc *sc, struct bio *bio)
376 {
377     debug_called(2);
378
379     amr_enqueue_bio(sc, bio);
380     amr_startio(sc);
381     return(0);
382 }
383
384 /********************************************************************************
385  * Accept an open operation on the control device.
386  */
387 static int
388 amr_open(dev_t dev, int flags, int fmt, d_thread_t *td)
389 {
390     int                 unit = minor(dev);
391     struct amr_softc    *sc = devclass_get_softc(devclass_find("amr"), unit);
392
393     debug_called(1);
394
395     sc->amr_state |= AMR_STATE_OPEN;
396     return(0);
397 }
398
399 /********************************************************************************
400  * Accept the last close on the control device.
401  */
402 static int
403 amr_close(dev_t dev, int flags, int fmt, d_thread_t *td)
404 {
405     int                 unit = minor(dev);
406     struct amr_softc    *sc = devclass_get_softc(devclass_find("amr"), unit);
407
408     debug_called(1);
409
410     sc->amr_state &= ~AMR_STATE_OPEN;
411     return (0);
412 }
413
414 /********************************************************************************
415  * Handle controller-specific control operations.
416  */
417 static int
418 amr_ioctl(dev_t dev, u_long cmd, caddr_t addr, int32_t flag, d_thread_t *td)
419 {
420     struct amr_softc            *sc = (struct amr_softc *)dev->si_drv1;
421     int                         *arg = (int *)addr;
422     struct amr_user_ioctl       *au = (struct amr_user_ioctl *)addr;
423     struct amr_command          *ac;
424     struct amr_mailbox_ioctl    *mbi;
425     struct amr_passthrough      *ap;
426     void                        *dp;
427     int                         error;
428
429     debug_called(1);
430
431     error = 0;
432     dp = NULL;
433     ap = NULL;
434     ac = NULL;
435     switch(cmd) {
436
437     case AMR_IO_VERSION:
438         debug(1, "AMR_IO_VERSION");
439         *arg = AMR_IO_VERSION_NUMBER;
440         break;
441
442     case AMR_IO_COMMAND:
443         debug(1, "AMR_IO_COMMAND  0x%x", au->au_cmd[0]);
444         /* handle inbound data buffer */
445         if (au->au_length != 0) {
446             if ((dp = malloc(au->au_length, M_DEVBUF, M_WAITOK)) == NULL) {
447                 error = ENOMEM;
448                 break;
449             }
450             if ((error = copyin(au->au_buffer, dp, au->au_length)) != 0)
451                 break;
452             debug(2, "copyin %ld bytes from %p -> %p", au->au_length, au->au_buffer, dp);
453         }
454
455         if ((ac = amr_alloccmd(sc)) == NULL) {
456             error = ENOMEM;
457             break;
458         }
459
460         /* handle SCSI passthrough command */
461         if (au->au_cmd[0] == AMR_CMD_PASS) {
462             if ((ap = malloc(sizeof(*ap), M_DEVBUF, M_WAITOK | M_ZERO)) == NULL) {
463                 error = ENOMEM;
464                 break;
465             }
466
467             /* copy cdb */
468             ap->ap_cdb_length = au->au_cmd[2];
469             bcopy(&au->au_cmd[3], &ap->ap_cdb[0], ap->ap_cdb_length);
470
471             /* build passthrough */
472             ap->ap_timeout              = au->au_cmd[ap->ap_cdb_length + 3] & 0x07;
473             ap->ap_ars                  = (au->au_cmd[ap->ap_cdb_length + 3] & 0x08) ? 1 : 0;
474             ap->ap_islogical            = (au->au_cmd[ap->ap_cdb_length + 3] & 0x80) ? 1 : 0;
475             ap->ap_logical_drive_no     = au->au_cmd[ap->ap_cdb_length + 4];
476             ap->ap_channel              = au->au_cmd[ap->ap_cdb_length + 5];
477             ap->ap_scsi_id              = au->au_cmd[ap->ap_cdb_length + 6];
478             ap->ap_request_sense_length = 14;
479             ap->ap_data_transfer_length = au->au_length;
480             /* XXX what about the request-sense area? does the caller want it? */
481
482             /* build command */
483             ac->ac_data = ap;
484             ac->ac_length = sizeof(*ap);
485             ac->ac_flags |= AMR_CMD_DATAOUT;
486             ac->ac_ccb_data = dp;
487             ac->ac_ccb_length = au->au_length;
488             if (au->au_direction & AMR_IO_READ)
489                 ac->ac_flags |= AMR_CMD_CCB_DATAIN;
490             if (au->au_direction & AMR_IO_WRITE)
491                 ac->ac_flags |= AMR_CMD_CCB_DATAOUT;
492
493             ac->ac_mailbox.mb_command = AMR_CMD_PASS;
494
495         } else {
496             /* direct command to controller */
497             mbi = (struct amr_mailbox_ioctl *)&ac->ac_mailbox;
498
499             /* copy pertinent mailbox items */
500             mbi->mb_command = au->au_cmd[0];
501             mbi->mb_channel = au->au_cmd[1];
502             mbi->mb_param = au->au_cmd[2];
503             mbi->mb_pad[0] = au->au_cmd[3];
504             mbi->mb_drive = au->au_cmd[4];
505
506             /* build the command */
507             ac->ac_data = dp;
508             ac->ac_length = au->au_length;
509             if (au->au_direction & AMR_IO_READ)
510                 ac->ac_flags |= AMR_CMD_DATAIN;
511             if (au->au_direction & AMR_IO_WRITE)
512                 ac->ac_flags |= AMR_CMD_DATAOUT;
513         }
514
515         /* run the command */
516         if ((error = amr_wait_command(ac)) != 0)
517             break;
518
519         /* copy out data and set status */
520         if (au->au_length != 0)
521             error = copyout(dp, au->au_buffer, au->au_length);
522         debug(2, "copyout %ld bytes from %p -> %p", au->au_length, dp, au->au_buffer);
523         if (dp != NULL)
524             debug(2, "%16d", (int)dp);
525         au->au_status = ac->ac_status;
526         break;
527
528     default:
529         debug(1, "unknown ioctl 0x%lx", cmd);
530         error = ENOIOCTL;
531         break;
532     }
533
534     if (dp != NULL)
535         free(dp, M_DEVBUF);
536     if (ap != NULL)
537         free(ap, M_DEVBUF);
538     if (ac != NULL)
539         amr_releasecmd(ac);
540     return(error);
541 }
542
543 /********************************************************************************
544  ********************************************************************************
545                                                                 Status Monitoring
546  ********************************************************************************
547  ********************************************************************************/
548
549 /********************************************************************************
550  * Perform a periodic check of the controller status
551  */
552 static void
553 amr_periodic(void *data)
554 {
555     struct amr_softc    *sc = (struct amr_softc *)data;
556
557     debug_called(2);
558
559     /* XXX perform periodic status checks here */
560
561     /* compensate for missed interrupts */
562     amr_done(sc);
563
564     /* reschedule */
565     callout_reset(&sc->amr_timeout, hz, amr_periodic, sc);
566 }
567
568 /********************************************************************************
569  ********************************************************************************
570                                                                  Command Wrappers
571  ********************************************************************************
572  ********************************************************************************/
573
574 /********************************************************************************
575  * Interrogate the controller for the operational parameters we require.
576  */
577 static int
578 amr_query_controller(struct amr_softc *sc)
579 {
580     struct amr_enquiry3 *aex;
581     struct amr_prodinfo *ap;
582     struct amr_enquiry  *ae;
583     int                 ldrv;
584
585     /* 
586      * If we haven't found the real limit yet, let us have a couple of commands in
587      * order to be able to probe.
588      */
589     if (sc->amr_maxio == 0)
590         sc->amr_maxio = 2;
591
592     /*
593      * Greater than 10 byte cdb support
594      */
595     sc->support_ext_cdb = amr_support_ext_cdb(sc);
596
597     if(sc->support_ext_cdb) {
598         debug(2,"supports extended CDBs.");
599     }
600
601     /* 
602      * Try to issue an ENQUIRY3 command 
603      */
604     if ((aex = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_ENQ3, 
605                            AMR_CONFIG_ENQ3_SOLICITED_FULL)) != NULL) {
606
607         /*
608          * Fetch current state of logical drives.
609          */
610         for (ldrv = 0; ldrv < aex->ae_numldrives; ldrv++) {
611             sc->amr_drive[ldrv].al_size       = aex->ae_drivesize[ldrv];
612             sc->amr_drive[ldrv].al_state      = aex->ae_drivestate[ldrv];
613             sc->amr_drive[ldrv].al_properties = aex->ae_driveprop[ldrv];
614             debug(2, "  drive %d: %d state %x properties %x\n", ldrv, sc->amr_drive[ldrv].al_size,
615                   sc->amr_drive[ldrv].al_state, sc->amr_drive[ldrv].al_properties);
616         }
617         free(aex, M_DEVBUF);
618
619         /*
620          * Get product info for channel count.
621          */
622         if ((ap = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0)) == NULL) {
623             device_printf(sc->amr_dev, "can't obtain product data from controller\n");
624             return(1);
625         }
626         sc->amr_maxdrives = 40;
627         sc->amr_maxchan = ap->ap_nschan;
628         sc->amr_maxio = ap->ap_maxio;
629         sc->amr_type |= AMR_TYPE_40LD;
630         free(ap, M_DEVBUF);
631
632     } else {
633
634         /* failed, try the 8LD ENQUIRY commands */
635         if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_EXT_ENQUIRY2, 0, 0)) == NULL) {
636             if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_ENQUIRY, 0, 0)) == NULL) {
637                 device_printf(sc->amr_dev, "can't obtain configuration data from controller\n");
638                 return(1);
639             }
640             ae->ae_signature = 0;
641         }
642
643         /*
644          * Fetch current state of logical drives.
645          */
646         for (ldrv = 0; ldrv < ae->ae_ldrv.al_numdrives; ldrv++) {
647             sc->amr_drive[ldrv].al_size       = ae->ae_ldrv.al_size[ldrv];
648             sc->amr_drive[ldrv].al_state      = ae->ae_ldrv.al_state[ldrv];
649             sc->amr_drive[ldrv].al_properties = ae->ae_ldrv.al_properties[ldrv];
650             debug(2, "  drive %d: %d state %x properties %x\n", ldrv, sc->amr_drive[ldrv].al_size,
651                   sc->amr_drive[ldrv].al_state, sc->amr_drive[ldrv].al_properties);
652         }
653
654         sc->amr_maxdrives = 8;
655         sc->amr_maxchan = ae->ae_adapter.aa_channels;
656         sc->amr_maxio = ae->ae_adapter.aa_maxio;
657         free(ae, M_DEVBUF);
658     }
659
660     /*
661      * Mark remaining drives as unused.
662      */
663     for (; ldrv < AMR_MAXLD; ldrv++)
664         sc->amr_drive[ldrv].al_size = 0xffffffff;
665
666     /* 
667      * Cap the maximum number of outstanding I/Os.  AMI's Linux driver doesn't trust
668      * the controller's reported value, and lockups have been seen when we do.
669      */
670     sc->amr_maxio = imin(sc->amr_maxio, AMR_LIMITCMD);
671
672     return(0);
673 }
674
675 /********************************************************************************
676  * Run a generic enquiry-style command.
677  */
678 static void *
679 amr_enquiry(struct amr_softc *sc, size_t bufsize, u_int8_t cmd, u_int8_t cmdsub, u_int8_t cmdqual)
680 {
681     struct amr_command  *ac;
682     void                *result;
683     u_int8_t            *mbox;
684     int                 error;
685
686     debug_called(1);
687
688     error = 1;
689     result = NULL;
690     
691     /* get ourselves a command buffer */
692     if ((ac = amr_alloccmd(sc)) == NULL)
693         goto out;
694     /* allocate the response structure */
695     result = malloc(bufsize, M_DEVBUF, M_INTWAIT);
696     /* set command flags */
697     ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT;
698     
699     /* point the command at our data */
700     ac->ac_data = result;
701     ac->ac_length = bufsize;
702     
703     /* build the command proper */
704     mbox = (u_int8_t *)&ac->ac_mailbox;         /* XXX want a real structure for this? */
705     mbox[0] = cmd;
706     mbox[2] = cmdsub;
707     mbox[3] = cmdqual;
708
709     /* can't assume that interrupts are going to work here, so play it safe */
710     if (sc->amr_poll_command(ac))
711         goto out;
712     error = ac->ac_status;
713     
714  out:
715     if (ac != NULL)
716         amr_releasecmd(ac);
717     if ((error != 0) && (result != NULL)) {
718         free(result, M_DEVBUF);
719         result = NULL;
720     }
721     return(result);
722 }
723
724 /********************************************************************************
725  * Flush the controller's internal cache, return status.
726  */
727 int
728 amr_flush(struct amr_softc *sc)
729 {
730     struct amr_command  *ac;
731     int                 error;
732
733     /* get ourselves a command buffer */
734     error = 1;
735     if ((ac = amr_alloccmd(sc)) == NULL)
736         goto out;
737     /* set command flags */
738     ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT;
739     
740     /* build the command proper */
741     ac->ac_mailbox.mb_command = AMR_CMD_FLUSH;
742
743     /* we have to poll, as the system may be going down or otherwise damaged */
744     if (sc->amr_poll_command(ac))
745         goto out;
746     error = ac->ac_status;
747     
748  out:
749     if (ac != NULL)
750         amr_releasecmd(ac);
751     return(error);
752 }
753
754 /********************************************************************************
755  * Detect extented cdb >> greater than 10 byte cdb support
756  * returns '1' means this support exist
757  * returns '0' means this support doesn't exist
758  */
759 static int
760 amr_support_ext_cdb(struct amr_softc *sc)
761 {
762     struct amr_command  *ac;
763     u_int8_t            *mbox;
764     int                 error;
765
766     /* get ourselves a command buffer */
767     error = 0;
768     if ((ac = amr_alloccmd(sc)) == NULL)
769         goto out;
770     /* set command flags */
771     ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT;
772
773     /* build the command proper */
774     mbox = (u_int8_t *)&ac->ac_mailbox;         /* XXX want a real structure for this? */
775     mbox[0] = 0xA4;
776     mbox[2] = 0x16;
777
778
779     /* we have to poll, as the system may be going down or otherwise damaged */
780     if (sc->amr_poll_command(ac))
781         goto out;
782     if( ac->ac_status == AMR_STATUS_SUCCESS ) {
783             error = 1;
784     }
785
786 out:
787     if (ac != NULL)
788         amr_releasecmd(ac);
789     return(error);
790 }
791
792 /********************************************************************************
793  * Try to find I/O work for the controller from one or more of the work queues.
794  *
795  * We make the assumption that if the controller is not ready to take a command
796  * at some given time, it will generate an interrupt at some later time when
797  * it is.
798  */
799 void
800 amr_startio(struct amr_softc *sc)
801 {
802     struct amr_command  *ac;
803
804     /* spin until something prevents us from doing any work */
805     for (;;) {
806
807         /* try to get a ready command */
808         ac = amr_dequeue_ready(sc);
809
810         /* if that failed, build a command from a bio */
811         if (ac == NULL)
812             (void)amr_bio_command(sc, &ac);
813
814         /* if that failed, build a command from a ccb */
815         if (ac == NULL)
816             (void)amr_cam_command(sc, &ac);
817
818         /* if we don't have anything to do, give up */
819         if (ac == NULL)
820             break;
821
822         /* try to give the command to the controller; if this fails save it for later and give up */
823         if (amr_start(ac)) {
824             debug(2, "controller busy, command deferred");
825             amr_requeue_ready(ac);      /* XXX schedule retry very soon? */
826             break;
827         }
828     }
829 }
830
831 /********************************************************************************
832  * Handle completion of an I/O command.
833  */
834 static void
835 amr_completeio(struct amr_command *ac)
836 {
837     struct amr_softc    *sc = ac->ac_sc;
838     
839     if (ac->ac_status != AMR_STATUS_SUCCESS) {  /* could be more verbose here? */
840         ac->ac_bio->bio_error = EIO;
841         ac->ac_bio->bio_flags |= BIO_ERROR;
842
843         device_printf(sc->amr_dev, "I/O error - 0x%x\n", ac->ac_status);
844 /*      amr_printcommand(ac);*/
845     }
846     amrd_intr(ac->ac_bio);
847     amr_releasecmd(ac);
848 }
849
850 /********************************************************************************
851  ********************************************************************************
852                                                                Command Processing
853  ********************************************************************************
854  ********************************************************************************/
855
856 /********************************************************************************
857  * Convert a bio off the top of the bio queue into a command.
858  */
859 static int
860 amr_bio_command(struct amr_softc *sc, struct amr_command **acp)
861 {
862     struct amr_command  *ac;
863     struct amrd_softc   *amrd;
864     struct bio          *bio;
865     int                 error;
866     int                 blkcount;
867     int                 driveno;
868     int                 cmd;
869
870     ac = NULL;
871     error = 0;
872
873     /* get a bio to work on */
874     if ((bio = amr_dequeue_bio(sc)) == NULL)
875         goto out;
876
877     /* get a command */
878     if ((ac = amr_alloccmd(sc)) == NULL) {
879         error = ENOMEM;
880         goto out;
881     }   
882         
883     /* connect the bio to the command */
884     ac->ac_complete = amr_completeio;
885     ac->ac_bio = bio;
886     ac->ac_data = bio->bio_data;
887     ac->ac_length = bio->bio_bcount;
888     if (BIO_IS_READ(bio)) {
889         ac->ac_flags |= AMR_CMD_DATAIN;
890         cmd = AMR_CMD_LREAD;
891     } else {
892         ac->ac_flags |= AMR_CMD_DATAOUT;
893         cmd = AMR_CMD_LWRITE;
894     }
895     amrd = (struct amrd_softc *)bio->bio_dev->si_drv1;
896     driveno = amrd->amrd_drive - sc->amr_drive;
897     blkcount = (bio->bio_bcount + AMR_BLKSIZE - 1) / AMR_BLKSIZE;
898
899     ac->ac_mailbox.mb_command = cmd;
900     ac->ac_mailbox.mb_blkcount = blkcount;
901     ac->ac_mailbox.mb_lba = bio->bio_pblkno;
902     ac->ac_mailbox.mb_drive = driveno;
903     /* we fill in the s/g related data when the command is mapped */
904
905     if ((bio->bio_pblkno + blkcount) > sc->amr_drive[driveno].al_size)
906         device_printf(sc->amr_dev, "I/O beyond end of unit (%lld,%d > %lu)\n", 
907                       (long long)bio->bio_pblkno, blkcount,
908                       (u_long)sc->amr_drive[driveno].al_size);
909
910 out:
911     if (error != 0) {
912         if (ac != NULL)
913             amr_releasecmd(ac);
914         if (bio != NULL)                        /* this breaks ordering... */
915             amr_enqueue_bio(sc, bio);
916     }
917     *acp = ac;
918     return(error);
919 }
920
921 /********************************************************************************
922  * Take a command, submit it to the controller and sleep until it completes
923  * or fails.  Interrupts must be enabled, returns nonzero on error.
924  */
925 static int
926 amr_wait_command(struct amr_command *ac)
927 {
928     int                 error, count;
929     
930     debug_called(1);
931
932     ac->ac_complete = NULL;
933     ac->ac_flags |= AMR_CMD_SLEEP;
934     if ((error = amr_start(ac)) != 0)
935         return(error);
936     
937     count = 0;
938     /* XXX better timeout? */
939     while ((ac->ac_flags & AMR_CMD_BUSY) && (count < 30)) {
940         tsleep(ac, PCATCH, "amrwcmd", hz);
941     }
942     return(0);
943 }
944
945 /********************************************************************************
946  * Take a command, submit it to the controller and busy-wait for it to return.
947  * Returns nonzero on error.  Can be safely called with interrupts enabled.
948  */
949 static int
950 amr_std_poll_command(struct amr_command *ac)
951 {
952     struct amr_softc    *sc = ac->ac_sc;
953     int                 error, count;
954
955     debug_called(2);
956
957     ac->ac_complete = NULL;
958     if ((error = amr_start(ac)) != 0)
959         return(error);
960
961     count = 0;
962     do {
963         /* 
964          * Poll for completion, although the interrupt handler may beat us to it. 
965          * Note that the timeout here is somewhat arbitrary.
966          */
967         amr_done(sc);
968         DELAY(1000);
969     } while ((ac->ac_flags & AMR_CMD_BUSY) && (count++ < 1000));
970     if (!(ac->ac_flags & AMR_CMD_BUSY)) {
971         error = 0;
972     } else {
973         /* XXX the slot is now marked permanently busy */
974         error = EIO;
975         device_printf(sc->amr_dev, "polled command timeout\n");
976     }
977     return(error);
978 }
979
980 /********************************************************************************
981  * Take a command, submit it to the controller and busy-wait for it to return.
982  * Returns nonzero on error.  Can be safely called with interrupts enabled.
983  */
984 static int
985 amr_quartz_poll_command(struct amr_command *ac)
986 {
987     struct amr_softc    *sc = ac->ac_sc;
988     int                 s;
989     int                 error,count;
990
991     debug_called(2);
992
993     /* now we have a slot, we can map the command (unmapped in amr_complete) */
994     amr_mapcmd(ac);
995
996     s = splbio();
997
998     if (sc->amr_state & AMR_STATE_INTEN) {
999             count=0;
1000             while (sc->amr_busyslots) {
1001                     tsleep(sc, PCATCH, "amrpoll", hz);
1002                     if(count++>10) {
1003                             break;
1004                     }
1005             }
1006             
1007             if(sc->amr_busyslots) {
1008                     device_printf(sc->amr_dev, "adapter is busy\n");
1009                     splx(s);
1010                     amr_unmapcmd(ac);
1011                     ac->ac_status=0;
1012                     return(1);
1013             }
1014     }
1015
1016     bcopy(&ac->ac_mailbox, (void *)(uintptr_t)(volatile void *)sc->amr_mailbox, AMR_MBOX_CMDSIZE);
1017
1018     /* clear the poll/ack fields in the mailbox */
1019     sc->amr_mailbox->mb_ident = 0xFE;
1020     sc->amr_mailbox->mb_nstatus = 0xFF;
1021     sc->amr_mailbox->mb_status = 0xFF;
1022     sc->amr_mailbox->mb_poll = 0;
1023     sc->amr_mailbox->mb_ack = 0;
1024     sc->amr_mailbox->mb_busy = 1;
1025
1026     AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_SUBMIT);
1027
1028     while(sc->amr_mailbox->mb_nstatus == 0xFF);
1029     while(sc->amr_mailbox->mb_status == 0xFF);
1030     ac->ac_status=sc->amr_mailbox->mb_status;
1031     error = (ac->ac_status !=AMR_STATUS_SUCCESS) ? 1:0;
1032     while(sc->amr_mailbox->mb_poll != 0x77);
1033     sc->amr_mailbox->mb_poll = 0;
1034     sc->amr_mailbox->mb_ack = 0x77;
1035
1036     /* acknowledge that we have the commands */
1037     AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_ACK);
1038     while(AMR_QGET_IDB(sc) & AMR_QIDB_ACK);
1039
1040     splx(s);
1041
1042     /* unmap the command's data buffer */
1043     amr_unmapcmd(ac);
1044
1045     return(error);
1046 }
1047
1048 /********************************************************************************
1049  * Get a free command slot for a command if it doesn't already have one.
1050  *
1051  * May be safely called multiple times for a given command.
1052  */
1053 static int
1054 amr_getslot(struct amr_command *ac)
1055 {
1056     struct amr_softc    *sc = ac->ac_sc;
1057     int                 s, slot, limit, error;
1058
1059     debug_called(3);
1060
1061     /* if the command already has a slot, don't try to give it another one */
1062     if (ac->ac_slot != 0)
1063         return(0);
1064
1065     /* enforce slot usage limit */
1066     limit = (ac->ac_flags & AMR_CMD_PRIORITY) ? sc->amr_maxio : sc->amr_maxio - 4;
1067     if (sc->amr_busyslots > limit)
1068         return(EBUSY);
1069     
1070     /*
1071      * Allocate a slot.  XXX linear scan is slow
1072      */
1073     error = EBUSY;
1074     s = splbio();
1075     for (slot = 0; slot < sc->amr_maxio; slot++) {
1076         if (sc->amr_busycmd[slot] == NULL) {
1077             sc->amr_busycmd[slot] = ac;
1078             sc->amr_busyslots++;
1079             ac->ac_slot = slot;
1080             error = 0;
1081             break;
1082         }
1083     }
1084     splx(s);
1085
1086     return(error);
1087 }
1088
1089 /********************************************************************************
1090  * Map/unmap (ac)'s data in the controller's addressable space as required.
1091  *
1092  * These functions may be safely called multiple times on a given command.
1093  */
1094 static void
1095 amr_setup_dmamap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1096 {
1097     struct amr_command  *ac = (struct amr_command *)arg;
1098     struct amr_softc    *sc = ac->ac_sc;
1099     struct amr_sgentry  *sg;
1100     int                 i;
1101     u_int8_t            *sgc;
1102
1103     debug_called(3);
1104
1105     /* get base address of s/g table */
1106     sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG);
1107
1108     /* save data physical address */
1109     ac->ac_dataphys = segs[0].ds_addr;
1110
1111     /* for AMR_CMD_CONFIG the s/g count goes elsewhere */
1112     if (ac->ac_mailbox.mb_command == AMR_CMD_CONFIG) {
1113         sgc = &(((struct amr_mailbox_ioctl *)&ac->ac_mailbox)->mb_param);
1114     } else {
1115         sgc = &ac->ac_mailbox.mb_nsgelem;
1116     }
1117
1118     /* decide whether we need to populate the s/g table */
1119     if (nsegments < 2) {
1120         *sgc = 0;
1121         ac->ac_mailbox.mb_nsgelem = 0;
1122         ac->ac_mailbox.mb_physaddr = ac->ac_dataphys;
1123     } else {
1124         ac->ac_mailbox.mb_nsgelem = nsegments;
1125         *sgc = nsegments;
1126         ac->ac_mailbox.mb_physaddr = sc->amr_sgbusaddr + (ac->ac_slot * AMR_NSEG * sizeof(struct amr_sgentry));
1127         for (i = 0; i < nsegments; i++, sg++) {
1128             sg->sg_addr = segs[i].ds_addr;
1129             sg->sg_count = segs[i].ds_len;
1130         }
1131     }
1132 }
1133
1134 static void
1135 amr_setup_ccbmap(void *arg, bus_dma_segment_t *segs, int nsegments, int error)
1136 {
1137     struct amr_command          *ac = (struct amr_command *)arg;
1138     struct amr_softc            *sc = ac->ac_sc;
1139     struct amr_sgentry          *sg;
1140     struct amr_passthrough      *ap = (struct amr_passthrough *)ac->ac_data;
1141     struct amr_ext_passthrough  *aep = (struct amr_ext_passthrough *)ac->ac_data;
1142     int                         i;
1143
1144     /* get base address of s/g table */
1145     sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG);
1146
1147     /* decide whether we need to populate the s/g table */
1148     if( ac->ac_mailbox.mb_command == AMR_CMD_EXTPASS ) {
1149         if (nsegments < 2) {
1150             aep->ap_no_sg_elements = 0;
1151             aep->ap_data_transfer_address =  segs[0].ds_addr;
1152         } else {
1153             /* save s/g table information in passthrough */
1154             aep->ap_no_sg_elements = nsegments;
1155             aep->ap_data_transfer_address = sc->amr_sgbusaddr + (ac->ac_slot * AMR_NSEG * sizeof(struct amr_sgentry));
1156             /* populate s/g table (overwrites previous call which mapped the passthrough) */
1157             for (i = 0; i < nsegments; i++, sg++) {
1158                 sg->sg_addr = segs[i].ds_addr;
1159                 sg->sg_count = segs[i].ds_len;
1160                 debug(3, " %d: 0x%x/%d", i, sg->sg_addr, sg->sg_count);
1161             }
1162         }
1163         debug(3, "slot %d  %d segments at 0x%x, passthrough at 0x%x", ac->ac_slot,
1164             aep->ap_no_sg_elements, aep->ap_data_transfer_address, ac->ac_dataphys);
1165     } else {
1166         if (nsegments < 2) {
1167             ap->ap_no_sg_elements = 0;
1168             ap->ap_data_transfer_address =  segs[0].ds_addr;
1169         } else {
1170             /* save s/g table information in passthrough */
1171             ap->ap_no_sg_elements = nsegments;
1172             ap->ap_data_transfer_address = sc->amr_sgbusaddr + (ac->ac_slot * AMR_NSEG * sizeof(struct amr_sgentry));
1173             /* populate s/g table (overwrites previous call which mapped the passthrough) */
1174             for (i = 0; i < nsegments; i++, sg++) {
1175                 sg->sg_addr = segs[i].ds_addr;
1176                 sg->sg_count = segs[i].ds_len;
1177                 debug(3, " %d: 0x%x/%d", i, sg->sg_addr, sg->sg_count);
1178             }
1179         }
1180         debug(3, "slot %d  %d segments at 0x%x, passthrough at 0x%x", ac->ac_slot,
1181             ap->ap_no_sg_elements, ap->ap_data_transfer_address, ac->ac_dataphys);
1182     }
1183 }
1184
1185 static void
1186 amr_mapcmd(struct amr_command *ac)
1187 {
1188     struct amr_softc    *sc = ac->ac_sc;
1189
1190     debug_called(3);
1191
1192     /* if the command involves data at all, and hasn't been mapped */
1193     if (!(ac->ac_flags & AMR_CMD_MAPPED)) {
1194
1195         if (ac->ac_data != NULL) {
1196             /* map the data buffers into bus space and build the s/g list */
1197             bus_dmamap_load(sc->amr_buffer_dmat, ac->ac_dmamap, ac->ac_data, ac->ac_length,
1198                             amr_setup_dmamap, ac, 0);
1199             if (ac->ac_flags & AMR_CMD_DATAIN)
1200                 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_PREREAD);
1201             if (ac->ac_flags & AMR_CMD_DATAOUT)
1202                 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_PREWRITE);
1203         }
1204
1205         if (ac->ac_ccb_data != NULL) {
1206             bus_dmamap_load(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, ac->ac_ccb_data, ac->ac_ccb_length,
1207                             amr_setup_ccbmap, ac, 0);
1208             if (ac->ac_flags & AMR_CMD_CCB_DATAIN)
1209                 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_PREREAD);
1210             if (ac->ac_flags & AMR_CMD_CCB_DATAOUT)
1211                 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_PREWRITE);
1212         }
1213         ac->ac_flags |= AMR_CMD_MAPPED;
1214     }
1215 }
1216
1217 static void
1218 amr_unmapcmd(struct amr_command *ac)
1219 {
1220     struct amr_softc    *sc = ac->ac_sc;
1221
1222     debug_called(3);
1223
1224     /* if the command involved data at all and was mapped */
1225     if (ac->ac_flags & AMR_CMD_MAPPED) {
1226
1227         if (ac->ac_data != NULL) {
1228             if (ac->ac_flags & AMR_CMD_DATAIN)
1229                 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_POSTREAD);
1230             if (ac->ac_flags & AMR_CMD_DATAOUT)
1231                 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_dmamap, BUS_DMASYNC_POSTWRITE);
1232             bus_dmamap_unload(sc->amr_buffer_dmat, ac->ac_dmamap);
1233         }
1234
1235         if (ac->ac_ccb_data != NULL) {
1236             if (ac->ac_flags & AMR_CMD_CCB_DATAIN)
1237                 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_POSTREAD);
1238             if (ac->ac_flags & AMR_CMD_CCB_DATAOUT)
1239                 bus_dmamap_sync(sc->amr_buffer_dmat, ac->ac_ccb_dmamap, BUS_DMASYNC_POSTWRITE);
1240             bus_dmamap_unload(sc->amr_buffer_dmat, ac->ac_ccb_dmamap);
1241         }
1242         ac->ac_flags &= ~AMR_CMD_MAPPED;
1243     }
1244 }
1245
1246 /********************************************************************************
1247  * Take a command and give it to the controller, returns 0 if successful, or
1248  * EBUSY if the command should be retried later.
1249  */
1250 static int
1251 amr_start(struct amr_command *ac)
1252 {
1253     struct amr_softc    *sc = ac->ac_sc;
1254     int                 done, s, i;
1255
1256     debug_called(3);
1257
1258     /* mark command as busy so that polling consumer can tell */
1259     ac->ac_flags |= AMR_CMD_BUSY;
1260
1261     /* get a command slot (freed in amr_done) */
1262     if (amr_getslot(ac))
1263         return(EBUSY);
1264
1265     /* now we have a slot, we can map the command (unmapped in amr_complete) */
1266     amr_mapcmd(ac);
1267
1268     /* mark the new mailbox we are going to copy in as busy */
1269     ac->ac_mailbox.mb_busy = 1;
1270
1271     /* clear the poll/ack fields in the mailbox */
1272     sc->amr_mailbox->mb_poll = 0;
1273     sc->amr_mailbox->mb_ack = 0;
1274
1275     /* 
1276      * Save the slot number so that we can locate this command when complete.
1277      * Note that ident = 0 seems to be special, so we don't use it.
1278      */
1279     ac->ac_mailbox.mb_ident = ac->ac_slot + 1;
1280
1281     /* 
1282      * Spin waiting for the mailbox, give up after ~1 second.  We expect the
1283      * controller to be able to handle our I/O.
1284      *
1285      * XXX perhaps we should wait for less time, and count on the deferred command
1286      * handling to deal with retries?
1287      */
1288     debug(4, "wait for mailbox");
1289     for (i = 10000, done = 0; (i > 0) && !done; i--) {
1290         s = splbio();
1291         
1292         /* is the mailbox free? */
1293         if (sc->amr_mailbox->mb_busy == 0) {
1294             debug(4, "got mailbox");
1295             sc->amr_mailbox64->mb64_segment = 0;
1296             bcopy(&ac->ac_mailbox, (void *)(uintptr_t)(volatile void *)sc->amr_mailbox, AMR_MBOX_CMDSIZE);
1297             done = 1;
1298
1299             /* not free, spin waiting */
1300         } else {
1301             debug(4, "busy flag %x\n", sc->amr_mailbox->mb_busy);
1302             /* this is somewhat ugly */
1303             DELAY(100);
1304         }
1305         splx(s);        /* drop spl to allow completion interrupts */
1306     }
1307
1308     /*
1309      * Now give the command to the controller
1310      */
1311     if (done) {
1312         if (sc->amr_submit_command(sc)) {
1313             /* the controller wasn't ready to take the command, forget that we tried to post it */
1314             sc->amr_mailbox->mb_busy = 0;
1315             return(EBUSY);
1316         }
1317         debug(3, "posted command");
1318         return(0);
1319     }
1320     
1321     /*
1322      * The controller wouldn't take the command.  Return the command as busy
1323      * so that it is retried later.
1324      */
1325     return(EBUSY);
1326 }
1327
1328 /********************************************************************************
1329  * Extract one or more completed commands from the controller (sc)
1330  *
1331  * Returns nonzero if any commands on the work queue were marked as completed.
1332  */
1333 int
1334 amr_done(struct amr_softc *sc)
1335 {
1336     struct amr_command  *ac;
1337     struct amr_mailbox  mbox;
1338     int                 i, idx, result;
1339     
1340     debug_called(3);
1341
1342     /* See if there's anything for us to do */
1343     result = 0;
1344
1345     /* loop collecting completed commands */
1346     for (;;) {
1347         /* poll for a completed command's identifier and status */
1348         if (sc->amr_get_work(sc, &mbox)) {
1349             result = 1;
1350             
1351             /* iterate over completed commands in this result */
1352             for (i = 0; i < mbox.mb_nstatus; i++) {
1353                 /* get pointer to busy command */
1354                 idx = mbox.mb_completed[i] - 1;
1355                 ac = sc->amr_busycmd[idx];
1356
1357                 /* really a busy command? */
1358                 if (ac != NULL) {
1359
1360                     /* pull the command from the busy index */
1361                     sc->amr_busycmd[idx] = NULL;
1362                     sc->amr_busyslots--;
1363                 
1364                     /* save status for later use */
1365                     ac->ac_status = mbox.mb_status;
1366                     amr_enqueue_completed(ac);
1367                     debug(3, "completed command with status %x", mbox.mb_status);
1368                 } else {
1369                     device_printf(sc->amr_dev, "bad slot %d completed\n", idx);
1370                 }
1371             }
1372         } else {
1373             break;      /* no work */
1374         }
1375     }
1376     
1377     /* if we've completed any commands, try posting some more */
1378     if (result)
1379         amr_startio(sc);
1380     
1381     /* handle completion and timeouts */
1382 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005
1383     if (sc->amr_state & AMR_STATE_INTEN) 
1384         taskqueue_enqueue(taskqueue_swi, &sc->amr_task_complete);
1385     else
1386 #endif
1387         amr_complete(sc, 0);
1388     
1389     return(result);
1390 }
1391
1392 /********************************************************************************
1393  * Do completion processing on done commands on (sc)
1394  */
1395 static void
1396 amr_complete(void *context, int pending)
1397 {
1398     struct amr_softc    *sc = (struct amr_softc *)context;
1399     struct amr_command  *ac;
1400
1401     debug_called(3);
1402
1403     /* pull completed commands off the queue */
1404     for (;;) {
1405         ac = amr_dequeue_completed(sc);
1406         if (ac == NULL)
1407             break;
1408
1409         /* unmap the command's data buffer */
1410         amr_unmapcmd(ac);
1411
1412         /* unbusy the command */
1413         ac->ac_flags &= ~AMR_CMD_BUSY;
1414             
1415         /* 
1416          * Is there a completion handler? 
1417          */
1418         if (ac->ac_complete != NULL) {
1419             ac->ac_complete(ac);
1420             
1421             /* 
1422              * Is someone sleeping on this one?
1423              */
1424         } else if (ac->ac_flags & AMR_CMD_SLEEP) {
1425             wakeup(ac);
1426         }
1427
1428         if(!sc->amr_busyslots) {
1429             wakeup(sc);
1430         }
1431     }
1432 }
1433
1434 /********************************************************************************
1435  ********************************************************************************
1436                                                         Command Buffer Management
1437  ********************************************************************************
1438  ********************************************************************************/
1439
1440 /********************************************************************************
1441  * Get a new command buffer.
1442  *
1443  * This may return NULL in low-memory cases.
1444  *
1445  * If possible, we recycle a command buffer that's been used before.
1446  */
1447 struct amr_command *
1448 amr_alloccmd(struct amr_softc *sc)
1449 {
1450     struct amr_command  *ac;
1451
1452     debug_called(3);
1453
1454     ac = amr_dequeue_free(sc);
1455     if (ac == NULL) {
1456         amr_alloccmd_cluster(sc);
1457         ac = amr_dequeue_free(sc);
1458     }
1459     if (ac == NULL)
1460         return(NULL);
1461
1462     /* clear out significant fields */
1463     ac->ac_slot = 0;
1464     ac->ac_status = 0;
1465     bzero(&ac->ac_mailbox, sizeof(struct amr_mailbox));
1466     ac->ac_flags = 0;
1467     ac->ac_bio = NULL;
1468     ac->ac_data = NULL;
1469     ac->ac_ccb_data = NULL;
1470     ac->ac_complete = NULL;
1471     return(ac);
1472 }
1473
1474 /********************************************************************************
1475  * Release a command buffer for recycling.
1476  */
1477 void
1478 amr_releasecmd(struct amr_command *ac)
1479 {
1480     debug_called(3);
1481
1482     amr_enqueue_free(ac);
1483 }
1484
1485 /********************************************************************************
1486  * Allocate a new command cluster and initialise it.
1487  */
1488 static void
1489 amr_alloccmd_cluster(struct amr_softc *sc)
1490 {
1491     struct amr_command_cluster  *acc;
1492     struct amr_command          *ac;
1493     int                         s, i;
1494
1495     acc = malloc(AMR_CMD_CLUSTERSIZE, M_DEVBUF, M_INTWAIT);
1496     s = splbio();
1497     TAILQ_INSERT_TAIL(&sc->amr_cmd_clusters, acc, acc_link);
1498     splx(s);
1499     for (i = 0; i < AMR_CMD_CLUSTERCOUNT; i++) {
1500         ac = &acc->acc_command[i];
1501         bzero(ac, sizeof(*ac));
1502         ac->ac_sc = sc;
1503         if (!bus_dmamap_create(sc->amr_buffer_dmat, 0, &ac->ac_dmamap) &&
1504             !bus_dmamap_create(sc->amr_buffer_dmat, 0, &ac->ac_ccb_dmamap))
1505             amr_releasecmd(ac);
1506     }
1507 }
1508
1509 /********************************************************************************
1510  * Free a command cluster
1511  */
1512 static void
1513 amr_freecmd_cluster(struct amr_command_cluster *acc)
1514 {
1515     struct amr_softc    *sc = acc->acc_command[0].ac_sc;
1516     int                 i;
1517
1518     for (i = 0; i < AMR_CMD_CLUSTERCOUNT; i++)
1519         bus_dmamap_destroy(sc->amr_buffer_dmat, acc->acc_command[i].ac_dmamap);
1520     free(acc, M_DEVBUF);
1521 }
1522
1523 /********************************************************************************
1524  ********************************************************************************
1525                                                          Interface-specific Shims
1526  ********************************************************************************
1527  ********************************************************************************/
1528
1529 /********************************************************************************
1530  * Tell the controller that the mailbox contains a valid command
1531  */
1532 static int
1533 amr_quartz_submit_command(struct amr_softc *sc)
1534 {
1535     debug_called(3);
1536
1537     if (AMR_QGET_IDB(sc) & AMR_QIDB_SUBMIT)
1538         return(EBUSY);
1539     AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_SUBMIT);
1540     return(0);
1541 }
1542
1543 static int
1544 amr_std_submit_command(struct amr_softc *sc)
1545 {
1546     debug_called(3);
1547
1548     if (AMR_SGET_MBSTAT(sc) & AMR_SMBOX_BUSYFLAG)
1549         return(EBUSY);
1550     AMR_SPOST_COMMAND(sc);
1551     return(0);
1552 }
1553
1554 /********************************************************************************
1555  * Claim any work that the controller has completed; acknowledge completion,
1556  * save details of the completion in (mbsave)
1557  */
1558 static int
1559 amr_quartz_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave)
1560 {
1561     int         s, worked;
1562     u_int32_t   outd;
1563
1564     debug_called(3);
1565
1566     worked = 0;
1567     s = splbio();
1568
1569     /* work waiting for us? */
1570     if ((outd = AMR_QGET_ODB(sc)) == AMR_QODB_READY) {
1571
1572         /* save mailbox, which contains a list of completed commands */
1573         bcopy((void *)(uintptr_t)(volatile void *)sc->amr_mailbox, mbsave, sizeof(*mbsave));
1574
1575         /* acknowledge interrupt */
1576         AMR_QPUT_ODB(sc, AMR_QODB_READY);
1577
1578         /* acknowledge that we have the commands */
1579         AMR_QPUT_IDB(sc, sc->amr_mailboxphys | AMR_QIDB_ACK);
1580
1581 #ifndef AMR_QUARTZ_GOFASTER
1582         /*
1583          * This waits for the controller to notice that we've taken the
1584          * command from it.  It's very inefficient, and we shouldn't do it,
1585          * but if we remove this code, we stop completing commands under
1586          * load.
1587          *
1588          * Peter J says we shouldn't do this.  The documentation says we
1589          * should.  Who is right?
1590          */
1591         while(AMR_QGET_IDB(sc) & AMR_QIDB_ACK)
1592             ;                           /* XXX aiee! what if it dies? */
1593 #endif
1594
1595         worked = 1;                     /* got some work */
1596     }
1597
1598     splx(s);
1599     return(worked);
1600 }
1601
1602 static int
1603 amr_std_get_work(struct amr_softc *sc, struct amr_mailbox *mbsave)
1604 {
1605     int         s, worked;
1606     u_int8_t    istat;
1607
1608     debug_called(3);
1609
1610     worked = 0;
1611     s = splbio();
1612
1613     /* check for valid interrupt status */
1614     istat = AMR_SGET_ISTAT(sc);
1615     if ((istat & AMR_SINTR_VALID) != 0) {
1616         AMR_SPUT_ISTAT(sc, istat);      /* ack interrupt status */
1617
1618         /* save mailbox, which contains a list of completed commands */
1619         bcopy((void *)(uintptr_t)(volatile void *)sc->amr_mailbox, mbsave, sizeof(*mbsave));
1620
1621         AMR_SACK_INTERRUPT(sc);         /* acknowledge we have the mailbox */
1622         worked = 1;
1623     }
1624
1625     splx(s);
1626     return(worked);
1627 }
1628
1629 /********************************************************************************
1630  * Notify the controller of the mailbox location.
1631  */
1632 static void
1633 amr_std_attach_mailbox(struct amr_softc *sc)
1634 {
1635
1636     /* program the mailbox physical address */
1637     AMR_SBYTE_SET(sc, AMR_SMBOX_0, sc->amr_mailboxphys         & 0xff);
1638     AMR_SBYTE_SET(sc, AMR_SMBOX_1, (sc->amr_mailboxphys >>  8) & 0xff);
1639     AMR_SBYTE_SET(sc, AMR_SMBOX_2, (sc->amr_mailboxphys >> 16) & 0xff);
1640     AMR_SBYTE_SET(sc, AMR_SMBOX_3, (sc->amr_mailboxphys >> 24) & 0xff);
1641     AMR_SBYTE_SET(sc, AMR_SMBOX_ENABLE, AMR_SMBOX_ADDR);
1642
1643     /* clear any outstanding interrupt and enable interrupts proper */
1644     AMR_SACK_INTERRUPT(sc);
1645     AMR_SENABLE_INTR(sc);
1646 }
1647
1648 #ifdef AMR_BOARD_INIT
1649 /********************************************************************************
1650  * Initialise the controller
1651  */
1652 static int
1653 amr_quartz_init(struct amr_softc *sc)
1654 {
1655     int         status, ostatus;
1656
1657     device_printf(sc->amr_dev, "initial init status %x\n", AMR_QGET_INITSTATUS(sc));
1658
1659     AMR_QRESET(sc);
1660
1661     ostatus = 0xff;
1662     while ((status = AMR_QGET_INITSTATUS(sc)) != AMR_QINIT_DONE) {
1663         if (status != ostatus) {
1664             device_printf(sc->amr_dev, "(%x) %s\n", status, amr_describe_code(amr_table_qinit, status));
1665             ostatus = status;
1666         }
1667         switch (status) {
1668         case AMR_QINIT_NOMEM:
1669             return(ENOMEM);
1670
1671         case AMR_QINIT_SCAN:
1672             /* XXX we could print channel/target here */
1673             break;
1674         }
1675     }
1676     return(0);
1677 }
1678
1679 static int
1680 amr_std_init(struct amr_softc *sc)
1681 {
1682     int         status, ostatus;
1683
1684     device_printf(sc->amr_dev, "initial init status %x\n", AMR_SGET_INITSTATUS(sc));
1685
1686     AMR_SRESET(sc);
1687  
1688     ostatus = 0xff;
1689     while ((status = AMR_SGET_INITSTATUS(sc)) != AMR_SINIT_DONE) {
1690         if (status != ostatus) {
1691             device_printf(sc->amr_dev, "(%x) %s\n", status, amr_describe_code(amr_table_sinit, status));
1692             ostatus = status;
1693         }
1694         switch (status) {
1695         case AMR_SINIT_NOMEM:
1696             return(ENOMEM);
1697
1698         case AMR_SINIT_INPROG:
1699             /* XXX we could print channel/target here? */
1700             break;
1701         }
1702     }
1703     return(0);
1704 }
1705 #endif
1706
1707 /********************************************************************************
1708  ********************************************************************************
1709                                                                         Debugging
1710  ********************************************************************************
1711  ********************************************************************************/
1712
1713 /********************************************************************************
1714  * Identify the controller and print some information about it.
1715  */
1716 static void
1717 amr_describe_controller(struct amr_softc *sc)
1718 {
1719     struct amr_prodinfo *ap;
1720     struct amr_enquiry  *ae;
1721     char                *prod;
1722
1723     /*
1724      * Try to get 40LD product info, which tells us what the card is labelled as.
1725      */
1726     if ((ap = amr_enquiry(sc, 2048, AMR_CMD_CONFIG, AMR_CONFIG_PRODUCT_INFO, 0)) != NULL) {
1727         device_printf(sc->amr_dev, "<LSILogic %.80s> Firmware %.16s, BIOS %.16s, %dMB RAM\n",
1728                       ap->ap_product, ap->ap_firmware, ap->ap_bios,
1729                       ap->ap_memsize);
1730
1731         free(ap, M_DEVBUF);
1732         return;
1733     }
1734
1735     /*
1736      * Try 8LD extended ENQUIRY to get controller signature, and use lookup table.
1737      */
1738     if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_EXT_ENQUIRY2, 0, 0)) != NULL) {
1739         prod = amr_describe_code(amr_table_adaptertype, ae->ae_signature);
1740
1741     } else if ((ae = (struct amr_enquiry *)amr_enquiry(sc, 2048, AMR_CMD_ENQUIRY, 0, 0)) != NULL) {
1742
1743         /*
1744          * Try to work it out based on the PCI signatures.
1745          */
1746         switch (pci_get_device(sc->amr_dev)) {
1747         case 0x9010:
1748             prod = "Series 428";
1749             break;
1750         case 0x9060:
1751             prod = "Series 434";
1752             break;
1753         default:
1754             prod = "unknown controller";
1755             break;
1756         }
1757     } else {
1758         prod = "unsupported controller";
1759     }
1760
1761     /*
1762      * HP NetRaid controllers have a special encoding of the firmware and
1763      * BIOS versions. The AMI version seems to have it as strings whereas
1764      * the HP version does it with a leading uppercase character and two
1765      * binary numbers.
1766      */
1767      
1768     if(ae->ae_adapter.aa_firmware[2] >= 'A' &&
1769        ae->ae_adapter.aa_firmware[2] <= 'Z' &&
1770        ae->ae_adapter.aa_firmware[1] <  ' ' &&
1771        ae->ae_adapter.aa_firmware[0] <  ' ' &&
1772        ae->ae_adapter.aa_bios[2] >= 'A'     &&
1773        ae->ae_adapter.aa_bios[2] <= 'Z'     &&
1774        ae->ae_adapter.aa_bios[1] <  ' '     &&
1775        ae->ae_adapter.aa_bios[0] <  ' ') {
1776
1777         /* this looks like we have an HP NetRaid version of the MegaRaid */
1778
1779         if(ae->ae_signature == AMR_SIG_438) {
1780                 /* the AMI 438 is a NetRaid 3si in HP-land */
1781                 prod = "HP NetRaid 3si";
1782         }
1783         
1784         device_printf(sc->amr_dev, "<%s> Firmware %c.%02d.%02d, BIOS %c.%02d.%02d, %dMB RAM\n",
1785                       prod, ae->ae_adapter.aa_firmware[2],
1786                       ae->ae_adapter.aa_firmware[1],
1787                       ae->ae_adapter.aa_firmware[0],
1788                       ae->ae_adapter.aa_bios[2],
1789                       ae->ae_adapter.aa_bios[1],
1790                       ae->ae_adapter.aa_bios[0],
1791                       ae->ae_adapter.aa_memorysize);            
1792     } else {
1793         device_printf(sc->amr_dev, "<%s> Firmware %.4s, BIOS %.4s, %dMB RAM\n", 
1794                       prod, ae->ae_adapter.aa_firmware, ae->ae_adapter.aa_bios,
1795                       ae->ae_adapter.aa_memorysize);
1796     }           
1797     free(ae, M_DEVBUF);
1798 }
1799
1800 int
1801 amr_dump_blocks(struct amr_softc *sc, int unit, u_int32_t lba, void *data, int blks)
1802 {
1803
1804     struct amr_command  *ac;
1805     int                 error = 1;
1806
1807     debug_called(1);
1808
1809     sc->amr_state &= ~AMR_STATE_INTEN;
1810
1811     /* get ourselves a command buffer */
1812     if ((ac = amr_alloccmd(sc)) == NULL)
1813         goto out;
1814     /* set command flags */
1815     ac->ac_flags |= AMR_CMD_PRIORITY | AMR_CMD_DATAOUT;
1816     
1817     /* point the command at our data */
1818     ac->ac_data = data;
1819     ac->ac_length = blks * AMR_BLKSIZE;
1820     
1821     /* build the command proper */
1822     ac->ac_mailbox.mb_command   = AMR_CMD_LWRITE;
1823     ac->ac_mailbox.mb_blkcount  = blks;
1824     ac->ac_mailbox.mb_lba       = lba;
1825     ac->ac_mailbox.mb_drive     = unit;
1826         
1827     /* can't assume that interrupts are going to work here, so play it safe */
1828     if (sc->amr_poll_command(ac))
1829         goto out;
1830     error = ac->ac_status;
1831     
1832  out:
1833     if (ac != NULL)
1834         amr_releasecmd(ac);
1835
1836     sc->amr_state |= AMR_STATE_INTEN;
1837
1838     return (error);     
1839 }
1840
1841
1842 #ifdef AMR_DEBUG
1843 /********************************************************************************
1844  * Print the command (ac) in human-readable format
1845  */
1846 #if 0
1847 static void
1848 amr_printcommand(struct amr_command *ac)
1849 {
1850     struct amr_softc    *sc = ac->ac_sc;
1851     struct amr_sgentry  *sg;
1852     int                 i;
1853     
1854     device_printf(sc->amr_dev, "cmd %x  ident %d  drive %d\n",
1855                   ac->ac_mailbox.mb_command, ac->ac_mailbox.mb_ident, ac->ac_mailbox.mb_drive);
1856     device_printf(sc->amr_dev, "blkcount %d  lba %d\n", 
1857                   ac->ac_mailbox.mb_blkcount, ac->ac_mailbox.mb_lba);
1858     device_printf(sc->amr_dev, "virtaddr %p  length %lu\n", ac->ac_data, (unsigned long)ac->ac_length);
1859     device_printf(sc->amr_dev, "sg physaddr %08x  nsg %d\n",
1860                   ac->ac_mailbox.mb_physaddr, ac->ac_mailbox.mb_nsgelem);
1861     device_printf(sc->amr_dev, "ccb %p  bio %p\n", ac->ac_ccb_data, ac->ac_bio);
1862
1863     /* get base address of s/g table */
1864     sg = sc->amr_sgtable + (ac->ac_slot * AMR_NSEG);
1865     for (i = 0; i < ac->ac_mailbox.mb_nsgelem; i++, sg++)
1866         device_printf(sc->amr_dev, "  %x/%d\n", sg->sg_addr, sg->sg_count);
1867 }
1868 #endif
1869 #endif