qlogicpti.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460
  1. /* qlogicpti.c: Performance Technologies QlogicISP sbus card driver.
  2. *
  3. * Copyright (C) 1996, 2006 David S. Miller (davem@davemloft.net)
  4. *
  5. * A lot of this driver was directly stolen from Erik H. Moe's PCI
  6. * Qlogic ISP driver. Mucho kudos to him for this code.
  7. *
  8. * An even bigger kudos to John Grana at Performance Technologies
  9. * for providing me with the hardware to write this driver, you rule
  10. * John you really do.
  11. *
  12. * May, 2, 1997: Added support for QLGC,isp --jj
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/delay.h>
  16. #include <linux/types.h>
  17. #include <linux/string.h>
  18. #include <linux/slab.h>
  19. #include <linux/blkdev.h>
  20. #include <linux/proc_fs.h>
  21. #include <linux/stat.h>
  22. #include <linux/init.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/module.h>
  26. #include <linux/jiffies.h>
  27. #include <linux/dma-mapping.h>
  28. #include <asm/byteorder.h>
  29. #include "qlogicpti.h"
  30. #include <asm/sbus.h>
  31. #include <asm/dma.h>
  32. #include <asm/system.h>
  33. #include <asm/ptrace.h>
  34. #include <asm/pgtable.h>
  35. #include <asm/oplib.h>
  36. #include <asm/io.h>
  37. #include <asm/irq.h>
  38. #include <scsi/scsi.h>
  39. #include <scsi/scsi_cmnd.h>
  40. #include <scsi/scsi_device.h>
  41. #include <scsi/scsi_eh.h>
  42. #include <scsi/scsi_tcq.h>
  43. #include <scsi/scsi_host.h>
  44. #define MAX_TARGETS 16
  45. #define MAX_LUNS 8 /* 32 for 1.31 F/W */
  46. #define DEFAULT_LOOP_COUNT 10000
  47. #include "qlogicpti_asm.c"
  48. static struct qlogicpti *qptichain = NULL;
  49. static DEFINE_SPINLOCK(qptichain_lock);
  50. #define PACKB(a, b) (((a)<<4)|(b))
  51. static const u_char mbox_param[] = {
  52. PACKB(1, 1), /* MBOX_NO_OP */
  53. PACKB(5, 5), /* MBOX_LOAD_RAM */
  54. PACKB(2, 0), /* MBOX_EXEC_FIRMWARE */
  55. PACKB(5, 5), /* MBOX_DUMP_RAM */
  56. PACKB(3, 3), /* MBOX_WRITE_RAM_WORD */
  57. PACKB(2, 3), /* MBOX_READ_RAM_WORD */
  58. PACKB(6, 6), /* MBOX_MAILBOX_REG_TEST */
  59. PACKB(2, 3), /* MBOX_VERIFY_CHECKSUM */
  60. PACKB(1, 3), /* MBOX_ABOUT_FIRMWARE */
  61. PACKB(0, 0), /* 0x0009 */
  62. PACKB(0, 0), /* 0x000a */
  63. PACKB(0, 0), /* 0x000b */
  64. PACKB(0, 0), /* 0x000c */
  65. PACKB(0, 0), /* 0x000d */
  66. PACKB(1, 2), /* MBOX_CHECK_FIRMWARE */
  67. PACKB(0, 0), /* 0x000f */
  68. PACKB(5, 5), /* MBOX_INIT_REQ_QUEUE */
  69. PACKB(6, 6), /* MBOX_INIT_RES_QUEUE */
  70. PACKB(4, 4), /* MBOX_EXECUTE_IOCB */
  71. PACKB(2, 2), /* MBOX_WAKE_UP */
  72. PACKB(1, 6), /* MBOX_STOP_FIRMWARE */
  73. PACKB(4, 4), /* MBOX_ABORT */
  74. PACKB(2, 2), /* MBOX_ABORT_DEVICE */
  75. PACKB(3, 3), /* MBOX_ABORT_TARGET */
  76. PACKB(2, 2), /* MBOX_BUS_RESET */
  77. PACKB(2, 3), /* MBOX_STOP_QUEUE */
  78. PACKB(2, 3), /* MBOX_START_QUEUE */
  79. PACKB(2, 3), /* MBOX_SINGLE_STEP_QUEUE */
  80. PACKB(2, 3), /* MBOX_ABORT_QUEUE */
  81. PACKB(2, 4), /* MBOX_GET_DEV_QUEUE_STATUS */
  82. PACKB(0, 0), /* 0x001e */
  83. PACKB(1, 3), /* MBOX_GET_FIRMWARE_STATUS */
  84. PACKB(1, 2), /* MBOX_GET_INIT_SCSI_ID */
  85. PACKB(1, 2), /* MBOX_GET_SELECT_TIMEOUT */
  86. PACKB(1, 3), /* MBOX_GET_RETRY_COUNT */
  87. PACKB(1, 2), /* MBOX_GET_TAG_AGE_LIMIT */
  88. PACKB(1, 2), /* MBOX_GET_CLOCK_RATE */
  89. PACKB(1, 2), /* MBOX_GET_ACT_NEG_STATE */
  90. PACKB(1, 2), /* MBOX_GET_ASYNC_DATA_SETUP_TIME */
  91. PACKB(1, 3), /* MBOX_GET_SBUS_PARAMS */
  92. PACKB(2, 4), /* MBOX_GET_TARGET_PARAMS */
  93. PACKB(2, 4), /* MBOX_GET_DEV_QUEUE_PARAMS */
  94. PACKB(0, 0), /* 0x002a */
  95. PACKB(0, 0), /* 0x002b */
  96. PACKB(0, 0), /* 0x002c */
  97. PACKB(0, 0), /* 0x002d */
  98. PACKB(0, 0), /* 0x002e */
  99. PACKB(0, 0), /* 0x002f */
  100. PACKB(2, 2), /* MBOX_SET_INIT_SCSI_ID */
  101. PACKB(2, 2), /* MBOX_SET_SELECT_TIMEOUT */
  102. PACKB(3, 3), /* MBOX_SET_RETRY_COUNT */
  103. PACKB(2, 2), /* MBOX_SET_TAG_AGE_LIMIT */
  104. PACKB(2, 2), /* MBOX_SET_CLOCK_RATE */
  105. PACKB(2, 2), /* MBOX_SET_ACTIVE_NEG_STATE */
  106. PACKB(2, 2), /* MBOX_SET_ASYNC_DATA_SETUP_TIME */
  107. PACKB(3, 3), /* MBOX_SET_SBUS_CONTROL_PARAMS */
  108. PACKB(4, 4), /* MBOX_SET_TARGET_PARAMS */
  109. PACKB(4, 4), /* MBOX_SET_DEV_QUEUE_PARAMS */
  110. PACKB(0, 0), /* 0x003a */
  111. PACKB(0, 0), /* 0x003b */
  112. PACKB(0, 0), /* 0x003c */
  113. PACKB(0, 0), /* 0x003d */
  114. PACKB(0, 0), /* 0x003e */
  115. PACKB(0, 0), /* 0x003f */
  116. PACKB(0, 0), /* 0x0040 */
  117. PACKB(0, 0), /* 0x0041 */
  118. PACKB(0, 0) /* 0x0042 */
  119. };
  120. #define MAX_MBOX_COMMAND ARRAY_SIZE(mbox_param)
  121. /* queue length's _must_ be power of two: */
  122. #define QUEUE_DEPTH(in, out, ql) ((in - out) & (ql))
  123. #define REQ_QUEUE_DEPTH(in, out) QUEUE_DEPTH(in, out, \
  124. QLOGICPTI_REQ_QUEUE_LEN)
  125. #define RES_QUEUE_DEPTH(in, out) QUEUE_DEPTH(in, out, RES_QUEUE_LEN)
  126. static inline void qlogicpti_enable_irqs(struct qlogicpti *qpti)
  127. {
  128. sbus_writew(SBUS_CTRL_ERIRQ | SBUS_CTRL_GENAB,
  129. qpti->qregs + SBUS_CTRL);
  130. }
  131. static inline void qlogicpti_disable_irqs(struct qlogicpti *qpti)
  132. {
  133. sbus_writew(0, qpti->qregs + SBUS_CTRL);
  134. }
  135. static inline void set_sbus_cfg1(struct qlogicpti *qpti)
  136. {
  137. u16 val;
  138. u8 bursts = qpti->bursts;
  139. #if 0 /* It appears that at least PTI cards do not support
  140. * 64-byte bursts and that setting the B64 bit actually
  141. * is a nop and the chip ends up using the smallest burst
  142. * size. -DaveM
  143. */
  144. if (sbus_can_burst64(qpti->sdev) && (bursts & DMA_BURST64)) {
  145. val = (SBUS_CFG1_BENAB | SBUS_CFG1_B64);
  146. } else
  147. #endif
  148. if (bursts & DMA_BURST32) {
  149. val = (SBUS_CFG1_BENAB | SBUS_CFG1_B32);
  150. } else if (bursts & DMA_BURST16) {
  151. val = (SBUS_CFG1_BENAB | SBUS_CFG1_B16);
  152. } else if (bursts & DMA_BURST8) {
  153. val = (SBUS_CFG1_BENAB | SBUS_CFG1_B8);
  154. } else {
  155. val = 0; /* No sbus bursts for you... */
  156. }
  157. sbus_writew(val, qpti->qregs + SBUS_CFG1);
  158. }
  159. static int qlogicpti_mbox_command(struct qlogicpti *qpti, u_short param[], int force)
  160. {
  161. int loop_count;
  162. u16 tmp;
  163. if (mbox_param[param[0]] == 0)
  164. return 1;
  165. /* Set SBUS semaphore. */
  166. tmp = sbus_readw(qpti->qregs + SBUS_SEMAPHORE);
  167. tmp |= SBUS_SEMAPHORE_LCK;
  168. sbus_writew(tmp, qpti->qregs + SBUS_SEMAPHORE);
  169. /* Wait for host IRQ bit to clear. */
  170. loop_count = DEFAULT_LOOP_COUNT;
  171. while (--loop_count && (sbus_readw(qpti->qregs + HCCTRL) & HCCTRL_HIRQ)) {
  172. barrier();
  173. cpu_relax();
  174. }
  175. if (!loop_count)
  176. printk(KERN_EMERG "qlogicpti%d: mbox_command loop timeout #1\n",
  177. qpti->qpti_id);
  178. /* Write mailbox command registers. */
  179. switch (mbox_param[param[0]] >> 4) {
  180. case 6: sbus_writew(param[5], qpti->qregs + MBOX5);
  181. case 5: sbus_writew(param[4], qpti->qregs + MBOX4);
  182. case 4: sbus_writew(param[3], qpti->qregs + MBOX3);
  183. case 3: sbus_writew(param[2], qpti->qregs + MBOX2);
  184. case 2: sbus_writew(param[1], qpti->qregs + MBOX1);
  185. case 1: sbus_writew(param[0], qpti->qregs + MBOX0);
  186. }
  187. /* Clear RISC interrupt. */
  188. tmp = sbus_readw(qpti->qregs + HCCTRL);
  189. tmp |= HCCTRL_CRIRQ;
  190. sbus_writew(tmp, qpti->qregs + HCCTRL);
  191. /* Clear SBUS semaphore. */
  192. sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
  193. /* Set HOST interrupt. */
  194. tmp = sbus_readw(qpti->qregs + HCCTRL);
  195. tmp |= HCCTRL_SHIRQ;
  196. sbus_writew(tmp, qpti->qregs + HCCTRL);
  197. /* Wait for HOST interrupt clears. */
  198. loop_count = DEFAULT_LOOP_COUNT;
  199. while (--loop_count &&
  200. (sbus_readw(qpti->qregs + HCCTRL) & HCCTRL_CRIRQ))
  201. udelay(20);
  202. if (!loop_count)
  203. printk(KERN_EMERG "qlogicpti%d: mbox_command[%04x] loop timeout #2\n",
  204. qpti->qpti_id, param[0]);
  205. /* Wait for SBUS semaphore to get set. */
  206. loop_count = DEFAULT_LOOP_COUNT;
  207. while (--loop_count &&
  208. !(sbus_readw(qpti->qregs + SBUS_SEMAPHORE) & SBUS_SEMAPHORE_LCK)) {
  209. udelay(20);
  210. /* Workaround for some buggy chips. */
  211. if (sbus_readw(qpti->qregs + MBOX0) & 0x4000)
  212. break;
  213. }
  214. if (!loop_count)
  215. printk(KERN_EMERG "qlogicpti%d: mbox_command[%04x] loop timeout #3\n",
  216. qpti->qpti_id, param[0]);
  217. /* Wait for MBOX busy condition to go away. */
  218. loop_count = DEFAULT_LOOP_COUNT;
  219. while (--loop_count && (sbus_readw(qpti->qregs + MBOX0) == 0x04))
  220. udelay(20);
  221. if (!loop_count)
  222. printk(KERN_EMERG "qlogicpti%d: mbox_command[%04x] loop timeout #4\n",
  223. qpti->qpti_id, param[0]);
  224. /* Read back output parameters. */
  225. switch (mbox_param[param[0]] & 0xf) {
  226. case 6: param[5] = sbus_readw(qpti->qregs + MBOX5);
  227. case 5: param[4] = sbus_readw(qpti->qregs + MBOX4);
  228. case 4: param[3] = sbus_readw(qpti->qregs + MBOX3);
  229. case 3: param[2] = sbus_readw(qpti->qregs + MBOX2);
  230. case 2: param[1] = sbus_readw(qpti->qregs + MBOX1);
  231. case 1: param[0] = sbus_readw(qpti->qregs + MBOX0);
  232. }
  233. /* Clear RISC interrupt. */
  234. tmp = sbus_readw(qpti->qregs + HCCTRL);
  235. tmp |= HCCTRL_CRIRQ;
  236. sbus_writew(tmp, qpti->qregs + HCCTRL);
  237. /* Release SBUS semaphore. */
  238. tmp = sbus_readw(qpti->qregs + SBUS_SEMAPHORE);
  239. tmp &= ~(SBUS_SEMAPHORE_LCK);
  240. sbus_writew(tmp, qpti->qregs + SBUS_SEMAPHORE);
  241. /* We're done. */
  242. return 0;
  243. }
  244. static inline void qlogicpti_set_hostdev_defaults(struct qlogicpti *qpti)
  245. {
  246. int i;
  247. qpti->host_param.initiator_scsi_id = qpti->scsi_id;
  248. qpti->host_param.bus_reset_delay = 3;
  249. qpti->host_param.retry_count = 0;
  250. qpti->host_param.retry_delay = 5;
  251. qpti->host_param.async_data_setup_time = 3;
  252. qpti->host_param.req_ack_active_negation = 1;
  253. qpti->host_param.data_line_active_negation = 1;
  254. qpti->host_param.data_dma_burst_enable = 1;
  255. qpti->host_param.command_dma_burst_enable = 1;
  256. qpti->host_param.tag_aging = 8;
  257. qpti->host_param.selection_timeout = 250;
  258. qpti->host_param.max_queue_depth = 256;
  259. for(i = 0; i < MAX_TARGETS; i++) {
  260. /*
  261. * disconnect, parity, arq, reneg on reset, and, oddly enough
  262. * tags...the midlayer's notion of tagged support has to match
  263. * our device settings, and since we base whether we enable a
  264. * tag on a per-cmnd basis upon what the midlayer sez, we
  265. * actually enable the capability here.
  266. */
  267. qpti->dev_param[i].device_flags = 0xcd;
  268. qpti->dev_param[i].execution_throttle = 16;
  269. if (qpti->ultra) {
  270. qpti->dev_param[i].synchronous_period = 12;
  271. qpti->dev_param[i].synchronous_offset = 8;
  272. } else {
  273. qpti->dev_param[i].synchronous_period = 25;
  274. qpti->dev_param[i].synchronous_offset = 12;
  275. }
  276. qpti->dev_param[i].device_enable = 1;
  277. }
  278. }
  279. static int qlogicpti_reset_hardware(struct Scsi_Host *host)
  280. {
  281. struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  282. u_short param[6];
  283. unsigned short risc_code_addr;
  284. int loop_count, i;
  285. unsigned long flags;
  286. risc_code_addr = 0x1000; /* all load addresses are at 0x1000 */
  287. spin_lock_irqsave(host->host_lock, flags);
  288. sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
  289. /* Only reset the scsi bus if it is not free. */
  290. if (sbus_readw(qpti->qregs + CPU_PCTRL) & CPU_PCTRL_BSY) {
  291. sbus_writew(CPU_ORIDE_RMOD, qpti->qregs + CPU_ORIDE);
  292. sbus_writew(CPU_CMD_BRESET, qpti->qregs + CPU_CMD);
  293. udelay(400);
  294. }
  295. sbus_writew(SBUS_CTRL_RESET, qpti->qregs + SBUS_CTRL);
  296. sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + CMD_DMA_CTRL);
  297. sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + DATA_DMA_CTRL);
  298. loop_count = DEFAULT_LOOP_COUNT;
  299. while (--loop_count && ((sbus_readw(qpti->qregs + MBOX0) & 0xff) == 0x04))
  300. udelay(20);
  301. if (!loop_count)
  302. printk(KERN_EMERG "qlogicpti%d: reset_hardware loop timeout\n",
  303. qpti->qpti_id);
  304. sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
  305. set_sbus_cfg1(qpti);
  306. qlogicpti_enable_irqs(qpti);
  307. if (sbus_readw(qpti->qregs + RISC_PSR) & RISC_PSR_ULTRA) {
  308. qpti->ultra = 1;
  309. sbus_writew((RISC_MTREG_P0ULTRA | RISC_MTREG_P1ULTRA),
  310. qpti->qregs + RISC_MTREG);
  311. } else {
  312. qpti->ultra = 0;
  313. sbus_writew((RISC_MTREG_P0DFLT | RISC_MTREG_P1DFLT),
  314. qpti->qregs + RISC_MTREG);
  315. }
  316. /* reset adapter and per-device default values. */
  317. /* do it after finding out whether we're ultra mode capable */
  318. qlogicpti_set_hostdev_defaults(qpti);
  319. /* Release the RISC processor. */
  320. sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
  321. /* Get RISC to start executing the firmware code. */
  322. param[0] = MBOX_EXEC_FIRMWARE;
  323. param[1] = risc_code_addr;
  324. if (qlogicpti_mbox_command(qpti, param, 1)) {
  325. printk(KERN_EMERG "qlogicpti%d: Cannot execute ISP firmware.\n",
  326. qpti->qpti_id);
  327. spin_unlock_irqrestore(host->host_lock, flags);
  328. return 1;
  329. }
  330. /* Set initiator scsi ID. */
  331. param[0] = MBOX_SET_INIT_SCSI_ID;
  332. param[1] = qpti->host_param.initiator_scsi_id;
  333. if (qlogicpti_mbox_command(qpti, param, 1) ||
  334. (param[0] != MBOX_COMMAND_COMPLETE)) {
  335. printk(KERN_EMERG "qlogicpti%d: Cannot set initiator SCSI ID.\n",
  336. qpti->qpti_id);
  337. spin_unlock_irqrestore(host->host_lock, flags);
  338. return 1;
  339. }
  340. /* Initialize state of the queues, both hw and sw. */
  341. qpti->req_in_ptr = qpti->res_out_ptr = 0;
  342. param[0] = MBOX_INIT_RES_QUEUE;
  343. param[1] = RES_QUEUE_LEN + 1;
  344. param[2] = (u_short) (qpti->res_dvma >> 16);
  345. param[3] = (u_short) (qpti->res_dvma & 0xffff);
  346. param[4] = param[5] = 0;
  347. if (qlogicpti_mbox_command(qpti, param, 1)) {
  348. printk(KERN_EMERG "qlogicpti%d: Cannot init response queue.\n",
  349. qpti->qpti_id);
  350. spin_unlock_irqrestore(host->host_lock, flags);
  351. return 1;
  352. }
  353. param[0] = MBOX_INIT_REQ_QUEUE;
  354. param[1] = QLOGICPTI_REQ_QUEUE_LEN + 1;
  355. param[2] = (u_short) (qpti->req_dvma >> 16);
  356. param[3] = (u_short) (qpti->req_dvma & 0xffff);
  357. param[4] = param[5] = 0;
  358. if (qlogicpti_mbox_command(qpti, param, 1)) {
  359. printk(KERN_EMERG "qlogicpti%d: Cannot init request queue.\n",
  360. qpti->qpti_id);
  361. spin_unlock_irqrestore(host->host_lock, flags);
  362. return 1;
  363. }
  364. param[0] = MBOX_SET_RETRY_COUNT;
  365. param[1] = qpti->host_param.retry_count;
  366. param[2] = qpti->host_param.retry_delay;
  367. qlogicpti_mbox_command(qpti, param, 0);
  368. param[0] = MBOX_SET_TAG_AGE_LIMIT;
  369. param[1] = qpti->host_param.tag_aging;
  370. qlogicpti_mbox_command(qpti, param, 0);
  371. for (i = 0; i < MAX_TARGETS; i++) {
  372. param[0] = MBOX_GET_DEV_QUEUE_PARAMS;
  373. param[1] = (i << 8);
  374. qlogicpti_mbox_command(qpti, param, 0);
  375. }
  376. param[0] = MBOX_GET_FIRMWARE_STATUS;
  377. qlogicpti_mbox_command(qpti, param, 0);
  378. param[0] = MBOX_SET_SELECT_TIMEOUT;
  379. param[1] = qpti->host_param.selection_timeout;
  380. qlogicpti_mbox_command(qpti, param, 0);
  381. for (i = 0; i < MAX_TARGETS; i++) {
  382. param[0] = MBOX_SET_TARGET_PARAMS;
  383. param[1] = (i << 8);
  384. param[2] = (qpti->dev_param[i].device_flags << 8);
  385. /*
  386. * Since we're now loading 1.31 f/w, force narrow/async.
  387. */
  388. param[2] |= 0xc0;
  389. param[3] = 0; /* no offset, we do not have sync mode yet */
  390. qlogicpti_mbox_command(qpti, param, 0);
  391. }
  392. /*
  393. * Always (sigh) do an initial bus reset (kicks f/w).
  394. */
  395. param[0] = MBOX_BUS_RESET;
  396. param[1] = qpti->host_param.bus_reset_delay;
  397. qlogicpti_mbox_command(qpti, param, 0);
  398. qpti->send_marker = 1;
  399. spin_unlock_irqrestore(host->host_lock, flags);
  400. return 0;
  401. }
  402. #define PTI_RESET_LIMIT 400
  403. static int __devinit qlogicpti_load_firmware(struct qlogicpti *qpti)
  404. {
  405. struct Scsi_Host *host = qpti->qhost;
  406. unsigned short csum = 0;
  407. unsigned short param[6];
  408. unsigned short *risc_code, risc_code_addr, risc_code_length;
  409. unsigned long flags;
  410. int i, timeout;
  411. risc_code = &sbus_risc_code01[0];
  412. risc_code_addr = 0x1000; /* all f/w modules load at 0x1000 */
  413. risc_code_length = sbus_risc_code_length01;
  414. spin_lock_irqsave(host->host_lock, flags);
  415. /* Verify the checksum twice, one before loading it, and once
  416. * afterwards via the mailbox commands.
  417. */
  418. for (i = 0; i < risc_code_length; i++)
  419. csum += risc_code[i];
  420. if (csum) {
  421. spin_unlock_irqrestore(host->host_lock, flags);
  422. printk(KERN_EMERG "qlogicpti%d: Aieee, firmware checksum failed!",
  423. qpti->qpti_id);
  424. return 1;
  425. }
  426. sbus_writew(SBUS_CTRL_RESET, qpti->qregs + SBUS_CTRL);
  427. sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + CMD_DMA_CTRL);
  428. sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + DATA_DMA_CTRL);
  429. timeout = PTI_RESET_LIMIT;
  430. while (--timeout && (sbus_readw(qpti->qregs + SBUS_CTRL) & SBUS_CTRL_RESET))
  431. udelay(20);
  432. if (!timeout) {
  433. spin_unlock_irqrestore(host->host_lock, flags);
  434. printk(KERN_EMERG "qlogicpti%d: Cannot reset the ISP.", qpti->qpti_id);
  435. return 1;
  436. }
  437. sbus_writew(HCCTRL_RESET, qpti->qregs + HCCTRL);
  438. mdelay(1);
  439. sbus_writew((SBUS_CTRL_GENAB | SBUS_CTRL_ERIRQ), qpti->qregs + SBUS_CTRL);
  440. set_sbus_cfg1(qpti);
  441. sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
  442. if (sbus_readw(qpti->qregs + RISC_PSR) & RISC_PSR_ULTRA) {
  443. qpti->ultra = 1;
  444. sbus_writew((RISC_MTREG_P0ULTRA | RISC_MTREG_P1ULTRA),
  445. qpti->qregs + RISC_MTREG);
  446. } else {
  447. qpti->ultra = 0;
  448. sbus_writew((RISC_MTREG_P0DFLT | RISC_MTREG_P1DFLT),
  449. qpti->qregs + RISC_MTREG);
  450. }
  451. sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
  452. /* Pin lines are only stable while RISC is paused. */
  453. sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
  454. if (sbus_readw(qpti->qregs + CPU_PDIFF) & CPU_PDIFF_MODE)
  455. qpti->differential = 1;
  456. else
  457. qpti->differential = 0;
  458. sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
  459. /* This shouldn't be necessary- we've reset things so we should be
  460. running from the ROM now.. */
  461. param[0] = MBOX_STOP_FIRMWARE;
  462. param[1] = param[2] = param[3] = param[4] = param[5] = 0;
  463. if (qlogicpti_mbox_command(qpti, param, 1)) {
  464. printk(KERN_EMERG "qlogicpti%d: Cannot stop firmware for reload.\n",
  465. qpti->qpti_id);
  466. spin_unlock_irqrestore(host->host_lock, flags);
  467. return 1;
  468. }
  469. /* Load it up.. */
  470. for (i = 0; i < risc_code_length; i++) {
  471. param[0] = MBOX_WRITE_RAM_WORD;
  472. param[1] = risc_code_addr + i;
  473. param[2] = risc_code[i];
  474. if (qlogicpti_mbox_command(qpti, param, 1) ||
  475. param[0] != MBOX_COMMAND_COMPLETE) {
  476. printk("qlogicpti%d: Firmware dload failed, I'm bolixed!\n",
  477. qpti->qpti_id);
  478. spin_unlock_irqrestore(host->host_lock, flags);
  479. return 1;
  480. }
  481. }
  482. /* Reset the ISP again. */
  483. sbus_writew(HCCTRL_RESET, qpti->qregs + HCCTRL);
  484. mdelay(1);
  485. qlogicpti_enable_irqs(qpti);
  486. sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
  487. sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
  488. /* Ask ISP to verify the checksum of the new code. */
  489. param[0] = MBOX_VERIFY_CHECKSUM;
  490. param[1] = risc_code_addr;
  491. if (qlogicpti_mbox_command(qpti, param, 1) ||
  492. (param[0] != MBOX_COMMAND_COMPLETE)) {
  493. printk(KERN_EMERG "qlogicpti%d: New firmware csum failure!\n",
  494. qpti->qpti_id);
  495. spin_unlock_irqrestore(host->host_lock, flags);
  496. return 1;
  497. }
  498. /* Start using newly downloaded firmware. */
  499. param[0] = MBOX_EXEC_FIRMWARE;
  500. param[1] = risc_code_addr;
  501. qlogicpti_mbox_command(qpti, param, 1);
  502. param[0] = MBOX_ABOUT_FIRMWARE;
  503. if (qlogicpti_mbox_command(qpti, param, 1) ||
  504. (param[0] != MBOX_COMMAND_COMPLETE)) {
  505. printk(KERN_EMERG "qlogicpti%d: AboutFirmware cmd fails.\n",
  506. qpti->qpti_id);
  507. spin_unlock_irqrestore(host->host_lock, flags);
  508. return 1;
  509. }
  510. /* Snag the major and minor revisions from the result. */
  511. qpti->fware_majrev = param[1];
  512. qpti->fware_minrev = param[2];
  513. qpti->fware_micrev = param[3];
  514. /* Set the clock rate */
  515. param[0] = MBOX_SET_CLOCK_RATE;
  516. param[1] = qpti->clock;
  517. if (qlogicpti_mbox_command(qpti, param, 1) ||
  518. (param[0] != MBOX_COMMAND_COMPLETE)) {
  519. printk(KERN_EMERG "qlogicpti%d: could not set clock rate.\n",
  520. qpti->qpti_id);
  521. spin_unlock_irqrestore(host->host_lock, flags);
  522. return 1;
  523. }
  524. if (qpti->is_pti != 0) {
  525. /* Load scsi initiator ID and interrupt level into sbus static ram. */
  526. param[0] = MBOX_WRITE_RAM_WORD;
  527. param[1] = 0xff80;
  528. param[2] = (unsigned short) qpti->scsi_id;
  529. qlogicpti_mbox_command(qpti, param, 1);
  530. param[0] = MBOX_WRITE_RAM_WORD;
  531. param[1] = 0xff00;
  532. param[2] = (unsigned short) 3;
  533. qlogicpti_mbox_command(qpti, param, 1);
  534. }
  535. spin_unlock_irqrestore(host->host_lock, flags);
  536. return 0;
  537. }
  538. static int qlogicpti_verify_tmon(struct qlogicpti *qpti)
  539. {
  540. int curstat = sbus_readb(qpti->sreg);
  541. curstat &= 0xf0;
  542. if (!(curstat & SREG_FUSE) && (qpti->swsreg & SREG_FUSE))
  543. printk("qlogicpti%d: Fuse returned to normal state.\n", qpti->qpti_id);
  544. if (!(curstat & SREG_TPOWER) && (qpti->swsreg & SREG_TPOWER))
  545. printk("qlogicpti%d: termpwr back to normal state.\n", qpti->qpti_id);
  546. if (curstat != qpti->swsreg) {
  547. int error = 0;
  548. if (curstat & SREG_FUSE) {
  549. error++;
  550. printk("qlogicpti%d: Fuse is open!\n", qpti->qpti_id);
  551. }
  552. if (curstat & SREG_TPOWER) {
  553. error++;
  554. printk("qlogicpti%d: termpwr failure\n", qpti->qpti_id);
  555. }
  556. if (qpti->differential &&
  557. (curstat & SREG_DSENSE) != SREG_DSENSE) {
  558. error++;
  559. printk("qlogicpti%d: You have a single ended device on a "
  560. "differential bus! Please fix!\n", qpti->qpti_id);
  561. }
  562. qpti->swsreg = curstat;
  563. return error;
  564. }
  565. return 0;
  566. }
  567. static irqreturn_t qpti_intr(int irq, void *dev_id);
  568. static void __devinit qpti_chain_add(struct qlogicpti *qpti)
  569. {
  570. spin_lock_irq(&qptichain_lock);
  571. if (qptichain != NULL) {
  572. struct qlogicpti *qlink = qptichain;
  573. while(qlink->next)
  574. qlink = qlink->next;
  575. qlink->next = qpti;
  576. } else {
  577. qptichain = qpti;
  578. }
  579. qpti->next = NULL;
  580. spin_unlock_irq(&qptichain_lock);
  581. }
  582. static void __devexit qpti_chain_del(struct qlogicpti *qpti)
  583. {
  584. spin_lock_irq(&qptichain_lock);
  585. if (qptichain == qpti) {
  586. qptichain = qpti->next;
  587. } else {
  588. struct qlogicpti *qlink = qptichain;
  589. while(qlink->next != qpti)
  590. qlink = qlink->next;
  591. qlink->next = qpti->next;
  592. }
  593. qpti->next = NULL;
  594. spin_unlock_irq(&qptichain_lock);
  595. }
  596. static int __devinit qpti_map_regs(struct qlogicpti *qpti)
  597. {
  598. struct sbus_dev *sdev = qpti->sdev;
  599. qpti->qregs = sbus_ioremap(&sdev->resource[0], 0,
  600. sdev->reg_addrs[0].reg_size,
  601. "PTI Qlogic/ISP");
  602. if (!qpti->qregs) {
  603. printk("PTI: Qlogic/ISP registers are unmappable\n");
  604. return -1;
  605. }
  606. if (qpti->is_pti) {
  607. qpti->sreg = sbus_ioremap(&sdev->resource[0], (16 * 4096),
  608. sizeof(unsigned char),
  609. "PTI Qlogic/ISP statreg");
  610. if (!qpti->sreg) {
  611. printk("PTI: Qlogic/ISP status register is unmappable\n");
  612. return -1;
  613. }
  614. }
  615. return 0;
  616. }
  617. static int __devinit qpti_register_irq(struct qlogicpti *qpti)
  618. {
  619. struct sbus_dev *sdev = qpti->sdev;
  620. qpti->qhost->irq = qpti->irq = sdev->irqs[0];
  621. /* We used to try various overly-clever things to
  622. * reduce the interrupt processing overhead on
  623. * sun4c/sun4m when multiple PTI's shared the
  624. * same IRQ. It was too complex and messy to
  625. * sanely maintain.
  626. */
  627. if (request_irq(qpti->irq, qpti_intr,
  628. IRQF_SHARED, "Qlogic/PTI", qpti))
  629. goto fail;
  630. printk("qlogicpti%d: IRQ %d ", qpti->qpti_id, qpti->irq);
  631. return 0;
  632. fail:
  633. printk("qlogicpti%d: Cannot acquire irq line\n", qpti->qpti_id);
  634. return -1;
  635. }
  636. static void __devinit qpti_get_scsi_id(struct qlogicpti *qpti)
  637. {
  638. qpti->scsi_id = prom_getintdefault(qpti->prom_node,
  639. "initiator-id",
  640. -1);
  641. if (qpti->scsi_id == -1)
  642. qpti->scsi_id = prom_getintdefault(qpti->prom_node,
  643. "scsi-initiator-id",
  644. -1);
  645. if (qpti->scsi_id == -1)
  646. qpti->scsi_id =
  647. prom_getintdefault(qpti->sdev->bus->prom_node,
  648. "scsi-initiator-id", 7);
  649. qpti->qhost->this_id = qpti->scsi_id;
  650. qpti->qhost->max_sectors = 64;
  651. printk("SCSI ID %d ", qpti->scsi_id);
  652. }
  653. static void qpti_get_bursts(struct qlogicpti *qpti)
  654. {
  655. struct sbus_dev *sdev = qpti->sdev;
  656. u8 bursts, bmask;
  657. bursts = prom_getintdefault(qpti->prom_node, "burst-sizes", 0xff);
  658. bmask = prom_getintdefault(sdev->bus->prom_node,
  659. "burst-sizes", 0xff);
  660. if (bmask != 0xff)
  661. bursts &= bmask;
  662. if (bursts == 0xff ||
  663. (bursts & DMA_BURST16) == 0 ||
  664. (bursts & DMA_BURST32) == 0)
  665. bursts = (DMA_BURST32 - 1);
  666. qpti->bursts = bursts;
  667. }
  668. static void qpti_get_clock(struct qlogicpti *qpti)
  669. {
  670. unsigned int cfreq;
  671. /* Check for what the clock input to this card is.
  672. * Default to 40Mhz.
  673. */
  674. cfreq = prom_getintdefault(qpti->prom_node,"clock-frequency",40000000);
  675. qpti->clock = (cfreq + 500000)/1000000;
  676. if (qpti->clock == 0) /* bullshit */
  677. qpti->clock = 40;
  678. }
  679. /* The request and response queues must each be aligned
  680. * on a page boundary.
  681. */
  682. static int __devinit qpti_map_queues(struct qlogicpti *qpti)
  683. {
  684. struct sbus_dev *sdev = qpti->sdev;
  685. #define QSIZE(entries) (((entries) + 1) * QUEUE_ENTRY_LEN)
  686. qpti->res_cpu = dma_alloc_coherent(&sdev->ofdev.dev,
  687. QSIZE(RES_QUEUE_LEN),
  688. &qpti->res_dvma, GFP_ATOMIC);
  689. if (qpti->res_cpu == NULL ||
  690. qpti->res_dvma == 0) {
  691. printk("QPTI: Cannot map response queue.\n");
  692. return -1;
  693. }
  694. qpti->req_cpu = dma_alloc_coherent(&sdev->ofdev.dev,
  695. QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
  696. &qpti->req_dvma, GFP_ATOMIC);
  697. if (qpti->req_cpu == NULL ||
  698. qpti->req_dvma == 0) {
  699. dma_free_coherent(&sdev->ofdev.dev, QSIZE(RES_QUEUE_LEN),
  700. qpti->res_cpu, qpti->res_dvma);
  701. printk("QPTI: Cannot map request queue.\n");
  702. return -1;
  703. }
  704. memset(qpti->res_cpu, 0, QSIZE(RES_QUEUE_LEN));
  705. memset(qpti->req_cpu, 0, QSIZE(QLOGICPTI_REQ_QUEUE_LEN));
  706. return 0;
  707. }
  708. const char *qlogicpti_info(struct Scsi_Host *host)
  709. {
  710. static char buf[80];
  711. struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  712. sprintf(buf, "PTI Qlogic,ISP SBUS SCSI irq %d regs at %p",
  713. qpti->qhost->irq, qpti->qregs);
  714. return buf;
  715. }
  716. /* I am a certified frobtronicist. */
  717. static inline void marker_frob(struct Command_Entry *cmd)
  718. {
  719. struct Marker_Entry *marker = (struct Marker_Entry *) cmd;
  720. memset(marker, 0, sizeof(struct Marker_Entry));
  721. marker->hdr.entry_cnt = 1;
  722. marker->hdr.entry_type = ENTRY_MARKER;
  723. marker->modifier = SYNC_ALL;
  724. marker->rsvd = 0;
  725. }
  726. static inline void cmd_frob(struct Command_Entry *cmd, struct scsi_cmnd *Cmnd,
  727. struct qlogicpti *qpti)
  728. {
  729. memset(cmd, 0, sizeof(struct Command_Entry));
  730. cmd->hdr.entry_cnt = 1;
  731. cmd->hdr.entry_type = ENTRY_COMMAND;
  732. cmd->target_id = Cmnd->device->id;
  733. cmd->target_lun = Cmnd->device->lun;
  734. cmd->cdb_length = Cmnd->cmd_len;
  735. cmd->control_flags = 0;
  736. if (Cmnd->device->tagged_supported) {
  737. if (qpti->cmd_count[Cmnd->device->id] == 0)
  738. qpti->tag_ages[Cmnd->device->id] = jiffies;
  739. if (time_after(jiffies, qpti->tag_ages[Cmnd->device->id] + (5*HZ))) {
  740. cmd->control_flags = CFLAG_ORDERED_TAG;
  741. qpti->tag_ages[Cmnd->device->id] = jiffies;
  742. } else
  743. cmd->control_flags = CFLAG_SIMPLE_TAG;
  744. }
  745. if ((Cmnd->cmnd[0] == WRITE_6) ||
  746. (Cmnd->cmnd[0] == WRITE_10) ||
  747. (Cmnd->cmnd[0] == WRITE_12))
  748. cmd->control_flags |= CFLAG_WRITE;
  749. else
  750. cmd->control_flags |= CFLAG_READ;
  751. cmd->time_out = 30;
  752. memcpy(cmd->cdb, Cmnd->cmnd, Cmnd->cmd_len);
  753. }
  754. /* Do it to it baby. */
  755. static inline int load_cmd(struct scsi_cmnd *Cmnd, struct Command_Entry *cmd,
  756. struct qlogicpti *qpti, u_int in_ptr, u_int out_ptr)
  757. {
  758. struct dataseg *ds;
  759. struct scatterlist *sg, *s;
  760. int i, n;
  761. if (scsi_bufflen(Cmnd)) {
  762. int sg_count;
  763. sg = scsi_sglist(Cmnd);
  764. sg_count = dma_map_sg(&qpti->sdev->ofdev.dev, sg,
  765. scsi_sg_count(Cmnd),
  766. Cmnd->sc_data_direction);
  767. ds = cmd->dataseg;
  768. cmd->segment_cnt = sg_count;
  769. /* Fill in first four sg entries: */
  770. n = sg_count;
  771. if (n > 4)
  772. n = 4;
  773. for_each_sg(sg, s, n, i) {
  774. ds[i].d_base = sg_dma_address(s);
  775. ds[i].d_count = sg_dma_len(s);
  776. }
  777. sg_count -= 4;
  778. sg = s;
  779. while (sg_count > 0) {
  780. struct Continuation_Entry *cont;
  781. ++cmd->hdr.entry_cnt;
  782. cont = (struct Continuation_Entry *) &qpti->req_cpu[in_ptr];
  783. in_ptr = NEXT_REQ_PTR(in_ptr);
  784. if (in_ptr == out_ptr)
  785. return -1;
  786. cont->hdr.entry_type = ENTRY_CONTINUATION;
  787. cont->hdr.entry_cnt = 0;
  788. cont->hdr.sys_def_1 = 0;
  789. cont->hdr.flags = 0;
  790. cont->reserved = 0;
  791. ds = cont->dataseg;
  792. n = sg_count;
  793. if (n > 7)
  794. n = 7;
  795. for_each_sg(sg, s, n, i) {
  796. ds[i].d_base = sg_dma_address(s);
  797. ds[i].d_count = sg_dma_len(s);
  798. }
  799. sg_count -= n;
  800. }
  801. } else {
  802. cmd->dataseg[0].d_base = 0;
  803. cmd->dataseg[0].d_count = 0;
  804. cmd->segment_cnt = 1; /* Shouldn't this be 0? */
  805. }
  806. /* Committed, record Scsi_Cmd so we can find it later. */
  807. cmd->handle = in_ptr;
  808. qpti->cmd_slots[in_ptr] = Cmnd;
  809. qpti->cmd_count[Cmnd->device->id]++;
  810. sbus_writew(in_ptr, qpti->qregs + MBOX4);
  811. qpti->req_in_ptr = in_ptr;
  812. return in_ptr;
  813. }
  814. static inline void update_can_queue(struct Scsi_Host *host, u_int in_ptr, u_int out_ptr)
  815. {
  816. /* Temporary workaround until bug is found and fixed (one bug has been found
  817. already, but fixing it makes things even worse) -jj */
  818. int num_free = QLOGICPTI_REQ_QUEUE_LEN - REQ_QUEUE_DEPTH(in_ptr, out_ptr) - 64;
  819. host->can_queue = host->host_busy + num_free;
  820. host->sg_tablesize = QLOGICPTI_MAX_SG(num_free);
  821. }
  822. static int qlogicpti_slave_configure(struct scsi_device *sdev)
  823. {
  824. struct qlogicpti *qpti = shost_priv(sdev->host);
  825. int tgt = sdev->id;
  826. u_short param[6];
  827. /* tags handled in midlayer */
  828. /* enable sync mode? */
  829. if (sdev->sdtr) {
  830. qpti->dev_param[tgt].device_flags |= 0x10;
  831. } else {
  832. qpti->dev_param[tgt].synchronous_offset = 0;
  833. qpti->dev_param[tgt].synchronous_period = 0;
  834. }
  835. /* are we wide capable? */
  836. if (sdev->wdtr)
  837. qpti->dev_param[tgt].device_flags |= 0x20;
  838. param[0] = MBOX_SET_TARGET_PARAMS;
  839. param[1] = (tgt << 8);
  840. param[2] = (qpti->dev_param[tgt].device_flags << 8);
  841. if (qpti->dev_param[tgt].device_flags & 0x10) {
  842. param[3] = (qpti->dev_param[tgt].synchronous_offset << 8) |
  843. qpti->dev_param[tgt].synchronous_period;
  844. } else {
  845. param[3] = 0;
  846. }
  847. qlogicpti_mbox_command(qpti, param, 0);
  848. return 0;
  849. }
  850. /*
  851. * The middle SCSI layer ensures that queuecommand never gets invoked
  852. * concurrently with itself or the interrupt handler (though the
  853. * interrupt handler may call this routine as part of
  854. * request-completion handling).
  855. *
  856. * "This code must fly." -davem
  857. */
  858. static int qlogicpti_queuecommand(struct scsi_cmnd *Cmnd, void (*done)(struct scsi_cmnd *))
  859. {
  860. struct Scsi_Host *host = Cmnd->device->host;
  861. struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  862. struct Command_Entry *cmd;
  863. u_int out_ptr;
  864. int in_ptr;
  865. Cmnd->scsi_done = done;
  866. in_ptr = qpti->req_in_ptr;
  867. cmd = (struct Command_Entry *) &qpti->req_cpu[in_ptr];
  868. out_ptr = sbus_readw(qpti->qregs + MBOX4);
  869. in_ptr = NEXT_REQ_PTR(in_ptr);
  870. if (in_ptr == out_ptr)
  871. goto toss_command;
  872. if (qpti->send_marker) {
  873. marker_frob(cmd);
  874. qpti->send_marker = 0;
  875. if (NEXT_REQ_PTR(in_ptr) == out_ptr) {
  876. sbus_writew(in_ptr, qpti->qregs + MBOX4);
  877. qpti->req_in_ptr = in_ptr;
  878. goto toss_command;
  879. }
  880. cmd = (struct Command_Entry *) &qpti->req_cpu[in_ptr];
  881. in_ptr = NEXT_REQ_PTR(in_ptr);
  882. }
  883. cmd_frob(cmd, Cmnd, qpti);
  884. if ((in_ptr = load_cmd(Cmnd, cmd, qpti, in_ptr, out_ptr)) == -1)
  885. goto toss_command;
  886. update_can_queue(host, in_ptr, out_ptr);
  887. return 0;
  888. toss_command:
  889. printk(KERN_EMERG "qlogicpti%d: request queue overflow\n",
  890. qpti->qpti_id);
  891. /* Unfortunately, unless you use the new EH code, which
  892. * we don't, the midlayer will ignore the return value,
  893. * which is insane. We pick up the pieces like this.
  894. */
  895. Cmnd->result = DID_BUS_BUSY;
  896. done(Cmnd);
  897. return 1;
  898. }
  899. static int qlogicpti_return_status(struct Status_Entry *sts, int id)
  900. {
  901. int host_status = DID_ERROR;
  902. switch (sts->completion_status) {
  903. case CS_COMPLETE:
  904. host_status = DID_OK;
  905. break;
  906. case CS_INCOMPLETE:
  907. if (!(sts->state_flags & SF_GOT_BUS))
  908. host_status = DID_NO_CONNECT;
  909. else if (!(sts->state_flags & SF_GOT_TARGET))
  910. host_status = DID_BAD_TARGET;
  911. else if (!(sts->state_flags & SF_SENT_CDB))
  912. host_status = DID_ERROR;
  913. else if (!(sts->state_flags & SF_TRANSFERRED_DATA))
  914. host_status = DID_ERROR;
  915. else if (!(sts->state_flags & SF_GOT_STATUS))
  916. host_status = DID_ERROR;
  917. else if (!(sts->state_flags & SF_GOT_SENSE))
  918. host_status = DID_ERROR;
  919. break;
  920. case CS_DMA_ERROR:
  921. case CS_TRANSPORT_ERROR:
  922. host_status = DID_ERROR;
  923. break;
  924. case CS_RESET_OCCURRED:
  925. case CS_BUS_RESET:
  926. host_status = DID_RESET;
  927. break;
  928. case CS_ABORTED:
  929. host_status = DID_ABORT;
  930. break;
  931. case CS_TIMEOUT:
  932. host_status = DID_TIME_OUT;
  933. break;
  934. case CS_DATA_OVERRUN:
  935. case CS_COMMAND_OVERRUN:
  936. case CS_STATUS_OVERRUN:
  937. case CS_BAD_MESSAGE:
  938. case CS_NO_MESSAGE_OUT:
  939. case CS_EXT_ID_FAILED:
  940. case CS_IDE_MSG_FAILED:
  941. case CS_ABORT_MSG_FAILED:
  942. case CS_NOP_MSG_FAILED:
  943. case CS_PARITY_ERROR_MSG_FAILED:
  944. case CS_DEVICE_RESET_MSG_FAILED:
  945. case CS_ID_MSG_FAILED:
  946. case CS_UNEXP_BUS_FREE:
  947. host_status = DID_ERROR;
  948. break;
  949. case CS_DATA_UNDERRUN:
  950. host_status = DID_OK;
  951. break;
  952. default:
  953. printk(KERN_EMERG "qlogicpti%d: unknown completion status 0x%04x\n",
  954. id, sts->completion_status);
  955. host_status = DID_ERROR;
  956. break;
  957. }
  958. return (sts->scsi_status & STATUS_MASK) | (host_status << 16);
  959. }
  960. static struct scsi_cmnd *qlogicpti_intr_handler(struct qlogicpti *qpti)
  961. {
  962. struct scsi_cmnd *Cmnd, *done_queue = NULL;
  963. struct Status_Entry *sts;
  964. u_int in_ptr, out_ptr;
  965. if (!(sbus_readw(qpti->qregs + SBUS_STAT) & SBUS_STAT_RINT))
  966. return NULL;
  967. in_ptr = sbus_readw(qpti->qregs + MBOX5);
  968. sbus_writew(HCCTRL_CRIRQ, qpti->qregs + HCCTRL);
  969. if (sbus_readw(qpti->qregs + SBUS_SEMAPHORE) & SBUS_SEMAPHORE_LCK) {
  970. switch (sbus_readw(qpti->qregs + MBOX0)) {
  971. case ASYNC_SCSI_BUS_RESET:
  972. case EXECUTION_TIMEOUT_RESET:
  973. qpti->send_marker = 1;
  974. break;
  975. case INVALID_COMMAND:
  976. case HOST_INTERFACE_ERROR:
  977. case COMMAND_ERROR:
  978. case COMMAND_PARAM_ERROR:
  979. break;
  980. };
  981. sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
  982. }
  983. /* This looks like a network driver! */
  984. out_ptr = qpti->res_out_ptr;
  985. while (out_ptr != in_ptr) {
  986. u_int cmd_slot;
  987. sts = (struct Status_Entry *) &qpti->res_cpu[out_ptr];
  988. out_ptr = NEXT_RES_PTR(out_ptr);
  989. /* We store an index in the handle, not the pointer in
  990. * some form. This avoids problems due to the fact
  991. * that the handle provided is only 32-bits. -DaveM
  992. */
  993. cmd_slot = sts->handle;
  994. Cmnd = qpti->cmd_slots[cmd_slot];
  995. qpti->cmd_slots[cmd_slot] = NULL;
  996. if (sts->completion_status == CS_RESET_OCCURRED ||
  997. sts->completion_status == CS_ABORTED ||
  998. (sts->status_flags & STF_BUS_RESET))
  999. qpti->send_marker = 1;
  1000. if (sts->state_flags & SF_GOT_SENSE)
  1001. memcpy(Cmnd->sense_buffer, sts->req_sense_data,
  1002. SCSI_SENSE_BUFFERSIZE);
  1003. if (sts->hdr.entry_type == ENTRY_STATUS)
  1004. Cmnd->result =
  1005. qlogicpti_return_status(sts, qpti->qpti_id);
  1006. else
  1007. Cmnd->result = DID_ERROR << 16;
  1008. if (scsi_bufflen(Cmnd))
  1009. dma_unmap_sg(&qpti->sdev->ofdev.dev,
  1010. scsi_sglist(Cmnd), scsi_sg_count(Cmnd),
  1011. Cmnd->sc_data_direction);
  1012. qpti->cmd_count[Cmnd->device->id]--;
  1013. sbus_writew(out_ptr, qpti->qregs + MBOX5);
  1014. Cmnd->host_scribble = (unsigned char *) done_queue;
  1015. done_queue = Cmnd;
  1016. }
  1017. qpti->res_out_ptr = out_ptr;
  1018. return done_queue;
  1019. }
  1020. static irqreturn_t qpti_intr(int irq, void *dev_id)
  1021. {
  1022. struct qlogicpti *qpti = dev_id;
  1023. unsigned long flags;
  1024. struct scsi_cmnd *dq;
  1025. spin_lock_irqsave(qpti->qhost->host_lock, flags);
  1026. dq = qlogicpti_intr_handler(qpti);
  1027. if (dq != NULL) {
  1028. do {
  1029. struct scsi_cmnd *next;
  1030. next = (struct scsi_cmnd *) dq->host_scribble;
  1031. dq->scsi_done(dq);
  1032. dq = next;
  1033. } while (dq != NULL);
  1034. }
  1035. spin_unlock_irqrestore(qpti->qhost->host_lock, flags);
  1036. return IRQ_HANDLED;
  1037. }
  1038. static int qlogicpti_abort(struct scsi_cmnd *Cmnd)
  1039. {
  1040. u_short param[6];
  1041. struct Scsi_Host *host = Cmnd->device->host;
  1042. struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  1043. int return_status = SUCCESS;
  1044. u32 cmd_cookie;
  1045. int i;
  1046. printk(KERN_WARNING "qlogicpti%d: Aborting cmd for tgt[%d] lun[%d]\n",
  1047. qpti->qpti_id, (int)Cmnd->device->id, (int)Cmnd->device->lun);
  1048. qlogicpti_disable_irqs(qpti);
  1049. /* Find the 32-bit cookie we gave to the firmware for
  1050. * this command.
  1051. */
  1052. for (i = 0; i < QLOGICPTI_REQ_QUEUE_LEN + 1; i++)
  1053. if (qpti->cmd_slots[i] == Cmnd)
  1054. break;
  1055. cmd_cookie = i;
  1056. param[0] = MBOX_ABORT;
  1057. param[1] = (((u_short) Cmnd->device->id) << 8) | Cmnd->device->lun;
  1058. param[2] = cmd_cookie >> 16;
  1059. param[3] = cmd_cookie & 0xffff;
  1060. if (qlogicpti_mbox_command(qpti, param, 0) ||
  1061. (param[0] != MBOX_COMMAND_COMPLETE)) {
  1062. printk(KERN_EMERG "qlogicpti%d: scsi abort failure: %x\n",
  1063. qpti->qpti_id, param[0]);
  1064. return_status = FAILED;
  1065. }
  1066. qlogicpti_enable_irqs(qpti);
  1067. return return_status;
  1068. }
  1069. static int qlogicpti_reset(struct scsi_cmnd *Cmnd)
  1070. {
  1071. u_short param[6];
  1072. struct Scsi_Host *host = Cmnd->device->host;
  1073. struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  1074. int return_status = SUCCESS;
  1075. printk(KERN_WARNING "qlogicpti%d: Resetting SCSI bus!\n",
  1076. qpti->qpti_id);
  1077. qlogicpti_disable_irqs(qpti);
  1078. param[0] = MBOX_BUS_RESET;
  1079. param[1] = qpti->host_param.bus_reset_delay;
  1080. if (qlogicpti_mbox_command(qpti, param, 0) ||
  1081. (param[0] != MBOX_COMMAND_COMPLETE)) {
  1082. printk(KERN_EMERG "qlogicisp%d: scsi bus reset failure: %x\n",
  1083. qpti->qpti_id, param[0]);
  1084. return_status = FAILED;
  1085. }
  1086. qlogicpti_enable_irqs(qpti);
  1087. return return_status;
  1088. }
  1089. static struct scsi_host_template qpti_template = {
  1090. .module = THIS_MODULE,
  1091. .name = "qlogicpti",
  1092. .info = qlogicpti_info,
  1093. .queuecommand = qlogicpti_queuecommand,
  1094. .slave_configure = qlogicpti_slave_configure,
  1095. .eh_abort_handler = qlogicpti_abort,
  1096. .eh_bus_reset_handler = qlogicpti_reset,
  1097. .can_queue = QLOGICPTI_REQ_QUEUE_LEN,
  1098. .this_id = 7,
  1099. .sg_tablesize = QLOGICPTI_MAX_SG(QLOGICPTI_REQ_QUEUE_LEN),
  1100. .cmd_per_lun = 1,
  1101. .use_clustering = ENABLE_CLUSTERING,
  1102. };
  1103. static int __devinit qpti_sbus_probe(struct of_device *dev, const struct of_device_id *match)
  1104. {
  1105. static int nqptis;
  1106. struct sbus_dev *sdev = to_sbus_device(&dev->dev);
  1107. struct device_node *dp = dev->node;
  1108. struct scsi_host_template *tpnt = match->data;
  1109. struct Scsi_Host *host;
  1110. struct qlogicpti *qpti;
  1111. const char *fcode;
  1112. /* Sometimes Antares cards come up not completely
  1113. * setup, and we get a report of a zero IRQ.
  1114. */
  1115. if (sdev->irqs[0] == 0)
  1116. return -ENODEV;
  1117. host = scsi_host_alloc(tpnt, sizeof(struct qlogicpti));
  1118. if (!host)
  1119. return -ENOMEM;
  1120. qpti = (struct qlogicpti *) host->hostdata;
  1121. host->max_id = MAX_TARGETS;
  1122. qpti->qhost = host;
  1123. qpti->sdev = sdev;
  1124. qpti->qpti_id = nqptis;
  1125. qpti->prom_node = sdev->prom_node;
  1126. strcpy(qpti->prom_name, sdev->ofdev.node->name);
  1127. qpti->is_pti = strcmp(qpti->prom_name, "QLGC,isp");
  1128. if (qpti_map_regs(qpti) < 0)
  1129. goto fail_unlink;
  1130. if (qpti_register_irq(qpti) < 0)
  1131. goto fail_unmap_regs;
  1132. qpti_get_scsi_id(qpti);
  1133. qpti_get_bursts(qpti);
  1134. qpti_get_clock(qpti);
  1135. /* Clear out scsi_cmnd array. */
  1136. memset(qpti->cmd_slots, 0, sizeof(qpti->cmd_slots));
  1137. if (qpti_map_queues(qpti) < 0)
  1138. goto fail_free_irq;
  1139. /* Load the firmware. */
  1140. if (qlogicpti_load_firmware(qpti))
  1141. goto fail_unmap_queues;
  1142. if (qpti->is_pti) {
  1143. /* Check the PTI status reg. */
  1144. if (qlogicpti_verify_tmon(qpti))
  1145. goto fail_unmap_queues;
  1146. }
  1147. /* Reset the ISP and init res/req queues. */
  1148. if (qlogicpti_reset_hardware(host))
  1149. goto fail_unmap_queues;
  1150. printk("(Firmware v%d.%d.%d)", qpti->fware_majrev,
  1151. qpti->fware_minrev, qpti->fware_micrev);
  1152. fcode = of_get_property(dp, "isp-fcode", NULL);
  1153. if (fcode && fcode[0])
  1154. printk("(FCode %s)", fcode);
  1155. if (of_find_property(dp, "differential", NULL) != NULL)
  1156. qpti->differential = 1;
  1157. printk("\nqlogicpti%d: [%s Wide, using %s interface]\n",
  1158. qpti->qpti_id,
  1159. (qpti->ultra ? "Ultra" : "Fast"),
  1160. (qpti->differential ? "differential" : "single ended"));
  1161. if (scsi_add_host(host, &dev->dev)) {
  1162. printk("qlogicpti%d: Failed scsi_add_host\n", qpti->qpti_id);
  1163. goto fail_unmap_queues;
  1164. }
  1165. dev_set_drvdata(&sdev->ofdev.dev, qpti);
  1166. qpti_chain_add(qpti);
  1167. scsi_scan_host(host);
  1168. nqptis++;
  1169. return 0;
  1170. fail_unmap_queues:
  1171. #define QSIZE(entries) (((entries) + 1) * QUEUE_ENTRY_LEN)
  1172. dma_free_coherent(&qpti->sdev->ofdev.dev,
  1173. QSIZE(RES_QUEUE_LEN),
  1174. qpti->res_cpu, qpti->res_dvma);
  1175. dma_free_coherent(&qpti->sdev->ofdev.dev,
  1176. QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
  1177. qpti->req_cpu, qpti->req_dvma);
  1178. #undef QSIZE
  1179. fail_unmap_regs:
  1180. sbus_iounmap(qpti->qregs,
  1181. qpti->sdev->reg_addrs[0].reg_size);
  1182. if (qpti->is_pti)
  1183. sbus_iounmap(qpti->sreg, sizeof(unsigned char));
  1184. fail_free_irq:
  1185. free_irq(qpti->irq, qpti);
  1186. fail_unlink:
  1187. scsi_host_put(host);
  1188. return -ENODEV;
  1189. }
  1190. static int __devexit qpti_sbus_remove(struct of_device *dev)
  1191. {
  1192. struct qlogicpti *qpti = dev_get_drvdata(&dev->dev);
  1193. qpti_chain_del(qpti);
  1194. scsi_remove_host(qpti->qhost);
  1195. /* Shut up the card. */
  1196. sbus_writew(0, qpti->qregs + SBUS_CTRL);
  1197. /* Free IRQ handler and unmap Qlogic,ISP and PTI status regs. */
  1198. free_irq(qpti->irq, qpti);
  1199. #define QSIZE(entries) (((entries) + 1) * QUEUE_ENTRY_LEN)
  1200. dma_free_coherent(&qpti->sdev->ofdev.dev,
  1201. QSIZE(RES_QUEUE_LEN),
  1202. qpti->res_cpu, qpti->res_dvma);
  1203. dma_free_coherent(&qpti->sdev->ofdev.dev,
  1204. QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
  1205. qpti->req_cpu, qpti->req_dvma);
  1206. #undef QSIZE
  1207. sbus_iounmap(qpti->qregs, qpti->sdev->reg_addrs[0].reg_size);
  1208. if (qpti->is_pti)
  1209. sbus_iounmap(qpti->sreg, sizeof(unsigned char));
  1210. scsi_host_put(qpti->qhost);
  1211. return 0;
  1212. }
  1213. static struct of_device_id qpti_match[] = {
  1214. {
  1215. .name = "ptisp",
  1216. .data = &qpti_template,
  1217. },
  1218. {
  1219. .name = "PTI,ptisp",
  1220. .data = &qpti_template,
  1221. },
  1222. {
  1223. .name = "QLGC,isp",
  1224. .data = &qpti_template,
  1225. },
  1226. {
  1227. .name = "SUNW,isp",
  1228. .data = &qpti_template,
  1229. },
  1230. {},
  1231. };
  1232. MODULE_DEVICE_TABLE(of, qpti_match);
  1233. static struct of_platform_driver qpti_sbus_driver = {
  1234. .name = "qpti",
  1235. .match_table = qpti_match,
  1236. .probe = qpti_sbus_probe,
  1237. .remove = __devexit_p(qpti_sbus_remove),
  1238. };
  1239. static int __init qpti_init(void)
  1240. {
  1241. return of_register_driver(&qpti_sbus_driver, &sbus_bus_type);
  1242. }
  1243. static void __exit qpti_exit(void)
  1244. {
  1245. of_unregister_driver(&qpti_sbus_driver);
  1246. }
  1247. MODULE_DESCRIPTION("QlogicISP SBUS driver");
  1248. MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
  1249. MODULE_LICENSE("GPL");
  1250. MODULE_VERSION("2.0");
  1251. module_init(qpti_init);
  1252. module_exit(qpti_exit);