qla_sup.c 78 KB

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