qla_sup.c 69 KB

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