qla_sup.c 71 KB

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