2 * Written by: David Jeffery
3 * Copyright (c) 2002 Adaptec Inc.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <dev/ips/ipsreg.h>
32 #include <dev/ips/ips.h>
36 static d_open_t ips_open;
37 static d_close_t ips_close;
38 static d_ioctl_t ips_ioctl;
40 MALLOC_DEFINE(M_IPSBUF, "ipsbuf","IPS driver buffer");
42 static struct cdevsw ips_cdevsw = {
43 .d_version = D_VERSION,
50 static const char* ips_adapter_name[] = {
52 "ServeRAID (copperhead)",
53 "ServeRAID II (copperhead refresh)",
54 "ServeRAID onboard (copperhead)",
55 "ServeRAID onboard (copperhead)",
56 "ServeRAID 3H (clarinet)",
57 "ServeRAID 3L (clarinet lite)",
58 "ServeRAID 4H (trombone)",
59 "ServeRAID 4M (morpheus)",
60 "ServeRAID 4L (morpheus lite)",
61 "ServeRAID 4Mx (neo)",
62 "ServeRAID 4Lx (neo lite)",
63 "ServeRAID 5i II (sarasota)",
64 "ServeRAID 5i (sarasota)",
65 "ServeRAID 6M (marco)",
66 "ServeRAID 6i (sebring)",
73 static int ips_open(struct cdev *dev, int flags, int fmt, struct thread *td)
75 ips_softc_t *sc = dev->si_drv1;
76 mtx_lock(&sc->queue_mtx);
77 sc->state |= IPS_DEV_OPEN;
78 mtx_unlock(&sc->queue_mtx);
82 static int ips_close(struct cdev *dev, int flags, int fmt, struct thread *td)
84 ips_softc_t *sc = dev->si_drv1;
86 mtx_lock(&sc->queue_mtx);
87 sc->state &= ~IPS_DEV_OPEN;
88 mtx_unlock(&sc->queue_mtx);
93 static int ips_ioctl(struct cdev *dev, u_long command, caddr_t addr, int32_t flags, struct thread *td)
98 return ips_ioctl_request(sc, command, addr, flags);
101 static void ips_cmd_dmaload(void *cmdptr, bus_dma_segment_t *segments,int segnum, int error)
103 ips_command_t *command = cmdptr;
104 PRINTF(10, "ips: in ips_cmd_dmaload\n");
106 command->command_phys_addr = segments[0].ds_addr;
110 /* is locking needed? what locking guarentees are there on removal? */
111 static int ips_cmdqueue_free(ips_softc_t *sc)
114 ips_command_t *command;
116 if(!sc->used_commands){
117 for(i = 0; i < sc->max_cmds; i++){
119 command = &sc->commandarray[i];
121 if(command->command_phys_addr == 0)
123 bus_dmamap_unload(sc->command_dmatag,
124 command->command_dmamap);
125 bus_dmamem_free(sc->command_dmatag,
126 command->command_buffer,
127 command->command_dmamap);
128 if (command->data_dmamap != NULL)
129 bus_dmamap_destroy(command->data_dmatag,
130 command->data_dmamap);
133 sc->state |= IPS_OFFLINE;
135 sc->staticcmd = NULL;
136 free(sc->commandarray, M_DEVBUF);
140 /* places all ips command structs on the free command queue. No locking as if someone else tries
141 * to access this during init, we have bigger problems */
142 static int ips_cmdqueue_init(ips_softc_t *sc)
145 ips_command_t *command;
147 sc->commandarray = (ips_command_t *)malloc(sizeof(ips_command_t) *
148 sc->max_cmds, M_DEVBUF, M_NOWAIT|M_ZERO);
149 if (sc->commandarray == NULL)
152 SLIST_INIT(&sc->free_cmd_list);
153 for(i = 0; i < sc->max_cmds; i++){
154 command = &sc->commandarray[i];
158 if(bus_dmamem_alloc(sc->command_dmatag,&command->command_buffer,
159 BUS_DMA_NOWAIT, &command->command_dmamap))
161 bus_dmamap_load(sc->command_dmatag, command->command_dmamap,
162 command->command_buffer,IPS_COMMAND_LEN,
163 ips_cmd_dmaload, command, BUS_DMA_NOWAIT);
164 if(!command->command_phys_addr){
165 bus_dmamem_free(sc->command_dmatag,
166 command->command_buffer, command->command_dmamap);
171 command->data_dmatag = sc->sg_dmatag;
172 if (bus_dmamap_create(command->data_dmatag, 0,
173 &command->data_dmamap))
175 SLIST_INSERT_HEAD(&sc->free_cmd_list, command, next);
177 sc->staticcmd = command;
179 sc->state &= ~IPS_OFFLINE;
182 ips_cmdqueue_free(sc);
186 /* returns a free command struct if one is available.
187 * It also blanks out anything that may be a wild pointer/value.
188 * Also, command buffers are not freed. They are
189 * small so they are saved and kept dmamapped and loaded.
191 int ips_get_free_cmd(ips_softc_t *sc, ips_command_t **cmd, unsigned long flags)
193 ips_command_t *command;
195 if(sc->state & IPS_OFFLINE){
198 if ((flags & IPS_STATIC_FLAG) == 0) {
199 command = SLIST_FIRST(&sc->free_cmd_list);
200 if(!command || (sc->state & IPS_TIMEOUT)){
203 SLIST_REMOVE_HEAD(&sc->free_cmd_list, next);
204 (sc->used_commands)++;
206 if (sc->state & IPS_STATIC_BUSY)
208 command = sc->staticcmd;
209 sc->state |= IPS_STATIC_BUSY;
211 clear_ips_command(command);
212 bzero(command->command_buffer, IPS_COMMAND_LEN);
217 /* adds a command back to the free command queue */
218 void ips_insert_free_cmd(ips_softc_t *sc, ips_command_t *command)
221 if (sema_value(&sc->cmd_sema) != 0)
222 panic("ips: command returned non-zero semaphore");
224 if (command != sc->staticcmd) {
225 SLIST_INSERT_HEAD(&sc->free_cmd_list, command, next);
226 (sc->used_commands)--;
228 sc->state &= ~IPS_STATIC_BUSY;
231 static const char* ips_diskdev_statename(u_int8_t state)
233 static char statebuf[20];
241 case IPS_LD_DEGRADED:
254 sprintf(statebuf,"UNKNOWN(0x%02x)", state);
258 static int ips_diskdev_init(ips_softc_t *sc)
261 for(i=0; i < IPS_MAX_NUM_DRIVES; i++){
262 if(sc->drives[i].state == IPS_LD_FREE) continue;
263 device_printf(sc->dev, "Logical Drive %d: RAID%d sectors: %u, state %s\n",
264 i, sc->drives[i].raid_lvl,
265 sc->drives[i].sector_count,
266 ips_diskdev_statename(sc->drives[i].state));
267 if(sc->drives[i].state == IPS_LD_OKAY ||
268 sc->drives[i].state == IPS_LD_DEGRADED){
269 sc->diskdev[i] = device_add_child(sc->dev, NULL, -1);
270 device_set_ivars(sc->diskdev[i],(void *)(uintptr_t) i);
273 if(bus_generic_attach(sc->dev)){
274 device_printf(sc->dev, "Attaching bus failed\n");
279 static int ips_diskdev_free(ips_softc_t *sc)
283 for(i = 0; i < IPS_MAX_NUM_DRIVES; i++){
285 error = device_delete_child(sc->dev, sc->diskdev[i]);
289 bus_generic_detach(sc->dev);
293 /* ips_timeout is periodically called to make sure no commands sent
294 * to the card have become stuck. If it finds a stuck command, it
295 * sets a flag so the driver won't start any more commands and then
296 * is periodically called to see if all outstanding commands have
297 * either finished or timed out. Once timed out, an attempt to
298 * reinitialize the card is made. If that fails, the driver gives
299 * up and declares the card dead. */
300 static void ips_timeout(void *arg)
302 ips_softc_t *sc = arg;
304 ips_command_t *command;
306 mtx_assert(&sc->queue_mtx, MA_OWNED);
307 command = &sc->commandarray[0];
308 for(i = 0; i < sc->max_cmds; i++){
309 if(!command[i].timeout){
312 command[i].timeout--;
313 if(!command[i].timeout){
314 if(!(sc->state & IPS_TIMEOUT)){
315 sc->state |= IPS_TIMEOUT;
316 device_printf(sc->dev, "WARNING: command timeout. Adapter is in toaster mode, resetting to known state\n");
318 ips_set_error(&command[i], ETIMEDOUT);
319 command[i].callback(&command[i]);
320 /* hmm, this should be enough cleanup */
324 if(!state && (sc->state & IPS_TIMEOUT)){
325 if(sc->ips_adapter_reinit(sc, 1)){
326 device_printf(sc->dev, "AIEE! adapter reset failed, giving up and going home! Have a nice day.\n");
327 sc->state |= IPS_OFFLINE;
328 sc->state &= ~IPS_TIMEOUT;
329 /* Grr, I hate this solution. I run waiting commands
330 one at a time and error them out just before they
331 would go to the card. This sucks. */
333 sc->state &= ~IPS_TIMEOUT;
335 if (sc->state != IPS_OFFLINE)
336 callout_reset(&sc->timer, 10 * hz, ips_timeout, sc);
339 /* check card and initialize it */
340 int ips_adapter_init(ips_softc_t *sc)
343 DEVICE_PRINTF(1,sc->dev, "initializing\n");
345 if (bus_dma_tag_create( /* parent */ sc->adapter_dmatag,
348 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
349 /* highaddr */ BUS_SPACE_MAXADDR,
351 /* filterarg */ NULL,
352 /* maxsize */ IPS_COMMAND_LEN +
355 /* maxsegsize*/ IPS_COMMAND_LEN +
360 &sc->command_dmatag) != 0) {
361 device_printf(sc->dev, "can't alloc command dma tag\n");
364 if (bus_dma_tag_create( /* parent */ sc->adapter_dmatag,
367 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
368 /* highaddr */ BUS_SPACE_MAXADDR,
370 /* filterarg */ NULL,
371 /* maxsize */ IPS_MAX_IOBUF_SIZE,
372 /* numsegs */ IPS_MAX_SG_ELEMENTS,
373 /* maxsegsize*/ IPS_MAX_IOBUF_SIZE,
375 /* lockfunc */ busdma_lock_mutex,
376 /* lockarg */ &sc->queue_mtx,
377 &sc->sg_dmatag) != 0) {
378 device_printf(sc->dev, "can't alloc SG dma tag\n");
381 /* create one command buffer until we know how many commands this card
384 ips_cmdqueue_init(sc);
386 if(sc->ips_adapter_reinit(sc, 0))
389 /* initialize ffdc values */
390 microtime(&sc->ffdc_resettime);
391 sc->ffdc_resetcount = 1;
392 if ((i = ips_ffdc_reset(sc)) != 0) {
393 device_printf(sc->dev, "failed to send ffdc reset to device (%d)\n", i);
396 if ((i = ips_get_adapter_info(sc)) != 0) {
397 device_printf(sc->dev, "failed to get adapter configuration data from device (%d)\n", i);
400 ips_update_nvram(sc); /* no error check as failure doesn't matter */
401 if(sc->adapter_type > 0 && sc->adapter_type <= IPS_ADAPTER_MAX_T){
402 device_printf(sc->dev, "adapter type: %s\n", ips_adapter_name[sc->adapter_type]);
404 if ((i = ips_get_drive_info(sc)) != 0) {
405 device_printf(sc->dev, "failed to get drive configuration data from device (%d)\n", i);
409 ips_cmdqueue_free(sc);
410 if(sc->adapter_info.max_concurrent_cmds)
411 sc->max_cmds = min(128, sc->adapter_info.max_concurrent_cmds);
414 if(ips_cmdqueue_init(sc)){
415 device_printf(sc->dev, "failed to initialize command buffers\n");
418 sc->device_file = make_dev(&ips_cdevsw, device_get_unit(sc->dev), UID_ROOT, GID_OPERATOR,
419 S_IRUSR | S_IWUSR, "ips%d", device_get_unit(sc->dev));
420 sc->device_file->si_drv1 = sc;
421 ips_diskdev_init(sc);
422 callout_reset(&sc->timer, 10 * hz, ips_timeout, sc);
426 ips_adapter_free(sc);
430 /* see if we should reinitialize the card and wait for it to timeout or complete initialization */
431 int ips_morpheus_reinit(ips_softc_t *sc, int force)
436 tmp = ips_read_4(sc, MORPHEUS_REG_OISR);
437 if(!force && (ips_read_4(sc, MORPHEUS_REG_OMR0) >= IPS_POST1_OK) &&
438 (ips_read_4(sc, MORPHEUS_REG_OMR1) != 0xdeadbeef) && !tmp){
439 ips_write_4(sc, MORPHEUS_REG_OIMR, 0);
442 ips_write_4(sc, MORPHEUS_REG_OIMR, 0xff);
443 ips_read_4(sc, MORPHEUS_REG_OIMR);
445 device_printf(sc->dev, "resetting adapter, this may take up to 5 minutes\n");
446 ips_write_4(sc, MORPHEUS_REG_IDR, 0x80000000);
448 ips_read_4(sc, MORPHEUS_REG_OIMR);
450 tmp = ips_read_4(sc, MORPHEUS_REG_OISR);
451 for(i = 0; i < 45 && !(tmp & MORPHEUS_BIT_POST1); i++){
453 DEVICE_PRINTF(2, sc->dev, "post1: %d\n", i);
454 tmp = ips_read_4(sc, MORPHEUS_REG_OISR);
456 if(tmp & MORPHEUS_BIT_POST1)
457 ips_write_4(sc, MORPHEUS_REG_OISR, MORPHEUS_BIT_POST1);
459 if( i == 45 || ips_read_4(sc, MORPHEUS_REG_OMR0) < IPS_POST1_OK){
460 device_printf(sc->dev,"Adapter error during initialization.\n");
463 for(i = 0; i < 240 && !(tmp & MORPHEUS_BIT_POST2); i++){
465 DEVICE_PRINTF(2, sc->dev, "post2: %d\n", i);
466 tmp = ips_read_4(sc, MORPHEUS_REG_OISR);
468 if(tmp & MORPHEUS_BIT_POST2)
469 ips_write_4(sc, MORPHEUS_REG_OISR, MORPHEUS_BIT_POST2);
471 if(i == 240 || !ips_read_4(sc, MORPHEUS_REG_OMR1)){
472 device_printf(sc->dev, "adapter failed config check\n");
475 ips_write_4(sc, MORPHEUS_REG_OIMR, 0);
476 if(force && ips_clear_adapter(sc)){
477 device_printf(sc->dev, "adapter clear failed\n");
483 /* clean up so we can unload the driver. */
484 int ips_adapter_free(ips_softc_t *sc)
487 if(sc->state & IPS_DEV_OPEN)
489 if((error = ips_diskdev_free(sc)))
491 if(ips_cmdqueue_free(sc)){
492 device_printf(sc->dev,
493 "trying to exit when command queue is not empty!\n");
496 DEVICE_PRINTF(1, sc->dev, "free\n");
497 callout_drain(&sc->timer);
500 bus_dma_tag_destroy(sc->sg_dmatag);
501 if(sc->command_dmatag)
502 bus_dma_tag_destroy(sc->command_dmatag);
504 destroy_dev(sc->device_file);
508 static __inline int ips_morpheus_check_intr(ips_softc_t *sc)
511 ips_cmd_status_t status;
512 ips_command_t *command;
516 oisr = ips_read_4(sc, MORPHEUS_REG_OISR);
517 PRINTF(9, "interrupt registers out:%x\n", oisr);
518 if(!(oisr & MORPHEUS_BIT_CMD_IRQ)){
519 DEVICE_PRINTF(2,sc->dev, "got a non-command irq\n");
522 while((status.value = ips_read_4(sc, MORPHEUS_REG_OQPR)) != 0xffffffff){
523 cmdnumber = status.fields.command_id;
524 command = &sc->commandarray[cmdnumber];
525 command->status.value = status.value;
526 command->timeout = 0;
527 command->callback(command);
534 void ips_morpheus_intr(void *void_sc)
536 ips_softc_t *sc = void_sc;
538 mtx_lock(&sc->queue_mtx);
539 ips_morpheus_check_intr(sc);
540 mtx_unlock(&sc->queue_mtx);
543 void ips_morpheus_poll(ips_command_t *command)
548 * Locks are not used here because this is only called during
551 ts = time_second + command->timeout;
552 while ((command->timeout != 0)
553 && (ips_morpheus_check_intr(command->sc) == 0)
554 && (ts > time_second))
558 void ips_issue_morpheus_cmd(ips_command_t *command)
560 /* hmmm, is there a cleaner way to do this? */
561 if(command->sc->state & IPS_OFFLINE){
562 ips_set_error(command, EINVAL);
563 command->callback(command);
566 command->timeout = 10;
567 ips_write_4(command->sc, MORPHEUS_REG_IQPR, command->command_phys_addr);
570 static void ips_copperhead_queue_callback(void *queueptr, bus_dma_segment_t *segments,int segnum, int error)
572 ips_copper_queue_t *queue = queueptr;
576 queue->base_phys_addr = segments[0].ds_addr;
579 static int ips_copperhead_queue_init(ips_softc_t *sc)
582 bus_dma_tag_t dmatag;
584 if (bus_dma_tag_create( /* parent */ sc->adapter_dmatag,
587 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
588 /* highaddr */ BUS_SPACE_MAXADDR,
590 /* filterarg */ NULL,
591 /* maxsize */ sizeof(ips_copper_queue_t),
593 /* maxsegsize*/ sizeof(ips_copper_queue_t),
598 device_printf(sc->dev, "can't alloc dma tag for statue queue\n");
602 if(bus_dmamem_alloc(dmatag, (void *)&(sc->copper_queue),
603 BUS_DMA_NOWAIT, &dmamap)){
607 bzero(sc->copper_queue, sizeof(ips_copper_queue_t));
608 sc->copper_queue->dmatag = dmatag;
609 sc->copper_queue->dmamap = dmamap;
610 sc->copper_queue->nextstatus = 1;
611 bus_dmamap_load(dmatag, dmamap,
612 &(sc->copper_queue->status[0]), IPS_MAX_CMD_NUM * 4,
613 ips_copperhead_queue_callback, sc->copper_queue,
615 if(sc->copper_queue->base_phys_addr == 0){
619 ips_write_4(sc, COPPER_REG_SQSR, sc->copper_queue->base_phys_addr);
620 ips_write_4(sc, COPPER_REG_SQER, sc->copper_queue->base_phys_addr +
621 IPS_MAX_CMD_NUM * 4);
622 ips_write_4(sc, COPPER_REG_SQHR, sc->copper_queue->base_phys_addr + 4);
623 ips_write_4(sc, COPPER_REG_SQTR, sc->copper_queue->base_phys_addr);
628 if (sc->copper_queue != NULL)
629 bus_dmamem_free(dmatag, sc->copper_queue, dmamap);
630 bus_dma_tag_destroy(dmatag);
634 /* see if we should reinitialize the card and wait for it to timeout or complete initialization FIXME */
635 int ips_copperhead_reinit(ips_softc_t *sc, int force)
638 u_int32_t postcode = 0, configstatus = 0;
639 ips_write_1(sc, COPPER_REG_SCPR, 0x80);
640 ips_write_1(sc, COPPER_REG_SCPR, 0);
641 device_printf(sc->dev, "reinitializing adapter, this could take several minutes.\n");
642 for(j = 0; j < 2; j++){
644 for(i = 0; i < 45; i++){
645 if(ips_read_1(sc, COPPER_REG_HISR) & COPPER_GHI_BIT){
646 postcode |= ips_read_1(sc, COPPER_REG_ISPR);
647 ips_write_1(sc, COPPER_REG_HISR,
656 for(j = 0; j < 2; j++){
658 for(i = 0; i < 240; i++){
659 if(ips_read_1(sc, COPPER_REG_HISR) & COPPER_GHI_BIT){
660 configstatus |= ips_read_1(sc, COPPER_REG_ISPR);
661 ips_write_1(sc, COPPER_REG_HISR,
670 for(i = 0; i < 240; i++){
671 if(!(ips_read_1(sc, COPPER_REG_CBSP) & COPPER_OP_BIT)){
678 ips_write_2(sc, COPPER_REG_CCCR, 0x1000 | COPPER_ILE_BIT);
679 ips_write_1(sc, COPPER_REG_SCPR, COPPER_EBM_BIT);
680 ips_copperhead_queue_init(sc);
681 ips_write_1(sc, COPPER_REG_HISR, COPPER_GHI_BIT);
682 i = ips_read_1(sc, COPPER_REG_SCPR);
683 ips_write_1(sc, COPPER_REG_HISR, COPPER_EI_BIT);
685 device_printf(sc->dev, "adapter initialization failed\n");
688 if(force && ips_clear_adapter(sc)){
689 device_printf(sc->dev, "adapter clear failed\n");
694 static u_int32_t ips_copperhead_cmd_status(ips_softc_t *sc)
697 int statnum = sc->copper_queue->nextstatus++;
698 if(sc->copper_queue->nextstatus == IPS_MAX_CMD_NUM)
699 sc->copper_queue->nextstatus = 0;
700 value = sc->copper_queue->status[statnum];
701 ips_write_4(sc, COPPER_REG_SQTR, sc->copper_queue->base_phys_addr +
707 void ips_copperhead_intr(void *void_sc)
709 ips_softc_t *sc = (ips_softc_t *)void_sc;
711 ips_cmd_status_t status;
713 mtx_lock(&sc->queue_mtx);
714 while(ips_read_1(sc, COPPER_REG_HISR) & COPPER_SCE_BIT){
715 status.value = ips_copperhead_cmd_status(sc);
716 cmdnumber = status.fields.command_id;
717 sc->commandarray[cmdnumber].status.value = status.value;
718 sc->commandarray[cmdnumber].timeout = 0;
719 sc->commandarray[cmdnumber].callback(&(sc->commandarray[cmdnumber]));
720 PRINTF(9, "ips: got command %d\n", cmdnumber);
722 mtx_unlock(&sc->queue_mtx);
726 void ips_issue_copperhead_cmd(ips_command_t *command)
729 /* hmmm, is there a cleaner way to do this? */
730 if(command->sc->state & IPS_OFFLINE){
731 ips_set_error(command, EINVAL);
732 command->callback(command);
735 command->timeout = 10;
736 for(i = 0; ips_read_4(command->sc, COPPER_REG_CCCR) & COPPER_SEM_BIT;
739 printf("sem bit still set, can't send a command\n");
742 DELAY(500);/* need to do a delay here */
744 ips_write_4(command->sc, COPPER_REG_CCSAR, command->command_phys_addr);
745 ips_write_2(command->sc, COPPER_REG_CCCR, COPPER_CMD_START);
748 void ips_copperhead_poll(ips_command_t *command)
751 printf("ips: cmd polling not implemented for copperhead devices\n");