qla_sup.c 75 KB

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