qla_sup.c 72 KB

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