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