qla_sup.c 74 KB

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