qlogicpti.c 43 KB

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