qla_sup.c 69 KB

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