qla_sup.c 70 KB

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