qlogicpti.c 40 KB

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