qlogicpti.c 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573
  1. /* qlogicpti.c: Performance Technologies QlogicISP sbus card driver.
  2. *
  3. * Copyright (C) 1996 David S. Miller (davem@caipfs.rutgers.edu)
  4. *
  5. * A lot of this driver was directly stolen from Erik H. Moe's PCI
  6. * Qlogic ISP driver. Mucho kudos to him for this code.
  7. *
  8. * An even bigger kudos to John Grana at Performance Technologies
  9. * for providing me with the hardware to write this driver, you rule
  10. * John you really do.
  11. *
  12. * May, 2, 1997: Added support for QLGC,isp --jj
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/delay.h>
  16. #include <linux/types.h>
  17. #include <linux/string.h>
  18. #include <linux/slab.h>
  19. #include <linux/blkdev.h>
  20. #include <linux/proc_fs.h>
  21. #include <linux/stat.h>
  22. #include <linux/init.h>
  23. #include <linux/spinlock.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/module.h>
  26. #include <linux/jiffies.h>
  27. #include <asm/byteorder.h>
  28. #include "qlogicpti.h"
  29. #include <asm/sbus.h>
  30. #include <asm/dma.h>
  31. #include <asm/system.h>
  32. #include <asm/ptrace.h>
  33. #include <asm/pgtable.h>
  34. #include <asm/oplib.h>
  35. #include <asm/io.h>
  36. #include <asm/irq.h>
  37. #include <scsi/scsi.h>
  38. #include <scsi/scsi_cmnd.h>
  39. #include <scsi/scsi_device.h>
  40. #include <scsi/scsi_eh.h>
  41. #include <scsi/scsi_request.h>
  42. #include <scsi/scsi_tcq.h>
  43. #include <scsi/scsi_host.h>
  44. #define MAX_TARGETS 16
  45. #define MAX_LUNS 8 /* 32 for 1.31 F/W */
  46. #define DEFAULT_LOOP_COUNT 10000
  47. #include "qlogicpti_asm.c"
  48. static struct qlogicpti *qptichain = NULL;
  49. static DEFINE_SPINLOCK(qptichain_lock);
  50. static int qptis_running = 0;
  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 (sizeof(mbox_param)/sizeof(u_short))
  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(qpti->sdev) && (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: mbox_command loop timeout #1\n");
  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: mbox_command[%04x] loop timeout #2\n",
  204. 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: mbox_command[%04x] loop timeout #3\n",
  216. 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: mbox_command[%04x] loop timeout #4\n",
  223. 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. /* this is very important to set! */
  279. qpti->sbits = 1 << qpti->scsi_id;
  280. }
  281. static int qlogicpti_reset_hardware(struct Scsi_Host *host)
  282. {
  283. struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  284. u_short param[6];
  285. unsigned short risc_code_addr;
  286. int loop_count, i;
  287. unsigned long flags;
  288. risc_code_addr = 0x1000; /* all load addresses are at 0x1000 */
  289. spin_lock_irqsave(host->host_lock, flags);
  290. sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
  291. /* Only reset the scsi bus if it is not free. */
  292. if (sbus_readw(qpti->qregs + CPU_PCTRL) & CPU_PCTRL_BSY) {
  293. sbus_writew(CPU_ORIDE_RMOD, qpti->qregs + CPU_ORIDE);
  294. sbus_writew(CPU_CMD_BRESET, qpti->qregs + CPU_CMD);
  295. udelay(400);
  296. }
  297. sbus_writew(SBUS_CTRL_RESET, qpti->qregs + SBUS_CTRL);
  298. sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + CMD_DMA_CTRL);
  299. sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + DATA_DMA_CTRL);
  300. loop_count = DEFAULT_LOOP_COUNT;
  301. while (--loop_count && ((sbus_readw(qpti->qregs + MBOX0) & 0xff) == 0x04))
  302. udelay(20);
  303. if (!loop_count)
  304. printk(KERN_EMERG "qlogicpti: reset_hardware loop timeout\n");
  305. sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
  306. set_sbus_cfg1(qpti);
  307. qlogicpti_enable_irqs(qpti);
  308. if (sbus_readw(qpti->qregs + RISC_PSR) & RISC_PSR_ULTRA) {
  309. qpti->ultra = 1;
  310. sbus_writew((RISC_MTREG_P0ULTRA | RISC_MTREG_P1ULTRA),
  311. qpti->qregs + RISC_MTREG);
  312. } else {
  313. qpti->ultra = 0;
  314. sbus_writew((RISC_MTREG_P0DFLT | RISC_MTREG_P1DFLT),
  315. qpti->qregs + RISC_MTREG);
  316. }
  317. /* reset adapter and per-device default values. */
  318. /* do it after finding out whether we're ultra mode capable */
  319. qlogicpti_set_hostdev_defaults(qpti);
  320. /* Release the RISC processor. */
  321. sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
  322. /* Get RISC to start executing the firmware code. */
  323. param[0] = MBOX_EXEC_FIRMWARE;
  324. param[1] = risc_code_addr;
  325. if (qlogicpti_mbox_command(qpti, param, 1)) {
  326. printk(KERN_EMERG "qlogicpti%d: Cannot execute ISP firmware.\n",
  327. qpti->qpti_id);
  328. spin_unlock_irqrestore(host->host_lock, flags);
  329. return 1;
  330. }
  331. /* Set initiator scsi ID. */
  332. param[0] = MBOX_SET_INIT_SCSI_ID;
  333. param[1] = qpti->host_param.initiator_scsi_id;
  334. if (qlogicpti_mbox_command(qpti, param, 1) ||
  335. (param[0] != MBOX_COMMAND_COMPLETE)) {
  336. printk(KERN_EMERG "qlogicpti%d: Cannot set initiator SCSI ID.\n",
  337. qpti->qpti_id);
  338. spin_unlock_irqrestore(host->host_lock, flags);
  339. return 1;
  340. }
  341. /* Initialize state of the queues, both hw and sw. */
  342. qpti->req_in_ptr = qpti->res_out_ptr = 0;
  343. param[0] = MBOX_INIT_RES_QUEUE;
  344. param[1] = RES_QUEUE_LEN + 1;
  345. param[2] = (u_short) (qpti->res_dvma >> 16);
  346. param[3] = (u_short) (qpti->res_dvma & 0xffff);
  347. param[4] = param[5] = 0;
  348. if (qlogicpti_mbox_command(qpti, param, 1)) {
  349. printk(KERN_EMERG "qlogicpti%d: Cannot init response queue.\n",
  350. qpti->qpti_id);
  351. spin_unlock_irqrestore(host->host_lock, flags);
  352. return 1;
  353. }
  354. param[0] = MBOX_INIT_REQ_QUEUE;
  355. param[1] = QLOGICPTI_REQ_QUEUE_LEN + 1;
  356. param[2] = (u_short) (qpti->req_dvma >> 16);
  357. param[3] = (u_short) (qpti->req_dvma & 0xffff);
  358. param[4] = param[5] = 0;
  359. if (qlogicpti_mbox_command(qpti, param, 1)) {
  360. printk(KERN_EMERG "qlogicpti%d: Cannot init request queue.\n",
  361. qpti->qpti_id);
  362. spin_unlock_irqrestore(host->host_lock, flags);
  363. return 1;
  364. }
  365. param[0] = MBOX_SET_RETRY_COUNT;
  366. param[1] = qpti->host_param.retry_count;
  367. param[2] = qpti->host_param.retry_delay;
  368. qlogicpti_mbox_command(qpti, param, 0);
  369. param[0] = MBOX_SET_TAG_AGE_LIMIT;
  370. param[1] = qpti->host_param.tag_aging;
  371. qlogicpti_mbox_command(qpti, param, 0);
  372. for (i = 0; i < MAX_TARGETS; i++) {
  373. param[0] = MBOX_GET_DEV_QUEUE_PARAMS;
  374. param[1] = (i << 8);
  375. qlogicpti_mbox_command(qpti, param, 0);
  376. }
  377. param[0] = MBOX_GET_FIRMWARE_STATUS;
  378. qlogicpti_mbox_command(qpti, param, 0);
  379. param[0] = MBOX_SET_SELECT_TIMEOUT;
  380. param[1] = qpti->host_param.selection_timeout;
  381. qlogicpti_mbox_command(qpti, param, 0);
  382. for (i = 0; i < MAX_TARGETS; i++) {
  383. param[0] = MBOX_SET_TARGET_PARAMS;
  384. param[1] = (i << 8);
  385. param[2] = (qpti->dev_param[i].device_flags << 8);
  386. /*
  387. * Since we're now loading 1.31 f/w, force narrow/async.
  388. */
  389. param[2] |= 0xc0;
  390. param[3] = 0; /* no offset, we do not have sync mode yet */
  391. qlogicpti_mbox_command(qpti, param, 0);
  392. }
  393. /*
  394. * Always (sigh) do an initial bus reset (kicks f/w).
  395. */
  396. param[0] = MBOX_BUS_RESET;
  397. param[1] = qpti->host_param.bus_reset_delay;
  398. qlogicpti_mbox_command(qpti, param, 0);
  399. qpti->send_marker = 1;
  400. spin_unlock_irqrestore(host->host_lock, flags);
  401. return 0;
  402. }
  403. #define PTI_RESET_LIMIT 400
  404. static int __init qlogicpti_load_firmware(struct qlogicpti *qpti)
  405. {
  406. struct Scsi_Host *host = qpti->qhost;
  407. unsigned short csum = 0;
  408. unsigned short param[6];
  409. unsigned short *risc_code, risc_code_addr, risc_code_length;
  410. unsigned long flags;
  411. int i, timeout;
  412. risc_code = &sbus_risc_code01[0];
  413. risc_code_addr = 0x1000; /* all f/w modules load at 0x1000 */
  414. risc_code_length = sbus_risc_code_length01;
  415. spin_lock_irqsave(host->host_lock, flags);
  416. /* Verify the checksum twice, one before loading it, and once
  417. * afterwards via the mailbox commands.
  418. */
  419. for (i = 0; i < risc_code_length; i++)
  420. csum += risc_code[i];
  421. if (csum) {
  422. spin_unlock_irqrestore(host->host_lock, flags);
  423. printk(KERN_EMERG "qlogicpti%d: Aieee, firmware checksum failed!",
  424. qpti->qpti_id);
  425. return 1;
  426. }
  427. sbus_writew(SBUS_CTRL_RESET, qpti->qregs + SBUS_CTRL);
  428. sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + CMD_DMA_CTRL);
  429. sbus_writew((DMA_CTRL_CCLEAR | DMA_CTRL_CIRQ), qpti->qregs + DATA_DMA_CTRL);
  430. timeout = PTI_RESET_LIMIT;
  431. while (--timeout && (sbus_readw(qpti->qregs + SBUS_CTRL) & SBUS_CTRL_RESET))
  432. udelay(20);
  433. if (!timeout) {
  434. spin_unlock_irqrestore(host->host_lock, flags);
  435. printk(KERN_EMERG "qlogicpti%d: Cannot reset the ISP.", qpti->qpti_id);
  436. return 1;
  437. }
  438. sbus_writew(HCCTRL_RESET, qpti->qregs + HCCTRL);
  439. mdelay(1);
  440. sbus_writew((SBUS_CTRL_GENAB | SBUS_CTRL_ERIRQ), qpti->qregs + SBUS_CTRL);
  441. set_sbus_cfg1(qpti);
  442. sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
  443. if (sbus_readw(qpti->qregs + RISC_PSR) & RISC_PSR_ULTRA) {
  444. qpti->ultra = 1;
  445. sbus_writew((RISC_MTREG_P0ULTRA | RISC_MTREG_P1ULTRA),
  446. qpti->qregs + RISC_MTREG);
  447. } else {
  448. qpti->ultra = 0;
  449. sbus_writew((RISC_MTREG_P0DFLT | RISC_MTREG_P1DFLT),
  450. qpti->qregs + RISC_MTREG);
  451. }
  452. sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
  453. /* Pin lines are only stable while RISC is paused. */
  454. sbus_writew(HCCTRL_PAUSE, qpti->qregs + HCCTRL);
  455. if (sbus_readw(qpti->qregs + CPU_PDIFF) & CPU_PDIFF_MODE)
  456. qpti->differential = 1;
  457. else
  458. qpti->differential = 0;
  459. sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
  460. /* This shouldn't be necessary- we've reset things so we should be
  461. running from the ROM now.. */
  462. param[0] = MBOX_STOP_FIRMWARE;
  463. param[1] = param[2] = param[3] = param[4] = param[5] = 0;
  464. if (qlogicpti_mbox_command(qpti, param, 1)) {
  465. printk(KERN_EMERG "qlogicpti%d: Cannot stop firmware for reload.\n",
  466. qpti->qpti_id);
  467. spin_unlock_irqrestore(host->host_lock, flags);
  468. return 1;
  469. }
  470. /* Load it up.. */
  471. for (i = 0; i < risc_code_length; i++) {
  472. param[0] = MBOX_WRITE_RAM_WORD;
  473. param[1] = risc_code_addr + i;
  474. param[2] = risc_code[i];
  475. if (qlogicpti_mbox_command(qpti, param, 1) ||
  476. param[0] != MBOX_COMMAND_COMPLETE) {
  477. printk("qlogicpti%d: Firmware dload failed, I'm bolixed!\n",
  478. qpti->qpti_id);
  479. spin_unlock_irqrestore(host->host_lock, flags);
  480. return 1;
  481. }
  482. }
  483. /* Reset the ISP again. */
  484. sbus_writew(HCCTRL_RESET, qpti->qregs + HCCTRL);
  485. mdelay(1);
  486. qlogicpti_enable_irqs(qpti);
  487. sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
  488. sbus_writew(HCCTRL_REL, qpti->qregs + HCCTRL);
  489. /* Ask ISP to verify the checksum of the new code. */
  490. param[0] = MBOX_VERIFY_CHECKSUM;
  491. param[1] = risc_code_addr;
  492. if (qlogicpti_mbox_command(qpti, param, 1) ||
  493. (param[0] != MBOX_COMMAND_COMPLETE)) {
  494. printk(KERN_EMERG "qlogicpti%d: New firmware csum failure!\n",
  495. qpti->qpti_id);
  496. spin_unlock_irqrestore(host->host_lock, flags);
  497. return 1;
  498. }
  499. /* Start using newly downloaded firmware. */
  500. param[0] = MBOX_EXEC_FIRMWARE;
  501. param[1] = risc_code_addr;
  502. qlogicpti_mbox_command(qpti, param, 1);
  503. param[0] = MBOX_ABOUT_FIRMWARE;
  504. if (qlogicpti_mbox_command(qpti, param, 1) ||
  505. (param[0] != MBOX_COMMAND_COMPLETE)) {
  506. printk(KERN_EMERG "qlogicpti%d: AboutFirmware cmd fails.\n",
  507. qpti->qpti_id);
  508. spin_unlock_irqrestore(host->host_lock, flags);
  509. return 1;
  510. }
  511. /* Snag the major and minor revisions from the result. */
  512. qpti->fware_majrev = param[1];
  513. qpti->fware_minrev = param[2];
  514. qpti->fware_micrev = param[3];
  515. /* Set the clock rate */
  516. param[0] = MBOX_SET_CLOCK_RATE;
  517. param[1] = qpti->clock;
  518. if (qlogicpti_mbox_command(qpti, param, 1) ||
  519. (param[0] != MBOX_COMMAND_COMPLETE)) {
  520. printk(KERN_EMERG "qlogicpti%d: could not set clock rate.\n",
  521. qpti->qpti_id);
  522. spin_unlock_irqrestore(host->host_lock, flags);
  523. return 1;
  524. }
  525. if (qpti->is_pti != 0) {
  526. /* Load scsi initiator ID and interrupt level into sbus static ram. */
  527. param[0] = MBOX_WRITE_RAM_WORD;
  528. param[1] = 0xff80;
  529. param[2] = (unsigned short) qpti->scsi_id;
  530. qlogicpti_mbox_command(qpti, param, 1);
  531. param[0] = MBOX_WRITE_RAM_WORD;
  532. param[1] = 0xff00;
  533. param[2] = (unsigned short) 3;
  534. qlogicpti_mbox_command(qpti, param, 1);
  535. }
  536. spin_unlock_irqrestore(host->host_lock, flags);
  537. return 0;
  538. }
  539. static int qlogicpti_verify_tmon(struct qlogicpti *qpti)
  540. {
  541. int curstat = sbus_readb(qpti->sreg);
  542. curstat &= 0xf0;
  543. if (!(curstat & SREG_FUSE) && (qpti->swsreg & SREG_FUSE))
  544. printk("qlogicpti%d: Fuse returned to normal state.\n", qpti->qpti_id);
  545. if (!(curstat & SREG_TPOWER) && (qpti->swsreg & SREG_TPOWER))
  546. printk("qlogicpti%d: termpwr back to normal state.\n", qpti->qpti_id);
  547. if (curstat != qpti->swsreg) {
  548. int error = 0;
  549. if (curstat & SREG_FUSE) {
  550. error++;
  551. printk("qlogicpti%d: Fuse is open!\n", qpti->qpti_id);
  552. }
  553. if (curstat & SREG_TPOWER) {
  554. error++;
  555. printk("qlogicpti%d: termpwr failure\n", qpti->qpti_id);
  556. }
  557. if (qpti->differential &&
  558. (curstat & SREG_DSENSE) != SREG_DSENSE) {
  559. error++;
  560. printk("qlogicpti%d: You have a single ended device on a "
  561. "differential bus! Please fix!\n", qpti->qpti_id);
  562. }
  563. qpti->swsreg = curstat;
  564. return error;
  565. }
  566. return 0;
  567. }
  568. static irqreturn_t qpti_intr(int irq, void *dev_id, struct pt_regs *regs);
  569. static void __init qpti_chain_add(struct qlogicpti *qpti)
  570. {
  571. spin_lock_irq(&qptichain_lock);
  572. if (qptichain != NULL) {
  573. struct qlogicpti *qlink = qptichain;
  574. while(qlink->next)
  575. qlink = qlink->next;
  576. qlink->next = qpti;
  577. } else {
  578. qptichain = qpti;
  579. }
  580. qpti->next = NULL;
  581. spin_unlock_irq(&qptichain_lock);
  582. }
  583. static void __init qpti_chain_del(struct qlogicpti *qpti)
  584. {
  585. spin_lock_irq(&qptichain_lock);
  586. if (qptichain == qpti) {
  587. qptichain = qpti->next;
  588. } else {
  589. struct qlogicpti *qlink = qptichain;
  590. while(qlink->next != qpti)
  591. qlink = qlink->next;
  592. qlink->next = qpti->next;
  593. }
  594. qpti->next = NULL;
  595. spin_unlock_irq(&qptichain_lock);
  596. }
  597. static int __init qpti_map_regs(struct qlogicpti *qpti)
  598. {
  599. struct sbus_dev *sdev = qpti->sdev;
  600. qpti->qregs = sbus_ioremap(&sdev->resource[0], 0,
  601. sdev->reg_addrs[0].reg_size,
  602. "PTI Qlogic/ISP");
  603. if (!qpti->qregs) {
  604. printk("PTI: Qlogic/ISP registers are unmappable\n");
  605. return -1;
  606. }
  607. if (qpti->is_pti) {
  608. qpti->sreg = sbus_ioremap(&sdev->resource[0], (16 * 4096),
  609. sizeof(unsigned char),
  610. "PTI Qlogic/ISP statreg");
  611. if (!qpti->sreg) {
  612. printk("PTI: Qlogic/ISP status register is unmappable\n");
  613. return -1;
  614. }
  615. }
  616. return 0;
  617. }
  618. static int __init qpti_register_irq(struct qlogicpti *qpti)
  619. {
  620. struct sbus_dev *sdev = qpti->sdev;
  621. qpti->qhost->irq = qpti->irq = sdev->irqs[0];
  622. /* We used to try various overly-clever things to
  623. * reduce the interrupt processing overhead on
  624. * sun4c/sun4m when multiple PTI's shared the
  625. * same IRQ. It was too complex and messy to
  626. * sanely maintain.
  627. */
  628. if (request_irq(qpti->irq, qpti_intr,
  629. SA_SHIRQ, "Qlogic/PTI", qpti))
  630. goto fail;
  631. printk("qpti%d: IRQ %s ", qpti->qpti_id, __irq_itoa(qpti->irq));
  632. return 0;
  633. fail:
  634. printk("qpti%d: Cannot acquire irq line\n", qpti->qpti_id);
  635. return -1;
  636. }
  637. static void __init qpti_get_scsi_id(struct qlogicpti *qpti)
  638. {
  639. qpti->scsi_id = prom_getintdefault(qpti->prom_node,
  640. "initiator-id",
  641. -1);
  642. if (qpti->scsi_id == -1)
  643. qpti->scsi_id = prom_getintdefault(qpti->prom_node,
  644. "scsi-initiator-id",
  645. -1);
  646. if (qpti->scsi_id == -1)
  647. qpti->scsi_id =
  648. prom_getintdefault(qpti->sdev->bus->prom_node,
  649. "scsi-initiator-id", 7);
  650. qpti->qhost->this_id = qpti->scsi_id;
  651. qpti->qhost->max_sectors = 64;
  652. printk("SCSI ID %d ", qpti->scsi_id);
  653. }
  654. static void qpti_get_bursts(struct qlogicpti *qpti)
  655. {
  656. struct sbus_dev *sdev = qpti->sdev;
  657. u8 bursts, bmask;
  658. bursts = prom_getintdefault(qpti->prom_node, "burst-sizes", 0xff);
  659. bmask = prom_getintdefault(sdev->bus->prom_node,
  660. "burst-sizes", 0xff);
  661. if (bmask != 0xff)
  662. bursts &= bmask;
  663. if (bursts == 0xff ||
  664. (bursts & DMA_BURST16) == 0 ||
  665. (bursts & DMA_BURST32) == 0)
  666. bursts = (DMA_BURST32 - 1);
  667. qpti->bursts = bursts;
  668. }
  669. static void qpti_get_clock(struct qlogicpti *qpti)
  670. {
  671. unsigned int cfreq;
  672. /* Check for what the clock input to this card is.
  673. * Default to 40Mhz.
  674. */
  675. cfreq = prom_getintdefault(qpti->prom_node,"clock-frequency",40000000);
  676. qpti->clock = (cfreq + 500000)/1000000;
  677. if (qpti->clock == 0) /* bullshit */
  678. qpti->clock = 40;
  679. }
  680. /* The request and response queues must each be aligned
  681. * on a page boundary.
  682. */
  683. static int __init qpti_map_queues(struct qlogicpti *qpti)
  684. {
  685. struct sbus_dev *sdev = qpti->sdev;
  686. #define QSIZE(entries) (((entries) + 1) * QUEUE_ENTRY_LEN)
  687. qpti->res_cpu = sbus_alloc_consistent(sdev,
  688. QSIZE(RES_QUEUE_LEN),
  689. &qpti->res_dvma);
  690. if (qpti->res_cpu == NULL ||
  691. qpti->res_dvma == 0) {
  692. printk("QPTI: Cannot map response queue.\n");
  693. return -1;
  694. }
  695. qpti->req_cpu = sbus_alloc_consistent(sdev,
  696. QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
  697. &qpti->req_dvma);
  698. if (qpti->req_cpu == NULL ||
  699. qpti->req_dvma == 0) {
  700. sbus_free_consistent(sdev, QSIZE(RES_QUEUE_LEN),
  701. qpti->res_cpu, qpti->res_dvma);
  702. printk("QPTI: Cannot map request queue.\n");
  703. return -1;
  704. }
  705. memset(qpti->res_cpu, 0, QSIZE(RES_QUEUE_LEN));
  706. memset(qpti->req_cpu, 0, QSIZE(QLOGICPTI_REQ_QUEUE_LEN));
  707. return 0;
  708. }
  709. /* Detect all PTI Qlogic ISP's in the machine. */
  710. static int __init qlogicpti_detect(struct scsi_host_template *tpnt)
  711. {
  712. struct qlogicpti *qpti;
  713. struct Scsi_Host *qpti_host;
  714. struct sbus_bus *sbus;
  715. struct sbus_dev *sdev;
  716. int nqptis = 0, nqptis_in_use = 0;
  717. tpnt->proc_name = "qlogicpti";
  718. for_each_sbus(sbus) {
  719. for_each_sbusdev(sdev, sbus) {
  720. /* Is this a red snapper? */
  721. if (strcmp(sdev->prom_name, "ptisp") &&
  722. strcmp(sdev->prom_name, "PTI,ptisp") &&
  723. strcmp(sdev->prom_name, "QLGC,isp") &&
  724. strcmp(sdev->prom_name, "SUNW,isp"))
  725. continue;
  726. /* Sometimes Antares cards come up not completely
  727. * setup, and we get a report of a zero IRQ.
  728. * Skip over them in such cases so we survive.
  729. */
  730. if (sdev->irqs[0] == 0) {
  731. printk("qpti%d: Adapter reports no interrupt, "
  732. "skipping over this card.", nqptis);
  733. continue;
  734. }
  735. /* Yep, register and allocate software state. */
  736. qpti_host = scsi_register(tpnt, sizeof(struct qlogicpti));
  737. if (!qpti_host) {
  738. printk("QPTI: Cannot register PTI Qlogic ISP SCSI host");
  739. continue;
  740. }
  741. qpti = (struct qlogicpti *) qpti_host->hostdata;
  742. /* We are wide capable, 16 targets. */
  743. qpti_host->max_id = MAX_TARGETS;
  744. /* Setup back pointers and misc. state. */
  745. qpti->qhost = qpti_host;
  746. qpti->sdev = sdev;
  747. qpti->qpti_id = nqptis++;
  748. qpti->prom_node = sdev->prom_node;
  749. prom_getstring(qpti->prom_node, "name",
  750. qpti->prom_name,
  751. sizeof(qpti->prom_name));
  752. /* This is not correct, actually. There's a switch
  753. * on the PTI cards that put them into "emulation"
  754. * mode- i.e., report themselves as QLGC,isp
  755. * instead of PTI,ptisp. The only real substantive
  756. * difference between non-pti and pti cards is
  757. * the tmon register. Which is possibly even
  758. * there for Qlogic cards, but non-functional.
  759. */
  760. qpti->is_pti = (strcmp (qpti->prom_name, "QLGC,isp") != 0);
  761. qpti_chain_add(qpti);
  762. if (qpti_map_regs(qpti) < 0)
  763. goto fail_unlink;
  764. if (qpti_register_irq(qpti) < 0)
  765. goto fail_unmap_regs;
  766. qpti_get_scsi_id(qpti);
  767. qpti_get_bursts(qpti);
  768. qpti_get_clock(qpti);
  769. /* Clear out scsi_cmnd array. */
  770. memset(qpti->cmd_slots, 0, sizeof(qpti->cmd_slots));
  771. if (qpti_map_queues(qpti) < 0)
  772. goto fail_free_irq;
  773. /* Load the firmware. */
  774. if (qlogicpti_load_firmware(qpti))
  775. goto fail_unmap_queues;
  776. if (qpti->is_pti) {
  777. /* Check the PTI status reg. */
  778. if (qlogicpti_verify_tmon(qpti))
  779. goto fail_unmap_queues;
  780. }
  781. /* Reset the ISP and init res/req queues. */
  782. if (qlogicpti_reset_hardware(qpti_host))
  783. goto fail_unmap_queues;
  784. printk("(Firmware v%d.%d.%d)", qpti->fware_majrev,
  785. qpti->fware_minrev, qpti->fware_micrev);
  786. {
  787. char buffer[60];
  788. prom_getstring (qpti->prom_node,
  789. "isp-fcode", buffer, 60);
  790. if (buffer[0])
  791. printk("(Firmware %s)", buffer);
  792. if (prom_getbool(qpti->prom_node, "differential"))
  793. qpti->differential = 1;
  794. }
  795. printk (" [%s Wide, using %s interface]\n",
  796. (qpti->ultra ? "Ultra" : "Fast"),
  797. (qpti->differential ? "differential" : "single ended"));
  798. nqptis_in_use++;
  799. continue;
  800. fail_unmap_queues:
  801. #define QSIZE(entries) (((entries) + 1) * QUEUE_ENTRY_LEN)
  802. sbus_free_consistent(qpti->sdev,
  803. QSIZE(RES_QUEUE_LEN),
  804. qpti->res_cpu, qpti->res_dvma);
  805. sbus_free_consistent(qpti->sdev,
  806. QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
  807. qpti->req_cpu, qpti->req_dvma);
  808. #undef QSIZE
  809. fail_free_irq:
  810. free_irq(qpti->irq, qpti);
  811. fail_unmap_regs:
  812. sbus_iounmap(qpti->qregs,
  813. qpti->sdev->reg_addrs[0].reg_size);
  814. if (qpti->is_pti)
  815. sbus_iounmap(qpti->sreg, sizeof(unsigned char));
  816. fail_unlink:
  817. qpti_chain_del(qpti);
  818. scsi_unregister(qpti->qhost);
  819. }
  820. }
  821. if (nqptis)
  822. printk("QPTI: Total of %d PTI Qlogic/ISP hosts found, %d actually in use.\n",
  823. nqptis, nqptis_in_use);
  824. qptis_running = nqptis_in_use;
  825. return nqptis;
  826. }
  827. static int qlogicpti_release(struct Scsi_Host *host)
  828. {
  829. struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  830. /* Remove visibility from IRQ handlers. */
  831. qpti_chain_del(qpti);
  832. /* Shut up the card. */
  833. sbus_writew(0, qpti->qregs + SBUS_CTRL);
  834. /* Free IRQ handler and unmap Qlogic,ISP and PTI status regs. */
  835. free_irq(qpti->irq, qpti);
  836. #define QSIZE(entries) (((entries) + 1) * QUEUE_ENTRY_LEN)
  837. sbus_free_consistent(qpti->sdev,
  838. QSIZE(RES_QUEUE_LEN),
  839. qpti->res_cpu, qpti->res_dvma);
  840. sbus_free_consistent(qpti->sdev,
  841. QSIZE(QLOGICPTI_REQ_QUEUE_LEN),
  842. qpti->req_cpu, qpti->req_dvma);
  843. #undef QSIZE
  844. sbus_iounmap(qpti->qregs, qpti->sdev->reg_addrs[0].reg_size);
  845. if (qpti->is_pti)
  846. sbus_iounmap(qpti->sreg, sizeof(unsigned char));
  847. return 0;
  848. }
  849. const char *qlogicpti_info(struct Scsi_Host *host)
  850. {
  851. static char buf[80];
  852. struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  853. sprintf(buf, "PTI Qlogic,ISP SBUS SCSI irq %s regs at %p",
  854. __irq_itoa(qpti->qhost->irq), qpti->qregs);
  855. return buf;
  856. }
  857. /* I am a certified frobtronicist. */
  858. static inline void marker_frob(struct Command_Entry *cmd)
  859. {
  860. struct Marker_Entry *marker = (struct Marker_Entry *) cmd;
  861. memset(marker, 0, sizeof(struct Marker_Entry));
  862. marker->hdr.entry_cnt = 1;
  863. marker->hdr.entry_type = ENTRY_MARKER;
  864. marker->modifier = SYNC_ALL;
  865. marker->rsvd = 0;
  866. }
  867. static inline void cmd_frob(struct Command_Entry *cmd, struct scsi_cmnd *Cmnd,
  868. struct qlogicpti *qpti)
  869. {
  870. memset(cmd, 0, sizeof(struct Command_Entry));
  871. cmd->hdr.entry_cnt = 1;
  872. cmd->hdr.entry_type = ENTRY_COMMAND;
  873. cmd->target_id = Cmnd->device->id;
  874. cmd->target_lun = Cmnd->device->lun;
  875. cmd->cdb_length = Cmnd->cmd_len;
  876. cmd->control_flags = 0;
  877. if (Cmnd->device->tagged_supported) {
  878. if (qpti->cmd_count[Cmnd->device->id] == 0)
  879. qpti->tag_ages[Cmnd->device->id] = jiffies;
  880. if (time_after(jiffies, qpti->tag_ages[Cmnd->device->id] + (5*HZ))) {
  881. cmd->control_flags = CFLAG_ORDERED_TAG;
  882. qpti->tag_ages[Cmnd->device->id] = jiffies;
  883. } else
  884. cmd->control_flags = CFLAG_SIMPLE_TAG;
  885. }
  886. if ((Cmnd->cmnd[0] == WRITE_6) ||
  887. (Cmnd->cmnd[0] == WRITE_10) ||
  888. (Cmnd->cmnd[0] == WRITE_12))
  889. cmd->control_flags |= CFLAG_WRITE;
  890. else
  891. cmd->control_flags |= CFLAG_READ;
  892. cmd->time_out = 30;
  893. memcpy(cmd->cdb, Cmnd->cmnd, Cmnd->cmd_len);
  894. }
  895. /* Do it to it baby. */
  896. static inline int load_cmd(struct scsi_cmnd *Cmnd, struct Command_Entry *cmd,
  897. struct qlogicpti *qpti, u_int in_ptr, u_int out_ptr)
  898. {
  899. struct dataseg *ds;
  900. struct scatterlist *sg;
  901. int i, n;
  902. if (Cmnd->use_sg) {
  903. int sg_count;
  904. sg = (struct scatterlist *) Cmnd->buffer;
  905. sg_count = sbus_map_sg(qpti->sdev, sg, Cmnd->use_sg, Cmnd->sc_data_direction);
  906. ds = cmd->dataseg;
  907. cmd->segment_cnt = sg_count;
  908. /* Fill in first four sg entries: */
  909. n = sg_count;
  910. if (n > 4)
  911. n = 4;
  912. for (i = 0; i < n; i++, sg++) {
  913. ds[i].d_base = sg_dma_address(sg);
  914. ds[i].d_count = sg_dma_len(sg);
  915. }
  916. sg_count -= 4;
  917. while (sg_count > 0) {
  918. struct Continuation_Entry *cont;
  919. ++cmd->hdr.entry_cnt;
  920. cont = (struct Continuation_Entry *) &qpti->req_cpu[in_ptr];
  921. in_ptr = NEXT_REQ_PTR(in_ptr);
  922. if (in_ptr == out_ptr)
  923. return -1;
  924. cont->hdr.entry_type = ENTRY_CONTINUATION;
  925. cont->hdr.entry_cnt = 0;
  926. cont->hdr.sys_def_1 = 0;
  927. cont->hdr.flags = 0;
  928. cont->reserved = 0;
  929. ds = cont->dataseg;
  930. n = sg_count;
  931. if (n > 7)
  932. n = 7;
  933. for (i = 0; i < n; i++, sg++) {
  934. ds[i].d_base = sg_dma_address(sg);
  935. ds[i].d_count = sg_dma_len(sg);
  936. }
  937. sg_count -= n;
  938. }
  939. } else if (Cmnd->request_bufflen) {
  940. Cmnd->SCp.ptr = (char *)(unsigned long)
  941. sbus_map_single(qpti->sdev,
  942. Cmnd->request_buffer,
  943. Cmnd->request_bufflen,
  944. Cmnd->sc_data_direction);
  945. cmd->dataseg[0].d_base = (u32) ((unsigned long)Cmnd->SCp.ptr);
  946. cmd->dataseg[0].d_count = Cmnd->request_bufflen;
  947. cmd->segment_cnt = 1;
  948. } else {
  949. cmd->dataseg[0].d_base = 0;
  950. cmd->dataseg[0].d_count = 0;
  951. cmd->segment_cnt = 1; /* Shouldn't this be 0? */
  952. }
  953. /* Committed, record Scsi_Cmd so we can find it later. */
  954. cmd->handle = in_ptr;
  955. qpti->cmd_slots[in_ptr] = Cmnd;
  956. qpti->cmd_count[Cmnd->device->id]++;
  957. sbus_writew(in_ptr, qpti->qregs + MBOX4);
  958. qpti->req_in_ptr = in_ptr;
  959. return in_ptr;
  960. }
  961. static inline void update_can_queue(struct Scsi_Host *host, u_int in_ptr, u_int out_ptr)
  962. {
  963. /* Temporary workaround until bug is found and fixed (one bug has been found
  964. already, but fixing it makes things even worse) -jj */
  965. int num_free = QLOGICPTI_REQ_QUEUE_LEN - REQ_QUEUE_DEPTH(in_ptr, out_ptr) - 64;
  966. host->can_queue = host->host_busy + num_free;
  967. host->sg_tablesize = QLOGICPTI_MAX_SG(num_free);
  968. }
  969. static unsigned int scsi_rbuf_get(struct scsi_cmnd *cmd, unsigned char **buf_out)
  970. {
  971. unsigned char *buf;
  972. unsigned int buflen;
  973. if (cmd->use_sg) {
  974. struct scatterlist *sg;
  975. sg = (struct scatterlist *) cmd->request_buffer;
  976. buf = kmap_atomic(sg->page, KM_IRQ0) + sg->offset;
  977. buflen = sg->length;
  978. } else {
  979. buf = cmd->request_buffer;
  980. buflen = cmd->request_bufflen;
  981. }
  982. *buf_out = buf;
  983. return buflen;
  984. }
  985. static void scsi_rbuf_put(struct scsi_cmnd *cmd, unsigned char *buf)
  986. {
  987. if (cmd->use_sg) {
  988. struct scatterlist *sg;
  989. sg = (struct scatterlist *) cmd->request_buffer;
  990. kunmap_atomic(buf - sg->offset, KM_IRQ0);
  991. }
  992. }
  993. /*
  994. * Until we scan the entire bus with inquiries, go throught this fella...
  995. */
  996. static void ourdone(struct scsi_cmnd *Cmnd)
  997. {
  998. struct qlogicpti *qpti = (struct qlogicpti *) Cmnd->device->host->hostdata;
  999. int tgt = Cmnd->device->id;
  1000. void (*done) (struct scsi_cmnd *);
  1001. /* This grot added by DaveM, blame him for ugliness.
  1002. * The issue is that in the 2.3.x driver we use the
  1003. * host_scribble portion of the scsi command as a
  1004. * completion linked list at interrupt service time,
  1005. * so we have to store the done function pointer elsewhere.
  1006. */
  1007. done = (void (*)(struct scsi_cmnd *))
  1008. (((unsigned long) Cmnd->SCp.Message)
  1009. #ifdef __sparc_v9__
  1010. | ((unsigned long) Cmnd->SCp.Status << 32UL)
  1011. #endif
  1012. );
  1013. if ((qpti->sbits & (1 << tgt)) == 0) {
  1014. int ok = host_byte(Cmnd->result) == DID_OK;
  1015. if (Cmnd->cmnd[0] == 0x12 && ok) {
  1016. unsigned char *iqd;
  1017. unsigned int iqd_len;
  1018. iqd_len = scsi_rbuf_get(Cmnd, &iqd);
  1019. /* tags handled in midlayer */
  1020. /* enable sync mode? */
  1021. if (iqd[7] & 0x10) {
  1022. qpti->dev_param[tgt].device_flags |= 0x10;
  1023. } else {
  1024. qpti->dev_param[tgt].synchronous_offset = 0;
  1025. qpti->dev_param[tgt].synchronous_period = 0;
  1026. }
  1027. /* are we wide capable? */
  1028. if (iqd[7] & 0x20) {
  1029. qpti->dev_param[tgt].device_flags |= 0x20;
  1030. }
  1031. scsi_rbuf_put(Cmnd, iqd);
  1032. qpti->sbits |= (1 << tgt);
  1033. } else if (!ok) {
  1034. qpti->sbits |= (1 << tgt);
  1035. }
  1036. }
  1037. done(Cmnd);
  1038. }
  1039. static int qlogicpti_queuecommand(struct scsi_cmnd *Cmnd, void (*done)(struct scsi_cmnd *));
  1040. static int qlogicpti_queuecommand_slow(struct scsi_cmnd *Cmnd,
  1041. void (*done)(struct scsi_cmnd *))
  1042. {
  1043. struct qlogicpti *qpti = (struct qlogicpti *) Cmnd->device->host->hostdata;
  1044. /*
  1045. * done checking this host adapter?
  1046. * If not, then rewrite the command
  1047. * to finish through ourdone so we
  1048. * can peek at Inquiry data results.
  1049. */
  1050. if (qpti->sbits && qpti->sbits != 0xffff) {
  1051. /* See above about in ourdone this ugliness... */
  1052. Cmnd->SCp.Message = ((unsigned long)done) & 0xffffffff;
  1053. #ifdef CONFIG_SPARC64
  1054. Cmnd->SCp.Status = ((unsigned long)done >> 32UL) & 0xffffffff;
  1055. #endif
  1056. return qlogicpti_queuecommand(Cmnd, ourdone);
  1057. }
  1058. /*
  1059. * We've peeked at all targets for this bus- time
  1060. * to set parameters for devices for real now.
  1061. */
  1062. if (qpti->sbits == 0xffff) {
  1063. int i;
  1064. for(i = 0; i < MAX_TARGETS; i++) {
  1065. u_short param[6];
  1066. param[0] = MBOX_SET_TARGET_PARAMS;
  1067. param[1] = (i << 8);
  1068. param[2] = (qpti->dev_param[i].device_flags << 8);
  1069. if (qpti->dev_param[i].device_flags & 0x10) {
  1070. param[3] = (qpti->dev_param[i].synchronous_offset << 8) |
  1071. qpti->dev_param[i].synchronous_period;
  1072. } else {
  1073. param[3] = 0;
  1074. }
  1075. (void) qlogicpti_mbox_command(qpti, param, 0);
  1076. }
  1077. /*
  1078. * set to zero so any traverse through ourdone
  1079. * doesn't start the whole process again,
  1080. */
  1081. qpti->sbits = 0;
  1082. }
  1083. /* check to see if we're done with all adapters... */
  1084. for (qpti = qptichain; qpti != NULL; qpti = qpti->next) {
  1085. if (qpti->sbits) {
  1086. break;
  1087. }
  1088. }
  1089. /*
  1090. * if we hit the end of the chain w/o finding adapters still
  1091. * capability-configuring, then we're done with all adapters
  1092. * and can rock on..
  1093. */
  1094. if (qpti == NULL)
  1095. Cmnd->device->host->hostt->queuecommand = qlogicpti_queuecommand;
  1096. return qlogicpti_queuecommand(Cmnd, done);
  1097. }
  1098. /*
  1099. * The middle SCSI layer ensures that queuecommand never gets invoked
  1100. * concurrently with itself or the interrupt handler (though the
  1101. * interrupt handler may call this routine as part of
  1102. * request-completion handling).
  1103. *
  1104. * "This code must fly." -davem
  1105. */
  1106. static int qlogicpti_queuecommand(struct scsi_cmnd *Cmnd, void (*done)(struct scsi_cmnd *))
  1107. {
  1108. struct Scsi_Host *host = Cmnd->device->host;
  1109. struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  1110. struct Command_Entry *cmd;
  1111. u_int out_ptr;
  1112. int in_ptr;
  1113. Cmnd->scsi_done = done;
  1114. in_ptr = qpti->req_in_ptr;
  1115. cmd = (struct Command_Entry *) &qpti->req_cpu[in_ptr];
  1116. out_ptr = sbus_readw(qpti->qregs + MBOX4);
  1117. in_ptr = NEXT_REQ_PTR(in_ptr);
  1118. if (in_ptr == out_ptr)
  1119. goto toss_command;
  1120. if (qpti->send_marker) {
  1121. marker_frob(cmd);
  1122. qpti->send_marker = 0;
  1123. if (NEXT_REQ_PTR(in_ptr) == out_ptr) {
  1124. sbus_writew(in_ptr, qpti->qregs + MBOX4);
  1125. qpti->req_in_ptr = in_ptr;
  1126. goto toss_command;
  1127. }
  1128. cmd = (struct Command_Entry *) &qpti->req_cpu[in_ptr];
  1129. in_ptr = NEXT_REQ_PTR(in_ptr);
  1130. }
  1131. cmd_frob(cmd, Cmnd, qpti);
  1132. if ((in_ptr = load_cmd(Cmnd, cmd, qpti, in_ptr, out_ptr)) == -1)
  1133. goto toss_command;
  1134. update_can_queue(host, in_ptr, out_ptr);
  1135. return 0;
  1136. toss_command:
  1137. printk(KERN_EMERG "qlogicpti%d: request queue overflow\n",
  1138. qpti->qpti_id);
  1139. /* Unfortunately, unless you use the new EH code, which
  1140. * we don't, the midlayer will ignore the return value,
  1141. * which is insane. We pick up the pieces like this.
  1142. */
  1143. Cmnd->result = DID_BUS_BUSY;
  1144. done(Cmnd);
  1145. return 1;
  1146. }
  1147. static int qlogicpti_return_status(struct Status_Entry *sts, int id)
  1148. {
  1149. int host_status = DID_ERROR;
  1150. switch (sts->completion_status) {
  1151. case CS_COMPLETE:
  1152. host_status = DID_OK;
  1153. break;
  1154. case CS_INCOMPLETE:
  1155. if (!(sts->state_flags & SF_GOT_BUS))
  1156. host_status = DID_NO_CONNECT;
  1157. else if (!(sts->state_flags & SF_GOT_TARGET))
  1158. host_status = DID_BAD_TARGET;
  1159. else if (!(sts->state_flags & SF_SENT_CDB))
  1160. host_status = DID_ERROR;
  1161. else if (!(sts->state_flags & SF_TRANSFERRED_DATA))
  1162. host_status = DID_ERROR;
  1163. else if (!(sts->state_flags & SF_GOT_STATUS))
  1164. host_status = DID_ERROR;
  1165. else if (!(sts->state_flags & SF_GOT_SENSE))
  1166. host_status = DID_ERROR;
  1167. break;
  1168. case CS_DMA_ERROR:
  1169. case CS_TRANSPORT_ERROR:
  1170. host_status = DID_ERROR;
  1171. break;
  1172. case CS_RESET_OCCURRED:
  1173. case CS_BUS_RESET:
  1174. host_status = DID_RESET;
  1175. break;
  1176. case CS_ABORTED:
  1177. host_status = DID_ABORT;
  1178. break;
  1179. case CS_TIMEOUT:
  1180. host_status = DID_TIME_OUT;
  1181. break;
  1182. case CS_DATA_OVERRUN:
  1183. case CS_COMMAND_OVERRUN:
  1184. case CS_STATUS_OVERRUN:
  1185. case CS_BAD_MESSAGE:
  1186. case CS_NO_MESSAGE_OUT:
  1187. case CS_EXT_ID_FAILED:
  1188. case CS_IDE_MSG_FAILED:
  1189. case CS_ABORT_MSG_FAILED:
  1190. case CS_NOP_MSG_FAILED:
  1191. case CS_PARITY_ERROR_MSG_FAILED:
  1192. case CS_DEVICE_RESET_MSG_FAILED:
  1193. case CS_ID_MSG_FAILED:
  1194. case CS_UNEXP_BUS_FREE:
  1195. host_status = DID_ERROR;
  1196. break;
  1197. case CS_DATA_UNDERRUN:
  1198. host_status = DID_OK;
  1199. break;
  1200. default:
  1201. printk(KERN_EMERG "qpti%d: unknown completion status 0x%04x\n",
  1202. id, sts->completion_status);
  1203. host_status = DID_ERROR;
  1204. break;
  1205. }
  1206. return (sts->scsi_status & STATUS_MASK) | (host_status << 16);
  1207. }
  1208. static struct scsi_cmnd *qlogicpti_intr_handler(struct qlogicpti *qpti)
  1209. {
  1210. struct scsi_cmnd *Cmnd, *done_queue = NULL;
  1211. struct Status_Entry *sts;
  1212. u_int in_ptr, out_ptr;
  1213. if (!(sbus_readw(qpti->qregs + SBUS_STAT) & SBUS_STAT_RINT))
  1214. return NULL;
  1215. in_ptr = sbus_readw(qpti->qregs + MBOX5);
  1216. sbus_writew(HCCTRL_CRIRQ, qpti->qregs + HCCTRL);
  1217. if (sbus_readw(qpti->qregs + SBUS_SEMAPHORE) & SBUS_SEMAPHORE_LCK) {
  1218. switch (sbus_readw(qpti->qregs + MBOX0)) {
  1219. case ASYNC_SCSI_BUS_RESET:
  1220. case EXECUTION_TIMEOUT_RESET:
  1221. qpti->send_marker = 1;
  1222. break;
  1223. case INVALID_COMMAND:
  1224. case HOST_INTERFACE_ERROR:
  1225. case COMMAND_ERROR:
  1226. case COMMAND_PARAM_ERROR:
  1227. break;
  1228. };
  1229. sbus_writew(0, qpti->qregs + SBUS_SEMAPHORE);
  1230. }
  1231. /* This looks like a network driver! */
  1232. out_ptr = qpti->res_out_ptr;
  1233. while (out_ptr != in_ptr) {
  1234. u_int cmd_slot;
  1235. sts = (struct Status_Entry *) &qpti->res_cpu[out_ptr];
  1236. out_ptr = NEXT_RES_PTR(out_ptr);
  1237. /* We store an index in the handle, not the pointer in
  1238. * some form. This avoids problems due to the fact
  1239. * that the handle provided is only 32-bits. -DaveM
  1240. */
  1241. cmd_slot = sts->handle;
  1242. Cmnd = qpti->cmd_slots[cmd_slot];
  1243. qpti->cmd_slots[cmd_slot] = NULL;
  1244. if (sts->completion_status == CS_RESET_OCCURRED ||
  1245. sts->completion_status == CS_ABORTED ||
  1246. (sts->status_flags & STF_BUS_RESET))
  1247. qpti->send_marker = 1;
  1248. if (sts->state_flags & SF_GOT_SENSE)
  1249. memcpy(Cmnd->sense_buffer, sts->req_sense_data,
  1250. sizeof(Cmnd->sense_buffer));
  1251. if (sts->hdr.entry_type == ENTRY_STATUS)
  1252. Cmnd->result =
  1253. qlogicpti_return_status(sts, qpti->qpti_id);
  1254. else
  1255. Cmnd->result = DID_ERROR << 16;
  1256. if (Cmnd->use_sg) {
  1257. sbus_unmap_sg(qpti->sdev,
  1258. (struct scatterlist *)Cmnd->buffer,
  1259. Cmnd->use_sg,
  1260. Cmnd->sc_data_direction);
  1261. } else {
  1262. sbus_unmap_single(qpti->sdev,
  1263. (__u32)((unsigned long)Cmnd->SCp.ptr),
  1264. Cmnd->request_bufflen,
  1265. Cmnd->sc_data_direction);
  1266. }
  1267. qpti->cmd_count[Cmnd->device->id]--;
  1268. sbus_writew(out_ptr, qpti->qregs + MBOX5);
  1269. Cmnd->host_scribble = (unsigned char *) done_queue;
  1270. done_queue = Cmnd;
  1271. }
  1272. qpti->res_out_ptr = out_ptr;
  1273. return done_queue;
  1274. }
  1275. static irqreturn_t qpti_intr(int irq, void *dev_id, struct pt_regs *regs)
  1276. {
  1277. struct qlogicpti *qpti = dev_id;
  1278. unsigned long flags;
  1279. struct scsi_cmnd *dq;
  1280. spin_lock_irqsave(qpti->qhost->host_lock, flags);
  1281. dq = qlogicpti_intr_handler(qpti);
  1282. if (dq != NULL) {
  1283. do {
  1284. struct scsi_cmnd *next;
  1285. next = (struct scsi_cmnd *) dq->host_scribble;
  1286. dq->scsi_done(dq);
  1287. dq = next;
  1288. } while (dq != NULL);
  1289. }
  1290. spin_unlock_irqrestore(qpti->qhost->host_lock, flags);
  1291. return IRQ_HANDLED;
  1292. }
  1293. static int qlogicpti_abort(struct scsi_cmnd *Cmnd)
  1294. {
  1295. u_short param[6];
  1296. struct Scsi_Host *host = Cmnd->device->host;
  1297. struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  1298. int return_status = SUCCESS;
  1299. u32 cmd_cookie;
  1300. int i;
  1301. printk(KERN_WARNING "qlogicpti : Aborting cmd for tgt[%d] lun[%d]\n",
  1302. (int)Cmnd->device->id, (int)Cmnd->device->lun);
  1303. qlogicpti_disable_irqs(qpti);
  1304. /* Find the 32-bit cookie we gave to the firmware for
  1305. * this command.
  1306. */
  1307. for (i = 0; i < QLOGICPTI_REQ_QUEUE_LEN + 1; i++)
  1308. if (qpti->cmd_slots[i] == Cmnd)
  1309. break;
  1310. cmd_cookie = i;
  1311. param[0] = MBOX_ABORT;
  1312. param[1] = (((u_short) Cmnd->device->id) << 8) | Cmnd->device->lun;
  1313. param[2] = cmd_cookie >> 16;
  1314. param[3] = cmd_cookie & 0xffff;
  1315. if (qlogicpti_mbox_command(qpti, param, 0) ||
  1316. (param[0] != MBOX_COMMAND_COMPLETE)) {
  1317. printk(KERN_EMERG "qlogicpti : scsi abort failure: %x\n", param[0]);
  1318. return_status = FAILED;
  1319. }
  1320. qlogicpti_enable_irqs(qpti);
  1321. return return_status;
  1322. }
  1323. static int qlogicpti_reset(struct scsi_cmnd *Cmnd)
  1324. {
  1325. u_short param[6];
  1326. struct Scsi_Host *host = Cmnd->device->host;
  1327. struct qlogicpti *qpti = (struct qlogicpti *) host->hostdata;
  1328. int return_status = SUCCESS;
  1329. printk(KERN_WARNING "qlogicpti : Resetting SCSI bus!\n");
  1330. qlogicpti_disable_irqs(qpti);
  1331. param[0] = MBOX_BUS_RESET;
  1332. param[1] = qpti->host_param.bus_reset_delay;
  1333. if (qlogicpti_mbox_command(qpti, param, 0) ||
  1334. (param[0] != MBOX_COMMAND_COMPLETE)) {
  1335. printk(KERN_EMERG "qlogicisp : scsi bus reset failure: %x\n", param[0]);
  1336. return_status = FAILED;
  1337. }
  1338. qlogicpti_enable_irqs(qpti);
  1339. return return_status;
  1340. }
  1341. static struct scsi_host_template driver_template = {
  1342. .detect = qlogicpti_detect,
  1343. .release = qlogicpti_release,
  1344. .info = qlogicpti_info,
  1345. .queuecommand = qlogicpti_queuecommand_slow,
  1346. .eh_abort_handler = qlogicpti_abort,
  1347. .eh_bus_reset_handler = qlogicpti_reset,
  1348. .can_queue = QLOGICPTI_REQ_QUEUE_LEN,
  1349. .this_id = 7,
  1350. .sg_tablesize = QLOGICPTI_MAX_SG(QLOGICPTI_REQ_QUEUE_LEN),
  1351. .cmd_per_lun = 1,
  1352. .use_clustering = ENABLE_CLUSTERING,
  1353. };
  1354. #include "scsi_module.c"
  1355. MODULE_LICENSE("GPL");