qla_sup.c 53 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069
  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. static inline uint32_t
  362. flash_conf_to_access_addr(uint32_t faddr)
  363. {
  364. return FARX_ACCESS_FLASH_CONF | faddr;
  365. }
  366. static inline uint32_t
  367. flash_data_to_access_addr(uint32_t faddr)
  368. {
  369. return FARX_ACCESS_FLASH_DATA | faddr;
  370. }
  371. static inline uint32_t
  372. nvram_conf_to_access_addr(uint32_t naddr)
  373. {
  374. return FARX_ACCESS_NVRAM_CONF | naddr;
  375. }
  376. static inline uint32_t
  377. nvram_data_to_access_addr(uint32_t naddr)
  378. {
  379. return FARX_ACCESS_NVRAM_DATA | naddr;
  380. }
  381. static uint32_t
  382. qla24xx_read_flash_dword(scsi_qla_host_t *ha, uint32_t addr)
  383. {
  384. int rval;
  385. uint32_t cnt, data;
  386. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  387. WRT_REG_DWORD(&reg->flash_addr, addr & ~FARX_DATA_FLAG);
  388. /* Wait for READ cycle to complete. */
  389. rval = QLA_SUCCESS;
  390. for (cnt = 3000;
  391. (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) == 0 &&
  392. rval == QLA_SUCCESS; cnt--) {
  393. if (cnt)
  394. udelay(10);
  395. else
  396. rval = QLA_FUNCTION_TIMEOUT;
  397. cond_resched();
  398. }
  399. /* TODO: What happens if we time out? */
  400. data = 0xDEADDEAD;
  401. if (rval == QLA_SUCCESS)
  402. data = RD_REG_DWORD(&reg->flash_data);
  403. return data;
  404. }
  405. uint32_t *
  406. qla24xx_read_flash_data(scsi_qla_host_t *ha, uint32_t *dwptr, uint32_t faddr,
  407. uint32_t dwords)
  408. {
  409. uint32_t i;
  410. /* Dword reads to flash. */
  411. for (i = 0; i < dwords; i++, faddr++)
  412. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  413. flash_data_to_access_addr(faddr)));
  414. return dwptr;
  415. }
  416. static int
  417. qla24xx_write_flash_dword(scsi_qla_host_t *ha, uint32_t addr, uint32_t data)
  418. {
  419. int rval;
  420. uint32_t cnt;
  421. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  422. WRT_REG_DWORD(&reg->flash_data, data);
  423. RD_REG_DWORD(&reg->flash_data); /* PCI Posting. */
  424. WRT_REG_DWORD(&reg->flash_addr, addr | FARX_DATA_FLAG);
  425. /* Wait for Write cycle to complete. */
  426. rval = QLA_SUCCESS;
  427. for (cnt = 500000; (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) &&
  428. rval == QLA_SUCCESS; cnt--) {
  429. if (cnt)
  430. udelay(10);
  431. else
  432. rval = QLA_FUNCTION_TIMEOUT;
  433. cond_resched();
  434. }
  435. return rval;
  436. }
  437. static void
  438. qla24xx_get_flash_manufacturer(scsi_qla_host_t *ha, uint8_t *man_id,
  439. uint8_t *flash_id)
  440. {
  441. uint32_t ids;
  442. ids = qla24xx_read_flash_dword(ha, flash_data_to_access_addr(0xd03ab));
  443. *man_id = LSB(ids);
  444. *flash_id = MSB(ids);
  445. /* Check if man_id and flash_id are valid. */
  446. if (ids != 0xDEADDEAD && (*man_id == 0 || *flash_id == 0)) {
  447. /* Read information using 0x9f opcode
  448. * Device ID, Mfg ID would be read in the format:
  449. * <Ext Dev Info><Device ID Part2><Device ID Part 1><Mfg ID>
  450. * Example: ATMEL 0x00 01 45 1F
  451. * Extract MFG and Dev ID from last two bytes.
  452. */
  453. ids = qla24xx_read_flash_dword(ha,
  454. flash_data_to_access_addr(0xd009f));
  455. *man_id = LSB(ids);
  456. *flash_id = MSB(ids);
  457. }
  458. }
  459. static int
  460. qla24xx_write_flash_data(scsi_qla_host_t *ha, uint32_t *dwptr, uint32_t faddr,
  461. uint32_t dwords)
  462. {
  463. int ret;
  464. uint32_t liter;
  465. uint32_t sec_mask, rest_addr, conf_addr, sec_end_mask;
  466. uint32_t fdata, findex ;
  467. uint8_t man_id, flash_id;
  468. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  469. ret = QLA_SUCCESS;
  470. qla24xx_get_flash_manufacturer(ha, &man_id, &flash_id);
  471. DEBUG9(printk("%s(%ld): Flash man_id=%d flash_id=%d\n", __func__,
  472. ha->host_no, man_id, flash_id));
  473. sec_end_mask = 0;
  474. conf_addr = flash_conf_to_access_addr(0x03d8);
  475. switch (man_id) {
  476. case 0xbf: /* STT flash. */
  477. rest_addr = 0x1fff;
  478. sec_mask = 0x3e000;
  479. if (flash_id == 0x80)
  480. conf_addr = flash_conf_to_access_addr(0x0352);
  481. break;
  482. case 0x13: /* ST M25P80. */
  483. rest_addr = 0x3fff;
  484. sec_mask = 0x3c000;
  485. break;
  486. case 0x1f: // Atmel 26DF081A
  487. rest_addr = 0x0fff;
  488. sec_mask = 0xff000;
  489. sec_end_mask = 0x003ff;
  490. conf_addr = flash_conf_to_access_addr(0x0320);
  491. break;
  492. default:
  493. /* Default to 64 kb sector size. */
  494. rest_addr = 0x3fff;
  495. sec_mask = 0x3c000;
  496. break;
  497. }
  498. /* Enable flash write. */
  499. WRT_REG_DWORD(&reg->ctrl_status,
  500. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  501. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  502. /* Disable flash write-protection. */
  503. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0);
  504. /* Some flash parts need an additional zero-write to clear bits.*/
  505. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0);
  506. do { /* Loop once to provide quick error exit. */
  507. for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
  508. if (man_id == 0x1f) {
  509. findex = faddr << 2;
  510. fdata = findex & sec_mask;
  511. } else {
  512. findex = faddr;
  513. fdata = (findex & sec_mask) << 2;
  514. }
  515. /* Are we at the beginning of a sector? */
  516. if ((findex & rest_addr) == 0) {
  517. /*
  518. * Do sector unprotect at 4K boundry for Atmel
  519. * part.
  520. */
  521. if (man_id == 0x1f)
  522. qla24xx_write_flash_dword(ha,
  523. flash_conf_to_access_addr(0x0339),
  524. (fdata & 0xff00) | ((fdata << 16) &
  525. 0xff0000) | ((fdata >> 16) & 0xff));
  526. ret = qla24xx_write_flash_dword(ha, conf_addr,
  527. (fdata & 0xff00) |((fdata << 16) &
  528. 0xff0000) | ((fdata >> 16) & 0xff));
  529. if (ret != QLA_SUCCESS) {
  530. DEBUG9(printk("%s(%ld) Unable to flash "
  531. "sector: address=%x.\n", __func__,
  532. ha->host_no, faddr));
  533. break;
  534. }
  535. }
  536. ret = qla24xx_write_flash_dword(ha,
  537. flash_data_to_access_addr(faddr),
  538. cpu_to_le32(*dwptr));
  539. if (ret != QLA_SUCCESS) {
  540. DEBUG9(printk("%s(%ld) Unable to program flash "
  541. "address=%x data=%x.\n", __func__,
  542. ha->host_no, faddr, *dwptr));
  543. break;
  544. }
  545. /* Do sector protect at 4K boundry for Atmel part. */
  546. if (man_id == 0x1f &&
  547. ((faddr & sec_end_mask) == 0x3ff))
  548. qla24xx_write_flash_dword(ha,
  549. flash_conf_to_access_addr(0x0336),
  550. (fdata & 0xff00) | ((fdata << 16) &
  551. 0xff0000) | ((fdata >> 16) & 0xff));
  552. }
  553. } while (0);
  554. /* Enable flash write-protection. */
  555. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0x9c);
  556. /* Disable flash write. */
  557. WRT_REG_DWORD(&reg->ctrl_status,
  558. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  559. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  560. return ret;
  561. }
  562. uint8_t *
  563. qla2x00_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  564. uint32_t bytes)
  565. {
  566. uint32_t i;
  567. uint16_t *wptr;
  568. /* Word reads to NVRAM via registers. */
  569. wptr = (uint16_t *)buf;
  570. qla2x00_lock_nvram_access(ha);
  571. for (i = 0; i < bytes >> 1; i++, naddr++)
  572. wptr[i] = cpu_to_le16(qla2x00_get_nvram_word(ha,
  573. naddr));
  574. qla2x00_unlock_nvram_access(ha);
  575. return buf;
  576. }
  577. uint8_t *
  578. qla24xx_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  579. uint32_t bytes)
  580. {
  581. uint32_t i;
  582. uint32_t *dwptr;
  583. /* Dword reads to flash. */
  584. dwptr = (uint32_t *)buf;
  585. for (i = 0; i < bytes >> 2; i++, naddr++)
  586. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  587. nvram_data_to_access_addr(naddr)));
  588. return buf;
  589. }
  590. int
  591. qla2x00_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  592. uint32_t bytes)
  593. {
  594. int ret, stat;
  595. uint32_t i;
  596. uint16_t *wptr;
  597. ret = QLA_SUCCESS;
  598. qla2x00_lock_nvram_access(ha);
  599. /* Disable NVRAM write-protection. */
  600. stat = qla2x00_clear_nvram_protection(ha);
  601. wptr = (uint16_t *)buf;
  602. for (i = 0; i < bytes >> 1; i++, naddr++) {
  603. qla2x00_write_nvram_word(ha, naddr,
  604. cpu_to_le16(*wptr));
  605. wptr++;
  606. }
  607. /* Enable NVRAM write-protection. */
  608. qla2x00_set_nvram_protection(ha, stat);
  609. qla2x00_unlock_nvram_access(ha);
  610. return ret;
  611. }
  612. int
  613. qla24xx_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  614. uint32_t bytes)
  615. {
  616. int ret;
  617. uint32_t i;
  618. uint32_t *dwptr;
  619. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  620. ret = QLA_SUCCESS;
  621. /* Enable flash write. */
  622. WRT_REG_DWORD(&reg->ctrl_status,
  623. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  624. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  625. /* Disable NVRAM write-protection. */
  626. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  627. 0);
  628. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  629. 0);
  630. /* Dword writes to flash. */
  631. dwptr = (uint32_t *)buf;
  632. for (i = 0; i < bytes >> 2; i++, naddr++, dwptr++) {
  633. ret = qla24xx_write_flash_dword(ha,
  634. nvram_data_to_access_addr(naddr),
  635. cpu_to_le32(*dwptr));
  636. if (ret != QLA_SUCCESS) {
  637. DEBUG9(printk("%s(%ld) Unable to program "
  638. "nvram address=%x data=%x.\n", __func__,
  639. ha->host_no, naddr, *dwptr));
  640. break;
  641. }
  642. }
  643. /* Enable NVRAM write-protection. */
  644. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  645. 0x8c);
  646. /* Disable flash write. */
  647. WRT_REG_DWORD(&reg->ctrl_status,
  648. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  649. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  650. return ret;
  651. }
  652. static inline void
  653. qla2x00_flip_colors(scsi_qla_host_t *ha, uint16_t *pflags)
  654. {
  655. if (IS_QLA2322(ha)) {
  656. /* Flip all colors. */
  657. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  658. /* Turn off. */
  659. ha->beacon_color_state = 0;
  660. *pflags = GPIO_LED_ALL_OFF;
  661. } else {
  662. /* Turn on. */
  663. ha->beacon_color_state = QLA_LED_ALL_ON;
  664. *pflags = GPIO_LED_RGA_ON;
  665. }
  666. } else {
  667. /* Flip green led only. */
  668. if (ha->beacon_color_state == QLA_LED_GRN_ON) {
  669. /* Turn off. */
  670. ha->beacon_color_state = 0;
  671. *pflags = GPIO_LED_GREEN_OFF_AMBER_OFF;
  672. } else {
  673. /* Turn on. */
  674. ha->beacon_color_state = QLA_LED_GRN_ON;
  675. *pflags = GPIO_LED_GREEN_ON_AMBER_OFF;
  676. }
  677. }
  678. }
  679. void
  680. qla2x00_beacon_blink(struct scsi_qla_host *ha)
  681. {
  682. uint16_t gpio_enable;
  683. uint16_t gpio_data;
  684. uint16_t led_color = 0;
  685. unsigned long flags;
  686. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  687. if (ha->pio_address)
  688. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  689. spin_lock_irqsave(&ha->hardware_lock, flags);
  690. /* Save the Original GPIOE. */
  691. if (ha->pio_address) {
  692. gpio_enable = RD_REG_WORD_PIO(&reg->gpioe);
  693. gpio_data = RD_REG_WORD_PIO(&reg->gpiod);
  694. } else {
  695. gpio_enable = RD_REG_WORD(&reg->gpioe);
  696. gpio_data = RD_REG_WORD(&reg->gpiod);
  697. }
  698. /* Set the modified gpio_enable values */
  699. gpio_enable |= GPIO_LED_MASK;
  700. if (ha->pio_address) {
  701. WRT_REG_WORD_PIO(&reg->gpioe, gpio_enable);
  702. } else {
  703. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  704. RD_REG_WORD(&reg->gpioe);
  705. }
  706. qla2x00_flip_colors(ha, &led_color);
  707. /* Clear out any previously set LED color. */
  708. gpio_data &= ~GPIO_LED_MASK;
  709. /* Set the new input LED color to GPIOD. */
  710. gpio_data |= led_color;
  711. /* Set the modified gpio_data values */
  712. if (ha->pio_address) {
  713. WRT_REG_WORD_PIO(&reg->gpiod, gpio_data);
  714. } else {
  715. WRT_REG_WORD(&reg->gpiod, gpio_data);
  716. RD_REG_WORD(&reg->gpiod);
  717. }
  718. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  719. }
  720. int
  721. qla2x00_beacon_on(struct scsi_qla_host *ha)
  722. {
  723. uint16_t gpio_enable;
  724. uint16_t gpio_data;
  725. unsigned long flags;
  726. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  727. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  728. ha->fw_options[1] |= FO1_DISABLE_GPIO6_7;
  729. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  730. qla_printk(KERN_WARNING, ha,
  731. "Unable to update fw options (beacon on).\n");
  732. return QLA_FUNCTION_FAILED;
  733. }
  734. if (ha->pio_address)
  735. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  736. /* Turn off LEDs. */
  737. spin_lock_irqsave(&ha->hardware_lock, flags);
  738. if (ha->pio_address) {
  739. gpio_enable = RD_REG_WORD_PIO(&reg->gpioe);
  740. gpio_data = RD_REG_WORD_PIO(&reg->gpiod);
  741. } else {
  742. gpio_enable = RD_REG_WORD(&reg->gpioe);
  743. gpio_data = RD_REG_WORD(&reg->gpiod);
  744. }
  745. gpio_enable |= GPIO_LED_MASK;
  746. /* Set the modified gpio_enable values. */
  747. if (ha->pio_address) {
  748. WRT_REG_WORD_PIO(&reg->gpioe, gpio_enable);
  749. } else {
  750. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  751. RD_REG_WORD(&reg->gpioe);
  752. }
  753. /* Clear out previously set LED colour. */
  754. gpio_data &= ~GPIO_LED_MASK;
  755. if (ha->pio_address) {
  756. WRT_REG_WORD_PIO(&reg->gpiod, gpio_data);
  757. } else {
  758. WRT_REG_WORD(&reg->gpiod, gpio_data);
  759. RD_REG_WORD(&reg->gpiod);
  760. }
  761. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  762. /*
  763. * Let the per HBA timer kick off the blinking process based on
  764. * the following flags. No need to do anything else now.
  765. */
  766. ha->beacon_blink_led = 1;
  767. ha->beacon_color_state = 0;
  768. return QLA_SUCCESS;
  769. }
  770. int
  771. qla2x00_beacon_off(struct scsi_qla_host *ha)
  772. {
  773. int rval = QLA_SUCCESS;
  774. ha->beacon_blink_led = 0;
  775. /* Set the on flag so when it gets flipped it will be off. */
  776. if (IS_QLA2322(ha))
  777. ha->beacon_color_state = QLA_LED_ALL_ON;
  778. else
  779. ha->beacon_color_state = QLA_LED_GRN_ON;
  780. ha->isp_ops.beacon_blink(ha); /* This turns green LED off */
  781. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  782. ha->fw_options[1] &= ~FO1_DISABLE_GPIO6_7;
  783. rval = qla2x00_set_fw_options(ha, ha->fw_options);
  784. if (rval != QLA_SUCCESS)
  785. qla_printk(KERN_WARNING, ha,
  786. "Unable to update fw options (beacon off).\n");
  787. return rval;
  788. }
  789. static inline void
  790. qla24xx_flip_colors(scsi_qla_host_t *ha, uint16_t *pflags)
  791. {
  792. /* Flip all colors. */
  793. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  794. /* Turn off. */
  795. ha->beacon_color_state = 0;
  796. *pflags = 0;
  797. } else {
  798. /* Turn on. */
  799. ha->beacon_color_state = QLA_LED_ALL_ON;
  800. *pflags = GPDX_LED_YELLOW_ON | GPDX_LED_AMBER_ON;
  801. }
  802. }
  803. void
  804. qla24xx_beacon_blink(struct scsi_qla_host *ha)
  805. {
  806. uint16_t led_color = 0;
  807. uint32_t gpio_data;
  808. unsigned long flags;
  809. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  810. /* Save the Original GPIOD. */
  811. spin_lock_irqsave(&ha->hardware_lock, flags);
  812. gpio_data = RD_REG_DWORD(&reg->gpiod);
  813. /* Enable the gpio_data reg for update. */
  814. gpio_data |= GPDX_LED_UPDATE_MASK;
  815. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  816. gpio_data = RD_REG_DWORD(&reg->gpiod);
  817. /* Set the color bits. */
  818. qla24xx_flip_colors(ha, &led_color);
  819. /* Clear out any previously set LED color. */
  820. gpio_data &= ~GPDX_LED_COLOR_MASK;
  821. /* Set the new input LED color to GPIOD. */
  822. gpio_data |= led_color;
  823. /* Set the modified gpio_data values. */
  824. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  825. gpio_data = RD_REG_DWORD(&reg->gpiod);
  826. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  827. }
  828. int
  829. qla24xx_beacon_on(struct scsi_qla_host *ha)
  830. {
  831. uint32_t gpio_data;
  832. unsigned long flags;
  833. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  834. if (ha->beacon_blink_led == 0) {
  835. /* Enable firmware for update */
  836. ha->fw_options[1] |= ADD_FO1_DISABLE_GPIO_LED_CTRL;
  837. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS)
  838. return QLA_FUNCTION_FAILED;
  839. if (qla2x00_get_fw_options(ha, ha->fw_options) !=
  840. QLA_SUCCESS) {
  841. qla_printk(KERN_WARNING, ha,
  842. "Unable to update fw options (beacon on).\n");
  843. return QLA_FUNCTION_FAILED;
  844. }
  845. spin_lock_irqsave(&ha->hardware_lock, flags);
  846. gpio_data = RD_REG_DWORD(&reg->gpiod);
  847. /* Enable the gpio_data reg for update. */
  848. gpio_data |= GPDX_LED_UPDATE_MASK;
  849. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  850. RD_REG_DWORD(&reg->gpiod);
  851. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  852. }
  853. /* So all colors blink together. */
  854. ha->beacon_color_state = 0;
  855. /* Let the per HBA timer kick off the blinking process. */
  856. ha->beacon_blink_led = 1;
  857. return QLA_SUCCESS;
  858. }
  859. int
  860. qla24xx_beacon_off(struct scsi_qla_host *ha)
  861. {
  862. uint32_t gpio_data;
  863. unsigned long flags;
  864. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  865. ha->beacon_blink_led = 0;
  866. ha->beacon_color_state = QLA_LED_ALL_ON;
  867. ha->isp_ops.beacon_blink(ha); /* Will flip to all off. */
  868. /* Give control back to firmware. */
  869. spin_lock_irqsave(&ha->hardware_lock, flags);
  870. gpio_data = RD_REG_DWORD(&reg->gpiod);
  871. /* Disable the gpio_data reg for update. */
  872. gpio_data &= ~GPDX_LED_UPDATE_MASK;
  873. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  874. RD_REG_DWORD(&reg->gpiod);
  875. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  876. ha->fw_options[1] &= ~ADD_FO1_DISABLE_GPIO_LED_CTRL;
  877. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  878. qla_printk(KERN_WARNING, ha,
  879. "Unable to update fw options (beacon off).\n");
  880. return QLA_FUNCTION_FAILED;
  881. }
  882. if (qla2x00_get_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  883. qla_printk(KERN_WARNING, ha,
  884. "Unable to get fw options (beacon off).\n");
  885. return QLA_FUNCTION_FAILED;
  886. }
  887. return QLA_SUCCESS;
  888. }
  889. /*
  890. * Flash support routines
  891. */
  892. /**
  893. * qla2x00_flash_enable() - Setup flash for reading and writing.
  894. * @ha: HA context
  895. */
  896. static void
  897. qla2x00_flash_enable(scsi_qla_host_t *ha)
  898. {
  899. uint16_t data;
  900. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  901. data = RD_REG_WORD(&reg->ctrl_status);
  902. data |= CSR_FLASH_ENABLE;
  903. WRT_REG_WORD(&reg->ctrl_status, data);
  904. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  905. }
  906. /**
  907. * qla2x00_flash_disable() - Disable flash and allow RISC to run.
  908. * @ha: HA context
  909. */
  910. static void
  911. qla2x00_flash_disable(scsi_qla_host_t *ha)
  912. {
  913. uint16_t data;
  914. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  915. data = RD_REG_WORD(&reg->ctrl_status);
  916. data &= ~(CSR_FLASH_ENABLE);
  917. WRT_REG_WORD(&reg->ctrl_status, data);
  918. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  919. }
  920. /**
  921. * qla2x00_read_flash_byte() - Reads a byte from flash
  922. * @ha: HA context
  923. * @addr: Address in flash to read
  924. *
  925. * A word is read from the chip, but, only the lower byte is valid.
  926. *
  927. * Returns the byte read from flash @addr.
  928. */
  929. static uint8_t
  930. qla2x00_read_flash_byte(scsi_qla_host_t *ha, uint32_t addr)
  931. {
  932. uint16_t data;
  933. uint16_t bank_select;
  934. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  935. bank_select = RD_REG_WORD(&reg->ctrl_status);
  936. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  937. /* Specify 64K address range: */
  938. /* clear out Module Select and Flash Address bits [19:16]. */
  939. bank_select &= ~0xf8;
  940. bank_select |= addr >> 12 & 0xf0;
  941. bank_select |= CSR_FLASH_64K_BANK;
  942. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  943. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  944. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  945. data = RD_REG_WORD(&reg->flash_data);
  946. return (uint8_t)data;
  947. }
  948. /* Setup bit 16 of flash address. */
  949. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  950. bank_select |= CSR_FLASH_64K_BANK;
  951. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  952. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  953. } else if (((addr & BIT_16) == 0) &&
  954. (bank_select & CSR_FLASH_64K_BANK)) {
  955. bank_select &= ~(CSR_FLASH_64K_BANK);
  956. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  957. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  958. }
  959. /* Always perform IO mapped accesses to the FLASH registers. */
  960. if (ha->pio_address) {
  961. uint16_t data2;
  962. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  963. WRT_REG_WORD_PIO(&reg->flash_address, (uint16_t)addr);
  964. do {
  965. data = RD_REG_WORD_PIO(&reg->flash_data);
  966. barrier();
  967. cpu_relax();
  968. data2 = RD_REG_WORD_PIO(&reg->flash_data);
  969. } while (data != data2);
  970. } else {
  971. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  972. data = qla2x00_debounce_register(&reg->flash_data);
  973. }
  974. return (uint8_t)data;
  975. }
  976. /**
  977. * qla2x00_write_flash_byte() - Write a byte to flash
  978. * @ha: HA context
  979. * @addr: Address in flash to write
  980. * @data: Data to write
  981. */
  982. static void
  983. qla2x00_write_flash_byte(scsi_qla_host_t *ha, uint32_t addr, uint8_t data)
  984. {
  985. uint16_t bank_select;
  986. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  987. bank_select = RD_REG_WORD(&reg->ctrl_status);
  988. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  989. /* Specify 64K address range: */
  990. /* clear out Module Select and Flash Address bits [19:16]. */
  991. bank_select &= ~0xf8;
  992. bank_select |= addr >> 12 & 0xf0;
  993. bank_select |= CSR_FLASH_64K_BANK;
  994. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  995. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  996. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  997. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  998. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  999. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1000. return;
  1001. }
  1002. /* Setup bit 16 of flash address. */
  1003. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  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. } else if (((addr & BIT_16) == 0) &&
  1008. (bank_select & CSR_FLASH_64K_BANK)) {
  1009. bank_select &= ~(CSR_FLASH_64K_BANK);
  1010. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1011. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1012. }
  1013. /* Always perform IO mapped accesses to the FLASH registers. */
  1014. if (ha->pio_address) {
  1015. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  1016. WRT_REG_WORD_PIO(&reg->flash_address, (uint16_t)addr);
  1017. WRT_REG_WORD_PIO(&reg->flash_data, (uint16_t)data);
  1018. } else {
  1019. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1020. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1021. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  1022. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1023. }
  1024. }
  1025. /**
  1026. * qla2x00_poll_flash() - Polls flash for completion.
  1027. * @ha: HA context
  1028. * @addr: Address in flash to poll
  1029. * @poll_data: Data to be polled
  1030. * @man_id: Flash manufacturer ID
  1031. * @flash_id: Flash ID
  1032. *
  1033. * This function polls the device until bit 7 of what is read matches data
  1034. * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
  1035. * out (a fatal error). The flash book recommeds reading bit 7 again after
  1036. * reading bit 5 as a 1.
  1037. *
  1038. * Returns 0 on success, else non-zero.
  1039. */
  1040. static int
  1041. qla2x00_poll_flash(scsi_qla_host_t *ha, uint32_t addr, uint8_t poll_data,
  1042. uint8_t man_id, uint8_t flash_id)
  1043. {
  1044. int status;
  1045. uint8_t flash_data;
  1046. uint32_t cnt;
  1047. status = 1;
  1048. /* Wait for 30 seconds for command to finish. */
  1049. poll_data &= BIT_7;
  1050. for (cnt = 3000000; cnt; cnt--) {
  1051. flash_data = qla2x00_read_flash_byte(ha, addr);
  1052. if ((flash_data & BIT_7) == poll_data) {
  1053. status = 0;
  1054. break;
  1055. }
  1056. if (man_id != 0x40 && man_id != 0xda) {
  1057. if ((flash_data & BIT_5) && cnt > 2)
  1058. cnt = 2;
  1059. }
  1060. udelay(10);
  1061. barrier();
  1062. cond_resched();
  1063. }
  1064. return status;
  1065. }
  1066. /**
  1067. * qla2x00_program_flash_address() - Programs a flash address
  1068. * @ha: HA context
  1069. * @addr: Address in flash to program
  1070. * @data: Data to be written in flash
  1071. * @man_id: Flash manufacturer ID
  1072. * @flash_id: Flash ID
  1073. *
  1074. * Returns 0 on success, else non-zero.
  1075. */
  1076. static int
  1077. qla2x00_program_flash_address(scsi_qla_host_t *ha, uint32_t addr, uint8_t data,
  1078. uint8_t man_id, uint8_t flash_id)
  1079. {
  1080. /* Write Program Command Sequence. */
  1081. if (IS_OEM_001(ha)) {
  1082. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1083. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1084. qla2x00_write_flash_byte(ha, 0xaaa, 0xa0);
  1085. qla2x00_write_flash_byte(ha, addr, data);
  1086. } else {
  1087. if (man_id == 0xda && flash_id == 0xc1) {
  1088. qla2x00_write_flash_byte(ha, addr, data);
  1089. if (addr & 0x7e)
  1090. return 0;
  1091. } else {
  1092. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1093. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1094. qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
  1095. qla2x00_write_flash_byte(ha, addr, data);
  1096. }
  1097. }
  1098. udelay(150);
  1099. /* Wait for write to complete. */
  1100. return qla2x00_poll_flash(ha, addr, data, man_id, flash_id);
  1101. }
  1102. /**
  1103. * qla2x00_erase_flash() - Erase the flash.
  1104. * @ha: HA context
  1105. * @man_id: Flash manufacturer ID
  1106. * @flash_id: Flash ID
  1107. *
  1108. * Returns 0 on success, else non-zero.
  1109. */
  1110. static int
  1111. qla2x00_erase_flash(scsi_qla_host_t *ha, uint8_t man_id, uint8_t flash_id)
  1112. {
  1113. /* Individual Sector Erase Command Sequence */
  1114. if (IS_OEM_001(ha)) {
  1115. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1116. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1117. qla2x00_write_flash_byte(ha, 0xaaa, 0x80);
  1118. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1119. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1120. qla2x00_write_flash_byte(ha, 0xaaa, 0x10);
  1121. } else {
  1122. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1123. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1124. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1125. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1126. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1127. qla2x00_write_flash_byte(ha, 0x5555, 0x10);
  1128. }
  1129. udelay(150);
  1130. /* Wait for erase to complete. */
  1131. return qla2x00_poll_flash(ha, 0x00, 0x80, man_id, flash_id);
  1132. }
  1133. /**
  1134. * qla2x00_erase_flash_sector() - Erase a flash sector.
  1135. * @ha: HA context
  1136. * @addr: Flash sector to erase
  1137. * @sec_mask: Sector address mask
  1138. * @man_id: Flash manufacturer ID
  1139. * @flash_id: Flash ID
  1140. *
  1141. * Returns 0 on success, else non-zero.
  1142. */
  1143. static int
  1144. qla2x00_erase_flash_sector(scsi_qla_host_t *ha, uint32_t addr,
  1145. uint32_t sec_mask, uint8_t man_id, uint8_t flash_id)
  1146. {
  1147. /* Individual Sector Erase Command Sequence */
  1148. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1149. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1150. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1151. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1152. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1153. if (man_id == 0x1f && flash_id == 0x13)
  1154. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
  1155. else
  1156. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
  1157. udelay(150);
  1158. /* Wait for erase to complete. */
  1159. return qla2x00_poll_flash(ha, addr, 0x80, man_id, flash_id);
  1160. }
  1161. /**
  1162. * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
  1163. * @man_id: Flash manufacturer ID
  1164. * @flash_id: Flash ID
  1165. */
  1166. static void
  1167. qla2x00_get_flash_manufacturer(scsi_qla_host_t *ha, uint8_t *man_id,
  1168. uint8_t *flash_id)
  1169. {
  1170. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1171. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1172. qla2x00_write_flash_byte(ha, 0x5555, 0x90);
  1173. *man_id = qla2x00_read_flash_byte(ha, 0x0000);
  1174. *flash_id = qla2x00_read_flash_byte(ha, 0x0001);
  1175. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1176. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1177. qla2x00_write_flash_byte(ha, 0x5555, 0xf0);
  1178. }
  1179. static void
  1180. qla2x00_read_flash_data(scsi_qla_host_t *ha, uint8_t *tmp_buf, uint32_t saddr,
  1181. uint32_t length)
  1182. {
  1183. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1184. uint32_t midpoint, ilength;
  1185. uint8_t data;
  1186. midpoint = length / 2;
  1187. WRT_REG_WORD(&reg->nvram, 0);
  1188. RD_REG_WORD(&reg->nvram);
  1189. for (ilength = 0; ilength < length; saddr++, ilength++, tmp_buf++) {
  1190. if (ilength == midpoint) {
  1191. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1192. RD_REG_WORD(&reg->nvram);
  1193. }
  1194. data = qla2x00_read_flash_byte(ha, saddr);
  1195. if (saddr % 100)
  1196. udelay(10);
  1197. *tmp_buf = data;
  1198. cond_resched();
  1199. }
  1200. }
  1201. static inline void
  1202. qla2x00_suspend_hba(struct scsi_qla_host *ha)
  1203. {
  1204. int cnt;
  1205. unsigned long flags;
  1206. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1207. /* Suspend HBA. */
  1208. scsi_block_requests(ha->host);
  1209. ha->isp_ops.disable_intrs(ha);
  1210. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1211. /* Pause RISC. */
  1212. spin_lock_irqsave(&ha->hardware_lock, flags);
  1213. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  1214. RD_REG_WORD(&reg->hccr);
  1215. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  1216. for (cnt = 0; cnt < 30000; cnt++) {
  1217. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
  1218. break;
  1219. udelay(100);
  1220. }
  1221. } else {
  1222. udelay(10);
  1223. }
  1224. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1225. }
  1226. static inline void
  1227. qla2x00_resume_hba(struct scsi_qla_host *ha)
  1228. {
  1229. /* Resume HBA. */
  1230. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1231. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1232. qla2xxx_wake_dpc(ha);
  1233. qla2x00_wait_for_hba_online(ha);
  1234. scsi_unblock_requests(ha->host);
  1235. }
  1236. uint8_t *
  1237. qla2x00_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1238. uint32_t offset, uint32_t length)
  1239. {
  1240. uint32_t addr, midpoint;
  1241. uint8_t *data;
  1242. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1243. /* Suspend HBA. */
  1244. qla2x00_suspend_hba(ha);
  1245. /* Go with read. */
  1246. midpoint = ha->optrom_size / 2;
  1247. qla2x00_flash_enable(ha);
  1248. WRT_REG_WORD(&reg->nvram, 0);
  1249. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1250. for (addr = offset, data = buf; addr < length; addr++, data++) {
  1251. if (addr == midpoint) {
  1252. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1253. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1254. }
  1255. *data = qla2x00_read_flash_byte(ha, addr);
  1256. }
  1257. qla2x00_flash_disable(ha);
  1258. /* Resume HBA. */
  1259. qla2x00_resume_hba(ha);
  1260. return buf;
  1261. }
  1262. int
  1263. qla2x00_write_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1264. uint32_t offset, uint32_t length)
  1265. {
  1266. int rval;
  1267. uint8_t man_id, flash_id, sec_number, data;
  1268. uint16_t wd;
  1269. uint32_t addr, liter, sec_mask, rest_addr;
  1270. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1271. /* Suspend HBA. */
  1272. qla2x00_suspend_hba(ha);
  1273. rval = QLA_SUCCESS;
  1274. sec_number = 0;
  1275. /* Reset ISP chip. */
  1276. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  1277. pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
  1278. /* Go with write. */
  1279. qla2x00_flash_enable(ha);
  1280. do { /* Loop once to provide quick error exit */
  1281. /* Structure of flash memory based on manufacturer */
  1282. if (IS_OEM_001(ha)) {
  1283. /* OEM variant with special flash part. */
  1284. man_id = flash_id = 0;
  1285. rest_addr = 0xffff;
  1286. sec_mask = 0x10000;
  1287. goto update_flash;
  1288. }
  1289. qla2x00_get_flash_manufacturer(ha, &man_id, &flash_id);
  1290. switch (man_id) {
  1291. case 0x20: /* ST flash. */
  1292. if (flash_id == 0xd2 || flash_id == 0xe3) {
  1293. /*
  1294. * ST m29w008at part - 64kb sector size with
  1295. * 32kb,8kb,8kb,16kb sectors at memory address
  1296. * 0xf0000.
  1297. */
  1298. rest_addr = 0xffff;
  1299. sec_mask = 0x10000;
  1300. break;
  1301. }
  1302. /*
  1303. * ST m29w010b part - 16kb sector size
  1304. * Default to 16kb sectors
  1305. */
  1306. rest_addr = 0x3fff;
  1307. sec_mask = 0x1c000;
  1308. break;
  1309. case 0x40: /* Mostel flash. */
  1310. /* Mostel v29c51001 part - 512 byte sector size. */
  1311. rest_addr = 0x1ff;
  1312. sec_mask = 0x1fe00;
  1313. break;
  1314. case 0xbf: /* SST flash. */
  1315. /* SST39sf10 part - 4kb sector size. */
  1316. rest_addr = 0xfff;
  1317. sec_mask = 0x1f000;
  1318. break;
  1319. case 0xda: /* Winbond flash. */
  1320. /* Winbond W29EE011 part - 256 byte sector size. */
  1321. rest_addr = 0x7f;
  1322. sec_mask = 0x1ff80;
  1323. break;
  1324. case 0xc2: /* Macronix flash. */
  1325. /* 64k sector size. */
  1326. if (flash_id == 0x38 || flash_id == 0x4f) {
  1327. rest_addr = 0xffff;
  1328. sec_mask = 0x10000;
  1329. break;
  1330. }
  1331. /* Fall through... */
  1332. case 0x1f: /* Atmel flash. */
  1333. /* 512k sector size. */
  1334. if (flash_id == 0x13) {
  1335. rest_addr = 0x7fffffff;
  1336. sec_mask = 0x80000000;
  1337. break;
  1338. }
  1339. /* Fall through... */
  1340. case 0x01: /* AMD flash. */
  1341. if (flash_id == 0x38 || flash_id == 0x40 ||
  1342. flash_id == 0x4f) {
  1343. /* Am29LV081 part - 64kb sector size. */
  1344. /* Am29LV002BT part - 64kb sector size. */
  1345. rest_addr = 0xffff;
  1346. sec_mask = 0x10000;
  1347. break;
  1348. } else if (flash_id == 0x3e) {
  1349. /*
  1350. * Am29LV008b part - 64kb sector size with
  1351. * 32kb,8kb,8kb,16kb sector at memory address
  1352. * h0xf0000.
  1353. */
  1354. rest_addr = 0xffff;
  1355. sec_mask = 0x10000;
  1356. break;
  1357. } else if (flash_id == 0x20 || flash_id == 0x6e) {
  1358. /*
  1359. * Am29LV010 part or AM29f010 - 16kb sector
  1360. * size.
  1361. */
  1362. rest_addr = 0x3fff;
  1363. sec_mask = 0x1c000;
  1364. break;
  1365. } else if (flash_id == 0x6d) {
  1366. /* Am29LV001 part - 8kb sector size. */
  1367. rest_addr = 0x1fff;
  1368. sec_mask = 0x1e000;
  1369. break;
  1370. }
  1371. default:
  1372. /* Default to 16 kb sector size. */
  1373. rest_addr = 0x3fff;
  1374. sec_mask = 0x1c000;
  1375. break;
  1376. }
  1377. update_flash:
  1378. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1379. if (qla2x00_erase_flash(ha, man_id, flash_id)) {
  1380. rval = QLA_FUNCTION_FAILED;
  1381. break;
  1382. }
  1383. }
  1384. for (addr = offset, liter = 0; liter < length; liter++,
  1385. addr++) {
  1386. data = buf[liter];
  1387. /* Are we at the beginning of a sector? */
  1388. if ((addr & rest_addr) == 0) {
  1389. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1390. if (addr >= 0x10000UL) {
  1391. if (((addr >> 12) & 0xf0) &&
  1392. ((man_id == 0x01 &&
  1393. flash_id == 0x3e) ||
  1394. (man_id == 0x20 &&
  1395. flash_id == 0xd2))) {
  1396. sec_number++;
  1397. if (sec_number == 1) {
  1398. rest_addr =
  1399. 0x7fff;
  1400. sec_mask =
  1401. 0x18000;
  1402. } else if (
  1403. sec_number == 2 ||
  1404. sec_number == 3) {
  1405. rest_addr =
  1406. 0x1fff;
  1407. sec_mask =
  1408. 0x1e000;
  1409. } else if (
  1410. sec_number == 4) {
  1411. rest_addr =
  1412. 0x3fff;
  1413. sec_mask =
  1414. 0x1c000;
  1415. }
  1416. }
  1417. }
  1418. } else if (addr == ha->optrom_size / 2) {
  1419. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1420. RD_REG_WORD(&reg->nvram);
  1421. }
  1422. if (flash_id == 0xda && man_id == 0xc1) {
  1423. qla2x00_write_flash_byte(ha, 0x5555,
  1424. 0xaa);
  1425. qla2x00_write_flash_byte(ha, 0x2aaa,
  1426. 0x55);
  1427. qla2x00_write_flash_byte(ha, 0x5555,
  1428. 0xa0);
  1429. } else if (!IS_QLA2322(ha) && !IS_QLA6322(ha)) {
  1430. /* Then erase it */
  1431. if (qla2x00_erase_flash_sector(ha,
  1432. addr, sec_mask, man_id,
  1433. flash_id)) {
  1434. rval = QLA_FUNCTION_FAILED;
  1435. break;
  1436. }
  1437. if (man_id == 0x01 && flash_id == 0x6d)
  1438. sec_number++;
  1439. }
  1440. }
  1441. if (man_id == 0x01 && flash_id == 0x6d) {
  1442. if (sec_number == 1 &&
  1443. addr == (rest_addr - 1)) {
  1444. rest_addr = 0x0fff;
  1445. sec_mask = 0x1f000;
  1446. } else if (sec_number == 3 && (addr & 0x7ffe)) {
  1447. rest_addr = 0x3fff;
  1448. sec_mask = 0x1c000;
  1449. }
  1450. }
  1451. if (qla2x00_program_flash_address(ha, addr, data,
  1452. man_id, flash_id)) {
  1453. rval = QLA_FUNCTION_FAILED;
  1454. break;
  1455. }
  1456. cond_resched();
  1457. }
  1458. } while (0);
  1459. qla2x00_flash_disable(ha);
  1460. /* Resume HBA. */
  1461. qla2x00_resume_hba(ha);
  1462. return rval;
  1463. }
  1464. uint8_t *
  1465. qla24xx_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1466. uint32_t offset, uint32_t length)
  1467. {
  1468. /* Suspend HBA. */
  1469. scsi_block_requests(ha->host);
  1470. ha->isp_ops.disable_intrs(ha);
  1471. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1472. /* Go with read. */
  1473. qla24xx_read_flash_data(ha, (uint32_t *)buf, offset >> 2, length >> 2);
  1474. /* Resume HBA. */
  1475. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1476. ha->isp_ops.enable_intrs(ha);
  1477. scsi_unblock_requests(ha->host);
  1478. return buf;
  1479. }
  1480. int
  1481. qla24xx_write_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1482. uint32_t offset, uint32_t length)
  1483. {
  1484. int rval;
  1485. /* Suspend HBA. */
  1486. scsi_block_requests(ha->host);
  1487. ha->isp_ops.disable_intrs(ha);
  1488. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1489. /* Go with write. */
  1490. rval = qla24xx_write_flash_data(ha, (uint32_t *)buf, offset >> 2,
  1491. length >> 2);
  1492. /* Resume HBA -- RISC reset needed. */
  1493. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1494. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1495. qla2xxx_wake_dpc(ha);
  1496. qla2x00_wait_for_hba_online(ha);
  1497. scsi_unblock_requests(ha->host);
  1498. return rval;
  1499. }
  1500. /**
  1501. * qla2x00_get_fcode_version() - Determine an FCODE image's version.
  1502. * @ha: HA context
  1503. * @pcids: Pointer to the FCODE PCI data structure
  1504. *
  1505. * The process of retrieving the FCODE version information is at best
  1506. * described as interesting.
  1507. *
  1508. * Within the first 100h bytes of the image an ASCII string is present
  1509. * which contains several pieces of information including the FCODE
  1510. * version. Unfortunately it seems the only reliable way to retrieve
  1511. * the version is by scanning for another sentinel within the string,
  1512. * the FCODE build date:
  1513. *
  1514. * ... 2.00.02 10/17/02 ...
  1515. *
  1516. * Returns QLA_SUCCESS on successful retrieval of version.
  1517. */
  1518. static void
  1519. qla2x00_get_fcode_version(scsi_qla_host_t *ha, uint32_t pcids)
  1520. {
  1521. int ret = QLA_FUNCTION_FAILED;
  1522. uint32_t istart, iend, iter, vend;
  1523. uint8_t do_next, rbyte, *vbyte;
  1524. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1525. /* Skip the PCI data structure. */
  1526. istart = pcids +
  1527. ((qla2x00_read_flash_byte(ha, pcids + 0x0B) << 8) |
  1528. qla2x00_read_flash_byte(ha, pcids + 0x0A));
  1529. iend = istart + 0x100;
  1530. do {
  1531. /* Scan for the sentinel date string...eeewww. */
  1532. do_next = 0;
  1533. iter = istart;
  1534. while ((iter < iend) && !do_next) {
  1535. iter++;
  1536. if (qla2x00_read_flash_byte(ha, iter) == '/') {
  1537. if (qla2x00_read_flash_byte(ha, iter + 2) ==
  1538. '/')
  1539. do_next++;
  1540. else if (qla2x00_read_flash_byte(ha,
  1541. iter + 3) == '/')
  1542. do_next++;
  1543. }
  1544. }
  1545. if (!do_next)
  1546. break;
  1547. /* Backtrack to previous ' ' (space). */
  1548. do_next = 0;
  1549. while ((iter > istart) && !do_next) {
  1550. iter--;
  1551. if (qla2x00_read_flash_byte(ha, iter) == ' ')
  1552. do_next++;
  1553. }
  1554. if (!do_next)
  1555. break;
  1556. /*
  1557. * Mark end of version tag, and find previous ' ' (space) or
  1558. * string length (recent FCODE images -- major hack ahead!!!).
  1559. */
  1560. vend = iter - 1;
  1561. do_next = 0;
  1562. while ((iter > istart) && !do_next) {
  1563. iter--;
  1564. rbyte = qla2x00_read_flash_byte(ha, iter);
  1565. if (rbyte == ' ' || rbyte == 0xd || rbyte == 0x10)
  1566. do_next++;
  1567. }
  1568. if (!do_next)
  1569. break;
  1570. /* Mark beginning of version tag, and copy data. */
  1571. iter++;
  1572. if ((vend - iter) &&
  1573. ((vend - iter) < sizeof(ha->fcode_revision))) {
  1574. vbyte = ha->fcode_revision;
  1575. while (iter <= vend) {
  1576. *vbyte++ = qla2x00_read_flash_byte(ha, iter);
  1577. iter++;
  1578. }
  1579. ret = QLA_SUCCESS;
  1580. }
  1581. } while (0);
  1582. if (ret != QLA_SUCCESS)
  1583. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1584. }
  1585. int
  1586. qla2x00_get_flash_version(scsi_qla_host_t *ha, void *mbuf)
  1587. {
  1588. int ret = QLA_SUCCESS;
  1589. uint8_t code_type, last_image;
  1590. uint32_t pcihdr, pcids;
  1591. uint8_t *dbyte;
  1592. uint16_t *dcode;
  1593. if (!ha->pio_address || !mbuf)
  1594. return QLA_FUNCTION_FAILED;
  1595. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  1596. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  1597. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1598. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1599. qla2x00_flash_enable(ha);
  1600. /* Begin with first PCI expansion ROM header. */
  1601. pcihdr = 0;
  1602. last_image = 1;
  1603. do {
  1604. /* Verify PCI expansion ROM header. */
  1605. if (qla2x00_read_flash_byte(ha, pcihdr) != 0x55 ||
  1606. qla2x00_read_flash_byte(ha, pcihdr + 0x01) != 0xaa) {
  1607. /* No signature */
  1608. DEBUG2(printk("scsi(%ld): No matching ROM "
  1609. "signature.\n", ha->host_no));
  1610. ret = QLA_FUNCTION_FAILED;
  1611. break;
  1612. }
  1613. /* Locate PCI data structure. */
  1614. pcids = pcihdr +
  1615. ((qla2x00_read_flash_byte(ha, pcihdr + 0x19) << 8) |
  1616. qla2x00_read_flash_byte(ha, pcihdr + 0x18));
  1617. /* Validate signature of PCI data structure. */
  1618. if (qla2x00_read_flash_byte(ha, pcids) != 'P' ||
  1619. qla2x00_read_flash_byte(ha, pcids + 0x1) != 'C' ||
  1620. qla2x00_read_flash_byte(ha, pcids + 0x2) != 'I' ||
  1621. qla2x00_read_flash_byte(ha, pcids + 0x3) != 'R') {
  1622. /* Incorrect header. */
  1623. DEBUG2(printk("%s(): PCI data struct not found "
  1624. "pcir_adr=%x.\n", __func__, pcids));
  1625. ret = QLA_FUNCTION_FAILED;
  1626. break;
  1627. }
  1628. /* Read version */
  1629. code_type = qla2x00_read_flash_byte(ha, pcids + 0x14);
  1630. switch (code_type) {
  1631. case ROM_CODE_TYPE_BIOS:
  1632. /* Intel x86, PC-AT compatible. */
  1633. ha->bios_revision[0] =
  1634. qla2x00_read_flash_byte(ha, pcids + 0x12);
  1635. ha->bios_revision[1] =
  1636. qla2x00_read_flash_byte(ha, pcids + 0x13);
  1637. DEBUG3(printk("%s(): read BIOS %d.%d.\n", __func__,
  1638. ha->bios_revision[1], ha->bios_revision[0]));
  1639. break;
  1640. case ROM_CODE_TYPE_FCODE:
  1641. /* Open Firmware standard for PCI (FCode). */
  1642. /* Eeeewww... */
  1643. qla2x00_get_fcode_version(ha, pcids);
  1644. break;
  1645. case ROM_CODE_TYPE_EFI:
  1646. /* Extensible Firmware Interface (EFI). */
  1647. ha->efi_revision[0] =
  1648. qla2x00_read_flash_byte(ha, pcids + 0x12);
  1649. ha->efi_revision[1] =
  1650. qla2x00_read_flash_byte(ha, pcids + 0x13);
  1651. DEBUG3(printk("%s(): read EFI %d.%d.\n", __func__,
  1652. ha->efi_revision[1], ha->efi_revision[0]));
  1653. break;
  1654. default:
  1655. DEBUG2(printk("%s(): Unrecognized code type %x at "
  1656. "pcids %x.\n", __func__, code_type, pcids));
  1657. break;
  1658. }
  1659. last_image = qla2x00_read_flash_byte(ha, pcids + 0x15) & BIT_7;
  1660. /* Locate next PCI expansion ROM. */
  1661. pcihdr += ((qla2x00_read_flash_byte(ha, pcids + 0x11) << 8) |
  1662. qla2x00_read_flash_byte(ha, pcids + 0x10)) * 512;
  1663. } while (!last_image);
  1664. if (IS_QLA2322(ha)) {
  1665. /* Read firmware image information. */
  1666. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1667. dbyte = mbuf;
  1668. memset(dbyte, 0, 8);
  1669. dcode = (uint16_t *)dbyte;
  1670. qla2x00_read_flash_data(ha, dbyte, FA_RISC_CODE_ADDR * 4 + 10,
  1671. 8);
  1672. DEBUG3(printk("%s(%ld): dumping fw ver from flash:\n",
  1673. __func__, ha->host_no));
  1674. DEBUG3(qla2x00_dump_buffer((uint8_t *)dbyte, 8));
  1675. if ((dcode[0] == 0xffff && dcode[1] == 0xffff &&
  1676. dcode[2] == 0xffff && dcode[3] == 0xffff) ||
  1677. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  1678. dcode[3] == 0)) {
  1679. DEBUG2(printk("%s(): Unrecognized fw revision at "
  1680. "%x.\n", __func__, FA_RISC_CODE_ADDR * 4));
  1681. } else {
  1682. /* values are in big endian */
  1683. ha->fw_revision[0] = dbyte[0] << 16 | dbyte[1];
  1684. ha->fw_revision[1] = dbyte[2] << 16 | dbyte[3];
  1685. ha->fw_revision[2] = dbyte[4] << 16 | dbyte[5];
  1686. }
  1687. }
  1688. qla2x00_flash_disable(ha);
  1689. return ret;
  1690. }
  1691. int
  1692. qla24xx_get_flash_version(scsi_qla_host_t *ha, void *mbuf)
  1693. {
  1694. int ret = QLA_SUCCESS;
  1695. uint32_t pcihdr, pcids;
  1696. uint32_t *dcode;
  1697. uint8_t *bcode;
  1698. uint8_t code_type, last_image;
  1699. int i;
  1700. if (!mbuf)
  1701. return QLA_FUNCTION_FAILED;
  1702. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  1703. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  1704. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1705. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1706. dcode = mbuf;
  1707. /* Begin with first PCI expansion ROM header. */
  1708. pcihdr = 0;
  1709. last_image = 1;
  1710. do {
  1711. /* Verify PCI expansion ROM header. */
  1712. qla24xx_read_flash_data(ha, dcode, pcihdr >> 2, 0x20);
  1713. bcode = mbuf + (pcihdr % 4);
  1714. if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa) {
  1715. /* No signature */
  1716. DEBUG2(printk("scsi(%ld): No matching ROM "
  1717. "signature.\n", ha->host_no));
  1718. ret = QLA_FUNCTION_FAILED;
  1719. break;
  1720. }
  1721. /* Locate PCI data structure. */
  1722. pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
  1723. qla24xx_read_flash_data(ha, dcode, pcids >> 2, 0x20);
  1724. bcode = mbuf + (pcihdr % 4);
  1725. /* Validate signature of PCI data structure. */
  1726. if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
  1727. bcode[0x2] != 'I' || bcode[0x3] != 'R') {
  1728. /* Incorrect header. */
  1729. DEBUG2(printk("%s(): PCI data struct not found "
  1730. "pcir_adr=%x.\n", __func__, pcids));
  1731. ret = QLA_FUNCTION_FAILED;
  1732. break;
  1733. }
  1734. /* Read version */
  1735. code_type = bcode[0x14];
  1736. switch (code_type) {
  1737. case ROM_CODE_TYPE_BIOS:
  1738. /* Intel x86, PC-AT compatible. */
  1739. ha->bios_revision[0] = bcode[0x12];
  1740. ha->bios_revision[1] = bcode[0x13];
  1741. DEBUG3(printk("%s(): read BIOS %d.%d.\n", __func__,
  1742. ha->bios_revision[1], ha->bios_revision[0]));
  1743. break;
  1744. case ROM_CODE_TYPE_FCODE:
  1745. /* Open Firmware standard for PCI (FCode). */
  1746. ha->fcode_revision[0] = bcode[0x12];
  1747. ha->fcode_revision[1] = bcode[0x13];
  1748. DEBUG3(printk("%s(): read FCODE %d.%d.\n", __func__,
  1749. ha->fcode_revision[1], ha->fcode_revision[0]));
  1750. break;
  1751. case ROM_CODE_TYPE_EFI:
  1752. /* Extensible Firmware Interface (EFI). */
  1753. ha->efi_revision[0] = bcode[0x12];
  1754. ha->efi_revision[1] = bcode[0x13];
  1755. DEBUG3(printk("%s(): read EFI %d.%d.\n", __func__,
  1756. ha->efi_revision[1], ha->efi_revision[0]));
  1757. break;
  1758. default:
  1759. DEBUG2(printk("%s(): Unrecognized code type %x at "
  1760. "pcids %x.\n", __func__, code_type, pcids));
  1761. break;
  1762. }
  1763. last_image = bcode[0x15] & BIT_7;
  1764. /* Locate next PCI expansion ROM. */
  1765. pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
  1766. } while (!last_image);
  1767. /* Read firmware image information. */
  1768. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1769. dcode = mbuf;
  1770. qla24xx_read_flash_data(ha, dcode, FA_RISC_CODE_ADDR + 4, 4);
  1771. for (i = 0; i < 4; i++)
  1772. dcode[i] = be32_to_cpu(dcode[i]);
  1773. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  1774. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  1775. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  1776. dcode[3] == 0)) {
  1777. DEBUG2(printk("%s(): Unrecognized fw version at %x.\n",
  1778. __func__, FA_RISC_CODE_ADDR));
  1779. } else {
  1780. ha->fw_revision[0] = dcode[0];
  1781. ha->fw_revision[1] = dcode[1];
  1782. ha->fw_revision[2] = dcode[2];
  1783. ha->fw_revision[3] = dcode[3];
  1784. }
  1785. return ret;
  1786. }