sym53c416.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844
  1. /*
  2. * sym53c416.c
  3. * Low-level SCSI driver for sym53c416 chip.
  4. * Copyright (C) 1998 Lieven Willems (lw_linux@hotmail.com)
  5. *
  6. * Changes :
  7. *
  8. * Marcelo Tosatti <marcelo@conectiva.com.br> : Added io_request_lock locking
  9. * Alan Cox <alan@lxorguk.ukuu.org.uk> : Cleaned up code formatting
  10. * Fixed an irq locking bug
  11. * Added ISAPnP support
  12. * Bjoern A. Zeeb <bzeeb@zabbadoz.net> : Initial irq locking updates
  13. * Added another card with ISAPnP support
  14. *
  15. * LILO command line usage: sym53c416=<PORTBASE>[,<IRQ>]
  16. *
  17. * This program is free software; you can redistribute it and/or modify it
  18. * under the terms of the GNU General Public License as published by the
  19. * Free Software Foundation; either version 2, or (at your option) any
  20. * later version.
  21. *
  22. * This program is distributed in the hope that it will be useful, but
  23. * WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  25. * General Public License for more details.
  26. *
  27. */
  28. #include <linux/module.h>
  29. #include <linux/kernel.h>
  30. #include <linux/types.h>
  31. #include <linux/init.h>
  32. #include <linux/string.h>
  33. #include <linux/ioport.h>
  34. #include <linux/interrupt.h>
  35. #include <linux/delay.h>
  36. #include <linux/proc_fs.h>
  37. #include <linux/spinlock.h>
  38. #include <asm/dma.h>
  39. #include <asm/system.h>
  40. #include <asm/io.h>
  41. #include <linux/blkdev.h>
  42. #include <linux/isapnp.h>
  43. #include "scsi.h"
  44. #include <scsi/scsi_host.h>
  45. #include "sym53c416.h"
  46. #define VERSION_STRING "Version 1.0.0-ac"
  47. #define TC_LOW 0x00 /* Transfer counter low */
  48. #define TC_MID 0x01 /* Transfer counter mid */
  49. #define SCSI_FIFO 0x02 /* SCSI FIFO register */
  50. #define COMMAND_REG 0x03 /* Command Register */
  51. #define STATUS_REG 0x04 /* Status Register (READ) */
  52. #define DEST_BUS_ID 0x04 /* Destination Bus ID (WRITE) */
  53. #define INT_REG 0x05 /* Interrupt Register (READ) */
  54. #define TOM 0x05 /* Time out multiplier (WRITE) */
  55. #define STP 0x06 /* Synchronous Transfer period */
  56. #define SYNC_OFFSET 0x07 /* Synchronous Offset */
  57. #define CONF_REG_1 0x08 /* Configuration register 1 */
  58. #define CONF_REG_2 0x0B /* Configuration register 2 */
  59. #define CONF_REG_3 0x0C /* Configuration register 3 */
  60. #define CONF_REG_4 0x0D /* Configuration register 4 */
  61. #define TC_HIGH 0x0E /* Transfer counter high */
  62. #define PIO_FIFO_1 0x10 /* PIO FIFO register 1 */
  63. #define PIO_FIFO_2 0x11 /* PIO FIFO register 2 */
  64. #define PIO_FIFO_3 0x12 /* PIO FIFO register 3 */
  65. #define PIO_FIFO_4 0x13 /* PIO FIFO register 4 */
  66. #define PIO_FIFO_CNT 0x14 /* PIO FIFO count */
  67. #define PIO_INT_REG 0x15 /* PIO interrupt register */
  68. #define CONF_REG_5 0x16 /* Configuration register 5 */
  69. #define FEATURE_EN 0x1D /* Feature Enable register */
  70. /* Configuration register 1 entries: */
  71. /* Bits 2-0: SCSI ID of host adapter */
  72. #define SCM 0x80 /* Slow Cable Mode */
  73. #define SRID 0x40 /* SCSI Reset Interrupt Disable */
  74. #define PTM 0x20 /* Parity Test Mode */
  75. #define EPC 0x10 /* Enable Parity Checking */
  76. #define CTME 0x08 /* Special Test Mode */
  77. /* Configuration register 2 entries: */
  78. #define FE 0x40 /* Features Enable */
  79. #define SCSI2 0x08 /* SCSI 2 Enable */
  80. #define TBPA 0x04 /* Target Bad Parity Abort */
  81. /* Configuration register 3 entries: */
  82. #define IDMRC 0x80 /* ID Message Reserved Check */
  83. #define QTE 0x40 /* Queue Tag Enable */
  84. #define CDB10 0x20 /* Command Descriptor Block 10 */
  85. #define FSCSI 0x10 /* FastSCSI */
  86. #define FCLK 0x08 /* FastClock */
  87. /* Configuration register 4 entries: */
  88. #define RBS 0x08 /* Register bank select */
  89. #define EAN 0x04 /* Enable Active Negotiation */
  90. /* Configuration register 5 entries: */
  91. #define LPSR 0x80 /* Lower Power SCSI Reset */
  92. #define IE 0x20 /* Interrupt Enable */
  93. #define LPM 0x02 /* Low Power Mode */
  94. #define WSE0 0x01 /* 0WS Enable */
  95. /* Interrupt register entries: */
  96. #define SRST 0x80 /* SCSI Reset */
  97. #define ILCMD 0x40 /* Illegal Command */
  98. #define DIS 0x20 /* Disconnect */
  99. #define BS 0x10 /* Bus Service */
  100. #define FC 0x08 /* Function Complete */
  101. #define RESEL 0x04 /* Reselected */
  102. #define SI 0x03 /* Selection Interrupt */
  103. /* Status Register Entries: */
  104. #define SCI 0x80 /* SCSI Core Int */
  105. #define GE 0x40 /* Gross Error */
  106. #define PE 0x20 /* Parity Error */
  107. #define TC 0x10 /* Terminal Count */
  108. #define VGC 0x08 /* Valid Group Code */
  109. #define PHBITS 0x07 /* Phase bits */
  110. /* PIO Interrupt Register Entries: */
  111. #define SCI 0x80 /* SCSI Core Int */
  112. #define PFI 0x40 /* PIO FIFO Interrupt */
  113. #define FULL 0x20 /* PIO FIFO Full */
  114. #define EMPTY 0x10 /* PIO FIFO Empty */
  115. #define CE 0x08 /* Collision Error */
  116. #define OUE 0x04 /* Overflow / Underflow error */
  117. #define FIE 0x02 /* Full Interrupt Enable */
  118. #define EIE 0x01 /* Empty Interrupt Enable */
  119. /* SYM53C416 SCSI phases (lower 3 bits of SYM53C416_STATUS_REG) */
  120. #define PHASE_DATA_OUT 0x00
  121. #define PHASE_DATA_IN 0x01
  122. #define PHASE_COMMAND 0x02
  123. #define PHASE_STATUS 0x03
  124. #define PHASE_RESERVED_1 0x04
  125. #define PHASE_RESERVED_2 0x05
  126. #define PHASE_MESSAGE_OUT 0x06
  127. #define PHASE_MESSAGE_IN 0x07
  128. /* SYM53C416 core commands */
  129. #define NOOP 0x00
  130. #define FLUSH_FIFO 0x01
  131. #define RESET_CHIP 0x02
  132. #define RESET_SCSI_BUS 0x03
  133. #define DISABLE_SEL_RESEL 0x45
  134. #define RESEL_SEQ 0x40
  135. #define SEL_WITHOUT_ATN_SEQ 0x41
  136. #define SEL_WITH_ATN_SEQ 0x42
  137. #define SEL_WITH_ATN_AND_STOP_SEQ 0x43
  138. #define ENABLE_SEL_RESEL 0x44
  139. #define SEL_WITH_ATN3_SEQ 0x46
  140. #define RESEL3_SEQ 0x47
  141. #define SND_MSG 0x20
  142. #define SND_STAT 0x21
  143. #define SND_DATA 0x22
  144. #define DISCONNECT_SEQ 0x23
  145. #define TERMINATE_SEQ 0x24
  146. #define TARGET_COMM_COMPLETE_SEQ 0x25
  147. #define DISCONN 0x27
  148. #define RECV_MSG_SEQ 0x28
  149. #define RECV_CMD 0x29
  150. #define RECV_DATA 0x2A
  151. #define RECV_CMD_SEQ 0x2B
  152. #define TARGET_ABORT_PIO 0x04
  153. #define TRANSFER_INFORMATION 0x10
  154. #define INIT_COMM_COMPLETE_SEQ 0x11
  155. #define MSG_ACCEPTED 0x12
  156. #define TRANSFER_PAD 0x18
  157. #define SET_ATN 0x1A
  158. #define RESET_ATN 0x1B
  159. #define ILLEGAL 0xFF
  160. #define PIO_MODE 0x80
  161. #define IO_RANGE 0x20 /* 0x00 - 0x1F */
  162. #define ID "sym53c416" /* Attention: copied to the sym53c416.h */
  163. #define PIO_SIZE 128 /* Size of PIO fifo is 128 bytes */
  164. #define READ_TIMEOUT 150
  165. #define WRITE_TIMEOUT 150
  166. #ifdef MODULE
  167. #define sym53c416_base sym53c416
  168. #define sym53c416_base_1 sym53c416_1
  169. #define sym53c416_base_2 sym53c416_2
  170. #define sym53c416_base_3 sym53c416_3
  171. static unsigned int sym53c416_base[2];
  172. static unsigned int sym53c416_base_1[2];
  173. static unsigned int sym53c416_base_2[2];
  174. static unsigned int sym53c416_base_3[2];
  175. #endif
  176. #define MAXHOSTS 4
  177. #define SG_ADDRESS(buffer) ((char *) sg_virt((buffer)))
  178. enum phases
  179. {
  180. idle,
  181. data_out,
  182. data_in,
  183. command_ph,
  184. status_ph,
  185. message_out,
  186. message_in
  187. };
  188. typedef struct
  189. {
  190. int base;
  191. int irq;
  192. int scsi_id;
  193. } host;
  194. static host hosts[MAXHOSTS] = {
  195. {0, 0, SYM53C416_SCSI_ID},
  196. {0, 0, SYM53C416_SCSI_ID},
  197. {0, 0, SYM53C416_SCSI_ID},
  198. {0, 0, SYM53C416_SCSI_ID}
  199. };
  200. static int host_index = 0;
  201. static char info[120];
  202. static Scsi_Cmnd *current_command = NULL;
  203. static int fastpio = 1;
  204. static int probeaddrs[] = {0x200, 0x220, 0x240, 0};
  205. static void sym53c416_set_transfer_counter(int base, unsigned int len)
  206. {
  207. /* Program Transfer Counter */
  208. outb(len & 0x0000FF, base + TC_LOW);
  209. outb((len & 0x00FF00) >> 8, base + TC_MID);
  210. outb((len & 0xFF0000) >> 16, base + TC_HIGH);
  211. }
  212. static DEFINE_SPINLOCK(sym53c416_lock);
  213. /* Returns the number of bytes read */
  214. static __inline__ unsigned int sym53c416_read(int base, unsigned char *buffer, unsigned int len)
  215. {
  216. unsigned int orig_len = len;
  217. unsigned long flags = 0;
  218. unsigned int bytes_left;
  219. unsigned long i;
  220. int timeout = READ_TIMEOUT;
  221. /* Do transfer */
  222. spin_lock_irqsave(&sym53c416_lock, flags);
  223. while(len && timeout)
  224. {
  225. bytes_left = inb(base + PIO_FIFO_CNT); /* Number of bytes in the PIO FIFO */
  226. if(fastpio && bytes_left > 3)
  227. {
  228. insl(base + PIO_FIFO_1, buffer, bytes_left >> 2);
  229. buffer += bytes_left & 0xFC;
  230. len -= bytes_left & 0xFC;
  231. }
  232. else if(bytes_left > 0)
  233. {
  234. len -= bytes_left;
  235. for(; bytes_left > 0; bytes_left--)
  236. *(buffer++) = inb(base + PIO_FIFO_1);
  237. }
  238. else
  239. {
  240. i = jiffies + timeout;
  241. spin_unlock_irqrestore(&sym53c416_lock, flags);
  242. while(time_before(jiffies, i) && (inb(base + PIO_INT_REG) & EMPTY) && timeout)
  243. if(inb(base + PIO_INT_REG) & SCI)
  244. timeout = 0;
  245. spin_lock_irqsave(&sym53c416_lock, flags);
  246. if(inb(base + PIO_INT_REG) & EMPTY)
  247. timeout = 0;
  248. }
  249. }
  250. spin_unlock_irqrestore(&sym53c416_lock, flags);
  251. return orig_len - len;
  252. }
  253. /* Returns the number of bytes written */
  254. static __inline__ unsigned int sym53c416_write(int base, unsigned char *buffer, unsigned int len)
  255. {
  256. unsigned int orig_len = len;
  257. unsigned long flags = 0;
  258. unsigned int bufferfree;
  259. unsigned long i;
  260. unsigned int timeout = WRITE_TIMEOUT;
  261. /* Do transfer */
  262. spin_lock_irqsave(&sym53c416_lock, flags);
  263. while(len && timeout)
  264. {
  265. bufferfree = PIO_SIZE - inb(base + PIO_FIFO_CNT);
  266. if(bufferfree > len)
  267. bufferfree = len;
  268. if(fastpio && bufferfree > 3)
  269. {
  270. outsl(base + PIO_FIFO_1, buffer, bufferfree >> 2);
  271. buffer += bufferfree & 0xFC;
  272. len -= bufferfree & 0xFC;
  273. }
  274. else if(bufferfree > 0)
  275. {
  276. len -= bufferfree;
  277. for(; bufferfree > 0; bufferfree--)
  278. outb(*(buffer++), base + PIO_FIFO_1);
  279. }
  280. else
  281. {
  282. i = jiffies + timeout;
  283. spin_unlock_irqrestore(&sym53c416_lock, flags);
  284. while(time_before(jiffies, i) && (inb(base + PIO_INT_REG) & FULL) && timeout)
  285. ;
  286. spin_lock_irqsave(&sym53c416_lock, flags);
  287. if(inb(base + PIO_INT_REG) & FULL)
  288. timeout = 0;
  289. }
  290. }
  291. spin_unlock_irqrestore(&sym53c416_lock, flags);
  292. return orig_len - len;
  293. }
  294. static irqreturn_t sym53c416_intr_handle(int irq, void *dev_id)
  295. {
  296. struct Scsi_Host *dev = dev_id;
  297. int base = dev->io_port;
  298. int i;
  299. unsigned long flags = 0;
  300. unsigned char status_reg, pio_int_reg, int_reg;
  301. struct scatterlist *sg;
  302. unsigned int tot_trans = 0;
  303. spin_lock_irqsave(dev->host_lock,flags);
  304. status_reg = inb(base + STATUS_REG);
  305. pio_int_reg = inb(base + PIO_INT_REG);
  306. int_reg = inb(base + INT_REG);
  307. spin_unlock_irqrestore(dev->host_lock, flags);
  308. /* First, we handle error conditions */
  309. if(int_reg & SCI) /* SCSI Reset */
  310. {
  311. printk(KERN_DEBUG "sym53c416: Reset received\n");
  312. current_command->SCp.phase = idle;
  313. current_command->result = DID_RESET << 16;
  314. spin_lock_irqsave(dev->host_lock, flags);
  315. current_command->scsi_done(current_command);
  316. spin_unlock_irqrestore(dev->host_lock, flags);
  317. goto out;
  318. }
  319. if(int_reg & ILCMD) /* Illegal Command */
  320. {
  321. printk(KERN_WARNING "sym53c416: Illegal Command: 0x%02x.\n", inb(base + COMMAND_REG));
  322. current_command->SCp.phase = idle;
  323. current_command->result = DID_ERROR << 16;
  324. spin_lock_irqsave(dev->host_lock, flags);
  325. current_command->scsi_done(current_command);
  326. spin_unlock_irqrestore(dev->host_lock, flags);
  327. goto out;
  328. }
  329. if(status_reg & GE) /* Gross Error */
  330. {
  331. printk(KERN_WARNING "sym53c416: Controller reports gross error.\n");
  332. current_command->SCp.phase = idle;
  333. current_command->result = DID_ERROR << 16;
  334. spin_lock_irqsave(dev->host_lock, flags);
  335. current_command->scsi_done(current_command);
  336. spin_unlock_irqrestore(dev->host_lock, flags);
  337. goto out;
  338. }
  339. if(status_reg & PE) /* Parity Error */
  340. {
  341. printk(KERN_WARNING "sym53c416:SCSI parity error.\n");
  342. current_command->SCp.phase = idle;
  343. current_command->result = DID_PARITY << 16;
  344. spin_lock_irqsave(dev->host_lock, flags);
  345. current_command->scsi_done(current_command);
  346. spin_unlock_irqrestore(dev->host_lock, flags);
  347. goto out;
  348. }
  349. if(pio_int_reg & (CE | OUE))
  350. {
  351. printk(KERN_WARNING "sym53c416: PIO interrupt error.\n");
  352. current_command->SCp.phase = idle;
  353. current_command->result = DID_ERROR << 16;
  354. spin_lock_irqsave(dev->host_lock, flags);
  355. current_command->scsi_done(current_command);
  356. spin_unlock_irqrestore(dev->host_lock, flags);
  357. goto out;
  358. }
  359. if(int_reg & DIS) /* Disconnect */
  360. {
  361. if(current_command->SCp.phase != message_in)
  362. current_command->result = DID_NO_CONNECT << 16;
  363. else
  364. current_command->result = (current_command->SCp.Status & 0xFF) | ((current_command->SCp.Message & 0xFF) << 8) | (DID_OK << 16);
  365. current_command->SCp.phase = idle;
  366. spin_lock_irqsave(dev->host_lock, flags);
  367. current_command->scsi_done(current_command);
  368. spin_unlock_irqrestore(dev->host_lock, flags);
  369. goto out;
  370. }
  371. /* Now we handle SCSI phases */
  372. switch(status_reg & PHBITS) /* Filter SCSI phase out of status reg */
  373. {
  374. case PHASE_DATA_OUT:
  375. {
  376. if(int_reg & BS)
  377. {
  378. current_command->SCp.phase = data_out;
  379. outb(FLUSH_FIFO, base + COMMAND_REG);
  380. sym53c416_set_transfer_counter(base,
  381. scsi_bufflen(current_command));
  382. outb(TRANSFER_INFORMATION | PIO_MODE, base + COMMAND_REG);
  383. scsi_for_each_sg(current_command,
  384. sg, scsi_sg_count(current_command), i) {
  385. tot_trans += sym53c416_write(base,
  386. SG_ADDRESS(sg),
  387. sg->length);
  388. }
  389. if(tot_trans < current_command->underflow)
  390. printk(KERN_WARNING "sym53c416: Underflow, wrote %d bytes, request for %d bytes.\n", tot_trans, current_command->underflow);
  391. }
  392. break;
  393. }
  394. case PHASE_DATA_IN:
  395. {
  396. if(int_reg & BS)
  397. {
  398. current_command->SCp.phase = data_in;
  399. outb(FLUSH_FIFO, base + COMMAND_REG);
  400. sym53c416_set_transfer_counter(base,
  401. scsi_bufflen(current_command));
  402. outb(TRANSFER_INFORMATION | PIO_MODE, base + COMMAND_REG);
  403. scsi_for_each_sg(current_command,
  404. sg, scsi_sg_count(current_command), i) {
  405. tot_trans += sym53c416_read(base,
  406. SG_ADDRESS(sg),
  407. sg->length);
  408. }
  409. if(tot_trans < current_command->underflow)
  410. printk(KERN_WARNING "sym53c416: Underflow, read %d bytes, request for %d bytes.\n", tot_trans, current_command->underflow);
  411. }
  412. break;
  413. }
  414. case PHASE_COMMAND:
  415. {
  416. current_command->SCp.phase = command_ph;
  417. printk(KERN_ERR "sym53c416: Unknown interrupt in command phase.\n");
  418. break;
  419. }
  420. case PHASE_STATUS:
  421. {
  422. current_command->SCp.phase = status_ph;
  423. outb(FLUSH_FIFO, base + COMMAND_REG);
  424. outb(INIT_COMM_COMPLETE_SEQ, base + COMMAND_REG);
  425. break;
  426. }
  427. case PHASE_RESERVED_1:
  428. case PHASE_RESERVED_2:
  429. {
  430. printk(KERN_ERR "sym53c416: Reserved phase occurred.\n");
  431. break;
  432. }
  433. case PHASE_MESSAGE_OUT:
  434. {
  435. current_command->SCp.phase = message_out;
  436. outb(SET_ATN, base + COMMAND_REG);
  437. outb(MSG_ACCEPTED, base + COMMAND_REG);
  438. break;
  439. }
  440. case PHASE_MESSAGE_IN:
  441. {
  442. current_command->SCp.phase = message_in;
  443. current_command->SCp.Status = inb(base + SCSI_FIFO);
  444. current_command->SCp.Message = inb(base + SCSI_FIFO);
  445. if(current_command->SCp.Message == SAVE_POINTERS || current_command->SCp.Message == DISCONNECT)
  446. outb(SET_ATN, base + COMMAND_REG);
  447. outb(MSG_ACCEPTED, base + COMMAND_REG);
  448. break;
  449. }
  450. }
  451. out:
  452. return IRQ_HANDLED;
  453. }
  454. static void sym53c416_init(int base, int scsi_id)
  455. {
  456. outb(RESET_CHIP, base + COMMAND_REG);
  457. outb(NOOP, base + COMMAND_REG);
  458. outb(0x99, base + TOM); /* Time out of 250 ms */
  459. outb(0x05, base + STP);
  460. outb(0x00, base + SYNC_OFFSET);
  461. outb(EPC | scsi_id, base + CONF_REG_1);
  462. outb(FE | SCSI2 | TBPA, base + CONF_REG_2);
  463. outb(IDMRC | QTE | CDB10 | FSCSI | FCLK, base + CONF_REG_3);
  464. outb(0x83 | EAN, base + CONF_REG_4);
  465. outb(IE | WSE0, base + CONF_REG_5);
  466. outb(0, base + FEATURE_EN);
  467. }
  468. static int sym53c416_probeirq(int base, int scsi_id)
  469. {
  470. int irq, irqs;
  471. unsigned long i;
  472. /* Clear interrupt register */
  473. inb(base + INT_REG);
  474. /* Start probing for irq's */
  475. irqs = probe_irq_on();
  476. /* Reinit chip */
  477. sym53c416_init(base, scsi_id);
  478. /* Cause interrupt */
  479. outb(NOOP, base + COMMAND_REG);
  480. outb(ILLEGAL, base + COMMAND_REG);
  481. outb(0x07, base + DEST_BUS_ID);
  482. outb(0x00, base + DEST_BUS_ID);
  483. /* Wait for interrupt to occur */
  484. i = jiffies + 20;
  485. while(time_before(jiffies, i) && !(inb(base + STATUS_REG) & SCI))
  486. barrier();
  487. if(time_before_eq(i, jiffies)) /* timed out */
  488. return 0;
  489. /* Get occurred irq */
  490. irq = probe_irq_off(irqs);
  491. sym53c416_init(base, scsi_id);
  492. return irq;
  493. }
  494. /* Setup: sym53c416=base,irq */
  495. void sym53c416_setup(char *str, int *ints)
  496. {
  497. int i;
  498. if(host_index >= MAXHOSTS)
  499. {
  500. printk(KERN_WARNING "sym53c416: Too many hosts defined\n");
  501. return;
  502. }
  503. if(ints[0] < 1 || ints[0] > 2)
  504. {
  505. printk(KERN_ERR "sym53c416: Wrong number of parameters:\n");
  506. printk(KERN_ERR "sym53c416: usage: sym53c416=<base>[,<irq>]\n");
  507. return;
  508. }
  509. for(i = 0; i < host_index && i >= 0; i++)
  510. if(hosts[i].base == ints[1])
  511. i = -2;
  512. if(i >= 0)
  513. {
  514. hosts[host_index].base = ints[1];
  515. hosts[host_index].irq = (ints[0] == 2)? ints[2] : 0;
  516. host_index++;
  517. }
  518. }
  519. static int sym53c416_test(int base)
  520. {
  521. outb(RESET_CHIP, base + COMMAND_REG);
  522. outb(NOOP, base + COMMAND_REG);
  523. if(inb(base + COMMAND_REG) != NOOP)
  524. return 0;
  525. if(!inb(base + TC_HIGH) || inb(base + TC_HIGH) == 0xFF)
  526. return 0;
  527. if((inb(base + PIO_INT_REG) & (FULL | EMPTY | CE | OUE | FIE | EIE)) != EMPTY)
  528. return 0;
  529. return 1;
  530. }
  531. static struct isapnp_device_id id_table[] __devinitdata = {
  532. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  533. ISAPNP_VENDOR('S','L','I'), ISAPNP_FUNCTION(0x4161), 0 },
  534. { ISAPNP_ANY_ID, ISAPNP_ANY_ID,
  535. ISAPNP_VENDOR('S','L','I'), ISAPNP_FUNCTION(0x4163), 0 },
  536. { ISAPNP_DEVICE_SINGLE_END }
  537. };
  538. MODULE_DEVICE_TABLE(isapnp, id_table);
  539. static void sym53c416_probe(void)
  540. {
  541. int *base = probeaddrs;
  542. int ints[2];
  543. ints[0] = 1;
  544. for(; *base; base++) {
  545. if (request_region(*base, IO_RANGE, ID)) {
  546. if (sym53c416_test(*base)) {
  547. ints[1] = *base;
  548. sym53c416_setup(NULL, ints);
  549. }
  550. release_region(*base, IO_RANGE);
  551. }
  552. }
  553. }
  554. int __init sym53c416_detect(struct scsi_host_template *tpnt)
  555. {
  556. unsigned long flags;
  557. struct Scsi_Host * shpnt = NULL;
  558. int i;
  559. int count;
  560. struct pnp_dev *idev = NULL;
  561. #ifdef MODULE
  562. int ints[3];
  563. ints[0] = 2;
  564. if(sym53c416_base[0])
  565. {
  566. ints[1] = sym53c416_base[0];
  567. ints[2] = sym53c416_base[1];
  568. sym53c416_setup(NULL, ints);
  569. }
  570. if(sym53c416_base_1[0])
  571. {
  572. ints[1] = sym53c416_base_1[0];
  573. ints[2] = sym53c416_base_1[1];
  574. sym53c416_setup(NULL, ints);
  575. }
  576. if(sym53c416_base_2[0])
  577. {
  578. ints[1] = sym53c416_base_2[0];
  579. ints[2] = sym53c416_base_2[1];
  580. sym53c416_setup(NULL, ints);
  581. }
  582. if(sym53c416_base_3[0])
  583. {
  584. ints[1] = sym53c416_base_3[0];
  585. ints[2] = sym53c416_base_3[1];
  586. sym53c416_setup(NULL, ints);
  587. }
  588. #endif
  589. printk(KERN_INFO "sym53c416.c: %s\n", VERSION_STRING);
  590. for (i=0; id_table[i].vendor != 0; i++) {
  591. while((idev=pnp_find_dev(NULL, id_table[i].vendor,
  592. id_table[i].function, idev))!=NULL)
  593. {
  594. int i[3];
  595. if(pnp_device_attach(idev)<0)
  596. {
  597. printk(KERN_WARNING "sym53c416: unable to attach PnP device.\n");
  598. continue;
  599. }
  600. if(pnp_activate_dev(idev) < 0)
  601. {
  602. printk(KERN_WARNING "sym53c416: unable to activate PnP device.\n");
  603. pnp_device_detach(idev);
  604. continue;
  605. }
  606. i[0] = 2;
  607. i[1] = pnp_port_start(idev, 0);
  608. i[2] = pnp_irq(idev, 0);
  609. printk(KERN_INFO "sym53c416: ISAPnP card found and configured at 0x%X, IRQ %d.\n",
  610. i[1], i[2]);
  611. sym53c416_setup(NULL, i);
  612. }
  613. }
  614. sym53c416_probe();
  615. /* Now we register and set up each host adapter found... */
  616. for(count = 0, i = 0; i < host_index; i++) {
  617. if (!request_region(hosts[i].base, IO_RANGE, ID))
  618. continue;
  619. if (!sym53c416_test(hosts[i].base)) {
  620. printk(KERN_WARNING "No sym53c416 found at address 0x%03x\n", hosts[i].base);
  621. goto fail_release_region;
  622. }
  623. /* We don't have an irq yet, so we should probe for one */
  624. if (!hosts[i].irq)
  625. hosts[i].irq = sym53c416_probeirq(hosts[i].base, hosts[i].scsi_id);
  626. if (!hosts[i].irq)
  627. goto fail_release_region;
  628. shpnt = scsi_register(tpnt, 0);
  629. if (!shpnt)
  630. goto fail_release_region;
  631. /* Request for specified IRQ */
  632. if (request_irq(hosts[i].irq, sym53c416_intr_handle, 0, ID, shpnt))
  633. goto fail_free_host;
  634. spin_lock_irqsave(&sym53c416_lock, flags);
  635. shpnt->unique_id = hosts[i].base;
  636. shpnt->io_port = hosts[i].base;
  637. shpnt->n_io_port = IO_RANGE;
  638. shpnt->irq = hosts[i].irq;
  639. shpnt->this_id = hosts[i].scsi_id;
  640. sym53c416_init(hosts[i].base, hosts[i].scsi_id);
  641. count++;
  642. spin_unlock_irqrestore(&sym53c416_lock, flags);
  643. continue;
  644. fail_free_host:
  645. scsi_unregister(shpnt);
  646. fail_release_region:
  647. release_region(hosts[i].base, IO_RANGE);
  648. }
  649. return count;
  650. }
  651. const char *sym53c416_info(struct Scsi_Host *SChost)
  652. {
  653. int i;
  654. int base = SChost->io_port;
  655. int irq = SChost->irq;
  656. int scsi_id = 0;
  657. int rev = inb(base + TC_HIGH);
  658. for(i = 0; i < host_index; i++)
  659. if(hosts[i].base == base)
  660. scsi_id = hosts[i].scsi_id;
  661. sprintf(info, "Symbios Logic 53c416 (rev. %d) at 0x%03x, irq %d, SCSI-ID %d, %s pio", rev, base, irq, scsi_id, (fastpio)? "fast" : "slow");
  662. return info;
  663. }
  664. int sym53c416_queuecommand(Scsi_Cmnd *SCpnt, void (*done)(Scsi_Cmnd *))
  665. {
  666. int base;
  667. unsigned long flags = 0;
  668. int i;
  669. /* Store base register as we can have more than one controller in the system */
  670. base = SCpnt->device->host->io_port;
  671. current_command = SCpnt; /* set current command */
  672. current_command->scsi_done = done; /* set ptr to done function */
  673. current_command->SCp.phase = command_ph; /* currect phase is the command phase */
  674. current_command->SCp.Status = 0;
  675. current_command->SCp.Message = 0;
  676. spin_lock_irqsave(&sym53c416_lock, flags);
  677. outb(scmd_id(SCpnt), base + DEST_BUS_ID); /* Set scsi id target */
  678. outb(FLUSH_FIFO, base + COMMAND_REG); /* Flush SCSI and PIO FIFO's */
  679. /* Write SCSI command into the SCSI fifo */
  680. for(i = 0; i < SCpnt->cmd_len; i++)
  681. outb(SCpnt->cmnd[i], base + SCSI_FIFO);
  682. /* Start selection sequence */
  683. outb(SEL_WITHOUT_ATN_SEQ, base + COMMAND_REG);
  684. /* Now an interrupt will be generated which we will catch in out interrupt routine */
  685. spin_unlock_irqrestore(&sym53c416_lock, flags);
  686. return 0;
  687. }
  688. static int sym53c416_host_reset(Scsi_Cmnd *SCpnt)
  689. {
  690. int base;
  691. int scsi_id = -1;
  692. int i;
  693. unsigned long flags;
  694. spin_lock_irqsave(&sym53c416_lock, flags);
  695. /* printk("sym53c416_reset\n"); */
  696. base = SCpnt->device->host->io_port;
  697. /* search scsi_id - fixme, we shouldnt need to iterate for this! */
  698. for(i = 0; i < host_index && scsi_id == -1; i++)
  699. if(hosts[i].base == base)
  700. scsi_id = hosts[i].scsi_id;
  701. outb(RESET_CHIP, base + COMMAND_REG);
  702. outb(NOOP | PIO_MODE, base + COMMAND_REG);
  703. outb(RESET_SCSI_BUS, base + COMMAND_REG);
  704. sym53c416_init(base, scsi_id);
  705. spin_unlock_irqrestore(&sym53c416_lock, flags);
  706. return SUCCESS;
  707. }
  708. static int sym53c416_release(struct Scsi_Host *shost)
  709. {
  710. if (shost->irq)
  711. free_irq(shost->irq, shost);
  712. if (shost->io_port && shost->n_io_port)
  713. release_region(shost->io_port, shost->n_io_port);
  714. return 0;
  715. }
  716. static int sym53c416_bios_param(struct scsi_device *sdev,
  717. struct block_device *dev,
  718. sector_t capacity, int *ip)
  719. {
  720. int size;
  721. size = capacity;
  722. ip[0] = 64; /* heads */
  723. ip[1] = 32; /* sectors */
  724. if((ip[2] = size >> 11) > 1024) /* cylinders, test for big disk */
  725. {
  726. ip[0] = 255; /* heads */
  727. ip[1] = 63; /* sectors */
  728. ip[2] = size / (255 * 63); /* cylinders */
  729. }
  730. return 0;
  731. }
  732. /* Loadable module support */
  733. #ifdef MODULE
  734. MODULE_AUTHOR("Lieven Willems");
  735. MODULE_LICENSE("GPL");
  736. module_param_array(sym53c416, uint, NULL, 0);
  737. module_param_array(sym53c416_1, uint, NULL, 0);
  738. module_param_array(sym53c416_2, uint, NULL, 0);
  739. module_param_array(sym53c416_3, uint, NULL, 0);
  740. #endif
  741. static struct scsi_host_template driver_template = {
  742. .proc_name = "sym53c416",
  743. .name = "Symbios Logic 53c416",
  744. .detect = sym53c416_detect,
  745. .info = sym53c416_info,
  746. .queuecommand = sym53c416_queuecommand,
  747. .eh_host_reset_handler =sym53c416_host_reset,
  748. .release = sym53c416_release,
  749. .bios_param = sym53c416_bios_param,
  750. .can_queue = 1,
  751. .this_id = SYM53C416_SCSI_ID,
  752. .sg_tablesize = 32,
  753. .cmd_per_lun = 1,
  754. .unchecked_isa_dma = 1,
  755. .use_clustering = ENABLE_CLUSTERING,
  756. };
  757. #include "scsi_module.c"