qla_sup.c 69 KB

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