PageRenderTime 1289ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/drivers/scsi/arm/fas216.c

https://github.com/Mengqi/linux-2.6
C | 1871 lines | 1221 code | 290 blank | 360 comment | 219 complexity | 6b5b8d1f0c566f07503efda783e0de09 MD5 | raw file
  1. /*
  2. * linux/drivers/acorn/scsi/fas216.c
  3. *
  4. * Copyright (C) 1997-2003 Russell King
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. * Based on information in qlogicfas.c by Tom Zerucha, Michael Griffith, and
  11. * other sources, including:
  12. * the AMD Am53CF94 data sheet
  13. * the AMD Am53C94 data sheet
  14. *
  15. * This is a generic driver. To use it, have a look at cumana_2.c. You
  16. * should define your own structure that overlays FAS216_Info, eg:
  17. * struct my_host_data {
  18. * FAS216_Info info;
  19. * ... my host specific data ...
  20. * };
  21. *
  22. * Changelog:
  23. * 30-08-1997 RMK Created
  24. * 14-09-1997 RMK Started disconnect support
  25. * 08-02-1998 RMK Corrected real DMA support
  26. * 15-02-1998 RMK Started sync xfer support
  27. * 06-04-1998 RMK Tightened conditions for printing incomplete
  28. * transfers
  29. * 02-05-1998 RMK Added extra checks in fas216_reset
  30. * 24-05-1998 RMK Fixed synchronous transfers with period >= 200ns
  31. * 27-06-1998 RMK Changed asm/delay.h to linux/delay.h
  32. * 26-08-1998 RMK Improved message support wrt MESSAGE_REJECT
  33. * 02-04-2000 RMK Converted to use the new error handling, and
  34. * automatically request sense data upon check
  35. * condition status from targets.
  36. */
  37. #include <linux/module.h>
  38. #include <linux/blkdev.h>
  39. #include <linux/kernel.h>
  40. #include <linux/string.h>
  41. #include <linux/ioport.h>
  42. #include <linux/proc_fs.h>
  43. #include <linux/delay.h>
  44. #include <linux/bitops.h>
  45. #include <linux/init.h>
  46. #include <linux/interrupt.h>
  47. #include <asm/dma.h>
  48. #include <asm/io.h>
  49. #include <asm/irq.h>
  50. #include <asm/ecard.h>
  51. #include "../scsi.h"
  52. #include <scsi/scsi_dbg.h>
  53. #include <scsi/scsi_host.h>
  54. #include "fas216.h"
  55. #include "scsi.h"
  56. /* NOTE: SCSI2 Synchronous transfers *require* DMA according to
  57. * the data sheet. This restriction is crazy, especially when
  58. * you only want to send 16 bytes! What were the guys who
  59. * designed this chip on at that time? Did they read the SCSI2
  60. * spec at all? The following sections are taken from the SCSI2
  61. * standard (s2r10) concerning this:
  62. *
  63. * > IMPLEMENTORS NOTES:
  64. * > (1) Re-negotiation at every selection is not recommended, since a
  65. * > significant performance impact is likely.
  66. *
  67. * > The implied synchronous agreement shall remain in effect until a BUS DEVICE
  68. * > RESET message is received, until a hard reset condition occurs, or until one
  69. * > of the two SCSI devices elects to modify the agreement. The default data
  70. * > transfer mode is asynchronous data transfer mode. The default data transfer
  71. * > mode is entered at power on, after a BUS DEVICE RESET message, or after a hard
  72. * > reset condition.
  73. *
  74. * In total, this means that once you have elected to use synchronous
  75. * transfers, you must always use DMA.
  76. *
  77. * I was thinking that this was a good chip until I found this restriction ;(
  78. */
  79. #define SCSI2_SYNC
  80. #undef SCSI2_TAG
  81. #undef DEBUG_CONNECT
  82. #undef DEBUG_MESSAGES
  83. #undef CHECK_STRUCTURE
  84. #define LOG_CONNECT (1 << 0)
  85. #define LOG_BUSSERVICE (1 << 1)
  86. #define LOG_FUNCTIONDONE (1 << 2)
  87. #define LOG_MESSAGES (1 << 3)
  88. #define LOG_BUFFER (1 << 4)
  89. #define LOG_ERROR (1 << 8)
  90. static int level_mask = LOG_ERROR;
  91. module_param(level_mask, int, 0644);
  92. static int __init fas216_log_setup(char *str)
  93. {
  94. char *s;
  95. level_mask = 0;
  96. while ((s = strsep(&str, ",")) != NULL) {
  97. switch (s[0]) {
  98. case 'a':
  99. if (strcmp(s, "all") == 0)
  100. level_mask |= -1;
  101. break;
  102. case 'b':
  103. if (strncmp(s, "bus", 3) == 0)
  104. level_mask |= LOG_BUSSERVICE;
  105. if (strncmp(s, "buf", 3) == 0)
  106. level_mask |= LOG_BUFFER;
  107. break;
  108. case 'c':
  109. level_mask |= LOG_CONNECT;
  110. break;
  111. case 'e':
  112. level_mask |= LOG_ERROR;
  113. break;
  114. case 'm':
  115. level_mask |= LOG_MESSAGES;
  116. break;
  117. case 'n':
  118. if (strcmp(s, "none") == 0)
  119. level_mask = 0;
  120. break;
  121. case 's':
  122. level_mask |= LOG_FUNCTIONDONE;
  123. break;
  124. }
  125. }
  126. return 1;
  127. }
  128. __setup("fas216_logging=", fas216_log_setup);
  129. static inline unsigned char fas216_readb(FAS216_Info *info, unsigned int reg)
  130. {
  131. unsigned int off = reg << info->scsi.io_shift;
  132. return readb(info->scsi.io_base + off);
  133. }
  134. static inline void fas216_writeb(FAS216_Info *info, unsigned int reg, unsigned int val)
  135. {
  136. unsigned int off = reg << info->scsi.io_shift;
  137. writeb(val, info->scsi.io_base + off);
  138. }
  139. static void fas216_dumpstate(FAS216_Info *info)
  140. {
  141. unsigned char is, stat, inst;
  142. is = fas216_readb(info, REG_IS);
  143. stat = fas216_readb(info, REG_STAT);
  144. inst = fas216_readb(info, REG_INST);
  145. printk("FAS216: CTCL=%02X CTCM=%02X CMD=%02X STAT=%02X"
  146. " INST=%02X IS=%02X CFIS=%02X",
  147. fas216_readb(info, REG_CTCL),
  148. fas216_readb(info, REG_CTCM),
  149. fas216_readb(info, REG_CMD), stat, inst, is,
  150. fas216_readb(info, REG_CFIS));
  151. printk(" CNTL1=%02X CNTL2=%02X CNTL3=%02X CTCH=%02X\n",
  152. fas216_readb(info, REG_CNTL1),
  153. fas216_readb(info, REG_CNTL2),
  154. fas216_readb(info, REG_CNTL3),
  155. fas216_readb(info, REG_CTCH));
  156. }
  157. static void print_SCp(struct scsi_pointer *SCp, const char *prefix, const char *suffix)
  158. {
  159. printk("%sptr %p this_residual 0x%x buffer %p buffers_residual 0x%x%s",
  160. prefix, SCp->ptr, SCp->this_residual, SCp->buffer,
  161. SCp->buffers_residual, suffix);
  162. }
  163. static void fas216_dumpinfo(FAS216_Info *info)
  164. {
  165. static int used = 0;
  166. int i;
  167. if (used++)
  168. return;
  169. printk("FAS216_Info=\n");
  170. printk(" { magic_start=%lX host=%p SCpnt=%p origSCpnt=%p\n",
  171. info->magic_start, info->host, info->SCpnt,
  172. info->origSCpnt);
  173. printk(" scsi={ io_shift=%X irq=%X cfg={ %X %X %X %X }\n",
  174. info->scsi.io_shift, info->scsi.irq,
  175. info->scsi.cfg[0], info->scsi.cfg[1], info->scsi.cfg[2],
  176. info->scsi.cfg[3]);
  177. printk(" type=%p phase=%X\n",
  178. info->scsi.type, info->scsi.phase);
  179. print_SCp(&info->scsi.SCp, " SCp={ ", " }\n");
  180. printk(" msgs async_stp=%X disconnectable=%d aborting=%d }\n",
  181. info->scsi.async_stp,
  182. info->scsi.disconnectable, info->scsi.aborting);
  183. printk(" stats={ queues=%X removes=%X fins=%X reads=%X writes=%X miscs=%X\n"
  184. " disconnects=%X aborts=%X bus_resets=%X host_resets=%X}\n",
  185. info->stats.queues, info->stats.removes, info->stats.fins,
  186. info->stats.reads, info->stats.writes, info->stats.miscs,
  187. info->stats.disconnects, info->stats.aborts, info->stats.bus_resets,
  188. info->stats.host_resets);
  189. printk(" ifcfg={ clockrate=%X select_timeout=%X asyncperiod=%X sync_max_depth=%X }\n",
  190. info->ifcfg.clockrate, info->ifcfg.select_timeout,
  191. info->ifcfg.asyncperiod, info->ifcfg.sync_max_depth);
  192. for (i = 0; i < 8; i++) {
  193. printk(" busyluns[%d]=%08lx dev[%d]={ disconnect_ok=%d stp=%X sof=%X sync_state=%X }\n",
  194. i, info->busyluns[i], i,
  195. info->device[i].disconnect_ok, info->device[i].stp,
  196. info->device[i].sof, info->device[i].sync_state);
  197. }
  198. printk(" dma={ transfer_type=%X setup=%p pseudo=%p stop=%p }\n",
  199. info->dma.transfer_type, info->dma.setup,
  200. info->dma.pseudo, info->dma.stop);
  201. printk(" internal_done=%X magic_end=%lX }\n",
  202. info->internal_done, info->magic_end);
  203. }
  204. #ifdef CHECK_STRUCTURE
  205. static void __fas216_checkmagic(FAS216_Info *info, const char *func)
  206. {
  207. int corruption = 0;
  208. if (info->magic_start != MAGIC) {
  209. printk(KERN_CRIT "FAS216 Error: magic at start corrupted\n");
  210. corruption++;
  211. }
  212. if (info->magic_end != MAGIC) {
  213. printk(KERN_CRIT "FAS216 Error: magic at end corrupted\n");
  214. corruption++;
  215. }
  216. if (corruption) {
  217. fas216_dumpinfo(info);
  218. panic("scsi memory space corrupted in %s", func);
  219. }
  220. }
  221. #define fas216_checkmagic(info) __fas216_checkmagic((info), __func__)
  222. #else
  223. #define fas216_checkmagic(info)
  224. #endif
  225. static const char *fas216_bus_phase(int stat)
  226. {
  227. static const char *phases[] = {
  228. "DATA OUT", "DATA IN",
  229. "COMMAND", "STATUS",
  230. "MISC OUT", "MISC IN",
  231. "MESG OUT", "MESG IN"
  232. };
  233. return phases[stat & STAT_BUSMASK];
  234. }
  235. static const char *fas216_drv_phase(FAS216_Info *info)
  236. {
  237. static const char *phases[] = {
  238. [PHASE_IDLE] = "idle",
  239. [PHASE_SELECTION] = "selection",
  240. [PHASE_COMMAND] = "command",
  241. [PHASE_DATAOUT] = "data out",
  242. [PHASE_DATAIN] = "data in",
  243. [PHASE_MSGIN] = "message in",
  244. [PHASE_MSGIN_DISCONNECT]= "disconnect",
  245. [PHASE_MSGOUT_EXPECT] = "expect message out",
  246. [PHASE_MSGOUT] = "message out",
  247. [PHASE_STATUS] = "status",
  248. [PHASE_DONE] = "done",
  249. };
  250. if (info->scsi.phase < ARRAY_SIZE(phases) &&
  251. phases[info->scsi.phase])
  252. return phases[info->scsi.phase];
  253. return "???";
  254. }
  255. static char fas216_target(FAS216_Info *info)
  256. {
  257. if (info->SCpnt)
  258. return '0' + info->SCpnt->device->id;
  259. else
  260. return 'H';
  261. }
  262. static void
  263. fas216_do_log(FAS216_Info *info, char target, char *fmt, va_list ap)
  264. {
  265. static char buf[1024];
  266. vsnprintf(buf, sizeof(buf), fmt, ap);
  267. printk("scsi%d.%c: %s", info->host->host_no, target, buf);
  268. }
  269. static void fas216_log_command(FAS216_Info *info, int level,
  270. struct scsi_cmnd *SCpnt, char *fmt, ...)
  271. {
  272. va_list args;
  273. if (level != 0 && !(level & level_mask))
  274. return;
  275. va_start(args, fmt);
  276. fas216_do_log(info, '0' + SCpnt->device->id, fmt, args);
  277. va_end(args);
  278. printk(" CDB: ");
  279. __scsi_print_command(SCpnt->cmnd);
  280. }
  281. static void
  282. fas216_log_target(FAS216_Info *info, int level, int target, char *fmt, ...)
  283. {
  284. va_list args;
  285. if (level != 0 && !(level & level_mask))
  286. return;
  287. if (target < 0)
  288. target = 'H';
  289. else
  290. target += '0';
  291. va_start(args, fmt);
  292. fas216_do_log(info, target, fmt, args);
  293. va_end(args);
  294. printk("\n");
  295. }
  296. static void fas216_log(FAS216_Info *info, int level, char *fmt, ...)
  297. {
  298. va_list args;
  299. if (level != 0 && !(level & level_mask))
  300. return;
  301. va_start(args, fmt);
  302. fas216_do_log(info, fas216_target(info), fmt, args);
  303. va_end(args);
  304. printk("\n");
  305. }
  306. #define PH_SIZE 32
  307. static struct { int stat, ssr, isr, ph; } ph_list[PH_SIZE];
  308. static int ph_ptr;
  309. static void add_debug_list(int stat, int ssr, int isr, int ph)
  310. {
  311. ph_list[ph_ptr].stat = stat;
  312. ph_list[ph_ptr].ssr = ssr;
  313. ph_list[ph_ptr].isr = isr;
  314. ph_list[ph_ptr].ph = ph;
  315. ph_ptr = (ph_ptr + 1) & (PH_SIZE-1);
  316. }
  317. static struct { int command; void *from; } cmd_list[8];
  318. static int cmd_ptr;
  319. static void fas216_cmd(FAS216_Info *info, unsigned int command)
  320. {
  321. cmd_list[cmd_ptr].command = command;
  322. cmd_list[cmd_ptr].from = __builtin_return_address(0);
  323. cmd_ptr = (cmd_ptr + 1) & 7;
  324. fas216_writeb(info, REG_CMD, command);
  325. }
  326. static void print_debug_list(void)
  327. {
  328. int i;
  329. i = ph_ptr;
  330. printk(KERN_ERR "SCSI IRQ trail\n");
  331. do {
  332. printk(" %02x:%02x:%02x:%1x",
  333. ph_list[i].stat, ph_list[i].ssr,
  334. ph_list[i].isr, ph_list[i].ph);
  335. i = (i + 1) & (PH_SIZE - 1);
  336. if (((i ^ ph_ptr) & 7) == 0)
  337. printk("\n");
  338. } while (i != ph_ptr);
  339. if ((i ^ ph_ptr) & 7)
  340. printk("\n");
  341. i = cmd_ptr;
  342. printk(KERN_ERR "FAS216 commands: ");
  343. do {
  344. printk("%02x:%p ", cmd_list[i].command, cmd_list[i].from);
  345. i = (i + 1) & 7;
  346. } while (i != cmd_ptr);
  347. printk("\n");
  348. }
  349. static void fas216_done(FAS216_Info *info, unsigned int result);
  350. /**
  351. * fas216_get_last_msg - retrive last message from the list
  352. * @info: interface to search
  353. * @pos: current fifo position
  354. *
  355. * Retrieve a last message from the list, using position in fifo.
  356. */
  357. static inline unsigned short
  358. fas216_get_last_msg(FAS216_Info *info, int pos)
  359. {
  360. unsigned short packed_msg = NOP;
  361. struct message *msg;
  362. int msgnr = 0;
  363. while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
  364. if (pos >= msg->fifo)
  365. break;
  366. }
  367. if (msg) {
  368. if (msg->msg[0] == EXTENDED_MESSAGE)
  369. packed_msg = EXTENDED_MESSAGE | msg->msg[2] << 8;
  370. else
  371. packed_msg = msg->msg[0];
  372. }
  373. fas216_log(info, LOG_MESSAGES,
  374. "Message: %04x found at position %02x\n", packed_msg, pos);
  375. return packed_msg;
  376. }
  377. /**
  378. * fas216_syncperiod - calculate STP register value
  379. * @info: state structure for interface connected to device
  380. * @ns: period in ns (between subsequent bytes)
  381. *
  382. * Calculate value to be loaded into the STP register for a given period
  383. * in ns. Returns a value suitable for REG_STP.
  384. */
  385. static int fas216_syncperiod(FAS216_Info *info, int ns)
  386. {
  387. int value = (info->ifcfg.clockrate * ns) / 1000;
  388. fas216_checkmagic(info);
  389. if (value < 4)
  390. value = 4;
  391. else if (value > 35)
  392. value = 35;
  393. return value & 31;
  394. }
  395. /**
  396. * fas216_set_sync - setup FAS216 chip for specified transfer period.
  397. * @info: state structure for interface connected to device
  398. * @target: target
  399. *
  400. * Correctly setup FAS216 chip for specified transfer period.
  401. * Notes : we need to switch the chip out of FASTSCSI mode if we have
  402. * a transfer period >= 200ns - otherwise the chip will violate
  403. * the SCSI timings.
  404. */
  405. static void fas216_set_sync(FAS216_Info *info, int target)
  406. {
  407. unsigned int cntl3;
  408. fas216_writeb(info, REG_SOF, info->device[target].sof);
  409. fas216_writeb(info, REG_STP, info->device[target].stp);
  410. cntl3 = info->scsi.cfg[2];
  411. if (info->device[target].period >= (200 / 4))
  412. cntl3 = cntl3 & ~CNTL3_FASTSCSI;
  413. fas216_writeb(info, REG_CNTL3, cntl3);
  414. }
  415. /* Synchronous transfer support
  416. *
  417. * Note: The SCSI II r10 spec says (5.6.12):
  418. *
  419. * (2) Due to historical problems with early host adapters that could
  420. * not accept an SDTR message, some targets may not initiate synchronous
  421. * negotiation after a power cycle as required by this standard. Host
  422. * adapters that support synchronous mode may avoid the ensuing failure
  423. * modes when the target is independently power cycled by initiating a
  424. * synchronous negotiation on each REQUEST SENSE and INQUIRY command.
  425. * This approach increases the SCSI bus overhead and is not recommended
  426. * for new implementations. The correct method is to respond to an
  427. * SDTR message with a MESSAGE REJECT message if the either the
  428. * initiator or target devices does not support synchronous transfers
  429. * or does not want to negotiate for synchronous transfers at the time.
  430. * Using the correct method assures compatibility with wide data
  431. * transfers and future enhancements.
  432. *
  433. * We will always initiate a synchronous transfer negotiation request on
  434. * every INQUIRY or REQUEST SENSE message, unless the target itself has
  435. * at some point performed a synchronous transfer negotiation request, or
  436. * we have synchronous transfers disabled for this device.
  437. */
  438. /**
  439. * fas216_handlesync - Handle a synchronous transfer message
  440. * @info: state structure for interface
  441. * @msg: message from target
  442. *
  443. * Handle a synchronous transfer message from the target
  444. */
  445. static void fas216_handlesync(FAS216_Info *info, char *msg)
  446. {
  447. struct fas216_device *dev = &info->device[info->SCpnt->device->id];
  448. enum { sync, async, none, reject } res = none;
  449. #ifdef SCSI2_SYNC
  450. switch (msg[0]) {
  451. case MESSAGE_REJECT:
  452. /* Synchronous transfer request failed.
  453. * Note: SCSI II r10:
  454. *
  455. * SCSI devices that are capable of synchronous
  456. * data transfers shall not respond to an SDTR
  457. * message with a MESSAGE REJECT message.
  458. *
  459. * Hence, if we get this condition, we disable
  460. * negotiation for this device.
  461. */
  462. if (dev->sync_state == neg_inprogress) {
  463. dev->sync_state = neg_invalid;
  464. res = async;
  465. }
  466. break;
  467. case EXTENDED_MESSAGE:
  468. switch (dev->sync_state) {
  469. /* We don't accept synchronous transfer requests.
  470. * Respond with a MESSAGE_REJECT to prevent a
  471. * synchronous transfer agreement from being reached.
  472. */
  473. case neg_invalid:
  474. res = reject;
  475. break;
  476. /* We were not negotiating a synchronous transfer,
  477. * but the device sent us a negotiation request.
  478. * Honour the request by sending back a SDTR
  479. * message containing our capability, limited by
  480. * the targets capability.
  481. */
  482. default:
  483. fas216_cmd(info, CMD_SETATN);
  484. if (msg[4] > info->ifcfg.sync_max_depth)
  485. msg[4] = info->ifcfg.sync_max_depth;
  486. if (msg[3] < 1000 / info->ifcfg.clockrate)
  487. msg[3] = 1000 / info->ifcfg.clockrate;
  488. msgqueue_flush(&info->scsi.msgs);
  489. msgqueue_addmsg(&info->scsi.msgs, 5,
  490. EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
  491. msg[3], msg[4]);
  492. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  493. /* This is wrong. The agreement is not in effect
  494. * until this message is accepted by the device
  495. */
  496. dev->sync_state = neg_targcomplete;
  497. res = sync;
  498. break;
  499. /* We initiated the synchronous transfer negotiation,
  500. * and have successfully received a response from the
  501. * target. The synchronous transfer agreement has been
  502. * reached. Note: if the values returned are out of our
  503. * bounds, we must reject the message.
  504. */
  505. case neg_inprogress:
  506. res = reject;
  507. if (msg[4] <= info->ifcfg.sync_max_depth &&
  508. msg[3] >= 1000 / info->ifcfg.clockrate) {
  509. dev->sync_state = neg_complete;
  510. res = sync;
  511. }
  512. break;
  513. }
  514. }
  515. #else
  516. res = reject;
  517. #endif
  518. switch (res) {
  519. case sync:
  520. dev->period = msg[3];
  521. dev->sof = msg[4];
  522. dev->stp = fas216_syncperiod(info, msg[3] * 4);
  523. fas216_set_sync(info, info->SCpnt->device->id);
  524. break;
  525. case reject:
  526. fas216_cmd(info, CMD_SETATN);
  527. msgqueue_flush(&info->scsi.msgs);
  528. msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
  529. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  530. case async:
  531. dev->period = info->ifcfg.asyncperiod / 4;
  532. dev->sof = 0;
  533. dev->stp = info->scsi.async_stp;
  534. fas216_set_sync(info, info->SCpnt->device->id);
  535. break;
  536. case none:
  537. break;
  538. }
  539. }
  540. /**
  541. * fas216_updateptrs - update data pointers after transfer suspended/paused
  542. * @info: interface's local pointer to update
  543. * @bytes_transferred: number of bytes transferred
  544. *
  545. * Update data pointers after transfer suspended/paused
  546. */
  547. static void fas216_updateptrs(FAS216_Info *info, int bytes_transferred)
  548. {
  549. struct scsi_pointer *SCp = &info->scsi.SCp;
  550. fas216_checkmagic(info);
  551. BUG_ON(bytes_transferred < 0);
  552. SCp->phase -= bytes_transferred;
  553. while (bytes_transferred != 0) {
  554. if (SCp->this_residual > bytes_transferred)
  555. break;
  556. /*
  557. * We have used up this buffer. Move on to the
  558. * next buffer.
  559. */
  560. bytes_transferred -= SCp->this_residual;
  561. if (!next_SCp(SCp) && bytes_transferred) {
  562. printk(KERN_WARNING "scsi%d.%c: out of buffers\n",
  563. info->host->host_no, '0' + info->SCpnt->device->id);
  564. return;
  565. }
  566. }
  567. SCp->this_residual -= bytes_transferred;
  568. if (SCp->this_residual)
  569. SCp->ptr += bytes_transferred;
  570. else
  571. SCp->ptr = NULL;
  572. }
  573. /**
  574. * fas216_pio - transfer data off of/on to card using programmed IO
  575. * @info: interface to transfer data to/from
  576. * @direction: direction to transfer data (DMA_OUT/DMA_IN)
  577. *
  578. * Transfer data off of/on to card using programmed IO.
  579. * Notes: this is incredibly slow.
  580. */
  581. static void fas216_pio(FAS216_Info *info, fasdmadir_t direction)
  582. {
  583. struct scsi_pointer *SCp = &info->scsi.SCp;
  584. fas216_checkmagic(info);
  585. if (direction == DMA_OUT)
  586. fas216_writeb(info, REG_FF, get_next_SCp_byte(SCp));
  587. else
  588. put_next_SCp_byte(SCp, fas216_readb(info, REG_FF));
  589. if (SCp->this_residual == 0)
  590. next_SCp(SCp);
  591. }
  592. static void fas216_set_stc(FAS216_Info *info, unsigned int length)
  593. {
  594. fas216_writeb(info, REG_STCL, length);
  595. fas216_writeb(info, REG_STCM, length >> 8);
  596. fas216_writeb(info, REG_STCH, length >> 16);
  597. }
  598. static unsigned int fas216_get_ctc(FAS216_Info *info)
  599. {
  600. return fas216_readb(info, REG_CTCL) +
  601. (fas216_readb(info, REG_CTCM) << 8) +
  602. (fas216_readb(info, REG_CTCH) << 16);
  603. }
  604. /**
  605. * fas216_cleanuptransfer - clean up after a transfer has completed.
  606. * @info: interface to clean up
  607. *
  608. * Update the data pointers according to the number of bytes transferred
  609. * on the SCSI bus.
  610. */
  611. static void fas216_cleanuptransfer(FAS216_Info *info)
  612. {
  613. unsigned long total, residual, fifo;
  614. fasdmatype_t dmatype = info->dma.transfer_type;
  615. info->dma.transfer_type = fasdma_none;
  616. /*
  617. * PIO transfers do not need to be cleaned up.
  618. */
  619. if (dmatype == fasdma_pio || dmatype == fasdma_none)
  620. return;
  621. if (dmatype == fasdma_real_all)
  622. total = info->scsi.SCp.phase;
  623. else
  624. total = info->scsi.SCp.this_residual;
  625. residual = fas216_get_ctc(info);
  626. fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
  627. fas216_log(info, LOG_BUFFER, "cleaning up from previous "
  628. "transfer: length 0x%06x, residual 0x%x, fifo %d",
  629. total, residual, fifo);
  630. /*
  631. * If we were performing Data-Out, the transfer counter
  632. * counts down each time a byte is transferred by the
  633. * host to the FIFO. This means we must include the
  634. * bytes left in the FIFO from the transfer counter.
  635. */
  636. if (info->scsi.phase == PHASE_DATAOUT)
  637. residual += fifo;
  638. fas216_updateptrs(info, total - residual);
  639. }
  640. /**
  641. * fas216_transfer - Perform a DMA/PIO transfer off of/on to card
  642. * @info: interface from which device disconnected from
  643. *
  644. * Start a DMA/PIO transfer off of/on to card
  645. */
  646. static void fas216_transfer(FAS216_Info *info)
  647. {
  648. fasdmadir_t direction;
  649. fasdmatype_t dmatype;
  650. fas216_log(info, LOG_BUFFER,
  651. "starttransfer: buffer %p length 0x%06x reqlen 0x%06x",
  652. info->scsi.SCp.ptr, info->scsi.SCp.this_residual,
  653. info->scsi.SCp.phase);
  654. if (!info->scsi.SCp.ptr) {
  655. fas216_log(info, LOG_ERROR, "null buffer passed to "
  656. "fas216_starttransfer");
  657. print_SCp(&info->scsi.SCp, "SCp: ", "\n");
  658. print_SCp(&info->SCpnt->SCp, "Cmnd SCp: ", "\n");
  659. return;
  660. }
  661. /*
  662. * If we have a synchronous transfer agreement in effect, we must
  663. * use DMA mode. If we are using asynchronous transfers, we may
  664. * use DMA mode or PIO mode.
  665. */
  666. if (info->device[info->SCpnt->device->id].sof)
  667. dmatype = fasdma_real_all;
  668. else
  669. dmatype = fasdma_pio;
  670. if (info->scsi.phase == PHASE_DATAOUT)
  671. direction = DMA_OUT;
  672. else
  673. direction = DMA_IN;
  674. if (info->dma.setup)
  675. dmatype = info->dma.setup(info->host, &info->scsi.SCp,
  676. direction, dmatype);
  677. info->dma.transfer_type = dmatype;
  678. if (dmatype == fasdma_real_all)
  679. fas216_set_stc(info, info->scsi.SCp.phase);
  680. else
  681. fas216_set_stc(info, info->scsi.SCp.this_residual);
  682. switch (dmatype) {
  683. case fasdma_pio:
  684. fas216_log(info, LOG_BUFFER, "PIO transfer");
  685. fas216_writeb(info, REG_SOF, 0);
  686. fas216_writeb(info, REG_STP, info->scsi.async_stp);
  687. fas216_cmd(info, CMD_TRANSFERINFO);
  688. fas216_pio(info, direction);
  689. break;
  690. case fasdma_pseudo:
  691. fas216_log(info, LOG_BUFFER, "pseudo transfer");
  692. fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
  693. info->dma.pseudo(info->host, &info->scsi.SCp,
  694. direction, info->SCpnt->transfersize);
  695. break;
  696. case fasdma_real_block:
  697. fas216_log(info, LOG_BUFFER, "block dma transfer");
  698. fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
  699. break;
  700. case fasdma_real_all:
  701. fas216_log(info, LOG_BUFFER, "total dma transfer");
  702. fas216_cmd(info, CMD_TRANSFERINFO | CMD_WITHDMA);
  703. break;
  704. default:
  705. fas216_log(info, LOG_BUFFER | LOG_ERROR,
  706. "invalid FAS216 DMA type");
  707. break;
  708. }
  709. }
  710. /**
  711. * fas216_stoptransfer - Stop a DMA transfer onto / off of the card
  712. * @info: interface from which device disconnected from
  713. *
  714. * Called when we switch away from DATA IN or DATA OUT phases.
  715. */
  716. static void fas216_stoptransfer(FAS216_Info *info)
  717. {
  718. fas216_checkmagic(info);
  719. if (info->dma.transfer_type == fasdma_real_all ||
  720. info->dma.transfer_type == fasdma_real_block)
  721. info->dma.stop(info->host, &info->scsi.SCp);
  722. fas216_cleanuptransfer(info);
  723. if (info->scsi.phase == PHASE_DATAIN) {
  724. unsigned int fifo;
  725. /*
  726. * If we were performing Data-In, then the FIFO counter
  727. * contains the number of bytes not transferred via DMA
  728. * from the on-board FIFO. Read them manually.
  729. */
  730. fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
  731. while (fifo && info->scsi.SCp.ptr) {
  732. *info->scsi.SCp.ptr = fas216_readb(info, REG_FF);
  733. fas216_updateptrs(info, 1);
  734. fifo--;
  735. }
  736. } else {
  737. /*
  738. * After a Data-Out phase, there may be unsent
  739. * bytes left in the FIFO. Flush them out.
  740. */
  741. fas216_cmd(info, CMD_FLUSHFIFO);
  742. }
  743. }
  744. static void fas216_aborttransfer(FAS216_Info *info)
  745. {
  746. fas216_checkmagic(info);
  747. if (info->dma.transfer_type == fasdma_real_all ||
  748. info->dma.transfer_type == fasdma_real_block)
  749. info->dma.stop(info->host, &info->scsi.SCp);
  750. info->dma.transfer_type = fasdma_none;
  751. fas216_cmd(info, CMD_FLUSHFIFO);
  752. }
  753. static void fas216_kick(FAS216_Info *info);
  754. /**
  755. * fas216_disconnected_intr - handle device disconnection
  756. * @info: interface from which device disconnected from
  757. *
  758. * Handle device disconnection
  759. */
  760. static void fas216_disconnect_intr(FAS216_Info *info)
  761. {
  762. unsigned long flags;
  763. fas216_checkmagic(info);
  764. fas216_log(info, LOG_CONNECT, "disconnect phase=%02x",
  765. info->scsi.phase);
  766. msgqueue_flush(&info->scsi.msgs);
  767. switch (info->scsi.phase) {
  768. case PHASE_SELECTION: /* while selecting - no target */
  769. case PHASE_SELSTEPS:
  770. fas216_done(info, DID_NO_CONNECT);
  771. break;
  772. case PHASE_MSGIN_DISCONNECT: /* message in - disconnecting */
  773. info->scsi.disconnectable = 1;
  774. info->scsi.phase = PHASE_IDLE;
  775. info->stats.disconnects += 1;
  776. spin_lock_irqsave(&info->host_lock, flags);
  777. if (info->scsi.phase == PHASE_IDLE)
  778. fas216_kick(info);
  779. spin_unlock_irqrestore(&info->host_lock, flags);
  780. break;
  781. case PHASE_DONE: /* at end of command - complete */
  782. fas216_done(info, DID_OK);
  783. break;
  784. case PHASE_MSGOUT: /* message out - possible ABORT message */
  785. if (fas216_get_last_msg(info, info->scsi.msgin_fifo) == ABORT) {
  786. info->scsi.aborting = 0;
  787. fas216_done(info, DID_ABORT);
  788. break;
  789. }
  790. default: /* huh? */
  791. printk(KERN_ERR "scsi%d.%c: unexpected disconnect in phase %s\n",
  792. info->host->host_no, fas216_target(info), fas216_drv_phase(info));
  793. print_debug_list();
  794. fas216_stoptransfer(info);
  795. fas216_done(info, DID_ERROR);
  796. break;
  797. }
  798. }
  799. /**
  800. * fas216_reselected_intr - start reconnection of a device
  801. * @info: interface which was reselected
  802. *
  803. * Start reconnection of a device
  804. */
  805. static void
  806. fas216_reselected_intr(FAS216_Info *info)
  807. {
  808. unsigned int cfis, i;
  809. unsigned char msg[4];
  810. unsigned char target, lun, tag;
  811. fas216_checkmagic(info);
  812. WARN_ON(info->scsi.phase == PHASE_SELECTION ||
  813. info->scsi.phase == PHASE_SELSTEPS);
  814. cfis = fas216_readb(info, REG_CFIS);
  815. fas216_log(info, LOG_CONNECT, "reconnect phase=%02x cfis=%02x",
  816. info->scsi.phase, cfis);
  817. cfis &= CFIS_CF;
  818. if (cfis < 2 || cfis > 4) {
  819. printk(KERN_ERR "scsi%d.H: incorrect number of bytes after reselect\n",
  820. info->host->host_no);
  821. goto bad_message;
  822. }
  823. for (i = 0; i < cfis; i++)
  824. msg[i] = fas216_readb(info, REG_FF);
  825. if (!(msg[0] & (1 << info->host->this_id)) ||
  826. !(msg[1] & 0x80))
  827. goto initiator_error;
  828. target = msg[0] & ~(1 << info->host->this_id);
  829. target = ffs(target) - 1;
  830. lun = msg[1] & 7;
  831. tag = 0;
  832. if (cfis >= 3) {
  833. if (msg[2] != SIMPLE_QUEUE_TAG)
  834. goto initiator_error;
  835. tag = msg[3];
  836. }
  837. /* set up for synchronous transfers */
  838. fas216_writeb(info, REG_SDID, target);
  839. fas216_set_sync(info, target);
  840. msgqueue_flush(&info->scsi.msgs);
  841. fas216_log(info, LOG_CONNECT, "Reconnected: target %1x lun %1x tag %02x",
  842. target, lun, tag);
  843. if (info->scsi.disconnectable && info->SCpnt) {
  844. info->scsi.disconnectable = 0;
  845. if (info->SCpnt->device->id == target &&
  846. info->SCpnt->device->lun == lun &&
  847. info->SCpnt->tag == tag) {
  848. fas216_log(info, LOG_CONNECT, "reconnected previously executing command");
  849. } else {
  850. queue_add_cmd_tail(&info->queues.disconnected, info->SCpnt);
  851. fas216_log(info, LOG_CONNECT, "had to move command to disconnected queue");
  852. info->SCpnt = NULL;
  853. }
  854. }
  855. if (!info->SCpnt) {
  856. info->SCpnt = queue_remove_tgtluntag(&info->queues.disconnected,
  857. target, lun, tag);
  858. fas216_log(info, LOG_CONNECT, "had to get command");
  859. }
  860. if (info->SCpnt) {
  861. /*
  862. * Restore data pointer from SAVED data pointer
  863. */
  864. info->scsi.SCp = info->SCpnt->SCp;
  865. fas216_log(info, LOG_CONNECT, "data pointers: [%p, %X]",
  866. info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
  867. info->scsi.phase = PHASE_MSGIN;
  868. } else {
  869. /*
  870. * Our command structure not found - abort the
  871. * command on the target. Since we have no
  872. * record of this command, we can't send
  873. * an INITIATOR DETECTED ERROR message.
  874. */
  875. fas216_cmd(info, CMD_SETATN);
  876. #if 0
  877. if (tag)
  878. msgqueue_addmsg(&info->scsi.msgs, 2, ABORT_TAG, tag);
  879. else
  880. #endif
  881. msgqueue_addmsg(&info->scsi.msgs, 1, ABORT);
  882. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  883. info->scsi.aborting = 1;
  884. }
  885. fas216_cmd(info, CMD_MSGACCEPTED);
  886. return;
  887. initiator_error:
  888. printk(KERN_ERR "scsi%d.H: error during reselection: bytes",
  889. info->host->host_no);
  890. for (i = 0; i < cfis; i++)
  891. printk(" %02x", msg[i]);
  892. printk("\n");
  893. bad_message:
  894. fas216_cmd(info, CMD_SETATN);
  895. msgqueue_flush(&info->scsi.msgs);
  896. msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
  897. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  898. fas216_cmd(info, CMD_MSGACCEPTED);
  899. }
  900. static void fas216_parse_message(FAS216_Info *info, unsigned char *message, int msglen)
  901. {
  902. int i;
  903. switch (message[0]) {
  904. case COMMAND_COMPLETE:
  905. if (msglen != 1)
  906. goto unrecognised;
  907. printk(KERN_ERR "scsi%d.%c: command complete with no "
  908. "status in MESSAGE_IN?\n",
  909. info->host->host_no, fas216_target(info));
  910. break;
  911. case SAVE_POINTERS:
  912. if (msglen != 1)
  913. goto unrecognised;
  914. /*
  915. * Save current data pointer to SAVED data pointer
  916. * SCSI II standard says that we must not acknowledge
  917. * this until we have really saved pointers.
  918. * NOTE: we DO NOT save the command nor status pointers
  919. * as required by the SCSI II standard. These always
  920. * point to the start of their respective areas.
  921. */
  922. info->SCpnt->SCp = info->scsi.SCp;
  923. info->SCpnt->SCp.sent_command = 0;
  924. fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
  925. "save data pointers: [%p, %X]",
  926. info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
  927. break;
  928. case RESTORE_POINTERS:
  929. if (msglen != 1)
  930. goto unrecognised;
  931. /*
  932. * Restore current data pointer from SAVED data pointer
  933. */
  934. info->scsi.SCp = info->SCpnt->SCp;
  935. fas216_log(info, LOG_CONNECT | LOG_MESSAGES | LOG_BUFFER,
  936. "restore data pointers: [%p, 0x%x]",
  937. info->scsi.SCp.ptr, info->scsi.SCp.this_residual);
  938. break;
  939. case DISCONNECT:
  940. if (msglen != 1)
  941. goto unrecognised;
  942. info->scsi.phase = PHASE_MSGIN_DISCONNECT;
  943. break;
  944. case MESSAGE_REJECT:
  945. if (msglen != 1)
  946. goto unrecognised;
  947. switch (fas216_get_last_msg(info, info->scsi.msgin_fifo)) {
  948. case EXTENDED_MESSAGE | EXTENDED_SDTR << 8:
  949. fas216_handlesync(info, message);
  950. break;
  951. default:
  952. fas216_log(info, 0, "reject, last message 0x%04x",
  953. fas216_get_last_msg(info, info->scsi.msgin_fifo));
  954. }
  955. break;
  956. case NOP:
  957. break;
  958. case EXTENDED_MESSAGE:
  959. if (msglen < 3)
  960. goto unrecognised;
  961. switch (message[2]) {
  962. case EXTENDED_SDTR: /* Sync transfer negotiation request/reply */
  963. fas216_handlesync(info, message);
  964. break;
  965. default:
  966. goto unrecognised;
  967. }
  968. break;
  969. default:
  970. goto unrecognised;
  971. }
  972. return;
  973. unrecognised:
  974. fas216_log(info, 0, "unrecognised message, rejecting");
  975. printk("scsi%d.%c: message was", info->host->host_no, fas216_target(info));
  976. for (i = 0; i < msglen; i++)
  977. printk("%s%02X", i & 31 ? " " : "\n ", message[i]);
  978. printk("\n");
  979. /*
  980. * Something strange seems to be happening here -
  981. * I can't use SETATN since the chip gives me an
  982. * invalid command interrupt when I do. Weird.
  983. */
  984. fas216_cmd(info, CMD_NOP);
  985. fas216_dumpstate(info);
  986. fas216_cmd(info, CMD_SETATN);
  987. msgqueue_flush(&info->scsi.msgs);
  988. msgqueue_addmsg(&info->scsi.msgs, 1, MESSAGE_REJECT);
  989. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  990. fas216_dumpstate(info);
  991. }
  992. static int fas216_wait_cmd(FAS216_Info *info, int cmd)
  993. {
  994. int tout;
  995. int stat;
  996. fas216_cmd(info, cmd);
  997. for (tout = 1000; tout; tout -= 1) {
  998. stat = fas216_readb(info, REG_STAT);
  999. if (stat & (STAT_INT|STAT_PARITYERROR))
  1000. break;
  1001. udelay(1);
  1002. }
  1003. return stat;
  1004. }
  1005. static int fas216_get_msg_byte(FAS216_Info *info)
  1006. {
  1007. unsigned int stat = fas216_wait_cmd(info, CMD_MSGACCEPTED);
  1008. if ((stat & STAT_INT) == 0)
  1009. goto timedout;
  1010. if ((stat & STAT_BUSMASK) != STAT_MESGIN)
  1011. goto unexpected_phase_change;
  1012. fas216_readb(info, REG_INST);
  1013. stat = fas216_wait_cmd(info, CMD_TRANSFERINFO);
  1014. if ((stat & STAT_INT) == 0)
  1015. goto timedout;
  1016. if (stat & STAT_PARITYERROR)
  1017. goto parity_error;
  1018. if ((stat & STAT_BUSMASK) != STAT_MESGIN)
  1019. goto unexpected_phase_change;
  1020. fas216_readb(info, REG_INST);
  1021. return fas216_readb(info, REG_FF);
  1022. timedout:
  1023. fas216_log(info, LOG_ERROR, "timed out waiting for message byte");
  1024. return -1;
  1025. unexpected_phase_change:
  1026. fas216_log(info, LOG_ERROR, "unexpected phase change: status = %02x", stat);
  1027. return -2;
  1028. parity_error:
  1029. fas216_log(info, LOG_ERROR, "parity error during message in phase");
  1030. return -3;
  1031. }
  1032. /**
  1033. * fas216_message - handle a function done interrupt from FAS216 chip
  1034. * @info: interface which caused function done interrupt
  1035. *
  1036. * Handle a function done interrupt from FAS216 chip
  1037. */
  1038. static void fas216_message(FAS216_Info *info)
  1039. {
  1040. unsigned char *message = info->scsi.message;
  1041. unsigned int msglen = 1;
  1042. int msgbyte = 0;
  1043. fas216_checkmagic(info);
  1044. message[0] = fas216_readb(info, REG_FF);
  1045. if (message[0] == EXTENDED_MESSAGE) {
  1046. msgbyte = fas216_get_msg_byte(info);
  1047. if (msgbyte >= 0) {
  1048. message[1] = msgbyte;
  1049. for (msglen = 2; msglen < message[1] + 2; msglen++) {
  1050. msgbyte = fas216_get_msg_byte(info);
  1051. if (msgbyte >= 0)
  1052. message[msglen] = msgbyte;
  1053. else
  1054. break;
  1055. }
  1056. }
  1057. }
  1058. if (msgbyte == -3)
  1059. goto parity_error;
  1060. #ifdef DEBUG_MESSAGES
  1061. {
  1062. int i;
  1063. printk("scsi%d.%c: message in: ",
  1064. info->host->host_no, fas216_target(info));
  1065. for (i = 0; i < msglen; i++)
  1066. printk("%02X ", message[i]);
  1067. printk("\n");
  1068. }
  1069. #endif
  1070. fas216_parse_message(info, message, msglen);
  1071. fas216_cmd(info, CMD_MSGACCEPTED);
  1072. return;
  1073. parity_error:
  1074. fas216_cmd(info, CMD_SETATN);
  1075. msgqueue_flush(&info->scsi.msgs);
  1076. msgqueue_addmsg(&info->scsi.msgs, 1, MSG_PARITY_ERROR);
  1077. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  1078. fas216_cmd(info, CMD_MSGACCEPTED);
  1079. return;
  1080. }
  1081. /**
  1082. * fas216_send_command - send command after all message bytes have been sent
  1083. * @info: interface which caused bus service
  1084. *
  1085. * Send a command to a target after all message bytes have been sent
  1086. */
  1087. static void fas216_send_command(FAS216_Info *info)
  1088. {
  1089. int i;
  1090. fas216_checkmagic(info);
  1091. fas216_cmd(info, CMD_NOP|CMD_WITHDMA);
  1092. fas216_cmd(info, CMD_FLUSHFIFO);
  1093. /* load command */
  1094. for (i = info->scsi.SCp.sent_command; i < info->SCpnt->cmd_len; i++)
  1095. fas216_writeb(info, REG_FF, info->SCpnt->cmnd[i]);
  1096. fas216_cmd(info, CMD_TRANSFERINFO);
  1097. info->scsi.phase = PHASE_COMMAND;
  1098. }
  1099. /**
  1100. * fas216_send_messageout - handle bus service to send a message
  1101. * @info: interface which caused bus service
  1102. *
  1103. * Handle bus service to send a message.
  1104. * Note: We do not allow the device to change the data direction!
  1105. */
  1106. static void fas216_send_messageout(FAS216_Info *info, int start)
  1107. {
  1108. unsigned int tot_msglen = msgqueue_msglength(&info->scsi.msgs);
  1109. fas216_checkmagic(info);
  1110. fas216_cmd(info, CMD_FLUSHFIFO);
  1111. if (tot_msglen) {
  1112. struct message *msg;
  1113. int msgnr = 0;
  1114. while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
  1115. int i;
  1116. for (i = start; i < msg->length; i++)
  1117. fas216_writeb(info, REG_FF, msg->msg[i]);
  1118. msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
  1119. start = 0;
  1120. }
  1121. } else
  1122. fas216_writeb(info, REG_FF, NOP);
  1123. fas216_cmd(info, CMD_TRANSFERINFO);
  1124. info->scsi.phase = PHASE_MSGOUT;
  1125. }
  1126. /**
  1127. * fas216_busservice_intr - handle bus service interrupt from FAS216 chip
  1128. * @info: interface which caused bus service interrupt
  1129. * @stat: Status register contents
  1130. * @is: SCSI Status register contents
  1131. *
  1132. * Handle a bus service interrupt from FAS216 chip
  1133. */
  1134. static void fas216_busservice_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
  1135. {
  1136. fas216_checkmagic(info);
  1137. fas216_log(info, LOG_BUSSERVICE,
  1138. "bus service: stat=%02x is=%02x phase=%02x",
  1139. stat, is, info->scsi.phase);
  1140. switch (info->scsi.phase) {
  1141. case PHASE_SELECTION:
  1142. if ((is & IS_BITS) != IS_MSGBYTESENT)
  1143. goto bad_is;
  1144. break;
  1145. case PHASE_SELSTEPS:
  1146. switch (is & IS_BITS) {
  1147. case IS_SELARB:
  1148. case IS_MSGBYTESENT:
  1149. goto bad_is;
  1150. case IS_NOTCOMMAND:
  1151. case IS_EARLYPHASE:
  1152. if ((stat & STAT_BUSMASK) == STAT_MESGIN)
  1153. break;
  1154. goto bad_is;
  1155. case IS_COMPLETE:
  1156. break;
  1157. }
  1158. default:
  1159. break;
  1160. }
  1161. fas216_cmd(info, CMD_NOP);
  1162. #define STATE(st,ph) ((ph) << 3 | (st))
  1163. /* This table describes the legal SCSI state transitions,
  1164. * as described by the SCSI II spec.
  1165. */
  1166. switch (STATE(stat & STAT_BUSMASK, info->scsi.phase)) {
  1167. case STATE(STAT_DATAIN, PHASE_SELSTEPS):/* Sel w/ steps -> Data In */
  1168. case STATE(STAT_DATAIN, PHASE_MSGOUT): /* Message Out -> Data In */
  1169. case STATE(STAT_DATAIN, PHASE_COMMAND): /* Command -> Data In */
  1170. case STATE(STAT_DATAIN, PHASE_MSGIN): /* Message In -> Data In */
  1171. info->scsi.phase = PHASE_DATAIN;
  1172. fas216_transfer(info);
  1173. return;
  1174. case STATE(STAT_DATAIN, PHASE_DATAIN): /* Data In -> Data In */
  1175. case STATE(STAT_DATAOUT, PHASE_DATAOUT):/* Data Out -> Data Out */
  1176. fas216_cleanuptransfer(info);
  1177. fas216_transfer(info);
  1178. return;
  1179. case STATE(STAT_DATAOUT, PHASE_SELSTEPS):/* Sel w/ steps-> Data Out */
  1180. case STATE(STAT_DATAOUT, PHASE_MSGOUT): /* Message Out -> Data Out */
  1181. case STATE(STAT_DATAOUT, PHASE_COMMAND):/* Command -> Data Out */
  1182. case STATE(STAT_DATAOUT, PHASE_MSGIN): /* Message In -> Data Out */
  1183. fas216_cmd(info, CMD_FLUSHFIFO);
  1184. info->scsi.phase = PHASE_DATAOUT;
  1185. fas216_transfer(info);
  1186. return;
  1187. case STATE(STAT_STATUS, PHASE_DATAOUT): /* Data Out -> Status */
  1188. case STATE(STAT_STATUS, PHASE_DATAIN): /* Data In -> Status */
  1189. fas216_stoptransfer(info);
  1190. case STATE(STAT_STATUS, PHASE_SELSTEPS):/* Sel w/ steps -> Status */
  1191. case STATE(STAT_STATUS, PHASE_MSGOUT): /* Message Out -> Status */
  1192. case STATE(STAT_STATUS, PHASE_COMMAND): /* Command -> Status */
  1193. case STATE(STAT_STATUS, PHASE_MSGIN): /* Message In -> Status */
  1194. fas216_cmd(info, CMD_INITCMDCOMPLETE);
  1195. info->scsi.phase = PHASE_STATUS;
  1196. return;
  1197. case STATE(STAT_MESGIN, PHASE_DATAOUT): /* Data Out -> Message In */
  1198. case STATE(STAT_MESGIN, PHASE_DATAIN): /* Data In -> Message In */
  1199. fas216_stoptransfer(info);
  1200. case STATE(STAT_MESGIN, PHASE_COMMAND): /* Command -> Message In */
  1201. case STATE(STAT_MESGIN, PHASE_SELSTEPS):/* Sel w/ steps -> Message In */
  1202. case STATE(STAT_MESGIN, PHASE_MSGOUT): /* Message Out -> Message In */
  1203. info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
  1204. fas216_cmd(info, CMD_FLUSHFIFO);
  1205. fas216_cmd(info, CMD_TRANSFERINFO);
  1206. info->scsi.phase = PHASE_MSGIN;
  1207. return;
  1208. case STATE(STAT_MESGIN, PHASE_MSGIN):
  1209. info->scsi.msgin_fifo = fas216_readb(info, REG_CFIS) & CFIS_CF;
  1210. fas216_cmd(info, CMD_TRANSFERINFO);
  1211. return;
  1212. case STATE(STAT_COMMAND, PHASE_MSGOUT): /* Message Out -> Command */
  1213. case STATE(STAT_COMMAND, PHASE_MSGIN): /* Message In -> Command */
  1214. fas216_send_command(info);
  1215. info->scsi.phase = PHASE_COMMAND;
  1216. return;
  1217. /*
  1218. * Selection -> Message Out
  1219. */
  1220. case STATE(STAT_MESGOUT, PHASE_SELECTION):
  1221. fas216_send_messageout(info, 1);
  1222. return;
  1223. /*
  1224. * Message Out -> Message Out
  1225. */
  1226. case STATE(STAT_MESGOUT, PHASE_SELSTEPS):
  1227. case STATE(STAT_MESGOUT, PHASE_MSGOUT):
  1228. /*
  1229. * If we get another message out phase, this usually
  1230. * means some parity error occurred. Resend complete
  1231. * set of messages. If we have more than one byte to
  1232. * send, we need to assert ATN again.
  1233. */
  1234. if (info->device[info->SCpnt->device->id].parity_check) {
  1235. /*
  1236. * We were testing... good, the device
  1237. * supports parity checking.
  1238. */
  1239. info->device[info->SCpnt->device->id].parity_check = 0;
  1240. info->device[info->SCpnt->device->id].parity_enabled = 1;
  1241. fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
  1242. }
  1243. if (msgqueue_msglength(&info->scsi.msgs) > 1)
  1244. fas216_cmd(info, CMD_SETATN);
  1245. /*FALLTHROUGH*/
  1246. /*
  1247. * Any -> Message Out
  1248. */
  1249. case STATE(STAT_MESGOUT, PHASE_MSGOUT_EXPECT):
  1250. fas216_send_messageout(info, 0);
  1251. return;
  1252. /* Error recovery rules.
  1253. * These either attempt to abort or retry the operation.
  1254. * TODO: we need more of these
  1255. */
  1256. case STATE(STAT_COMMAND, PHASE_COMMAND):/* Command -> Command */
  1257. /* error - we've sent out all the command bytes
  1258. * we have.
  1259. * NOTE: we need SAVE DATA POINTERS/RESTORE DATA POINTERS
  1260. * to include the command bytes sent for this to work
  1261. * correctly.
  1262. */
  1263. printk(KERN_ERR "scsi%d.%c: "
  1264. "target trying to receive more command bytes\n",
  1265. info->host->host_no, fas216_target(info));
  1266. fas216_cmd(info, CMD_SETATN);
  1267. fas216_set_stc(info, 15);
  1268. fas216_cmd(info, CMD_PADBYTES | CMD_WITHDMA);
  1269. msgqueue_flush(&info->scsi.msgs);
  1270. msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
  1271. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  1272. return;
  1273. }
  1274. if (info->scsi.phase == PHASE_MSGIN_DISCONNECT) {
  1275. printk(KERN_ERR "scsi%d.%c: disconnect message received, but bus service %s?\n",
  1276. info->host->host_no, fas216_target(info),
  1277. fas216_bus_phase(stat));
  1278. msgqueue_flush(&info->scsi.msgs);
  1279. fas216_cmd(info, CMD_SETATN);
  1280. msgqueue_addmsg(&info->scsi.msgs, 1, INITIATOR_ERROR);
  1281. info->scsi.phase = PHASE_MSGOUT_EXPECT;
  1282. info->scsi.aborting = 1;
  1283. fas216_cmd(info, CMD_TRANSFERINFO);
  1284. return;
  1285. }
  1286. printk(KERN_ERR "scsi%d.%c: bus phase %s after %s?\n",
  1287. info->host->host_no, fas216_target(info),
  1288. fas216_bus_phase(stat),
  1289. fas216_drv_phase(info));
  1290. print_debug_list();
  1291. return;
  1292. bad_is:
  1293. fas216_log(info, 0, "bus service at step %d?", is & IS_BITS);
  1294. fas216_dumpstate(info);
  1295. print_debug_list();
  1296. fas216_done(info, DID_ERROR);
  1297. }
  1298. /**
  1299. * fas216_funcdone_intr - handle a function done interrupt from FAS216 chip
  1300. * @info: interface which caused function done interrupt
  1301. * @stat: Status register contents
  1302. * @is: SCSI Status register contents
  1303. *
  1304. * Handle a function done interrupt from FAS216 chip
  1305. */
  1306. static void fas216_funcdone_intr(FAS216_Info *info, unsigned int stat, unsigned int is)
  1307. {
  1308. unsigned int fifo_len = fas216_readb(info, REG_CFIS) & CFIS_CF;
  1309. fas216_checkmagic(info);
  1310. fas216_log(info, LOG_FUNCTIONDONE,
  1311. "function done: stat=%02x is=%02x phase=%02x",
  1312. stat, is, info->scsi.phase);
  1313. switch (info->scsi.phase) {
  1314. case PHASE_STATUS: /* status phase - read status and msg */
  1315. if (fifo_len != 2) {
  1316. fas216_log(info, 0, "odd number of bytes in FIFO: %d", fifo_len);
  1317. }
  1318. /*
  1319. * Read status then message byte.
  1320. */
  1321. info->scsi.SCp.Status = fas216_readb(info, REG_FF);
  1322. info->scsi.SCp.Message = fas216_readb(info, REG_FF);
  1323. info->scsi.phase = PHASE_DONE;
  1324. fas216_cmd(info, CMD_MSGACCEPTED);
  1325. break;
  1326. case PHASE_IDLE:
  1327. case PHASE_SELECTION:
  1328. case PHASE_SELSTEPS:
  1329. break;
  1330. case PHASE_MSGIN: /* message in phase */
  1331. if ((stat & STAT_BUSMASK) == STAT_MESGIN) {
  1332. info->scsi.msgin_fifo = fifo_len;
  1333. fas216_message(info);
  1334. break;
  1335. }
  1336. default:
  1337. fas216_log(info, 0, "internal phase %s for function done?"
  1338. " What do I do with this?",
  1339. fas216_target(info), fas216_drv_phase(info));
  1340. }
  1341. }
  1342. static void fas216_bus_reset(FAS216_Info *info)
  1343. {
  1344. neg_t sync_state;
  1345. int i;
  1346. msgqueue_flush(&info->scsi.msgs);
  1347. sync_state = neg_invalid;
  1348. #ifdef SCSI2_SYNC
  1349. if (info->ifcfg.capabilities & (FASCAP_DMA|FASCAP_PSEUDODMA))
  1350. sync_state = neg_wait;
  1351. #endif
  1352. info->scsi.phase = PHASE_IDLE;
  1353. info->SCpnt = NULL; /* bug! */
  1354. memset(&info->scsi.SCp, 0, sizeof(info->scsi.SCp));
  1355. for (i = 0; i < 8; i++) {
  1356. info->device[i].disconnect_ok = info->ifcfg.disconnect_ok;
  1357. info->device[i].sync_state = sync_state;
  1358. info->device[i].period = info->ifcfg.asyncperiod / 4;
  1359. info->device[i].stp = info->scsi.async_stp;
  1360. info->device[i].sof = 0;
  1361. info->device[i].wide_xfer = 0;
  1362. }
  1363. info->rst_bus_status = 1;
  1364. wake_up(&info->eh_wait);
  1365. }
  1366. /**
  1367. * fas216_intr - handle interrupts to progress a command
  1368. * @info: interface to service
  1369. *
  1370. * Handle interrupts from the interface to progress a command
  1371. */
  1372. irqreturn_t fas216_intr(FAS216_Info *info)
  1373. {
  1374. unsigned char inst, is, stat;
  1375. int handled = IRQ_NONE;
  1376. fas216_checkmagic(info);
  1377. stat = fas216_readb(info, REG_STAT);
  1378. is = fas216_readb(info, REG_IS);
  1379. inst = fas216_readb(info, REG_INST);
  1380. add_debug_list(stat, is, inst, info->scsi.phase);
  1381. if (stat & STAT_INT) {
  1382. if (inst & INST_BUSRESET) {
  1383. fas216_log(info, 0, "bus reset detected");
  1384. fas216_bus_reset(info);
  1385. scsi_report_bus_reset(info->host, 0);
  1386. } else if (inst & INST_ILLEGALCMD) {
  1387. fas216_log(info, LOG_ERROR, "illegal command given\n");
  1388. fas216_dumpstate(info);
  1389. print_debug_list();
  1390. } else if (inst & INST_DISCONNECT)
  1391. fas216_disconnect_intr(info);
  1392. else if (inst & INST_RESELECTED) /* reselected */
  1393. fas216_reselected_intr(info);
  1394. else if (inst & INST_BUSSERVICE) /* bus service request */
  1395. fas216_busservice_intr(info, stat, is);
  1396. else if (inst & INST_FUNCDONE) /* function done */
  1397. fas216_funcdone_intr(info, stat, is);
  1398. else
  1399. fas216_log(info, 0, "unknown interrupt received:"
  1400. " phase %s inst %02X is %02X stat %02X",
  1401. fas216_drv_phase(info), inst, is, stat);
  1402. handled = IRQ_HANDLED;
  1403. }
  1404. return handled;
  1405. }
  1406. static void __fas216_start_command(FAS216_Info *info, struct scsi_cmnd *SCpnt)
  1407. {
  1408. int tot_msglen;
  1409. /* following what the ESP driver says */
  1410. fas216_set_stc(info, 0);
  1411. fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
  1412. /* flush FIFO */
  1413. fas216_cmd(info, CMD_FLUSHFIFO);
  1414. /* load bus-id and timeout */
  1415. fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
  1416. fas216_writeb(info, REG_STIM, info->ifcfg.select_timeout);
  1417. /* synchronous transfers */
  1418. fas216_set_sync(info, SCpnt->device->id);
  1419. tot_msglen = msgqueue_msglength(&info->scsi.msgs);
  1420. #ifdef DEBUG_MESSAGES
  1421. {
  1422. struct message *msg;
  1423. int msgnr = 0, i;
  1424. printk("scsi%d.%c: message out: ",
  1425. info->host->host_no, '0' + SCpnt->device->id);
  1426. while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
  1427. printk("{ ");
  1428. for (i = 0; i < msg->length; i++)
  1429. printk("%02x ", msg->msg[i]);
  1430. printk("} ");
  1431. }
  1432. printk("\n");
  1433. }
  1434. #endif
  1435. if (tot_msglen == 1 || tot_msglen == 3) {
  1436. /*
  1437. * We have an easy message length to send...
  1438. */
  1439. struct message *msg;
  1440. int msgnr = 0, i;
  1441. info->scsi.phase = PHASE_SELSTEPS;
  1442. /* load message bytes */
  1443. while ((msg = msgqueue_getmsg(&info->scsi.msgs, msgnr++)) != NULL) {
  1444. for (i = 0; i < msg->length; i++)
  1445. fas216_writeb(info, REG_FF, msg->msg[i]);
  1446. msg->fifo = tot_msglen - (fas216_readb(info, REG_CFIS) & CFIS_CF);
  1447. }
  1448. /* load command */
  1449. for (i = 0; i < SCpnt->cmd_len; i++)
  1450. fas216_writeb(info, REG_FF, SCpnt->cmnd[i]);
  1451. if (tot_msglen == 1)
  1452. fas216_cmd(info, CMD_SELECTATN);
  1453. else
  1454. fas216_cmd(info, CMD_SELECTATN3);
  1455. } else {
  1456. /*
  1457. * We have an unusual number of message bytes to send.
  1458. * Load first byte into fifo, and issue SELECT with ATN and
  1459. * stop steps.
  1460. */
  1461. struct message *msg = msgqueue_getmsg(&info->scsi.msgs, 0);
  1462. fas216_writeb(info, REG_FF, msg->msg[0]);
  1463. msg->fifo = 1;
  1464. fas216_cmd(info, CMD_SELECTATNSTOP);
  1465. }
  1466. }
  1467. /*
  1468. * Decide whether we need to perform a parity test on this device.
  1469. * Can also be used to force parity error conditions during initial
  1470. * information transfer phase (message out) for test purposes.
  1471. */
  1472. static int parity_test(FAS216_Info *info, int target)
  1473. {
  1474. #if 0
  1475. if (target == 3) {
  1476. info->device[target].parity_check = 0;
  1477. return 1;
  1478. }
  1479. #endif
  1480. return info->device[target].parity_check;
  1481. }
  1482. static void fas216_start_command(FAS216_Info *info, struct scsi_cmnd *SCpnt)
  1483. {
  1484. int disconnect_ok;
  1485. /*
  1486. * claim host busy
  1487. */
  1488. info->scsi.phase = PHASE_SELECTION;
  1489. info->scsi.SCp = SCpnt->SCp;
  1490. info->SCpnt = SCpnt;
  1491. info->dma.transfer_type = fasdma_none;
  1492. if (parity_test(info, SCpnt->device->id))
  1493. fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0] | CNTL1_PTE);
  1494. else
  1495. fas216_writeb(info, REG_CNTL1, info->scsi.cfg[0]);
  1496. /*
  1497. * Don't allow request sense commands to disconnect.
  1498. */
  1499. disconnect_ok = SCpnt->cmnd[0] != REQUEST_SENSE &&
  1500. info->device[SCpnt->device->id].disconnect_ok;
  1501. /*
  1502. * build outgoing message bytes
  1503. */
  1504. msgqueue_flush(&info->scsi.msgs);
  1505. msgqueue_addmsg(&info->scsi.msgs, 1, IDENTIFY(disconnect_ok, SCpnt->device->lun));
  1506. /*
  1507. * add tag message if required
  1508. */
  1509. if (SCpnt->tag)
  1510. msgqueue_addmsg(&info->scsi.msgs, 2, SIMPLE_QUEUE_TAG, SCpnt->tag);
  1511. do {
  1512. #ifdef SCSI2_SYNC
  1513. if ((info->device[SCpnt->device->id].sync_state == neg_wait ||
  1514. info->device[SCpnt->device->id].sync_state == neg_complete) &&
  1515. (SCpnt->cmnd[0] == REQUEST_SENSE ||
  1516. SCpnt->cmnd[0] == INQUIRY)) {
  1517. info->device[SCpnt->device->id].sync_state = neg_inprogress;
  1518. msgqueue_addmsg(&info->scsi.msgs, 5,
  1519. EXTENDED_MESSAGE, 3, EXTENDED_SDTR,
  1520. 1000 / info->ifcfg.clockrate,
  1521. info->ifcfg.sync_max_depth);
  1522. break;
  1523. }
  1524. #endif
  1525. } while (0);
  1526. __fas216_start_command(info, SCpnt);
  1527. }
  1528. static void fas216_allocate_tag(FAS216_Info *info, struct scsi_cmnd *SCpnt)
  1529. {
  1530. #ifdef SCSI2_TAG
  1531. /*
  1532. * tagged queuing - allocate a new tag to this command
  1533. */
  1534. if (SCpnt->device->simple_tags && SCpnt->cmnd[0] != REQUEST_SENSE &&
  1535. SCpnt->cmnd[0] != INQUIRY) {
  1536. SCpnt->device->current_tag += 1;
  1537. if (SCpnt->device->current_tag == 0)
  1538. SCpnt->device->current_tag = 1;
  1539. SCpnt->tag = SCpnt->device->current_tag;
  1540. } else
  1541. #endif
  1542. set_bit(SCpnt->device->id * 8 + SCpnt->device->lun, info->busyluns);
  1543. info->stats.removes += 1;
  1544. switch (SCpnt->cmnd[0]) {
  1545. case WRITE_6:
  1546. case WRITE_10:
  1547. case WRITE_12:
  1548. info->stats.writes += 1;
  1549. break;
  1550. case READ_6:
  1551. case READ_10:
  1552. case READ_12:
  1553. info->stats.reads += 1;
  1554. break;
  1555. default:
  1556. info->stats.miscs += 1;
  1557. break;
  1558. }
  1559. }
  1560. static void fas216_do_bus_device_reset(FAS216_Info *info,
  1561. struct scsi_cmnd *SCpnt)
  1562. {
  1563. struct message *msg;
  1564. /*
  1565. * claim host busy
  1566. */
  1567. info->scsi.phase = PHASE_SELECTION;
  1568. info->scsi.SCp = SCpnt->SCp;
  1569. info->SCpnt = SCpnt;
  1570. info->dma.transfer_type = fasdma_none;
  1571. fas216_log(info, LOG_ERROR, "sending bus device reset");
  1572. msgqueue_flush(&info->scsi.msgs);
  1573. msgqueue_addmsg(&info->scsi.msgs, 1, BUS_DEVICE_RESET);
  1574. /* following what the ESP driver says */
  1575. fas216_set_stc(info, 0);
  1576. fas216_cmd(info, CMD_NOP | CMD_WITHDMA);
  1577. /* flush FIFO */
  1578. fas216_cmd(info, CMD_FLUSHFIFO);
  1579. /* load bus-id and timeout */
  1580. fas216_writeb(info, REG_SDID, BUSID(SCpnt->device->id));
  1581. fas216_writeb(info, REG_STI