2 * Copyright (c) 1999,2000 Michael Smith
3 * Copyright (c) 2000 BSDi
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
27 * Copyright (c) 2002 Eric Moore
28 * Copyright (c) 2002 LSI Logic Corporation
29 * All rights reserved.
31 * Redistribution and use in source and binary forms, with or without
32 * modification, are permitted provided that the following conditions
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
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
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.25.4.2 2008/09/16 12:51:21 swildner Exp $
60 * Driver for the AMI MegaRaid family of controllers.
63 #include <sys/param.h>
64 #include <sys/systm.h>
65 #include <sys/malloc.h>
66 #include <sys/kernel.h>
68 #include "amr_compat.h"
71 #include <sys/devicestat.h>
76 #include <bus/pci/pcireg.h>
77 #include <bus/pci/pcivar.h>
82 #define AMR_DEFINE_TABLES
83 #include "amr_tables.h"
85 #define AMR_CDEV_MAJOR 132
87 static d_open_t amr_open
;
88 static d_close_t amr_close
;
89 static d_ioctl_t amr_ioctl
;
91 static struct dev_ops amr_ops
= {
92 { "amr", AMR_CDEV_MAJOR
, 0 },
99 * Initialisation, bus interface.
101 static void amr_startup(void *arg
);
106 static int amr_query_controller(struct amr_softc
*sc
);
107 static void *amr_enquiry(struct amr_softc
*sc
, size_t bufsize
,
108 u_int8_t cmd
, u_int8_t cmdsub
, u_int8_t cmdqual
);
109 static void amr_completeio(struct amr_command
*ac
);
110 static int amr_support_ext_cdb(struct amr_softc
*sc
);
113 * Command buffer allocation.
115 static void amr_alloccmd_cluster(struct amr_softc
*sc
);
116 static void amr_freecmd_cluster(struct amr_command_cluster
*acc
);
119 * Command processing.
121 static int amr_bio_command(struct amr_softc
*sc
, struct amr_command
**acp
);
122 static int amr_wait_command(struct amr_command
*ac
);
123 static int amr_getslot(struct amr_command
*ac
);
124 static void amr_mapcmd(struct amr_command
*ac
);
125 static void amr_unmapcmd(struct amr_command
*ac
);
126 static int amr_start(struct amr_command
*ac
);
127 static void amr_complete(void *context
, int pending
);
132 static void amr_periodic(void *data
);
135 * Interface-specific shims
137 static int amr_quartz_submit_command(struct amr_softc
*sc
);
138 static int amr_quartz_get_work(struct amr_softc
*sc
, struct amr_mailbox
*mbsave
);
139 static int amr_quartz_poll_command(struct amr_command
*ac
);
141 static int amr_std_submit_command(struct amr_softc
*sc
);
142 static int amr_std_get_work(struct amr_softc
*sc
, struct amr_mailbox
*mbsave
);
143 static int amr_std_poll_command(struct amr_command
*ac
);
144 static void amr_std_attach_mailbox(struct amr_softc
*sc
);
146 #ifdef AMR_BOARD_INIT
147 static int amr_quartz_init(struct amr_softc
*sc
);
148 static int amr_std_init(struct amr_softc
*sc
);
154 static void amr_describe_controller(struct amr_softc
*sc
);
157 static void amr_printcommand(struct amr_command
*ac
);
161 DECLARE_DUMMY_MODULE(amr
);
163 /********************************************************************************
164 ********************************************************************************
166 ********************************************************************************
167 ********************************************************************************/
169 /********************************************************************************
170 ********************************************************************************
172 ********************************************************************************
173 ********************************************************************************/
175 /********************************************************************************
176 * Initialise the controller and softc.
179 amr_attach(struct amr_softc
*sc
)
185 * Initialise per-controller queues.
187 TAILQ_INIT(&sc
->amr_completed
);
188 TAILQ_INIT(&sc
->amr_freecmds
);
189 TAILQ_INIT(&sc
->amr_cmd_clusters
);
190 TAILQ_INIT(&sc
->amr_ready
);
191 bioq_init(&sc
->amr_bioq
);
193 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005
195 * Initialise command-completion task.
197 TASK_INIT(&sc
->amr_task_complete
, 0, amr_complete
, sc
);
200 debug(2, "queue init done");
203 * Configure for this controller type.
205 if (AMR_IS_QUARTZ(sc
)) {
206 sc
->amr_submit_command
= amr_quartz_submit_command
;
207 sc
->amr_get_work
= amr_quartz_get_work
;
208 sc
->amr_poll_command
= amr_quartz_poll_command
;
210 sc
->amr_submit_command
= amr_std_submit_command
;
211 sc
->amr_get_work
= amr_std_get_work
;
212 sc
->amr_poll_command
= amr_std_poll_command
;
213 amr_std_attach_mailbox(sc
);
216 #ifdef AMR_BOARD_INIT
217 if ((AMR_IS_QUARTZ(sc
) ? amr_quartz_init(sc
) : amr_std_init(sc
))))
222 * Quiz controller for features and limits.
224 if (amr_query_controller(sc
))
227 debug(2, "controller query complete");
230 * Attach our 'real' SCSI channels to CAM.
232 if (amr_cam_attach(sc
))
234 debug(2, "CAM attach done");
237 * Create the control device.
239 dev_ops_add(&amr_ops
, -1, device_get_unit(sc
->amr_dev
));
240 sc
->amr_dev_t
= make_dev(&amr_ops
, device_get_unit(sc
->amr_dev
),
241 UID_ROOT
, GID_OPERATOR
, S_IRUSR
| S_IWUSR
,
242 "amr%d", device_get_unit(sc
->amr_dev
));
243 sc
->amr_dev_t
->si_drv1
= sc
;
244 reference_dev(sc
->amr_dev_t
);
247 * Schedule ourselves to bring the controller up once interrupts are
250 bzero(&sc
->amr_ich
, sizeof(struct intr_config_hook
));
251 sc
->amr_ich
.ich_func
= amr_startup
;
252 sc
->amr_ich
.ich_arg
= sc
;
253 sc
->amr_ich
.ich_desc
= "amr";
254 if (config_intrhook_establish(&sc
->amr_ich
) != 0) {
255 device_printf(sc
->amr_dev
, "can't establish configuration hook\n");
260 * Print a little information about the controller.
262 amr_describe_controller(sc
);
264 debug(2, "attach complete");
268 /********************************************************************************
269 * Locate disk resources and attach children to them.
272 amr_startup(void *arg
)
274 struct amr_softc
*sc
= (struct amr_softc
*)arg
;
275 struct amr_logdrive
*dr
;
279 callout_init(&sc
->amr_timeout
);
281 /* pull ourselves off the intrhook chain */
282 config_intrhook_disestablish(&sc
->amr_ich
);
284 /* get up-to-date drive information */
285 if (amr_query_controller(sc
)) {
286 device_printf(sc
->amr_dev
, "can't scan controller for drives\n");
290 /* iterate over available drives */
291 for (i
= 0, dr
= &sc
->amr_drive
[0]; (i
< AMR_MAXLD
) && (dr
->al_size
!= 0xffffffff); i
++, dr
++) {
292 /* are we already attached to this drive? */
293 if (dr
->al_disk
== 0) {
294 /* generate geometry information */
295 if (dr
->al_size
> 0x200000) { /* extended translation? */
302 dr
->al_cylinders
= dr
->al_size
/ (dr
->al_heads
* dr
->al_sectors
);
304 dr
->al_disk
= device_add_child(sc
->amr_dev
, NULL
, -1);
305 if (dr
->al_disk
== 0)
306 device_printf(sc
->amr_dev
, "device_add_child failed\n");
307 device_set_ivars(dr
->al_disk
, dr
);
311 if ((error
= bus_generic_attach(sc
->amr_dev
)) != 0)
312 device_printf(sc
->amr_dev
, "bus_generic_attach returned %d\n", error
);
314 /* mark controller back up */
315 sc
->amr_state
&= ~AMR_STATE_SHUTDOWN
;
317 /* interrupts will be enabled before we do anything more */
318 sc
->amr_state
|= AMR_STATE_INTEN
;
321 * Start the timeout routine.
323 /* callout_reset(&sc->amr_timeout, hz, amr_periodic, sc); */
328 /*******************************************************************************
329 * Free resources associated with a controller instance
332 amr_free(struct amr_softc
*sc
)
334 struct amr_command_cluster
*acc
;
336 /* detach from CAM */
339 /* cancel status timeout */
340 callout_stop(&sc
->amr_timeout
);
342 /* throw away any command buffers */
343 while ((acc
= TAILQ_FIRST(&sc
->amr_cmd_clusters
)) != NULL
) {
344 TAILQ_REMOVE(&sc
->amr_cmd_clusters
, acc
, acc_link
);
345 amr_freecmd_cluster(acc
);
348 /* destroy control device */
349 if( sc
->amr_dev_t
!= (cdev_t
)NULL
)
350 destroy_dev(sc
->amr_dev_t
);
351 dev_ops_remove(&amr_ops
, -1, device_get_unit(sc
->amr_dev
));
354 /*******************************************************************************
355 * Receive a bio structure from a child device and queue it on a particular
356 * disk resource, then poke the disk resource to start as much work as it can.
359 amr_submit_bio(struct amr_softc
*sc
, struct bio
*bio
)
363 amr_enqueue_bio(sc
, bio
);
368 /********************************************************************************
369 * Accept an open operation on the control device.
372 amr_open(struct dev_open_args
*ap
)
374 cdev_t dev
= ap
->a_head
.a_dev
;
375 int unit
= minor(dev
);
376 struct amr_softc
*sc
= devclass_get_softc(devclass_find("amr"), unit
);
380 sc
->amr_state
|= AMR_STATE_OPEN
;
384 /********************************************************************************
385 * Accept the last close on the control device.
388 amr_close(struct dev_close_args
*ap
)
390 cdev_t dev
= ap
->a_head
.a_dev
;
391 int unit
= minor(dev
);
392 struct amr_softc
*sc
= devclass_get_softc(devclass_find("amr"), unit
);
396 sc
->amr_state
&= ~AMR_STATE_OPEN
;
400 /********************************************************************************
401 * Handle controller-specific control operations.
404 amr_ioctl(struct dev_ioctl_args
*ap
)
406 cdev_t dev
= ap
->a_head
.a_dev
;
407 struct amr_softc
*sc
= (struct amr_softc
*)dev
->si_drv1
;
408 int *arg
= (int *)ap
->a_data
;
409 struct amr_user_ioctl
*au
= (struct amr_user_ioctl
*)ap
->a_data
;
410 struct amr_command
*ac
;
411 struct amr_mailbox_ioctl
*mbi
;
412 struct amr_passthrough
*apt
;
425 debug(1, "AMR_IO_VERSION");
426 *arg
= AMR_IO_VERSION_NUMBER
;
430 debug(1, "AMR_IO_COMMAND 0x%x", au
->au_cmd
[0]);
431 /* handle inbound data buffer */
432 if (au
->au_length
!= 0) {
433 dp
= kmalloc(au
->au_length
, M_DEVBUF
, M_WAITOK
);
434 if ((error
= copyin(au
->au_buffer
, dp
, au
->au_length
)) != 0)
436 debug(2, "copyin %ld bytes from %p -> %p", au
->au_length
, au
->au_buffer
, dp
);
439 if ((ac
= amr_alloccmd(sc
)) == NULL
) {
444 /* handle SCSI passthrough command */
445 if (au
->au_cmd
[0] == AMR_CMD_PASS
) {
446 apt
= kmalloc(sizeof(*apt
), M_DEVBUF
, M_WAITOK
| M_ZERO
);
449 apt
->ap_cdb_length
= au
->au_cmd
[2];
450 bcopy(&au
->au_cmd
[3], &apt
->ap_cdb
[0], apt
->ap_cdb_length
);
452 /* build passthrough */
453 apt
->ap_timeout
= au
->au_cmd
[apt
->ap_cdb_length
+ 3] & 0x07;
454 apt
->ap_ars
= (au
->au_cmd
[apt
->ap_cdb_length
+ 3] & 0x08) ? 1 : 0;
455 apt
->ap_islogical
= (au
->au_cmd
[apt
->ap_cdb_length
+ 3] & 0x80) ? 1 : 0;
456 apt
->ap_logical_drive_no
= au
->au_cmd
[apt
->ap_cdb_length
+ 4];
457 apt
->ap_channel
= au
->au_cmd
[apt
->ap_cdb_length
+ 5];
458 apt
->ap_scsi_id
= au
->au_cmd
[apt
->ap_cdb_length
+ 6];
459 apt
->ap_request_sense_length
= 14;
460 apt
->ap_data_transfer_length
= au
->au_length
;
461 /* XXX what about the request-sense area? does the caller want it? */
465 ac
->ac_length
= sizeof(*apt
);
466 ac
->ac_flags
|= AMR_CMD_DATAOUT
;
467 ac
->ac_ccb_data
= dp
;
468 ac
->ac_ccb_length
= au
->au_length
;
469 if (au
->au_direction
& AMR_IO_READ
)
470 ac
->ac_flags
|= AMR_CMD_CCB_DATAIN
;
471 if (au
->au_direction
& AMR_IO_WRITE
)
472 ac
->ac_flags
|= AMR_CMD_CCB_DATAOUT
;
474 ac
->ac_mailbox
.mb_command
= AMR_CMD_PASS
;
477 /* direct command to controller */
478 mbi
= (struct amr_mailbox_ioctl
*)&ac
->ac_mailbox
;
480 /* copy pertinent mailbox items */
481 mbi
->mb_command
= au
->au_cmd
[0];
482 mbi
->mb_channel
= au
->au_cmd
[1];
483 mbi
->mb_param
= au
->au_cmd
[2];
484 mbi
->mb_pad
[0] = au
->au_cmd
[3];
485 mbi
->mb_drive
= au
->au_cmd
[4];
487 /* build the command */
489 ac
->ac_length
= au
->au_length
;
490 if (au
->au_direction
& AMR_IO_READ
)
491 ac
->ac_flags
|= AMR_CMD_DATAIN
;
492 if (au
->au_direction
& AMR_IO_WRITE
)
493 ac
->ac_flags
|= AMR_CMD_DATAOUT
;
496 /* run the command */
497 if ((error
= amr_wait_command(ac
)) != 0)
500 /* copy out data and set status */
501 if (au
->au_length
!= 0)
502 error
= copyout(dp
, au
->au_buffer
, au
->au_length
);
503 debug(2, "copyout %ld bytes from %p -> %p", au
->au_length
, dp
, au
->au_buffer
);
505 debug(2, "%16d", (int)dp
);
506 au
->au_status
= ac
->ac_status
;
510 debug(1, "unknown ioctl 0x%lx", cmd
);
518 kfree(apt
, M_DEVBUF
);
524 /********************************************************************************
525 ********************************************************************************
527 ********************************************************************************
528 ********************************************************************************/
530 /********************************************************************************
531 * Perform a periodic check of the controller status
534 amr_periodic(void *data
)
536 struct amr_softc
*sc
= (struct amr_softc
*)data
;
540 /* XXX perform periodic status checks here */
542 /* compensate for missed interrupts */
546 callout_reset(&sc
->amr_timeout
, hz
, amr_periodic
, sc
);
549 /********************************************************************************
550 ********************************************************************************
552 ********************************************************************************
553 ********************************************************************************/
555 /********************************************************************************
556 * Interrogate the controller for the operational parameters we require.
559 amr_query_controller(struct amr_softc
*sc
)
561 struct amr_enquiry3
*aex
;
562 struct amr_prodinfo
*ap
;
563 struct amr_enquiry
*ae
;
567 * If we haven't found the real limit yet, let us have a couple of commands in
568 * order to be able to probe.
570 if (sc
->amr_maxio
== 0)
574 * Greater than 10 byte cdb support
576 sc
->support_ext_cdb
= amr_support_ext_cdb(sc
);
578 if(sc
->support_ext_cdb
) {
579 debug(2,"supports extended CDBs.");
583 * Try to issue an ENQUIRY3 command
585 if ((aex
= amr_enquiry(sc
, 2048, AMR_CMD_CONFIG
, AMR_CONFIG_ENQ3
,
586 AMR_CONFIG_ENQ3_SOLICITED_FULL
)) != NULL
) {
589 * Fetch current state of logical drives.
591 for (ldrv
= 0; ldrv
< aex
->ae_numldrives
; ldrv
++) {
592 sc
->amr_drive
[ldrv
].al_size
= aex
->ae_drivesize
[ldrv
];
593 sc
->amr_drive
[ldrv
].al_state
= aex
->ae_drivestate
[ldrv
];
594 sc
->amr_drive
[ldrv
].al_properties
= aex
->ae_driveprop
[ldrv
];
595 debug(2, " drive %d: %d state %x properties %x\n", ldrv
, sc
->amr_drive
[ldrv
].al_size
,
596 sc
->amr_drive
[ldrv
].al_state
, sc
->amr_drive
[ldrv
].al_properties
);
598 kfree(aex
, M_DEVBUF
);
601 * Get product info for channel count.
603 if ((ap
= amr_enquiry(sc
, 2048, AMR_CMD_CONFIG
, AMR_CONFIG_PRODUCT_INFO
, 0)) == NULL
) {
604 device_printf(sc
->amr_dev
, "can't obtain product data from controller\n");
607 sc
->amr_maxdrives
= 40;
608 sc
->amr_maxchan
= ap
->ap_nschan
;
609 sc
->amr_maxio
= ap
->ap_maxio
;
610 sc
->amr_type
|= AMR_TYPE_40LD
;
615 /* failed, try the 8LD ENQUIRY commands */
616 if ((ae
= (struct amr_enquiry
*)amr_enquiry(sc
, 2048, AMR_CMD_EXT_ENQUIRY2
, 0, 0)) == NULL
) {
617 if ((ae
= (struct amr_enquiry
*)amr_enquiry(sc
, 2048, AMR_CMD_ENQUIRY
, 0, 0)) == NULL
) {
618 device_printf(sc
->amr_dev
, "can't obtain configuration data from controller\n");
621 ae
->ae_signature
= 0;
625 * Fetch current state of logical drives.
627 for (ldrv
= 0; ldrv
< ae
->ae_ldrv
.al_numdrives
; ldrv
++) {
628 sc
->amr_drive
[ldrv
].al_size
= ae
->ae_ldrv
.al_size
[ldrv
];
629 sc
->amr_drive
[ldrv
].al_state
= ae
->ae_ldrv
.al_state
[ldrv
];
630 sc
->amr_drive
[ldrv
].al_properties
= ae
->ae_ldrv
.al_properties
[ldrv
];
631 debug(2, " drive %d: %d state %x properties %x\n", ldrv
, sc
->amr_drive
[ldrv
].al_size
,
632 sc
->amr_drive
[ldrv
].al_state
, sc
->amr_drive
[ldrv
].al_properties
);
635 sc
->amr_maxdrives
= 8;
636 sc
->amr_maxchan
= ae
->ae_adapter
.aa_channels
;
637 sc
->amr_maxio
= ae
->ae_adapter
.aa_maxio
;
642 * Mark remaining drives as unused.
644 for (; ldrv
< AMR_MAXLD
; ldrv
++)
645 sc
->amr_drive
[ldrv
].al_size
= 0xffffffff;
648 * Cap the maximum number of outstanding I/Os. AMI's Linux driver doesn't trust
649 * the controller's reported value, and lockups have been seen when we do.
651 sc
->amr_maxio
= imin(sc
->amr_maxio
, AMR_LIMITCMD
);
656 /********************************************************************************
657 * Run a generic enquiry-style command.
660 amr_enquiry(struct amr_softc
*sc
, size_t bufsize
, u_int8_t cmd
, u_int8_t cmdsub
, u_int8_t cmdqual
)
662 struct amr_command
*ac
;
672 /* get ourselves a command buffer */
673 if ((ac
= amr_alloccmd(sc
)) == NULL
)
675 /* allocate the response structure */
676 result
= kmalloc(bufsize
, M_DEVBUF
, M_INTWAIT
);
677 /* set command flags */
678 ac
->ac_flags
|= AMR_CMD_PRIORITY
| AMR_CMD_DATAOUT
;
680 /* point the command at our data */
681 ac
->ac_data
= result
;
682 ac
->ac_length
= bufsize
;
684 /* build the command proper */
685 mbox
= (u_int8_t
*)&ac
->ac_mailbox
; /* XXX want a real structure for this? */
690 /* can't assume that interrupts are going to work here, so play it safe */
691 if (sc
->amr_poll_command(ac
))
693 error
= ac
->ac_status
;
698 if ((error
!= 0) && (result
!= NULL
)) {
699 kfree(result
, M_DEVBUF
);
705 /********************************************************************************
706 * Flush the controller's internal cache, return status.
709 amr_flush(struct amr_softc
*sc
)
711 struct amr_command
*ac
;
714 /* get ourselves a command buffer */
716 if ((ac
= amr_alloccmd(sc
)) == NULL
)
718 /* set command flags */
719 ac
->ac_flags
|= AMR_CMD_PRIORITY
| AMR_CMD_DATAOUT
;
721 /* build the command proper */
722 ac
->ac_mailbox
.mb_command
= AMR_CMD_FLUSH
;
724 /* we have to poll, as the system may be going down or otherwise damaged */
725 if (sc
->amr_poll_command(ac
))
727 error
= ac
->ac_status
;
735 /********************************************************************************
736 * Detect extented cdb >> greater than 10 byte cdb support
737 * returns '1' means this support exist
738 * returns '0' means this support doesn't exist
741 amr_support_ext_cdb(struct amr_softc
*sc
)
743 struct amr_command
*ac
;
747 /* get ourselves a command buffer */
749 if ((ac
= amr_alloccmd(sc
)) == NULL
)
751 /* set command flags */
752 ac
->ac_flags
|= AMR_CMD_PRIORITY
| AMR_CMD_DATAOUT
;
754 /* build the command proper */
755 mbox
= (u_int8_t
*)&ac
->ac_mailbox
; /* XXX want a real structure for this? */
760 /* we have to poll, as the system may be going down or otherwise damaged */
761 if (sc
->amr_poll_command(ac
))
763 if( ac
->ac_status
== AMR_STATUS_SUCCESS
) {
773 /********************************************************************************
774 * Try to find I/O work for the controller from one or more of the work queues.
776 * We make the assumption that if the controller is not ready to take a command
777 * at some given time, it will generate an interrupt at some later time when
781 amr_startio(struct amr_softc
*sc
)
783 struct amr_command
*ac
;
785 /* spin until something prevents us from doing any work */
788 /* try to get a ready command */
789 ac
= amr_dequeue_ready(sc
);
791 /* if that failed, build a command from a bio */
793 (void)amr_bio_command(sc
, &ac
);
795 /* if that failed, build a command from a ccb */
797 (void)amr_cam_command(sc
, &ac
);
799 /* if we don't have anything to do, give up */
803 /* try to give the command to the controller; if this fails save it for later and give up */
805 debug(2, "controller busy, command deferred");
806 amr_requeue_ready(ac
); /* XXX schedule retry very soon? */
812 /********************************************************************************
813 * Handle completion of an I/O command.
816 amr_completeio(struct amr_command
*ac
)
818 struct amr_softc
*sc
= ac
->ac_sc
;
820 if (ac
->ac_status
!= AMR_STATUS_SUCCESS
) { /* could be more verbose here? */
821 ac
->ac_bio
->bio_buf
->b_error
= EIO
;
822 ac
->ac_bio
->bio_buf
->b_flags
|= B_ERROR
;
824 device_printf(sc
->amr_dev
, "I/O error - 0x%x\n", ac
->ac_status
);
825 /* amr_printcommand(ac);*/
827 amrd_intr(ac
->ac_bio
);
831 /********************************************************************************
832 ********************************************************************************
834 ********************************************************************************
835 ********************************************************************************/
837 /********************************************************************************
838 * Convert a bio off the top of the bio queue into a command.
841 amr_bio_command(struct amr_softc
*sc
, struct amr_command
**acp
)
843 struct amr_command
*ac
;
844 struct amrd_softc
*amrd
;
855 /* get a bio to work on */
856 if ((bio
= amr_dequeue_bio(sc
)) == NULL
)
860 if ((ac
= amr_alloccmd(sc
)) == NULL
) {
865 /* connect the bio to the command */
866 ac
->ac_complete
= amr_completeio
;
868 ac
->ac_data
= bio
->bio_buf
->b_data
;
869 ac
->ac_length
= bio
->bio_buf
->b_bcount
;
870 if (bio
->bio_buf
->b_cmd
== BUF_CMD_READ
) {
871 ac
->ac_flags
|= AMR_CMD_DATAIN
;
874 ac
->ac_flags
|= AMR_CMD_DATAOUT
;
875 cmd
= AMR_CMD_LWRITE
;
877 amrd
= (struct amrd_softc
*)bio
->bio_driver_info
;
878 driveno
= amrd
->amrd_drive
- sc
->amr_drive
;
879 blkcount
= (bio
->bio_buf
->b_bcount
+ AMR_BLKSIZE
- 1) / AMR_BLKSIZE
;
880 lba
= bio
->bio_offset
/ AMR_BLKSIZE
;
881 KKASSERT(lba
< 0x100000000ULL
);
883 ac
->ac_mailbox
.mb_command
= cmd
;
884 ac
->ac_mailbox
.mb_blkcount
= blkcount
;
885 ac
->ac_mailbox
.mb_lba
= lba
;
886 ac
->ac_mailbox
.mb_drive
= driveno
;
887 /* we fill in the s/g related data when the command is mapped */
889 if ((lba
+ blkcount
) > sc
->amr_drive
[driveno
].al_size
)
890 device_printf(sc
->amr_dev
, "I/O beyond end of unit (%lld,%d > %lu)\n",
892 (u_long
)sc
->amr_drive
[driveno
].al_size
);
898 if (bio
!= NULL
) /* this breaks ordering... */
899 amr_enqueue_bio(sc
, bio
);
905 /********************************************************************************
906 * Take a command, submit it to the controller and sleep until it completes
907 * or fails. Interrupts must be enabled, returns nonzero on error.
910 amr_wait_command(struct amr_command
*ac
)
916 ac
->ac_complete
= NULL
;
917 ac
->ac_flags
|= AMR_CMD_SLEEP
;
918 if ((error
= amr_start(ac
)) != 0)
922 /* XXX better timeout? */
923 while ((ac
->ac_flags
& AMR_CMD_BUSY
) && (count
< 30)) {
924 tsleep(ac
, PCATCH
, "amrwcmd", hz
);
929 /********************************************************************************
930 * Take a command, submit it to the controller and busy-wait for it to return.
931 * Returns nonzero on error. Can be safely called with interrupts enabled.
934 amr_std_poll_command(struct amr_command
*ac
)
936 struct amr_softc
*sc
= ac
->ac_sc
;
941 ac
->ac_complete
= NULL
;
942 if ((error
= amr_start(ac
)) != 0)
948 * Poll for completion, although the interrupt handler may beat us to it.
949 * Note that the timeout here is somewhat arbitrary.
953 } while ((ac
->ac_flags
& AMR_CMD_BUSY
) && (count
++ < 1000));
954 if (!(ac
->ac_flags
& AMR_CMD_BUSY
)) {
957 /* XXX the slot is now marked permanently busy */
959 device_printf(sc
->amr_dev
, "polled command timeout\n");
964 /********************************************************************************
965 * Take a command, submit it to the controller and busy-wait for it to return.
966 * Returns nonzero on error. Can be safely called with interrupts enabled.
969 amr_quartz_poll_command(struct amr_command
*ac
)
971 struct amr_softc
*sc
= ac
->ac_sc
;
976 /* now we have a slot, we can map the command (unmapped in amr_complete) */
981 if (sc
->amr_state
& AMR_STATE_INTEN
) {
983 while (sc
->amr_busyslots
) {
984 tsleep(sc
, PCATCH
, "amrpoll", hz
);
990 if(sc
->amr_busyslots
) {
991 device_printf(sc
->amr_dev
, "adapter is busy\n");
999 bcopy(&ac
->ac_mailbox
, (void *)(uintptr_t)(volatile void *)sc
->amr_mailbox
, AMR_MBOX_CMDSIZE
);
1001 /* clear the poll/ack fields in the mailbox */
1002 sc
->amr_mailbox
->mb_ident
= 0xFE;
1003 sc
->amr_mailbox
->mb_nstatus
= 0xFF;
1004 sc
->amr_mailbox
->mb_status
= 0xFF;
1005 sc
->amr_mailbox
->mb_poll
= 0;
1006 sc
->amr_mailbox
->mb_ack
= 0;
1007 sc
->amr_mailbox
->mb_busy
= 1;
1009 AMR_QPUT_IDB(sc
, sc
->amr_mailboxphys
| AMR_QIDB_SUBMIT
);
1011 while(sc
->amr_mailbox
->mb_nstatus
== 0xFF);
1012 while(sc
->amr_mailbox
->mb_status
== 0xFF);
1013 ac
->ac_status
=sc
->amr_mailbox
->mb_status
;
1014 error
= (ac
->ac_status
!=AMR_STATUS_SUCCESS
) ? 1:0;
1015 while(sc
->amr_mailbox
->mb_poll
!= 0x77);
1016 sc
->amr_mailbox
->mb_poll
= 0;
1017 sc
->amr_mailbox
->mb_ack
= 0x77;
1019 /* acknowledge that we have the commands */
1020 AMR_QPUT_IDB(sc
, sc
->amr_mailboxphys
| AMR_QIDB_ACK
);
1021 while(AMR_QGET_IDB(sc
) & AMR_QIDB_ACK
);
1025 /* unmap the command's data buffer */
1031 /********************************************************************************
1032 * Get a free command slot for a command if it doesn't already have one.
1034 * May be safely called multiple times for a given command.
1037 amr_getslot(struct amr_command
*ac
)
1039 struct amr_softc
*sc
= ac
->ac_sc
;
1040 int slot
, limit
, error
;
1044 /* if the command already has a slot, don't try to give it another one */
1045 if (ac
->ac_slot
!= 0)
1048 /* enforce slot usage limit */
1049 limit
= (ac
->ac_flags
& AMR_CMD_PRIORITY
) ? sc
->amr_maxio
: sc
->amr_maxio
- 4;
1050 if (sc
->amr_busyslots
> limit
)
1054 * Allocate a slot. XXX linear scan is slow
1058 for (slot
= 0; slot
< sc
->amr_maxio
; slot
++) {
1059 if (sc
->amr_busycmd
[slot
] == NULL
) {
1060 sc
->amr_busycmd
[slot
] = ac
;
1061 sc
->amr_busyslots
++;
1072 /********************************************************************************
1073 * Map/unmap (ac)'s data in the controller's addressable space as required.
1075 * These functions may be safely called multiple times on a given command.
1078 amr_setup_dmamap(void *arg
, bus_dma_segment_t
*segs
, int nsegments
, int error
)
1080 struct amr_command
*ac
= (struct amr_command
*)arg
;
1081 struct amr_softc
*sc
= ac
->ac_sc
;
1082 struct amr_sgentry
*sg
;
1088 /* get base address of s/g table */
1089 sg
= sc
->amr_sgtable
+ (ac
->ac_slot
* AMR_NSEG
);
1091 /* save data physical address */
1092 ac
->ac_dataphys
= segs
[0].ds_addr
;
1094 /* for AMR_CMD_CONFIG the s/g count goes elsewhere */
1095 if (ac
->ac_mailbox
.mb_command
== AMR_CMD_CONFIG
) {
1096 sgc
= &(((struct amr_mailbox_ioctl
*)&ac
->ac_mailbox
)->mb_param
);
1098 sgc
= &ac
->ac_mailbox
.mb_nsgelem
;
1101 /* decide whether we need to populate the s/g table */
1102 if (nsegments
< 2) {
1104 ac
->ac_mailbox
.mb_nsgelem
= 0;
1105 ac
->ac_mailbox
.mb_physaddr
= ac
->ac_dataphys
;
1107 ac
->ac_mailbox
.mb_nsgelem
= nsegments
;
1109 ac
->ac_mailbox
.mb_physaddr
= sc
->amr_sgbusaddr
+ (ac
->ac_slot
* AMR_NSEG
* sizeof(struct amr_sgentry
));
1110 for (i
= 0; i
< nsegments
; i
++, sg
++) {
1111 sg
->sg_addr
= segs
[i
].ds_addr
;
1112 sg
->sg_count
= segs
[i
].ds_len
;
1118 amr_setup_ccbmap(void *arg
, bus_dma_segment_t
*segs
, int nsegments
, int error
)
1120 struct amr_command
*ac
= (struct amr_command
*)arg
;
1121 struct amr_softc
*sc
= ac
->ac_sc
;
1122 struct amr_sgentry
*sg
;
1123 struct amr_passthrough
*ap
= (struct amr_passthrough
*)ac
->ac_data
;
1124 struct amr_ext_passthrough
*aep
= (struct amr_ext_passthrough
*)ac
->ac_data
;
1127 /* get base address of s/g table */
1128 sg
= sc
->amr_sgtable
+ (ac
->ac_slot
* AMR_NSEG
);
1130 /* decide whether we need to populate the s/g table */
1131 if( ac
->ac_mailbox
.mb_command
== AMR_CMD_EXTPASS
) {
1132 if (nsegments
< 2) {
1133 aep
->ap_no_sg_elements
= 0;
1134 aep
->ap_data_transfer_address
= segs
[0].ds_addr
;
1136 /* save s/g table information in passthrough */
1137 aep
->ap_no_sg_elements
= nsegments
;
1138 aep
->ap_data_transfer_address
= sc
->amr_sgbusaddr
+ (ac
->ac_slot
* AMR_NSEG
* sizeof(struct amr_sgentry
));
1139 /* populate s/g table (overwrites previous call which mapped the passthrough) */
1140 for (i
= 0; i
< nsegments
; i
++, sg
++) {
1141 sg
->sg_addr
= segs
[i
].ds_addr
;
1142 sg
->sg_count
= segs
[i
].ds_len
;
1143 debug(3, " %d: 0x%x/%d", i
, sg
->sg_addr
, sg
->sg_count
);
1146 debug(3, "slot %d %d segments at 0x%x, passthrough at 0x%x", ac
->ac_slot
,
1147 aep
->ap_no_sg_elements
, aep
->ap_data_transfer_address
, ac
->ac_dataphys
);
1149 if (nsegments
< 2) {
1150 ap
->ap_no_sg_elements
= 0;
1151 ap
->ap_data_transfer_address
= segs
[0].ds_addr
;
1153 /* save s/g table information in passthrough */
1154 ap
->ap_no_sg_elements
= nsegments
;
1155 ap
->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
);
1163 debug(3, "slot %d %d segments at 0x%x, passthrough at 0x%x", ac
->ac_slot
,
1164 ap
->ap_no_sg_elements
, ap
->ap_data_transfer_address
, ac
->ac_dataphys
);
1169 amr_mapcmd(struct amr_command
*ac
)
1171 struct amr_softc
*sc
= ac
->ac_sc
;
1175 /* if the command involves data at all, and hasn't been mapped */
1176 if (!(ac
->ac_flags
& AMR_CMD_MAPPED
)) {
1178 if (ac
->ac_data
!= NULL
) {
1179 /* map the data buffers into bus space and build the s/g list */
1180 bus_dmamap_load(sc
->amr_buffer_dmat
, ac
->ac_dmamap
, ac
->ac_data
, ac
->ac_length
,
1181 amr_setup_dmamap
, ac
, 0);
1182 if (ac
->ac_flags
& AMR_CMD_DATAIN
)
1183 bus_dmamap_sync(sc
->amr_buffer_dmat
, ac
->ac_dmamap
, BUS_DMASYNC_PREREAD
);
1184 if (ac
->ac_flags
& AMR_CMD_DATAOUT
)
1185 bus_dmamap_sync(sc
->amr_buffer_dmat
, ac
->ac_dmamap
, BUS_DMASYNC_PREWRITE
);
1188 if (ac
->ac_ccb_data
!= NULL
) {
1189 bus_dmamap_load(sc
->amr_buffer_dmat
, ac
->ac_ccb_dmamap
, ac
->ac_ccb_data
, ac
->ac_ccb_length
,
1190 amr_setup_ccbmap
, ac
, 0);
1191 if (ac
->ac_flags
& AMR_CMD_CCB_DATAIN
)
1192 bus_dmamap_sync(sc
->amr_buffer_dmat
, ac
->ac_ccb_dmamap
, BUS_DMASYNC_PREREAD
);
1193 if (ac
->ac_flags
& AMR_CMD_CCB_DATAOUT
)
1194 bus_dmamap_sync(sc
->amr_buffer_dmat
, ac
->ac_ccb_dmamap
, BUS_DMASYNC_PREWRITE
);
1196 ac
->ac_flags
|= AMR_CMD_MAPPED
;
1201 amr_unmapcmd(struct amr_command
*ac
)
1203 struct amr_softc
*sc
= ac
->ac_sc
;
1207 /* if the command involved data at all and was mapped */
1208 if (ac
->ac_flags
& AMR_CMD_MAPPED
) {
1210 if (ac
->ac_data
!= NULL
) {
1211 if (ac
->ac_flags
& AMR_CMD_DATAIN
)
1212 bus_dmamap_sync(sc
->amr_buffer_dmat
, ac
->ac_dmamap
, BUS_DMASYNC_POSTREAD
);
1213 if (ac
->ac_flags
& AMR_CMD_DATAOUT
)
1214 bus_dmamap_sync(sc
->amr_buffer_dmat
, ac
->ac_dmamap
, BUS_DMASYNC_POSTWRITE
);
1215 bus_dmamap_unload(sc
->amr_buffer_dmat
, ac
->ac_dmamap
);
1218 if (ac
->ac_ccb_data
!= NULL
) {
1219 if (ac
->ac_flags
& AMR_CMD_CCB_DATAIN
)
1220 bus_dmamap_sync(sc
->amr_buffer_dmat
, ac
->ac_ccb_dmamap
, BUS_DMASYNC_POSTREAD
);
1221 if (ac
->ac_flags
& AMR_CMD_CCB_DATAOUT
)
1222 bus_dmamap_sync(sc
->amr_buffer_dmat
, ac
->ac_ccb_dmamap
, BUS_DMASYNC_POSTWRITE
);
1223 bus_dmamap_unload(sc
->amr_buffer_dmat
, ac
->ac_ccb_dmamap
);
1225 ac
->ac_flags
&= ~AMR_CMD_MAPPED
;
1229 /********************************************************************************
1230 * Take a command and give it to the controller, returns 0 if successful, or
1231 * EBUSY if the command should be retried later.
1234 amr_start(struct amr_command
*ac
)
1236 struct amr_softc
*sc
= ac
->ac_sc
;
1241 /* mark command as busy so that polling consumer can tell */
1242 ac
->ac_flags
|= AMR_CMD_BUSY
;
1244 /* get a command slot (freed in amr_done) */
1245 if (amr_getslot(ac
))
1248 /* now we have a slot, we can map the command (unmapped in amr_complete) */
1251 /* mark the new mailbox we are going to copy in as busy */
1252 ac
->ac_mailbox
.mb_busy
= 1;
1254 /* clear the poll/ack fields in the mailbox */
1255 sc
->amr_mailbox
->mb_poll
= 0;
1256 sc
->amr_mailbox
->mb_ack
= 0;
1259 * Save the slot number so that we can locate this command when complete.
1260 * Note that ident = 0 seems to be special, so we don't use it.
1262 ac
->ac_mailbox
.mb_ident
= ac
->ac_slot
+ 1;
1265 * Spin waiting for the mailbox, give up after ~1 second. We expect the
1266 * controller to be able to handle our I/O.
1268 * XXX perhaps we should wait for less time, and count on the deferred command
1269 * handling to deal with retries?
1271 debug(4, "wait for mailbox");
1272 for (i
= 10000, done
= 0; (i
> 0) && !done
; i
--) {
1275 /* is the mailbox free? */
1276 if (sc
->amr_mailbox
->mb_busy
== 0) {
1277 debug(4, "got mailbox");
1278 sc
->amr_mailbox64
->mb64_segment
= 0;
1279 bcopy(&ac
->ac_mailbox
, (void *)(uintptr_t)(volatile void *)sc
->amr_mailbox
, AMR_MBOX_CMDSIZE
);
1282 /* not free, spin waiting */
1284 debug(4, "busy flag %x\n", sc
->amr_mailbox
->mb_busy
);
1285 /* this is somewhat ugly */
1292 * Now give the command to the controller
1295 if (sc
->amr_submit_command(sc
)) {
1296 /* the controller wasn't ready to take the command, forget that we tried to post it */
1297 sc
->amr_mailbox
->mb_busy
= 0;
1300 debug(3, "posted command");
1305 * The controller wouldn't take the command. Return the command as busy
1306 * so that it is retried later.
1311 /********************************************************************************
1312 * Extract one or more completed commands from the controller (sc)
1314 * Returns nonzero if any commands on the work queue were marked as completed.
1317 amr_done(struct amr_softc
*sc
)
1319 struct amr_command
*ac
;
1320 struct amr_mailbox mbox
;
1325 /* See if there's anything for us to do */
1328 /* loop collecting completed commands */
1330 /* poll for a completed command's identifier and status */
1331 if (sc
->amr_get_work(sc
, &mbox
)) {
1334 /* iterate over completed commands in this result */
1335 for (i
= 0; i
< mbox
.mb_nstatus
; i
++) {
1336 /* get pointer to busy command */
1337 idx
= mbox
.mb_completed
[i
] - 1;
1338 ac
= sc
->amr_busycmd
[idx
];
1340 /* really a busy command? */
1343 /* pull the command from the busy index */
1344 sc
->amr_busycmd
[idx
] = NULL
;
1345 sc
->amr_busyslots
--;
1347 /* save status for later use */
1348 ac
->ac_status
= mbox
.mb_status
;
1349 amr_enqueue_completed(ac
);
1350 debug(3, "completed command with status %x", mbox
.mb_status
);
1352 device_printf(sc
->amr_dev
, "bad slot %d completed\n", idx
);
1356 break; /* no work */
1360 /* if we've completed any commands, try posting some more */
1364 /* handle completion and timeouts */
1365 #if defined(__FreeBSD__) && __FreeBSD_version >= 500005
1366 if (sc
->amr_state
& AMR_STATE_INTEN
)
1367 taskqueue_enqueue(taskqueue_swi
, &sc
->amr_task_complete
);
1370 amr_complete(sc
, 0);
1375 /********************************************************************************
1376 * Do completion processing on done commands on (sc)
1379 amr_complete(void *context
, int pending
)
1381 struct amr_softc
*sc
= (struct amr_softc
*)context
;
1382 struct amr_command
*ac
;
1386 /* pull completed commands off the queue */
1388 ac
= amr_dequeue_completed(sc
);
1392 /* unmap the command's data buffer */
1395 /* unbusy the command */
1396 ac
->ac_flags
&= ~AMR_CMD_BUSY
;
1399 * Is there a completion handler?
1401 if (ac
->ac_complete
!= NULL
) {
1402 ac
->ac_complete(ac
);
1405 * Is someone sleeping on this one?
1407 } else if (ac
->ac_flags
& AMR_CMD_SLEEP
) {
1411 if(!sc
->amr_busyslots
) {
1417 /********************************************************************************
1418 ********************************************************************************
1419 Command Buffer Management
1420 ********************************************************************************
1421 ********************************************************************************/
1423 /********************************************************************************
1424 * Get a new command buffer.
1426 * This may return NULL in low-memory cases.
1428 * If possible, we recycle a command buffer that's been used before.
1430 struct amr_command
*
1431 amr_alloccmd(struct amr_softc
*sc
)
1433 struct amr_command
*ac
;
1437 ac
= amr_dequeue_free(sc
);
1439 amr_alloccmd_cluster(sc
);
1440 ac
= amr_dequeue_free(sc
);
1445 /* clear out significant fields */
1448 bzero(&ac
->ac_mailbox
, sizeof(struct amr_mailbox
));
1452 ac
->ac_ccb_data
= NULL
;
1453 ac
->ac_complete
= NULL
;
1457 /********************************************************************************
1458 * Release a command buffer for recycling.
1461 amr_releasecmd(struct amr_command
*ac
)
1465 amr_enqueue_free(ac
);
1468 /********************************************************************************
1469 * Allocate a new command cluster and initialise it.
1472 amr_alloccmd_cluster(struct amr_softc
*sc
)
1474 struct amr_command_cluster
*acc
;
1475 struct amr_command
*ac
;
1478 acc
= kmalloc(AMR_CMD_CLUSTERSIZE
, M_DEVBUF
, M_INTWAIT
);
1480 TAILQ_INSERT_TAIL(&sc
->amr_cmd_clusters
, acc
, acc_link
);
1482 for (i
= 0; i
< AMR_CMD_CLUSTERCOUNT
; i
++) {
1483 ac
= &acc
->acc_command
[i
];
1484 bzero(ac
, sizeof(*ac
));
1486 if (!bus_dmamap_create(sc
->amr_buffer_dmat
, 0, &ac
->ac_dmamap
) &&
1487 !bus_dmamap_create(sc
->amr_buffer_dmat
, 0, &ac
->ac_ccb_dmamap
))
1492 /********************************************************************************
1493 * Free a command cluster
1496 amr_freecmd_cluster(struct amr_command_cluster
*acc
)
1498 struct amr_softc
*sc
= acc
->acc_command
[0].ac_sc
;
1501 for (i
= 0; i
< AMR_CMD_CLUSTERCOUNT
; i
++)
1502 bus_dmamap_destroy(sc
->amr_buffer_dmat
, acc
->acc_command
[i
].ac_dmamap
);
1503 kfree(acc
, M_DEVBUF
);
1506 /********************************************************************************
1507 ********************************************************************************
1508 Interface-specific Shims
1509 ********************************************************************************
1510 ********************************************************************************/
1512 /********************************************************************************
1513 * Tell the controller that the mailbox contains a valid command
1516 amr_quartz_submit_command(struct amr_softc
*sc
)
1520 if (AMR_QGET_IDB(sc
) & AMR_QIDB_SUBMIT
)
1522 AMR_QPUT_IDB(sc
, sc
->amr_mailboxphys
| AMR_QIDB_SUBMIT
);
1527 amr_std_submit_command(struct amr_softc
*sc
)
1531 if (AMR_SGET_MBSTAT(sc
) & AMR_SMBOX_BUSYFLAG
)
1533 AMR_SPOST_COMMAND(sc
);
1537 /********************************************************************************
1538 * Claim any work that the controller has completed; acknowledge completion,
1539 * save details of the completion in (mbsave)
1542 amr_quartz_get_work(struct amr_softc
*sc
, struct amr_mailbox
*mbsave
)
1552 /* work waiting for us? */
1553 if ((outd
= AMR_QGET_ODB(sc
)) == AMR_QODB_READY
) {
1555 /* save mailbox, which contains a list of completed commands */
1556 bcopy((void *)(uintptr_t)(volatile void *)sc
->amr_mailbox
, mbsave
, sizeof(*mbsave
));
1558 /* acknowledge interrupt */
1559 AMR_QPUT_ODB(sc
, AMR_QODB_READY
);
1561 /* acknowledge that we have the commands */
1562 AMR_QPUT_IDB(sc
, sc
->amr_mailboxphys
| AMR_QIDB_ACK
);
1564 #ifndef AMR_QUARTZ_GOFASTER
1566 * This waits for the controller to notice that we've taken the
1567 * command from it. It's very inefficient, and we shouldn't do it,
1568 * but if we remove this code, we stop completing commands under
1571 * Peter J says we shouldn't do this. The documentation says we
1572 * should. Who is right?
1574 while(AMR_QGET_IDB(sc
) & AMR_QIDB_ACK
)
1575 ; /* XXX aiee! what if it dies? */
1578 worked
= 1; /* got some work */
1586 amr_std_get_work(struct amr_softc
*sc
, struct amr_mailbox
*mbsave
)
1596 /* check for valid interrupt status */
1597 istat
= AMR_SGET_ISTAT(sc
);
1598 if ((istat
& AMR_SINTR_VALID
) != 0) {
1599 AMR_SPUT_ISTAT(sc
, istat
); /* ack interrupt status */
1601 /* save mailbox, which contains a list of completed commands */
1602 bcopy((void *)(uintptr_t)(volatile void *)sc
->amr_mailbox
, mbsave
, sizeof(*mbsave
));
1604 AMR_SACK_INTERRUPT(sc
); /* acknowledge we have the mailbox */
1612 /********************************************************************************
1613 * Notify the controller of the mailbox location.
1616 amr_std_attach_mailbox(struct amr_softc
*sc
)
1619 /* program the mailbox physical address */
1620 AMR_SBYTE_SET(sc
, AMR_SMBOX_0
, sc
->amr_mailboxphys
& 0xff);
1621 AMR_SBYTE_SET(sc
, AMR_SMBOX_1
, (sc
->amr_mailboxphys
>> 8) & 0xff);
1622 AMR_SBYTE_SET(sc
, AMR_SMBOX_2
, (sc
->amr_mailboxphys
>> 16) & 0xff);
1623 AMR_SBYTE_SET(sc
, AMR_SMBOX_3
, (sc
->amr_mailboxphys
>> 24) & 0xff);
1624 AMR_SBYTE_SET(sc
, AMR_SMBOX_ENABLE
, AMR_SMBOX_ADDR
);
1626 /* clear any outstanding interrupt and enable interrupts proper */
1627 AMR_SACK_INTERRUPT(sc
);
1628 AMR_SENABLE_INTR(sc
);
1631 #ifdef AMR_BOARD_INIT
1632 /********************************************************************************
1633 * Initialise the controller
1636 amr_quartz_init(struct amr_softc
*sc
)
1638 int status
, ostatus
;
1640 device_printf(sc
->amr_dev
, "initial init status %x\n", AMR_QGET_INITSTATUS(sc
));
1645 while ((status
= AMR_QGET_INITSTATUS(sc
)) != AMR_QINIT_DONE
) {
1646 if (status
!= ostatus
) {
1647 device_printf(sc
->amr_dev
, "(%x) %s\n", status
, amr_describe_code(amr_table_qinit
, status
));
1651 case AMR_QINIT_NOMEM
:
1654 case AMR_QINIT_SCAN
:
1655 /* XXX we could print channel/target here */
1663 amr_std_init(struct amr_softc
*sc
)
1665 int status
, ostatus
;
1667 device_printf(sc
->amr_dev
, "initial init status %x\n", AMR_SGET_INITSTATUS(sc
));
1672 while ((status
= AMR_SGET_INITSTATUS(sc
)) != AMR_SINIT_DONE
) {
1673 if (status
!= ostatus
) {
1674 device_printf(sc
->amr_dev
, "(%x) %s\n", status
, amr_describe_code(amr_table_sinit
, status
));
1678 case AMR_SINIT_NOMEM
:
1681 case AMR_SINIT_INPROG
:
1682 /* XXX we could print channel/target here? */
1690 /********************************************************************************
1691 ********************************************************************************
1693 ********************************************************************************
1694 ********************************************************************************/
1696 /********************************************************************************
1697 * Identify the controller and print some information about it.
1700 amr_describe_controller(struct amr_softc
*sc
)
1702 struct amr_prodinfo
*ap
;
1703 struct amr_enquiry
*ae
;
1707 * Try to get 40LD product info, which tells us what the card is labelled as.
1709 if ((ap
= amr_enquiry(sc
, 2048, AMR_CMD_CONFIG
, AMR_CONFIG_PRODUCT_INFO
, 0)) != NULL
) {
1710 device_printf(sc
->amr_dev
, "<LSILogic %.80s> Firmware %.16s, BIOS %.16s, %dMB RAM\n",
1711 ap
->ap_product
, ap
->ap_firmware
, ap
->ap_bios
,
1714 kfree(ap
, M_DEVBUF
);
1719 * Try 8LD extended ENQUIRY to get controller signature, and use lookup table.
1721 if ((ae
= (struct amr_enquiry
*)amr_enquiry(sc
, 2048, AMR_CMD_EXT_ENQUIRY2
, 0, 0)) != NULL
) {
1722 prod
= amr_describe_code(amr_table_adaptertype
, ae
->ae_signature
);
1724 } else if ((ae
= (struct amr_enquiry
*)amr_enquiry(sc
, 2048, AMR_CMD_ENQUIRY
, 0, 0)) != NULL
) {
1727 * Try to work it out based on the PCI signatures.
1729 switch (pci_get_device(sc
->amr_dev
)) {
1731 prod
= "Series 428";
1734 prod
= "Series 434";
1737 prod
= "unknown controller";
1741 device_printf(sc
->amr_dev
, "<unsupported controller>\n");
1746 * HP NetRaid controllers have a special encoding of the firmware and
1747 * BIOS versions. The AMI version seems to have it as strings whereas
1748 * the HP version does it with a leading uppercase character and two
1752 if(ae
->ae_adapter
.aa_firmware
[2] >= 'A' &&
1753 ae
->ae_adapter
.aa_firmware
[2] <= 'Z' &&
1754 ae
->ae_adapter
.aa_firmware
[1] < ' ' &&
1755 ae
->ae_adapter
.aa_firmware
[0] < ' ' &&
1756 ae
->ae_adapter
.aa_bios
[2] >= 'A' &&
1757 ae
->ae_adapter
.aa_bios
[2] <= 'Z' &&
1758 ae
->ae_adapter
.aa_bios
[1] < ' ' &&
1759 ae
->ae_adapter
.aa_bios
[0] < ' ') {
1761 /* this looks like we have an HP NetRaid version of the MegaRaid */
1763 if(ae
->ae_signature
== AMR_SIG_438
) {
1764 /* the AMI 438 is a NetRaid 3si in HP-land */
1765 prod
= "HP NetRaid 3si";
1768 device_printf(sc
->amr_dev
, "<%s> Firmware %c.%02d.%02d, BIOS %c.%02d.%02d, %dMB RAM\n",
1769 prod
, ae
->ae_adapter
.aa_firmware
[2],
1770 ae
->ae_adapter
.aa_firmware
[1],
1771 ae
->ae_adapter
.aa_firmware
[0],
1772 ae
->ae_adapter
.aa_bios
[2],
1773 ae
->ae_adapter
.aa_bios
[1],
1774 ae
->ae_adapter
.aa_bios
[0],
1775 ae
->ae_adapter
.aa_memorysize
);
1777 device_printf(sc
->amr_dev
, "<%s> Firmware %.4s, BIOS %.4s, %dMB RAM\n",
1778 prod
, ae
->ae_adapter
.aa_firmware
, ae
->ae_adapter
.aa_bios
,
1779 ae
->ae_adapter
.aa_memorysize
);
1781 kfree(ae
, M_DEVBUF
);
1785 amr_dump_blocks(struct amr_softc
*sc
, int unit
, u_int64_t lba
, void *data
, int blks
)
1788 struct amr_command
*ac
;
1793 sc
->amr_state
&= ~AMR_STATE_INTEN
;
1795 /* get ourselves a command buffer */
1796 if ((ac
= amr_alloccmd(sc
)) == NULL
)
1798 /* set command flags */
1799 ac
->ac_flags
|= AMR_CMD_PRIORITY
| AMR_CMD_DATAOUT
;
1801 /* point the command at our data */
1803 ac
->ac_length
= blks
* AMR_BLKSIZE
;
1805 /* build the command proper */
1806 ac
->ac_mailbox
.mb_command
= AMR_CMD_LWRITE
;
1807 ac
->ac_mailbox
.mb_blkcount
= blks
;
1808 ac
->ac_mailbox
.mb_lba
= lba
;
1809 ac
->ac_mailbox
.mb_drive
= unit
;
1811 /* can't assume that interrupts are going to work here, so play it safe */
1812 if (sc
->amr_poll_command(ac
))
1814 error
= ac
->ac_status
;
1820 sc
->amr_state
|= AMR_STATE_INTEN
;
1827 /********************************************************************************
1828 * Print the command (ac) in human-readable format
1832 amr_printcommand(struct amr_command
*ac
)
1834 struct amr_softc
*sc
= ac
->ac_sc
;
1835 struct amr_sgentry
*sg
;
1838 device_printf(sc
->amr_dev
, "cmd %x ident %d drive %d\n",
1839 ac
->ac_mailbox
.mb_command
, ac
->ac_mailbox
.mb_ident
, ac
->ac_mailbox
.mb_drive
);
1840 device_printf(sc
->amr_dev
, "blkcount %d lba %d\n",
1841 ac
->ac_mailbox
.mb_blkcount
, ac
->ac_mailbox
.mb_lba
);
1842 device_printf(sc
->amr_dev
, "virtaddr %p length %lu\n", ac
->ac_data
, (unsigned long)ac
->ac_length
);
1843 device_printf(sc
->amr_dev
, "sg physaddr %08x nsg %d\n",
1844 ac
->ac_mailbox
.mb_physaddr
, ac
->ac_mailbox
.mb_nsgelem
);
1845 device_printf(sc
->amr_dev
, "ccb %p bio %p\n", ac
->ac_ccb_data
, ac
->ac_bio
);
1847 /* get base address of s/g table */
1848 sg
= sc
->amr_sgtable
+ (ac
->ac_slot
* AMR_NSEG
);
1849 for (i
= 0; i
< ac
->ac_mailbox
.mb_nsgelem
; i
++, sg
++)
1850 device_printf(sc
->amr_dev
, " %x/%d\n", sg
->sg_addr
, sg
->sg_count
);