qla_sup.c 56 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2005 QLogic Corporation
  4. *
  5. * See LICENSE.qla2xxx for copyright and licensing details.
  6. */
  7. #include "qla_def.h"
  8. #include <linux/delay.h>
  9. #include <asm/uaccess.h>
  10. static uint16_t qla2x00_nvram_request(scsi_qla_host_t *, uint32_t);
  11. static void qla2x00_nv_deselect(scsi_qla_host_t *);
  12. static void qla2x00_nv_write(scsi_qla_host_t *, uint16_t);
  13. /*
  14. * NVRAM support routines
  15. */
  16. /**
  17. * qla2x00_lock_nvram_access() -
  18. * @ha: HA context
  19. */
  20. void
  21. qla2x00_lock_nvram_access(scsi_qla_host_t *ha)
  22. {
  23. uint16_t data;
  24. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  25. if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
  26. data = RD_REG_WORD(&reg->nvram);
  27. while (data & NVR_BUSY) {
  28. udelay(100);
  29. data = RD_REG_WORD(&reg->nvram);
  30. }
  31. /* Lock resource */
  32. WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0x1);
  33. RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  34. udelay(5);
  35. data = RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  36. while ((data & BIT_0) == 0) {
  37. /* Lock failed */
  38. udelay(100);
  39. WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0x1);
  40. RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  41. udelay(5);
  42. data = RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  43. }
  44. }
  45. }
  46. /**
  47. * qla2x00_unlock_nvram_access() -
  48. * @ha: HA context
  49. */
  50. void
  51. qla2x00_unlock_nvram_access(scsi_qla_host_t *ha)
  52. {
  53. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  54. if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
  55. WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0);
  56. RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  57. }
  58. }
  59. /**
  60. * qla2x00_get_nvram_word() - Calculates word position in NVRAM and calls the
  61. * request routine to get the word from NVRAM.
  62. * @ha: HA context
  63. * @addr: Address in NVRAM to read
  64. *
  65. * Returns the word read from nvram @addr.
  66. */
  67. uint16_t
  68. qla2x00_get_nvram_word(scsi_qla_host_t *ha, uint32_t addr)
  69. {
  70. uint16_t data;
  71. uint32_t nv_cmd;
  72. nv_cmd = addr << 16;
  73. nv_cmd |= NV_READ_OP;
  74. data = qla2x00_nvram_request(ha, nv_cmd);
  75. return (data);
  76. }
  77. /**
  78. * qla2x00_write_nvram_word() - Write NVRAM data.
  79. * @ha: HA context
  80. * @addr: Address in NVRAM to write
  81. * @data: word to program
  82. */
  83. void
  84. qla2x00_write_nvram_word(scsi_qla_host_t *ha, uint32_t addr, uint16_t data)
  85. {
  86. int count;
  87. uint16_t word;
  88. uint32_t nv_cmd, wait_cnt;
  89. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  90. qla2x00_nv_write(ha, NVR_DATA_OUT);
  91. qla2x00_nv_write(ha, 0);
  92. qla2x00_nv_write(ha, 0);
  93. for (word = 0; word < 8; word++)
  94. qla2x00_nv_write(ha, NVR_DATA_OUT);
  95. qla2x00_nv_deselect(ha);
  96. /* Write data */
  97. nv_cmd = (addr << 16) | NV_WRITE_OP;
  98. nv_cmd |= data;
  99. nv_cmd <<= 5;
  100. for (count = 0; count < 27; count++) {
  101. if (nv_cmd & BIT_31)
  102. qla2x00_nv_write(ha, NVR_DATA_OUT);
  103. else
  104. qla2x00_nv_write(ha, 0);
  105. nv_cmd <<= 1;
  106. }
  107. qla2x00_nv_deselect(ha);
  108. /* Wait for NVRAM to become ready */
  109. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  110. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  111. wait_cnt = NVR_WAIT_CNT;
  112. do {
  113. if (!--wait_cnt) {
  114. DEBUG9_10(printk("%s(%ld): NVRAM didn't go ready...\n",
  115. __func__, ha->host_no));
  116. break;
  117. }
  118. NVRAM_DELAY();
  119. word = RD_REG_WORD(&reg->nvram);
  120. } while ((word & NVR_DATA_IN) == 0);
  121. qla2x00_nv_deselect(ha);
  122. /* Disable writes */
  123. qla2x00_nv_write(ha, NVR_DATA_OUT);
  124. for (count = 0; count < 10; count++)
  125. qla2x00_nv_write(ha, 0);
  126. qla2x00_nv_deselect(ha);
  127. }
  128. static int
  129. qla2x00_write_nvram_word_tmo(scsi_qla_host_t *ha, uint32_t addr, uint16_t data,
  130. uint32_t tmo)
  131. {
  132. int ret, count;
  133. uint16_t word;
  134. uint32_t nv_cmd;
  135. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  136. ret = QLA_SUCCESS;
  137. qla2x00_nv_write(ha, NVR_DATA_OUT);
  138. qla2x00_nv_write(ha, 0);
  139. qla2x00_nv_write(ha, 0);
  140. for (word = 0; word < 8; word++)
  141. qla2x00_nv_write(ha, NVR_DATA_OUT);
  142. qla2x00_nv_deselect(ha);
  143. /* Write data */
  144. nv_cmd = (addr << 16) | NV_WRITE_OP;
  145. nv_cmd |= data;
  146. nv_cmd <<= 5;
  147. for (count = 0; count < 27; count++) {
  148. if (nv_cmd & BIT_31)
  149. qla2x00_nv_write(ha, NVR_DATA_OUT);
  150. else
  151. qla2x00_nv_write(ha, 0);
  152. nv_cmd <<= 1;
  153. }
  154. qla2x00_nv_deselect(ha);
  155. /* Wait for NVRAM to become ready */
  156. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  157. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  158. do {
  159. NVRAM_DELAY();
  160. word = RD_REG_WORD(&reg->nvram);
  161. if (!--tmo) {
  162. ret = QLA_FUNCTION_FAILED;
  163. break;
  164. }
  165. } while ((word & NVR_DATA_IN) == 0);
  166. qla2x00_nv_deselect(ha);
  167. /* Disable writes */
  168. qla2x00_nv_write(ha, NVR_DATA_OUT);
  169. for (count = 0; count < 10; count++)
  170. qla2x00_nv_write(ha, 0);
  171. qla2x00_nv_deselect(ha);
  172. return ret;
  173. }
  174. /**
  175. * qla2x00_nvram_request() - Sends read command to NVRAM and gets data from
  176. * NVRAM.
  177. * @ha: HA context
  178. * @nv_cmd: NVRAM command
  179. *
  180. * Bit definitions for NVRAM command:
  181. *
  182. * Bit 26 = start bit
  183. * Bit 25, 24 = opcode
  184. * Bit 23-16 = address
  185. * Bit 15-0 = write data
  186. *
  187. * Returns the word read from nvram @addr.
  188. */
  189. static uint16_t
  190. qla2x00_nvram_request(scsi_qla_host_t *ha, uint32_t nv_cmd)
  191. {
  192. uint8_t cnt;
  193. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  194. uint16_t data = 0;
  195. uint16_t reg_data;
  196. /* Send command to NVRAM. */
  197. nv_cmd <<= 5;
  198. for (cnt = 0; cnt < 11; cnt++) {
  199. if (nv_cmd & BIT_31)
  200. qla2x00_nv_write(ha, NVR_DATA_OUT);
  201. else
  202. qla2x00_nv_write(ha, 0);
  203. nv_cmd <<= 1;
  204. }
  205. /* Read data from NVRAM. */
  206. for (cnt = 0; cnt < 16; cnt++) {
  207. WRT_REG_WORD(&reg->nvram, NVR_SELECT | NVR_CLOCK);
  208. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  209. NVRAM_DELAY();
  210. data <<= 1;
  211. reg_data = RD_REG_WORD(&reg->nvram);
  212. if (reg_data & NVR_DATA_IN)
  213. data |= BIT_0;
  214. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  215. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  216. NVRAM_DELAY();
  217. }
  218. /* Deselect chip. */
  219. WRT_REG_WORD(&reg->nvram, NVR_DESELECT);
  220. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  221. NVRAM_DELAY();
  222. return (data);
  223. }
  224. /**
  225. * qla2x00_nv_write() - Clean NVRAM operations.
  226. * @ha: HA context
  227. */
  228. static void
  229. qla2x00_nv_deselect(scsi_qla_host_t *ha)
  230. {
  231. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  232. WRT_REG_WORD(&reg->nvram, NVR_DESELECT);
  233. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  234. NVRAM_DELAY();
  235. }
  236. /**
  237. * qla2x00_nv_write() - Prepare for NVRAM read/write operation.
  238. * @ha: HA context
  239. * @data: Serial interface selector
  240. */
  241. static void
  242. qla2x00_nv_write(scsi_qla_host_t *ha, uint16_t data)
  243. {
  244. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  245. WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
  246. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  247. NVRAM_DELAY();
  248. WRT_REG_WORD(&reg->nvram, data | NVR_SELECT| NVR_CLOCK |
  249. NVR_WRT_ENABLE);
  250. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  251. NVRAM_DELAY();
  252. WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
  253. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  254. NVRAM_DELAY();
  255. }
  256. /**
  257. * qla2x00_clear_nvram_protection() -
  258. * @ha: HA context
  259. */
  260. static int
  261. qla2x00_clear_nvram_protection(scsi_qla_host_t *ha)
  262. {
  263. int ret, stat;
  264. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  265. uint32_t word, wait_cnt;
  266. uint16_t wprot, wprot_old;
  267. /* Clear NVRAM write protection. */
  268. ret = QLA_FUNCTION_FAILED;
  269. wprot_old = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
  270. stat = qla2x00_write_nvram_word_tmo(ha, ha->nvram_base,
  271. __constant_cpu_to_le16(0x1234), 100000);
  272. wprot = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
  273. if (stat != QLA_SUCCESS || wprot != 0x1234) {
  274. /* Write enable. */
  275. qla2x00_nv_write(ha, NVR_DATA_OUT);
  276. qla2x00_nv_write(ha, 0);
  277. qla2x00_nv_write(ha, 0);
  278. for (word = 0; word < 8; word++)
  279. qla2x00_nv_write(ha, NVR_DATA_OUT);
  280. qla2x00_nv_deselect(ha);
  281. /* Enable protection register. */
  282. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  283. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  284. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  285. for (word = 0; word < 8; word++)
  286. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  287. qla2x00_nv_deselect(ha);
  288. /* Clear protection register (ffff is cleared). */
  289. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  290. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  291. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  292. for (word = 0; word < 8; word++)
  293. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  294. qla2x00_nv_deselect(ha);
  295. /* Wait for NVRAM to become ready. */
  296. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  297. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  298. wait_cnt = NVR_WAIT_CNT;
  299. do {
  300. if (!--wait_cnt) {
  301. DEBUG9_10(printk("%s(%ld): NVRAM didn't go "
  302. "ready...\n", __func__,
  303. ha->host_no));
  304. break;
  305. }
  306. NVRAM_DELAY();
  307. word = RD_REG_WORD(&reg->nvram);
  308. } while ((word & NVR_DATA_IN) == 0);
  309. if (wait_cnt)
  310. ret = QLA_SUCCESS;
  311. } else
  312. qla2x00_write_nvram_word(ha, ha->nvram_base, wprot_old);
  313. return ret;
  314. }
  315. static void
  316. qla2x00_set_nvram_protection(scsi_qla_host_t *ha, int stat)
  317. {
  318. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  319. uint32_t word, wait_cnt;
  320. if (stat != QLA_SUCCESS)
  321. return;
  322. /* Set NVRAM write protection. */
  323. /* Write enable. */
  324. qla2x00_nv_write(ha, NVR_DATA_OUT);
  325. qla2x00_nv_write(ha, 0);
  326. qla2x00_nv_write(ha, 0);
  327. for (word = 0; word < 8; word++)
  328. qla2x00_nv_write(ha, NVR_DATA_OUT);
  329. qla2x00_nv_deselect(ha);
  330. /* Enable protection register. */
  331. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  332. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  333. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  334. for (word = 0; word < 8; word++)
  335. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  336. qla2x00_nv_deselect(ha);
  337. /* Enable protection register. */
  338. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  339. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  340. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  341. for (word = 0; word < 8; word++)
  342. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  343. qla2x00_nv_deselect(ha);
  344. /* Wait for NVRAM to become ready. */
  345. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  346. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  347. wait_cnt = NVR_WAIT_CNT;
  348. do {
  349. if (!--wait_cnt) {
  350. DEBUG9_10(printk("%s(%ld): NVRAM didn't go ready...\n",
  351. __func__, ha->host_no));
  352. break;
  353. }
  354. NVRAM_DELAY();
  355. word = RD_REG_WORD(&reg->nvram);
  356. } while ((word & NVR_DATA_IN) == 0);
  357. }
  358. /*****************************************************************************/
  359. /* Flash Manipulation Routines */
  360. /*****************************************************************************/
  361. #define OPTROM_BURST_SIZE 0x1000
  362. #define OPTROM_BURST_DWORDS (OPTROM_BURST_SIZE / 4)
  363. static inline uint32_t
  364. flash_conf_to_access_addr(uint32_t faddr)
  365. {
  366. return FARX_ACCESS_FLASH_CONF | faddr;
  367. }
  368. static inline uint32_t
  369. flash_data_to_access_addr(uint32_t faddr)
  370. {
  371. return FARX_ACCESS_FLASH_DATA | faddr;
  372. }
  373. static inline uint32_t
  374. nvram_conf_to_access_addr(uint32_t naddr)
  375. {
  376. return FARX_ACCESS_NVRAM_CONF | naddr;
  377. }
  378. static inline uint32_t
  379. nvram_data_to_access_addr(uint32_t naddr)
  380. {
  381. return FARX_ACCESS_NVRAM_DATA | naddr;
  382. }
  383. static uint32_t
  384. qla24xx_read_flash_dword(scsi_qla_host_t *ha, uint32_t addr)
  385. {
  386. int rval;
  387. uint32_t cnt, data;
  388. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  389. WRT_REG_DWORD(&reg->flash_addr, addr & ~FARX_DATA_FLAG);
  390. /* Wait for READ cycle to complete. */
  391. rval = QLA_SUCCESS;
  392. for (cnt = 3000;
  393. (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) == 0 &&
  394. rval == QLA_SUCCESS; cnt--) {
  395. if (cnt)
  396. udelay(10);
  397. else
  398. rval = QLA_FUNCTION_TIMEOUT;
  399. cond_resched();
  400. }
  401. /* TODO: What happens if we time out? */
  402. data = 0xDEADDEAD;
  403. if (rval == QLA_SUCCESS)
  404. data = RD_REG_DWORD(&reg->flash_data);
  405. return data;
  406. }
  407. uint32_t *
  408. qla24xx_read_flash_data(scsi_qla_host_t *ha, uint32_t *dwptr, uint32_t faddr,
  409. uint32_t dwords)
  410. {
  411. uint32_t i;
  412. /* Dword reads to flash. */
  413. for (i = 0; i < dwords; i++, faddr++)
  414. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  415. flash_data_to_access_addr(faddr)));
  416. return dwptr;
  417. }
  418. static int
  419. qla24xx_write_flash_dword(scsi_qla_host_t *ha, uint32_t addr, uint32_t data)
  420. {
  421. int rval;
  422. uint32_t cnt;
  423. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  424. WRT_REG_DWORD(&reg->flash_data, data);
  425. RD_REG_DWORD(&reg->flash_data); /* PCI Posting. */
  426. WRT_REG_DWORD(&reg->flash_addr, addr | FARX_DATA_FLAG);
  427. /* Wait for Write cycle to complete. */
  428. rval = QLA_SUCCESS;
  429. for (cnt = 500000; (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) &&
  430. rval == QLA_SUCCESS; cnt--) {
  431. if (cnt)
  432. udelay(10);
  433. else
  434. rval = QLA_FUNCTION_TIMEOUT;
  435. cond_resched();
  436. }
  437. return rval;
  438. }
  439. static void
  440. qla24xx_get_flash_manufacturer(scsi_qla_host_t *ha, uint8_t *man_id,
  441. uint8_t *flash_id)
  442. {
  443. uint32_t ids;
  444. ids = qla24xx_read_flash_dword(ha, flash_data_to_access_addr(0xd03ab));
  445. *man_id = LSB(ids);
  446. *flash_id = MSB(ids);
  447. /* Check if man_id and flash_id are valid. */
  448. if (ids != 0xDEADDEAD && (*man_id == 0 || *flash_id == 0)) {
  449. /* Read information using 0x9f opcode
  450. * Device ID, Mfg ID would be read in the format:
  451. * <Ext Dev Info><Device ID Part2><Device ID Part 1><Mfg ID>
  452. * Example: ATMEL 0x00 01 45 1F
  453. * Extract MFG and Dev ID from last two bytes.
  454. */
  455. ids = qla24xx_read_flash_dword(ha,
  456. flash_data_to_access_addr(0xd009f));
  457. *man_id = LSB(ids);
  458. *flash_id = MSB(ids);
  459. }
  460. }
  461. static int
  462. qla24xx_write_flash_data(scsi_qla_host_t *ha, uint32_t *dwptr, uint32_t faddr,
  463. uint32_t dwords)
  464. {
  465. int ret;
  466. uint32_t liter, miter;
  467. uint32_t sec_mask, rest_addr, conf_addr;
  468. uint32_t fdata, findex ;
  469. uint8_t man_id, flash_id;
  470. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  471. dma_addr_t optrom_dma;
  472. void *optrom = NULL;
  473. uint32_t *s, *d;
  474. ret = QLA_SUCCESS;
  475. /* Prepare burst-capable write on supported ISPs. */
  476. if (IS_QLA25XX(ha) && !(faddr & 0xfff) &&
  477. dwords > OPTROM_BURST_DWORDS) {
  478. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  479. &optrom_dma, GFP_KERNEL);
  480. if (!optrom) {
  481. qla_printk(KERN_DEBUG, ha,
  482. "Unable to allocate memory for optrom burst write "
  483. "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
  484. }
  485. }
  486. qla24xx_get_flash_manufacturer(ha, &man_id, &flash_id);
  487. DEBUG9(printk("%s(%ld): Flash man_id=%d flash_id=%d\n", __func__,
  488. ha->host_no, man_id, flash_id));
  489. conf_addr = flash_conf_to_access_addr(0x03d8);
  490. switch (man_id) {
  491. case 0xbf: /* STT flash. */
  492. if (flash_id == 0x8e) {
  493. rest_addr = 0x3fff;
  494. sec_mask = 0x7c000;
  495. } else {
  496. rest_addr = 0x1fff;
  497. sec_mask = 0x7e000;
  498. }
  499. if (flash_id == 0x80)
  500. conf_addr = flash_conf_to_access_addr(0x0352);
  501. break;
  502. case 0x13: /* ST M25P80. */
  503. rest_addr = 0x3fff;
  504. sec_mask = 0x7c000;
  505. break;
  506. case 0x1f: // Atmel 26DF081A
  507. rest_addr = 0x3fff;
  508. sec_mask = 0x7c000;
  509. conf_addr = flash_conf_to_access_addr(0x0320);
  510. break;
  511. default:
  512. /* Default to 64 kb sector size. */
  513. rest_addr = 0x3fff;
  514. sec_mask = 0x7c000;
  515. break;
  516. }
  517. /* Enable flash write. */
  518. WRT_REG_DWORD(&reg->ctrl_status,
  519. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  520. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  521. /* Disable flash write-protection. */
  522. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0);
  523. /* Some flash parts need an additional zero-write to clear bits.*/
  524. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0);
  525. for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
  526. if (man_id == 0x1f) {
  527. findex = faddr << 2;
  528. fdata = findex & sec_mask;
  529. } else {
  530. findex = faddr;
  531. fdata = (findex & sec_mask) << 2;
  532. }
  533. /* Are we at the beginning of a sector? */
  534. if ((findex & rest_addr) == 0) {
  535. /* Do sector unprotect at 4K boundry for Atmel part. */
  536. if (man_id == 0x1f)
  537. qla24xx_write_flash_dword(ha,
  538. flash_conf_to_access_addr(0x0339),
  539. (fdata & 0xff00) | ((fdata << 16) &
  540. 0xff0000) | ((fdata >> 16) & 0xff));
  541. ret = qla24xx_write_flash_dword(ha, conf_addr,
  542. (fdata & 0xff00) |((fdata << 16) &
  543. 0xff0000) | ((fdata >> 16) & 0xff));
  544. if (ret != QLA_SUCCESS) {
  545. DEBUG9(printk("%s(%ld) Unable to flash "
  546. "sector: address=%x.\n", __func__,
  547. ha->host_no, faddr));
  548. break;
  549. }
  550. }
  551. /* Go with burst-write. */
  552. if (optrom && (liter + OPTROM_BURST_DWORDS) < dwords) {
  553. /* Copy data to DMA'ble buffer. */
  554. for (miter = 0, s = optrom, d = dwptr;
  555. miter < OPTROM_BURST_DWORDS; miter++, s++, d++)
  556. *s = cpu_to_le32(*d);
  557. ret = qla2x00_load_ram(ha, optrom_dma,
  558. flash_data_to_access_addr(faddr),
  559. OPTROM_BURST_DWORDS);
  560. if (ret != QLA_SUCCESS) {
  561. qla_printk(KERN_WARNING, ha,
  562. "Unable to burst-write optrom segment "
  563. "(%x/%x/%llx).\n", ret,
  564. flash_data_to_access_addr(faddr),
  565. (unsigned long long)optrom_dma);
  566. qla_printk(KERN_WARNING, ha,
  567. "Reverting to slow-write.\n");
  568. dma_free_coherent(&ha->pdev->dev,
  569. OPTROM_BURST_SIZE, optrom, optrom_dma);
  570. optrom = NULL;
  571. } else {
  572. liter += OPTROM_BURST_DWORDS - 1;
  573. faddr += OPTROM_BURST_DWORDS - 1;
  574. dwptr += OPTROM_BURST_DWORDS - 1;
  575. continue;
  576. }
  577. }
  578. ret = qla24xx_write_flash_dword(ha,
  579. flash_data_to_access_addr(faddr), cpu_to_le32(*dwptr));
  580. if (ret != QLA_SUCCESS) {
  581. DEBUG9(printk("%s(%ld) Unable to program flash "
  582. "address=%x data=%x.\n", __func__,
  583. ha->host_no, faddr, *dwptr));
  584. break;
  585. }
  586. /* Do sector protect at 4K boundry for Atmel part. */
  587. if (man_id == 0x1f &&
  588. ((faddr & rest_addr) == rest_addr))
  589. qla24xx_write_flash_dword(ha,
  590. flash_conf_to_access_addr(0x0336),
  591. (fdata & 0xff00) | ((fdata << 16) &
  592. 0xff0000) | ((fdata >> 16) & 0xff));
  593. }
  594. /* Enable flash write-protection. */
  595. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0x9c);
  596. /* Disable flash write. */
  597. WRT_REG_DWORD(&reg->ctrl_status,
  598. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  599. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  600. if (optrom)
  601. dma_free_coherent(&ha->pdev->dev,
  602. OPTROM_BURST_SIZE, optrom, optrom_dma);
  603. return ret;
  604. }
  605. uint8_t *
  606. qla2x00_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  607. uint32_t bytes)
  608. {
  609. uint32_t i;
  610. uint16_t *wptr;
  611. /* Word reads to NVRAM via registers. */
  612. wptr = (uint16_t *)buf;
  613. qla2x00_lock_nvram_access(ha);
  614. for (i = 0; i < bytes >> 1; i++, naddr++)
  615. wptr[i] = cpu_to_le16(qla2x00_get_nvram_word(ha,
  616. naddr));
  617. qla2x00_unlock_nvram_access(ha);
  618. return buf;
  619. }
  620. uint8_t *
  621. qla24xx_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  622. uint32_t bytes)
  623. {
  624. uint32_t i;
  625. uint32_t *dwptr;
  626. /* Dword reads to flash. */
  627. dwptr = (uint32_t *)buf;
  628. for (i = 0; i < bytes >> 2; i++, naddr++)
  629. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  630. nvram_data_to_access_addr(naddr)));
  631. return buf;
  632. }
  633. int
  634. qla2x00_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  635. uint32_t bytes)
  636. {
  637. int ret, stat;
  638. uint32_t i;
  639. uint16_t *wptr;
  640. ret = QLA_SUCCESS;
  641. qla2x00_lock_nvram_access(ha);
  642. /* Disable NVRAM write-protection. */
  643. stat = qla2x00_clear_nvram_protection(ha);
  644. wptr = (uint16_t *)buf;
  645. for (i = 0; i < bytes >> 1; i++, naddr++) {
  646. qla2x00_write_nvram_word(ha, naddr,
  647. cpu_to_le16(*wptr));
  648. wptr++;
  649. }
  650. /* Enable NVRAM write-protection. */
  651. qla2x00_set_nvram_protection(ha, stat);
  652. qla2x00_unlock_nvram_access(ha);
  653. return ret;
  654. }
  655. int
  656. qla24xx_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  657. uint32_t bytes)
  658. {
  659. int ret;
  660. uint32_t i;
  661. uint32_t *dwptr;
  662. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  663. ret = QLA_SUCCESS;
  664. /* Enable flash write. */
  665. WRT_REG_DWORD(&reg->ctrl_status,
  666. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  667. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  668. /* Disable NVRAM write-protection. */
  669. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  670. 0);
  671. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  672. 0);
  673. /* Dword writes to flash. */
  674. dwptr = (uint32_t *)buf;
  675. for (i = 0; i < bytes >> 2; i++, naddr++, dwptr++) {
  676. ret = qla24xx_write_flash_dword(ha,
  677. nvram_data_to_access_addr(naddr),
  678. cpu_to_le32(*dwptr));
  679. if (ret != QLA_SUCCESS) {
  680. DEBUG9(printk("%s(%ld) Unable to program "
  681. "nvram address=%x data=%x.\n", __func__,
  682. ha->host_no, naddr, *dwptr));
  683. break;
  684. }
  685. }
  686. /* Enable NVRAM write-protection. */
  687. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  688. 0x8c);
  689. /* Disable flash write. */
  690. WRT_REG_DWORD(&reg->ctrl_status,
  691. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  692. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  693. return ret;
  694. }
  695. uint8_t *
  696. qla25xx_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  697. uint32_t bytes)
  698. {
  699. uint32_t i;
  700. uint32_t *dwptr;
  701. /* Dword reads to flash. */
  702. dwptr = (uint32_t *)buf;
  703. for (i = 0; i < bytes >> 2; i++, naddr++)
  704. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  705. flash_data_to_access_addr(FA_VPD_NVRAM_ADDR | naddr)));
  706. return buf;
  707. }
  708. int
  709. qla25xx_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  710. uint32_t bytes)
  711. {
  712. return qla24xx_write_flash_data(ha, (uint32_t *)buf,
  713. FA_VPD_NVRAM_ADDR | naddr, bytes >> 2);
  714. }
  715. static inline void
  716. qla2x00_flip_colors(scsi_qla_host_t *ha, uint16_t *pflags)
  717. {
  718. if (IS_QLA2322(ha)) {
  719. /* Flip all colors. */
  720. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  721. /* Turn off. */
  722. ha->beacon_color_state = 0;
  723. *pflags = GPIO_LED_ALL_OFF;
  724. } else {
  725. /* Turn on. */
  726. ha->beacon_color_state = QLA_LED_ALL_ON;
  727. *pflags = GPIO_LED_RGA_ON;
  728. }
  729. } else {
  730. /* Flip green led only. */
  731. if (ha->beacon_color_state == QLA_LED_GRN_ON) {
  732. /* Turn off. */
  733. ha->beacon_color_state = 0;
  734. *pflags = GPIO_LED_GREEN_OFF_AMBER_OFF;
  735. } else {
  736. /* Turn on. */
  737. ha->beacon_color_state = QLA_LED_GRN_ON;
  738. *pflags = GPIO_LED_GREEN_ON_AMBER_OFF;
  739. }
  740. }
  741. }
  742. void
  743. qla2x00_beacon_blink(struct scsi_qla_host *ha)
  744. {
  745. uint16_t gpio_enable;
  746. uint16_t gpio_data;
  747. uint16_t led_color = 0;
  748. unsigned long flags;
  749. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  750. if (ha->pio_address)
  751. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  752. spin_lock_irqsave(&ha->hardware_lock, flags);
  753. /* Save the Original GPIOE. */
  754. if (ha->pio_address) {
  755. gpio_enable = RD_REG_WORD_PIO(&reg->gpioe);
  756. gpio_data = RD_REG_WORD_PIO(&reg->gpiod);
  757. } else {
  758. gpio_enable = RD_REG_WORD(&reg->gpioe);
  759. gpio_data = RD_REG_WORD(&reg->gpiod);
  760. }
  761. /* Set the modified gpio_enable values */
  762. gpio_enable |= GPIO_LED_MASK;
  763. if (ha->pio_address) {
  764. WRT_REG_WORD_PIO(&reg->gpioe, gpio_enable);
  765. } else {
  766. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  767. RD_REG_WORD(&reg->gpioe);
  768. }
  769. qla2x00_flip_colors(ha, &led_color);
  770. /* Clear out any previously set LED color. */
  771. gpio_data &= ~GPIO_LED_MASK;
  772. /* Set the new input LED color to GPIOD. */
  773. gpio_data |= led_color;
  774. /* Set the modified gpio_data values */
  775. if (ha->pio_address) {
  776. WRT_REG_WORD_PIO(&reg->gpiod, gpio_data);
  777. } else {
  778. WRT_REG_WORD(&reg->gpiod, gpio_data);
  779. RD_REG_WORD(&reg->gpiod);
  780. }
  781. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  782. }
  783. int
  784. qla2x00_beacon_on(struct scsi_qla_host *ha)
  785. {
  786. uint16_t gpio_enable;
  787. uint16_t gpio_data;
  788. unsigned long flags;
  789. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  790. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  791. ha->fw_options[1] |= FO1_DISABLE_GPIO6_7;
  792. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  793. qla_printk(KERN_WARNING, ha,
  794. "Unable to update fw options (beacon on).\n");
  795. return QLA_FUNCTION_FAILED;
  796. }
  797. if (ha->pio_address)
  798. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  799. /* Turn off LEDs. */
  800. spin_lock_irqsave(&ha->hardware_lock, flags);
  801. if (ha->pio_address) {
  802. gpio_enable = RD_REG_WORD_PIO(&reg->gpioe);
  803. gpio_data = RD_REG_WORD_PIO(&reg->gpiod);
  804. } else {
  805. gpio_enable = RD_REG_WORD(&reg->gpioe);
  806. gpio_data = RD_REG_WORD(&reg->gpiod);
  807. }
  808. gpio_enable |= GPIO_LED_MASK;
  809. /* Set the modified gpio_enable values. */
  810. if (ha->pio_address) {
  811. WRT_REG_WORD_PIO(&reg->gpioe, gpio_enable);
  812. } else {
  813. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  814. RD_REG_WORD(&reg->gpioe);
  815. }
  816. /* Clear out previously set LED colour. */
  817. gpio_data &= ~GPIO_LED_MASK;
  818. if (ha->pio_address) {
  819. WRT_REG_WORD_PIO(&reg->gpiod, gpio_data);
  820. } else {
  821. WRT_REG_WORD(&reg->gpiod, gpio_data);
  822. RD_REG_WORD(&reg->gpiod);
  823. }
  824. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  825. /*
  826. * Let the per HBA timer kick off the blinking process based on
  827. * the following flags. No need to do anything else now.
  828. */
  829. ha->beacon_blink_led = 1;
  830. ha->beacon_color_state = 0;
  831. return QLA_SUCCESS;
  832. }
  833. int
  834. qla2x00_beacon_off(struct scsi_qla_host *ha)
  835. {
  836. int rval = QLA_SUCCESS;
  837. ha->beacon_blink_led = 0;
  838. /* Set the on flag so when it gets flipped it will be off. */
  839. if (IS_QLA2322(ha))
  840. ha->beacon_color_state = QLA_LED_ALL_ON;
  841. else
  842. ha->beacon_color_state = QLA_LED_GRN_ON;
  843. ha->isp_ops->beacon_blink(ha); /* This turns green LED off */
  844. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  845. ha->fw_options[1] &= ~FO1_DISABLE_GPIO6_7;
  846. rval = qla2x00_set_fw_options(ha, ha->fw_options);
  847. if (rval != QLA_SUCCESS)
  848. qla_printk(KERN_WARNING, ha,
  849. "Unable to update fw options (beacon off).\n");
  850. return rval;
  851. }
  852. static inline void
  853. qla24xx_flip_colors(scsi_qla_host_t *ha, uint16_t *pflags)
  854. {
  855. /* Flip all colors. */
  856. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  857. /* Turn off. */
  858. ha->beacon_color_state = 0;
  859. *pflags = 0;
  860. } else {
  861. /* Turn on. */
  862. ha->beacon_color_state = QLA_LED_ALL_ON;
  863. *pflags = GPDX_LED_YELLOW_ON | GPDX_LED_AMBER_ON;
  864. }
  865. }
  866. void
  867. qla24xx_beacon_blink(struct scsi_qla_host *ha)
  868. {
  869. uint16_t led_color = 0;
  870. uint32_t gpio_data;
  871. unsigned long flags;
  872. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  873. /* Save the Original GPIOD. */
  874. spin_lock_irqsave(&ha->hardware_lock, flags);
  875. gpio_data = RD_REG_DWORD(&reg->gpiod);
  876. /* Enable the gpio_data reg for update. */
  877. gpio_data |= GPDX_LED_UPDATE_MASK;
  878. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  879. gpio_data = RD_REG_DWORD(&reg->gpiod);
  880. /* Set the color bits. */
  881. qla24xx_flip_colors(ha, &led_color);
  882. /* Clear out any previously set LED color. */
  883. gpio_data &= ~GPDX_LED_COLOR_MASK;
  884. /* Set the new input LED color to GPIOD. */
  885. gpio_data |= led_color;
  886. /* Set the modified gpio_data values. */
  887. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  888. gpio_data = RD_REG_DWORD(&reg->gpiod);
  889. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  890. }
  891. int
  892. qla24xx_beacon_on(struct scsi_qla_host *ha)
  893. {
  894. uint32_t gpio_data;
  895. unsigned long flags;
  896. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  897. if (ha->beacon_blink_led == 0) {
  898. /* Enable firmware for update */
  899. ha->fw_options[1] |= ADD_FO1_DISABLE_GPIO_LED_CTRL;
  900. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS)
  901. return QLA_FUNCTION_FAILED;
  902. if (qla2x00_get_fw_options(ha, ha->fw_options) !=
  903. QLA_SUCCESS) {
  904. qla_printk(KERN_WARNING, ha,
  905. "Unable to update fw options (beacon on).\n");
  906. return QLA_FUNCTION_FAILED;
  907. }
  908. spin_lock_irqsave(&ha->hardware_lock, flags);
  909. gpio_data = RD_REG_DWORD(&reg->gpiod);
  910. /* Enable the gpio_data reg for update. */
  911. gpio_data |= GPDX_LED_UPDATE_MASK;
  912. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  913. RD_REG_DWORD(&reg->gpiod);
  914. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  915. }
  916. /* So all colors blink together. */
  917. ha->beacon_color_state = 0;
  918. /* Let the per HBA timer kick off the blinking process. */
  919. ha->beacon_blink_led = 1;
  920. return QLA_SUCCESS;
  921. }
  922. int
  923. qla24xx_beacon_off(struct scsi_qla_host *ha)
  924. {
  925. uint32_t gpio_data;
  926. unsigned long flags;
  927. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  928. ha->beacon_blink_led = 0;
  929. ha->beacon_color_state = QLA_LED_ALL_ON;
  930. ha->isp_ops->beacon_blink(ha); /* Will flip to all off. */
  931. /* Give control back to firmware. */
  932. spin_lock_irqsave(&ha->hardware_lock, flags);
  933. gpio_data = RD_REG_DWORD(&reg->gpiod);
  934. /* Disable the gpio_data reg for update. */
  935. gpio_data &= ~GPDX_LED_UPDATE_MASK;
  936. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  937. RD_REG_DWORD(&reg->gpiod);
  938. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  939. ha->fw_options[1] &= ~ADD_FO1_DISABLE_GPIO_LED_CTRL;
  940. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  941. qla_printk(KERN_WARNING, ha,
  942. "Unable to update fw options (beacon off).\n");
  943. return QLA_FUNCTION_FAILED;
  944. }
  945. if (qla2x00_get_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  946. qla_printk(KERN_WARNING, ha,
  947. "Unable to get fw options (beacon off).\n");
  948. return QLA_FUNCTION_FAILED;
  949. }
  950. return QLA_SUCCESS;
  951. }
  952. /*
  953. * Flash support routines
  954. */
  955. /**
  956. * qla2x00_flash_enable() - Setup flash for reading and writing.
  957. * @ha: HA context
  958. */
  959. static void
  960. qla2x00_flash_enable(scsi_qla_host_t *ha)
  961. {
  962. uint16_t data;
  963. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  964. data = RD_REG_WORD(&reg->ctrl_status);
  965. data |= CSR_FLASH_ENABLE;
  966. WRT_REG_WORD(&reg->ctrl_status, data);
  967. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  968. }
  969. /**
  970. * qla2x00_flash_disable() - Disable flash and allow RISC to run.
  971. * @ha: HA context
  972. */
  973. static void
  974. qla2x00_flash_disable(scsi_qla_host_t *ha)
  975. {
  976. uint16_t data;
  977. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  978. data = RD_REG_WORD(&reg->ctrl_status);
  979. data &= ~(CSR_FLASH_ENABLE);
  980. WRT_REG_WORD(&reg->ctrl_status, data);
  981. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  982. }
  983. /**
  984. * qla2x00_read_flash_byte() - Reads a byte from flash
  985. * @ha: HA context
  986. * @addr: Address in flash to read
  987. *
  988. * A word is read from the chip, but, only the lower byte is valid.
  989. *
  990. * Returns the byte read from flash @addr.
  991. */
  992. static uint8_t
  993. qla2x00_read_flash_byte(scsi_qla_host_t *ha, uint32_t addr)
  994. {
  995. uint16_t data;
  996. uint16_t bank_select;
  997. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  998. bank_select = RD_REG_WORD(&reg->ctrl_status);
  999. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1000. /* Specify 64K address range: */
  1001. /* clear out Module Select and Flash Address bits [19:16]. */
  1002. bank_select &= ~0xf8;
  1003. bank_select |= addr >> 12 & 0xf0;
  1004. bank_select |= CSR_FLASH_64K_BANK;
  1005. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1006. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1007. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1008. data = RD_REG_WORD(&reg->flash_data);
  1009. return (uint8_t)data;
  1010. }
  1011. /* Setup bit 16 of flash address. */
  1012. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1013. bank_select |= CSR_FLASH_64K_BANK;
  1014. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1015. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1016. } else if (((addr & BIT_16) == 0) &&
  1017. (bank_select & CSR_FLASH_64K_BANK)) {
  1018. bank_select &= ~(CSR_FLASH_64K_BANK);
  1019. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1020. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1021. }
  1022. /* Always perform IO mapped accesses to the FLASH registers. */
  1023. if (ha->pio_address) {
  1024. uint16_t data2;
  1025. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  1026. WRT_REG_WORD_PIO(&reg->flash_address, (uint16_t)addr);
  1027. do {
  1028. data = RD_REG_WORD_PIO(&reg->flash_data);
  1029. barrier();
  1030. cpu_relax();
  1031. data2 = RD_REG_WORD_PIO(&reg->flash_data);
  1032. } while (data != data2);
  1033. } else {
  1034. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1035. data = qla2x00_debounce_register(&reg->flash_data);
  1036. }
  1037. return (uint8_t)data;
  1038. }
  1039. /**
  1040. * qla2x00_write_flash_byte() - Write a byte to flash
  1041. * @ha: HA context
  1042. * @addr: Address in flash to write
  1043. * @data: Data to write
  1044. */
  1045. static void
  1046. qla2x00_write_flash_byte(scsi_qla_host_t *ha, uint32_t addr, uint8_t data)
  1047. {
  1048. uint16_t bank_select;
  1049. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1050. bank_select = RD_REG_WORD(&reg->ctrl_status);
  1051. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1052. /* Specify 64K address range: */
  1053. /* clear out Module Select and Flash Address bits [19:16]. */
  1054. bank_select &= ~0xf8;
  1055. bank_select |= addr >> 12 & 0xf0;
  1056. bank_select |= CSR_FLASH_64K_BANK;
  1057. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1058. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1059. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1060. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1061. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  1062. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1063. return;
  1064. }
  1065. /* Setup bit 16 of flash address. */
  1066. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1067. bank_select |= CSR_FLASH_64K_BANK;
  1068. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1069. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1070. } else if (((addr & BIT_16) == 0) &&
  1071. (bank_select & CSR_FLASH_64K_BANK)) {
  1072. bank_select &= ~(CSR_FLASH_64K_BANK);
  1073. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1074. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1075. }
  1076. /* Always perform IO mapped accesses to the FLASH registers. */
  1077. if (ha->pio_address) {
  1078. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  1079. WRT_REG_WORD_PIO(&reg->flash_address, (uint16_t)addr);
  1080. WRT_REG_WORD_PIO(&reg->flash_data, (uint16_t)data);
  1081. } else {
  1082. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1083. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1084. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  1085. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1086. }
  1087. }
  1088. /**
  1089. * qla2x00_poll_flash() - Polls flash for completion.
  1090. * @ha: HA context
  1091. * @addr: Address in flash to poll
  1092. * @poll_data: Data to be polled
  1093. * @man_id: Flash manufacturer ID
  1094. * @flash_id: Flash ID
  1095. *
  1096. * This function polls the device until bit 7 of what is read matches data
  1097. * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
  1098. * out (a fatal error). The flash book recommeds reading bit 7 again after
  1099. * reading bit 5 as a 1.
  1100. *
  1101. * Returns 0 on success, else non-zero.
  1102. */
  1103. static int
  1104. qla2x00_poll_flash(scsi_qla_host_t *ha, uint32_t addr, uint8_t poll_data,
  1105. uint8_t man_id, uint8_t flash_id)
  1106. {
  1107. int status;
  1108. uint8_t flash_data;
  1109. uint32_t cnt;
  1110. status = 1;
  1111. /* Wait for 30 seconds for command to finish. */
  1112. poll_data &= BIT_7;
  1113. for (cnt = 3000000; cnt; cnt--) {
  1114. flash_data = qla2x00_read_flash_byte(ha, addr);
  1115. if ((flash_data & BIT_7) == poll_data) {
  1116. status = 0;
  1117. break;
  1118. }
  1119. if (man_id != 0x40 && man_id != 0xda) {
  1120. if ((flash_data & BIT_5) && cnt > 2)
  1121. cnt = 2;
  1122. }
  1123. udelay(10);
  1124. barrier();
  1125. cond_resched();
  1126. }
  1127. return status;
  1128. }
  1129. /**
  1130. * qla2x00_program_flash_address() - Programs a flash address
  1131. * @ha: HA context
  1132. * @addr: Address in flash to program
  1133. * @data: Data to be written in flash
  1134. * @man_id: Flash manufacturer ID
  1135. * @flash_id: Flash ID
  1136. *
  1137. * Returns 0 on success, else non-zero.
  1138. */
  1139. static int
  1140. qla2x00_program_flash_address(scsi_qla_host_t *ha, uint32_t addr, uint8_t data,
  1141. uint8_t man_id, uint8_t flash_id)
  1142. {
  1143. /* Write Program Command Sequence. */
  1144. if (IS_OEM_001(ha)) {
  1145. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1146. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1147. qla2x00_write_flash_byte(ha, 0xaaa, 0xa0);
  1148. qla2x00_write_flash_byte(ha, addr, data);
  1149. } else {
  1150. if (man_id == 0xda && flash_id == 0xc1) {
  1151. qla2x00_write_flash_byte(ha, addr, data);
  1152. if (addr & 0x7e)
  1153. return 0;
  1154. } else {
  1155. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1156. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1157. qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
  1158. qla2x00_write_flash_byte(ha, addr, data);
  1159. }
  1160. }
  1161. udelay(150);
  1162. /* Wait for write to complete. */
  1163. return qla2x00_poll_flash(ha, addr, data, man_id, flash_id);
  1164. }
  1165. /**
  1166. * qla2x00_erase_flash() - Erase the flash.
  1167. * @ha: HA context
  1168. * @man_id: Flash manufacturer ID
  1169. * @flash_id: Flash ID
  1170. *
  1171. * Returns 0 on success, else non-zero.
  1172. */
  1173. static int
  1174. qla2x00_erase_flash(scsi_qla_host_t *ha, uint8_t man_id, uint8_t flash_id)
  1175. {
  1176. /* Individual Sector Erase Command Sequence */
  1177. if (IS_OEM_001(ha)) {
  1178. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1179. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1180. qla2x00_write_flash_byte(ha, 0xaaa, 0x80);
  1181. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1182. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1183. qla2x00_write_flash_byte(ha, 0xaaa, 0x10);
  1184. } else {
  1185. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1186. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1187. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1188. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1189. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1190. qla2x00_write_flash_byte(ha, 0x5555, 0x10);
  1191. }
  1192. udelay(150);
  1193. /* Wait for erase to complete. */
  1194. return qla2x00_poll_flash(ha, 0x00, 0x80, man_id, flash_id);
  1195. }
  1196. /**
  1197. * qla2x00_erase_flash_sector() - Erase a flash sector.
  1198. * @ha: HA context
  1199. * @addr: Flash sector to erase
  1200. * @sec_mask: Sector address mask
  1201. * @man_id: Flash manufacturer ID
  1202. * @flash_id: Flash ID
  1203. *
  1204. * Returns 0 on success, else non-zero.
  1205. */
  1206. static int
  1207. qla2x00_erase_flash_sector(scsi_qla_host_t *ha, uint32_t addr,
  1208. uint32_t sec_mask, uint8_t man_id, uint8_t flash_id)
  1209. {
  1210. /* Individual Sector Erase Command Sequence */
  1211. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1212. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1213. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1214. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1215. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1216. if (man_id == 0x1f && flash_id == 0x13)
  1217. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
  1218. else
  1219. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
  1220. udelay(150);
  1221. /* Wait for erase to complete. */
  1222. return qla2x00_poll_flash(ha, addr, 0x80, man_id, flash_id);
  1223. }
  1224. /**
  1225. * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
  1226. * @man_id: Flash manufacturer ID
  1227. * @flash_id: Flash ID
  1228. */
  1229. static void
  1230. qla2x00_get_flash_manufacturer(scsi_qla_host_t *ha, uint8_t *man_id,
  1231. uint8_t *flash_id)
  1232. {
  1233. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1234. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1235. qla2x00_write_flash_byte(ha, 0x5555, 0x90);
  1236. *man_id = qla2x00_read_flash_byte(ha, 0x0000);
  1237. *flash_id = qla2x00_read_flash_byte(ha, 0x0001);
  1238. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1239. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1240. qla2x00_write_flash_byte(ha, 0x5555, 0xf0);
  1241. }
  1242. static void
  1243. qla2x00_read_flash_data(scsi_qla_host_t *ha, uint8_t *tmp_buf, uint32_t saddr,
  1244. uint32_t length)
  1245. {
  1246. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1247. uint32_t midpoint, ilength;
  1248. uint8_t data;
  1249. midpoint = length / 2;
  1250. WRT_REG_WORD(&reg->nvram, 0);
  1251. RD_REG_WORD(&reg->nvram);
  1252. for (ilength = 0; ilength < length; saddr++, ilength++, tmp_buf++) {
  1253. if (ilength == midpoint) {
  1254. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1255. RD_REG_WORD(&reg->nvram);
  1256. }
  1257. data = qla2x00_read_flash_byte(ha, saddr);
  1258. if (saddr % 100)
  1259. udelay(10);
  1260. *tmp_buf = data;
  1261. cond_resched();
  1262. }
  1263. }
  1264. static inline void
  1265. qla2x00_suspend_hba(struct scsi_qla_host *ha)
  1266. {
  1267. int cnt;
  1268. unsigned long flags;
  1269. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1270. /* Suspend HBA. */
  1271. scsi_block_requests(ha->host);
  1272. ha->isp_ops->disable_intrs(ha);
  1273. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1274. /* Pause RISC. */
  1275. spin_lock_irqsave(&ha->hardware_lock, flags);
  1276. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  1277. RD_REG_WORD(&reg->hccr);
  1278. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  1279. for (cnt = 0; cnt < 30000; cnt++) {
  1280. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
  1281. break;
  1282. udelay(100);
  1283. }
  1284. } else {
  1285. udelay(10);
  1286. }
  1287. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1288. }
  1289. static inline void
  1290. qla2x00_resume_hba(struct scsi_qla_host *ha)
  1291. {
  1292. /* Resume HBA. */
  1293. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1294. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1295. qla2xxx_wake_dpc(ha);
  1296. qla2x00_wait_for_hba_online(ha);
  1297. scsi_unblock_requests(ha->host);
  1298. }
  1299. uint8_t *
  1300. qla2x00_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1301. uint32_t offset, uint32_t length)
  1302. {
  1303. uint32_t addr, midpoint;
  1304. uint8_t *data;
  1305. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1306. /* Suspend HBA. */
  1307. qla2x00_suspend_hba(ha);
  1308. /* Go with read. */
  1309. midpoint = ha->optrom_size / 2;
  1310. qla2x00_flash_enable(ha);
  1311. WRT_REG_WORD(&reg->nvram, 0);
  1312. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1313. for (addr = offset, data = buf; addr < length; addr++, data++) {
  1314. if (addr == midpoint) {
  1315. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1316. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1317. }
  1318. *data = qla2x00_read_flash_byte(ha, addr);
  1319. }
  1320. qla2x00_flash_disable(ha);
  1321. /* Resume HBA. */
  1322. qla2x00_resume_hba(ha);
  1323. return buf;
  1324. }
  1325. int
  1326. qla2x00_write_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1327. uint32_t offset, uint32_t length)
  1328. {
  1329. int rval;
  1330. uint8_t man_id, flash_id, sec_number, data;
  1331. uint16_t wd;
  1332. uint32_t addr, liter, sec_mask, rest_addr;
  1333. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1334. /* Suspend HBA. */
  1335. qla2x00_suspend_hba(ha);
  1336. rval = QLA_SUCCESS;
  1337. sec_number = 0;
  1338. /* Reset ISP chip. */
  1339. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  1340. pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
  1341. /* Go with write. */
  1342. qla2x00_flash_enable(ha);
  1343. do { /* Loop once to provide quick error exit */
  1344. /* Structure of flash memory based on manufacturer */
  1345. if (IS_OEM_001(ha)) {
  1346. /* OEM variant with special flash part. */
  1347. man_id = flash_id = 0;
  1348. rest_addr = 0xffff;
  1349. sec_mask = 0x10000;
  1350. goto update_flash;
  1351. }
  1352. qla2x00_get_flash_manufacturer(ha, &man_id, &flash_id);
  1353. switch (man_id) {
  1354. case 0x20: /* ST flash. */
  1355. if (flash_id == 0xd2 || flash_id == 0xe3) {
  1356. /*
  1357. * ST m29w008at part - 64kb sector size with
  1358. * 32kb,8kb,8kb,16kb sectors at memory address
  1359. * 0xf0000.
  1360. */
  1361. rest_addr = 0xffff;
  1362. sec_mask = 0x10000;
  1363. break;
  1364. }
  1365. /*
  1366. * ST m29w010b part - 16kb sector size
  1367. * Default to 16kb sectors
  1368. */
  1369. rest_addr = 0x3fff;
  1370. sec_mask = 0x1c000;
  1371. break;
  1372. case 0x40: /* Mostel flash. */
  1373. /* Mostel v29c51001 part - 512 byte sector size. */
  1374. rest_addr = 0x1ff;
  1375. sec_mask = 0x1fe00;
  1376. break;
  1377. case 0xbf: /* SST flash. */
  1378. /* SST39sf10 part - 4kb sector size. */
  1379. rest_addr = 0xfff;
  1380. sec_mask = 0x1f000;
  1381. break;
  1382. case 0xda: /* Winbond flash. */
  1383. /* Winbond W29EE011 part - 256 byte sector size. */
  1384. rest_addr = 0x7f;
  1385. sec_mask = 0x1ff80;
  1386. break;
  1387. case 0xc2: /* Macronix flash. */
  1388. /* 64k sector size. */
  1389. if (flash_id == 0x38 || flash_id == 0x4f) {
  1390. rest_addr = 0xffff;
  1391. sec_mask = 0x10000;
  1392. break;
  1393. }
  1394. /* Fall through... */
  1395. case 0x1f: /* Atmel flash. */
  1396. /* 512k sector size. */
  1397. if (flash_id == 0x13) {
  1398. rest_addr = 0x7fffffff;
  1399. sec_mask = 0x80000000;
  1400. break;
  1401. }
  1402. /* Fall through... */
  1403. case 0x01: /* AMD flash. */
  1404. if (flash_id == 0x38 || flash_id == 0x40 ||
  1405. flash_id == 0x4f) {
  1406. /* Am29LV081 part - 64kb sector size. */
  1407. /* Am29LV002BT part - 64kb sector size. */
  1408. rest_addr = 0xffff;
  1409. sec_mask = 0x10000;
  1410. break;
  1411. } else if (flash_id == 0x3e) {
  1412. /*
  1413. * Am29LV008b part - 64kb sector size with
  1414. * 32kb,8kb,8kb,16kb sector at memory address
  1415. * h0xf0000.
  1416. */
  1417. rest_addr = 0xffff;
  1418. sec_mask = 0x10000;
  1419. break;
  1420. } else if (flash_id == 0x20 || flash_id == 0x6e) {
  1421. /*
  1422. * Am29LV010 part or AM29f010 - 16kb sector
  1423. * size.
  1424. */
  1425. rest_addr = 0x3fff;
  1426. sec_mask = 0x1c000;
  1427. break;
  1428. } else if (flash_id == 0x6d) {
  1429. /* Am29LV001 part - 8kb sector size. */
  1430. rest_addr = 0x1fff;
  1431. sec_mask = 0x1e000;
  1432. break;
  1433. }
  1434. default:
  1435. /* Default to 16 kb sector size. */
  1436. rest_addr = 0x3fff;
  1437. sec_mask = 0x1c000;
  1438. break;
  1439. }
  1440. update_flash:
  1441. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1442. if (qla2x00_erase_flash(ha, man_id, flash_id)) {
  1443. rval = QLA_FUNCTION_FAILED;
  1444. break;
  1445. }
  1446. }
  1447. for (addr = offset, liter = 0; liter < length; liter++,
  1448. addr++) {
  1449. data = buf[liter];
  1450. /* Are we at the beginning of a sector? */
  1451. if ((addr & rest_addr) == 0) {
  1452. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1453. if (addr >= 0x10000UL) {
  1454. if (((addr >> 12) & 0xf0) &&
  1455. ((man_id == 0x01 &&
  1456. flash_id == 0x3e) ||
  1457. (man_id == 0x20 &&
  1458. flash_id == 0xd2))) {
  1459. sec_number++;
  1460. if (sec_number == 1) {
  1461. rest_addr =
  1462. 0x7fff;
  1463. sec_mask =
  1464. 0x18000;
  1465. } else if (
  1466. sec_number == 2 ||
  1467. sec_number == 3) {
  1468. rest_addr =
  1469. 0x1fff;
  1470. sec_mask =
  1471. 0x1e000;
  1472. } else if (
  1473. sec_number == 4) {
  1474. rest_addr =
  1475. 0x3fff;
  1476. sec_mask =
  1477. 0x1c000;
  1478. }
  1479. }
  1480. }
  1481. } else if (addr == ha->optrom_size / 2) {
  1482. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1483. RD_REG_WORD(&reg->nvram);
  1484. }
  1485. if (flash_id == 0xda && man_id == 0xc1) {
  1486. qla2x00_write_flash_byte(ha, 0x5555,
  1487. 0xaa);
  1488. qla2x00_write_flash_byte(ha, 0x2aaa,
  1489. 0x55);
  1490. qla2x00_write_flash_byte(ha, 0x5555,
  1491. 0xa0);
  1492. } else if (!IS_QLA2322(ha) && !IS_QLA6322(ha)) {
  1493. /* Then erase it */
  1494. if (qla2x00_erase_flash_sector(ha,
  1495. addr, sec_mask, man_id,
  1496. flash_id)) {
  1497. rval = QLA_FUNCTION_FAILED;
  1498. break;
  1499. }
  1500. if (man_id == 0x01 && flash_id == 0x6d)
  1501. sec_number++;
  1502. }
  1503. }
  1504. if (man_id == 0x01 && flash_id == 0x6d) {
  1505. if (sec_number == 1 &&
  1506. addr == (rest_addr - 1)) {
  1507. rest_addr = 0x0fff;
  1508. sec_mask = 0x1f000;
  1509. } else if (sec_number == 3 && (addr & 0x7ffe)) {
  1510. rest_addr = 0x3fff;
  1511. sec_mask = 0x1c000;
  1512. }
  1513. }
  1514. if (qla2x00_program_flash_address(ha, addr, data,
  1515. man_id, flash_id)) {
  1516. rval = QLA_FUNCTION_FAILED;
  1517. break;
  1518. }
  1519. cond_resched();
  1520. }
  1521. } while (0);
  1522. qla2x00_flash_disable(ha);
  1523. /* Resume HBA. */
  1524. qla2x00_resume_hba(ha);
  1525. return rval;
  1526. }
  1527. uint8_t *
  1528. qla24xx_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1529. uint32_t offset, uint32_t length)
  1530. {
  1531. /* Suspend HBA. */
  1532. scsi_block_requests(ha->host);
  1533. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1534. /* Go with read. */
  1535. qla24xx_read_flash_data(ha, (uint32_t *)buf, offset >> 2, length >> 2);
  1536. /* Resume HBA. */
  1537. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1538. scsi_unblock_requests(ha->host);
  1539. return buf;
  1540. }
  1541. int
  1542. qla24xx_write_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1543. uint32_t offset, uint32_t length)
  1544. {
  1545. int rval;
  1546. /* Suspend HBA. */
  1547. scsi_block_requests(ha->host);
  1548. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1549. /* Go with write. */
  1550. rval = qla24xx_write_flash_data(ha, (uint32_t *)buf, offset >> 2,
  1551. length >> 2);
  1552. /* Resume HBA -- RISC reset needed. */
  1553. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1554. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1555. qla2xxx_wake_dpc(ha);
  1556. qla2x00_wait_for_hba_online(ha);
  1557. scsi_unblock_requests(ha->host);
  1558. return rval;
  1559. }
  1560. uint8_t *
  1561. qla25xx_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1562. uint32_t offset, uint32_t length)
  1563. {
  1564. int rval;
  1565. dma_addr_t optrom_dma;
  1566. void *optrom;
  1567. uint8_t *pbuf;
  1568. uint32_t faddr, left, burst;
  1569. if (offset & 0xfff)
  1570. goto slow_read;
  1571. if (length < OPTROM_BURST_SIZE)
  1572. goto slow_read;
  1573. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  1574. &optrom_dma, GFP_KERNEL);
  1575. if (!optrom) {
  1576. qla_printk(KERN_DEBUG, ha,
  1577. "Unable to allocate memory for optrom burst read "
  1578. "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
  1579. goto slow_read;
  1580. }
  1581. pbuf = buf;
  1582. faddr = offset >> 2;
  1583. left = length >> 2;
  1584. burst = OPTROM_BURST_DWORDS;
  1585. while (left != 0) {
  1586. if (burst > left)
  1587. burst = left;
  1588. rval = qla2x00_dump_ram(ha, optrom_dma,
  1589. flash_data_to_access_addr(faddr), burst);
  1590. if (rval) {
  1591. qla_printk(KERN_WARNING, ha,
  1592. "Unable to burst-read optrom segment "
  1593. "(%x/%x/%llx).\n", rval,
  1594. flash_data_to_access_addr(faddr),
  1595. (unsigned long long)optrom_dma);
  1596. qla_printk(KERN_WARNING, ha,
  1597. "Reverting to slow-read.\n");
  1598. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  1599. optrom, optrom_dma);
  1600. goto slow_read;
  1601. }
  1602. memcpy(pbuf, optrom, burst * 4);
  1603. left -= burst;
  1604. faddr += burst;
  1605. pbuf += burst * 4;
  1606. }
  1607. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE, optrom,
  1608. optrom_dma);
  1609. return buf;
  1610. slow_read:
  1611. return qla24xx_read_optrom_data(ha, buf, offset, length);
  1612. }
  1613. /**
  1614. * qla2x00_get_fcode_version() - Determine an FCODE image's version.
  1615. * @ha: HA context
  1616. * @pcids: Pointer to the FCODE PCI data structure
  1617. *
  1618. * The process of retrieving the FCODE version information is at best
  1619. * described as interesting.
  1620. *
  1621. * Within the first 100h bytes of the image an ASCII string is present
  1622. * which contains several pieces of information including the FCODE
  1623. * version. Unfortunately it seems the only reliable way to retrieve
  1624. * the version is by scanning for another sentinel within the string,
  1625. * the FCODE build date:
  1626. *
  1627. * ... 2.00.02 10/17/02 ...
  1628. *
  1629. * Returns QLA_SUCCESS on successful retrieval of version.
  1630. */
  1631. static void
  1632. qla2x00_get_fcode_version(scsi_qla_host_t *ha, uint32_t pcids)
  1633. {
  1634. int ret = QLA_FUNCTION_FAILED;
  1635. uint32_t istart, iend, iter, vend;
  1636. uint8_t do_next, rbyte, *vbyte;
  1637. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1638. /* Skip the PCI data structure. */
  1639. istart = pcids +
  1640. ((qla2x00_read_flash_byte(ha, pcids + 0x0B) << 8) |
  1641. qla2x00_read_flash_byte(ha, pcids + 0x0A));
  1642. iend = istart + 0x100;
  1643. do {
  1644. /* Scan for the sentinel date string...eeewww. */
  1645. do_next = 0;
  1646. iter = istart;
  1647. while ((iter < iend) && !do_next) {
  1648. iter++;
  1649. if (qla2x00_read_flash_byte(ha, iter) == '/') {
  1650. if (qla2x00_read_flash_byte(ha, iter + 2) ==
  1651. '/')
  1652. do_next++;
  1653. else if (qla2x00_read_flash_byte(ha,
  1654. iter + 3) == '/')
  1655. do_next++;
  1656. }
  1657. }
  1658. if (!do_next)
  1659. break;
  1660. /* Backtrack to previous ' ' (space). */
  1661. do_next = 0;
  1662. while ((iter > istart) && !do_next) {
  1663. iter--;
  1664. if (qla2x00_read_flash_byte(ha, iter) == ' ')
  1665. do_next++;
  1666. }
  1667. if (!do_next)
  1668. break;
  1669. /*
  1670. * Mark end of version tag, and find previous ' ' (space) or
  1671. * string length (recent FCODE images -- major hack ahead!!!).
  1672. */
  1673. vend = iter - 1;
  1674. do_next = 0;
  1675. while ((iter > istart) && !do_next) {
  1676. iter--;
  1677. rbyte = qla2x00_read_flash_byte(ha, iter);
  1678. if (rbyte == ' ' || rbyte == 0xd || rbyte == 0x10)
  1679. do_next++;
  1680. }
  1681. if (!do_next)
  1682. break;
  1683. /* Mark beginning of version tag, and copy data. */
  1684. iter++;
  1685. if ((vend - iter) &&
  1686. ((vend - iter) < sizeof(ha->fcode_revision))) {
  1687. vbyte = ha->fcode_revision;
  1688. while (iter <= vend) {
  1689. *vbyte++ = qla2x00_read_flash_byte(ha, iter);
  1690. iter++;
  1691. }
  1692. ret = QLA_SUCCESS;
  1693. }
  1694. } while (0);
  1695. if (ret != QLA_SUCCESS)
  1696. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1697. }
  1698. int
  1699. qla2x00_get_flash_version(scsi_qla_host_t *ha, void *mbuf)
  1700. {
  1701. int ret = QLA_SUCCESS;
  1702. uint8_t code_type, last_image;
  1703. uint32_t pcihdr, pcids;
  1704. uint8_t *dbyte;
  1705. uint16_t *dcode;
  1706. if (!ha->pio_address || !mbuf)
  1707. return QLA_FUNCTION_FAILED;
  1708. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  1709. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  1710. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1711. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1712. qla2x00_flash_enable(ha);
  1713. /* Begin with first PCI expansion ROM header. */
  1714. pcihdr = 0;
  1715. last_image = 1;
  1716. do {
  1717. /* Verify PCI expansion ROM header. */
  1718. if (qla2x00_read_flash_byte(ha, pcihdr) != 0x55 ||
  1719. qla2x00_read_flash_byte(ha, pcihdr + 0x01) != 0xaa) {
  1720. /* No signature */
  1721. DEBUG2(printk("scsi(%ld): No matching ROM "
  1722. "signature.\n", ha->host_no));
  1723. ret = QLA_FUNCTION_FAILED;
  1724. break;
  1725. }
  1726. /* Locate PCI data structure. */
  1727. pcids = pcihdr +
  1728. ((qla2x00_read_flash_byte(ha, pcihdr + 0x19) << 8) |
  1729. qla2x00_read_flash_byte(ha, pcihdr + 0x18));
  1730. /* Validate signature of PCI data structure. */
  1731. if (qla2x00_read_flash_byte(ha, pcids) != 'P' ||
  1732. qla2x00_read_flash_byte(ha, pcids + 0x1) != 'C' ||
  1733. qla2x00_read_flash_byte(ha, pcids + 0x2) != 'I' ||
  1734. qla2x00_read_flash_byte(ha, pcids + 0x3) != 'R') {
  1735. /* Incorrect header. */
  1736. DEBUG2(printk("%s(): PCI data struct not found "
  1737. "pcir_adr=%x.\n", __func__, pcids));
  1738. ret = QLA_FUNCTION_FAILED;
  1739. break;
  1740. }
  1741. /* Read version */
  1742. code_type = qla2x00_read_flash_byte(ha, pcids + 0x14);
  1743. switch (code_type) {
  1744. case ROM_CODE_TYPE_BIOS:
  1745. /* Intel x86, PC-AT compatible. */
  1746. ha->bios_revision[0] =
  1747. qla2x00_read_flash_byte(ha, pcids + 0x12);
  1748. ha->bios_revision[1] =
  1749. qla2x00_read_flash_byte(ha, pcids + 0x13);
  1750. DEBUG3(printk("%s(): read BIOS %d.%d.\n", __func__,
  1751. ha->bios_revision[1], ha->bios_revision[0]));
  1752. break;
  1753. case ROM_CODE_TYPE_FCODE:
  1754. /* Open Firmware standard for PCI (FCode). */
  1755. /* Eeeewww... */
  1756. qla2x00_get_fcode_version(ha, pcids);
  1757. break;
  1758. case ROM_CODE_TYPE_EFI:
  1759. /* Extensible Firmware Interface (EFI). */
  1760. ha->efi_revision[0] =
  1761. qla2x00_read_flash_byte(ha, pcids + 0x12);
  1762. ha->efi_revision[1] =
  1763. qla2x00_read_flash_byte(ha, pcids + 0x13);
  1764. DEBUG3(printk("%s(): read EFI %d.%d.\n", __func__,
  1765. ha->efi_revision[1], ha->efi_revision[0]));
  1766. break;
  1767. default:
  1768. DEBUG2(printk("%s(): Unrecognized code type %x at "
  1769. "pcids %x.\n", __func__, code_type, pcids));
  1770. break;
  1771. }
  1772. last_image = qla2x00_read_flash_byte(ha, pcids + 0x15) & BIT_7;
  1773. /* Locate next PCI expansion ROM. */
  1774. pcihdr += ((qla2x00_read_flash_byte(ha, pcids + 0x11) << 8) |
  1775. qla2x00_read_flash_byte(ha, pcids + 0x10)) * 512;
  1776. } while (!last_image);
  1777. if (IS_QLA2322(ha)) {
  1778. /* Read firmware image information. */
  1779. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1780. dbyte = mbuf;
  1781. memset(dbyte, 0, 8);
  1782. dcode = (uint16_t *)dbyte;
  1783. qla2x00_read_flash_data(ha, dbyte, FA_RISC_CODE_ADDR * 4 + 10,
  1784. 8);
  1785. DEBUG3(printk("%s(%ld): dumping fw ver from flash:\n",
  1786. __func__, ha->host_no));
  1787. DEBUG3(qla2x00_dump_buffer((uint8_t *)dbyte, 8));
  1788. if ((dcode[0] == 0xffff && dcode[1] == 0xffff &&
  1789. dcode[2] == 0xffff && dcode[3] == 0xffff) ||
  1790. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  1791. dcode[3] == 0)) {
  1792. DEBUG2(printk("%s(): Unrecognized fw revision at "
  1793. "%x.\n", __func__, FA_RISC_CODE_ADDR * 4));
  1794. } else {
  1795. /* values are in big endian */
  1796. ha->fw_revision[0] = dbyte[0] << 16 | dbyte[1];
  1797. ha->fw_revision[1] = dbyte[2] << 16 | dbyte[3];
  1798. ha->fw_revision[2] = dbyte[4] << 16 | dbyte[5];
  1799. }
  1800. }
  1801. qla2x00_flash_disable(ha);
  1802. return ret;
  1803. }
  1804. int
  1805. qla24xx_get_flash_version(scsi_qla_host_t *ha, void *mbuf)
  1806. {
  1807. int ret = QLA_SUCCESS;
  1808. uint32_t pcihdr, pcids;
  1809. uint32_t *dcode;
  1810. uint8_t *bcode;
  1811. uint8_t code_type, last_image;
  1812. int i;
  1813. if (!mbuf)
  1814. return QLA_FUNCTION_FAILED;
  1815. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  1816. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  1817. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1818. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1819. dcode = mbuf;
  1820. /* Begin with first PCI expansion ROM header. */
  1821. pcihdr = 0;
  1822. last_image = 1;
  1823. do {
  1824. /* Verify PCI expansion ROM header. */
  1825. qla24xx_read_flash_data(ha, dcode, pcihdr >> 2, 0x20);
  1826. bcode = mbuf + (pcihdr % 4);
  1827. if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa) {
  1828. /* No signature */
  1829. DEBUG2(printk("scsi(%ld): No matching ROM "
  1830. "signature.\n", ha->host_no));
  1831. ret = QLA_FUNCTION_FAILED;
  1832. break;
  1833. }
  1834. /* Locate PCI data structure. */
  1835. pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
  1836. qla24xx_read_flash_data(ha, dcode, pcids >> 2, 0x20);
  1837. bcode = mbuf + (pcihdr % 4);
  1838. /* Validate signature of PCI data structure. */
  1839. if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
  1840. bcode[0x2] != 'I' || bcode[0x3] != 'R') {
  1841. /* Incorrect header. */
  1842. DEBUG2(printk("%s(): PCI data struct not found "
  1843. "pcir_adr=%x.\n", __func__, pcids));
  1844. ret = QLA_FUNCTION_FAILED;
  1845. break;
  1846. }
  1847. /* Read version */
  1848. code_type = bcode[0x14];
  1849. switch (code_type) {
  1850. case ROM_CODE_TYPE_BIOS:
  1851. /* Intel x86, PC-AT compatible. */
  1852. ha->bios_revision[0] = bcode[0x12];
  1853. ha->bios_revision[1] = bcode[0x13];
  1854. DEBUG3(printk("%s(): read BIOS %d.%d.\n", __func__,
  1855. ha->bios_revision[1], ha->bios_revision[0]));
  1856. break;
  1857. case ROM_CODE_TYPE_FCODE:
  1858. /* Open Firmware standard for PCI (FCode). */
  1859. ha->fcode_revision[0] = bcode[0x12];
  1860. ha->fcode_revision[1] = bcode[0x13];
  1861. DEBUG3(printk("%s(): read FCODE %d.%d.\n", __func__,
  1862. ha->fcode_revision[1], ha->fcode_revision[0]));
  1863. break;
  1864. case ROM_CODE_TYPE_EFI:
  1865. /* Extensible Firmware Interface (EFI). */
  1866. ha->efi_revision[0] = bcode[0x12];
  1867. ha->efi_revision[1] = bcode[0x13];
  1868. DEBUG3(printk("%s(): read EFI %d.%d.\n", __func__,
  1869. ha->efi_revision[1], ha->efi_revision[0]));
  1870. break;
  1871. default:
  1872. DEBUG2(printk("%s(): Unrecognized code type %x at "
  1873. "pcids %x.\n", __func__, code_type, pcids));
  1874. break;
  1875. }
  1876. last_image = bcode[0x15] & BIT_7;
  1877. /* Locate next PCI expansion ROM. */
  1878. pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
  1879. } while (!last_image);
  1880. /* Read firmware image information. */
  1881. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1882. dcode = mbuf;
  1883. qla24xx_read_flash_data(ha, dcode, FA_RISC_CODE_ADDR + 4, 4);
  1884. for (i = 0; i < 4; i++)
  1885. dcode[i] = be32_to_cpu(dcode[i]);
  1886. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  1887. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  1888. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  1889. dcode[3] == 0)) {
  1890. DEBUG2(printk("%s(): Unrecognized fw version at %x.\n",
  1891. __func__, FA_RISC_CODE_ADDR));
  1892. } else {
  1893. ha->fw_revision[0] = dcode[0];
  1894. ha->fw_revision[1] = dcode[1];
  1895. ha->fw_revision[2] = dcode[2];
  1896. ha->fw_revision[3] = dcode[3];
  1897. }
  1898. return ret;
  1899. }