qla_sup.c 56 KB

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