Merge from vendor branch AWK:
[dragonfly.git] / sys / dev / raid / ips / ips_commands.c
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.3 2004/02/26 14:07:21 joerg 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                 return 1;
464         }
465         if (COMMAND_ERROR(status)) {
466                 free(status, M_DEVBUF);
467                 device_printf(sc->dev, "ERROR: cache flush command failed!\n");
468                 return 1;
469         }
470         free(status, M_DEVBUF);
471         return 0;
472 }
473
474 /*
475  * Simplified localtime to provide timevalues for ffdc.
476  * Taken from libc/stdtime/localtime.c
477  */
478 static void
479 ips_ffdc_settime(ips_adapter_ffdc_cmd *command, time_t sctime)
480 {
481         long    days, rem, y;
482         int     yleap, *ip, month;
483         int     year_lengths[2] = { IPS_DAYSPERNYEAR, IPS_DAYSPERLYEAR };
484         int     mon_lengths[2][IPS_MONSPERYEAR] = {
485                 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
486                 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
487         };
488
489         days = sctime / IPS_SECSPERDAY;
490         rem  = sctime % IPS_SECSPERDAY;
491
492         command->hour = rem / IPS_SECSPERHOUR;
493         rem           = rem % IPS_SECSPERHOUR;
494
495         command->minute = rem / IPS_SECSPERMIN;
496         command->second = rem % IPS_SECSPERMIN;
497
498         y = IPS_EPOCH_YEAR;
499         while (days < 0 || days >= (long)year_lengths[yleap = ips_isleap(y)]) {
500                 long    newy;
501
502                 newy = y + days / IPS_DAYSPERNYEAR;
503                 if (days < 0)
504                         --newy;
505                 days -= (newy - y) * IPS_DAYSPERNYEAR +
506                     IPS_LEAPS_THRU_END_OF(newy - 1) -
507                     IPS_LEAPS_THRU_END_OF(y - 1);
508                 y = newy;
509         }
510         command->yearH = y / 100;
511         command->yearL = y % 100;
512         ip = mon_lengths[yleap];
513         for (month = 0; days >= (long)ip[month]; ++month)
514                 days = days - (long)ip[month];
515         command->month = month + 1;
516         command->day = days + 1;
517 }
518
519 static int
520 ips_send_ffdc_reset_cmd(ips_command_t *command)
521 {
522         ips_softc_t *sc = command->sc;
523         ips_cmd_status_t *status = command->arg;
524         ips_adapter_ffdc_cmd *command_struct;
525
526         PRINTF(10, "ips test: got a command, building ffdc reset command\n");
527         command->callback = ips_wakeup_callback;
528         command_struct = (ips_adapter_ffdc_cmd *)command->command_buffer;
529         command_struct->command = IPS_FFDC_CMD;
530         command_struct->id = command->id;
531         command_struct->reset_count = sc->ffdc_resetcount;
532         command_struct->reset_type  = 0x0;
533         ips_ffdc_settime(command_struct, sc->ffdc_resettime.tv_sec);
534         bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
535             BUS_DMASYNC_PREWRITE);
536         IPS_LOCK(sc);
537         sc->ips_issue_cmd(command);
538         if (status->value != IPS_ERROR_STATUS)
539                 ips_msleep(status, sc, 0, "ffdc", 0);
540         IPS_UNLOCK(sc);
541         ips_insert_free_cmd(sc, command);
542         return 0;
543 }
544
545 int
546 ips_ffdc_reset(ips_softc_t *sc)
547 {
548         ips_cmd_status_t *status;
549
550         status = malloc(sizeof(ips_cmd_status_t), M_DEVBUF, M_NOWAIT | M_ZERO);
551         if (status == NULL)
552                 return ENOMEM;
553         if (ips_get_free_cmd(sc, ips_send_ffdc_reset_cmd, status,
554             IPS_NOWAIT_FLAG)) {
555                 free(status, M_DEVBUF);
556                 device_printf(sc->dev, "ERROR: unable to get a command! "
557                     "can't send ffdc reset!\n");
558                 return 1;
559         }
560         if (COMMAND_ERROR(status)) {
561                 free(status, M_DEVBUF);
562                 device_printf(sc->dev, "ERROR: ffdc reset command failed!\n");
563                 return 1;
564         }
565         free(status, M_DEVBUF);
566         return 0;
567 }
568
569 static void
570 ips_write_nvram(ips_command_t *command)
571 {
572         ips_softc_t *sc = command->sc;
573         ips_rw_nvram_cmd *command_struct;
574         ips_nvram_page5 *nvram;
575
576         /*FIXME check for error */
577         command->callback = ips_wakeup_callback;
578         command_struct = (ips_rw_nvram_cmd *)command->command_buffer;
579         command_struct->command = IPS_RW_NVRAM_CMD;
580         command_struct->id = command->id;
581         command_struct->pagenum = 5;
582         command_struct->rw      = 1;    /* write */
583         bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
584             BUS_DMASYNC_POSTREAD);
585         nvram = command->data_buffer;
586         /* retrieve adapter info and save in sc */
587         sc->adapter_type = nvram->adapter_type;
588         strncpy(nvram->driver_high, IPS_VERSION_MAJOR, 4);
589         strncpy(nvram->driver_low, IPS_VERSION_MINOR, 4);
590         nvram->operating_system = IPS_OS_FREEBSD;
591         bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
592             BUS_DMASYNC_PREWRITE);
593         sc->ips_issue_cmd(command);
594 }
595
596 static void
597 ips_read_nvram_callback(void *cmdptr, bus_dma_segment_t *segments, int segnum,
598                         int error)
599 {
600         ips_softc_t *sc;
601         ips_command_t *command = cmdptr;
602         ips_rw_nvram_cmd *command_struct;
603
604         sc = command->sc;
605         if (error) {
606                 ips_cmd_status_t *status = command->arg;
607
608                 status->value = IPS_ERROR_STATUS;
609                 ips_insert_free_cmd(sc, command);
610                 printf("ips: error = %d in ips_read_nvram_callback\n", error);
611                 return;
612         }
613         command_struct = (ips_rw_nvram_cmd *)command->command_buffer;
614         command_struct->command = IPS_RW_NVRAM_CMD;
615         command_struct->id = command->id;
616         command_struct->pagenum = 5;
617         command_struct->rw = 0;
618         command_struct->buffaddr = segments[0].ds_addr;
619
620         bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
621             BUS_DMASYNC_PREWRITE);
622         bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
623             BUS_DMASYNC_PREREAD);
624         sc->ips_issue_cmd(command);
625 }
626
627 static int
628 ips_read_nvram(ips_command_t *command)
629 {
630         int error = 0;
631         ips_softc_t *sc = command->sc;
632         ips_cmd_status_t *status = command->arg;
633
634         if (bus_dma_tag_create( /* parent    */ sc->adapter_dmatag,
635                                 /* alignemnt */ 1,
636                                 /* boundary  */ 0,
637                                 /* lowaddr   */ BUS_SPACE_MAXADDR_32BIT,
638                                 /* highaddr  */ BUS_SPACE_MAXADDR,
639                                 /* filter    */ NULL,
640                                 /* filterarg */ NULL,
641                                 /* maxsize   */ IPS_NVRAM_PAGE_SIZE,
642                                 /* numsegs   */ 1,
643                                 /* maxsegsize*/ IPS_NVRAM_PAGE_SIZE,
644                                 /* flags     */ 0,
645                                 &command->data_dmatag) != 0) {
646                 printf("ips: can't alloc dma tag for nvram\n");
647                 error = ENOMEM;
648                 goto exit;
649         }
650         if (bus_dmamem_alloc(command->data_dmatag, &command->data_buffer,
651             BUS_DMA_NOWAIT, &command->data_dmamap)) {
652                 error = ENOMEM;
653                 goto exit;
654         }
655         command->callback = ips_write_nvram;
656         IPS_LOCK(sc);
657         bus_dmamap_load(command->data_dmatag, command->data_dmamap,
658             command->data_buffer, IPS_NVRAM_PAGE_SIZE, ips_read_nvram_callback,
659             command, BUS_DMA_NOWAIT);
660         if ((status->value == IPS_ERROR_STATUS) ||
661             ips_msleep(status, sc, 0, "ips", 0) == EWOULDBLOCK)
662                 error = ETIMEDOUT;
663         IPS_UNLOCK(sc);
664         if (error == 0) {
665                 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
666                                 BUS_DMASYNC_POSTWRITE);
667         }
668         bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
669 exit:
670         bus_dmamem_free(command->data_dmatag, command->data_buffer,
671                         command->data_dmamap);
672         bus_dma_tag_destroy(command->data_dmatag);
673         ips_insert_free_cmd(sc, command);
674         return error;
675 }
676
677 int
678 ips_update_nvram(ips_softc_t *sc)
679 {
680         ips_cmd_status_t *status;
681
682         status = malloc(sizeof(ips_cmd_status_t), M_DEVBUF, M_NOWAIT | M_ZERO);
683         if (status == NULL)
684                 return ENOMEM;
685         if (ips_get_free_cmd(sc, ips_read_nvram, status, IPS_NOWAIT_FLAG)) {
686                 free(status, M_DEVBUF);
687                 device_printf(sc->dev, "ERROR: unable to get a command! "
688                     "can't update nvram\n");
689                 return 1;
690         }
691         if (COMMAND_ERROR(status)) {
692                 free(status, M_DEVBUF);
693                 device_printf(sc->dev, "ERROR: nvram update command failed!\n");
694                 return 1;
695         }
696         free(status, M_DEVBUF);
697         return 0;
698 }
699
700 static int
701 ips_send_config_sync_cmd(ips_command_t *command)
702 {
703         ips_softc_t *sc = command->sc;
704         ips_cmd_status_t *status = command->arg;
705         ips_generic_cmd *command_struct;
706
707         PRINTF(10, "ips test: got a command, building flush command\n");
708         command->callback = ips_wakeup_callback;
709         command_struct = (ips_generic_cmd *)command->command_buffer;
710         command_struct->command = IPS_CONFIG_SYNC_CMD;
711         command_struct->id = command->id;
712         command_struct->reserve2 = IPS_POCL;
713         bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
714             BUS_DMASYNC_PREWRITE);
715         IPS_LOCK(sc);
716         sc->ips_issue_cmd(command);
717         if (status->value != IPS_ERROR_STATUS)
718                 ips_msleep(status, sc, 0, "ipssyn", 0);
719         IPS_UNLOCK(sc);
720         ips_insert_free_cmd(sc, command);
721         return 0;
722 }
723
724 static int
725 ips_send_error_table_cmd(ips_command_t *command)
726 {
727         ips_softc_t *sc = command->sc;
728         ips_cmd_status_t *status = command->arg;
729         ips_generic_cmd *command_struct;
730
731         PRINTF(10, "ips test: got a command, building errortable command\n");
732         command->callback = ips_wakeup_callback;
733         command_struct = (ips_generic_cmd *)command->command_buffer;
734         command_struct->command = IPS_ERROR_TABLE_CMD;
735         command_struct->id = command->id;
736         command_struct->reserve2 = IPS_CSL;
737         bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
738             BUS_DMASYNC_PREWRITE);
739         IPS_LOCK(sc);
740         sc->ips_issue_cmd(command);
741         if (status->value != IPS_ERROR_STATUS)
742                 ips_msleep(status, sc, 0, "ipsetc", 0);
743         IPS_UNLOCK(sc);
744         ips_insert_free_cmd(sc, command);
745         return 0;
746 }
747
748 int
749 ips_clear_adapter(ips_softc_t *sc)
750 {
751         ips_cmd_status_t *status;
752
753         status = malloc(sizeof(ips_cmd_status_t), M_DEVBUF, M_NOWAIT | M_ZERO);
754         if (status == NULL)
755                 return ENOMEM;
756         device_printf(sc->dev, "syncing config\n");
757         if (ips_get_free_cmd(sc, ips_send_config_sync_cmd, status,
758             IPS_NOWAIT_FLAG)) {
759                 free(status, M_DEVBUF);
760                 device_printf(sc->dev, "ERROR: unable to get a command! "
761                     "can't sync cache!\n");
762                 return 1;
763         }
764         if (COMMAND_ERROR(status)) {
765                 free(status, M_DEVBUF);
766                 device_printf(sc->dev, "ERROR: cache sync command failed!\n");
767                 return 1;
768         }
769         device_printf(sc->dev, "clearing error table\n");
770         if (ips_get_free_cmd(sc, ips_send_error_table_cmd, status,
771             IPS_NOWAIT_FLAG)) {
772                 free(status, M_DEVBUF);
773                 device_printf(sc->dev, "ERROR: unable to get a command! "
774                     "can't sync cache!\n");
775                 return 1;
776         }
777         if (COMMAND_ERROR(status)) {
778                 device_printf(sc->dev, "ERROR: etable command failed!\n");
779                 free(status, M_DEVBUF);
780                 return 1;
781         }
782         free(status, M_DEVBUF);
783         return 0;
784 }