/drivers/scsi/aha152x.c

https://bitbucket.org/cyanogenmod/android_kernel_asus_tf300t · C · 3985 lines · 2794 code · 615 blank · 576 comment · 598 complexity · d03cc28137d1cd08fbfbd47e155f6a28 MD5 · raw file

Large files are truncated click here to view the full file

  1. /* aha152x.c -- Adaptec AHA-152x driver
  2. * Author: Jürgen E. Fischer, fischer@norbit.de
  3. * Copyright 1993-2004 Jürgen E. Fischer
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2, or (at your option) any
  8. * later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. *
  16. * $Id: aha152x.c,v 2.7 2004/01/24 11:42:59 fischer Exp $
  17. *
  18. * $Log: aha152x.c,v $
  19. * Revision 2.7 2004/01/24 11:42:59 fischer
  20. * - gather code that is not used by PCMCIA at the end
  21. * - move request_region for !PCMCIA case to detection
  22. * - migration to new scsi host api (remove legacy code)
  23. * - free host scribble before scsi_done
  24. * - fix error handling
  25. * - one isapnp device added to id_table
  26. *
  27. * Revision 2.6 2003/10/30 20:52:47 fischer
  28. * - interfaces changes for kernel 2.6
  29. * - aha152x_probe_one introduced for pcmcia stub
  30. * - fixed pnpdev handling
  31. * - instead of allocation a new one, reuse command for request sense after check condition and reset
  32. * - fixes race in is_complete
  33. *
  34. * Revision 2.5 2002/04/14 11:24:53 fischer
  35. * - isapnp support
  36. * - abort fixed
  37. * - 2.5 support
  38. *
  39. * Revision 2.4 2000/12/16 12:53:56 fischer
  40. * - allow REQUEST SENSE to be queued
  41. * - handle shared PCI interrupts
  42. *
  43. * Revision 2.3 2000/11/04 16:40:26 fischer
  44. * - handle data overruns
  45. * - extend timeout for data phases
  46. *
  47. * Revision 2.2 2000/08/08 19:54:53 fischer
  48. * - minor changes
  49. *
  50. * Revision 2.1 2000/05/17 16:23:17 fischer
  51. * - signature update
  52. * - fix for data out w/o scatter gather
  53. *
  54. * Revision 2.0 1999/12/25 15:07:32 fischer
  55. * - interrupt routine completly reworked
  56. * - basic support for new eh code
  57. *
  58. * Revision 1.21 1999/11/10 23:46:36 fischer
  59. * - default to synchronous operation
  60. * - synchronous negotiation fixed
  61. * - added timeout to loops
  62. * - debugging output can be controlled through procfs
  63. *
  64. * Revision 1.20 1999/11/07 18:37:31 fischer
  65. * - synchronous operation works
  66. * - resid support for sg driver
  67. *
  68. * Revision 1.19 1999/11/02 22:39:59 fischer
  69. * - moved leading comments to README.aha152x
  70. * - new additional module parameters
  71. * - updates for 2.3
  72. * - support for the Tripace TC1550 controller
  73. * - interrupt handling changed
  74. *
  75. * Revision 1.18 1996/09/07 20:10:40 fischer
  76. * - fixed can_queue handling (multiple outstanding commands working again)
  77. *
  78. * Revision 1.17 1996/08/17 16:05:14 fischer
  79. * - biosparam improved
  80. * - interrupt verification
  81. * - updated documentation
  82. * - cleanups
  83. *
  84. * Revision 1.16 1996/06/09 00:04:56 root
  85. * - added configuration symbols for insmod (aha152x/aha152x1)
  86. *
  87. * Revision 1.15 1996/04/30 14:52:06 fischer
  88. * - proc info fixed
  89. * - support for extended translation for >1GB disks
  90. *
  91. * Revision 1.14 1996/01/17 15:11:20 fischer
  92. * - fixed lockup in MESSAGE IN phase after reconnection
  93. *
  94. * Revision 1.13 1996/01/09 02:15:53 fischer
  95. * - some cleanups
  96. * - moved request_irq behind controller initialization
  97. * (to avoid spurious interrupts)
  98. *
  99. * Revision 1.12 1995/12/16 12:26:07 fischer
  100. * - barrier()s added
  101. * - configurable RESET delay added
  102. *
  103. * Revision 1.11 1995/12/06 21:18:35 fischer
  104. * - some minor updates
  105. *
  106. * Revision 1.10 1995/07/22 19:18:45 fischer
  107. * - support for 2 controllers
  108. * - started synchronous data transfers (not working yet)
  109. *
  110. * Revision 1.9 1995/03/18 09:20:24 root
  111. * - patches for PCMCIA and modules
  112. *
  113. * Revision 1.8 1995/01/21 22:07:19 root
  114. * - snarf_region => request_region
  115. * - aha152x_intr interface change
  116. *
  117. * Revision 1.7 1995/01/02 23:19:36 root
  118. * - updated COMMAND_SIZE to cmd_len
  119. * - changed sti() to restore_flags()
  120. * - fixed some #ifdef which generated warnings
  121. *
  122. * Revision 1.6 1994/11/24 20:35:27 root
  123. * - problem with odd number of bytes in fifo fixed
  124. *
  125. * Revision 1.5 1994/10/30 14:39:56 root
  126. * - abort code fixed
  127. * - debugging improved
  128. *
  129. * Revision 1.4 1994/09/12 11:33:01 root
  130. * - irqaction to request_irq
  131. * - abortion updated
  132. *
  133. * Revision 1.3 1994/08/04 13:53:05 root
  134. * - updates for mid-level-driver changes
  135. * - accept unexpected BUSFREE phase as error condition
  136. * - parity check now configurable
  137. *
  138. * Revision 1.2 1994/07/03 12:56:36 root
  139. * - cleaned up debugging code
  140. * - more tweaking on reset delays
  141. * - updated abort/reset code (pretty untested...)
  142. *
  143. * Revision 1.1 1994/05/28 21:18:49 root
  144. * - update for mid-level interface change (abort-reset)
  145. * - delays after resets adjusted for some slow devices
  146. *
  147. * Revision 1.0 1994/03/25 12:52:00 root
  148. * - Fixed "more data than expected" problem
  149. * - added new BIOS signatures
  150. *
  151. * Revision 0.102 1994/01/31 20:44:12 root
  152. * - minor changes in insw/outsw handling
  153. *
  154. * Revision 0.101 1993/12/13 01:16:27 root
  155. * - fixed STATUS phase (non-GOOD stati were dropped sometimes;
  156. * fixes problems with CD-ROM sector size detection & media change)
  157. *
  158. * Revision 0.100 1993/12/10 16:58:47 root
  159. * - fix for unsuccessful selections in case of non-continuous id assignments
  160. * on the scsi bus.
  161. *
  162. * Revision 0.99 1993/10/24 16:19:59 root
  163. * - fixed DATA IN (rare read errors gone)
  164. *
  165. * Revision 0.98 1993/10/17 12:54:44 root
  166. * - fixed some recent fixes (shame on me)
  167. * - moved initialization of scratch area to aha152x_queue
  168. *
  169. * Revision 0.97 1993/10/09 18:53:53 root
  170. * - DATA IN fixed. Rarely left data in the fifo.
  171. *
  172. * Revision 0.96 1993/10/03 00:53:59 root
  173. * - minor changes on DATA IN
  174. *
  175. * Revision 0.95 1993/09/24 10:36:01 root
  176. * - change handling of MSGI after reselection
  177. * - fixed sti/cli
  178. * - minor changes
  179. *
  180. * Revision 0.94 1993/09/18 14:08:22 root
  181. * - fixed bug in multiple outstanding command code
  182. * - changed detection
  183. * - support for kernel command line configuration
  184. * - reset corrected
  185. * - changed message handling
  186. *
  187. * Revision 0.93 1993/09/15 20:41:19 root
  188. * - fixed bugs with multiple outstanding commands
  189. *
  190. * Revision 0.92 1993/09/13 02:46:33 root
  191. * - multiple outstanding commands work (no problems with IBM drive)
  192. *
  193. * Revision 0.91 1993/09/12 20:51:46 root
  194. * added multiple outstanding commands
  195. * (some problem with this $%&? IBM device remain)
  196. *
  197. * Revision 0.9 1993/09/12 11:11:22 root
  198. * - corrected auto-configuration
  199. * - changed the auto-configuration (added some '#define's)
  200. * - added support for dis-/reconnection
  201. *
  202. * Revision 0.8 1993/09/06 23:09:39 root
  203. * - added support for the drive activity light
  204. * - minor changes
  205. *
  206. * Revision 0.7 1993/09/05 14:30:15 root
  207. * - improved phase detection
  208. * - now using the new snarf_region code of 0.99pl13
  209. *
  210. * Revision 0.6 1993/09/02 11:01:38 root
  211. * first public release; added some signatures and biosparam()
  212. *
  213. * Revision 0.5 1993/08/30 10:23:30 root
  214. * fixed timing problems with my IBM drive
  215. *
  216. * Revision 0.4 1993/08/29 14:06:52 root
  217. * fixed some problems with timeouts due incomplete commands
  218. *
  219. * Revision 0.3 1993/08/28 15:55:03 root
  220. * writing data works too. mounted and worked on a dos partition
  221. *
  222. * Revision 0.2 1993/08/27 22:42:07 root
  223. * reading data works. Mounted a msdos partition.
  224. *
  225. * Revision 0.1 1993/08/25 13:38:30 root
  226. * first "damn thing doesn't work" version
  227. *
  228. * Revision 0.0 1993/08/14 19:54:25 root
  229. * empty function bodies; detect() works.
  230. *
  231. *
  232. **************************************************************************
  233. see Documentation/scsi/aha152x.txt for configuration details
  234. **************************************************************************/
  235. #include <linux/module.h>
  236. #include <asm/irq.h>
  237. #include <linux/io.h>
  238. #include <linux/blkdev.h>
  239. #include <asm/system.h>
  240. #include <linux/completion.h>
  241. #include <linux/errno.h>
  242. #include <linux/string.h>
  243. #include <linux/wait.h>
  244. #include <linux/ioport.h>
  245. #include <linux/delay.h>
  246. #include <linux/proc_fs.h>
  247. #include <linux/interrupt.h>
  248. #include <linux/init.h>
  249. #include <linux/kernel.h>
  250. #include <linux/isapnp.h>
  251. #include <linux/spinlock.h>
  252. #include <linux/workqueue.h>
  253. #include <linux/list.h>
  254. #include <linux/slab.h>
  255. #include <scsi/scsicam.h>
  256. #include "scsi.h"
  257. #include <scsi/scsi_dbg.h>
  258. #include <scsi/scsi_host.h>
  259. #include <scsi/scsi_transport_spi.h>
  260. #include <scsi/scsi_eh.h>
  261. #include "aha152x.h"
  262. static LIST_HEAD(aha152x_host_list);
  263. /* DEFINES */
  264. /* For PCMCIA cards, always use AUTOCONF */
  265. #if defined(PCMCIA) || defined(MODULE)
  266. #if !defined(AUTOCONF)
  267. #define AUTOCONF
  268. #endif
  269. #endif
  270. #if !defined(AUTOCONF) && !defined(SETUP0)
  271. #error define AUTOCONF or SETUP0
  272. #endif
  273. #if defined(AHA152X_DEBUG)
  274. #define DEBUG_DEFAULT debug_eh
  275. #define DPRINTK(when,msgs...) \
  276. do { if(HOSTDATA(shpnt)->debug & (when)) printk(msgs); } while(0)
  277. #define DO_LOCK(flags) \
  278. do { \
  279. if(spin_is_locked(&QLOCK)) { \
  280. DPRINTK(debug_intr, DEBUG_LEAD "(%s:%d) already locked at %s:%d\n", CMDINFO(CURRENT_SC), __func__, __LINE__, QLOCKER, QLOCKERL); \
  281. } \
  282. DPRINTK(debug_locking, DEBUG_LEAD "(%s:%d) locking\n", CMDINFO(CURRENT_SC), __func__, __LINE__); \
  283. spin_lock_irqsave(&QLOCK,flags); \
  284. DPRINTK(debug_locking, DEBUG_LEAD "(%s:%d) locked\n", CMDINFO(CURRENT_SC), __func__, __LINE__); \
  285. QLOCKER=__func__; \
  286. QLOCKERL=__LINE__; \
  287. } while(0)
  288. #define DO_UNLOCK(flags) \
  289. do { \
  290. DPRINTK(debug_locking, DEBUG_LEAD "(%s:%d) unlocking (locked at %s:%d)\n", CMDINFO(CURRENT_SC), __func__, __LINE__, QLOCKER, QLOCKERL); \
  291. spin_unlock_irqrestore(&QLOCK,flags); \
  292. DPRINTK(debug_locking, DEBUG_LEAD "(%s:%d) unlocked\n", CMDINFO(CURRENT_SC), __func__, __LINE__); \
  293. QLOCKER="(not locked)"; \
  294. QLOCKERL=0; \
  295. } while(0)
  296. #else
  297. #define DPRINTK(when,msgs...)
  298. #define DO_LOCK(flags) spin_lock_irqsave(&QLOCK,flags)
  299. #define DO_UNLOCK(flags) spin_unlock_irqrestore(&QLOCK,flags)
  300. #endif
  301. #define LEAD "(scsi%d:%d:%d) "
  302. #define WARN_LEAD KERN_WARNING LEAD
  303. #define INFO_LEAD KERN_INFO LEAD
  304. #define NOTE_LEAD KERN_NOTICE LEAD
  305. #define ERR_LEAD KERN_ERR LEAD
  306. #define DEBUG_LEAD KERN_DEBUG LEAD
  307. #define CMDINFO(cmd) \
  308. (cmd) ? ((cmd)->device->host->host_no) : -1, \
  309. (cmd) ? ((cmd)->device->id & 0x0f) : -1, \
  310. (cmd) ? ((cmd)->device->lun & 0x07) : -1
  311. static inline void
  312. CMD_INC_RESID(struct scsi_cmnd *cmd, int inc)
  313. {
  314. scsi_set_resid(cmd, scsi_get_resid(cmd) + inc);
  315. }
  316. #define DELAY_DEFAULT 1000
  317. #if defined(PCMCIA)
  318. #define IRQ_MIN 0
  319. #define IRQ_MAX 16
  320. #else
  321. #define IRQ_MIN 9
  322. #if defined(__PPC)
  323. #define IRQ_MAX (nr_irqs-1)
  324. #else
  325. #define IRQ_MAX 12
  326. #endif
  327. #endif
  328. enum {
  329. not_issued = 0x0001, /* command not yet issued */
  330. selecting = 0x0002, /* target is beeing selected */
  331. identified = 0x0004, /* IDENTIFY was sent */
  332. disconnected = 0x0008, /* target disconnected */
  333. completed = 0x0010, /* target sent COMMAND COMPLETE */
  334. aborted = 0x0020, /* ABORT was sent */
  335. resetted = 0x0040, /* BUS DEVICE RESET was sent */
  336. spiordy = 0x0080, /* waiting for SPIORDY to raise */
  337. syncneg = 0x0100, /* synchronous negotiation in progress */
  338. aborting = 0x0200, /* ABORT is pending */
  339. resetting = 0x0400, /* BUS DEVICE RESET is pending */
  340. check_condition = 0x0800, /* requesting sense after CHECK CONDITION */
  341. };
  342. MODULE_AUTHOR("Jürgen Fischer");
  343. MODULE_DESCRIPTION(AHA152X_REVID);
  344. MODULE_LICENSE("GPL");
  345. #if !defined(PCMCIA)
  346. #if defined(MODULE)
  347. static int io[] = {0, 0};
  348. module_param_array(io, int, NULL, 0);
  349. MODULE_PARM_DESC(io,"base io address of controller");
  350. static int irq[] = {0, 0};
  351. module_param_array(irq, int, NULL, 0);
  352. MODULE_PARM_DESC(irq,"interrupt for controller");
  353. static int scsiid[] = {7, 7};
  354. module_param_array(scsiid, int, NULL, 0);
  355. MODULE_PARM_DESC(scsiid,"scsi id of controller");
  356. static int reconnect[] = {1, 1};
  357. module_param_array(reconnect, int, NULL, 0);
  358. MODULE_PARM_DESC(reconnect,"allow targets to disconnect");
  359. static int parity[] = {1, 1};
  360. module_param_array(parity, int, NULL, 0);
  361. MODULE_PARM_DESC(parity,"use scsi parity");
  362. static int sync[] = {1, 1};
  363. module_param_array(sync, int, NULL, 0);
  364. MODULE_PARM_DESC(sync,"use synchronous transfers");
  365. static int delay[] = {DELAY_DEFAULT, DELAY_DEFAULT};
  366. module_param_array(delay, int, NULL, 0);
  367. MODULE_PARM_DESC(delay,"scsi reset delay");
  368. static int exttrans[] = {0, 0};
  369. module_param_array(exttrans, int, NULL, 0);
  370. MODULE_PARM_DESC(exttrans,"use extended translation");
  371. #if !defined(AHA152X_DEBUG)
  372. static int aha152x[] = {0, 11, 7, 1, 1, 0, DELAY_DEFAULT, 0};
  373. module_param_array(aha152x, int, NULL, 0);
  374. MODULE_PARM_DESC(aha152x, "parameters for first controller");
  375. static int aha152x1[] = {0, 11, 7, 1, 1, 0, DELAY_DEFAULT, 0};
  376. module_param_array(aha152x1, int, NULL, 0);
  377. MODULE_PARM_DESC(aha152x1, "parameters for second controller");
  378. #else
  379. static int debug[] = {DEBUG_DEFAULT, DEBUG_DEFAULT};
  380. module_param_array(debug, int, NULL, 0);
  381. MODULE_PARM_DESC(debug, "flags for driver debugging");
  382. static int aha152x[] = {0, 11, 7, 1, 1, 1, DELAY_DEFAULT, 0, DEBUG_DEFAULT};
  383. module_param_array(aha152x, int, NULL, 0);
  384. MODULE_PARM_DESC(aha152x, "parameters for first controller");
  385. static int aha152x1[] = {0, 11, 7, 1, 1, 1, DELAY_DEFAULT, 0, DEBUG_DEFAULT};
  386. module_param_array(aha152x1, int, NULL, 0);
  387. MODULE_PARM_DESC(aha152x1, "parameters for second controller");
  388. #endif /* !defined(AHA152X_DEBUG) */
  389. #endif /* MODULE */
  390. #ifdef __ISAPNP__
  391. static struct isapnp_device_id id_table[] __devinitdata = {
  392. { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1502), 0 },
  393. { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1505), 0 },
  394. { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1510), 0 },
  395. { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1515), 0 },
  396. { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1520), 0 },
  397. { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x2015), 0 },
  398. { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1522), 0 },
  399. { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x2215), 0 },
  400. { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1530), 0 },
  401. { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x3015), 0 },
  402. { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x1532), 0 },
  403. { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x3215), 0 },
  404. { ISAPNP_ANY_ID, ISAPNP_ANY_ID, ISAPNP_VENDOR('A', 'D', 'P'), ISAPNP_FUNCTION(0x6360), 0 },
  405. { ISAPNP_DEVICE_SINGLE_END, }
  406. };
  407. MODULE_DEVICE_TABLE(isapnp, id_table);
  408. #endif /* ISAPNP */
  409. #endif /* !PCMCIA */
  410. static struct scsi_host_template aha152x_driver_template;
  411. /*
  412. * internal states of the host
  413. *
  414. */
  415. enum aha152x_state {
  416. idle=0,
  417. unknown,
  418. seldo,
  419. seldi,
  420. selto,
  421. busfree,
  422. msgo,
  423. cmd,
  424. msgi,
  425. status,
  426. datai,
  427. datao,
  428. parerr,
  429. rsti,
  430. maxstate
  431. };
  432. /*
  433. * current state information of the host
  434. *
  435. */
  436. struct aha152x_hostdata {
  437. Scsi_Cmnd *issue_SC;
  438. /* pending commands to issue */
  439. Scsi_Cmnd *current_SC;
  440. /* current command on the bus */
  441. Scsi_Cmnd *disconnected_SC;
  442. /* commands that disconnected */
  443. Scsi_Cmnd *done_SC;
  444. /* command that was completed */
  445. spinlock_t lock;
  446. /* host lock */
  447. #if defined(AHA152X_DEBUG)
  448. const char *locker;
  449. /* which function has the lock */
  450. int lockerl; /* where did it get it */
  451. int debug; /* current debugging setting */
  452. #endif
  453. #if defined(AHA152X_STAT)
  454. int total_commands;
  455. int disconnections;
  456. int busfree_without_any_action;
  457. int busfree_without_old_command;
  458. int busfree_without_new_command;
  459. int busfree_without_done_command;
  460. int busfree_with_check_condition;
  461. int count[maxstate];
  462. int count_trans[maxstate];
  463. unsigned long time[maxstate];
  464. #endif
  465. int commands; /* current number of commands */
  466. int reconnect; /* disconnection allowed */
  467. int parity; /* parity checking enabled */
  468. int synchronous; /* synchronous transferes enabled */
  469. int delay; /* reset out delay */
  470. int ext_trans; /* extended translation enabled */
  471. int swint; /* software-interrupt was fired during detect() */
  472. int service; /* bh needs to be run */
  473. int in_intr; /* bh is running */
  474. /* current state,
  475. previous state,
  476. last state different from current state */
  477. enum aha152x_state state, prevstate, laststate;
  478. int target;
  479. /* reconnecting target */
  480. unsigned char syncrate[8];
  481. /* current synchronous transfer agreements */
  482. unsigned char syncneg[8];
  483. /* 0: no negotiation;
  484. * 1: negotiation in progress;
  485. * 2: negotiation completed
  486. */
  487. int cmd_i;
  488. /* number of sent bytes of current command */
  489. int msgi_len;
  490. /* number of received message bytes */
  491. unsigned char msgi[256];
  492. /* received message bytes */
  493. int msgo_i, msgo_len;
  494. /* number of sent bytes and length of current messages */
  495. unsigned char msgo[256];
  496. /* pending messages */
  497. int data_len;
  498. /* number of sent/received bytes in dataphase */
  499. unsigned long io_port0;
  500. unsigned long io_port1;
  501. #ifdef __ISAPNP__
  502. struct pnp_dev *pnpdev;
  503. #endif
  504. struct list_head host_list;
  505. };
  506. /*
  507. * host specific command extension
  508. *
  509. */
  510. struct aha152x_scdata {
  511. Scsi_Cmnd *next; /* next sc in queue */
  512. struct completion *done;/* semaphore to block on */
  513. struct scsi_eh_save ses;
  514. };
  515. /* access macros for hostdata */
  516. #define HOSTDATA(shpnt) ((struct aha152x_hostdata *) &shpnt->hostdata)
  517. #define HOSTNO ((shpnt)->host_no)
  518. #define CURRENT_SC (HOSTDATA(shpnt)->current_SC)
  519. #define DONE_SC (HOSTDATA(shpnt)->done_SC)
  520. #define ISSUE_SC (HOSTDATA(shpnt)->issue_SC)
  521. #define DISCONNECTED_SC (HOSTDATA(shpnt)->disconnected_SC)
  522. #define QLOCK (HOSTDATA(shpnt)->lock)
  523. #define QLOCKER (HOSTDATA(shpnt)->locker)
  524. #define QLOCKERL (HOSTDATA(shpnt)->lockerl)
  525. #define STATE (HOSTDATA(shpnt)->state)
  526. #define PREVSTATE (HOSTDATA(shpnt)->prevstate)
  527. #define LASTSTATE (HOSTDATA(shpnt)->laststate)
  528. #define RECONN_TARGET (HOSTDATA(shpnt)->target)
  529. #define CMD_I (HOSTDATA(shpnt)->cmd_i)
  530. #define MSGO(i) (HOSTDATA(shpnt)->msgo[i])
  531. #define MSGO_I (HOSTDATA(shpnt)->msgo_i)
  532. #define MSGOLEN (HOSTDATA(shpnt)->msgo_len)
  533. #define ADDMSGO(x) (MSGOLEN<256 ? (void)(MSGO(MSGOLEN++)=x) : aha152x_error(shpnt,"MSGO overflow"))
  534. #define MSGI(i) (HOSTDATA(shpnt)->msgi[i])
  535. #define MSGILEN (HOSTDATA(shpnt)->msgi_len)
  536. #define ADDMSGI(x) (MSGILEN<256 ? (void)(MSGI(MSGILEN++)=x) : aha152x_error(shpnt,"MSGI overflow"))
  537. #define DATA_LEN (HOSTDATA(shpnt)->data_len)
  538. #define SYNCRATE (HOSTDATA(shpnt)->syncrate[CURRENT_SC->device->id])
  539. #define SYNCNEG (HOSTDATA(shpnt)->syncneg[CURRENT_SC->device->id])
  540. #define DELAY (HOSTDATA(shpnt)->delay)
  541. #define EXT_TRANS (HOSTDATA(shpnt)->ext_trans)
  542. #define TC1550 (HOSTDATA(shpnt)->tc1550)
  543. #define RECONNECT (HOSTDATA(shpnt)->reconnect)
  544. #define PARITY (HOSTDATA(shpnt)->parity)
  545. #define SYNCHRONOUS (HOSTDATA(shpnt)->synchronous)
  546. #define HOSTIOPORT0 (HOSTDATA(shpnt)->io_port0)
  547. #define HOSTIOPORT1 (HOSTDATA(shpnt)->io_port1)
  548. #define SCDATA(SCpnt) ((struct aha152x_scdata *) (SCpnt)->host_scribble)
  549. #define SCNEXT(SCpnt) SCDATA(SCpnt)->next
  550. #define SCSEM(SCpnt) SCDATA(SCpnt)->done
  551. #define SG_ADDRESS(buffer) ((char *) sg_virt((buffer)))
  552. /* state handling */
  553. static void seldi_run(struct Scsi_Host *shpnt);
  554. static void seldo_run(struct Scsi_Host *shpnt);
  555. static void selto_run(struct Scsi_Host *shpnt);
  556. static void busfree_run(struct Scsi_Host *shpnt);
  557. static void msgo_init(struct Scsi_Host *shpnt);
  558. static void msgo_run(struct Scsi_Host *shpnt);
  559. static void msgo_end(struct Scsi_Host *shpnt);
  560. static void cmd_init(struct Scsi_Host *shpnt);
  561. static void cmd_run(struct Scsi_Host *shpnt);
  562. static void cmd_end(struct Scsi_Host *shpnt);
  563. static void datai_init(struct Scsi_Host *shpnt);
  564. static void datai_run(struct Scsi_Host *shpnt);
  565. static void datai_end(struct Scsi_Host *shpnt);
  566. static void datao_init(struct Scsi_Host *shpnt);
  567. static void datao_run(struct Scsi_Host *shpnt);
  568. static void datao_end(struct Scsi_Host *shpnt);
  569. static void status_run(struct Scsi_Host *shpnt);
  570. static void msgi_run(struct Scsi_Host *shpnt);
  571. static void msgi_end(struct Scsi_Host *shpnt);
  572. static void parerr_run(struct Scsi_Host *shpnt);
  573. static void rsti_run(struct Scsi_Host *shpnt);
  574. static void is_complete(struct Scsi_Host *shpnt);
  575. /*
  576. * driver states
  577. *
  578. */
  579. static struct {
  580. char *name;
  581. void (*init)(struct Scsi_Host *);
  582. void (*run)(struct Scsi_Host *);
  583. void (*end)(struct Scsi_Host *);
  584. int spio;
  585. } states[] = {
  586. { "idle", NULL, NULL, NULL, 0},
  587. { "unknown", NULL, NULL, NULL, 0},
  588. { "seldo", NULL, seldo_run, NULL, 0},
  589. { "seldi", NULL, seldi_run, NULL, 0},
  590. { "selto", NULL, selto_run, NULL, 0},
  591. { "busfree", NULL, busfree_run, NULL, 0},
  592. { "msgo", msgo_init, msgo_run, msgo_end, 1},
  593. { "cmd", cmd_init, cmd_run, cmd_end, 1},
  594. { "msgi", NULL, msgi_run, msgi_end, 1},
  595. { "status", NULL, status_run, NULL, 1},
  596. { "datai", datai_init, datai_run, datai_end, 0},
  597. { "datao", datao_init, datao_run, datao_end, 0},
  598. { "parerr", NULL, parerr_run, NULL, 0},
  599. { "rsti", NULL, rsti_run, NULL, 0},
  600. };
  601. /* setup & interrupt */
  602. static irqreturn_t intr(int irq, void *dev_id);
  603. static void reset_ports(struct Scsi_Host *shpnt);
  604. static void aha152x_error(struct Scsi_Host *shpnt, char *msg);
  605. static void done(struct Scsi_Host *shpnt, int error);
  606. /* diagnostics */
  607. static void disp_ports(struct Scsi_Host *shpnt);
  608. static void show_command(Scsi_Cmnd * ptr);
  609. static void show_queues(struct Scsi_Host *shpnt);
  610. static void disp_enintr(struct Scsi_Host *shpnt);
  611. /*
  612. * queue services:
  613. *
  614. */
  615. static inline void append_SC(Scsi_Cmnd **SC, Scsi_Cmnd *new_SC)
  616. {
  617. Scsi_Cmnd *end;
  618. SCNEXT(new_SC) = NULL;
  619. if (!*SC)
  620. *SC = new_SC;
  621. else {
  622. for (end = *SC; SCNEXT(end); end = SCNEXT(end))
  623. ;
  624. SCNEXT(end) = new_SC;
  625. }
  626. }
  627. static inline Scsi_Cmnd *remove_first_SC(Scsi_Cmnd ** SC)
  628. {
  629. Scsi_Cmnd *ptr;
  630. ptr = *SC;
  631. if (ptr) {
  632. *SC = SCNEXT(*SC);
  633. SCNEXT(ptr)=NULL;
  634. }
  635. return ptr;
  636. }
  637. static inline Scsi_Cmnd *remove_lun_SC(Scsi_Cmnd ** SC, int target, int lun)
  638. {
  639. Scsi_Cmnd *ptr, *prev;
  640. for (ptr = *SC, prev = NULL;
  641. ptr && ((ptr->device->id != target) || (ptr->device->lun != lun));
  642. prev = ptr, ptr = SCNEXT(ptr))
  643. ;
  644. if (ptr) {
  645. if (prev)
  646. SCNEXT(prev) = SCNEXT(ptr);
  647. else
  648. *SC = SCNEXT(ptr);
  649. SCNEXT(ptr)=NULL;
  650. }
  651. return ptr;
  652. }
  653. static inline Scsi_Cmnd *remove_SC(Scsi_Cmnd **SC, Scsi_Cmnd *SCp)
  654. {
  655. Scsi_Cmnd *ptr, *prev;
  656. for (ptr = *SC, prev = NULL;
  657. ptr && SCp!=ptr;
  658. prev = ptr, ptr = SCNEXT(ptr))
  659. ;
  660. if (ptr) {
  661. if (prev)
  662. SCNEXT(prev) = SCNEXT(ptr);
  663. else
  664. *SC = SCNEXT(ptr);
  665. SCNEXT(ptr)=NULL;
  666. }
  667. return ptr;
  668. }
  669. static irqreturn_t swintr(int irqno, void *dev_id)
  670. {
  671. struct Scsi_Host *shpnt = dev_id;
  672. HOSTDATA(shpnt)->swint++;
  673. SETPORT(DMACNTRL0, INTEN);
  674. return IRQ_HANDLED;
  675. }
  676. struct Scsi_Host *aha152x_probe_one(struct aha152x_setup *setup)
  677. {
  678. struct Scsi_Host *shpnt;
  679. shpnt = scsi_host_alloc(&aha152x_driver_template, sizeof(struct aha152x_hostdata));
  680. if (!shpnt) {
  681. printk(KERN_ERR "aha152x: scsi_host_alloc failed\n");
  682. return NULL;
  683. }
  684. memset(HOSTDATA(shpnt), 0, sizeof *HOSTDATA(shpnt));
  685. INIT_LIST_HEAD(&HOSTDATA(shpnt)->host_list);
  686. /* need to have host registered before triggering any interrupt */
  687. list_add_tail(&HOSTDATA(shpnt)->host_list, &aha152x_host_list);
  688. shpnt->io_port = setup->io_port;
  689. shpnt->n_io_port = IO_RANGE;
  690. shpnt->irq = setup->irq;
  691. if (!setup->tc1550) {
  692. HOSTIOPORT0 = setup->io_port;
  693. HOSTIOPORT1 = setup->io_port;
  694. } else {
  695. HOSTIOPORT0 = setup->io_port+0x10;
  696. HOSTIOPORT1 = setup->io_port-0x10;
  697. }
  698. spin_lock_init(&QLOCK);
  699. RECONNECT = setup->reconnect;
  700. SYNCHRONOUS = setup->synchronous;
  701. PARITY = setup->parity;
  702. DELAY = setup->delay;
  703. EXT_TRANS = setup->ext_trans;
  704. #if defined(AHA152X_DEBUG)
  705. HOSTDATA(shpnt)->debug = setup->debug;
  706. #endif
  707. SETPORT(SCSIID, setup->scsiid << 4);
  708. shpnt->this_id = setup->scsiid;
  709. if (setup->reconnect)
  710. shpnt->can_queue = AHA152X_MAXQUEUE;
  711. /* RESET OUT */
  712. printk("aha152x: resetting bus...\n");
  713. SETPORT(SCSISEQ, SCSIRSTO);
  714. mdelay(256);
  715. SETPORT(SCSISEQ, 0);
  716. mdelay(DELAY);
  717. reset_ports(shpnt);
  718. printk(KERN_INFO
  719. "aha152x%d%s: "
  720. "vital data: rev=%x, "
  721. "io=0x%03lx (0x%03lx/0x%03lx), "
  722. "irq=%d, "
  723. "scsiid=%d, "
  724. "reconnect=%s, "
  725. "parity=%s, "
  726. "synchronous=%s, "
  727. "delay=%d, "
  728. "extended translation=%s\n",
  729. shpnt->host_no, setup->tc1550 ? " (tc1550 mode)" : "",
  730. GETPORT(REV) & 0x7,
  731. shpnt->io_port, HOSTIOPORT0, HOSTIOPORT1,
  732. shpnt->irq,
  733. shpnt->this_id,
  734. RECONNECT ? "enabled" : "disabled",
  735. PARITY ? "enabled" : "disabled",
  736. SYNCHRONOUS ? "enabled" : "disabled",
  737. DELAY,
  738. EXT_TRANS ? "enabled" : "disabled");
  739. /* not expecting any interrupts */
  740. SETPORT(SIMODE0, 0);
  741. SETPORT(SIMODE1, 0);
  742. if( request_irq(shpnt->irq, swintr, IRQF_DISABLED|IRQF_SHARED, "aha152x", shpnt) ) {
  743. printk(KERN_ERR "aha152x%d: irq %d busy.\n", shpnt->host_no, shpnt->irq);
  744. goto out_host_put;
  745. }
  746. HOSTDATA(shpnt)->swint = 0;
  747. printk(KERN_INFO "aha152x%d: trying software interrupt, ", shpnt->host_no);
  748. mb();
  749. SETPORT(DMACNTRL0, SWINT|INTEN);
  750. mdelay(1000);
  751. free_irq(shpnt->irq, shpnt);
  752. if (!HOSTDATA(shpnt)->swint) {
  753. if (TESTHI(DMASTAT, INTSTAT)) {
  754. printk("lost.\n");
  755. } else {
  756. printk("failed.\n");
  757. }
  758. SETPORT(DMACNTRL0, INTEN);
  759. printk(KERN_ERR "aha152x%d: irq %d possibly wrong. "
  760. "Please verify.\n", shpnt->host_no, shpnt->irq);
  761. goto out_host_put;
  762. }
  763. printk("ok.\n");
  764. /* clear interrupts */
  765. SETPORT(SSTAT0, 0x7f);
  766. SETPORT(SSTAT1, 0xef);
  767. if ( request_irq(shpnt->irq, intr, IRQF_DISABLED|IRQF_SHARED, "aha152x", shpnt) ) {
  768. printk(KERN_ERR "aha152x%d: failed to reassign irq %d.\n", shpnt->host_no, shpnt->irq);
  769. goto out_host_put;
  770. }
  771. if( scsi_add_host(shpnt, NULL) ) {
  772. free_irq(shpnt->irq, shpnt);
  773. printk(KERN_ERR "aha152x%d: failed to add host.\n", shpnt->host_no);
  774. goto out_host_put;
  775. }
  776. scsi_scan_host(shpnt);
  777. return shpnt;
  778. out_host_put:
  779. list_del(&HOSTDATA(shpnt)->host_list);
  780. scsi_host_put(shpnt);
  781. return NULL;
  782. }
  783. void aha152x_release(struct Scsi_Host *shpnt)
  784. {
  785. if (!shpnt)
  786. return;
  787. scsi_remove_host(shpnt);
  788. if (shpnt->irq)
  789. free_irq(shpnt->irq, shpnt);
  790. #if !defined(PCMCIA)
  791. if (shpnt->io_port)
  792. release_region(shpnt->io_port, IO_RANGE);
  793. #endif
  794. #ifdef __ISAPNP__
  795. if (HOSTDATA(shpnt)->pnpdev)
  796. pnp_device_detach(HOSTDATA(shpnt)->pnpdev);
  797. #endif
  798. list_del(&HOSTDATA(shpnt)->host_list);
  799. scsi_host_put(shpnt);
  800. }
  801. /*
  802. * setup controller to generate interrupts depending
  803. * on current state (lock has to be acquired)
  804. *
  805. */
  806. static int setup_expected_interrupts(struct Scsi_Host *shpnt)
  807. {
  808. if(CURRENT_SC) {
  809. CURRENT_SC->SCp.phase |= 1 << 16;
  810. if(CURRENT_SC->SCp.phase & selecting) {
  811. DPRINTK(debug_intr, DEBUG_LEAD "expecting: (seldo) (seltimo) (seldi)\n", CMDINFO(CURRENT_SC));
  812. SETPORT(SSTAT1, SELTO);
  813. SETPORT(SIMODE0, ENSELDO | (DISCONNECTED_SC ? ENSELDI : 0));
  814. SETPORT(SIMODE1, ENSELTIMO);
  815. } else {
  816. DPRINTK(debug_intr, DEBUG_LEAD "expecting: (phase change) (busfree) %s\n", CMDINFO(CURRENT_SC), CURRENT_SC->SCp.phase & spiordy ? "(spiordy)" : "");
  817. SETPORT(SIMODE0, (CURRENT_SC->SCp.phase & spiordy) ? ENSPIORDY : 0);
  818. SETPORT(SIMODE1, ENPHASEMIS | ENSCSIRST | ENSCSIPERR | ENBUSFREE);
  819. }
  820. } else if(STATE==seldi) {
  821. DPRINTK(debug_intr, DEBUG_LEAD "expecting: (phase change) (identify)\n", CMDINFO(CURRENT_SC));
  822. SETPORT(SIMODE0, 0);
  823. SETPORT(SIMODE1, ENPHASEMIS | ENSCSIRST | ENSCSIPERR | ENBUSFREE);
  824. } else {
  825. DPRINTK(debug_intr, DEBUG_LEAD "expecting: %s %s\n",
  826. CMDINFO(CURRENT_SC),
  827. DISCONNECTED_SC ? "(reselection)" : "",
  828. ISSUE_SC ? "(busfree)" : "");
  829. SETPORT(SIMODE0, DISCONNECTED_SC ? ENSELDI : 0);
  830. SETPORT(SIMODE1, ENSCSIRST | ( (ISSUE_SC||DONE_SC) ? ENBUSFREE : 0));
  831. }
  832. if(!HOSTDATA(shpnt)->in_intr)
  833. SETBITS(DMACNTRL0, INTEN);
  834. return TESTHI(DMASTAT, INTSTAT);
  835. }
  836. /*
  837. * Queue a command and setup interrupts for a free bus.
  838. */
  839. static int aha152x_internal_queue(Scsi_Cmnd *SCpnt, struct completion *complete,
  840. int phase, void (*done)(Scsi_Cmnd *))
  841. {
  842. struct Scsi_Host *shpnt = SCpnt->device->host;
  843. unsigned long flags;
  844. #if defined(AHA152X_DEBUG)
  845. if (HOSTDATA(shpnt)->debug & debug_queue) {
  846. printk(INFO_LEAD "queue: %p; cmd_len=%d pieces=%d size=%u cmnd=",
  847. CMDINFO(SCpnt), SCpnt, SCpnt->cmd_len,
  848. scsi_sg_count(SCpnt), scsi_bufflen(SCpnt));
  849. __scsi_print_command(SCpnt->cmnd);
  850. }
  851. #endif
  852. SCpnt->scsi_done = done;
  853. SCpnt->SCp.phase = not_issued | phase;
  854. SCpnt->SCp.Status = 0x1; /* Ilegal status by SCSI standard */
  855. SCpnt->SCp.Message = 0;
  856. SCpnt->SCp.have_data_in = 0;
  857. SCpnt->SCp.sent_command = 0;
  858. if(SCpnt->SCp.phase & (resetting|check_condition)) {
  859. if (!SCpnt->host_scribble || SCSEM(SCpnt) || SCNEXT(SCpnt)) {
  860. printk(ERR_LEAD "cannot reuse command\n", CMDINFO(SCpnt));
  861. return FAILED;
  862. }
  863. } else {
  864. SCpnt->host_scribble = kmalloc(sizeof(struct aha152x_scdata), GFP_ATOMIC);
  865. if(!SCpnt->host_scribble) {
  866. printk(ERR_LEAD "allocation failed\n", CMDINFO(SCpnt));
  867. return FAILED;
  868. }
  869. }
  870. SCNEXT(SCpnt) = NULL;
  871. SCSEM(SCpnt) = complete;
  872. /* setup scratch area
  873. SCp.ptr : buffer pointer
  874. SCp.this_residual : buffer length
  875. SCp.buffer : next buffer
  876. SCp.buffers_residual : left buffers in list
  877. SCp.phase : current state of the command */
  878. if ((phase & resetting) || !scsi_sglist(SCpnt)) {
  879. SCpnt->SCp.ptr = NULL;
  880. SCpnt->SCp.this_residual = 0;
  881. scsi_set_resid(SCpnt, 0);
  882. SCpnt->SCp.buffer = NULL;
  883. SCpnt->SCp.buffers_residual = 0;
  884. } else {
  885. scsi_set_resid(SCpnt, scsi_bufflen(SCpnt));
  886. SCpnt->SCp.buffer = scsi_sglist(SCpnt);
  887. SCpnt->SCp.ptr = SG_ADDRESS(SCpnt->SCp.buffer);
  888. SCpnt->SCp.this_residual = SCpnt->SCp.buffer->length;
  889. SCpnt->SCp.buffers_residual = scsi_sg_count(SCpnt) - 1;
  890. }
  891. DO_LOCK(flags);
  892. #if defined(AHA152X_STAT)
  893. HOSTDATA(shpnt)->total_commands++;
  894. #endif
  895. /* Turn led on, when this is the first command. */
  896. HOSTDATA(shpnt)->commands++;
  897. if (HOSTDATA(shpnt)->commands==1)
  898. SETPORT(PORTA, 1);
  899. append_SC(&ISSUE_SC, SCpnt);
  900. if(!HOSTDATA(shpnt)->in_intr)
  901. setup_expected_interrupts(shpnt);
  902. DO_UNLOCK(flags);
  903. return 0;
  904. }
  905. /*
  906. * queue a command
  907. *
  908. */
  909. static int aha152x_queue_lck(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
  910. {
  911. #if 0
  912. if(*SCpnt->cmnd == REQUEST_SENSE) {
  913. SCpnt->result = 0;
  914. done(SCpnt);
  915. return 0;
  916. }
  917. #endif
  918. return aha152x_internal_queue(SCpnt, NULL, 0, done);
  919. }
  920. static DEF_SCSI_QCMD(aha152x_queue)
  921. /*
  922. *
  923. *
  924. */
  925. static void reset_done(Scsi_Cmnd *SCpnt)
  926. {
  927. #if 0
  928. struct Scsi_Host *shpnt = SCpnt->host;
  929. DPRINTK(debug_eh, INFO_LEAD "reset_done called\n", CMDINFO(SCpnt));
  930. #endif
  931. if(SCSEM(SCpnt)) {
  932. complete(SCSEM(SCpnt));
  933. } else {
  934. printk(KERN_ERR "aha152x: reset_done w/o completion\n");
  935. }
  936. }
  937. /*
  938. * Abort a command
  939. *
  940. */
  941. static int aha152x_abort(Scsi_Cmnd *SCpnt)
  942. {
  943. struct Scsi_Host *shpnt = SCpnt->device->host;
  944. Scsi_Cmnd *ptr;
  945. unsigned long flags;
  946. #if defined(AHA152X_DEBUG)
  947. if(HOSTDATA(shpnt)->debug & debug_eh) {
  948. printk(DEBUG_LEAD "abort(%p)", CMDINFO(SCpnt), SCpnt);
  949. show_queues(shpnt);
  950. }
  951. #endif
  952. DO_LOCK(flags);
  953. ptr=remove_SC(&ISSUE_SC, SCpnt);
  954. if(ptr) {
  955. DPRINTK(debug_eh, DEBUG_LEAD "not yet issued - SUCCESS\n", CMDINFO(SCpnt));
  956. HOSTDATA(shpnt)->commands--;
  957. if (!HOSTDATA(shpnt)->commands)
  958. SETPORT(PORTA, 0);
  959. DO_UNLOCK(flags);
  960. kfree(SCpnt->host_scribble);
  961. SCpnt->host_scribble=NULL;
  962. return SUCCESS;
  963. }
  964. DO_UNLOCK(flags);
  965. /*
  966. * FIXME:
  967. * for current command: queue ABORT for message out and raise ATN
  968. * for disconnected command: pseudo SC with ABORT message or ABORT on reselection?
  969. *
  970. */
  971. printk(ERR_LEAD "cannot abort running or disconnected command\n", CMDINFO(SCpnt));
  972. return FAILED;
  973. }
  974. /*
  975. * Reset a device
  976. *
  977. */
  978. static int aha152x_device_reset(Scsi_Cmnd * SCpnt)
  979. {
  980. struct Scsi_Host *shpnt = SCpnt->device->host;
  981. DECLARE_COMPLETION(done);
  982. int ret, issued, disconnected;
  983. unsigned char old_cmd_len = SCpnt->cmd_len;
  984. unsigned long flags;
  985. unsigned long timeleft;
  986. #if defined(AHA152X_DEBUG)
  987. if(HOSTDATA(shpnt)->debug & debug_eh) {
  988. printk(INFO_LEAD "aha152x_device_reset(%p)", CMDINFO(SCpnt), SCpnt);
  989. show_queues(shpnt);
  990. }
  991. #endif
  992. if(CURRENT_SC==SCpnt) {
  993. printk(ERR_LEAD "cannot reset current device\n", CMDINFO(SCpnt));
  994. return FAILED;
  995. }
  996. DO_LOCK(flags);
  997. issued = remove_SC(&ISSUE_SC, SCpnt) == NULL;
  998. disconnected = issued && remove_SC(&DISCONNECTED_SC, SCpnt);
  999. DO_UNLOCK(flags);
  1000. SCpnt->cmd_len = 0;
  1001. aha152x_internal_queue(SCpnt, &done, resetting, reset_done);
  1002. timeleft = wait_for_completion_timeout(&done, 100*HZ);
  1003. if (!timeleft) {
  1004. /* remove command from issue queue */
  1005. DO_LOCK(flags);
  1006. remove_SC(&ISSUE_SC, SCpnt);
  1007. DO_UNLOCK(flags);
  1008. }
  1009. SCpnt->cmd_len = old_cmd_len;
  1010. DO_LOCK(flags);
  1011. if(SCpnt->SCp.phase & resetted) {
  1012. HOSTDATA(shpnt)->commands--;
  1013. if (!HOSTDATA(shpnt)->commands)
  1014. SETPORT(PORTA, 0);
  1015. kfree(SCpnt->host_scribble);
  1016. SCpnt->host_scribble=NULL;
  1017. ret = SUCCESS;
  1018. } else {
  1019. /* requeue */
  1020. if(!issued) {
  1021. append_SC(&ISSUE_SC, SCpnt);
  1022. } else if(disconnected) {
  1023. append_SC(&DISCONNECTED_SC, SCpnt);
  1024. }
  1025. ret = FAILED;
  1026. }
  1027. DO_UNLOCK(flags);
  1028. return ret;
  1029. }
  1030. static void free_hard_reset_SCs(struct Scsi_Host *shpnt, Scsi_Cmnd **SCs)
  1031. {
  1032. Scsi_Cmnd *ptr;
  1033. ptr=*SCs;
  1034. while(ptr) {
  1035. Scsi_Cmnd *next;
  1036. if(SCDATA(ptr)) {
  1037. next = SCNEXT(ptr);
  1038. } else {
  1039. printk(DEBUG_LEAD "queue corrupted at %p\n", CMDINFO(ptr), ptr);
  1040. next = NULL;
  1041. }
  1042. if (!ptr->device->soft_reset) {
  1043. DPRINTK(debug_eh, DEBUG_LEAD "disconnected command %p removed\n", CMDINFO(ptr), ptr);
  1044. remove_SC(SCs, ptr);
  1045. HOSTDATA(shpnt)->commands--;
  1046. kfree(ptr->host_scribble);
  1047. ptr->host_scribble=NULL;
  1048. }
  1049. ptr = next;
  1050. }
  1051. }
  1052. /*
  1053. * Reset the bus
  1054. *
  1055. */
  1056. static int aha152x_bus_reset_host(struct Scsi_Host *shpnt)
  1057. {
  1058. unsigned long flags;
  1059. DO_LOCK(flags);
  1060. #if defined(AHA152X_DEBUG)
  1061. if(HOSTDATA(shpnt)->debug & debug_eh) {
  1062. printk(KERN_DEBUG "scsi%d: bus reset", shpnt->host_no);
  1063. show_queues(shpnt);
  1064. }
  1065. #endif
  1066. free_hard_reset_SCs(shpnt, &ISSUE_SC);
  1067. free_hard_reset_SCs(shpnt, &DISCONNECTED_SC);
  1068. DPRINTK(debug_eh, KERN_DEBUG "scsi%d: resetting bus\n", shpnt->host_no);
  1069. SETPORT(SCSISEQ, SCSIRSTO);
  1070. mdelay(256);
  1071. SETPORT(SCSISEQ, 0);
  1072. mdelay(DELAY);
  1073. DPRINTK(debug_eh, KERN_DEBUG "scsi%d: bus resetted\n", shpnt->host_no);
  1074. setup_expected_interrupts(shpnt);
  1075. if(HOSTDATA(shpnt)->commands==0)
  1076. SETPORT(PORTA, 0);
  1077. DO_UNLOCK(flags);
  1078. return SUCCESS;
  1079. }
  1080. /*
  1081. * Reset the bus
  1082. *
  1083. */
  1084. static int aha152x_bus_reset(Scsi_Cmnd *SCpnt)
  1085. {
  1086. return aha152x_bus_reset_host(SCpnt->device->host);
  1087. }
  1088. /*
  1089. * Restore default values to the AIC-6260 registers and reset the fifos
  1090. *
  1091. */
  1092. static void reset_ports(struct Scsi_Host *shpnt)
  1093. {
  1094. unsigned long flags;
  1095. /* disable interrupts */
  1096. SETPORT(DMACNTRL0, RSTFIFO);
  1097. SETPORT(SCSISEQ, 0);
  1098. SETPORT(SXFRCTL1, 0);
  1099. SETPORT(SCSISIG, 0);
  1100. SETRATE(0);
  1101. /* clear all interrupt conditions */
  1102. SETPORT(SSTAT0, 0x7f);
  1103. SETPORT(SSTAT1, 0xef);
  1104. SETPORT(SSTAT4, SYNCERR | FWERR | FRERR);
  1105. SETPORT(DMACNTRL0, 0);
  1106. SETPORT(DMACNTRL1, 0);
  1107. SETPORT(BRSTCNTRL, 0xf1);
  1108. /* clear SCSI fifos and transfer count */
  1109. SETPORT(SXFRCTL0, CH1|CLRCH1|CLRSTCNT);
  1110. SETPORT(SXFRCTL0, CH1);
  1111. DO_LOCK(flags);
  1112. setup_expected_interrupts(shpnt);
  1113. DO_UNLOCK(flags);
  1114. }
  1115. /*
  1116. * Reset the host (bus and controller)
  1117. *
  1118. */
  1119. int aha152x_host_reset_host(struct Scsi_Host *shpnt)
  1120. {
  1121. DPRINTK(debug_eh, KERN_DEBUG "scsi%d: host reset\n", shpnt->host_no);
  1122. aha152x_bus_reset_host(shpnt);
  1123. DPRINTK(debug_eh, KERN_DEBUG "scsi%d: resetting ports\n", shpnt->host_no);
  1124. reset_ports(shpnt);
  1125. return SUCCESS;
  1126. }
  1127. /*
  1128. * Reset the host (bus and controller)
  1129. *
  1130. */
  1131. static int aha152x_host_reset(Scsi_Cmnd *SCpnt)
  1132. {
  1133. return aha152x_host_reset_host(SCpnt->device->host);
  1134. }
  1135. /*
  1136. * Return the "logical geometry"
  1137. *
  1138. */
  1139. static int aha152x_biosparam(struct scsi_device *sdev, struct block_device *bdev,
  1140. sector_t capacity, int *info_array)
  1141. {
  1142. struct Scsi_Host *shpnt = sdev->host;
  1143. /* try default translation */
  1144. info_array[0] = 64;
  1145. info_array[1] = 32;
  1146. info_array[2] = (unsigned long)capacity / (64 * 32);
  1147. /* for disks >1GB do some guessing */
  1148. if (info_array[2] >= 1024) {
  1149. int info[3];
  1150. /* try to figure out the geometry from the partition table */
  1151. if (scsicam_bios_param(bdev, capacity, info) < 0 ||
  1152. !((info[0] == 64 && info[1] == 32) || (info[0] == 255 && info[1] == 63))) {
  1153. if (EXT_TRANS) {
  1154. printk(KERN_NOTICE
  1155. "aha152x: unable to verify geometry for disk with >1GB.\n"
  1156. " using extended translation.\n");
  1157. info_array[0] = 255;
  1158. info_array[1] = 63;
  1159. info_array[2] = (unsigned long)capacity / (255 * 63);
  1160. } else {
  1161. printk(KERN_NOTICE
  1162. "aha152x: unable to verify geometry for disk with >1GB.\n"
  1163. " Using default translation. Please verify yourself.\n"
  1164. " Perhaps you need to enable extended translation in the driver.\n"
  1165. " See Documentation/scsi/aha152x.txt for details.\n");
  1166. }
  1167. } else {
  1168. info_array[0] = info[0];
  1169. info_array[1] = info[1];
  1170. info_array[2] = info[2];
  1171. if (info[0] == 255 && !EXT_TRANS) {
  1172. printk(KERN_NOTICE
  1173. "aha152x: current partition table is using extended translation.\n"
  1174. " using it also, although it's not explicitly enabled.\n");
  1175. }
  1176. }
  1177. }
  1178. return 0;
  1179. }
  1180. /*
  1181. * Internal done function
  1182. *
  1183. */
  1184. static void done(struct Scsi_Host *shpnt, int error)
  1185. {
  1186. if (CURRENT_SC) {
  1187. if(DONE_SC)
  1188. printk(ERR_LEAD "there's already a completed command %p - will cause abort\n", CMDINFO(CURRENT_SC), DONE_SC);
  1189. DONE_SC = CURRENT_SC;
  1190. CURRENT_SC = NULL;
  1191. DONE_SC->result = error;
  1192. } else
  1193. printk(KERN_ERR "aha152x: done() called outside of command\n");
  1194. }
  1195. static struct work_struct aha152x_tq;
  1196. /*
  1197. * Run service completions on the card with interrupts enabled.
  1198. *
  1199. */
  1200. static void run(struct work_struct *work)
  1201. {
  1202. struct aha152x_hostdata *hd;
  1203. list_for_each_entry(hd, &aha152x_host_list, host_list) {
  1204. struct Scsi_Host *shost = container_of((void *)hd, struct Scsi_Host, hostdata);
  1205. is_complete(shost);
  1206. }
  1207. }
  1208. /*
  1209. * Interrupt handler
  1210. *
  1211. */
  1212. static irqreturn_t intr(int irqno, void *dev_id)
  1213. {
  1214. struct Scsi_Host *shpnt = dev_id;
  1215. unsigned long flags;
  1216. unsigned char rev, dmacntrl0;
  1217. /*
  1218. * Read a couple of registers that are known to not be all 1's. If
  1219. * we read all 1's (-1), that means that either:
  1220. *
  1221. * a. The host adapter chip has gone bad, and we cannot control it,
  1222. * OR
  1223. * b. The host adapter is a PCMCIA card that has been ejected
  1224. *
  1225. * In either case, we cannot do anything with the host adapter at
  1226. * this point in time. So just ignore the interrupt and return.
  1227. * In the latter case, the interrupt might actually be meant for
  1228. * someone else sharing this IRQ, and that driver will handle it.
  1229. */
  1230. rev = GETPORT(REV);
  1231. dmacntrl0 = GETPORT(DMACNTRL0);
  1232. if ((rev == 0xFF) && (dmacntrl0 == 0xFF))
  1233. return IRQ_NONE;
  1234. if( TESTLO(DMASTAT, INTSTAT) )
  1235. return IRQ_NONE;
  1236. /* no more interrupts from the controller, while we're busy.
  1237. INTEN is restored by the BH handler */
  1238. CLRBITS(DMACNTRL0, INTEN);
  1239. DO_LOCK(flags);
  1240. if( HOSTDATA(shpnt)->service==0 ) {
  1241. HOSTDATA(shpnt)->service=1;
  1242. /* Poke the BH handler */
  1243. INIT_WORK(&aha152x_tq, run);
  1244. schedule_work(&aha152x_tq);
  1245. }
  1246. DO_UNLOCK(flags);
  1247. return IRQ_HANDLED;
  1248. }
  1249. /*
  1250. * busfree phase
  1251. * - handle completition/disconnection/error of current command
  1252. * - start selection for next command (if any)
  1253. */
  1254. static void busfree_run(struct Scsi_Host *shpnt)
  1255. {
  1256. unsigned long flags;
  1257. #if defined(AHA152X_STAT)
  1258. int action=0;
  1259. #endif
  1260. SETPORT(SXFRCTL0, CH1|CLRCH1|CLRSTCNT);
  1261. SETPORT(SXFRCTL0, CH1);
  1262. SETPORT(SSTAT1, CLRBUSFREE);
  1263. if(CURRENT_SC) {
  1264. #if defined(AHA152X_STAT)
  1265. action++;
  1266. #endif
  1267. CURRENT_SC->SCp.phase &= ~syncneg;
  1268. if(CURRENT_SC->SCp.phase & completed) {
  1269. /* target sent COMMAND COMPLETE */
  1270. done(shpnt, (CURRENT_SC->SCp.Status & 0xff) | ((CURRENT_SC->SCp.Message & 0xff) << 8) | (DID_OK << 16));
  1271. } else if(CURRENT_SC->SCp.phase & aborted) {
  1272. DPRINTK(debug_eh, DEBUG_LEAD "ABORT sent\n", CMDINFO(CURRENT_SC));
  1273. done(shpnt, (CURRENT_SC->SCp.Status & 0xff) | ((CURRENT_SC->SCp.Message & 0xff) << 8) | (DID_ABORT << 16));
  1274. } else if(CURRENT_SC->SCp.phase & resetted) {
  1275. DPRINTK(debug_eh, DEBUG_LEAD "BUS DEVICE RESET sent\n", CMDINFO(CURRENT_SC));
  1276. done(shpnt, (CURRENT_SC->SCp.Status & 0xff) | ((CURRENT_SC->SCp.Message & 0xff) << 8) | (DID_RESET << 16));
  1277. } else if(CURRENT_SC->SCp.phase & disconnected) {
  1278. /* target sent DISCONNECT */
  1279. DPRINTK(debug_selection, DEBUG_LEAD "target disconnected at %d/%d\n",
  1280. CMDINFO(CURRENT_SC),
  1281. scsi_get_resid(CURRENT_SC),
  1282. scsi_bufflen(CURRENT_SC));
  1283. #if defined(AHA152X_STAT)
  1284. HOSTDATA(shpnt)->disconnections++;
  1285. #endif
  1286. append_SC(&DISCONNECTED_SC, CURRENT_SC);
  1287. CURRENT_SC->SCp.phase |= 1 << 16;
  1288. CURRENT_SC = NULL;
  1289. } else {
  1290. done(shpnt, DID_ERROR << 16);
  1291. }
  1292. #if defined(AHA152X_STAT)
  1293. } else {
  1294. HOSTDATA(shpnt)->busfree_without_old_command++;
  1295. #endif
  1296. }
  1297. DO_LOCK(flags);
  1298. if(DONE_SC) {
  1299. #if defined(AHA152X_STAT)
  1300. action++;
  1301. #endif
  1302. if(DONE_SC->SCp.phase & check_condition) {
  1303. struct scsi_cmnd *cmd = HOSTDATA(shpnt)->done_SC;
  1304. struct aha152x_scdata *sc = SCDATA(cmd);
  1305. #if 0
  1306. if(HOSTDATA(shpnt)->debug & debug_eh) {
  1307. printk(ERR_LEAD "received sense: ", CMDINFO(DONE_SC));
  1308. scsi_print_sense("bh", DONE_SC);
  1309. }
  1310. #endif
  1311. scsi_eh_restore_cmnd(cmd, &sc->ses);
  1312. cmd->SCp.Status = SAM_STAT_CHECK_CONDITION;
  1313. HOSTDATA(shpnt)->commands--;
  1314. if (!HOSTDATA(shpnt)->commands)
  1315. SETPORT(PORTA, 0); /* turn led off */
  1316. } else if(DONE_SC->SCp.Status==SAM_STAT_CHECK_CONDITION) {
  1317. #if defined(AHA152X_STAT)
  1318. HOSTDATA(shpnt)->busfree_with_check_condition++;
  1319. #endif
  1320. #if 0
  1321. DPRINTK(debug_eh, ERR_LEAD "CHECK CONDITION found\n", CMDINFO(DONE_SC));
  1322. #endif
  1323. if(!(DONE_SC->SCp.phase & not_issued)) {
  1324. struct aha152x_scdata *sc;
  1325. Scsi_Cmnd *ptr = DONE_SC;
  1326. DONE_SC=NULL;
  1327. #if 0
  1328. DPRINTK(debug_eh, ERR_LEAD "requesting sense\n", CMDINFO(ptr));
  1329. #endif
  1330. sc = SCDATA(ptr);
  1331. /* It was allocated in aha152x_internal_queue? */
  1332. BUG_ON(!sc);
  1333. scsi_eh_prep_cmnd(ptr, &sc->ses, NULL, 0, ~0);
  1334. DO_UNLOCK(flags);
  1335. aha152x_internal_queue(ptr, NULL, check_condition, ptr->scsi_done);
  1336. DO_LOCK(flags);
  1337. #if 0
  1338. } else {
  1339. DPRINTK(debug_eh, ERR_LEAD "command not issued - CHECK CONDITION ignored\n", CMDINFO(DONE_SC));
  1340. #endif
  1341. }
  1342. }
  1343. if(DONE_SC && DONE_SC->scsi_done) {
  1344. #if defined(AHA152X_DEBUG)
  1345. int hostno=DONE_SC->device->host->host_no;
  1346. int id=DONE_SC->device->id & 0xf;
  1347. int lun=DONE_SC->device->lun & 0x7;
  1348. #endif
  1349. Scsi_Cmnd *ptr = DONE_SC;
  1350. DONE_SC=NULL;
  1351. /* turn led off, when no commands are in the driver */
  1352. HOSTDATA(shpnt)->commands--;
  1353. if (!HOSTDATA(shpnt)->commands)
  1354. SETPORT(PORTA, 0); /* turn led off */
  1355. if(ptr->scsi_done != reset_done) {
  1356. kfree(ptr->host_scribble);
  1357. ptr->host_scribble=NULL;
  1358. }
  1359. DO_UNLOCK(flags);
  1360. DPRINTK(debug_done, DEBUG_LEAD "calling scsi_done(%p)\n", hostno, id, lun, ptr);
  1361. ptr->scsi_done(ptr);
  1362. DPRINTK(debug_done, DEBUG_LEAD "scsi_done(%p) returned\n", hostno, id, lun, ptr);
  1363. DO_LOCK(flags);
  1364. }
  1365. DONE_SC=NULL;
  1366. #if defined(AHA152X_STAT)
  1367. } else {
  1368. HOSTDATA(shpnt)->busfree_without_done_command++;
  1369. #endif
  1370. }
  1371. if(ISSUE_SC)
  1372. CURRENT_SC = remove_first_SC(&ISSUE_SC);
  1373. DO_UNLOCK(flags);
  1374. if(CURRENT_SC) {
  1375. #if defined(AHA152X_STAT)
  1376. action++;
  1377. #endif
  1378. CURRENT_SC->SCp.phase |= selecting;
  1379. DPRINTK(debug_selection, DEBUG_LEAD "selecting target\n", CMDINFO(CURRENT_SC));
  1380. /* clear selection timeout */
  1381. SETPORT(SSTAT1, SELTO);
  1382. SETPORT(SCSIID, (shpnt->this_id << OID_) | CURRENT_SC->device->id);
  1383. SETPORT(SXFRCTL1, (PARITY ? ENSPCHK : 0 ) | ENSTIMER);
  1384. SETPORT(SCSISEQ, ENSELO | ENAUTOATNO | (DISCONNECTED_SC ? ENRESELI : 0));
  1385. } else {
  1386. #if defined(AHA152X_STAT)
  1387. HOSTDATA(shpnt)->busfree_without_new_command++;
  1388. #endif
  1389. SETPORT(SCSISEQ, DISCONNECTED_SC ? ENRESELI : 0);
  1390. }
  1391. #if defined(AHA152X_STAT)
  1392. if(!action)
  1393. HOSTDATA(shpnt)->busfree_without_any_action++;
  1394. #endif
  1395. }
  1396. /*
  1397. * Selection done (OUT)
  1398. * - queue IDENTIFY message and SDTR to selected target for message out
  1399. * (ATN asserted automagically via ENAUTOATNO in busfree())
  1400. */
  1401. static void seldo_run(struct Scsi_Host *shpnt)
  1402. {
  1403. SETPORT(SCSISIG, 0);
  1404. SETPORT(SSTAT1, CLRBUSFREE);
  1405. SETPORT(SSTAT1, CLRPHASECHG);
  1406. CURRENT_SC->SCp.phase &= ~(selecting|not_issued);
  1407. SETPORT(SCSISEQ, 0);
  1408. if (TESTLO(SSTAT0, SELDO)) {
  1409. printk(ERR_LEAD "aha152x: passing bus free condition\n", CMDINFO(CURRENT_SC));
  1410. done(shpnt, DID_NO_CONNECT << 16);
  1411. return;
  1412. }
  1413. SETPORT(SSTAT0, CLRSELDO);
  1414. ADDMSGO(IDENTIFY(RECONNECT, CURRENT_SC->device->lun));
  1415. if (CURRENT_SC->SCp.phase & aborting) {
  1416. ADDMSGO(ABORT);
  1417. } else if (CURRENT_SC->SCp.phase & resetting) {
  1418. ADDMSGO(BUS_DEVICE_RESET);
  1419. } else if (SYNCNEG==0 && SYNCHRONOUS) {
  1420. CURRENT_SC->SCp.phase |= syncneg;
  1421. MSGOLEN += spi_populate_sync_msg(&MSGO(MSGOLEN), 50, 8);
  1422. SYNCNEG=1; /* negotiation in progress */
  1423. }
  1424. SETRATE(SYNCRATE);
  1425. }
  1426. /*
  1427. * Selection timeout
  1428. * - return command to mid-level with failure cause
  1429. *
  1430. */
  1431. static void selto_run(struct Scsi_Host *shpnt)
  1432. {
  1433. SETPORT(SCSISEQ, 0);
  1434. SETPORT(SSTAT1, CLRSELTIMO);
  1435. DPRINTK(debug_selection, DEBUG_LEAD "selection timeout\n", CMDINFO(CURRENT_SC));
  1436. if(!CURRENT_SC) {
  1437. DPRINTK(debug_selection, DEBUG_LEAD "!CURRENT_SC\n", CMDINFO(CURRENT_SC));
  1438. return;
  1439. }
  1440. CURRENT_SC->SCp.phase &= ~selecting;
  1441. if (CURRENT_SC->SCp.phase & aborted) {
  1442. DPRINTK(debug_selection, DEBUG_LEAD "aborted\n", CMDINFO(CURRENT_SC));
  1443. done(shpnt, DID_ABORT << 16);
  1444. } else if (TESTLO(SSTAT0, SELINGO)) {
  1445. DPRINTK(debug_selection, DEBUG_LEAD "arbitration not won\n", CMDINFO(CURRENT_SC));
  1446. done(shpnt, DID_BUS_BUSY << 16);
  1447. } else {
  1448. /* ARBITRATION won, but SELECTION failed */
  1449. DPRINTK(debug_selection, DEBUG_LEAD "selection failed\n", CMDINFO(CURRENT_SC));
  1450. done(shpnt, DID_NO_CONNECT << 16);
  1451. }
  1452. }
  1453. /*
  1454. * Selection in done
  1455. * - put current command back to issue queue
  1456. * (reconnection of a disconnected nexus instead
  1457. * of successful selection out)
  1458. *
  1459. */
  1460. static void seldi_run(struct Scsi_Host *shpnt)
  1461. {
  1462. int selid;
  1463. int target;
  1464. unsigned long flags;
  1465. SETPORT(SCSISIG, 0);
  1466. SETPORT(SSTAT0, CLRSELDI);
  1467. SETPORT(SSTAT1, CLRBUSFREE);
  1468. SETPORT(SSTAT1, CLRPHASECHG);
  1469. if(CURRENT_SC) {
  1470. if(!(CURRENT_SC->SCp.phase & not_issued))
  1471. printk(ERR_LEAD "command should not have been issued yet\n", CMDINFO(CURRENT_SC));
  1472. DPRINTK(debug_selection, ERR_LEAD "command requeued - reselection\n", CMDINFO(CURRENT_SC));
  1473. DO_LOCK(flags);
  1474. append_SC(&ISSUE_SC, CURRENT_SC);
  1475. DO_UNLOCK(flags);
  1476. CURRENT_SC = NULL;
  1477. }
  1478. if(!DISCONNECTED_SC) {
  1479. DPRINTK(debug_selection, DEBUG_LEAD "unexpected SELDI ", CMDINFO(CURRENT_SC));
  1480. return;
  1481. }
  1482. RECONN_TARGET=-1;
  1483. selid = GETPORT(SELID) & ~(1 << shpnt->this_id);
  1484. if (selid==0) {
  1485. printk("aha152x%d: target id unknown (%02x)\n", HOSTNO, selid);
  1486. return;
  1487. }
  1488. for(target=7; !(selid & (1 << target)); target--)
  1489. ;
  1490. if(selid & ~(1 << target)) {
  1491. printk("aha152x%d: multiple targets reconnected (%02x)\n",
  1492. HOSTNO, selid);
  1493. }
  1494. SETPORT(SCSIID, (shpnt->this_id << OID_) | target);
  1495. SETPORT(SCSISEQ, 0);
  1496. SETRATE(HOSTDATA(shpnt)->syncrate[target]);
  1497. RECONN_TARGET=target;
  1498. DPRINTK(debug_selection, DEBUG_LEAD "target %d reselected (%02x).\n", CMDINFO(CURRENT_SC), target, selid);
  1499. }
  1500. /*
  1501. * message in phase
  1502. * - handle initial message after reconnection to identify
  1503. * reconnecting nexus
  1504. * - queue command on DISCONNECTED_SC on DISCONNECT message
  1505. * - set completed flag on COMMAND COMPLETE
  1506. * (other completition code moved to busfree_run)
  1507. * - handle response to SDTR
  1508. * - clear synchronous transfer agreements on BUS RESET
  1509. *
  1510. * FIXME: what about SAVE POINTERS, RESTORE POINTERS?
  1511. *
  1512. */
  1513. static void msgi_run(struct Scsi_Host *shpnt)
  1514. {
  1515. for(;;) {
  1516. int sstat1 = GETPORT(SSTAT1);
  1517. if(sstat1 & (PHASECHG|PHASEMIS|BUSFREE) || !(sstat1 & REQINIT))
  1518. return;
  1519. if(TESTLO(SSTAT0,SPIORDY)) {
  1520. DPRINTK(debug_msgi, DEBUG_LEAD "!SPIORDY\n", CMDINFO(CURRENT_SC));
  1521. return;
  1522. }
  1523. ADDMSGI(GETPORT(SCSIDAT));
  1524. #if defined(AHA152X_DEBUG)
  1525. if (HOSTDATA(shpnt)->debug & debug_msgi) {
  1526. printk(INFO_LEAD "inbound message %02x ", CMDINFO(CURRENT_SC), MSGI(0));
  1527. spi_print_msg(&MSGI(0));
  1528. printk("\n");
  1529. }
  1530. #endif
  1531. if(!CURRENT_SC) {
  1532. if(LASTSTATE!=seldi) {
  1533. printk(KERN_ERR "aha152x%d: message in w/o current command not after reselection\n", HOSTNO);
  1534. }
  1535. /*
  1536. * Handle reselection
  1537. */
  1538. if(!(MSGI(0) & IDENTIFY_BASE)) {
  1539. printk(KERN_ERR "aha152x%d: target didn't identify after reselection\n", HOSTNO);
  1540. continue;
  1541. }
  1542. CURRENT_SC = remove_lun_SC(&DISCONNECTED_SC, RECONN_TARGET, MSGI(0) & 0x3f);
  1543. if (!CURRENT_SC) {
  1544. show_queues(shpnt);
  1545. printk(KERN_ERR "aha152x%d: no disconnected command for target %d/%d\n", HOSTNO, RECONN_TARGET, MSGI(0) & 0x3f);
  1546. continue;
  1547. }
  1548. DPRINTK(debug_msgi, DEBUG_LEAD "target reconnected\n", CMDINFO(CURRENT_SC));
  1549. CURRENT_SC->SCp.Message = MSGI(0);
  1550. CURRENT_SC->SCp.phase &= ~disconnected;
  1551. MSGILEN=0;
  1552. /* next message if any */
  1553. continue;
  1554. }
  1555. CURRENT_SC->SCp.Message = MSGI(0);
  1556. switch (MSGI(0)) {
  1557. case DISCONNECT:
  1558. if (!RECONNECT)
  1559. printk(WARN_LEAD "target was not allowed to disconnect\n", CMDINFO(CURRENT_SC));
  1560. CURRENT_SC->SCp.phase |= disconnected;
  1561. break;
  1562. case COMMAND_COMPLETE:
  1563. if(CURRENT_SC->SCp.phase & completed)
  1564. DPRINTK(debug_msgi, DEBUG_LEAD "again COMMAND COMPLETE\n", CMDINFO(CURRENT_SC));
  1565. CURRENT_SC->SCp.phase |= completed;
  1566. break;
  1567. case MESSAGE_REJECT:
  1568. if