qla_sup.c 74 KB

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