2 * Written by: David Jeffery
3 * Copyright (c) 2002 Adaptec Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
31 #include <dev/ips/ipsreg.h>
32 #include <dev/ips/ips.h>
35 * This is an interrupt callback. It is called from
36 * interrupt context when the adapter has completed the
37 * command. This very generic callback simply stores
38 * the command's return value in command->arg and wake's
39 * up anyone waiting on the command.
41 static void ips_wakeup_callback(ips_command_t *command)
43 bus_dmamap_sync(command->sc->command_dmatag, command->command_dmamap,
44 BUS_DMASYNC_POSTWRITE);
45 sema_post(&command->sc->cmd_sema);
47 /* Below are a series of functions for sending an IO request
48 * to the adapter. The flow order is: start, send, callback, finish.
49 * The caller must have already assembled an iorequest struct to hold
50 * the details of the IO request. */
51 static void ips_io_request_finish(ips_command_t *command)
54 struct bio *iobuf = command->arg;
55 if(ips_read_request(iobuf)) {
56 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
57 BUS_DMASYNC_POSTREAD);
59 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
60 BUS_DMASYNC_POSTWRITE);
62 bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
63 if(COMMAND_ERROR(command)){
64 iobuf->bio_flags |=BIO_ERROR;
65 iobuf->bio_error = EIO;
66 printf("ips: io error, status= 0x%x\n", command->status.value);
68 ips_insert_free_cmd(command->sc, command);
72 static void ips_io_request_callback(void *cmdptr, bus_dma_segment_t *segments,int segnum, int error)
75 ips_command_t *command = cmdptr;
76 ips_sg_element_t *sg_list;
77 ips_io_cmd *command_struct;
78 struct bio *iobuf = command->arg;
84 printf("ips: error = %d in ips_sg_request_callback\n", error);
85 bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
86 iobuf->bio_flags |= BIO_ERROR;
87 iobuf->bio_error = ENOMEM;
88 ips_insert_free_cmd(sc, command);
92 command_struct = (ips_io_cmd *)command->command_buffer;
93 command_struct->id = command->id;
94 command_struct->drivenum = (uintptr_t)iobuf->bio_driver1;
96 if(ips_read_request(iobuf))
97 cmdtype = IPS_SG_READ_CMD;
99 cmdtype = IPS_SG_WRITE_CMD;
100 command_struct->segnum = segnum;
101 sg_list = (ips_sg_element_t *)((u_int8_t *)
102 command->command_buffer + IPS_COMMAND_LEN);
103 for(i = 0; i < segnum; i++){
104 sg_list[i].addr = segments[i].ds_addr;
105 sg_list[i].len = segments[i].ds_len;
106 length += segments[i].ds_len;
108 command_struct->buffaddr =
109 (u_int32_t)command->command_phys_addr + IPS_COMMAND_LEN;
111 if(ips_read_request(iobuf))
112 cmdtype = IPS_READ_CMD;
114 cmdtype = IPS_WRITE_CMD;
115 command_struct->buffaddr = segments[0].ds_addr;
116 length = segments[0].ds_len;
118 command_struct->command = cmdtype;
119 command_struct->lba = iobuf->bio_pblkno;
120 length = (length + IPS_BLKSIZE - 1)/IPS_BLKSIZE;
121 command_struct->length = length;
122 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
123 BUS_DMASYNC_PREWRITE);
124 if(ips_read_request(iobuf)) {
125 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
126 BUS_DMASYNC_PREREAD);
128 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
129 BUS_DMASYNC_PREWRITE);
131 PRINTF(10, "ips test: command id: %d segments: %d "
132 "pblkno: %lld length: %d, ds_len: %d\n", command->id, segnum,
134 length, segments[0].ds_len);
136 sc->ips_issue_cmd(command);
140 static int ips_send_io_request(ips_command_t *command, struct bio *iobuf)
142 command->callback = ips_io_request_finish;
143 command->arg = iobuf;
144 PRINTF(10, "ips test: : bcount %ld\n", iobuf->bio_bcount);
145 bus_dmamap_load(command->data_dmatag, command->data_dmamap,
146 iobuf->bio_data, iobuf->bio_bcount,
147 ips_io_request_callback, command, 0);
151 void ips_start_io_request(ips_softc_t *sc)
154 ips_command_t *command;
156 iobuf = bioq_first(&sc->queue);
160 if (ips_get_free_cmd(sc, &command, 0))
163 bioq_remove(&sc->queue, iobuf);
164 ips_send_io_request(command, iobuf);
168 /* Below are a series of functions for sending an adapter info request
169 * to the adapter. The flow order is: get, send, callback. It uses
170 * the generic finish callback at the top of this file.
171 * This can be used to get configuration/status info from the card */
172 static void ips_adapter_info_callback(void *cmdptr, bus_dma_segment_t *segments,int segnum, int error)
175 ips_command_t *command = cmdptr;
176 ips_adapter_info_cmd *command_struct;
179 ips_set_error(command, error);
180 printf("ips: error = %d in ips_get_adapter_info\n", error);
183 command_struct = (ips_adapter_info_cmd *)command->command_buffer;
184 command_struct->command = IPS_ADAPTER_INFO_CMD;
185 command_struct->id = command->id;
186 command_struct->buffaddr = segments[0].ds_addr;
188 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
189 BUS_DMASYNC_PREWRITE);
190 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
191 BUS_DMASYNC_PREREAD);
192 sc->ips_issue_cmd(command);
193 if (sema_timedwait(&sc->cmd_sema, 30*hz) != 0) {
194 ips_set_error(command, ETIMEDOUT);
198 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
199 BUS_DMASYNC_POSTREAD);
200 memcpy(&(sc->adapter_info), command->data_buffer, IPS_ADAPTER_INFO_LEN);
205 static int ips_send_adapter_info_cmd(ips_command_t *command)
208 ips_softc_t *sc = command->sc;
210 if (bus_dma_tag_create( /* parent */ sc->adapter_dmatag,
213 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
214 /* highaddr */ BUS_SPACE_MAXADDR,
216 /* filterarg */ NULL,
217 /* maxsize */ IPS_ADAPTER_INFO_LEN,
219 /* maxsegsize*/ IPS_ADAPTER_INFO_LEN,
223 &command->data_dmatag) != 0) {
224 printf("ips: can't alloc dma tag for adapter status\n");
228 if(bus_dmamem_alloc(command->data_dmatag, &command->data_buffer,
229 BUS_DMA_NOWAIT, &command->data_dmamap)){
233 command->callback = ips_wakeup_callback;
234 error = bus_dmamap_load(command->data_dmatag, command->data_dmamap,
235 command->data_buffer,IPS_ADAPTER_INFO_LEN,
236 ips_adapter_info_callback, command,
240 bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
243 /* I suppose I should clean up my memory allocations */
244 bus_dmamem_free(command->data_dmatag, command->data_buffer,
245 command->data_dmamap);
246 bus_dma_tag_destroy(command->data_dmatag);
247 ips_insert_free_cmd(sc, command);
251 int ips_get_adapter_info(ips_softc_t *sc)
253 ips_command_t *command;
256 if (ips_get_free_cmd(sc, &command, IPS_STATIC_FLAG) > 0){
257 device_printf(sc->dev, "unable to get adapter configuration\n");
260 ips_send_adapter_info_cmd(command);
261 if (COMMAND_ERROR(command)){
267 /* Below are a series of functions for sending a drive info request
268 * to the adapter. The flow order is: get, send, callback. It uses
269 * the generic finish callback at the top of this file.
270 * This can be used to get drive status info from the card */
271 static void ips_drive_info_callback(void *cmdptr, bus_dma_segment_t *segments,int segnum, int error)
274 ips_command_t *command = cmdptr;
275 ips_drive_cmd *command_struct;
276 ips_drive_info_t *driveinfo;
280 ips_set_error(command, error);
281 printf("ips: error = %d in ips_get_drive_info\n", error);
284 command_struct = (ips_drive_cmd *)command->command_buffer;
285 command_struct->command = IPS_DRIVE_INFO_CMD;
286 command_struct->id = command->id;
287 command_struct->buffaddr = segments[0].ds_addr;
289 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
290 BUS_DMASYNC_PREWRITE);
291 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
292 BUS_DMASYNC_PREREAD);
293 sc->ips_issue_cmd(command);
294 if (sema_timedwait(&sc->cmd_sema, 10*hz) != 0) {
295 ips_set_error(command, ETIMEDOUT);
299 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
300 BUS_DMASYNC_POSTREAD);
301 driveinfo = command->data_buffer;
302 memcpy(sc->drives, driveinfo->drives, sizeof(ips_drive_t) * 8);
303 sc->drivecount = driveinfo->drivecount;
304 device_printf(sc->dev, "logical drives: %d\n",sc->drivecount);
307 static int ips_send_drive_info_cmd(ips_command_t *command)
310 ips_softc_t *sc = command->sc;
312 if (bus_dma_tag_create( /* parent */ sc->adapter_dmatag,
315 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
316 /* highaddr */ BUS_SPACE_MAXADDR,
318 /* filterarg */ NULL,
319 /* maxsize */ IPS_DRIVE_INFO_LEN,
321 /* maxsegsize*/ IPS_DRIVE_INFO_LEN,
325 &command->data_dmatag) != 0) {
326 printf("ips: can't alloc dma tag for drive status\n");
330 if(bus_dmamem_alloc(command->data_dmatag, &command->data_buffer,
331 BUS_DMA_NOWAIT, &command->data_dmamap)){
335 command->callback = ips_wakeup_callback;
336 error = bus_dmamap_load(command->data_dmatag, command->data_dmamap,
337 command->data_buffer,IPS_DRIVE_INFO_LEN,
338 ips_drive_info_callback, command,
341 bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
344 /* I suppose I should clean up my memory allocations */
345 bus_dmamem_free(command->data_dmatag, command->data_buffer,
346 command->data_dmamap);
347 bus_dma_tag_destroy(command->data_dmatag);
348 ips_insert_free_cmd(sc, command);
352 int ips_get_drive_info(ips_softc_t *sc)
355 ips_command_t *command;
357 if (ips_get_free_cmd(sc, &command, IPS_STATIC_FLAG) > 0){
358 device_printf(sc->dev, "unable to get drive configuration\n");
361 ips_send_drive_info_cmd(command);
362 if(COMMAND_ERROR(command)){
368 /* Below is a pair of functions for making sure data is safely
369 * on disk by flushing the adapter's cache. */
370 static int ips_send_flush_cache_cmd(ips_command_t *command)
372 ips_softc_t *sc = command->sc;
373 ips_generic_cmd *command_struct;
375 PRINTF(10,"ips test: got a command, building flush command\n");
376 command->callback = ips_wakeup_callback;
377 command_struct = (ips_generic_cmd *)command->command_buffer;
378 command_struct->command = IPS_CACHE_FLUSH_CMD;
379 command_struct->id = command->id;
380 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
381 BUS_DMASYNC_PREWRITE);
382 sc->ips_issue_cmd(command);
383 if (COMMAND_ERROR(command) == 0)
384 sema_wait(&sc->cmd_sema);
385 ips_insert_free_cmd(sc, command);
389 int ips_flush_cache(ips_softc_t *sc)
391 ips_command_t *command;
393 device_printf(sc->dev, "flushing cache\n");
394 if (ips_get_free_cmd(sc, &command, IPS_STATIC_FLAG)){
395 device_printf(sc->dev, "ERROR: unable to get a command! can't flush cache!\n");
397 ips_send_flush_cache_cmd(command);
398 if(COMMAND_ERROR(command)){
399 device_printf(sc->dev, "ERROR: cache flush command failed!\n");
404 /* Simplified localtime to provide timevalues for ffdc.
405 * Taken from libc/stdtime/localtime.c
407 void static ips_ffdc_settime(ips_adapter_ffdc_cmd *command, time_t sctime)
410 int yleap, *ip, month;
411 int year_lengths[2] = { IPS_DAYSPERNYEAR, IPS_DAYSPERLYEAR };
412 int mon_lengths[2][IPS_MONSPERYEAR] = {
413 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
414 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
417 days = sctime / IPS_SECSPERDAY;
418 rem = sctime % IPS_SECSPERDAY;
420 command->hour = rem / IPS_SECSPERHOUR;
421 rem = rem % IPS_SECSPERHOUR;
423 command->minute = rem / IPS_SECSPERMIN;
424 command->second = rem % IPS_SECSPERMIN;
427 while (days < 0 || days >= (long) year_lengths[yleap = ips_isleap(y)]) {
430 newy = y + days / IPS_DAYSPERNYEAR;
433 days -= (newy - y) * IPS_DAYSPERNYEAR +
434 IPS_LEAPS_THRU_END_OF(newy - 1) -
435 IPS_LEAPS_THRU_END_OF(y - 1);
438 command->yearH = y / 100;
439 command->yearL = y % 100;
440 ip = mon_lengths[yleap];
441 for(month = 0; days >= (long) ip[month]; ++month)
442 days = days - (long) ip[month];
443 command->month = month + 1;
444 command->day = days + 1;
447 static int ips_send_ffdc_reset_cmd(ips_command_t *command)
449 ips_softc_t *sc = command->sc;
450 ips_adapter_ffdc_cmd *command_struct;
452 PRINTF(10,"ips test: got a command, building ffdc reset command\n");
453 command->callback = ips_wakeup_callback;
454 command_struct = (ips_adapter_ffdc_cmd *)command->command_buffer;
455 command_struct->command = IPS_FFDC_CMD;
456 command_struct->id = command->id;
457 command_struct->reset_count = sc->ffdc_resetcount;
458 command_struct->reset_type = 0x0;
459 ips_ffdc_settime(command_struct, sc->ffdc_resettime.tv_sec);
461 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
462 BUS_DMASYNC_PREWRITE);
463 sc->ips_issue_cmd(command);
464 if (COMMAND_ERROR(command) == 0)
465 sema_wait(&sc->cmd_sema);
466 ips_insert_free_cmd(sc, command);
470 int ips_ffdc_reset(ips_softc_t *sc)
472 ips_command_t *command;
474 if (ips_get_free_cmd(sc, &command, IPS_STATIC_FLAG)){
475 device_printf(sc->dev, "ERROR: unable to get a command! can't send ffdc reset!\n");
477 ips_send_ffdc_reset_cmd(command);
478 if(COMMAND_ERROR(command)){
479 device_printf(sc->dev, "ERROR: ffdc reset command failed!\n");
484 static void ips_write_nvram(ips_command_t *command){
485 ips_softc_t *sc = command->sc;
486 ips_rw_nvram_cmd *command_struct;
487 ips_nvram_page5 *nvram;
489 /*FIXME check for error */
490 command->callback = ips_wakeup_callback;
491 command_struct = (ips_rw_nvram_cmd *)command->command_buffer;
492 command_struct->command = IPS_RW_NVRAM_CMD;
493 command_struct->id = command->id;
494 command_struct->pagenum = 5;
495 command_struct->rw = 1; /*write*/
496 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
497 BUS_DMASYNC_POSTREAD);
498 nvram = command->data_buffer;
499 /* retrieve adapter info and save in sc */
500 sc->adapter_type = nvram->adapter_type;
502 strncpy(nvram->driver_high, IPS_VERSION_MAJOR, 4);
503 strncpy(nvram->driver_low, IPS_VERSION_MINOR, 4);
504 nvram->operating_system = IPS_OS_FREEBSD;
505 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
506 BUS_DMASYNC_PREWRITE);
507 sc->ips_issue_cmd(command);
510 static void ips_read_nvram_callback(void *cmdptr, bus_dma_segment_t *segments,int segnum, int error)
513 ips_command_t *command = cmdptr;
514 ips_rw_nvram_cmd *command_struct;
517 ips_set_error(command, error);
518 printf("ips: error = %d in ips_read_nvram_callback\n", error);
521 command_struct = (ips_rw_nvram_cmd *)command->command_buffer;
522 command_struct->command = IPS_RW_NVRAM_CMD;
523 command_struct->id = command->id;
524 command_struct->pagenum = 5;
525 command_struct->rw = 0;
526 command_struct->buffaddr = segments[0].ds_addr;
528 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
529 BUS_DMASYNC_PREWRITE);
530 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
531 BUS_DMASYNC_PREREAD);
532 sc->ips_issue_cmd(command);
533 if (sema_timedwait(&sc->cmd_sema, 30*hz) != 0) {
534 ips_set_error(command, ETIMEDOUT);
537 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
538 BUS_DMASYNC_POSTWRITE);
541 static int ips_read_nvram(ips_command_t *command)
544 ips_softc_t *sc = command->sc;
546 if (bus_dma_tag_create( /* parent */ sc->adapter_dmatag,
549 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
550 /* highaddr */ BUS_SPACE_MAXADDR,
552 /* filterarg */ NULL,
553 /* maxsize */ IPS_NVRAM_PAGE_SIZE,
555 /* maxsegsize*/ IPS_NVRAM_PAGE_SIZE,
559 &command->data_dmatag) != 0) {
560 printf("ips: can't alloc dma tag for nvram\n");
564 if(bus_dmamem_alloc(command->data_dmatag, &command->data_buffer,
565 BUS_DMA_NOWAIT, &command->data_dmamap)){
569 command->callback = ips_write_nvram;
570 error = bus_dmamap_load(command->data_dmatag, command->data_dmamap,
571 command->data_buffer,IPS_NVRAM_PAGE_SIZE,
572 ips_read_nvram_callback, command,
575 bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
578 bus_dmamem_free(command->data_dmatag, command->data_buffer,
579 command->data_dmamap);
580 bus_dma_tag_destroy(command->data_dmatag);
581 ips_insert_free_cmd(sc, command);
585 int ips_update_nvram(ips_softc_t *sc)
587 ips_command_t *command;
589 if (ips_get_free_cmd(sc, &command, IPS_STATIC_FLAG)){
590 device_printf(sc->dev, "ERROR: unable to get a command! can't update nvram\n");
593 ips_read_nvram(command);
594 if(COMMAND_ERROR(command)){
595 device_printf(sc->dev, "ERROR: nvram update command failed!\n");
603 static int ips_send_config_sync_cmd(ips_command_t *command)
605 ips_softc_t *sc = command->sc;
606 ips_generic_cmd *command_struct;
608 PRINTF(10,"ips test: got a command, building flush command\n");
609 command->callback = ips_wakeup_callback;
610 command_struct = (ips_generic_cmd *)command->command_buffer;
611 command_struct->command = IPS_CONFIG_SYNC_CMD;
612 command_struct->id = command->id;
613 command_struct->reserve2 = IPS_POCL;
614 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
615 BUS_DMASYNC_PREWRITE);
616 sc->ips_issue_cmd(command);
617 if (COMMAND_ERROR(command) == 0)
618 sema_wait(&sc->cmd_sema);
619 ips_insert_free_cmd(sc, command);
623 static int ips_send_error_table_cmd(ips_command_t *command)
625 ips_softc_t *sc = command->sc;
626 ips_generic_cmd *command_struct;
628 PRINTF(10,"ips test: got a command, building errortable command\n");
629 command->callback = ips_wakeup_callback;
630 command_struct = (ips_generic_cmd *)command->command_buffer;
631 command_struct->command = IPS_ERROR_TABLE_CMD;
632 command_struct->id = command->id;
633 command_struct->reserve2 = IPS_CSL;
634 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
635 BUS_DMASYNC_PREWRITE);
636 sc->ips_issue_cmd(command);
637 if (COMMAND_ERROR(command) == 0)
638 sema_wait(&sc->cmd_sema);
639 ips_insert_free_cmd(sc, command);
644 int ips_clear_adapter(ips_softc_t *sc)
646 ips_command_t *command;
648 device_printf(sc->dev, "syncing config\n");
649 if (ips_get_free_cmd(sc, &command, IPS_STATIC_FLAG)){
650 device_printf(sc->dev, "ERROR: unable to get a command! can't sync cache!\n");
653 ips_send_config_sync_cmd(command);
654 if(COMMAND_ERROR(command)){
655 device_printf(sc->dev, "ERROR: cache sync command failed!\n");
659 device_printf(sc->dev, "clearing error table\n");
660 if(ips_get_free_cmd(sc, &command, IPS_STATIC_FLAG)){
661 device_printf(sc->dev, "ERROR: unable to get a command! can't sync cache!\n");
664 ips_send_error_table_cmd(command);
665 if(COMMAND_ERROR(command)){
666 device_printf(sc->dev, "ERROR: etable command failed!\n");