qlogicpti.c 40 KB

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