qla_sup.c 57 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229
  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. static 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. static 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. static 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. static 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, cnt;
  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 and wait for completion. */
  596. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0x9c);
  597. for (cnt = 300; cnt &&
  598. qla24xx_read_flash_dword(ha,
  599. flash_conf_to_access_addr(0x005)) & BIT_0;
  600. cnt--) {
  601. udelay(10);
  602. }
  603. /* Disable flash write. */
  604. WRT_REG_DWORD(&reg->ctrl_status,
  605. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  606. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  607. if (optrom)
  608. dma_free_coherent(&ha->pdev->dev,
  609. OPTROM_BURST_SIZE, optrom, optrom_dma);
  610. return ret;
  611. }
  612. uint8_t *
  613. qla2x00_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  614. uint32_t bytes)
  615. {
  616. uint32_t i;
  617. uint16_t *wptr;
  618. /* Word reads to NVRAM via registers. */
  619. wptr = (uint16_t *)buf;
  620. qla2x00_lock_nvram_access(ha);
  621. for (i = 0; i < bytes >> 1; i++, naddr++)
  622. wptr[i] = cpu_to_le16(qla2x00_get_nvram_word(ha,
  623. naddr));
  624. qla2x00_unlock_nvram_access(ha);
  625. return buf;
  626. }
  627. uint8_t *
  628. qla24xx_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  629. uint32_t bytes)
  630. {
  631. uint32_t i;
  632. uint32_t *dwptr;
  633. /* Dword reads to flash. */
  634. dwptr = (uint32_t *)buf;
  635. for (i = 0; i < bytes >> 2; i++, naddr++)
  636. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  637. nvram_data_to_access_addr(naddr)));
  638. return buf;
  639. }
  640. int
  641. qla2x00_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  642. uint32_t bytes)
  643. {
  644. int ret, stat;
  645. uint32_t i;
  646. uint16_t *wptr;
  647. unsigned long flags;
  648. ret = QLA_SUCCESS;
  649. spin_lock_irqsave(&ha->hardware_lock, flags);
  650. qla2x00_lock_nvram_access(ha);
  651. /* Disable NVRAM write-protection. */
  652. stat = qla2x00_clear_nvram_protection(ha);
  653. wptr = (uint16_t *)buf;
  654. for (i = 0; i < bytes >> 1; i++, naddr++) {
  655. qla2x00_write_nvram_word(ha, naddr,
  656. cpu_to_le16(*wptr));
  657. wptr++;
  658. }
  659. /* Enable NVRAM write-protection. */
  660. qla2x00_set_nvram_protection(ha, stat);
  661. qla2x00_unlock_nvram_access(ha);
  662. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  663. return ret;
  664. }
  665. int
  666. qla24xx_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  667. uint32_t bytes)
  668. {
  669. int ret;
  670. uint32_t i;
  671. uint32_t *dwptr;
  672. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  673. unsigned long flags;
  674. ret = QLA_SUCCESS;
  675. spin_lock_irqsave(&ha->hardware_lock, flags);
  676. /* Enable flash write. */
  677. WRT_REG_DWORD(&reg->ctrl_status,
  678. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  679. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  680. /* Disable NVRAM write-protection. */
  681. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  682. 0);
  683. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  684. 0);
  685. /* Dword writes to flash. */
  686. dwptr = (uint32_t *)buf;
  687. for (i = 0; i < bytes >> 2; i++, naddr++, dwptr++) {
  688. ret = qla24xx_write_flash_dword(ha,
  689. nvram_data_to_access_addr(naddr),
  690. cpu_to_le32(*dwptr));
  691. if (ret != QLA_SUCCESS) {
  692. DEBUG9(printk("%s(%ld) Unable to program "
  693. "nvram address=%x data=%x.\n", __func__,
  694. ha->host_no, naddr, *dwptr));
  695. break;
  696. }
  697. }
  698. /* Enable NVRAM write-protection. */
  699. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  700. 0x8c);
  701. /* Disable flash write. */
  702. WRT_REG_DWORD(&reg->ctrl_status,
  703. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  704. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  705. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  706. return ret;
  707. }
  708. uint8_t *
  709. qla25xx_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  710. uint32_t bytes)
  711. {
  712. uint32_t i;
  713. uint32_t *dwptr;
  714. /* Dword reads to flash. */
  715. dwptr = (uint32_t *)buf;
  716. for (i = 0; i < bytes >> 2; i++, naddr++)
  717. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  718. flash_data_to_access_addr(FA_VPD_NVRAM_ADDR | naddr)));
  719. return buf;
  720. }
  721. int
  722. qla25xx_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  723. uint32_t bytes)
  724. {
  725. #define RMW_BUFFER_SIZE (64 * 1024)
  726. uint8_t *dbuf;
  727. dbuf = vmalloc(RMW_BUFFER_SIZE);
  728. if (!dbuf)
  729. return QLA_MEMORY_ALLOC_FAILED;
  730. ha->isp_ops->read_optrom(ha, dbuf, FA_VPD_NVRAM_ADDR << 2,
  731. RMW_BUFFER_SIZE);
  732. memcpy(dbuf + (naddr << 2), buf, bytes);
  733. ha->isp_ops->write_optrom(ha, dbuf, FA_VPD_NVRAM_ADDR << 2,
  734. RMW_BUFFER_SIZE);
  735. vfree(dbuf);
  736. return QLA_SUCCESS;
  737. }
  738. static inline void
  739. qla2x00_flip_colors(scsi_qla_host_t *ha, uint16_t *pflags)
  740. {
  741. if (IS_QLA2322(ha)) {
  742. /* Flip all colors. */
  743. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  744. /* Turn off. */
  745. ha->beacon_color_state = 0;
  746. *pflags = GPIO_LED_ALL_OFF;
  747. } else {
  748. /* Turn on. */
  749. ha->beacon_color_state = QLA_LED_ALL_ON;
  750. *pflags = GPIO_LED_RGA_ON;
  751. }
  752. } else {
  753. /* Flip green led only. */
  754. if (ha->beacon_color_state == QLA_LED_GRN_ON) {
  755. /* Turn off. */
  756. ha->beacon_color_state = 0;
  757. *pflags = GPIO_LED_GREEN_OFF_AMBER_OFF;
  758. } else {
  759. /* Turn on. */
  760. ha->beacon_color_state = QLA_LED_GRN_ON;
  761. *pflags = GPIO_LED_GREEN_ON_AMBER_OFF;
  762. }
  763. }
  764. }
  765. void
  766. qla2x00_beacon_blink(struct scsi_qla_host *ha)
  767. {
  768. uint16_t gpio_enable;
  769. uint16_t gpio_data;
  770. uint16_t led_color = 0;
  771. unsigned long flags;
  772. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  773. if (ha->pio_address)
  774. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  775. spin_lock_irqsave(&ha->hardware_lock, flags);
  776. /* Save the Original GPIOE. */
  777. if (ha->pio_address) {
  778. gpio_enable = RD_REG_WORD_PIO(&reg->gpioe);
  779. gpio_data = RD_REG_WORD_PIO(&reg->gpiod);
  780. } else {
  781. gpio_enable = RD_REG_WORD(&reg->gpioe);
  782. gpio_data = RD_REG_WORD(&reg->gpiod);
  783. }
  784. /* Set the modified gpio_enable values */
  785. gpio_enable |= GPIO_LED_MASK;
  786. if (ha->pio_address) {
  787. WRT_REG_WORD_PIO(&reg->gpioe, gpio_enable);
  788. } else {
  789. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  790. RD_REG_WORD(&reg->gpioe);
  791. }
  792. qla2x00_flip_colors(ha, &led_color);
  793. /* Clear out any previously set LED color. */
  794. gpio_data &= ~GPIO_LED_MASK;
  795. /* Set the new input LED color to GPIOD. */
  796. gpio_data |= led_color;
  797. /* Set the modified gpio_data values */
  798. if (ha->pio_address) {
  799. WRT_REG_WORD_PIO(&reg->gpiod, gpio_data);
  800. } else {
  801. WRT_REG_WORD(&reg->gpiod, gpio_data);
  802. RD_REG_WORD(&reg->gpiod);
  803. }
  804. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  805. }
  806. int
  807. qla2x00_beacon_on(struct scsi_qla_host *ha)
  808. {
  809. uint16_t gpio_enable;
  810. uint16_t gpio_data;
  811. unsigned long flags;
  812. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  813. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  814. ha->fw_options[1] |= FO1_DISABLE_GPIO6_7;
  815. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  816. qla_printk(KERN_WARNING, ha,
  817. "Unable to update fw options (beacon on).\n");
  818. return QLA_FUNCTION_FAILED;
  819. }
  820. if (ha->pio_address)
  821. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  822. /* Turn off LEDs. */
  823. spin_lock_irqsave(&ha->hardware_lock, flags);
  824. if (ha->pio_address) {
  825. gpio_enable = RD_REG_WORD_PIO(&reg->gpioe);
  826. gpio_data = RD_REG_WORD_PIO(&reg->gpiod);
  827. } else {
  828. gpio_enable = RD_REG_WORD(&reg->gpioe);
  829. gpio_data = RD_REG_WORD(&reg->gpiod);
  830. }
  831. gpio_enable |= GPIO_LED_MASK;
  832. /* Set the modified gpio_enable values. */
  833. if (ha->pio_address) {
  834. WRT_REG_WORD_PIO(&reg->gpioe, gpio_enable);
  835. } else {
  836. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  837. RD_REG_WORD(&reg->gpioe);
  838. }
  839. /* Clear out previously set LED colour. */
  840. gpio_data &= ~GPIO_LED_MASK;
  841. if (ha->pio_address) {
  842. WRT_REG_WORD_PIO(&reg->gpiod, gpio_data);
  843. } else {
  844. WRT_REG_WORD(&reg->gpiod, gpio_data);
  845. RD_REG_WORD(&reg->gpiod);
  846. }
  847. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  848. /*
  849. * Let the per HBA timer kick off the blinking process based on
  850. * the following flags. No need to do anything else now.
  851. */
  852. ha->beacon_blink_led = 1;
  853. ha->beacon_color_state = 0;
  854. return QLA_SUCCESS;
  855. }
  856. int
  857. qla2x00_beacon_off(struct scsi_qla_host *ha)
  858. {
  859. int rval = QLA_SUCCESS;
  860. ha->beacon_blink_led = 0;
  861. /* Set the on flag so when it gets flipped it will be off. */
  862. if (IS_QLA2322(ha))
  863. ha->beacon_color_state = QLA_LED_ALL_ON;
  864. else
  865. ha->beacon_color_state = QLA_LED_GRN_ON;
  866. ha->isp_ops->beacon_blink(ha); /* This turns green LED off */
  867. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  868. ha->fw_options[1] &= ~FO1_DISABLE_GPIO6_7;
  869. rval = qla2x00_set_fw_options(ha, ha->fw_options);
  870. if (rval != QLA_SUCCESS)
  871. qla_printk(KERN_WARNING, ha,
  872. "Unable to update fw options (beacon off).\n");
  873. return rval;
  874. }
  875. static inline void
  876. qla24xx_flip_colors(scsi_qla_host_t *ha, uint16_t *pflags)
  877. {
  878. /* Flip all colors. */
  879. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  880. /* Turn off. */
  881. ha->beacon_color_state = 0;
  882. *pflags = 0;
  883. } else {
  884. /* Turn on. */
  885. ha->beacon_color_state = QLA_LED_ALL_ON;
  886. *pflags = GPDX_LED_YELLOW_ON | GPDX_LED_AMBER_ON;
  887. }
  888. }
  889. void
  890. qla24xx_beacon_blink(struct scsi_qla_host *ha)
  891. {
  892. uint16_t led_color = 0;
  893. uint32_t gpio_data;
  894. unsigned long flags;
  895. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  896. /* Save the Original GPIOD. */
  897. spin_lock_irqsave(&ha->hardware_lock, flags);
  898. gpio_data = RD_REG_DWORD(&reg->gpiod);
  899. /* Enable the gpio_data reg for update. */
  900. gpio_data |= GPDX_LED_UPDATE_MASK;
  901. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  902. gpio_data = RD_REG_DWORD(&reg->gpiod);
  903. /* Set the color bits. */
  904. qla24xx_flip_colors(ha, &led_color);
  905. /* Clear out any previously set LED color. */
  906. gpio_data &= ~GPDX_LED_COLOR_MASK;
  907. /* Set the new input LED color to GPIOD. */
  908. gpio_data |= led_color;
  909. /* Set the modified gpio_data values. */
  910. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  911. gpio_data = RD_REG_DWORD(&reg->gpiod);
  912. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  913. }
  914. int
  915. qla24xx_beacon_on(struct scsi_qla_host *ha)
  916. {
  917. uint32_t gpio_data;
  918. unsigned long flags;
  919. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  920. if (ha->beacon_blink_led == 0) {
  921. /* Enable firmware for update */
  922. ha->fw_options[1] |= ADD_FO1_DISABLE_GPIO_LED_CTRL;
  923. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS)
  924. return QLA_FUNCTION_FAILED;
  925. if (qla2x00_get_fw_options(ha, ha->fw_options) !=
  926. QLA_SUCCESS) {
  927. qla_printk(KERN_WARNING, ha,
  928. "Unable to update fw options (beacon on).\n");
  929. return QLA_FUNCTION_FAILED;
  930. }
  931. spin_lock_irqsave(&ha->hardware_lock, flags);
  932. gpio_data = RD_REG_DWORD(&reg->gpiod);
  933. /* Enable the gpio_data reg for update. */
  934. gpio_data |= GPDX_LED_UPDATE_MASK;
  935. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  936. RD_REG_DWORD(&reg->gpiod);
  937. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  938. }
  939. /* So all colors blink together. */
  940. ha->beacon_color_state = 0;
  941. /* Let the per HBA timer kick off the blinking process. */
  942. ha->beacon_blink_led = 1;
  943. return QLA_SUCCESS;
  944. }
  945. int
  946. qla24xx_beacon_off(struct scsi_qla_host *ha)
  947. {
  948. uint32_t gpio_data;
  949. unsigned long flags;
  950. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  951. ha->beacon_blink_led = 0;
  952. ha->beacon_color_state = QLA_LED_ALL_ON;
  953. ha->isp_ops->beacon_blink(ha); /* Will flip to all off. */
  954. /* Give control back to firmware. */
  955. spin_lock_irqsave(&ha->hardware_lock, flags);
  956. gpio_data = RD_REG_DWORD(&reg->gpiod);
  957. /* Disable the gpio_data reg for update. */
  958. gpio_data &= ~GPDX_LED_UPDATE_MASK;
  959. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  960. RD_REG_DWORD(&reg->gpiod);
  961. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  962. ha->fw_options[1] &= ~ADD_FO1_DISABLE_GPIO_LED_CTRL;
  963. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  964. qla_printk(KERN_WARNING, ha,
  965. "Unable to update fw options (beacon off).\n");
  966. return QLA_FUNCTION_FAILED;
  967. }
  968. if (qla2x00_get_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  969. qla_printk(KERN_WARNING, ha,
  970. "Unable to get fw options (beacon off).\n");
  971. return QLA_FUNCTION_FAILED;
  972. }
  973. return QLA_SUCCESS;
  974. }
  975. /*
  976. * Flash support routines
  977. */
  978. /**
  979. * qla2x00_flash_enable() - Setup flash for reading and writing.
  980. * @ha: HA context
  981. */
  982. static void
  983. qla2x00_flash_enable(scsi_qla_host_t *ha)
  984. {
  985. uint16_t data;
  986. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  987. data = RD_REG_WORD(&reg->ctrl_status);
  988. data |= CSR_FLASH_ENABLE;
  989. WRT_REG_WORD(&reg->ctrl_status, data);
  990. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  991. }
  992. /**
  993. * qla2x00_flash_disable() - Disable flash and allow RISC to run.
  994. * @ha: HA context
  995. */
  996. static void
  997. qla2x00_flash_disable(scsi_qla_host_t *ha)
  998. {
  999. uint16_t data;
  1000. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1001. data = RD_REG_WORD(&reg->ctrl_status);
  1002. data &= ~(CSR_FLASH_ENABLE);
  1003. WRT_REG_WORD(&reg->ctrl_status, data);
  1004. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1005. }
  1006. /**
  1007. * qla2x00_read_flash_byte() - Reads a byte from flash
  1008. * @ha: HA context
  1009. * @addr: Address in flash to read
  1010. *
  1011. * A word is read from the chip, but, only the lower byte is valid.
  1012. *
  1013. * Returns the byte read from flash @addr.
  1014. */
  1015. static uint8_t
  1016. qla2x00_read_flash_byte(scsi_qla_host_t *ha, uint32_t addr)
  1017. {
  1018. uint16_t data;
  1019. uint16_t bank_select;
  1020. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1021. bank_select = RD_REG_WORD(&reg->ctrl_status);
  1022. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1023. /* Specify 64K address range: */
  1024. /* clear out Module Select and Flash Address bits [19:16]. */
  1025. bank_select &= ~0xf8;
  1026. bank_select |= addr >> 12 & 0xf0;
  1027. bank_select |= CSR_FLASH_64K_BANK;
  1028. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1029. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1030. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1031. data = RD_REG_WORD(&reg->flash_data);
  1032. return (uint8_t)data;
  1033. }
  1034. /* Setup bit 16 of flash address. */
  1035. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1036. bank_select |= CSR_FLASH_64K_BANK;
  1037. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1038. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1039. } else if (((addr & BIT_16) == 0) &&
  1040. (bank_select & CSR_FLASH_64K_BANK)) {
  1041. bank_select &= ~(CSR_FLASH_64K_BANK);
  1042. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1043. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1044. }
  1045. /* Always perform IO mapped accesses to the FLASH registers. */
  1046. if (ha->pio_address) {
  1047. uint16_t data2;
  1048. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  1049. WRT_REG_WORD_PIO(&reg->flash_address, (uint16_t)addr);
  1050. do {
  1051. data = RD_REG_WORD_PIO(&reg->flash_data);
  1052. barrier();
  1053. cpu_relax();
  1054. data2 = RD_REG_WORD_PIO(&reg->flash_data);
  1055. } while (data != data2);
  1056. } else {
  1057. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1058. data = qla2x00_debounce_register(&reg->flash_data);
  1059. }
  1060. return (uint8_t)data;
  1061. }
  1062. /**
  1063. * qla2x00_write_flash_byte() - Write a byte to flash
  1064. * @ha: HA context
  1065. * @addr: Address in flash to write
  1066. * @data: Data to write
  1067. */
  1068. static void
  1069. qla2x00_write_flash_byte(scsi_qla_host_t *ha, uint32_t addr, uint8_t data)
  1070. {
  1071. uint16_t bank_select;
  1072. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1073. bank_select = RD_REG_WORD(&reg->ctrl_status);
  1074. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1075. /* Specify 64K address range: */
  1076. /* clear out Module Select and Flash Address bits [19:16]. */
  1077. bank_select &= ~0xf8;
  1078. bank_select |= addr >> 12 & 0xf0;
  1079. bank_select |= CSR_FLASH_64K_BANK;
  1080. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1081. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1082. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1083. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1084. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  1085. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1086. return;
  1087. }
  1088. /* Setup bit 16 of flash address. */
  1089. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1090. bank_select |= CSR_FLASH_64K_BANK;
  1091. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1092. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1093. } else if (((addr & BIT_16) == 0) &&
  1094. (bank_select & CSR_FLASH_64K_BANK)) {
  1095. bank_select &= ~(CSR_FLASH_64K_BANK);
  1096. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1097. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1098. }
  1099. /* Always perform IO mapped accesses to the FLASH registers. */
  1100. if (ha->pio_address) {
  1101. reg = (struct device_reg_2xxx __iomem *)ha->pio_address;
  1102. WRT_REG_WORD_PIO(&reg->flash_address, (uint16_t)addr);
  1103. WRT_REG_WORD_PIO(&reg->flash_data, (uint16_t)data);
  1104. } else {
  1105. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1106. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1107. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  1108. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1109. }
  1110. }
  1111. /**
  1112. * qla2x00_poll_flash() - Polls flash for completion.
  1113. * @ha: HA context
  1114. * @addr: Address in flash to poll
  1115. * @poll_data: Data to be polled
  1116. * @man_id: Flash manufacturer ID
  1117. * @flash_id: Flash ID
  1118. *
  1119. * This function polls the device until bit 7 of what is read matches data
  1120. * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
  1121. * out (a fatal error). The flash book recommeds reading bit 7 again after
  1122. * reading bit 5 as a 1.
  1123. *
  1124. * Returns 0 on success, else non-zero.
  1125. */
  1126. static int
  1127. qla2x00_poll_flash(scsi_qla_host_t *ha, uint32_t addr, uint8_t poll_data,
  1128. uint8_t man_id, uint8_t flash_id)
  1129. {
  1130. int status;
  1131. uint8_t flash_data;
  1132. uint32_t cnt;
  1133. status = 1;
  1134. /* Wait for 30 seconds for command to finish. */
  1135. poll_data &= BIT_7;
  1136. for (cnt = 3000000; cnt; cnt--) {
  1137. flash_data = qla2x00_read_flash_byte(ha, addr);
  1138. if ((flash_data & BIT_7) == poll_data) {
  1139. status = 0;
  1140. break;
  1141. }
  1142. if (man_id != 0x40 && man_id != 0xda) {
  1143. if ((flash_data & BIT_5) && cnt > 2)
  1144. cnt = 2;
  1145. }
  1146. udelay(10);
  1147. barrier();
  1148. cond_resched();
  1149. }
  1150. return status;
  1151. }
  1152. /**
  1153. * qla2x00_program_flash_address() - Programs a flash address
  1154. * @ha: HA context
  1155. * @addr: Address in flash to program
  1156. * @data: Data to be written in flash
  1157. * @man_id: Flash manufacturer ID
  1158. * @flash_id: Flash ID
  1159. *
  1160. * Returns 0 on success, else non-zero.
  1161. */
  1162. static int
  1163. qla2x00_program_flash_address(scsi_qla_host_t *ha, uint32_t addr, uint8_t data,
  1164. uint8_t man_id, uint8_t flash_id)
  1165. {
  1166. /* Write Program Command Sequence. */
  1167. if (IS_OEM_001(ha)) {
  1168. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1169. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1170. qla2x00_write_flash_byte(ha, 0xaaa, 0xa0);
  1171. qla2x00_write_flash_byte(ha, addr, data);
  1172. } else {
  1173. if (man_id == 0xda && flash_id == 0xc1) {
  1174. qla2x00_write_flash_byte(ha, addr, data);
  1175. if (addr & 0x7e)
  1176. return 0;
  1177. } else {
  1178. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1179. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1180. qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
  1181. qla2x00_write_flash_byte(ha, addr, data);
  1182. }
  1183. }
  1184. udelay(150);
  1185. /* Wait for write to complete. */
  1186. return qla2x00_poll_flash(ha, addr, data, man_id, flash_id);
  1187. }
  1188. /**
  1189. * qla2x00_erase_flash() - Erase the flash.
  1190. * @ha: HA context
  1191. * @man_id: Flash manufacturer ID
  1192. * @flash_id: Flash ID
  1193. *
  1194. * Returns 0 on success, else non-zero.
  1195. */
  1196. static int
  1197. qla2x00_erase_flash(scsi_qla_host_t *ha, uint8_t man_id, uint8_t flash_id)
  1198. {
  1199. /* Individual Sector Erase Command Sequence */
  1200. if (IS_OEM_001(ha)) {
  1201. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1202. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1203. qla2x00_write_flash_byte(ha, 0xaaa, 0x80);
  1204. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1205. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1206. qla2x00_write_flash_byte(ha, 0xaaa, 0x10);
  1207. } else {
  1208. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1209. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1210. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1211. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1212. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1213. qla2x00_write_flash_byte(ha, 0x5555, 0x10);
  1214. }
  1215. udelay(150);
  1216. /* Wait for erase to complete. */
  1217. return qla2x00_poll_flash(ha, 0x00, 0x80, man_id, flash_id);
  1218. }
  1219. /**
  1220. * qla2x00_erase_flash_sector() - Erase a flash sector.
  1221. * @ha: HA context
  1222. * @addr: Flash sector to erase
  1223. * @sec_mask: Sector address mask
  1224. * @man_id: Flash manufacturer ID
  1225. * @flash_id: Flash ID
  1226. *
  1227. * Returns 0 on success, else non-zero.
  1228. */
  1229. static int
  1230. qla2x00_erase_flash_sector(scsi_qla_host_t *ha, uint32_t addr,
  1231. uint32_t sec_mask, uint8_t man_id, uint8_t flash_id)
  1232. {
  1233. /* Individual Sector Erase Command Sequence */
  1234. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1235. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1236. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1237. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1238. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1239. if (man_id == 0x1f && flash_id == 0x13)
  1240. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
  1241. else
  1242. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
  1243. udelay(150);
  1244. /* Wait for erase to complete. */
  1245. return qla2x00_poll_flash(ha, addr, 0x80, man_id, flash_id);
  1246. }
  1247. /**
  1248. * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
  1249. * @man_id: Flash manufacturer ID
  1250. * @flash_id: Flash ID
  1251. */
  1252. static void
  1253. qla2x00_get_flash_manufacturer(scsi_qla_host_t *ha, uint8_t *man_id,
  1254. uint8_t *flash_id)
  1255. {
  1256. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1257. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1258. qla2x00_write_flash_byte(ha, 0x5555, 0x90);
  1259. *man_id = qla2x00_read_flash_byte(ha, 0x0000);
  1260. *flash_id = qla2x00_read_flash_byte(ha, 0x0001);
  1261. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1262. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1263. qla2x00_write_flash_byte(ha, 0x5555, 0xf0);
  1264. }
  1265. static void
  1266. qla2x00_read_flash_data(scsi_qla_host_t *ha, uint8_t *tmp_buf, uint32_t saddr,
  1267. uint32_t length)
  1268. {
  1269. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1270. uint32_t midpoint, ilength;
  1271. uint8_t data;
  1272. midpoint = length / 2;
  1273. WRT_REG_WORD(&reg->nvram, 0);
  1274. RD_REG_WORD(&reg->nvram);
  1275. for (ilength = 0; ilength < length; saddr++, ilength++, tmp_buf++) {
  1276. if (ilength == midpoint) {
  1277. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1278. RD_REG_WORD(&reg->nvram);
  1279. }
  1280. data = qla2x00_read_flash_byte(ha, saddr);
  1281. if (saddr % 100)
  1282. udelay(10);
  1283. *tmp_buf = data;
  1284. cond_resched();
  1285. }
  1286. }
  1287. static inline void
  1288. qla2x00_suspend_hba(struct scsi_qla_host *ha)
  1289. {
  1290. int cnt;
  1291. unsigned long flags;
  1292. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1293. /* Suspend HBA. */
  1294. scsi_block_requests(ha->host);
  1295. ha->isp_ops->disable_intrs(ha);
  1296. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1297. /* Pause RISC. */
  1298. spin_lock_irqsave(&ha->hardware_lock, flags);
  1299. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  1300. RD_REG_WORD(&reg->hccr);
  1301. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  1302. for (cnt = 0; cnt < 30000; cnt++) {
  1303. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
  1304. break;
  1305. udelay(100);
  1306. }
  1307. } else {
  1308. udelay(10);
  1309. }
  1310. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1311. }
  1312. static inline void
  1313. qla2x00_resume_hba(struct scsi_qla_host *ha)
  1314. {
  1315. /* Resume HBA. */
  1316. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1317. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1318. qla2xxx_wake_dpc(ha);
  1319. qla2x00_wait_for_hba_online(ha);
  1320. scsi_unblock_requests(ha->host);
  1321. }
  1322. uint8_t *
  1323. qla2x00_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1324. uint32_t offset, uint32_t length)
  1325. {
  1326. uint32_t addr, midpoint;
  1327. uint8_t *data;
  1328. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1329. /* Suspend HBA. */
  1330. qla2x00_suspend_hba(ha);
  1331. /* Go with read. */
  1332. midpoint = ha->optrom_size / 2;
  1333. qla2x00_flash_enable(ha);
  1334. WRT_REG_WORD(&reg->nvram, 0);
  1335. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1336. for (addr = offset, data = buf; addr < length; addr++, data++) {
  1337. if (addr == midpoint) {
  1338. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1339. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1340. }
  1341. *data = qla2x00_read_flash_byte(ha, addr);
  1342. }
  1343. qla2x00_flash_disable(ha);
  1344. /* Resume HBA. */
  1345. qla2x00_resume_hba(ha);
  1346. return buf;
  1347. }
  1348. int
  1349. qla2x00_write_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1350. uint32_t offset, uint32_t length)
  1351. {
  1352. int rval;
  1353. uint8_t man_id, flash_id, sec_number, data;
  1354. uint16_t wd;
  1355. uint32_t addr, liter, sec_mask, rest_addr;
  1356. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1357. /* Suspend HBA. */
  1358. qla2x00_suspend_hba(ha);
  1359. rval = QLA_SUCCESS;
  1360. sec_number = 0;
  1361. /* Reset ISP chip. */
  1362. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  1363. pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
  1364. /* Go with write. */
  1365. qla2x00_flash_enable(ha);
  1366. do { /* Loop once to provide quick error exit */
  1367. /* Structure of flash memory based on manufacturer */
  1368. if (IS_OEM_001(ha)) {
  1369. /* OEM variant with special flash part. */
  1370. man_id = flash_id = 0;
  1371. rest_addr = 0xffff;
  1372. sec_mask = 0x10000;
  1373. goto update_flash;
  1374. }
  1375. qla2x00_get_flash_manufacturer(ha, &man_id, &flash_id);
  1376. switch (man_id) {
  1377. case 0x20: /* ST flash. */
  1378. if (flash_id == 0xd2 || flash_id == 0xe3) {
  1379. /*
  1380. * ST m29w008at part - 64kb sector size with
  1381. * 32kb,8kb,8kb,16kb sectors at memory address
  1382. * 0xf0000.
  1383. */
  1384. rest_addr = 0xffff;
  1385. sec_mask = 0x10000;
  1386. break;
  1387. }
  1388. /*
  1389. * ST m29w010b part - 16kb sector size
  1390. * Default to 16kb sectors
  1391. */
  1392. rest_addr = 0x3fff;
  1393. sec_mask = 0x1c000;
  1394. break;
  1395. case 0x40: /* Mostel flash. */
  1396. /* Mostel v29c51001 part - 512 byte sector size. */
  1397. rest_addr = 0x1ff;
  1398. sec_mask = 0x1fe00;
  1399. break;
  1400. case 0xbf: /* SST flash. */
  1401. /* SST39sf10 part - 4kb sector size. */
  1402. rest_addr = 0xfff;
  1403. sec_mask = 0x1f000;
  1404. break;
  1405. case 0xda: /* Winbond flash. */
  1406. /* Winbond W29EE011 part - 256 byte sector size. */
  1407. rest_addr = 0x7f;
  1408. sec_mask = 0x1ff80;
  1409. break;
  1410. case 0xc2: /* Macronix flash. */
  1411. /* 64k sector size. */
  1412. if (flash_id == 0x38 || flash_id == 0x4f) {
  1413. rest_addr = 0xffff;
  1414. sec_mask = 0x10000;
  1415. break;
  1416. }
  1417. /* Fall through... */
  1418. case 0x1f: /* Atmel flash. */
  1419. /* 512k sector size. */
  1420. if (flash_id == 0x13) {
  1421. rest_addr = 0x7fffffff;
  1422. sec_mask = 0x80000000;
  1423. break;
  1424. }
  1425. /* Fall through... */
  1426. case 0x01: /* AMD flash. */
  1427. if (flash_id == 0x38 || flash_id == 0x40 ||
  1428. flash_id == 0x4f) {
  1429. /* Am29LV081 part - 64kb sector size. */
  1430. /* Am29LV002BT part - 64kb sector size. */
  1431. rest_addr = 0xffff;
  1432. sec_mask = 0x10000;
  1433. break;
  1434. } else if (flash_id == 0x3e) {
  1435. /*
  1436. * Am29LV008b part - 64kb sector size with
  1437. * 32kb,8kb,8kb,16kb sector at memory address
  1438. * h0xf0000.
  1439. */
  1440. rest_addr = 0xffff;
  1441. sec_mask = 0x10000;
  1442. break;
  1443. } else if (flash_id == 0x20 || flash_id == 0x6e) {
  1444. /*
  1445. * Am29LV010 part or AM29f010 - 16kb sector
  1446. * size.
  1447. */
  1448. rest_addr = 0x3fff;
  1449. sec_mask = 0x1c000;
  1450. break;
  1451. } else if (flash_id == 0x6d) {
  1452. /* Am29LV001 part - 8kb sector size. */
  1453. rest_addr = 0x1fff;
  1454. sec_mask = 0x1e000;
  1455. break;
  1456. }
  1457. default:
  1458. /* Default to 16 kb sector size. */
  1459. rest_addr = 0x3fff;
  1460. sec_mask = 0x1c000;
  1461. break;
  1462. }
  1463. update_flash:
  1464. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1465. if (qla2x00_erase_flash(ha, man_id, flash_id)) {
  1466. rval = QLA_FUNCTION_FAILED;
  1467. break;
  1468. }
  1469. }
  1470. for (addr = offset, liter = 0; liter < length; liter++,
  1471. addr++) {
  1472. data = buf[liter];
  1473. /* Are we at the beginning of a sector? */
  1474. if ((addr & rest_addr) == 0) {
  1475. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1476. if (addr >= 0x10000UL) {
  1477. if (((addr >> 12) & 0xf0) &&
  1478. ((man_id == 0x01 &&
  1479. flash_id == 0x3e) ||
  1480. (man_id == 0x20 &&
  1481. flash_id == 0xd2))) {
  1482. sec_number++;
  1483. if (sec_number == 1) {
  1484. rest_addr =
  1485. 0x7fff;
  1486. sec_mask =
  1487. 0x18000;
  1488. } else if (
  1489. sec_number == 2 ||
  1490. sec_number == 3) {
  1491. rest_addr =
  1492. 0x1fff;
  1493. sec_mask =
  1494. 0x1e000;
  1495. } else if (
  1496. sec_number == 4) {
  1497. rest_addr =
  1498. 0x3fff;
  1499. sec_mask =
  1500. 0x1c000;
  1501. }
  1502. }
  1503. }
  1504. } else if (addr == ha->optrom_size / 2) {
  1505. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1506. RD_REG_WORD(&reg->nvram);
  1507. }
  1508. if (flash_id == 0xda && man_id == 0xc1) {
  1509. qla2x00_write_flash_byte(ha, 0x5555,
  1510. 0xaa);
  1511. qla2x00_write_flash_byte(ha, 0x2aaa,
  1512. 0x55);
  1513. qla2x00_write_flash_byte(ha, 0x5555,
  1514. 0xa0);
  1515. } else if (!IS_QLA2322(ha) && !IS_QLA6322(ha)) {
  1516. /* Then erase it */
  1517. if (qla2x00_erase_flash_sector(ha,
  1518. addr, sec_mask, man_id,
  1519. flash_id)) {
  1520. rval = QLA_FUNCTION_FAILED;
  1521. break;
  1522. }
  1523. if (man_id == 0x01 && flash_id == 0x6d)
  1524. sec_number++;
  1525. }
  1526. }
  1527. if (man_id == 0x01 && flash_id == 0x6d) {
  1528. if (sec_number == 1 &&
  1529. addr == (rest_addr - 1)) {
  1530. rest_addr = 0x0fff;
  1531. sec_mask = 0x1f000;
  1532. } else if (sec_number == 3 && (addr & 0x7ffe)) {
  1533. rest_addr = 0x3fff;
  1534. sec_mask = 0x1c000;
  1535. }
  1536. }
  1537. if (qla2x00_program_flash_address(ha, addr, data,
  1538. man_id, flash_id)) {
  1539. rval = QLA_FUNCTION_FAILED;
  1540. break;
  1541. }
  1542. cond_resched();
  1543. }
  1544. } while (0);
  1545. qla2x00_flash_disable(ha);
  1546. /* Resume HBA. */
  1547. qla2x00_resume_hba(ha);
  1548. return rval;
  1549. }
  1550. uint8_t *
  1551. qla24xx_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1552. uint32_t offset, uint32_t length)
  1553. {
  1554. /* Suspend HBA. */
  1555. scsi_block_requests(ha->host);
  1556. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1557. /* Go with read. */
  1558. qla24xx_read_flash_data(ha, (uint32_t *)buf, offset >> 2, length >> 2);
  1559. /* Resume HBA. */
  1560. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1561. scsi_unblock_requests(ha->host);
  1562. return buf;
  1563. }
  1564. int
  1565. qla24xx_write_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1566. uint32_t offset, uint32_t length)
  1567. {
  1568. int rval;
  1569. /* Suspend HBA. */
  1570. scsi_block_requests(ha->host);
  1571. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1572. /* Go with write. */
  1573. rval = qla24xx_write_flash_data(ha, (uint32_t *)buf, offset >> 2,
  1574. length >> 2);
  1575. /* Resume HBA -- RISC reset needed. */
  1576. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1577. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1578. qla2xxx_wake_dpc(ha);
  1579. qla2x00_wait_for_hba_online(ha);
  1580. scsi_unblock_requests(ha->host);
  1581. return rval;
  1582. }
  1583. uint8_t *
  1584. qla25xx_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1585. uint32_t offset, uint32_t length)
  1586. {
  1587. int rval;
  1588. dma_addr_t optrom_dma;
  1589. void *optrom;
  1590. uint8_t *pbuf;
  1591. uint32_t faddr, left, burst;
  1592. if (offset & 0xfff)
  1593. goto slow_read;
  1594. if (length < OPTROM_BURST_SIZE)
  1595. goto slow_read;
  1596. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  1597. &optrom_dma, GFP_KERNEL);
  1598. if (!optrom) {
  1599. qla_printk(KERN_DEBUG, ha,
  1600. "Unable to allocate memory for optrom burst read "
  1601. "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
  1602. goto slow_read;
  1603. }
  1604. pbuf = buf;
  1605. faddr = offset >> 2;
  1606. left = length >> 2;
  1607. burst = OPTROM_BURST_DWORDS;
  1608. while (left != 0) {
  1609. if (burst > left)
  1610. burst = left;
  1611. rval = qla2x00_dump_ram(ha, optrom_dma,
  1612. flash_data_to_access_addr(faddr), burst);
  1613. if (rval) {
  1614. qla_printk(KERN_WARNING, ha,
  1615. "Unable to burst-read optrom segment "
  1616. "(%x/%x/%llx).\n", rval,
  1617. flash_data_to_access_addr(faddr),
  1618. (unsigned long long)optrom_dma);
  1619. qla_printk(KERN_WARNING, ha,
  1620. "Reverting to slow-read.\n");
  1621. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  1622. optrom, optrom_dma);
  1623. goto slow_read;
  1624. }
  1625. memcpy(pbuf, optrom, burst * 4);
  1626. left -= burst;
  1627. faddr += burst;
  1628. pbuf += burst * 4;
  1629. }
  1630. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE, optrom,
  1631. optrom_dma);
  1632. return buf;
  1633. slow_read:
  1634. return qla24xx_read_optrom_data(ha, buf, offset, length);
  1635. }
  1636. /**
  1637. * qla2x00_get_fcode_version() - Determine an FCODE image's version.
  1638. * @ha: HA context
  1639. * @pcids: Pointer to the FCODE PCI data structure
  1640. *
  1641. * The process of retrieving the FCODE version information is at best
  1642. * described as interesting.
  1643. *
  1644. * Within the first 100h bytes of the image an ASCII string is present
  1645. * which contains several pieces of information including the FCODE
  1646. * version. Unfortunately it seems the only reliable way to retrieve
  1647. * the version is by scanning for another sentinel within the string,
  1648. * the FCODE build date:
  1649. *
  1650. * ... 2.00.02 10/17/02 ...
  1651. *
  1652. * Returns QLA_SUCCESS on successful retrieval of version.
  1653. */
  1654. static void
  1655. qla2x00_get_fcode_version(scsi_qla_host_t *ha, uint32_t pcids)
  1656. {
  1657. int ret = QLA_FUNCTION_FAILED;
  1658. uint32_t istart, iend, iter, vend;
  1659. uint8_t do_next, rbyte, *vbyte;
  1660. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1661. /* Skip the PCI data structure. */
  1662. istart = pcids +
  1663. ((qla2x00_read_flash_byte(ha, pcids + 0x0B) << 8) |
  1664. qla2x00_read_flash_byte(ha, pcids + 0x0A));
  1665. iend = istart + 0x100;
  1666. do {
  1667. /* Scan for the sentinel date string...eeewww. */
  1668. do_next = 0;
  1669. iter = istart;
  1670. while ((iter < iend) && !do_next) {
  1671. iter++;
  1672. if (qla2x00_read_flash_byte(ha, iter) == '/') {
  1673. if (qla2x00_read_flash_byte(ha, iter + 2) ==
  1674. '/')
  1675. do_next++;
  1676. else if (qla2x00_read_flash_byte(ha,
  1677. iter + 3) == '/')
  1678. do_next++;
  1679. }
  1680. }
  1681. if (!do_next)
  1682. break;
  1683. /* Backtrack to previous ' ' (space). */
  1684. do_next = 0;
  1685. while ((iter > istart) && !do_next) {
  1686. iter--;
  1687. if (qla2x00_read_flash_byte(ha, iter) == ' ')
  1688. do_next++;
  1689. }
  1690. if (!do_next)
  1691. break;
  1692. /*
  1693. * Mark end of version tag, and find previous ' ' (space) or
  1694. * string length (recent FCODE images -- major hack ahead!!!).
  1695. */
  1696. vend = iter - 1;
  1697. do_next = 0;
  1698. while ((iter > istart) && !do_next) {
  1699. iter--;
  1700. rbyte = qla2x00_read_flash_byte(ha, iter);
  1701. if (rbyte == ' ' || rbyte == 0xd || rbyte == 0x10)
  1702. do_next++;
  1703. }
  1704. if (!do_next)
  1705. break;
  1706. /* Mark beginning of version tag, and copy data. */
  1707. iter++;
  1708. if ((vend - iter) &&
  1709. ((vend - iter) < sizeof(ha->fcode_revision))) {
  1710. vbyte = ha->fcode_revision;
  1711. while (iter <= vend) {
  1712. *vbyte++ = qla2x00_read_flash_byte(ha, iter);
  1713. iter++;
  1714. }
  1715. ret = QLA_SUCCESS;
  1716. }
  1717. } while (0);
  1718. if (ret != QLA_SUCCESS)
  1719. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1720. }
  1721. int
  1722. qla2x00_get_flash_version(scsi_qla_host_t *ha, void *mbuf)
  1723. {
  1724. int ret = QLA_SUCCESS;
  1725. uint8_t code_type, last_image;
  1726. uint32_t pcihdr, pcids;
  1727. uint8_t *dbyte;
  1728. uint16_t *dcode;
  1729. if (!ha->pio_address || !mbuf)
  1730. return QLA_FUNCTION_FAILED;
  1731. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  1732. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  1733. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1734. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1735. qla2x00_flash_enable(ha);
  1736. /* Begin with first PCI expansion ROM header. */
  1737. pcihdr = 0;
  1738. last_image = 1;
  1739. do {
  1740. /* Verify PCI expansion ROM header. */
  1741. if (qla2x00_read_flash_byte(ha, pcihdr) != 0x55 ||
  1742. qla2x00_read_flash_byte(ha, pcihdr + 0x01) != 0xaa) {
  1743. /* No signature */
  1744. DEBUG2(printk("scsi(%ld): No matching ROM "
  1745. "signature.\n", ha->host_no));
  1746. ret = QLA_FUNCTION_FAILED;
  1747. break;
  1748. }
  1749. /* Locate PCI data structure. */
  1750. pcids = pcihdr +
  1751. ((qla2x00_read_flash_byte(ha, pcihdr + 0x19) << 8) |
  1752. qla2x00_read_flash_byte(ha, pcihdr + 0x18));
  1753. /* Validate signature of PCI data structure. */
  1754. if (qla2x00_read_flash_byte(ha, pcids) != 'P' ||
  1755. qla2x00_read_flash_byte(ha, pcids + 0x1) != 'C' ||
  1756. qla2x00_read_flash_byte(ha, pcids + 0x2) != 'I' ||
  1757. qla2x00_read_flash_byte(ha, pcids + 0x3) != 'R') {
  1758. /* Incorrect header. */
  1759. DEBUG2(printk("%s(): PCI data struct not found "
  1760. "pcir_adr=%x.\n", __func__, pcids));
  1761. ret = QLA_FUNCTION_FAILED;
  1762. break;
  1763. }
  1764. /* Read version */
  1765. code_type = qla2x00_read_flash_byte(ha, pcids + 0x14);
  1766. switch (code_type) {
  1767. case ROM_CODE_TYPE_BIOS:
  1768. /* Intel x86, PC-AT compatible. */
  1769. ha->bios_revision[0] =
  1770. qla2x00_read_flash_byte(ha, pcids + 0x12);
  1771. ha->bios_revision[1] =
  1772. qla2x00_read_flash_byte(ha, pcids + 0x13);
  1773. DEBUG3(printk("%s(): read BIOS %d.%d.\n", __func__,
  1774. ha->bios_revision[1], ha->bios_revision[0]));
  1775. break;
  1776. case ROM_CODE_TYPE_FCODE:
  1777. /* Open Firmware standard for PCI (FCode). */
  1778. /* Eeeewww... */
  1779. qla2x00_get_fcode_version(ha, pcids);
  1780. break;
  1781. case ROM_CODE_TYPE_EFI:
  1782. /* Extensible Firmware Interface (EFI). */
  1783. ha->efi_revision[0] =
  1784. qla2x00_read_flash_byte(ha, pcids + 0x12);
  1785. ha->efi_revision[1] =
  1786. qla2x00_read_flash_byte(ha, pcids + 0x13);
  1787. DEBUG3(printk("%s(): read EFI %d.%d.\n", __func__,
  1788. ha->efi_revision[1], ha->efi_revision[0]));
  1789. break;
  1790. default:
  1791. DEBUG2(printk("%s(): Unrecognized code type %x at "
  1792. "pcids %x.\n", __func__, code_type, pcids));
  1793. break;
  1794. }
  1795. last_image = qla2x00_read_flash_byte(ha, pcids + 0x15) & BIT_7;
  1796. /* Locate next PCI expansion ROM. */
  1797. pcihdr += ((qla2x00_read_flash_byte(ha, pcids + 0x11) << 8) |
  1798. qla2x00_read_flash_byte(ha, pcids + 0x10)) * 512;
  1799. } while (!last_image);
  1800. if (IS_QLA2322(ha)) {
  1801. /* Read firmware image information. */
  1802. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1803. dbyte = mbuf;
  1804. memset(dbyte, 0, 8);
  1805. dcode = (uint16_t *)dbyte;
  1806. qla2x00_read_flash_data(ha, dbyte, FA_RISC_CODE_ADDR * 4 + 10,
  1807. 8);
  1808. DEBUG3(printk("%s(%ld): dumping fw ver from flash:\n",
  1809. __func__, ha->host_no));
  1810. DEBUG3(qla2x00_dump_buffer((uint8_t *)dbyte, 8));
  1811. if ((dcode[0] == 0xffff && dcode[1] == 0xffff &&
  1812. dcode[2] == 0xffff && dcode[3] == 0xffff) ||
  1813. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  1814. dcode[3] == 0)) {
  1815. DEBUG2(printk("%s(): Unrecognized fw revision at "
  1816. "%x.\n", __func__, FA_RISC_CODE_ADDR * 4));
  1817. } else {
  1818. /* values are in big endian */
  1819. ha->fw_revision[0] = dbyte[0] << 16 | dbyte[1];
  1820. ha->fw_revision[1] = dbyte[2] << 16 | dbyte[3];
  1821. ha->fw_revision[2] = dbyte[4] << 16 | dbyte[5];
  1822. }
  1823. }
  1824. qla2x00_flash_disable(ha);
  1825. return ret;
  1826. }
  1827. int
  1828. qla24xx_get_flash_version(scsi_qla_host_t *ha, void *mbuf)
  1829. {
  1830. int ret = QLA_SUCCESS;
  1831. uint32_t pcihdr, pcids;
  1832. uint32_t *dcode;
  1833. uint8_t *bcode;
  1834. uint8_t code_type, last_image;
  1835. int i;
  1836. if (!mbuf)
  1837. return QLA_FUNCTION_FAILED;
  1838. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  1839. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  1840. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1841. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1842. dcode = mbuf;
  1843. /* Begin with first PCI expansion ROM header. */
  1844. pcihdr = 0;
  1845. last_image = 1;
  1846. do {
  1847. /* Verify PCI expansion ROM header. */
  1848. qla24xx_read_flash_data(ha, dcode, pcihdr >> 2, 0x20);
  1849. bcode = mbuf + (pcihdr % 4);
  1850. if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa) {
  1851. /* No signature */
  1852. DEBUG2(printk("scsi(%ld): No matching ROM "
  1853. "signature.\n", ha->host_no));
  1854. ret = QLA_FUNCTION_FAILED;
  1855. break;
  1856. }
  1857. /* Locate PCI data structure. */
  1858. pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
  1859. qla24xx_read_flash_data(ha, dcode, pcids >> 2, 0x20);
  1860. bcode = mbuf + (pcihdr % 4);
  1861. /* Validate signature of PCI data structure. */
  1862. if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
  1863. bcode[0x2] != 'I' || bcode[0x3] != 'R') {
  1864. /* Incorrect header. */
  1865. DEBUG2(printk("%s(): PCI data struct not found "
  1866. "pcir_adr=%x.\n", __func__, pcids));
  1867. ret = QLA_FUNCTION_FAILED;
  1868. break;
  1869. }
  1870. /* Read version */
  1871. code_type = bcode[0x14];
  1872. switch (code_type) {
  1873. case ROM_CODE_TYPE_BIOS:
  1874. /* Intel x86, PC-AT compatible. */
  1875. ha->bios_revision[0] = bcode[0x12];
  1876. ha->bios_revision[1] = bcode[0x13];
  1877. DEBUG3(printk("%s(): read BIOS %d.%d.\n", __func__,
  1878. ha->bios_revision[1], ha->bios_revision[0]));
  1879. break;
  1880. case ROM_CODE_TYPE_FCODE:
  1881. /* Open Firmware standard for PCI (FCode). */
  1882. ha->fcode_revision[0] = bcode[0x12];
  1883. ha->fcode_revision[1] = bcode[0x13];
  1884. DEBUG3(printk("%s(): read FCODE %d.%d.\n", __func__,
  1885. ha->fcode_revision[1], ha->fcode_revision[0]));
  1886. break;
  1887. case ROM_CODE_TYPE_EFI:
  1888. /* Extensible Firmware Interface (EFI). */
  1889. ha->efi_revision[0] = bcode[0x12];
  1890. ha->efi_revision[1] = bcode[0x13];
  1891. DEBUG3(printk("%s(): read EFI %d.%d.\n", __func__,
  1892. ha->efi_revision[1], ha->efi_revision[0]));
  1893. break;
  1894. default:
  1895. DEBUG2(printk("%s(): Unrecognized code type %x at "
  1896. "pcids %x.\n", __func__, code_type, pcids));
  1897. break;
  1898. }
  1899. last_image = bcode[0x15] & BIT_7;
  1900. /* Locate next PCI expansion ROM. */
  1901. pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
  1902. } while (!last_image);
  1903. /* Read firmware image information. */
  1904. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1905. dcode = mbuf;
  1906. qla24xx_read_flash_data(ha, dcode, FA_RISC_CODE_ADDR + 4, 4);
  1907. for (i = 0; i < 4; i++)
  1908. dcode[i] = be32_to_cpu(dcode[i]);
  1909. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  1910. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  1911. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  1912. dcode[3] == 0)) {
  1913. DEBUG2(printk("%s(): Unrecognized fw version at %x.\n",
  1914. __func__, FA_RISC_CODE_ADDR));
  1915. } else {
  1916. ha->fw_revision[0] = dcode[0];
  1917. ha->fw_revision[1] = dcode[1];
  1918. ha->fw_revision[2] = dcode[2];
  1919. ha->fw_revision[3] = dcode[3];
  1920. }
  1921. return ret;
  1922. }