PageRenderTime 1094ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 1ms

/drivers/scsi/in2000.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2
C | 2337 lines | 1360 code | 388 blank | 589 comment | 283 complexity | 5f7d74d39b73bd2c3f2fd10b3c42579a MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.0, AGPL-1.0

Large files files are truncated, but you can click here to view the full file

  1. /*
  2. * in2000.c - Linux device driver for the
  3. * Always IN2000 ISA SCSI card.
  4. *
  5. * Copyright (c) 1996 John Shifflett, GeoLog Consulting
  6. * john@geolog.com
  7. * jshiffle@netcom.com
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2, or (at your option)
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * For the avoidance of doubt the "preferred form" of this code is one which
  20. * is in an open non patent encumbered format. Where cryptographic key signing
  21. * forms part of the process of creating an executable the information
  22. * including keys needed to generate an equivalently functional executable
  23. * are deemed to be part of the source code.
  24. *
  25. * Drew Eckhardt's excellent 'Generic NCR5380' sources provided
  26. * much of the inspiration and some of the code for this driver.
  27. * The Linux IN2000 driver distributed in the Linux kernels through
  28. * version 1.2.13 was an extremely valuable reference on the arcane
  29. * (and still mysterious) workings of the IN2000's fifo. It also
  30. * is where I lifted in2000_biosparam(), the gist of the card
  31. * detection scheme, and other bits of code. Many thanks to the
  32. * talented and courageous people who wrote, contributed to, and
  33. * maintained that driver (including Brad McLean, Shaun Savage,
  34. * Bill Earnest, Larry Doolittle, Roger Sunshine, John Luckey,
  35. * Matt Postiff, Peter Lu, zerucha@shell.portal.com, and Eric
  36. * Youngdale). I should also mention the driver written by
  37. * Hamish Macdonald for the (GASP!) Amiga A2091 card, included
  38. * in the Linux-m68k distribution; it gave me a good initial
  39. * understanding of the proper way to run a WD33c93 chip, and I
  40. * ended up stealing lots of code from it.
  41. *
  42. * _This_ driver is (I feel) an improvement over the old one in
  43. * several respects:
  44. * - All problems relating to the data size of a SCSI request are
  45. * gone (as far as I know). The old driver couldn't handle
  46. * swapping to partitions because that involved 4k blocks, nor
  47. * could it deal with the st.c tape driver unmodified, because
  48. * that usually involved 4k - 32k blocks. The old driver never
  49. * quite got away from a morbid dependence on 2k block sizes -
  50. * which of course is the size of the card's fifo.
  51. *
  52. * - Target Disconnection/Reconnection is now supported. Any
  53. * system with more than one device active on the SCSI bus
  54. * will benefit from this. The driver defaults to what I'm
  55. * calling 'adaptive disconnect' - meaning that each command
  56. * is evaluated individually as to whether or not it should
  57. * be run with the option to disconnect/reselect (if the
  58. * device chooses), or as a "SCSI-bus-hog".
  59. *
  60. * - Synchronous data transfers are now supported. Because there
  61. * are a few devices (and many improperly terminated systems)
  62. * that choke when doing sync, the default is sync DISABLED
  63. * for all devices. This faster protocol can (and should!)
  64. * be enabled on selected devices via the command-line.
  65. *
  66. * - Runtime operating parameters can now be specified through
  67. * either the LILO or the 'insmod' command line. For LILO do:
  68. * "in2000=blah,blah,blah"
  69. * and with insmod go like:
  70. * "insmod /usr/src/linux/modules/in2000.o setup_strings=blah,blah"
  71. * The defaults should be good for most people. See the comment
  72. * for 'setup_strings' below for more details.
  73. *
  74. * - The old driver relied exclusively on what the Western Digital
  75. * docs call "Combination Level 2 Commands", which are a great
  76. * idea in that the CPU is relieved of a lot of interrupt
  77. * overhead. However, by accepting a certain (user-settable)
  78. * amount of additional interrupts, this driver achieves
  79. * better control over the SCSI bus, and data transfers are
  80. * almost as fast while being much easier to define, track,
  81. * and debug.
  82. *
  83. * - You can force detection of a card whose BIOS has been disabled.
  84. *
  85. * - Multiple IN2000 cards might almost be supported. I've tried to
  86. * keep it in mind, but have no way to test...
  87. *
  88. *
  89. * TODO:
  90. * tagged queuing. multiple cards.
  91. *
  92. *
  93. * NOTE:
  94. * When using this or any other SCSI driver as a module, you'll
  95. * find that with the stock kernel, at most _two_ SCSI hard
  96. * drives will be linked into the device list (ie, usable).
  97. * If your IN2000 card has more than 2 disks on its bus, you
  98. * might want to change the define of 'SD_EXTRA_DEVS' in the
  99. * 'hosts.h' file from 2 to whatever is appropriate. It took
  100. * me a while to track down this surprisingly obscure and
  101. * undocumented little "feature".
  102. *
  103. *
  104. * People with bug reports, wish-lists, complaints, comments,
  105. * or improvements are asked to pah-leeez email me (John Shifflett)
  106. * at john@geolog.com or jshiffle@netcom.com! I'm anxious to get
  107. * this thing into as good a shape as possible, and I'm positive
  108. * there are lots of lurking bugs and "Stupid Places".
  109. *
  110. * Updated for Linux 2.5 by Alan Cox <alan@lxorguk.ukuu.org.uk>
  111. * - Using new_eh handler
  112. * - Hopefully got all the locking right again
  113. * See "FIXME" notes for items that could do with more work
  114. */
  115. #include <linux/module.h>
  116. #include <linux/blkdev.h>
  117. #include <linux/interrupt.h>
  118. #include <linux/string.h>
  119. #include <linux/delay.h>
  120. #include <linux/proc_fs.h>
  121. #include <linux/ioport.h>
  122. #include <linux/stat.h>
  123. #include <asm/io.h>
  124. #include <asm/system.h>
  125. #include "scsi.h"
  126. #include <scsi/scsi_host.h>
  127. #define IN2000_VERSION "1.33-2.5"
  128. #define IN2000_DATE "2002/11/03"
  129. #include "in2000.h"
  130. /*
  131. * 'setup_strings' is a single string used to pass operating parameters and
  132. * settings from the kernel/module command-line to the driver. 'setup_args[]'
  133. * is an array of strings that define the compile-time default values for
  134. * these settings. If Linux boots with a LILO or insmod command-line, those
  135. * settings are combined with 'setup_args[]'. Note that LILO command-lines
  136. * are prefixed with "in2000=" while insmod uses a "setup_strings=" prefix.
  137. * The driver recognizes the following keywords (lower case required) and
  138. * arguments:
  139. *
  140. * - ioport:addr -Where addr is IO address of a (usually ROM-less) card.
  141. * - noreset -No optional args. Prevents SCSI bus reset at boot time.
  142. * - nosync:x -x is a bitmask where the 1st 7 bits correspond with
  143. * the 7 possible SCSI devices (bit 0 for device #0, etc).
  144. * Set a bit to PREVENT sync negotiation on that device.
  145. * The driver default is sync DISABLED on all devices.
  146. * - period:ns -ns is the minimum # of nanoseconds in a SCSI data transfer
  147. * period. Default is 500; acceptable values are 250 - 1000.
  148. * - disconnect:x -x = 0 to never allow disconnects, 2 to always allow them.
  149. * x = 1 does 'adaptive' disconnects, which is the default
  150. * and generally the best choice.
  151. * - debug:x -If 'DEBUGGING_ON' is defined, x is a bitmask that causes
  152. * various types of debug output to printed - see the DB_xxx
  153. * defines in in2000.h
  154. * - proc:x -If 'PROC_INTERFACE' is defined, x is a bitmask that
  155. * determines how the /proc interface works and what it
  156. * does - see the PR_xxx defines in in2000.h
  157. *
  158. * Syntax Notes:
  159. * - Numeric arguments can be decimal or the '0x' form of hex notation. There
  160. * _must_ be a colon between a keyword and its numeric argument, with no
  161. * spaces.
  162. * - Keywords are separated by commas, no spaces, in the standard kernel
  163. * command-line manner.
  164. * - A keyword in the 'nth' comma-separated command-line member will overwrite
  165. * the 'nth' element of setup_args[]. A blank command-line member (in
  166. * other words, a comma with no preceding keyword) will _not_ overwrite
  167. * the corresponding setup_args[] element.
  168. *
  169. * A few LILO examples (for insmod, use 'setup_strings' instead of 'in2000'):
  170. * - in2000=ioport:0x220,noreset
  171. * - in2000=period:250,disconnect:2,nosync:0x03
  172. * - in2000=debug:0x1e
  173. * - in2000=proc:3
  174. */
  175. /* Normally, no defaults are specified... */
  176. static char *setup_args[] = { "", "", "", "", "", "", "", "", "" };
  177. /* filled in by 'insmod' */
  178. static char *setup_strings;
  179. module_param(setup_strings, charp, 0);
  180. static inline uchar read_3393(struct IN2000_hostdata *hostdata, uchar reg_num)
  181. {
  182. write1_io(reg_num, IO_WD_ADDR);
  183. return read1_io(IO_WD_DATA);
  184. }
  185. #define READ_AUX_STAT() read1_io(IO_WD_ASR)
  186. static inline void write_3393(struct IN2000_hostdata *hostdata, uchar reg_num, uchar value)
  187. {
  188. write1_io(reg_num, IO_WD_ADDR);
  189. write1_io(value, IO_WD_DATA);
  190. }
  191. static inline void write_3393_cmd(struct IN2000_hostdata *hostdata, uchar cmd)
  192. {
  193. /* while (READ_AUX_STAT() & ASR_CIP)
  194. printk("|");*/
  195. write1_io(WD_COMMAND, IO_WD_ADDR);
  196. write1_io(cmd, IO_WD_DATA);
  197. }
  198. static uchar read_1_byte(struct IN2000_hostdata *hostdata)
  199. {
  200. uchar asr, x = 0;
  201. write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
  202. write_3393_cmd(hostdata, WD_CMD_TRANS_INFO | 0x80);
  203. do {
  204. asr = READ_AUX_STAT();
  205. if (asr & ASR_DBR)
  206. x = read_3393(hostdata, WD_DATA);
  207. } while (!(asr & ASR_INT));
  208. return x;
  209. }
  210. static void write_3393_count(struct IN2000_hostdata *hostdata, unsigned long value)
  211. {
  212. write1_io(WD_TRANSFER_COUNT_MSB, IO_WD_ADDR);
  213. write1_io((value >> 16), IO_WD_DATA);
  214. write1_io((value >> 8), IO_WD_DATA);
  215. write1_io(value, IO_WD_DATA);
  216. }
  217. static unsigned long read_3393_count(struct IN2000_hostdata *hostdata)
  218. {
  219. unsigned long value;
  220. write1_io(WD_TRANSFER_COUNT_MSB, IO_WD_ADDR);
  221. value = read1_io(IO_WD_DATA) << 16;
  222. value |= read1_io(IO_WD_DATA) << 8;
  223. value |= read1_io(IO_WD_DATA);
  224. return value;
  225. }
  226. /* The 33c93 needs to be told which direction a command transfers its
  227. * data; we use this function to figure it out. Returns true if there
  228. * will be a DATA_OUT phase with this command, false otherwise.
  229. * (Thanks to Joerg Dorchain for the research and suggestion.)
  230. */
  231. static int is_dir_out(Scsi_Cmnd * cmd)
  232. {
  233. switch (cmd->cmnd[0]) {
  234. case WRITE_6:
  235. case WRITE_10:
  236. case WRITE_12:
  237. case WRITE_LONG:
  238. case WRITE_SAME:
  239. case WRITE_BUFFER:
  240. case WRITE_VERIFY:
  241. case WRITE_VERIFY_12:
  242. case COMPARE:
  243. case COPY:
  244. case COPY_VERIFY:
  245. case SEARCH_EQUAL:
  246. case SEARCH_HIGH:
  247. case SEARCH_LOW:
  248. case SEARCH_EQUAL_12:
  249. case SEARCH_HIGH_12:
  250. case SEARCH_LOW_12:
  251. case FORMAT_UNIT:
  252. case REASSIGN_BLOCKS:
  253. case RESERVE:
  254. case MODE_SELECT:
  255. case MODE_SELECT_10:
  256. case LOG_SELECT:
  257. case SEND_DIAGNOSTIC:
  258. case CHANGE_DEFINITION:
  259. case UPDATE_BLOCK:
  260. case SET_WINDOW:
  261. case MEDIUM_SCAN:
  262. case SEND_VOLUME_TAG:
  263. case 0xea:
  264. return 1;
  265. default:
  266. return 0;
  267. }
  268. }
  269. static struct sx_period sx_table[] = {
  270. {1, 0x20},
  271. {252, 0x20},
  272. {376, 0x30},
  273. {500, 0x40},
  274. {624, 0x50},
  275. {752, 0x60},
  276. {876, 0x70},
  277. {1000, 0x00},
  278. {0, 0}
  279. };
  280. static int round_period(unsigned int period)
  281. {
  282. int x;
  283. for (x = 1; sx_table[x].period_ns; x++) {
  284. if ((period <= sx_table[x - 0].period_ns) && (period > sx_table[x - 1].period_ns)) {
  285. return x;
  286. }
  287. }
  288. return 7;
  289. }
  290. static uchar calc_sync_xfer(unsigned int period, unsigned int offset)
  291. {
  292. uchar result;
  293. period *= 4; /* convert SDTR code to ns */
  294. result = sx_table[round_period(period)].reg_value;
  295. result |= (offset < OPTIMUM_SX_OFF) ? offset : OPTIMUM_SX_OFF;
  296. return result;
  297. }
  298. static void in2000_execute(struct Scsi_Host *instance);
  299. static int in2000_queuecommand_lck(Scsi_Cmnd * cmd, void (*done) (Scsi_Cmnd *))
  300. {
  301. struct Scsi_Host *instance;
  302. struct IN2000_hostdata *hostdata;
  303. Scsi_Cmnd *tmp;
  304. instance = cmd->device->host;
  305. hostdata = (struct IN2000_hostdata *) instance->hostdata;
  306. DB(DB_QUEUE_COMMAND, scmd_printk(KERN_DEBUG, cmd, "Q-%02x(", cmd->cmnd[0]))
  307. /* Set up a few fields in the Scsi_Cmnd structure for our own use:
  308. * - host_scribble is the pointer to the next cmd in the input queue
  309. * - scsi_done points to the routine we call when a cmd is finished
  310. * - result is what you'd expect
  311. */
  312. cmd->host_scribble = NULL;
  313. cmd->scsi_done = done;
  314. cmd->result = 0;
  315. /* We use the Scsi_Pointer structure that's included with each command
  316. * as a scratchpad (as it's intended to be used!). The handy thing about
  317. * the SCp.xxx fields is that they're always associated with a given
  318. * cmd, and are preserved across disconnect-reselect. This means we
  319. * can pretty much ignore SAVE_POINTERS and RESTORE_POINTERS messages
  320. * if we keep all the critical pointers and counters in SCp:
  321. * - SCp.ptr is the pointer into the RAM buffer
  322. * - SCp.this_residual is the size of that buffer
  323. * - SCp.buffer points to the current scatter-gather buffer
  324. * - SCp.buffers_residual tells us how many S.G. buffers there are
  325. * - SCp.have_data_in helps keep track of >2048 byte transfers
  326. * - SCp.sent_command is not used
  327. * - SCp.phase records this command's SRCID_ER bit setting
  328. */
  329. if (scsi_bufflen(cmd)) {
  330. cmd->SCp.buffer = scsi_sglist(cmd);
  331. cmd->SCp.buffers_residual = scsi_sg_count(cmd) - 1;
  332. cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
  333. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  334. } else {
  335. cmd->SCp.buffer = NULL;
  336. cmd->SCp.buffers_residual = 0;
  337. cmd->SCp.ptr = NULL;
  338. cmd->SCp.this_residual = 0;
  339. }
  340. cmd->SCp.have_data_in = 0;
  341. /* We don't set SCp.phase here - that's done in in2000_execute() */
  342. /* WD docs state that at the conclusion of a "LEVEL2" command, the
  343. * status byte can be retrieved from the LUN register. Apparently,
  344. * this is the case only for *uninterrupted* LEVEL2 commands! If
  345. * there are any unexpected phases entered, even if they are 100%
  346. * legal (different devices may choose to do things differently),
  347. * the LEVEL2 command sequence is exited. This often occurs prior
  348. * to receiving the status byte, in which case the driver does a
  349. * status phase interrupt and gets the status byte on its own.
  350. * While such a command can then be "resumed" (ie restarted to
  351. * finish up as a LEVEL2 command), the LUN register will NOT be
  352. * a valid status byte at the command's conclusion, and we must
  353. * use the byte obtained during the earlier interrupt. Here, we
  354. * preset SCp.Status to an illegal value (0xff) so that when
  355. * this command finally completes, we can tell where the actual
  356. * status byte is stored.
  357. */
  358. cmd->SCp.Status = ILLEGAL_STATUS_BYTE;
  359. /* We need to disable interrupts before messing with the input
  360. * queue and calling in2000_execute().
  361. */
  362. /*
  363. * Add the cmd to the end of 'input_Q'. Note that REQUEST_SENSE
  364. * commands are added to the head of the queue so that the desired
  365. * sense data is not lost before REQUEST_SENSE executes.
  366. */
  367. if (!(hostdata->input_Q) || (cmd->cmnd[0] == REQUEST_SENSE)) {
  368. cmd->host_scribble = (uchar *) hostdata->input_Q;
  369. hostdata->input_Q = cmd;
  370. } else { /* find the end of the queue */
  371. for (tmp = (Scsi_Cmnd *) hostdata->input_Q; tmp->host_scribble; tmp = (Scsi_Cmnd *) tmp->host_scribble);
  372. tmp->host_scribble = (uchar *) cmd;
  373. }
  374. /* We know that there's at least one command in 'input_Q' now.
  375. * Go see if any of them are runnable!
  376. */
  377. in2000_execute(cmd->device->host);
  378. DB(DB_QUEUE_COMMAND, printk(")Q "))
  379. return 0;
  380. }
  381. static DEF_SCSI_QCMD(in2000_queuecommand)
  382. /*
  383. * This routine attempts to start a scsi command. If the host_card is
  384. * already connected, we give up immediately. Otherwise, look through
  385. * the input_Q, using the first command we find that's intended
  386. * for a currently non-busy target/lun.
  387. * Note that this function is always called with interrupts already
  388. * disabled (either from in2000_queuecommand() or in2000_intr()).
  389. */
  390. static void in2000_execute(struct Scsi_Host *instance)
  391. {
  392. struct IN2000_hostdata *hostdata;
  393. Scsi_Cmnd *cmd, *prev;
  394. int i;
  395. unsigned short *sp;
  396. unsigned short f;
  397. unsigned short flushbuf[16];
  398. hostdata = (struct IN2000_hostdata *) instance->hostdata;
  399. DB(DB_EXECUTE, printk("EX("))
  400. if (hostdata->selecting || hostdata->connected) {
  401. DB(DB_EXECUTE, printk(")EX-0 "))
  402. return;
  403. }
  404. /*
  405. * Search through the input_Q for a command destined
  406. * for an idle target/lun.
  407. */
  408. cmd = (Scsi_Cmnd *) hostdata->input_Q;
  409. prev = NULL;
  410. while (cmd) {
  411. if (!(hostdata->busy[cmd->device->id] & (1 << cmd->device->lun)))
  412. break;
  413. prev = cmd;
  414. cmd = (Scsi_Cmnd *) cmd->host_scribble;
  415. }
  416. /* quit if queue empty or all possible targets are busy */
  417. if (!cmd) {
  418. DB(DB_EXECUTE, printk(")EX-1 "))
  419. return;
  420. }
  421. /* remove command from queue */
  422. if (prev)
  423. prev->host_scribble = cmd->host_scribble;
  424. else
  425. hostdata->input_Q = (Scsi_Cmnd *) cmd->host_scribble;
  426. #ifdef PROC_STATISTICS
  427. hostdata->cmd_cnt[cmd->device->id]++;
  428. #endif
  429. /*
  430. * Start the selection process
  431. */
  432. if (is_dir_out(cmd))
  433. write_3393(hostdata, WD_DESTINATION_ID, cmd->device->id);
  434. else
  435. write_3393(hostdata, WD_DESTINATION_ID, cmd->device->id | DSTID_DPD);
  436. /* Now we need to figure out whether or not this command is a good
  437. * candidate for disconnect/reselect. We guess to the best of our
  438. * ability, based on a set of hierarchical rules. When several
  439. * devices are operating simultaneously, disconnects are usually
  440. * an advantage. In a single device system, or if only 1 device
  441. * is being accessed, transfers usually go faster if disconnects
  442. * are not allowed:
  443. *
  444. * + Commands should NEVER disconnect if hostdata->disconnect =
  445. * DIS_NEVER (this holds for tape drives also), and ALWAYS
  446. * disconnect if hostdata->disconnect = DIS_ALWAYS.
  447. * + Tape drive commands should always be allowed to disconnect.
  448. * + Disconnect should be allowed if disconnected_Q isn't empty.
  449. * + Commands should NOT disconnect if input_Q is empty.
  450. * + Disconnect should be allowed if there are commands in input_Q
  451. * for a different target/lun. In this case, the other commands
  452. * should be made disconnect-able, if not already.
  453. *
  454. * I know, I know - this code would flunk me out of any
  455. * "C Programming 101" class ever offered. But it's easy
  456. * to change around and experiment with for now.
  457. */
  458. cmd->SCp.phase = 0; /* assume no disconnect */
  459. if (hostdata->disconnect == DIS_NEVER)
  460. goto no;
  461. if (hostdata->disconnect == DIS_ALWAYS)
  462. goto yes;
  463. if (cmd->device->type == 1) /* tape drive? */
  464. goto yes;
  465. if (hostdata->disconnected_Q) /* other commands disconnected? */
  466. goto yes;
  467. if (!(hostdata->input_Q)) /* input_Q empty? */
  468. goto no;
  469. for (prev = (Scsi_Cmnd *) hostdata->input_Q; prev; prev = (Scsi_Cmnd *) prev->host_scribble) {
  470. if ((prev->device->id != cmd->device->id) || (prev->device->lun != cmd->device->lun)) {
  471. for (prev = (Scsi_Cmnd *) hostdata->input_Q; prev; prev = (Scsi_Cmnd *) prev->host_scribble)
  472. prev->SCp.phase = 1;
  473. goto yes;
  474. }
  475. }
  476. goto no;
  477. yes:
  478. cmd->SCp.phase = 1;
  479. #ifdef PROC_STATISTICS
  480. hostdata->disc_allowed_cnt[cmd->device->id]++;
  481. #endif
  482. no:
  483. write_3393(hostdata, WD_SOURCE_ID, ((cmd->SCp.phase) ? SRCID_ER : 0));
  484. write_3393(hostdata, WD_TARGET_LUN, cmd->device->lun);
  485. write_3393(hostdata, WD_SYNCHRONOUS_TRANSFER, hostdata->sync_xfer[cmd->device->id]);
  486. hostdata->busy[cmd->device->id] |= (1 << cmd->device->lun);
  487. if ((hostdata->level2 <= L2_NONE) || (hostdata->sync_stat[cmd->device->id] == SS_UNSET)) {
  488. /*
  489. * Do a 'Select-With-ATN' command. This will end with
  490. * one of the following interrupts:
  491. * CSR_RESEL_AM: failure - can try again later.
  492. * CSR_TIMEOUT: failure - give up.
  493. * CSR_SELECT: success - proceed.
  494. */
  495. hostdata->selecting = cmd;
  496. /* Every target has its own synchronous transfer setting, kept in
  497. * the sync_xfer array, and a corresponding status byte in sync_stat[].
  498. * Each target's sync_stat[] entry is initialized to SS_UNSET, and its
  499. * sync_xfer[] entry is initialized to the default/safe value. SS_UNSET
  500. * means that the parameters are undetermined as yet, and that we
  501. * need to send an SDTR message to this device after selection is
  502. * complete. We set SS_FIRST to tell the interrupt routine to do so,
  503. * unless we don't want to even _try_ synchronous transfers: In this
  504. * case we set SS_SET to make the defaults final.
  505. */
  506. if (hostdata->sync_stat[cmd->device->id] == SS_UNSET) {
  507. if (hostdata->sync_off & (1 << cmd->device->id))
  508. hostdata->sync_stat[cmd->device->id] = SS_SET;
  509. else
  510. hostdata->sync_stat[cmd->device->id] = SS_FIRST;
  511. }
  512. hostdata->state = S_SELECTING;
  513. write_3393_count(hostdata, 0); /* this guarantees a DATA_PHASE interrupt */
  514. write_3393_cmd(hostdata, WD_CMD_SEL_ATN);
  515. }
  516. else {
  517. /*
  518. * Do a 'Select-With-ATN-Xfer' command. This will end with
  519. * one of the following interrupts:
  520. * CSR_RESEL_AM: failure - can try again later.
  521. * CSR_TIMEOUT: failure - give up.
  522. * anything else: success - proceed.
  523. */
  524. hostdata->connected = cmd;
  525. write_3393(hostdata, WD_COMMAND_PHASE, 0);
  526. /* copy command_descriptor_block into WD chip
  527. * (take advantage of auto-incrementing)
  528. */
  529. write1_io(WD_CDB_1, IO_WD_ADDR);
  530. for (i = 0; i < cmd->cmd_len; i++)
  531. write1_io(cmd->cmnd[i], IO_WD_DATA);
  532. /* The wd33c93 only knows about Group 0, 1, and 5 commands when
  533. * it's doing a 'select-and-transfer'. To be safe, we write the
  534. * size of the CDB into the OWN_ID register for every case. This
  535. * way there won't be problems with vendor-unique, audio, etc.
  536. */
  537. write_3393(hostdata, WD_OWN_ID, cmd->cmd_len);
  538. /* When doing a non-disconnect command, we can save ourselves a DATA
  539. * phase interrupt later by setting everything up now. With writes we
  540. * need to pre-fill the fifo; if there's room for the 32 flush bytes,
  541. * put them in there too - that'll avoid a fifo interrupt. Reads are
  542. * somewhat simpler.
  543. * KLUDGE NOTE: It seems that you can't completely fill the fifo here:
  544. * This results in the IO_FIFO_COUNT register rolling over to zero,
  545. * and apparently the gate array logic sees this as empty, not full,
  546. * so the 3393 chip is never signalled to start reading from the
  547. * fifo. Or maybe it's seen as a permanent fifo interrupt condition.
  548. * Regardless, we fix this by temporarily pretending that the fifo
  549. * is 16 bytes smaller. (I see now that the old driver has a comment
  550. * about "don't fill completely" in an analogous place - must be the
  551. * same deal.) This results in CDROM, swap partitions, and tape drives
  552. * needing an extra interrupt per write command - I think we can live
  553. * with that!
  554. */
  555. if (!(cmd->SCp.phase)) {
  556. write_3393_count(hostdata, cmd->SCp.this_residual);
  557. write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_BUS);
  558. write1_io(0, IO_FIFO_WRITE); /* clear fifo counter, write mode */
  559. if (is_dir_out(cmd)) {
  560. hostdata->fifo = FI_FIFO_WRITING;
  561. if ((i = cmd->SCp.this_residual) > (IN2000_FIFO_SIZE - 16))
  562. i = IN2000_FIFO_SIZE - 16;
  563. cmd->SCp.have_data_in = i; /* this much data in fifo */
  564. i >>= 1; /* Gulp. Assuming modulo 2. */
  565. sp = (unsigned short *) cmd->SCp.ptr;
  566. f = hostdata->io_base + IO_FIFO;
  567. #ifdef FAST_WRITE_IO
  568. FAST_WRITE2_IO();
  569. #else
  570. while (i--)
  571. write2_io(*sp++, IO_FIFO);
  572. #endif
  573. /* Is there room for the flush bytes? */
  574. if (cmd->SCp.have_data_in <= ((IN2000_FIFO_SIZE - 16) - 32)) {
  575. sp = flushbuf;
  576. i = 16;
  577. #ifdef FAST_WRITE_IO
  578. FAST_WRITE2_IO();
  579. #else
  580. while (i--)
  581. write2_io(0, IO_FIFO);
  582. #endif
  583. }
  584. }
  585. else {
  586. write1_io(0, IO_FIFO_READ); /* put fifo in read mode */
  587. hostdata->fifo = FI_FIFO_READING;
  588. cmd->SCp.have_data_in = 0; /* nothing transferred yet */
  589. }
  590. } else {
  591. write_3393_count(hostdata, 0); /* this guarantees a DATA_PHASE interrupt */
  592. }
  593. hostdata->state = S_RUNNING_LEVEL2;
  594. write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
  595. }
  596. /*
  597. * Since the SCSI bus can handle only 1 connection at a time,
  598. * we get out of here now. If the selection fails, or when
  599. * the command disconnects, we'll come back to this routine
  600. * to search the input_Q again...
  601. */
  602. DB(DB_EXECUTE, printk("%s)EX-2 ", (cmd->SCp.phase) ? "d:" : ""))
  603. }
  604. static void transfer_pio(uchar * buf, int cnt, int data_in_dir, struct IN2000_hostdata *hostdata)
  605. {
  606. uchar asr;
  607. DB(DB_TRANSFER, printk("(%p,%d,%s)", buf, cnt, data_in_dir ? "in" : "out"))
  608. write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_POLLED);
  609. write_3393_count(hostdata, cnt);
  610. write_3393_cmd(hostdata, WD_CMD_TRANS_INFO);
  611. if (data_in_dir) {
  612. do {
  613. asr = READ_AUX_STAT();
  614. if (asr & ASR_DBR)
  615. *buf++ = read_3393(hostdata, WD_DATA);
  616. } while (!(asr & ASR_INT));
  617. } else {
  618. do {
  619. asr = READ_AUX_STAT();
  620. if (asr & ASR_DBR)
  621. write_3393(hostdata, WD_DATA, *buf++);
  622. } while (!(asr & ASR_INT));
  623. }
  624. /* Note: we are returning with the interrupt UN-cleared.
  625. * Since (presumably) an entire I/O operation has
  626. * completed, the bus phase is probably different, and
  627. * the interrupt routine will discover this when it
  628. * responds to the uncleared int.
  629. */
  630. }
  631. static void transfer_bytes(Scsi_Cmnd * cmd, int data_in_dir)
  632. {
  633. struct IN2000_hostdata *hostdata;
  634. unsigned short *sp;
  635. unsigned short f;
  636. int i;
  637. hostdata = (struct IN2000_hostdata *) cmd->device->host->hostdata;
  638. /* Normally, you'd expect 'this_residual' to be non-zero here.
  639. * In a series of scatter-gather transfers, however, this
  640. * routine will usually be called with 'this_residual' equal
  641. * to 0 and 'buffers_residual' non-zero. This means that a
  642. * previous transfer completed, clearing 'this_residual', and
  643. * now we need to setup the next scatter-gather buffer as the
  644. * source or destination for THIS transfer.
  645. */
  646. if (!cmd->SCp.this_residual && cmd->SCp.buffers_residual) {
  647. ++cmd->SCp.buffer;
  648. --cmd->SCp.buffers_residual;
  649. cmd->SCp.this_residual = cmd->SCp.buffer->length;
  650. cmd->SCp.ptr = sg_virt(cmd->SCp.buffer);
  651. }
  652. /* Set up hardware registers */
  653. write_3393(hostdata, WD_SYNCHRONOUS_TRANSFER, hostdata->sync_xfer[cmd->device->id]);
  654. write_3393_count(hostdata, cmd->SCp.this_residual);
  655. write_3393(hostdata, WD_CONTROL, CTRL_IDI | CTRL_EDI | CTRL_BUS);
  656. write1_io(0, IO_FIFO_WRITE); /* zero counter, assume write */
  657. /* Reading is easy. Just issue the command and return - we'll
  658. * get an interrupt later when we have actual data to worry about.
  659. */
  660. if (data_in_dir) {
  661. write1_io(0, IO_FIFO_READ);
  662. if ((hostdata->level2 >= L2_DATA) || (hostdata->level2 == L2_BASIC && cmd->SCp.phase == 0)) {
  663. write_3393(hostdata, WD_COMMAND_PHASE, 0x45);
  664. write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
  665. hostdata->state = S_RUNNING_LEVEL2;
  666. } else
  667. write_3393_cmd(hostdata, WD_CMD_TRANS_INFO);
  668. hostdata->fifo = FI_FIFO_READING;
  669. cmd->SCp.have_data_in = 0;
  670. return;
  671. }
  672. /* Writing is more involved - we'll start the WD chip and write as
  673. * much data to the fifo as we can right now. Later interrupts will
  674. * write any bytes that don't make it at this stage.
  675. */
  676. if ((hostdata->level2 >= L2_DATA) || (hostdata->level2 == L2_BASIC && cmd->SCp.phase == 0)) {
  677. write_3393(hostdata, WD_COMMAND_PHASE, 0x45);
  678. write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
  679. hostdata->state = S_RUNNING_LEVEL2;
  680. } else
  681. write_3393_cmd(hostdata, WD_CMD_TRANS_INFO);
  682. hostdata->fifo = FI_FIFO_WRITING;
  683. sp = (unsigned short *) cmd->SCp.ptr;
  684. if ((i = cmd->SCp.this_residual) > IN2000_FIFO_SIZE)
  685. i = IN2000_FIFO_SIZE;
  686. cmd->SCp.have_data_in = i;
  687. i >>= 1; /* Gulp. We assume this_residual is modulo 2 */
  688. f = hostdata->io_base + IO_FIFO;
  689. #ifdef FAST_WRITE_IO
  690. FAST_WRITE2_IO();
  691. #else
  692. while (i--)
  693. write2_io(*sp++, IO_FIFO);
  694. #endif
  695. }
  696. /* We need to use spin_lock_irqsave() & spin_unlock_irqrestore() in this
  697. * function in order to work in an SMP environment. (I'd be surprised
  698. * if the driver is ever used by anyone on a real multi-CPU motherboard,
  699. * but it _does_ need to be able to compile and run in an SMP kernel.)
  700. */
  701. static irqreturn_t in2000_intr(int irqnum, void *dev_id)
  702. {
  703. struct Scsi_Host *instance = dev_id;
  704. struct IN2000_hostdata *hostdata;
  705. Scsi_Cmnd *patch, *cmd;
  706. uchar asr, sr, phs, id, lun, *ucp, msg;
  707. int i, j;
  708. unsigned long length;
  709. unsigned short *sp;
  710. unsigned short f;
  711. unsigned long flags;
  712. hostdata = (struct IN2000_hostdata *) instance->hostdata;
  713. /* Get the spin_lock and disable further ints, for SMP */
  714. spin_lock_irqsave(instance->host_lock, flags);
  715. #ifdef PROC_STATISTICS
  716. hostdata->int_cnt++;
  717. #endif
  718. /* The IN2000 card has 2 interrupt sources OR'ed onto its IRQ line - the
  719. * WD3393 chip and the 2k fifo (which is actually a dual-port RAM combined
  720. * with a big logic array, so it's a little different than what you might
  721. * expect). As far as I know, there's no reason that BOTH can't be active
  722. * at the same time, but there's a problem: while we can read the 3393
  723. * to tell if _it_ wants an interrupt, I don't know of a way to ask the
  724. * fifo the same question. The best we can do is check the 3393 and if
  725. * it _isn't_ the source of the interrupt, then we can be pretty sure
  726. * that the fifo is the culprit.
  727. * UPDATE: I have it on good authority (Bill Earnest) that bit 0 of the
  728. * IO_FIFO_COUNT register mirrors the fifo interrupt state. I
  729. * assume that bit clear means interrupt active. As it turns
  730. * out, the driver really doesn't need to check for this after
  731. * all, so my remarks above about a 'problem' can safely be
  732. * ignored. The way the logic is set up, there's no advantage
  733. * (that I can see) to worrying about it.
  734. *
  735. * It seems that the fifo interrupt signal is negated when we extract
  736. * bytes during read or write bytes during write.
  737. * - fifo will interrupt when data is moving from it to the 3393, and
  738. * there are 31 (or less?) bytes left to go. This is sort of short-
  739. * sighted: what if you don't WANT to do more? In any case, our
  740. * response is to push more into the fifo - either actual data or
  741. * dummy bytes if need be. Note that we apparently have to write at
  742. * least 32 additional bytes to the fifo after an interrupt in order
  743. * to get it to release the ones it was holding on to - writing fewer
  744. * than 32 will result in another fifo int.
  745. * UPDATE: Again, info from Bill Earnest makes this more understandable:
  746. * 32 bytes = two counts of the fifo counter register. He tells
  747. * me that the fifo interrupt is a non-latching signal derived
  748. * from a straightforward boolean interpretation of the 7
  749. * highest bits of the fifo counter and the fifo-read/fifo-write
  750. * state. Who'd a thought?
  751. */
  752. write1_io(0, IO_LED_ON);
  753. asr = READ_AUX_STAT();
  754. if (!(asr & ASR_INT)) { /* no WD33c93 interrupt? */
  755. /* Ok. This is definitely a FIFO-only interrupt.
  756. *
  757. * If FI_FIFO_READING is set, there are up to 2048 bytes waiting to be read,
  758. * maybe more to come from the SCSI bus. Read as many as we can out of the
  759. * fifo and into memory at the location of SCp.ptr[SCp.have_data_in], and
  760. * update have_data_in afterwards.
  761. *
  762. * If we have FI_FIFO_WRITING, the FIFO has almost run out of bytes to move
  763. * into the WD3393 chip (I think the interrupt happens when there are 31
  764. * bytes left, but it may be fewer...). The 3393 is still waiting, so we
  765. * shove some more into the fifo, which gets things moving again. If the
  766. * original SCSI command specified more than 2048 bytes, there may still
  767. * be some of that data left: fine - use it (from SCp.ptr[SCp.have_data_in]).
  768. * Don't forget to update have_data_in. If we've already written out the
  769. * entire buffer, feed 32 dummy bytes to the fifo - they're needed to
  770. * push out the remaining real data.
  771. * (Big thanks to Bill Earnest for getting me out of the mud in here.)
  772. */
  773. cmd = (Scsi_Cmnd *) hostdata->connected; /* assume we're connected */
  774. CHECK_NULL(cmd, "fifo_int")
  775. if (hostdata->fifo == FI_FIFO_READING) {
  776. DB(DB_FIFO, printk("{R:%02x} ", read1_io(IO_FIFO_COUNT)))
  777. sp = (unsigned short *) (cmd->SCp.ptr + cmd->SCp.have_data_in);
  778. i = read1_io(IO_FIFO_COUNT) & 0xfe;
  779. i <<= 2; /* # of words waiting in the fifo */
  780. f = hostdata->io_base + IO_FIFO;
  781. #ifdef FAST_READ_IO
  782. FAST_READ2_IO();
  783. #else
  784. while (i--)
  785. *sp++ = read2_io(IO_FIFO);
  786. #endif
  787. i = sp - (unsigned short *) (cmd->SCp.ptr + cmd->SCp.have_data_in);
  788. i <<= 1;
  789. cmd->SCp.have_data_in += i;
  790. }
  791. else if (hostdata->fifo == FI_FIFO_WRITING) {
  792. DB(DB_FIFO, printk("{W:%02x} ", read1_io(IO_FIFO_COUNT)))
  793. /* If all bytes have been written to the fifo, flush out the stragglers.
  794. * Note that while writing 16 dummy words seems arbitrary, we don't
  795. * have another choice that I can see. What we really want is to read
  796. * the 3393 transfer count register (that would tell us how many bytes
  797. * needed flushing), but the TRANSFER_INFO command hasn't completed
  798. * yet (not enough bytes!) and that register won't be accessible. So,
  799. * we use 16 words - a number obtained through trial and error.
  800. * UPDATE: Bill says this is exactly what Always does, so there.
  801. * More thanks due him for help in this section.
  802. */
  803. if (cmd->SCp.this_residual == cmd->SCp.have_data_in) {
  804. i = 16;
  805. while (i--) /* write 32 dummy bytes */
  806. write2_io(0, IO_FIFO);
  807. }
  808. /* If there are still bytes left in the SCSI buffer, write as many as we
  809. * can out to the fifo.
  810. */
  811. else {
  812. sp = (unsigned short *) (cmd->SCp.ptr + cmd->SCp.have_data_in);
  813. i = cmd->SCp.this_residual - cmd->SCp.have_data_in; /* bytes yet to go */
  814. j = read1_io(IO_FIFO_COUNT) & 0xfe;
  815. j <<= 2; /* how many words the fifo has room for */
  816. if ((j << 1) > i)
  817. j = (i >> 1);
  818. while (j--)
  819. write2_io(*sp++, IO_FIFO);
  820. i = sp - (unsigned short *) (cmd->SCp.ptr + cmd->SCp.have_data_in);
  821. i <<= 1;
  822. cmd->SCp.have_data_in += i;
  823. }
  824. }
  825. else {
  826. printk("*** Spurious FIFO interrupt ***");
  827. }
  828. write1_io(0, IO_LED_OFF);
  829. /* release the SMP spin_lock and restore irq state */
  830. spin_unlock_irqrestore(instance->host_lock, flags);
  831. return IRQ_HANDLED;
  832. }
  833. /* This interrupt was triggered by the WD33c93 chip. The fifo interrupt
  834. * may also be asserted, but we don't bother to check it: we get more
  835. * detailed info from FIFO_READING and FIFO_WRITING (see below).
  836. */
  837. cmd = (Scsi_Cmnd *) hostdata->connected; /* assume we're connected */
  838. sr = read_3393(hostdata, WD_SCSI_STATUS); /* clear the interrupt */
  839. phs = read_3393(hostdata, WD_COMMAND_PHASE);
  840. if (!cmd && (sr != CSR_RESEL_AM && sr != CSR_TIMEOUT && sr != CSR_SELECT)) {
  841. printk("\nNR:wd-intr-1\n");
  842. write1_io(0, IO_LED_OFF);
  843. /* release the SMP spin_lock and restore irq state */
  844. spin_unlock_irqrestore(instance->host_lock, flags);
  845. return IRQ_HANDLED;
  846. }
  847. DB(DB_INTR, printk("{%02x:%02x-", asr, sr))
  848. /* After starting a FIFO-based transfer, the next _WD3393_ interrupt is
  849. * guaranteed to be in response to the completion of the transfer.
  850. * If we were reading, there's probably data in the fifo that needs
  851. * to be copied into RAM - do that here. Also, we have to update
  852. * 'this_residual' and 'ptr' based on the contents of the
  853. * TRANSFER_COUNT register, in case the device decided to do an
  854. * intermediate disconnect (a device may do this if it has to
  855. * do a seek, or just to be nice and let other devices have
  856. * some bus time during long transfers).
  857. * After doing whatever is necessary with the fifo, we go on and
  858. * service the WD3393 interrupt normally.
  859. */
  860. if (hostdata->fifo == FI_FIFO_READING) {
  861. /* buffer index = start-of-buffer + #-of-bytes-already-read */
  862. sp = (unsigned short *) (cmd->SCp.ptr + cmd->SCp.have_data_in);
  863. /* bytes remaining in fifo = (total-wanted - #-not-got) - #-already-read */
  864. i = (cmd->SCp.this_residual - read_3393_count(hostdata)) - cmd->SCp.have_data_in;
  865. i >>= 1; /* Gulp. We assume this will always be modulo 2 */
  866. f = hostdata->io_base + IO_FIFO;
  867. #ifdef FAST_READ_IO
  868. FAST_READ2_IO();
  869. #else
  870. while (i--)
  871. *sp++ = read2_io(IO_FIFO);
  872. #endif
  873. hostdata->fifo = FI_FIFO_UNUSED;
  874. length = cmd->SCp.this_residual;
  875. cmd->SCp.this_residual = read_3393_count(hostdata);
  876. cmd->SCp.ptr += (length - cmd->SCp.this_residual);
  877. DB(DB_TRANSFER, printk("(%p,%d)", cmd->SCp.ptr, cmd->SCp.this_residual))
  878. }
  879. else if (hostdata->fifo == FI_FIFO_WRITING) {
  880. hostdata->fifo = FI_FIFO_UNUSED;
  881. length = cmd->SCp.this_residual;
  882. cmd->SCp.this_residual = read_3393_count(hostdata);
  883. cmd->SCp.ptr += (length - cmd->SCp.this_residual);
  884. DB(DB_TRANSFER, printk("(%p,%d)", cmd->SCp.ptr, cmd->SCp.this_residual))
  885. }
  886. /* Respond to the specific WD3393 interrupt - there are quite a few! */
  887. switch (sr) {
  888. case CSR_TIMEOUT:
  889. DB(DB_INTR, printk("TIMEOUT"))
  890. if (hostdata->state == S_RUNNING_LEVEL2)
  891. hostdata->connected = NULL;
  892. else {
  893. cmd = (Scsi_Cmnd *) hostdata->selecting; /* get a valid cmd */
  894. CHECK_NULL(cmd, "csr_timeout")
  895. hostdata->selecting = NULL;
  896. }
  897. cmd->result = DID_NO_CONNECT << 16;
  898. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  899. hostdata->state = S_UNCONNECTED;
  900. cmd->scsi_done(cmd);
  901. /* We are not connected to a target - check to see if there
  902. * are commands waiting to be executed.
  903. */
  904. in2000_execute(instance);
  905. break;
  906. /* Note: this interrupt should not occur in a LEVEL2 command */
  907. case CSR_SELECT:
  908. DB(DB_INTR, printk("SELECT"))
  909. hostdata->connected = cmd = (Scsi_Cmnd *) hostdata->selecting;
  910. CHECK_NULL(cmd, "csr_select")
  911. hostdata->selecting = NULL;
  912. /* construct an IDENTIFY message with correct disconnect bit */
  913. hostdata->outgoing_msg[0] = (0x80 | 0x00 | cmd->device->lun);
  914. if (cmd->SCp.phase)
  915. hostdata->outgoing_msg[0] |= 0x40;
  916. if (hostdata->sync_stat[cmd->device->id] == SS_FIRST) {
  917. #ifdef SYNC_DEBUG
  918. printk(" sending SDTR ");
  919. #endif
  920. hostdata->sync_stat[cmd->device->id] = SS_WAITING;
  921. /* tack on a 2nd message to ask about synchronous transfers */
  922. hostdata->outgoing_msg[1] = EXTENDED_MESSAGE;
  923. hostdata->outgoing_msg[2] = 3;
  924. hostdata->outgoing_msg[3] = EXTENDED_SDTR;
  925. hostdata->outgoing_msg[4] = OPTIMUM_SX_PER / 4;
  926. hostdata->outgoing_msg[5] = OPTIMUM_SX_OFF;
  927. hostdata->outgoing_len = 6;
  928. } else
  929. hostdata->outgoing_len = 1;
  930. hostdata->state = S_CONNECTED;
  931. break;
  932. case CSR_XFER_DONE | PHS_DATA_IN:
  933. case CSR_UNEXP | PHS_DATA_IN:
  934. case CSR_SRV_REQ | PHS_DATA_IN:
  935. DB(DB_INTR, printk("IN-%d.%d", cmd->SCp.this_residual, cmd->SCp.buffers_residual))
  936. transfer_bytes(cmd, DATA_IN_DIR);
  937. if (hostdata->state != S_RUNNING_LEVEL2)
  938. hostdata->state = S_CONNECTED;
  939. break;
  940. case CSR_XFER_DONE | PHS_DATA_OUT:
  941. case CSR_UNEXP | PHS_DATA_OUT:
  942. case CSR_SRV_REQ | PHS_DATA_OUT:
  943. DB(DB_INTR, printk("OUT-%d.%d", cmd->SCp.this_residual, cmd->SCp.buffers_residual))
  944. transfer_bytes(cmd, DATA_OUT_DIR);
  945. if (hostdata->state != S_RUNNING_LEVEL2)
  946. hostdata->state = S_CONNECTED;
  947. break;
  948. /* Note: this interrupt should not occur in a LEVEL2 command */
  949. case CSR_XFER_DONE | PHS_COMMAND:
  950. case CSR_UNEXP | PHS_COMMAND:
  951. case CSR_SRV_REQ | PHS_COMMAND:
  952. DB(DB_INTR, printk("CMND-%02x", cmd->cmnd[0]))
  953. transfer_pio(cmd->cmnd, cmd->cmd_len, DATA_OUT_DIR, hostdata);
  954. hostdata->state = S_CONNECTED;
  955. break;
  956. case CSR_XFER_DONE | PHS_STATUS:
  957. case CSR_UNEXP | PHS_STATUS:
  958. case CSR_SRV_REQ | PHS_STATUS:
  959. DB(DB_INTR, printk("STATUS="))
  960. cmd->SCp.Status = read_1_byte(hostdata);
  961. DB(DB_INTR, printk("%02x", cmd->SCp.Status))
  962. if (hostdata->level2 >= L2_BASIC) {
  963. sr = read_3393(hostdata, WD_SCSI_STATUS); /* clear interrupt */
  964. hostdata->state = S_RUNNING_LEVEL2;
  965. write_3393(hostdata, WD_COMMAND_PHASE, 0x50);
  966. write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
  967. } else {
  968. hostdata->state = S_CONNECTED;
  969. }
  970. break;
  971. case CSR_XFER_DONE | PHS_MESS_IN:
  972. case CSR_UNEXP | PHS_MESS_IN:
  973. case CSR_SRV_REQ | PHS_MESS_IN:
  974. DB(DB_INTR, printk("MSG_IN="))
  975. msg = read_1_byte(hostdata);
  976. sr = read_3393(hostdata, WD_SCSI_STATUS); /* clear interrupt */
  977. hostdata->incoming_msg[hostdata->incoming_ptr] = msg;
  978. if (hostdata->incoming_msg[0] == EXTENDED_MESSAGE)
  979. msg = EXTENDED_MESSAGE;
  980. else
  981. hostdata->incoming_ptr = 0;
  982. cmd->SCp.Message = msg;
  983. switch (msg) {
  984. case COMMAND_COMPLETE:
  985. DB(DB_INTR, printk("CCMP"))
  986. write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
  987. hostdata->state = S_PRE_CMP_DISC;
  988. break;
  989. case SAVE_POINTERS:
  990. DB(DB_INTR, printk("SDP"))
  991. write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
  992. hostdata->state = S_CONNECTED;
  993. break;
  994. case RESTORE_POINTERS:
  995. DB(DB_INTR, printk("RDP"))
  996. if (hostdata->level2 >= L2_BASIC) {
  997. write_3393(hostdata, WD_COMMAND_PHASE, 0x45);
  998. write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
  999. hostdata->state = S_RUNNING_LEVEL2;
  1000. } else {
  1001. write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
  1002. hostdata->state = S_CONNECTED;
  1003. }
  1004. break;
  1005. case DISCONNECT:
  1006. DB(DB_INTR, printk("DIS"))
  1007. cmd->device->disconnect = 1;
  1008. write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
  1009. hostdata->state = S_PRE_TMP_DISC;
  1010. break;
  1011. case MESSAGE_REJECT:
  1012. DB(DB_INTR, printk("REJ"))
  1013. #ifdef SYNC_DEBUG
  1014. printk("-REJ-");
  1015. #endif
  1016. if (hostdata->sync_stat[cmd->device->id] == SS_WAITING)
  1017. hostdata->sync_stat[cmd->device->id] = SS_SET;
  1018. write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
  1019. hostdata->state = S_CONNECTED;
  1020. break;
  1021. case EXTENDED_MESSAGE:
  1022. DB(DB_INTR, printk("EXT"))
  1023. ucp = hostdata->incoming_msg;
  1024. #ifdef SYNC_DEBUG
  1025. printk("%02x", ucp[hostdata->incoming_ptr]);
  1026. #endif
  1027. /* Is this the last byte of the extended message? */
  1028. if ((hostdata->incoming_ptr >= 2) && (hostdata->incoming_ptr == (ucp[1] + 1))) {
  1029. switch (ucp[2]) { /* what's the EXTENDED code? */
  1030. case EXTENDED_SDTR:
  1031. id = calc_sync_xfer(ucp[3], ucp[4]);
  1032. if (hostdata->sync_stat[cmd->device->id] != SS_WAITING) {
  1033. /* A device has sent an unsolicited SDTR message; rather than go
  1034. * through the effort of decoding it and then figuring out what
  1035. * our reply should be, we're just gonna say that we have a
  1036. * synchronous fifo depth of 0. This will result in asynchronous
  1037. * transfers - not ideal but so much easier.
  1038. * Actually, this is OK because it assures us that if we don't
  1039. * specifically ask for sync transfers, we won't do any.
  1040. */
  1041. write_3393_cmd(hostdata, WD_CMD_ASSERT_ATN); /* want MESS_OUT */
  1042. hostdata->outgoing_msg[0] = EXTENDED_MESSAGE;
  1043. hostdata->outgoing_msg[1] = 3;
  1044. hostdata->outgoing_msg[2] = EXTENDED_SDTR;
  1045. hostdata->outgoing_msg[3] = hostdata->default_sx_per / 4;
  1046. hostdata->outgoing_msg[4] = 0;
  1047. hostdata->outgoing_len = 5;
  1048. hostdata->sync_xfer[cmd->device->id] = calc_sync_xfer(hostdata->default_sx_per / 4, 0);
  1049. } else {
  1050. hostdata->sync_xfer[cmd->device->id] = id;
  1051. }
  1052. #ifdef SYNC_DEBUG
  1053. printk("sync_xfer=%02x", hostdata->sync_xfer[cmd->device->id]);
  1054. #endif
  1055. hostdata->sync_stat[cmd->device->id] = SS_SET;
  1056. write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
  1057. hostdata->state = S_CONNECTED;
  1058. break;
  1059. case EXTENDED_WDTR:
  1060. write_3393_cmd(hostdata, WD_CMD_ASSERT_ATN); /* want MESS_OUT */
  1061. printk("sending WDTR ");
  1062. hostdata->outgoing_msg[0] = EXTENDED_MESSAGE;
  1063. hostdata->outgoing_msg[1] = 2;
  1064. hostdata->outgoing_msg[2] = EXTENDED_WDTR;
  1065. hostdata->outgoing_msg[3] = 0; /* 8 bit transfer width */
  1066. hostdata->outgoing_len = 4;
  1067. write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
  1068. hostdata->state = S_CONNECTED;
  1069. break;
  1070. default:
  1071. write_3393_cmd(hostdata, WD_CMD_ASSERT_ATN); /* want MESS_OUT */
  1072. printk("Rejecting Unknown Extended Message(%02x). ", ucp[2]);
  1073. hostdata->outgoing_msg[0] = MESSAGE_REJECT;
  1074. hostdata->outgoing_len = 1;
  1075. write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
  1076. hostdata->state = S_CONNECTED;
  1077. break;
  1078. }
  1079. hostdata->incoming_ptr = 0;
  1080. }
  1081. /* We need to read more MESS_IN bytes for the extended message */
  1082. else {
  1083. hostdata->incoming_ptr++;
  1084. write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
  1085. hostdata->state = S_CONNECTED;
  1086. }
  1087. break;
  1088. default:
  1089. printk("Rejecting Unknown Message(%02x) ", msg);
  1090. write_3393_cmd(hostdata, WD_CMD_ASSERT_ATN); /* want MESS_OUT */
  1091. hostdata->outgoing_msg[0] = MESSAGE_REJECT;
  1092. hostdata->outgoing_len = 1;
  1093. write_3393_cmd(hostdata, WD_CMD_NEGATE_ACK);
  1094. hostdata->state = S_CONNECTED;
  1095. }
  1096. break;
  1097. /* Note: this interrupt will occur only after a LEVEL2 command */
  1098. case CSR_SEL_XFER_DONE:
  1099. /* Make sure that reselection is enabled at this point - it may
  1100. * have been turned off for the command that just completed.
  1101. */
  1102. write_3393(hostdata, WD_SOURCE_ID, SRCID_ER);
  1103. if (phs == 0x60) {
  1104. DB(DB_INTR, printk("SX-DONE"))
  1105. cmd->SCp.Message = COMMAND_COMPLETE;
  1106. lun = read_3393(hostdata, WD_TARGET_LUN);
  1107. DB(DB_INTR, printk(":%d.%d", cmd->SCp.Status, lun))
  1108. hostdata->connected = NULL;
  1109. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  1110. hostdata->state = S_UNCONNECTED;
  1111. if (cmd->SCp.Status == ILLEGAL_STATUS_BYTE)
  1112. cmd->SCp.Status = lun;
  1113. if (cmd->cmnd[0] == REQUEST_SENSE && cmd->SCp.Status != GOOD)
  1114. cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
  1115. else
  1116. cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
  1117. cmd->scsi_done(cmd);
  1118. /* We are no longer connected to a target - check to see if
  1119. * there are commands waiting to be executed.
  1120. */
  1121. in2000_execute(instance);
  1122. } else {
  1123. printk("%02x:%02x:%02x: Unknown SEL_XFER_DONE phase!!---", asr, sr, phs);
  1124. }
  1125. break;
  1126. /* Note: this interrupt will occur only after a LEVEL2 command */
  1127. case CSR_SDP:
  1128. DB(DB_INTR, printk("SDP"))
  1129. hostdata->state = S_RUNNING_LEVEL2;
  1130. write_3393(hostdata, WD_COMMAND_PHASE, 0x41);
  1131. write_3393_cmd(hostdata, WD_CMD_SEL_ATN_XFER);
  1132. break;
  1133. case CSR_XFER_DONE | PHS_MESS_OUT:
  1134. case CSR_UNEXP | PHS_MESS_OUT:
  1135. case CSR_SRV_REQ | PHS_MESS_OUT:
  1136. DB(DB_INTR, printk("MSG_OUT="))
  1137. /* To get here, we've probably requested MESSAGE_OUT and have
  1138. * already put the correct bytes in outgoing_msg[] and filled
  1139. * in outgoing_len. We simply send them out to the SCSI bus.
  1140. * Sometimes we get MESSAGE_OUT phase when we're not expecting
  1141. * it - like when our SDTR message is rejected by a target. Some
  1142. * targets send the REJECT before receiving all of the extended
  1143. * message, and then seem to go back to MESSAGE_OUT for a byte
  1144. * or two. Not sure why, or if I'm doing something wrong to
  1145. * cause this to happen. Regardless, it seems that sending
  1146. * NOP messages in these situations results in no harm and
  1147. * makes everyone happy.
  1148. */
  1149. if (hostdata->outgoing_len == 0) {
  1150. hostdata->outgoing_len = 1;
  1151. hostdata->outgoing_msg[0] = NOP;
  1152. }
  1153. transfer_pio(hostdata->outgoing_msg, hostdata->outgoing_len, DATA_OUT_DIR, hostdata);
  1154. DB(DB_INTR, printk("%02x", hostdata->outgoing_msg[0]))
  1155. hostdata->outgoing_len = 0;
  1156. hostdata->state = S_CONNECTED;
  1157. break;
  1158. case CSR_UNEXP_DISC:
  1159. /* I think I've seen this after a request-sense that was in response
  1160. * to an error condition, but not sure. We certainly need to do
  1161. * something when we get this interrupt - the question is 'what?'.
  1162. * Let's think positively, and assume some command has finished
  1163. * in a legal manner (like a command that provokes a request-sense),
  1164. * so we treat it as a normal command-complete-disconnect.
  1165. */
  1166. /* Make sure that reselection is enabled at this point - it may
  1167. * have been turned off for the command that just completed.
  1168. */
  1169. write_3393(hostdata, WD_SOURCE_ID, SRCID_ER);
  1170. if (cmd == NULL) {
  1171. printk(" - Already disconnected! ");
  1172. hostdata->state = S_UNCONNECTED;
  1173. /* release the SMP spin_lock and restore irq state */
  1174. spin_unlock_irqrestore(instance->host_lock, flags);
  1175. return IRQ_HANDLED;
  1176. }
  1177. DB(DB_INTR, printk("UNEXP_DISC"))
  1178. hostdata->connected = NULL;
  1179. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  1180. hostdata->state = S_UNCONNECTED;
  1181. if (cmd->cmnd[0] == REQUEST_SENSE && cmd->SCp.Status != GOOD)
  1182. cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
  1183. else
  1184. cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
  1185. cmd->scsi_done(cmd);
  1186. /* We are no longer connected to a target - check to see if
  1187. * there are commands waiting to be executed.
  1188. */
  1189. in2000_execute(instance);
  1190. break;
  1191. case CSR_DISC:
  1192. /* Make sure that reselection is enabled at this point - it may
  1193. * have been turned off for the command that just completed.
  1194. */
  1195. write_3393(hostdata, WD_SOURCE_ID, SRCID_ER);
  1196. DB(DB_INTR, printk("DISC"))
  1197. if (cmd == NULL) {
  1198. printk(" - Already disconnected! ");
  1199. hostdata->state = S_UNCONNECTED;
  1200. }
  1201. switch (hostdata->state) {
  1202. case S_PRE_CMP_DISC:
  1203. hostdata->connected = NULL;
  1204. hostdata->busy[cmd->device->id] &= ~(1 << cmd->device->lun);
  1205. hostdata->state = S_UNCONNECTED;
  1206. DB(DB_INTR, printk(":%d", cmd->SCp.Status))
  1207. if (cmd->cmnd[0] == REQUEST_SENSE && cmd->SCp.Status != GOOD)
  1208. cmd->result = (cmd->result & 0x00ffff) | (DID_ERROR << 16);
  1209. else
  1210. cmd->result = cmd->SCp.Status | (cmd->SCp.Message << 8);
  1211. cmd->scsi_done(cmd);
  1212. break;
  1213. case S_PRE_TMP_DISC:
  1214. case S_RUNNING_LEVEL2:
  1215. cmd->host_scribble = (uchar *) hostdata->disconnected_Q;
  1216. hostdata->disconnected_Q = cmd;
  1217. hostdata->connected = NULL;
  1218. hostdata->state = S_UNCONNECTED;
  1219. #ifdef PROC_STATISTICS
  1220. hostdata->disc_done_cnt[cmd->device->id]++;
  1221. #endif
  1222. break;
  1223. default:
  1224. printk("*** Unexpected DISCONNECT interrupt! ***");
  1225. hostdata->state = S_UNCONNECTED;
  1226. }
  1227. /* We are no longer connected to a target - check to see if
  1228. * there are commands waiting to be executed.
  1229. */
  1230. in2000_execute(instance);
  1231. break;
  1232. case CSR_RESEL_AM:
  1233. DB(DB_INTR, printk("RESEL"))
  1234. /* First we have to make sure this reselection didn't */
  1235. /* happen during Arbitration/Selection of some other device. */
  1236. /* If yes, put losing command back on top of input_Q. */
  1237. if (hostdat

Large files files are truncated, but you can click here to view the full file