/kern_oII/drivers/scsi/sym53c8xx_2/sym_hipd.c

http://omnia2droid.googlecode.com/ · C · 5840 lines · 3185 code · 571 blank · 2084 comment · 818 complexity · 607009861f7d5813b3cca1d9d87f7518 MD5 · raw file

  1. /*
  2. * Device driver for the SYMBIOS/LSILOGIC 53C8XX and 53C1010 family
  3. * of PCI-SCSI IO processors.
  4. *
  5. * Copyright (C) 1999-2001 Gerard Roudier <groudier@free.fr>
  6. * Copyright (c) 2003-2005 Matthew Wilcox <matthew@wil.cx>
  7. *
  8. * This driver is derived from the Linux sym53c8xx driver.
  9. * Copyright (C) 1998-2000 Gerard Roudier
  10. *
  11. * The sym53c8xx driver is derived from the ncr53c8xx driver that had been
  12. * a port of the FreeBSD ncr driver to Linux-1.2.13.
  13. *
  14. * The original ncr driver has been written for 386bsd and FreeBSD by
  15. * Wolfgang Stanglmeier <wolf@cologne.de>
  16. * Stefan Esser <se@mi.Uni-Koeln.de>
  17. * Copyright (C) 1994 Wolfgang Stanglmeier
  18. *
  19. * Other major contributions:
  20. *
  21. * NVRAM detection and reading.
  22. * Copyright (C) 1997 Richard Waltham <dormouse@farsrobt.demon.co.uk>
  23. *
  24. *-----------------------------------------------------------------------------
  25. *
  26. * This program is free software; you can redistribute it and/or modify
  27. * it under the terms of the GNU General Public License as published by
  28. * the Free Software Foundation; either version 2 of the License, or
  29. * (at your option) any later version.
  30. *
  31. * This program is distributed in the hope that it will be useful,
  32. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  33. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  34. * GNU General Public License for more details.
  35. *
  36. * You should have received a copy of the GNU General Public License
  37. * along with this program; if not, write to the Free Software
  38. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  39. */
  40. #include <linux/slab.h>
  41. #include <asm/param.h> /* for timeouts in units of HZ */
  42. #include "sym_glue.h"
  43. #include "sym_nvram.h"
  44. #if 0
  45. #define SYM_DEBUG_GENERIC_SUPPORT
  46. #endif
  47. /*
  48. * Needed function prototypes.
  49. */
  50. static void sym_int_ma (struct sym_hcb *np);
  51. static void sym_int_sir(struct sym_hcb *);
  52. static struct sym_ccb *sym_alloc_ccb(struct sym_hcb *np);
  53. static struct sym_ccb *sym_ccb_from_dsa(struct sym_hcb *np, u32 dsa);
  54. static void sym_alloc_lcb_tags (struct sym_hcb *np, u_char tn, u_char ln);
  55. static void sym_complete_error (struct sym_hcb *np, struct sym_ccb *cp);
  56. static void sym_complete_ok (struct sym_hcb *np, struct sym_ccb *cp);
  57. static int sym_compute_residual(struct sym_hcb *np, struct sym_ccb *cp);
  58. /*
  59. * Print a buffer in hexadecimal format with a ".\n" at end.
  60. */
  61. static void sym_printl_hex(u_char *p, int n)
  62. {
  63. while (n-- > 0)
  64. printf (" %x", *p++);
  65. printf (".\n");
  66. }
  67. static void sym_print_msg(struct sym_ccb *cp, char *label, u_char *msg)
  68. {
  69. if (label)
  70. sym_print_addr(cp->cmd, "%s: ", label);
  71. else
  72. sym_print_addr(cp->cmd, "");
  73. spi_print_msg(msg);
  74. printf("\n");
  75. }
  76. static void sym_print_nego_msg(struct sym_hcb *np, int target, char *label, u_char *msg)
  77. {
  78. struct sym_tcb *tp = &np->target[target];
  79. dev_info(&tp->starget->dev, "%s: ", label);
  80. spi_print_msg(msg);
  81. printf("\n");
  82. }
  83. /*
  84. * Print something that tells about extended errors.
  85. */
  86. void sym_print_xerr(struct scsi_cmnd *cmd, int x_status)
  87. {
  88. if (x_status & XE_PARITY_ERR) {
  89. sym_print_addr(cmd, "unrecovered SCSI parity error.\n");
  90. }
  91. if (x_status & XE_EXTRA_DATA) {
  92. sym_print_addr(cmd, "extraneous data discarded.\n");
  93. }
  94. if (x_status & XE_BAD_PHASE) {
  95. sym_print_addr(cmd, "illegal scsi phase (4/5).\n");
  96. }
  97. if (x_status & XE_SODL_UNRUN) {
  98. sym_print_addr(cmd, "ODD transfer in DATA OUT phase.\n");
  99. }
  100. if (x_status & XE_SWIDE_OVRUN) {
  101. sym_print_addr(cmd, "ODD transfer in DATA IN phase.\n");
  102. }
  103. }
  104. /*
  105. * Return a string for SCSI BUS mode.
  106. */
  107. static char *sym_scsi_bus_mode(int mode)
  108. {
  109. switch(mode) {
  110. case SMODE_HVD: return "HVD";
  111. case SMODE_SE: return "SE";
  112. case SMODE_LVD: return "LVD";
  113. }
  114. return "??";
  115. }
  116. /*
  117. * Soft reset the chip.
  118. *
  119. * Raising SRST when the chip is running may cause
  120. * problems on dual function chips (see below).
  121. * On the other hand, LVD devices need some delay
  122. * to settle and report actual BUS mode in STEST4.
  123. */
  124. static void sym_chip_reset (struct sym_hcb *np)
  125. {
  126. OUTB(np, nc_istat, SRST);
  127. INB(np, nc_mbox1);
  128. udelay(10);
  129. OUTB(np, nc_istat, 0);
  130. INB(np, nc_mbox1);
  131. udelay(2000); /* For BUS MODE to settle */
  132. }
  133. /*
  134. * Really soft reset the chip.:)
  135. *
  136. * Some 896 and 876 chip revisions may hang-up if we set
  137. * the SRST (soft reset) bit at the wrong time when SCRIPTS
  138. * are running.
  139. * So, we need to abort the current operation prior to
  140. * soft resetting the chip.
  141. */
  142. static void sym_soft_reset (struct sym_hcb *np)
  143. {
  144. u_char istat = 0;
  145. int i;
  146. if (!(np->features & FE_ISTAT1) || !(INB(np, nc_istat1) & SCRUN))
  147. goto do_chip_reset;
  148. OUTB(np, nc_istat, CABRT);
  149. for (i = 100000 ; i ; --i) {
  150. istat = INB(np, nc_istat);
  151. if (istat & SIP) {
  152. INW(np, nc_sist);
  153. }
  154. else if (istat & DIP) {
  155. if (INB(np, nc_dstat) & ABRT)
  156. break;
  157. }
  158. udelay(5);
  159. }
  160. OUTB(np, nc_istat, 0);
  161. if (!i)
  162. printf("%s: unable to abort current chip operation, "
  163. "ISTAT=0x%02x.\n", sym_name(np), istat);
  164. do_chip_reset:
  165. sym_chip_reset(np);
  166. }
  167. /*
  168. * Start reset process.
  169. *
  170. * The interrupt handler will reinitialize the chip.
  171. */
  172. static void sym_start_reset(struct sym_hcb *np)
  173. {
  174. sym_reset_scsi_bus(np, 1);
  175. }
  176. int sym_reset_scsi_bus(struct sym_hcb *np, int enab_int)
  177. {
  178. u32 term;
  179. int retv = 0;
  180. sym_soft_reset(np); /* Soft reset the chip */
  181. if (enab_int)
  182. OUTW(np, nc_sien, RST);
  183. /*
  184. * Enable Tolerant, reset IRQD if present and
  185. * properly set IRQ mode, prior to resetting the bus.
  186. */
  187. OUTB(np, nc_stest3, TE);
  188. OUTB(np, nc_dcntl, (np->rv_dcntl & IRQM));
  189. OUTB(np, nc_scntl1, CRST);
  190. INB(np, nc_mbox1);
  191. udelay(200);
  192. if (!SYM_SETUP_SCSI_BUS_CHECK)
  193. goto out;
  194. /*
  195. * Check for no terminators or SCSI bus shorts to ground.
  196. * Read SCSI data bus, data parity bits and control signals.
  197. * We are expecting RESET to be TRUE and other signals to be
  198. * FALSE.
  199. */
  200. term = INB(np, nc_sstat0);
  201. term = ((term & 2) << 7) + ((term & 1) << 17); /* rst sdp0 */
  202. term |= ((INB(np, nc_sstat2) & 0x01) << 26) | /* sdp1 */
  203. ((INW(np, nc_sbdl) & 0xff) << 9) | /* d7-0 */
  204. ((INW(np, nc_sbdl) & 0xff00) << 10) | /* d15-8 */
  205. INB(np, nc_sbcl); /* req ack bsy sel atn msg cd io */
  206. if (!np->maxwide)
  207. term &= 0x3ffff;
  208. if (term != (2<<7)) {
  209. printf("%s: suspicious SCSI data while resetting the BUS.\n",
  210. sym_name(np));
  211. printf("%s: %sdp0,d7-0,rst,req,ack,bsy,sel,atn,msg,c/d,i/o = "
  212. "0x%lx, expecting 0x%lx\n",
  213. sym_name(np),
  214. (np->features & FE_WIDE) ? "dp1,d15-8," : "",
  215. (u_long)term, (u_long)(2<<7));
  216. if (SYM_SETUP_SCSI_BUS_CHECK == 1)
  217. retv = 1;
  218. }
  219. out:
  220. OUTB(np, nc_scntl1, 0);
  221. return retv;
  222. }
  223. /*
  224. * Select SCSI clock frequency
  225. */
  226. static void sym_selectclock(struct sym_hcb *np, u_char scntl3)
  227. {
  228. /*
  229. * If multiplier not present or not selected, leave here.
  230. */
  231. if (np->multiplier <= 1) {
  232. OUTB(np, nc_scntl3, scntl3);
  233. return;
  234. }
  235. if (sym_verbose >= 2)
  236. printf ("%s: enabling clock multiplier\n", sym_name(np));
  237. OUTB(np, nc_stest1, DBLEN); /* Enable clock multiplier */
  238. /*
  239. * Wait for the LCKFRQ bit to be set if supported by the chip.
  240. * Otherwise wait 50 micro-seconds (at least).
  241. */
  242. if (np->features & FE_LCKFRQ) {
  243. int i = 20;
  244. while (!(INB(np, nc_stest4) & LCKFRQ) && --i > 0)
  245. udelay(20);
  246. if (!i)
  247. printf("%s: the chip cannot lock the frequency\n",
  248. sym_name(np));
  249. } else {
  250. INB(np, nc_mbox1);
  251. udelay(50+10);
  252. }
  253. OUTB(np, nc_stest3, HSC); /* Halt the scsi clock */
  254. OUTB(np, nc_scntl3, scntl3);
  255. OUTB(np, nc_stest1, (DBLEN|DBLSEL));/* Select clock multiplier */
  256. OUTB(np, nc_stest3, 0x00); /* Restart scsi clock */
  257. }
  258. /*
  259. * Determine the chip's clock frequency.
  260. *
  261. * This is essential for the negotiation of the synchronous
  262. * transfer rate.
  263. *
  264. * Note: we have to return the correct value.
  265. * THERE IS NO SAFE DEFAULT VALUE.
  266. *
  267. * Most NCR/SYMBIOS boards are delivered with a 40 Mhz clock.
  268. * 53C860 and 53C875 rev. 1 support fast20 transfers but
  269. * do not have a clock doubler and so are provided with a
  270. * 80 MHz clock. All other fast20 boards incorporate a doubler
  271. * and so should be delivered with a 40 MHz clock.
  272. * The recent fast40 chips (895/896/895A/1010) use a 40 Mhz base
  273. * clock and provide a clock quadrupler (160 Mhz).
  274. */
  275. /*
  276. * calculate SCSI clock frequency (in KHz)
  277. */
  278. static unsigned getfreq (struct sym_hcb *np, int gen)
  279. {
  280. unsigned int ms = 0;
  281. unsigned int f;
  282. /*
  283. * Measure GEN timer delay in order
  284. * to calculate SCSI clock frequency
  285. *
  286. * This code will never execute too
  287. * many loop iterations (if DELAY is
  288. * reasonably correct). It could get
  289. * too low a delay (too high a freq.)
  290. * if the CPU is slow executing the
  291. * loop for some reason (an NMI, for
  292. * example). For this reason we will
  293. * if multiple measurements are to be
  294. * performed trust the higher delay
  295. * (lower frequency returned).
  296. */
  297. OUTW(np, nc_sien, 0); /* mask all scsi interrupts */
  298. INW(np, nc_sist); /* clear pending scsi interrupt */
  299. OUTB(np, nc_dien, 0); /* mask all dma interrupts */
  300. INW(np, nc_sist); /* another one, just to be sure :) */
  301. /*
  302. * The C1010-33 core does not report GEN in SIST,
  303. * if this interrupt is masked in SIEN.
  304. * I don't know yet if the C1010-66 behaves the same way.
  305. */
  306. if (np->features & FE_C10) {
  307. OUTW(np, nc_sien, GEN);
  308. OUTB(np, nc_istat1, SIRQD);
  309. }
  310. OUTB(np, nc_scntl3, 4); /* set pre-scaler to divide by 3 */
  311. OUTB(np, nc_stime1, 0); /* disable general purpose timer */
  312. OUTB(np, nc_stime1, gen); /* set to nominal delay of 1<<gen * 125us */
  313. while (!(INW(np, nc_sist) & GEN) && ms++ < 100000)
  314. udelay(1000/4); /* count in 1/4 of ms */
  315. OUTB(np, nc_stime1, 0); /* disable general purpose timer */
  316. /*
  317. * Undo C1010-33 specific settings.
  318. */
  319. if (np->features & FE_C10) {
  320. OUTW(np, nc_sien, 0);
  321. OUTB(np, nc_istat1, 0);
  322. }
  323. /*
  324. * set prescaler to divide by whatever 0 means
  325. * 0 ought to choose divide by 2, but appears
  326. * to set divide by 3.5 mode in my 53c810 ...
  327. */
  328. OUTB(np, nc_scntl3, 0);
  329. /*
  330. * adjust for prescaler, and convert into KHz
  331. */
  332. f = ms ? ((1 << gen) * (4340*4)) / ms : 0;
  333. /*
  334. * The C1010-33 result is biased by a factor
  335. * of 2/3 compared to earlier chips.
  336. */
  337. if (np->features & FE_C10)
  338. f = (f * 2) / 3;
  339. if (sym_verbose >= 2)
  340. printf ("%s: Delay (GEN=%d): %u msec, %u KHz\n",
  341. sym_name(np), gen, ms/4, f);
  342. return f;
  343. }
  344. static unsigned sym_getfreq (struct sym_hcb *np)
  345. {
  346. u_int f1, f2;
  347. int gen = 8;
  348. getfreq (np, gen); /* throw away first result */
  349. f1 = getfreq (np, gen);
  350. f2 = getfreq (np, gen);
  351. if (f1 > f2) f1 = f2; /* trust lower result */
  352. return f1;
  353. }
  354. /*
  355. * Get/probe chip SCSI clock frequency
  356. */
  357. static void sym_getclock (struct sym_hcb *np, int mult)
  358. {
  359. unsigned char scntl3 = np->sv_scntl3;
  360. unsigned char stest1 = np->sv_stest1;
  361. unsigned f1;
  362. np->multiplier = 1;
  363. f1 = 40000;
  364. /*
  365. * True with 875/895/896/895A with clock multiplier selected
  366. */
  367. if (mult > 1 && (stest1 & (DBLEN+DBLSEL)) == DBLEN+DBLSEL) {
  368. if (sym_verbose >= 2)
  369. printf ("%s: clock multiplier found\n", sym_name(np));
  370. np->multiplier = mult;
  371. }
  372. /*
  373. * If multiplier not found or scntl3 not 7,5,3,
  374. * reset chip and get frequency from general purpose timer.
  375. * Otherwise trust scntl3 BIOS setting.
  376. */
  377. if (np->multiplier != mult || (scntl3 & 7) < 3 || !(scntl3 & 1)) {
  378. OUTB(np, nc_stest1, 0); /* make sure doubler is OFF */
  379. f1 = sym_getfreq (np);
  380. if (sym_verbose)
  381. printf ("%s: chip clock is %uKHz\n", sym_name(np), f1);
  382. if (f1 < 45000) f1 = 40000;
  383. else if (f1 < 55000) f1 = 50000;
  384. else f1 = 80000;
  385. if (f1 < 80000 && mult > 1) {
  386. if (sym_verbose >= 2)
  387. printf ("%s: clock multiplier assumed\n",
  388. sym_name(np));
  389. np->multiplier = mult;
  390. }
  391. } else {
  392. if ((scntl3 & 7) == 3) f1 = 40000;
  393. else if ((scntl3 & 7) == 5) f1 = 80000;
  394. else f1 = 160000;
  395. f1 /= np->multiplier;
  396. }
  397. /*
  398. * Compute controller synchronous parameters.
  399. */
  400. f1 *= np->multiplier;
  401. np->clock_khz = f1;
  402. }
  403. /*
  404. * Get/probe PCI clock frequency
  405. */
  406. static int sym_getpciclock (struct sym_hcb *np)
  407. {
  408. int f = 0;
  409. /*
  410. * For now, we only need to know about the actual
  411. * PCI BUS clock frequency for C1010-66 chips.
  412. */
  413. #if 1
  414. if (np->features & FE_66MHZ) {
  415. #else
  416. if (1) {
  417. #endif
  418. OUTB(np, nc_stest1, SCLK); /* Use the PCI clock as SCSI clock */
  419. f = sym_getfreq(np);
  420. OUTB(np, nc_stest1, 0);
  421. }
  422. np->pciclk_khz = f;
  423. return f;
  424. }
  425. /*
  426. * SYMBIOS chip clock divisor table.
  427. *
  428. * Divisors are multiplied by 10,000,000 in order to make
  429. * calculations more simple.
  430. */
  431. #define _5M 5000000
  432. static const u32 div_10M[] = {2*_5M, 3*_5M, 4*_5M, 6*_5M, 8*_5M, 12*_5M, 16*_5M};
  433. /*
  434. * Get clock factor and sync divisor for a given
  435. * synchronous factor period.
  436. */
  437. static int
  438. sym_getsync(struct sym_hcb *np, u_char dt, u_char sfac, u_char *divp, u_char *fakp)
  439. {
  440. u32 clk = np->clock_khz; /* SCSI clock frequency in kHz */
  441. int div = np->clock_divn; /* Number of divisors supported */
  442. u32 fak; /* Sync factor in sxfer */
  443. u32 per; /* Period in tenths of ns */
  444. u32 kpc; /* (per * clk) */
  445. int ret;
  446. /*
  447. * Compute the synchronous period in tenths of nano-seconds
  448. */
  449. if (dt && sfac <= 9) per = 125;
  450. else if (sfac <= 10) per = 250;
  451. else if (sfac == 11) per = 303;
  452. else if (sfac == 12) per = 500;
  453. else per = 40 * sfac;
  454. ret = per;
  455. kpc = per * clk;
  456. if (dt)
  457. kpc <<= 1;
  458. /*
  459. * For earliest C10 revision 0, we cannot use extra
  460. * clocks for the setting of the SCSI clocking.
  461. * Note that this limits the lowest sync data transfer
  462. * to 5 Mega-transfers per second and may result in
  463. * using higher clock divisors.
  464. */
  465. #if 1
  466. if ((np->features & (FE_C10|FE_U3EN)) == FE_C10) {
  467. /*
  468. * Look for the lowest clock divisor that allows an
  469. * output speed not faster than the period.
  470. */
  471. while (div > 0) {
  472. --div;
  473. if (kpc > (div_10M[div] << 2)) {
  474. ++div;
  475. break;
  476. }
  477. }
  478. fak = 0; /* No extra clocks */
  479. if (div == np->clock_divn) { /* Are we too fast ? */
  480. ret = -1;
  481. }
  482. *divp = div;
  483. *fakp = fak;
  484. return ret;
  485. }
  486. #endif
  487. /*
  488. * Look for the greatest clock divisor that allows an
  489. * input speed faster than the period.
  490. */
  491. while (div-- > 0)
  492. if (kpc >= (div_10M[div] << 2)) break;
  493. /*
  494. * Calculate the lowest clock factor that allows an output
  495. * speed not faster than the period, and the max output speed.
  496. * If fak >= 1 we will set both XCLKH_ST and XCLKH_DT.
  497. * If fak >= 2 we will also set XCLKS_ST and XCLKS_DT.
  498. */
  499. if (dt) {
  500. fak = (kpc - 1) / (div_10M[div] << 1) + 1 - 2;
  501. /* ret = ((2+fak)*div_10M[div])/np->clock_khz; */
  502. } else {
  503. fak = (kpc - 1) / div_10M[div] + 1 - 4;
  504. /* ret = ((4+fak)*div_10M[div])/np->clock_khz; */
  505. }
  506. /*
  507. * Check against our hardware limits, or bugs :).
  508. */
  509. if (fak > 2) {
  510. fak = 2;
  511. ret = -1;
  512. }
  513. /*
  514. * Compute and return sync parameters.
  515. */
  516. *divp = div;
  517. *fakp = fak;
  518. return ret;
  519. }
  520. /*
  521. * SYMBIOS chips allow burst lengths of 2, 4, 8, 16, 32, 64,
  522. * 128 transfers. All chips support at least 16 transfers
  523. * bursts. The 825A, 875 and 895 chips support bursts of up
  524. * to 128 transfers and the 895A and 896 support bursts of up
  525. * to 64 transfers. All other chips support up to 16
  526. * transfers bursts.
  527. *
  528. * For PCI 32 bit data transfers each transfer is a DWORD.
  529. * It is a QUADWORD (8 bytes) for PCI 64 bit data transfers.
  530. *
  531. * We use log base 2 (burst length) as internal code, with
  532. * value 0 meaning "burst disabled".
  533. */
  534. /*
  535. * Burst length from burst code.
  536. */
  537. #define burst_length(bc) (!(bc))? 0 : 1 << (bc)
  538. /*
  539. * Burst code from io register bits.
  540. */
  541. #define burst_code(dmode, ctest4, ctest5) \
  542. (ctest4) & 0x80? 0 : (((dmode) & 0xc0) >> 6) + ((ctest5) & 0x04) + 1
  543. /*
  544. * Set initial io register bits from burst code.
  545. */
  546. static inline void sym_init_burst(struct sym_hcb *np, u_char bc)
  547. {
  548. np->rv_ctest4 &= ~0x80;
  549. np->rv_dmode &= ~(0x3 << 6);
  550. np->rv_ctest5 &= ~0x4;
  551. if (!bc) {
  552. np->rv_ctest4 |= 0x80;
  553. }
  554. else {
  555. --bc;
  556. np->rv_dmode |= ((bc & 0x3) << 6);
  557. np->rv_ctest5 |= (bc & 0x4);
  558. }
  559. }
  560. /*
  561. * Save initial settings of some IO registers.
  562. * Assumed to have been set by BIOS.
  563. * We cannot reset the chip prior to reading the
  564. * IO registers, since informations will be lost.
  565. * Since the SCRIPTS processor may be running, this
  566. * is not safe on paper, but it seems to work quite
  567. * well. :)
  568. */
  569. static void sym_save_initial_setting (struct sym_hcb *np)
  570. {
  571. np->sv_scntl0 = INB(np, nc_scntl0) & 0x0a;
  572. np->sv_scntl3 = INB(np, nc_scntl3) & 0x07;
  573. np->sv_dmode = INB(np, nc_dmode) & 0xce;
  574. np->sv_dcntl = INB(np, nc_dcntl) & 0xa8;
  575. np->sv_ctest3 = INB(np, nc_ctest3) & 0x01;
  576. np->sv_ctest4 = INB(np, nc_ctest4) & 0x80;
  577. np->sv_gpcntl = INB(np, nc_gpcntl);
  578. np->sv_stest1 = INB(np, nc_stest1);
  579. np->sv_stest2 = INB(np, nc_stest2) & 0x20;
  580. np->sv_stest4 = INB(np, nc_stest4);
  581. if (np->features & FE_C10) { /* Always large DMA fifo + ultra3 */
  582. np->sv_scntl4 = INB(np, nc_scntl4);
  583. np->sv_ctest5 = INB(np, nc_ctest5) & 0x04;
  584. }
  585. else
  586. np->sv_ctest5 = INB(np, nc_ctest5) & 0x24;
  587. }
  588. /*
  589. * Set SCSI BUS mode.
  590. * - LVD capable chips (895/895A/896/1010) report the current BUS mode
  591. * through the STEST4 IO register.
  592. * - For previous generation chips (825/825A/875), the user has to tell us
  593. * how to check against HVD, since a 100% safe algorithm is not possible.
  594. */
  595. static void sym_set_bus_mode(struct sym_hcb *np, struct sym_nvram *nvram)
  596. {
  597. if (np->scsi_mode)
  598. return;
  599. np->scsi_mode = SMODE_SE;
  600. if (np->features & (FE_ULTRA2|FE_ULTRA3))
  601. np->scsi_mode = (np->sv_stest4 & SMODE);
  602. else if (np->features & FE_DIFF) {
  603. if (SYM_SETUP_SCSI_DIFF == 1) {
  604. if (np->sv_scntl3) {
  605. if (np->sv_stest2 & 0x20)
  606. np->scsi_mode = SMODE_HVD;
  607. } else if (nvram->type == SYM_SYMBIOS_NVRAM) {
  608. if (!(INB(np, nc_gpreg) & 0x08))
  609. np->scsi_mode = SMODE_HVD;
  610. }
  611. } else if (SYM_SETUP_SCSI_DIFF == 2)
  612. np->scsi_mode = SMODE_HVD;
  613. }
  614. if (np->scsi_mode == SMODE_HVD)
  615. np->rv_stest2 |= 0x20;
  616. }
  617. /*
  618. * Prepare io register values used by sym_start_up()
  619. * according to selected and supported features.
  620. */
  621. static int sym_prepare_setting(struct Scsi_Host *shost, struct sym_hcb *np, struct sym_nvram *nvram)
  622. {
  623. struct sym_data *sym_data = shost_priv(shost);
  624. struct pci_dev *pdev = sym_data->pdev;
  625. u_char burst_max;
  626. u32 period;
  627. int i;
  628. np->maxwide = (np->features & FE_WIDE) ? 1 : 0;
  629. /*
  630. * Guess the frequency of the chip's clock.
  631. */
  632. if (np->features & (FE_ULTRA3 | FE_ULTRA2))
  633. np->clock_khz = 160000;
  634. else if (np->features & FE_ULTRA)
  635. np->clock_khz = 80000;
  636. else
  637. np->clock_khz = 40000;
  638. /*
  639. * Get the clock multiplier factor.
  640. */
  641. if (np->features & FE_QUAD)
  642. np->multiplier = 4;
  643. else if (np->features & FE_DBLR)
  644. np->multiplier = 2;
  645. else
  646. np->multiplier = 1;
  647. /*
  648. * Measure SCSI clock frequency for chips
  649. * it may vary from assumed one.
  650. */
  651. if (np->features & FE_VARCLK)
  652. sym_getclock(np, np->multiplier);
  653. /*
  654. * Divisor to be used for async (timer pre-scaler).
  655. */
  656. i = np->clock_divn - 1;
  657. while (--i >= 0) {
  658. if (10ul * SYM_CONF_MIN_ASYNC * np->clock_khz > div_10M[i]) {
  659. ++i;
  660. break;
  661. }
  662. }
  663. np->rv_scntl3 = i+1;
  664. /*
  665. * The C1010 uses hardwired divisors for async.
  666. * So, we just throw away, the async. divisor.:-)
  667. */
  668. if (np->features & FE_C10)
  669. np->rv_scntl3 = 0;
  670. /*
  671. * Minimum synchronous period factor supported by the chip.
  672. * Btw, 'period' is in tenths of nanoseconds.
  673. */
  674. period = (4 * div_10M[0] + np->clock_khz - 1) / np->clock_khz;
  675. if (period <= 250) np->minsync = 10;
  676. else if (period <= 303) np->minsync = 11;
  677. else if (period <= 500) np->minsync = 12;
  678. else np->minsync = (period + 40 - 1) / 40;
  679. /*
  680. * Check against chip SCSI standard support (SCSI-2,ULTRA,ULTRA2).
  681. */
  682. if (np->minsync < 25 &&
  683. !(np->features & (FE_ULTRA|FE_ULTRA2|FE_ULTRA3)))
  684. np->minsync = 25;
  685. else if (np->minsync < 12 &&
  686. !(np->features & (FE_ULTRA2|FE_ULTRA3)))
  687. np->minsync = 12;
  688. /*
  689. * Maximum synchronous period factor supported by the chip.
  690. */
  691. period = (11 * div_10M[np->clock_divn - 1]) / (4 * np->clock_khz);
  692. np->maxsync = period > 2540 ? 254 : period / 10;
  693. /*
  694. * If chip is a C1010, guess the sync limits in DT mode.
  695. */
  696. if ((np->features & (FE_C10|FE_ULTRA3)) == (FE_C10|FE_ULTRA3)) {
  697. if (np->clock_khz == 160000) {
  698. np->minsync_dt = 9;
  699. np->maxsync_dt = 50;
  700. np->maxoffs_dt = nvram->type ? 62 : 31;
  701. }
  702. }
  703. /*
  704. * 64 bit addressing (895A/896/1010) ?
  705. */
  706. if (np->features & FE_DAC) {
  707. if (!use_dac(np))
  708. np->rv_ccntl1 |= (DDAC);
  709. else if (SYM_CONF_DMA_ADDRESSING_MODE == 1)
  710. np->rv_ccntl1 |= (XTIMOD | EXTIBMV);
  711. else if (SYM_CONF_DMA_ADDRESSING_MODE == 2)
  712. np->rv_ccntl1 |= (0 | EXTIBMV);
  713. }
  714. /*
  715. * Phase mismatch handled by SCRIPTS (895A/896/1010) ?
  716. */
  717. if (np->features & FE_NOPM)
  718. np->rv_ccntl0 |= (ENPMJ);
  719. /*
  720. * C1010-33 Errata: Part Number:609-039638 (rev. 1) is fixed.
  721. * In dual channel mode, contention occurs if internal cycles
  722. * are used. Disable internal cycles.
  723. */
  724. if (pdev->device == PCI_DEVICE_ID_LSI_53C1010_33 &&
  725. pdev->revision < 0x1)
  726. np->rv_ccntl0 |= DILS;
  727. /*
  728. * Select burst length (dwords)
  729. */
  730. burst_max = SYM_SETUP_BURST_ORDER;
  731. if (burst_max == 255)
  732. burst_max = burst_code(np->sv_dmode, np->sv_ctest4,
  733. np->sv_ctest5);
  734. if (burst_max > 7)
  735. burst_max = 7;
  736. if (burst_max > np->maxburst)
  737. burst_max = np->maxburst;
  738. /*
  739. * DEL 352 - 53C810 Rev x11 - Part Number 609-0392140 - ITEM 2.
  740. * This chip and the 860 Rev 1 may wrongly use PCI cache line
  741. * based transactions on LOAD/STORE instructions. So we have
  742. * to prevent these chips from using such PCI transactions in
  743. * this driver. The generic ncr driver that does not use
  744. * LOAD/STORE instructions does not need this work-around.
  745. */
  746. if ((pdev->device == PCI_DEVICE_ID_NCR_53C810 &&
  747. pdev->revision >= 0x10 && pdev->revision <= 0x11) ||
  748. (pdev->device == PCI_DEVICE_ID_NCR_53C860 &&
  749. pdev->revision <= 0x1))
  750. np->features &= ~(FE_WRIE|FE_ERL|FE_ERMP);
  751. /*
  752. * Select all supported special features.
  753. * If we are using on-board RAM for scripts, prefetch (PFEN)
  754. * does not help, but burst op fetch (BOF) does.
  755. * Disabling PFEN makes sure BOF will be used.
  756. */
  757. if (np->features & FE_ERL)
  758. np->rv_dmode |= ERL; /* Enable Read Line */
  759. if (np->features & FE_BOF)
  760. np->rv_dmode |= BOF; /* Burst Opcode Fetch */
  761. if (np->features & FE_ERMP)
  762. np->rv_dmode |= ERMP; /* Enable Read Multiple */
  763. #if 1
  764. if ((np->features & FE_PFEN) && !np->ram_ba)
  765. #else
  766. if (np->features & FE_PFEN)
  767. #endif
  768. np->rv_dcntl |= PFEN; /* Prefetch Enable */
  769. if (np->features & FE_CLSE)
  770. np->rv_dcntl |= CLSE; /* Cache Line Size Enable */
  771. if (np->features & FE_WRIE)
  772. np->rv_ctest3 |= WRIE; /* Write and Invalidate */
  773. if (np->features & FE_DFS)
  774. np->rv_ctest5 |= DFS; /* Dma Fifo Size */
  775. /*
  776. * Select some other
  777. */
  778. np->rv_ctest4 |= MPEE; /* Master parity checking */
  779. np->rv_scntl0 |= 0x0a; /* full arb., ena parity, par->ATN */
  780. /*
  781. * Get parity checking, host ID and verbose mode from NVRAM
  782. */
  783. np->myaddr = 255;
  784. np->scsi_mode = 0;
  785. sym_nvram_setup_host(shost, np, nvram);
  786. /*
  787. * Get SCSI addr of host adapter (set by bios?).
  788. */
  789. if (np->myaddr == 255) {
  790. np->myaddr = INB(np, nc_scid) & 0x07;
  791. if (!np->myaddr)
  792. np->myaddr = SYM_SETUP_HOST_ID;
  793. }
  794. /*
  795. * Prepare initial io register bits for burst length
  796. */
  797. sym_init_burst(np, burst_max);
  798. sym_set_bus_mode(np, nvram);
  799. /*
  800. * Set LED support from SCRIPTS.
  801. * Ignore this feature for boards known to use a
  802. * specific GPIO wiring and for the 895A, 896
  803. * and 1010 that drive the LED directly.
  804. */
  805. if ((SYM_SETUP_SCSI_LED ||
  806. (nvram->type == SYM_SYMBIOS_NVRAM ||
  807. (nvram->type == SYM_TEKRAM_NVRAM &&
  808. pdev->device == PCI_DEVICE_ID_NCR_53C895))) &&
  809. !(np->features & FE_LEDC) && !(np->sv_gpcntl & 0x01))
  810. np->features |= FE_LED0;
  811. /*
  812. * Set irq mode.
  813. */
  814. switch(SYM_SETUP_IRQ_MODE & 3) {
  815. case 2:
  816. np->rv_dcntl |= IRQM;
  817. break;
  818. case 1:
  819. np->rv_dcntl |= (np->sv_dcntl & IRQM);
  820. break;
  821. default:
  822. break;
  823. }
  824. /*
  825. * Configure targets according to driver setup.
  826. * If NVRAM present get targets setup from NVRAM.
  827. */
  828. for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
  829. struct sym_tcb *tp = &np->target[i];
  830. tp->usrflags |= (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
  831. tp->usrtags = SYM_SETUP_MAX_TAG;
  832. tp->usr_width = np->maxwide;
  833. tp->usr_period = 9;
  834. sym_nvram_setup_target(tp, i, nvram);
  835. if (!tp->usrtags)
  836. tp->usrflags &= ~SYM_TAGS_ENABLED;
  837. }
  838. /*
  839. * Let user know about the settings.
  840. */
  841. printf("%s: %s, ID %d, Fast-%d, %s, %s\n", sym_name(np),
  842. sym_nvram_type(nvram), np->myaddr,
  843. (np->features & FE_ULTRA3) ? 80 :
  844. (np->features & FE_ULTRA2) ? 40 :
  845. (np->features & FE_ULTRA) ? 20 : 10,
  846. sym_scsi_bus_mode(np->scsi_mode),
  847. (np->rv_scntl0 & 0xa) ? "parity checking" : "NO parity");
  848. /*
  849. * Tell him more on demand.
  850. */
  851. if (sym_verbose) {
  852. printf("%s: %s IRQ line driver%s\n",
  853. sym_name(np),
  854. np->rv_dcntl & IRQM ? "totem pole" : "open drain",
  855. np->ram_ba ? ", using on-chip SRAM" : "");
  856. printf("%s: using %s firmware.\n", sym_name(np), np->fw_name);
  857. if (np->features & FE_NOPM)
  858. printf("%s: handling phase mismatch from SCRIPTS.\n",
  859. sym_name(np));
  860. }
  861. /*
  862. * And still more.
  863. */
  864. if (sym_verbose >= 2) {
  865. printf ("%s: initial SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
  866. "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
  867. sym_name(np), np->sv_scntl3, np->sv_dmode, np->sv_dcntl,
  868. np->sv_ctest3, np->sv_ctest4, np->sv_ctest5);
  869. printf ("%s: final SCNTL3/DMODE/DCNTL/CTEST3/4/5 = "
  870. "(hex) %02x/%02x/%02x/%02x/%02x/%02x\n",
  871. sym_name(np), np->rv_scntl3, np->rv_dmode, np->rv_dcntl,
  872. np->rv_ctest3, np->rv_ctest4, np->rv_ctest5);
  873. }
  874. return 0;
  875. }
  876. /*
  877. * Test the pci bus snoop logic :-(
  878. *
  879. * Has to be called with interrupts disabled.
  880. */
  881. #ifdef CONFIG_SCSI_SYM53C8XX_MMIO
  882. static int sym_regtest(struct sym_hcb *np)
  883. {
  884. register volatile u32 data;
  885. /*
  886. * chip registers may NOT be cached.
  887. * write 0xffffffff to a read only register area,
  888. * and try to read it back.
  889. */
  890. data = 0xffffffff;
  891. OUTL(np, nc_dstat, data);
  892. data = INL(np, nc_dstat);
  893. #if 1
  894. if (data == 0xffffffff) {
  895. #else
  896. if ((data & 0xe2f0fffd) != 0x02000080) {
  897. #endif
  898. printf ("CACHE TEST FAILED: reg dstat-sstat2 readback %x.\n",
  899. (unsigned) data);
  900. return 0x10;
  901. }
  902. return 0;
  903. }
  904. #else
  905. static inline int sym_regtest(struct sym_hcb *np)
  906. {
  907. return 0;
  908. }
  909. #endif
  910. static int sym_snooptest(struct sym_hcb *np)
  911. {
  912. u32 sym_rd, sym_wr, sym_bk, host_rd, host_wr, pc, dstat;
  913. int i, err;
  914. err = sym_regtest(np);
  915. if (err)
  916. return err;
  917. restart_test:
  918. /*
  919. * Enable Master Parity Checking as we intend
  920. * to enable it for normal operations.
  921. */
  922. OUTB(np, nc_ctest4, (np->rv_ctest4 & MPEE));
  923. /*
  924. * init
  925. */
  926. pc = SCRIPTZ_BA(np, snooptest);
  927. host_wr = 1;
  928. sym_wr = 2;
  929. /*
  930. * Set memory and register.
  931. */
  932. np->scratch = cpu_to_scr(host_wr);
  933. OUTL(np, nc_temp, sym_wr);
  934. /*
  935. * Start script (exchange values)
  936. */
  937. OUTL(np, nc_dsa, np->hcb_ba);
  938. OUTL_DSP(np, pc);
  939. /*
  940. * Wait 'til done (with timeout)
  941. */
  942. for (i=0; i<SYM_SNOOP_TIMEOUT; i++)
  943. if (INB(np, nc_istat) & (INTF|SIP|DIP))
  944. break;
  945. if (i>=SYM_SNOOP_TIMEOUT) {
  946. printf ("CACHE TEST FAILED: timeout.\n");
  947. return (0x20);
  948. }
  949. /*
  950. * Check for fatal DMA errors.
  951. */
  952. dstat = INB(np, nc_dstat);
  953. #if 1 /* Band aiding for broken hardwares that fail PCI parity */
  954. if ((dstat & MDPE) && (np->rv_ctest4 & MPEE)) {
  955. printf ("%s: PCI DATA PARITY ERROR DETECTED - "
  956. "DISABLING MASTER DATA PARITY CHECKING.\n",
  957. sym_name(np));
  958. np->rv_ctest4 &= ~MPEE;
  959. goto restart_test;
  960. }
  961. #endif
  962. if (dstat & (MDPE|BF|IID)) {
  963. printf ("CACHE TEST FAILED: DMA error (dstat=0x%02x).", dstat);
  964. return (0x80);
  965. }
  966. /*
  967. * Save termination position.
  968. */
  969. pc = INL(np, nc_dsp);
  970. /*
  971. * Read memory and register.
  972. */
  973. host_rd = scr_to_cpu(np->scratch);
  974. sym_rd = INL(np, nc_scratcha);
  975. sym_bk = INL(np, nc_temp);
  976. /*
  977. * Check termination position.
  978. */
  979. if (pc != SCRIPTZ_BA(np, snoopend)+8) {
  980. printf ("CACHE TEST FAILED: script execution failed.\n");
  981. printf ("start=%08lx, pc=%08lx, end=%08lx\n",
  982. (u_long) SCRIPTZ_BA(np, snooptest), (u_long) pc,
  983. (u_long) SCRIPTZ_BA(np, snoopend) +8);
  984. return (0x40);
  985. }
  986. /*
  987. * Show results.
  988. */
  989. if (host_wr != sym_rd) {
  990. printf ("CACHE TEST FAILED: host wrote %d, chip read %d.\n",
  991. (int) host_wr, (int) sym_rd);
  992. err |= 1;
  993. }
  994. if (host_rd != sym_wr) {
  995. printf ("CACHE TEST FAILED: chip wrote %d, host read %d.\n",
  996. (int) sym_wr, (int) host_rd);
  997. err |= 2;
  998. }
  999. if (sym_bk != sym_wr) {
  1000. printf ("CACHE TEST FAILED: chip wrote %d, read back %d.\n",
  1001. (int) sym_wr, (int) sym_bk);
  1002. err |= 4;
  1003. }
  1004. return err;
  1005. }
  1006. /*
  1007. * log message for real hard errors
  1008. *
  1009. * sym0 targ 0?: ERROR (ds:si) (so-si-sd) (sx/s3/s4) @ name (dsp:dbc).
  1010. * reg: r0 r1 r2 r3 r4 r5 r6 ..... rf.
  1011. *
  1012. * exception register:
  1013. * ds: dstat
  1014. * si: sist
  1015. *
  1016. * SCSI bus lines:
  1017. * so: control lines as driven by chip.
  1018. * si: control lines as seen by chip.
  1019. * sd: scsi data lines as seen by chip.
  1020. *
  1021. * wide/fastmode:
  1022. * sx: sxfer (see the manual)
  1023. * s3: scntl3 (see the manual)
  1024. * s4: scntl4 (see the manual)
  1025. *
  1026. * current script command:
  1027. * dsp: script address (relative to start of script).
  1028. * dbc: first word of script command.
  1029. *
  1030. * First 24 register of the chip:
  1031. * r0..rf
  1032. */
  1033. static void sym_log_hard_error(struct Scsi_Host *shost, u_short sist, u_char dstat)
  1034. {
  1035. struct sym_hcb *np = sym_get_hcb(shost);
  1036. u32 dsp;
  1037. int script_ofs;
  1038. int script_size;
  1039. char *script_name;
  1040. u_char *script_base;
  1041. int i;
  1042. dsp = INL(np, nc_dsp);
  1043. if (dsp > np->scripta_ba &&
  1044. dsp <= np->scripta_ba + np->scripta_sz) {
  1045. script_ofs = dsp - np->scripta_ba;
  1046. script_size = np->scripta_sz;
  1047. script_base = (u_char *) np->scripta0;
  1048. script_name = "scripta";
  1049. }
  1050. else if (np->scriptb_ba < dsp &&
  1051. dsp <= np->scriptb_ba + np->scriptb_sz) {
  1052. script_ofs = dsp - np->scriptb_ba;
  1053. script_size = np->scriptb_sz;
  1054. script_base = (u_char *) np->scriptb0;
  1055. script_name = "scriptb";
  1056. } else {
  1057. script_ofs = dsp;
  1058. script_size = 0;
  1059. script_base = NULL;
  1060. script_name = "mem";
  1061. }
  1062. printf ("%s:%d: ERROR (%x:%x) (%x-%x-%x) (%x/%x/%x) @ (%s %x:%08x).\n",
  1063. sym_name(np), (unsigned)INB(np, nc_sdid)&0x0f, dstat, sist,
  1064. (unsigned)INB(np, nc_socl), (unsigned)INB(np, nc_sbcl),
  1065. (unsigned)INB(np, nc_sbdl), (unsigned)INB(np, nc_sxfer),
  1066. (unsigned)INB(np, nc_scntl3),
  1067. (np->features & FE_C10) ? (unsigned)INB(np, nc_scntl4) : 0,
  1068. script_name, script_ofs, (unsigned)INL(np, nc_dbc));
  1069. if (((script_ofs & 3) == 0) &&
  1070. (unsigned)script_ofs < script_size) {
  1071. printf ("%s: script cmd = %08x\n", sym_name(np),
  1072. scr_to_cpu((int) *(u32 *)(script_base + script_ofs)));
  1073. }
  1074. printf("%s: regdump:", sym_name(np));
  1075. for (i = 0; i < 24; i++)
  1076. printf(" %02x", (unsigned)INB_OFF(np, i));
  1077. printf(".\n");
  1078. /*
  1079. * PCI BUS error.
  1080. */
  1081. if (dstat & (MDPE|BF))
  1082. sym_log_bus_error(shost);
  1083. }
  1084. void sym_dump_registers(struct Scsi_Host *shost)
  1085. {
  1086. struct sym_hcb *np = sym_get_hcb(shost);
  1087. u_short sist;
  1088. u_char dstat;
  1089. sist = INW(np, nc_sist);
  1090. dstat = INB(np, nc_dstat);
  1091. sym_log_hard_error(shost, sist, dstat);
  1092. }
  1093. static struct sym_chip sym_dev_table[] = {
  1094. {PCI_DEVICE_ID_NCR_53C810, 0x0f, "810", 4, 8, 4, 64,
  1095. FE_ERL}
  1096. ,
  1097. #ifdef SYM_DEBUG_GENERIC_SUPPORT
  1098. {PCI_DEVICE_ID_NCR_53C810, 0xff, "810a", 4, 8, 4, 1,
  1099. FE_BOF}
  1100. ,
  1101. #else
  1102. {PCI_DEVICE_ID_NCR_53C810, 0xff, "810a", 4, 8, 4, 1,
  1103. FE_CACHE_SET|FE_LDSTR|FE_PFEN|FE_BOF}
  1104. ,
  1105. #endif
  1106. {PCI_DEVICE_ID_NCR_53C815, 0xff, "815", 4, 8, 4, 64,
  1107. FE_BOF|FE_ERL}
  1108. ,
  1109. {PCI_DEVICE_ID_NCR_53C825, 0x0f, "825", 6, 8, 4, 64,
  1110. FE_WIDE|FE_BOF|FE_ERL|FE_DIFF}
  1111. ,
  1112. {PCI_DEVICE_ID_NCR_53C825, 0xff, "825a", 6, 8, 4, 2,
  1113. FE_WIDE|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|FE_RAM|FE_DIFF}
  1114. ,
  1115. {PCI_DEVICE_ID_NCR_53C860, 0xff, "860", 4, 8, 5, 1,
  1116. FE_ULTRA|FE_CACHE_SET|FE_BOF|FE_LDSTR|FE_PFEN}
  1117. ,
  1118. {PCI_DEVICE_ID_NCR_53C875, 0x01, "875", 6, 16, 5, 2,
  1119. FE_WIDE|FE_ULTRA|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1120. FE_RAM|FE_DIFF|FE_VARCLK}
  1121. ,
  1122. {PCI_DEVICE_ID_NCR_53C875, 0xff, "875", 6, 16, 5, 2,
  1123. FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1124. FE_RAM|FE_DIFF|FE_VARCLK}
  1125. ,
  1126. {PCI_DEVICE_ID_NCR_53C875J, 0xff, "875J", 6, 16, 5, 2,
  1127. FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1128. FE_RAM|FE_DIFF|FE_VARCLK}
  1129. ,
  1130. {PCI_DEVICE_ID_NCR_53C885, 0xff, "885", 6, 16, 5, 2,
  1131. FE_WIDE|FE_ULTRA|FE_DBLR|FE_CACHE0_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1132. FE_RAM|FE_DIFF|FE_VARCLK}
  1133. ,
  1134. #ifdef SYM_DEBUG_GENERIC_SUPPORT
  1135. {PCI_DEVICE_ID_NCR_53C895, 0xff, "895", 6, 31, 7, 2,
  1136. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|
  1137. FE_RAM|FE_LCKFRQ}
  1138. ,
  1139. #else
  1140. {PCI_DEVICE_ID_NCR_53C895, 0xff, "895", 6, 31, 7, 2,
  1141. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1142. FE_RAM|FE_LCKFRQ}
  1143. ,
  1144. #endif
  1145. {PCI_DEVICE_ID_NCR_53C896, 0xff, "896", 6, 31, 7, 4,
  1146. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1147. FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
  1148. ,
  1149. {PCI_DEVICE_ID_LSI_53C895A, 0xff, "895a", 6, 31, 7, 4,
  1150. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1151. FE_RAM|FE_RAM8K|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
  1152. ,
  1153. {PCI_DEVICE_ID_LSI_53C875A, 0xff, "875a", 6, 31, 7, 4,
  1154. FE_WIDE|FE_ULTRA|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1155. FE_RAM|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_LCKFRQ}
  1156. ,
  1157. {PCI_DEVICE_ID_LSI_53C1010_33, 0x00, "1010-33", 6, 31, 7, 8,
  1158. FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
  1159. FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
  1160. FE_C10}
  1161. ,
  1162. {PCI_DEVICE_ID_LSI_53C1010_33, 0xff, "1010-33", 6, 31, 7, 8,
  1163. FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
  1164. FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_CRC|
  1165. FE_C10|FE_U3EN}
  1166. ,
  1167. {PCI_DEVICE_ID_LSI_53C1010_66, 0xff, "1010-66", 6, 31, 7, 8,
  1168. FE_WIDE|FE_ULTRA3|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFBC|FE_LDSTR|FE_PFEN|
  1169. FE_RAM|FE_RAM8K|FE_64BIT|FE_DAC|FE_IO256|FE_NOPM|FE_LEDC|FE_66MHZ|FE_CRC|
  1170. FE_C10|FE_U3EN}
  1171. ,
  1172. {PCI_DEVICE_ID_LSI_53C1510, 0xff, "1510d", 6, 31, 7, 4,
  1173. FE_WIDE|FE_ULTRA2|FE_QUAD|FE_CACHE_SET|FE_BOF|FE_DFS|FE_LDSTR|FE_PFEN|
  1174. FE_RAM|FE_IO256|FE_LEDC}
  1175. };
  1176. #define sym_num_devs (ARRAY_SIZE(sym_dev_table))
  1177. /*
  1178. * Look up the chip table.
  1179. *
  1180. * Return a pointer to the chip entry if found,
  1181. * zero otherwise.
  1182. */
  1183. struct sym_chip *
  1184. sym_lookup_chip_table (u_short device_id, u_char revision)
  1185. {
  1186. struct sym_chip *chip;
  1187. int i;
  1188. for (i = 0; i < sym_num_devs; i++) {
  1189. chip = &sym_dev_table[i];
  1190. if (device_id != chip->device_id)
  1191. continue;
  1192. if (revision > chip->revision_id)
  1193. continue;
  1194. return chip;
  1195. }
  1196. return NULL;
  1197. }
  1198. #if SYM_CONF_DMA_ADDRESSING_MODE == 2
  1199. /*
  1200. * Lookup the 64 bit DMA segments map.
  1201. * This is only used if the direct mapping
  1202. * has been unsuccessful.
  1203. */
  1204. int sym_lookup_dmap(struct sym_hcb *np, u32 h, int s)
  1205. {
  1206. int i;
  1207. if (!use_dac(np))
  1208. goto weird;
  1209. /* Look up existing mappings */
  1210. for (i = SYM_DMAP_SIZE-1; i > 0; i--) {
  1211. if (h == np->dmap_bah[i])
  1212. return i;
  1213. }
  1214. /* If direct mapping is free, get it */
  1215. if (!np->dmap_bah[s])
  1216. goto new;
  1217. /* Collision -> lookup free mappings */
  1218. for (s = SYM_DMAP_SIZE-1; s > 0; s--) {
  1219. if (!np->dmap_bah[s])
  1220. goto new;
  1221. }
  1222. weird:
  1223. panic("sym: ran out of 64 bit DMA segment registers");
  1224. return -1;
  1225. new:
  1226. np->dmap_bah[s] = h;
  1227. np->dmap_dirty = 1;
  1228. return s;
  1229. }
  1230. /*
  1231. * Update IO registers scratch C..R so they will be
  1232. * in sync. with queued CCB expectations.
  1233. */
  1234. static void sym_update_dmap_regs(struct sym_hcb *np)
  1235. {
  1236. int o, i;
  1237. if (!np->dmap_dirty)
  1238. return;
  1239. o = offsetof(struct sym_reg, nc_scrx[0]);
  1240. for (i = 0; i < SYM_DMAP_SIZE; i++) {
  1241. OUTL_OFF(np, o, np->dmap_bah[i]);
  1242. o += 4;
  1243. }
  1244. np->dmap_dirty = 0;
  1245. }
  1246. #endif
  1247. /* Enforce all the fiddly SPI rules and the chip limitations */
  1248. static void sym_check_goals(struct sym_hcb *np, struct scsi_target *starget,
  1249. struct sym_trans *goal)
  1250. {
  1251. if (!spi_support_wide(starget))
  1252. goal->width = 0;
  1253. if (!spi_support_sync(starget)) {
  1254. goal->iu = 0;
  1255. goal->dt = 0;
  1256. goal->qas = 0;
  1257. goal->offset = 0;
  1258. return;
  1259. }
  1260. if (spi_support_dt(starget)) {
  1261. if (spi_support_dt_only(starget))
  1262. goal->dt = 1;
  1263. if (goal->offset == 0)
  1264. goal->dt = 0;
  1265. } else {
  1266. goal->dt = 0;
  1267. }
  1268. /* Some targets fail to properly negotiate DT in SE mode */
  1269. if ((np->scsi_mode != SMODE_LVD) || !(np->features & FE_U3EN))
  1270. goal->dt = 0;
  1271. if (goal->dt) {
  1272. /* all DT transfers must be wide */
  1273. goal->width = 1;
  1274. if (goal->offset > np->maxoffs_dt)
  1275. goal->offset = np->maxoffs_dt;
  1276. if (goal->period < np->minsync_dt)
  1277. goal->period = np->minsync_dt;
  1278. if (goal->period > np->maxsync_dt)
  1279. goal->period = np->maxsync_dt;
  1280. } else {
  1281. goal->iu = goal->qas = 0;
  1282. if (goal->offset > np->maxoffs)
  1283. goal->offset = np->maxoffs;
  1284. if (goal->period < np->minsync)
  1285. goal->period = np->minsync;
  1286. if (goal->period > np->maxsync)
  1287. goal->period = np->maxsync;
  1288. }
  1289. }
  1290. /*
  1291. * Prepare the next negotiation message if needed.
  1292. *
  1293. * Fill in the part of message buffer that contains the
  1294. * negotiation and the nego_status field of the CCB.
  1295. * Returns the size of the message in bytes.
  1296. */
  1297. static int sym_prepare_nego(struct sym_hcb *np, struct sym_ccb *cp, u_char *msgptr)
  1298. {
  1299. struct sym_tcb *tp = &np->target[cp->target];
  1300. struct scsi_target *starget = tp->starget;
  1301. struct sym_trans *goal = &tp->tgoal;
  1302. int msglen = 0;
  1303. int nego;
  1304. sym_check_goals(np, starget, goal);
  1305. /*
  1306. * Many devices implement PPR in a buggy way, so only use it if we
  1307. * really want to.
  1308. */
  1309. if (goal->renego == NS_PPR || (goal->offset &&
  1310. (goal->iu || goal->dt || goal->qas || (goal->period < 0xa)))) {
  1311. nego = NS_PPR;
  1312. } else if (goal->renego == NS_WIDE || goal->width) {
  1313. nego = NS_WIDE;
  1314. } else if (goal->renego == NS_SYNC || goal->offset) {
  1315. nego = NS_SYNC;
  1316. } else {
  1317. goal->check_nego = 0;
  1318. nego = 0;
  1319. }
  1320. switch (nego) {
  1321. case NS_SYNC:
  1322. msglen += spi_populate_sync_msg(msgptr + msglen, goal->period,
  1323. goal->offset);
  1324. break;
  1325. case NS_WIDE:
  1326. msglen += spi_populate_width_msg(msgptr + msglen, goal->width);
  1327. break;
  1328. case NS_PPR:
  1329. msglen += spi_populate_ppr_msg(msgptr + msglen, goal->period,
  1330. goal->offset, goal->width,
  1331. (goal->iu ? PPR_OPT_IU : 0) |
  1332. (goal->dt ? PPR_OPT_DT : 0) |
  1333. (goal->qas ? PPR_OPT_QAS : 0));
  1334. break;
  1335. }
  1336. cp->nego_status = nego;
  1337. if (nego) {
  1338. tp->nego_cp = cp; /* Keep track a nego will be performed */
  1339. if (DEBUG_FLAGS & DEBUG_NEGO) {
  1340. sym_print_nego_msg(np, cp->target,
  1341. nego == NS_SYNC ? "sync msgout" :
  1342. nego == NS_WIDE ? "wide msgout" :
  1343. "ppr msgout", msgptr);
  1344. }
  1345. }
  1346. return msglen;
  1347. }
  1348. /*
  1349. * Insert a job into the start queue.
  1350. */
  1351. void sym_put_start_queue(struct sym_hcb *np, struct sym_ccb *cp)
  1352. {
  1353. u_short qidx;
  1354. #ifdef SYM_CONF_IARB_SUPPORT
  1355. /*
  1356. * If the previously queued CCB is not yet done,
  1357. * set the IARB hint. The SCRIPTS will go with IARB
  1358. * for this job when starting the previous one.
  1359. * We leave devices a chance to win arbitration by
  1360. * not using more than 'iarb_max' consecutive
  1361. * immediate arbitrations.
  1362. */
  1363. if (np->last_cp && np->iarb_count < np->iarb_max) {
  1364. np->last_cp->host_flags |= HF_HINT_IARB;
  1365. ++np->iarb_count;
  1366. }
  1367. else
  1368. np->iarb_count = 0;
  1369. np->last_cp = cp;
  1370. #endif
  1371. #if SYM_CONF_DMA_ADDRESSING_MODE == 2
  1372. /*
  1373. * Make SCRIPTS aware of the 64 bit DMA
  1374. * segment registers not being up-to-date.
  1375. */
  1376. if (np->dmap_dirty)
  1377. cp->host_xflags |= HX_DMAP_DIRTY;
  1378. #endif
  1379. /*
  1380. * Insert first the idle task and then our job.
  1381. * The MBs should ensure proper ordering.
  1382. */
  1383. qidx = np->squeueput + 2;
  1384. if (qidx >= MAX_QUEUE*2) qidx = 0;
  1385. np->squeue [qidx] = cpu_to_scr(np->idletask_ba);
  1386. MEMORY_WRITE_BARRIER();
  1387. np->squeue [np->squeueput] = cpu_to_scr(cp->ccb_ba);
  1388. np->squeueput = qidx;
  1389. if (DEBUG_FLAGS & DEBUG_QUEUE)
  1390. scmd_printk(KERN_DEBUG, cp->cmd, "queuepos=%d\n",
  1391. np->squeueput);
  1392. /*
  1393. * Script processor may be waiting for reselect.
  1394. * Wake it up.
  1395. */
  1396. MEMORY_WRITE_BARRIER();
  1397. OUTB(np, nc_istat, SIGP|np->istat_sem);
  1398. }
  1399. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  1400. /*
  1401. * Start next ready-to-start CCBs.
  1402. */
  1403. void sym_start_next_ccbs(struct sym_hcb *np, struct sym_lcb *lp, int maxn)
  1404. {
  1405. SYM_QUEHEAD *qp;
  1406. struct sym_ccb *cp;
  1407. /*
  1408. * Paranoia, as usual. :-)
  1409. */
  1410. assert(!lp->started_tags || !lp->started_no_tag);
  1411. /*
  1412. * Try to start as many commands as asked by caller.
  1413. * Prevent from having both tagged and untagged
  1414. * commands queued to the device at the same time.
  1415. */
  1416. while (maxn--) {
  1417. qp = sym_remque_head(&lp->waiting_ccbq);
  1418. if (!qp)
  1419. break;
  1420. cp = sym_que_entry(qp, struct sym_ccb, link2_ccbq);
  1421. if (cp->tag != NO_TAG) {
  1422. if (lp->started_no_tag ||
  1423. lp->started_tags >= lp->started_max) {
  1424. sym_insque_head(qp, &lp->waiting_ccbq);
  1425. break;
  1426. }
  1427. lp->itlq_tbl[cp->tag] = cpu_to_scr(cp->ccb_ba);
  1428. lp->head.resel_sa =
  1429. cpu_to_scr(SCRIPTA_BA(np, resel_tag));
  1430. ++lp->started_tags;
  1431. } else {
  1432. if (lp->started_no_tag || lp->started_tags) {
  1433. sym_insque_head(qp, &lp->waiting_ccbq);
  1434. break;
  1435. }
  1436. lp->head.itl_task_sa = cpu_to_scr(cp->ccb_ba);
  1437. lp->head.resel_sa =
  1438. cpu_to_scr(SCRIPTA_BA(np, resel_no_tag));
  1439. ++lp->started_no_tag;
  1440. }
  1441. cp->started = 1;
  1442. sym_insque_tail(qp, &lp->started_ccbq);
  1443. sym_put_start_queue(np, cp);
  1444. }
  1445. }
  1446. #endif /* SYM_OPT_HANDLE_DEVICE_QUEUEING */
  1447. /*
  1448. * The chip may have completed jobs. Look at the DONE QUEUE.
  1449. *
  1450. * On paper, memory read barriers may be needed here to
  1451. * prevent out of order LOADs by the CPU from having
  1452. * prefetched stale data prior to DMA having occurred.
  1453. */
  1454. static int sym_wakeup_done (struct sym_hcb *np)
  1455. {
  1456. struct sym_ccb *cp;
  1457. int i, n;
  1458. u32 dsa;
  1459. n = 0;
  1460. i = np->dqueueget;
  1461. /* MEMORY_READ_BARRIER(); */
  1462. while (1) {
  1463. dsa = scr_to_cpu(np->dqueue[i]);
  1464. if (!dsa)
  1465. break;
  1466. np->dqueue[i] = 0;
  1467. if ((i = i+2) >= MAX_QUEUE*2)
  1468. i = 0;
  1469. cp = sym_ccb_from_dsa(np, dsa);
  1470. if (cp) {
  1471. MEMORY_READ_BARRIER();
  1472. sym_complete_ok (np, cp);
  1473. ++n;
  1474. }
  1475. else
  1476. printf ("%s: bad DSA (%x) in done queue.\n",
  1477. sym_name(np), (u_int) dsa);
  1478. }
  1479. np->dqueueget = i;
  1480. return n;
  1481. }
  1482. /*
  1483. * Complete all CCBs queued to the COMP queue.
  1484. *
  1485. * These CCBs are assumed:
  1486. * - Not to be referenced either by devices or
  1487. * SCRIPTS-related queues and datas.
  1488. * - To have to be completed with an error condition
  1489. * or requeued.
  1490. *
  1491. * The device queue freeze count is incremented
  1492. * for each CCB that does not prevent this.
  1493. * This function is called when all CCBs involved
  1494. * in error handling/recovery have been reaped.
  1495. */
  1496. static void sym_flush_comp_queue(struct sym_hcb *np, int cam_status)
  1497. {
  1498. SYM_QUEHEAD *qp;
  1499. struct sym_ccb *cp;
  1500. while ((qp = sym_remque_head(&np->comp_ccbq)) != NULL) {
  1501. struct scsi_cmnd *cmd;
  1502. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  1503. sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
  1504. /* Leave quiet CCBs waiting for resources */
  1505. if (cp->host_status == HS_WAIT)
  1506. continue;
  1507. cmd = cp->cmd;
  1508. if (cam_status)
  1509. sym_set_cam_status(cmd, cam_status);
  1510. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  1511. if (sym_get_cam_status(cmd) == DID_SOFT_ERROR) {
  1512. struct sym_tcb *tp = &np->target[cp->target];
  1513. struct sym_lcb *lp = sym_lp(tp, cp->lun);
  1514. if (lp) {
  1515. sym_remque(&cp->link2_ccbq);
  1516. sym_insque_tail(&cp->link2_ccbq,
  1517. &lp->waiting_ccbq);
  1518. if (cp->started) {
  1519. if (cp->tag != NO_TAG)
  1520. --lp->started_tags;
  1521. else
  1522. --lp->started_no_tag;
  1523. }
  1524. }
  1525. cp->started = 0;
  1526. continue;
  1527. }
  1528. #endif
  1529. sym_free_ccb(np, cp);
  1530. sym_xpt_done(np, cmd);
  1531. }
  1532. }
  1533. /*
  1534. * Complete all active CCBs with error.
  1535. * Used on CHIP/SCSI RESET.
  1536. */
  1537. static void sym_flush_busy_queue (struct sym_hcb *np, int cam_status)
  1538. {
  1539. /*
  1540. * Move all active CCBs to the COMP queue
  1541. * and flush this queue.
  1542. */
  1543. sym_que_splice(&np->busy_ccbq, &np->comp_ccbq);
  1544. sym_que_init(&np->busy_ccbq);
  1545. sym_flush_comp_queue(np, cam_status);
  1546. }
  1547. /*
  1548. * Start chip.
  1549. *
  1550. * 'reason' means:
  1551. * 0: initialisation.
  1552. * 1: SCSI BUS RESET delivered or received.
  1553. * 2: SCSI BUS MODE changed.
  1554. */
  1555. void sym_start_up(struct Scsi_Host *shost, int reason)
  1556. {
  1557. struct sym_data *sym_data = shost_priv(shost);
  1558. struct pci_dev *pdev = sym_data->pdev;
  1559. struct sym_hcb *np = sym_data->ncb;
  1560. int i;
  1561. u32 phys;
  1562. /*
  1563. * Reset chip if asked, otherwise just clear fifos.
  1564. */
  1565. if (reason == 1)
  1566. sym_soft_reset(np);
  1567. else {
  1568. OUTB(np, nc_stest3, TE|CSF);
  1569. OUTONB(np, nc_ctest3, CLF);
  1570. }
  1571. /*
  1572. * Clear Start Queue
  1573. */
  1574. phys = np->squeue_ba;
  1575. for (i = 0; i < MAX_QUEUE*2; i += 2) {
  1576. np->squeue[i] = cpu_to_scr(np->idletask_ba);
  1577. np->squeue[i+1] = cpu_to_scr(phys + (i+2)*4);
  1578. }
  1579. np->squeue[MAX_QUEUE*2-1] = cpu_to_scr(phys);
  1580. /*
  1581. * Start at first entry.
  1582. */
  1583. np->squeueput = 0;
  1584. /*
  1585. * Clear Done Queue
  1586. */
  1587. phys = np->dqueue_ba;
  1588. for (i = 0; i < MAX_QUEUE*2; i += 2) {
  1589. np->dqueue[i] = 0;
  1590. np->dqueue[i+1] = cpu_to_scr(phys + (i+2)*4);
  1591. }
  1592. np->dqueue[MAX_QUEUE*2-1] = cpu_to_scr(phys);
  1593. /*
  1594. * Start at first entry.
  1595. */
  1596. np->dqueueget = 0;
  1597. /*
  1598. * Install patches in scripts.
  1599. * This also let point to first position the start
  1600. * and done queue pointers used from SCRIPTS.
  1601. */
  1602. np->fw_patch(shost);
  1603. /*
  1604. * Wakeup all pending jobs.
  1605. */
  1606. sym_flush_busy_queue(np, DID_RESET);
  1607. /*
  1608. * Init chip.
  1609. */
  1610. OUTB(np, nc_istat, 0x00); /* Remove Reset, abort */
  1611. INB(np, nc_mbox1);
  1612. udelay(2000); /* The 895 needs time for the bus mode to settle */
  1613. OUTB(np, nc_scntl0, np->rv_scntl0 | 0xc0);
  1614. /* full arb., ena parity, par->ATN */
  1615. OUTB(np, nc_scntl1, 0x00); /* odd parity, and remove CRST!! */
  1616. sym_selectclock(np, np->rv_scntl3); /* Select SCSI clock */
  1617. OUTB(np, nc_scid , RRE|np->myaddr); /* Adapter SCSI address */
  1618. OUTW(np, nc_respid, 1ul<<np->myaddr); /* Id to respond to */
  1619. OUTB(np, nc_istat , SIGP ); /* Signal Process */
  1620. OUTB(np, nc_dmode , np->rv_dmode); /* Burst length, dma mode */
  1621. OUTB(np, nc_ctest5, np->rv_ctest5); /* Large fifo + large burst */
  1622. OUTB(np, nc_dcntl , NOCOM|np->rv_dcntl); /* Protect SFBR */
  1623. OUTB(np, nc_ctest3, np->rv_ctest3); /* Write and invalidate */
  1624. OUTB(np, nc_ctest4, np->rv_ctest4); /* Master parity checking */
  1625. /* Extended Sreq/Sack filtering not supported on the C10 */
  1626. if (np->features & FE_C10)
  1627. OUTB(np, nc_stest2, np->rv_stest2);
  1628. else
  1629. OUTB(np, nc_stest2, EXT|np->rv_stest2);
  1630. OUTB(np, nc_stest3, TE); /* TolerANT enable */
  1631. OUTB(np, nc_stime0, 0x0c); /* HTH disabled STO 0.25 sec */
  1632. /*
  1633. * For now, disable AIP generation on C1010-66.
  1634. */
  1635. if (pdev->device == PCI_DEVICE_ID_LSI_53C1010_66)
  1636. OUTB(np, nc_aipcntl1, DISAIP);
  1637. /*
  1638. * C10101 rev. 0 errata.
  1639. * Errant SGE's when in narrow. Write bits 4 & 5 of
  1640. * STEST1 register to disable SGE. We probably should do
  1641. * that from SCRIPTS for each selection/reselection, but
  1642. * I just don't want. :)
  1643. */
  1644. if (pdev->device == PCI_DEVICE_ID_LSI_53C1010_33 &&
  1645. pdev->revision < 1)
  1646. OUTB(np, nc_stest1, INB(np, nc_stest1) | 0x30);
  1647. /*
  1648. * DEL 441 - 53C876 Rev 5 - Part Number 609-0392787/2788 - ITEM 2.
  1649. * Disable overlapped arbitration for some dual function devices,
  1650. * regardless revision id (kind of post-chip-design feature. ;-))
  1651. */
  1652. if (pdev->device == PCI_DEVICE_ID_NCR_53C875)
  1653. OUTB(np, nc_ctest0, (1<<5));
  1654. else if (pdev->device == PCI_DEVICE_ID_NCR_53C896)
  1655. np->rv_ccntl0 |= DPR;
  1656. /*
  1657. * Write CCNTL0/CCNTL1 for chips capable of 64 bit addressing
  1658. * and/or hardware phase mismatch, since only such chips
  1659. * seem to support those IO registers.
  1660. */
  1661. if (np->features & (FE_DAC|FE_NOPM)) {
  1662. OUTB(np, nc_ccntl0, np->rv_ccntl0);
  1663. OUTB(np, nc_ccntl1, np->rv_ccntl1);
  1664. }
  1665. #if SYM_CONF_DMA_ADDRESSING_MODE == 2
  1666. /*
  1667. * Set up scratch C and DRS IO registers to map the 32 bit
  1668. * DMA address range our data structures are located in.
  1669. */
  1670. if (use_dac(np)) {
  1671. np->dmap_bah[0] = 0; /* ??? */
  1672. OUTL(np, nc_scrx[0], np->dmap_bah[0]);
  1673. OUTL(np, nc_drs, np->dmap_bah[0]);
  1674. }
  1675. #endif
  1676. /*
  1677. * If phase mismatch handled by scripts (895A/896/1010),
  1678. * set PM jump addresses.
  1679. */
  1680. if (np->features & FE_NOPM) {
  1681. OUTL(np, nc_pmjad1, SCRIPTB_BA(np, pm_handle));
  1682. OUTL(np, nc_pmjad2, SCRIPTB_BA(np, pm_handle));
  1683. }
  1684. /*
  1685. * Enable GPIO0 pin for writing if LED support from SCRIPTS.
  1686. * Also set GPIO5 and clear GPIO6 if hardware LED control.
  1687. */
  1688. if (np->features & FE_LED0)
  1689. OUTB(np, nc_gpcntl, INB(np, nc_gpcntl) & ~0x01);
  1690. else if (np->features & FE_LEDC)
  1691. OUTB(np, nc_gpcntl, (INB(np, nc_gpcntl) & ~0x41) | 0x20);
  1692. /*
  1693. * enable ints
  1694. */
  1695. OUTW(np, nc_sien , STO|HTH|MA|SGE|UDC|RST|PAR);
  1696. OUTB(np, nc_dien , MDPE|BF|SSI|SIR|IID);
  1697. /*
  1698. * For 895/6 enable SBMC interrupt and save current SCSI bus mode.
  1699. * Try to eat the spurious SBMC interrupt that may occur when
  1700. * we reset the chip but not the SCSI BUS (at initialization).
  1701. */
  1702. if (np->features & (FE_ULTRA2|FE_ULTRA3)) {
  1703. OUTONW(np, nc_sien, SBMC);
  1704. if (reason == 0) {
  1705. INB(np, nc_mbox1);
  1706. mdelay(100);
  1707. INW(np, nc_sist);
  1708. }
  1709. np->scsi_mode = INB(np, nc_stest4) & SMODE;
  1710. }
  1711. /*
  1712. * Fill in target structure.
  1713. * Reinitialize usrsync.
  1714. * Reinitialize usrwide.
  1715. * Prepare sync negotiation according to actual SCSI bus mode.
  1716. */
  1717. for (i=0;i<SYM_CONF_MAX_TARGET;i++) {
  1718. struct sym_tcb *tp = &np->target[i];
  1719. tp->to_reset = 0;
  1720. tp->head.sval = 0;
  1721. tp->head.wval = np->rv_scntl3;
  1722. tp->head.uval = 0;
  1723. if (tp->lun0p)
  1724. tp->lun0p->to_clear = 0;
  1725. if (tp->lunmp) {
  1726. int ln;
  1727. for (ln = 1; ln < SYM_CONF_MAX_LUN; ln++)
  1728. if (tp->lunmp[ln])
  1729. tp->lunmp[ln]->to_clear = 0;
  1730. }
  1731. }
  1732. /*
  1733. * Download SCSI SCRIPTS to on-chip RAM if present,
  1734. * and start script processor.
  1735. * We do the download preferently from the CPU.
  1736. * For platforms that may not support PCI memory mapping,
  1737. * we use simple SCRIPTS that performs MEMORY MOVEs.
  1738. */
  1739. phys = SCRIPTA_BA(np, init);
  1740. if (np->ram_ba) {
  1741. if (sym_verbose >= 2)
  1742. printf("%s: Downloading SCSI SCRIPTS.\n", sym_name(np));
  1743. memcpy_toio(np->s.ramaddr, np->scripta0, np->scripta_sz);
  1744. if (np->features & FE_RAM8K) {
  1745. memcpy_toio(np->s.ramaddr + 4096, np->scriptb0, np->scriptb_sz);
  1746. phys = scr_to_cpu(np->scr_ram_seg);
  1747. OUTL(np, nc_mmws, phys);
  1748. OUTL(np, nc_mmrs, phys);
  1749. OUTL(np, nc_sfs, phys);
  1750. phys = SCRIPTB_BA(np, start64);
  1751. }
  1752. }
  1753. np->istat_sem = 0;
  1754. OUTL(np, nc_dsa, np->hcb_ba);
  1755. OUTL_DSP(np, phys);
  1756. /*
  1757. * Notify the XPT about the RESET condition.
  1758. */
  1759. if (reason != 0)
  1760. sym_xpt_async_bus_reset(np);
  1761. }
  1762. /*
  1763. * Switch trans mode for current job and its target.
  1764. */
  1765. static void sym_settrans(struct sym_hcb *np, int target, u_char opts, u_char ofs,
  1766. u_char per, u_char wide, u_char div, u_char fak)
  1767. {
  1768. SYM_QUEHEAD *qp;
  1769. u_char sval, wval, uval;
  1770. struct sym_tcb *tp = &np->target[target];
  1771. assert(target == (INB(np, nc_sdid) & 0x0f));
  1772. sval = tp->head.sval;
  1773. wval = tp->head.wval;
  1774. uval = tp->head.uval;
  1775. #if 0
  1776. printf("XXXX sval=%x wval=%x uval=%x (%x)\n",
  1777. sval, wval, uval, np->rv_scntl3);
  1778. #endif
  1779. /*
  1780. * Set the offset.
  1781. */
  1782. if (!(np->features & FE_C10))
  1783. sval = (sval & ~0x1f) | ofs;
  1784. else
  1785. sval = (sval & ~0x3f) | ofs;
  1786. /*
  1787. * Set the sync divisor and extra clock factor.
  1788. */
  1789. if (ofs != 0) {
  1790. wval = (wval & ~0x70) | ((div+1) << 4);
  1791. if (!(np->features & FE_C10))
  1792. sval = (sval & ~0xe0) | (fak << 5);
  1793. else {
  1794. uval = uval & ~(XCLKH_ST|XCLKH_DT|XCLKS_ST|XCLKS_DT);
  1795. if (fak >= 1) uval |= (XCLKH_ST|XCLKH_DT);
  1796. if (fak >= 2) uval |= (XCLKS_ST|XCLKS_DT);
  1797. }
  1798. }
  1799. /*
  1800. * Set the bus width.
  1801. */
  1802. wval = wval & ~EWS;
  1803. if (wide != 0)
  1804. wval |= EWS;
  1805. /*
  1806. * Set misc. ultra enable bits.
  1807. */
  1808. if (np->features & FE_C10) {
  1809. uval = uval & ~(U3EN|AIPCKEN);
  1810. if (opts) {
  1811. assert(np->features & FE_U3EN);
  1812. uval |= U3EN;
  1813. }
  1814. } else {
  1815. wval = wval & ~ULTRA;
  1816. if (per <= 12) wval |= ULTRA;
  1817. }
  1818. /*
  1819. * Stop there if sync parameters are unchanged.
  1820. */
  1821. if (tp->head.sval == sval &&
  1822. tp->head.wval == wval &&
  1823. tp->head.uval == uval)
  1824. return;
  1825. tp->head.sval = sval;
  1826. tp->head.wval = wval;
  1827. tp->head.uval = uval;
  1828. /*
  1829. * Disable extended Sreq/Sack filtering if per < 50.
  1830. * Not supported on the C1010.
  1831. */
  1832. if (per < 50 && !(np->features & FE_C10))
  1833. OUTOFFB(np, nc_stest2, EXT);
  1834. /*
  1835. * set actual value and sync_status
  1836. */
  1837. OUTB(np, nc_sxfer, tp->head.sval);
  1838. OUTB(np, nc_scntl3, tp->head.wval);
  1839. if (np->features & FE_C10) {
  1840. OUTB(np, nc_scntl4, tp->head.uval);
  1841. }
  1842. /*
  1843. * patch ALL busy ccbs of this target.
  1844. */
  1845. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  1846. struct sym_ccb *cp;
  1847. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  1848. if (cp->target != target)
  1849. continue;
  1850. cp->phys.select.sel_scntl3 = tp->head.wval;
  1851. cp->phys.select.sel_sxfer = tp->head.sval;
  1852. if (np->features & FE_C10) {
  1853. cp->phys.select.sel_scntl4 = tp->head.uval;
  1854. }
  1855. }
  1856. }
  1857. static void sym_announce_transfer_rate(struct sym_tcb *tp)
  1858. {
  1859. struct scsi_target *starget = tp->starget;
  1860. if (tp->tprint.period != spi_period(starget) ||
  1861. tp->tprint.offset != spi_offset(starget) ||
  1862. tp->tprint.width != spi_width(starget) ||
  1863. tp->tprint.iu != spi_iu(starget) ||
  1864. tp->tprint.dt != spi_dt(starget) ||
  1865. tp->tprint.qas != spi_qas(starget) ||
  1866. !tp->tprint.check_nego) {
  1867. tp->tprint.period = spi_period(starget);
  1868. tp->tprint.offset = spi_offset(starget);
  1869. tp->tprint.width = spi_width(starget);
  1870. tp->tprint.iu = spi_iu(starget);
  1871. tp->tprint.dt = spi_dt(starget);
  1872. tp->tprint.qas = spi_qas(starget);
  1873. tp->tprint.check_nego = 1;
  1874. spi_display_xfer_agreement(starget);
  1875. }
  1876. }
  1877. /*
  1878. * We received a WDTR.
  1879. * Let everything be aware of the changes.
  1880. */
  1881. static void sym_setwide(struct sym_hcb *np, int target, u_char wide)
  1882. {
  1883. struct sym_tcb *tp = &np->target[target];
  1884. struct scsi_target *starget = tp->starget;
  1885. sym_settrans(np, target, 0, 0, 0, wide, 0, 0);
  1886. if (wide)
  1887. tp->tgoal.renego = NS_WIDE;
  1888. else
  1889. tp->tgoal.renego = 0;
  1890. tp->tgoal.check_nego = 0;
  1891. tp->tgoal.width = wide;
  1892. spi_offset(starget) = 0;
  1893. spi_period(starget) = 0;
  1894. spi_width(starget) = wide;
  1895. spi_iu(starget) = 0;
  1896. spi_dt(starget) = 0;
  1897. spi_qas(starget) = 0;
  1898. if (sym_verbose >= 3)
  1899. sym_announce_transfer_rate(tp);
  1900. }
  1901. /*
  1902. * We received a SDTR.
  1903. * Let everything be aware of the changes.
  1904. */
  1905. static void
  1906. sym_setsync(struct sym_hcb *np, int target,
  1907. u_char ofs, u_char per, u_char div, u_char fak)
  1908. {
  1909. struct sym_tcb *tp = &np->target[target];
  1910. struct scsi_target *starget = tp->starget;
  1911. u_char wide = (tp->head.wval & EWS) ? BUS_16_BIT : BUS_8_BIT;
  1912. sym_settrans(np, target, 0, ofs, per, wide, div, fak);
  1913. if (wide)
  1914. tp->tgoal.renego = NS_WIDE;
  1915. else if (ofs)
  1916. tp->tgoal.renego = NS_SYNC;
  1917. else
  1918. tp->tgoal.renego = 0;
  1919. spi_period(starget) = per;
  1920. spi_offset(starget) = ofs;
  1921. spi_iu(starget) = spi_dt(starget) = spi_qas(starget) = 0;
  1922. if (!tp->tgoal.dt && !tp->tgoal.iu && !tp->tgoal.qas) {
  1923. tp->tgoal.period = per;
  1924. tp->tgoal.offset = ofs;
  1925. tp->tgoal.check_nego = 0;
  1926. }
  1927. sym_announce_transfer_rate(tp);
  1928. }
  1929. /*
  1930. * We received a PPR.
  1931. * Let everything be aware of the changes.
  1932. */
  1933. static void
  1934. sym_setpprot(struct sym_hcb *np, int target, u_char opts, u_char ofs,
  1935. u_char per, u_char wide, u_char div, u_char fak)
  1936. {
  1937. struct sym_tcb *tp = &np->target[target];
  1938. struct scsi_target *starget = tp->starget;
  1939. sym_settrans(np, target, opts, ofs, per, wide, div, fak);
  1940. if (wide || ofs)
  1941. tp->tgoal.renego = NS_PPR;
  1942. else
  1943. tp->tgoal.renego = 0;
  1944. spi_width(starget) = tp->tgoal.width = wide;
  1945. spi_period(starget) = tp->tgoal.period = per;
  1946. spi_offset(starget) = tp->tgoal.offset = ofs;
  1947. spi_iu(starget) = tp->tgoal.iu = !!(opts & PPR_OPT_IU);
  1948. spi_dt(starget) = tp->tgoal.dt = !!(opts & PPR_OPT_DT);
  1949. spi_qas(starget) = tp->tgoal.qas = !!(opts & PPR_OPT_QAS);
  1950. tp->tgoal.check_nego = 0;
  1951. sym_announce_transfer_rate(tp);
  1952. }
  1953. /*
  1954. * generic recovery from scsi interrupt
  1955. *
  1956. * The doc says that when the chip gets an SCSI interrupt,
  1957. * it tries to stop in an orderly fashion, by completing
  1958. * an instruction fetch that had started or by flushing
  1959. * the DMA fifo for a write to memory that was executing.
  1960. * Such a fashion is not enough to know if the instruction
  1961. * that was just before the current DSP value has been
  1962. * executed or not.
  1963. *
  1964. * There are some small SCRIPTS sections that deal with
  1965. * the start queue and the done queue that may break any
  1966. * assomption from the C code if we are interrupted
  1967. * inside, so we reset if this happens. Btw, since these
  1968. * SCRIPTS sections are executed while the SCRIPTS hasn't
  1969. * started SCSI operations, it is very unlikely to happen.
  1970. *
  1971. * All the driver data structures are supposed to be
  1972. * allocated from the same 4 GB memory window, so there
  1973. * is a 1 to 1 relationship between DSA and driver data
  1974. * structures. Since we are careful :) to invalidate the
  1975. * DSA when we complete a command or when the SCRIPTS
  1976. * pushes a DSA into a queue, we can trust it when it
  1977. * points to a CCB.
  1978. */
  1979. static void sym_recover_scsi_int (struct sym_hcb *np, u_char hsts)
  1980. {
  1981. u32 dsp = INL(np, nc_dsp);
  1982. u32 dsa = INL(np, nc_dsa);
  1983. struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
  1984. /*
  1985. * If we haven't been interrupted inside the SCRIPTS
  1986. * critical pathes, we can safely restart the SCRIPTS
  1987. * and trust the DSA value if it matches a CCB.
  1988. */
  1989. if ((!(dsp > SCRIPTA_BA(np, getjob_begin) &&
  1990. dsp < SCRIPTA_BA(np, getjob_end) + 1)) &&
  1991. (!(dsp > SCRIPTA_BA(np, ungetjob) &&
  1992. dsp < SCRIPTA_BA(np, reselect) + 1)) &&
  1993. (!(dsp > SCRIPTB_BA(np, sel_for_abort) &&
  1994. dsp < SCRIPTB_BA(np, sel_for_abort_1) + 1)) &&
  1995. (!(dsp > SCRIPTA_BA(np, done) &&
  1996. dsp < SCRIPTA_BA(np, done_end) + 1))) {
  1997. OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
  1998. OUTB(np, nc_stest3, TE|CSF); /* clear scsi fifo */
  1999. /*
  2000. * If we have a CCB, let the SCRIPTS call us back for
  2001. * the handling of the error with SCRATCHA filled with
  2002. * STARTPOS. This way, we will be able to freeze the
  2003. * device queue and requeue awaiting IOs.
  2004. */
  2005. if (cp) {
  2006. cp->host_status = hsts;
  2007. OUTL_DSP(np, SCRIPTA_BA(np, complete_error));
  2008. }
  2009. /*
  2010. * Otherwise just restart the SCRIPTS.
  2011. */
  2012. else {
  2013. OUTL(np, nc_dsa, 0xffffff);
  2014. OUTL_DSP(np, SCRIPTA_BA(np, start));
  2015. }
  2016. }
  2017. else
  2018. goto reset_all;
  2019. return;
  2020. reset_all:
  2021. sym_start_reset(np);
  2022. }
  2023. /*
  2024. * chip exception handler for selection timeout
  2025. */
  2026. static void sym_int_sto (struct sym_hcb *np)
  2027. {
  2028. u32 dsp = INL(np, nc_dsp);
  2029. if (DEBUG_FLAGS & DEBUG_TINY) printf ("T");
  2030. if (dsp == SCRIPTA_BA(np, wf_sel_done) + 8)
  2031. sym_recover_scsi_int(np, HS_SEL_TIMEOUT);
  2032. else
  2033. sym_start_reset(np);
  2034. }
  2035. /*
  2036. * chip exception handler for unexpected disconnect
  2037. */
  2038. static void sym_int_udc (struct sym_hcb *np)
  2039. {
  2040. printf ("%s: unexpected disconnect\n", sym_name(np));
  2041. sym_recover_scsi_int(np, HS_UNEXPECTED);
  2042. }
  2043. /*
  2044. * chip exception handler for SCSI bus mode change
  2045. *
  2046. * spi2-r12 11.2.3 says a transceiver mode change must
  2047. * generate a reset event and a device that detects a reset
  2048. * event shall initiate a hard reset. It says also that a
  2049. * device that detects a mode change shall set data transfer
  2050. * mode to eight bit asynchronous, etc...
  2051. * So, just reinitializing all except chip should be enough.
  2052. */
  2053. static void sym_int_sbmc(struct Scsi_Host *shost)
  2054. {
  2055. struct sym_hcb *np = sym_get_hcb(shost);
  2056. u_char scsi_mode = INB(np, nc_stest4) & SMODE;
  2057. /*
  2058. * Notify user.
  2059. */
  2060. printf("%s: SCSI BUS mode change from %s to %s.\n", sym_name(np),
  2061. sym_scsi_bus_mode(np->scsi_mode), sym_scsi_bus_mode(scsi_mode));
  2062. /*
  2063. * Should suspend command processing for a few seconds and
  2064. * reinitialize all except the chip.
  2065. */
  2066. sym_start_up(shost, 2);
  2067. }
  2068. /*
  2069. * chip exception handler for SCSI parity error.
  2070. *
  2071. * When the chip detects a SCSI parity error and is
  2072. * currently executing a (CH)MOV instruction, it does
  2073. * not interrupt immediately, but tries to finish the
  2074. * transfer of the current scatter entry before
  2075. * interrupting. The following situations may occur:
  2076. *
  2077. * - The complete scatter entry has been transferred
  2078. * without the device having changed phase.
  2079. * The chip will then interrupt with the DSP pointing
  2080. * to the instruction that follows the MOV.
  2081. *
  2082. * - A phase mismatch occurs before the MOV finished
  2083. * and phase errors are to be handled by the C code.
  2084. * The chip will then interrupt with both PAR and MA
  2085. * conditions set.
  2086. *
  2087. * - A phase mismatch occurs before the MOV finished and
  2088. * phase errors are to be handled by SCRIPTS.
  2089. * The chip will load the DSP with the phase mismatch
  2090. * JUMP address and interrupt the host processor.
  2091. */
  2092. static void sym_int_par (struct sym_hcb *np, u_short sist)
  2093. {
  2094. u_char hsts = INB(np, HS_PRT);
  2095. u32 dsp = INL(np, nc_dsp);
  2096. u32 dbc = INL(np, nc_dbc);
  2097. u32 dsa = INL(np, nc_dsa);
  2098. u_char sbcl = INB(np, nc_sbcl);
  2099. u_char cmd = dbc >> 24;
  2100. int phase = cmd & 7;
  2101. struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
  2102. if (printk_ratelimit())
  2103. printf("%s: SCSI parity error detected: SCR1=%d DBC=%x SBCL=%x\n",
  2104. sym_name(np), hsts, dbc, sbcl);
  2105. /*
  2106. * Check that the chip is connected to the SCSI BUS.
  2107. */
  2108. if (!(INB(np, nc_scntl1) & ISCON)) {
  2109. sym_recover_scsi_int(np, HS_UNEXPECTED);
  2110. return;
  2111. }
  2112. /*
  2113. * If the nexus is not clearly identified, reset the bus.
  2114. * We will try to do better later.
  2115. */
  2116. if (!cp)
  2117. goto reset_all;
  2118. /*
  2119. * Check instruction was a MOV, direction was INPUT and
  2120. * ATN is asserted.
  2121. */
  2122. if ((cmd & 0xc0) || !(phase & 1) || !(sbcl & 0x8))
  2123. goto reset_all;
  2124. /*
  2125. * Keep track of the parity error.
  2126. */
  2127. OUTONB(np, HF_PRT, HF_EXT_ERR);
  2128. cp->xerr_status |= XE_PARITY_ERR;
  2129. /*
  2130. * Prepare the message to send to the device.
  2131. */
  2132. np->msgout[0] = (phase == 7) ? M_PARITY : M_ID_ERROR;
  2133. /*
  2134. * If the old phase was DATA IN phase, we have to deal with
  2135. * the 3 situations described above.
  2136. * For other input phases (MSG IN and STATUS), the device
  2137. * must resend the whole thing that failed parity checking
  2138. * or signal error. So, jumping to dispatcher should be OK.
  2139. */
  2140. if (phase == 1 || phase == 5) {
  2141. /* Phase mismatch handled by SCRIPTS */
  2142. if (dsp == SCRIPTB_BA(np, pm_handle))
  2143. OUTL_DSP(np, dsp);
  2144. /* Phase mismatch handled by the C code */
  2145. else if (sist & MA)
  2146. sym_int_ma (np);
  2147. /* No phase mismatch occurred */
  2148. else {
  2149. sym_set_script_dp (np, cp, dsp);
  2150. OUTL_DSP(np, SCRIPTA_BA(np, dispatch));
  2151. }
  2152. }
  2153. else if (phase == 7) /* We definitely cannot handle parity errors */
  2154. #if 1 /* in message-in phase due to the relection */
  2155. goto reset_all; /* path and various message anticipations. */
  2156. #else
  2157. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  2158. #endif
  2159. else
  2160. OUTL_DSP(np, SCRIPTA_BA(np, dispatch));
  2161. return;
  2162. reset_all:
  2163. sym_start_reset(np);
  2164. return;
  2165. }
  2166. /*
  2167. * chip exception handler for phase errors.
  2168. *
  2169. * We have to construct a new transfer descriptor,
  2170. * to transfer the rest of the current block.
  2171. */
  2172. static void sym_int_ma (struct sym_hcb *np)
  2173. {
  2174. u32 dbc;
  2175. u32 rest;
  2176. u32 dsp;
  2177. u32 dsa;
  2178. u32 nxtdsp;
  2179. u32 *vdsp;
  2180. u32 oadr, olen;
  2181. u32 *tblp;
  2182. u32 newcmd;
  2183. u_int delta;
  2184. u_char cmd;
  2185. u_char hflags, hflags0;
  2186. struct sym_pmc *pm;
  2187. struct sym_ccb *cp;
  2188. dsp = INL(np, nc_dsp);
  2189. dbc = INL(np, nc_dbc);
  2190. dsa = INL(np, nc_dsa);
  2191. cmd = dbc >> 24;
  2192. rest = dbc & 0xffffff;
  2193. delta = 0;
  2194. /*
  2195. * locate matching cp if any.
  2196. */
  2197. cp = sym_ccb_from_dsa(np, dsa);
  2198. /*
  2199. * Donnot take into account dma fifo and various buffers in
  2200. * INPUT phase since the chip flushes everything before
  2201. * raising the MA interrupt for interrupted INPUT phases.
  2202. * For DATA IN phase, we will check for the SWIDE later.
  2203. */
  2204. if ((cmd & 7) != 1 && (cmd & 7) != 5) {
  2205. u_char ss0, ss2;
  2206. if (np->features & FE_DFBC)
  2207. delta = INW(np, nc_dfbc);
  2208. else {
  2209. u32 dfifo;
  2210. /*
  2211. * Read DFIFO, CTEST[4-6] using 1 PCI bus ownership.
  2212. */
  2213. dfifo = INL(np, nc_dfifo);
  2214. /*
  2215. * Calculate remaining bytes in DMA fifo.
  2216. * (CTEST5 = dfifo >> 16)
  2217. */
  2218. if (dfifo & (DFS << 16))
  2219. delta = ((((dfifo >> 8) & 0x300) |
  2220. (dfifo & 0xff)) - rest) & 0x3ff;
  2221. else
  2222. delta = ((dfifo & 0xff) - rest) & 0x7f;
  2223. }
  2224. /*
  2225. * The data in the dma fifo has not been transfered to
  2226. * the target -> add the amount to the rest
  2227. * and clear the data.
  2228. * Check the sstat2 register in case of wide transfer.
  2229. */
  2230. rest += delta;
  2231. ss0 = INB(np, nc_sstat0);
  2232. if (ss0 & OLF) rest++;
  2233. if (!(np->features & FE_C10))
  2234. if (ss0 & ORF) rest++;
  2235. if (cp && (cp->phys.select.sel_scntl3 & EWS)) {
  2236. ss2 = INB(np, nc_sstat2);
  2237. if (ss2 & OLF1) rest++;
  2238. if (!(np->features & FE_C10))
  2239. if (ss2 & ORF1) rest++;
  2240. }
  2241. /*
  2242. * Clear fifos.
  2243. */
  2244. OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* dma fifo */
  2245. OUTB(np, nc_stest3, TE|CSF); /* scsi fifo */
  2246. }
  2247. /*
  2248. * log the information
  2249. */
  2250. if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_PHASE))
  2251. printf ("P%x%x RL=%d D=%d ", cmd&7, INB(np, nc_sbcl)&7,
  2252. (unsigned) rest, (unsigned) delta);
  2253. /*
  2254. * try to find the interrupted script command,
  2255. * and the address at which to continue.
  2256. */
  2257. vdsp = NULL;
  2258. nxtdsp = 0;
  2259. if (dsp > np->scripta_ba &&
  2260. dsp <= np->scripta_ba + np->scripta_sz) {
  2261. vdsp = (u32 *)((char*)np->scripta0 + (dsp-np->scripta_ba-8));
  2262. nxtdsp = dsp;
  2263. }
  2264. else if (dsp > np->scriptb_ba &&
  2265. dsp <= np->scriptb_ba + np->scriptb_sz) {
  2266. vdsp = (u32 *)((char*)np->scriptb0 + (dsp-np->scriptb_ba-8));
  2267. nxtdsp = dsp;
  2268. }
  2269. /*
  2270. * log the information
  2271. */
  2272. if (DEBUG_FLAGS & DEBUG_PHASE) {
  2273. printf ("\nCP=%p DSP=%x NXT=%x VDSP=%p CMD=%x ",
  2274. cp, (unsigned)dsp, (unsigned)nxtdsp, vdsp, cmd);
  2275. }
  2276. if (!vdsp) {
  2277. printf ("%s: interrupted SCRIPT address not found.\n",
  2278. sym_name (np));
  2279. goto reset_all;
  2280. }
  2281. if (!cp) {
  2282. printf ("%s: SCSI phase error fixup: CCB already dequeued.\n",
  2283. sym_name (np));
  2284. goto reset_all;
  2285. }
  2286. /*
  2287. * get old startaddress and old length.
  2288. */
  2289. oadr = scr_to_cpu(vdsp[1]);
  2290. if (cmd & 0x10) { /* Table indirect */
  2291. tblp = (u32 *) ((char*) &cp->phys + oadr);
  2292. olen = scr_to_cpu(tblp[0]);
  2293. oadr = scr_to_cpu(tblp[1]);
  2294. } else {
  2295. tblp = (u32 *) 0;
  2296. olen = scr_to_cpu(vdsp[0]) & 0xffffff;
  2297. }
  2298. if (DEBUG_FLAGS & DEBUG_PHASE) {
  2299. printf ("OCMD=%x\nTBLP=%p OLEN=%x OADR=%x\n",
  2300. (unsigned) (scr_to_cpu(vdsp[0]) >> 24),
  2301. tblp,
  2302. (unsigned) olen,
  2303. (unsigned) oadr);
  2304. }
  2305. /*
  2306. * check cmd against assumed interrupted script command.
  2307. * If dt data phase, the MOVE instruction hasn't bit 4 of
  2308. * the phase.
  2309. */
  2310. if (((cmd & 2) ? cmd : (cmd & ~4)) != (scr_to_cpu(vdsp[0]) >> 24)) {
  2311. sym_print_addr(cp->cmd,
  2312. "internal error: cmd=%02x != %02x=(vdsp[0] >> 24)\n",
  2313. cmd, scr_to_cpu(vdsp[0]) >> 24);
  2314. goto reset_all;
  2315. }
  2316. /*
  2317. * if old phase not dataphase, leave here.
  2318. */
  2319. if (cmd & 2) {
  2320. sym_print_addr(cp->cmd,
  2321. "phase change %x-%x %d@%08x resid=%d.\n",
  2322. cmd&7, INB(np, nc_sbcl)&7, (unsigned)olen,
  2323. (unsigned)oadr, (unsigned)rest);
  2324. goto unexpected_phase;
  2325. }
  2326. /*
  2327. * Choose the correct PM save area.
  2328. *
  2329. * Look at the PM_SAVE SCRIPT if you want to understand
  2330. * this stuff. The equivalent code is implemented in
  2331. * SCRIPTS for the 895A, 896 and 1010 that are able to
  2332. * handle PM from the SCRIPTS processor.
  2333. */
  2334. hflags0 = INB(np, HF_PRT);
  2335. hflags = hflags0;
  2336. if (hflags & (HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED)) {
  2337. if (hflags & HF_IN_PM0)
  2338. nxtdsp = scr_to_cpu(cp->phys.pm0.ret);
  2339. else if (hflags & HF_IN_PM1)
  2340. nxtdsp = scr_to_cpu(cp->phys.pm1.ret);
  2341. if (hflags & HF_DP_SAVED)
  2342. hflags ^= HF_ACT_PM;
  2343. }
  2344. if (!(hflags & HF_ACT_PM)) {
  2345. pm = &cp->phys.pm0;
  2346. newcmd = SCRIPTA_BA(np, pm0_data);
  2347. }
  2348. else {
  2349. pm = &cp->phys.pm1;
  2350. newcmd = SCRIPTA_BA(np, pm1_data);
  2351. }
  2352. hflags &= ~(HF_IN_PM0 | HF_IN_PM1 | HF_DP_SAVED);
  2353. if (hflags != hflags0)
  2354. OUTB(np, HF_PRT, hflags);
  2355. /*
  2356. * fillin the phase mismatch context
  2357. */
  2358. pm->sg.addr = cpu_to_scr(oadr + olen - rest);
  2359. pm->sg.size = cpu_to_scr(rest);
  2360. pm->ret = cpu_to_scr(nxtdsp);
  2361. /*
  2362. * If we have a SWIDE,
  2363. * - prepare the address to write the SWIDE from SCRIPTS,
  2364. * - compute the SCRIPTS address to restart from,
  2365. * - move current data pointer context by one byte.
  2366. */
  2367. nxtdsp = SCRIPTA_BA(np, dispatch);
  2368. if ((cmd & 7) == 1 && cp && (cp->phys.select.sel_scntl3 & EWS) &&
  2369. (INB(np, nc_scntl2) & WSR)) {
  2370. u32 tmp;
  2371. /*
  2372. * Set up the table indirect for the MOVE
  2373. * of the residual byte and adjust the data
  2374. * pointer context.
  2375. */
  2376. tmp = scr_to_cpu(pm->sg.addr);
  2377. cp->phys.wresid.addr = cpu_to_scr(tmp);
  2378. pm->sg.addr = cpu_to_scr(tmp + 1);
  2379. tmp = scr_to_cpu(pm->sg.size);
  2380. cp->phys.wresid.size = cpu_to_scr((tmp&0xff000000) | 1);
  2381. pm->sg.size = cpu_to_scr(tmp - 1);
  2382. /*
  2383. * If only the residual byte is to be moved,
  2384. * no PM context is needed.
  2385. */
  2386. if ((tmp&0xffffff) == 1)
  2387. newcmd = pm->ret;
  2388. /*
  2389. * Prepare the address of SCRIPTS that will
  2390. * move the residual byte to memory.
  2391. */
  2392. nxtdsp = SCRIPTB_BA(np, wsr_ma_helper);
  2393. }
  2394. if (DEBUG_FLAGS & DEBUG_PHASE) {
  2395. sym_print_addr(cp->cmd, "PM %x %x %x / %x %x %x.\n",
  2396. hflags0, hflags, newcmd,
  2397. (unsigned)scr_to_cpu(pm->sg.addr),
  2398. (unsigned)scr_to_cpu(pm->sg.size),
  2399. (unsigned)scr_to_cpu(pm->ret));
  2400. }
  2401. /*
  2402. * Restart the SCRIPTS processor.
  2403. */
  2404. sym_set_script_dp (np, cp, newcmd);
  2405. OUTL_DSP(np, nxtdsp);
  2406. return;
  2407. /*
  2408. * Unexpected phase changes that occurs when the current phase
  2409. * is not a DATA IN or DATA OUT phase are due to error conditions.
  2410. * Such event may only happen when the SCRIPTS is using a
  2411. * multibyte SCSI MOVE.
  2412. *
  2413. * Phase change Some possible cause
  2414. *
  2415. * COMMAND --> MSG IN SCSI parity error detected by target.
  2416. * COMMAND --> STATUS Bad command or refused by target.
  2417. * MSG OUT --> MSG IN Message rejected by target.
  2418. * MSG OUT --> COMMAND Bogus target that discards extended
  2419. * negotiation messages.
  2420. *
  2421. * The code below does not care of the new phase and so
  2422. * trusts the target. Why to annoy it ?
  2423. * If the interrupted phase is COMMAND phase, we restart at
  2424. * dispatcher.
  2425. * If a target does not get all the messages after selection,
  2426. * the code assumes blindly that the target discards extended
  2427. * messages and clears the negotiation status.
  2428. * If the target does not want all our response to negotiation,
  2429. * we force a SIR_NEGO_PROTO interrupt (it is a hack that avoids
  2430. * bloat for such a should_not_happen situation).
  2431. * In all other situation, we reset the BUS.
  2432. * Are these assumptions reasonnable ? (Wait and see ...)
  2433. */
  2434. unexpected_phase:
  2435. dsp -= 8;
  2436. nxtdsp = 0;
  2437. switch (cmd & 7) {
  2438. case 2: /* COMMAND phase */
  2439. nxtdsp = SCRIPTA_BA(np, dispatch);
  2440. break;
  2441. #if 0
  2442. case 3: /* STATUS phase */
  2443. nxtdsp = SCRIPTA_BA(np, dispatch);
  2444. break;
  2445. #endif
  2446. case 6: /* MSG OUT phase */
  2447. /*
  2448. * If the device may want to use untagged when we want
  2449. * tagged, we prepare an IDENTIFY without disc. granted,
  2450. * since we will not be able to handle reselect.
  2451. * Otherwise, we just don't care.
  2452. */
  2453. if (dsp == SCRIPTA_BA(np, send_ident)) {
  2454. if (cp->tag != NO_TAG && olen - rest <= 3) {
  2455. cp->host_status = HS_BUSY;
  2456. np->msgout[0] = IDENTIFY(0, cp->lun);
  2457. nxtdsp = SCRIPTB_BA(np, ident_break_atn);
  2458. }
  2459. else
  2460. nxtdsp = SCRIPTB_BA(np, ident_break);
  2461. }
  2462. else if (dsp == SCRIPTB_BA(np, send_wdtr) ||
  2463. dsp == SCRIPTB_BA(np, send_sdtr) ||
  2464. dsp == SCRIPTB_BA(np, send_ppr)) {
  2465. nxtdsp = SCRIPTB_BA(np, nego_bad_phase);
  2466. if (dsp == SCRIPTB_BA(np, send_ppr)) {
  2467. struct scsi_device *dev = cp->cmd->device;
  2468. dev->ppr = 0;
  2469. }
  2470. }
  2471. break;
  2472. #if 0
  2473. case 7: /* MSG IN phase */
  2474. nxtdsp = SCRIPTA_BA(np, clrack);
  2475. break;
  2476. #endif
  2477. }
  2478. if (nxtdsp) {
  2479. OUTL_DSP(np, nxtdsp);
  2480. return;
  2481. }
  2482. reset_all:
  2483. sym_start_reset(np);
  2484. }
  2485. /*
  2486. * chip interrupt handler
  2487. *
  2488. * In normal situations, interrupt conditions occur one at
  2489. * a time. But when something bad happens on the SCSI BUS,
  2490. * the chip may raise several interrupt flags before
  2491. * stopping and interrupting the CPU. The additionnal
  2492. * interrupt flags are stacked in some extra registers
  2493. * after the SIP and/or DIP flag has been raised in the
  2494. * ISTAT. After the CPU has read the interrupt condition
  2495. * flag from SIST or DSTAT, the chip unstacks the other
  2496. * interrupt flags and sets the corresponding bits in
  2497. * SIST or DSTAT. Since the chip starts stacking once the
  2498. * SIP or DIP flag is set, there is a small window of time
  2499. * where the stacking does not occur.
  2500. *
  2501. * Typically, multiple interrupt conditions may happen in
  2502. * the following situations:
  2503. *
  2504. * - SCSI parity error + Phase mismatch (PAR|MA)
  2505. * When an parity error is detected in input phase
  2506. * and the device switches to msg-in phase inside a
  2507. * block MOV.
  2508. * - SCSI parity error + Unexpected disconnect (PAR|UDC)
  2509. * When a stupid device does not want to handle the
  2510. * recovery of an SCSI parity error.
  2511. * - Some combinations of STO, PAR, UDC, ...
  2512. * When using non compliant SCSI stuff, when user is
  2513. * doing non compliant hot tampering on the BUS, when
  2514. * something really bad happens to a device, etc ...
  2515. *
  2516. * The heuristic suggested by SYMBIOS to handle
  2517. * multiple interrupts is to try unstacking all
  2518. * interrupts conditions and to handle them on some
  2519. * priority based on error severity.
  2520. * This will work when the unstacking has been
  2521. * successful, but we cannot be 100 % sure of that,
  2522. * since the CPU may have been faster to unstack than
  2523. * the chip is able to stack. Hmmm ... But it seems that
  2524. * such a situation is very unlikely to happen.
  2525. *
  2526. * If this happen, for example STO caught by the CPU
  2527. * then UDC happenning before the CPU have restarted
  2528. * the SCRIPTS, the driver may wrongly complete the
  2529. * same command on UDC, since the SCRIPTS didn't restart
  2530. * and the DSA still points to the same command.
  2531. * We avoid this situation by setting the DSA to an
  2532. * invalid value when the CCB is completed and before
  2533. * restarting the SCRIPTS.
  2534. *
  2535. * Another issue is that we need some section of our
  2536. * recovery procedures to be somehow uninterruptible but
  2537. * the SCRIPTS processor does not provides such a
  2538. * feature. For this reason, we handle recovery preferently
  2539. * from the C code and check against some SCRIPTS critical
  2540. * sections from the C code.
  2541. *
  2542. * Hopefully, the interrupt handling of the driver is now
  2543. * able to resist to weird BUS error conditions, but donnot
  2544. * ask me for any guarantee that it will never fail. :-)
  2545. * Use at your own decision and risk.
  2546. */
  2547. irqreturn_t sym_interrupt(struct Scsi_Host *shost)
  2548. {
  2549. struct sym_data *sym_data = shost_priv(shost);
  2550. struct sym_hcb *np = sym_data->ncb;
  2551. struct pci_dev *pdev = sym_data->pdev;
  2552. u_char istat, istatc;
  2553. u_char dstat;
  2554. u_short sist;
  2555. /*
  2556. * interrupt on the fly ?
  2557. * (SCRIPTS may still be running)
  2558. *
  2559. * A `dummy read' is needed to ensure that the
  2560. * clear of the INTF flag reaches the device
  2561. * and that posted writes are flushed to memory
  2562. * before the scanning of the DONE queue.
  2563. * Note that SCRIPTS also (dummy) read to memory
  2564. * prior to deliver the INTF interrupt condition.
  2565. */
  2566. istat = INB(np, nc_istat);
  2567. if (istat & INTF) {
  2568. OUTB(np, nc_istat, (istat & SIGP) | INTF | np->istat_sem);
  2569. istat |= INB(np, nc_istat); /* DUMMY READ */
  2570. if (DEBUG_FLAGS & DEBUG_TINY) printf ("F ");
  2571. sym_wakeup_done(np);
  2572. }
  2573. if (!(istat & (SIP|DIP)))
  2574. return (istat & INTF) ? IRQ_HANDLED : IRQ_NONE;
  2575. #if 0 /* We should never get this one */
  2576. if (istat & CABRT)
  2577. OUTB(np, nc_istat, CABRT);
  2578. #endif
  2579. /*
  2580. * PAR and MA interrupts may occur at the same time,
  2581. * and we need to know of both in order to handle
  2582. * this situation properly. We try to unstack SCSI
  2583. * interrupts for that reason. BTW, I dislike a LOT
  2584. * such a loop inside the interrupt routine.
  2585. * Even if DMA interrupt stacking is very unlikely to
  2586. * happen, we also try unstacking these ones, since
  2587. * this has no performance impact.
  2588. */
  2589. sist = 0;
  2590. dstat = 0;
  2591. istatc = istat;
  2592. do {
  2593. if (istatc & SIP)
  2594. sist |= INW(np, nc_sist);
  2595. if (istatc & DIP)
  2596. dstat |= INB(np, nc_dstat);
  2597. istatc = INB(np, nc_istat);
  2598. istat |= istatc;
  2599. /* Prevent deadlock waiting on a condition that may
  2600. * never clear. */
  2601. if (unlikely(sist == 0xffff && dstat == 0xff)) {
  2602. if (pci_channel_offline(pdev))
  2603. return IRQ_NONE;
  2604. }
  2605. } while (istatc & (SIP|DIP));
  2606. if (DEBUG_FLAGS & DEBUG_TINY)
  2607. printf ("<%d|%x:%x|%x:%x>",
  2608. (int)INB(np, nc_scr0),
  2609. dstat,sist,
  2610. (unsigned)INL(np, nc_dsp),
  2611. (unsigned)INL(np, nc_dbc));
  2612. /*
  2613. * On paper, a memory read barrier may be needed here to
  2614. * prevent out of order LOADs by the CPU from having
  2615. * prefetched stale data prior to DMA having occurred.
  2616. * And since we are paranoid ... :)
  2617. */
  2618. MEMORY_READ_BARRIER();
  2619. /*
  2620. * First, interrupts we want to service cleanly.
  2621. *
  2622. * Phase mismatch (MA) is the most frequent interrupt
  2623. * for chip earlier than the 896 and so we have to service
  2624. * it as quickly as possible.
  2625. * A SCSI parity error (PAR) may be combined with a phase
  2626. * mismatch condition (MA).
  2627. * Programmed interrupts (SIR) are used to call the C code
  2628. * from SCRIPTS.
  2629. * The single step interrupt (SSI) is not used in this
  2630. * driver.
  2631. */
  2632. if (!(sist & (STO|GEN|HTH|SGE|UDC|SBMC|RST)) &&
  2633. !(dstat & (MDPE|BF|ABRT|IID))) {
  2634. if (sist & PAR) sym_int_par (np, sist);
  2635. else if (sist & MA) sym_int_ma (np);
  2636. else if (dstat & SIR) sym_int_sir(np);
  2637. else if (dstat & SSI) OUTONB_STD();
  2638. else goto unknown_int;
  2639. return IRQ_HANDLED;
  2640. }
  2641. /*
  2642. * Now, interrupts that donnot happen in normal
  2643. * situations and that we may need to recover from.
  2644. *
  2645. * On SCSI RESET (RST), we reset everything.
  2646. * On SCSI BUS MODE CHANGE (SBMC), we complete all
  2647. * active CCBs with RESET status, prepare all devices
  2648. * for negotiating again and restart the SCRIPTS.
  2649. * On STO and UDC, we complete the CCB with the corres-
  2650. * ponding status and restart the SCRIPTS.
  2651. */
  2652. if (sist & RST) {
  2653. printf("%s: SCSI BUS reset detected.\n", sym_name(np));
  2654. sym_start_up(shost, 1);
  2655. return IRQ_HANDLED;
  2656. }
  2657. OUTB(np, nc_ctest3, np->rv_ctest3 | CLF); /* clear dma fifo */
  2658. OUTB(np, nc_stest3, TE|CSF); /* clear scsi fifo */
  2659. if (!(sist & (GEN|HTH|SGE)) &&
  2660. !(dstat & (MDPE|BF|ABRT|IID))) {
  2661. if (sist & SBMC) sym_int_sbmc(shost);
  2662. else if (sist & STO) sym_int_sto (np);
  2663. else if (sist & UDC) sym_int_udc (np);
  2664. else goto unknown_int;
  2665. return IRQ_HANDLED;
  2666. }
  2667. /*
  2668. * Now, interrupts we are not able to recover cleanly.
  2669. *
  2670. * Log message for hard errors.
  2671. * Reset everything.
  2672. */
  2673. sym_log_hard_error(shost, sist, dstat);
  2674. if ((sist & (GEN|HTH|SGE)) ||
  2675. (dstat & (MDPE|BF|ABRT|IID))) {
  2676. sym_start_reset(np);
  2677. return IRQ_HANDLED;
  2678. }
  2679. unknown_int:
  2680. /*
  2681. * We just miss the cause of the interrupt. :(
  2682. * Print a message. The timeout will do the real work.
  2683. */
  2684. printf( "%s: unknown interrupt(s) ignored, "
  2685. "ISTAT=0x%x DSTAT=0x%x SIST=0x%x\n",
  2686. sym_name(np), istat, dstat, sist);
  2687. return IRQ_NONE;
  2688. }
  2689. /*
  2690. * Dequeue from the START queue all CCBs that match
  2691. * a given target/lun/task condition (-1 means all),
  2692. * and move them from the BUSY queue to the COMP queue
  2693. * with DID_SOFT_ERROR status condition.
  2694. * This function is used during error handling/recovery.
  2695. * It is called with SCRIPTS not running.
  2696. */
  2697. static int
  2698. sym_dequeue_from_squeue(struct sym_hcb *np, int i, int target, int lun, int task)
  2699. {
  2700. int j;
  2701. struct sym_ccb *cp;
  2702. /*
  2703. * Make sure the starting index is within range.
  2704. */
  2705. assert((i >= 0) && (i < 2*MAX_QUEUE));
  2706. /*
  2707. * Walk until end of START queue and dequeue every job
  2708. * that matches the target/lun/task condition.
  2709. */
  2710. j = i;
  2711. while (i != np->squeueput) {
  2712. cp = sym_ccb_from_dsa(np, scr_to_cpu(np->squeue[i]));
  2713. assert(cp);
  2714. #ifdef SYM_CONF_IARB_SUPPORT
  2715. /* Forget hints for IARB, they may be no longer relevant */
  2716. cp->host_flags &= ~HF_HINT_IARB;
  2717. #endif
  2718. if ((target == -1 || cp->target == target) &&
  2719. (lun == -1 || cp->lun == lun) &&
  2720. (task == -1 || cp->tag == task)) {
  2721. sym_set_cam_status(cp->cmd, DID_SOFT_ERROR);
  2722. sym_remque(&cp->link_ccbq);
  2723. sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
  2724. }
  2725. else {
  2726. if (i != j)
  2727. np->squeue[j] = np->squeue[i];
  2728. if ((j += 2) >= MAX_QUEUE*2) j = 0;
  2729. }
  2730. if ((i += 2) >= MAX_QUEUE*2) i = 0;
  2731. }
  2732. if (i != j) /* Copy back the idle task if needed */
  2733. np->squeue[j] = np->squeue[i];
  2734. np->squeueput = j; /* Update our current start queue pointer */
  2735. return (i - j) / 2;
  2736. }
  2737. /*
  2738. * chip handler for bad SCSI status condition
  2739. *
  2740. * In case of bad SCSI status, we unqueue all the tasks
  2741. * currently queued to the controller but not yet started
  2742. * and then restart the SCRIPTS processor immediately.
  2743. *
  2744. * QUEUE FULL and BUSY conditions are handled the same way.
  2745. * Basically all the not yet started tasks are requeued in
  2746. * device queue and the queue is frozen until a completion.
  2747. *
  2748. * For CHECK CONDITION and COMMAND TERMINATED status, we use
  2749. * the CCB of the failed command to prepare a REQUEST SENSE
  2750. * SCSI command and queue it to the controller queue.
  2751. *
  2752. * SCRATCHA is assumed to have been loaded with STARTPOS
  2753. * before the SCRIPTS called the C code.
  2754. */
  2755. static void sym_sir_bad_scsi_status(struct sym_hcb *np, int num, struct sym_ccb *cp)
  2756. {
  2757. u32 startp;
  2758. u_char s_status = cp->ssss_status;
  2759. u_char h_flags = cp->host_flags;
  2760. int msglen;
  2761. int i;
  2762. /*
  2763. * Compute the index of the next job to start from SCRIPTS.
  2764. */
  2765. i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
  2766. /*
  2767. * The last CCB queued used for IARB hint may be
  2768. * no longer relevant. Forget it.
  2769. */
  2770. #ifdef SYM_CONF_IARB_SUPPORT
  2771. if (np->last_cp)
  2772. np->last_cp = 0;
  2773. #endif
  2774. /*
  2775. * Now deal with the SCSI status.
  2776. */
  2777. switch(s_status) {
  2778. case S_BUSY:
  2779. case S_QUEUE_FULL:
  2780. if (sym_verbose >= 2) {
  2781. sym_print_addr(cp->cmd, "%s\n",
  2782. s_status == S_BUSY ? "BUSY" : "QUEUE FULL\n");
  2783. }
  2784. default: /* S_INT, S_INT_COND_MET, S_CONFLICT */
  2785. sym_complete_error (np, cp);
  2786. break;
  2787. case S_TERMINATED:
  2788. case S_CHECK_COND:
  2789. /*
  2790. * If we get an SCSI error when requesting sense, give up.
  2791. */
  2792. if (h_flags & HF_SENSE) {
  2793. sym_complete_error (np, cp);
  2794. break;
  2795. }
  2796. /*
  2797. * Dequeue all queued CCBs for that device not yet started,
  2798. * and restart the SCRIPTS processor immediately.
  2799. */
  2800. sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
  2801. OUTL_DSP(np, SCRIPTA_BA(np, start));
  2802. /*
  2803. * Save some info of the actual IO.
  2804. * Compute the data residual.
  2805. */
  2806. cp->sv_scsi_status = cp->ssss_status;
  2807. cp->sv_xerr_status = cp->xerr_status;
  2808. cp->sv_resid = sym_compute_residual(np, cp);
  2809. /*
  2810. * Prepare all needed data structures for
  2811. * requesting sense data.
  2812. */
  2813. cp->scsi_smsg2[0] = IDENTIFY(0, cp->lun);
  2814. msglen = 1;
  2815. /*
  2816. * If we are currently using anything different from
  2817. * async. 8 bit data transfers with that target,
  2818. * start a negotiation, since the device may want
  2819. * to report us a UNIT ATTENTION condition due to
  2820. * a cause we currently ignore, and we donnot want
  2821. * to be stuck with WIDE and/or SYNC data transfer.
  2822. *
  2823. * cp->nego_status is filled by sym_prepare_nego().
  2824. */
  2825. cp->nego_status = 0;
  2826. msglen += sym_prepare_nego(np, cp, &cp->scsi_smsg2[msglen]);
  2827. /*
  2828. * Message table indirect structure.
  2829. */
  2830. cp->phys.smsg.addr = CCB_BA(cp, scsi_smsg2);
  2831. cp->phys.smsg.size = cpu_to_scr(msglen);
  2832. /*
  2833. * sense command
  2834. */
  2835. cp->phys.cmd.addr = CCB_BA(cp, sensecmd);
  2836. cp->phys.cmd.size = cpu_to_scr(6);
  2837. /*
  2838. * patch requested size into sense command
  2839. */
  2840. cp->sensecmd[0] = REQUEST_SENSE;
  2841. cp->sensecmd[1] = 0;
  2842. if (cp->cmd->device->scsi_level <= SCSI_2 && cp->lun <= 7)
  2843. cp->sensecmd[1] = cp->lun << 5;
  2844. cp->sensecmd[4] = SYM_SNS_BBUF_LEN;
  2845. cp->data_len = SYM_SNS_BBUF_LEN;
  2846. /*
  2847. * sense data
  2848. */
  2849. memset(cp->sns_bbuf, 0, SYM_SNS_BBUF_LEN);
  2850. cp->phys.sense.addr = CCB_BA(cp, sns_bbuf);
  2851. cp->phys.sense.size = cpu_to_scr(SYM_SNS_BBUF_LEN);
  2852. /*
  2853. * requeue the command.
  2854. */
  2855. startp = SCRIPTB_BA(np, sdata_in);
  2856. cp->phys.head.savep = cpu_to_scr(startp);
  2857. cp->phys.head.lastp = cpu_to_scr(startp);
  2858. cp->startp = cpu_to_scr(startp);
  2859. cp->goalp = cpu_to_scr(startp + 16);
  2860. cp->host_xflags = 0;
  2861. cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
  2862. cp->ssss_status = S_ILLEGAL;
  2863. cp->host_flags = (HF_SENSE|HF_DATA_IN);
  2864. cp->xerr_status = 0;
  2865. cp->extra_bytes = 0;
  2866. cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, select));
  2867. /*
  2868. * Requeue the command.
  2869. */
  2870. sym_put_start_queue(np, cp);
  2871. /*
  2872. * Give back to upper layer everything we have dequeued.
  2873. */
  2874. sym_flush_comp_queue(np, 0);
  2875. break;
  2876. }
  2877. }
  2878. /*
  2879. * After a device has accepted some management message
  2880. * as BUS DEVICE RESET, ABORT TASK, etc ..., or when
  2881. * a device signals a UNIT ATTENTION condition, some
  2882. * tasks are thrown away by the device. We are required
  2883. * to reflect that on our tasks list since the device
  2884. * will never complete these tasks.
  2885. *
  2886. * This function move from the BUSY queue to the COMP
  2887. * queue all disconnected CCBs for a given target that
  2888. * match the following criteria:
  2889. * - lun=-1 means any logical UNIT otherwise a given one.
  2890. * - task=-1 means any task, otherwise a given one.
  2891. */
  2892. int sym_clear_tasks(struct sym_hcb *np, int cam_status, int target, int lun, int task)
  2893. {
  2894. SYM_QUEHEAD qtmp, *qp;
  2895. int i = 0;
  2896. struct sym_ccb *cp;
  2897. /*
  2898. * Move the entire BUSY queue to our temporary queue.
  2899. */
  2900. sym_que_init(&qtmp);
  2901. sym_que_splice(&np->busy_ccbq, &qtmp);
  2902. sym_que_init(&np->busy_ccbq);
  2903. /*
  2904. * Put all CCBs that matches our criteria into
  2905. * the COMP queue and put back other ones into
  2906. * the BUSY queue.
  2907. */
  2908. while ((qp = sym_remque_head(&qtmp)) != NULL) {
  2909. struct scsi_cmnd *cmd;
  2910. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  2911. cmd = cp->cmd;
  2912. if (cp->host_status != HS_DISCONNECT ||
  2913. cp->target != target ||
  2914. (lun != -1 && cp->lun != lun) ||
  2915. (task != -1 &&
  2916. (cp->tag != NO_TAG && cp->scsi_smsg[2] != task))) {
  2917. sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
  2918. continue;
  2919. }
  2920. sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
  2921. /* Preserve the software timeout condition */
  2922. if (sym_get_cam_status(cmd) != DID_TIME_OUT)
  2923. sym_set_cam_status(cmd, cam_status);
  2924. ++i;
  2925. #if 0
  2926. printf("XXXX TASK @%p CLEARED\n", cp);
  2927. #endif
  2928. }
  2929. return i;
  2930. }
  2931. /*
  2932. * chip handler for TASKS recovery
  2933. *
  2934. * We cannot safely abort a command, while the SCRIPTS
  2935. * processor is running, since we just would be in race
  2936. * with it.
  2937. *
  2938. * As long as we have tasks to abort, we keep the SEM
  2939. * bit set in the ISTAT. When this bit is set, the
  2940. * SCRIPTS processor interrupts (SIR_SCRIPT_STOPPED)
  2941. * each time it enters the scheduler.
  2942. *
  2943. * If we have to reset a target, clear tasks of a unit,
  2944. * or to perform the abort of a disconnected job, we
  2945. * restart the SCRIPTS for selecting the target. Once
  2946. * selected, the SCRIPTS interrupts (SIR_TARGET_SELECTED).
  2947. * If it loses arbitration, the SCRIPTS will interrupt again
  2948. * the next time it will enter its scheduler, and so on ...
  2949. *
  2950. * On SIR_TARGET_SELECTED, we scan for the more
  2951. * appropriate thing to do:
  2952. *
  2953. * - If nothing, we just sent a M_ABORT message to the
  2954. * target to get rid of the useless SCSI bus ownership.
  2955. * According to the specs, no tasks shall be affected.
  2956. * - If the target is to be reset, we send it a M_RESET
  2957. * message.
  2958. * - If a logical UNIT is to be cleared , we send the
  2959. * IDENTIFY(lun) + M_ABORT.
  2960. * - If an untagged task is to be aborted, we send the
  2961. * IDENTIFY(lun) + M_ABORT.
  2962. * - If a tagged task is to be aborted, we send the
  2963. * IDENTIFY(lun) + task attributes + M_ABORT_TAG.
  2964. *
  2965. * Once our 'kiss of death' :) message has been accepted
  2966. * by the target, the SCRIPTS interrupts again
  2967. * (SIR_ABORT_SENT). On this interrupt, we complete
  2968. * all the CCBs that should have been aborted by the
  2969. * target according to our message.
  2970. */
  2971. static void sym_sir_task_recovery(struct sym_hcb *np, int num)
  2972. {
  2973. SYM_QUEHEAD *qp;
  2974. struct sym_ccb *cp;
  2975. struct sym_tcb *tp = NULL; /* gcc isn't quite smart enough yet */
  2976. struct scsi_target *starget;
  2977. int target=-1, lun=-1, task;
  2978. int i, k;
  2979. switch(num) {
  2980. /*
  2981. * The SCRIPTS processor stopped before starting
  2982. * the next command in order to allow us to perform
  2983. * some task recovery.
  2984. */
  2985. case SIR_SCRIPT_STOPPED:
  2986. /*
  2987. * Do we have any target to reset or unit to clear ?
  2988. */
  2989. for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
  2990. tp = &np->target[i];
  2991. if (tp->to_reset ||
  2992. (tp->lun0p && tp->lun0p->to_clear)) {
  2993. target = i;
  2994. break;
  2995. }
  2996. if (!tp->lunmp)
  2997. continue;
  2998. for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
  2999. if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
  3000. target = i;
  3001. break;
  3002. }
  3003. }
  3004. if (target != -1)
  3005. break;
  3006. }
  3007. /*
  3008. * If not, walk the busy queue for any
  3009. * disconnected CCB to be aborted.
  3010. */
  3011. if (target == -1) {
  3012. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  3013. cp = sym_que_entry(qp,struct sym_ccb,link_ccbq);
  3014. if (cp->host_status != HS_DISCONNECT)
  3015. continue;
  3016. if (cp->to_abort) {
  3017. target = cp->target;
  3018. break;
  3019. }
  3020. }
  3021. }
  3022. /*
  3023. * If some target is to be selected,
  3024. * prepare and start the selection.
  3025. */
  3026. if (target != -1) {
  3027. tp = &np->target[target];
  3028. np->abrt_sel.sel_id = target;
  3029. np->abrt_sel.sel_scntl3 = tp->head.wval;
  3030. np->abrt_sel.sel_sxfer = tp->head.sval;
  3031. OUTL(np, nc_dsa, np->hcb_ba);
  3032. OUTL_DSP(np, SCRIPTB_BA(np, sel_for_abort));
  3033. return;
  3034. }
  3035. /*
  3036. * Now look for a CCB to abort that haven't started yet.
  3037. * Btw, the SCRIPTS processor is still stopped, so
  3038. * we are not in race.
  3039. */
  3040. i = 0;
  3041. cp = NULL;
  3042. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  3043. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  3044. if (cp->host_status != HS_BUSY &&
  3045. cp->host_status != HS_NEGOTIATE)
  3046. continue;
  3047. if (!cp->to_abort)
  3048. continue;
  3049. #ifdef SYM_CONF_IARB_SUPPORT
  3050. /*
  3051. * If we are using IMMEDIATE ARBITRATION, we donnot
  3052. * want to cancel the last queued CCB, since the
  3053. * SCRIPTS may have anticipated the selection.
  3054. */
  3055. if (cp == np->last_cp) {
  3056. cp->to_abort = 0;
  3057. continue;
  3058. }
  3059. #endif
  3060. i = 1; /* Means we have found some */
  3061. break;
  3062. }
  3063. if (!i) {
  3064. /*
  3065. * We are done, so we donnot need
  3066. * to synchronize with the SCRIPTS anylonger.
  3067. * Remove the SEM flag from the ISTAT.
  3068. */
  3069. np->istat_sem = 0;
  3070. OUTB(np, nc_istat, SIGP);
  3071. break;
  3072. }
  3073. /*
  3074. * Compute index of next position in the start
  3075. * queue the SCRIPTS intends to start and dequeue
  3076. * all CCBs for that device that haven't been started.
  3077. */
  3078. i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
  3079. i = sym_dequeue_from_squeue(np, i, cp->target, cp->lun, -1);
  3080. /*
  3081. * Make sure at least our IO to abort has been dequeued.
  3082. */
  3083. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  3084. assert(i && sym_get_cam_status(cp->cmd) == DID_SOFT_ERROR);
  3085. #else
  3086. sym_remque(&cp->link_ccbq);
  3087. sym_insque_tail(&cp->link_ccbq, &np->comp_ccbq);
  3088. #endif
  3089. /*
  3090. * Keep track in cam status of the reason of the abort.
  3091. */
  3092. if (cp->to_abort == 2)
  3093. sym_set_cam_status(cp->cmd, DID_TIME_OUT);
  3094. else
  3095. sym_set_cam_status(cp->cmd, DID_ABORT);
  3096. /*
  3097. * Complete with error everything that we have dequeued.
  3098. */
  3099. sym_flush_comp_queue(np, 0);
  3100. break;
  3101. /*
  3102. * The SCRIPTS processor has selected a target
  3103. * we may have some manual recovery to perform for.
  3104. */
  3105. case SIR_TARGET_SELECTED:
  3106. target = INB(np, nc_sdid) & 0xf;
  3107. tp = &np->target[target];
  3108. np->abrt_tbl.addr = cpu_to_scr(vtobus(np->abrt_msg));
  3109. /*
  3110. * If the target is to be reset, prepare a
  3111. * M_RESET message and clear the to_reset flag
  3112. * since we donnot expect this operation to fail.
  3113. */
  3114. if (tp->to_reset) {
  3115. np->abrt_msg[0] = M_RESET;
  3116. np->abrt_tbl.size = 1;
  3117. tp->to_reset = 0;
  3118. break;
  3119. }
  3120. /*
  3121. * Otherwise, look for some logical unit to be cleared.
  3122. */
  3123. if (tp->lun0p && tp->lun0p->to_clear)
  3124. lun = 0;
  3125. else if (tp->lunmp) {
  3126. for (k = 1 ; k < SYM_CONF_MAX_LUN ; k++) {
  3127. if (tp->lunmp[k] && tp->lunmp[k]->to_clear) {
  3128. lun = k;
  3129. break;
  3130. }
  3131. }
  3132. }
  3133. /*
  3134. * If a logical unit is to be cleared, prepare
  3135. * an IDENTIFY(lun) + ABORT MESSAGE.
  3136. */
  3137. if (lun != -1) {
  3138. struct sym_lcb *lp = sym_lp(tp, lun);
  3139. lp->to_clear = 0; /* We don't expect to fail here */
  3140. np->abrt_msg[0] = IDENTIFY(0, lun);
  3141. np->abrt_msg[1] = M_ABORT;
  3142. np->abrt_tbl.size = 2;
  3143. break;
  3144. }
  3145. /*
  3146. * Otherwise, look for some disconnected job to
  3147. * abort for this target.
  3148. */
  3149. i = 0;
  3150. cp = NULL;
  3151. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  3152. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  3153. if (cp->host_status != HS_DISCONNECT)
  3154. continue;
  3155. if (cp->target != target)
  3156. continue;
  3157. if (!cp->to_abort)
  3158. continue;
  3159. i = 1; /* Means we have some */
  3160. break;
  3161. }
  3162. /*
  3163. * If we have none, probably since the device has
  3164. * completed the command before we won abitration,
  3165. * send a M_ABORT message without IDENTIFY.
  3166. * According to the specs, the device must just
  3167. * disconnect the BUS and not abort any task.
  3168. */
  3169. if (!i) {
  3170. np->abrt_msg[0] = M_ABORT;
  3171. np->abrt_tbl.size = 1;
  3172. break;
  3173. }
  3174. /*
  3175. * We have some task to abort.
  3176. * Set the IDENTIFY(lun)
  3177. */
  3178. np->abrt_msg[0] = IDENTIFY(0, cp->lun);
  3179. /*
  3180. * If we want to abort an untagged command, we
  3181. * will send a IDENTIFY + M_ABORT.
  3182. * Otherwise (tagged command), we will send
  3183. * a IDENTITFY + task attributes + ABORT TAG.
  3184. */
  3185. if (cp->tag == NO_TAG) {
  3186. np->abrt_msg[1] = M_ABORT;
  3187. np->abrt_tbl.size = 2;
  3188. } else {
  3189. np->abrt_msg[1] = cp->scsi_smsg[1];
  3190. np->abrt_msg[2] = cp->scsi_smsg[2];
  3191. np->abrt_msg[3] = M_ABORT_TAG;
  3192. np->abrt_tbl.size = 4;
  3193. }
  3194. /*
  3195. * Keep track of software timeout condition, since the
  3196. * peripheral driver may not count retries on abort
  3197. * conditions not due to timeout.
  3198. */
  3199. if (cp->to_abort == 2)
  3200. sym_set_cam_status(cp->cmd, DID_TIME_OUT);
  3201. cp->to_abort = 0; /* We donnot expect to fail here */
  3202. break;
  3203. /*
  3204. * The target has accepted our message and switched
  3205. * to BUS FREE phase as we expected.
  3206. */
  3207. case SIR_ABORT_SENT:
  3208. target = INB(np, nc_sdid) & 0xf;
  3209. tp = &np->target[target];
  3210. starget = tp->starget;
  3211. /*
  3212. ** If we didn't abort anything, leave here.
  3213. */
  3214. if (np->abrt_msg[0] == M_ABORT)
  3215. break;
  3216. /*
  3217. * If we sent a M_RESET, then a hardware reset has
  3218. * been performed by the target.
  3219. * - Reset everything to async 8 bit
  3220. * - Tell ourself to negotiate next time :-)
  3221. * - Prepare to clear all disconnected CCBs for
  3222. * this target from our task list (lun=task=-1)
  3223. */
  3224. lun = -1;
  3225. task = -1;
  3226. if (np->abrt_msg[0] == M_RESET) {
  3227. tp->head.sval = 0;
  3228. tp->head.wval = np->rv_scntl3;
  3229. tp->head.uval = 0;
  3230. spi_period(starget) = 0;
  3231. spi_offset(starget) = 0;
  3232. spi_width(starget) = 0;
  3233. spi_iu(starget) = 0;
  3234. spi_dt(starget) = 0;
  3235. spi_qas(starget) = 0;
  3236. tp->tgoal.check_nego = 1;
  3237. tp->tgoal.renego = 0;
  3238. }
  3239. /*
  3240. * Otherwise, check for the LUN and TASK(s)
  3241. * concerned by the cancelation.
  3242. * If it is not ABORT_TAG then it is CLEAR_QUEUE
  3243. * or an ABORT message :-)
  3244. */
  3245. else {
  3246. lun = np->abrt_msg[0] & 0x3f;
  3247. if (np->abrt_msg[1] == M_ABORT_TAG)
  3248. task = np->abrt_msg[2];
  3249. }
  3250. /*
  3251. * Complete all the CCBs the device should have
  3252. * aborted due to our 'kiss of death' message.
  3253. */
  3254. i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
  3255. sym_dequeue_from_squeue(np, i, target, lun, -1);
  3256. sym_clear_tasks(np, DID_ABORT, target, lun, task);
  3257. sym_flush_comp_queue(np, 0);
  3258. /*
  3259. * If we sent a BDR, make upper layer aware of that.
  3260. */
  3261. if (np->abrt_msg[0] == M_RESET)
  3262. starget_printk(KERN_NOTICE, starget,
  3263. "has been reset\n");
  3264. break;
  3265. }
  3266. /*
  3267. * Print to the log the message we intend to send.
  3268. */
  3269. if (num == SIR_TARGET_SELECTED) {
  3270. dev_info(&tp->starget->dev, "control msgout:");
  3271. sym_printl_hex(np->abrt_msg, np->abrt_tbl.size);
  3272. np->abrt_tbl.size = cpu_to_scr(np->abrt_tbl.size);
  3273. }
  3274. /*
  3275. * Let the SCRIPTS processor continue.
  3276. */
  3277. OUTONB_STD();
  3278. }
  3279. /*
  3280. * Gerard's alchemy:) that deals with with the data
  3281. * pointer for both MDP and the residual calculation.
  3282. *
  3283. * I didn't want to bloat the code by more than 200
  3284. * lines for the handling of both MDP and the residual.
  3285. * This has been achieved by using a data pointer
  3286. * representation consisting in an index in the data
  3287. * array (dp_sg) and a negative offset (dp_ofs) that
  3288. * have the following meaning:
  3289. *
  3290. * - dp_sg = SYM_CONF_MAX_SG
  3291. * we are at the end of the data script.
  3292. * - dp_sg < SYM_CONF_MAX_SG
  3293. * dp_sg points to the next entry of the scatter array
  3294. * we want to transfer.
  3295. * - dp_ofs < 0
  3296. * dp_ofs represents the residual of bytes of the
  3297. * previous entry scatter entry we will send first.
  3298. * - dp_ofs = 0
  3299. * no residual to send first.
  3300. *
  3301. * The function sym_evaluate_dp() accepts an arbitray
  3302. * offset (basically from the MDP message) and returns
  3303. * the corresponding values of dp_sg and dp_ofs.
  3304. */
  3305. static int sym_evaluate_dp(struct sym_hcb *np, struct sym_ccb *cp, u32 scr, int *ofs)
  3306. {
  3307. u32 dp_scr;
  3308. int dp_ofs, dp_sg, dp_sgmin;
  3309. int tmp;
  3310. struct sym_pmc *pm;
  3311. /*
  3312. * Compute the resulted data pointer in term of a script
  3313. * address within some DATA script and a signed byte offset.
  3314. */
  3315. dp_scr = scr;
  3316. dp_ofs = *ofs;
  3317. if (dp_scr == SCRIPTA_BA(np, pm0_data))
  3318. pm = &cp->phys.pm0;
  3319. else if (dp_scr == SCRIPTA_BA(np, pm1_data))
  3320. pm = &cp->phys.pm1;
  3321. else
  3322. pm = NULL;
  3323. if (pm) {
  3324. dp_scr = scr_to_cpu(pm->ret);
  3325. dp_ofs -= scr_to_cpu(pm->sg.size) & 0x00ffffff;
  3326. }
  3327. /*
  3328. * If we are auto-sensing, then we are done.
  3329. */
  3330. if (cp->host_flags & HF_SENSE) {
  3331. *ofs = dp_ofs;
  3332. return 0;
  3333. }
  3334. /*
  3335. * Deduce the index of the sg entry.
  3336. * Keep track of the index of the first valid entry.
  3337. * If result is dp_sg = SYM_CONF_MAX_SG, then we are at the
  3338. * end of the data.
  3339. */
  3340. tmp = scr_to_cpu(cp->goalp);
  3341. dp_sg = SYM_CONF_MAX_SG;
  3342. if (dp_scr != tmp)
  3343. dp_sg -= (tmp - 8 - (int)dp_scr) / (2*4);
  3344. dp_sgmin = SYM_CONF_MAX_SG - cp->segments;
  3345. /*
  3346. * Move to the sg entry the data pointer belongs to.
  3347. *
  3348. * If we are inside the data area, we expect result to be:
  3349. *
  3350. * Either,
  3351. * dp_ofs = 0 and dp_sg is the index of the sg entry
  3352. * the data pointer belongs to (or the end of the data)
  3353. * Or,
  3354. * dp_ofs < 0 and dp_sg is the index of the sg entry
  3355. * the data pointer belongs to + 1.
  3356. */
  3357. if (dp_ofs < 0) {
  3358. int n;
  3359. while (dp_sg > dp_sgmin) {
  3360. --dp_sg;
  3361. tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
  3362. n = dp_ofs + (tmp & 0xffffff);
  3363. if (n > 0) {
  3364. ++dp_sg;
  3365. break;
  3366. }
  3367. dp_ofs = n;
  3368. }
  3369. }
  3370. else if (dp_ofs > 0) {
  3371. while (dp_sg < SYM_CONF_MAX_SG) {
  3372. tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
  3373. dp_ofs -= (tmp & 0xffffff);
  3374. ++dp_sg;
  3375. if (dp_ofs <= 0)
  3376. break;
  3377. }
  3378. }
  3379. /*
  3380. * Make sure the data pointer is inside the data area.
  3381. * If not, return some error.
  3382. */
  3383. if (dp_sg < dp_sgmin || (dp_sg == dp_sgmin && dp_ofs < 0))
  3384. goto out_err;
  3385. else if (dp_sg > SYM_CONF_MAX_SG ||
  3386. (dp_sg == SYM_CONF_MAX_SG && dp_ofs > 0))
  3387. goto out_err;
  3388. /*
  3389. * Save the extreme pointer if needed.
  3390. */
  3391. if (dp_sg > cp->ext_sg ||
  3392. (dp_sg == cp->ext_sg && dp_ofs > cp->ext_ofs)) {
  3393. cp->ext_sg = dp_sg;
  3394. cp->ext_ofs = dp_ofs;
  3395. }
  3396. /*
  3397. * Return data.
  3398. */
  3399. *ofs = dp_ofs;
  3400. return dp_sg;
  3401. out_err:
  3402. return -1;
  3403. }
  3404. /*
  3405. * chip handler for MODIFY DATA POINTER MESSAGE
  3406. *
  3407. * We also call this function on IGNORE WIDE RESIDUE
  3408. * messages that do not match a SWIDE full condition.
  3409. * Btw, we assume in that situation that such a message
  3410. * is equivalent to a MODIFY DATA POINTER (offset=-1).
  3411. */
  3412. static void sym_modify_dp(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp, int ofs)
  3413. {
  3414. int dp_ofs = ofs;
  3415. u32 dp_scr = sym_get_script_dp (np, cp);
  3416. u32 dp_ret;
  3417. u32 tmp;
  3418. u_char hflags;
  3419. int dp_sg;
  3420. struct sym_pmc *pm;
  3421. /*
  3422. * Not supported for auto-sense.
  3423. */
  3424. if (cp->host_flags & HF_SENSE)
  3425. goto out_reject;
  3426. /*
  3427. * Apply our alchemy:) (see comments in sym_evaluate_dp()),
  3428. * to the resulted data pointer.
  3429. */
  3430. dp_sg = sym_evaluate_dp(np, cp, dp_scr, &dp_ofs);
  3431. if (dp_sg < 0)
  3432. goto out_reject;
  3433. /*
  3434. * And our alchemy:) allows to easily calculate the data
  3435. * script address we want to return for the next data phase.
  3436. */
  3437. dp_ret = cpu_to_scr(cp->goalp);
  3438. dp_ret = dp_ret - 8 - (SYM_CONF_MAX_SG - dp_sg) * (2*4);
  3439. /*
  3440. * If offset / scatter entry is zero we donnot need
  3441. * a context for the new current data pointer.
  3442. */
  3443. if (dp_ofs == 0) {
  3444. dp_scr = dp_ret;
  3445. goto out_ok;
  3446. }
  3447. /*
  3448. * Get a context for the new current data pointer.
  3449. */
  3450. hflags = INB(np, HF_PRT);
  3451. if (hflags & HF_DP_SAVED)
  3452. hflags ^= HF_ACT_PM;
  3453. if (!(hflags & HF_ACT_PM)) {
  3454. pm = &cp->phys.pm0;
  3455. dp_scr = SCRIPTA_BA(np, pm0_data);
  3456. }
  3457. else {
  3458. pm = &cp->phys.pm1;
  3459. dp_scr = SCRIPTA_BA(np, pm1_data);
  3460. }
  3461. hflags &= ~(HF_DP_SAVED);
  3462. OUTB(np, HF_PRT, hflags);
  3463. /*
  3464. * Set up the new current data pointer.
  3465. * ofs < 0 there, and for the next data phase, we
  3466. * want to transfer part of the data of the sg entry
  3467. * corresponding to index dp_sg-1 prior to returning
  3468. * to the main data script.
  3469. */
  3470. pm->ret = cpu_to_scr(dp_ret);
  3471. tmp = scr_to_cpu(cp->phys.data[dp_sg-1].addr);
  3472. tmp += scr_to_cpu(cp->phys.data[dp_sg-1].size) + dp_ofs;
  3473. pm->sg.addr = cpu_to_scr(tmp);
  3474. pm->sg.size = cpu_to_scr(-dp_ofs);
  3475. out_ok:
  3476. sym_set_script_dp (np, cp, dp_scr);
  3477. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  3478. return;
  3479. out_reject:
  3480. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  3481. }
  3482. /*
  3483. * chip calculation of the data residual.
  3484. *
  3485. * As I used to say, the requirement of data residual
  3486. * in SCSI is broken, useless and cannot be achieved
  3487. * without huge complexity.
  3488. * But most OSes and even the official CAM require it.
  3489. * When stupidity happens to be so widely spread inside
  3490. * a community, it gets hard to convince.
  3491. *
  3492. * Anyway, I don't care, since I am not going to use
  3493. * any software that considers this data residual as
  3494. * a relevant information. :)
  3495. */
  3496. int sym_compute_residual(struct sym_hcb *np, struct sym_ccb *cp)
  3497. {
  3498. int dp_sg, dp_sgmin, resid = 0;
  3499. int dp_ofs = 0;
  3500. /*
  3501. * Check for some data lost or just thrown away.
  3502. * We are not required to be quite accurate in this
  3503. * situation. Btw, if we are odd for output and the
  3504. * device claims some more data, it may well happen
  3505. * than our residual be zero. :-)
  3506. */
  3507. if (cp->xerr_status & (XE_EXTRA_DATA|XE_SODL_UNRUN|XE_SWIDE_OVRUN)) {
  3508. if (cp->xerr_status & XE_EXTRA_DATA)
  3509. resid -= cp->extra_bytes;
  3510. if (cp->xerr_status & XE_SODL_UNRUN)
  3511. ++resid;
  3512. if (cp->xerr_status & XE_SWIDE_OVRUN)
  3513. --resid;
  3514. }
  3515. /*
  3516. * If all data has been transferred,
  3517. * there is no residual.
  3518. */
  3519. if (cp->phys.head.lastp == cp->goalp)
  3520. return resid;
  3521. /*
  3522. * If no data transfer occurs, or if the data
  3523. * pointer is weird, return full residual.
  3524. */
  3525. if (cp->startp == cp->phys.head.lastp ||
  3526. sym_evaluate_dp(np, cp, scr_to_cpu(cp->phys.head.lastp),
  3527. &dp_ofs) < 0) {
  3528. return cp->data_len - cp->odd_byte_adjustment;
  3529. }
  3530. /*
  3531. * If we were auto-sensing, then we are done.
  3532. */
  3533. if (cp->host_flags & HF_SENSE) {
  3534. return -dp_ofs;
  3535. }
  3536. /*
  3537. * We are now full comfortable in the computation
  3538. * of the data residual (2's complement).
  3539. */
  3540. dp_sgmin = SYM_CONF_MAX_SG - cp->segments;
  3541. resid = -cp->ext_ofs;
  3542. for (dp_sg = cp->ext_sg; dp_sg < SYM_CONF_MAX_SG; ++dp_sg) {
  3543. u_int tmp = scr_to_cpu(cp->phys.data[dp_sg].size);
  3544. resid += (tmp & 0xffffff);
  3545. }
  3546. resid -= cp->odd_byte_adjustment;
  3547. /*
  3548. * Hopefully, the result is not too wrong.
  3549. */
  3550. return resid;
  3551. }
  3552. /*
  3553. * Negotiation for WIDE and SYNCHRONOUS DATA TRANSFER.
  3554. *
  3555. * When we try to negotiate, we append the negotiation message
  3556. * to the identify and (maybe) simple tag message.
  3557. * The host status field is set to HS_NEGOTIATE to mark this
  3558. * situation.
  3559. *
  3560. * If the target doesn't answer this message immediately
  3561. * (as required by the standard), the SIR_NEGO_FAILED interrupt
  3562. * will be raised eventually.
  3563. * The handler removes the HS_NEGOTIATE status, and sets the
  3564. * negotiated value to the default (async / nowide).
  3565. *
  3566. * If we receive a matching answer immediately, we check it
  3567. * for validity, and set the values.
  3568. *
  3569. * If we receive a Reject message immediately, we assume the
  3570. * negotiation has failed, and fall back to standard values.
  3571. *
  3572. * If we receive a negotiation message while not in HS_NEGOTIATE
  3573. * state, it's a target initiated negotiation. We prepare a
  3574. * (hopefully) valid answer, set our parameters, and send back
  3575. * this answer to the target.
  3576. *
  3577. * If the target doesn't fetch the answer (no message out phase),
  3578. * we assume the negotiation has failed, and fall back to default
  3579. * settings (SIR_NEGO_PROTO interrupt).
  3580. *
  3581. * When we set the values, we adjust them in all ccbs belonging
  3582. * to this target, in the controller's register, and in the "phys"
  3583. * field of the controller's struct sym_hcb.
  3584. */
  3585. /*
  3586. * chip handler for SYNCHRONOUS DATA TRANSFER REQUEST (SDTR) message.
  3587. */
  3588. static int
  3589. sym_sync_nego_check(struct sym_hcb *np, int req, struct sym_ccb *cp)
  3590. {
  3591. int target = cp->target;
  3592. u_char chg, ofs, per, fak, div;
  3593. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3594. sym_print_nego_msg(np, target, "sync msgin", np->msgin);
  3595. }
  3596. /*
  3597. * Get requested values.
  3598. */
  3599. chg = 0;
  3600. per = np->msgin[3];
  3601. ofs = np->msgin[4];
  3602. /*
  3603. * Check values against our limits.
  3604. */
  3605. if (ofs) {
  3606. if (ofs > np->maxoffs)
  3607. {chg = 1; ofs = np->maxoffs;}
  3608. }
  3609. if (ofs) {
  3610. if (per < np->minsync)
  3611. {chg = 1; per = np->minsync;}
  3612. }
  3613. /*
  3614. * Get new chip synchronous parameters value.
  3615. */
  3616. div = fak = 0;
  3617. if (ofs && sym_getsync(np, 0, per, &div, &fak) < 0)
  3618. goto reject_it;
  3619. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3620. sym_print_addr(cp->cmd,
  3621. "sdtr: ofs=%d per=%d div=%d fak=%d chg=%d.\n",
  3622. ofs, per, div, fak, chg);
  3623. }
  3624. /*
  3625. * If it was an answer we want to change,
  3626. * then it isn't acceptable. Reject it.
  3627. */
  3628. if (!req && chg)
  3629. goto reject_it;
  3630. /*
  3631. * Apply new values.
  3632. */
  3633. sym_setsync (np, target, ofs, per, div, fak);
  3634. /*
  3635. * It was an answer. We are done.
  3636. */
  3637. if (!req)
  3638. return 0;
  3639. /*
  3640. * It was a request. Prepare an answer message.
  3641. */
  3642. spi_populate_sync_msg(np->msgout, per, ofs);
  3643. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3644. sym_print_nego_msg(np, target, "sync msgout", np->msgout);
  3645. }
  3646. np->msgin [0] = M_NOOP;
  3647. return 0;
  3648. reject_it:
  3649. sym_setsync (np, target, 0, 0, 0, 0);
  3650. return -1;
  3651. }
  3652. static void sym_sync_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3653. {
  3654. int req = 1;
  3655. int result;
  3656. /*
  3657. * Request or answer ?
  3658. */
  3659. if (INB(np, HS_PRT) == HS_NEGOTIATE) {
  3660. OUTB(np, HS_PRT, HS_BUSY);
  3661. if (cp->nego_status && cp->nego_status != NS_SYNC)
  3662. goto reject_it;
  3663. req = 0;
  3664. }
  3665. /*
  3666. * Check and apply new values.
  3667. */
  3668. result = sym_sync_nego_check(np, req, cp);
  3669. if (result) /* Not acceptable, reject it */
  3670. goto reject_it;
  3671. if (req) { /* Was a request, send response. */
  3672. cp->nego_status = NS_SYNC;
  3673. OUTL_DSP(np, SCRIPTB_BA(np, sdtr_resp));
  3674. }
  3675. else /* Was a response, we are done. */
  3676. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  3677. return;
  3678. reject_it:
  3679. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  3680. }
  3681. /*
  3682. * chip handler for PARALLEL PROTOCOL REQUEST (PPR) message.
  3683. */
  3684. static int
  3685. sym_ppr_nego_check(struct sym_hcb *np, int req, int target)
  3686. {
  3687. struct sym_tcb *tp = &np->target[target];
  3688. unsigned char fak, div;
  3689. int dt, chg = 0;
  3690. unsigned char per = np->msgin[3];
  3691. unsigned char ofs = np->msgin[5];
  3692. unsigned char wide = np->msgin[6];
  3693. unsigned char opts = np->msgin[7] & PPR_OPT_MASK;
  3694. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3695. sym_print_nego_msg(np, target, "ppr msgin", np->msgin);
  3696. }
  3697. /*
  3698. * Check values against our limits.
  3699. */
  3700. if (wide > np->maxwide) {
  3701. chg = 1;
  3702. wide = np->maxwide;
  3703. }
  3704. if (!wide || !(np->features & FE_U3EN))
  3705. opts = 0;
  3706. if (opts != (np->msgin[7] & PPR_OPT_MASK))
  3707. chg = 1;
  3708. dt = opts & PPR_OPT_DT;
  3709. if (ofs) {
  3710. unsigned char maxoffs = dt ? np->maxoffs_dt : np->maxoffs;
  3711. if (ofs > maxoffs) {
  3712. chg = 1;
  3713. ofs = maxoffs;
  3714. }
  3715. }
  3716. if (ofs) {
  3717. unsigned char minsync = dt ? np->minsync_dt : np->minsync;
  3718. if (per < minsync) {
  3719. chg = 1;
  3720. per = minsync;
  3721. }
  3722. }
  3723. /*
  3724. * Get new chip synchronous parameters value.
  3725. */
  3726. div = fak = 0;
  3727. if (ofs && sym_getsync(np, dt, per, &div, &fak) < 0)
  3728. goto reject_it;
  3729. /*
  3730. * If it was an answer we want to change,
  3731. * then it isn't acceptable. Reject it.
  3732. */
  3733. if (!req && chg)
  3734. goto reject_it;
  3735. /*
  3736. * Apply new values.
  3737. */
  3738. sym_setpprot(np, target, opts, ofs, per, wide, div, fak);
  3739. /*
  3740. * It was an answer. We are done.
  3741. */
  3742. if (!req)
  3743. return 0;
  3744. /*
  3745. * It was a request. Prepare an answer message.
  3746. */
  3747. spi_populate_ppr_msg(np->msgout, per, ofs, wide, opts);
  3748. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3749. sym_print_nego_msg(np, target, "ppr msgout", np->msgout);
  3750. }
  3751. np->msgin [0] = M_NOOP;
  3752. return 0;
  3753. reject_it:
  3754. sym_setpprot (np, target, 0, 0, 0, 0, 0, 0);
  3755. /*
  3756. * If it is a device response that should result in
  3757. * ST, we may want to try a legacy negotiation later.
  3758. */
  3759. if (!req && !opts) {
  3760. tp->tgoal.period = per;
  3761. tp->tgoal.offset = ofs;
  3762. tp->tgoal.width = wide;
  3763. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  3764. tp->tgoal.check_nego = 1;
  3765. }
  3766. return -1;
  3767. }
  3768. static void sym_ppr_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3769. {
  3770. int req = 1;
  3771. int result;
  3772. /*
  3773. * Request or answer ?
  3774. */
  3775. if (INB(np, HS_PRT) == HS_NEGOTIATE) {
  3776. OUTB(np, HS_PRT, HS_BUSY);
  3777. if (cp->nego_status && cp->nego_status != NS_PPR)
  3778. goto reject_it;
  3779. req = 0;
  3780. }
  3781. /*
  3782. * Check and apply new values.
  3783. */
  3784. result = sym_ppr_nego_check(np, req, cp->target);
  3785. if (result) /* Not acceptable, reject it */
  3786. goto reject_it;
  3787. if (req) { /* Was a request, send response. */
  3788. cp->nego_status = NS_PPR;
  3789. OUTL_DSP(np, SCRIPTB_BA(np, ppr_resp));
  3790. }
  3791. else /* Was a response, we are done. */
  3792. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  3793. return;
  3794. reject_it:
  3795. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  3796. }
  3797. /*
  3798. * chip handler for WIDE DATA TRANSFER REQUEST (WDTR) message.
  3799. */
  3800. static int
  3801. sym_wide_nego_check(struct sym_hcb *np, int req, struct sym_ccb *cp)
  3802. {
  3803. int target = cp->target;
  3804. u_char chg, wide;
  3805. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3806. sym_print_nego_msg(np, target, "wide msgin", np->msgin);
  3807. }
  3808. /*
  3809. * Get requested values.
  3810. */
  3811. chg = 0;
  3812. wide = np->msgin[3];
  3813. /*
  3814. * Check values against our limits.
  3815. */
  3816. if (wide > np->maxwide) {
  3817. chg = 1;
  3818. wide = np->maxwide;
  3819. }
  3820. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3821. sym_print_addr(cp->cmd, "wdtr: wide=%d chg=%d.\n",
  3822. wide, chg);
  3823. }
  3824. /*
  3825. * If it was an answer we want to change,
  3826. * then it isn't acceptable. Reject it.
  3827. */
  3828. if (!req && chg)
  3829. goto reject_it;
  3830. /*
  3831. * Apply new values.
  3832. */
  3833. sym_setwide (np, target, wide);
  3834. /*
  3835. * It was an answer. We are done.
  3836. */
  3837. if (!req)
  3838. return 0;
  3839. /*
  3840. * It was a request. Prepare an answer message.
  3841. */
  3842. spi_populate_width_msg(np->msgout, wide);
  3843. np->msgin [0] = M_NOOP;
  3844. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3845. sym_print_nego_msg(np, target, "wide msgout", np->msgout);
  3846. }
  3847. return 0;
  3848. reject_it:
  3849. return -1;
  3850. }
  3851. static void sym_wide_nego(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3852. {
  3853. int req = 1;
  3854. int result;
  3855. /*
  3856. * Request or answer ?
  3857. */
  3858. if (INB(np, HS_PRT) == HS_NEGOTIATE) {
  3859. OUTB(np, HS_PRT, HS_BUSY);
  3860. if (cp->nego_status && cp->nego_status != NS_WIDE)
  3861. goto reject_it;
  3862. req = 0;
  3863. }
  3864. /*
  3865. * Check and apply new values.
  3866. */
  3867. result = sym_wide_nego_check(np, req, cp);
  3868. if (result) /* Not acceptable, reject it */
  3869. goto reject_it;
  3870. if (req) { /* Was a request, send response. */
  3871. cp->nego_status = NS_WIDE;
  3872. OUTL_DSP(np, SCRIPTB_BA(np, wdtr_resp));
  3873. } else { /* Was a response. */
  3874. /*
  3875. * Negotiate for SYNC immediately after WIDE response.
  3876. * This allows to negotiate for both WIDE and SYNC on
  3877. * a single SCSI command (Suggested by Justin Gibbs).
  3878. */
  3879. if (tp->tgoal.offset) {
  3880. spi_populate_sync_msg(np->msgout, tp->tgoal.period,
  3881. tp->tgoal.offset);
  3882. if (DEBUG_FLAGS & DEBUG_NEGO) {
  3883. sym_print_nego_msg(np, cp->target,
  3884. "sync msgout", np->msgout);
  3885. }
  3886. cp->nego_status = NS_SYNC;
  3887. OUTB(np, HS_PRT, HS_NEGOTIATE);
  3888. OUTL_DSP(np, SCRIPTB_BA(np, sdtr_resp));
  3889. return;
  3890. } else
  3891. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  3892. }
  3893. return;
  3894. reject_it:
  3895. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  3896. }
  3897. /*
  3898. * Reset DT, SYNC or WIDE to default settings.
  3899. *
  3900. * Called when a negotiation does not succeed either
  3901. * on rejection or on protocol error.
  3902. *
  3903. * A target that understands a PPR message should never
  3904. * reject it, and messing with it is very unlikely.
  3905. * So, if a PPR makes problems, we may just want to
  3906. * try a legacy negotiation later.
  3907. */
  3908. static void sym_nego_default(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3909. {
  3910. switch (cp->nego_status) {
  3911. case NS_PPR:
  3912. #if 0
  3913. sym_setpprot (np, cp->target, 0, 0, 0, 0, 0, 0);
  3914. #else
  3915. if (tp->tgoal.period < np->minsync)
  3916. tp->tgoal.period = np->minsync;
  3917. if (tp->tgoal.offset > np->maxoffs)
  3918. tp->tgoal.offset = np->maxoffs;
  3919. tp->tgoal.iu = tp->tgoal.dt = tp->tgoal.qas = 0;
  3920. tp->tgoal.check_nego = 1;
  3921. #endif
  3922. break;
  3923. case NS_SYNC:
  3924. sym_setsync (np, cp->target, 0, 0, 0, 0);
  3925. break;
  3926. case NS_WIDE:
  3927. sym_setwide (np, cp->target, 0);
  3928. break;
  3929. }
  3930. np->msgin [0] = M_NOOP;
  3931. np->msgout[0] = M_NOOP;
  3932. cp->nego_status = 0;
  3933. }
  3934. /*
  3935. * chip handler for MESSAGE REJECT received in response to
  3936. * PPR, WIDE or SYNCHRONOUS negotiation.
  3937. */
  3938. static void sym_nego_rejected(struct sym_hcb *np, struct sym_tcb *tp, struct sym_ccb *cp)
  3939. {
  3940. sym_nego_default(np, tp, cp);
  3941. OUTB(np, HS_PRT, HS_BUSY);
  3942. }
  3943. /*
  3944. * chip exception handler for programmed interrupts.
  3945. */
  3946. static void sym_int_sir(struct sym_hcb *np)
  3947. {
  3948. u_char num = INB(np, nc_dsps);
  3949. u32 dsa = INL(np, nc_dsa);
  3950. struct sym_ccb *cp = sym_ccb_from_dsa(np, dsa);
  3951. u_char target = INB(np, nc_sdid) & 0x0f;
  3952. struct sym_tcb *tp = &np->target[target];
  3953. int tmp;
  3954. if (DEBUG_FLAGS & DEBUG_TINY) printf ("I#%d", num);
  3955. switch (num) {
  3956. #if SYM_CONF_DMA_ADDRESSING_MODE == 2
  3957. /*
  3958. * SCRIPTS tell us that we may have to update
  3959. * 64 bit DMA segment registers.
  3960. */
  3961. case SIR_DMAP_DIRTY:
  3962. sym_update_dmap_regs(np);
  3963. goto out;
  3964. #endif
  3965. /*
  3966. * Command has been completed with error condition
  3967. * or has been auto-sensed.
  3968. */
  3969. case SIR_COMPLETE_ERROR:
  3970. sym_complete_error(np, cp);
  3971. return;
  3972. /*
  3973. * The C code is currently trying to recover from something.
  3974. * Typically, user want to abort some command.
  3975. */
  3976. case SIR_SCRIPT_STOPPED:
  3977. case SIR_TARGET_SELECTED:
  3978. case SIR_ABORT_SENT:
  3979. sym_sir_task_recovery(np, num);
  3980. return;
  3981. /*
  3982. * The device didn't go to MSG OUT phase after having
  3983. * been selected with ATN. We do not want to handle that.
  3984. */
  3985. case SIR_SEL_ATN_NO_MSG_OUT:
  3986. scmd_printk(KERN_WARNING, cp->cmd,
  3987. "No MSG OUT phase after selection with ATN\n");
  3988. goto out_stuck;
  3989. /*
  3990. * The device didn't switch to MSG IN phase after
  3991. * having reselected the initiator.
  3992. */
  3993. case SIR_RESEL_NO_MSG_IN:
  3994. scmd_printk(KERN_WARNING, cp->cmd,
  3995. "No MSG IN phase after reselection\n");
  3996. goto out_stuck;
  3997. /*
  3998. * After reselection, the device sent a message that wasn't
  3999. * an IDENTIFY.
  4000. */
  4001. case SIR_RESEL_NO_IDENTIFY:
  4002. scmd_printk(KERN_WARNING, cp->cmd,
  4003. "No IDENTIFY after reselection\n");
  4004. goto out_stuck;
  4005. /*
  4006. * The device reselected a LUN we do not know about.
  4007. */
  4008. case SIR_RESEL_BAD_LUN:
  4009. np->msgout[0] = M_RESET;
  4010. goto out;
  4011. /*
  4012. * The device reselected for an untagged nexus and we
  4013. * haven't any.
  4014. */
  4015. case SIR_RESEL_BAD_I_T_L:
  4016. np->msgout[0] = M_ABORT;
  4017. goto out;
  4018. /*
  4019. * The device reselected for a tagged nexus that we do not have.
  4020. */
  4021. case SIR_RESEL_BAD_I_T_L_Q:
  4022. np->msgout[0] = M_ABORT_TAG;
  4023. goto out;
  4024. /*
  4025. * The SCRIPTS let us know that the device has grabbed
  4026. * our message and will abort the job.
  4027. */
  4028. case SIR_RESEL_ABORTED:
  4029. np->lastmsg = np->msgout[0];
  4030. np->msgout[0] = M_NOOP;
  4031. scmd_printk(KERN_WARNING, cp->cmd,
  4032. "message %x sent on bad reselection\n", np->lastmsg);
  4033. goto out;
  4034. /*
  4035. * The SCRIPTS let us know that a message has been
  4036. * successfully sent to the device.
  4037. */
  4038. case SIR_MSG_OUT_DONE:
  4039. np->lastmsg = np->msgout[0];
  4040. np->msgout[0] = M_NOOP;
  4041. /* Should we really care of that */
  4042. if (np->lastmsg == M_PARITY || np->lastmsg == M_ID_ERROR) {
  4043. if (cp) {
  4044. cp->xerr_status &= ~XE_PARITY_ERR;
  4045. if (!cp->xerr_status)
  4046. OUTOFFB(np, HF_PRT, HF_EXT_ERR);
  4047. }
  4048. }
  4049. goto out;
  4050. /*
  4051. * The device didn't send a GOOD SCSI status.
  4052. * We may have some work to do prior to allow
  4053. * the SCRIPTS processor to continue.
  4054. */
  4055. case SIR_BAD_SCSI_STATUS:
  4056. if (!cp)
  4057. goto out;
  4058. sym_sir_bad_scsi_status(np, num, cp);
  4059. return;
  4060. /*
  4061. * We are asked by the SCRIPTS to prepare a
  4062. * REJECT message.
  4063. */
  4064. case SIR_REJECT_TO_SEND:
  4065. sym_print_msg(cp, "M_REJECT to send for ", np->msgin);
  4066. np->msgout[0] = M_REJECT;
  4067. goto out;
  4068. /*
  4069. * We have been ODD at the end of a DATA IN
  4070. * transfer and the device didn't send a
  4071. * IGNORE WIDE RESIDUE message.
  4072. * It is a data overrun condition.
  4073. */
  4074. case SIR_SWIDE_OVERRUN:
  4075. if (cp) {
  4076. OUTONB(np, HF_PRT, HF_EXT_ERR);
  4077. cp->xerr_status |= XE_SWIDE_OVRUN;
  4078. }
  4079. goto out;
  4080. /*
  4081. * We have been ODD at the end of a DATA OUT
  4082. * transfer.
  4083. * It is a data underrun condition.
  4084. */
  4085. case SIR_SODL_UNDERRUN:
  4086. if (cp) {
  4087. OUTONB(np, HF_PRT, HF_EXT_ERR);
  4088. cp->xerr_status |= XE_SODL_UNRUN;
  4089. }
  4090. goto out;
  4091. /*
  4092. * The device wants us to tranfer more data than
  4093. * expected or in the wrong direction.
  4094. * The number of extra bytes is in scratcha.
  4095. * It is a data overrun condition.
  4096. */
  4097. case SIR_DATA_OVERRUN:
  4098. if (cp) {
  4099. OUTONB(np, HF_PRT, HF_EXT_ERR);
  4100. cp->xerr_status |= XE_EXTRA_DATA;
  4101. cp->extra_bytes += INL(np, nc_scratcha);
  4102. }
  4103. goto out;
  4104. /*
  4105. * The device switched to an illegal phase (4/5).
  4106. */
  4107. case SIR_BAD_PHASE:
  4108. if (cp) {
  4109. OUTONB(np, HF_PRT, HF_EXT_ERR);
  4110. cp->xerr_status |= XE_BAD_PHASE;
  4111. }
  4112. goto out;
  4113. /*
  4114. * We received a message.
  4115. */
  4116. case SIR_MSG_RECEIVED:
  4117. if (!cp)
  4118. goto out_stuck;
  4119. switch (np->msgin [0]) {
  4120. /*
  4121. * We received an extended message.
  4122. * We handle MODIFY DATA POINTER, SDTR, WDTR
  4123. * and reject all other extended messages.
  4124. */
  4125. case M_EXTENDED:
  4126. switch (np->msgin [2]) {
  4127. case M_X_MODIFY_DP:
  4128. if (DEBUG_FLAGS & DEBUG_POINTER)
  4129. sym_print_msg(cp, NULL, np->msgin);
  4130. tmp = (np->msgin[3]<<24) + (np->msgin[4]<<16) +
  4131. (np->msgin[5]<<8) + (np->msgin[6]);
  4132. sym_modify_dp(np, tp, cp, tmp);
  4133. return;
  4134. case M_X_SYNC_REQ:
  4135. sym_sync_nego(np, tp, cp);
  4136. return;
  4137. case M_X_PPR_REQ:
  4138. sym_ppr_nego(np, tp, cp);
  4139. return;
  4140. case M_X_WIDE_REQ:
  4141. sym_wide_nego(np, tp, cp);
  4142. return;
  4143. default:
  4144. goto out_reject;
  4145. }
  4146. break;
  4147. /*
  4148. * We received a 1/2 byte message not handled from SCRIPTS.
  4149. * We are only expecting MESSAGE REJECT and IGNORE WIDE
  4150. * RESIDUE messages that haven't been anticipated by
  4151. * SCRIPTS on SWIDE full condition. Unanticipated IGNORE
  4152. * WIDE RESIDUE messages are aliased as MODIFY DP (-1).
  4153. */
  4154. case M_IGN_RESIDUE:
  4155. if (DEBUG_FLAGS & DEBUG_POINTER)
  4156. sym_print_msg(cp, NULL, np->msgin);
  4157. if (cp->host_flags & HF_SENSE)
  4158. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  4159. else
  4160. sym_modify_dp(np, tp, cp, -1);
  4161. return;
  4162. case M_REJECT:
  4163. if (INB(np, HS_PRT) == HS_NEGOTIATE)
  4164. sym_nego_rejected(np, tp, cp);
  4165. else {
  4166. sym_print_addr(cp->cmd,
  4167. "M_REJECT received (%x:%x).\n",
  4168. scr_to_cpu(np->lastmsg), np->msgout[0]);
  4169. }
  4170. goto out_clrack;
  4171. break;
  4172. default:
  4173. goto out_reject;
  4174. }
  4175. break;
  4176. /*
  4177. * We received an unknown message.
  4178. * Ignore all MSG IN phases and reject it.
  4179. */
  4180. case SIR_MSG_WEIRD:
  4181. sym_print_msg(cp, "WEIRD message received", np->msgin);
  4182. OUTL_DSP(np, SCRIPTB_BA(np, msg_weird));
  4183. return;
  4184. /*
  4185. * Negotiation failed.
  4186. * Target does not send us the reply.
  4187. * Remove the HS_NEGOTIATE status.
  4188. */
  4189. case SIR_NEGO_FAILED:
  4190. OUTB(np, HS_PRT, HS_BUSY);
  4191. /*
  4192. * Negotiation failed.
  4193. * Target does not want answer message.
  4194. */
  4195. case SIR_NEGO_PROTO:
  4196. sym_nego_default(np, tp, cp);
  4197. goto out;
  4198. }
  4199. out:
  4200. OUTONB_STD();
  4201. return;
  4202. out_reject:
  4203. OUTL_DSP(np, SCRIPTB_BA(np, msg_bad));
  4204. return;
  4205. out_clrack:
  4206. OUTL_DSP(np, SCRIPTA_BA(np, clrack));
  4207. return;
  4208. out_stuck:
  4209. return;
  4210. }
  4211. /*
  4212. * Acquire a control block
  4213. */
  4214. struct sym_ccb *sym_get_ccb (struct sym_hcb *np, struct scsi_cmnd *cmd, u_char tag_order)
  4215. {
  4216. u_char tn = cmd->device->id;
  4217. u_char ln = cmd->device->lun;
  4218. struct sym_tcb *tp = &np->target[tn];
  4219. struct sym_lcb *lp = sym_lp(tp, ln);
  4220. u_short tag = NO_TAG;
  4221. SYM_QUEHEAD *qp;
  4222. struct sym_ccb *cp = NULL;
  4223. /*
  4224. * Look for a free CCB
  4225. */
  4226. if (sym_que_empty(&np->free_ccbq))
  4227. sym_alloc_ccb(np);
  4228. qp = sym_remque_head(&np->free_ccbq);
  4229. if (!qp)
  4230. goto out;
  4231. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  4232. {
  4233. /*
  4234. * If we have been asked for a tagged command.
  4235. */
  4236. if (tag_order) {
  4237. /*
  4238. * Debugging purpose.
  4239. */
  4240. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4241. if (lp->busy_itl != 0)
  4242. goto out_free;
  4243. #endif
  4244. /*
  4245. * Allocate resources for tags if not yet.
  4246. */
  4247. if (!lp->cb_tags) {
  4248. sym_alloc_lcb_tags(np, tn, ln);
  4249. if (!lp->cb_tags)
  4250. goto out_free;
  4251. }
  4252. /*
  4253. * Get a tag for this SCSI IO and set up
  4254. * the CCB bus address for reselection,
  4255. * and count it for this LUN.
  4256. * Toggle reselect path to tagged.
  4257. */
  4258. if (lp->busy_itlq < SYM_CONF_MAX_TASK) {
  4259. tag = lp->cb_tags[lp->ia_tag];
  4260. if (++lp->ia_tag == SYM_CONF_MAX_TASK)
  4261. lp->ia_tag = 0;
  4262. ++lp->busy_itlq;
  4263. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4264. lp->itlq_tbl[tag] = cpu_to_scr(cp->ccb_ba);
  4265. lp->head.resel_sa =
  4266. cpu_to_scr(SCRIPTA_BA(np, resel_tag));
  4267. #endif
  4268. #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
  4269. cp->tags_si = lp->tags_si;
  4270. ++lp->tags_sum[cp->tags_si];
  4271. ++lp->tags_since;
  4272. #endif
  4273. }
  4274. else
  4275. goto out_free;
  4276. }
  4277. /*
  4278. * This command will not be tagged.
  4279. * If we already have either a tagged or untagged
  4280. * one, refuse to overlap this untagged one.
  4281. */
  4282. else {
  4283. /*
  4284. * Debugging purpose.
  4285. */
  4286. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4287. if (lp->busy_itl != 0 || lp->busy_itlq != 0)
  4288. goto out_free;
  4289. #endif
  4290. /*
  4291. * Count this nexus for this LUN.
  4292. * Set up the CCB bus address for reselection.
  4293. * Toggle reselect path to untagged.
  4294. */
  4295. ++lp->busy_itl;
  4296. #ifndef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4297. if (lp->busy_itl == 1) {
  4298. lp->head.itl_task_sa = cpu_to_scr(cp->ccb_ba);
  4299. lp->head.resel_sa =
  4300. cpu_to_scr(SCRIPTA_BA(np, resel_no_tag));
  4301. }
  4302. else
  4303. goto out_free;
  4304. #endif
  4305. }
  4306. }
  4307. /*
  4308. * Put the CCB into the busy queue.
  4309. */
  4310. sym_insque_tail(&cp->link_ccbq, &np->busy_ccbq);
  4311. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4312. if (lp) {
  4313. sym_remque(&cp->link2_ccbq);
  4314. sym_insque_tail(&cp->link2_ccbq, &lp->waiting_ccbq);
  4315. }
  4316. #endif
  4317. cp->to_abort = 0;
  4318. cp->odd_byte_adjustment = 0;
  4319. cp->tag = tag;
  4320. cp->order = tag_order;
  4321. cp->target = tn;
  4322. cp->lun = ln;
  4323. if (DEBUG_FLAGS & DEBUG_TAGS) {
  4324. sym_print_addr(cmd, "ccb @%p using tag %d.\n", cp, tag);
  4325. }
  4326. out:
  4327. return cp;
  4328. out_free:
  4329. sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
  4330. return NULL;
  4331. }
  4332. /*
  4333. * Release one control block
  4334. */
  4335. void sym_free_ccb (struct sym_hcb *np, struct sym_ccb *cp)
  4336. {
  4337. struct sym_tcb *tp = &np->target[cp->target];
  4338. struct sym_lcb *lp = sym_lp(tp, cp->lun);
  4339. if (DEBUG_FLAGS & DEBUG_TAGS) {
  4340. sym_print_addr(cp->cmd, "ccb @%p freeing tag %d.\n",
  4341. cp, cp->tag);
  4342. }
  4343. /*
  4344. * If LCB available,
  4345. */
  4346. if (lp) {
  4347. /*
  4348. * If tagged, release the tag, set the relect path
  4349. */
  4350. if (cp->tag != NO_TAG) {
  4351. #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
  4352. --lp->tags_sum[cp->tags_si];
  4353. #endif
  4354. /*
  4355. * Free the tag value.
  4356. */
  4357. lp->cb_tags[lp->if_tag] = cp->tag;
  4358. if (++lp->if_tag == SYM_CONF_MAX_TASK)
  4359. lp->if_tag = 0;
  4360. /*
  4361. * Make the reselect path invalid,
  4362. * and uncount this CCB.
  4363. */
  4364. lp->itlq_tbl[cp->tag] = cpu_to_scr(np->bad_itlq_ba);
  4365. --lp->busy_itlq;
  4366. } else { /* Untagged */
  4367. /*
  4368. * Make the reselect path invalid,
  4369. * and uncount this CCB.
  4370. */
  4371. lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
  4372. --lp->busy_itl;
  4373. }
  4374. /*
  4375. * If no JOB active, make the LUN reselect path invalid.
  4376. */
  4377. if (lp->busy_itlq == 0 && lp->busy_itl == 0)
  4378. lp->head.resel_sa =
  4379. cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
  4380. }
  4381. /*
  4382. * We donnot queue more than 1 ccb per target
  4383. * with negotiation at any time. If this ccb was
  4384. * used for negotiation, clear this info in the tcb.
  4385. */
  4386. if (cp == tp->nego_cp)
  4387. tp->nego_cp = NULL;
  4388. #ifdef SYM_CONF_IARB_SUPPORT
  4389. /*
  4390. * If we just complete the last queued CCB,
  4391. * clear this info that is no longer relevant.
  4392. */
  4393. if (cp == np->last_cp)
  4394. np->last_cp = 0;
  4395. #endif
  4396. /*
  4397. * Make this CCB available.
  4398. */
  4399. cp->cmd = NULL;
  4400. cp->host_status = HS_IDLE;
  4401. sym_remque(&cp->link_ccbq);
  4402. sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
  4403. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4404. if (lp) {
  4405. sym_remque(&cp->link2_ccbq);
  4406. sym_insque_tail(&cp->link2_ccbq, &np->dummy_ccbq);
  4407. if (cp->started) {
  4408. if (cp->tag != NO_TAG)
  4409. --lp->started_tags;
  4410. else
  4411. --lp->started_no_tag;
  4412. }
  4413. }
  4414. cp->started = 0;
  4415. #endif
  4416. }
  4417. /*
  4418. * Allocate a CCB from memory and initialize its fixed part.
  4419. */
  4420. static struct sym_ccb *sym_alloc_ccb(struct sym_hcb *np)
  4421. {
  4422. struct sym_ccb *cp = NULL;
  4423. int hcode;
  4424. /*
  4425. * Prevent from allocating more CCBs than we can
  4426. * queue to the controller.
  4427. */
  4428. if (np->actccbs >= SYM_CONF_MAX_START)
  4429. return NULL;
  4430. /*
  4431. * Allocate memory for this CCB.
  4432. */
  4433. cp = sym_calloc_dma(sizeof(struct sym_ccb), "CCB");
  4434. if (!cp)
  4435. goto out_free;
  4436. /*
  4437. * Count it.
  4438. */
  4439. np->actccbs++;
  4440. /*
  4441. * Compute the bus address of this ccb.
  4442. */
  4443. cp->ccb_ba = vtobus(cp);
  4444. /*
  4445. * Insert this ccb into the hashed list.
  4446. */
  4447. hcode = CCB_HASH_CODE(cp->ccb_ba);
  4448. cp->link_ccbh = np->ccbh[hcode];
  4449. np->ccbh[hcode] = cp;
  4450. /*
  4451. * Initialyze the start and restart actions.
  4452. */
  4453. cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  4454. cp->phys.head.go.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
  4455. /*
  4456. * Initilialyze some other fields.
  4457. */
  4458. cp->phys.smsg_ext.addr = cpu_to_scr(HCB_BA(np, msgin[2]));
  4459. /*
  4460. * Chain into free ccb queue.
  4461. */
  4462. sym_insque_head(&cp->link_ccbq, &np->free_ccbq);
  4463. /*
  4464. * Chain into optionnal lists.
  4465. */
  4466. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4467. sym_insque_head(&cp->link2_ccbq, &np->dummy_ccbq);
  4468. #endif
  4469. return cp;
  4470. out_free:
  4471. if (cp)
  4472. sym_mfree_dma(cp, sizeof(*cp), "CCB");
  4473. return NULL;
  4474. }
  4475. /*
  4476. * Look up a CCB from a DSA value.
  4477. */
  4478. static struct sym_ccb *sym_ccb_from_dsa(struct sym_hcb *np, u32 dsa)
  4479. {
  4480. int hcode;
  4481. struct sym_ccb *cp;
  4482. hcode = CCB_HASH_CODE(dsa);
  4483. cp = np->ccbh[hcode];
  4484. while (cp) {
  4485. if (cp->ccb_ba == dsa)
  4486. break;
  4487. cp = cp->link_ccbh;
  4488. }
  4489. return cp;
  4490. }
  4491. /*
  4492. * Target control block initialisation.
  4493. * Nothing important to do at the moment.
  4494. */
  4495. static void sym_init_tcb (struct sym_hcb *np, u_char tn)
  4496. {
  4497. #if 0 /* Hmmm... this checking looks paranoid. */
  4498. /*
  4499. * Check some alignments required by the chip.
  4500. */
  4501. assert (((offsetof(struct sym_reg, nc_sxfer) ^
  4502. offsetof(struct sym_tcb, head.sval)) &3) == 0);
  4503. assert (((offsetof(struct sym_reg, nc_scntl3) ^
  4504. offsetof(struct sym_tcb, head.wval)) &3) == 0);
  4505. #endif
  4506. }
  4507. /*
  4508. * Lun control block allocation and initialization.
  4509. */
  4510. struct sym_lcb *sym_alloc_lcb (struct sym_hcb *np, u_char tn, u_char ln)
  4511. {
  4512. struct sym_tcb *tp = &np->target[tn];
  4513. struct sym_lcb *lp = NULL;
  4514. /*
  4515. * Initialize the target control block if not yet.
  4516. */
  4517. sym_init_tcb (np, tn);
  4518. /*
  4519. * Allocate the LCB bus address array.
  4520. * Compute the bus address of this table.
  4521. */
  4522. if (ln && !tp->luntbl) {
  4523. int i;
  4524. tp->luntbl = sym_calloc_dma(256, "LUNTBL");
  4525. if (!tp->luntbl)
  4526. goto fail;
  4527. for (i = 0 ; i < 64 ; i++)
  4528. tp->luntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
  4529. tp->head.luntbl_sa = cpu_to_scr(vtobus(tp->luntbl));
  4530. }
  4531. /*
  4532. * Allocate the table of pointers for LUN(s) > 0, if needed.
  4533. */
  4534. if (ln && !tp->lunmp) {
  4535. tp->lunmp = kcalloc(SYM_CONF_MAX_LUN, sizeof(struct sym_lcb *),
  4536. GFP_ATOMIC);
  4537. if (!tp->lunmp)
  4538. goto fail;
  4539. }
  4540. /*
  4541. * Allocate the lcb.
  4542. * Make it available to the chip.
  4543. */
  4544. lp = sym_calloc_dma(sizeof(struct sym_lcb), "LCB");
  4545. if (!lp)
  4546. goto fail;
  4547. if (ln) {
  4548. tp->lunmp[ln] = lp;
  4549. tp->luntbl[ln] = cpu_to_scr(vtobus(lp));
  4550. }
  4551. else {
  4552. tp->lun0p = lp;
  4553. tp->head.lun0_sa = cpu_to_scr(vtobus(lp));
  4554. }
  4555. tp->nlcb++;
  4556. /*
  4557. * Let the itl task point to error handling.
  4558. */
  4559. lp->head.itl_task_sa = cpu_to_scr(np->bad_itl_ba);
  4560. /*
  4561. * Set the reselect pattern to our default. :)
  4562. */
  4563. lp->head.resel_sa = cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
  4564. /*
  4565. * Set user capabilities.
  4566. */
  4567. lp->user_flags = tp->usrflags & (SYM_DISC_ENABLED | SYM_TAGS_ENABLED);
  4568. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4569. /*
  4570. * Initialize device queueing.
  4571. */
  4572. sym_que_init(&lp->waiting_ccbq);
  4573. sym_que_init(&lp->started_ccbq);
  4574. lp->started_max = SYM_CONF_MAX_TASK;
  4575. lp->started_limit = SYM_CONF_MAX_TASK;
  4576. #endif
  4577. fail:
  4578. return lp;
  4579. }
  4580. /*
  4581. * Allocate LCB resources for tagged command queuing.
  4582. */
  4583. static void sym_alloc_lcb_tags (struct sym_hcb *np, u_char tn, u_char ln)
  4584. {
  4585. struct sym_tcb *tp = &np->target[tn];
  4586. struct sym_lcb *lp = sym_lp(tp, ln);
  4587. int i;
  4588. /*
  4589. * Allocate the task table and and the tag allocation
  4590. * circular buffer. We want both or none.
  4591. */
  4592. lp->itlq_tbl = sym_calloc_dma(SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
  4593. if (!lp->itlq_tbl)
  4594. goto fail;
  4595. lp->cb_tags = kcalloc(SYM_CONF_MAX_TASK, 1, GFP_ATOMIC);
  4596. if (!lp->cb_tags) {
  4597. sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
  4598. lp->itlq_tbl = NULL;
  4599. goto fail;
  4600. }
  4601. /*
  4602. * Initialize the task table with invalid entries.
  4603. */
  4604. for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
  4605. lp->itlq_tbl[i] = cpu_to_scr(np->notask_ba);
  4606. /*
  4607. * Fill up the tag buffer with tag numbers.
  4608. */
  4609. for (i = 0 ; i < SYM_CONF_MAX_TASK ; i++)
  4610. lp->cb_tags[i] = i;
  4611. /*
  4612. * Make the task table available to SCRIPTS,
  4613. * And accept tagged commands now.
  4614. */
  4615. lp->head.itlq_tbl_sa = cpu_to_scr(vtobus(lp->itlq_tbl));
  4616. return;
  4617. fail:
  4618. return;
  4619. }
  4620. /*
  4621. * Lun control block deallocation. Returns the number of valid remaing LCBs
  4622. * for the target.
  4623. */
  4624. int sym_free_lcb(struct sym_hcb *np, u_char tn, u_char ln)
  4625. {
  4626. struct sym_tcb *tp = &np->target[tn];
  4627. struct sym_lcb *lp = sym_lp(tp, ln);
  4628. tp->nlcb--;
  4629. if (ln) {
  4630. if (!tp->nlcb) {
  4631. kfree(tp->lunmp);
  4632. sym_mfree_dma(tp->luntbl, 256, "LUNTBL");
  4633. tp->lunmp = NULL;
  4634. tp->luntbl = NULL;
  4635. tp->head.luntbl_sa = cpu_to_scr(vtobus(np->badluntbl));
  4636. } else {
  4637. tp->luntbl[ln] = cpu_to_scr(vtobus(&np->badlun_sa));
  4638. tp->lunmp[ln] = NULL;
  4639. }
  4640. } else {
  4641. tp->lun0p = NULL;
  4642. tp->head.lun0_sa = cpu_to_scr(vtobus(&np->badlun_sa));
  4643. }
  4644. if (lp->itlq_tbl) {
  4645. sym_mfree_dma(lp->itlq_tbl, SYM_CONF_MAX_TASK*4, "ITLQ_TBL");
  4646. kfree(lp->cb_tags);
  4647. }
  4648. sym_mfree_dma(lp, sizeof(*lp), "LCB");
  4649. return tp->nlcb;
  4650. }
  4651. /*
  4652. * Queue a SCSI IO to the controller.
  4653. */
  4654. int sym_queue_scsiio(struct sym_hcb *np, struct scsi_cmnd *cmd, struct sym_ccb *cp)
  4655. {
  4656. struct scsi_device *sdev = cmd->device;
  4657. struct sym_tcb *tp;
  4658. struct sym_lcb *lp;
  4659. u_char *msgptr;
  4660. u_int msglen;
  4661. int can_disconnect;
  4662. /*
  4663. * Keep track of the IO in our CCB.
  4664. */
  4665. cp->cmd = cmd;
  4666. /*
  4667. * Retrieve the target descriptor.
  4668. */
  4669. tp = &np->target[cp->target];
  4670. /*
  4671. * Retrieve the lun descriptor.
  4672. */
  4673. lp = sym_lp(tp, sdev->lun);
  4674. can_disconnect = (cp->tag != NO_TAG) ||
  4675. (lp && (lp->curr_flags & SYM_DISC_ENABLED));
  4676. msgptr = cp->scsi_smsg;
  4677. msglen = 0;
  4678. msgptr[msglen++] = IDENTIFY(can_disconnect, sdev->lun);
  4679. /*
  4680. * Build the tag message if present.
  4681. */
  4682. if (cp->tag != NO_TAG) {
  4683. u_char order = cp->order;
  4684. switch(order) {
  4685. case M_ORDERED_TAG:
  4686. break;
  4687. case M_HEAD_TAG:
  4688. break;
  4689. default:
  4690. order = M_SIMPLE_TAG;
  4691. }
  4692. #ifdef SYM_OPT_LIMIT_COMMAND_REORDERING
  4693. /*
  4694. * Avoid too much reordering of SCSI commands.
  4695. * The algorithm tries to prevent completion of any
  4696. * tagged command from being delayed against more
  4697. * than 3 times the max number of queued commands.
  4698. */
  4699. if (lp && lp->tags_since > 3*SYM_CONF_MAX_TAG) {
  4700. lp->tags_si = !(lp->tags_si);
  4701. if (lp->tags_sum[lp->tags_si]) {
  4702. order = M_ORDERED_TAG;
  4703. if ((DEBUG_FLAGS & DEBUG_TAGS)||sym_verbose>1) {
  4704. sym_print_addr(cmd,
  4705. "ordered tag forced.\n");
  4706. }
  4707. }
  4708. lp->tags_since = 0;
  4709. }
  4710. #endif
  4711. msgptr[msglen++] = order;
  4712. /*
  4713. * For less than 128 tags, actual tags are numbered
  4714. * 1,3,5,..2*MAXTAGS+1,since we may have to deal
  4715. * with devices that have problems with #TAG 0 or too
  4716. * great #TAG numbers. For more tags (up to 256),
  4717. * we use directly our tag number.
  4718. */
  4719. #if SYM_CONF_MAX_TASK > (512/4)
  4720. msgptr[msglen++] = cp->tag;
  4721. #else
  4722. msgptr[msglen++] = (cp->tag << 1) + 1;
  4723. #endif
  4724. }
  4725. /*
  4726. * Build a negotiation message if needed.
  4727. * (nego_status is filled by sym_prepare_nego())
  4728. *
  4729. * Always negotiate on INQUIRY and REQUEST SENSE.
  4730. *
  4731. */
  4732. cp->nego_status = 0;
  4733. if ((tp->tgoal.check_nego ||
  4734. cmd->cmnd[0] == INQUIRY || cmd->cmnd[0] == REQUEST_SENSE) &&
  4735. !tp->nego_cp && lp) {
  4736. msglen += sym_prepare_nego(np, cp, msgptr + msglen);
  4737. }
  4738. /*
  4739. * Startqueue
  4740. */
  4741. cp->phys.head.go.start = cpu_to_scr(SCRIPTA_BA(np, select));
  4742. cp->phys.head.go.restart = cpu_to_scr(SCRIPTA_BA(np, resel_dsa));
  4743. /*
  4744. * select
  4745. */
  4746. cp->phys.select.sel_id = cp->target;
  4747. cp->phys.select.sel_scntl3 = tp->head.wval;
  4748. cp->phys.select.sel_sxfer = tp->head.sval;
  4749. cp->phys.select.sel_scntl4 = tp->head.uval;
  4750. /*
  4751. * message
  4752. */
  4753. cp->phys.smsg.addr = CCB_BA(cp, scsi_smsg);
  4754. cp->phys.smsg.size = cpu_to_scr(msglen);
  4755. /*
  4756. * status
  4757. */
  4758. cp->host_xflags = 0;
  4759. cp->host_status = cp->nego_status ? HS_NEGOTIATE : HS_BUSY;
  4760. cp->ssss_status = S_ILLEGAL;
  4761. cp->xerr_status = 0;
  4762. cp->host_flags = 0;
  4763. cp->extra_bytes = 0;
  4764. /*
  4765. * extreme data pointer.
  4766. * shall be positive, so -1 is lower than lowest.:)
  4767. */
  4768. cp->ext_sg = -1;
  4769. cp->ext_ofs = 0;
  4770. /*
  4771. * Build the CDB and DATA descriptor block
  4772. * and start the IO.
  4773. */
  4774. return sym_setup_data_and_start(np, cmd, cp);
  4775. }
  4776. /*
  4777. * Reset a SCSI target (all LUNs of this target).
  4778. */
  4779. int sym_reset_scsi_target(struct sym_hcb *np, int target)
  4780. {
  4781. struct sym_tcb *tp;
  4782. if (target == np->myaddr || (u_int)target >= SYM_CONF_MAX_TARGET)
  4783. return -1;
  4784. tp = &np->target[target];
  4785. tp->to_reset = 1;
  4786. np->istat_sem = SEM;
  4787. OUTB(np, nc_istat, SIGP|SEM);
  4788. return 0;
  4789. }
  4790. /*
  4791. * Abort a SCSI IO.
  4792. */
  4793. static int sym_abort_ccb(struct sym_hcb *np, struct sym_ccb *cp, int timed_out)
  4794. {
  4795. /*
  4796. * Check that the IO is active.
  4797. */
  4798. if (!cp || !cp->host_status || cp->host_status == HS_WAIT)
  4799. return -1;
  4800. /*
  4801. * If a previous abort didn't succeed in time,
  4802. * perform a BUS reset.
  4803. */
  4804. if (cp->to_abort) {
  4805. sym_reset_scsi_bus(np, 1);
  4806. return 0;
  4807. }
  4808. /*
  4809. * Mark the CCB for abort and allow time for.
  4810. */
  4811. cp->to_abort = timed_out ? 2 : 1;
  4812. /*
  4813. * Tell the SCRIPTS processor to stop and synchronize with us.
  4814. */
  4815. np->istat_sem = SEM;
  4816. OUTB(np, nc_istat, SIGP|SEM);
  4817. return 0;
  4818. }
  4819. int sym_abort_scsiio(struct sym_hcb *np, struct scsi_cmnd *cmd, int timed_out)
  4820. {
  4821. struct sym_ccb *cp;
  4822. SYM_QUEHEAD *qp;
  4823. /*
  4824. * Look up our CCB control block.
  4825. */
  4826. cp = NULL;
  4827. FOR_EACH_QUEUED_ELEMENT(&np->busy_ccbq, qp) {
  4828. struct sym_ccb *cp2 = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  4829. if (cp2->cmd == cmd) {
  4830. cp = cp2;
  4831. break;
  4832. }
  4833. }
  4834. return sym_abort_ccb(np, cp, timed_out);
  4835. }
  4836. /*
  4837. * Complete execution of a SCSI command with extended
  4838. * error, SCSI status error, or having been auto-sensed.
  4839. *
  4840. * The SCRIPTS processor is not running there, so we
  4841. * can safely access IO registers and remove JOBs from
  4842. * the START queue.
  4843. * SCRATCHA is assumed to have been loaded with STARTPOS
  4844. * before the SCRIPTS called the C code.
  4845. */
  4846. void sym_complete_error(struct sym_hcb *np, struct sym_ccb *cp)
  4847. {
  4848. struct scsi_device *sdev;
  4849. struct scsi_cmnd *cmd;
  4850. struct sym_tcb *tp;
  4851. struct sym_lcb *lp;
  4852. int resid;
  4853. int i;
  4854. /*
  4855. * Paranoid check. :)
  4856. */
  4857. if (!cp || !cp->cmd)
  4858. return;
  4859. cmd = cp->cmd;
  4860. sdev = cmd->device;
  4861. if (DEBUG_FLAGS & (DEBUG_TINY|DEBUG_RESULT)) {
  4862. dev_info(&sdev->sdev_gendev, "CCB=%p STAT=%x/%x/%x\n", cp,
  4863. cp->host_status, cp->ssss_status, cp->host_flags);
  4864. }
  4865. /*
  4866. * Get target and lun pointers.
  4867. */
  4868. tp = &np->target[cp->target];
  4869. lp = sym_lp(tp, sdev->lun);
  4870. /*
  4871. * Check for extended errors.
  4872. */
  4873. if (cp->xerr_status) {
  4874. if (sym_verbose)
  4875. sym_print_xerr(cmd, cp->xerr_status);
  4876. if (cp->host_status == HS_COMPLETE)
  4877. cp->host_status = HS_COMP_ERR;
  4878. }
  4879. /*
  4880. * Calculate the residual.
  4881. */
  4882. resid = sym_compute_residual(np, cp);
  4883. if (!SYM_SETUP_RESIDUAL_SUPPORT) {/* If user does not want residuals */
  4884. resid = 0; /* throw them away. :) */
  4885. cp->sv_resid = 0;
  4886. }
  4887. #ifdef DEBUG_2_0_X
  4888. if (resid)
  4889. printf("XXXX RESID= %d - 0x%x\n", resid, resid);
  4890. #endif
  4891. /*
  4892. * Dequeue all queued CCBs for that device
  4893. * not yet started by SCRIPTS.
  4894. */
  4895. i = (INL(np, nc_scratcha) - np->squeue_ba) / 4;
  4896. i = sym_dequeue_from_squeue(np, i, cp->target, sdev->lun, -1);
  4897. /*
  4898. * Restart the SCRIPTS processor.
  4899. */
  4900. OUTL_DSP(np, SCRIPTA_BA(np, start));
  4901. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4902. if (cp->host_status == HS_COMPLETE &&
  4903. cp->ssss_status == S_QUEUE_FULL) {
  4904. if (!lp || lp->started_tags - i < 2)
  4905. goto weirdness;
  4906. /*
  4907. * Decrease queue depth as needed.
  4908. */
  4909. lp->started_max = lp->started_tags - i - 1;
  4910. lp->num_sgood = 0;
  4911. if (sym_verbose >= 2) {
  4912. sym_print_addr(cmd, " queue depth is now %d\n",
  4913. lp->started_max);
  4914. }
  4915. /*
  4916. * Repair the CCB.
  4917. */
  4918. cp->host_status = HS_BUSY;
  4919. cp->ssss_status = S_ILLEGAL;
  4920. /*
  4921. * Let's requeue it to device.
  4922. */
  4923. sym_set_cam_status(cmd, DID_SOFT_ERROR);
  4924. goto finish;
  4925. }
  4926. weirdness:
  4927. #endif
  4928. /*
  4929. * Build result in CAM ccb.
  4930. */
  4931. sym_set_cam_result_error(np, cp, resid);
  4932. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4933. finish:
  4934. #endif
  4935. /*
  4936. * Add this one to the COMP queue.
  4937. */
  4938. sym_remque(&cp->link_ccbq);
  4939. sym_insque_head(&cp->link_ccbq, &np->comp_ccbq);
  4940. /*
  4941. * Complete all those commands with either error
  4942. * or requeue condition.
  4943. */
  4944. sym_flush_comp_queue(np, 0);
  4945. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  4946. /*
  4947. * Donnot start more than 1 command after an error.
  4948. */
  4949. sym_start_next_ccbs(np, lp, 1);
  4950. #endif
  4951. }
  4952. /*
  4953. * Complete execution of a successful SCSI command.
  4954. *
  4955. * Only successful commands go to the DONE queue,
  4956. * since we need to have the SCRIPTS processor
  4957. * stopped on any error condition.
  4958. * The SCRIPTS processor is running while we are
  4959. * completing successful commands.
  4960. */
  4961. void sym_complete_ok (struct sym_hcb *np, struct sym_ccb *cp)
  4962. {
  4963. struct sym_tcb *tp;
  4964. struct sym_lcb *lp;
  4965. struct scsi_cmnd *cmd;
  4966. int resid;
  4967. /*
  4968. * Paranoid check. :)
  4969. */
  4970. if (!cp || !cp->cmd)
  4971. return;
  4972. assert (cp->host_status == HS_COMPLETE);
  4973. /*
  4974. * Get user command.
  4975. */
  4976. cmd = cp->cmd;
  4977. /*
  4978. * Get target and lun pointers.
  4979. */
  4980. tp = &np->target[cp->target];
  4981. lp = sym_lp(tp, cp->lun);
  4982. /*
  4983. * If all data have been transferred, given than no
  4984. * extended error did occur, there is no residual.
  4985. */
  4986. resid = 0;
  4987. if (cp->phys.head.lastp != cp->goalp)
  4988. resid = sym_compute_residual(np, cp);
  4989. /*
  4990. * Wrong transfer residuals may be worse than just always
  4991. * returning zero. User can disable this feature in
  4992. * sym53c8xx.h. Residual support is enabled by default.
  4993. */
  4994. if (!SYM_SETUP_RESIDUAL_SUPPORT)
  4995. resid = 0;
  4996. #ifdef DEBUG_2_0_X
  4997. if (resid)
  4998. printf("XXXX RESID= %d - 0x%x\n", resid, resid);
  4999. #endif
  5000. /*
  5001. * Build result in CAM ccb.
  5002. */
  5003. sym_set_cam_result_ok(cp, cmd, resid);
  5004. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  5005. /*
  5006. * If max number of started ccbs had been reduced,
  5007. * increase it if 200 good status received.
  5008. */
  5009. if (lp && lp->started_max < lp->started_limit) {
  5010. ++lp->num_sgood;
  5011. if (lp->num_sgood >= 200) {
  5012. lp->num_sgood = 0;
  5013. ++lp->started_max;
  5014. if (sym_verbose >= 2) {
  5015. sym_print_addr(cmd, " queue depth is now %d\n",
  5016. lp->started_max);
  5017. }
  5018. }
  5019. }
  5020. #endif
  5021. /*
  5022. * Free our CCB.
  5023. */
  5024. sym_free_ccb (np, cp);
  5025. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  5026. /*
  5027. * Requeue a couple of awaiting scsi commands.
  5028. */
  5029. if (!sym_que_empty(&lp->waiting_ccbq))
  5030. sym_start_next_ccbs(np, lp, 2);
  5031. #endif
  5032. /*
  5033. * Complete the command.
  5034. */
  5035. sym_xpt_done(np, cmd);
  5036. }
  5037. /*
  5038. * Soft-attach the controller.
  5039. */
  5040. int sym_hcb_attach(struct Scsi_Host *shost, struct sym_fw *fw, struct sym_nvram *nvram)
  5041. {
  5042. struct sym_hcb *np = sym_get_hcb(shost);
  5043. int i;
  5044. /*
  5045. * Get some info about the firmware.
  5046. */
  5047. np->scripta_sz = fw->a_size;
  5048. np->scriptb_sz = fw->b_size;
  5049. np->scriptz_sz = fw->z_size;
  5050. np->fw_setup = fw->setup;
  5051. np->fw_patch = fw->patch;
  5052. np->fw_name = fw->name;
  5053. /*
  5054. * Save setting of some IO registers, so we will
  5055. * be able to probe specific implementations.
  5056. */
  5057. sym_save_initial_setting (np);
  5058. /*
  5059. * Reset the chip now, since it has been reported
  5060. * that SCSI clock calibration may not work properly
  5061. * if the chip is currently active.
  5062. */
  5063. sym_chip_reset(np);
  5064. /*
  5065. * Prepare controller and devices settings, according
  5066. * to chip features, user set-up and driver set-up.
  5067. */
  5068. sym_prepare_setting(shost, np, nvram);
  5069. /*
  5070. * Check the PCI clock frequency.
  5071. * Must be performed after prepare_setting since it destroys
  5072. * STEST1 that is used to probe for the clock doubler.
  5073. */
  5074. i = sym_getpciclock(np);
  5075. if (i > 37000 && !(np->features & FE_66MHZ))
  5076. printf("%s: PCI BUS clock seems too high: %u KHz.\n",
  5077. sym_name(np), i);
  5078. /*
  5079. * Allocate the start queue.
  5080. */
  5081. np->squeue = sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"SQUEUE");
  5082. if (!np->squeue)
  5083. goto attach_failed;
  5084. np->squeue_ba = vtobus(np->squeue);
  5085. /*
  5086. * Allocate the done queue.
  5087. */
  5088. np->dqueue = sym_calloc_dma(sizeof(u32)*(MAX_QUEUE*2),"DQUEUE");
  5089. if (!np->dqueue)
  5090. goto attach_failed;
  5091. np->dqueue_ba = vtobus(np->dqueue);
  5092. /*
  5093. * Allocate the target bus address array.
  5094. */
  5095. np->targtbl = sym_calloc_dma(256, "TARGTBL");
  5096. if (!np->targtbl)
  5097. goto attach_failed;
  5098. np->targtbl_ba = vtobus(np->targtbl);
  5099. /*
  5100. * Allocate SCRIPTS areas.
  5101. */
  5102. np->scripta0 = sym_calloc_dma(np->scripta_sz, "SCRIPTA0");
  5103. np->scriptb0 = sym_calloc_dma(np->scriptb_sz, "SCRIPTB0");
  5104. np->scriptz0 = sym_calloc_dma(np->scriptz_sz, "SCRIPTZ0");
  5105. if (!np->scripta0 || !np->scriptb0 || !np->scriptz0)
  5106. goto attach_failed;
  5107. /*
  5108. * Allocate the array of lists of CCBs hashed by DSA.
  5109. */
  5110. np->ccbh = kcalloc(CCB_HASH_SIZE, sizeof(struct sym_ccb **), GFP_KERNEL);
  5111. if (!np->ccbh)
  5112. goto attach_failed;
  5113. /*
  5114. * Initialyze the CCB free and busy queues.
  5115. */
  5116. sym_que_init(&np->free_ccbq);
  5117. sym_que_init(&np->busy_ccbq);
  5118. sym_que_init(&np->comp_ccbq);
  5119. /*
  5120. * Initialization for optional handling
  5121. * of device queueing.
  5122. */
  5123. #ifdef SYM_OPT_HANDLE_DEVICE_QUEUEING
  5124. sym_que_init(&np->dummy_ccbq);
  5125. #endif
  5126. /*
  5127. * Allocate some CCB. We need at least ONE.
  5128. */
  5129. if (!sym_alloc_ccb(np))
  5130. goto attach_failed;
  5131. /*
  5132. * Calculate BUS addresses where we are going
  5133. * to load the SCRIPTS.
  5134. */
  5135. np->scripta_ba = vtobus(np->scripta0);
  5136. np->scriptb_ba = vtobus(np->scriptb0);
  5137. np->scriptz_ba = vtobus(np->scriptz0);
  5138. if (np->ram_ba) {
  5139. np->scripta_ba = np->ram_ba;
  5140. if (np->features & FE_RAM8K) {
  5141. np->scriptb_ba = np->scripta_ba + 4096;
  5142. #if 0 /* May get useful for 64 BIT PCI addressing */
  5143. np->scr_ram_seg = cpu_to_scr(np->scripta_ba >> 32);
  5144. #endif
  5145. }
  5146. }
  5147. /*
  5148. * Copy scripts to controller instance.
  5149. */
  5150. memcpy(np->scripta0, fw->a_base, np->scripta_sz);
  5151. memcpy(np->scriptb0, fw->b_base, np->scriptb_sz);
  5152. memcpy(np->scriptz0, fw->z_base, np->scriptz_sz);
  5153. /*
  5154. * Setup variable parts in scripts and compute
  5155. * scripts bus addresses used from the C code.
  5156. */
  5157. np->fw_setup(np, fw);
  5158. /*
  5159. * Bind SCRIPTS with physical addresses usable by the
  5160. * SCRIPTS processor (as seen from the BUS = BUS addresses).
  5161. */
  5162. sym_fw_bind_script(np, (u32 *) np->scripta0, np->scripta_sz);
  5163. sym_fw_bind_script(np, (u32 *) np->scriptb0, np->scriptb_sz);
  5164. sym_fw_bind_script(np, (u32 *) np->scriptz0, np->scriptz_sz);
  5165. #ifdef SYM_CONF_IARB_SUPPORT
  5166. /*
  5167. * If user wants IARB to be set when we win arbitration
  5168. * and have other jobs, compute the max number of consecutive
  5169. * settings of IARB hints before we leave devices a chance to
  5170. * arbitrate for reselection.
  5171. */
  5172. #ifdef SYM_SETUP_IARB_MAX
  5173. np->iarb_max = SYM_SETUP_IARB_MAX;
  5174. #else
  5175. np->iarb_max = 4;
  5176. #endif
  5177. #endif
  5178. /*
  5179. * Prepare the idle and invalid task actions.
  5180. */
  5181. np->idletask.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  5182. np->idletask.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
  5183. np->idletask_ba = vtobus(&np->idletask);
  5184. np->notask.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  5185. np->notask.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
  5186. np->notask_ba = vtobus(&np->notask);
  5187. np->bad_itl.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  5188. np->bad_itl.restart = cpu_to_scr(SCRIPTB_BA(np, bad_i_t_l));
  5189. np->bad_itl_ba = vtobus(&np->bad_itl);
  5190. np->bad_itlq.start = cpu_to_scr(SCRIPTA_BA(np, idle));
  5191. np->bad_itlq.restart = cpu_to_scr(SCRIPTB_BA(np,bad_i_t_l_q));
  5192. np->bad_itlq_ba = vtobus(&np->bad_itlq);
  5193. /*
  5194. * Allocate and prepare the lun JUMP table that is used
  5195. * for a target prior the probing of devices (bad lun table).
  5196. * A private table will be allocated for the target on the
  5197. * first INQUIRY response received.
  5198. */
  5199. np->badluntbl = sym_calloc_dma(256, "BADLUNTBL");
  5200. if (!np->badluntbl)
  5201. goto attach_failed;
  5202. np->badlun_sa = cpu_to_scr(SCRIPTB_BA(np, resel_bad_lun));
  5203. for (i = 0 ; i < 64 ; i++) /* 64 luns/target, no less */
  5204. np->badluntbl[i] = cpu_to_scr(vtobus(&np->badlun_sa));
  5205. /*
  5206. * Prepare the bus address array that contains the bus
  5207. * address of each target control block.
  5208. * For now, assume all logical units are wrong. :)
  5209. */
  5210. for (i = 0 ; i < SYM_CONF_MAX_TARGET ; i++) {
  5211. np->targtbl[i] = cpu_to_scr(vtobus(&np->target[i]));
  5212. np->target[i].head.luntbl_sa =
  5213. cpu_to_scr(vtobus(np->badluntbl));
  5214. np->target[i].head.lun0_sa =
  5215. cpu_to_scr(vtobus(&np->badlun_sa));
  5216. }
  5217. /*
  5218. * Now check the cache handling of the pci chipset.
  5219. */
  5220. if (sym_snooptest (np)) {
  5221. printf("%s: CACHE INCORRECTLY CONFIGURED.\n", sym_name(np));
  5222. goto attach_failed;
  5223. }
  5224. /*
  5225. * Sigh! we are done.
  5226. */
  5227. return 0;
  5228. attach_failed:
  5229. return -ENXIO;
  5230. }
  5231. /*
  5232. * Free everything that has been allocated for this device.
  5233. */
  5234. void sym_hcb_free(struct sym_hcb *np)
  5235. {
  5236. SYM_QUEHEAD *qp;
  5237. struct sym_ccb *cp;
  5238. struct sym_tcb *tp;
  5239. int target;
  5240. if (np->scriptz0)
  5241. sym_mfree_dma(np->scriptz0, np->scriptz_sz, "SCRIPTZ0");
  5242. if (np->scriptb0)
  5243. sym_mfree_dma(np->scriptb0, np->scriptb_sz, "SCRIPTB0");
  5244. if (np->scripta0)
  5245. sym_mfree_dma(np->scripta0, np->scripta_sz, "SCRIPTA0");
  5246. if (np->squeue)
  5247. sym_mfree_dma(np->squeue, sizeof(u32)*(MAX_QUEUE*2), "SQUEUE");
  5248. if (np->dqueue)
  5249. sym_mfree_dma(np->dqueue, sizeof(u32)*(MAX_QUEUE*2), "DQUEUE");
  5250. if (np->actccbs) {
  5251. while ((qp = sym_remque_head(&np->free_ccbq)) != NULL) {
  5252. cp = sym_que_entry(qp, struct sym_ccb, link_ccbq);
  5253. sym_mfree_dma(cp, sizeof(*cp), "CCB");
  5254. }
  5255. }
  5256. kfree(np->ccbh);
  5257. if (np->badluntbl)
  5258. sym_mfree_dma(np->badluntbl, 256,"BADLUNTBL");
  5259. for (target = 0; target < SYM_CONF_MAX_TARGET ; target++) {
  5260. tp = &np->target[target];
  5261. if (tp->luntbl)
  5262. sym_mfree_dma(tp->luntbl, 256, "LUNTBL");
  5263. #if SYM_CONF_MAX_LUN > 1
  5264. kfree(tp->lunmp);
  5265. #endif
  5266. }
  5267. if (np->targtbl)
  5268. sym_mfree_dma(np->targtbl, 256, "TARGTBL");
  5269. }