2 * CAM SCSI interface for the Advanced Systems Inc.
3 * Second Generation SCSI controllers.
5 * Product specific probe and attach routines can be found in:
7 * adw_pci.c ABP[3]940UW, ABP950UW, ABP3940U2W
9 * Copyright (c) 1998, 1999, 2000 Justin Gibbs.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. The name of the author may not be used to endorse or promote products
19 * derived from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
25 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 * advansys.c - Linux Host Driver for AdvanSys SCSI Adapters
37 * Copyright (c) 1995-1998 Advanced System Products, Inc.
38 * All Rights Reserved.
40 * Redistribution and use in source and binary forms, with or without
41 * modification, are permitted provided that redistributions of source
42 * code retain the above copyright notice and this comment without
46 #include <sys/cdefs.h>
47 __FBSDID("$FreeBSD$");
49 #include <sys/param.h>
51 #include <sys/systm.h>
52 #include <sys/kernel.h>
53 #include <sys/malloc.h>
55 #include <sys/module.h>
56 #include <sys/mutex.h>
59 #include <machine/bus.h>
60 #include <machine/resource.h>
65 #include <cam/cam_ccb.h>
66 #include <cam/cam_sim.h>
67 #include <cam/cam_xpt_sim.h>
68 #include <cam/cam_debug.h>
70 #include <cam/scsi/scsi_message.h>
72 #include <dev/advansys/adwvar.h>
74 /* Definitions for our use of the SIM private CCB area */
75 #define ccb_acb_ptr spriv_ptr0
76 #define ccb_adw_ptr spriv_ptr1
78 static __inline cam_status adwccbstatus(union ccb*);
79 static __inline struct acb* adwgetacb(struct adw_softc *adw);
80 static __inline void adwfreeacb(struct adw_softc *adw,
83 static void adwmapmem(void *arg, bus_dma_segment_t *segs,
85 static struct sg_map_node*
86 adwallocsgmap(struct adw_softc *adw);
87 static int adwallocacbs(struct adw_softc *adw);
89 static void adwexecuteacb(void *arg, bus_dma_segment_t *dm_segs,
91 static void adw_action(struct cam_sim *sim, union ccb *ccb);
92 static void adw_intr_locked(struct adw_softc *adw);
93 static void adw_poll(struct cam_sim *sim);
94 static void adw_async(void *callback_arg, u_int32_t code,
95 struct cam_path *path, void *arg);
96 static void adwprocesserror(struct adw_softc *adw, struct acb *acb);
97 static void adwtimeout(void *arg);
98 static void adw_handle_device_reset(struct adw_softc *adw,
100 static void adw_handle_bus_reset(struct adw_softc *adw,
103 static __inline cam_status
104 adwccbstatus(union ccb* ccb)
106 return (ccb->ccb_h.status & CAM_STATUS_MASK);
109 static __inline struct acb*
110 adwgetacb(struct adw_softc *adw)
115 mtx_assert(&adw->lock, MA_OWNED);
116 if ((acb = SLIST_FIRST(&adw->free_acb_list)) != NULL) {
117 SLIST_REMOVE_HEAD(&adw->free_acb_list, links);
118 } else if (adw->num_acbs < adw->max_acbs) {
120 acb = SLIST_FIRST(&adw->free_acb_list);
122 device_printf(adw->device, "Can't malloc ACB\n");
124 SLIST_REMOVE_HEAD(&adw->free_acb_list, links);
132 adwfreeacb(struct adw_softc *adw, struct acb *acb)
136 mtx_assert(&adw->lock, MA_OWNED);
137 if ((acb->state & ACB_ACTIVE) != 0)
138 LIST_REMOVE(&acb->ccb->ccb_h, sim_links.le);
139 if ((acb->state & ACB_RELEASE_SIMQ) != 0)
140 acb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
141 else if ((adw->state & ADW_RESOURCE_SHORTAGE) != 0
142 && (acb->ccb->ccb_h.status & CAM_RELEASE_SIMQ) == 0) {
143 acb->ccb->ccb_h.status |= CAM_RELEASE_SIMQ;
144 adw->state &= ~ADW_RESOURCE_SHORTAGE;
146 acb->state = ACB_FREE;
147 SLIST_INSERT_HEAD(&adw->free_acb_list, acb, links);
151 adwmapmem(void *arg, bus_dma_segment_t *segs, int nseg, int error)
153 bus_addr_t *busaddrp;
155 busaddrp = (bus_addr_t *)arg;
156 *busaddrp = segs->ds_addr;
159 static struct sg_map_node *
160 adwallocsgmap(struct adw_softc *adw)
162 struct sg_map_node *sg_map;
164 sg_map = malloc(sizeof(*sg_map), M_DEVBUF, M_NOWAIT);
169 /* Allocate S/G space for the next batch of ACBS */
170 if (bus_dmamem_alloc(adw->sg_dmat, (void **)&sg_map->sg_vaddr,
171 BUS_DMA_NOWAIT, &sg_map->sg_dmamap) != 0) {
172 free(sg_map, M_DEVBUF);
176 SLIST_INSERT_HEAD(&adw->sg_maps, sg_map, links);
178 bus_dmamap_load(adw->sg_dmat, sg_map->sg_dmamap, sg_map->sg_vaddr,
179 PAGE_SIZE, adwmapmem, &sg_map->sg_physaddr, /*flags*/0);
181 bzero(sg_map->sg_vaddr, PAGE_SIZE);
186 * Allocate another chunk of CCB's. Return count of entries added.
189 adwallocacbs(struct adw_softc *adw)
191 struct acb *next_acb;
192 struct sg_map_node *sg_map;
194 struct adw_sg_block *blocks;
198 next_acb = &adw->acbs[adw->num_acbs];
199 sg_map = adwallocsgmap(adw);
204 blocks = sg_map->sg_vaddr;
205 busaddr = sg_map->sg_physaddr;
207 newcount = (PAGE_SIZE / (ADW_SG_BLOCKCNT * sizeof(*blocks)));
208 for (i = 0; adw->num_acbs < adw->max_acbs && i < newcount; i++) {
211 error = bus_dmamap_create(adw->buffer_dmat, /*flags*/0,
215 next_acb->queue.scsi_req_baddr = acbvtob(adw, next_acb);
216 next_acb->queue.scsi_req_bo = acbvtobo(adw, next_acb);
217 next_acb->queue.sense_baddr =
218 acbvtob(adw, next_acb) + offsetof(struct acb, sense_data);
219 next_acb->sg_blocks = blocks;
220 next_acb->sg_busaddr = busaddr;
221 next_acb->state = ACB_FREE;
222 callout_init_mtx(&next_acb->timer, &adw->lock, 0);
223 SLIST_INSERT_HEAD(&adw->free_acb_list, next_acb, links);
224 blocks += ADW_SG_BLOCKCNT;
225 busaddr += ADW_SG_BLOCKCNT * sizeof(*blocks);
233 adwexecuteacb(void *arg, bus_dma_segment_t *dm_segs, int nseg, int error)
237 struct adw_softc *adw;
239 acb = (struct acb *)arg;
241 adw = (struct adw_softc *)ccb->ccb_h.ccb_adw_ptr;
244 mtx_assert(&adw->lock, MA_OWNED);
247 device_printf(adw->device, "Unexepected error 0x%x "
248 "returned from bus_dmamap_load\n", error);
249 if (ccb->ccb_h.status == CAM_REQ_INPROG) {
250 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
251 ccb->ccb_h.status = CAM_REQ_TOO_BIG|CAM_DEV_QFRZN;
253 adwfreeacb(adw, acb);
261 acb->queue.data_addr = dm_segs[0].ds_addr;
262 acb->queue.data_cnt = ccb->csio.dxfer_len;
264 struct adw_sg_block *sg_block;
265 struct adw_sg_elm *sg;
266 bus_addr_t sg_busaddr;
268 bus_dma_segment_t *end_seg;
270 end_seg = dm_segs + nseg;
272 sg_busaddr = acb->sg_busaddr;
274 /* Copy the segments into our SG list */
275 for (sg_block = acb->sg_blocks;; sg_block++) {
278 sg = sg_block->sg_list;
279 for (i = 0; i < ADW_NO_OF_SG_PER_BLOCK; i++) {
280 if (dm_segs >= end_seg)
283 sg->sg_addr = dm_segs->ds_addr;
284 sg->sg_count = dm_segs->ds_len;
288 sg_block->sg_cnt = i;
290 if (dm_segs == end_seg) {
291 sg_block->sg_busaddr_next = 0;
295 sizeof(struct adw_sg_block);
296 sg_block->sg_busaddr_next = sg_busaddr;
299 acb->queue.sg_real_addr = acb->sg_busaddr;
301 acb->queue.sg_real_addr = 0;
304 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
305 op = BUS_DMASYNC_PREREAD;
307 op = BUS_DMASYNC_PREWRITE;
309 bus_dmamap_sync(adw->buffer_dmat, acb->dmamap, op);
312 acb->queue.data_addr = 0;
313 acb->queue.data_cnt = 0;
314 acb->queue.sg_real_addr = 0;
318 * Last time we need to check if this CCB needs to
321 if (ccb->ccb_h.status != CAM_REQ_INPROG) {
323 bus_dmamap_unload(adw->buffer_dmat, acb->dmamap);
324 adwfreeacb(adw, acb);
329 acb->state |= ACB_ACTIVE;
330 ccb->ccb_h.status |= CAM_SIM_QUEUED;
331 LIST_INSERT_HEAD(&adw->pending_ccbs, &ccb->ccb_h, sim_links.le);
332 callout_reset(&acb->timer, (ccb->ccb_h.timeout * hz) / 1000,
335 adw_send_acb(adw, acb, acbvtob(adw, acb));
339 adw_action(struct cam_sim *sim, union ccb *ccb)
341 struct adw_softc *adw;
343 CAM_DEBUG(ccb->ccb_h.path, CAM_DEBUG_TRACE, ("adw_action\n"));
345 adw = (struct adw_softc *)cam_sim_softc(sim);
347 mtx_assert(&adw->lock, MA_OWNED);
349 switch (ccb->ccb_h.func_code) {
350 /* Common cases first */
351 case XPT_SCSI_IO: /* Execute the requested I/O operation */
353 struct ccb_scsiio *csio;
354 struct ccb_hdr *ccbh;
361 /* Max supported CDB length is 12 bytes */
362 if (csio->cdb_len > 12) {
363 ccb->ccb_h.status = CAM_REQ_INVALID;
368 if ((acb = adwgetacb(adw)) == NULL) {
369 adw->state |= ADW_RESOURCE_SHORTAGE;
370 xpt_freeze_simq(sim, /*count*/1);
371 ccb->ccb_h.status = CAM_REQUEUE_REQ;
376 /* Link acb and ccb so we can find one from the other */
378 ccb->ccb_h.ccb_acb_ptr = acb;
379 ccb->ccb_h.ccb_adw_ptr = adw;
382 acb->queue.target_cmd = 0;
383 acb->queue.target_id = ccb->ccb_h.target_id;
384 acb->queue.target_lun = ccb->ccb_h.target_lun;
386 acb->queue.mflag = 0;
387 acb->queue.sense_len =
388 MIN(csio->sense_len, sizeof(acb->sense_data));
389 acb->queue.cdb_len = csio->cdb_len;
390 if ((ccb->ccb_h.flags & CAM_TAG_ACTION_VALID) != 0) {
391 switch (csio->tag_action) {
392 case MSG_SIMPLE_Q_TAG:
393 acb->queue.scsi_cntl = ADW_QSC_SIMPLE_Q_TAG;
395 case MSG_HEAD_OF_Q_TAG:
396 acb->queue.scsi_cntl = ADW_QSC_HEAD_OF_Q_TAG;
398 case MSG_ORDERED_Q_TAG:
399 acb->queue.scsi_cntl = ADW_QSC_ORDERED_Q_TAG;
402 acb->queue.scsi_cntl = ADW_QSC_NO_TAGMSG;
406 acb->queue.scsi_cntl = ADW_QSC_NO_TAGMSG;
408 if ((ccb->ccb_h.flags & CAM_DIS_DISCONNECT) != 0)
409 acb->queue.scsi_cntl |= ADW_QSC_NO_DISC;
411 acb->queue.done_status = 0;
412 acb->queue.scsi_status = 0;
413 acb->queue.host_status = 0;
414 acb->queue.sg_wk_ix = 0;
415 if ((ccb->ccb_h.flags & CAM_CDB_POINTER) != 0) {
416 if ((ccb->ccb_h.flags & CAM_CDB_PHYS) == 0) {
417 bcopy(csio->cdb_io.cdb_ptr,
418 acb->queue.cdb, csio->cdb_len);
420 /* I guess I could map it in... */
421 ccb->ccb_h.status = CAM_REQ_INVALID;
422 adwfreeacb(adw, acb);
427 bcopy(csio->cdb_io.cdb_bytes,
428 acb->queue.cdb, csio->cdb_len);
431 error = bus_dmamap_load_ccb(adw->buffer_dmat,
436 if (error == EINPROGRESS) {
438 * So as to maintain ordering, freeze the controller
439 * queue until our mapping is returned.
441 xpt_freeze_simq(sim, 1);
442 acb->state |= CAM_RELEASE_SIMQ;
446 case XPT_RESET_DEV: /* Bus Device Reset the specified SCSI device */
448 adw_idle_cmd_status_t status;
450 status = adw_idle_cmd_send(adw, ADW_IDLE_CMD_DEVICE_RESET,
451 ccb->ccb_h.target_id);
452 if (status == ADW_IDLE_CMD_SUCCESS) {
453 ccb->ccb_h.status = CAM_REQ_CMP;
455 xpt_print_path(ccb->ccb_h.path);
456 printf("BDR Delivered\n");
459 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
463 case XPT_ABORT: /* Abort the specified CCB */
465 ccb->ccb_h.status = CAM_REQ_INVALID;
468 case XPT_SET_TRAN_SETTINGS:
470 struct ccb_trans_settings_scsi *scsi;
471 struct ccb_trans_settings_spi *spi;
472 struct ccb_trans_settings *cts;
476 target_mask = 0x01 << ccb->ccb_h.target_id;
478 scsi = &cts->proto_specific.scsi;
479 spi = &cts->xport_specific.spi;
480 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
483 sdtrdone = adw_lram_read_16(adw, ADW_MC_SDTR_DONE);
484 if ((spi->valid & CTS_SPI_VALID_DISC) != 0) {
488 adw_lram_read_16(adw, ADW_MC_DISC_ENABLE);
490 if ((spi->flags & CTS_SPI_FLAGS_DISC_ENB) != 0)
491 discenb |= target_mask;
493 discenb &= ~target_mask;
495 adw_lram_write_16(adw, ADW_MC_DISC_ENABLE,
499 if ((scsi->valid & CTS_SCSI_VALID_TQ) != 0) {
501 if ((scsi->flags & CTS_SCSI_FLAGS_TAG_ENB) != 0)
502 adw->tagenb |= target_mask;
504 adw->tagenb &= ~target_mask;
507 if ((spi->valid & CTS_SPI_VALID_BUS_WIDTH) != 0) {
513 adw_lram_read_16(adw, ADW_MC_WDTR_ABLE);
514 wdtrenb = wdtrenb_orig;
515 wdtrdone = adw_lram_read_16(adw,
517 switch (spi->bus_width) {
518 case MSG_EXT_WDTR_BUS_32_BIT:
519 case MSG_EXT_WDTR_BUS_16_BIT:
520 wdtrenb |= target_mask;
522 case MSG_EXT_WDTR_BUS_8_BIT:
524 wdtrenb &= ~target_mask;
527 if (wdtrenb != wdtrenb_orig) {
528 adw_lram_write_16(adw,
531 wdtrdone &= ~target_mask;
532 adw_lram_write_16(adw,
535 /* Wide negotiation forces async */
536 sdtrdone &= ~target_mask;
537 adw_lram_write_16(adw,
543 if (((spi->valid & CTS_SPI_VALID_SYNC_RATE) != 0)
544 || ((spi->valid & CTS_SPI_VALID_SYNC_OFFSET) != 0)) {
550 sdtr = adw_get_chip_sdtr(adw,
551 ccb->ccb_h.target_id);
553 sdtrable = adw_lram_read_16(adw,
555 sdtrable_orig = sdtrable;
558 & CTS_SPI_VALID_SYNC_RATE) != 0) {
566 & CTS_SPI_VALID_SYNC_OFFSET) != 0) {
567 if (spi->sync_offset == 0)
568 sdtr = ADW_MC_SDTR_ASYNC;
571 if (sdtr == ADW_MC_SDTR_ASYNC)
572 sdtrable &= ~target_mask;
574 sdtrable |= target_mask;
575 if (sdtr != sdtr_orig
576 || sdtrable != sdtrable_orig) {
577 adw_set_chip_sdtr(adw,
578 ccb->ccb_h.target_id,
580 sdtrdone &= ~target_mask;
581 adw_lram_write_16(adw, ADW_MC_SDTR_ABLE,
583 adw_lram_write_16(adw, ADW_MC_SDTR_DONE,
589 ccb->ccb_h.status = CAM_REQ_CMP;
593 case XPT_GET_TRAN_SETTINGS:
594 /* Get default/user set transfer settings for the target */
596 struct ccb_trans_settings_scsi *scsi;
597 struct ccb_trans_settings_spi *spi;
598 struct ccb_trans_settings *cts;
602 target_mask = 0x01 << ccb->ccb_h.target_id;
603 cts->protocol = PROTO_SCSI;
604 cts->protocol_version = SCSI_REV_2;
605 cts->transport = XPORT_SPI;
606 cts->transport_version = 2;
608 scsi = &cts->proto_specific.scsi;
609 spi = &cts->xport_specific.spi;
610 if (cts->type == CTS_TYPE_CURRENT_SETTINGS) {
614 if ((adw->user_discenb & target_mask) != 0)
615 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
617 if ((adw->user_tagenb & target_mask) != 0)
618 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
620 if ((adw->user_wdtr & target_mask) != 0)
621 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
623 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
625 mc_sdtr = adw_get_user_sdtr(adw, ccb->ccb_h.target_id);
626 spi->sync_period = adw_find_period(adw, mc_sdtr);
627 if (spi->sync_period != 0)
628 spi->sync_offset = 15; /* XXX ??? */
630 spi->sync_offset = 0;
637 if ((adw_lram_read_16(adw, ADW_MC_DISC_ENABLE)
639 spi->flags |= CTS_SPI_FLAGS_DISC_ENB;
641 if ((adw->tagenb & target_mask) != 0)
642 scsi->flags |= CTS_SCSI_FLAGS_TAG_ENB;
645 adw_lram_read_16(adw,
646 ADW_MC_DEVICE_HSHK_CFG_TABLE
647 + (2 * ccb->ccb_h.target_id));
649 if ((targ_tinfo & ADW_HSHK_CFG_WIDE_XFR) != 0)
650 spi->bus_width = MSG_EXT_WDTR_BUS_16_BIT;
652 spi->bus_width = MSG_EXT_WDTR_BUS_8_BIT;
655 adw_hshk_cfg_period_factor(targ_tinfo);
657 spi->sync_offset = targ_tinfo & ADW_HSHK_CFG_OFFSET;
658 if (spi->sync_period == 0)
659 spi->sync_offset = 0;
661 if (spi->sync_offset == 0)
662 spi->sync_period = 0;
665 spi->valid = CTS_SPI_VALID_SYNC_RATE
666 | CTS_SPI_VALID_SYNC_OFFSET
667 | CTS_SPI_VALID_BUS_WIDTH
668 | CTS_SPI_VALID_DISC;
669 scsi->valid = CTS_SCSI_VALID_TQ;
670 ccb->ccb_h.status = CAM_REQ_CMP;
674 case XPT_CALC_GEOMETRY:
677 * XXX Use Adaptec translation until I find out how to
678 * get this information from the card.
680 cam_calc_geometry(&ccb->ccg, /*extended*/1);
684 case XPT_RESET_BUS: /* Reset the specified SCSI bus */
688 failure = adw_reset_bus(adw);
690 ccb->ccb_h.status = CAM_REQ_CMP_ERR;
693 xpt_print_path(adw->path);
694 printf("Bus Reset Delivered\n");
696 ccb->ccb_h.status = CAM_REQ_CMP;
701 case XPT_TERM_IO: /* Terminate the I/O process */
703 ccb->ccb_h.status = CAM_REQ_INVALID;
706 case XPT_PATH_INQ: /* Path routing inquiry */
708 struct ccb_pathinq *cpi = &ccb->cpi;
710 cpi->version_num = 1;
711 cpi->hba_inquiry = PI_WIDE_16|PI_SDTR_ABLE|PI_TAG_ABLE;
712 cpi->target_sprt = 0;
714 cpi->hba_eng_cnt = 0;
715 cpi->max_target = ADW_MAX_TID;
716 cpi->max_lun = ADW_MAX_LUN;
717 cpi->initiator_id = adw->initiator_id;
718 cpi->bus_id = cam_sim_bus(sim);
719 cpi->base_transfer_speed = 3300;
720 strncpy(cpi->sim_vid, "FreeBSD", SIM_IDLEN);
721 strncpy(cpi->hba_vid, "AdvanSys", HBA_IDLEN);
722 strncpy(cpi->dev_name, cam_sim_name(sim), DEV_IDLEN);
723 cpi->unit_number = cam_sim_unit(sim);
724 cpi->transport = XPORT_SPI;
725 cpi->transport_version = 2;
726 cpi->protocol = PROTO_SCSI;
727 cpi->protocol_version = SCSI_REV_2;
728 cpi->ccb_h.status = CAM_REQ_CMP;
733 ccb->ccb_h.status = CAM_REQ_INVALID;
740 adw_poll(struct cam_sim *sim)
742 adw_intr_locked(cam_sim_softc(sim));
746 adw_async(void *callback_arg, u_int32_t code, struct cam_path *path, void *arg)
751 adw_alloc(device_t dev, struct resource *regs, int regs_type, int regs_id)
753 struct adw_softc *adw;
755 adw = device_get_softc(dev);
756 LIST_INIT(&adw->pending_ccbs);
757 SLIST_INIT(&adw->sg_maps);
758 mtx_init(&adw->lock, "adw", NULL, MTX_DEF);
760 adw->regs_res_type = regs_type;
761 adw->regs_res_id = regs_id;
767 adw_free(struct adw_softc *adw)
769 switch (adw->init_level) {
772 struct sg_map_node *sg_map;
774 while ((sg_map = SLIST_FIRST(&adw->sg_maps)) != NULL) {
775 SLIST_REMOVE_HEAD(&adw->sg_maps, links);
776 bus_dmamap_unload(adw->sg_dmat,
778 bus_dmamem_free(adw->sg_dmat, sg_map->sg_vaddr,
780 free(sg_map, M_DEVBUF);
782 bus_dma_tag_destroy(adw->sg_dmat);
785 bus_dmamap_unload(adw->acb_dmat, adw->acb_dmamap);
787 bus_dmamem_free(adw->acb_dmat, adw->acbs,
789 bus_dmamap_destroy(adw->acb_dmat, adw->acb_dmamap);
791 bus_dma_tag_destroy(adw->acb_dmat);
793 bus_dmamap_unload(adw->carrier_dmat, adw->carrier_dmamap);
795 bus_dmamem_free(adw->carrier_dmat, adw->carriers,
796 adw->carrier_dmamap);
797 bus_dmamap_destroy(adw->carrier_dmat, adw->carrier_dmamap);
799 bus_dma_tag_destroy(adw->carrier_dmat);
801 bus_dma_tag_destroy(adw->buffer_dmat);
803 bus_dma_tag_destroy(adw->parent_dmat);
808 if (adw->regs != NULL)
809 bus_release_resource(adw->device,
814 if (adw->irq != NULL)
815 bus_release_resource(adw->device,
819 if (adw->sim != NULL) {
820 if (adw->path != NULL) {
821 xpt_async(AC_LOST_DEVICE, adw->path, NULL);
822 xpt_free_path(adw->path);
824 xpt_bus_deregister(cam_sim_path(adw->sim));
825 cam_sim_free(adw->sim, /*free_devq*/TRUE);
827 mtx_destroy(&adw->lock);
831 adw_init(struct adw_softc *adw)
833 struct adw_eeprom eep_config;
839 checksum = adw_eeprom_read(adw, &eep_config);
840 bcopy(eep_config.serial_number, adw->serial_number,
841 sizeof(adw->serial_number));
842 if (checksum != eep_config.checksum) {
843 u_int16_t serial_number[3];
845 adw->flags |= ADW_EEPROM_FAILED;
846 device_printf(adw->device,
847 "EEPROM checksum failed. Restoring Defaults\n");
850 * Restore the default EEPROM settings.
851 * Assume the 6 byte board serial number that was read
852 * from EEPROM is correct even if the EEPROM checksum
855 bcopy(adw->default_eeprom, &eep_config, sizeof(eep_config));
856 bcopy(adw->serial_number, eep_config.serial_number,
857 sizeof(serial_number));
858 adw_eeprom_write(adw, &eep_config);
861 /* Pull eeprom information into our softc. */
862 adw->bios_ctrl = eep_config.bios_ctrl;
863 adw->user_wdtr = eep_config.wdtr_able;
864 for (tid = 0; tid < ADW_MAX_TID; tid++) {
868 tid_mask = 0x1 << tid;
869 if ((adw->features & ADW_ULTRA) != 0) {
871 * Ultra chips store sdtr and ultraenb
872 * bits in their seeprom, so we must
873 * construct valid mc_sdtr entries for
876 if (eep_config.sync1.sync_enable & tid_mask) {
877 if (eep_config.sync2.ultra_enable & tid_mask)
878 mc_sdtr = ADW_MC_SDTR_20;
880 mc_sdtr = ADW_MC_SDTR_10;
882 mc_sdtr = ADW_MC_SDTR_ASYNC;
884 switch (ADW_TARGET_GROUP(tid)) {
886 mc_sdtr = eep_config.sync4.sdtr4;
889 mc_sdtr = eep_config.sync3.sdtr3;
892 mc_sdtr = eep_config.sync2.sdtr2;
894 default: /* Shut up compiler */
896 mc_sdtr = eep_config.sync1.sdtr1;
899 mc_sdtr >>= ADW_TARGET_GROUP_SHIFT(tid);
902 adw_set_user_sdtr(adw, tid, mc_sdtr);
904 adw->user_tagenb = eep_config.tagqng_able;
905 adw->user_discenb = eep_config.disc_enable;
906 adw->max_acbs = eep_config.max_host_qng;
907 adw->initiator_id = (eep_config.adapter_scsi_id & ADW_MAX_TID);
910 * Sanity check the number of host openings.
912 if (adw->max_acbs > ADW_DEF_MAX_HOST_QNG)
913 adw->max_acbs = ADW_DEF_MAX_HOST_QNG;
914 else if (adw->max_acbs < ADW_DEF_MIN_HOST_QNG) {
915 /* If the value is zero, assume it is uninitialized. */
916 if (adw->max_acbs == 0)
917 adw->max_acbs = ADW_DEF_MAX_HOST_QNG;
919 adw->max_acbs = ADW_DEF_MIN_HOST_QNG;
923 if ((adw->features & ADW_ULTRA2) != 0) {
924 switch (eep_config.termination_lvd) {
926 device_printf(adw->device,
927 "Invalid EEPROM LVD Termination Settings.\n");
928 device_printf(adw->device,
929 "Reverting to Automatic LVD Termination\n");
931 case ADW_EEPROM_TERM_AUTO:
933 case ADW_EEPROM_TERM_BOTH_ON:
934 scsicfg1 |= ADW2_SCSI_CFG1_TERM_LVD_LO;
936 case ADW_EEPROM_TERM_HIGH_ON:
937 scsicfg1 |= ADW2_SCSI_CFG1_TERM_LVD_HI;
939 case ADW_EEPROM_TERM_OFF:
940 scsicfg1 |= ADW2_SCSI_CFG1_DIS_TERM_DRV;
945 switch (eep_config.termination_se) {
947 device_printf(adw->device,
948 "Invalid SE EEPROM Termination Settings.\n");
949 device_printf(adw->device,
950 "Reverting to Automatic SE Termination\n");
952 case ADW_EEPROM_TERM_AUTO:
954 case ADW_EEPROM_TERM_BOTH_ON:
955 scsicfg1 |= ADW_SCSI_CFG1_TERM_CTL_L;
957 case ADW_EEPROM_TERM_HIGH_ON:
958 scsicfg1 |= ADW_SCSI_CFG1_TERM_CTL_H;
960 case ADW_EEPROM_TERM_OFF:
961 scsicfg1 |= ADW_SCSI_CFG1_TERM_CTL_MANUAL;
964 device_printf(adw->device, "SCSI ID %d, ", adw->initiator_id);
966 /* DMA tag for mapping buffers into device visible space. */
967 if (bus_dma_tag_create(
968 /* parent */ adw->parent_dmat,
971 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
972 /* highaddr */ BUS_SPACE_MAXADDR,
974 /* filterarg */ NULL,
975 /* maxsize */ MAXBSIZE,
976 /* nsegments */ ADW_SGSIZE,
977 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
978 /* flags */ BUS_DMA_ALLOCNOW,
979 /* lockfunc */ busdma_lock_mutex,
980 /* lockarg */ &adw->lock,
981 &adw->buffer_dmat) != 0) {
987 /* DMA tag for our ccb carrier structures */
988 if (bus_dma_tag_create(
989 /* parent */ adw->parent_dmat,
990 /* alignment */ 0x10,
992 /* lowaddr */ BUS_SPACE_MAXADDR_32BIT,
993 /* highaddr */ BUS_SPACE_MAXADDR,
995 /* filterarg */ NULL,
996 /* maxsize */ (adw->max_acbs +
997 ADW_NUM_CARRIER_QUEUES + 1) *
998 sizeof(struct adw_carrier),
1000 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
1002 /* lockfunc */ NULL,
1004 &adw->carrier_dmat) != 0) {
1010 /* Allocation for our ccb carrier structures */
1011 if (bus_dmamem_alloc(adw->carrier_dmat, (void **)&adw->carriers,
1012 BUS_DMA_NOWAIT, &adw->carrier_dmamap) != 0) {
1018 /* And permanently map them */
1019 bus_dmamap_load(adw->carrier_dmat, adw->carrier_dmamap,
1021 (adw->max_acbs + ADW_NUM_CARRIER_QUEUES + 1)
1022 * sizeof(struct adw_carrier),
1023 adwmapmem, &adw->carrier_busbase, /*flags*/0);
1025 /* Clear them out. */
1026 bzero(adw->carriers, (adw->max_acbs + ADW_NUM_CARRIER_QUEUES + 1)
1027 * sizeof(struct adw_carrier));
1029 /* Setup our free carrier list */
1030 adw->free_carriers = adw->carriers;
1031 for (i = 0; i < adw->max_acbs + ADW_NUM_CARRIER_QUEUES; i++) {
1032 adw->carriers[i].carr_offset =
1033 carriervtobo(adw, &adw->carriers[i]);
1034 adw->carriers[i].carr_ba =
1035 carriervtob(adw, &adw->carriers[i]);
1036 adw->carriers[i].areq_ba = 0;
1037 adw->carriers[i].next_ba =
1038 carriervtobo(adw, &adw->carriers[i+1]);
1040 /* Terminal carrier. Never leaves the freelist */
1041 adw->carriers[i].carr_offset =
1042 carriervtobo(adw, &adw->carriers[i]);
1043 adw->carriers[i].carr_ba =
1044 carriervtob(adw, &adw->carriers[i]);
1045 adw->carriers[i].areq_ba = 0;
1046 adw->carriers[i].next_ba = ~0;
1050 /* DMA tag for our acb structures */
1051 if (bus_dma_tag_create(
1052 /* parent */ adw->parent_dmat,
1055 /* lowaddr */ BUS_SPACE_MAXADDR,
1056 /* highaddr */ BUS_SPACE_MAXADDR,
1058 /* filterarg */ NULL,
1059 /* maxsize */ adw->max_acbs * sizeof(struct acb),
1061 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
1063 /* lockfunc */ NULL,
1065 &adw->acb_dmat) != 0) {
1071 /* Allocation for our ccbs */
1072 if (bus_dmamem_alloc(adw->acb_dmat, (void **)&adw->acbs,
1073 BUS_DMA_NOWAIT, &adw->acb_dmamap) != 0)
1078 /* And permanently map them */
1079 bus_dmamap_load(adw->acb_dmat, adw->acb_dmamap,
1081 adw->max_acbs * sizeof(struct acb),
1082 adwmapmem, &adw->acb_busbase, /*flags*/0);
1084 /* Clear them out. */
1085 bzero(adw->acbs, adw->max_acbs * sizeof(struct acb));
1087 /* DMA tag for our S/G structures. We allocate in page sized chunks */
1088 if (bus_dma_tag_create(
1089 /* parent */ adw->parent_dmat,
1092 /* lowaddr */ BUS_SPACE_MAXADDR,
1093 /* highaddr */ BUS_SPACE_MAXADDR,
1095 /* filterarg */ NULL,
1096 /* maxsize */ PAGE_SIZE,
1098 /* maxsegsz */ BUS_SPACE_MAXSIZE_32BIT,
1100 /* lockfunc */ NULL,
1102 &adw->sg_dmat) != 0) {
1108 /* Allocate our first batch of ccbs */
1109 mtx_lock(&adw->lock);
1110 if (adwallocacbs(adw) == 0) {
1111 mtx_unlock(&adw->lock);
1115 if (adw_init_chip(adw, scsicfg1) != 0) {
1116 mtx_unlock(&adw->lock);
1120 printf("Queue Depth %d\n", adw->max_acbs);
1121 mtx_unlock(&adw->lock);
1127 * Attach all the sub-devices we can find
1130 adw_attach(struct adw_softc *adw)
1132 struct ccb_setasync csa;
1133 struct cam_devq *devq;
1136 /* Hook up our interrupt handler */
1137 error = bus_setup_intr(adw->device, adw->irq,
1138 INTR_TYPE_CAM | INTR_ENTROPY | INTR_MPSAFE, NULL, adw_intr, adw,
1141 device_printf(adw->device, "bus_setup_intr() failed: %d\n",
1146 /* Start the Risc processor now that we are fully configured. */
1147 adw_outw(adw, ADW_RISC_CSR, ADW_RISC_CSR_RUN);
1150 * Create the device queue for our SIM.
1152 devq = cam_simq_alloc(adw->max_acbs);
1157 * Construct our SIM entry.
1159 adw->sim = cam_sim_alloc(adw_action, adw_poll, "adw", adw,
1160 device_get_unit(adw->device), &adw->lock, 1, adw->max_acbs, devq);
1161 if (adw->sim == NULL)
1167 mtx_lock(&adw->lock);
1168 if (xpt_bus_register(adw->sim, adw->device, 0) != CAM_SUCCESS) {
1169 cam_sim_free(adw->sim, /*free devq*/TRUE);
1174 if (xpt_create_path(&adw->path, /*periph*/NULL, cam_sim_path(adw->sim),
1175 CAM_TARGET_WILDCARD, CAM_LUN_WILDCARD)
1177 xpt_setup_ccb(&csa.ccb_h, adw->path, /*priority*/5);
1178 csa.ccb_h.func_code = XPT_SASYNC_CB;
1179 csa.event_enable = AC_LOST_DEVICE;
1180 csa.callback = adw_async;
1181 csa.callback_arg = adw;
1182 xpt_action((union ccb *)&csa);
1186 mtx_unlock(&adw->lock);
1193 struct adw_softc *adw;
1196 mtx_lock(&adw->lock);
1197 adw_intr_locked(adw);
1198 mtx_unlock(&adw->lock);
1202 adw_intr_locked(struct adw_softc *adw)
1206 if ((adw_inw(adw, ADW_CTRL_REG) & ADW_CTRL_REG_HOST_INTR) == 0)
1209 /* Reading the register clears the interrupt. */
1210 int_stat = adw_inb(adw, ADW_INTR_STATUS_REG);
1212 if ((int_stat & ADW_INTR_STATUS_INTRB) != 0) {
1215 /* Async Microcode Event */
1216 intrb_code = adw_lram_read_8(adw, ADW_MC_INTRB_CODE);
1217 switch (intrb_code) {
1218 case ADW_ASYNC_CARRIER_READY_FAILURE:
1220 * The RISC missed our update of
1223 if (LIST_FIRST(&adw->pending_ccbs) != NULL)
1224 adw_tickle_risc(adw, ADW_TICKLE_A);
1226 case ADW_ASYNC_SCSI_BUS_RESET_DET:
1228 * The firmware detected a SCSI Bus reset.
1230 device_printf(adw->device, "Someone Reset the Bus\n");
1231 adw_handle_bus_reset(adw, /*initiated*/FALSE);
1233 case ADW_ASYNC_RDMA_FAILURE:
1235 * Handle RDMA failure by resetting the
1236 * SCSI Bus and chip.
1239 AdvResetChipAndSB(adv_dvc_varp);
1243 case ADW_ASYNC_HOST_SCSI_BUS_RESET:
1245 * Host generated SCSI bus reset occurred.
1247 adw_handle_bus_reset(adw, /*initiated*/TRUE);
1250 printf("adw_intr: unknown async code 0x%x\n",
1257 * Run down the RequestQ.
1259 while ((adw->responseq->next_ba & ADW_RQ_DONE) != 0) {
1260 struct adw_carrier *free_carrier;
1265 printf("0x%x, 0x%x, 0x%x, 0x%x\n",
1266 adw->responseq->carr_offset,
1267 adw->responseq->carr_ba,
1268 adw->responseq->areq_ba,
1269 adw->responseq->next_ba);
1272 * The firmware copies the adw_scsi_req_q.acb_baddr
1273 * field into the areq_ba field of the carrier.
1275 acb = acbbotov(adw, adw->responseq->areq_ba);
1278 * The least significant four bits of the next_ba
1279 * field are used as flags. Mask them out and then
1280 * advance through the list.
1282 free_carrier = adw->responseq;
1284 carrierbotov(adw, free_carrier->next_ba & ADW_NEXT_BA_MASK);
1285 free_carrier->next_ba = adw->free_carriers->carr_offset;
1286 adw->free_carriers = free_carrier;
1290 callout_stop(&acb->timer);
1291 if ((ccb->ccb_h.flags & CAM_DIR_MASK) != CAM_DIR_NONE) {
1292 bus_dmasync_op_t op;
1294 if ((ccb->ccb_h.flags & CAM_DIR_MASK) == CAM_DIR_IN)
1295 op = BUS_DMASYNC_POSTREAD;
1297 op = BUS_DMASYNC_POSTWRITE;
1298 bus_dmamap_sync(adw->buffer_dmat, acb->dmamap, op);
1299 bus_dmamap_unload(adw->buffer_dmat, acb->dmamap);
1300 ccb->csio.resid = acb->queue.data_cnt;
1302 ccb->csio.resid = 0;
1304 /* Common Cases inline... */
1305 if (acb->queue.host_status == QHSTA_NO_ERROR
1306 && (acb->queue.done_status == QD_NO_ERROR
1307 || acb->queue.done_status == QD_WITH_ERROR)) {
1308 ccb->csio.scsi_status = acb->queue.scsi_status;
1309 ccb->ccb_h.status = 0;
1310 switch (ccb->csio.scsi_status) {
1311 case SCSI_STATUS_OK:
1312 ccb->ccb_h.status |= CAM_REQ_CMP;
1314 case SCSI_STATUS_CHECK_COND:
1315 case SCSI_STATUS_CMD_TERMINATED:
1316 bcopy(&acb->sense_data, &ccb->csio.sense_data,
1317 ccb->csio.sense_len);
1318 ccb->ccb_h.status |= CAM_AUTOSNS_VALID;
1319 ccb->csio.sense_resid = acb->queue.sense_len;
1322 ccb->ccb_h.status |= CAM_SCSI_STATUS_ERROR
1324 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1327 adwfreeacb(adw, acb);
1330 adwprocesserror(adw, acb);
1336 adwprocesserror(struct adw_softc *adw, struct acb *acb)
1341 if (acb->queue.done_status == QD_ABORTED_BY_HOST) {
1342 ccb->ccb_h.status = CAM_REQ_ABORTED;
1345 switch (acb->queue.host_status) {
1346 case QHSTA_M_SEL_TIMEOUT:
1347 ccb->ccb_h.status = CAM_SEL_TIMEOUT;
1349 case QHSTA_M_SXFR_OFF_UFLW:
1350 case QHSTA_M_SXFR_OFF_OFLW:
1351 case QHSTA_M_DATA_OVER_RUN:
1352 ccb->ccb_h.status = CAM_DATA_RUN_ERR;
1354 case QHSTA_M_SXFR_DESELECTED:
1355 case QHSTA_M_UNEXPECTED_BUS_FREE:
1356 ccb->ccb_h.status = CAM_UNEXP_BUSFREE;
1358 case QHSTA_M_SCSI_BUS_RESET:
1359 case QHSTA_M_SCSI_BUS_RESET_UNSOL:
1360 ccb->ccb_h.status = CAM_SCSI_BUS_RESET;
1362 case QHSTA_M_BUS_DEVICE_RESET:
1363 ccb->ccb_h.status = CAM_BDR_SENT;
1365 case QHSTA_M_QUEUE_ABORTED:
1366 /* BDR or Bus Reset */
1367 xpt_print_path(adw->path);
1368 printf("Saw Queue Aborted\n");
1369 ccb->ccb_h.status = adw->last_reset;
1371 case QHSTA_M_SXFR_SDMA_ERR:
1372 case QHSTA_M_SXFR_SXFR_PERR:
1373 case QHSTA_M_RDMA_PERR:
1374 ccb->ccb_h.status = CAM_UNCOR_PARITY;
1376 case QHSTA_M_WTM_TIMEOUT:
1377 case QHSTA_M_SXFR_WD_TMO:
1379 /* The SCSI bus hung in a phase */
1380 xpt_print_path(adw->path);
1381 printf("Watch Dog timer expired. Resetting bus\n");
1385 case QHSTA_M_SXFR_XFR_PH_ERR:
1386 ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
1388 case QHSTA_M_SXFR_UNKNOWN_ERROR:
1390 case QHSTA_M_BAD_CMPL_STATUS_IN:
1391 /* No command complete after a status message */
1392 ccb->ccb_h.status = CAM_SEQUENCE_FAIL;
1394 case QHSTA_M_AUTO_REQ_SENSE_FAIL:
1395 ccb->ccb_h.status = CAM_AUTOSENSE_FAIL;
1397 case QHSTA_M_INVALID_DEVICE:
1398 ccb->ccb_h.status = CAM_PATH_INVALID;
1400 case QHSTA_M_NO_AUTO_REQ_SENSE:
1402 * User didn't request sense, but we got a
1405 ccb->csio.scsi_status = acb->queue.scsi_status;
1406 ccb->ccb_h.status = CAM_SCSI_STATUS_ERROR;
1409 panic("%s: Unhandled Host status error %x",
1410 device_get_nameunit(adw->device),
1411 acb->queue.host_status);
1415 if ((acb->state & ACB_RECOVERY_ACB) != 0) {
1416 if (ccb->ccb_h.status == CAM_SCSI_BUS_RESET
1417 || ccb->ccb_h.status == CAM_BDR_SENT)
1418 ccb->ccb_h.status = CAM_CMD_TIMEOUT;
1420 if (ccb->ccb_h.status != CAM_REQ_CMP) {
1421 xpt_freeze_devq(ccb->ccb_h.path, /*count*/1);
1422 ccb->ccb_h.status |= CAM_DEV_QFRZN;
1424 adwfreeacb(adw, acb);
1429 adwtimeout(void *arg)
1433 struct adw_softc *adw;
1434 adw_idle_cmd_status_t status;
1437 acb = (struct acb *)arg;
1439 adw = (struct adw_softc *)ccb->ccb_h.ccb_adw_ptr;
1440 xpt_print_path(ccb->ccb_h.path);
1441 printf("ACB %p - timed out\n", (void *)acb);
1443 mtx_assert(&adw->lock, MA_OWNED);
1445 if ((acb->state & ACB_ACTIVE) == 0) {
1446 xpt_print_path(ccb->ccb_h.path);
1447 printf("ACB %p - timed out CCB already completed\n",
1452 acb->state |= ACB_RECOVERY_ACB;
1453 target_id = ccb->ccb_h.target_id;
1455 /* Attempt a BDR first */
1456 status = adw_idle_cmd_send(adw, ADW_IDLE_CMD_DEVICE_RESET,
1457 ccb->ccb_h.target_id);
1458 if (status == ADW_IDLE_CMD_SUCCESS) {
1459 device_printf(adw->device,
1460 "BDR Delivered. No longer in timeout\n");
1461 adw_handle_device_reset(adw, target_id);
1464 xpt_print_path(adw->path);
1465 printf("Bus Reset Delivered. No longer in timeout\n");
1470 adw_handle_device_reset(struct adw_softc *adw, u_int target)
1472 struct cam_path *path;
1475 error = xpt_create_path(&path, /*periph*/NULL, cam_sim_path(adw->sim),
1476 target, CAM_LUN_WILDCARD);
1478 if (error == CAM_REQ_CMP) {
1479 xpt_async(AC_SENT_BDR, path, NULL);
1480 xpt_free_path(path);
1482 adw->last_reset = CAM_BDR_SENT;
1486 adw_handle_bus_reset(struct adw_softc *adw, int initiated)
1490 * The microcode currently sets the SCSI Bus Reset signal
1491 * while handling the AscSendIdleCmd() IDLE_CMD_SCSI_RESET
1492 * command above. But the SCSI Bus Reset Hold Time in the
1493 * microcode is not deterministic (it may in fact be for less
1494 * than the SCSI Spec. minimum of 25 us). Therefore on return
1495 * the Adv Library sets the SCSI Bus Reset signal for
1496 * ADW_SCSI_RESET_HOLD_TIME_US, which is defined to be greater
1501 scsi_ctrl = adw_inw(adw, ADW_SCSI_CTRL) & ~ADW_SCSI_CTRL_RSTOUT;
1502 adw_outw(adw, ADW_SCSI_CTRL, scsi_ctrl | ADW_SCSI_CTRL_RSTOUT);
1503 DELAY(ADW_SCSI_RESET_HOLD_TIME_US);
1504 adw_outw(adw, ADW_SCSI_CTRL, scsi_ctrl);
1507 * We will perform the async notification when the
1508 * SCSI Reset interrupt occurs.
1511 xpt_async(AC_BUS_RESET, adw->path, NULL);
1512 adw->last_reset = CAM_SCSI_BUS_RESET;
1514 MODULE_DEPEND(adw, cam, 1, 1, 1);