qla_sup.c 79 KB

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