2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Written by: David Jeffery
5 * Copyright (c) 2002 Adaptec Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
33 #include <dev/ips/ipsreg.h>
34 #include <dev/ips/ips.h>
37 * This is an interrupt callback. It is called from
38 * interrupt context when the adapter has completed the
39 * command. This very generic callback simply stores
40 * the command's return value in command->arg and wake's
41 * up anyone waiting on the command.
43 static void ips_wakeup_callback(ips_command_t *command)
45 bus_dmamap_sync(command->sc->command_dmatag, command->command_dmamap,
46 BUS_DMASYNC_POSTWRITE);
47 sema_post(&command->sc->cmd_sema);
49 /* Below are a series of functions for sending an IO request
50 * to the adapter. The flow order is: start, send, callback, finish.
51 * The caller must have already assembled an iorequest struct to hold
52 * the details of the IO request. */
53 static void ips_io_request_finish(ips_command_t *command)
56 struct bio *iobuf = command->arg;
57 if(ips_read_request(iobuf)) {
58 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
59 BUS_DMASYNC_POSTREAD);
61 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
62 BUS_DMASYNC_POSTWRITE);
64 bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
65 if(COMMAND_ERROR(command)){
66 iobuf->bio_flags |=BIO_ERROR;
67 iobuf->bio_error = EIO;
68 printf("ips: io error, status= 0x%x\n", command->status.value);
70 ips_insert_free_cmd(command->sc, command);
74 static void ips_io_request_callback(void *cmdptr, bus_dma_segment_t *segments,int segnum, int error)
77 ips_command_t *command = cmdptr;
78 ips_sg_element_t *sg_list;
79 ips_io_cmd *command_struct;
80 struct bio *iobuf = command->arg;
86 printf("ips: error = %d in ips_sg_request_callback\n", error);
87 bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
88 iobuf->bio_flags |= BIO_ERROR;
89 iobuf->bio_error = ENOMEM;
90 ips_insert_free_cmd(sc, command);
94 command_struct = (ips_io_cmd *)command->command_buffer;
95 command_struct->id = command->id;
96 command_struct->drivenum = (uintptr_t)iobuf->bio_driver1;
98 if(ips_read_request(iobuf))
99 cmdtype = IPS_SG_READ_CMD;
101 cmdtype = IPS_SG_WRITE_CMD;
102 command_struct->segnum = segnum;
103 sg_list = (ips_sg_element_t *)((u_int8_t *)
104 command->command_buffer + IPS_COMMAND_LEN);
105 for(i = 0; i < segnum; i++){
106 sg_list[i].addr = segments[i].ds_addr;
107 sg_list[i].len = segments[i].ds_len;
108 length += segments[i].ds_len;
110 command_struct->buffaddr =
111 (u_int32_t)command->command_phys_addr + IPS_COMMAND_LEN;
113 if(ips_read_request(iobuf))
114 cmdtype = IPS_READ_CMD;
116 cmdtype = IPS_WRITE_CMD;
117 command_struct->buffaddr = segments[0].ds_addr;
118 length = segments[0].ds_len;
120 command_struct->command = cmdtype;
121 command_struct->lba = iobuf->bio_pblkno;
122 length = (length + IPS_BLKSIZE - 1)/IPS_BLKSIZE;
123 command_struct->length = length;
124 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
125 BUS_DMASYNC_PREWRITE);
126 if(ips_read_request(iobuf)) {
127 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
128 BUS_DMASYNC_PREREAD);
130 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
131 BUS_DMASYNC_PREWRITE);
133 PRINTF(10, "ips test: command id: %d segments: %d "
134 "pblkno: %lld length: %d, ds_len: %d\n", command->id, segnum,
136 length, segments[0].ds_len);
138 sc->ips_issue_cmd(command);
142 static int ips_send_io_request(ips_command_t *command, struct bio *iobuf)
144 command->callback = ips_io_request_finish;
145 command->arg = iobuf;
146 PRINTF(10, "ips test: : bcount %ld\n", iobuf->bio_bcount);
147 bus_dmamap_load(command->data_dmatag, command->data_dmamap,
148 iobuf->bio_data, iobuf->bio_bcount,
149 ips_io_request_callback, command, 0);
153 void ips_start_io_request(ips_softc_t *sc)
156 ips_command_t *command;
158 iobuf = bioq_first(&sc->queue);
162 if (ips_get_free_cmd(sc, &command, 0))
165 bioq_remove(&sc->queue, iobuf);
166 ips_send_io_request(command, iobuf);
170 /* Below are a series of functions for sending an adapter info request
171 * to the adapter. The flow order is: get, send, callback. It uses
172 * the generic finish callback at the top of this file.
173 * This can be used to get configuration/status info from the card */
174 static void ips_adapter_info_callback(void *cmdptr, bus_dma_segment_t *segments,int segnum, int error)
177 ips_command_t *command = cmdptr;
178 ips_adapter_info_cmd *command_struct;
181 ips_set_error(command, error);
182 printf("ips: error = %d in ips_get_adapter_info\n", error);
185 command_struct = (ips_adapter_info_cmd *)command->command_buffer;
186 command_struct->command = IPS_ADAPTER_INFO_CMD;
187 command_struct->id = command->id;
188 command_struct->buffaddr = segments[0].ds_addr;
190 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
191 BUS_DMASYNC_PREWRITE);
192 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
193 BUS_DMASYNC_PREREAD);
194 sc->ips_issue_cmd(command);
195 if (sema_timedwait(&sc->cmd_sema, 30*hz) != 0) {
196 ips_set_error(command, ETIMEDOUT);
200 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
201 BUS_DMASYNC_POSTREAD);
202 memcpy(&(sc->adapter_info), command->data_buffer, IPS_ADAPTER_INFO_LEN);
207 static int ips_send_adapter_info_cmd(ips_command_t *command)
210 ips_softc_t *sc = command->sc;
212 if (bus_dma_tag_create( /* parent */ sc->adapter_dmatag,
215 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
216 /* highaddr */ BUS_SPACE_MAXADDR,
218 /* filterarg */ NULL,
219 /* maxsize */ IPS_ADAPTER_INFO_LEN,
221 /* maxsegsize*/ IPS_ADAPTER_INFO_LEN,
225 &command->data_dmatag) != 0) {
226 printf("ips: can't alloc dma tag for adapter status\n");
230 if(bus_dmamem_alloc(command->data_dmatag, &command->data_buffer,
231 BUS_DMA_NOWAIT, &command->data_dmamap)){
235 command->callback = ips_wakeup_callback;
236 error = bus_dmamap_load(command->data_dmatag, command->data_dmamap,
237 command->data_buffer,IPS_ADAPTER_INFO_LEN,
238 ips_adapter_info_callback, command,
242 bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
245 /* I suppose I should clean up my memory allocations */
246 bus_dmamem_free(command->data_dmatag, command->data_buffer,
247 command->data_dmamap);
248 bus_dma_tag_destroy(command->data_dmatag);
249 ips_insert_free_cmd(sc, command);
253 int ips_get_adapter_info(ips_softc_t *sc)
255 ips_command_t *command;
258 if (ips_get_free_cmd(sc, &command, IPS_STATIC_FLAG) > 0){
259 device_printf(sc->dev, "unable to get adapter configuration\n");
262 ips_send_adapter_info_cmd(command);
263 if (COMMAND_ERROR(command)){
269 /* Below are a series of functions for sending a drive info request
270 * to the adapter. The flow order is: get, send, callback. It uses
271 * the generic finish callback at the top of this file.
272 * This can be used to get drive status info from the card */
273 static void ips_drive_info_callback(void *cmdptr, bus_dma_segment_t *segments,int segnum, int error)
276 ips_command_t *command = cmdptr;
277 ips_drive_cmd *command_struct;
278 ips_drive_info_t *driveinfo;
282 ips_set_error(command, error);
283 printf("ips: error = %d in ips_get_drive_info\n", error);
286 command_struct = (ips_drive_cmd *)command->command_buffer;
287 command_struct->command = IPS_DRIVE_INFO_CMD;
288 command_struct->id = command->id;
289 command_struct->buffaddr = segments[0].ds_addr;
291 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
292 BUS_DMASYNC_PREWRITE);
293 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
294 BUS_DMASYNC_PREREAD);
295 sc->ips_issue_cmd(command);
296 if (sema_timedwait(&sc->cmd_sema, 10*hz) != 0) {
297 ips_set_error(command, ETIMEDOUT);
301 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
302 BUS_DMASYNC_POSTREAD);
303 driveinfo = command->data_buffer;
304 memcpy(sc->drives, driveinfo->drives, sizeof(ips_drive_t) * 8);
305 sc->drivecount = driveinfo->drivecount;
306 device_printf(sc->dev, "logical drives: %d\n",sc->drivecount);
309 static int ips_send_drive_info_cmd(ips_command_t *command)
312 ips_softc_t *sc = command->sc;
314 if (bus_dma_tag_create( /* parent */ sc->adapter_dmatag,
317 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
318 /* highaddr */ BUS_SPACE_MAXADDR,
320 /* filterarg */ NULL,
321 /* maxsize */ IPS_DRIVE_INFO_LEN,
323 /* maxsegsize*/ IPS_DRIVE_INFO_LEN,
327 &command->data_dmatag) != 0) {
328 printf("ips: can't alloc dma tag for drive status\n");
332 if(bus_dmamem_alloc(command->data_dmatag, &command->data_buffer,
333 BUS_DMA_NOWAIT, &command->data_dmamap)){
337 command->callback = ips_wakeup_callback;
338 error = bus_dmamap_load(command->data_dmatag, command->data_dmamap,
339 command->data_buffer,IPS_DRIVE_INFO_LEN,
340 ips_drive_info_callback, command,
343 bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
346 /* I suppose I should clean up my memory allocations */
347 bus_dmamem_free(command->data_dmatag, command->data_buffer,
348 command->data_dmamap);
349 bus_dma_tag_destroy(command->data_dmatag);
350 ips_insert_free_cmd(sc, command);
354 int ips_get_drive_info(ips_softc_t *sc)
357 ips_command_t *command;
359 if (ips_get_free_cmd(sc, &command, IPS_STATIC_FLAG) > 0){
360 device_printf(sc->dev, "unable to get drive configuration\n");
363 ips_send_drive_info_cmd(command);
364 if(COMMAND_ERROR(command)){
370 /* Below is a pair of functions for making sure data is safely
371 * on disk by flushing the adapter's cache. */
372 static int ips_send_flush_cache_cmd(ips_command_t *command)
374 ips_softc_t *sc = command->sc;
375 ips_generic_cmd *command_struct;
377 PRINTF(10,"ips test: got a command, building flush command\n");
378 command->callback = ips_wakeup_callback;
379 command_struct = (ips_generic_cmd *)command->command_buffer;
380 command_struct->command = IPS_CACHE_FLUSH_CMD;
381 command_struct->id = command->id;
382 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
383 BUS_DMASYNC_PREWRITE);
384 sc->ips_issue_cmd(command);
385 if (COMMAND_ERROR(command) == 0)
386 sema_wait(&sc->cmd_sema);
387 ips_insert_free_cmd(sc, command);
391 int ips_flush_cache(ips_softc_t *sc)
393 ips_command_t *command;
395 device_printf(sc->dev, "flushing cache\n");
396 if (ips_get_free_cmd(sc, &command, IPS_STATIC_FLAG)){
397 device_printf(sc->dev, "ERROR: unable to get a command! can't flush cache!\n");
399 ips_send_flush_cache_cmd(command);
400 if(COMMAND_ERROR(command)){
401 device_printf(sc->dev, "ERROR: cache flush command failed!\n");
406 /* Simplified localtime to provide timevalues for ffdc.
407 * Taken from libc/stdtime/localtime.c
409 void static ips_ffdc_settime(ips_adapter_ffdc_cmd *command, time_t sctime)
412 int yleap, *ip, month;
413 int year_lengths[2] = { IPS_DAYSPERNYEAR, IPS_DAYSPERLYEAR };
414 int mon_lengths[2][IPS_MONSPERYEAR] = {
415 { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
416 { 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
419 days = sctime / IPS_SECSPERDAY;
420 rem = sctime % IPS_SECSPERDAY;
422 command->hour = rem / IPS_SECSPERHOUR;
423 rem = rem % IPS_SECSPERHOUR;
425 command->minute = rem / IPS_SECSPERMIN;
426 command->second = rem % IPS_SECSPERMIN;
429 while (days < 0 || days >= (long) year_lengths[yleap = ips_isleap(y)]) {
432 newy = y + days / IPS_DAYSPERNYEAR;
435 days -= (newy - y) * IPS_DAYSPERNYEAR +
436 IPS_LEAPS_THRU_END_OF(newy - 1) -
437 IPS_LEAPS_THRU_END_OF(y - 1);
440 command->yearH = y / 100;
441 command->yearL = y % 100;
442 ip = mon_lengths[yleap];
443 for(month = 0; days >= (long) ip[month]; ++month)
444 days = days - (long) ip[month];
445 command->month = month + 1;
446 command->day = days + 1;
449 static int ips_send_ffdc_reset_cmd(ips_command_t *command)
451 ips_softc_t *sc = command->sc;
452 ips_adapter_ffdc_cmd *command_struct;
454 PRINTF(10,"ips test: got a command, building ffdc reset command\n");
455 command->callback = ips_wakeup_callback;
456 command_struct = (ips_adapter_ffdc_cmd *)command->command_buffer;
457 command_struct->command = IPS_FFDC_CMD;
458 command_struct->id = command->id;
459 command_struct->reset_count = sc->ffdc_resetcount;
460 command_struct->reset_type = 0x0;
461 ips_ffdc_settime(command_struct, sc->ffdc_resettime.tv_sec);
463 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
464 BUS_DMASYNC_PREWRITE);
465 sc->ips_issue_cmd(command);
466 if (COMMAND_ERROR(command) == 0)
467 sema_wait(&sc->cmd_sema);
468 ips_insert_free_cmd(sc, command);
472 int ips_ffdc_reset(ips_softc_t *sc)
474 ips_command_t *command;
476 if (ips_get_free_cmd(sc, &command, IPS_STATIC_FLAG)){
477 device_printf(sc->dev, "ERROR: unable to get a command! can't send ffdc reset!\n");
479 ips_send_ffdc_reset_cmd(command);
480 if(COMMAND_ERROR(command)){
481 device_printf(sc->dev, "ERROR: ffdc reset command failed!\n");
486 static void ips_write_nvram(ips_command_t *command){
487 ips_softc_t *sc = command->sc;
488 ips_rw_nvram_cmd *command_struct;
489 ips_nvram_page5 *nvram;
491 /*FIXME check for error */
492 command->callback = ips_wakeup_callback;
493 command_struct = (ips_rw_nvram_cmd *)command->command_buffer;
494 command_struct->command = IPS_RW_NVRAM_CMD;
495 command_struct->id = command->id;
496 command_struct->pagenum = 5;
497 command_struct->rw = 1; /*write*/
498 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
499 BUS_DMASYNC_POSTREAD);
500 nvram = command->data_buffer;
501 /* retrieve adapter info and save in sc */
502 sc->adapter_type = nvram->adapter_type;
504 strncpy(nvram->driver_high, IPS_VERSION_MAJOR, 4);
505 strncpy(nvram->driver_low, IPS_VERSION_MINOR, 4);
506 nvram->operating_system = IPS_OS_FREEBSD;
507 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
508 BUS_DMASYNC_PREWRITE);
509 sc->ips_issue_cmd(command);
512 static void ips_read_nvram_callback(void *cmdptr, bus_dma_segment_t *segments,int segnum, int error)
515 ips_command_t *command = cmdptr;
516 ips_rw_nvram_cmd *command_struct;
519 ips_set_error(command, error);
520 printf("ips: error = %d in ips_read_nvram_callback\n", error);
523 command_struct = (ips_rw_nvram_cmd *)command->command_buffer;
524 command_struct->command = IPS_RW_NVRAM_CMD;
525 command_struct->id = command->id;
526 command_struct->pagenum = 5;
527 command_struct->rw = 0;
528 command_struct->buffaddr = segments[0].ds_addr;
530 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
531 BUS_DMASYNC_PREWRITE);
532 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
533 BUS_DMASYNC_PREREAD);
534 sc->ips_issue_cmd(command);
535 if (sema_timedwait(&sc->cmd_sema, 30*hz) != 0) {
536 ips_set_error(command, ETIMEDOUT);
539 bus_dmamap_sync(command->data_dmatag, command->data_dmamap,
540 BUS_DMASYNC_POSTWRITE);
543 static int ips_read_nvram(ips_command_t *command)
546 ips_softc_t *sc = command->sc;
548 if (bus_dma_tag_create( /* parent */ sc->adapter_dmatag,
551 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
552 /* highaddr */ BUS_SPACE_MAXADDR,
554 /* filterarg */ NULL,
555 /* maxsize */ IPS_NVRAM_PAGE_SIZE,
557 /* maxsegsize*/ IPS_NVRAM_PAGE_SIZE,
561 &command->data_dmatag) != 0) {
562 printf("ips: can't alloc dma tag for nvram\n");
566 if(bus_dmamem_alloc(command->data_dmatag, &command->data_buffer,
567 BUS_DMA_NOWAIT, &command->data_dmamap)){
571 command->callback = ips_write_nvram;
572 error = bus_dmamap_load(command->data_dmatag, command->data_dmamap,
573 command->data_buffer,IPS_NVRAM_PAGE_SIZE,
574 ips_read_nvram_callback, command,
577 bus_dmamap_unload(command->data_dmatag, command->data_dmamap);
580 bus_dmamem_free(command->data_dmatag, command->data_buffer,
581 command->data_dmamap);
582 bus_dma_tag_destroy(command->data_dmatag);
583 ips_insert_free_cmd(sc, command);
587 int ips_update_nvram(ips_softc_t *sc)
589 ips_command_t *command;
591 if (ips_get_free_cmd(sc, &command, IPS_STATIC_FLAG)){
592 device_printf(sc->dev, "ERROR: unable to get a command! can't update nvram\n");
595 ips_read_nvram(command);
596 if(COMMAND_ERROR(command)){
597 device_printf(sc->dev, "ERROR: nvram update command failed!\n");
605 static int ips_send_config_sync_cmd(ips_command_t *command)
607 ips_softc_t *sc = command->sc;
608 ips_generic_cmd *command_struct;
610 PRINTF(10,"ips test: got a command, building flush command\n");
611 command->callback = ips_wakeup_callback;
612 command_struct = (ips_generic_cmd *)command->command_buffer;
613 command_struct->command = IPS_CONFIG_SYNC_CMD;
614 command_struct->id = command->id;
615 command_struct->reserve2 = IPS_POCL;
616 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
617 BUS_DMASYNC_PREWRITE);
618 sc->ips_issue_cmd(command);
619 if (COMMAND_ERROR(command) == 0)
620 sema_wait(&sc->cmd_sema);
621 ips_insert_free_cmd(sc, command);
625 static int ips_send_error_table_cmd(ips_command_t *command)
627 ips_softc_t *sc = command->sc;
628 ips_generic_cmd *command_struct;
630 PRINTF(10,"ips test: got a command, building errortable command\n");
631 command->callback = ips_wakeup_callback;
632 command_struct = (ips_generic_cmd *)command->command_buffer;
633 command_struct->command = IPS_ERROR_TABLE_CMD;
634 command_struct->id = command->id;
635 command_struct->reserve2 = IPS_CSL;
636 bus_dmamap_sync(sc->command_dmatag, command->command_dmamap,
637 BUS_DMASYNC_PREWRITE);
638 sc->ips_issue_cmd(command);
639 if (COMMAND_ERROR(command) == 0)
640 sema_wait(&sc->cmd_sema);
641 ips_insert_free_cmd(sc, command);
646 int ips_clear_adapter(ips_softc_t *sc)
648 ips_command_t *command;
650 device_printf(sc->dev, "syncing config\n");
651 if (ips_get_free_cmd(sc, &command, IPS_STATIC_FLAG)){
652 device_printf(sc->dev, "ERROR: unable to get a command! can't sync cache!\n");
655 ips_send_config_sync_cmd(command);
656 if(COMMAND_ERROR(command)){
657 device_printf(sc->dev, "ERROR: cache sync command failed!\n");
661 device_printf(sc->dev, "clearing error table\n");
662 if(ips_get_free_cmd(sc, &command, IPS_STATIC_FLAG)){
663 device_printf(sc->dev, "ERROR: unable to get a command! can't sync cache!\n");
666 ips_send_error_table_cmd(command);
667 if(COMMAND_ERROR(command)){
668 device_printf(sc->dev, "ERROR: etable command failed!\n");