qla_sup.c 78 KB

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