aic7xxx_core.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:189k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  1. /*
  2.  * Core routines and tables shareable across OS platforms.
  3.  *
  4.  * Copyright (c) 1994-2001 Justin T. Gibbs.
  5.  * Copyright (c) 2000-2001 Adaptec Inc.
  6.  * All rights reserved.
  7.  *
  8.  * Redistribution and use in source and binary forms, with or without
  9.  * modification, are permitted provided that the following conditions
  10.  * are met:
  11.  * 1. Redistributions of source code must retain the above copyright
  12.  *    notice, this list of conditions, and the following disclaimer,
  13.  *    without modification.
  14.  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
  15.  *    substantially similar to the "NO WARRANTY" disclaimer below
  16.  *    ("Disclaimer") and any redistribution must be conditioned upon
  17.  *    including a substantially similar Disclaimer requirement for further
  18.  *    binary redistribution.
  19.  * 3. Neither the names of the above-listed copyright holders nor the names
  20.  *    of any contributors may be used to endorse or promote products derived
  21.  *    from this software without specific prior written permission.
  22.  *
  23.  * Alternatively, this software may be distributed under the terms of the
  24.  * GNU General Public License ("GPL") version 2 as published by the Free
  25.  * Software Foundation.
  26.  *
  27.  * NO WARRANTY
  28.  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  29.  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  30.  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
  31.  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  32.  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  33.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  34.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  36.  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  37.  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38.  * POSSIBILITY OF SUCH DAMAGES.
  39.  *
  40.  * $Id: //depot/aic7xxx/aic7xxx/aic7xxx.c#69 $
  41.  *
  42.  * $FreeBSD: src/sys/dev/aic7xxx/aic7xxx.c,v 1.41.2.22 2002/04/29 19:36:26 gibbs Exp $
  43.  */
  44. #ifdef __linux__
  45. #include "aic7xxx_osm.h"
  46. #include "aic7xxx_inline.h"
  47. #include "aicasm/aicasm_insformat.h"
  48. #else
  49. #include <dev/aic7xxx/aic7xxx_osm.h>
  50. #include <dev/aic7xxx/aic7xxx_inline.h>
  51. #include <dev/aic7xxx/aicasm/aicasm_insformat.h>
  52. #endif
  53. /****************************** Softc Data ************************************/
  54. struct ahc_softc_tailq ahc_tailq = TAILQ_HEAD_INITIALIZER(ahc_tailq);
  55. /***************************** Lookup Tables **********************************/
  56. char *ahc_chip_names[] =
  57. {
  58. "NONE",
  59. "aic7770",
  60. "aic7850",
  61. "aic7855",
  62. "aic7859",
  63. "aic7860",
  64. "aic7870",
  65. "aic7880",
  66. "aic7895",
  67. "aic7895C",
  68. "aic7890/91",
  69. "aic7896/97",
  70. "aic7892",
  71. "aic7899"
  72. };
  73. static const u_int num_chip_names = NUM_ELEMENTS(ahc_chip_names);
  74. /*
  75.  * Hardware error codes.
  76.  */
  77. struct ahc_hard_error_entry {
  78.         uint8_t errno;
  79. char *errmesg;
  80. };
  81. static struct ahc_hard_error_entry ahc_hard_errors[] = {
  82. { ILLHADDR, "Illegal Host Access" },
  83. { ILLSADDR, "Illegal Sequencer Address referrenced" },
  84. { ILLOPCODE, "Illegal Opcode in sequencer program" },
  85. { SQPARERR, "Sequencer Parity Error" },
  86. { DPARERR, "Data-path Parity Error" },
  87. { MPARERR, "Scratch or SCB Memory Parity Error" },
  88. { PCIERRSTAT, "PCI Error detected" },
  89. { CIOPARERR, "CIOBUS Parity Error" },
  90. };
  91. static const u_int num_errors = NUM_ELEMENTS(ahc_hard_errors);
  92. static struct ahc_phase_table_entry ahc_phase_table[] =
  93. {
  94. { P_DATAOUT, MSG_NOOP, "in Data-out phase" },
  95. { P_DATAIN, MSG_INITIATOR_DET_ERR, "in Data-in phase" },
  96. { P_DATAOUT_DT, MSG_NOOP, "in DT Data-out phase" },
  97. { P_DATAIN_DT, MSG_INITIATOR_DET_ERR, "in DT Data-in phase" },
  98. { P_COMMAND, MSG_NOOP, "in Command phase" },
  99. { P_MESGOUT, MSG_NOOP, "in Message-out phase" },
  100. { P_STATUS, MSG_INITIATOR_DET_ERR, "in Status phase" },
  101. { P_MESGIN, MSG_PARITY_ERROR, "in Message-in phase" },
  102. { P_BUSFREE, MSG_NOOP, "while idle" },
  103. { 0, MSG_NOOP, "in unknown phase" }
  104. };
  105. /*
  106.  * In most cases we only wish to itterate over real phases, so
  107.  * exclude the last element from the count.
  108.  */
  109. static const u_int num_phases = NUM_ELEMENTS(ahc_phase_table) - 1;
  110. /*
  111.  * Valid SCSIRATE values.  (p. 3-17)
  112.  * Provides a mapping of tranfer periods in ns to the proper value to
  113.  * stick in the scsixfer reg.
  114.  */
  115. static struct ahc_syncrate ahc_syncrates[] =
  116. {
  117.       /* ultra2    fast/ultra  period     rate */
  118. { 0x42,      0x000,      9,      "80.0" },
  119. { 0x03,      0x000,     10,      "40.0" },
  120. { 0x04,      0x000,     11,      "33.0" },
  121. { 0x05,      0x100,     12,      "20.0" },
  122. { 0x06,      0x110,     15,      "16.0" },
  123. { 0x07,      0x120,     18,      "13.4" },
  124. { 0x08,      0x000,     25,      "10.0" },
  125. { 0x19,      0x010,     31,      "8.0"  },
  126. { 0x1a,      0x020,     37,      "6.67" },
  127. { 0x1b,      0x030,     43,      "5.7"  },
  128. { 0x1c,      0x040,     50,      "5.0"  },
  129. { 0x00,      0x050,     56,      "4.4"  },
  130. { 0x00,      0x060,     62,      "4.0"  },
  131. { 0x00,      0x070,     68,      "3.6"  },
  132. { 0x00,      0x000,      0,      NULL   }
  133. };
  134. /* Our Sequencer Program */
  135. #include "aic7xxx_seq.h"
  136. /**************************** Function Declarations ***************************/
  137. static void ahc_force_renegotiation(struct ahc_softc *ahc);
  138. static struct ahc_tmode_tstate*
  139. ahc_alloc_tstate(struct ahc_softc *ahc,
  140.  u_int scsi_id, char channel);
  141. #ifdef AHC_TARGET_MODE
  142. static void ahc_free_tstate(struct ahc_softc *ahc,
  143. u_int scsi_id, char channel, int force);
  144. #endif
  145. static struct ahc_syncrate*
  146. ahc_devlimited_syncrate(struct ahc_softc *ahc,
  147.         struct ahc_initiator_tinfo *,
  148. u_int *period,
  149. u_int *ppr_options,
  150. role_t role);
  151. static void ahc_update_pending_scbs(struct ahc_softc *ahc);
  152. static void ahc_fetch_devinfo(struct ahc_softc *ahc,
  153.   struct ahc_devinfo *devinfo);
  154. static void ahc_scb_devinfo(struct ahc_softc *ahc,
  155. struct ahc_devinfo *devinfo,
  156. struct scb *scb);
  157. static void ahc_assert_atn(struct ahc_softc *ahc);
  158. static void ahc_setup_initiator_msgout(struct ahc_softc *ahc,
  159.    struct ahc_devinfo *devinfo,
  160.    struct scb *scb);
  161. static void ahc_build_transfer_msg(struct ahc_softc *ahc,
  162.        struct ahc_devinfo *devinfo);
  163. static void ahc_construct_sdtr(struct ahc_softc *ahc,
  164.    struct ahc_devinfo *devinfo,
  165.    u_int period, u_int offset);
  166. static void ahc_construct_wdtr(struct ahc_softc *ahc,
  167.    struct ahc_devinfo *devinfo,
  168.    u_int bus_width);
  169. static void ahc_construct_ppr(struct ahc_softc *ahc,
  170.   struct ahc_devinfo *devinfo,
  171.   u_int period, u_int offset,
  172.   u_int bus_width, u_int ppr_options);
  173. static void ahc_clear_msg_state(struct ahc_softc *ahc);
  174. static void ahc_handle_message_phase(struct ahc_softc *ahc);
  175. typedef enum {
  176. AHCMSG_1B,
  177. AHCMSG_2B,
  178. AHCMSG_EXT
  179. } ahc_msgtype;
  180. static int ahc_sent_msg(struct ahc_softc *ahc, ahc_msgtype type,
  181.      u_int msgval, int full);
  182. static int ahc_parse_msg(struct ahc_softc *ahc,
  183.       struct ahc_devinfo *devinfo);
  184. static int ahc_handle_msg_reject(struct ahc_softc *ahc,
  185.       struct ahc_devinfo *devinfo);
  186. static void ahc_handle_ign_wide_residue(struct ahc_softc *ahc,
  187. struct ahc_devinfo *devinfo);
  188. static void ahc_reinitialize_dataptrs(struct ahc_softc *ahc);
  189. static void ahc_handle_devreset(struct ahc_softc *ahc,
  190.     struct ahc_devinfo *devinfo,
  191.     cam_status status, char *message,
  192.     int verbose_level);
  193. #if AHC_TARGET_MODE
  194. static void ahc_setup_target_msgin(struct ahc_softc *ahc,
  195.        struct ahc_devinfo *devinfo,
  196.        struct scb *scb);
  197. #endif
  198. static bus_dmamap_callback_t ahc_dmamap_cb; 
  199. static void ahc_build_free_scb_list(struct ahc_softc *ahc);
  200. static int ahc_init_scbdata(struct ahc_softc *ahc);
  201. static void ahc_fini_scbdata(struct ahc_softc *ahc);
  202. static void ahc_qinfifo_requeue(struct ahc_softc *ahc,
  203.     struct scb *prev_scb,
  204.     struct scb *scb);
  205. static int ahc_qinfifo_count(struct ahc_softc *ahc);
  206. static u_int ahc_rem_scb_from_disc_list(struct ahc_softc *ahc,
  207.    u_int prev, u_int scbptr);
  208. static void ahc_add_curscb_to_free_list(struct ahc_softc *ahc);
  209. static u_int ahc_rem_wscb(struct ahc_softc *ahc,
  210.      u_int scbpos, u_int prev);
  211. static void ahc_reset_current_bus(struct ahc_softc *ahc);
  212. #ifdef AHC_DUMP_SEQ
  213. static void ahc_dumpseq(struct ahc_softc *ahc);
  214. #endif
  215. static void ahc_loadseq(struct ahc_softc *ahc);
  216. static int ahc_check_patch(struct ahc_softc *ahc,
  217. struct patch **start_patch,
  218. u_int start_instr, u_int *skip_addr);
  219. static void ahc_download_instr(struct ahc_softc *ahc,
  220.    u_int instrptr, uint8_t *dconsts);
  221. #ifdef AHC_TARGET_MODE
  222. static void ahc_queue_lstate_event(struct ahc_softc *ahc,
  223.        struct ahc_tmode_lstate *lstate,
  224.        u_int initiator_id,
  225.        u_int event_type,
  226.        u_int event_arg);
  227. static void ahc_update_scsiid(struct ahc_softc *ahc,
  228.   u_int targid_mask);
  229. static int ahc_handle_target_cmd(struct ahc_softc *ahc,
  230.       struct target_cmd *cmd);
  231. #endif
  232. /************************* Sequencer Execution Control ************************/
  233. /*
  234.  * Restart the sequencer program from address zero
  235.  */
  236. void
  237. ahc_restart(struct ahc_softc *ahc)
  238. {
  239. ahc_pause(ahc);
  240. /* No more pending messages. */
  241. ahc_clear_msg_state(ahc);
  242. ahc_outb(ahc, SCSISIGO, 0); /* De-assert BSY */
  243. ahc_outb(ahc, MSG_OUT, MSG_NOOP); /* No message to send */
  244. ahc_outb(ahc, SXFRCTL1, ahc_inb(ahc, SXFRCTL1) & ~BITBUCKET);
  245. ahc_outb(ahc, LASTPHASE, P_BUSFREE);
  246. ahc_outb(ahc, SAVED_SCSIID, 0xFF);
  247. ahc_outb(ahc, SAVED_LUN, 0xFF);
  248. /*
  249.  * Ensure that the sequencer's idea of TQINPOS
  250.  * matches our own.  The sequencer increments TQINPOS
  251.  * only after it sees a DMA complete and a reset could
  252.  * occur before the increment leaving the kernel to believe
  253.  * the command arrived but the sequencer to not.
  254.  */
  255. ahc_outb(ahc, TQINPOS, ahc->tqinfifonext);
  256. /* Always allow reselection */
  257. ahc_outb(ahc, SCSISEQ,
  258.  ahc_inb(ahc, SCSISEQ_TEMPLATE) & (ENSELI|ENRSELI|ENAUTOATNP));
  259. if ((ahc->features & AHC_CMD_CHAN) != 0) {
  260. /* Ensure that no DMA operations are in progress */
  261. ahc_outb(ahc, CCSCBCNT, 0);
  262. ahc_outb(ahc, CCSGCTL, 0);
  263. ahc_outb(ahc, CCSCBCTL, 0);
  264. }
  265. /*
  266.  * If we were in the process of DMA'ing SCB data into
  267.  * an SCB, replace that SCB on the free list.  This prevents
  268.  * an SCB leak.
  269.  */
  270. if ((ahc_inb(ahc, SEQ_FLAGS2) & SCB_DMA) != 0) {
  271. ahc_add_curscb_to_free_list(ahc);
  272. ahc_outb(ahc, SEQ_FLAGS2,
  273.  ahc_inb(ahc, SEQ_FLAGS2) & ~SCB_DMA);
  274. }
  275. ahc_outb(ahc, MWI_RESIDUAL, 0);
  276. ahc_outb(ahc, SEQCTL, FASTMODE);
  277. ahc_outb(ahc, SEQADDR0, 0);
  278. ahc_outb(ahc, SEQADDR1, 0);
  279. ahc_unpause(ahc);
  280. }
  281. /************************* Input/Output Queues ********************************/
  282. void
  283. ahc_run_qoutfifo(struct ahc_softc *ahc)
  284. {
  285. struct scb *scb;
  286. u_int  scb_index;
  287. ahc_sync_qoutfifo(ahc, BUS_DMASYNC_POSTREAD);
  288. while (ahc->qoutfifo[ahc->qoutfifonext] != SCB_LIST_NULL) {
  289. scb_index = ahc->qoutfifo[ahc->qoutfifonext];
  290. if ((ahc->qoutfifonext & 0x03) == 0x03) {
  291. u_int modnext;
  292. /*
  293.  * Clear 32bits of QOUTFIFO at a time
  294.  * so that we don't clobber an incoming
  295.  * byte DMA to the array on architectures
  296.  * that only support 32bit load and store
  297.  * operations.
  298.  */
  299. modnext = ahc->qoutfifonext & ~0x3;
  300. *((uint32_t *)(&ahc->qoutfifo[modnext])) = 0xFFFFFFFFUL;
  301. ahc_dmamap_sync(ahc, ahc->shared_data_dmat,
  302. ahc->shared_data_dmamap,
  303. /*offset*/modnext, /*len*/4,
  304. BUS_DMASYNC_PREREAD);
  305. }
  306. ahc->qoutfifonext++;
  307. scb = ahc_lookup_scb(ahc, scb_index);
  308. if (scb == NULL) {
  309. printf("%s: WARNING no command for scb %d "
  310.        "(cmdcmplt)nQOUTPOS = %dn",
  311.        ahc_name(ahc), scb_index,
  312.        ahc->qoutfifonext - 1);
  313. continue;
  314. }
  315. /*
  316.  * Save off the residual
  317.  * if there is one.
  318.  */
  319. ahc_update_residual(ahc, scb);
  320. ahc_done(ahc, scb);
  321. }
  322. }
  323. void
  324. ahc_run_untagged_queues(struct ahc_softc *ahc)
  325. {
  326. int i;
  327. for (i = 0; i < 16; i++)
  328. ahc_run_untagged_queue(ahc, &ahc->untagged_queues[i]);
  329. }
  330. void
  331. ahc_run_untagged_queue(struct ahc_softc *ahc, struct scb_tailq *queue)
  332. {
  333. struct scb *scb;
  334. if (ahc->untagged_queue_lock != 0)
  335. return;
  336. if ((scb = TAILQ_FIRST(queue)) != NULL
  337.  && (scb->flags & SCB_ACTIVE) == 0) {
  338. scb->flags |= SCB_ACTIVE;
  339. ahc_queue_scb(ahc, scb);
  340. }
  341. }
  342. /************************* Interrupt Handling *********************************/
  343. void
  344. ahc_handle_brkadrint(struct ahc_softc *ahc)
  345. {
  346. /*
  347.  * We upset the sequencer :-(
  348.  * Lookup the error message
  349.  */
  350. int i;
  351. int error;
  352. error = ahc_inb(ahc, ERROR);
  353. for (i = 0; error != 1 && i < num_errors; i++)
  354. error >>= 1;
  355. printf("%s: brkadrint, %s at seqaddr = 0x%xn",
  356.        ahc_name(ahc), ahc_hard_errors[i].errmesg,
  357.        ahc_inb(ahc, SEQADDR0) |
  358.        (ahc_inb(ahc, SEQADDR1) << 8));
  359. ahc_dump_card_state(ahc);
  360. /* Tell everyone that this HBA is no longer availible */
  361. ahc_abort_scbs(ahc, CAM_TARGET_WILDCARD, ALL_CHANNELS,
  362.        CAM_LUN_WILDCARD, SCB_LIST_NULL, ROLE_UNKNOWN,
  363.        CAM_NO_HBA);
  364. /* Disable all interrupt sources by resetting the controller */
  365. ahc_shutdown(ahc);
  366. }
  367. void
  368. ahc_handle_seqint(struct ahc_softc *ahc, u_int intstat)
  369. {
  370. struct scb *scb;
  371. struct ahc_devinfo devinfo;
  372. ahc_fetch_devinfo(ahc, &devinfo);
  373. /*
  374.  * Clear the upper byte that holds SEQINT status
  375.  * codes and clear the SEQINT bit. We will unpause
  376.  * the sequencer, if appropriate, after servicing
  377.  * the request.
  378.  */
  379. ahc_outb(ahc, CLRINT, CLRSEQINT);
  380. switch (intstat & SEQINT_MASK) {
  381. case BAD_STATUS:
  382. {
  383. u_int  scb_index;
  384. struct hardware_scb *hscb;
  385. /*
  386.  * Set the default return value to 0 (don't
  387.  * send sense).  The sense code will change
  388.  * this if needed.
  389.  */
  390. ahc_outb(ahc, RETURN_1, 0);
  391. /*
  392.  * The sequencer will notify us when a command
  393.  * has an error that would be of interest to
  394.  * the kernel.  This allows us to leave the sequencer
  395.  * running in the common case of command completes
  396.  * without error.  The sequencer will already have
  397.  * dma'd the SCB back up to us, so we can reference
  398.  * the in kernel copy directly.
  399.  */
  400. scb_index = ahc_inb(ahc, SCB_TAG);
  401. scb = ahc_lookup_scb(ahc, scb_index);
  402. if (scb == NULL) {
  403. printf("%s:%c:%d: ahc_intr - referenced scb "
  404.        "not valid during seqint 0x%x scb(%d)n",
  405.        ahc_name(ahc), devinfo.channel,
  406.        devinfo.target, intstat, scb_index);
  407. ahc_dump_card_state(ahc);
  408. panic("for safety");
  409. goto unpause;
  410. }
  411. hscb = scb->hscb; 
  412. /* Don't want to clobber the original sense code */
  413. if ((scb->flags & SCB_SENSE) != 0) {
  414. /*
  415.  * Clear the SCB_SENSE Flag and have
  416.  * the sequencer do a normal command
  417.  * complete.
  418.  */
  419. scb->flags &= ~SCB_SENSE;
  420. ahc_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
  421. break;
  422. }
  423. ahc_set_transaction_status(scb, CAM_SCSI_STATUS_ERROR);
  424. /* Freeze the queue until the client sees the error. */
  425. ahc_freeze_devq(ahc, scb);
  426. ahc_freeze_scb(scb);
  427. ahc_set_scsi_status(scb, hscb->shared_data.status.scsi_status);
  428. switch (hscb->shared_data.status.scsi_status) {
  429. case SCSI_STATUS_OK:
  430. printf("%s: Interrupted for staus of 0???n",
  431.        ahc_name(ahc));
  432. break;
  433. case SCSI_STATUS_CMD_TERMINATED:
  434. case SCSI_STATUS_CHECK_COND:
  435. {
  436. struct ahc_dma_seg *sg;
  437. struct scsi_sense *sc;
  438. struct ahc_initiator_tinfo *targ_info;
  439. struct ahc_tmode_tstate *tstate;
  440. struct ahc_transinfo *tinfo;
  441. #ifdef AHC_DEBUG
  442. if (ahc_debug & AHC_SHOWSENSE) {
  443. ahc_print_path(ahc, scb);
  444. printf("SCB %d: requests Check Statusn",
  445.        scb->hscb->tag);
  446. }
  447. #endif
  448. if (ahc_perform_autosense(scb) == 0)
  449. break;
  450. targ_info = ahc_fetch_transinfo(ahc,
  451. devinfo.channel,
  452. devinfo.our_scsiid,
  453. devinfo.target,
  454. &tstate);
  455. tinfo = &targ_info->curr;
  456. sg = scb->sg_list;
  457. sc = (struct scsi_sense *)(&hscb->shared_data.cdb); 
  458. /*
  459.  * Save off the residual if there is one.
  460.  */
  461. ahc_update_residual(ahc, scb);
  462. #ifdef AHC_DEBUG
  463. if (ahc_debug & AHC_SHOWSENSE) {
  464. ahc_print_path(ahc, scb);
  465. printf("Sending Sensen");
  466. }
  467. #endif
  468. sg->addr = ahc_get_sense_bufaddr(ahc, scb);
  469. sg->len = ahc_get_sense_bufsize(ahc, scb);
  470. sg->len |= AHC_DMA_LAST_SEG;
  471. /* Fixup byte order */
  472. sg->addr = ahc_htole32(sg->addr);
  473. sg->len = ahc_htole32(sg->len);
  474. sc->opcode = REQUEST_SENSE;
  475. sc->byte2 = 0;
  476. if (tinfo->protocol_version <= SCSI_REV_2
  477.  && SCB_GET_LUN(scb) < 8)
  478. sc->byte2 = SCB_GET_LUN(scb) << 5;
  479. sc->unused[0] = 0;
  480. sc->unused[1] = 0;
  481. sc->length = sg->len;
  482. sc->control = 0;
  483. /*
  484.  * We can't allow the target to disconnect.
  485.  * This will be an untagged transaction and
  486.  * having the target disconnect will make this
  487.  * transaction indestinguishable from outstanding
  488.  * tagged transactions.
  489.  */
  490. hscb->control = 0;
  491. /*
  492.  * This request sense could be because the
  493.  * the device lost power or in some other
  494.  * way has lost our transfer negotiations.
  495.  * Renegotiate if appropriate.  Unit attention
  496.  * errors will be reported before any data
  497.  * phases occur.
  498.  */
  499. if (ahc_get_residual(scb) 
  500.  == ahc_get_transfer_length(scb)) {
  501. ahc_update_neg_request(ahc, &devinfo,
  502.        tstate, targ_info,
  503.        /*force*/TRUE);
  504. }
  505. if (tstate->auto_negotiate & devinfo.target_mask) {
  506. hscb->control |= MK_MESSAGE;
  507. scb->flags &= ~SCB_NEGOTIATE;
  508. scb->flags |= SCB_AUTO_NEGOTIATE;
  509. }
  510. hscb->cdb_len = sizeof(*sc);
  511. hscb->dataptr = sg->addr; 
  512. hscb->datacnt = sg->len;
  513. hscb->sgptr = scb->sg_list_phys | SG_FULL_RESID;
  514. hscb->sgptr = ahc_htole32(hscb->sgptr);
  515. scb->sg_count = 1;
  516. scb->flags |= SCB_SENSE;
  517. ahc_qinfifo_requeue_tail(ahc, scb);
  518. ahc_outb(ahc, RETURN_1, SEND_SENSE);
  519. #ifdef __FreeBSD__
  520. /*
  521.  * Ensure we have enough time to actually
  522.  * retrieve the sense.
  523.  */
  524. untimeout(ahc_timeout, (caddr_t)scb,
  525.   scb->io_ctx->ccb_h.timeout_ch);
  526. scb->io_ctx->ccb_h.timeout_ch =
  527.     timeout(ahc_timeout, (caddr_t)scb, 5 * hz);
  528. #endif
  529. break;
  530. }
  531. default:
  532. break;
  533. }
  534. break;
  535. }
  536. case NO_MATCH:
  537. {
  538. /* Ensure we don't leave the selection hardware on */
  539. ahc_outb(ahc, SCSISEQ,
  540.  ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
  541. printf("%s:%c:%d: no active SCB for reconnecting "
  542.        "target - issuing BUS DEVICE RESETn",
  543.        ahc_name(ahc), devinfo.channel, devinfo.target);
  544. printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
  545.        "ARG_1 == 0x%x ACCUM = 0x%xn",
  546.        ahc_inb(ahc, SAVED_SCSIID), ahc_inb(ahc, SAVED_LUN),
  547.        ahc_inb(ahc, ARG_1), ahc_inb(ahc, ACCUM));
  548. printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
  549.        "SINDEX == 0x%xn",
  550.        ahc_inb(ahc, SEQ_FLAGS), ahc_inb(ahc, SCBPTR),
  551.        ahc_index_busy_tcl(ahc,
  552.     BUILD_TCL(ahc_inb(ahc, SAVED_SCSIID),
  553.       ahc_inb(ahc, SAVED_LUN))),
  554.        ahc_inb(ahc, SINDEX));
  555. printf("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
  556.        "SCB_TAG == 0x%x, SCB_CONTROL == 0x%xn",
  557.        ahc_inb(ahc, SCSIID), ahc_inb(ahc, SCB_SCSIID),
  558.        ahc_inb(ahc, SCB_LUN), ahc_inb(ahc, SCB_TAG),
  559.        ahc_inb(ahc, SCB_CONTROL));
  560. printf("SCSIBUSL == 0x%x, SCSISIGI == 0x%xn",
  561.        ahc_inb(ahc, SCSIBUSL), ahc_inb(ahc, SCSISIGI));
  562. printf("SXFRCTL0 == 0x%xn", ahc_inb(ahc, SXFRCTL0));
  563. printf("SEQCTL == 0x%xn", ahc_inb(ahc, SEQCTL));
  564. ahc_dump_card_state(ahc);
  565. ahc->msgout_buf[0] = MSG_BUS_DEV_RESET;
  566. ahc->msgout_len = 1;
  567. ahc->msgout_index = 0;
  568. ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  569. ahc_outb(ahc, MSG_OUT, HOST_MSG);
  570. ahc_assert_atn(ahc);
  571. break;
  572. }
  573. case SEND_REJECT: 
  574. {
  575. u_int rejbyte = ahc_inb(ahc, ACCUM);
  576. printf("%s:%c:%d: Warning - unknown message received from "
  577.        "target (0x%x).  Rejectingn", 
  578.        ahc_name(ahc), devinfo.channel, devinfo.target, rejbyte);
  579. break; 
  580. }
  581. case NO_IDENT: 
  582. {
  583. /*
  584.  * The reconnecting target either did not send an identify
  585.  * message, or did, but we didn't find an SCB to match and
  586.  * before it could respond to our ATN/abort, it hit a dataphase.
  587.  * The only safe thing to do is to blow it away with a bus
  588.  * reset.
  589.  */
  590. int found;
  591. printf("%s:%c:%d: Target did not send an IDENTIFY message. "
  592.        "LASTPHASE = 0x%x, SAVED_SCSIID == 0x%xn",
  593.        ahc_name(ahc), devinfo.channel, devinfo.target,
  594.        ahc_inb(ahc, LASTPHASE), ahc_inb(ahc, SAVED_SCSIID));
  595. found = ahc_reset_channel(ahc, devinfo.channel, 
  596.   /*initiate reset*/TRUE);
  597. printf("%s: Issued Channel %c Bus Reset. "
  598.        "%d SCBs abortedn", ahc_name(ahc), devinfo.channel,
  599.        found);
  600. return;
  601. }
  602. case IGN_WIDE_RES:
  603. ahc_handle_ign_wide_residue(ahc, &devinfo);
  604. break;
  605. case PDATA_REINIT:
  606. ahc_reinitialize_dataptrs(ahc);
  607. break;
  608. case BAD_PHASE:
  609. {
  610. u_int lastphase;
  611. lastphase = ahc_inb(ahc, LASTPHASE);
  612. printf("%s:%c:%d: unknown scsi bus phase %x, "
  613.        "lastphase = 0x%x.  Attempting to continuen",
  614.        ahc_name(ahc), devinfo.channel, devinfo.target,
  615.        lastphase, ahc_inb(ahc, SCSISIGI));
  616. break;
  617. }
  618. case MISSED_BUSFREE:
  619. {
  620. u_int lastphase;
  621. lastphase = ahc_inb(ahc, LASTPHASE);
  622. printf("%s:%c:%d: Missed busfree. "
  623.        "Lastphase = 0x%x, Curphase = 0x%xn",
  624.        ahc_name(ahc), devinfo.channel, devinfo.target,
  625.        lastphase, ahc_inb(ahc, SCSISIGI));
  626. ahc_restart(ahc);
  627. return;
  628. }
  629. case HOST_MSG_LOOP:
  630. {
  631. /*
  632.  * The sequencer has encountered a message phase
  633.  * that requires host assistance for completion.
  634.  * While handling the message phase(s), we will be
  635.  * notified by the sequencer after each byte is
  636.  * transfered so we can track bus phase changes.
  637.  *
  638.  * If this is the first time we've seen a HOST_MSG_LOOP
  639.  * interrupt, initialize the state of the host message
  640.  * loop.
  641.  */
  642. if (ahc->msg_type == MSG_TYPE_NONE) {
  643. struct scb *scb;
  644. u_int scb_index;
  645. u_int bus_phase;
  646. bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  647. if (bus_phase != P_MESGIN
  648.  && bus_phase != P_MESGOUT) {
  649. printf("ahc_intr: HOST_MSG_LOOP bad "
  650.        "phase 0x%xn",
  651.       bus_phase);
  652. /*
  653.  * Probably transitioned to bus free before
  654.  * we got here.  Just punt the message.
  655.  */
  656. ahc_clear_intstat(ahc);
  657. ahc_restart(ahc);
  658. return;
  659. }
  660. scb_index = ahc_inb(ahc, SCB_TAG);
  661. scb = ahc_lookup_scb(ahc, scb_index);
  662. if (devinfo.role == ROLE_INITIATOR) {
  663. if (scb == NULL)
  664. panic("HOST_MSG_LOOP with "
  665.       "invalid SCB %xn", scb_index);
  666. if (bus_phase == P_MESGOUT)
  667. ahc_setup_initiator_msgout(ahc,
  668.    &devinfo,
  669.    scb);
  670. else {
  671. ahc->msg_type =
  672.     MSG_TYPE_INITIATOR_MSGIN;
  673. ahc->msgin_index = 0;
  674. }
  675. }
  676. #if AHC_TARGET_MODE
  677. else {
  678. if (bus_phase == P_MESGOUT) {
  679. ahc->msg_type =
  680.     MSG_TYPE_TARGET_MSGOUT;
  681. ahc->msgin_index = 0;
  682. }
  683. else 
  684. ahc_setup_target_msgin(ahc,
  685.        &devinfo,
  686.        scb);
  687. }
  688. #endif
  689. }
  690. ahc_handle_message_phase(ahc);
  691. break;
  692. }
  693. case PERR_DETECTED:
  694. {
  695. /*
  696.  * If we've cleared the parity error interrupt
  697.  * but the sequencer still believes that SCSIPERR
  698.  * is true, it must be that the parity error is
  699.  * for the currently presented byte on the bus,
  700.  * and we are not in a phase (data-in) where we will
  701.  * eventually ack this byte.  Ack the byte and
  702.  * throw it away in the hope that the target will
  703.  * take us to message out to deliver the appropriate
  704.  * error message.
  705.  */
  706. if ((intstat & SCSIINT) == 0
  707.  && (ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0) {
  708. if ((ahc->features & AHC_DT) == 0) {
  709. u_int curphase;
  710. /*
  711.  * The hardware will only let you ack bytes
  712.  * if the expected phase in SCSISIGO matches
  713.  * the current phase.  Make sure this is
  714.  * currently the case.
  715.  */
  716. curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  717. ahc_outb(ahc, LASTPHASE, curphase);
  718. ahc_outb(ahc, SCSISIGO, curphase);
  719. }
  720. ahc_inb(ahc, SCSIDATL);
  721. }
  722. break;
  723. }
  724. case DATA_OVERRUN:
  725. {
  726. /*
  727.  * When the sequencer detects an overrun, it
  728.  * places the controller in "BITBUCKET" mode
  729.  * and allows the target to complete its transfer.
  730.  * Unfortunately, none of the counters get updated
  731.  * when the controller is in this mode, so we have
  732.  * no way of knowing how large the overrun was.
  733.  */
  734. u_int scbindex = ahc_inb(ahc, SCB_TAG);
  735. u_int lastphase = ahc_inb(ahc, LASTPHASE);
  736. u_int i;
  737. scb = ahc_lookup_scb(ahc, scbindex);
  738. for (i = 0; i < num_phases; i++) {
  739. if (lastphase == ahc_phase_table[i].phase)
  740. break;
  741. }
  742. ahc_print_path(ahc, scb);
  743. printf("data overrun detected %s."
  744.        "  Tag == 0x%x.n",
  745.        ahc_phase_table[i].phasemsg,
  746.           scb->hscb->tag);
  747. ahc_print_path(ahc, scb);
  748. printf("%s seen Data Phase.  Length = %ld.  NumSGs = %d.n",
  749.        ahc_inb(ahc, SEQ_FLAGS) & DPHASE ? "Have" : "Haven't",
  750.        ahc_get_transfer_length(scb), scb->sg_count);
  751. if (scb->sg_count > 0) {
  752. for (i = 0; i < scb->sg_count; i++) {
  753. printf("sg[%d] - Addr 0x%x%x : Length %dn",
  754.        i,
  755.        (ahc_le32toh(scb->sg_list[i].len) >> 24
  756.         & SG_HIGH_ADDR_BITS),
  757.        ahc_le32toh(scb->sg_list[i].addr),
  758.        ahc_le32toh(scb->sg_list[i].len)
  759.        & AHC_SG_LEN_MASK);
  760. }
  761. }
  762. /*
  763.  * Set this and it will take effect when the
  764.  * target does a command complete.
  765.  */
  766. ahc_freeze_devq(ahc, scb);
  767. if ((scb->flags & SCB_SENSE) == 0) {
  768. ahc_set_transaction_status(scb, CAM_DATA_RUN_ERR);
  769. } else {
  770. scb->flags &= ~SCB_SENSE;
  771. ahc_set_transaction_status(scb, CAM_AUTOSENSE_FAIL);
  772. }
  773. ahc_freeze_scb(scb);
  774. if ((ahc->features & AHC_ULTRA2) != 0) {
  775. /*
  776.  * Clear the channel in case we return
  777.  * to data phase later.
  778.  */
  779. ahc_outb(ahc, SXFRCTL0,
  780.  ahc_inb(ahc, SXFRCTL0) | CLRSTCNT|CLRCHN);
  781. ahc_outb(ahc, SXFRCTL0,
  782.  ahc_inb(ahc, SXFRCTL0) | CLRSTCNT|CLRCHN);
  783. }
  784. if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
  785. u_int dscommand1;
  786. /* Ensure HHADDR is 0 for future DMA operations. */
  787. dscommand1 = ahc_inb(ahc, DSCOMMAND1);
  788. ahc_outb(ahc, DSCOMMAND1, dscommand1 | HADDLDSEL0);
  789. ahc_outb(ahc, HADDR, 0);
  790. ahc_outb(ahc, DSCOMMAND1, dscommand1);
  791. }
  792. break;
  793. }
  794. case MKMSG_FAILED:
  795. {
  796. u_int scbindex;
  797. printf("%s:%c:%d:%d: Attempt to issue message failedn",
  798.        ahc_name(ahc), devinfo.channel, devinfo.target,
  799.        devinfo.lun);
  800. scbindex = ahc_inb(ahc, SCB_TAG);
  801. scb = ahc_lookup_scb(ahc, scbindex);
  802. if (scb != NULL
  803.  && (scb->flags & SCB_RECOVERY_SCB) != 0)
  804. /*
  805.  * Ensure that we didn't put a second instance of this
  806.  * SCB into the QINFIFO.
  807.  */
  808. ahc_search_qinfifo(ahc, SCB_GET_TARGET(ahc, scb),
  809.    SCB_GET_CHANNEL(ahc, scb),
  810.    SCB_GET_LUN(scb), scb->hscb->tag,
  811.    ROLE_INITIATOR, /*status*/0,
  812.    SEARCH_REMOVE);
  813. break;
  814. }
  815. case NO_FREE_SCB:
  816. {
  817. printf("%s: No free or disconnected SCBsn", ahc_name(ahc));
  818. ahc_dump_card_state(ahc);
  819. panic("for safety");
  820. break;
  821. }
  822. case SCB_MISMATCH:
  823. {
  824. u_int scbptr;
  825. scbptr = ahc_inb(ahc, SCBPTR);
  826. printf("Bogus TAG after DMA.  SCBPTR %d, tag %d, our tag %dn",
  827.        scbptr, ahc_inb(ahc, ARG_1),
  828.        ahc->scb_data->hscbs[scbptr].tag);
  829. ahc_dump_card_state(ahc);
  830. panic("for saftey");
  831. break;
  832. }
  833. case OUT_OF_RANGE:
  834. {
  835. printf("%s: BTT calculation out of rangen", ahc_name(ahc));
  836. printf("SAVED_SCSIID == 0x%x, SAVED_LUN == 0x%x, "
  837.        "ARG_1 == 0x%x ACCUM = 0x%xn",
  838.        ahc_inb(ahc, SAVED_SCSIID), ahc_inb(ahc, SAVED_LUN),
  839.        ahc_inb(ahc, ARG_1), ahc_inb(ahc, ACCUM));
  840. printf("SEQ_FLAGS == 0x%x, SCBPTR == 0x%x, BTT == 0x%x, "
  841.        "SINDEX == 0x%xn, A == 0x%xn",
  842.        ahc_inb(ahc, SEQ_FLAGS), ahc_inb(ahc, SCBPTR),
  843.        ahc_index_busy_tcl(ahc,
  844.     BUILD_TCL(ahc_inb(ahc, SAVED_SCSIID),
  845.       ahc_inb(ahc, SAVED_LUN))),
  846.        ahc_inb(ahc, SINDEX),
  847.        ahc_inb(ahc, ACCUM));
  848. printf("SCSIID == 0x%x, SCB_SCSIID == 0x%x, SCB_LUN == 0x%x, "
  849.        "SCB_TAG == 0x%x, SCB_CONTROL == 0x%xn",
  850.        ahc_inb(ahc, SCSIID), ahc_inb(ahc, SCB_SCSIID),
  851.        ahc_inb(ahc, SCB_LUN), ahc_inb(ahc, SCB_TAG),
  852.        ahc_inb(ahc, SCB_CONTROL));
  853. printf("SCSIBUSL == 0x%x, SCSISIGI == 0x%xn",
  854.        ahc_inb(ahc, SCSIBUSL), ahc_inb(ahc, SCSISIGI));
  855. ahc_dump_card_state(ahc);
  856. panic("for safety");
  857. break;
  858. }
  859. default:
  860. printf("ahc_intr: seqint, "
  861.        "intstat == 0x%x, scsisigi = 0x%xn",
  862.        intstat, ahc_inb(ahc, SCSISIGI));
  863. break;
  864. }
  865. unpause:
  866. /*
  867.  *  The sequencer is paused immediately on
  868.  *  a SEQINT, so we should restart it when
  869.  *  we're done.
  870.  */
  871. ahc_unpause(ahc);
  872. }
  873. void
  874. ahc_handle_scsiint(struct ahc_softc *ahc, u_int intstat)
  875. {
  876. u_int scb_index;
  877. u_int status0;
  878. u_int status;
  879. struct scb *scb;
  880. char cur_channel;
  881. char intr_channel;
  882. /* Make sure the sequencer is in a safe location. */
  883. ahc_clear_critical_section(ahc);
  884. if ((ahc->features & AHC_TWIN) != 0
  885.  && ((ahc_inb(ahc, SBLKCTL) & SELBUSB) != 0))
  886. cur_channel = 'B';
  887. else
  888. cur_channel = 'A';
  889. intr_channel = cur_channel;
  890. if ((ahc->features & AHC_ULTRA2) != 0)
  891. status0 = ahc_inb(ahc, SSTAT0) & IOERR;
  892. else
  893. status0 = 0;
  894. status = ahc_inb(ahc, SSTAT1) & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
  895. if (status == 0 && status0 == 0) {
  896. if ((ahc->features & AHC_TWIN) != 0) {
  897. /* Try the other channel */
  898.   ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB);
  899. status = ahc_inb(ahc, SSTAT1)
  900.        & (SELTO|SCSIRSTI|BUSFREE|SCSIPERR);
  901. intr_channel = (cur_channel == 'A') ? 'B' : 'A';
  902. }
  903. if (status == 0) {
  904. printf("%s: Spurious SCSI interruptn", ahc_name(ahc));
  905. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  906. ahc_unpause(ahc);
  907. return;
  908. }
  909. }
  910. scb_index = ahc_inb(ahc, SCB_TAG);
  911. scb = ahc_lookup_scb(ahc, scb_index);
  912. if (scb != NULL
  913.  && (ahc_inb(ahc, SEQ_FLAGS) & IDENTIFY_SEEN) == 0)
  914. scb = NULL;
  915. if ((ahc->features & AHC_ULTRA2) != 0
  916.  && (status0 & IOERR) != 0) {
  917. int now_lvd;
  918. now_lvd = ahc_inb(ahc, SBLKCTL) & ENAB40;
  919. printf("%s: Transceiver State Has Changed to %s moden",
  920.        ahc_name(ahc), now_lvd ? "LVD" : "SE");
  921. ahc_outb(ahc, CLRSINT0, CLRIOERR);
  922. /*
  923.  * When transitioning to SE mode, the reset line
  924.  * glitches, triggering an arbitration bug in some
  925.  * Ultra2 controllers.  This bug is cleared when we
  926.  * assert the reset line.  Since a reset glitch has
  927.  * already occurred with this transition and a
  928.  * transceiver state change is handled just like
  929.  * a bus reset anyway, asserting the reset line
  930.  * ourselves is safe.
  931.  */
  932. ahc_reset_channel(ahc, intr_channel,
  933.  /*Initiate Reset*/now_lvd == 0);
  934. } else if ((status & SCSIRSTI) != 0) {
  935. printf("%s: Someone reset channel %cn",
  936. ahc_name(ahc), intr_channel);
  937. if (intr_channel != cur_channel)
  938.   ahc_outb(ahc, SBLKCTL, ahc_inb(ahc, SBLKCTL) ^ SELBUSB);
  939. ahc_reset_channel(ahc, intr_channel, /*Initiate Reset*/FALSE);
  940. } else if ((status & SCSIPERR) != 0) {
  941. /*
  942.  * Determine the bus phase and queue an appropriate message.
  943.  * SCSIPERR is latched true as soon as a parity error
  944.  * occurs.  If the sequencer acked the transfer that
  945.  * caused the parity error and the currently presented
  946.  * transfer on the bus has correct parity, SCSIPERR will
  947.  * be cleared by CLRSCSIPERR.  Use this to determine if
  948.  * we should look at the last phase the sequencer recorded,
  949.  * or the current phase presented on the bus.
  950.  */
  951. u_int mesg_out;
  952. u_int curphase;
  953. u_int errorphase;
  954. u_int lastphase;
  955. u_int scsirate;
  956. u_int i;
  957. u_int sstat2;
  958. lastphase = ahc_inb(ahc, LASTPHASE);
  959. curphase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  960. sstat2 = ahc_inb(ahc, SSTAT2);
  961. ahc_outb(ahc, CLRSINT1, CLRSCSIPERR);
  962. /*
  963.  * For all phases save DATA, the sequencer won't
  964.  * automatically ack a byte that has a parity error
  965.  * in it.  So the only way that the current phase
  966.  * could be 'data-in' is if the parity error is for
  967.  * an already acked byte in the data phase.  During
  968.  * synchronous data-in transfers, we may actually
  969.  * ack bytes before latching the current phase in
  970.  * LASTPHASE, leading to the discrepancy between
  971.  * curphase and lastphase.
  972.  */
  973. if ((ahc_inb(ahc, SSTAT1) & SCSIPERR) != 0
  974.  || curphase == P_DATAIN || curphase == P_DATAIN_DT)
  975. errorphase = curphase;
  976. else
  977. errorphase = lastphase;
  978. for (i = 0; i < num_phases; i++) {
  979. if (errorphase == ahc_phase_table[i].phase)
  980. break;
  981. }
  982. mesg_out = ahc_phase_table[i].mesg_out;
  983. if (scb != NULL)
  984. ahc_print_path(ahc, scb);
  985. else
  986. printf("%s:%c:%d: ", ahc_name(ahc), intr_channel,
  987.        SCSIID_TARGET(ahc, ahc_inb(ahc, SAVED_SCSIID)));
  988. scsirate = ahc_inb(ahc, SCSIRATE);
  989. printf("parity error detected %s. "
  990.        "SEQADDR(0x%x) SCSIRATE(0x%x)n",
  991.        ahc_phase_table[i].phasemsg,
  992.        ahc_inb(ahc, SEQADDR0) | (ahc_inb(ahc, SEQADDR1) << 8),
  993.        scsirate);
  994. if ((ahc->features & AHC_DT) != 0) {
  995. if ((sstat2 & CRCVALERR) != 0)
  996. printf("tCRC Value Mismatchn");
  997. if ((sstat2 & CRCENDERR) != 0)
  998. printf("tNo terminal CRC packet receviedn");
  999. if ((sstat2 & CRCREQERR) != 0)
  1000. printf("tIllegal CRC packet requestn");
  1001. if ((sstat2 & DUAL_EDGE_ERR) != 0)
  1002. printf("tUnexpected %sDT Data Phasen",
  1003.        (scsirate & SINGLE_EDGE) ? "" : "non-");
  1004. }
  1005. /*
  1006.  * We've set the hardware to assert ATN if we   
  1007.  * get a parity error on "in" phases, so all we  
  1008.  * need to do is stuff the message buffer with
  1009.  * the appropriate message.  "In" phases have set
  1010.  * mesg_out to something other than MSG_NOP.
  1011.  */
  1012. if (mesg_out != MSG_NOOP) {
  1013. if (ahc->msg_type != MSG_TYPE_NONE)
  1014. ahc->send_msg_perror = TRUE;
  1015. else
  1016. ahc_outb(ahc, MSG_OUT, mesg_out);
  1017. }
  1018. /*
  1019.  * Force a renegotiation with this target just in
  1020.  * case we are out of sync for some external reason
  1021.  * unknown (or unreported) by the target.
  1022.  */
  1023. ahc_force_renegotiation(ahc);
  1024. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1025. ahc_unpause(ahc);
  1026. } else if ((status & SELTO) != 0) {
  1027. u_int scbptr;
  1028. /* Stop the selection */
  1029. ahc_outb(ahc, SCSISEQ, 0);
  1030. /* No more pending messages */
  1031. ahc_clear_msg_state(ahc);
  1032. /* Clear interrupt state */
  1033. ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
  1034. ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRBUSFREE|CLRSCSIPERR);
  1035. /*
  1036.  * Although the driver does not care about the
  1037.  * 'Selection in Progress' status bit, the busy
  1038.  * LED does.  SELINGO is only cleared by a sucessfull
  1039.  * selection, so we must manually clear it to insure
  1040.  * the LED turns off just incase no future successful
  1041.  * selections occur (e.g. no devices on the bus).
  1042.  */
  1043. ahc_outb(ahc, CLRSINT0, CLRSELINGO);
  1044. scbptr = ahc_inb(ahc, WAITING_SCBH);
  1045. ahc_outb(ahc, SCBPTR, scbptr);
  1046. scb_index = ahc_inb(ahc, SCB_TAG);
  1047. scb = ahc_lookup_scb(ahc, scb_index);
  1048. if (scb == NULL) {
  1049. printf("%s: ahc_intr - referenced scb not "
  1050.        "valid during SELTO scb(%d, %d)n",
  1051.        ahc_name(ahc), scbptr, scb_index);
  1052. } else {
  1053. ahc_set_transaction_status(scb, CAM_SEL_TIMEOUT);
  1054. ahc_freeze_devq(ahc, scb);
  1055. }
  1056. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1057. /*
  1058.  * Force a renegotiation with this target just in
  1059.  * case the cable was pulled and will later be
  1060.  * re-attached.  The target may forget its negotiation
  1061.  * settings with us should it attempt to reselect
  1062.  * during the interruption.  The target will not issue
  1063.  * a unit attention in this case, so we must always
  1064.  * renegotiate.
  1065.  */
  1066. ahc_force_renegotiation(ahc);
  1067. ahc_restart(ahc);
  1068. } else if ((status & BUSFREE) != 0
  1069. && (ahc_inb(ahc, SIMODE1) & ENBUSFREE) != 0) {
  1070. u_int lastphase;
  1071. u_int saved_scsiid;
  1072. u_int saved_lun;
  1073. u_int target;
  1074. u_int initiator_role_id;
  1075. char channel;
  1076. int printerror;
  1077. /*
  1078.  * Clear our selection hardware as soon as possible.
  1079.  * We may have an entry in the waiting Q for this target,
  1080.  * that is affected by this busfree and we don't want to
  1081.  * go about selecting the target while we handle the event.
  1082.  */
  1083. ahc_outb(ahc, SCSISEQ,
  1084.  ahc_inb(ahc, SCSISEQ) & (ENSELI|ENRSELI|ENAUTOATNP));
  1085. /*
  1086.  * Disable busfree interrupts and clear the busfree
  1087.  * interrupt status.  We do this here so that several
  1088.  * bus transactions occur prior to clearing the SCSIINT
  1089.  * latch.  It can take a bit for the clearing to take effect.
  1090.  */
  1091. ahc_outb(ahc, SIMODE1, ahc_inb(ahc, SIMODE1) & ~ENBUSFREE);
  1092. ahc_outb(ahc, CLRSINT1, CLRBUSFREE|CLRSCSIPERR);
  1093. /*
  1094.  * Look at what phase we were last in.
  1095.  * If its message out, chances are pretty good
  1096.  * that the busfree was in response to one of
  1097.  * our abort requests.
  1098.  */
  1099. lastphase = ahc_inb(ahc, LASTPHASE);
  1100. saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
  1101. saved_lun = ahc_inb(ahc, SAVED_LUN);
  1102. target = SCSIID_TARGET(ahc, saved_scsiid);
  1103. initiator_role_id = SCSIID_OUR_ID(saved_scsiid);
  1104. channel = SCSIID_CHANNEL(ahc, saved_scsiid);
  1105. printerror = 1;
  1106. if (lastphase == P_MESGOUT) {
  1107. struct ahc_devinfo devinfo;
  1108. u_int tag;
  1109. ahc_fetch_devinfo(ahc, &devinfo);
  1110. tag = SCB_LIST_NULL;
  1111. if (ahc_sent_msg(ahc, AHCMSG_1B, MSG_ABORT_TAG, TRUE)
  1112.  || ahc_sent_msg(ahc, AHCMSG_1B, MSG_ABORT, TRUE)) {
  1113. if (ahc->msgout_buf[ahc->msgout_index - 1]
  1114.  == MSG_ABORT_TAG)
  1115. tag = scb->hscb->tag;
  1116. ahc_print_path(ahc, scb);
  1117. printf("SCB %d - Abort%s Completed.n",
  1118.        scb->hscb->tag, tag == SCB_LIST_NULL ?
  1119.        "" : " Tag");
  1120. ahc_abort_scbs(ahc, target, channel,
  1121.        saved_lun, tag,
  1122.        ROLE_INITIATOR,
  1123.        CAM_REQ_ABORTED);
  1124. printerror = 0;
  1125. } else if (ahc_sent_msg(ahc, AHCMSG_1B,
  1126. MSG_BUS_DEV_RESET, TRUE)) {
  1127. #ifdef __FreeBSD__
  1128. /*
  1129.  * Don't mark the user's request for this BDR
  1130.  * as completing with CAM_BDR_SENT.  CAM3
  1131.  * specifies CAM_REQ_CMP.
  1132.  */
  1133. if (scb != NULL
  1134.  && scb->io_ctx->ccb_h.func_code== XPT_RESET_DEV
  1135.  && ahc_match_scb(ahc, scb, target, channel,
  1136.   CAM_LUN_WILDCARD,
  1137.   SCB_LIST_NULL,
  1138.   ROLE_INITIATOR)) {
  1139. ahc_set_transaction_status(scb, CAM_REQ_CMP);
  1140. }
  1141. #endif
  1142. ahc_compile_devinfo(&devinfo,
  1143.     initiator_role_id,
  1144.     target,
  1145.     CAM_LUN_WILDCARD,
  1146.     channel,
  1147.     ROLE_INITIATOR);
  1148. ahc_handle_devreset(ahc, &devinfo,
  1149.     CAM_BDR_SENT,
  1150.     "Bus Device Reset",
  1151.     /*verbose_level*/0);
  1152. printerror = 0;
  1153. } else if (ahc_sent_msg(ahc, AHCMSG_EXT,
  1154. MSG_EXT_PPR, FALSE)) {
  1155. struct ahc_initiator_tinfo *tinfo;
  1156. struct ahc_tmode_tstate *tstate;
  1157. /*
  1158.  * PPR Rejected.  Try non-ppr negotiation
  1159.  * and retry command.
  1160.  */
  1161. tinfo = ahc_fetch_transinfo(ahc,
  1162.     devinfo.channel,
  1163.     devinfo.our_scsiid,
  1164.     devinfo.target,
  1165.     &tstate);
  1166. tinfo->curr.transport_version = 2;
  1167. tinfo->goal.transport_version = 2;
  1168. tinfo->goal.ppr_options = 0;
  1169. ahc_qinfifo_requeue_tail(ahc, scb);
  1170. printerror = 0;
  1171. } else if (ahc_sent_msg(ahc, AHCMSG_EXT,
  1172. MSG_EXT_WDTR, FALSE)
  1173. || ahc_sent_msg(ahc, AHCMSG_EXT,
  1174. MSG_EXT_SDTR, FALSE)) {
  1175. /*
  1176.  * Negotiation Rejected.  Go-async and
  1177.  * retry command.
  1178.  */
  1179. ahc_set_width(ahc, &devinfo,
  1180.       MSG_EXT_WDTR_BUS_8_BIT,
  1181.       AHC_TRANS_CUR|AHC_TRANS_GOAL,
  1182.       /*paused*/TRUE);
  1183. ahc_set_syncrate(ahc, &devinfo,
  1184. /*syncrate*/NULL,
  1185. /*period*/0, /*offset*/0,
  1186. /*ppr_options*/0,
  1187. AHC_TRANS_CUR|AHC_TRANS_GOAL,
  1188. /*paused*/TRUE);
  1189. ahc_qinfifo_requeue_tail(ahc, scb);
  1190. printerror = 0;
  1191. }
  1192. }
  1193. if (printerror != 0) {
  1194. u_int i;
  1195. if (scb != NULL) {
  1196. u_int tag;
  1197. if ((scb->hscb->control & TAG_ENB) != 0)
  1198. tag = scb->hscb->tag;
  1199. else
  1200. tag = SCB_LIST_NULL;
  1201. ahc_print_path(ahc, scb);
  1202. ahc_abort_scbs(ahc, target, channel,
  1203.        SCB_GET_LUN(scb), tag,
  1204.        ROLE_INITIATOR,
  1205.        CAM_UNEXP_BUSFREE);
  1206. } else {
  1207. /*
  1208.  * We had not fully identified this connection,
  1209.  * so we cannot abort anything.
  1210.  */
  1211. printf("%s: ", ahc_name(ahc));
  1212. }
  1213. for (i = 0; i < num_phases; i++) {
  1214. if (lastphase == ahc_phase_table[i].phase)
  1215. break;
  1216. }
  1217. /*
  1218.  * Renegotiate with this device at the
  1219.  * next oportunity just in case this busfree
  1220.  * is due to a negotiation mismatch with the
  1221.  * device.
  1222.  */
  1223. ahc_force_renegotiation(ahc);
  1224. printf("Unexpected busfree %sn"
  1225.        "SEQADDR == 0x%xn",
  1226.        ahc_phase_table[i].phasemsg,
  1227.        ahc_inb(ahc, SEQADDR0)
  1228. | (ahc_inb(ahc, SEQADDR1) << 8));
  1229. }
  1230. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1231. ahc_restart(ahc);
  1232. } else {
  1233. printf("%s: Missing case in ahc_handle_scsiint. status = %xn",
  1234.        ahc_name(ahc), status);
  1235. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1236. }
  1237. }
  1238. /*
  1239.  * Force renegotiation to occur the next time we initiate
  1240.  * a command to the current device.
  1241.  */
  1242. static void
  1243. ahc_force_renegotiation(struct ahc_softc *ahc)
  1244. {
  1245. struct ahc_devinfo devinfo;
  1246. struct ahc_initiator_tinfo *targ_info;
  1247. struct ahc_tmode_tstate *tstate;
  1248. ahc_fetch_devinfo(ahc, &devinfo);
  1249. targ_info = ahc_fetch_transinfo(ahc,
  1250. devinfo.channel,
  1251. devinfo.our_scsiid,
  1252. devinfo.target,
  1253. &tstate);
  1254. ahc_update_neg_request(ahc, &devinfo, tstate,
  1255.        targ_info, /*force*/TRUE);
  1256. }
  1257. #define AHC_MAX_STEPS 2000
  1258. void
  1259. ahc_clear_critical_section(struct ahc_softc *ahc)
  1260. {
  1261. int stepping;
  1262. int steps;
  1263. u_int simode0;
  1264. u_int simode1;
  1265. if (ahc->num_critical_sections == 0)
  1266. return;
  1267. stepping = FALSE;
  1268. steps = 0;
  1269. simode0 = 0;
  1270. simode1 = 0;
  1271. for (;;) {
  1272. struct cs *cs;
  1273. u_int seqaddr;
  1274. u_int i;
  1275. seqaddr = ahc_inb(ahc, SEQADDR0)
  1276. | (ahc_inb(ahc, SEQADDR1) << 8);
  1277. /*
  1278.  * Seqaddr represents the next instruction to execute, 
  1279.  * so we are really executing the instruction just
  1280.  * before it.
  1281.  */
  1282. if (seqaddr != 0)
  1283. seqaddr -= 1;
  1284. cs = ahc->critical_sections;
  1285. for (i = 0; i < ahc->num_critical_sections; i++, cs++) {
  1286. if (cs->begin < seqaddr && cs->end >= seqaddr)
  1287. break;
  1288. }
  1289. if (i == ahc->num_critical_sections)
  1290. break;
  1291. if (steps > AHC_MAX_STEPS) {
  1292. printf("%s: Infinite loop in critical sectionn",
  1293.        ahc_name(ahc));
  1294. ahc_dump_card_state(ahc);
  1295. panic("critical section loop");
  1296. }
  1297. steps++;
  1298. if (stepping == FALSE) {
  1299. /*
  1300.  * Disable all interrupt sources so that the
  1301.  * sequencer will not be stuck by a pausing
  1302.  * interrupt condition while we attempt to
  1303.  * leave a critical section.
  1304.  */
  1305. simode0 = ahc_inb(ahc, SIMODE0);
  1306. ahc_outb(ahc, SIMODE0, 0);
  1307. simode1 = ahc_inb(ahc, SIMODE1);
  1308. ahc_outb(ahc, SIMODE1, 0);
  1309. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1310. ahc_outb(ahc, SEQCTL, ahc_inb(ahc, SEQCTL) | STEP);
  1311. stepping = TRUE;
  1312. }
  1313. ahc_outb(ahc, HCNTRL, ahc->unpause);
  1314. while (!ahc_is_paused(ahc))
  1315. ahc_delay(200);
  1316. }
  1317. if (stepping) {
  1318. ahc_outb(ahc, SIMODE0, simode0);
  1319. ahc_outb(ahc, SIMODE1, simode1);
  1320. ahc_outb(ahc, SEQCTL, ahc_inb(ahc, SEQCTL) & ~STEP);
  1321. }
  1322. }
  1323. /*
  1324.  * Clear any pending interrupt status.
  1325.  */
  1326. void
  1327. ahc_clear_intstat(struct ahc_softc *ahc)
  1328. {
  1329. /* Clear any interrupt conditions this may have caused */
  1330. ahc_outb(ahc, CLRSINT1, CLRSELTIMEO|CLRATNO|CLRSCSIRSTI
  1331. |CLRBUSFREE|CLRSCSIPERR|CLRPHASECHG|
  1332. CLRREQINIT);
  1333. ahc_flush_device_writes(ahc);
  1334. ahc_outb(ahc, CLRSINT0, CLRSELDO|CLRSELDI|CLRSELINGO);
  1335.   ahc_flush_device_writes(ahc);
  1336. ahc_outb(ahc, CLRINT, CLRSCSIINT);
  1337. ahc_flush_device_writes(ahc);
  1338. }
  1339. /**************************** Debugging Routines ******************************/
  1340. #ifdef AHC_DEBUG
  1341. int ahc_debug = AHC_DEBUG;
  1342. #endif
  1343. void
  1344. ahc_print_scb(struct scb *scb)
  1345. {
  1346. int i;
  1347. struct hardware_scb *hscb = scb->hscb;
  1348. printf("scb:%p control:0x%x scsiid:0x%x lun:%d cdb_len:%dn",
  1349.        (void *)scb,
  1350.        hscb->control,
  1351.        hscb->scsiid,
  1352.        hscb->lun,
  1353.        hscb->cdb_len);
  1354. printf("Shared Data: ");
  1355. for (i = 0; i < sizeof(hscb->shared_data.cdb); i++)
  1356. printf("%#02x", hscb->shared_data.cdb[i]);
  1357. printf("        dataptr:%#x datacnt:%#x sgptr:%#x tag:%#xn",
  1358. ahc_le32toh(hscb->dataptr),
  1359. ahc_le32toh(hscb->datacnt),
  1360. ahc_le32toh(hscb->sgptr),
  1361. hscb->tag);
  1362. if (scb->sg_count > 0) {
  1363. for (i = 0; i < scb->sg_count; i++) {
  1364. printf("sg[%d] - Addr 0x%x%x : Length %dn",
  1365.        i,
  1366.        (ahc_le32toh(scb->sg_list[i].len) >> 24
  1367.         & SG_HIGH_ADDR_BITS),
  1368.        ahc_le32toh(scb->sg_list[i].addr),
  1369.        ahc_le32toh(scb->sg_list[i].len));
  1370. }
  1371. }
  1372. }
  1373. /************************* Transfer Negotiation *******************************/
  1374. /*
  1375.  * Allocate per target mode instance (ID we respond to as a target)
  1376.  * transfer negotiation data structures.
  1377.  */
  1378. static struct ahc_tmode_tstate *
  1379. ahc_alloc_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel)
  1380. {
  1381. struct ahc_tmode_tstate *master_tstate;
  1382. struct ahc_tmode_tstate *tstate;
  1383. int i;
  1384. master_tstate = ahc->enabled_targets[ahc->our_id];
  1385. if (channel == 'B') {
  1386. scsi_id += 8;
  1387. master_tstate = ahc->enabled_targets[ahc->our_id_b + 8];
  1388. }
  1389. if (ahc->enabled_targets[scsi_id] != NULL
  1390.  && ahc->enabled_targets[scsi_id] != master_tstate)
  1391. panic("%s: ahc_alloc_tstate - Target already allocated",
  1392.       ahc_name(ahc));
  1393. tstate = malloc(sizeof(*tstate), M_DEVBUF, M_NOWAIT);
  1394. if (tstate == NULL)
  1395. return (NULL);
  1396. /*
  1397.  * If we have allocated a master tstate, copy user settings from
  1398.  * the master tstate (taken from SRAM or the EEPROM) for this
  1399.  * channel, but reset our current and goal settings to async/narrow
  1400.  * until an initiator talks to us.
  1401.  */
  1402. if (master_tstate != NULL) {
  1403. memcpy(tstate, master_tstate, sizeof(*tstate));
  1404. memset(tstate->enabled_luns, 0, sizeof(tstate->enabled_luns));
  1405. tstate->ultraenb = 0;
  1406. for (i = 0; i < AHC_NUM_TARGETS; i++) {
  1407. memset(&tstate->transinfo[i].curr, 0,
  1408.       sizeof(tstate->transinfo[i].curr));
  1409. memset(&tstate->transinfo[i].goal, 0,
  1410.       sizeof(tstate->transinfo[i].goal));
  1411. }
  1412. } else
  1413. memset(tstate, 0, sizeof(*tstate));
  1414. ahc->enabled_targets[scsi_id] = tstate;
  1415. return (tstate);
  1416. }
  1417. #ifdef AHC_TARGET_MODE
  1418. /*
  1419.  * Free per target mode instance (ID we respond to as a target)
  1420.  * transfer negotiation data structures.
  1421.  */
  1422. static void
  1423. ahc_free_tstate(struct ahc_softc *ahc, u_int scsi_id, char channel, int force)
  1424. {
  1425. struct ahc_tmode_tstate *tstate;
  1426. /*
  1427.  * Don't clean up our "master" tstate.
  1428.  * It has our default user settings.
  1429.  */
  1430. if (((channel == 'B' && scsi_id == ahc->our_id_b)
  1431.   || (channel == 'A' && scsi_id == ahc->our_id))
  1432.  && force == FALSE)
  1433. return;
  1434. if (channel == 'B')
  1435. scsi_id += 8;
  1436. tstate = ahc->enabled_targets[scsi_id];
  1437. if (tstate != NULL)
  1438. free(tstate, M_DEVBUF);
  1439. ahc->enabled_targets[scsi_id] = NULL;
  1440. }
  1441. #endif
  1442. /*
  1443.  * Called when we have an active connection to a target on the bus,
  1444.  * this function finds the nearest syncrate to the input period limited
  1445.  * by the capabilities of the bus connectivity of and sync settings for
  1446.  * the target.
  1447.  */
  1448. struct ahc_syncrate *
  1449. ahc_devlimited_syncrate(struct ahc_softc *ahc,
  1450. struct ahc_initiator_tinfo *tinfo,
  1451. u_int *period, u_int *ppr_options, role_t role)
  1452. {
  1453. struct ahc_transinfo *transinfo;
  1454. u_int maxsync;
  1455. if ((ahc->features & AHC_ULTRA2) != 0) {
  1456. if ((ahc_inb(ahc, SBLKCTL) & ENAB40) != 0
  1457.  && (ahc_inb(ahc, SSTAT2) & EXP_ACTIVE) == 0) {
  1458. maxsync = AHC_SYNCRATE_DT;
  1459. } else {
  1460. maxsync = AHC_SYNCRATE_ULTRA;
  1461. /* Can't do DT on an SE bus */
  1462. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  1463. }
  1464. } else if ((ahc->features & AHC_ULTRA) != 0) {
  1465. maxsync = AHC_SYNCRATE_ULTRA;
  1466. } else {
  1467. maxsync = AHC_SYNCRATE_FAST;
  1468. }
  1469. /*
  1470.  * Never allow a value higher than our current goal
  1471.  * period otherwise we may allow a target initiated
  1472.  * negotiation to go above the limit as set by the
  1473.  * user.  In the case of an initiator initiated
  1474.  * sync negotiation, we limit based on the user
  1475.  * setting.  This allows the system to still accept
  1476.  * incoming negotiations even if target initiated
  1477.  * negotiation is not performed.
  1478.  */
  1479. if (role == ROLE_TARGET)
  1480. transinfo = &tinfo->user;
  1481. else 
  1482. transinfo = &tinfo->goal;
  1483. *ppr_options &= transinfo->ppr_options;
  1484. if (transinfo->period == 0) {
  1485. *period = 0;
  1486. *ppr_options = 0;
  1487. return (NULL);
  1488. }
  1489. *period = MAX(*period, transinfo->period);
  1490. return (ahc_find_syncrate(ahc, period, ppr_options, maxsync));
  1491. }
  1492. /*
  1493.  * Look up the valid period to SCSIRATE conversion in our table.
  1494.  * Return the period and offset that should be sent to the target
  1495.  * if this was the beginning of an SDTR.
  1496.  */
  1497. struct ahc_syncrate *
  1498. ahc_find_syncrate(struct ahc_softc *ahc, u_int *period,
  1499.   u_int *ppr_options, u_int maxsync)
  1500. {
  1501. struct ahc_syncrate *syncrate;
  1502. if ((ahc->features & AHC_DT) == 0)
  1503. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  1504. /* Skip all DT only entries if DT is not available */
  1505. if ((*ppr_options & MSG_EXT_PPR_DT_REQ) == 0
  1506.  && maxsync < AHC_SYNCRATE_ULTRA2)
  1507. maxsync = AHC_SYNCRATE_ULTRA2;
  1508. for (syncrate = &ahc_syncrates[maxsync];
  1509.      syncrate->rate != NULL;
  1510.      syncrate++) {
  1511. /*
  1512.  * The Ultra2 table doesn't go as low
  1513.  * as for the Fast/Ultra cards.
  1514.  */
  1515. if ((ahc->features & AHC_ULTRA2) != 0
  1516.  && (syncrate->sxfr_u2 == 0))
  1517. break;
  1518. if (*period <= syncrate->period) {
  1519. /*
  1520.  * When responding to a target that requests
  1521.  * sync, the requested rate may fall between
  1522.  * two rates that we can output, but still be
  1523.  * a rate that we can receive.  Because of this,
  1524.  * we want to respond to the target with
  1525.  * the same rate that it sent to us even
  1526.  * if the period we use to send data to it
  1527.  * is lower.  Only lower the response period
  1528.  * if we must.
  1529.  */
  1530. if (syncrate == &ahc_syncrates[maxsync])
  1531. *period = syncrate->period;
  1532. /*
  1533.  * At some speeds, we only support
  1534.  * ST transfers.
  1535.  */
  1536.   if ((syncrate->sxfr_u2 & ST_SXFR) != 0)
  1537. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  1538. break;
  1539. }
  1540. }
  1541. if ((*period == 0)
  1542.  || (syncrate->rate == NULL)
  1543.  || ((ahc->features & AHC_ULTRA2) != 0
  1544.   && (syncrate->sxfr_u2 == 0))) {
  1545. /* Use asynchronous transfers. */
  1546. *period = 0;
  1547. syncrate = NULL;
  1548. *ppr_options &= ~MSG_EXT_PPR_DT_REQ;
  1549. }
  1550. return (syncrate);
  1551. }
  1552. /*
  1553.  * Convert from an entry in our syncrate table to the SCSI equivalent
  1554.  * sync "period" factor.
  1555.  */
  1556. u_int
  1557. ahc_find_period(struct ahc_softc *ahc, u_int scsirate, u_int maxsync)
  1558. {
  1559. struct ahc_syncrate *syncrate;
  1560. if ((ahc->features & AHC_ULTRA2) != 0)
  1561. scsirate &= SXFR_ULTRA2;
  1562. else
  1563. scsirate &= SXFR;
  1564. syncrate = &ahc_syncrates[maxsync];
  1565. while (syncrate->rate != NULL) {
  1566. if ((ahc->features & AHC_ULTRA2) != 0) {
  1567. if (syncrate->sxfr_u2 == 0)
  1568. break;
  1569. else if (scsirate == (syncrate->sxfr_u2 & SXFR_ULTRA2))
  1570. return (syncrate->period);
  1571. } else if (scsirate == (syncrate->sxfr & SXFR)) {
  1572. return (syncrate->period);
  1573. }
  1574. syncrate++;
  1575. }
  1576. return (0); /* async */
  1577. }
  1578. /*
  1579.  * Truncate the given synchronous offset to a value the
  1580.  * current adapter type and syncrate are capable of.
  1581.  */
  1582. void
  1583. ahc_validate_offset(struct ahc_softc *ahc,
  1584.     struct ahc_initiator_tinfo *tinfo,
  1585.     struct ahc_syncrate *syncrate,
  1586.     u_int *offset, int wide, role_t role)
  1587. {
  1588. u_int maxoffset;
  1589. /* Limit offset to what we can do */
  1590. if (syncrate == NULL) {
  1591. maxoffset = 0;
  1592. } else if ((ahc->features & AHC_ULTRA2) != 0) {
  1593. maxoffset = MAX_OFFSET_ULTRA2;
  1594. } else {
  1595. if (wide)
  1596. maxoffset = MAX_OFFSET_16BIT;
  1597. else
  1598. maxoffset = MAX_OFFSET_8BIT;
  1599. }
  1600. *offset = MIN(*offset, maxoffset);
  1601. if (tinfo != NULL) {
  1602. if (role == ROLE_TARGET)
  1603. *offset = MIN(*offset, tinfo->user.offset);
  1604. else
  1605. *offset = MIN(*offset, tinfo->goal.offset);
  1606. }
  1607. }
  1608. /*
  1609.  * Truncate the given transfer width parameter to a value the
  1610.  * current adapter type is capable of.
  1611.  */
  1612. void
  1613. ahc_validate_width(struct ahc_softc *ahc, struct ahc_initiator_tinfo *tinfo,
  1614.    u_int *bus_width, role_t role)
  1615. {
  1616. switch (*bus_width) {
  1617. default:
  1618. if (ahc->features & AHC_WIDE) {
  1619. /* Respond Wide */
  1620. *bus_width = MSG_EXT_WDTR_BUS_16_BIT;
  1621. break;
  1622. }
  1623. /* FALLTHROUGH */
  1624. case MSG_EXT_WDTR_BUS_8_BIT:
  1625. *bus_width = MSG_EXT_WDTR_BUS_8_BIT;
  1626. break;
  1627. }
  1628. if (tinfo != NULL) {
  1629. if (role == ROLE_TARGET)
  1630. *bus_width = MIN(tinfo->user.width, *bus_width);
  1631. else
  1632. *bus_width = MIN(tinfo->goal.width, *bus_width);
  1633. }
  1634. }
  1635. /*
  1636.  * Update the bitmask of targets for which the controller should
  1637.  * negotiate with at the next convenient oportunity.  This currently
  1638.  * means the next time we send the initial identify messages for
  1639.  * a new transaction.
  1640.  */
  1641. int
  1642. ahc_update_neg_request(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1643.        struct ahc_tmode_tstate *tstate,
  1644.        struct ahc_initiator_tinfo *tinfo, int force)
  1645. {
  1646. u_int auto_negotiate_orig;
  1647. auto_negotiate_orig = tstate->auto_negotiate;
  1648. if (tinfo->curr.period != tinfo->goal.period
  1649.  || tinfo->curr.width != tinfo->goal.width
  1650.  || tinfo->curr.offset != tinfo->goal.offset
  1651.  || tinfo->curr.ppr_options != tinfo->goal.ppr_options
  1652.  || (force
  1653.   && (tinfo->goal.period != 0
  1654.    || tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT
  1655.    || tinfo->goal.ppr_options != 0)))
  1656. tstate->auto_negotiate |= devinfo->target_mask;
  1657. else
  1658. tstate->auto_negotiate &= ~devinfo->target_mask;
  1659. return (auto_negotiate_orig != tstate->auto_negotiate);
  1660. }
  1661. /*
  1662.  * Update the user/goal/curr tables of synchronous negotiation
  1663.  * parameters as well as, in the case of a current or active update,
  1664.  * any data structures on the host controller.  In the case of an
  1665.  * active update, the specified target is currently talking to us on
  1666.  * the bus, so the transfer parameter update must take effect
  1667.  * immediately.
  1668.  */
  1669. void
  1670. ahc_set_syncrate(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1671.  struct ahc_syncrate *syncrate, u_int period,
  1672.  u_int offset, u_int ppr_options, u_int type, int paused)
  1673. {
  1674. struct ahc_initiator_tinfo *tinfo;
  1675. struct ahc_tmode_tstate *tstate;
  1676. u_int old_period;
  1677. u_int old_offset;
  1678. u_int old_ppr;
  1679. int active;
  1680. int update_needed;
  1681. active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
  1682. update_needed = 0;
  1683. if (syncrate == NULL) {
  1684. period = 0;
  1685. offset = 0;
  1686. }
  1687. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  1688.     devinfo->target, &tstate);
  1689. if ((type & AHC_TRANS_USER) != 0) {
  1690. tinfo->user.period = period;
  1691. tinfo->user.offset = offset;
  1692. tinfo->user.ppr_options = ppr_options;
  1693. }
  1694. if ((type & AHC_TRANS_GOAL) != 0) {
  1695. tinfo->goal.period = period;
  1696. tinfo->goal.offset = offset;
  1697. tinfo->goal.ppr_options = ppr_options;
  1698. }
  1699. old_period = tinfo->curr.period;
  1700. old_offset = tinfo->curr.offset;
  1701. old_ppr    = tinfo->curr.ppr_options;
  1702. if ((type & AHC_TRANS_CUR) != 0
  1703.  && (old_period != period
  1704.   || old_offset != offset
  1705.   || old_ppr != ppr_options)) {
  1706. u_int scsirate;
  1707. update_needed++;
  1708. scsirate = tinfo->scsirate;
  1709. if ((ahc->features & AHC_ULTRA2) != 0) {
  1710. scsirate &= ~(SXFR_ULTRA2|SINGLE_EDGE|ENABLE_CRC);
  1711. if (syncrate != NULL) {
  1712. scsirate |= syncrate->sxfr_u2;
  1713. if ((ppr_options & MSG_EXT_PPR_DT_REQ) != 0)
  1714. scsirate |= ENABLE_CRC;
  1715. else
  1716. scsirate |= SINGLE_EDGE;
  1717. }
  1718. } else {
  1719. scsirate &= ~(SXFR|SOFS);
  1720. /*
  1721.  * Ensure Ultra mode is set properly for
  1722.  * this target.
  1723.  */
  1724. tstate->ultraenb &= ~devinfo->target_mask;
  1725. if (syncrate != NULL) {
  1726. if (syncrate->sxfr & ULTRA_SXFR) {
  1727. tstate->ultraenb |=
  1728. devinfo->target_mask;
  1729. }
  1730. scsirate |= syncrate->sxfr & SXFR;
  1731. scsirate |= offset & SOFS;
  1732. }
  1733. if (active) {
  1734. u_int sxfrctl0;
  1735. sxfrctl0 = ahc_inb(ahc, SXFRCTL0);
  1736. sxfrctl0 &= ~FAST20;
  1737. if (tstate->ultraenb & devinfo->target_mask)
  1738. sxfrctl0 |= FAST20;
  1739. ahc_outb(ahc, SXFRCTL0, sxfrctl0);
  1740. }
  1741. }
  1742. if (active) {
  1743. ahc_outb(ahc, SCSIRATE, scsirate);
  1744. if ((ahc->features & AHC_ULTRA2) != 0)
  1745. ahc_outb(ahc, SCSIOFFSET, offset);
  1746. }
  1747. tinfo->scsirate = scsirate;
  1748. tinfo->curr.period = period;
  1749. tinfo->curr.offset = offset;
  1750. tinfo->curr.ppr_options = ppr_options;
  1751. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  1752.        CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
  1753. if (bootverbose) {
  1754. if (offset != 0) {
  1755. printf("%s: target %d synchronous at %sMHz%s, "
  1756.        "offset = 0x%xn", ahc_name(ahc),
  1757.        devinfo->target, syncrate->rate,
  1758.        (ppr_options & MSG_EXT_PPR_DT_REQ)
  1759.        ? " DT" : "", offset);
  1760. } else {
  1761. printf("%s: target %d using "
  1762.        "asynchronous transfersn",
  1763.        ahc_name(ahc), devinfo->target);
  1764. }
  1765. }
  1766. }
  1767. update_needed += ahc_update_neg_request(ahc, devinfo, tstate,
  1768. tinfo, /*force*/FALSE);
  1769. if (update_needed)
  1770. ahc_update_pending_scbs(ahc);
  1771. }
  1772. /*
  1773.  * Update the user/goal/curr tables of wide negotiation
  1774.  * parameters as well as, in the case of a current or active update,
  1775.  * any data structures on the host controller.  In the case of an
  1776.  * active update, the specified target is currently talking to us on
  1777.  * the bus, so the transfer parameter update must take effect
  1778.  * immediately.
  1779.  */
  1780. void
  1781. ahc_set_width(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1782.       u_int width, u_int type, int paused)
  1783. {
  1784. struct ahc_initiator_tinfo *tinfo;
  1785. struct ahc_tmode_tstate *tstate;
  1786. u_int oldwidth;
  1787. int active;
  1788. int update_needed;
  1789. active = (type & AHC_TRANS_ACTIVE) == AHC_TRANS_ACTIVE;
  1790. update_needed = 0;
  1791. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  1792.     devinfo->target, &tstate);
  1793. if ((type & AHC_TRANS_USER) != 0)
  1794. tinfo->user.width = width;
  1795. if ((type & AHC_TRANS_GOAL) != 0)
  1796. tinfo->goal.width = width;
  1797. oldwidth = tinfo->curr.width;
  1798. if ((type & AHC_TRANS_CUR) != 0 && oldwidth != width) {
  1799. u_int scsirate;
  1800. update_needed++;
  1801. scsirate =  tinfo->scsirate;
  1802. scsirate &= ~WIDEXFER;
  1803. if (width == MSG_EXT_WDTR_BUS_16_BIT)
  1804. scsirate |= WIDEXFER;
  1805. tinfo->scsirate = scsirate;
  1806. if (active)
  1807. ahc_outb(ahc, SCSIRATE, scsirate);
  1808. tinfo->curr.width = width;
  1809. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  1810.        CAM_LUN_WILDCARD, AC_TRANSFER_NEG, NULL);
  1811. if (bootverbose) {
  1812. printf("%s: target %d using %dbit transfersn",
  1813.        ahc_name(ahc), devinfo->target,
  1814.        8 * (0x01 << width));
  1815. }
  1816. }
  1817. update_needed += ahc_update_neg_request(ahc, devinfo, tstate,
  1818. tinfo, /*force*/FALSE);
  1819. if (update_needed)
  1820. ahc_update_pending_scbs(ahc);
  1821. }
  1822. /*
  1823.  * Update the current state of tagged queuing for a given target.
  1824.  */
  1825. void
  1826. ahc_set_tags(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1827.      ahc_queue_alg alg)
  1828. {
  1829.   ahc_platform_set_tags(ahc, devinfo, alg);
  1830.   ahc_send_async(ahc, devinfo->channel, devinfo->target,
  1831.          devinfo->lun, AC_TRANSFER_NEG, &alg);
  1832. }
  1833. /*
  1834.  * When the transfer settings for a connection change, update any
  1835.  * in-transit SCBs to contain the new data so the hardware will
  1836.  * be set correctly during future (re)selections.
  1837.  */
  1838. static void
  1839. ahc_update_pending_scbs(struct ahc_softc *ahc)
  1840. {
  1841. struct scb *pending_scb;
  1842. int pending_scb_count;
  1843. int i;
  1844. int paused;
  1845. u_int saved_scbptr;
  1846. /*
  1847.  * Traverse the pending SCB list and ensure that all of the
  1848.  * SCBs there have the proper settings.
  1849.  */
  1850. pending_scb_count = 0;
  1851. LIST_FOREACH(pending_scb, &ahc->pending_scbs, pending_links) {
  1852. struct ahc_devinfo devinfo;
  1853. struct hardware_scb *pending_hscb;
  1854. struct ahc_initiator_tinfo *tinfo;
  1855. struct ahc_tmode_tstate *tstate;
  1856. ahc_scb_devinfo(ahc, &devinfo, pending_scb);
  1857. tinfo = ahc_fetch_transinfo(ahc, devinfo.channel,
  1858.     devinfo.our_scsiid,
  1859.     devinfo.target, &tstate);
  1860. pending_hscb = pending_scb->hscb;
  1861. pending_hscb->control &= ~ULTRAENB;
  1862. if ((tstate->ultraenb & devinfo.target_mask) != 0)
  1863. pending_hscb->control |= ULTRAENB;
  1864. pending_hscb->scsirate = tinfo->scsirate;
  1865. pending_hscb->scsioffset = tinfo->curr.offset;
  1866. if ((tstate->auto_negotiate & devinfo.target_mask) == 0
  1867.  && (pending_scb->flags & SCB_AUTO_NEGOTIATE) != 0) {
  1868. pending_scb->flags &= ~SCB_AUTO_NEGOTIATE;
  1869. pending_hscb->control &= ~MK_MESSAGE;
  1870. }
  1871. ahc_sync_scb(ahc, pending_scb,
  1872.      BUS_DMASYNC_PREREAD|BUS_DMASYNC_PREWRITE);
  1873. pending_scb_count++;
  1874. }
  1875. if (pending_scb_count == 0)
  1876. return;
  1877. if (ahc_is_paused(ahc)) {
  1878. paused = 1;
  1879. } else {
  1880. paused = 0;
  1881. ahc_pause(ahc);
  1882. }
  1883. saved_scbptr = ahc_inb(ahc, SCBPTR);
  1884. /* Ensure that the hscbs down on the card match the new information */
  1885. for (i = 0; i < ahc->scb_data->maxhscbs; i++) {
  1886. struct hardware_scb *pending_hscb;
  1887. u_int control;
  1888. u_int scb_tag;
  1889. ahc_outb(ahc, SCBPTR, i);
  1890. scb_tag = ahc_inb(ahc, SCB_TAG);
  1891. pending_scb = ahc_lookup_scb(ahc, scb_tag);
  1892. if (pending_scb == NULL)
  1893. continue;
  1894. pending_hscb = pending_scb->hscb;
  1895. control = ahc_inb(ahc, SCB_CONTROL);
  1896. control &= ~(ULTRAENB|MK_MESSAGE);
  1897. control |= pending_hscb->control & (ULTRAENB|MK_MESSAGE);
  1898. ahc_outb(ahc, SCB_CONTROL, control);
  1899. ahc_outb(ahc, SCB_SCSIRATE, pending_hscb->scsirate);
  1900. ahc_outb(ahc, SCB_SCSIOFFSET, pending_hscb->scsioffset);
  1901. }
  1902. ahc_outb(ahc, SCBPTR, saved_scbptr);
  1903. if (paused == 0)
  1904. ahc_unpause(ahc);
  1905. }
  1906. /**************************** Pathing Information *****************************/
  1907. static void
  1908. ahc_fetch_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  1909. {
  1910. u_int saved_scsiid;
  1911. role_t role;
  1912. int our_id;
  1913. if (ahc_inb(ahc, SSTAT0) & TARGET)
  1914. role = ROLE_TARGET;
  1915. else
  1916. role = ROLE_INITIATOR;
  1917. if (role == ROLE_TARGET
  1918.  && (ahc->features & AHC_MULTI_TID) != 0
  1919.  && (ahc_inb(ahc, SEQ_FLAGS) & CMDPHASE_PENDING) != 0) {
  1920. /* We were selected, so pull our id from TARGIDIN */
  1921. our_id = ahc_inb(ahc, TARGIDIN) & OID;
  1922. } else if ((ahc->features & AHC_ULTRA2) != 0)
  1923. our_id = ahc_inb(ahc, SCSIID_ULTRA2) & OID;
  1924. else
  1925. our_id = ahc_inb(ahc, SCSIID) & OID;
  1926. saved_scsiid = ahc_inb(ahc, SAVED_SCSIID);
  1927. ahc_compile_devinfo(devinfo,
  1928.     our_id,
  1929.     SCSIID_TARGET(ahc, saved_scsiid),
  1930.     ahc_inb(ahc, SAVED_LUN),
  1931.     SCSIID_CHANNEL(ahc, saved_scsiid),
  1932.     role);
  1933. }
  1934. struct ahc_phase_table_entry*
  1935. ahc_lookup_phase_entry(int phase)
  1936. {
  1937. struct ahc_phase_table_entry *entry;
  1938. struct ahc_phase_table_entry *last_entry;
  1939. /*
  1940.  * num_phases doesn't include the default entry which
  1941.  * will be returned if the phase doesn't match.
  1942.  */
  1943. last_entry = &ahc_phase_table[num_phases];
  1944. for (entry = ahc_phase_table; entry < last_entry; entry++) {
  1945. if (phase == entry->phase)
  1946. break;
  1947. }
  1948. return (entry);
  1949. }
  1950. void
  1951. ahc_compile_devinfo(struct ahc_devinfo *devinfo, u_int our_id, u_int target,
  1952.     u_int lun, char channel, role_t role)
  1953. {
  1954. devinfo->our_scsiid = our_id;
  1955. devinfo->target = target;
  1956. devinfo->lun = lun;
  1957. devinfo->target_offset = target;
  1958. devinfo->channel = channel;
  1959. devinfo->role = role;
  1960. if (channel == 'B')
  1961. devinfo->target_offset += 8;
  1962. devinfo->target_mask = (0x01 << devinfo->target_offset);
  1963. }
  1964. static void
  1965. ahc_scb_devinfo(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1966. struct scb *scb)
  1967. {
  1968. role_t role;
  1969. int our_id;
  1970. our_id = SCSIID_OUR_ID(scb->hscb->scsiid);
  1971. role = ROLE_INITIATOR;
  1972. if ((scb->hscb->control & TARGET_SCB) != 0)
  1973. role = ROLE_TARGET;
  1974. ahc_compile_devinfo(devinfo, our_id, SCB_GET_TARGET(ahc, scb),
  1975.     SCB_GET_LUN(scb), SCB_GET_CHANNEL(ahc, scb), role);
  1976. }
  1977. /************************ Message Phase Processing ****************************/
  1978. static void
  1979. ahc_assert_atn(struct ahc_softc *ahc)
  1980. {
  1981. u_int scsisigo;
  1982. scsisigo = ATNO;
  1983. if ((ahc->features & AHC_DT) == 0)
  1984. scsisigo |= ahc_inb(ahc, SCSISIGI);
  1985. ahc_outb(ahc, SCSISIGO, scsisigo);
  1986. }
  1987. /*
  1988.  * When an initiator transaction with the MK_MESSAGE flag either reconnects
  1989.  * or enters the initial message out phase, we are interrupted.  Fill our
  1990.  * outgoing message buffer with the appropriate message and beging handing
  1991.  * the message phase(s) manually.
  1992.  */
  1993. static void
  1994. ahc_setup_initiator_msgout(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  1995.    struct scb *scb)
  1996. {
  1997. /*
  1998.  * To facilitate adding multiple messages together,
  1999.  * each routine should increment the index and len
  2000.  * variables instead of setting them explicitly.
  2001.  */
  2002. ahc->msgout_index = 0;
  2003. ahc->msgout_len = 0;
  2004. if ((scb->flags & SCB_DEVICE_RESET) == 0
  2005.  && ahc_inb(ahc, MSG_OUT) == MSG_IDENTIFYFLAG) {
  2006. u_int identify_msg;
  2007. identify_msg = MSG_IDENTIFYFLAG | SCB_GET_LUN(scb);
  2008. if ((scb->hscb->control & DISCENB) != 0)
  2009. identify_msg |= MSG_IDENTIFY_DISCFLAG;
  2010. ahc->msgout_buf[ahc->msgout_index++] = identify_msg;
  2011. ahc->msgout_len++;
  2012. if ((scb->hscb->control & TAG_ENB) != 0) {
  2013. ahc->msgout_buf[ahc->msgout_index++] =
  2014.     scb->hscb->control & (TAG_ENB|SCB_TAG_TYPE);
  2015. ahc->msgout_buf[ahc->msgout_index++] = scb->hscb->tag;
  2016. ahc->msgout_len += 2;
  2017. }
  2018. }
  2019. if (scb->flags & SCB_DEVICE_RESET) {
  2020. ahc->msgout_buf[ahc->msgout_index++] = MSG_BUS_DEV_RESET;
  2021. ahc->msgout_len++;
  2022. ahc_print_path(ahc, scb);
  2023. printf("Bus Device Reset Message Sentn");
  2024. /*
  2025.  * Clear our selection hardware in advance of
  2026.  * the busfree.  We may have an entry in the waiting
  2027.  * Q for this target, and we don't want to go about
  2028.  * selecting while we handle the busfree and blow it
  2029.  * away.
  2030.  */
  2031. ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
  2032. } else if ((scb->flags & SCB_ABORT) != 0) {
  2033. if ((scb->hscb->control & TAG_ENB) != 0)
  2034. ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT_TAG;
  2035. else
  2036. ahc->msgout_buf[ahc->msgout_index++] = MSG_ABORT;
  2037. ahc->msgout_len++;
  2038. ahc_print_path(ahc, scb);
  2039. printf("Abort%s Message Sentn",
  2040.        (scb->hscb->control & TAG_ENB) != 0 ? " Tag" : "");
  2041. /*
  2042.  * Clear our selection hardware in advance of
  2043.  * the busfree.  We may have an entry in the waiting
  2044.  * Q for this target, and we don't want to go about
  2045.  * selecting while we handle the busfree and blow it
  2046.  * away.
  2047.  */
  2048. ahc_outb(ahc, SCSISEQ, (ahc_inb(ahc, SCSISEQ) & ~ENSELO));
  2049. } else if ((scb->flags & (SCB_AUTO_NEGOTIATE|SCB_NEGOTIATE)) != 0) {
  2050. ahc_build_transfer_msg(ahc, devinfo);
  2051. } else {
  2052. printf("ahc_intr: AWAITING_MSG for an SCB that "
  2053.        "does not have a waiting messagen");
  2054. printf("SCSIID = %x, target_mask = %xn", scb->hscb->scsiid,
  2055.        devinfo->target_mask);
  2056. panic("SCB = %d, SCB Control = %x, MSG_OUT = %x "
  2057.       "SCB flags = %x", scb->hscb->tag, scb->hscb->control,
  2058.       ahc_inb(ahc, MSG_OUT), scb->flags);
  2059. }
  2060. /*
  2061.  * Clear the MK_MESSAGE flag from the SCB so we aren't
  2062.  * asked to send this message again.
  2063.  */
  2064. ahc_outb(ahc, SCB_CONTROL, ahc_inb(ahc, SCB_CONTROL) & ~MK_MESSAGE);
  2065. scb->hscb->control &= ~MK_MESSAGE;
  2066. ahc->msgout_index = 0;
  2067. ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  2068. }
  2069. /*
  2070.  * Build an appropriate transfer negotiation message for the
  2071.  * currently active target.
  2072.  */
  2073. static void
  2074. ahc_build_transfer_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  2075. {
  2076. /*
  2077.  * We need to initiate transfer negotiations.
  2078.  * If our current and goal settings are identical,
  2079.  * we want to renegotiate due to a check condition.
  2080.  */
  2081. struct ahc_initiator_tinfo *tinfo;
  2082. struct ahc_tmode_tstate *tstate;
  2083. struct ahc_syncrate *rate;
  2084. int dowide;
  2085. int dosync;
  2086. int doppr;
  2087. int use_ppr;
  2088. u_int period;
  2089. u_int ppr_options;
  2090. u_int offset;
  2091. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  2092.     devinfo->target, &tstate);
  2093. /*
  2094.  * Filter our period based on the current connection.
  2095.  * If we can't perform DT transfers on this segment (not in LVD
  2096.  * mode for instance), then our decision to issue a PPR message
  2097.  * may change.
  2098.  */
  2099. period = tinfo->goal.period;
  2100. ppr_options = tinfo->goal.ppr_options;
  2101. /* Target initiated PPR is not allowed in the SCSI spec */
  2102. if (devinfo->role == ROLE_TARGET)
  2103. ppr_options = 0;
  2104. rate = ahc_devlimited_syncrate(ahc, tinfo, &period,
  2105.        &ppr_options, devinfo->role);
  2106. dowide = tinfo->curr.width != tinfo->goal.width;
  2107. dosync = tinfo->curr.period != period;
  2108. doppr = tinfo->curr.ppr_options != ppr_options;
  2109. if (!dowide && !dosync && !doppr) {
  2110. dowide = tinfo->goal.width != MSG_EXT_WDTR_BUS_8_BIT;
  2111. dosync = tinfo->goal.period != 0;
  2112. doppr = tinfo->goal.ppr_options != 0;
  2113. }
  2114. if (!dowide && !dosync && !doppr) {
  2115. panic("ahc_intr: AWAITING_MSG for negotiation, "
  2116.       "but no negotiation neededn");
  2117. }
  2118. use_ppr = (tinfo->curr.transport_version >= 3) || doppr;
  2119. /* Target initiated PPR is not allowed in the SCSI spec */
  2120. if (devinfo->role == ROLE_TARGET)
  2121. use_ppr = 0;
  2122. /*
  2123.  * Both the PPR message and SDTR message require the
  2124.  * goal syncrate to be limited to what the target device
  2125.  * is capable of handling (based on whether an LVD->SE
  2126.  * expander is on the bus), so combine these two cases.
  2127.  * Regardless, guarantee that if we are using WDTR and SDTR
  2128.  * messages that WDTR comes first.
  2129.  */
  2130. if (use_ppr || (dosync && !dowide)) {
  2131. offset = tinfo->goal.offset;
  2132. ahc_validate_offset(ahc, tinfo, rate, &offset,
  2133.     use_ppr ? tinfo->goal.width
  2134.     : tinfo->curr.width,
  2135.     devinfo->role);
  2136. if (use_ppr) {
  2137. ahc_construct_ppr(ahc, devinfo, period, offset,
  2138.   tinfo->goal.width, ppr_options);
  2139. } else {
  2140. ahc_construct_sdtr(ahc, devinfo, period, offset);
  2141. }
  2142. } else {
  2143. ahc_construct_wdtr(ahc, devinfo, tinfo->goal.width);
  2144. }
  2145. }
  2146. /*
  2147.  * Build a synchronous negotiation message in our message
  2148.  * buffer based on the input parameters.
  2149.  */
  2150. static void
  2151. ahc_construct_sdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2152.    u_int period, u_int offset)
  2153. {
  2154. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXTENDED;
  2155. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_SDTR_LEN;
  2156. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_SDTR;
  2157. ahc->msgout_buf[ahc->msgout_index++] = period;
  2158. ahc->msgout_buf[ahc->msgout_index++] = offset;
  2159. ahc->msgout_len += 5;
  2160. if (bootverbose) {
  2161. printf("(%s:%c:%d:%d): Sending SDTR period %x, offset %xn",
  2162.        ahc_name(ahc), devinfo->channel, devinfo->target,
  2163.        devinfo->lun, period, offset);
  2164. }
  2165. }
  2166. /*
  2167.  * Build a wide negotiation message in our message
  2168.  * buffer based on the input parameters.
  2169.  */
  2170. static void
  2171. ahc_construct_wdtr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2172.    u_int bus_width)
  2173. {
  2174. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXTENDED;
  2175. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_WDTR_LEN;
  2176. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_WDTR;
  2177. ahc->msgout_buf[ahc->msgout_index++] = bus_width;
  2178. ahc->msgout_len += 4;
  2179. if (bootverbose) {
  2180. printf("(%s:%c:%d:%d): Sending WDTR %xn",
  2181.        ahc_name(ahc), devinfo->channel, devinfo->target,
  2182.        devinfo->lun, bus_width);
  2183. }
  2184. }
  2185. /*
  2186.  * Build a parallel protocol request message in our message
  2187.  * buffer based on the input parameters.
  2188.  */
  2189. static void
  2190. ahc_construct_ppr(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  2191.   u_int period, u_int offset, u_int bus_width,
  2192.   u_int ppr_options)
  2193. {
  2194. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXTENDED;
  2195. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_PPR_LEN;
  2196. ahc->msgout_buf[ahc->msgout_index++] = MSG_EXT_PPR;
  2197. ahc->msgout_buf[ahc->msgout_index++] = period;
  2198. ahc->msgout_buf[ahc->msgout_index++] = 0;
  2199. ahc->msgout_buf[ahc->msgout_index++] = offset;
  2200. ahc->msgout_buf[ahc->msgout_index++] = bus_width;
  2201. ahc->msgout_buf[ahc->msgout_index++] = ppr_options;
  2202. ahc->msgout_len += 8;
  2203. if (bootverbose) {
  2204. printf("(%s:%c:%d:%d): Sending PPR bus_width %x, period %x, "
  2205.        "offset %x, ppr_options %xn", ahc_name(ahc),
  2206.        devinfo->channel, devinfo->target, devinfo->lun,
  2207.        bus_width, period, offset, ppr_options);
  2208. }
  2209. }
  2210. /*
  2211.  * Clear any active message state.
  2212.  */
  2213. static void
  2214. ahc_clear_msg_state(struct ahc_softc *ahc)
  2215. {
  2216. ahc->msgout_len = 0;
  2217. ahc->msgin_index = 0;
  2218. ahc->msg_type = MSG_TYPE_NONE;
  2219. if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0) {
  2220. /*
  2221.  * The target didn't care to respond to our
  2222.  * message request, so clear ATN.
  2223.  */
  2224. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2225. }
  2226. ahc_outb(ahc, MSG_OUT, MSG_NOOP);
  2227. ahc_outb(ahc, SEQ_FLAGS2,
  2228.  ahc_inb(ahc, SEQ_FLAGS2) & ~TARGET_MSG_PENDING);
  2229. }
  2230. /*
  2231.  * Manual message loop handler.
  2232.  */
  2233. static void
  2234. ahc_handle_message_phase(struct ahc_softc *ahc)
  2235. struct ahc_devinfo devinfo;
  2236. u_int bus_phase;
  2237. int end_session;
  2238. ahc_fetch_devinfo(ahc, &devinfo);
  2239. end_session = FALSE;
  2240. bus_phase = ahc_inb(ahc, SCSISIGI) & PHASE_MASK;
  2241. reswitch:
  2242. switch (ahc->msg_type) {
  2243. case MSG_TYPE_INITIATOR_MSGOUT:
  2244. {
  2245. int lastbyte;
  2246. int phasemis;
  2247. int msgdone;
  2248. if (ahc->msgout_len == 0)
  2249. panic("HOST_MSG_LOOP interrupt with no active message");
  2250. phasemis = bus_phase != P_MESGOUT;
  2251. if (phasemis) {
  2252. if (bus_phase == P_MESGIN) {
  2253. /*
  2254.  * Change gears and see if
  2255.  * this messages is of interest to
  2256.  * us or should be passed back to
  2257.  * the sequencer.
  2258.  */
  2259. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2260. ahc->send_msg_perror = FALSE;
  2261. ahc->msg_type = MSG_TYPE_INITIATOR_MSGIN;
  2262. ahc->msgin_index = 0;
  2263. goto reswitch;
  2264. }
  2265. end_session = TRUE;
  2266. break;
  2267. }
  2268. if (ahc->send_msg_perror) {
  2269. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2270. ahc_outb(ahc, CLRSINT1, CLRREQINIT);
  2271. ahc_outb(ahc, SCSIDATL, MSG_PARITY_ERROR);
  2272. break;
  2273. }
  2274. msgdone = ahc->msgout_index == ahc->msgout_len;
  2275. if (msgdone) {
  2276. /*
  2277.  * The target has requested a retry.
  2278.  * Re-assert ATN, reset our message index to
  2279.  * 0, and try again.
  2280.  */
  2281. ahc->msgout_index = 0;
  2282. ahc_assert_atn(ahc);
  2283. }
  2284. lastbyte = ahc->msgout_index == (ahc->msgout_len - 1);
  2285. if (lastbyte) {
  2286. /* Last byte is signified by dropping ATN */
  2287. ahc_outb(ahc, CLRSINT1, CLRATNO);
  2288. }
  2289. /*
  2290.  * Clear our interrupt status and present
  2291.  * the next byte on the bus.
  2292.  */
  2293. ahc_outb(ahc, CLRSINT1, CLRREQINIT);
  2294. ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
  2295. break;
  2296. }
  2297. case MSG_TYPE_INITIATOR_MSGIN:
  2298. {
  2299. int phasemis;
  2300. int message_done;
  2301. phasemis = bus_phase != P_MESGIN;
  2302. if (phasemis) {
  2303. ahc->msgin_index = 0;
  2304. if (bus_phase == P_MESGOUT
  2305.  && (ahc->send_msg_perror == TRUE
  2306.   || (ahc->msgout_len != 0
  2307.    && ahc->msgout_index == 0))) {
  2308. ahc->msg_type = MSG_TYPE_INITIATOR_MSGOUT;
  2309. goto reswitch;
  2310. }
  2311. end_session = TRUE;
  2312. break;
  2313. }
  2314. /* Pull the byte in without acking it */
  2315. ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIBUSL);
  2316. message_done = ahc_parse_msg(ahc, &devinfo);
  2317. if (message_done) {
  2318. /*
  2319.  * Clear our incoming message buffer in case there
  2320.  * is another message following this one.
  2321.  */
  2322. ahc->msgin_index = 0;
  2323. /*
  2324.  * If this message illicited a response,
  2325.  * assert ATN so the target takes us to the
  2326.  * message out phase.
  2327.  */
  2328. if (ahc->msgout_len != 0)
  2329. ahc_assert_atn(ahc);
  2330. } else 
  2331. ahc->msgin_index++;
  2332. if (message_done == MSGLOOP_TERMINATED) {
  2333. end_session = TRUE;
  2334. } else {
  2335. /* Ack the byte */
  2336. ahc_outb(ahc, CLRSINT1, CLRREQINIT);
  2337. ahc_inb(ahc, SCSIDATL);
  2338. }
  2339. break;
  2340. }
  2341. case MSG_TYPE_TARGET_MSGIN:
  2342. {
  2343. int msgdone;
  2344. int msgout_request;
  2345. if (ahc->msgout_len == 0)
  2346. panic("Target MSGIN with no active message");
  2347. /*
  2348.  * If we interrupted a mesgout session, the initiator
  2349.  * will not know this until our first REQ.  So, we
  2350.  * only honor mesgout requests after we've sent our
  2351.  * first byte.
  2352.  */
  2353. if ((ahc_inb(ahc, SCSISIGI) & ATNI) != 0
  2354.  && ahc->msgout_index > 0)
  2355. msgout_request = TRUE;
  2356. else
  2357. msgout_request = FALSE;
  2358. if (msgout_request) {
  2359. /*
  2360.  * Change gears and see if
  2361.  * this messages is of interest to
  2362.  * us or should be passed back to
  2363.  * the sequencer.
  2364.  */
  2365. ahc->msg_type = MSG_TYPE_TARGET_MSGOUT;
  2366. ahc_outb(ahc, SCSISIGO, P_MESGOUT | BSYO);
  2367. ahc->msgin_index = 0;
  2368. /* Dummy read to REQ for first byte */
  2369. ahc_inb(ahc, SCSIDATL);
  2370. ahc_outb(ahc, SXFRCTL0,
  2371.  ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  2372. break;
  2373. }
  2374. msgdone = ahc->msgout_index == ahc->msgout_len;
  2375. if (msgdone) {
  2376. ahc_outb(ahc, SXFRCTL0,
  2377.  ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
  2378. end_session = TRUE;
  2379. break;
  2380. }
  2381. /*
  2382.  * Present the next byte on the bus.
  2383.  */
  2384. ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  2385. ahc_outb(ahc, SCSIDATL, ahc->msgout_buf[ahc->msgout_index++]);
  2386. break;
  2387. }
  2388. case MSG_TYPE_TARGET_MSGOUT:
  2389. {
  2390. int lastbyte;
  2391. int msgdone;
  2392. /*
  2393.  * The initiator signals that this is
  2394.  * the last byte by dropping ATN.
  2395.  */
  2396. lastbyte = (ahc_inb(ahc, SCSISIGI) & ATNI) == 0;
  2397. /*
  2398.  * Read the latched byte, but turn off SPIOEN first
  2399.  * so that we don't inadvertently cause a REQ for the
  2400.  * next byte.
  2401.  */
  2402. ahc_outb(ahc, SXFRCTL0, ahc_inb(ahc, SXFRCTL0) & ~SPIOEN);
  2403. ahc->msgin_buf[ahc->msgin_index] = ahc_inb(ahc, SCSIDATL);
  2404. msgdone = ahc_parse_msg(ahc, &devinfo);
  2405. if (msgdone == MSGLOOP_TERMINATED) {
  2406. /*
  2407.  * The message is *really* done in that it caused
  2408.  * us to go to bus free.  The sequencer has already
  2409.  * been reset at this point, so pull the ejection
  2410.  * handle.
  2411.  */
  2412. return;
  2413. }
  2414. ahc->msgin_index++;
  2415. /*
  2416.  * XXX Read spec about initiator dropping ATN too soon
  2417.  *     and use msgdone to detect it.
  2418.  */
  2419. if (msgdone == MSGLOOP_MSGCOMPLETE) {
  2420. ahc->msgin_index = 0;
  2421. /*
  2422.  * If this message illicited a response, transition
  2423.  * to the Message in phase and send it.
  2424.  */
  2425. if (ahc->msgout_len != 0) {
  2426. ahc_outb(ahc, SCSISIGO, P_MESGIN | BSYO);
  2427. ahc_outb(ahc, SXFRCTL0,
  2428.  ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  2429. ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
  2430. ahc->msgin_index = 0;
  2431. break;
  2432. }
  2433. }
  2434. if (lastbyte)
  2435. end_session = TRUE;
  2436. else {
  2437. /* Ask for the next byte. */
  2438. ahc_outb(ahc, SXFRCTL0,
  2439.  ahc_inb(ahc, SXFRCTL0) | SPIOEN);
  2440. }
  2441. break;
  2442. }
  2443. default:
  2444. panic("Unknown REQINIT message type");
  2445. }
  2446. if (end_session) {
  2447. ahc_clear_msg_state(ahc);
  2448. ahc_outb(ahc, RETURN_1, EXIT_MSG_LOOP);
  2449. } else
  2450. ahc_outb(ahc, RETURN_1, CONT_MSG_LOOP);
  2451. }
  2452. /*
  2453.  * See if we sent a particular extended message to the target.
  2454.  * If "full" is true, return true only if the target saw the full
  2455.  * message.  If "full" is false, return true if the target saw at
  2456.  * least the first byte of the message.
  2457.  */
  2458. static int
  2459. ahc_sent_msg(struct ahc_softc *ahc, ahc_msgtype type, u_int msgval, int full)
  2460. {
  2461. int found;
  2462. u_int index;
  2463. found = FALSE;
  2464. index = 0;
  2465. while (index < ahc->msgout_len) {
  2466. if (ahc->msgout_buf[index] == MSG_EXTENDED) {
  2467. u_int end_index;
  2468. end_index = index + 1 + ahc->msgout_buf[index + 1];
  2469. if (ahc->msgout_buf[index+2] == msgval
  2470.  && type == AHCMSG_EXT) {
  2471. if (full) {
  2472. if (ahc->msgout_index > end_index)
  2473. found = TRUE;
  2474. } else if (ahc->msgout_index > index)
  2475. found = TRUE;
  2476. }
  2477. index = end_index;
  2478. } else if (ahc->msgout_buf[index] >= MSG_SIMPLE_TASK
  2479. && ahc->msgout_buf[index] <= MSG_IGN_WIDE_RESIDUE) {
  2480. /* Skip tag type and tag id or residue param*/
  2481. index += 2;
  2482. } else {
  2483. /* Single byte message */
  2484. if (type == AHCMSG_1B
  2485.  && ahc->msgout_buf[index] == msgval
  2486.  && ahc->msgout_index > index)
  2487. found = TRUE;
  2488. index++;
  2489. }
  2490. if (found)
  2491. break;
  2492. }
  2493. return (found);
  2494. }
  2495. /*
  2496.  * Wait for a complete incoming message, parse it, and respond accordingly.
  2497.  */
  2498. static int
  2499. ahc_parse_msg(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  2500. {
  2501. struct ahc_initiator_tinfo *tinfo;
  2502. struct ahc_tmode_tstate *tstate;
  2503. int reject;
  2504. int done;
  2505. int response;
  2506. u_int targ_scsirate;
  2507. done = MSGLOOP_IN_PROG;
  2508. response = FALSE;
  2509. reject = FALSE;
  2510. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel, devinfo->our_scsiid,
  2511.     devinfo->target, &tstate);
  2512. targ_scsirate = tinfo->scsirate;
  2513. /*
  2514.  * Parse as much of the message as is availible,
  2515.  * rejecting it if we don't support it.  When
  2516.  * the entire message is availible and has been
  2517.  * handled, return MSGLOOP_MSGCOMPLETE, indicating
  2518.  * that we have parsed an entire message.
  2519.  *
  2520.  * In the case of extended messages, we accept the length
  2521.  * byte outright and perform more checking once we know the
  2522.  * extended message type.
  2523.  */
  2524. switch (ahc->msgin_buf[0]) {
  2525. case MSG_DISCONNECT:
  2526. case MSG_SAVEDATAPOINTER:
  2527. case MSG_CMDCOMPLETE:
  2528. case MSG_RESTOREPOINTERS:
  2529. case MSG_IGN_WIDE_RESIDUE:
  2530. /*
  2531.  * End our message loop as these are messages
  2532.  * the sequencer handles on its own.
  2533.  */
  2534. done = MSGLOOP_TERMINATED;
  2535. break;
  2536. case MSG_MESSAGE_REJECT:
  2537. response = ahc_handle_msg_reject(ahc, devinfo);
  2538. /* FALLTHROUGH */
  2539. case MSG_NOOP:
  2540. done = MSGLOOP_MSGCOMPLETE;
  2541. break;
  2542. case MSG_EXTENDED:
  2543. {
  2544. /* Wait for enough of the message to begin validation */
  2545. if (ahc->msgin_index < 2)
  2546. break;
  2547. switch (ahc->msgin_buf[2]) {
  2548. case MSG_EXT_SDTR:
  2549. {
  2550. struct  ahc_syncrate *syncrate;
  2551. u_int  period;
  2552. u_int  ppr_options;
  2553. u_int  offset;
  2554. u_int  saved_offset;
  2555. if (ahc->msgin_buf[1] != MSG_EXT_SDTR_LEN) {
  2556. reject = TRUE;
  2557. break;
  2558. }
  2559. /*
  2560.  * Wait until we have both args before validating
  2561.  * and acting on this message.
  2562.  *
  2563.  * Add one to MSG_EXT_SDTR_LEN to account for
  2564.  * the extended message preamble.
  2565.  */
  2566. if (ahc->msgin_index < (MSG_EXT_SDTR_LEN + 1))
  2567. break;
  2568. period = ahc->msgin_buf[3];
  2569. ppr_options = 0;
  2570. saved_offset = offset = ahc->msgin_buf[4];
  2571. syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period,
  2572.    &ppr_options,
  2573.    devinfo->role);
  2574. ahc_validate_offset(ahc, tinfo, syncrate, &offset,
  2575.     targ_scsirate & WIDEXFER,
  2576.     devinfo->role);
  2577. if (bootverbose) {
  2578. printf("(%s:%c:%d:%d): Received "
  2579.        "SDTR period %x, offset %xnt"
  2580.        "Filtered to period %x, offset %xn",
  2581.        ahc_name(ahc), devinfo->channel,
  2582.        devinfo->target, devinfo->lun,
  2583.        ahc->msgin_buf[3], saved_offset,
  2584.        period, offset);
  2585. }
  2586. ahc_set_syncrate(ahc, devinfo, 
  2587.  syncrate, period,
  2588.  offset, ppr_options,
  2589.  AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  2590.  /*paused*/TRUE);
  2591. /*
  2592.  * See if we initiated Sync Negotiation
  2593.  * and didn't have to fall down to async
  2594.  * transfers.
  2595.  */
  2596. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, TRUE)) {
  2597. /* We started it */
  2598. if (saved_offset != offset) {
  2599. /* Went too low - force async */
  2600. reject = TRUE;
  2601. }
  2602. } else {
  2603. /*
  2604.  * Send our own SDTR in reply
  2605.  */
  2606. if (bootverbose
  2607.  && devinfo->role == ROLE_INITIATOR) {
  2608. printf("(%s:%c:%d:%d): Target "
  2609.        "Initiated SDTRn",
  2610.        ahc_name(ahc), devinfo->channel,
  2611.        devinfo->target, devinfo->lun);
  2612. }
  2613. ahc->msgout_index = 0;
  2614. ahc->msgout_len = 0;
  2615. ahc_construct_sdtr(ahc, devinfo,
  2616.    period, offset);
  2617. ahc->msgout_index = 0;
  2618. response = TRUE;
  2619. }
  2620. done = MSGLOOP_MSGCOMPLETE;
  2621. break;
  2622. }
  2623. case MSG_EXT_WDTR:
  2624. {
  2625. u_int bus_width;
  2626. u_int saved_width;
  2627. u_int sending_reply;
  2628. sending_reply = FALSE;
  2629. if (ahc->msgin_buf[1] != MSG_EXT_WDTR_LEN) {
  2630. reject = TRUE;
  2631. break;
  2632. }
  2633. /*
  2634.  * Wait until we have our arg before validating
  2635.  * and acting on this message.
  2636.  *
  2637.  * Add one to MSG_EXT_WDTR_LEN to account for
  2638.  * the extended message preamble.
  2639.  */
  2640. if (ahc->msgin_index < (MSG_EXT_WDTR_LEN + 1))
  2641. break;
  2642. bus_width = ahc->msgin_buf[3];
  2643. saved_width = bus_width;
  2644. ahc_validate_width(ahc, tinfo, &bus_width,
  2645.    devinfo->role);
  2646. if (bootverbose) {
  2647. printf("(%s:%c:%d:%d): Received WDTR "
  2648.        "%x filtered to %xn",
  2649.        ahc_name(ahc), devinfo->channel,
  2650.        devinfo->target, devinfo->lun,
  2651.        saved_width, bus_width);
  2652. }
  2653. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, TRUE)) {
  2654. /*
  2655.  * Don't send a WDTR back to the
  2656.  * target, since we asked first.
  2657.  * If the width went higher than our
  2658.  * request, reject it.
  2659.  */
  2660. if (saved_width > bus_width) {
  2661. reject = TRUE;
  2662. printf("(%s:%c:%d:%d): requested %dBit "
  2663.        "transfers.  Rejecting...n",
  2664.        ahc_name(ahc), devinfo->channel,
  2665.        devinfo->target, devinfo->lun,
  2666.        8 * (0x01 << bus_width));
  2667. bus_width = 0;
  2668. }
  2669. } else {
  2670. /*
  2671.  * Send our own WDTR in reply
  2672.  */
  2673. if (bootverbose
  2674.  && devinfo->role == ROLE_INITIATOR) {
  2675. printf("(%s:%c:%d:%d): Target "
  2676.        "Initiated WDTRn",
  2677.        ahc_name(ahc), devinfo->channel,
  2678.        devinfo->target, devinfo->lun);
  2679. }
  2680. ahc->msgout_index = 0;
  2681. ahc->msgout_len = 0;
  2682. ahc_construct_wdtr(ahc, devinfo, bus_width);
  2683. ahc->msgout_index = 0;
  2684. response = TRUE;
  2685. sending_reply = TRUE;
  2686. }
  2687. ahc_set_width(ahc, devinfo, bus_width,
  2688.       AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  2689.       /*paused*/TRUE);
  2690. /* After a wide message, we are async */
  2691. ahc_set_syncrate(ahc, devinfo,
  2692.  /*syncrate*/NULL, /*period*/0,
  2693.  /*offset*/0, /*ppr_options*/0,
  2694.  AHC_TRANS_ACTIVE, /*paused*/TRUE);
  2695. if (sending_reply == FALSE && reject == FALSE) {
  2696. if (tinfo->goal.period) {
  2697. ahc->msgout_index = 0;
  2698. ahc->msgout_len = 0;
  2699. ahc_build_transfer_msg(ahc, devinfo);
  2700. ahc->msgout_index = 0;
  2701. response = TRUE;
  2702. }
  2703. }
  2704. done = MSGLOOP_MSGCOMPLETE;
  2705. break;
  2706. }
  2707. case MSG_EXT_PPR:
  2708. {
  2709. struct ahc_syncrate *syncrate;
  2710. u_int period;
  2711. u_int offset;
  2712. u_int bus_width;
  2713. u_int ppr_options;
  2714. u_int saved_width;
  2715. u_int saved_offset;
  2716. u_int saved_ppr_options;
  2717. if (ahc->msgin_buf[1] != MSG_EXT_PPR_LEN) {
  2718. reject = TRUE;
  2719. break;
  2720. }
  2721. /*
  2722.  * Wait until we have all args before validating
  2723.  * and acting on this message.
  2724.  *
  2725.  * Add one to MSG_EXT_PPR_LEN to account for
  2726.  * the extended message preamble.
  2727.  */
  2728. if (ahc->msgin_index < (MSG_EXT_PPR_LEN + 1))
  2729. break;
  2730. period = ahc->msgin_buf[3];
  2731. offset = ahc->msgin_buf[5];
  2732. bus_width = ahc->msgin_buf[6];
  2733. saved_width = bus_width;
  2734. ppr_options = ahc->msgin_buf[7];
  2735. /*
  2736.  * According to the spec, a DT only
  2737.  * period factor with no DT option
  2738.  * set implies async.
  2739.  */
  2740. if ((ppr_options & MSG_EXT_PPR_DT_REQ) == 0
  2741.  && period == 9)
  2742. offset = 0;
  2743. saved_ppr_options = ppr_options;
  2744. saved_offset = offset;
  2745. /*
  2746.  * Mask out any options we don't support
  2747.  * on any controller.  Transfer options are
  2748.  * only available if we are negotiating wide.
  2749.  */
  2750. ppr_options &= MSG_EXT_PPR_DT_REQ;
  2751. if (bus_width == 0)
  2752. ppr_options = 0;
  2753. ahc_validate_width(ahc, tinfo, &bus_width,
  2754.    devinfo->role);
  2755. syncrate = ahc_devlimited_syncrate(ahc, tinfo, &period,
  2756.    &ppr_options,
  2757.    devinfo->role);
  2758. ahc_validate_offset(ahc, tinfo, syncrate,
  2759.     &offset, bus_width,
  2760.     devinfo->role);
  2761. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, TRUE)) {
  2762. /*
  2763.  * If we are unable to do any of the
  2764.  * requested options (we went too low),
  2765.  * then we'll have to reject the message.
  2766.  */
  2767. if (saved_width > bus_width
  2768.  || saved_offset != offset
  2769.  || saved_ppr_options != ppr_options) {
  2770. reject = TRUE;
  2771. period = 0;
  2772. offset = 0;
  2773. bus_width = 0;
  2774. ppr_options = 0;
  2775. syncrate = NULL;
  2776. }
  2777. } else {
  2778. if (devinfo->role != ROLE_TARGET)
  2779. printf("(%s:%c:%d:%d): Target "
  2780.        "Initiated PPRn",
  2781.        ahc_name(ahc), devinfo->channel,
  2782.        devinfo->target, devinfo->lun);
  2783. else
  2784. printf("(%s:%c:%d:%d): Initiator "
  2785.        "Initiated PPRn",
  2786.        ahc_name(ahc), devinfo->channel,
  2787.        devinfo->target, devinfo->lun);
  2788. ahc->msgout_index = 0;
  2789. ahc->msgout_len = 0;
  2790. ahc_construct_ppr(ahc, devinfo, period, offset,
  2791.   bus_width, ppr_options);
  2792. ahc->msgout_index = 0;
  2793. response = TRUE;
  2794. }
  2795. if (bootverbose) {
  2796. printf("(%s:%c:%d:%d): Received PPR width %x, "
  2797.        "period %x, offset %x,options %xn"
  2798.        "tFiltered to width %x, period %x, "
  2799.        "offset %x, options %xn",
  2800.        ahc_name(ahc), devinfo->channel,
  2801.        devinfo->target, devinfo->lun,
  2802.        saved_width, ahc->msgin_buf[3],
  2803.        saved_offset, saved_ppr_options,
  2804.        bus_width, period, offset, ppr_options);
  2805. }
  2806. ahc_set_width(ahc, devinfo, bus_width,
  2807.       AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  2808.       /*paused*/TRUE);
  2809. ahc_set_syncrate(ahc, devinfo,
  2810.  syncrate, period,
  2811.  offset, ppr_options,
  2812.  AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  2813.  /*paused*/TRUE);
  2814. done = MSGLOOP_MSGCOMPLETE;
  2815. break;
  2816. }
  2817. default:
  2818. /* Unknown extended message.  Reject it. */
  2819. reject = TRUE;
  2820. break;
  2821. }
  2822. break;
  2823. }
  2824. #ifdef AHC_TARGET_MODE
  2825. case MSG_BUS_DEV_RESET:
  2826. ahc_handle_devreset(ahc, devinfo,
  2827.     CAM_BDR_SENT,
  2828.     "Bus Device Reset Received",
  2829.     /*verbose_level*/0);
  2830. ahc_restart(ahc);
  2831. done = MSGLOOP_TERMINATED;
  2832. break;
  2833. case MSG_ABORT_TAG:
  2834. case MSG_ABORT:
  2835. case MSG_CLEAR_QUEUE:
  2836. {
  2837. int tag;
  2838. /* Target mode messages */
  2839. if (devinfo->role != ROLE_TARGET) {
  2840. reject = TRUE;
  2841. break;
  2842. }
  2843. tag = SCB_LIST_NULL;
  2844. if (ahc->msgin_buf[0] == MSG_ABORT_TAG)
  2845. tag = ahc_inb(ahc, INITIATOR_TAG);
  2846. ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
  2847.        devinfo->lun, tag, ROLE_TARGET,
  2848.        CAM_REQ_ABORTED);
  2849. tstate = ahc->enabled_targets[devinfo->our_scsiid];
  2850. if (tstate != NULL) {
  2851. struct ahc_tmode_lstate* lstate;
  2852. lstate = tstate->enabled_luns[devinfo->lun];
  2853. if (lstate != NULL) {
  2854. ahc_queue_lstate_event(ahc, lstate,
  2855.        devinfo->our_scsiid,
  2856.        ahc->msgin_buf[0],
  2857.        /*arg*/tag);
  2858. ahc_send_lstate_events(ahc, lstate);
  2859. }
  2860. }
  2861. ahc_restart(ahc);
  2862. done = MSGLOOP_TERMINATED;
  2863. break;
  2864. }
  2865. #endif
  2866. case MSG_TERM_IO_PROC:
  2867. default:
  2868. reject = TRUE;
  2869. break;
  2870. }
  2871. if (reject) {
  2872. /*
  2873.  * Setup to reject the message.
  2874.  */
  2875. ahc->msgout_index = 0;
  2876. ahc->msgout_len = 1;
  2877. ahc->msgout_buf[0] = MSG_MESSAGE_REJECT;
  2878. done = MSGLOOP_MSGCOMPLETE;
  2879. response = TRUE;
  2880. }
  2881. if (done != MSGLOOP_IN_PROG && !response)
  2882. /* Clear the outgoing message buffer */
  2883. ahc->msgout_len = 0;
  2884. return (done);
  2885. }
  2886. /*
  2887.  * Process a message reject message.
  2888.  */
  2889. static int
  2890. ahc_handle_msg_reject(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  2891. {
  2892. /*
  2893.  * What we care about here is if we had an
  2894.  * outstanding SDTR or WDTR message for this
  2895.  * target.  If we did, this is a signal that
  2896.  * the target is refusing negotiation.
  2897.  */
  2898. struct scb *scb;
  2899. struct ahc_initiator_tinfo *tinfo;
  2900. struct ahc_tmode_tstate *tstate;
  2901. u_int scb_index;
  2902. u_int last_msg;
  2903. int   response = 0;
  2904. scb_index = ahc_inb(ahc, SCB_TAG);
  2905. scb = ahc_lookup_scb(ahc, scb_index);
  2906. tinfo = ahc_fetch_transinfo(ahc, devinfo->channel,
  2907.     devinfo->our_scsiid,
  2908.     devinfo->target, &tstate);
  2909. /* Might be necessary */
  2910. last_msg = ahc_inb(ahc, LAST_MSG);
  2911. if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_PPR, /*full*/FALSE)) {
  2912. /*
  2913.  * Target does not support the PPR message.
  2914.  * Attempt to negotiate SPI-2 style.
  2915.  */
  2916. if (bootverbose) {
  2917. printf("(%s:%c:%d:%d): PPR Rejected. "
  2918.        "Trying WDTR/SDTRn",
  2919.        ahc_name(ahc), devinfo->channel,
  2920.        devinfo->target, devinfo->lun);
  2921. }
  2922. tinfo->goal.ppr_options = 0;
  2923. tinfo->curr.transport_version = 2;
  2924. tinfo->goal.transport_version = 2;
  2925. ahc->msgout_index = 0;
  2926. ahc->msgout_len = 0;
  2927. ahc_build_transfer_msg(ahc, devinfo);
  2928. ahc->msgout_index = 0;
  2929. response = 1;
  2930. } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_WDTR, /*full*/FALSE)) {
  2931. /* note 8bit xfers */
  2932. printf("(%s:%c:%d:%d): refuses WIDE negotiation.  Using "
  2933.        "8bit transfersn", ahc_name(ahc),
  2934.        devinfo->channel, devinfo->target, devinfo->lun);
  2935. ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  2936.       AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  2937.       /*paused*/TRUE);
  2938. /*
  2939.  * No need to clear the sync rate.  If the target
  2940.  * did not accept the command, our syncrate is
  2941.  * unaffected.  If the target started the negotiation,
  2942.  * but rejected our response, we already cleared the
  2943.  * sync rate before sending our WDTR.
  2944.  */
  2945. if (tinfo->goal.period) {
  2946. /* Start the sync negotiation */
  2947. ahc->msgout_index = 0;
  2948. ahc->msgout_len = 0;
  2949. ahc_build_transfer_msg(ahc, devinfo);
  2950. ahc->msgout_index = 0;
  2951. response = 1;
  2952. }
  2953. } else if (ahc_sent_msg(ahc, AHCMSG_EXT, MSG_EXT_SDTR, /*full*/FALSE)) {
  2954. /* note asynch xfers and clear flag */
  2955. ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL, /*period*/0,
  2956.  /*offset*/0, /*ppr_options*/0,
  2957.  AHC_TRANS_ACTIVE|AHC_TRANS_GOAL,
  2958.  /*paused*/TRUE);
  2959. printf("(%s:%c:%d:%d): refuses synchronous negotiation. "
  2960.        "Using asynchronous transfersn",
  2961.        ahc_name(ahc), devinfo->channel,
  2962.        devinfo->target, devinfo->lun);
  2963. } else if ((scb->hscb->control & MSG_SIMPLE_TASK) != 0) {
  2964. int tag_type;
  2965. int mask;
  2966. tag_type = (scb->hscb->control & MSG_SIMPLE_TASK);
  2967. if (tag_type == MSG_SIMPLE_TASK) {
  2968. printf("(%s:%c:%d:%d): refuses tagged commands.  "
  2969.        "Performing non-tagged I/On", ahc_name(ahc),
  2970.        devinfo->channel, devinfo->target, devinfo->lun);
  2971. ahc_set_tags(ahc, devinfo, AHC_QUEUE_NONE);
  2972. mask = ~0x23;
  2973. } else {
  2974. printf("(%s:%c:%d:%d): refuses %s tagged commands.  "
  2975.        "Performing simple queue tagged I/O onlyn",
  2976.        ahc_name(ahc), devinfo->channel, devinfo->target,
  2977.        devinfo->lun, tag_type == MSG_ORDERED_TASK
  2978.        ? "ordered" : "head of queue");
  2979. ahc_set_tags(ahc, devinfo, AHC_QUEUE_BASIC);
  2980. mask = ~0x03;
  2981. }
  2982. /*
  2983.  * Resend the identify for this CCB as the target
  2984.  * may believe that the selection is invalid otherwise.
  2985.  */
  2986. ahc_outb(ahc, SCB_CONTROL,
  2987.  ahc_inb(ahc, SCB_CONTROL) & mask);
  2988.   scb->hscb->control &= mask;
  2989. ahc_set_transaction_tag(scb, /*enabled*/FALSE,
  2990. /*type*/MSG_SIMPLE_TASK);
  2991. ahc_outb(ahc, MSG_OUT, MSG_IDENTIFYFLAG);
  2992. ahc_assert_atn(ahc);
  2993. /*
  2994.  * This transaction is now at the head of
  2995.  * the untagged queue for this target.
  2996.  */
  2997. if ((ahc->flags & AHC_SCB_BTT) == 0) {
  2998. struct scb_tailq *untagged_q;
  2999. untagged_q =
  3000.     &(ahc->untagged_queues[devinfo->target_offset]);
  3001. TAILQ_INSERT_HEAD(untagged_q, scb, links.tqe);
  3002. scb->flags |= SCB_UNTAGGEDQ;
  3003. }
  3004. ahc_busy_tcl(ahc, BUILD_TCL(scb->hscb->scsiid, devinfo->lun),
  3005.      scb->hscb->tag);
  3006. /*
  3007.  * Requeue all tagged commands for this target
  3008.  * currently in our posession so they can be
  3009.  * converted to untagged commands.
  3010.  */
  3011. ahc_search_qinfifo(ahc, SCB_GET_TARGET(ahc, scb),
  3012.    SCB_GET_CHANNEL(ahc, scb),
  3013.    SCB_GET_LUN(scb), /*tag*/SCB_LIST_NULL,
  3014.    ROLE_INITIATOR, CAM_REQUEUE_REQ,
  3015.    SEARCH_COMPLETE);
  3016. } else {
  3017. /*
  3018.  * Otherwise, we ignore it.
  3019.  */
  3020. printf("%s:%c:%d: Message reject for %x -- ignoredn",
  3021.        ahc_name(ahc), devinfo->channel, devinfo->target,
  3022.        last_msg);
  3023. }
  3024. return (response);
  3025. }
  3026. /*
  3027.  * Process an ingnore wide residue message.
  3028.  */
  3029. static void
  3030. ahc_handle_ign_wide_residue(struct ahc_softc *ahc, struct ahc_devinfo *devinfo)
  3031. {
  3032. u_int scb_index;
  3033. struct scb *scb;
  3034. scb_index = ahc_inb(ahc, SCB_TAG);
  3035. scb = ahc_lookup_scb(ahc, scb_index);
  3036. /*
  3037.  * XXX Actually check data direction in the sequencer?
  3038.  * Perhaps add datadir to some spare bits in the hscb?
  3039.  */
  3040. if ((ahc_inb(ahc, SEQ_FLAGS) & DPHASE) == 0
  3041.  || ahc_get_transfer_dir(scb) != CAM_DIR_IN) {
  3042. /*
  3043.  * Ignore the message if we haven't
  3044.  * seen an appropriate data phase yet.
  3045.  */
  3046. } else {
  3047. /*
  3048.  * If the residual occurred on the last
  3049.  * transfer and the transfer request was
  3050.  * expected to end on an odd count, do
  3051.  * nothing.  Otherwise, subtract a byte
  3052.  * and update the residual count accordingly.
  3053.  */
  3054. uint32_t sgptr;
  3055. sgptr = ahc_inb(ahc, SCB_RESIDUAL_SGPTR);
  3056. if ((sgptr & SG_LIST_NULL) != 0
  3057.  && ahc_inb(ahc, DATA_COUNT_ODD) == 1) {
  3058. /*
  3059.  * If the residual occurred on the last
  3060.  * transfer and the transfer request was
  3061.  * expected to end on an odd count, do
  3062.  * nothing.
  3063.  */
  3064. } else {
  3065. struct ahc_dma_seg *sg;
  3066. uint32_t data_cnt;
  3067. uint32_t data_addr;
  3068. uint32_t sglen;
  3069. /* Pull in the rest of the sgptr */
  3070. sgptr |= (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 3) << 24)
  3071.       | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 2) << 16)
  3072.       | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 1) << 8);
  3073. sgptr &= SG_PTR_MASK;
  3074. data_cnt = (ahc_inb(ahc, SCB_RESIDUAL_DATACNT+3) << 24)
  3075.  | (ahc_inb(ahc, SCB_RESIDUAL_DATACNT+2) << 16)
  3076.  | (ahc_inb(ahc, SCB_RESIDUAL_DATACNT+1) << 8)
  3077.  | (ahc_inb(ahc, SCB_RESIDUAL_DATACNT));
  3078. data_addr = (ahc_inb(ahc, SHADDR + 3) << 24)
  3079.   | (ahc_inb(ahc, SHADDR + 2) << 16)
  3080.   | (ahc_inb(ahc, SHADDR + 1) << 8)
  3081.   | (ahc_inb(ahc, SHADDR));
  3082. data_cnt += 1;
  3083. data_addr -= 1;
  3084. sg = ahc_sg_bus_to_virt(scb, sgptr);
  3085. /*
  3086.  * The residual sg ptr points to the next S/G
  3087.  * to load so we must go back one.
  3088.  */
  3089. sg--;
  3090. sglen = ahc_le32toh(sg->len) & AHC_SG_LEN_MASK;
  3091. if (sg != scb->sg_list
  3092.  && sglen < (data_cnt & AHC_SG_LEN_MASK)) {
  3093. sg--;
  3094. sglen = ahc_le32toh(sg->len);
  3095. /*
  3096.  * Preserve High Address and SG_LIST bits
  3097.  * while setting the count to 1.
  3098.  */
  3099. data_cnt = 1 | (sglen & (~AHC_SG_LEN_MASK));
  3100. data_addr = ahc_le32toh(sg->addr)
  3101.   + (sglen & AHC_SG_LEN_MASK) - 1;
  3102. /*
  3103.  * Increment sg so it points to the
  3104.  * "next" sg.
  3105.  */
  3106. sg++;
  3107. sgptr = ahc_sg_virt_to_bus(scb, sg);
  3108. ahc_outb(ahc, SCB_RESIDUAL_SGPTR + 3,
  3109.  sgptr >> 24);
  3110. ahc_outb(ahc, SCB_RESIDUAL_SGPTR + 2,
  3111.  sgptr >> 16);
  3112. ahc_outb(ahc, SCB_RESIDUAL_SGPTR + 1,
  3113.  sgptr >> 8);
  3114. ahc_outb(ahc, SCB_RESIDUAL_SGPTR, sgptr);
  3115. }
  3116. ahc_outb(ahc, SCB_RESIDUAL_DATACNT + 3, data_cnt >> 24);
  3117. ahc_outb(ahc, SCB_RESIDUAL_DATACNT + 2, data_cnt >> 16);
  3118. ahc_outb(ahc, SCB_RESIDUAL_DATACNT + 1, data_cnt >> 8);
  3119. ahc_outb(ahc, SCB_RESIDUAL_DATACNT, data_cnt);
  3120. }
  3121. }
  3122. }
  3123. /*
  3124.  * Reinitialize the data pointers for the active transfer
  3125.  * based on its current residual.
  3126.  */
  3127. static void
  3128. ahc_reinitialize_dataptrs(struct ahc_softc *ahc)
  3129. {
  3130. struct  scb *scb;
  3131. struct  ahc_dma_seg *sg;
  3132. u_int  scb_index;
  3133. uint32_t sgptr;
  3134. uint32_t resid;
  3135. uint32_t dataptr;
  3136. scb_index = ahc_inb(ahc, SCB_TAG);
  3137. scb = ahc_lookup_scb(ahc, scb_index);
  3138. sgptr = (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 3) << 24)
  3139.       | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 2) << 16)
  3140.       | (ahc_inb(ahc, SCB_RESIDUAL_SGPTR + 1) << 8)
  3141.       | ahc_inb(ahc, SCB_RESIDUAL_SGPTR);
  3142. sgptr &= SG_PTR_MASK;
  3143. sg = ahc_sg_bus_to_virt(scb, sgptr);
  3144. /* The residual sg_ptr always points to the next sg */
  3145. sg--;
  3146. resid = (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 2) << 16)
  3147.       | (ahc_inb(ahc, SCB_RESIDUAL_DATACNT + 1) << 8)
  3148.       | ahc_inb(ahc, SCB_RESIDUAL_DATACNT);
  3149. dataptr = ahc_le32toh(sg->addr)
  3150. + (ahc_le32toh(sg->len) & AHC_SG_LEN_MASK)
  3151. - resid;
  3152. if ((ahc->flags & AHC_39BIT_ADDRESSING) != 0) {
  3153. u_int dscommand1;
  3154. dscommand1 = ahc_inb(ahc, DSCOMMAND1);
  3155. ahc_outb(ahc, DSCOMMAND1, dscommand1 | HADDLDSEL0);
  3156. ahc_outb(ahc, HADDR,
  3157.  (ahc_le32toh(sg->len) >> 24) & SG_HIGH_ADDR_BITS);
  3158. ahc_outb(ahc, DSCOMMAND1, dscommand1);
  3159. }
  3160. ahc_outb(ahc, HADDR + 3, dataptr >> 24);
  3161. ahc_outb(ahc, HADDR + 2, dataptr >> 16);
  3162. ahc_outb(ahc, HADDR + 1, dataptr >> 8);
  3163. ahc_outb(ahc, HADDR, dataptr);
  3164. ahc_outb(ahc, HCNT + 2, resid >> 16);
  3165. ahc_outb(ahc, HCNT + 1, resid >> 8);
  3166. ahc_outb(ahc, HCNT, resid);
  3167. if ((ahc->features & AHC_ULTRA2) == 0) {
  3168. ahc_outb(ahc, STCNT + 2, resid >> 16);
  3169. ahc_outb(ahc, STCNT + 1, resid >> 8);
  3170. ahc_outb(ahc, STCNT, resid);
  3171. }
  3172. }
  3173. /*
  3174.  * Handle the effects of issuing a bus device reset message.
  3175.  */
  3176. static void
  3177. ahc_handle_devreset(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  3178.     cam_status status, char *message, int verbose_level)
  3179. {
  3180. #ifdef AHC_TARGET_MODE
  3181. struct ahc_tmode_tstate* tstate;
  3182. u_int lun;
  3183. #endif
  3184. int found;
  3185. found = ahc_abort_scbs(ahc, devinfo->target, devinfo->channel,
  3186.        CAM_LUN_WILDCARD, SCB_LIST_NULL, devinfo->role,
  3187.        status);
  3188. #ifdef AHC_TARGET_MODE
  3189. /*
  3190.  * Send an immediate notify ccb to all target mord peripheral
  3191.  * drivers affected by this action.
  3192.  */
  3193. tstate = ahc->enabled_targets[devinfo->our_scsiid];
  3194. if (tstate != NULL) {
  3195. for (lun = 0; lun < AHC_NUM_LUNS; lun++) {
  3196. struct ahc_tmode_lstate* lstate;
  3197. lstate = tstate->enabled_luns[lun];
  3198. if (lstate == NULL)
  3199. continue;
  3200. ahc_queue_lstate_event(ahc, lstate, devinfo->our_scsiid,
  3201.        MSG_BUS_DEV_RESET, /*arg*/0);
  3202. ahc_send_lstate_events(ahc, lstate);
  3203. }
  3204. }
  3205. #endif
  3206. /*
  3207.  * Go back to async/narrow transfers and renegotiate.
  3208.  */
  3209. ahc_set_width(ahc, devinfo, MSG_EXT_WDTR_BUS_8_BIT,
  3210.       AHC_TRANS_CUR, /*paused*/TRUE);
  3211. ahc_set_syncrate(ahc, devinfo, /*syncrate*/NULL,
  3212.  /*period*/0, /*offset*/0, /*ppr_options*/0,
  3213.  AHC_TRANS_CUR, /*paused*/TRUE);
  3214. ahc_send_async(ahc, devinfo->channel, devinfo->target,
  3215.        CAM_LUN_WILDCARD, AC_SENT_BDR, NULL);
  3216. if (message != NULL
  3217.  && (verbose_level <= bootverbose))
  3218. printf("%s: %s on %c:%d. %d SCBs abortedn", ahc_name(ahc),
  3219.        message, devinfo->channel, devinfo->target, found);
  3220. }
  3221. #ifdef AHC_TARGET_MODE
  3222. static void
  3223. ahc_setup_target_msgin(struct ahc_softc *ahc, struct ahc_devinfo *devinfo,
  3224.        struct scb *scb)
  3225. {
  3226. /*              
  3227.  * To facilitate adding multiple messages together,
  3228.  * each routine should increment the index and len
  3229.  * variables instead of setting them explicitly.
  3230.  */             
  3231. ahc->msgout_index = 0;
  3232. ahc->msgout_len = 0;
  3233. if (scb != NULL && (scb->flags & SCB_AUTO_NEGOTIATE) != 0)
  3234. ahc_build_transfer_msg(ahc, devinfo);
  3235. else
  3236. panic("ahc_intr: AWAITING target message with no message");
  3237. ahc->msgout_index = 0;
  3238. ahc->msg_type = MSG_TYPE_TARGET_MSGIN;
  3239. }
  3240. #endif
  3241. /**************************** Initialization **********************************/
  3242. /*
  3243.  * Allocate a controller structure for a new device
  3244.  * and perform initial initializion.
  3245.  */
  3246. struct ahc_softc *
  3247. ahc_alloc(void *platform_arg, char *name)
  3248. {
  3249. struct  ahc_softc *ahc;
  3250. int i;
  3251. #ifndef __FreeBSD__
  3252. ahc = malloc(sizeof(*ahc), M_DEVBUF, M_NOWAIT);
  3253. if (!ahc) {
  3254. printf("aic7xxx: cannot malloc softc!n");
  3255. free(name, M_DEVBUF);
  3256. return NULL;
  3257. }
  3258. #else
  3259. ahc = device_get_softc((device_t)platform_arg);
  3260. #endif
  3261. memset(ahc, 0, sizeof(*ahc));
  3262. ahc->seep_config = malloc(sizeof(*ahc->seep_config),
  3263.   M_DEVBUF, M_NOWAIT);
  3264. if (ahc->seep_config == NULL) {