qla_sup.c 78 KB

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