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