xref: /dragonfly/sys/dev/raid/ips/ips_commands.c (revision 4e7eb5cc)
1 /*-
2  * Written by: David Jeffery
3  * Copyright (c) 2002 Adaptec Inc.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
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.
14  *
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
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/sys/dev/ips/ips_commands.c,v 1.8 2004/01/01 10:22:10 mbr
28  * $DragonFly: src/sys/dev/raid/ips/ips_commands.c,v 1.1 2004/01/15 15:41:23 drhodus Exp $
29  */
30 
31 #include <sys/cdefs.h>
32 
33 #include <dev/raid/ips/ips.h>
34 
35 static int
36 ips_msleep(void *ident, struct ips_softc *sc, int priority, const char *wmesg,
37 	   int timo)
38 {
39 	int	r;
40 
41 	IPS_UNLOCK(sc);
42 	r = tsleep(ident, priority, wmesg, timo);
43 	IPS_LOCK(sc);
44 	return r;
45 }
46 
47 /*
48  * This is an interrupt callback.  It is called from
49  * interrupt context when the adapter has completed the
50  * command.  This very generic callback simply stores
51  * the command's return value in command->arg and wake's
52  * up anyone waiting on the command.
53  */
54 static void
55 ips_wakeup_callback(ips_command_t *command)
56 {
57 	ips_cmd_status_t *status;
58 
59 	status = command->arg;
60 	status->value = command->status.value;
61 	bus_dmamap_sync(command->sc->command_dmatag, command->command_dmamap,
62 			BUS_DMASYNC_POSTWRITE);
63 	IPS_LOCK(command->sc);
64 	wakeup(status);
65 	IPS_UNLOCK(command->sc);
66 }
67 /* Below are a series of functions for sending an IO request
68  * to the adapter.  The flow order is: start, send, callback, finish.
69  * The caller must have already assembled an iorequest struct to hold
70  * the details of the IO request. */
71 static void ips_io_request_finish(ips_command_t *command)
72 {
73 	struct bio *iobuf = command->arg;
74 
75 	if (ips_read_request(iobuf)) {
76 		bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
77 				BUS_DMASYNC_POSTREAD);
78 	} else {
79 		bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
80 				BUS_DMASYNC_POSTWRITE);
81 	}
82 	bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
83 	bus_dmamap_destroy(command->data_dmatag, command->data_dmamap);
84 	if (COMMAND_ERROR(&command->status)) {
85 		iobuf->bio_flags |=BIO_ERROR;
86 		iobuf->bio_error = EIO;
87 	}
88 	ips_insert_free_cmd(command->sc, command);
89 	ipsd_finish(iobuf);
90 }
91 
92 static void
93 ips_io_request_callback(void *cmdptr, bus_dma_segment_t *segments, int segnum,
94 			int error)
95 {
96 	ips_softc_t *sc;
97 	ips_command_t *command = cmdptr;
98 	ips_sg_element_t *sg_list;
99 	ips_io_cmd *command_struct;
100 	struct bio *iobuf = command->arg;
101 	int i, length = 0;
102 	u_int8_t cmdtype;
103 
104 	sc = command->sc;
105 	if (error) {
106 		printf("ips: error = %d in ips_sg_request_callback\n", error);
107 		bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
108 		bus_dmamap_destroy(command->data_dmatag, command->data_dmamap);
109 		iobuf->bio_flags |= BIO_ERROR;
110 		iobuf->bio_error = ENOMEM;
111 		ips_insert_free_cmd(sc, command);
112 		ipsd_finish(iobuf);
113 		return;
114 	}
115 	command_struct = (ips_io_cmd *)command->command_buffer;
116 	command_struct->id = command->id;
117 	command_struct->drivenum = (uintptr_t)iobuf->bio_driver1;
118 	if (segnum != 1) {
119 		if (ips_read_request(iobuf))
120 			cmdtype = IPS_SG_READ_CMD;
121 		else
122 			cmdtype = IPS_SG_WRITE_CMD;
123 		command_struct->segnum = segnum;
124 		sg_list = (ips_sg_element_t *)((u_int8_t *)
125 			   command->command_buffer + IPS_COMMAND_LEN);
126 		for (i = 0; i < segnum; i++) {
127 			sg_list[i].addr = segments[i].ds_addr;
128 			sg_list[i].len = segments[i].ds_len;
129 			length += segments[i].ds_len;
130 		}
131 		command_struct->buffaddr =
132 		    (u_int32_t)command->command_phys_addr + IPS_COMMAND_LEN;
133 	} else {
134 		if (ips_read_request(iobuf))
135 			cmdtype = IPS_READ_CMD;
136 		else
137 			cmdtype = IPS_WRITE_CMD;
138 		command_struct->buffaddr = segments[0].ds_addr;
139 		length = segments[0].ds_len;
140 	}
141 	command_struct->command = cmdtype;
142 	command_struct->lba = iobuf->bio_pblkno;
143 	length = (length + IPS_BLKSIZE - 1)/IPS_BLKSIZE;
144 	command_struct->length = length;
145 	bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
146 			BUS_DMASYNC_PREWRITE);
147 	if (ips_read_request(iobuf)) {
148 		bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
149 				BUS_DMASYNC_PREREAD);
150 	} else {
151 		bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
152 				BUS_DMASYNC_PREWRITE);
153 	}
154 	/*
155 	 * the cast to long long below is necessary because our b_pblkno
156 	 * is 32bit wide whereas it's 64bit on FreeBSD-CURRENT.
157 	 */
158 	PRINTF(10, "ips test: command id: %d segments: %d "
159 		"pblkno: %lld length: %d, ds_len: %d\n", command->id, segnum,
160 		(long long)iobuf->bio_pblkno,
161 		length, segments[0].ds_len);
162 
163 	sc->ips_issue_cmd(command);
164 	return;
165 }
166 
167 static int
168 ips_send_io_request(ips_command_t *command)
169 {
170 	ips_softc_t *sc = command->sc;
171 	struct bio *iobuf = command->arg;
172 	command->data_dmatag = sc->sg_dmatag;
173 
174 	if (bus_dmamap_create(command->data_dmatag, 0, &command->data_dmamap)) {
175 		device_printf(sc->dev, "dmamap failed\n");
176 		iobuf->bio_flags |= BIO_ERROR;
177 		iobuf->bio_error = ENOMEM;
178 		ips_insert_free_cmd(sc, command);
179 		ipsd_finish(iobuf);
180 		return 0;
181 	}
182 	command->callback = ips_io_request_finish;
183 	PRINTF(10, "ips test: : bcount %ld\n", iobuf->bio_bcount);
184 	bus_dmamap_load(command->data_dmatag, command->data_dmamap,
185 			iobuf->bio_data, iobuf->bio_bcount,
186 			ips_io_request_callback, command, 0);
187 	return 0;
188 }
189 
190 void
191 ips_start_io_request(ips_softc_t *sc, struct bio *iobuf)
192 {
193 	if (ips_get_free_cmd(sc, ips_send_io_request, iobuf, 0)) {
194 		device_printf(sc->dev, "no mem for command slots!\n");
195 		iobuf->bio_flags |= BIO_ERROR;
196 		iobuf->bio_error = ENOMEM;
197 		ipsd_finish(iobuf);
198 		return;
199 	}
200 	return;
201 }
202 
203 /*
204  * Below are a series of functions for sending an adapter info request
205  * to the adapter.  The flow order is: get, send, callback. It uses
206  * the generic finish callback at the top of this file.
207  * This can be used to get configuration/status info from the card
208  */
209 static void
210 ips_adapter_info_callback(void *cmdptr, bus_dma_segment_t *segments,int segnum,
211 			  int error)
212 {
213 	ips_softc_t *sc;
214 	ips_command_t *command = cmdptr;
215 	ips_adapter_info_cmd *command_struct;
216 	sc = command->sc;
217 	if (error) {
218 		ips_cmd_status_t * status = command->arg;
219 		status->value = IPS_ERROR_STATUS; /* a lovely error value */
220 		ips_insert_free_cmd(sc, command);
221 		printf("ips: error = %d in ips_get_adapter_info\n", error);
222 		return;
223 	}
224 	command_struct = (ips_adapter_info_cmd *)command->command_buffer;
225 	command_struct->command = IPS_ADAPTER_INFO_CMD;
226 	command_struct->id = command->id;
227 	command_struct->buffaddr = segments[0].ds_addr;
228 
229 	bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
230 			BUS_DMASYNC_PREWRITE);
231 	bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
232 			BUS_DMASYNC_PREREAD);
233 	sc->ips_issue_cmd(command);
234 }
235 
236 static int
237 ips_send_adapter_info_cmd(ips_command_t *command)
238 {
239 	ips_softc_t *sc = command->sc;
240 	ips_cmd_status_t *status = command->arg;
241 	int error = 0;
242 
243 	if (bus_dma_tag_create(	/* parent    */	sc->adapter_dmatag,
244 				/* alignemnt */	1,
245 				/* boundary  */	0,
246 				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
247 				/* highaddr  */	BUS_SPACE_MAXADDR,
248 				/* filter    */	NULL,
249 				/* filterarg */	NULL,
250 				/* maxsize   */	IPS_ADAPTER_INFO_LEN,
251 				/* numsegs   */	1,
252 				/* maxsegsize*/	IPS_ADAPTER_INFO_LEN,
253 				/* flags     */	0,
254 				&command->data_dmatag) != 0) {
255 		printf("ips: can't alloc dma tag for adapter status\n");
256 		error = ENOMEM;
257 		goto exit;
258 	}
259 	if (bus_dmamem_alloc(command->data_dmatag, &command->data_buffer,
260 	   BUS_DMA_NOWAIT, &command->data_dmamap)) {
261 		error = ENOMEM;
262 		goto exit;
263 	}
264 	command->callback = ips_wakeup_callback;
265 	IPS_LOCK(sc);
266 	bus_dmamap_load(command->data_dmatag, command->data_dmamap,
267 	    command->data_buffer, IPS_ADAPTER_INFO_LEN,
268 	    ips_adapter_info_callback, command, BUS_DMA_NOWAIT);
269 	if ((status->value == IPS_ERROR_STATUS) ||
270 	    ips_msleep(status, sc, 0, "ips", 30 * hz) == EWOULDBLOCK)
271 		error = ETIMEDOUT;
272 	IPS_UNLOCK(sc);
273 	if (error == 0) {
274 		bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
275 		    BUS_DMASYNC_POSTREAD);
276 		memcpy(&(sc->adapter_info), command->data_buffer,
277 			IPS_ADAPTER_INFO_LEN);
278 	}
279 	bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
280 exit:
281 	/* I suppose I should clean up my memory allocations */
282 	bus_dmamem_free(command->data_dmatag, command->data_buffer,
283 	    command->data_dmamap);
284 	bus_dma_tag_destroy(command->data_dmatag);
285 	ips_insert_free_cmd(sc, command);
286 	return error;
287 }
288 
289 int
290 ips_get_adapter_info(ips_softc_t *sc)
291 {
292 	int error = 0;
293 	ips_cmd_status_t *status;
294 
295 	status = malloc(sizeof(ips_cmd_status_t), M_DEVBUF, M_NOWAIT | M_ZERO);
296 	if (status == NULL)
297 		return ENOMEM;
298 	if (ips_get_free_cmd(sc, ips_send_adapter_info_cmd, status,
299 	    IPS_NOWAIT_FLAG) > 0) {
300 		device_printf(sc->dev, "unable to get adapter configuration\n");
301 		free(status, M_DEVBUF);
302 		return ENXIO;
303 	}
304 	if (COMMAND_ERROR(status))
305 		error = ENXIO;
306 	free(status, M_DEVBUF);
307 	return error;
308 }
309 
310 /*
311  * Below are a series of functions for sending a drive info request
312  * to the adapter.  The flow order is: get, send, callback. It uses
313  * the generic finish callback at the top of this file.
314  * This can be used to get drive status info from the card
315  */
316 static void
317 ips_drive_info_callback(void *cmdptr, bus_dma_segment_t *segments, int segnum,
318 			int error)
319 {
320 	ips_softc_t *sc;
321 	ips_command_t *command = cmdptr;
322 	ips_drive_cmd *command_struct;
323 
324 	sc = command->sc;
325 	if (error) {
326 		ips_cmd_status_t *status = command->arg;
327 
328 		status->value = IPS_ERROR_STATUS;
329 		ips_insert_free_cmd(sc, command);
330 		printf("ips: error = %d in ips_get_drive_info\n", error);
331 		return;
332 	}
333 	command_struct = (ips_drive_cmd *)command->command_buffer;
334 	command_struct->command = IPS_DRIVE_INFO_CMD;
335 	command_struct->id = command->id;
336 	command_struct->buffaddr = segments[0].ds_addr;
337 	bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
338 	    BUS_DMASYNC_PREWRITE);
339 	bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
340 	    BUS_DMASYNC_PREREAD);
341 	sc->ips_issue_cmd(command);
342 }
343 
344 static int
345 ips_send_drive_info_cmd(ips_command_t *command)
346 {
347 	int error = 0;
348 	ips_softc_t *sc = command->sc;
349 	ips_cmd_status_t *status = command->arg;
350 	ips_drive_info_t *driveinfo;
351 
352 	if (bus_dma_tag_create(	/* parent    */	sc->adapter_dmatag,
353 				/* alignemnt */	1,
354 				/* boundary  */	0,
355 				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
356 				/* highaddr  */	BUS_SPACE_MAXADDR,
357 				/* filter    */	NULL,
358 				/* filterarg */	NULL,
359 				/* maxsize   */	IPS_DRIVE_INFO_LEN,
360 				/* numsegs   */	1,
361 				/* maxsegsize*/	IPS_DRIVE_INFO_LEN,
362 				/* flags     */	0,
363 				&command->data_dmatag) != 0) {
364 		printf("ips: can't alloc dma tag for drive status\n");
365 		error = ENOMEM;
366 		goto exit;
367 	}
368 	if (bus_dmamem_alloc(command->data_dmatag, &command->data_buffer,
369 	    BUS_DMA_NOWAIT, &command->data_dmamap)) {
370 		error = ENOMEM;
371 		goto exit;
372 	}
373 	command->callback = ips_wakeup_callback;
374 	IPS_LOCK(sc);
375 	bus_dmamap_load(command->data_dmatag, command->data_dmamap,
376 	    command->data_buffer,IPS_DRIVE_INFO_LEN,
377 	    ips_drive_info_callback, command, BUS_DMA_NOWAIT);
378 	if ((status->value == IPS_ERROR_STATUS) ||
379 	    ips_msleep(status, sc, 0, "ips", 10 * hz) == EWOULDBLOCK)
380 		error = ETIMEDOUT;
381 	IPS_UNLOCK(sc);
382 
383 	if (error == 0) {
384 		bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
385 		    BUS_DMASYNC_POSTREAD);
386 		driveinfo = command->data_buffer;
387 		memcpy(sc->drives, driveinfo->drives, sizeof(ips_drive_t) * 8);
388 		sc->drivecount = driveinfo->drivecount;
389 		device_printf(sc->dev, "logical drives: %d\n", sc->drivecount);
390 	}
391 	bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
392 exit:
393 	/* I suppose I should clean up my memory allocations */
394 	bus_dmamem_free(command->data_dmatag, command->data_buffer,
395 			command->data_dmamap);
396 	bus_dma_tag_destroy(command->data_dmatag);
397 	ips_insert_free_cmd(sc, command);
398 	return error;
399 
400 }
401 int
402 ips_get_drive_info(ips_softc_t *sc)
403 {
404 	int error = 0;
405 	ips_cmd_status_t *status;
406 
407 	status = malloc(sizeof(ips_cmd_status_t), M_DEVBUF, M_NOWAIT | M_ZERO);
408 	if (status == NULL)
409 		return ENOMEM;
410 	if (ips_get_free_cmd(sc, ips_send_drive_info_cmd, status,
411 	    IPS_NOWAIT_FLAG) > 0) {
412 		free(status, M_DEVBUF);
413 		device_printf(sc->dev, "unable to get drive configuration\n");
414 		return ENXIO;
415 	}
416 	if (COMMAND_ERROR(status))
417 		error = ENXIO;
418 	free(status, M_DEVBUF);
419 	return error;
420 }
421 
422 /*
423  * Below is a pair of functions for making sure data is safely
424  * on disk by flushing the adapter's cache.
425  */
426 static int
427 ips_send_flush_cache_cmd(ips_command_t *command)
428 {
429 	ips_softc_t *sc = command->sc;
430 	ips_cmd_status_t *status = command->arg;
431 	ips_generic_cmd *command_struct;
432 
433 	PRINTF(10,"ips test: got a command, building flush command\n");
434 	command->callback = ips_wakeup_callback;
435 	command_struct = (ips_generic_cmd *)command->command_buffer;
436 	command_struct->command = IPS_CACHE_FLUSH_CMD;
437 	command_struct->id = command->id;
438 	bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
439 	    BUS_DMASYNC_PREWRITE);
440 	IPS_LOCK(sc);
441 	sc->ips_issue_cmd(command);
442 	if (status->value != IPS_ERROR_STATUS)
443 		ips_msleep(status, sc, 0, "flush2", 0);
444 	IPS_UNLOCK(sc);
445 	ips_insert_free_cmd(sc, command);
446 	return 0;
447 }
448 
449 int
450 ips_flush_cache(ips_softc_t *sc)
451 {
452 	ips_cmd_status_t *status;
453 
454 	status = malloc(sizeof(ips_cmd_status_t), M_DEVBUF, M_NOWAIT | M_ZERO);
455 	if (status == NULL)
456 		return ENOMEM;
457 	device_printf(sc->dev, "flushing cache\n");
458 	if (ips_get_free_cmd(sc, ips_send_flush_cache_cmd, status,
459 	    IPS_NOWAIT_FLAG)) {
460 		free(status, M_DEVBUF);
461 		device_printf(sc->dev, "ERROR: unable to get a command! "
462 		    "can't flush cache!\n");
463 	}
464 	if (COMMAND_ERROR(status)) {
465 		device_printf(sc->dev, "ERROR: cache flush command failed!\n");
466 	}
467 	free(status, M_DEVBUF);
468 	return 0;
469 }
470 
471 /*
472  * Simplified localtime to provide timevalues for ffdc.
473  * Taken from libc/stdtime/localtime.c
474  */
475 static void
476 ips_ffdc_settime(ips_adapter_ffdc_cmd *command, time_t sctime)
477 {
478 	long	days, rem, y;
479 	int	yleap, *ip, month;
480 	int	year_lengths[2] = { IPS_DAYSPERNYEAR, IPS_DAYSPERLYEAR };
481 	int	mon_lengths[2][IPS_MONSPERYEAR] = {
482 		{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
483 		{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
484 	};
485 
486 	days = sctime / IPS_SECSPERDAY;
487 	rem  = sctime % IPS_SECSPERDAY;
488 
489 	command->hour = rem / IPS_SECSPERHOUR;
490 	rem	      = rem % IPS_SECSPERHOUR;
491 
492 	command->minute = rem / IPS_SECSPERMIN;
493 	command->second = rem % IPS_SECSPERMIN;
494 
495 	y = IPS_EPOCH_YEAR;
496 	while (days < 0 || days >= (long)year_lengths[yleap = ips_isleap(y)]) {
497 		long    newy;
498 
499 		newy = y + days / IPS_DAYSPERNYEAR;
500 		if (days < 0)
501 			--newy;
502 		days -= (newy - y) * IPS_DAYSPERNYEAR +
503 		    IPS_LEAPS_THRU_END_OF(newy - 1) -
504 		    IPS_LEAPS_THRU_END_OF(y - 1);
505 		y = newy;
506 	}
507 	command->yearH = y / 100;
508 	command->yearL = y % 100;
509 	ip = mon_lengths[yleap];
510 	for (month = 0; days >= (long)ip[month]; ++month)
511 		days = days - (long)ip[month];
512 	command->month = month + 1;
513 	command->day = days + 1;
514 }
515 
516 static int
517 ips_send_ffdc_reset_cmd(ips_command_t *command)
518 {
519 	ips_softc_t *sc = command->sc;
520 	ips_cmd_status_t *status = command->arg;
521 	ips_adapter_ffdc_cmd *command_struct;
522 
523 	PRINTF(10, "ips test: got a command, building ffdc reset command\n");
524 	command->callback = ips_wakeup_callback;
525 	command_struct = (ips_adapter_ffdc_cmd *)command->command_buffer;
526 	command_struct->command = IPS_FFDC_CMD;
527 	command_struct->id = command->id;
528 	command_struct->reset_count = sc->ffdc_resetcount;
529 	command_struct->reset_type  = 0x0;
530 	ips_ffdc_settime(command_struct, sc->ffdc_resettime.tv_sec);
531 	bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
532 	    BUS_DMASYNC_PREWRITE);
533 	IPS_LOCK(sc);
534 	sc->ips_issue_cmd(command);
535 	if (status->value != IPS_ERROR_STATUS)
536 		ips_msleep(status, sc, 0, "ffdc", 0);
537 	IPS_UNLOCK(sc);
538 	ips_insert_free_cmd(sc, command);
539 	return 0;
540 }
541 
542 int
543 ips_ffdc_reset(ips_softc_t *sc)
544 {
545 	ips_cmd_status_t *status;
546 
547 	status = malloc(sizeof(ips_cmd_status_t), M_DEVBUF, M_NOWAIT | M_ZERO);
548 	if (status == NULL)
549 		return ENOMEM;
550 	if (ips_get_free_cmd(sc, ips_send_ffdc_reset_cmd, status,
551 	    IPS_NOWAIT_FLAG)) {
552 		free(status, M_DEVBUF);
553 		device_printf(sc->dev, "ERROR: unable to get a command! "
554 		    "can't send ffdc reset!\n");
555 	}
556 	if (COMMAND_ERROR(status))
557 		device_printf(sc->dev, "ERROR: ffdc reset command failed!\n");
558 	free(status, M_DEVBUF);
559 	return 0;
560 }
561 
562 static void
563 ips_write_nvram(ips_command_t *command)
564 {
565 	ips_softc_t *sc = command->sc;
566 	ips_rw_nvram_cmd *command_struct;
567 	ips_nvram_page5 *nvram;
568 
569 	/*FIXME check for error */
570 	command->callback = ips_wakeup_callback;
571 	command_struct = (ips_rw_nvram_cmd *)command->command_buffer;
572 	command_struct->command = IPS_RW_NVRAM_CMD;
573 	command_struct->id = command->id;
574 	command_struct->pagenum = 5;
575 	command_struct->rw	= 1;	/* write */
576 	bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
577 	    BUS_DMASYNC_POSTREAD);
578 	nvram = command->data_buffer;
579 	/* retrieve adapter info and save in sc */
580 	sc->adapter_type = nvram->adapter_type;
581 	strncpy(nvram->driver_high, IPS_VERSION_MAJOR, 4);
582 	strncpy(nvram->driver_low, IPS_VERSION_MINOR, 4);
583 	nvram->operating_system = IPS_OS_FREEBSD;
584 	bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
585 	    BUS_DMASYNC_PREWRITE);
586 	sc->ips_issue_cmd(command);
587 }
588 
589 static void
590 ips_read_nvram_callback(void *cmdptr, bus_dma_segment_t *segments, int segnum,
591 			int error)
592 {
593 	ips_softc_t *sc;
594 	ips_command_t *command = cmdptr;
595 	ips_rw_nvram_cmd *command_struct;
596 
597 	sc = command->sc;
598 	if (error) {
599 		ips_cmd_status_t *status = command->arg;
600 
601 		status->value = IPS_ERROR_STATUS;
602 		ips_insert_free_cmd(sc, command);
603 		printf("ips: error = %d in ips_read_nvram_callback\n", error);
604 		return;
605 	}
606 	command_struct = (ips_rw_nvram_cmd *)command->command_buffer;
607 	command_struct->command = IPS_RW_NVRAM_CMD;
608 	command_struct->id = command->id;
609 	command_struct->pagenum = 5;
610 	command_struct->rw = 0;
611 	command_struct->buffaddr = segments[0].ds_addr;
612 
613 	bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
614 	    BUS_DMASYNC_PREWRITE);
615 	bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
616 	    BUS_DMASYNC_PREREAD);
617 	sc->ips_issue_cmd(command);
618 }
619 
620 static int
621 ips_read_nvram(ips_command_t *command)
622 {
623 	int error = 0;
624 	ips_softc_t *sc = command->sc;
625 	ips_cmd_status_t *status = command->arg;
626 
627 	if (bus_dma_tag_create(	/* parent    */	sc->adapter_dmatag,
628 				/* alignemnt */	1,
629 				/* boundary  */	0,
630 				/* lowaddr   */	BUS_SPACE_MAXADDR_32BIT,
631 				/* highaddr  */	BUS_SPACE_MAXADDR,
632 				/* filter    */	NULL,
633 				/* filterarg */	NULL,
634 				/* maxsize   */	IPS_NVRAM_PAGE_SIZE,
635 				/* numsegs   */	1,
636 				/* maxsegsize*/	IPS_NVRAM_PAGE_SIZE,
637 				/* flags     */	0,
638 				&command->data_dmatag) != 0) {
639 		printf("ips: can't alloc dma tag for nvram\n");
640 		error = ENOMEM;
641 		goto exit;
642 	}
643 	if (bus_dmamem_alloc(command->data_dmatag, &command->data_buffer,
644 	    BUS_DMA_NOWAIT, &command->data_dmamap)) {
645 		error = ENOMEM;
646 		goto exit;
647 	}
648 	command->callback = ips_write_nvram;
649 	IPS_LOCK(sc);
650 	bus_dmamap_load(command->data_dmatag, command->data_dmamap,
651 	    command->data_buffer, IPS_NVRAM_PAGE_SIZE, ips_read_nvram_callback,
652 	    command, BUS_DMA_NOWAIT);
653 	if ((status->value == IPS_ERROR_STATUS) ||
654 	    ips_msleep(status, sc, 0, "ips", 0) == EWOULDBLOCK)
655 		error = ETIMEDOUT;
656 	IPS_UNLOCK(sc);
657 	if (error == 0) {
658 		bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
659 				BUS_DMASYNC_POSTWRITE);
660 	}
661 	bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
662 exit:
663 	bus_dmamem_free(command->data_dmatag, command->data_buffer,
664 			command->data_dmamap);
665 	bus_dma_tag_destroy(command->data_dmatag);
666 	ips_insert_free_cmd(sc, command);
667 	return error;
668 }
669 
670 int
671 ips_update_nvram(ips_softc_t *sc)
672 {
673 	ips_cmd_status_t *status;
674 
675 	status = malloc(sizeof(ips_cmd_status_t), M_DEVBUF, M_NOWAIT | M_ZERO);
676 	if (status == NULL)
677 		return ENOMEM;
678 	if (ips_get_free_cmd(sc, ips_read_nvram, status, IPS_NOWAIT_FLAG)) {
679 		free(status, M_DEVBUF);
680 		device_printf(sc->dev, "ERROR: unable to get a command! "
681 		    "can't update nvram\n");
682 		return 1;
683 	}
684 	if (COMMAND_ERROR(status))
685 		device_printf(sc->dev, "ERROR: nvram update command failed!\n");
686 	free(status, M_DEVBUF);
687 	return 0;
688 }
689 
690 static int
691 ips_send_config_sync_cmd(ips_command_t *command)
692 {
693 	ips_softc_t *sc = command->sc;
694 	ips_cmd_status_t *status = command->arg;
695 	ips_generic_cmd *command_struct;
696 
697 	PRINTF(10, "ips test: got a command, building flush command\n");
698 	command->callback = ips_wakeup_callback;
699 	command_struct = (ips_generic_cmd *)command->command_buffer;
700 	command_struct->command = IPS_CONFIG_SYNC_CMD;
701 	command_struct->id = command->id;
702 	command_struct->reserve2 = IPS_POCL;
703 	bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
704 	    BUS_DMASYNC_PREWRITE);
705 	IPS_LOCK(sc);
706 	sc->ips_issue_cmd(command);
707 	if (status->value != IPS_ERROR_STATUS)
708 		ips_msleep(status, sc, 0, "ipssyn", 0);
709 	IPS_UNLOCK(sc);
710 	ips_insert_free_cmd(sc, command);
711 	return 0;
712 }
713 
714 static int
715 ips_send_error_table_cmd(ips_command_t *command)
716 {
717 	ips_softc_t *sc = command->sc;
718 	ips_cmd_status_t *status = command->arg;
719 	ips_generic_cmd *command_struct;
720 
721 	PRINTF(10, "ips test: got a command, building errortable command\n");
722 	command->callback = ips_wakeup_callback;
723 	command_struct = (ips_generic_cmd *)command->command_buffer;
724 	command_struct->command = IPS_ERROR_TABLE_CMD;
725 	command_struct->id = command->id;
726 	command_struct->reserve2 = IPS_CSL;
727 	bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
728 	    BUS_DMASYNC_PREWRITE);
729 	IPS_LOCK(sc);
730 	sc->ips_issue_cmd(command);
731 	if (status->value != IPS_ERROR_STATUS)
732 		ips_msleep(status, sc, 0, "ipsetc", 0);
733 	IPS_UNLOCK(sc);
734 	ips_insert_free_cmd(sc, command);
735 	return 0;
736 }
737 
738 int
739 ips_clear_adapter(ips_softc_t *sc)
740 {
741 	ips_cmd_status_t *status;
742 
743 	status = malloc(sizeof(ips_cmd_status_t), M_DEVBUF, M_NOWAIT | M_ZERO);
744 	if (status == NULL)
745 		return ENOMEM;
746 	device_printf(sc->dev, "syncing config\n");
747 	if (ips_get_free_cmd(sc, ips_send_config_sync_cmd, status,
748 	    IPS_NOWAIT_FLAG)) {
749 		free(status, M_DEVBUF);
750 		device_printf(sc->dev, "ERROR: unable to get a command! "
751 		    "can't sync cache!\n");
752 		return 1;
753 	}
754 	if (COMMAND_ERROR(status)) {
755 		free(status, M_DEVBUF);
756 		device_printf(sc->dev, "ERROR: cache sync command failed!\n");
757 		return 1;
758 	}
759 	device_printf(sc->dev, "clearing error table\n");
760 	if (ips_get_free_cmd(sc, ips_send_error_table_cmd, status,
761 	    IPS_NOWAIT_FLAG)) {
762 		free(status, M_DEVBUF);
763 		device_printf(sc->dev, "ERROR: unable to get a command! "
764 		    "can't sync cache!\n");
765 		return 1;
766 	}
767 	if (COMMAND_ERROR(status)) {
768 		device_printf(sc->dev, "ERROR: etable command failed!\n");
769 		free(status, M_DEVBUF);
770 		return 1;
771 	}
772 	free(status, M_DEVBUF);
773 	return 0;
774 }
775