qlogicpti.c 40 KB

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