qla_sup.c 53 KB

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