qla_sup.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658
  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)) {
  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 void
  812. qla24xx_unprotect_flash(struct qla_hw_data *ha)
  813. {
  814. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  815. /* Enable flash write. */
  816. WRT_REG_DWORD(&reg->ctrl_status,
  817. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  818. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  819. if (!ha->fdt_wrt_disable)
  820. return;
  821. /* Disable flash write-protection. */
  822. qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
  823. /* Some flash parts need an additional zero-write to clear bits.*/
  824. qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
  825. }
  826. static void
  827. qla24xx_protect_flash(struct qla_hw_data *ha)
  828. {
  829. uint32_t cnt;
  830. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  831. if (!ha->fdt_wrt_disable)
  832. goto skip_wrt_protect;
  833. /* Enable flash write-protection and wait for completion. */
  834. qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101),
  835. ha->fdt_wrt_disable);
  836. for (cnt = 300; cnt &&
  837. qla24xx_read_flash_dword(ha, flash_conf_addr(ha, 0x005)) & BIT_0;
  838. cnt--) {
  839. udelay(10);
  840. }
  841. skip_wrt_protect:
  842. /* Disable flash write. */
  843. WRT_REG_DWORD(&reg->ctrl_status,
  844. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  845. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  846. }
  847. static int
  848. qla24xx_write_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
  849. uint32_t dwords)
  850. {
  851. int ret;
  852. uint32_t liter, miter;
  853. uint32_t sec_mask, rest_addr;
  854. uint32_t fdata;
  855. dma_addr_t optrom_dma;
  856. void *optrom = NULL;
  857. uint32_t *s, *d;
  858. struct qla_hw_data *ha = vha->hw;
  859. ret = QLA_SUCCESS;
  860. /* Prepare burst-capable write on supported ISPs. */
  861. if ((IS_QLA25XX(ha) || IS_QLA81XX(ha)) && !(faddr & 0xfff) &&
  862. dwords > OPTROM_BURST_DWORDS) {
  863. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  864. &optrom_dma, GFP_KERNEL);
  865. if (!optrom) {
  866. qla_printk(KERN_DEBUG, ha,
  867. "Unable to allocate memory for optrom burst write "
  868. "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
  869. }
  870. }
  871. rest_addr = (ha->fdt_block_size >> 2) - 1;
  872. sec_mask = ~rest_addr;
  873. qla24xx_unprotect_flash(ha);
  874. for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
  875. fdata = (faddr & sec_mask) << 2;
  876. /* Are we at the beginning of a sector? */
  877. if ((faddr & rest_addr) == 0) {
  878. /* Do sector unprotect. */
  879. if (ha->fdt_unprotect_sec_cmd)
  880. qla24xx_write_flash_dword(ha,
  881. ha->fdt_unprotect_sec_cmd,
  882. (fdata & 0xff00) | ((fdata << 16) &
  883. 0xff0000) | ((fdata >> 16) & 0xff));
  884. ret = qla24xx_write_flash_dword(ha, ha->fdt_erase_cmd,
  885. (fdata & 0xff00) |((fdata << 16) &
  886. 0xff0000) | ((fdata >> 16) & 0xff));
  887. if (ret != QLA_SUCCESS) {
  888. DEBUG9(qla_printk("Unable to flash sector: "
  889. "address=%x.\n", faddr));
  890. break;
  891. }
  892. }
  893. /* Go with burst-write. */
  894. if (optrom && (liter + OPTROM_BURST_DWORDS) <= dwords) {
  895. /* Copy data to DMA'ble buffer. */
  896. for (miter = 0, s = optrom, d = dwptr;
  897. miter < OPTROM_BURST_DWORDS; miter++, s++, d++)
  898. *s = cpu_to_le32(*d);
  899. ret = qla2x00_load_ram(vha, optrom_dma,
  900. flash_data_addr(ha, faddr),
  901. OPTROM_BURST_DWORDS);
  902. if (ret != QLA_SUCCESS) {
  903. qla_printk(KERN_WARNING, ha,
  904. "Unable to burst-write optrom segment "
  905. "(%x/%x/%llx).\n", ret,
  906. flash_data_addr(ha, faddr),
  907. (unsigned long long)optrom_dma);
  908. qla_printk(KERN_WARNING, ha,
  909. "Reverting to slow-write.\n");
  910. dma_free_coherent(&ha->pdev->dev,
  911. OPTROM_BURST_SIZE, optrom, optrom_dma);
  912. optrom = NULL;
  913. } else {
  914. liter += OPTROM_BURST_DWORDS - 1;
  915. faddr += OPTROM_BURST_DWORDS - 1;
  916. dwptr += OPTROM_BURST_DWORDS - 1;
  917. continue;
  918. }
  919. }
  920. ret = qla24xx_write_flash_dword(ha,
  921. flash_data_addr(ha, faddr), cpu_to_le32(*dwptr));
  922. if (ret != QLA_SUCCESS) {
  923. DEBUG9(printk("%s(%ld) Unable to program flash "
  924. "address=%x data=%x.\n", __func__,
  925. vha->host_no, faddr, *dwptr));
  926. break;
  927. }
  928. /* Do sector protect. */
  929. if (ha->fdt_unprotect_sec_cmd &&
  930. ((faddr & rest_addr) == rest_addr))
  931. qla24xx_write_flash_dword(ha,
  932. ha->fdt_protect_sec_cmd,
  933. (fdata & 0xff00) | ((fdata << 16) &
  934. 0xff0000) | ((fdata >> 16) & 0xff));
  935. }
  936. qla24xx_protect_flash(ha);
  937. if (optrom)
  938. dma_free_coherent(&ha->pdev->dev,
  939. OPTROM_BURST_SIZE, optrom, optrom_dma);
  940. return ret;
  941. }
  942. uint8_t *
  943. qla2x00_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
  944. uint32_t bytes)
  945. {
  946. uint32_t i;
  947. uint16_t *wptr;
  948. struct qla_hw_data *ha = vha->hw;
  949. /* Word reads to NVRAM via registers. */
  950. wptr = (uint16_t *)buf;
  951. qla2x00_lock_nvram_access(ha);
  952. for (i = 0; i < bytes >> 1; i++, naddr++)
  953. wptr[i] = cpu_to_le16(qla2x00_get_nvram_word(ha,
  954. naddr));
  955. qla2x00_unlock_nvram_access(ha);
  956. return buf;
  957. }
  958. uint8_t *
  959. qla24xx_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
  960. uint32_t bytes)
  961. {
  962. uint32_t i;
  963. uint32_t *dwptr;
  964. struct qla_hw_data *ha = vha->hw;
  965. /* Dword reads to flash. */
  966. dwptr = (uint32_t *)buf;
  967. for (i = 0; i < bytes >> 2; i++, naddr++)
  968. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  969. nvram_data_addr(ha, naddr)));
  970. return buf;
  971. }
  972. int
  973. qla2x00_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
  974. uint32_t bytes)
  975. {
  976. int ret, stat;
  977. uint32_t i;
  978. uint16_t *wptr;
  979. unsigned long flags;
  980. struct qla_hw_data *ha = vha->hw;
  981. ret = QLA_SUCCESS;
  982. spin_lock_irqsave(&ha->hardware_lock, flags);
  983. qla2x00_lock_nvram_access(ha);
  984. /* Disable NVRAM write-protection. */
  985. stat = qla2x00_clear_nvram_protection(ha);
  986. wptr = (uint16_t *)buf;
  987. for (i = 0; i < bytes >> 1; i++, naddr++) {
  988. qla2x00_write_nvram_word(ha, naddr,
  989. cpu_to_le16(*wptr));
  990. wptr++;
  991. }
  992. /* Enable NVRAM write-protection. */
  993. qla2x00_set_nvram_protection(ha, stat);
  994. qla2x00_unlock_nvram_access(ha);
  995. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  996. return ret;
  997. }
  998. int
  999. qla24xx_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
  1000. uint32_t bytes)
  1001. {
  1002. int ret;
  1003. uint32_t i;
  1004. uint32_t *dwptr;
  1005. struct qla_hw_data *ha = vha->hw;
  1006. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1007. ret = QLA_SUCCESS;
  1008. /* Enable flash write. */
  1009. WRT_REG_DWORD(&reg->ctrl_status,
  1010. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  1011. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  1012. /* Disable NVRAM write-protection. */
  1013. qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
  1014. qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
  1015. /* Dword writes to flash. */
  1016. dwptr = (uint32_t *)buf;
  1017. for (i = 0; i < bytes >> 2; i++, naddr++, dwptr++) {
  1018. ret = qla24xx_write_flash_dword(ha,
  1019. nvram_data_addr(ha, naddr), cpu_to_le32(*dwptr));
  1020. if (ret != QLA_SUCCESS) {
  1021. DEBUG9(qla_printk("Unable to program nvram address=%x "
  1022. "data=%x.\n", naddr, *dwptr));
  1023. break;
  1024. }
  1025. }
  1026. /* Enable NVRAM write-protection. */
  1027. qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0x8c);
  1028. /* Disable flash write. */
  1029. WRT_REG_DWORD(&reg->ctrl_status,
  1030. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  1031. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  1032. return ret;
  1033. }
  1034. uint8_t *
  1035. qla25xx_read_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
  1036. uint32_t bytes)
  1037. {
  1038. uint32_t i;
  1039. uint32_t *dwptr;
  1040. struct qla_hw_data *ha = vha->hw;
  1041. /* Dword reads to flash. */
  1042. dwptr = (uint32_t *)buf;
  1043. for (i = 0; i < bytes >> 2; i++, naddr++)
  1044. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  1045. flash_data_addr(ha, ha->flt_region_vpd_nvram | naddr)));
  1046. return buf;
  1047. }
  1048. int
  1049. qla25xx_write_nvram_data(scsi_qla_host_t *vha, uint8_t *buf, uint32_t naddr,
  1050. uint32_t bytes)
  1051. {
  1052. struct qla_hw_data *ha = vha->hw;
  1053. #define RMW_BUFFER_SIZE (64 * 1024)
  1054. uint8_t *dbuf;
  1055. dbuf = vmalloc(RMW_BUFFER_SIZE);
  1056. if (!dbuf)
  1057. return QLA_MEMORY_ALLOC_FAILED;
  1058. ha->isp_ops->read_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
  1059. RMW_BUFFER_SIZE);
  1060. memcpy(dbuf + (naddr << 2), buf, bytes);
  1061. ha->isp_ops->write_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
  1062. RMW_BUFFER_SIZE);
  1063. vfree(dbuf);
  1064. return QLA_SUCCESS;
  1065. }
  1066. static inline void
  1067. qla2x00_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
  1068. {
  1069. if (IS_QLA2322(ha)) {
  1070. /* Flip all colors. */
  1071. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  1072. /* Turn off. */
  1073. ha->beacon_color_state = 0;
  1074. *pflags = GPIO_LED_ALL_OFF;
  1075. } else {
  1076. /* Turn on. */
  1077. ha->beacon_color_state = QLA_LED_ALL_ON;
  1078. *pflags = GPIO_LED_RGA_ON;
  1079. }
  1080. } else {
  1081. /* Flip green led only. */
  1082. if (ha->beacon_color_state == QLA_LED_GRN_ON) {
  1083. /* Turn off. */
  1084. ha->beacon_color_state = 0;
  1085. *pflags = GPIO_LED_GREEN_OFF_AMBER_OFF;
  1086. } else {
  1087. /* Turn on. */
  1088. ha->beacon_color_state = QLA_LED_GRN_ON;
  1089. *pflags = GPIO_LED_GREEN_ON_AMBER_OFF;
  1090. }
  1091. }
  1092. }
  1093. #define PIO_REG(h, r) ((h)->pio_address + offsetof(struct device_reg_2xxx, r))
  1094. void
  1095. qla2x00_beacon_blink(struct scsi_qla_host *vha)
  1096. {
  1097. uint16_t gpio_enable;
  1098. uint16_t gpio_data;
  1099. uint16_t led_color = 0;
  1100. unsigned long flags;
  1101. struct qla_hw_data *ha = vha->hw;
  1102. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1103. spin_lock_irqsave(&ha->hardware_lock, flags);
  1104. /* Save the Original GPIOE. */
  1105. if (ha->pio_address) {
  1106. gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
  1107. gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
  1108. } else {
  1109. gpio_enable = RD_REG_WORD(&reg->gpioe);
  1110. gpio_data = RD_REG_WORD(&reg->gpiod);
  1111. }
  1112. /* Set the modified gpio_enable values */
  1113. gpio_enable |= GPIO_LED_MASK;
  1114. if (ha->pio_address) {
  1115. WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
  1116. } else {
  1117. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  1118. RD_REG_WORD(&reg->gpioe);
  1119. }
  1120. qla2x00_flip_colors(ha, &led_color);
  1121. /* Clear out any previously set LED color. */
  1122. gpio_data &= ~GPIO_LED_MASK;
  1123. /* Set the new input LED color to GPIOD. */
  1124. gpio_data |= led_color;
  1125. /* Set the modified gpio_data values */
  1126. if (ha->pio_address) {
  1127. WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
  1128. } else {
  1129. WRT_REG_WORD(&reg->gpiod, gpio_data);
  1130. RD_REG_WORD(&reg->gpiod);
  1131. }
  1132. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1133. }
  1134. int
  1135. qla2x00_beacon_on(struct scsi_qla_host *vha)
  1136. {
  1137. uint16_t gpio_enable;
  1138. uint16_t gpio_data;
  1139. unsigned long flags;
  1140. struct qla_hw_data *ha = vha->hw;
  1141. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1142. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  1143. ha->fw_options[1] |= FO1_DISABLE_GPIO6_7;
  1144. if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
  1145. qla_printk(KERN_WARNING, ha,
  1146. "Unable to update fw options (beacon on).\n");
  1147. return QLA_FUNCTION_FAILED;
  1148. }
  1149. /* Turn off LEDs. */
  1150. spin_lock_irqsave(&ha->hardware_lock, flags);
  1151. if (ha->pio_address) {
  1152. gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
  1153. gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
  1154. } else {
  1155. gpio_enable = RD_REG_WORD(&reg->gpioe);
  1156. gpio_data = RD_REG_WORD(&reg->gpiod);
  1157. }
  1158. gpio_enable |= GPIO_LED_MASK;
  1159. /* Set the modified gpio_enable values. */
  1160. if (ha->pio_address) {
  1161. WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
  1162. } else {
  1163. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  1164. RD_REG_WORD(&reg->gpioe);
  1165. }
  1166. /* Clear out previously set LED colour. */
  1167. gpio_data &= ~GPIO_LED_MASK;
  1168. if (ha->pio_address) {
  1169. WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
  1170. } else {
  1171. WRT_REG_WORD(&reg->gpiod, gpio_data);
  1172. RD_REG_WORD(&reg->gpiod);
  1173. }
  1174. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1175. /*
  1176. * Let the per HBA timer kick off the blinking process based on
  1177. * the following flags. No need to do anything else now.
  1178. */
  1179. ha->beacon_blink_led = 1;
  1180. ha->beacon_color_state = 0;
  1181. return QLA_SUCCESS;
  1182. }
  1183. int
  1184. qla2x00_beacon_off(struct scsi_qla_host *vha)
  1185. {
  1186. int rval = QLA_SUCCESS;
  1187. struct qla_hw_data *ha = vha->hw;
  1188. ha->beacon_blink_led = 0;
  1189. /* Set the on flag so when it gets flipped it will be off. */
  1190. if (IS_QLA2322(ha))
  1191. ha->beacon_color_state = QLA_LED_ALL_ON;
  1192. else
  1193. ha->beacon_color_state = QLA_LED_GRN_ON;
  1194. ha->isp_ops->beacon_blink(vha); /* This turns green LED off */
  1195. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  1196. ha->fw_options[1] &= ~FO1_DISABLE_GPIO6_7;
  1197. rval = qla2x00_set_fw_options(vha, ha->fw_options);
  1198. if (rval != QLA_SUCCESS)
  1199. qla_printk(KERN_WARNING, ha,
  1200. "Unable to update fw options (beacon off).\n");
  1201. return rval;
  1202. }
  1203. static inline void
  1204. qla24xx_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
  1205. {
  1206. /* Flip all colors. */
  1207. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  1208. /* Turn off. */
  1209. ha->beacon_color_state = 0;
  1210. *pflags = 0;
  1211. } else {
  1212. /* Turn on. */
  1213. ha->beacon_color_state = QLA_LED_ALL_ON;
  1214. *pflags = GPDX_LED_YELLOW_ON | GPDX_LED_AMBER_ON;
  1215. }
  1216. }
  1217. void
  1218. qla24xx_beacon_blink(struct scsi_qla_host *vha)
  1219. {
  1220. uint16_t led_color = 0;
  1221. uint32_t gpio_data;
  1222. unsigned long flags;
  1223. struct qla_hw_data *ha = vha->hw;
  1224. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1225. /* Save the Original GPIOD. */
  1226. spin_lock_irqsave(&ha->hardware_lock, flags);
  1227. gpio_data = RD_REG_DWORD(&reg->gpiod);
  1228. /* Enable the gpio_data reg for update. */
  1229. gpio_data |= GPDX_LED_UPDATE_MASK;
  1230. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  1231. gpio_data = RD_REG_DWORD(&reg->gpiod);
  1232. /* Set the color bits. */
  1233. qla24xx_flip_colors(ha, &led_color);
  1234. /* Clear out any previously set LED color. */
  1235. gpio_data &= ~GPDX_LED_COLOR_MASK;
  1236. /* Set the new input LED color to GPIOD. */
  1237. gpio_data |= led_color;
  1238. /* Set the modified gpio_data values. */
  1239. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  1240. gpio_data = RD_REG_DWORD(&reg->gpiod);
  1241. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1242. }
  1243. int
  1244. qla24xx_beacon_on(struct scsi_qla_host *vha)
  1245. {
  1246. uint32_t gpio_data;
  1247. unsigned long flags;
  1248. struct qla_hw_data *ha = vha->hw;
  1249. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1250. if (ha->beacon_blink_led == 0) {
  1251. /* Enable firmware for update */
  1252. ha->fw_options[1] |= ADD_FO1_DISABLE_GPIO_LED_CTRL;
  1253. if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS)
  1254. return QLA_FUNCTION_FAILED;
  1255. if (qla2x00_get_fw_options(vha, ha->fw_options) !=
  1256. QLA_SUCCESS) {
  1257. qla_printk(KERN_WARNING, ha,
  1258. "Unable to update fw options (beacon on).\n");
  1259. return QLA_FUNCTION_FAILED;
  1260. }
  1261. spin_lock_irqsave(&ha->hardware_lock, flags);
  1262. gpio_data = RD_REG_DWORD(&reg->gpiod);
  1263. /* Enable the gpio_data reg for update. */
  1264. gpio_data |= GPDX_LED_UPDATE_MASK;
  1265. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  1266. RD_REG_DWORD(&reg->gpiod);
  1267. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1268. }
  1269. /* So all colors blink together. */
  1270. ha->beacon_color_state = 0;
  1271. /* Let the per HBA timer kick off the blinking process. */
  1272. ha->beacon_blink_led = 1;
  1273. return QLA_SUCCESS;
  1274. }
  1275. int
  1276. qla24xx_beacon_off(struct scsi_qla_host *vha)
  1277. {
  1278. uint32_t gpio_data;
  1279. unsigned long flags;
  1280. struct qla_hw_data *ha = vha->hw;
  1281. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1282. ha->beacon_blink_led = 0;
  1283. ha->beacon_color_state = QLA_LED_ALL_ON;
  1284. ha->isp_ops->beacon_blink(vha); /* Will flip to all off. */
  1285. /* Give control back to firmware. */
  1286. spin_lock_irqsave(&ha->hardware_lock, flags);
  1287. gpio_data = RD_REG_DWORD(&reg->gpiod);
  1288. /* Disable the gpio_data reg for update. */
  1289. gpio_data &= ~GPDX_LED_UPDATE_MASK;
  1290. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  1291. RD_REG_DWORD(&reg->gpiod);
  1292. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1293. ha->fw_options[1] &= ~ADD_FO1_DISABLE_GPIO_LED_CTRL;
  1294. if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
  1295. qla_printk(KERN_WARNING, ha,
  1296. "Unable to update fw options (beacon off).\n");
  1297. return QLA_FUNCTION_FAILED;
  1298. }
  1299. if (qla2x00_get_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
  1300. qla_printk(KERN_WARNING, ha,
  1301. "Unable to get fw options (beacon off).\n");
  1302. return QLA_FUNCTION_FAILED;
  1303. }
  1304. return QLA_SUCCESS;
  1305. }
  1306. /*
  1307. * Flash support routines
  1308. */
  1309. /**
  1310. * qla2x00_flash_enable() - Setup flash for reading and writing.
  1311. * @ha: HA context
  1312. */
  1313. static void
  1314. qla2x00_flash_enable(struct qla_hw_data *ha)
  1315. {
  1316. uint16_t data;
  1317. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1318. data = RD_REG_WORD(&reg->ctrl_status);
  1319. data |= CSR_FLASH_ENABLE;
  1320. WRT_REG_WORD(&reg->ctrl_status, data);
  1321. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1322. }
  1323. /**
  1324. * qla2x00_flash_disable() - Disable flash and allow RISC to run.
  1325. * @ha: HA context
  1326. */
  1327. static void
  1328. qla2x00_flash_disable(struct qla_hw_data *ha)
  1329. {
  1330. uint16_t data;
  1331. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1332. data = RD_REG_WORD(&reg->ctrl_status);
  1333. data &= ~(CSR_FLASH_ENABLE);
  1334. WRT_REG_WORD(&reg->ctrl_status, data);
  1335. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1336. }
  1337. /**
  1338. * qla2x00_read_flash_byte() - Reads a byte from flash
  1339. * @ha: HA context
  1340. * @addr: Address in flash to read
  1341. *
  1342. * A word is read from the chip, but, only the lower byte is valid.
  1343. *
  1344. * Returns the byte read from flash @addr.
  1345. */
  1346. static uint8_t
  1347. qla2x00_read_flash_byte(struct qla_hw_data *ha, uint32_t addr)
  1348. {
  1349. uint16_t data;
  1350. uint16_t bank_select;
  1351. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1352. bank_select = RD_REG_WORD(&reg->ctrl_status);
  1353. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1354. /* Specify 64K address range: */
  1355. /* clear out Module Select and Flash Address bits [19:16]. */
  1356. bank_select &= ~0xf8;
  1357. bank_select |= addr >> 12 & 0xf0;
  1358. bank_select |= CSR_FLASH_64K_BANK;
  1359. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1360. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1361. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1362. data = RD_REG_WORD(&reg->flash_data);
  1363. return (uint8_t)data;
  1364. }
  1365. /* Setup bit 16 of flash address. */
  1366. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1367. bank_select |= CSR_FLASH_64K_BANK;
  1368. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1369. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1370. } else if (((addr & BIT_16) == 0) &&
  1371. (bank_select & CSR_FLASH_64K_BANK)) {
  1372. bank_select &= ~(CSR_FLASH_64K_BANK);
  1373. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1374. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1375. }
  1376. /* Always perform IO mapped accesses to the FLASH registers. */
  1377. if (ha->pio_address) {
  1378. uint16_t data2;
  1379. WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
  1380. do {
  1381. data = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
  1382. barrier();
  1383. cpu_relax();
  1384. data2 = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
  1385. } while (data != data2);
  1386. } else {
  1387. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1388. data = qla2x00_debounce_register(&reg->flash_data);
  1389. }
  1390. return (uint8_t)data;
  1391. }
  1392. /**
  1393. * qla2x00_write_flash_byte() - Write a byte to flash
  1394. * @ha: HA context
  1395. * @addr: Address in flash to write
  1396. * @data: Data to write
  1397. */
  1398. static void
  1399. qla2x00_write_flash_byte(struct qla_hw_data *ha, uint32_t addr, uint8_t data)
  1400. {
  1401. uint16_t bank_select;
  1402. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1403. bank_select = RD_REG_WORD(&reg->ctrl_status);
  1404. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1405. /* Specify 64K address range: */
  1406. /* clear out Module Select and Flash Address bits [19:16]. */
  1407. bank_select &= ~0xf8;
  1408. bank_select |= addr >> 12 & 0xf0;
  1409. bank_select |= CSR_FLASH_64K_BANK;
  1410. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1411. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1412. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1413. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1414. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  1415. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1416. return;
  1417. }
  1418. /* Setup bit 16 of flash address. */
  1419. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1420. bank_select |= CSR_FLASH_64K_BANK;
  1421. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1422. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1423. } else if (((addr & BIT_16) == 0) &&
  1424. (bank_select & CSR_FLASH_64K_BANK)) {
  1425. bank_select &= ~(CSR_FLASH_64K_BANK);
  1426. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1427. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1428. }
  1429. /* Always perform IO mapped accesses to the FLASH registers. */
  1430. if (ha->pio_address) {
  1431. WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
  1432. WRT_REG_WORD_PIO(PIO_REG(ha, flash_data), (uint16_t)data);
  1433. } else {
  1434. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1435. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1436. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  1437. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1438. }
  1439. }
  1440. /**
  1441. * qla2x00_poll_flash() - Polls flash for completion.
  1442. * @ha: HA context
  1443. * @addr: Address in flash to poll
  1444. * @poll_data: Data to be polled
  1445. * @man_id: Flash manufacturer ID
  1446. * @flash_id: Flash ID
  1447. *
  1448. * This function polls the device until bit 7 of what is read matches data
  1449. * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
  1450. * out (a fatal error). The flash book recommeds reading bit 7 again after
  1451. * reading bit 5 as a 1.
  1452. *
  1453. * Returns 0 on success, else non-zero.
  1454. */
  1455. static int
  1456. qla2x00_poll_flash(struct qla_hw_data *ha, uint32_t addr, uint8_t poll_data,
  1457. uint8_t man_id, uint8_t flash_id)
  1458. {
  1459. int status;
  1460. uint8_t flash_data;
  1461. uint32_t cnt;
  1462. status = 1;
  1463. /* Wait for 30 seconds for command to finish. */
  1464. poll_data &= BIT_7;
  1465. for (cnt = 3000000; cnt; cnt--) {
  1466. flash_data = qla2x00_read_flash_byte(ha, addr);
  1467. if ((flash_data & BIT_7) == poll_data) {
  1468. status = 0;
  1469. break;
  1470. }
  1471. if (man_id != 0x40 && man_id != 0xda) {
  1472. if ((flash_data & BIT_5) && cnt > 2)
  1473. cnt = 2;
  1474. }
  1475. udelay(10);
  1476. barrier();
  1477. cond_resched();
  1478. }
  1479. return status;
  1480. }
  1481. /**
  1482. * qla2x00_program_flash_address() - Programs a flash address
  1483. * @ha: HA context
  1484. * @addr: Address in flash to program
  1485. * @data: Data to be written in flash
  1486. * @man_id: Flash manufacturer ID
  1487. * @flash_id: Flash ID
  1488. *
  1489. * Returns 0 on success, else non-zero.
  1490. */
  1491. static int
  1492. qla2x00_program_flash_address(struct qla_hw_data *ha, uint32_t addr,
  1493. uint8_t data, uint8_t man_id, uint8_t flash_id)
  1494. {
  1495. /* Write Program Command Sequence. */
  1496. if (IS_OEM_001(ha)) {
  1497. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1498. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1499. qla2x00_write_flash_byte(ha, 0xaaa, 0xa0);
  1500. qla2x00_write_flash_byte(ha, addr, data);
  1501. } else {
  1502. if (man_id == 0xda && flash_id == 0xc1) {
  1503. qla2x00_write_flash_byte(ha, addr, data);
  1504. if (addr & 0x7e)
  1505. return 0;
  1506. } else {
  1507. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1508. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1509. qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
  1510. qla2x00_write_flash_byte(ha, addr, data);
  1511. }
  1512. }
  1513. udelay(150);
  1514. /* Wait for write to complete. */
  1515. return qla2x00_poll_flash(ha, addr, data, man_id, flash_id);
  1516. }
  1517. /**
  1518. * qla2x00_erase_flash() - Erase the flash.
  1519. * @ha: HA context
  1520. * @man_id: Flash manufacturer ID
  1521. * @flash_id: Flash ID
  1522. *
  1523. * Returns 0 on success, else non-zero.
  1524. */
  1525. static int
  1526. qla2x00_erase_flash(struct qla_hw_data *ha, uint8_t man_id, uint8_t flash_id)
  1527. {
  1528. /* Individual Sector Erase Command Sequence */
  1529. if (IS_OEM_001(ha)) {
  1530. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1531. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1532. qla2x00_write_flash_byte(ha, 0xaaa, 0x80);
  1533. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1534. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1535. qla2x00_write_flash_byte(ha, 0xaaa, 0x10);
  1536. } else {
  1537. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1538. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1539. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1540. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1541. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1542. qla2x00_write_flash_byte(ha, 0x5555, 0x10);
  1543. }
  1544. udelay(150);
  1545. /* Wait for erase to complete. */
  1546. return qla2x00_poll_flash(ha, 0x00, 0x80, man_id, flash_id);
  1547. }
  1548. /**
  1549. * qla2x00_erase_flash_sector() - Erase a flash sector.
  1550. * @ha: HA context
  1551. * @addr: Flash sector to erase
  1552. * @sec_mask: Sector address mask
  1553. * @man_id: Flash manufacturer ID
  1554. * @flash_id: Flash ID
  1555. *
  1556. * Returns 0 on success, else non-zero.
  1557. */
  1558. static int
  1559. qla2x00_erase_flash_sector(struct qla_hw_data *ha, uint32_t addr,
  1560. uint32_t sec_mask, uint8_t man_id, uint8_t flash_id)
  1561. {
  1562. /* Individual Sector Erase Command Sequence */
  1563. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1564. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1565. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1566. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1567. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1568. if (man_id == 0x1f && flash_id == 0x13)
  1569. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
  1570. else
  1571. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
  1572. udelay(150);
  1573. /* Wait for erase to complete. */
  1574. return qla2x00_poll_flash(ha, addr, 0x80, man_id, flash_id);
  1575. }
  1576. /**
  1577. * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
  1578. * @man_id: Flash manufacturer ID
  1579. * @flash_id: Flash ID
  1580. */
  1581. static void
  1582. qla2x00_get_flash_manufacturer(struct qla_hw_data *ha, uint8_t *man_id,
  1583. uint8_t *flash_id)
  1584. {
  1585. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1586. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1587. qla2x00_write_flash_byte(ha, 0x5555, 0x90);
  1588. *man_id = qla2x00_read_flash_byte(ha, 0x0000);
  1589. *flash_id = qla2x00_read_flash_byte(ha, 0x0001);
  1590. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1591. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1592. qla2x00_write_flash_byte(ha, 0x5555, 0xf0);
  1593. }
  1594. static void
  1595. qla2x00_read_flash_data(struct qla_hw_data *ha, uint8_t *tmp_buf,
  1596. uint32_t saddr, uint32_t length)
  1597. {
  1598. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1599. uint32_t midpoint, ilength;
  1600. uint8_t data;
  1601. midpoint = length / 2;
  1602. WRT_REG_WORD(&reg->nvram, 0);
  1603. RD_REG_WORD(&reg->nvram);
  1604. for (ilength = 0; ilength < length; saddr++, ilength++, tmp_buf++) {
  1605. if (ilength == midpoint) {
  1606. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1607. RD_REG_WORD(&reg->nvram);
  1608. }
  1609. data = qla2x00_read_flash_byte(ha, saddr);
  1610. if (saddr % 100)
  1611. udelay(10);
  1612. *tmp_buf = data;
  1613. cond_resched();
  1614. }
  1615. }
  1616. static inline void
  1617. qla2x00_suspend_hba(struct scsi_qla_host *vha)
  1618. {
  1619. int cnt;
  1620. unsigned long flags;
  1621. struct qla_hw_data *ha = vha->hw;
  1622. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1623. /* Suspend HBA. */
  1624. scsi_block_requests(vha->host);
  1625. ha->isp_ops->disable_intrs(ha);
  1626. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1627. /* Pause RISC. */
  1628. spin_lock_irqsave(&ha->hardware_lock, flags);
  1629. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  1630. RD_REG_WORD(&reg->hccr);
  1631. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  1632. for (cnt = 0; cnt < 30000; cnt++) {
  1633. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
  1634. break;
  1635. udelay(100);
  1636. }
  1637. } else {
  1638. udelay(10);
  1639. }
  1640. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1641. }
  1642. static inline void
  1643. qla2x00_resume_hba(struct scsi_qla_host *vha)
  1644. {
  1645. struct qla_hw_data *ha = vha->hw;
  1646. /* Resume HBA. */
  1647. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1648. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  1649. qla2xxx_wake_dpc(vha);
  1650. qla2x00_wait_for_hba_online(vha);
  1651. scsi_unblock_requests(vha->host);
  1652. }
  1653. uint8_t *
  1654. qla2x00_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
  1655. uint32_t offset, uint32_t length)
  1656. {
  1657. uint32_t addr, midpoint;
  1658. uint8_t *data;
  1659. struct qla_hw_data *ha = vha->hw;
  1660. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1661. /* Suspend HBA. */
  1662. qla2x00_suspend_hba(vha);
  1663. /* Go with read. */
  1664. midpoint = ha->optrom_size / 2;
  1665. qla2x00_flash_enable(ha);
  1666. WRT_REG_WORD(&reg->nvram, 0);
  1667. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1668. for (addr = offset, data = buf; addr < length; addr++, data++) {
  1669. if (addr == midpoint) {
  1670. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1671. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1672. }
  1673. *data = qla2x00_read_flash_byte(ha, addr);
  1674. }
  1675. qla2x00_flash_disable(ha);
  1676. /* Resume HBA. */
  1677. qla2x00_resume_hba(vha);
  1678. return buf;
  1679. }
  1680. int
  1681. qla2x00_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
  1682. uint32_t offset, uint32_t length)
  1683. {
  1684. int rval;
  1685. uint8_t man_id, flash_id, sec_number, data;
  1686. uint16_t wd;
  1687. uint32_t addr, liter, sec_mask, rest_addr;
  1688. struct qla_hw_data *ha = vha->hw;
  1689. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1690. /* Suspend HBA. */
  1691. qla2x00_suspend_hba(vha);
  1692. rval = QLA_SUCCESS;
  1693. sec_number = 0;
  1694. /* Reset ISP chip. */
  1695. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  1696. pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
  1697. /* Go with write. */
  1698. qla2x00_flash_enable(ha);
  1699. do { /* Loop once to provide quick error exit */
  1700. /* Structure of flash memory based on manufacturer */
  1701. if (IS_OEM_001(ha)) {
  1702. /* OEM variant with special flash part. */
  1703. man_id = flash_id = 0;
  1704. rest_addr = 0xffff;
  1705. sec_mask = 0x10000;
  1706. goto update_flash;
  1707. }
  1708. qla2x00_get_flash_manufacturer(ha, &man_id, &flash_id);
  1709. switch (man_id) {
  1710. case 0x20: /* ST flash. */
  1711. if (flash_id == 0xd2 || flash_id == 0xe3) {
  1712. /*
  1713. * ST m29w008at part - 64kb sector size with
  1714. * 32kb,8kb,8kb,16kb sectors at memory address
  1715. * 0xf0000.
  1716. */
  1717. rest_addr = 0xffff;
  1718. sec_mask = 0x10000;
  1719. break;
  1720. }
  1721. /*
  1722. * ST m29w010b part - 16kb sector size
  1723. * Default to 16kb sectors
  1724. */
  1725. rest_addr = 0x3fff;
  1726. sec_mask = 0x1c000;
  1727. break;
  1728. case 0x40: /* Mostel flash. */
  1729. /* Mostel v29c51001 part - 512 byte sector size. */
  1730. rest_addr = 0x1ff;
  1731. sec_mask = 0x1fe00;
  1732. break;
  1733. case 0xbf: /* SST flash. */
  1734. /* SST39sf10 part - 4kb sector size. */
  1735. rest_addr = 0xfff;
  1736. sec_mask = 0x1f000;
  1737. break;
  1738. case 0xda: /* Winbond flash. */
  1739. /* Winbond W29EE011 part - 256 byte sector size. */
  1740. rest_addr = 0x7f;
  1741. sec_mask = 0x1ff80;
  1742. break;
  1743. case 0xc2: /* Macronix flash. */
  1744. /* 64k sector size. */
  1745. if (flash_id == 0x38 || flash_id == 0x4f) {
  1746. rest_addr = 0xffff;
  1747. sec_mask = 0x10000;
  1748. break;
  1749. }
  1750. /* Fall through... */
  1751. case 0x1f: /* Atmel flash. */
  1752. /* 512k sector size. */
  1753. if (flash_id == 0x13) {
  1754. rest_addr = 0x7fffffff;
  1755. sec_mask = 0x80000000;
  1756. break;
  1757. }
  1758. /* Fall through... */
  1759. case 0x01: /* AMD flash. */
  1760. if (flash_id == 0x38 || flash_id == 0x40 ||
  1761. flash_id == 0x4f) {
  1762. /* Am29LV081 part - 64kb sector size. */
  1763. /* Am29LV002BT part - 64kb sector size. */
  1764. rest_addr = 0xffff;
  1765. sec_mask = 0x10000;
  1766. break;
  1767. } else if (flash_id == 0x3e) {
  1768. /*
  1769. * Am29LV008b part - 64kb sector size with
  1770. * 32kb,8kb,8kb,16kb sector at memory address
  1771. * h0xf0000.
  1772. */
  1773. rest_addr = 0xffff;
  1774. sec_mask = 0x10000;
  1775. break;
  1776. } else if (flash_id == 0x20 || flash_id == 0x6e) {
  1777. /*
  1778. * Am29LV010 part or AM29f010 - 16kb sector
  1779. * size.
  1780. */
  1781. rest_addr = 0x3fff;
  1782. sec_mask = 0x1c000;
  1783. break;
  1784. } else if (flash_id == 0x6d) {
  1785. /* Am29LV001 part - 8kb sector size. */
  1786. rest_addr = 0x1fff;
  1787. sec_mask = 0x1e000;
  1788. break;
  1789. }
  1790. default:
  1791. /* Default to 16 kb sector size. */
  1792. rest_addr = 0x3fff;
  1793. sec_mask = 0x1c000;
  1794. break;
  1795. }
  1796. update_flash:
  1797. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1798. if (qla2x00_erase_flash(ha, man_id, flash_id)) {
  1799. rval = QLA_FUNCTION_FAILED;
  1800. break;
  1801. }
  1802. }
  1803. for (addr = offset, liter = 0; liter < length; liter++,
  1804. addr++) {
  1805. data = buf[liter];
  1806. /* Are we at the beginning of a sector? */
  1807. if ((addr & rest_addr) == 0) {
  1808. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1809. if (addr >= 0x10000UL) {
  1810. if (((addr >> 12) & 0xf0) &&
  1811. ((man_id == 0x01 &&
  1812. flash_id == 0x3e) ||
  1813. (man_id == 0x20 &&
  1814. flash_id == 0xd2))) {
  1815. sec_number++;
  1816. if (sec_number == 1) {
  1817. rest_addr =
  1818. 0x7fff;
  1819. sec_mask =
  1820. 0x18000;
  1821. } else if (
  1822. sec_number == 2 ||
  1823. sec_number == 3) {
  1824. rest_addr =
  1825. 0x1fff;
  1826. sec_mask =
  1827. 0x1e000;
  1828. } else if (
  1829. sec_number == 4) {
  1830. rest_addr =
  1831. 0x3fff;
  1832. sec_mask =
  1833. 0x1c000;
  1834. }
  1835. }
  1836. }
  1837. } else if (addr == ha->optrom_size / 2) {
  1838. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1839. RD_REG_WORD(&reg->nvram);
  1840. }
  1841. if (flash_id == 0xda && man_id == 0xc1) {
  1842. qla2x00_write_flash_byte(ha, 0x5555,
  1843. 0xaa);
  1844. qla2x00_write_flash_byte(ha, 0x2aaa,
  1845. 0x55);
  1846. qla2x00_write_flash_byte(ha, 0x5555,
  1847. 0xa0);
  1848. } else if (!IS_QLA2322(ha) && !IS_QLA6322(ha)) {
  1849. /* Then erase it */
  1850. if (qla2x00_erase_flash_sector(ha,
  1851. addr, sec_mask, man_id,
  1852. flash_id)) {
  1853. rval = QLA_FUNCTION_FAILED;
  1854. break;
  1855. }
  1856. if (man_id == 0x01 && flash_id == 0x6d)
  1857. sec_number++;
  1858. }
  1859. }
  1860. if (man_id == 0x01 && flash_id == 0x6d) {
  1861. if (sec_number == 1 &&
  1862. addr == (rest_addr - 1)) {
  1863. rest_addr = 0x0fff;
  1864. sec_mask = 0x1f000;
  1865. } else if (sec_number == 3 && (addr & 0x7ffe)) {
  1866. rest_addr = 0x3fff;
  1867. sec_mask = 0x1c000;
  1868. }
  1869. }
  1870. if (qla2x00_program_flash_address(ha, addr, data,
  1871. man_id, flash_id)) {
  1872. rval = QLA_FUNCTION_FAILED;
  1873. break;
  1874. }
  1875. cond_resched();
  1876. }
  1877. } while (0);
  1878. qla2x00_flash_disable(ha);
  1879. /* Resume HBA. */
  1880. qla2x00_resume_hba(vha);
  1881. return rval;
  1882. }
  1883. uint8_t *
  1884. qla24xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
  1885. uint32_t offset, uint32_t length)
  1886. {
  1887. struct qla_hw_data *ha = vha->hw;
  1888. /* Suspend HBA. */
  1889. scsi_block_requests(vha->host);
  1890. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1891. /* Go with read. */
  1892. qla24xx_read_flash_data(vha, (uint32_t *)buf, offset >> 2, length >> 2);
  1893. /* Resume HBA. */
  1894. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1895. scsi_unblock_requests(vha->host);
  1896. return buf;
  1897. }
  1898. int
  1899. qla24xx_write_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
  1900. uint32_t offset, uint32_t length)
  1901. {
  1902. int rval;
  1903. struct qla_hw_data *ha = vha->hw;
  1904. /* Suspend HBA. */
  1905. scsi_block_requests(vha->host);
  1906. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1907. /* Go with write. */
  1908. rval = qla24xx_write_flash_data(vha, (uint32_t *)buf, offset >> 2,
  1909. length >> 2);
  1910. /* Resume HBA -- RISC reset needed. */
  1911. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1912. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  1913. qla2xxx_wake_dpc(vha);
  1914. qla2x00_wait_for_hba_online(vha);
  1915. scsi_unblock_requests(vha->host);
  1916. return rval;
  1917. }
  1918. uint8_t *
  1919. qla25xx_read_optrom_data(struct scsi_qla_host *vha, uint8_t *buf,
  1920. uint32_t offset, uint32_t length)
  1921. {
  1922. int rval;
  1923. dma_addr_t optrom_dma;
  1924. void *optrom;
  1925. uint8_t *pbuf;
  1926. uint32_t faddr, left, burst;
  1927. struct qla_hw_data *ha = vha->hw;
  1928. if (offset & 0xfff)
  1929. goto slow_read;
  1930. if (length < OPTROM_BURST_SIZE)
  1931. goto slow_read;
  1932. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  1933. &optrom_dma, GFP_KERNEL);
  1934. if (!optrom) {
  1935. qla_printk(KERN_DEBUG, ha,
  1936. "Unable to allocate memory for optrom burst read "
  1937. "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
  1938. goto slow_read;
  1939. }
  1940. pbuf = buf;
  1941. faddr = offset >> 2;
  1942. left = length >> 2;
  1943. burst = OPTROM_BURST_DWORDS;
  1944. while (left != 0) {
  1945. if (burst > left)
  1946. burst = left;
  1947. rval = qla2x00_dump_ram(vha, optrom_dma,
  1948. flash_data_addr(ha, faddr), burst);
  1949. if (rval) {
  1950. qla_printk(KERN_WARNING, ha,
  1951. "Unable to burst-read optrom segment "
  1952. "(%x/%x/%llx).\n", rval,
  1953. flash_data_addr(ha, faddr),
  1954. (unsigned long long)optrom_dma);
  1955. qla_printk(KERN_WARNING, ha,
  1956. "Reverting to slow-read.\n");
  1957. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  1958. optrom, optrom_dma);
  1959. goto slow_read;
  1960. }
  1961. memcpy(pbuf, optrom, burst * 4);
  1962. left -= burst;
  1963. faddr += burst;
  1964. pbuf += burst * 4;
  1965. }
  1966. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE, optrom,
  1967. optrom_dma);
  1968. return buf;
  1969. slow_read:
  1970. return qla24xx_read_optrom_data(vha, buf, offset, length);
  1971. }
  1972. /**
  1973. * qla2x00_get_fcode_version() - Determine an FCODE image's version.
  1974. * @ha: HA context
  1975. * @pcids: Pointer to the FCODE PCI data structure
  1976. *
  1977. * The process of retrieving the FCODE version information is at best
  1978. * described as interesting.
  1979. *
  1980. * Within the first 100h bytes of the image an ASCII string is present
  1981. * which contains several pieces of information including the FCODE
  1982. * version. Unfortunately it seems the only reliable way to retrieve
  1983. * the version is by scanning for another sentinel within the string,
  1984. * the FCODE build date:
  1985. *
  1986. * ... 2.00.02 10/17/02 ...
  1987. *
  1988. * Returns QLA_SUCCESS on successful retrieval of version.
  1989. */
  1990. static void
  1991. qla2x00_get_fcode_version(struct qla_hw_data *ha, uint32_t pcids)
  1992. {
  1993. int ret = QLA_FUNCTION_FAILED;
  1994. uint32_t istart, iend, iter, vend;
  1995. uint8_t do_next, rbyte, *vbyte;
  1996. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1997. /* Skip the PCI data structure. */
  1998. istart = pcids +
  1999. ((qla2x00_read_flash_byte(ha, pcids + 0x0B) << 8) |
  2000. qla2x00_read_flash_byte(ha, pcids + 0x0A));
  2001. iend = istart + 0x100;
  2002. do {
  2003. /* Scan for the sentinel date string...eeewww. */
  2004. do_next = 0;
  2005. iter = istart;
  2006. while ((iter < iend) && !do_next) {
  2007. iter++;
  2008. if (qla2x00_read_flash_byte(ha, iter) == '/') {
  2009. if (qla2x00_read_flash_byte(ha, iter + 2) ==
  2010. '/')
  2011. do_next++;
  2012. else if (qla2x00_read_flash_byte(ha,
  2013. iter + 3) == '/')
  2014. do_next++;
  2015. }
  2016. }
  2017. if (!do_next)
  2018. break;
  2019. /* Backtrack to previous ' ' (space). */
  2020. do_next = 0;
  2021. while ((iter > istart) && !do_next) {
  2022. iter--;
  2023. if (qla2x00_read_flash_byte(ha, iter) == ' ')
  2024. do_next++;
  2025. }
  2026. if (!do_next)
  2027. break;
  2028. /*
  2029. * Mark end of version tag, and find previous ' ' (space) or
  2030. * string length (recent FCODE images -- major hack ahead!!!).
  2031. */
  2032. vend = iter - 1;
  2033. do_next = 0;
  2034. while ((iter > istart) && !do_next) {
  2035. iter--;
  2036. rbyte = qla2x00_read_flash_byte(ha, iter);
  2037. if (rbyte == ' ' || rbyte == 0xd || rbyte == 0x10)
  2038. do_next++;
  2039. }
  2040. if (!do_next)
  2041. break;
  2042. /* Mark beginning of version tag, and copy data. */
  2043. iter++;
  2044. if ((vend - iter) &&
  2045. ((vend - iter) < sizeof(ha->fcode_revision))) {
  2046. vbyte = ha->fcode_revision;
  2047. while (iter <= vend) {
  2048. *vbyte++ = qla2x00_read_flash_byte(ha, iter);
  2049. iter++;
  2050. }
  2051. ret = QLA_SUCCESS;
  2052. }
  2053. } while (0);
  2054. if (ret != QLA_SUCCESS)
  2055. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  2056. }
  2057. int
  2058. qla2x00_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
  2059. {
  2060. int ret = QLA_SUCCESS;
  2061. uint8_t code_type, last_image;
  2062. uint32_t pcihdr, pcids;
  2063. uint8_t *dbyte;
  2064. uint16_t *dcode;
  2065. struct qla_hw_data *ha = vha->hw;
  2066. if (!ha->pio_address || !mbuf)
  2067. return QLA_FUNCTION_FAILED;
  2068. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  2069. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  2070. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  2071. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2072. qla2x00_flash_enable(ha);
  2073. /* Begin with first PCI expansion ROM header. */
  2074. pcihdr = 0;
  2075. last_image = 1;
  2076. do {
  2077. /* Verify PCI expansion ROM header. */
  2078. if (qla2x00_read_flash_byte(ha, pcihdr) != 0x55 ||
  2079. qla2x00_read_flash_byte(ha, pcihdr + 0x01) != 0xaa) {
  2080. /* No signature */
  2081. DEBUG2(qla_printk(KERN_DEBUG, ha, "No matching ROM "
  2082. "signature.\n"));
  2083. ret = QLA_FUNCTION_FAILED;
  2084. break;
  2085. }
  2086. /* Locate PCI data structure. */
  2087. pcids = pcihdr +
  2088. ((qla2x00_read_flash_byte(ha, pcihdr + 0x19) << 8) |
  2089. qla2x00_read_flash_byte(ha, pcihdr + 0x18));
  2090. /* Validate signature of PCI data structure. */
  2091. if (qla2x00_read_flash_byte(ha, pcids) != 'P' ||
  2092. qla2x00_read_flash_byte(ha, pcids + 0x1) != 'C' ||
  2093. qla2x00_read_flash_byte(ha, pcids + 0x2) != 'I' ||
  2094. qla2x00_read_flash_byte(ha, pcids + 0x3) != 'R') {
  2095. /* Incorrect header. */
  2096. DEBUG2(qla_printk(KERN_INFO, ha, "PCI data struct not "
  2097. "found pcir_adr=%x.\n", pcids));
  2098. ret = QLA_FUNCTION_FAILED;
  2099. break;
  2100. }
  2101. /* Read version */
  2102. code_type = qla2x00_read_flash_byte(ha, pcids + 0x14);
  2103. switch (code_type) {
  2104. case ROM_CODE_TYPE_BIOS:
  2105. /* Intel x86, PC-AT compatible. */
  2106. ha->bios_revision[0] =
  2107. qla2x00_read_flash_byte(ha, pcids + 0x12);
  2108. ha->bios_revision[1] =
  2109. qla2x00_read_flash_byte(ha, pcids + 0x13);
  2110. DEBUG3(qla_printk(KERN_DEBUG, ha, "read BIOS %d.%d.\n",
  2111. ha->bios_revision[1], ha->bios_revision[0]));
  2112. break;
  2113. case ROM_CODE_TYPE_FCODE:
  2114. /* Open Firmware standard for PCI (FCode). */
  2115. /* Eeeewww... */
  2116. qla2x00_get_fcode_version(ha, pcids);
  2117. break;
  2118. case ROM_CODE_TYPE_EFI:
  2119. /* Extensible Firmware Interface (EFI). */
  2120. ha->efi_revision[0] =
  2121. qla2x00_read_flash_byte(ha, pcids + 0x12);
  2122. ha->efi_revision[1] =
  2123. qla2x00_read_flash_byte(ha, pcids + 0x13);
  2124. DEBUG3(qla_printk(KERN_DEBUG, ha, "read EFI %d.%d.\n",
  2125. ha->efi_revision[1], ha->efi_revision[0]));
  2126. break;
  2127. default:
  2128. DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized code "
  2129. "type %x at pcids %x.\n", code_type, pcids));
  2130. break;
  2131. }
  2132. last_image = qla2x00_read_flash_byte(ha, pcids + 0x15) & BIT_7;
  2133. /* Locate next PCI expansion ROM. */
  2134. pcihdr += ((qla2x00_read_flash_byte(ha, pcids + 0x11) << 8) |
  2135. qla2x00_read_flash_byte(ha, pcids + 0x10)) * 512;
  2136. } while (!last_image);
  2137. if (IS_QLA2322(ha)) {
  2138. /* Read firmware image information. */
  2139. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2140. dbyte = mbuf;
  2141. memset(dbyte, 0, 8);
  2142. dcode = (uint16_t *)dbyte;
  2143. qla2x00_read_flash_data(ha, dbyte, ha->flt_region_fw * 4 + 10,
  2144. 8);
  2145. DEBUG3(qla_printk(KERN_DEBUG, ha, "dumping fw ver from "
  2146. "flash:\n"));
  2147. DEBUG3(qla2x00_dump_buffer((uint8_t *)dbyte, 8));
  2148. if ((dcode[0] == 0xffff && dcode[1] == 0xffff &&
  2149. dcode[2] == 0xffff && dcode[3] == 0xffff) ||
  2150. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  2151. dcode[3] == 0)) {
  2152. DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized fw "
  2153. "revision at %x.\n", ha->flt_region_fw * 4));
  2154. } else {
  2155. /* values are in big endian */
  2156. ha->fw_revision[0] = dbyte[0] << 16 | dbyte[1];
  2157. ha->fw_revision[1] = dbyte[2] << 16 | dbyte[3];
  2158. ha->fw_revision[2] = dbyte[4] << 16 | dbyte[5];
  2159. }
  2160. }
  2161. qla2x00_flash_disable(ha);
  2162. return ret;
  2163. }
  2164. int
  2165. qla24xx_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
  2166. {
  2167. int ret = QLA_SUCCESS;
  2168. uint32_t pcihdr, pcids;
  2169. uint32_t *dcode;
  2170. uint8_t *bcode;
  2171. uint8_t code_type, last_image;
  2172. int i;
  2173. struct qla_hw_data *ha = vha->hw;
  2174. if (!mbuf)
  2175. return QLA_FUNCTION_FAILED;
  2176. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  2177. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  2178. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  2179. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2180. dcode = mbuf;
  2181. /* Begin with first PCI expansion ROM header. */
  2182. pcihdr = ha->flt_region_boot;
  2183. last_image = 1;
  2184. do {
  2185. /* Verify PCI expansion ROM header. */
  2186. qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, 0x20);
  2187. bcode = mbuf + (pcihdr % 4);
  2188. if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa) {
  2189. /* No signature */
  2190. DEBUG2(qla_printk(KERN_DEBUG, ha, "No matching ROM "
  2191. "signature.\n"));
  2192. ret = QLA_FUNCTION_FAILED;
  2193. break;
  2194. }
  2195. /* Locate PCI data structure. */
  2196. pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
  2197. qla24xx_read_flash_data(vha, dcode, pcids >> 2, 0x20);
  2198. bcode = mbuf + (pcihdr % 4);
  2199. /* Validate signature of PCI data structure. */
  2200. if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
  2201. bcode[0x2] != 'I' || bcode[0x3] != 'R') {
  2202. /* Incorrect header. */
  2203. DEBUG2(qla_printk(KERN_INFO, ha, "PCI data struct not "
  2204. "found pcir_adr=%x.\n", pcids));
  2205. ret = QLA_FUNCTION_FAILED;
  2206. break;
  2207. }
  2208. /* Read version */
  2209. code_type = bcode[0x14];
  2210. switch (code_type) {
  2211. case ROM_CODE_TYPE_BIOS:
  2212. /* Intel x86, PC-AT compatible. */
  2213. ha->bios_revision[0] = bcode[0x12];
  2214. ha->bios_revision[1] = bcode[0x13];
  2215. DEBUG3(qla_printk(KERN_DEBUG, ha, "read BIOS %d.%d.\n",
  2216. ha->bios_revision[1], ha->bios_revision[0]));
  2217. break;
  2218. case ROM_CODE_TYPE_FCODE:
  2219. /* Open Firmware standard for PCI (FCode). */
  2220. ha->fcode_revision[0] = bcode[0x12];
  2221. ha->fcode_revision[1] = bcode[0x13];
  2222. DEBUG3(qla_printk(KERN_DEBUG, ha, "read FCODE %d.%d.\n",
  2223. ha->fcode_revision[1], ha->fcode_revision[0]));
  2224. break;
  2225. case ROM_CODE_TYPE_EFI:
  2226. /* Extensible Firmware Interface (EFI). */
  2227. ha->efi_revision[0] = bcode[0x12];
  2228. ha->efi_revision[1] = bcode[0x13];
  2229. DEBUG3(qla_printk(KERN_DEBUG, ha, "read EFI %d.%d.\n",
  2230. ha->efi_revision[1], ha->efi_revision[0]));
  2231. break;
  2232. default:
  2233. DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized code "
  2234. "type %x at pcids %x.\n", code_type, pcids));
  2235. break;
  2236. }
  2237. last_image = bcode[0x15] & BIT_7;
  2238. /* Locate next PCI expansion ROM. */
  2239. pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
  2240. } while (!last_image);
  2241. /* Read firmware image information. */
  2242. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2243. dcode = mbuf;
  2244. qla24xx_read_flash_data(vha, dcode, ha->flt_region_fw + 4, 4);
  2245. for (i = 0; i < 4; i++)
  2246. dcode[i] = be32_to_cpu(dcode[i]);
  2247. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  2248. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  2249. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  2250. dcode[3] == 0)) {
  2251. DEBUG2(qla_printk(KERN_INFO, ha, "Unrecognized fw "
  2252. "revision at %x.\n", ha->flt_region_fw * 4));
  2253. } else {
  2254. ha->fw_revision[0] = dcode[0];
  2255. ha->fw_revision[1] = dcode[1];
  2256. ha->fw_revision[2] = dcode[2];
  2257. ha->fw_revision[3] = dcode[3];
  2258. }
  2259. return ret;
  2260. }
  2261. static int
  2262. qla2xxx_is_vpd_valid(uint8_t *pos, uint8_t *end)
  2263. {
  2264. if (pos >= end || *pos != 0x82)
  2265. return 0;
  2266. pos += 3 + pos[1];
  2267. if (pos >= end || *pos != 0x90)
  2268. return 0;
  2269. pos += 3 + pos[1];
  2270. if (pos >= end || *pos != 0x78)
  2271. return 0;
  2272. return 1;
  2273. }
  2274. int
  2275. qla2xxx_get_vpd_field(scsi_qla_host_t *vha, char *key, char *str, size_t size)
  2276. {
  2277. struct qla_hw_data *ha = vha->hw;
  2278. uint8_t *pos = ha->vpd;
  2279. uint8_t *end = pos + ha->vpd_size;
  2280. int len = 0;
  2281. if (!IS_FWI2_CAPABLE(ha) || !qla2xxx_is_vpd_valid(pos, end))
  2282. return 0;
  2283. while (pos < end && *pos != 0x78) {
  2284. len = (*pos == 0x82) ? pos[1] : pos[2];
  2285. if (!strncmp(pos, key, strlen(key)))
  2286. break;
  2287. if (*pos != 0x90 && *pos != 0x91)
  2288. pos += len;
  2289. pos += 3;
  2290. }
  2291. if (pos < end - len && *pos != 0x78)
  2292. return snprintf(str, size, "%.*s", len, pos + 3);
  2293. return 0;
  2294. }