qla_sup.c 56 KB

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