qlogicpti.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478
  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/gfp.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 <linux/firmware.h>
  31. #include <asm/byteorder.h>
  32. #include "qlogicpti.h"
  33. #include <asm/dma.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. 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() && (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 qlogicpti_load_firmware(struct qlogicpti *qpti)
  404. {
  405. const struct firmware *fw;
  406. const char fwname[] = "qlogic/isp1000.bin";
  407. const __le16 *fw_data;
  408. struct Scsi_Host *host = qpti->qhost;
  409. unsigned short csum = 0;
  410. unsigned short param[6];
  411. unsigned short risc_code_addr, risc_code_length;
  412. int err;
  413. unsigned long flags;
  414. int i, timeout;
  415. err = request_firmware(&fw, fwname, &qpti->op->dev);
  416. if (err)
  417. return err;
  418. if (fw->size % 2) {
  419. printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
  420. fw->size, fwname);
  421. err = -EINVAL;
  422. goto outfirm;
  423. }
  424. fw_data = (const __le16 *)&fw->data[0];
  425. risc_code_addr = 0x1000; /* all f/w modules load at 0x1000 */
  426. risc_code_length = fw->size / 2;
  427. spin_lock_irqsave(host->host_lock, flags);
  428. /* Verify the checksum twice, one before loading it, and once
  429. * afterwards via the mailbox commands.
  430. */
  431. for (i = 0; i < risc_code_length; i++)
  432. csum += __le16_to_cpu(fw_data[i]);
  433. if (csum) {
  434. printk(KERN_EMERG "qlogicpti%d: Aieee, firmware checksum failed!",
  435. qpti->qpti_id);
  436. err = 1;
  437. goto out;
  438. }
  439. sbus_writew(SBUS_CTRL_RESET, qpti->qregs + SBUS_CTRL);
  440. sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + CMD_DMA_CTRL);
  441. sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + DATA_DMA_CTRL);
  442. timeout = PTI_RESET_LIMIT;
  443. while (--timeout && (sbus_readw(qpti->qregs + SBUS_CTRL) & SBUS_CTRL_RESET))
  444. udelay(20);
  445. if (!timeout) {
  446. printk(KERN_EMERG "qlogicpti%d: Cannot reset the ISP.", qpti->qpti_id);
  447. err = 1;
  448. goto out;
  449. }
  450. sbus_writew(HCCTRL_RESET, qpti->qregs + HCCTRL);
  451. mdelay(1);
  452. sbus_writew((SBUS_CTRL_GENAB | SBUS_CTRL_ERIRQ), qpti->qregs + SBUS_CTRL);
  453. set_sbus_cfg1(qpti);
  454. sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
  455. if (sbus_readw(qpti->qregs + RISC_PSR) & RISC_PSR_ULTRA) {
  456. qpti->ultra = 1;
  457. sbus_writew((RISC_MTREG_P0ULTRA | RISC_MTREG_P1ULTRA),
  458. qpti->qregs + RISC_MTREG);
  459. } else {
  460. qpti->ultra = 0;
  461. sbus_writew((RISC_MTREG_P0DFLT | RISC_MTREG_P1DFLT),
  462. qpti->qregs + RISC_MTREG);
  463. }
  464. sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
  465. /* Pin lines are only stable while RISC is paused. */
  466. sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
  467. if (sbus_readw(qpti->qregs + CPU_PDIFF) & CPU_PDIFF_MODE)
  468. qpti->differential = 1;
  469. else
  470. qpti->differential = 0;
  471. sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
  472. /* This shouldn't be necessary- we've reset things so we should be
  473. running from the ROM now.. */
  474. param[0] = MBOX_STOP_FIRMWARE;
  475. param[1] = param[2] = param[3] = param[4] = param[5] = 0;
  476. if (qlogicpti_mbox_command(qpti, param, 1)) {
  477. printk(KERN_EMERG "qlogicpti%d: Cannot stop firmware for reload.\n",
  478. qpti->qpti_id);
  479. err = 1;
  480. goto out;
  481. }
  482. /* Load it up.. */
  483. for (i = 0; i < risc_code_length; i++) {
  484. param[0] = MBOX_WRITE_RAM_WORD;
  485. param[1] = risc_code_addr + i;
  486. param[2] = __le16_to_cpu(fw_data[i]);
  487. if (qlogicpti_mbox_command(qpti, param, 1) ||
  488. param[0] != MBOX_COMMAND_COMPLETE) {
  489. printk("qlogicpti%d: Firmware dload failed, I'm bolixed!\n",
  490. qpti->qpti_id);
  491. err = 1;
  492. goto out;
  493. }
  494. }
  495. /* Reset the ISP again. */
  496. sbus_writew(HCCTRL_RESET, qpti->qregs + HCCTRL);
  497. mdelay(1);
  498. qlogicpti_enable_irqs(qpti);
  499. sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
  500. sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
  501. /* Ask ISP to verify the checksum of the new code. */
  502. param[0] = MBOX_VERIFY_CHECKSUM;
  503. param[1] = risc_code_addr;
  504. if (qlogicpti_mbox_command(qpti, param, 1) ||
  505. (param[0] != MBOX_COMMAND_COMPLETE)) {
  506. printk(KERN_EMERG "qlogicpti%d: New firmware csum failure!\n",
  507. qpti->qpti_id);
  508. err = 1;
  509. goto out;
  510. }
  511. /* Start using newly downloaded firmware. */
  512. param[0] = MBOX_EXEC_FIRMWARE;
  513. param[1] = risc_code_addr;
  514. qlogicpti_mbox_command(qpti, param, 1);
  515. param[0] = MBOX_ABOUT_FIRMWARE;
  516. if (qlogicpti_mbox_command(qpti, param, 1) ||
  517. (param[0] != MBOX_COMMAND_COMPLETE)) {
  518. printk(KERN_EMERG "qlogicpti%d: AboutFirmware cmd fails.\n",
  519. qpti->qpti_id);
  520. err = 1;
  521. goto out;
  522. }
  523. /* Snag the major and minor revisions from the result. */
  524. qpti->fware_majrev = param[1];
  525. qpti->fware_minrev = param[2];
  526. qpti->fware_micrev = param[3];
  527. /* Set the clock rate */
  528. param[0] = MBOX_SET_CLOCK_RATE;
  529. param[1] = qpti->clock;
  530. if (qlogicpti_mbox_command(qpti, param, 1) ||
  531. (param[0] != MBOX_COMMAND_COMPLETE)) {
  532. printk(KERN_EMERG "qlogicpti%d: could not set clock rate.\n",
  533. qpti->qpti_id);
  534. err = 1;
  535. goto out;
  536. }
  537. if (qpti->is_pti != 0) {
  538. /* Load scsi initiator ID and interrupt level into sbus static ram. */
  539. param[0] = MBOX_WRITE_RAM_WORD;
  540. param[1] = 0xff80;
  541. param[2] = (unsigned short) qpti->scsi_id;
  542. qlogicpti_mbox_command(qpti, param, 1);
  543. param[0] = MBOX_WRITE_RAM_WORD;
  544. param[1] = 0xff00;
  545. param[2] = (unsigned short) 3;
  546. qlogicpti_mbox_command(qpti, param, 1);
  547. }
  548. out:
  549. spin_unlock_irqrestore(host->host_lock, flags);
  550. outfirm:
  551. release_firmware(fw);
  552. return err;
  553. }
  554. static int qlogicpti_verify_tmon(struct qlogicpti *qpti)
  555. {
  556. int curstat = sbus_readb(qpti->sreg);
  557. curstat &= 0xf0;
  558. if (!(curstat & SREG_FUSE) && (qpti->swsreg & SREG_FUSE))
  559. printk("qlogicpti%d: Fuse returned to normal state.\n", qpti->qpti_id);
  560. if (!(curstat & SREG_TPOWER) && (qpti->swsreg & SREG_TPOWER))
  561. printk("qlogicpti%d: termpwr back to normal state.\n", qpti->qpti_id);
  562. if (curstat != qpti->swsreg) {
  563. int error = 0;
  564. if (curstat & SREG_FUSE) {
  565. error++;
  566. printk("qlogicpti%d: Fuse is open!\n", qpti->qpti_id);
  567. }
  568. if (curstat & SREG_TPOWER) {
  569. error++;
  570. printk("qlogicpti%d: termpwr failure\n", qpti->qpti_id);
  571. }
  572. if (qpti->differential &&
  573. (curstat & SREG_DSENSE) != SREG_DSENSE) {
  574. error++;
  575. printk("qlogicpti%d: You have a single ended device on a "
  576. "differential bus! Please fix!\n", qpti->qpti_id);
  577. }
  578. qpti->swsreg = curstat;
  579. return error;
  580. }
  581. return 0;
  582. }
  583. static irqreturn_t qpti_intr(int irq, void *dev_id);
  584. static void qpti_chain_add(struct qlogicpti *qpti)
  585. {
  586. spin_lock_irq(&qptichain_lock);
  587. if (qptichain != NULL) {
  588. struct qlogicpti *qlink = qptichain;
  589. while(qlink->next)
  590. qlink = qlink->next;
  591. qlink->next = qpti;
  592. } else {
  593. qptichain = qpti;
  594. }
  595. qpti->next = NULL;
  596. spin_unlock_irq(&qptichain_lock);
  597. }
  598. static void qpti_chain_del(struct qlogicpti *qpti)
  599. {
  600. spin_lock_irq(&qptichain_lock);
  601. if (qptichain == qpti) {
  602. qptichain = qpti->next;
  603. } else {
  604. struct qlogicpti *qlink = qptichain;
  605. while(qlink->next != qpti)
  606. qlink = qlink->next;
  607. qlink->next = qpti->next;
  608. }
  609. qpti->next = NULL;
  610. spin_unlock_irq(&qptichain_lock);
  611. }
  612. static int qpti_map_regs(struct qlogicpti *qpti)
  613. {
  614. struct platform_device *op = qpti->op;
  615. qpti->qregs = of_ioremap(&op->resource[0], 0,
  616. resource_size(&op->resource[0]),
  617. "PTI Qlogic/ISP");
  618. if (!qpti->qregs) {
  619. printk("PTI: Qlogic/ISP registers are unmappable\n");
  620. return -ENODEV;
  621. }
  622. if (qpti->is_pti) {
  623. qpti->sreg = of_ioremap(&op->resource[0], (16 * 4096),
  624. sizeof(unsigned char),
  625. "PTI Qlogic/ISP statreg");
  626. if (!qpti->sreg) {
  627. printk("PTI: Qlogic/ISP status register is unmappable\n");
  628. return -ENODEV;
  629. }
  630. }
  631. return 0;
  632. }
  633. static int qpti_register_irq(struct qlogicpti *qpti)
  634. {
  635. struct platform_device *op = qpti->op;
  636. qpti->qhost->irq = qpti->irq = op->archdata.irqs[0];
  637. /* We used to try various overly-clever things to
  638. * reduce the interrupt processing overhead on
  639. * sun4c/sun4m when multiple PTI's shared the
  640. * same IRQ. It was too complex and messy to
  641. * sanely maintain.
  642. */
  643. if (request_irq(qpti->irq, qpti_intr,
  644. IRQF_SHARED, "QlogicPTI", qpti))
  645. goto fail;
  646. printk("qlogicpti%d: IRQ %d ", qpti->qpti_id, qpti->irq);
  647. return 0;
  648. fail:
  649. printk("qlogicpti%d: Cannot acquire irq line\n", qpti->qpti_id);
  650. return -1;
  651. }
  652. static void qpti_get_scsi_id(struct qlogicpti *qpti)
  653. {
  654. struct platform_device *op = qpti->op;
  655. struct device_node *dp;
  656. dp = op->dev.of_node;
  657. qpti->scsi_id = of_getintprop_default(dp, "initiator-id", -1);
  658. if (qpti->scsi_id == -1)
  659. qpti->scsi_id = of_getintprop_default(dp, "scsi-initiator-id",
  660. -1);
  661. if (qpti->scsi_id == -1)
  662. qpti->scsi_id =
  663. of_getintprop_default(dp->parent,
  664. "scsi-initiator-id", 7);
  665. qpti->qhost->this_id = qpti->scsi_id;
  666. qpti->qhost->max_sectors = 64;
  667. printk("SCSI ID %d ", qpti->scsi_id);
  668. }
  669. static void qpti_get_bursts(struct qlogicpti *qpti)
  670. {
  671. struct platform_device *op = qpti->op;
  672. u8 bursts, bmask;
  673. bursts = of_getintprop_default(op->dev.of_node, "burst-sizes", 0xff);
  674. bmask = of_getintprop_default(op->dev.of_node->parent, "burst-sizes", 0xff);
  675. if (bmask != 0xff)
  676. bursts &= bmask;
  677. if (bursts == 0xff ||
  678. (bursts & DMA_BURST16) == 0 ||
  679. (bursts & DMA_BURST32) == 0)
  680. bursts = (DMA_BURST32 - 1);
  681. qpti->bursts = bursts;
  682. }
  683. static void qpti_get_clock(struct qlogicpti *qpti)
  684. {
  685. unsigned int cfreq;
  686. /* Check for what the clock input to this card is.
  687. * Default to 40Mhz.
  688. */
  689. cfreq = prom_getintdefault(qpti->prom_node,"clock-frequency",40000000);
  690. qpti->clock = (cfreq + 500000)/1000000;
  691. if (qpti->clock == 0) /* bullshit */
  692. qpti->clock = 40;
  693. }
  694. /* The request and response queues must each be aligned
  695. * on a page boundary.
  696. */
  697. static int qpti_map_queues(struct qlogicpti *qpti)
  698. {
  699. struct platform_device *op = qpti->op;
  700. #define QSIZE(entries) (((entries) + 1) * QUEUE_ENTRY_LEN)
  701. qpti->res_cpu = dma_alloc_coherent(&op->dev,
  702. QSIZE(RES_QUEUE_LEN),
  703. &qpti->res_dvma, GFP_ATOMIC);
  704. if (qpti->res_cpu == NULL ||
  705. qpti->res_dvma == 0) {
  706. printk("QPTI: Cannot map response queue.\n");
  707. return -1;
  708. }
  709. qpti->req_cpu = dma_alloc_coherent(&op->dev,
  710. QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
  711. &qpti->req_dvma, GFP_ATOMIC);
  712. if (qpti->req_cpu == NULL ||
  713. qpti->req_dvma == 0) {
  714. dma_free_coherent(&op->dev, QSIZE(RES_QUEUE_LEN),
  715. qpti->res_cpu, qpti->res_dvma);
  716. printk("QPTI: Cannot map request queue.\n");
  717. return -1;
  718. }
  719. memset(qpti->res_cpu, 0, QSIZE(RES_QUEUE_LEN));
  720. memset(qpti->req_cpu, 0, QSIZE(QLOGICPTI_REQ_QUEUE_LEN));
  721. return 0;
  722. }
  723. const char *qlogicpti_info(struct Scsi_Host *host)
  724. {
  725. static char buf[80];
  726. struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  727. sprintf(buf, "PTI Qlogic,ISP SBUS SCSI irq %d regs at %p",
  728. qpti->qhost->irq, qpti->qregs);
  729. return buf;
  730. }
  731. /* I am a certified frobtronicist. */
  732. static inline void marker_frob(struct Command_Entry *cmd)
  733. {
  734. struct Marker_Entry *marker = (struct Marker_Entry *) cmd;
  735. memset(marker, 0, sizeof(struct Marker_Entry));
  736. marker->hdr.entry_cnt = 1;
  737. marker->hdr.entry_type = ENTRY_MARKER;
  738. marker->modifier = SYNC_ALL;
  739. marker->rsvd = 0;
  740. }
  741. static inline void cmd_frob(struct Command_Entry *cmd, struct scsi_cmnd *Cmnd,
  742. struct qlogicpti *qpti)
  743. {
  744. memset(cmd, 0, sizeof(struct Command_Entry));
  745. cmd->hdr.entry_cnt = 1;
  746. cmd->hdr.entry_type = ENTRY_COMMAND;
  747. cmd->target_id = Cmnd->device->id;
  748. cmd->target_lun = Cmnd->device->lun;
  749. cmd->cdb_length = Cmnd->cmd_len;
  750. cmd->control_flags = 0;
  751. if (Cmnd->device->tagged_supported) {
  752. if (qpti->cmd_count[Cmnd->device->id] == 0)
  753. qpti->tag_ages[Cmnd->device->id] = jiffies;
  754. if (time_after(jiffies, qpti->tag_ages[Cmnd->device->id] + (5*HZ))) {
  755. cmd->control_flags = CFLAG_ORDERED_TAG;
  756. qpti->tag_ages[Cmnd->device->id] = jiffies;
  757. } else
  758. cmd->control_flags = CFLAG_SIMPLE_TAG;
  759. }
  760. if ((Cmnd->cmnd[0] == WRITE_6) ||
  761. (Cmnd->cmnd[0] == WRITE_10) ||
  762. (Cmnd->cmnd[0] == WRITE_12))
  763. cmd->control_flags |= CFLAG_WRITE;
  764. else
  765. cmd->control_flags |= CFLAG_READ;
  766. cmd->time_out = Cmnd->request->timeout/HZ;
  767. memcpy(cmd->cdb, Cmnd->cmnd, Cmnd->cmd_len);
  768. }
  769. /* Do it to it baby. */
  770. static inline int load_cmd(struct scsi_cmnd *Cmnd, struct Command_Entry *cmd,
  771. struct qlogicpti *qpti, u_int in_ptr, u_int out_ptr)
  772. {
  773. struct dataseg *ds;
  774. struct scatterlist *sg, *s;
  775. int i, n;
  776. if (scsi_bufflen(Cmnd)) {
  777. int sg_count;
  778. sg = scsi_sglist(Cmnd);
  779. sg_count = dma_map_sg(&qpti->op->dev, sg,
  780. scsi_sg_count(Cmnd),
  781. Cmnd->sc_data_direction);
  782. ds = cmd->dataseg;
  783. cmd->segment_cnt = sg_count;
  784. /* Fill in first four sg entries: */
  785. n = sg_count;
  786. if (n > 4)
  787. n = 4;
  788. for_each_sg(sg, s, n, i) {
  789. ds[i].d_base = sg_dma_address(s);
  790. ds[i].d_count = sg_dma_len(s);
  791. }
  792. sg_count -= 4;
  793. sg = s;
  794. while (sg_count > 0) {
  795. struct Continuation_Entry *cont;
  796. ++cmd->hdr.entry_cnt;
  797. cont = (struct Continuation_Entry *) &qpti->req_cpu[in_ptr];
  798. in_ptr = NEXT_REQ_PTR(in_ptr);
  799. if (in_ptr == out_ptr)
  800. return -1;
  801. cont->hdr.entry_type = ENTRY_CONTINUATION;
  802. cont->hdr.entry_cnt = 0;
  803. cont->hdr.sys_def_1 = 0;
  804. cont->hdr.flags = 0;
  805. cont->reserved = 0;
  806. ds = cont->dataseg;
  807. n = sg_count;
  808. if (n > 7)
  809. n = 7;
  810. for_each_sg(sg, s, n, i) {
  811. ds[i].d_base = sg_dma_address(s);
  812. ds[i].d_count = sg_dma_len(s);
  813. }
  814. sg_count -= n;
  815. sg = s;
  816. }
  817. } else {
  818. cmd->dataseg[0].d_base = 0;
  819. cmd->dataseg[0].d_count = 0;
  820. cmd->segment_cnt = 1; /* Shouldn't this be 0? */
  821. }
  822. /* Committed, record Scsi_Cmd so we can find it later. */
  823. cmd->handle = in_ptr;
  824. qpti->cmd_slots[in_ptr] = Cmnd;
  825. qpti->cmd_count[Cmnd->device->id]++;
  826. sbus_writew(in_ptr, qpti->qregs + MBOX4);
  827. qpti->req_in_ptr = in_ptr;
  828. return in_ptr;
  829. }
  830. static inline void update_can_queue(struct Scsi_Host *host, u_int in_ptr, u_int out_ptr)
  831. {
  832. /* Temporary workaround until bug is found and fixed (one bug has been found
  833. already, but fixing it makes things even worse) -jj */
  834. int num_free = QLOGICPTI_REQ_QUEUE_LEN - REQ_QUEUE_DEPTH(in_ptr, out_ptr) - 64;
  835. host->can_queue = scsi_host_busy(host) + num_free;
  836. host->sg_tablesize = QLOGICPTI_MAX_SG(num_free);
  837. }
  838. static int qlogicpti_slave_configure(struct scsi_device *sdev)
  839. {
  840. struct qlogicpti *qpti = shost_priv(sdev->host);
  841. int tgt = sdev->id;
  842. u_short param[6];
  843. /* tags handled in midlayer */
  844. /* enable sync mode? */
  845. if (sdev->sdtr) {
  846. qpti->dev_param[tgt].device_flags |= 0x10;
  847. } else {
  848. qpti->dev_param[tgt].synchronous_offset = 0;
  849. qpti->dev_param[tgt].synchronous_period = 0;
  850. }
  851. /* are we wide capable? */
  852. if (sdev->wdtr)
  853. qpti->dev_param[tgt].device_flags |= 0x20;
  854. param[0] = MBOX_SET_TARGET_PARAMS;
  855. param[1] = (tgt << 8);
  856. param[2] = (qpti->dev_param[tgt].device_flags << 8);
  857. if (qpti->dev_param[tgt].device_flags & 0x10) {
  858. param[3] = (qpti->dev_param[tgt].synchronous_offset << 8) |
  859. qpti->dev_param[tgt].synchronous_period;
  860. } else {
  861. param[3] = 0;
  862. }
  863. qlogicpti_mbox_command(qpti, param, 0);
  864. return 0;
  865. }
  866. /*
  867. * The middle SCSI layer ensures that queuecommand never gets invoked
  868. * concurrently with itself or the interrupt handler (though the
  869. * interrupt handler may call this routine as part of
  870. * request-completion handling).
  871. *
  872. * "This code must fly." -davem
  873. */
  874. static int qlogicpti_queuecommand_lck(struct scsi_cmnd *Cmnd, void (*done)(struct scsi_cmnd *))
  875. {
  876. struct Scsi_Host *host = Cmnd->device->host;
  877. struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  878. struct Command_Entry *cmd;
  879. u_int out_ptr;
  880. int in_ptr;
  881. Cmnd->scsi_done = done;
  882. in_ptr = qpti->req_in_ptr;
  883. cmd = (struct Command_Entry *) &qpti->req_cpu[in_ptr];
  884. out_ptr = sbus_readw(qpti->qregs + MBOX4);
  885. in_ptr = NEXT_REQ_PTR(in_ptr);
  886. if (in_ptr == out_ptr)
  887. goto toss_command;
  888. if (qpti->send_marker) {
  889. marker_frob(cmd);
  890. qpti->send_marker = 0;
  891. if (NEXT_REQ_PTR(in_ptr) == out_ptr) {
  892. sbus_writew(in_ptr, qpti->qregs + MBOX4);
  893. qpti->req_in_ptr = in_ptr;
  894. goto toss_command;
  895. }
  896. cmd = (struct Command_Entry *) &qpti->req_cpu[in_ptr];
  897. in_ptr = NEXT_REQ_PTR(in_ptr);
  898. }
  899. cmd_frob(cmd, Cmnd, qpti);
  900. if ((in_ptr = load_cmd(Cmnd, cmd, qpti, in_ptr, out_ptr)) == -1)
  901. goto toss_command;
  902. update_can_queue(host, in_ptr, out_ptr);
  903. return 0;
  904. toss_command:
  905. printk(KERN_EMERG "qlogicpti%d: request queue overflow\n",
  906. qpti->qpti_id);
  907. /* Unfortunately, unless you use the new EH code, which
  908. * we don't, the midlayer will ignore the return value,
  909. * which is insane. We pick up the pieces like this.
  910. */
  911. Cmnd->result = DID_BUS_BUSY;
  912. done(Cmnd);
  913. return 1;
  914. }
  915. static DEF_SCSI_QCMD(qlogicpti_queuecommand)
  916. static int qlogicpti_return_status(struct Status_Entry *sts, int id)
  917. {
  918. int host_status = DID_ERROR;
  919. switch (sts->completion_status) {
  920. case CS_COMPLETE:
  921. host_status = DID_OK;
  922. break;
  923. case CS_INCOMPLETE:
  924. if (!(sts->state_flags & SF_GOT_BUS))
  925. host_status = DID_NO_CONNECT;
  926. else if (!(sts->state_flags & SF_GOT_TARGET))
  927. host_status = DID_BAD_TARGET;
  928. else if (!(sts->state_flags & SF_SENT_CDB))
  929. host_status = DID_ERROR;
  930. else if (!(sts->state_flags & SF_TRANSFERRED_DATA))
  931. host_status = DID_ERROR;
  932. else if (!(sts->state_flags & SF_GOT_STATUS))
  933. host_status = DID_ERROR;
  934. else if (!(sts->state_flags & SF_GOT_SENSE))
  935. host_status = DID_ERROR;
  936. break;
  937. case CS_DMA_ERROR:
  938. case CS_TRANSPORT_ERROR:
  939. host_status = DID_ERROR;
  940. break;
  941. case CS_RESET_OCCURRED:
  942. case CS_BUS_RESET:
  943. host_status = DID_RESET;
  944. break;
  945. case CS_ABORTED:
  946. host_status = DID_ABORT;
  947. break;
  948. case CS_TIMEOUT:
  949. host_status = DID_TIME_OUT;
  950. break;
  951. case CS_DATA_OVERRUN:
  952. case CS_COMMAND_OVERRUN:
  953. case CS_STATUS_OVERRUN:
  954. case CS_BAD_MESSAGE:
  955. case CS_NO_MESSAGE_OUT:
  956. case CS_EXT_ID_FAILED:
  957. case CS_IDE_MSG_FAILED:
  958. case CS_ABORT_MSG_FAILED:
  959. case CS_NOP_MSG_FAILED:
  960. case CS_PARITY_ERROR_MSG_FAILED:
  961. case CS_DEVICE_RESET_MSG_FAILED:
  962. case CS_ID_MSG_FAILED:
  963. case CS_UNEXP_BUS_FREE:
  964. host_status = DID_ERROR;
  965. break;
  966. case CS_DATA_UNDERRUN:
  967. host_status = DID_OK;
  968. break;
  969. default:
  970. printk(KERN_EMERG "qlogicpti%d: unknown completion status 0x%04x\n",
  971. id, sts->completion_status);
  972. host_status = DID_ERROR;
  973. break;
  974. }
  975. return (sts->scsi_status & STATUS_MASK) | (host_status << 16);
  976. }
  977. static struct scsi_cmnd *qlogicpti_intr_handler(struct qlogicpti *qpti)
  978. {
  979. struct scsi_cmnd *Cmnd, *done_queue = NULL;
  980. struct Status_Entry *sts;
  981. u_int in_ptr, out_ptr;
  982. if (!(sbus_readw(qpti->qregs + SBUS_STAT) & SBUS_STAT_RINT))
  983. return NULL;
  984. in_ptr = sbus_readw(qpti->qregs + MBOX5);
  985. sbus_writew(HCCTRL_CRIRQ, qpti->qregs + HCCTRL);
  986. if (sbus_readw(qpti->qregs + SBUS_SEMAPHORE) & SBUS_SEMAPHORE_LCK) {
  987. switch (sbus_readw(qpti->qregs + MBOX0)) {
  988. case ASYNC_SCSI_BUS_RESET:
  989. case EXECUTION_TIMEOUT_RESET:
  990. qpti->send_marker = 1;
  991. break;
  992. case INVALID_COMMAND:
  993. case HOST_INTERFACE_ERROR:
  994. case COMMAND_ERROR:
  995. case COMMAND_PARAM_ERROR:
  996. break;
  997. };
  998. sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
  999. }
  1000. /* This looks like a network driver! */
  1001. out_ptr = qpti->res_out_ptr;
  1002. while (out_ptr != in_ptr) {
  1003. u_int cmd_slot;
  1004. sts = (struct Status_Entry *) &qpti->res_cpu[out_ptr];
  1005. out_ptr = NEXT_RES_PTR(out_ptr);
  1006. /* We store an index in the handle, not the pointer in
  1007. * some form. This avoids problems due to the fact
  1008. * that the handle provided is only 32-bits. -DaveM
  1009. */
  1010. cmd_slot = sts->handle;
  1011. Cmnd = qpti->cmd_slots[cmd_slot];
  1012. qpti->cmd_slots[cmd_slot] = NULL;
  1013. if (sts->completion_status == CS_RESET_OCCURRED ||
  1014. sts->completion_status == CS_ABORTED ||
  1015. (sts->status_flags & STF_BUS_RESET))
  1016. qpti->send_marker = 1;
  1017. if (sts->state_flags & SF_GOT_SENSE)
  1018. memcpy(Cmnd->sense_buffer, sts->req_sense_data,
  1019. SCSI_SENSE_BUFFERSIZE);
  1020. if (sts->hdr.entry_type == ENTRY_STATUS)
  1021. Cmnd->result =
  1022. qlogicpti_return_status(sts, qpti->qpti_id);
  1023. else
  1024. Cmnd->result = DID_ERROR << 16;
  1025. if (scsi_bufflen(Cmnd))
  1026. dma_unmap_sg(&qpti->op->dev,
  1027. scsi_sglist(Cmnd), scsi_sg_count(Cmnd),
  1028. Cmnd->sc_data_direction);
  1029. qpti->cmd_count[Cmnd->device->id]--;
  1030. sbus_writew(out_ptr, qpti->qregs + MBOX5);
  1031. Cmnd->host_scribble = (unsigned char *) done_queue;
  1032. done_queue = Cmnd;
  1033. }
  1034. qpti->res_out_ptr = out_ptr;
  1035. return done_queue;
  1036. }
  1037. static irqreturn_t qpti_intr(int irq, void *dev_id)
  1038. {
  1039. struct qlogicpti *qpti = dev_id;
  1040. unsigned long flags;
  1041. struct scsi_cmnd *dq;
  1042. spin_lock_irqsave(qpti->qhost->host_lock, flags);
  1043. dq = qlogicpti_intr_handler(qpti);
  1044. if (dq != NULL) {
  1045. do {
  1046. struct scsi_cmnd *next;
  1047. next = (struct scsi_cmnd *) dq->host_scribble;
  1048. dq->scsi_done(dq);
  1049. dq = next;
  1050. } while (dq != NULL);
  1051. }
  1052. spin_unlock_irqrestore(qpti->qhost->host_lock, flags);
  1053. return IRQ_HANDLED;
  1054. }
  1055. static int qlogicpti_abort(struct scsi_cmnd *Cmnd)
  1056. {
  1057. u_short param[6];
  1058. struct Scsi_Host *host = Cmnd->device->host;
  1059. struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  1060. int return_status = SUCCESS;
  1061. u32 cmd_cookie;
  1062. int i;
  1063. printk(KERN_WARNING "qlogicpti%d: Aborting cmd for tgt[%d] lun[%d]\n",
  1064. qpti->qpti_id, (int)Cmnd->device->id, (int)Cmnd->device->lun);
  1065. qlogicpti_disable_irqs(qpti);
  1066. /* Find the 32-bit cookie we gave to the firmware for
  1067. * this command.
  1068. */
  1069. for (i = 0; i < QLOGICPTI_REQ_QUEUE_LEN + 1; i++)
  1070. if (qpti->cmd_slots[i] == Cmnd)
  1071. break;
  1072. cmd_cookie = i;
  1073. param[0] = MBOX_ABORT;
  1074. param[1] = (((u_short) Cmnd->device->id) << 8) | Cmnd->device->lun;
  1075. param[2] = cmd_cookie >> 16;
  1076. param[3] = cmd_cookie & 0xffff;
  1077. if (qlogicpti_mbox_command(qpti, param, 0) ||
  1078. (param[0] != MBOX_COMMAND_COMPLETE)) {
  1079. printk(KERN_EMERG "qlogicpti%d: scsi abort failure: %x\n",
  1080. qpti->qpti_id, param[0]);
  1081. return_status = FAILED;
  1082. }
  1083. qlogicpti_enable_irqs(qpti);
  1084. return return_status;
  1085. }
  1086. static int qlogicpti_reset(struct scsi_cmnd *Cmnd)
  1087. {
  1088. u_short param[6];
  1089. struct Scsi_Host *host = Cmnd->device->host;
  1090. struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  1091. int return_status = SUCCESS;
  1092. printk(KERN_WARNING "qlogicpti%d: Resetting SCSI bus!\n",
  1093. qpti->qpti_id);
  1094. qlogicpti_disable_irqs(qpti);
  1095. param[0] = MBOX_BUS_RESET;
  1096. param[1] = qpti->host_param.bus_reset_delay;
  1097. if (qlogicpti_mbox_command(qpti, param, 0) ||
  1098. (param[0] != MBOX_COMMAND_COMPLETE)) {
  1099. printk(KERN_EMERG "qlogicisp%d: scsi bus reset failure: %x\n",
  1100. qpti->qpti_id, param[0]);
  1101. return_status = FAILED;
  1102. }
  1103. qlogicpti_enable_irqs(qpti);
  1104. return return_status;
  1105. }
  1106. static struct scsi_host_template qpti_template = {
  1107. .module = THIS_MODULE,
  1108. .name = "qlogicpti",
  1109. .info = qlogicpti_info,
  1110. .queuecommand = qlogicpti_queuecommand,
  1111. .slave_configure = qlogicpti_slave_configure,
  1112. .eh_abort_handler = qlogicpti_abort,
  1113. .eh_host_reset_handler = qlogicpti_reset,
  1114. .can_queue = QLOGICPTI_REQ_QUEUE_LEN,
  1115. .this_id = 7,
  1116. .sg_tablesize = QLOGICPTI_MAX_SG(QLOGICPTI_REQ_QUEUE_LEN),
  1117. .use_clustering = ENABLE_CLUSTERING,
  1118. };
  1119. static const struct of_device_id qpti_match[];
  1120. static int qpti_sbus_probe(struct platform_device *op)
  1121. {
  1122. struct device_node *dp = op->dev.of_node;
  1123. struct Scsi_Host *host;
  1124. struct qlogicpti *qpti;
  1125. static int nqptis;
  1126. const char *fcode;
  1127. /* Sometimes Antares cards come up not completely
  1128. * setup, and we get a report of a zero IRQ.
  1129. */
  1130. if (op->archdata.irqs[0] == 0)
  1131. return -ENODEV;
  1132. host = scsi_host_alloc(&qpti_template, sizeof(struct qlogicpti));
  1133. if (!host)
  1134. return -ENOMEM;
  1135. qpti = shost_priv(host);
  1136. host->max_id = MAX_TARGETS;
  1137. qpti->qhost = host;
  1138. qpti->op = op;
  1139. qpti->qpti_id = nqptis;
  1140. strcpy(qpti->prom_name, op->dev.of_node->name);
  1141. qpti->is_pti = strcmp(qpti->prom_name, "QLGC,isp");
  1142. if (qpti_map_regs(qpti) < 0)
  1143. goto fail_unlink;
  1144. if (qpti_register_irq(qpti) < 0)
  1145. goto fail_unmap_regs;
  1146. qpti_get_scsi_id(qpti);
  1147. qpti_get_bursts(qpti);
  1148. qpti_get_clock(qpti);
  1149. /* Clear out scsi_cmnd array. */
  1150. memset(qpti->cmd_slots, 0, sizeof(qpti->cmd_slots));
  1151. if (qpti_map_queues(qpti) < 0)
  1152. goto fail_free_irq;
  1153. /* Load the firmware. */
  1154. if (qlogicpti_load_firmware(qpti))
  1155. goto fail_unmap_queues;
  1156. if (qpti->is_pti) {
  1157. /* Check the PTI status reg. */
  1158. if (qlogicpti_verify_tmon(qpti))
  1159. goto fail_unmap_queues;
  1160. }
  1161. /* Reset the ISP and init res/req queues. */
  1162. if (qlogicpti_reset_hardware(host))
  1163. goto fail_unmap_queues;
  1164. printk("(Firmware v%d.%d.%d)", qpti->fware_majrev,
  1165. qpti->fware_minrev, qpti->fware_micrev);
  1166. fcode = of_get_property(dp, "isp-fcode", NULL);
  1167. if (fcode && fcode[0])
  1168. printk("(FCode %s)", fcode);
  1169. if (of_find_property(dp, "differential", NULL) != NULL)
  1170. qpti->differential = 1;
  1171. printk("\nqlogicpti%d: [%s Wide, using %s interface]\n",
  1172. qpti->qpti_id,
  1173. (qpti->ultra ? "Ultra" : "Fast"),
  1174. (qpti->differential ? "differential" : "single ended"));
  1175. if (scsi_add_host(host, &op->dev)) {
  1176. printk("qlogicpti%d: Failed scsi_add_host\n", qpti->qpti_id);
  1177. goto fail_unmap_queues;
  1178. }
  1179. dev_set_drvdata(&op->dev, qpti);
  1180. qpti_chain_add(qpti);
  1181. scsi_scan_host(host);
  1182. nqptis++;
  1183. return 0;
  1184. fail_unmap_queues:
  1185. #define QSIZE(entries) (((entries) + 1) * QUEUE_ENTRY_LEN)
  1186. dma_free_coherent(&op->dev,
  1187. QSIZE(RES_QUEUE_LEN),
  1188. qpti->res_cpu, qpti->res_dvma);
  1189. dma_free_coherent(&op->dev,
  1190. QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
  1191. qpti->req_cpu, qpti->req_dvma);
  1192. #undef QSIZE
  1193. fail_free_irq:
  1194. free_irq(qpti->irq, qpti);
  1195. fail_unmap_regs:
  1196. of_iounmap(&op->resource[0], qpti->qregs,
  1197. resource_size(&op->resource[0]));
  1198. if (qpti->is_pti)
  1199. of_iounmap(&op->resource[0], qpti->sreg,
  1200. sizeof(unsigned char));
  1201. fail_unlink:
  1202. scsi_host_put(host);
  1203. return -ENODEV;
  1204. }
  1205. static int qpti_sbus_remove(struct platform_device *op)
  1206. {
  1207. struct qlogicpti *qpti = dev_get_drvdata(&op->dev);
  1208. qpti_chain_del(qpti);
  1209. scsi_remove_host(qpti->qhost);
  1210. /* Shut up the card. */
  1211. sbus_writew(0, qpti->qregs + SBUS_CTRL);
  1212. /* Free IRQ handler and unmap Qlogic,ISP and PTI status regs. */
  1213. free_irq(qpti->irq, qpti);
  1214. #define QSIZE(entries) (((entries) + 1) * QUEUE_ENTRY_LEN)
  1215. dma_free_coherent(&op->dev,
  1216. QSIZE(RES_QUEUE_LEN),
  1217. qpti->res_cpu, qpti->res_dvma);
  1218. dma_free_coherent(&op->dev,
  1219. QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
  1220. qpti->req_cpu, qpti->req_dvma);
  1221. #undef QSIZE
  1222. of_iounmap(&op->resource[0], qpti->qregs,
  1223. resource_size(&op->resource[0]));
  1224. if (qpti->is_pti)
  1225. of_iounmap(&op->resource[0], qpti->sreg, sizeof(unsigned char));
  1226. scsi_host_put(qpti->qhost);
  1227. return 0;
  1228. }
  1229. static const struct of_device_id qpti_match[] = {
  1230. {
  1231. .name = "ptisp",
  1232. },
  1233. {
  1234. .name = "PTI,ptisp",
  1235. },
  1236. {
  1237. .name = "QLGC,isp",
  1238. },
  1239. {
  1240. .name = "SUNW,isp",
  1241. },
  1242. {},
  1243. };
  1244. MODULE_DEVICE_TABLE(of, qpti_match);
  1245. static struct platform_driver qpti_sbus_driver = {
  1246. .driver = {
  1247. .name = "qpti",
  1248. .of_match_table = qpti_match,
  1249. },
  1250. .probe = qpti_sbus_probe,
  1251. .remove = qpti_sbus_remove,
  1252. };
  1253. static int __init qpti_init(void)
  1254. {
  1255. return platform_driver_register(&qpti_sbus_driver);
  1256. }
  1257. static void __exit qpti_exit(void)
  1258. {
  1259. platform_driver_unregister(&qpti_sbus_driver);
  1260. }
  1261. MODULE_DESCRIPTION("QlogicISP SBUS driver");
  1262. MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
  1263. MODULE_LICENSE("GPL");
  1264. MODULE_VERSION("2.1");
  1265. MODULE_FIRMWARE("qlogic/isp1000.bin");
  1266. module_init(qpti_init);
  1267. module_exit(qpti_exit);