sym53c416.c 26 KB

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