PageRenderTime 61ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/drivers/scsi/wd33c93.c

http://github.com/mirrors/linux
C | 2201 lines | 1435 code | 270 blank | 496 comment | 301 complexity | cf519db08d0123d3d265cbf546fba082 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 1996 John Shifflett, GeoLog Consulting
  4. * john@geolog.com
  5. * jshiffle@netcom.com
  6. */
  7. /*
  8. * Drew Eckhardt's excellent 'Generic NCR5380' sources from Linux-PC
  9. * provided much of the inspiration and some of the code for this
  10. * driver. Everything I know about Amiga DMA was gleaned from careful
  11. * reading of Hamish Mcdonald's original wd33c93 driver; in fact, I
  12. * borrowed shamelessly from all over that source. Thanks Hamish!
  13. *
  14. * _This_ driver is (I feel) an improvement over the old one in
  15. * several respects:
  16. *
  17. * - Target Disconnection/Reconnection is now supported. Any
  18. * system with more than one device active on the SCSI bus
  19. * will benefit from this. The driver defaults to what I
  20. * call 'adaptive disconnect' - meaning that each command
  21. * is evaluated individually as to whether or not it should
  22. * be run with the option to disconnect/reselect (if the
  23. * device chooses), or as a "SCSI-bus-hog".
  24. *
  25. * - Synchronous data transfers are now supported. Because of
  26. * a few devices that choke after telling the driver that
  27. * they can do sync transfers, we don't automatically use
  28. * this faster protocol - it can be enabled via the command-
  29. * line on a device-by-device basis.
  30. *
  31. * - Runtime operating parameters can now be specified through
  32. * the 'amiboot' or the 'insmod' command line. For amiboot do:
  33. * "amiboot [usual stuff] wd33c93=blah,blah,blah"
  34. * The defaults should be good for most people. See the comment
  35. * for 'setup_strings' below for more details.
  36. *
  37. * - The old driver relied exclusively on what the Western Digital
  38. * docs call "Combination Level 2 Commands", which are a great
  39. * idea in that the CPU is relieved of a lot of interrupt
  40. * overhead. However, by accepting a certain (user-settable)
  41. * amount of additional interrupts, this driver achieves
  42. * better control over the SCSI bus, and data transfers are
  43. * almost as fast while being much easier to define, track,
  44. * and debug.
  45. *
  46. *
  47. * TODO:
  48. * more speed. linked commands.
  49. *
  50. *
  51. * People with bug reports, wish-lists, complaints, comments,
  52. * or improvements are asked to pah-leeez email me (John Shifflett)
  53. * at john@geolog.com or jshiffle@netcom.com! I'm anxious to get
  54. * this thing into as good a shape as possible, and I'm positive
  55. * there are lots of lurking bugs and "Stupid Places".
  56. *
  57. * Updates:
  58. *
  59. * Added support for pre -A chips, which don't have advanced features
  60. * and will generate CSR_RESEL rather than CSR_RESEL_AM.
  61. * Richard Hirst <richard@sleepie.demon.co.uk> August 2000
  62. *
  63. * Added support for Burst Mode DMA and Fast SCSI. Enabled the use of
  64. * default_sx_per for asynchronous data transfers. Added adjustment
  65. * of transfer periods in sx_table to the actual input-clock.
  66. * peter fuerst <post@pfrst.de> February 2007
  67. */
  68. #include <linux/module.h>
  69. #include <linux/string.h>
  70. #include <linux/delay.h>
  71. #include <linux/init.h>
  72. #include <linux/interrupt.h>
  73. #include <linux/blkdev.h>
  74. #include <scsi/scsi.h>
  75. #include <scsi/scsi_cmnd.h>
  76. #include <scsi/scsi_device.h>
  77. #include <scsi/scsi_host.h>
  78. #include <asm/irq.h>
  79. #include "wd33c93.h"
  80. #define optimum_sx_per(hostdata) (hostdata)->sx_table[1].period_ns
  81. #define WD33C93_VERSION "1.26++"
  82. #define WD33C93_DATE "10/Feb/2007"
  83. MODULE_AUTHOR("John Shifflett");
  84. MODULE_DESCRIPTION("Generic WD33C93 SCSI driver");
  85. MODULE_LICENSE("GPL");
  86. /*
  87. * 'setup_strings' is a single string used to pass operating parameters and
  88. * settings from the kernel/module command-line to the driver. 'setup_args[]'
  89. * is an array of strings that define the compile-time default values for
  90. * these settings. If Linux boots with an amiboot or insmod command-line,
  91. * those settings are combined with 'setup_args[]'. Note that amiboot
  92. * command-lines are prefixed with "wd33c93=" while insmod uses a
  93. * "setup_strings=" prefix. The driver recognizes the following keywords
  94. * (lower case required) and arguments:
  95. *
  96. * - nosync:bitmask -bitmask is a byte where the 1st 7 bits correspond with
  97. * the 7 possible SCSI devices. Set a bit to negotiate for
  98. * asynchronous transfers on that device. To maintain
  99. * backwards compatibility, a command-line such as
  100. * "wd33c93=255" will be automatically translated to
  101. * "wd33c93=nosync:0xff".
  102. * - nodma:x -x = 1 to disable DMA, x = 0 to enable it. Argument is
  103. * optional - if not present, same as "nodma:1".
  104. * - period:ns -ns is the minimum # of nanoseconds in a SCSI data transfer
  105. * period. Default is 500; acceptable values are 250 - 1000.
  106. * - disconnect:x -x = 0 to never allow disconnects, 2 to always allow them.
  107. * x = 1 does 'adaptive' disconnects, which is the default
  108. * and generally the best choice.
  109. * - debug:x -If 'DEBUGGING_ON' is defined, x is a bit mask that causes
  110. * various types of debug output to printed - see the DB_xxx
  111. * defines in wd33c93.h
  112. * - clock:x -x = clock input in MHz for WD33c93 chip. Normal values
  113. * would be from 8 through 20. Default is 8.
  114. * - burst:x -x = 1 to use Burst Mode (or Demand-Mode) DMA, x = 0 to use
  115. * Single Byte DMA, which is the default. Argument is
  116. * optional - if not present, same as "burst:1".
  117. * - fast:x -x = 1 to enable Fast SCSI, which is only effective with
  118. * input-clock divisor 4 (WD33C93_FS_16_20), x = 0 to disable
  119. * it, which is the default. Argument is optional - if not
  120. * present, same as "fast:1".
  121. * - next -No argument. Used to separate blocks of keywords when
  122. * there's more than one host adapter in the system.
  123. *
  124. * Syntax Notes:
  125. * - Numeric arguments can be decimal or the '0x' form of hex notation. There
  126. * _must_ be a colon between a keyword and its numeric argument, with no
  127. * spaces.
  128. * - Keywords are separated by commas, no spaces, in the standard kernel
  129. * command-line manner.
  130. * - A keyword in the 'nth' comma-separated command-line member will overwrite
  131. * the 'nth' element of setup_args[]. A blank command-line member (in
  132. * other words, a comma with no preceding keyword) will _not_ overwrite
  133. * the corresponding setup_args[] element.
  134. * - If a keyword is used more than once, the first one applies to the first
  135. * SCSI host found, the second to the second card, etc, unless the 'next'
  136. * keyword is used to change the order.
  137. *
  138. * Some amiboot examples (for insmod, use 'setup_strings' instead of 'wd33c93'):
  139. * - wd33c93=nosync:255
  140. * - wd33c93=nodma
  141. * - wd33c93=nodma:1
  142. * - wd33c93=disconnect:2,nosync:0x08,period:250
  143. * - wd33c93=debug:0x1c
  144. */
  145. /* Normally, no defaults are specified */
  146. static char *setup_args[] = { "", "", "", "", "", "", "", "", "", "" };
  147. static char *setup_strings;
  148. module_param(setup_strings, charp, 0);
  149. static void wd33c93_execute(struct Scsi_Host *instance);
  150. #ifdef CONFIG_WD33C93_PIO
  151. static inline uchar
  152. read_wd33c93(const wd33c93_regs regs, uchar reg_num)
  153. {
  154. uchar data;
  155. outb(reg_num, regs.SASR);
  156. data = inb(regs.SCMD);
  157. return data;
  158. }
  159. static inline unsigned long
  160. read_wd33c93_count(const wd33c93_regs regs)
  161. {
  162. unsigned long value;
  163. outb(WD_TRANSFER_COUNT_MSB, regs.SASR);
  164. value = inb(regs.SCMD) << 16;
  165. value |= inb(regs.SCMD) << 8;
  166. value |= inb(regs.SCMD);
  167. return value;
  168. }
  169. static inline uchar
  170. read_aux_stat(const wd33c93_regs regs)
  171. {
  172. return inb(regs.SASR);
  173. }
  174. static inline void
  175. write_wd33c93(const wd33c93_regs regs, uchar reg_num, uchar value)
  176. {
  177. outb(reg_num, regs.SASR);
  178. outb(value, regs.SCMD);
  179. }
  180. static inline void
  181. write_wd33c93_count(const wd33c93_regs regs, unsigned long value)
  182. {
  183. outb(WD_TRANSFER_COUNT_MSB, regs.SASR);
  184. outb((value >> 16) & 0xff, regs.SCMD);
  185. outb((value >> 8) & 0xff, regs.SCMD);
  186. outb( value & 0xff, regs.SCMD);
  187. }
  188. #define write_wd33c93_cmd(regs, cmd) \
  189. write_wd33c93((regs), WD_COMMAND, (cmd))
  190. static inline void
  191. write_wd33c93_cdb(const wd33c93_regs regs, uint len, uchar cmnd[])
  192. {
  193. int i;
  194. outb(WD_CDB_1, regs.SASR);
  195. for (i=0; i<len; i++)
  196. outb(cmnd[i], regs.SCMD);
  197. }
  198. #else /* CONFIG_WD33C93_PIO */
  199. static inline uchar
  200. read_wd33c93(const wd33c93_regs regs, uchar reg_num)
  201. {
  202. *regs.SASR = reg_num;
  203. mb();
  204. return (*regs.SCMD);
  205. }
  206. static unsigned long
  207. read_wd33c93_count(const wd33c93_regs regs)
  208. {
  209. unsigned long value;
  210. *regs.SASR = WD_TRANSFER_COUNT_MSB;
  211. mb();
  212. value = *regs.SCMD << 16;
  213. value |= *regs.SCMD << 8;
  214. value |= *regs.SCMD;
  215. mb();
  216. return value;
  217. }
  218. static inline uchar
  219. read_aux_stat(const wd33c93_regs regs)
  220. {
  221. return *regs.SASR;
  222. }
  223. static inline void
  224. write_wd33c93(const wd33c93_regs regs, uchar reg_num, uchar value)
  225. {
  226. *regs.SASR = reg_num;
  227. mb();
  228. *regs.SCMD = value;
  229. mb();
  230. }
  231. static void
  232. write_wd33c93_count(const wd33c93_regs regs, unsigned long value)
  233. {
  234. *regs.SASR = WD_TRANSFER_COUNT_MSB;
  235. mb();
  236. *regs.SCMD = value >> 16;
  237. *regs.SCMD = value >> 8;
  238. *regs.SCMD = value;
  239. mb();
  240. }
  241. static inline void
  242. write_wd33c93_cmd(const wd33c93_regs regs, uchar cmd)
  243. {
  244. *regs.SASR = WD_COMMAND;
  245. mb();
  246. *regs.SCMD = cmd;
  247. mb();
  248. }
  249. static inline void
  250. write_wd33c93_cdb(const wd33c93_regs regs, uint len, uchar cmnd[])
  251. {
  252. int i;
  253. *regs.SASR = WD_CDB_1;
  254. for (i = 0; i < len; i++)
  255. *regs.SCMD = cmnd[i];
  256. }
  257. #endif /* CONFIG_WD33C93_PIO */
  258. static inline uchar
  259. read_1_byte(const wd33c93_regs regs)
  260. {
  261. uchar asr;
  262. uchar x = 0;
  263. write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
  264. write_wd33c93_cmd(regs, WD_CMD_TRANS_INFO | 0x80);
  265. do {
  266. asr = read_aux_stat(regs);
  267. if (asr & ASR_DBR)
  268. x = read_wd33c93(regs, WD_DATA);
  269. } while (!(asr & ASR_INT));
  270. return x;
  271. }
  272. static int
  273. round_period(unsigned int period, const struct sx_period *sx_table)
  274. {
  275. int x;
  276. for (x = 1; sx_table[x].period_ns; x++) {
  277. if ((period <= sx_table[x - 0].period_ns) &&
  278. (period > sx_table[x - 1].period_ns)) {
  279. return x;
  280. }
  281. }
  282. return 7;
  283. }
  284. /*
  285. * Calculate Synchronous Transfer Register value from SDTR code.
  286. */
  287. static uchar
  288. calc_sync_xfer(unsigned int period, unsigned int offset, unsigned int fast,
  289. const struct sx_period *sx_table)
  290. {
  291. /* When doing Fast SCSI synchronous data transfers, the corresponding
  292. * value in 'sx_table' is two times the actually used transfer period.
  293. */
  294. uchar result;
  295. if (offset && fast) {
  296. fast = STR_FSS;
  297. period *= 2;
  298. } else {
  299. fast = 0;
  300. }
  301. period *= 4; /* convert SDTR code to ns */
  302. result = sx_table[round_period(period,sx_table)].reg_value;
  303. result |= (offset < OPTIMUM_SX_OFF) ? offset : OPTIMUM_SX_OFF;
  304. result |= fast;
  305. return result;
  306. }
  307. /*
  308. * Calculate SDTR code bytes [3],[4] from period and offset.
  309. */
  310. static inline void
  311. calc_sync_msg(unsigned int period, unsigned int offset, unsigned int fast,
  312. uchar msg[2])
  313. {
  314. /* 'period' is a "normal"-mode value, like the ones in 'sx_table'. The
  315. * actually used transfer period for Fast SCSI synchronous data
  316. * transfers is half that value.
  317. */
  318. period /= 4;
  319. if (offset && fast)
  320. period /= 2;
  321. msg[0] = period;
  322. msg[1] = offset;
  323. }
  324. static int
  325. wd33c93_queuecommand_lck(struct scsi_cmnd *cmd,
  326. void (*done)(struct scsi_cmnd *))
  327. {
  328. struct WD33C93_hostdata *hostdata;
  329. struct scsi_cmnd *tmp;
  330. hostdata = (struct WD33C93_hostdata *) cmd->device->host->hostdata;
  331. DB(DB_QUEUE_COMMAND,
  332. printk("Q-%d-%02x( ", cmd->device->id, cmd->cmnd[0]))
  333. /* Set up a few fields in the scsi_cmnd structure for our own use:
  334. * - host_scribble is the pointer to the next cmd in the input queue
  335. * - scsi_done points to the routine we call when a cmd is finished
  336. * - result is what you'd expect
  337. */
  338. cmd->host_scribble = NULL;
  339. cmd->scsi_done = done;
  340. cmd->result = 0;
  341. /* We use the Scsi_Pointer structure that's included with each command
  342. * as a scratchpad (as it's intended to be used!). The handy thing about
  343. * the SCp.xxx fields is that they're always associated with a given
  344. * cmd, and are preserved across disconnect-reselect. This means we
  345. * can pretty much ignore SAVE_POINTERS and RESTORE_POINTERS messages
  346. * if we keep all the critical pointers and counters in SCp:
  347. * - SCp.ptr is the pointer into the RAM buffer
  348. * - SCp.this_residual is the size of that buffer
  349. * - SCp.buffer points to the current scatter-gather buffer
  350. * - SCp.buffers_residual tells us how many S.G. buffers there are
  351. * - SCp.have_data_in is not used
  352. * - SCp.sent_command is not used
  353. * - SCp.phase records this command's SRCID_ER bit setting
  354. */
  355. if (scsi_bufflen(cmd)) {
  356. cmd->SCp.buffer = scsi_sglist(cmd);
  357. cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
  358. cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
  359. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  360. } else {
  361. cmd->SCp.buffer = NULL;
  362. cmd->SCp.buffers_residual = 0;
  363. cmd->SCp.ptr = NULL;
  364. cmd->SCp.this_residual = 0;
  365. }
  366. /* WD docs state that at the conclusion of a "LEVEL2" command, the
  367. * status byte can be retrieved from the LUN register. Apparently,
  368. * this is the case only for *uninterrupted* LEVEL2 commands! If
  369. * there are any unexpected phases entered, even if they are 100%
  370. * legal (different devices may choose to do things differently),
  371. * the LEVEL2 command sequence is exited. This often occurs prior
  372. * to receiving the status byte, in which case the driver does a
  373. * status phase interrupt and gets the status byte on its own.
  374. * While such a command can then be "resumed" (ie restarted to
  375. * finish up as a LEVEL2 command), the LUN register will NOT be
  376. * a valid status byte at the command's conclusion, and we must
  377. * use the byte obtained during the earlier interrupt. Here, we
  378. * preset SCp.Status to an illegal value (0xff) so that when
  379. * this command finally completes, we can tell where the actual
  380. * status byte is stored.
  381. */
  382. cmd->SCp.Status = ILLEGAL_STATUS_BYTE;
  383. /*
  384. * Add the cmd to the end of 'input_Q'. Note that REQUEST SENSE
  385. * commands are added to the head of the queue so that the desired
  386. * sense data is not lost before REQUEST_SENSE executes.
  387. */
  388. spin_lock_irq(&hostdata->lock);
  389. if (!(hostdata->input_Q) || (cmd->cmnd[0] == REQUEST_SENSE)) {
  390. cmd->host_scribble = (uchar *) hostdata->input_Q;
  391. hostdata->input_Q = cmd;
  392. } else { /* find the end of the queue */
  393. for (tmp = (struct scsi_cmnd *) hostdata->input_Q;
  394. tmp->host_scribble;
  395. tmp = (struct scsi_cmnd *) tmp->host_scribble) ;
  396. tmp->host_scribble = (uchar *) cmd;
  397. }
  398. /* We know that there's at least one command in 'input_Q' now.
  399. * Go see if any of them are runnable!
  400. */
  401. wd33c93_execute(cmd->device->host);
  402. DB(DB_QUEUE_COMMAND, printk(")Q "))
  403. spin_unlock_irq(&hostdata->lock);
  404. return 0;
  405. }
  406. DEF_SCSI_QCMD(wd33c93_queuecommand)
  407. /*
  408. * This routine attempts to start a scsi command. If the host_card is
  409. * already connected, we give up immediately. Otherwise, look through
  410. * the input_Q, using the first command we find that's intended
  411. * for a currently non-busy target/lun.
  412. *
  413. * wd33c93_execute() is always called with interrupts disabled or from
  414. * the wd33c93_intr itself, which means that a wd33c93 interrupt
  415. * cannot occur while we are in here.
  416. */
  417. static void
  418. wd33c93_execute(struct Scsi_Host *instance)
  419. {
  420. struct WD33C93_hostdata *hostdata =
  421. (struct WD33C93_hostdata *) instance->hostdata;
  422. const wd33c93_regs regs = hostdata->regs;
  423. struct scsi_cmnd *cmd, *prev;
  424. DB(DB_EXECUTE, printk("EX("))
  425. if (hostdata->selecting || hostdata->connected) {
  426. DB(DB_EXECUTE, printk(")EX-0 "))
  427. return;
  428. }
  429. /*
  430. * Search through the input_Q for a command destined
  431. * for an idle target/lun.
  432. */
  433. cmd = (struct scsi_cmnd *) hostdata->input_Q;
  434. prev = NULL;
  435. while (cmd) {
  436. if (!(hostdata->busy[cmd->device->id] &
  437. (1 << (cmd->device->lun & 0xff))))
  438. break;
  439. prev = cmd;
  440. cmd = (struct scsi_cmnd *) cmd->host_scribble;
  441. }
  442. /* quit if queue empty or all possible targets are busy */
  443. if (!cmd) {
  444. DB(DB_EXECUTE, printk(")EX-1 "))
  445. return;
  446. }
  447. /* remove command from queue */
  448. if (prev)
  449. prev->host_scribble = cmd->host_scribble;
  450. else
  451. hostdata->input_Q = (struct scsi_cmnd *) cmd->host_scribble;
  452. #ifdef PROC_STATISTICS
  453. hostdata->cmd_cnt[cmd->device->id]++;
  454. #endif
  455. /*
  456. * Start the selection process
  457. */
  458. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  459. write_wd33c93(regs, WD_DESTINATION_ID, cmd->device->id);
  460. else
  461. write_wd33c93(regs, WD_DESTINATION_ID, cmd->device->id | DSTID_DPD);
  462. /* Now we need to figure out whether or not this command is a good
  463. * candidate for disconnect/reselect. We guess to the best of our
  464. * ability, based on a set of hierarchical rules. When several
  465. * devices are operating simultaneously, disconnects are usually
  466. * an advantage. In a single device system, or if only 1 device
  467. * is being accessed, transfers usually go faster if disconnects
  468. * are not allowed:
  469. *
  470. * + Commands should NEVER disconnect if hostdata->disconnect =
  471. * DIS_NEVER (this holds for tape drives also), and ALWAYS
  472. * disconnect if hostdata->disconnect = DIS_ALWAYS.
  473. * + Tape drive commands should always be allowed to disconnect.
  474. * + Disconnect should be allowed if disconnected_Q isn't empty.
  475. * + Commands should NOT disconnect if input_Q is empty.
  476. * + Disconnect should be allowed if there are commands in input_Q
  477. * for a different target/lun. In this case, the other commands
  478. * should be made disconnect-able, if not already.
  479. *
  480. * I know, I know - this code would flunk me out of any
  481. * "C Programming 101" class ever offered. But it's easy
  482. * to change around and experiment with for now.
  483. */
  484. cmd->SCp.phase = 0; /* assume no disconnect */
  485. if (hostdata->disconnect == DIS_NEVER)
  486. goto no;
  487. if (hostdata->disconnect == DIS_ALWAYS)
  488. goto yes;
  489. if (cmd->device->type == 1) /* tape drive? */
  490. goto yes;
  491. if (hostdata->disconnected_Q) /* other commands disconnected? */
  492. goto yes;
  493. if (!(hostdata->input_Q)) /* input_Q empty? */
  494. goto no;
  495. for (prev = (struct scsi_cmnd *) hostdata->input_Q; prev;
  496. prev = (struct scsi_cmnd *) prev->host_scribble) {
  497. if ((prev->device->id != cmd->device->id) ||
  498. (prev->device->lun != cmd->device->lun)) {
  499. for (prev = (struct scsi_cmnd *) hostdata->input_Q; prev;
  500. prev = (struct scsi_cmnd *) prev->host_scribble)
  501. prev->SCp.phase = 1;
  502. goto yes;
  503. }
  504. }
  505. goto no;
  506. yes:
  507. cmd->SCp.phase = 1;
  508. #ifdef PROC_STATISTICS
  509. hostdata->disc_allowed_cnt[cmd->device->id]++;
  510. #endif
  511. no:
  512. write_wd33c93(regs, WD_SOURCE_ID, ((cmd->SCp.phase) ? SRCID_ER : 0));
  513. write_wd33c93(regs, WD_TARGET_LUN, (u8)cmd->device->lun);
  514. write_wd33c93(regs, WD_SYNCHRONOUS_TRANSFER,
  515. hostdata->sync_xfer[cmd->device->id]);
  516. hostdata->busy[cmd->device->id] |= (1 << (cmd->device->lun & 0xFF));
  517. if ((hostdata->level2 == L2_NONE) ||
  518. (hostdata->sync_stat[cmd->device->id] == SS_UNSET)) {
  519. /*
  520. * Do a 'Select-With-ATN' command. This will end with
  521. * one of the following interrupts:
  522. * CSR_RESEL_AM: failure - can try again later.
  523. * CSR_TIMEOUT: failure - give up.
  524. * CSR_SELECT: success - proceed.
  525. */
  526. hostdata->selecting = cmd;
  527. /* Every target has its own synchronous transfer setting, kept in the
  528. * sync_xfer array, and a corresponding status byte in sync_stat[].
  529. * Each target's sync_stat[] entry is initialized to SX_UNSET, and its
  530. * sync_xfer[] entry is initialized to the default/safe value. SS_UNSET
  531. * means that the parameters are undetermined as yet, and that we
  532. * need to send an SDTR message to this device after selection is
  533. * complete: We set SS_FIRST to tell the interrupt routine to do so.
  534. * If we've been asked not to try synchronous transfers on this
  535. * target (and _all_ luns within it), we'll still send the SDTR message
  536. * later, but at that time we'll negotiate for async by specifying a
  537. * sync fifo depth of 0.
  538. */
  539. if (hostdata->sync_stat[cmd->device->id] == SS_UNSET)
  540. hostdata->sync_stat[cmd->device->id] = SS_FIRST;
  541. hostdata->state = S_SELECTING;
  542. write_wd33c93_count(regs, 0); /* guarantee a DATA_PHASE interrupt */
  543. write_wd33c93_cmd(regs, WD_CMD_SEL_ATN);
  544. } else {
  545. /*
  546. * Do a 'Select-With-ATN-Xfer' command. This will end with
  547. * one of the following interrupts:
  548. * CSR_RESEL_AM: failure - can try again later.
  549. * CSR_TIMEOUT: failure - give up.
  550. * anything else: success - proceed.
  551. */
  552. hostdata->connected = cmd;
  553. write_wd33c93(regs, WD_COMMAND_PHASE, 0);
  554. /* copy command_descriptor_block into WD chip
  555. * (take advantage of auto-incrementing)
  556. */
  557. write_wd33c93_cdb(regs, cmd->cmd_len, cmd->cmnd);
  558. /* The wd33c93 only knows about Group 0, 1, and 5 commands when
  559. * it's doing a 'select-and-transfer'. To be safe, we write the
  560. * size of the CDB into the OWN_ID register for every case. This
  561. * way there won't be problems with vendor-unique, audio, etc.
  562. */
  563. write_wd33c93(regs, WD_OWN_ID, cmd->cmd_len);
  564. /* When doing a non-disconnect command with DMA, we can save
  565. * ourselves a DATA phase interrupt later by setting everything
  566. * up ahead of time.
  567. */
  568. if ((cmd->SCp.phase == 0) && (hostdata->no_dma == 0)) {
  569. if (hostdata->dma_setup(cmd,
  570. (cmd->sc_data_direction == DMA_TO_DEVICE) ?
  571. DATA_OUT_DIR : DATA_IN_DIR))
  572. write_wd33c93_count(regs, 0); /* guarantee a DATA_PHASE interrupt */
  573. else {
  574. write_wd33c93_count(regs,
  575. cmd->SCp.this_residual);
  576. write_wd33c93(regs, WD_CONTROL,
  577. CTRL_IDI | CTRL_EDI | hostdata->dma_mode);
  578. hostdata->dma = D_DMA_RUNNING;
  579. }
  580. } else
  581. write_wd33c93_count(regs, 0); /* guarantee a DATA_PHASE interrupt */
  582. hostdata->state = S_RUNNING_LEVEL2;
  583. write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
  584. }
  585. /*
  586. * Since the SCSI bus can handle only 1 connection at a time,
  587. * we get out of here now. If the selection fails, or when
  588. * the command disconnects, we'll come back to this routine
  589. * to search the input_Q again...
  590. */
  591. DB(DB_EXECUTE,
  592. printk("%s)EX-2 ", (cmd->SCp.phase) ? "d:" : ""))
  593. }
  594. static void
  595. transfer_pio(const wd33c93_regs regs, uchar * buf, int cnt,
  596. int data_in_dir, struct WD33C93_hostdata *hostdata)
  597. {
  598. uchar asr;
  599. DB(DB_TRANSFER,
  600. printk("(%p,%d,%s:", buf, cnt, data_in_dir ? "in" : "out"))
  601. write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
  602. write_wd33c93_count(regs, cnt);
  603. write_wd33c93_cmd(regs, WD_CMD_TRANS_INFO);
  604. if (data_in_dir) {
  605. do {
  606. asr = read_aux_stat(regs);
  607. if (asr & ASR_DBR)
  608. *buf++ = read_wd33c93(regs, WD_DATA);
  609. } while (!(asr & ASR_INT));
  610. } else {
  611. do {
  612. asr = read_aux_stat(regs);
  613. if (asr & ASR_DBR)
  614. write_wd33c93(regs, WD_DATA, *buf++);
  615. } while (!(asr & ASR_INT));
  616. }
  617. /* Note: we are returning with the interrupt UN-cleared.
  618. * Since (presumably) an entire I/O operation has
  619. * completed, the bus phase is probably different, and
  620. * the interrupt routine will discover this when it
  621. * responds to the uncleared int.
  622. */
  623. }
  624. static void
  625. transfer_bytes(const wd33c93_regs regs, struct scsi_cmnd *cmd,
  626. int data_in_dir)
  627. {
  628. struct WD33C93_hostdata *hostdata;
  629. unsigned long length;
  630. hostdata = (struct WD33C93_hostdata *) cmd->device->host->hostdata;
  631. /* Normally, you'd expect 'this_residual' to be non-zero here.
  632. * In a series of scatter-gather transfers, however, this
  633. * routine will usually be called with 'this_residual' equal
  634. * to 0 and 'buffers_residual' non-zero. This means that a
  635. * previous transfer completed, clearing 'this_residual', and
  636. * now we need to setup the next scatter-gather buffer as the
  637. * source or destination for THIS transfer.
  638. */
  639. if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
  640. cmd->SCp.buffer = sg_next(cmd->SCp.buffer);
  641. --cmd->SCp.buffers_residual;
  642. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  643. cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
  644. }
  645. if (!cmd->SCp.this_residual) /* avoid bogus setups */
  646. return;
  647. write_wd33c93(regs, WD_SYNCHRONOUS_TRANSFER,
  648. hostdata->sync_xfer[cmd->device->id]);
  649. /* 'hostdata->no_dma' is TRUE if we don't even want to try DMA.
  650. * Update 'this_residual' and 'ptr' after 'transfer_pio()' returns.
  651. */
  652. if (hostdata->no_dma || hostdata->dma_setup(cmd, data_in_dir)) {
  653. #ifdef PROC_STATISTICS
  654. hostdata->pio_cnt++;
  655. #endif
  656. transfer_pio(regs, (uchar *) cmd->SCp.ptr,
  657. cmd->SCp.this_residual, data_in_dir, hostdata);
  658. length = cmd->SCp.this_residual;
  659. cmd->SCp.this_residual = read_wd33c93_count(regs);
  660. cmd->SCp.ptr += (length - cmd->SCp.this_residual);
  661. }
  662. /* We are able to do DMA (in fact, the Amiga hardware is
  663. * already going!), so start up the wd33c93 in DMA mode.
  664. * We set 'hostdata->dma' = D_DMA_RUNNING so that when the
  665. * transfer completes and causes an interrupt, we're
  666. * reminded to tell the Amiga to shut down its end. We'll
  667. * postpone the updating of 'this_residual' and 'ptr'
  668. * until then.
  669. */
  670. else {
  671. #ifdef PROC_STATISTICS
  672. hostdata->dma_cnt++;
  673. #endif
  674. write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | hostdata->dma_mode);
  675. write_wd33c93_count(regs, cmd->SCp.this_residual);
  676. if ((hostdata->level2 >= L2_DATA) ||
  677. (hostdata->level2 == L2_BASIC && cmd->SCp.phase == 0)) {
  678. write_wd33c93(regs, WD_COMMAND_PHASE, 0x45);
  679. write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
  680. hostdata->state = S_RUNNING_LEVEL2;
  681. } else
  682. write_wd33c93_cmd(regs, WD_CMD_TRANS_INFO);
  683. hostdata->dma = D_DMA_RUNNING;
  684. }
  685. }
  686. void
  687. wd33c93_intr(struct Scsi_Host *instance)
  688. {
  689. struct WD33C93_hostdata *hostdata =
  690. (struct WD33C93_hostdata *) instance->hostdata;
  691. const wd33c93_regs regs = hostdata->regs;
  692. struct scsi_cmnd *patch, *cmd;
  693. uchar asr, sr, phs, id, lun, *ucp, msg;
  694. unsigned long length, flags;
  695. asr = read_aux_stat(regs);
  696. if (!(asr & ASR_INT) || (asr & ASR_BSY))
  697. return;
  698. spin_lock_irqsave(&hostdata->lock, flags);
  699. #ifdef PROC_STATISTICS
  700. hostdata->int_cnt++;
  701. #endif
  702. cmd = (struct scsi_cmnd *) hostdata->connected; /* assume we're connected */
  703. sr = read_wd33c93(regs, WD_SCSI_STATUS); /* clear the interrupt */
  704. phs = read_wd33c93(regs, WD_COMMAND_PHASE);
  705. DB(DB_INTR, printk("{%02x:%02x-", asr, sr))
  706. /* After starting a DMA transfer, the next interrupt
  707. * is guaranteed to be in response to completion of
  708. * the transfer. Since the Amiga DMA hardware runs in
  709. * in an open-ended fashion, it needs to be told when
  710. * to stop; do that here if D_DMA_RUNNING is true.
  711. * Also, we have to update 'this_residual' and 'ptr'
  712. * based on the contents of the TRANSFER_COUNT register,
  713. * in case the device decided to do an intermediate
  714. * disconnect (a device may do this if it has to do a
  715. * seek, or just to be nice and let other devices have
  716. * some bus time during long transfers). After doing
  717. * whatever is needed, we go on and service the WD3393
  718. * interrupt normally.
  719. */
  720. if (hostdata->dma == D_DMA_RUNNING) {
  721. DB(DB_TRANSFER,
  722. printk("[%p/%d:", cmd->SCp.ptr, cmd->SCp.this_residual))
  723. hostdata->dma_stop(cmd->device->host, cmd, 1);
  724. hostdata->dma = D_DMA_OFF;
  725. length = cmd->SCp.this_residual;
  726. cmd->SCp.this_residual = read_wd33c93_count(regs);
  727. cmd->SCp.ptr += (length - cmd->SCp.this_residual);
  728. DB(DB_TRANSFER,
  729. printk("%p/%d]", cmd->SCp.ptr, cmd->SCp.this_residual))
  730. }
  731. /* Respond to the specific WD3393 interrupt - there are quite a few! */
  732. switch (sr) {
  733. case CSR_TIMEOUT:
  734. DB(DB_INTR, printk("TIMEOUT"))
  735. if (hostdata->state == S_RUNNING_LEVEL2)
  736. hostdata->connected = NULL;
  737. else {
  738. cmd = (struct scsi_cmnd *) hostdata->selecting; /* get a valid cmd */
  739. hostdata->selecting = NULL;
  740. }
  741. cmd->result = DID_NO_CONNECT << 16;
  742. hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xff));
  743. hostdata->state = S_UNCONNECTED;
  744. cmd->scsi_done(cmd);
  745. /* From esp.c:
  746. * There is a window of time within the scsi_done() path
  747. * of execution where interrupts are turned back on full
  748. * blast and left that way. During that time we could
  749. * reconnect to a disconnected command, then we'd bomb
  750. * out below. We could also end up executing two commands
  751. * at _once_. ...just so you know why the restore_flags()
  752. * is here...
  753. */
  754. spin_unlock_irqrestore(&hostdata->lock, flags);
  755. /* We are not connected to a target - check to see if there
  756. * are commands waiting to be executed.
  757. */
  758. wd33c93_execute(instance);
  759. break;
  760. /* Note: this interrupt should not occur in a LEVEL2 command */
  761. case CSR_SELECT:
  762. DB(DB_INTR, printk("SELECT"))
  763. hostdata->connected = cmd =
  764. (struct scsi_cmnd *) hostdata->selecting;
  765. hostdata->selecting = NULL;
  766. /* construct an IDENTIFY message with correct disconnect bit */
  767. hostdata->outgoing_msg[0] = IDENTIFY(0, cmd->device->lun);
  768. if (cmd->SCp.phase)
  769. hostdata->outgoing_msg[0] |= 0x40;
  770. if (hostdata->sync_stat[cmd->device->id] == SS_FIRST) {
  771. hostdata->sync_stat[cmd->device->id] = SS_WAITING;
  772. /* Tack on a 2nd message to ask about synchronous transfers. If we've
  773. * been asked to do only asynchronous transfers on this device, we
  774. * request a fifo depth of 0, which is equivalent to async - should
  775. * solve the problems some people have had with GVP's Guru ROM.
  776. */
  777. hostdata->outgoing_msg[1] = EXTENDED_MESSAGE;
  778. hostdata->outgoing_msg[2] = 3;
  779. hostdata->outgoing_msg[3] = EXTENDED_SDTR;
  780. if (hostdata->no_sync & (1 << cmd->device->id)) {
  781. calc_sync_msg(hostdata->default_sx_per, 0,
  782. 0, hostdata->outgoing_msg + 4);
  783. } else {
  784. calc_sync_msg(optimum_sx_per(hostdata),
  785. OPTIMUM_SX_OFF,
  786. hostdata->fast,
  787. hostdata->outgoing_msg + 4);
  788. }
  789. hostdata->outgoing_len = 6;
  790. #ifdef SYNC_DEBUG
  791. ucp = hostdata->outgoing_msg + 1;
  792. printk(" sending SDTR %02x03%02x%02x%02x ",
  793. ucp[0], ucp[2], ucp[3], ucp[4]);
  794. #endif
  795. } else
  796. hostdata->outgoing_len = 1;
  797. hostdata->state = S_CONNECTED;
  798. spin_unlock_irqrestore(&hostdata->lock, flags);
  799. break;
  800. case CSR_XFER_DONE | PHS_DATA_IN:
  801. case CSR_UNEXP | PHS_DATA_IN:
  802. case CSR_SRV_REQ | PHS_DATA_IN:
  803. DB(DB_INTR,
  804. printk("IN-%d.%d", cmd->SCp.this_residual,
  805. cmd->SCp.buffers_residual))
  806. transfer_bytes(regs, cmd, DATA_IN_DIR);
  807. if (hostdata->state != S_RUNNING_LEVEL2)
  808. hostdata->state = S_CONNECTED;
  809. spin_unlock_irqrestore(&hostdata->lock, flags);
  810. break;
  811. case CSR_XFER_DONE | PHS_DATA_OUT:
  812. case CSR_UNEXP | PHS_DATA_OUT:
  813. case CSR_SRV_REQ | PHS_DATA_OUT:
  814. DB(DB_INTR,
  815. printk("OUT-%d.%d", cmd->SCp.this_residual,
  816. cmd->SCp.buffers_residual))
  817. transfer_bytes(regs, cmd, DATA_OUT_DIR);
  818. if (hostdata->state != S_RUNNING_LEVEL2)
  819. hostdata->state = S_CONNECTED;
  820. spin_unlock_irqrestore(&hostdata->lock, flags);
  821. break;
  822. /* Note: this interrupt should not occur in a LEVEL2 command */
  823. case CSR_XFER_DONE | PHS_COMMAND:
  824. case CSR_UNEXP | PHS_COMMAND:
  825. case CSR_SRV_REQ | PHS_COMMAND:
  826. DB(DB_INTR, printk("CMND-%02x", cmd->cmnd[0]))
  827. transfer_pio(regs, cmd->cmnd, cmd->cmd_len, DATA_OUT_DIR,
  828. hostdata);
  829. hostdata->state = S_CONNECTED;
  830. spin_unlock_irqrestore(&hostdata->lock, flags);
  831. break;
  832. case CSR_XFER_DONE | PHS_STATUS:
  833. case CSR_UNEXP | PHS_STATUS:
  834. case CSR_SRV_REQ | PHS_STATUS:
  835. DB(DB_INTR, printk("STATUS="))
  836. cmd->SCp.Status = read_1_byte(regs);
  837. DB(DB_INTR, printk("%02x", cmd->SCp.Status))
  838. if (hostdata->level2 >= L2_BASIC) {
  839. sr = read_wd33c93(regs, WD_SCSI_STATUS); /* clear interrupt */
  840. udelay(7);
  841. hostdata->state = S_RUNNING_LEVEL2;
  842. write_wd33c93(regs, WD_COMMAND_PHASE, 0x50);
  843. write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
  844. } else {
  845. hostdata->state = S_CONNECTED;
  846. }
  847. spin_unlock_irqrestore(&hostdata->lock, flags);
  848. break;
  849. case CSR_XFER_DONE | PHS_MESS_IN:
  850. case CSR_UNEXP | PHS_MESS_IN:
  851. case CSR_SRV_REQ | PHS_MESS_IN:
  852. DB(DB_INTR, printk("MSG_IN="))
  853. msg = read_1_byte(regs);
  854. sr = read_wd33c93(regs, WD_SCSI_STATUS); /* clear interrupt */
  855. udelay(7);
  856. hostdata->incoming_msg[hostdata->incoming_ptr] = msg;
  857. if (hostdata->incoming_msg[0] == EXTENDED_MESSAGE)
  858. msg = EXTENDED_MESSAGE;
  859. else
  860. hostdata->incoming_ptr = 0;
  861. cmd->SCp.Message = msg;
  862. switch (msg) {
  863. case COMMAND_COMPLETE:
  864. DB(DB_INTR, printk("CCMP"))
  865. write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
  866. hostdata->state = S_PRE_CMP_DISC;
  867. break;
  868. case SAVE_POINTERS:
  869. DB(DB_INTR, printk("SDP"))
  870. write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
  871. hostdata->state = S_CONNECTED;
  872. break;
  873. case RESTORE_POINTERS:
  874. DB(DB_INTR, printk("RDP"))
  875. if (hostdata->level2 >= L2_BASIC) {
  876. write_wd33c93(regs, WD_COMMAND_PHASE, 0x45);
  877. write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
  878. hostdata->state = S_RUNNING_LEVEL2;
  879. } else {
  880. write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
  881. hostdata->state = S_CONNECTED;
  882. }
  883. break;
  884. case DISCONNECT:
  885. DB(DB_INTR, printk("DIS"))
  886. cmd->device->disconnect = 1;
  887. write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
  888. hostdata->state = S_PRE_TMP_DISC;
  889. break;
  890. case MESSAGE_REJECT:
  891. DB(DB_INTR, printk("REJ"))
  892. #ifdef SYNC_DEBUG
  893. printk("-REJ-");
  894. #endif
  895. if (hostdata->sync_stat[cmd->device->id] == SS_WAITING) {
  896. hostdata->sync_stat[cmd->device->id] = SS_SET;
  897. /* we want default_sx_per, not DEFAULT_SX_PER */
  898. hostdata->sync_xfer[cmd->device->id] =
  899. calc_sync_xfer(hostdata->default_sx_per
  900. / 4, 0, 0, hostdata->sx_table);
  901. }
  902. write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
  903. hostdata->state = S_CONNECTED;
  904. break;
  905. case EXTENDED_MESSAGE:
  906. DB(DB_INTR, printk("EXT"))
  907. ucp = hostdata->incoming_msg;
  908. #ifdef SYNC_DEBUG
  909. printk("%02x", ucp[hostdata->incoming_ptr]);
  910. #endif
  911. /* Is this the last byte of the extended message? */
  912. if ((hostdata->incoming_ptr >= 2) &&
  913. (hostdata->incoming_ptr == (ucp[1] + 1))) {
  914. switch (ucp[2]) { /* what's the EXTENDED code? */
  915. case EXTENDED_SDTR:
  916. /* default to default async period */
  917. id = calc_sync_xfer(hostdata->
  918. default_sx_per / 4, 0,
  919. 0, hostdata->sx_table);
  920. if (hostdata->sync_stat[cmd->device->id] !=
  921. SS_WAITING) {
  922. /* A device has sent an unsolicited SDTR message; rather than go
  923. * through the effort of decoding it and then figuring out what
  924. * our reply should be, we're just gonna say that we have a
  925. * synchronous fifo depth of 0. This will result in asynchronous
  926. * transfers - not ideal but so much easier.
  927. * Actually, this is OK because it assures us that if we don't
  928. * specifically ask for sync transfers, we won't do any.
  929. */
  930. write_wd33c93_cmd(regs, WD_CMD_ASSERT_ATN); /* want MESS_OUT */
  931. hostdata->outgoing_msg[0] =
  932. EXTENDED_MESSAGE;
  933. hostdata->outgoing_msg[1] = 3;
  934. hostdata->outgoing_msg[2] =
  935. EXTENDED_SDTR;
  936. calc_sync_msg(hostdata->
  937. default_sx_per, 0,
  938. 0, hostdata->outgoing_msg + 3);
  939. hostdata->outgoing_len = 5;
  940. } else {
  941. if (ucp[4]) /* well, sync transfer */
  942. id = calc_sync_xfer(ucp[3], ucp[4],
  943. hostdata->fast,
  944. hostdata->sx_table);
  945. else if (ucp[3]) /* very unlikely... */
  946. id = calc_sync_xfer(ucp[3], ucp[4],
  947. 0, hostdata->sx_table);
  948. }
  949. hostdata->sync_xfer[cmd->device->id] = id;
  950. #ifdef SYNC_DEBUG
  951. printk(" sync_xfer=%02x\n",
  952. hostdata->sync_xfer[cmd->device->id]);
  953. #endif
  954. hostdata->sync_stat[cmd->device->id] =
  955. SS_SET;
  956. write_wd33c93_cmd(regs,
  957. WD_CMD_NEGATE_ACK);
  958. hostdata->state = S_CONNECTED;
  959. break;
  960. case EXTENDED_WDTR:
  961. write_wd33c93_cmd(regs, WD_CMD_ASSERT_ATN); /* want MESS_OUT */
  962. printk("sending WDTR ");
  963. hostdata->outgoing_msg[0] =
  964. EXTENDED_MESSAGE;
  965. hostdata->outgoing_msg[1] = 2;
  966. hostdata->outgoing_msg[2] =
  967. EXTENDED_WDTR;
  968. hostdata->outgoing_msg[3] = 0; /* 8 bit transfer width */
  969. hostdata->outgoing_len = 4;
  970. write_wd33c93_cmd(regs,
  971. WD_CMD_NEGATE_ACK);
  972. hostdata->state = S_CONNECTED;
  973. break;
  974. default:
  975. write_wd33c93_cmd(regs, WD_CMD_ASSERT_ATN); /* want MESS_OUT */
  976. printk
  977. ("Rejecting Unknown Extended Message(%02x). ",
  978. ucp[2]);
  979. hostdata->outgoing_msg[0] =
  980. MESSAGE_REJECT;
  981. hostdata->outgoing_len = 1;
  982. write_wd33c93_cmd(regs,
  983. WD_CMD_NEGATE_ACK);
  984. hostdata->state = S_CONNECTED;
  985. break;
  986. }
  987. hostdata->incoming_ptr = 0;
  988. }
  989. /* We need to read more MESS_IN bytes for the extended message */
  990. else {
  991. hostdata->incoming_ptr++;
  992. write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
  993. hostdata->state = S_CONNECTED;
  994. }
  995. break;
  996. default:
  997. printk("Rejecting Unknown Message(%02x) ", msg);
  998. write_wd33c93_cmd(regs, WD_CMD_ASSERT_ATN); /* want MESS_OUT */
  999. hostdata->outgoing_msg[0] = MESSAGE_REJECT;
  1000. hostdata->outgoing_len = 1;
  1001. write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
  1002. hostdata->state = S_CONNECTED;
  1003. }
  1004. spin_unlock_irqrestore(&hostdata->lock, flags);
  1005. break;
  1006. /* Note: this interrupt will occur only after a LEVEL2 command */
  1007. case CSR_SEL_XFER_DONE:
  1008. /* Make sure that reselection is enabled at this point - it may
  1009. * have been turned off for the command that just completed.
  1010. */
  1011. write_wd33c93(regs, WD_SOURCE_ID, SRCID_ER);
  1012. if (phs == 0x60) {
  1013. DB(DB_INTR, printk("SX-DONE"))
  1014. cmd->SCp.Message = COMMAND_COMPLETE;
  1015. lun = read_wd33c93(regs, WD_TARGET_LUN);
  1016. DB(DB_INTR, printk(":%d.%d", cmd->SCp.Status, lun))
  1017. hostdata->connected = NULL;
  1018. hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xff));
  1019. hostdata->state = S_UNCONNECTED;
  1020. if (cmd->SCp.Status == ILLEGAL_STATUS_BYTE)
  1021. cmd->SCp.Status = lun;
  1022. if (cmd->cmnd[0] == REQUEST_SENSE
  1023. && cmd->SCp.Status != GOOD)
  1024. cmd->result =
  1025. (cmd->
  1026. result & 0x00ffff) | (DID_ERROR << 16);
  1027. else
  1028. cmd->result =
  1029. cmd->SCp.Status | (cmd->SCp.Message << 8);
  1030. cmd->scsi_done(cmd);
  1031. /* We are no longer connected to a target - check to see if
  1032. * there are commands waiting to be executed.
  1033. */
  1034. spin_unlock_irqrestore(&hostdata->lock, flags);
  1035. wd33c93_execute(instance);
  1036. } else {
  1037. printk
  1038. ("%02x:%02x:%02x: Unknown SEL_XFER_DONE phase!!---",
  1039. asr, sr, phs);
  1040. spin_unlock_irqrestore(&hostdata->lock, flags);
  1041. }
  1042. break;
  1043. /* Note: this interrupt will occur only after a LEVEL2 command */
  1044. case CSR_SDP:
  1045. DB(DB_INTR, printk("SDP"))
  1046. hostdata->state = S_RUNNING_LEVEL2;
  1047. write_wd33c93(regs, WD_COMMAND_PHASE, 0x41);
  1048. write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
  1049. spin_unlock_irqrestore(&hostdata->lock, flags);
  1050. break;
  1051. case CSR_XFER_DONE | PHS_MESS_OUT:
  1052. case CSR_UNEXP | PHS_MESS_OUT:
  1053. case CSR_SRV_REQ | PHS_MESS_OUT:
  1054. DB(DB_INTR, printk("MSG_OUT="))
  1055. /* To get here, we've probably requested MESSAGE_OUT and have
  1056. * already put the correct bytes in outgoing_msg[] and filled
  1057. * in outgoing_len. We simply send them out to the SCSI bus.
  1058. * Sometimes we get MESSAGE_OUT phase when we're not expecting
  1059. * it - like when our SDTR message is rejected by a target. Some
  1060. * targets send the REJECT before receiving all of the extended
  1061. * message, and then seem to go back to MESSAGE_OUT for a byte
  1062. * or two. Not sure why, or if I'm doing something wrong to
  1063. * cause this to happen. Regardless, it seems that sending
  1064. * NOP messages in these situations results in no harm and
  1065. * makes everyone happy.
  1066. */
  1067. if (hostdata->outgoing_len == 0) {
  1068. hostdata->outgoing_len = 1;
  1069. hostdata->outgoing_msg[0] = NOP;
  1070. }
  1071. transfer_pio(regs, hostdata->outgoing_msg,
  1072. hostdata->outgoing_len, DATA_OUT_DIR, hostdata);
  1073. DB(DB_INTR, printk("%02x", hostdata->outgoing_msg[0]))
  1074. hostdata->outgoing_len = 0;
  1075. hostdata->state = S_CONNECTED;
  1076. spin_unlock_irqrestore(&hostdata->lock, flags);
  1077. break;
  1078. case CSR_UNEXP_DISC:
  1079. /* I think I've seen this after a request-sense that was in response
  1080. * to an error condition, but not sure. We certainly need to do
  1081. * something when we get this interrupt - the question is 'what?'.
  1082. * Let's think positively, and assume some command has finished
  1083. * in a legal manner (like a command that provokes a request-sense),
  1084. * so we treat it as a normal command-complete-disconnect.
  1085. */
  1086. /* Make sure that reselection is enabled at this point - it may
  1087. * have been turned off for the command that just completed.
  1088. */
  1089. write_wd33c93(regs, WD_SOURCE_ID, SRCID_ER);
  1090. if (cmd == NULL) {
  1091. printk(" - Already disconnected! ");
  1092. hostdata->state = S_UNCONNECTED;
  1093. spin_unlock_irqrestore(&hostdata->lock, flags);
  1094. return;
  1095. }
  1096. DB(DB_INTR, printk("UNEXP_DISC"))
  1097. hostdata->connected = NULL;
  1098. hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xff));
  1099. hostdata->state = S_UNCONNECTED;
  1100. if (cmd->cmnd[0] == REQUEST_SENSE && cmd->SCp.Status != GOOD)
  1101. cmd->result =
  1102. (cmd->result & 0x00ffff) | (DID_ERROR << 16);
  1103. else
  1104. cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
  1105. cmd->scsi_done(cmd);
  1106. /* We are no longer connected to a target - check to see if
  1107. * there are commands waiting to be executed.
  1108. */
  1109. /* look above for comments on scsi_done() */
  1110. spin_unlock_irqrestore(&hostdata->lock, flags);
  1111. wd33c93_execute(instance);
  1112. break;
  1113. case CSR_DISC:
  1114. /* Make sure that reselection is enabled at this point - it may
  1115. * have been turned off for the command that just completed.
  1116. */
  1117. write_wd33c93(regs, WD_SOURCE_ID, SRCID_ER);
  1118. DB(DB_INTR, printk("DISC"))
  1119. if (cmd == NULL) {
  1120. printk(" - Already disconnected! ");
  1121. hostdata->state = S_UNCONNECTED;
  1122. }
  1123. switch (hostdata->state) {
  1124. case S_PRE_CMP_DISC:
  1125. hostdata->connected = NULL;
  1126. hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xff));
  1127. hostdata->state = S_UNCONNECTED;
  1128. DB(DB_INTR, printk(":%d", cmd->SCp.Status))
  1129. if (cmd->cmnd[0] == REQUEST_SENSE
  1130. && cmd->SCp.Status != GOOD)
  1131. cmd->result =
  1132. (cmd->
  1133. result & 0x00ffff) | (DID_ERROR << 16);
  1134. else
  1135. cmd->result =
  1136. cmd->SCp.Status | (cmd->SCp.Message << 8);
  1137. cmd->scsi_done(cmd);
  1138. break;
  1139. case S_PRE_TMP_DISC:
  1140. case S_RUNNING_LEVEL2:
  1141. cmd->host_scribble = (uchar *) hostdata->disconnected_Q;
  1142. hostdata->disconnected_Q = cmd;
  1143. hostdata->connected = NULL;
  1144. hostdata->state = S_UNCONNECTED;
  1145. #ifdef PROC_STATISTICS
  1146. hostdata->disc_done_cnt[cmd->device->id]++;
  1147. #endif
  1148. break;
  1149. default:
  1150. printk("*** Unexpected DISCONNECT interrupt! ***");
  1151. hostdata->state = S_UNCONNECTED;
  1152. }
  1153. /* We are no longer connected to a target - check to see if
  1154. * there are commands waiting to be executed.
  1155. */
  1156. spin_unlock_irqrestore(&hostdata->lock, flags);
  1157. wd33c93_execute(instance);
  1158. break;
  1159. case CSR_RESEL_AM:
  1160. case CSR_RESEL:
  1161. DB(DB_INTR, printk("RESEL%s", sr == CSR_RESEL_AM ? "_AM" : ""))
  1162. /* Old chips (pre -A ???) don't have advanced features and will
  1163. * generate CSR_RESEL. In that case we have to extract the LUN the
  1164. * hard way (see below).
  1165. * First we have to make sure this reselection didn't
  1166. * happen during Arbitration/Selection of some other device.
  1167. * If yes, put losing command back on top of input_Q.
  1168. */
  1169. if (hostdata->level2 <= L2_NONE) {
  1170. if (hostdata->selecting) {
  1171. cmd = (struct scsi_cmnd *) hostdata->selecting;
  1172. hostdata->selecting = NULL;
  1173. hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xff));
  1174. cmd->host_scribble =
  1175. (uchar *) hostdata->input_Q;
  1176. hostdata->input_Q = cmd;
  1177. }
  1178. }
  1179. else {
  1180. if (cmd) {
  1181. if (phs == 0x00) {
  1182. hostdata->busy[cmd->device->id] &=
  1183. ~(1 << (cmd->device->lun & 0xff));
  1184. cmd->host_scribble =
  1185. (uchar *) hostdata->input_Q;
  1186. hostdata->input_Q = cmd;
  1187. } else {
  1188. printk
  1189. ("---%02x:%02x:%02x-TROUBLE: Intrusive ReSelect!---",
  1190. asr, sr, phs);
  1191. while (1)
  1192. printk("\r");
  1193. }
  1194. }
  1195. }
  1196. /* OK - find out which device reselected us. */
  1197. id = read_wd33c93(regs, WD_SOURCE_ID);
  1198. id &= SRCID_MASK;
  1199. /* and extract the lun from the ID message. (Note that we don't
  1200. * bother to check for a valid message here - I guess this is
  1201. * not the right way to go, but...)
  1202. */
  1203. if (sr == CSR_RESEL_AM) {
  1204. lun = read_wd33c93(regs, WD_DATA);
  1205. if (hostdata->level2 < L2_RESELECT)
  1206. write_wd33c93_cmd(regs, WD_CMD_NEGATE_ACK);
  1207. lun &= 7;
  1208. } else {
  1209. /* Old chip; wait for msgin phase to pick up the LUN. */
  1210. for (lun = 255; lun; lun--) {
  1211. if ((asr = read_aux_stat(regs)) & ASR_INT)
  1212. break;
  1213. udelay(10);
  1214. }
  1215. if (!(asr & ASR_INT)) {
  1216. printk
  1217. ("wd33c93: Reselected without IDENTIFY\n");
  1218. lun = 0;
  1219. } else {
  1220. /* Verify this is a change to MSG_IN and read the message */
  1221. sr = read_wd33c93(regs, WD_SCSI_STATUS);
  1222. udelay(7);
  1223. if (sr == (CSR_ABORT | PHS_MESS_IN) ||
  1224. sr == (CSR_UNEXP | PHS_MESS_IN) ||
  1225. sr == (CSR_SRV_REQ | PHS_MESS_IN)) {
  1226. /* Got MSG_IN, grab target LUN */
  1227. lun = read_1_byte(regs);
  1228. /* Now we expect a 'paused with ACK asserted' int.. */
  1229. asr = read_aux_stat(regs);
  1230. if (!(asr & ASR_INT)) {
  1231. udelay(10);
  1232. asr = read_aux_stat(regs);
  1233. if (!(asr & ASR_INT))
  1234. printk
  1235. ("wd33c93: No int after LUN on RESEL (%02x)\n",
  1236. asr);
  1237. }
  1238. sr = read_wd33c93(regs, WD_SCSI_STATUS);
  1239. udelay(7);
  1240. if (sr != CSR_MSGIN)
  1241. printk
  1242. ("wd33c93: Not paused with ACK on RESEL (%02x)\n",
  1243. sr);
  1244. lun &= 7;
  1245. write_wd33c93_cmd(regs,
  1246. WD_CMD_NEGATE_ACK);
  1247. } else {
  1248. printk
  1249. ("wd33c93: Not MSG_IN on reselect (%02x)\n",
  1250. sr);
  1251. lun = 0;
  1252. }
  1253. }
  1254. }
  1255. /* Now we look for the command that's reconnecting. */
  1256. cmd = (struct scsi_cmnd *) hostdata->disconnected_Q;
  1257. patch = NULL;
  1258. while (cmd) {
  1259. if (id == cmd->device->id && lun == (u8)cmd->device->lun)
  1260. break;
  1261. patch = cmd;
  1262. cmd = (struct scsi_cmnd *) cmd->host_scribble;
  1263. }
  1264. /* Hmm. Couldn't find a valid command.... What to do? */
  1265. if (!cmd) {
  1266. printk
  1267. ("---TROUBLE: target %d.%d not in disconnect queue---",
  1268. id, (u8)lun);
  1269. spin_unlock_irqrestore(&hostdata->lock, flags);
  1270. return;
  1271. }
  1272. /* Ok, found the command - now start it up again. */
  1273. if (patch)
  1274. patch->host_scribble = cmd->host_scribble;
  1275. else
  1276. hostdata->disconnected_Q =
  1277. (struct scsi_cmnd *) cmd->host_scribble;
  1278. hostdata->connected = cmd;
  1279. /* We don't need to worry about 'initialize_SCp()' or 'hostdata->busy[]'
  1280. * because these things are preserved over a disconnect.
  1281. * But we DO need to fix the DPD bit so it's correct for this command.
  1282. */
  1283. if (cmd->sc_data_direction == DMA_TO_DEVICE)
  1284. write_wd33c93(regs, WD_DESTINATION_ID, cmd->device->id);
  1285. else
  1286. write_wd33c93(regs, WD_DESTINATION_ID,
  1287. cmd->device->id | DSTID_DPD);
  1288. if (hostdata->level2 >= L2_RESELECT) {
  1289. write_wd33c93_count(regs, 0); /* we want a DATA_PHASE interrupt */
  1290. write_wd33c93(regs, WD_COMMAND_PHASE, 0x45);
  1291. write_wd33c93_cmd(regs, WD_CMD_SEL_ATN_XFER);
  1292. hostdata->state = S_RUNNING_LEVEL2;
  1293. } else
  1294. hostdata->state = S_CONNECTED;
  1295. spin_unlock_irqrestore(&hostdata->lock, flags);
  1296. break;
  1297. default:
  1298. printk("--UNKNOWN INTERRUPT:%02x:%02x:%02x--", asr, sr, phs);
  1299. spin_unlock_irqrestore(&hostdata->lock, flags);
  1300. }
  1301. DB(DB_INTR, printk("} "))
  1302. }
  1303. static void
  1304. reset_wd33c93(struct Scsi_Host *instance)
  1305. {
  1306. struct WD33C93_hostdata *hostdata =
  1307. (struct WD33C93_hostdata *) instance->hostdata;
  1308. const wd33c93_regs regs = hostdata->regs;
  1309. uchar sr;
  1310. #ifdef CONFIG_SGI_IP22
  1311. {
  1312. int busycount = 0;
  1313. extern void sgiwd93_reset(unsigned long);
  1314. /* wait 'til the chip gets some time for us */
  1315. while ((read_aux_stat(regs) & ASR_BSY) && busycount++ < 100)
  1316. udelay (10);
  1317. /*
  1318. * there are scsi devices out there, which manage to lock up
  1319. * the wd33c93 in a busy condition. In this state it won't
  1320. * accept the reset command. The only way to solve this is to
  1321. * give the chip a hardware reset (if possible). The code below
  1322. * does this for the SGI Indy, where this is possible
  1323. */
  1324. /* still busy ? */
  1325. if (read_aux_stat(regs) & ASR_BSY)
  1326. sgiwd93_reset(instance->base); /* yeah, give it the hard one */
  1327. }
  1328. #endif
  1329. write_wd33c93(regs, WD_OWN_ID, OWNID_EAF | OWNID_RAF |
  1330. instance->this_id | hostdata->clock_freq);
  1331. write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
  1332. write_wd33c93(regs, WD_SYNCHRONOUS_TRANSFER,
  1333. calc_sync_xfer(hostdata->default_sx_per / 4,
  1334. DEFAULT_SX_OFF, 0, hostdata->sx_table));
  1335. write_wd33c93(regs, WD_COMMAND, WD_CMD_RESET);
  1336. #ifdef CONFIG_MVME147_SCSI
  1337. udelay(25); /* The old wd33c93 on MVME147 needs this, at least */
  1338. #endif
  1339. while (!(read_aux_stat(regs) & ASR_INT))
  1340. ;
  1341. sr = read_wd33c93(regs, WD_SCSI_STATUS);
  1342. hostdata->microcode = read_wd33c93(regs, WD_CDB_1);
  1343. if (sr == 0x00)
  1344. hostdata->chip = C_WD33C93;
  1345. else if (sr == 0x01) {
  1346. write_wd33c93(regs, WD_QUEUE_TAG, 0xa5); /* any random number */
  1347. sr = read_wd33c93(regs, WD_QUEUE_TAG);
  1348. if (sr == 0xa5) {
  1349. hostdata->chip = C_WD33C93B;
  1350. write_wd33c93(regs, WD_QUEUE_TAG, 0);
  1351. } else
  1352. hostdata->chip = C_WD33C93A;
  1353. } else
  1354. hostdata->chip = C_UNKNOWN_CHIP;
  1355. if (hostdata->chip != C_WD33C93B) /* Fast SCSI unavailable */
  1356. hostdata->fast = 0;
  1357. write_wd33c93(regs, WD_TIMEOUT_PERIOD, TIMEOUT_PERIOD_VALUE);
  1358. write_wd33c93(regs, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
  1359. }
  1360. int
  1361. wd33c93_host_reset(struct scsi_cmnd * SCpnt)
  1362. {
  1363. struct Scsi_Host *instance;
  1364. struct WD33C93_hostdata *hostdata;
  1365. int i;
  1366. instance = SCpnt->device->host;
  1367. spin_lock_irq(instance->host_lock);
  1368. hostdata = (struct WD33C93_hostdata *) instance->hostdata;
  1369. printk("scsi%d: reset. ", instance->host_no);
  1370. disable_irq(instance->irq);
  1371. hostdata->dma_stop(instance, NULL, 0);
  1372. for (i = 0; i < 8; i++) {
  1373. hostdata->busy[i] = 0;
  1374. hostdata->sync_xfer[i] =
  1375. calc_sync_xfer(DEFAULT_SX_PER / 4, DEFAULT_SX_OFF,
  1376. 0, hostdata->sx_table);
  1377. hostdata->sync_stat[i] = SS_UNSET; /* using default sync values */
  1378. }
  1379. hostdata->input_Q = NULL;
  1380. hostdata->selecting = NULL;
  1381. hostdata->connected = NULL;
  1382. hostdata->disconnected_Q = NULL;
  1383. hostdata->state = S_UNCONNECTED;
  1384. hostdata->dma = D_DMA_OFF;
  1385. hostdata->incoming_ptr = 0;
  1386. hostdata->outgoing_len = 0;
  1387. reset_wd33c93(instance);
  1388. SCpnt->result = DID_RESET << 16;
  1389. enable_irq(instance->irq);
  1390. spin_unlock_irq(instance->host_lock);
  1391. return SUCCESS;
  1392. }
  1393. int
  1394. wd33c93_abort(struct scsi_cmnd * cmd)
  1395. {
  1396. struct Scsi_Host *instance;
  1397. struct WD33C93_hostdata *hostdata;
  1398. wd33c93_regs regs;
  1399. struct scsi_cmnd *tmp, *prev;
  1400. disable_irq(cmd->device->host->irq);
  1401. instance = cmd->device->host;
  1402. hostdata = (struct WD33C93_hostdata *) instance->hostdata;
  1403. regs = hostdata->regs;
  1404. /*
  1405. * Case 1 : If the command hasn't been issued yet, we simply remove it
  1406. * from the input_Q.
  1407. */
  1408. tmp = (struct scsi_cmnd *) hostdata->input_Q;
  1409. prev = NULL;
  1410. while (tmp) {
  1411. if (tmp == cmd) {
  1412. if (prev)
  1413. prev->host_scribble = cmd->host_scribble;
  1414. else
  1415. hostdata->input_Q =
  1416. (struct scsi_cmnd *) cmd->host_scribble;
  1417. cmd->host_scribble = NULL;
  1418. cmd->result = DID_ABORT << 16;
  1419. printk
  1420. ("scsi%d: Abort - removing command from input_Q. ",
  1421. instance->host_no);
  1422. enable_irq(cmd->device->host->irq);
  1423. cmd->scsi_done(cmd);
  1424. return SUCCESS;
  1425. }
  1426. prev = tmp;
  1427. tmp = (struct scsi_cmnd *) tmp->host_scribble;
  1428. }
  1429. /*
  1430. * Case 2 : If the command is connected, we're going to fail the abort
  1431. * and let the high level SCSI driver retry at a later time or
  1432. * issue a reset.
  1433. *
  1434. * Timeouts, and therefore aborted commands, will be highly unlikely
  1435. * and handling them cleanly in this situation would make the common
  1436. * case of noresets less efficient, and would pollute our code. So,
  1437. * we fail.
  1438. */
  1439. if (hostdata->connected == cmd) {
  1440. uchar sr, asr;
  1441. unsigned long timeout;
  1442. printk("scsi%d: Aborting connected command - ",
  1443. instance->host_no);
  1444. printk("stopping DMA - ");
  1445. if (hostdata->dma == D_DMA_RUNNING) {
  1446. hostdata->dma_stop(instance, cmd, 0);
  1447. hostdata->dma = D_DMA_OFF;
  1448. }
  1449. printk("sending wd33c93 ABORT command - ");
  1450. write_wd33c93(regs, WD_CONTROL,
  1451. CTRL_IDI | CTRL_EDI | CTRL_POLLED);
  1452. write_wd33c93_cmd(regs, WD_CMD_ABORT);
  1453. /* Now we have to attempt to flush out the FIFO... */
  1454. printk("flushing fifo - ");
  1455. timeout = 1000000;
  1456. do {
  1457. asr = read_aux_stat(regs);
  1458. if (asr & ASR_DBR)
  1459. read_wd33c93(regs, WD_DATA);
  1460. } while (!(asr & ASR_INT) && timeout-- > 0);
  1461. sr = read_wd33c93(regs, WD_SCSI_STATUS);
  1462. printk
  1463. ("asr=%02x, sr=%02x, %ld bytes un-transferred (timeout=%ld) - ",
  1464. asr, sr, read_wd33c93_count(regs), timeout);
  1465. /*
  1466. * Abort command processed.
  1467. * Still connected.
  1468. * We must disconnect.
  1469. */
  1470. printk("sending wd33c93 DISCONNECT command - ");
  1471. write_wd33c93_cmd(regs, WD_CMD_DISCONNECT);
  1472. timeout = 1000000;
  1473. asr = read_aux_stat(regs);
  1474. while ((asr & ASR_CIP) && timeout-- > 0)
  1475. asr = read_aux_stat(regs);
  1476. sr = read_wd33c93(regs, WD_SCSI_STATUS);
  1477. printk("asr=%02x, sr=%02x.", asr, sr);
  1478. hostdata->busy[cmd->device->id] &= ~(1 << (cmd->device->lun & 0xff));
  1479. hostdata->connected = NULL;
  1480. hostdata->state = S_UNCONNECTED;
  1481. cmd->result = DID_ABORT << 16;
  1482. /* sti();*/
  1483. wd33c93_execute(instance);
  1484. enable_irq(cmd->device->host->irq);
  1485. cmd->scsi_done(cmd);
  1486. return SUCCESS;
  1487. }
  1488. /*
  1489. * Case 3: If the command is currently disconnected from the bus,
  1490. * we're not going to expend much effort here: Let's just return
  1491. * an ABORT_SNOOZE and hope for the best...
  1492. */
  1493. tmp = (struct scsi_cmnd *) hostdata->disconnected_Q;
  1494. while (tmp) {
  1495. if (tmp == cmd) {
  1496. printk
  1497. ("scsi%d: Abort - command found on disconnected_Q - ",
  1498. instance->host_no);
  1499. printk("Abort SNOOZE. ");
  1500. enable_irq(cmd->device->host->irq);
  1501. return FAILED;
  1502. }
  1503. tmp = (struct scsi_cmnd *) tmp->host_scribble;
  1504. }
  1505. /*
  1506. * Case 4 : If we reached this point, the command was not found in any of
  1507. * the queues.
  1508. *
  1509. * We probably reached this point because of an unlikely race condition
  1510. * between the command completing successfully and the abortion code,
  1511. * so we won't panic, but we will notify the user in case something really
  1512. * broke.
  1513. */
  1514. /* sti();*/
  1515. wd33c93_execute(instance);
  1516. enable_irq(cmd->device->host->irq);
  1517. printk("scsi%d: warning : SCSI command probably completed successfully"
  1518. " before abortion. ", instance->host_no);
  1519. return FAILED;
  1520. }
  1521. #define MAX_WD33C93_HOSTS 4
  1522. #define MAX_SETUP_ARGS ARRAY_SIZE(setup_args)
  1523. #define SETUP_BUFFER_SIZE 200
  1524. static char setup_buffer[SETUP_BUFFER_SIZE];
  1525. static char setup_used[MAX_SETUP_ARGS];
  1526. static int done_setup = 0;
  1527. static int
  1528. wd33c93_setup(char *str)
  1529. {
  1530. int i;
  1531. char *p1, *p2;
  1532. /* The kernel does some processing of the command-line before calling
  1533. * this function: If it begins with any decimal or hex number arguments,
  1534. * ints[0] = how many numbers found and ints[1] through [n] are the values
  1535. * themselves. str points to where the non-numeric arguments (if any)
  1536. * start: We do our own parsing of those. We construct synthetic 'nosync'
  1537. * keywords out of numeric args (to maintain compatibility with older
  1538. * versions) and then add the rest of the arguments.
  1539. */
  1540. p1 = setup_buffer;
  1541. *p1 = '\0';
  1542. if (str)
  1543. strncpy(p1, str, SETUP_BUFFER_SIZE - strlen(setup_buffer));
  1544. setup_buffer[SETUP_BUFFER_SIZE - 1] = '\0';
  1545. p1 = setup_buffer;
  1546. i = 0;
  1547. while (*p1 && (i < MAX_SETUP_ARGS)) {
  1548. p2 = strchr(p1, ',');
  1549. if (p2) {
  1550. *p2 = '\0';
  1551. if (p1 != p2)
  1552. setup_args[i] = p1;
  1553. p1 = p2 + 1;
  1554. i++;
  1555. } else {
  1556. setup_args[i] = p1;
  1557. break;
  1558. }
  1559. }
  1560. for (i = 0; i < MAX_SETUP_ARGS; i++)
  1561. setup_used[i] = 0;
  1562. done_setup = 1;
  1563. return 1;
  1564. }
  1565. __setup("wd33c93=", wd33c93_setup);
  1566. /* check_setup_args() returns index if key found, 0 if not
  1567. */
  1568. static int
  1569. check_setup_args(char *key, int *flags, int *val, char *buf)
  1570. {
  1571. int x;
  1572. char *cp;
  1573. for (x = 0; x < MAX_SETUP_ARGS; x++) {
  1574. if (setup_used[x])
  1575. continue;
  1576. if (!strncmp(setup_args[x], key, strlen(key)))
  1577. break;
  1578. if (!strncmp(setup_args[x], "next", strlen("next")))
  1579. return 0;
  1580. }
  1581. if (x == MAX_SETUP_ARGS)
  1582. return 0;
  1583. setup_used[x] = 1;
  1584. cp = setup_args[x] + strlen(key);
  1585. *val = -1;
  1586. if (*cp != ':')
  1587. return ++x;
  1588. cp++;
  1589. if ((*cp >= '0') && (*cp <= '9')) {
  1590. *val = simple_strtoul(cp, NULL, 0);
  1591. }
  1592. return ++x;
  1593. }
  1594. /*
  1595. * Calculate internal data-transfer-clock cycle from input-clock
  1596. * frequency (/MHz) and fill 'sx_table'.
  1597. *
  1598. * The original driver used to rely on a fixed sx_table, containing periods
  1599. * for (only) the lower limits of the respective input-clock-frequency ranges
  1600. * (8-10/12-15/16-20 MHz). Although it seems, that no problems occurred with
  1601. * this setting so far, it might be desirable to adjust the transfer periods
  1602. * closer to the really attached, possibly 25% higher, input-clock, since
  1603. * - the wd33c93 may really use a significant shorter period, than it has
  1604. * negotiated (eg. thrashing the target, which expects 4/8MHz, with 5/10MHz
  1605. * instead).
  1606. * - the wd33c93 may ask the target for a lower transfer rate, than the target
  1607. * is capable of (eg. negotiating for an assumed minimum of 252ns instead of
  1608. * possible 200ns, which indeed shows up in tests as an approx. 10% lower
  1609. * transfer rate).
  1610. */
  1611. static inline unsigned int
  1612. round_4(unsigned int x)
  1613. {
  1614. switch (x & 3) {
  1615. case 1: --x;
  1616. break;
  1617. case 2: ++x;
  1618. /* fall through */
  1619. case 3: ++x;
  1620. }
  1621. return x;
  1622. }
  1623. static void
  1624. calc_sx_table(unsigned int mhz, struct sx_period sx_table[9])
  1625. {
  1626. unsigned int d, i;
  1627. if (mhz < 11)
  1628. d = 2; /* divisor for 8-10 MHz input-clock */
  1629. else if (mhz < 16)
  1630. d = 3; /* divisor for 12-15 MHz input-clock */
  1631. else
  1632. d = 4; /* divisor for 16-20 MHz input-clock */
  1633. d = (100000 * d) / 2 / mhz; /* 100 x DTCC / nanosec */
  1634. sx_table[0].period_ns = 1;
  1635. sx_table[0].reg_value = 0x20;
  1636. for (i = 1; i < 8; i++) {
  1637. sx_table[i].period_ns = round_4((i+1)*d / 100);
  1638. sx_table[i].reg_value = (i+1)*0x10;
  1639. }
  1640. sx_table[7].reg_value = 0;
  1641. sx_table[8].period_ns = 0;
  1642. sx_table[8].reg_value = 0;
  1643. }
  1644. /*
  1645. * check and, maybe, map an init- or "clock:"- argument.
  1646. */
  1647. static uchar
  1648. set_clk_freq(int freq, int *mhz)
  1649. {
  1650. int x = freq;
  1651. if (WD33C93_FS_8_10 == freq)
  1652. freq = 8;
  1653. else if (WD33C93_FS_12_15 == freq)
  1654. freq = 12;
  1655. else if (WD33C93_FS_16_20 == freq)
  1656. freq = 16;
  1657. else if (freq > 7 && freq < 11)
  1658. x = WD33C93_FS_8_10;
  1659. else if (freq > 11 && freq < 16)
  1660. x = WD33C93_FS_12_15;
  1661. else if (freq > 15 && freq < 21)
  1662. x = WD33C93_FS_16_20;
  1663. else {
  1664. /* Hmm, wouldn't it be safer to assume highest freq here? */
  1665. x = WD33C93_FS_8_10;
  1666. freq = 8;
  1667. }
  1668. *mhz = freq;
  1669. return x;
  1670. }
  1671. /*
  1672. * to be used with the resync: fast: ... options
  1673. */
  1674. static inline void set_resync ( struct WD33C93_hostdata *hd, int mask )
  1675. {
  1676. int i;
  1677. for (i = 0; i < 8; i++)
  1678. if (mask & (1 << i))
  1679. hd->sync_stat[i] = SS_UNSET;
  1680. }
  1681. void
  1682. wd33c93_init(struct Scsi_Host *instance, const wd33c93_regs regs,
  1683. dma_setup_t setup, dma_stop_t stop, int clock_freq)
  1684. {
  1685. struct WD33C93_hostdata *hostdata;
  1686. int i;
  1687. int flags;
  1688. int val;
  1689. char buf[32];
  1690. if (!done_setup && setup_strings)
  1691. wd33c93_setup(setup_strings);
  1692. hostdata = (struct WD33C93_hostdata *) instance->hostdata;
  1693. hostdata->regs = regs;
  1694. hostdata->clock_freq = set_clk_freq(clock_freq, &i);
  1695. calc_sx_table(i, hostdata->sx_table);
  1696. hostdata->dma_setup = setup;
  1697. hostdata->dma_stop = stop;
  1698. hostdata->dma_bounce_buffer = NULL;
  1699. hostdata->dma_bounce_len = 0;
  1700. for (i = 0; i < 8; i++) {
  1701. hostdata->busy[i] = 0;
  1702. hostdata->sync_xfer[i] =
  1703. calc_sync_xfer(DEFAULT_SX_PER / 4, DEFAULT_SX_OFF,
  1704. 0, hostdata->sx_table);
  1705. hostdata->sync_stat[i] = SS_UNSET; /* using default sync values */
  1706. #ifdef PROC_STATISTICS
  1707. hostdata->cmd_cnt[i] = 0;
  1708. hostdata->disc_allowed_cnt[i] = 0;
  1709. hostdata->disc_done_cnt[i] = 0;
  1710. #endif
  1711. }
  1712. hostdata->input_Q = NULL;
  1713. hostdata->selecting = NULL;
  1714. hostdata->connected = NULL;
  1715. hostdata->disconnected_Q = NULL;
  1716. hostdata->state = S_UNCONNECTED;
  1717. hostdata->dma = D_DMA_OFF;
  1718. hostdata->level2 = L2_BASIC;
  1719. hostdata->disconnect = DIS_ADAPTIVE;
  1720. hostdata->args = DEBUG_DEFAULTS;
  1721. hostdata->incoming_ptr = 0;
  1722. hostdata->outgoing_len = 0;
  1723. hostdata->default_sx_per = DEFAULT_SX_PER;
  1724. hostdata->no_dma = 0; /* default is DMA enabled */
  1725. #ifdef PROC_INTERFACE
  1726. hostdata->proc = PR_VERSION | PR_INFO | PR_STATISTICS |
  1727. PR_CONNECTED | PR_INPUTQ | PR_DISCQ | PR_STOP;
  1728. #ifdef PROC_STATISTICS
  1729. hostdata->dma_cnt = 0;
  1730. hostdata->pio_cnt = 0;
  1731. hostdata->int_cnt = 0;
  1732. #endif
  1733. #endif
  1734. if (check_setup_args("clock", &flags, &val, buf)) {
  1735. hostdata->clock_freq = set_clk_freq(val, &val);
  1736. calc_sx_table(val, hostdata->sx_table);
  1737. }
  1738. if (check_setup_args("nosync", &flags, &val, buf))
  1739. hostdata->no_sync = val;
  1740. if (check_setup_args("nodma", &flags, &val, buf))
  1741. hostdata->no_dma = (val == -1) ? 1 : val;
  1742. if (check_setup_args("period", &flags, &val, buf))
  1743. hostdata->default_sx_per =
  1744. hostdata->sx_table[round_period((unsigned int) val,
  1745. hostdata->sx_table)].period_ns;
  1746. if (check_setup_args("disconnect", &flags, &val, buf)) {
  1747. if ((val >= DIS_NEVER) && (val <= DIS_ALWAYS))
  1748. hostdata->disconnect = val;
  1749. else
  1750. hostdata->disconnect = DIS_ADAPTIVE;
  1751. }
  1752. if (check_setup_args("level2", &flags, &val, buf))
  1753. hostdata->level2 = val;
  1754. if (check_setup_args("debug", &flags, &val, buf))
  1755. hostdata->args = val & DB_MASK;
  1756. if (check_setup_args("burst", &flags, &val, buf))
  1757. hostdata->dma_mode = val ? CTRL_BURST:CTRL_DMA;
  1758. if (WD33C93_FS_16_20 == hostdata->clock_freq /* divisor 4 */
  1759. && check_setup_args("fast", &flags, &val, buf))
  1760. hostdata->fast = !!val;
  1761. if ((i = check_setup_args("next", &flags, &val, buf))) {
  1762. while (i)
  1763. setup_used[--i] = 1;
  1764. }
  1765. #ifdef PROC_INTERFACE
  1766. if (check_setup_args("proc", &flags, &val, buf))
  1767. hostdata->proc = val;
  1768. #endif
  1769. spin_lock_irq(&hostdata->lock);
  1770. reset_wd33c93(instance);
  1771. spin_unlock_irq(&hostdata->lock);
  1772. printk("wd33c93-%d: chip=%s/%d no_sync=0x%x no_dma=%d",
  1773. instance->host_no,
  1774. (hostdata->chip == C_WD33C93) ? "WD33c93" : (hostdata->chip ==
  1775. C_WD33C93A) ?
  1776. "WD33c93A" : (hostdata->chip ==
  1777. C_WD33C93B) ? "WD33c93B" : "unknown",
  1778. hostdata->microcode, hostdata->no_sync, hostdata->no_dma);
  1779. #ifdef DEBUGGING_ON
  1780. printk(" debug_flags=0x%02x\n", hostdata->args);
  1781. #else
  1782. printk(" debugging=OFF\n");
  1783. #endif
  1784. printk(" setup_args=");
  1785. for (i = 0; i < MAX_SETUP_ARGS; i++)
  1786. printk("%s,", setup_args[i]);
  1787. printk("\n");
  1788. printk(" Version %s - %s\n", WD33C93_VERSION, WD33C93_DATE);
  1789. }
  1790. int wd33c93_write_info(struct Scsi_Host *instance, char *buf, int len)
  1791. {
  1792. #ifdef PROC_INTERFACE
  1793. char *bp;
  1794. struct WD33C93_hostdata *hd;
  1795. int x;
  1796. hd = (struct WD33C93_hostdata *) instance->hostdata;
  1797. /* We accept the following
  1798. * keywords (same format as command-line, but arguments are not optional):
  1799. * debug
  1800. * disconnect
  1801. * period
  1802. * resync
  1803. * proc
  1804. * nodma
  1805. * level2
  1806. * burst
  1807. * fast
  1808. * nosync
  1809. */
  1810. buf[len] = '\0';
  1811. for (bp = buf; *bp; ) {
  1812. while (',' == *bp || ' ' == *bp)
  1813. ++bp;
  1814. if (!strncmp(bp, "debug:", 6)) {
  1815. hd->args = simple_strtoul(bp+6, &bp, 0) & DB_MASK;
  1816. } else if (!strncmp(bp, "disconnect:", 11)) {
  1817. x = simple_strtoul(bp+11, &bp, 0);
  1818. if (x < DIS_NEVER || x > DIS_ALWAYS)
  1819. x = DIS_ADAPTIVE;
  1820. hd->disconnect = x;
  1821. } else if (!strncmp(bp, "period:", 7)) {
  1822. x = simple_strtoul(bp+7, &bp, 0);
  1823. hd->default_sx_per =
  1824. hd->sx_table[round_period((unsigned int) x,
  1825. hd->sx_table)].period_ns;
  1826. } else if (!strncmp(bp, "resync:", 7)) {
  1827. set_resync(hd, (int)simple_strtoul(bp+7, &bp, 0));
  1828. } else if (!strncmp(bp, "proc:", 5)) {
  1829. hd->proc = simple_strtoul(bp+5, &bp, 0);
  1830. } else if (!strncmp(bp, "nodma:", 6)) {
  1831. hd->no_dma = simple_strtoul(bp+6, &bp, 0);
  1832. } else if (!strncmp(bp, "level2:", 7)) {
  1833. hd->level2 = simple_strtoul(bp+7, &bp, 0);
  1834. } else if (!strncmp(bp, "burst:", 6)) {
  1835. hd->dma_mode =
  1836. simple_strtol(bp+6, &bp, 0) ? CTRL_BURST:CTRL_DMA;
  1837. } else if (!strncmp(bp, "fast:", 5)) {
  1838. x = !!simple_strtol(bp+5, &bp, 0);
  1839. if (x != hd->fast)
  1840. set_resync(hd, 0xff);
  1841. hd->fast = x;
  1842. } else if (!strncmp(bp, "nosync:", 7)) {
  1843. x = simple_strtoul(bp+7, &bp, 0);
  1844. set_resync(hd, x ^ hd->no_sync);
  1845. hd->no_sync = x;
  1846. } else {
  1847. break; /* unknown keyword,syntax-error,... */
  1848. }
  1849. }
  1850. return len;
  1851. #else
  1852. return 0;
  1853. #endif
  1854. }
  1855. int
  1856. wd33c93_show_info(struct seq_file *m, struct Scsi_Host *instance)
  1857. {
  1858. #ifdef PROC_INTERFACE
  1859. struct WD33C93_hostdata *hd;
  1860. struct scsi_cmnd *cmd;
  1861. int x;
  1862. hd = (struct WD33C93_hostdata *) instance->hostdata;
  1863. spin_lock_irq(&hd->lock);
  1864. if (hd->proc & PR_VERSION)
  1865. seq_printf(m, "\nVersion %s - %s.",
  1866. WD33C93_VERSION, WD33C93_DATE);
  1867. if (hd->proc & PR_INFO) {
  1868. seq_printf(m, "\nclock_freq=%02x no_sync=%02x no_dma=%d"
  1869. " dma_mode=%02x fast=%d",
  1870. hd->clock_freq, hd->no_sync, hd->no_dma, hd->dma_mode, hd->fast);
  1871. seq_puts(m, "\nsync_xfer[] = ");
  1872. for (x = 0; x < 7; x++)
  1873. seq_printf(m, "\t%02x", hd->sync_xfer[x]);
  1874. seq_puts(m, "\nsync_stat[] = ");
  1875. for (x = 0; x < 7; x++)
  1876. seq_printf(m, "\t%02x", hd->sync_stat[x]);
  1877. }
  1878. #ifdef PROC_STATISTICS
  1879. if (hd->proc & PR_STATISTICS) {
  1880. seq_puts(m, "\ncommands issued: ");
  1881. for (x = 0; x < 7; x++)
  1882. seq_printf(m, "\t%ld", hd->cmd_cnt[x]);
  1883. seq_puts(m, "\ndisconnects allowed:");
  1884. for (x = 0; x < 7; x++)
  1885. seq_printf(m, "\t%ld", hd->disc_allowed_cnt[x]);
  1886. seq_puts(m, "\ndisconnects done: ");
  1887. for (x = 0; x < 7; x++)
  1888. seq_printf(m, "\t%ld", hd->disc_done_cnt[x]);
  1889. seq_printf(m,
  1890. "\ninterrupts: %ld, DATA_PHASE ints: %ld DMA, %ld PIO",
  1891. hd->int_cnt, hd->dma_cnt, hd->pio_cnt);
  1892. }
  1893. #endif
  1894. if (hd->proc & PR_CONNECTED) {
  1895. seq_puts(m, "\nconnected: ");
  1896. if (hd->connected) {
  1897. cmd = (struct scsi_cmnd *) hd->connected;
  1898. seq_printf(m, " %d:%llu(%02x)",
  1899. cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
  1900. }
  1901. }
  1902. if (hd->proc & PR_INPUTQ) {
  1903. seq_puts(m, "\ninput_Q: ");
  1904. cmd = (struct scsi_cmnd *) hd->input_Q;
  1905. while (cmd) {
  1906. seq_printf(m, " %d:%llu(%02x)",
  1907. cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
  1908. cmd = (struct scsi_cmnd *) cmd->host_scribble;
  1909. }
  1910. }
  1911. if (hd->proc & PR_DISCQ) {
  1912. seq_puts(m, "\ndisconnected_Q:");
  1913. cmd = (struct scsi_cmnd *) hd->disconnected_Q;
  1914. while (cmd) {
  1915. seq_printf(m, " %d:%llu(%02x)",
  1916. cmd->device->id, cmd->device->lun, cmd->cmnd[0]);
  1917. cmd = (struct scsi_cmnd *) cmd->host_scribble;
  1918. }
  1919. }
  1920. seq_putc(m, '\n');
  1921. spin_unlock_irq(&hd->lock);
  1922. #endif /* PROC_INTERFACE */
  1923. return 0;
  1924. }
  1925. EXPORT_SYMBOL(wd33c93_host_reset);
  1926. EXPORT_SYMBOL(wd33c93_init);
  1927. EXPORT_SYMBOL(wd33c93_abort);
  1928. EXPORT_SYMBOL(wd33c93_queuecommand);
  1929. EXPORT_SYMBOL(wd33c93_intr);
  1930. EXPORT_SYMBOL(wd33c93_show_info);
  1931. EXPORT_SYMBOL(wd33c93_write_info);