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