qla_sup.c 70 KB

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