xref: /freebsd/sys/dev/ips/ips.c (revision da5432eda807c4b7232d030d5157d5b417ea4f52)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Written by: David Jeffery
5  * Copyright (c) 2002 Adaptec Inc.
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 #include <sys/types.h>
32 #include <sys/lock.h>
33 #include <sys/mutex.h>
34 #include <sys/stat.h>
35 #include <sys/time.h>
36 
37 #include <dev/ips/ipsreg.h>
38 #include <dev/ips/ips.h>
39 
40 static d_open_t ips_open;
41 static d_close_t ips_close;
42 static d_ioctl_t ips_ioctl;
43 
44 MALLOC_DEFINE(M_IPSBUF, "ipsbuf","IPS driver buffer");
45 
46 static struct cdevsw ips_cdevsw = {
47 	.d_version =	D_VERSION,
48 	.d_open =	ips_open,
49 	.d_close =	ips_close,
50 	.d_ioctl =	ips_ioctl,
51 	.d_name =	"ips",
52 };
53 
54 static const char* ips_adapter_name[] = {
55 	"N/A",
56 	"ServeRAID (copperhead)",
57 	"ServeRAID II (copperhead refresh)",
58 	"ServeRAID onboard (copperhead)",
59 	"ServeRAID onboard (copperhead)",
60 	"ServeRAID 3H (clarinet)",
61 	"ServeRAID 3L (clarinet lite)",
62 	"ServeRAID 4H (trombone)",
63 	"ServeRAID 4M (morpheus)",
64 	"ServeRAID 4L (morpheus lite)",
65 	"ServeRAID 4Mx (neo)",
66 	"ServeRAID 4Lx (neo lite)",
67 	"ServeRAID 5i II (sarasota)",
68 	"ServeRAID 5i (sarasota)",
69 	"ServeRAID 6M (marco)",
70 	"ServeRAID 6i (sebring)",
71 	"ServeRAID 7t",
72 	"ServeRAID 7k",
73 	"ServeRAID 7M"
74 };
75 
76 
77 static int ips_open(struct cdev *dev, int flags, int fmt, struct thread *td)
78 {
79 	ips_softc_t *sc = dev->si_drv1;
80 	mtx_lock(&sc->queue_mtx);
81 	sc->state |= IPS_DEV_OPEN;
82 	mtx_unlock(&sc->queue_mtx);
83         return 0;
84 }
85 
86 static int ips_close(struct cdev *dev, int flags, int fmt, struct thread *td)
87 {
88 	ips_softc_t *sc = dev->si_drv1;
89 
90 	mtx_lock(&sc->queue_mtx);
91 	sc->state &= ~IPS_DEV_OPEN;
92 	mtx_unlock(&sc->queue_mtx);
93 
94         return 0;
95 }
96 
97 static int ips_ioctl(struct cdev *dev, u_long command, caddr_t addr, int32_t flags, struct thread *td)
98 {
99 	ips_softc_t *sc;
100 
101 	sc = dev->si_drv1;
102 	return ips_ioctl_request(sc, command, addr, flags);
103 }
104 
105 static void ips_cmd_dmaload(void *cmdptr, bus_dma_segment_t *segments,int segnum, int error)
106 {
107 	ips_command_t *command = cmdptr;
108 	PRINTF(10, "ips: in ips_cmd_dmaload\n");
109 	if(!error)
110 		command->command_phys_addr = segments[0].ds_addr;
111 
112 }
113 
114 /* is locking needed? what locking guarantees are there on removal? */
115 static int ips_cmdqueue_free(ips_softc_t *sc)
116 {
117 	int i, error = -1;
118 	ips_command_t *command;
119 
120 	if(!sc->used_commands){
121 		for(i = 0; i < sc->max_cmds; i++){
122 
123 			command = &sc->commandarray[i];
124 
125 			if(command->command_phys_addr == 0)
126 				continue;
127 			bus_dmamap_unload(sc->command_dmatag,
128 					  command->command_dmamap);
129 			bus_dmamem_free(sc->command_dmatag,
130 					command->command_buffer,
131 					command->command_dmamap);
132 			if (command->data_dmamap != NULL)
133 				bus_dmamap_destroy(command->data_dmatag,
134 				    command->data_dmamap);
135 		}
136 		error = 0;
137 		sc->state |= IPS_OFFLINE;
138 	}
139 	sc->staticcmd = NULL;
140 	free(sc->commandarray, M_DEVBUF);
141 	return error;
142 }
143 
144 /* places all ips command structs on the free command queue.  No locking as if someone else tries
145  * to access this during init, we have bigger problems */
146 static int ips_cmdqueue_init(ips_softc_t *sc)
147 {
148 	int i;
149 	ips_command_t *command;
150 
151 	sc->commandarray = (ips_command_t *)malloc(sizeof(ips_command_t) *
152 	    sc->max_cmds, M_DEVBUF, M_NOWAIT|M_ZERO);
153 	if (sc->commandarray == NULL)
154 		return (ENOMEM);
155 
156 	SLIST_INIT(&sc->free_cmd_list);
157 	for(i = 0; i < sc->max_cmds; i++){
158 		command = &sc->commandarray[i];
159 		command->id = i;
160 		command->sc = sc;
161 
162 		if(bus_dmamem_alloc(sc->command_dmatag,&command->command_buffer,
163 		    BUS_DMA_NOWAIT, &command->command_dmamap))
164 			goto error;
165 		bus_dmamap_load(sc->command_dmatag, command->command_dmamap,
166 				command->command_buffer,IPS_COMMAND_LEN,
167 				ips_cmd_dmaload, command, BUS_DMA_NOWAIT);
168 		if(!command->command_phys_addr){
169 			bus_dmamem_free(sc->command_dmatag,
170 			    command->command_buffer, command->command_dmamap);
171 			goto error;
172 		}
173 
174 		if (i != 0) {
175 			command->data_dmatag = sc->sg_dmatag;
176 			if (bus_dmamap_create(command->data_dmatag, 0,
177 			    &command->data_dmamap))
178 				goto error;
179 			SLIST_INSERT_HEAD(&sc->free_cmd_list, command, next);
180 		} else
181 			sc->staticcmd = command;
182 	}
183 	sc->state &= ~IPS_OFFLINE;
184 	return 0;
185 error:
186 	ips_cmdqueue_free(sc);
187 	return ENOMEM;
188 }
189 
190 /* returns a free command struct if one is available.
191  * It also blanks out anything that may be a wild pointer/value.
192  * Also, command buffers are not freed.  They are
193  * small so they are saved and kept dmamapped and loaded.
194  */
195 int ips_get_free_cmd(ips_softc_t *sc, ips_command_t **cmd, unsigned long flags)
196 {
197 	ips_command_t *command;
198 
199 	if(sc->state & IPS_OFFLINE){
200 		return EIO;
201 	}
202 	if ((flags & IPS_STATIC_FLAG) == 0) {
203 		command = SLIST_FIRST(&sc->free_cmd_list);
204 		if(!command || (sc->state & IPS_TIMEOUT)){
205 			return EBUSY;
206 		}
207 		SLIST_REMOVE_HEAD(&sc->free_cmd_list, next);
208 		(sc->used_commands)++;
209 	} else {
210 		if (sc->state & IPS_STATIC_BUSY)
211 			return EAGAIN;
212 		command = sc->staticcmd;
213 		sc->state |= IPS_STATIC_BUSY;
214 	}
215 	clear_ips_command(command);
216 	bzero(command->command_buffer, IPS_COMMAND_LEN);
217 	*cmd = command;
218 	return 0;
219 }
220 
221 /* adds a command back to the free command queue */
222 void ips_insert_free_cmd(ips_softc_t *sc, ips_command_t *command)
223 {
224 
225 	if (sema_value(&sc->cmd_sema) != 0)
226 		panic("ips: command returned non-zero semaphore");
227 
228 	if (command != sc->staticcmd) {
229 		SLIST_INSERT_HEAD(&sc->free_cmd_list, command, next);
230 		(sc->used_commands)--;
231 	} else {
232 		sc->state &= ~IPS_STATIC_BUSY;
233 	}
234 }
235 static const char* ips_diskdev_statename(u_int8_t state)
236 {
237 	static char statebuf[20];
238 	switch(state){
239 		case IPS_LD_OFFLINE:
240 			return("OFFLINE");
241 			break;
242 		case IPS_LD_OKAY:
243 			return("OK");
244 			break;
245 		case IPS_LD_DEGRADED:
246 			return("DEGRADED");
247 			break;
248 		case IPS_LD_FREE:
249 			return("FREE");
250 			break;
251 		case IPS_LD_SYS:
252 			return("SYS");
253 			break;
254 		case IPS_LD_CRS:
255 			return("CRS");
256 			break;
257 	}
258 	sprintf(statebuf,"UNKNOWN(0x%02x)", state);
259 	return(statebuf);
260 }
261 
262 static int ips_diskdev_init(ips_softc_t *sc)
263 {
264 	int i;
265 	for(i=0; i < IPS_MAX_NUM_DRIVES; i++){
266 		if(sc->drives[i].state == IPS_LD_FREE) continue;
267 		device_printf(sc->dev, "Logical Drive %d: RAID%d sectors: %u, state %s\n",
268 			i, sc->drives[i].raid_lvl,
269 			sc->drives[i].sector_count,
270 			ips_diskdev_statename(sc->drives[i].state));
271 		if(sc->drives[i].state == IPS_LD_OKAY ||
272 		   sc->drives[i].state == IPS_LD_DEGRADED){
273 			sc->diskdev[i] = device_add_child(sc->dev, NULL, -1);
274 			device_set_ivars(sc->diskdev[i],(void *)(uintptr_t) i);
275 		}
276 	}
277 	if(bus_generic_attach(sc->dev)){
278 		device_printf(sc->dev, "Attaching bus failed\n");
279 	}
280 	return 0;
281 }
282 
283 static int ips_diskdev_free(ips_softc_t *sc)
284 {
285 	int i;
286 	int error = 0;
287 	for(i = 0; i < IPS_MAX_NUM_DRIVES; i++){
288 		if(sc->diskdev[i]) {
289 			error = device_delete_child(sc->dev, sc->diskdev[i]);
290 			if(error)
291 				return error;
292 		}
293 	}
294 	bus_generic_detach(sc->dev);
295 	return 0;
296 }
297 
298 /* ips_timeout is periodically called to make sure no commands sent
299  * to the card have become stuck.  If it finds a stuck command, it
300  * sets a flag so the driver won't start any more commands and then
301  * is periodically called to see if all outstanding commands have
302  * either finished or timed out.  Once timed out, an attempt to
303  * reinitialize the card is made.  If that fails, the driver gives
304  * up and declares the card dead. */
305 static void ips_timeout(void *arg)
306 {
307 	ips_softc_t *sc = arg;
308 	int i, state = 0;
309 	ips_command_t *command;
310 
311 	mtx_assert(&sc->queue_mtx, MA_OWNED);
312 	command = &sc->commandarray[0];
313 	for(i = 0; i < sc->max_cmds; i++){
314 		if(!command[i].timeout){
315 			continue;
316 		}
317 		command[i].timeout--;
318 		if(!command[i].timeout){
319 			if(!(sc->state & IPS_TIMEOUT)){
320 				sc->state |= IPS_TIMEOUT;
321 				device_printf(sc->dev, "WARNING: command timeout. Adapter is in toaster mode, resetting to known state\n");
322 			}
323 			ips_set_error(&command[i], ETIMEDOUT);
324 			command[i].callback(&command[i]);
325 			/* hmm, this should be enough cleanup */
326 		} else
327 			state = 1;
328 	}
329 	if(!state && (sc->state & IPS_TIMEOUT)){
330 		if(sc->ips_adapter_reinit(sc, 1)){
331 			device_printf(sc->dev, "AIEE! adapter reset failed, giving up and going home! Have a nice day.\n");
332 			sc->state |= IPS_OFFLINE;
333 			sc->state &= ~IPS_TIMEOUT;
334 			/* Grr, I hate this solution. I run waiting commands
335 			   one at a time and error them out just before they
336 			   would go to the card. This sucks. */
337 		} else
338 			sc->state &= ~IPS_TIMEOUT;
339 	}
340 	if (sc->state != IPS_OFFLINE)
341 		callout_reset(&sc->timer, 10 * hz, ips_timeout, sc);
342 }
343 
344 /* check card and initialize it */
345 int ips_adapter_init(ips_softc_t *sc)
346 {
347         int i;
348         DEVICE_PRINTF(1,sc->dev, "initializing\n");
349 
350         if (bus_dma_tag_create(	/* parent    */	sc->adapter_dmatag,
351 				/* alignemnt */	1,
352 				/* boundary  */	0,
353 				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
354 				/* highaddr  */	BUS_SPACE_MAXADDR,
355 				/* filter    */	NULL,
356 				/* filterarg */	NULL,
357 				/* maxsize   */	IPS_COMMAND_LEN +
358 						    IPS_MAX_SG_LEN,
359 				/* numsegs   */	1,
360 				/* maxsegsize*/	IPS_COMMAND_LEN +
361 						    IPS_MAX_SG_LEN,
362 				/* flags     */	0,
363 				/* lockfunc  */ NULL,
364 				/* lockarg   */ NULL,
365 				&sc->command_dmatag) != 0) {
366                 device_printf(sc->dev, "can't alloc command dma tag\n");
367 		goto error;
368         }
369 	if (bus_dma_tag_create(	/* parent    */	sc->adapter_dmatag,
370 				/* alignemnt */	1,
371 				/* boundary  */	0,
372 				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
373 				/* highaddr  */	BUS_SPACE_MAXADDR,
374 				/* filter    */	NULL,
375 				/* filterarg */	NULL,
376 				/* maxsize   */	IPS_MAX_IOBUF_SIZE,
377 				/* numsegs   */	IPS_MAX_SG_ELEMENTS,
378 				/* maxsegsize*/	IPS_MAX_IOBUF_SIZE,
379 				/* flags     */	0,
380 				/* lockfunc  */ busdma_lock_mutex,
381 				/* lockarg   */ &sc->queue_mtx,
382 				&sc->sg_dmatag) != 0) {
383 		device_printf(sc->dev, "can't alloc SG dma tag\n");
384 		goto error;
385 	}
386 	/* create one command buffer until we know how many commands this card
387            can handle */
388 	sc->max_cmds = 1;
389 	ips_cmdqueue_init(sc);
390 
391 	if(sc->ips_adapter_reinit(sc, 0))
392 		goto error;
393 
394 	/* initialize ffdc values */
395 	microtime(&sc->ffdc_resettime);
396 	sc->ffdc_resetcount = 1;
397 	if ((i = ips_ffdc_reset(sc)) != 0) {
398 		device_printf(sc->dev, "failed to send ffdc reset to device (%d)\n", i);
399 		goto error;
400 	}
401 	if ((i = ips_get_adapter_info(sc)) != 0) {
402 		device_printf(sc->dev, "failed to get adapter configuration data from device (%d)\n", i);
403 		goto error;
404 	}
405 	ips_update_nvram(sc); /* no error check as failure doesn't matter */
406 	if(sc->adapter_type > 0 && sc->adapter_type <= IPS_ADAPTER_MAX_T){
407 		device_printf(sc->dev, "adapter type: %s\n", ips_adapter_name[sc->adapter_type]);
408 	}
409  	if ((i = ips_get_drive_info(sc)) != 0) {
410 		device_printf(sc->dev, "failed to get drive configuration data from device (%d)\n", i);
411 		goto error;
412 	}
413 
414         ips_cmdqueue_free(sc);
415 	if(sc->adapter_info.max_concurrent_cmds)
416         	sc->max_cmds = min(128, sc->adapter_info.max_concurrent_cmds);
417 	else
418 		sc->max_cmds = 32;
419         if(ips_cmdqueue_init(sc)){
420 		device_printf(sc->dev, "failed to initialize command buffers\n");
421 		goto error;
422 	}
423         sc->device_file = make_dev(&ips_cdevsw, device_get_unit(sc->dev), UID_ROOT, GID_OPERATOR,
424                                         S_IRUSR | S_IWUSR, "ips%d", device_get_unit(sc->dev));
425 	sc->device_file->si_drv1 = sc;
426 	ips_diskdev_init(sc);
427 	callout_reset(&sc->timer, 10 * hz, ips_timeout, sc);
428         return 0;
429 
430 error:
431 	ips_adapter_free(sc);
432 	return ENXIO;
433 }
434 
435 /* see if we should reinitialize the card and wait for it to timeout or complete initialization */
436 int ips_morpheus_reinit(ips_softc_t *sc, int force)
437 {
438         u_int32_t tmp;
439 	int i;
440 
441 	tmp = ips_read_4(sc, MORPHEUS_REG_OISR);
442 	if(!force && (ips_read_4(sc, MORPHEUS_REG_OMR0) >= IPS_POST1_OK) &&
443 	    (ips_read_4(sc, MORPHEUS_REG_OMR1) != 0xdeadbeef) && !tmp){
444 		ips_write_4(sc, MORPHEUS_REG_OIMR, 0);
445 		return 0;
446 	}
447 	ips_write_4(sc, MORPHEUS_REG_OIMR, 0xff);
448 	ips_read_4(sc, MORPHEUS_REG_OIMR);
449 
450 	device_printf(sc->dev, "resetting adapter, this may take up to 5 minutes\n");
451 	ips_write_4(sc, MORPHEUS_REG_IDR, 0x80000000);
452 	DELAY(5000000);
453 	ips_read_4(sc, MORPHEUS_REG_OIMR);
454 
455 	tmp = ips_read_4(sc, MORPHEUS_REG_OISR);
456 	for(i = 0; i < 45 && !(tmp & MORPHEUS_BIT_POST1); i++){
457 		DELAY(1000000);
458 		DEVICE_PRINTF(2, sc->dev, "post1: %d\n", i);
459 		tmp = ips_read_4(sc, MORPHEUS_REG_OISR);
460 	}
461 	if(tmp & MORPHEUS_BIT_POST1)
462 		ips_write_4(sc, MORPHEUS_REG_OISR, MORPHEUS_BIT_POST1);
463 
464         if( i == 45 || ips_read_4(sc, MORPHEUS_REG_OMR0) < IPS_POST1_OK){
465                 device_printf(sc->dev,"Adapter error during initialization.\n");
466 		return 1;
467         }
468 	for(i = 0; i < 240 && !(tmp & MORPHEUS_BIT_POST2); i++){
469 		DELAY(1000000);
470 		DEVICE_PRINTF(2, sc->dev, "post2: %d\n", i);
471 		tmp = ips_read_4(sc, MORPHEUS_REG_OISR);
472 	}
473 	if(tmp & MORPHEUS_BIT_POST2)
474 		ips_write_4(sc, MORPHEUS_REG_OISR, MORPHEUS_BIT_POST2);
475 
476 	if(i == 240 || !ips_read_4(sc, MORPHEUS_REG_OMR1)){
477 		device_printf(sc->dev, "adapter failed config check\n");
478 		return 1;
479         }
480 	ips_write_4(sc, MORPHEUS_REG_OIMR, 0);
481 	if(force && ips_clear_adapter(sc)){
482 		device_printf(sc->dev, "adapter clear failed\n");
483 		return 1;
484 	}
485 	return 0;
486 }
487 
488 /* clean up so we can unload the driver. */
489 int ips_adapter_free(ips_softc_t *sc)
490 {
491 	int error = 0;
492 	if(sc->state & IPS_DEV_OPEN)
493 		return EBUSY;
494 	if((error = ips_diskdev_free(sc)))
495 		return error;
496 	if(ips_cmdqueue_free(sc)){
497 		device_printf(sc->dev,
498 		     "trying to exit when command queue is not empty!\n");
499 		return EBUSY;
500 	}
501 	DEVICE_PRINTF(1, sc->dev, "free\n");
502 	callout_drain(&sc->timer);
503 
504 	if(sc->sg_dmatag)
505 		bus_dma_tag_destroy(sc->sg_dmatag);
506 	if(sc->command_dmatag)
507 		bus_dma_tag_destroy(sc->command_dmatag);
508 	if(sc->device_file)
509 	        destroy_dev(sc->device_file);
510         return 0;
511 }
512 
513 static __inline int ips_morpheus_check_intr(ips_softc_t *sc)
514 {
515 	int cmdnumber;
516 	ips_cmd_status_t status;
517 	ips_command_t *command;
518 	int found = 0;
519 	u_int32_t oisr;
520 
521 	oisr = ips_read_4(sc, MORPHEUS_REG_OISR);
522 	PRINTF(9, "interrupt registers out:%x\n", oisr);
523 	if(!(oisr & MORPHEUS_BIT_CMD_IRQ)){
524 		DEVICE_PRINTF(2,sc->dev, "got a non-command irq\n");
525 		return (0);
526 	}
527 	while((status.value = ips_read_4(sc, MORPHEUS_REG_OQPR)) != 0xffffffff){
528 		cmdnumber = status.fields.command_id;
529 		command = &sc->commandarray[cmdnumber];
530 		command->status.value = status.value;
531 		command->timeout = 0;
532 		command->callback(command);
533 
534 		found = 1;
535 	}
536         return (found);
537 }
538 
539 void ips_morpheus_intr(void *void_sc)
540 {
541 	ips_softc_t *sc = void_sc;
542 
543 	mtx_lock(&sc->queue_mtx);
544 	ips_morpheus_check_intr(sc);
545 	mtx_unlock(&sc->queue_mtx);
546 }
547 
548 void ips_morpheus_poll(ips_command_t *command)
549 {
550 	uint32_t ts;
551 
552 	/*
553 	 * Locks are not used here because this is only called during
554 	 * crashdumps.
555 	 */
556 	ts = time_second + command->timeout;
557 	while ((command->timeout != 0)
558 	 && (ips_morpheus_check_intr(command->sc) == 0)
559 	 && (ts > time_second))
560 		DELAY(1000);
561 }
562 
563 void ips_issue_morpheus_cmd(ips_command_t *command)
564 {
565 	/* hmmm, is there a cleaner way to do this? */
566 	if(command->sc->state & IPS_OFFLINE){
567 		ips_set_error(command, EINVAL);
568 		command->callback(command);
569 		return;
570 	}
571 	command->timeout = 10;
572 	ips_write_4(command->sc, MORPHEUS_REG_IQPR, command->command_phys_addr);
573 }
574 
575 static void ips_copperhead_queue_callback(void *queueptr, bus_dma_segment_t *segments,int segnum, int error)
576 {
577 	ips_copper_queue_t *queue = queueptr;
578 	if(error){
579 		return;
580 	}
581 	queue->base_phys_addr = segments[0].ds_addr;
582 }
583 
584 static int ips_copperhead_queue_init(ips_softc_t *sc)
585 {
586 	int error;
587 	bus_dma_tag_t dmatag;
588 	bus_dmamap_t dmamap;
589        	if (bus_dma_tag_create(	/* parent    */	sc->adapter_dmatag,
590 				/* alignemnt */	1,
591 				/* boundary  */	0,
592 				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
593 				/* highaddr  */	BUS_SPACE_MAXADDR,
594 				/* filter    */	NULL,
595 				/* filterarg */	NULL,
596 				/* maxsize   */	sizeof(ips_copper_queue_t),
597 				/* numsegs   */	1,
598 				/* maxsegsize*/	sizeof(ips_copper_queue_t),
599 				/* flags     */	0,
600 				/* lockfunc  */ NULL,
601 				/* lockarg   */ NULL,
602 				&dmatag) != 0) {
603                 device_printf(sc->dev, "can't alloc dma tag for statue queue\n");
604 		error = ENOMEM;
605 		return error;
606         }
607 	if(bus_dmamem_alloc(dmatag, (void *)&(sc->copper_queue),
608 	   		    BUS_DMA_NOWAIT, &dmamap)){
609 		error = ENOMEM;
610 		goto exit;
611 	}
612 	bzero(sc->copper_queue, sizeof(ips_copper_queue_t));
613 	sc->copper_queue->dmatag = dmatag;
614 	sc->copper_queue->dmamap = dmamap;
615 	sc->copper_queue->nextstatus = 1;
616 	bus_dmamap_load(dmatag, dmamap,
617 			&(sc->copper_queue->status[0]), IPS_MAX_CMD_NUM * 4,
618 			ips_copperhead_queue_callback, sc->copper_queue,
619 			BUS_DMA_NOWAIT);
620 	if(sc->copper_queue->base_phys_addr == 0){
621 		error = ENOMEM;
622 		goto exit;
623 	}
624 	ips_write_4(sc, COPPER_REG_SQSR, sc->copper_queue->base_phys_addr);
625 	ips_write_4(sc, COPPER_REG_SQER, sc->copper_queue->base_phys_addr +
626 		    IPS_MAX_CMD_NUM * 4);
627 	ips_write_4(sc, COPPER_REG_SQHR, sc->copper_queue->base_phys_addr + 4);
628 	ips_write_4(sc, COPPER_REG_SQTR, sc->copper_queue->base_phys_addr);
629 
630 
631 	return 0;
632 exit:
633 	if (sc->copper_queue != NULL)
634 		bus_dmamem_free(dmatag, sc->copper_queue, dmamap);
635 	bus_dma_tag_destroy(dmatag);
636 	return error;
637 }
638 
639 /* see if we should reinitialize the card and wait for it to timeout or complete initialization FIXME */
640 int ips_copperhead_reinit(ips_softc_t *sc, int force)
641 {
642 	int i, j;
643 	u_int32_t configstatus = 0;
644 	ips_write_1(sc, COPPER_REG_SCPR, 0x80);
645 	ips_write_1(sc, COPPER_REG_SCPR, 0);
646 	device_printf(sc->dev, "reinitializing adapter, this could take several minutes.\n");
647 	for(j = 0; j < 2; j++){
648 		for(i = 0; i < 45; i++){
649 			if(ips_read_1(sc, COPPER_REG_HISR) & COPPER_GHI_BIT){
650 				ips_write_1(sc, COPPER_REG_HISR,
651 					    COPPER_GHI_BIT);
652 				break;
653 			} else
654 				DELAY(1000000);
655 		}
656 		if(i == 45)
657 			return 1;
658 	}
659 	for(j = 0; j < 2; j++){
660 		configstatus <<= 8;
661 		for(i = 0; i < 240; i++){
662 			if(ips_read_1(sc, COPPER_REG_HISR) & COPPER_GHI_BIT){
663 				configstatus |= ips_read_1(sc, COPPER_REG_ISPR);
664 				ips_write_1(sc, COPPER_REG_HISR,
665 					    COPPER_GHI_BIT);
666 				break;
667 			} else
668 				DELAY(1000000);
669 		}
670 		if(i == 240)
671 			return 1;
672 	}
673 	for(i = 0; i < 240; i++){
674 		if(!(ips_read_1(sc, COPPER_REG_CBSP) & COPPER_OP_BIT)){
675 			break;
676 		} else
677 			DELAY(1000000);
678 	}
679 	if(i == 240)
680 		return 1;
681 	ips_write_2(sc, COPPER_REG_CCCR, 0x1000 | COPPER_ILE_BIT);
682 	ips_write_1(sc, COPPER_REG_SCPR, COPPER_EBM_BIT);
683 	ips_copperhead_queue_init(sc);
684 	ips_write_1(sc, COPPER_REG_HISR, COPPER_GHI_BIT);
685 	i = ips_read_1(sc, COPPER_REG_SCPR);
686 	ips_write_1(sc, COPPER_REG_HISR, COPPER_EI_BIT);
687 	if(!configstatus){
688 		device_printf(sc->dev, "adapter initialization failed\n");
689 		return 1;
690 	}
691 	if(force && ips_clear_adapter(sc)){
692 		device_printf(sc->dev, "adapter clear failed\n");
693 		return 1;
694 	}
695 	return 0;
696 }
697 static u_int32_t ips_copperhead_cmd_status(ips_softc_t *sc)
698 {
699 	u_int32_t value;
700 	int statnum = sc->copper_queue->nextstatus++;
701 	if(sc->copper_queue->nextstatus == IPS_MAX_CMD_NUM)
702 		sc->copper_queue->nextstatus = 0;
703 	value = sc->copper_queue->status[statnum];
704 	ips_write_4(sc, COPPER_REG_SQTR, sc->copper_queue->base_phys_addr +
705 		    4 * statnum);
706 	return value;
707 }
708 
709 
710 void ips_copperhead_intr(void *void_sc)
711 {
712         ips_softc_t *sc = (ips_softc_t *)void_sc;
713 	int cmdnumber;
714 	ips_cmd_status_t status;
715 
716 	mtx_lock(&sc->queue_mtx);
717 	while(ips_read_1(sc, COPPER_REG_HISR) & COPPER_SCE_BIT){
718 		status.value = ips_copperhead_cmd_status(sc);
719 		cmdnumber = status.fields.command_id;
720 		sc->commandarray[cmdnumber].status.value = status.value;
721 		sc->commandarray[cmdnumber].timeout = 0;
722 		sc->commandarray[cmdnumber].callback(&(sc->commandarray[cmdnumber]));
723 		PRINTF(9, "ips: got command %d\n", cmdnumber);
724 	}
725 	mtx_unlock(&sc->queue_mtx);
726         return;
727 }
728 
729 void ips_issue_copperhead_cmd(ips_command_t *command)
730 {
731 	int i;
732 	/* hmmm, is there a cleaner way to do this? */
733 	if(command->sc->state & IPS_OFFLINE){
734 		ips_set_error(command, EINVAL);
735 		command->callback(command);
736 		return;
737 	}
738 	command->timeout = 10;
739 	for(i = 0; ips_read_4(command->sc, COPPER_REG_CCCR) & COPPER_SEM_BIT;
740 	    i++ ){
741 		if( i == 20){
742 printf("sem bit still set, can't send a command\n");
743 			return;
744 		}
745 		DELAY(500);/* need to do a delay here */
746 	}
747 	ips_write_4(command->sc, COPPER_REG_CCSAR, command->command_phys_addr);
748 	ips_write_2(command->sc, COPPER_REG_CCCR, COPPER_CMD_START);
749 }
750 
751 void ips_copperhead_poll(ips_command_t *command)
752 {
753 
754 	printf("ips: cmd polling not implemented for copperhead devices\n");
755 }
756