qla_sup.c 95 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * QLogic Fibre Channel HBA Driver
  4. * Copyright (c) 2003-2014 QLogic Corporation
  5. */
  6. #include "qla_def.h"
  7. #include <linux/delay.h>
  8. #include <linux/slab.h>
  9. #include <linux/vmalloc.h>
  10. #include <linux/uaccess.h>
  11. /*
  12. * NVRAM support routines
  13. */
  14. /**
  15. * qla2x00_lock_nvram_access() -
  16. * @ha: HA context
  17. */
  18. static void
  19. qla2x00_lock_nvram_access(struct qla_hw_data *ha)
  20. {
  21. uint16_t data;
  22. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  23. if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
  24. data = rd_reg_word(&reg->nvram);
  25. while (data & NVR_BUSY) {
  26. udelay(100);
  27. data = rd_reg_word(&reg->nvram);
  28. }
  29. /* Lock resource */
  30. wrt_reg_word(&reg->u.isp2300.host_semaphore, 0x1);
  31. rd_reg_word(&reg->u.isp2300.host_semaphore);
  32. udelay(5);
  33. data = rd_reg_word(&reg->u.isp2300.host_semaphore);
  34. while ((data & BIT_0) == 0) {
  35. /* Lock failed */
  36. udelay(100);
  37. wrt_reg_word(&reg->u.isp2300.host_semaphore, 0x1);
  38. rd_reg_word(&reg->u.isp2300.host_semaphore);
  39. udelay(5);
  40. data = rd_reg_word(&reg->u.isp2300.host_semaphore);
  41. }
  42. }
  43. }
  44. /**
  45. * qla2x00_unlock_nvram_access() -
  46. * @ha: HA context
  47. */
  48. static void
  49. qla2x00_unlock_nvram_access(struct qla_hw_data *ha)
  50. {
  51. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  52. if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
  53. wrt_reg_word(&reg->u.isp2300.host_semaphore, 0);
  54. rd_reg_word(&reg->u.isp2300.host_semaphore);
  55. }
  56. }
  57. /**
  58. * qla2x00_nv_write() - Prepare for NVRAM read/write operation.
  59. * @ha: HA context
  60. * @data: Serial interface selector
  61. */
  62. static void
  63. qla2x00_nv_write(struct qla_hw_data *ha, uint16_t data)
  64. {
  65. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  66. wrt_reg_word(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
  67. rd_reg_word(&reg->nvram); /* PCI Posting. */
  68. NVRAM_DELAY();
  69. wrt_reg_word(&reg->nvram, data | NVR_SELECT | NVR_CLOCK |
  70. NVR_WRT_ENABLE);
  71. rd_reg_word(&reg->nvram); /* PCI Posting. */
  72. NVRAM_DELAY();
  73. wrt_reg_word(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
  74. rd_reg_word(&reg->nvram); /* PCI Posting. */
  75. NVRAM_DELAY();
  76. }
  77. /**
  78. * qla2x00_nvram_request() - Sends read command to NVRAM and gets data from
  79. * NVRAM.
  80. * @ha: HA context
  81. * @nv_cmd: NVRAM command
  82. *
  83. * Bit definitions for NVRAM command:
  84. *
  85. * Bit 26 = start bit
  86. * Bit 25, 24 = opcode
  87. * Bit 23-16 = address
  88. * Bit 15-0 = write data
  89. *
  90. * Returns the word read from nvram @addr.
  91. */
  92. static uint16_t
  93. qla2x00_nvram_request(struct qla_hw_data *ha, uint32_t nv_cmd)
  94. {
  95. uint8_t cnt;
  96. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  97. uint16_t data = 0;
  98. uint16_t reg_data;
  99. /* Send command to NVRAM. */
  100. nv_cmd <<= 5;
  101. for (cnt = 0; cnt < 11; cnt++) {
  102. if (nv_cmd & BIT_31)
  103. qla2x00_nv_write(ha, NVR_DATA_OUT);
  104. else
  105. qla2x00_nv_write(ha, 0);
  106. nv_cmd <<= 1;
  107. }
  108. /* Read data from NVRAM. */
  109. for (cnt = 0; cnt < 16; cnt++) {
  110. wrt_reg_word(&reg->nvram, NVR_SELECT | NVR_CLOCK);
  111. rd_reg_word(&reg->nvram); /* PCI Posting. */
  112. NVRAM_DELAY();
  113. data <<= 1;
  114. reg_data = rd_reg_word(&reg->nvram);
  115. if (reg_data & NVR_DATA_IN)
  116. data |= BIT_0;
  117. wrt_reg_word(&reg->nvram, NVR_SELECT);
  118. rd_reg_word(&reg->nvram); /* PCI Posting. */
  119. NVRAM_DELAY();
  120. }
  121. /* Deselect chip. */
  122. wrt_reg_word(&reg->nvram, NVR_DESELECT);
  123. rd_reg_word(&reg->nvram); /* PCI Posting. */
  124. NVRAM_DELAY();
  125. return data;
  126. }
  127. /**
  128. * qla2x00_get_nvram_word() - Calculates word position in NVRAM and calls the
  129. * request routine to get the word from NVRAM.
  130. * @ha: HA context
  131. * @addr: Address in NVRAM to read
  132. *
  133. * Returns the word read from nvram @addr.
  134. */
  135. static uint16_t
  136. qla2x00_get_nvram_word(struct qla_hw_data *ha, uint32_t addr)
  137. {
  138. uint16_t data;
  139. uint32_t nv_cmd;
  140. nv_cmd = addr << 16;
  141. nv_cmd |= NV_READ_OP;
  142. data = qla2x00_nvram_request(ha, nv_cmd);
  143. return (data);
  144. }
  145. /**
  146. * qla2x00_nv_deselect() - Deselect NVRAM operations.
  147. * @ha: HA context
  148. */
  149. static void
  150. qla2x00_nv_deselect(struct qla_hw_data *ha)
  151. {
  152. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  153. wrt_reg_word(&reg->nvram, NVR_DESELECT);
  154. rd_reg_word(&reg->nvram); /* PCI Posting. */
  155. NVRAM_DELAY();
  156. }
  157. /**
  158. * qla2x00_write_nvram_word() - Write NVRAM data.
  159. * @ha: HA context
  160. * @addr: Address in NVRAM to write
  161. * @data: word to program
  162. */
  163. static void
  164. qla2x00_write_nvram_word(struct qla_hw_data *ha, uint32_t addr, __le16 data)
  165. {
  166. int count;
  167. uint16_t word;
  168. uint32_t nv_cmd, wait_cnt;
  169. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  170. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  171. qla2x00_nv_write(ha, NVR_DATA_OUT);
  172. qla2x00_nv_write(ha, 0);
  173. qla2x00_nv_write(ha, 0);
  174. for (word = 0; word < 8; word++)
  175. qla2x00_nv_write(ha, NVR_DATA_OUT);
  176. qla2x00_nv_deselect(ha);
  177. /* Write data */
  178. nv_cmd = (addr << 16) | NV_WRITE_OP;
  179. nv_cmd |= (__force u16)data;
  180. nv_cmd <<= 5;
  181. for (count = 0; count < 27; count++) {
  182. if (nv_cmd & BIT_31)
  183. qla2x00_nv_write(ha, NVR_DATA_OUT);
  184. else
  185. qla2x00_nv_write(ha, 0);
  186. nv_cmd <<= 1;
  187. }
  188. qla2x00_nv_deselect(ha);
  189. /* Wait for NVRAM to become ready */
  190. wrt_reg_word(&reg->nvram, NVR_SELECT);
  191. rd_reg_word(&reg->nvram); /* PCI Posting. */
  192. wait_cnt = NVR_WAIT_CNT;
  193. do {
  194. if (!--wait_cnt) {
  195. ql_dbg(ql_dbg_user, vha, 0x708d,
  196. "NVRAM didn't go ready...\n");
  197. break;
  198. }
  199. NVRAM_DELAY();
  200. word = rd_reg_word(&reg->nvram);
  201. } while ((word & NVR_DATA_IN) == 0);
  202. qla2x00_nv_deselect(ha);
  203. /* Disable writes */
  204. qla2x00_nv_write(ha, NVR_DATA_OUT);
  205. for (count = 0; count < 10; count++)
  206. qla2x00_nv_write(ha, 0);
  207. qla2x00_nv_deselect(ha);
  208. }
  209. static int
  210. qla2x00_write_nvram_word_tmo(struct qla_hw_data *ha, uint32_t addr,
  211. __le16 data, uint32_t tmo)
  212. {
  213. int ret, count;
  214. uint16_t word;
  215. uint32_t nv_cmd;
  216. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  217. ret = QLA_SUCCESS;
  218. qla2x00_nv_write(ha, NVR_DATA_OUT);
  219. qla2x00_nv_write(ha, 0);
  220. qla2x00_nv_write(ha, 0);
  221. for (word = 0; word < 8; word++)
  222. qla2x00_nv_write(ha, NVR_DATA_OUT);
  223. qla2x00_nv_deselect(ha);
  224. /* Write data */
  225. nv_cmd = (addr << 16) | NV_WRITE_OP;
  226. nv_cmd |= (__force u16)data;
  227. nv_cmd <<= 5;
  228. for (count = 0; count < 27; count++) {
  229. if (nv_cmd & BIT_31)
  230. qla2x00_nv_write(ha, NVR_DATA_OUT);
  231. else
  232. qla2x00_nv_write(ha, 0);
  233. nv_cmd <<= 1;
  234. }
  235. qla2x00_nv_deselect(ha);
  236. /* Wait for NVRAM to become ready */
  237. wrt_reg_word(&reg->nvram, NVR_SELECT);
  238. rd_reg_word(&reg->nvram); /* PCI Posting. */
  239. do {
  240. NVRAM_DELAY();
  241. word = rd_reg_word(&reg->nvram);
  242. if (!--tmo) {
  243. ret = QLA_FUNCTION_FAILED;
  244. break;
  245. }
  246. } while ((word & NVR_DATA_IN) == 0);
  247. qla2x00_nv_deselect(ha);
  248. /* Disable writes */
  249. qla2x00_nv_write(ha, NVR_DATA_OUT);
  250. for (count = 0; count < 10; count++)
  251. qla2x00_nv_write(ha, 0);
  252. qla2x00_nv_deselect(ha);
  253. return ret;
  254. }
  255. /**
  256. * qla2x00_clear_nvram_protection() -
  257. * @ha: HA context
  258. */
  259. static int
  260. qla2x00_clear_nvram_protection(struct qla_hw_data *ha)
  261. {
  262. int ret, stat;
  263. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  264. uint32_t word, wait_cnt;
  265. __le16 wprot, wprot_old;
  266. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  267. /* Clear NVRAM write protection. */
  268. ret = QLA_FUNCTION_FAILED;
  269. wprot_old = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
  270. stat = qla2x00_write_nvram_word_tmo(ha, ha->nvram_base,
  271. cpu_to_le16(0x1234), 100000);
  272. wprot = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
  273. if (stat != QLA_SUCCESS || wprot != cpu_to_le16(0x1234)) {
  274. /* Write enable. */
  275. qla2x00_nv_write(ha, NVR_DATA_OUT);
  276. qla2x00_nv_write(ha, 0);
  277. qla2x00_nv_write(ha, 0);
  278. for (word = 0; word < 8; word++)
  279. qla2x00_nv_write(ha, NVR_DATA_OUT);
  280. qla2x00_nv_deselect(ha);
  281. /* Enable protection register. */
  282. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  283. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  284. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  285. for (word = 0; word < 8; word++)
  286. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  287. qla2x00_nv_deselect(ha);
  288. /* Clear protection register (ffff is cleared). */
  289. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  290. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  291. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  292. for (word = 0; word < 8; word++)
  293. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  294. qla2x00_nv_deselect(ha);
  295. /* Wait for NVRAM to become ready. */
  296. wrt_reg_word(&reg->nvram, NVR_SELECT);
  297. rd_reg_word(&reg->nvram); /* PCI Posting. */
  298. wait_cnt = NVR_WAIT_CNT;
  299. do {
  300. if (!--wait_cnt) {
  301. ql_dbg(ql_dbg_user, vha, 0x708e,
  302. "NVRAM didn't go ready...\n");
  303. break;
  304. }
  305. NVRAM_DELAY();
  306. word = rd_reg_word(&reg->nvram);
  307. } while ((word & NVR_DATA_IN) == 0);
  308. if (wait_cnt)
  309. ret = QLA_SUCCESS;
  310. } else
  311. qla2x00_write_nvram_word(ha, ha->nvram_base, wprot_old);
  312. return ret;
  313. }
  314. static void
  315. qla2x00_set_nvram_protection(struct qla_hw_data *ha, int stat)
  316. {
  317. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  318. uint32_t word, wait_cnt;
  319. scsi_qla_host_t *vha = pci_get_drvdata(ha->pdev);
  320. if (stat != QLA_SUCCESS)
  321. return;
  322. /* Set NVRAM write protection. */
  323. /* Write enable. */
  324. qla2x00_nv_write(ha, NVR_DATA_OUT);
  325. qla2x00_nv_write(ha, 0);
  326. qla2x00_nv_write(ha, 0);
  327. for (word = 0; word < 8; word++)
  328. qla2x00_nv_write(ha, NVR_DATA_OUT);
  329. qla2x00_nv_deselect(ha);
  330. /* Enable protection register. */
  331. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  332. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  333. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  334. for (word = 0; word < 8; word++)
  335. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  336. qla2x00_nv_deselect(ha);
  337. /* Enable protection register. */
  338. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  339. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  340. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  341. for (word = 0; word < 8; word++)
  342. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  343. qla2x00_nv_deselect(ha);
  344. /* Wait for NVRAM to become ready. */
  345. wrt_reg_word(&reg->nvram, NVR_SELECT);
  346. rd_reg_word(&reg->nvram); /* PCI Posting. */
  347. wait_cnt = NVR_WAIT_CNT;
  348. do {
  349. if (!--wait_cnt) {
  350. ql_dbg(ql_dbg_user, vha, 0x708f,
  351. "NVRAM didn't go ready...\n");
  352. break;
  353. }
  354. NVRAM_DELAY();
  355. word = rd_reg_word(&reg->nvram);
  356. } while ((word & NVR_DATA_IN) == 0);
  357. }
  358. /*****************************************************************************/
  359. /* Flash Manipulation Routines */
  360. /*****************************************************************************/
  361. static inline uint32_t
  362. flash_conf_addr(struct qla_hw_data *ha, uint32_t faddr)
  363. {
  364. return ha->flash_conf_off + faddr;
  365. }
  366. static inline uint32_t
  367. flash_data_addr(struct qla_hw_data *ha, uint32_t faddr)
  368. {
  369. return ha->flash_data_off + faddr;
  370. }
  371. static inline uint32_t
  372. nvram_conf_addr(struct qla_hw_data *ha, uint32_t naddr)
  373. {
  374. return ha->nvram_conf_off + naddr;
  375. }
  376. static inline uint32_t
  377. nvram_data_addr(struct qla_hw_data *ha, uint32_t naddr)
  378. {
  379. return ha->nvram_data_off + naddr;
  380. }
  381. static int
  382. qla24xx_read_flash_dword(struct qla_hw_data *ha, uint32_t addr, uint32_t *data)
  383. {
  384. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  385. ulong cnt = 30000;
  386. wrt_reg_dword(&reg->flash_addr, addr & ~FARX_DATA_FLAG);
  387. while (cnt--) {
  388. if (rd_reg_dword(&reg->flash_addr) & FARX_DATA_FLAG) {
  389. *data = rd_reg_dword(&reg->flash_data);
  390. return QLA_SUCCESS;
  391. }
  392. udelay(10);
  393. cond_resched();
  394. }
  395. ql_log(ql_log_warn, pci_get_drvdata(ha->pdev), 0x7090,
  396. "Flash read dword at %x timeout.\n", addr);
  397. *data = 0xDEADDEAD;
  398. return QLA_FUNCTION_TIMEOUT;
  399. }
  400. int
  401. qla24xx_read_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
  402. uint32_t dwords)
  403. {
  404. ulong i;
  405. int ret = QLA_SUCCESS;
  406. struct qla_hw_data *ha = vha->hw;
  407. /* Dword reads to flash. */
  408. faddr = flash_data_addr(ha, faddr);
  409. for (i = 0; i < dwords; i++, faddr++, dwptr++) {
  410. ret = qla24xx_read_flash_dword(ha, faddr, dwptr);
  411. if (ret != QLA_SUCCESS)
  412. break;
  413. cpu_to_le32s(dwptr);
  414. }
  415. return ret;
  416. }
  417. static int
  418. qla24xx_write_flash_dword(struct qla_hw_data *ha, uint32_t addr, uint32_t data)
  419. {
  420. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  421. ulong cnt = 500000;
  422. wrt_reg_dword(&reg->flash_data, data);
  423. wrt_reg_dword(&reg->flash_addr, addr | FARX_DATA_FLAG);
  424. while (cnt--) {
  425. if (!(rd_reg_dword(&reg->flash_addr) & FARX_DATA_FLAG))
  426. return QLA_SUCCESS;
  427. udelay(10);
  428. cond_resched();
  429. }
  430. ql_log(ql_log_warn, pci_get_drvdata(ha->pdev), 0x7090,
  431. "Flash write dword at %x timeout.\n", addr);
  432. return QLA_FUNCTION_TIMEOUT;
  433. }
  434. static void
  435. qla24xx_get_flash_manufacturer(struct qla_hw_data *ha, uint8_t *man_id,
  436. uint8_t *flash_id)
  437. {
  438. uint32_t faddr, ids = 0;
  439. *man_id = *flash_id = 0;
  440. faddr = flash_conf_addr(ha, 0x03ab);
  441. if (!qla24xx_read_flash_dword(ha, faddr, &ids)) {
  442. *man_id = LSB(ids);
  443. *flash_id = MSB(ids);
  444. }
  445. /* Check if man_id and flash_id are valid. */
  446. if (ids != 0xDEADDEAD && (*man_id == 0 || *flash_id == 0)) {
  447. /* Read information using 0x9f opcode
  448. * Device ID, Mfg ID would be read in the format:
  449. * <Ext Dev Info><Device ID Part2><Device ID Part 1><Mfg ID>
  450. * Example: ATMEL 0x00 01 45 1F
  451. * Extract MFG and Dev ID from last two bytes.
  452. */
  453. faddr = flash_conf_addr(ha, 0x009f);
  454. if (!qla24xx_read_flash_dword(ha, faddr, &ids)) {
  455. *man_id = LSB(ids);
  456. *flash_id = MSB(ids);
  457. }
  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. uint16_t cnt, chksum;
  466. __le16 *wptr;
  467. struct qla_hw_data *ha = vha->hw;
  468. struct req_que *req = ha->req_q_map[0];
  469. struct qla_flt_location *fltl = (void *)req->ring;
  470. uint32_t *dcode = (uint32_t *)req->ring;
  471. uint8_t *buf = (void *)req->ring, *bcode, last_image;
  472. int rc;
  473. /*
  474. * FLT-location structure resides after the last PCI region.
  475. */
  476. /* Begin with sane defaults. */
  477. loc = locations[0];
  478. *start = 0;
  479. if (IS_QLA24XX_TYPE(ha))
  480. *start = FA_FLASH_LAYOUT_ADDR_24;
  481. else if (IS_QLA25XX(ha))
  482. *start = FA_FLASH_LAYOUT_ADDR;
  483. else if (IS_QLA81XX(ha))
  484. *start = FA_FLASH_LAYOUT_ADDR_81;
  485. else if (IS_P3P_TYPE(ha)) {
  486. *start = FA_FLASH_LAYOUT_ADDR_82;
  487. goto end;
  488. } else if (IS_QLA83XX(ha) || IS_QLA27XX(ha)) {
  489. *start = FA_FLASH_LAYOUT_ADDR_83;
  490. goto end;
  491. } else if (IS_QLA28XX(ha)) {
  492. *start = FA_FLASH_LAYOUT_ADDR_28;
  493. goto end;
  494. }
  495. /* Begin with first PCI expansion ROM header. */
  496. pcihdr = 0;
  497. do {
  498. /* Verify PCI expansion ROM header. */
  499. rc = qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, 0x20);
  500. if (rc) {
  501. ql_log(ql_log_info, vha, 0x016d,
  502. "Unable to read PCI Expansion Rom Header (%x).\n", rc);
  503. return QLA_FUNCTION_FAILED;
  504. }
  505. bcode = buf + (pcihdr % 4);
  506. if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa)
  507. goto end;
  508. /* Locate PCI data structure. */
  509. pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
  510. rc = qla24xx_read_flash_data(vha, dcode, pcids >> 2, 0x20);
  511. if (rc) {
  512. ql_log(ql_log_info, vha, 0x0179,
  513. "Unable to read PCI Data Structure (%x).\n", rc);
  514. return QLA_FUNCTION_FAILED;
  515. }
  516. bcode = buf + (pcihdr % 4);
  517. /* Validate signature of PCI data structure. */
  518. if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
  519. bcode[0x2] != 'I' || bcode[0x3] != 'R')
  520. goto end;
  521. last_image = bcode[0x15] & BIT_7;
  522. /* Locate next PCI expansion ROM. */
  523. pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
  524. } while (!last_image);
  525. /* Now verify FLT-location structure. */
  526. rc = qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, sizeof(*fltl) >> 2);
  527. if (rc) {
  528. ql_log(ql_log_info, vha, 0x017a,
  529. "Unable to read FLT (%x).\n", rc);
  530. return QLA_FUNCTION_FAILED;
  531. }
  532. if (memcmp(fltl->sig, "QFLT", 4))
  533. goto end;
  534. wptr = (__force __le16 *)req->ring;
  535. cnt = sizeof(*fltl) / sizeof(*wptr);
  536. for (chksum = 0; cnt--; wptr++)
  537. chksum += le16_to_cpu(*wptr);
  538. if (chksum) {
  539. ql_log(ql_log_fatal, vha, 0x0045,
  540. "Inconsistent FLTL detected: checksum=0x%x.\n", chksum);
  541. ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010e,
  542. fltl, sizeof(*fltl));
  543. return QLA_FUNCTION_FAILED;
  544. }
  545. /* Good data. Use specified location. */
  546. loc = locations[1];
  547. *start = (le16_to_cpu(fltl->start_hi) << 16 |
  548. le16_to_cpu(fltl->start_lo)) >> 2;
  549. end:
  550. ql_dbg(ql_dbg_init, vha, 0x0046,
  551. "FLTL[%s] = 0x%x.\n",
  552. loc, *start);
  553. return QLA_SUCCESS;
  554. }
  555. static void
  556. qla2xxx_get_flt_info(scsi_qla_host_t *vha, uint32_t flt_addr)
  557. {
  558. const char *locations[] = { "DEF", "FLT" }, *loc = locations[1];
  559. const uint32_t def_fw[] =
  560. { FA_RISC_CODE_ADDR, FA_RISC_CODE_ADDR, FA_RISC_CODE_ADDR_81 };
  561. const uint32_t def_boot[] =
  562. { FA_BOOT_CODE_ADDR, FA_BOOT_CODE_ADDR, FA_BOOT_CODE_ADDR_81 };
  563. const uint32_t def_vpd_nvram[] =
  564. { FA_VPD_NVRAM_ADDR, FA_VPD_NVRAM_ADDR, FA_VPD_NVRAM_ADDR_81 };
  565. const uint32_t def_vpd0[] =
  566. { 0, 0, FA_VPD0_ADDR_81 };
  567. const uint32_t def_vpd1[] =
  568. { 0, 0, FA_VPD1_ADDR_81 };
  569. const uint32_t def_nvram0[] =
  570. { 0, 0, FA_NVRAM0_ADDR_81 };
  571. const uint32_t def_nvram1[] =
  572. { 0, 0, FA_NVRAM1_ADDR_81 };
  573. const uint32_t def_fdt[] =
  574. { FA_FLASH_DESCR_ADDR_24, FA_FLASH_DESCR_ADDR,
  575. FA_FLASH_DESCR_ADDR_81 };
  576. const uint32_t def_npiv_conf0[] =
  577. { FA_NPIV_CONF0_ADDR_24, FA_NPIV_CONF0_ADDR,
  578. FA_NPIV_CONF0_ADDR_81 };
  579. const uint32_t def_npiv_conf1[] =
  580. { FA_NPIV_CONF1_ADDR_24, FA_NPIV_CONF1_ADDR,
  581. FA_NPIV_CONF1_ADDR_81 };
  582. const uint32_t fcp_prio_cfg0[] =
  583. { FA_FCP_PRIO0_ADDR, FA_FCP_PRIO0_ADDR_25,
  584. 0 };
  585. const uint32_t fcp_prio_cfg1[] =
  586. { FA_FCP_PRIO1_ADDR, FA_FCP_PRIO1_ADDR_25,
  587. 0 };
  588. struct qla_hw_data *ha = vha->hw;
  589. uint32_t def = IS_QLA81XX(ha) ? 2 : IS_QLA25XX(ha) ? 1 : 0;
  590. struct qla_flt_header *flt = ha->flt;
  591. struct qla_flt_region *region = &flt->region[0];
  592. __le16 *wptr;
  593. uint16_t cnt, chksum;
  594. uint32_t start;
  595. /* Assign FCP prio region since older adapters may not have FLT, or
  596. FCP prio region in it's FLT.
  597. */
  598. ha->flt_region_fcp_prio = (ha->port_no == 0) ?
  599. fcp_prio_cfg0[def] : fcp_prio_cfg1[def];
  600. ha->flt_region_flt = flt_addr;
  601. wptr = (__force __le16 *)ha->flt;
  602. ha->isp_ops->read_optrom(vha, flt, flt_addr << 2,
  603. (sizeof(struct qla_flt_header) + FLT_REGIONS_SIZE));
  604. if (le16_to_cpu(*wptr) == 0xffff)
  605. goto no_flash_data;
  606. if (flt->version != cpu_to_le16(1)) {
  607. ql_log(ql_log_warn, vha, 0x0047,
  608. "Unsupported FLT detected: version=0x%x length=0x%x checksum=0x%x.\n",
  609. le16_to_cpu(flt->version), le16_to_cpu(flt->length),
  610. le16_to_cpu(flt->checksum));
  611. goto no_flash_data;
  612. }
  613. cnt = (sizeof(*flt) + le16_to_cpu(flt->length)) / sizeof(*wptr);
  614. for (chksum = 0; cnt--; wptr++)
  615. chksum += le16_to_cpu(*wptr);
  616. if (chksum) {
  617. ql_log(ql_log_fatal, vha, 0x0048,
  618. "Inconsistent FLT detected: version=0x%x length=0x%x checksum=0x%x.\n",
  619. le16_to_cpu(flt->version), le16_to_cpu(flt->length),
  620. le16_to_cpu(flt->checksum));
  621. goto no_flash_data;
  622. }
  623. cnt = le16_to_cpu(flt->length) / sizeof(*region);
  624. for ( ; cnt; cnt--, region++) {
  625. /* Store addresses as DWORD offsets. */
  626. start = le32_to_cpu(region->start) >> 2;
  627. ql_dbg(ql_dbg_init, vha, 0x0049,
  628. "FLT[%#x]: start=%#x end=%#x size=%#x.\n",
  629. le16_to_cpu(region->code), start,
  630. le32_to_cpu(region->end) >> 2,
  631. le32_to_cpu(region->size) >> 2);
  632. if (region->attribute)
  633. ql_log(ql_dbg_init, vha, 0xffff,
  634. "Region %x is secure\n", region->code);
  635. switch (le16_to_cpu(region->code)) {
  636. case FLT_REG_FCOE_FW:
  637. if (!IS_QLA8031(ha))
  638. break;
  639. ha->flt_region_fw = start;
  640. break;
  641. case FLT_REG_FW:
  642. if (IS_QLA8031(ha))
  643. break;
  644. ha->flt_region_fw = start;
  645. break;
  646. case FLT_REG_BOOT_CODE:
  647. ha->flt_region_boot = start;
  648. break;
  649. case FLT_REG_VPD_0:
  650. if (IS_QLA8031(ha))
  651. break;
  652. ha->flt_region_vpd_nvram = start;
  653. if (IS_P3P_TYPE(ha))
  654. break;
  655. if (ha->port_no == 0)
  656. ha->flt_region_vpd = start;
  657. break;
  658. case FLT_REG_VPD_1:
  659. if (IS_P3P_TYPE(ha) || IS_QLA8031(ha))
  660. break;
  661. if (ha->port_no == 1)
  662. ha->flt_region_vpd = start;
  663. break;
  664. case FLT_REG_VPD_2:
  665. if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
  666. break;
  667. if (ha->port_no == 2)
  668. ha->flt_region_vpd = start;
  669. break;
  670. case FLT_REG_VPD_3:
  671. if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
  672. break;
  673. if (ha->port_no == 3)
  674. ha->flt_region_vpd = start;
  675. break;
  676. case FLT_REG_NVRAM_0:
  677. if (IS_QLA8031(ha))
  678. break;
  679. if (ha->port_no == 0)
  680. ha->flt_region_nvram = start;
  681. break;
  682. case FLT_REG_NVRAM_1:
  683. if (IS_QLA8031(ha))
  684. break;
  685. if (ha->port_no == 1)
  686. ha->flt_region_nvram = start;
  687. break;
  688. case FLT_REG_NVRAM_2:
  689. if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
  690. break;
  691. if (ha->port_no == 2)
  692. ha->flt_region_nvram = start;
  693. break;
  694. case FLT_REG_NVRAM_3:
  695. if (!IS_QLA27XX(ha) && !IS_QLA28XX(ha))
  696. break;
  697. if (ha->port_no == 3)
  698. ha->flt_region_nvram = start;
  699. break;
  700. case FLT_REG_FDT:
  701. ha->flt_region_fdt = start;
  702. break;
  703. case FLT_REG_NPIV_CONF_0:
  704. if (ha->port_no == 0)
  705. ha->flt_region_npiv_conf = start;
  706. break;
  707. case FLT_REG_NPIV_CONF_1:
  708. if (ha->port_no == 1)
  709. ha->flt_region_npiv_conf = start;
  710. break;
  711. case FLT_REG_GOLD_FW:
  712. ha->flt_region_gold_fw = start;
  713. break;
  714. case FLT_REG_FCP_PRIO_0:
  715. if (ha->port_no == 0)
  716. ha->flt_region_fcp_prio = start;
  717. break;
  718. case FLT_REG_FCP_PRIO_1:
  719. if (ha->port_no == 1)
  720. ha->flt_region_fcp_prio = start;
  721. break;
  722. case FLT_REG_BOOT_CODE_82XX:
  723. ha->flt_region_boot = start;
  724. break;
  725. case FLT_REG_BOOT_CODE_8044:
  726. if (IS_QLA8044(ha))
  727. ha->flt_region_boot = start;
  728. break;
  729. case FLT_REG_FW_82XX:
  730. ha->flt_region_fw = start;
  731. break;
  732. case FLT_REG_CNA_FW:
  733. if (IS_CNA_CAPABLE(ha))
  734. ha->flt_region_fw = start;
  735. break;
  736. case FLT_REG_GOLD_FW_82XX:
  737. ha->flt_region_gold_fw = start;
  738. break;
  739. case FLT_REG_BOOTLOAD_82XX:
  740. ha->flt_region_bootload = start;
  741. break;
  742. case FLT_REG_VPD_8XXX:
  743. if (IS_CNA_CAPABLE(ha))
  744. ha->flt_region_vpd = start;
  745. break;
  746. case FLT_REG_FCOE_NVRAM_0:
  747. if (!(IS_QLA8031(ha) || IS_QLA8044(ha)))
  748. break;
  749. if (ha->port_no == 0)
  750. ha->flt_region_nvram = start;
  751. break;
  752. case FLT_REG_FCOE_NVRAM_1:
  753. if (!(IS_QLA8031(ha) || IS_QLA8044(ha)))
  754. break;
  755. if (ha->port_no == 1)
  756. ha->flt_region_nvram = start;
  757. break;
  758. case FLT_REG_IMG_PRI_27XX:
  759. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  760. ha->flt_region_img_status_pri = start;
  761. break;
  762. case FLT_REG_IMG_SEC_27XX:
  763. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  764. ha->flt_region_img_status_sec = start;
  765. break;
  766. case FLT_REG_FW_SEC_27XX:
  767. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  768. ha->flt_region_fw_sec = start;
  769. break;
  770. case FLT_REG_BOOTLOAD_SEC_27XX:
  771. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  772. ha->flt_region_boot_sec = start;
  773. break;
  774. case FLT_REG_AUX_IMG_PRI_28XX:
  775. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  776. ha->flt_region_aux_img_status_pri = start;
  777. break;
  778. case FLT_REG_AUX_IMG_SEC_28XX:
  779. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  780. ha->flt_region_aux_img_status_sec = start;
  781. break;
  782. case FLT_REG_NVRAM_SEC_28XX_0:
  783. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  784. if (ha->port_no == 0)
  785. ha->flt_region_nvram_sec = start;
  786. break;
  787. case FLT_REG_NVRAM_SEC_28XX_1:
  788. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  789. if (ha->port_no == 1)
  790. ha->flt_region_nvram_sec = start;
  791. break;
  792. case FLT_REG_NVRAM_SEC_28XX_2:
  793. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  794. if (ha->port_no == 2)
  795. ha->flt_region_nvram_sec = start;
  796. break;
  797. case FLT_REG_NVRAM_SEC_28XX_3:
  798. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  799. if (ha->port_no == 3)
  800. ha->flt_region_nvram_sec = start;
  801. break;
  802. case FLT_REG_VPD_SEC_27XX_0:
  803. case FLT_REG_VPD_SEC_28XX_0:
  804. if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
  805. ha->flt_region_vpd_nvram_sec = start;
  806. if (ha->port_no == 0)
  807. ha->flt_region_vpd_sec = start;
  808. }
  809. break;
  810. case FLT_REG_VPD_SEC_27XX_1:
  811. case FLT_REG_VPD_SEC_28XX_1:
  812. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  813. if (ha->port_no == 1)
  814. ha->flt_region_vpd_sec = start;
  815. break;
  816. case FLT_REG_VPD_SEC_27XX_2:
  817. case FLT_REG_VPD_SEC_28XX_2:
  818. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  819. if (ha->port_no == 2)
  820. ha->flt_region_vpd_sec = start;
  821. break;
  822. case FLT_REG_VPD_SEC_27XX_3:
  823. case FLT_REG_VPD_SEC_28XX_3:
  824. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  825. if (ha->port_no == 3)
  826. ha->flt_region_vpd_sec = start;
  827. break;
  828. }
  829. }
  830. goto done;
  831. no_flash_data:
  832. /* Use hardcoded defaults. */
  833. loc = locations[0];
  834. ha->flt_region_fw = def_fw[def];
  835. ha->flt_region_boot = def_boot[def];
  836. ha->flt_region_vpd_nvram = def_vpd_nvram[def];
  837. ha->flt_region_vpd = (ha->port_no == 0) ?
  838. def_vpd0[def] : def_vpd1[def];
  839. ha->flt_region_nvram = (ha->port_no == 0) ?
  840. def_nvram0[def] : def_nvram1[def];
  841. ha->flt_region_fdt = def_fdt[def];
  842. ha->flt_region_npiv_conf = (ha->port_no == 0) ?
  843. def_npiv_conf0[def] : def_npiv_conf1[def];
  844. done:
  845. ql_dbg(ql_dbg_init, vha, 0x004a,
  846. "FLT[%s]: boot=0x%x fw=0x%x vpd_nvram=0x%x vpd=0x%x nvram=0x%x "
  847. "fdt=0x%x flt=0x%x npiv=0x%x fcp_prif_cfg=0x%x.\n",
  848. loc, ha->flt_region_boot, ha->flt_region_fw,
  849. ha->flt_region_vpd_nvram, ha->flt_region_vpd, ha->flt_region_nvram,
  850. ha->flt_region_fdt, ha->flt_region_flt, ha->flt_region_npiv_conf,
  851. ha->flt_region_fcp_prio);
  852. }
  853. static void
  854. qla2xxx_get_fdt_info(scsi_qla_host_t *vha)
  855. {
  856. #define FLASH_BLK_SIZE_4K 0x1000
  857. #define FLASH_BLK_SIZE_32K 0x8000
  858. #define FLASH_BLK_SIZE_64K 0x10000
  859. const char *loc, *locations[] = { "MID", "FDT" };
  860. struct qla_hw_data *ha = vha->hw;
  861. struct req_que *req = ha->req_q_map[0];
  862. uint16_t cnt, chksum;
  863. __le16 *wptr = (__force __le16 *)req->ring;
  864. struct qla_fdt_layout *fdt = (struct qla_fdt_layout *)req->ring;
  865. uint8_t man_id, flash_id;
  866. uint16_t mid = 0, fid = 0;
  867. ha->isp_ops->read_optrom(vha, fdt, ha->flt_region_fdt << 2,
  868. OPTROM_BURST_DWORDS);
  869. if (le16_to_cpu(*wptr) == 0xffff)
  870. goto no_flash_data;
  871. if (memcmp(fdt->sig, "QLID", 4))
  872. goto no_flash_data;
  873. for (cnt = 0, chksum = 0; cnt < sizeof(*fdt) >> 1; cnt++, wptr++)
  874. chksum += le16_to_cpu(*wptr);
  875. if (chksum) {
  876. ql_dbg(ql_dbg_init, vha, 0x004c,
  877. "Inconsistent FDT detected:"
  878. " checksum=0x%x id=%c version0x%x.\n", chksum,
  879. fdt->sig[0], le16_to_cpu(fdt->version));
  880. ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x0113,
  881. fdt, sizeof(*fdt));
  882. goto no_flash_data;
  883. }
  884. loc = locations[1];
  885. mid = le16_to_cpu(fdt->man_id);
  886. fid = le16_to_cpu(fdt->id);
  887. ha->fdt_wrt_disable = fdt->wrt_disable_bits;
  888. ha->fdt_wrt_enable = fdt->wrt_enable_bits;
  889. ha->fdt_wrt_sts_reg_cmd = fdt->wrt_sts_reg_cmd;
  890. if (IS_QLA8044(ha))
  891. ha->fdt_erase_cmd = fdt->erase_cmd;
  892. else
  893. ha->fdt_erase_cmd =
  894. flash_conf_addr(ha, 0x0300 | fdt->erase_cmd);
  895. ha->fdt_block_size = le32_to_cpu(fdt->block_size);
  896. if (fdt->unprotect_sec_cmd) {
  897. ha->fdt_unprotect_sec_cmd = flash_conf_addr(ha, 0x0300 |
  898. fdt->unprotect_sec_cmd);
  899. ha->fdt_protect_sec_cmd = fdt->protect_sec_cmd ?
  900. flash_conf_addr(ha, 0x0300 | fdt->protect_sec_cmd) :
  901. flash_conf_addr(ha, 0x0336);
  902. }
  903. goto done;
  904. no_flash_data:
  905. loc = locations[0];
  906. if (IS_P3P_TYPE(ha)) {
  907. ha->fdt_block_size = FLASH_BLK_SIZE_64K;
  908. goto done;
  909. }
  910. qla24xx_get_flash_manufacturer(ha, &man_id, &flash_id);
  911. mid = man_id;
  912. fid = flash_id;
  913. ha->fdt_wrt_disable = 0x9c;
  914. ha->fdt_erase_cmd = flash_conf_addr(ha, 0x03d8);
  915. switch (man_id) {
  916. case 0xbf: /* STT flash. */
  917. if (flash_id == 0x8e)
  918. ha->fdt_block_size = FLASH_BLK_SIZE_64K;
  919. else
  920. ha->fdt_block_size = FLASH_BLK_SIZE_32K;
  921. if (flash_id == 0x80)
  922. ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0352);
  923. break;
  924. case 0x13: /* ST M25P80. */
  925. ha->fdt_block_size = FLASH_BLK_SIZE_64K;
  926. break;
  927. case 0x1f: /* Atmel 26DF081A. */
  928. ha->fdt_block_size = FLASH_BLK_SIZE_4K;
  929. ha->fdt_erase_cmd = flash_conf_addr(ha, 0x0320);
  930. ha->fdt_unprotect_sec_cmd = flash_conf_addr(ha, 0x0339);
  931. ha->fdt_protect_sec_cmd = flash_conf_addr(ha, 0x0336);
  932. break;
  933. default:
  934. /* Default to 64 kb sector size. */
  935. ha->fdt_block_size = FLASH_BLK_SIZE_64K;
  936. break;
  937. }
  938. done:
  939. ql_dbg(ql_dbg_init, vha, 0x004d,
  940. "FDT[%s]: (0x%x/0x%x) erase=0x%x "
  941. "pr=%x wrtd=0x%x blk=0x%x.\n",
  942. loc, mid, fid,
  943. ha->fdt_erase_cmd, ha->fdt_protect_sec_cmd,
  944. ha->fdt_wrt_disable, ha->fdt_block_size);
  945. }
  946. static void
  947. qla2xxx_get_idc_param(scsi_qla_host_t *vha)
  948. {
  949. #define QLA82XX_IDC_PARAM_ADDR 0x003e885c
  950. __le32 *wptr;
  951. struct qla_hw_data *ha = vha->hw;
  952. struct req_que *req = ha->req_q_map[0];
  953. if (!(IS_P3P_TYPE(ha)))
  954. return;
  955. wptr = (__force __le32 *)req->ring;
  956. ha->isp_ops->read_optrom(vha, req->ring, QLA82XX_IDC_PARAM_ADDR, 8);
  957. if (*wptr == cpu_to_le32(0xffffffff)) {
  958. ha->fcoe_dev_init_timeout = QLA82XX_ROM_DEV_INIT_TIMEOUT;
  959. ha->fcoe_reset_timeout = QLA82XX_ROM_DRV_RESET_ACK_TIMEOUT;
  960. } else {
  961. ha->fcoe_dev_init_timeout = le32_to_cpu(*wptr);
  962. wptr++;
  963. ha->fcoe_reset_timeout = le32_to_cpu(*wptr);
  964. }
  965. ql_dbg(ql_dbg_init, vha, 0x004e,
  966. "fcoe_dev_init_timeout=%d "
  967. "fcoe_reset_timeout=%d.\n", ha->fcoe_dev_init_timeout,
  968. ha->fcoe_reset_timeout);
  969. return;
  970. }
  971. int
  972. qla2xxx_get_flash_info(scsi_qla_host_t *vha)
  973. {
  974. int ret;
  975. uint32_t flt_addr;
  976. struct qla_hw_data *ha = vha->hw;
  977. if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
  978. !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha) &&
  979. !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
  980. return QLA_SUCCESS;
  981. ret = qla2xxx_find_flt_start(vha, &flt_addr);
  982. if (ret != QLA_SUCCESS)
  983. return ret;
  984. qla2xxx_get_flt_info(vha, flt_addr);
  985. qla2xxx_get_fdt_info(vha);
  986. qla2xxx_get_idc_param(vha);
  987. return QLA_SUCCESS;
  988. }
  989. void
  990. qla2xxx_flash_npiv_conf(scsi_qla_host_t *vha)
  991. {
  992. #define NPIV_CONFIG_SIZE (16*1024)
  993. void *data;
  994. __le16 *wptr;
  995. uint16_t cnt, chksum;
  996. int i;
  997. struct qla_npiv_header hdr;
  998. struct qla_npiv_entry *entry;
  999. struct qla_hw_data *ha = vha->hw;
  1000. if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha) &&
  1001. !IS_CNA_CAPABLE(ha) && !IS_QLA2031(ha))
  1002. return;
  1003. if (ha->flags.nic_core_reset_hdlr_active)
  1004. return;
  1005. if (IS_QLA8044(ha))
  1006. return;
  1007. ha->isp_ops->read_optrom(vha, &hdr, ha->flt_region_npiv_conf << 2,
  1008. sizeof(struct qla_npiv_header));
  1009. if (hdr.version == cpu_to_le16(0xffff))
  1010. return;
  1011. if (hdr.version != cpu_to_le16(1)) {
  1012. ql_dbg(ql_dbg_user, vha, 0x7090,
  1013. "Unsupported NPIV-Config "
  1014. "detected: version=0x%x entries=0x%x checksum=0x%x.\n",
  1015. le16_to_cpu(hdr.version), le16_to_cpu(hdr.entries),
  1016. le16_to_cpu(hdr.checksum));
  1017. return;
  1018. }
  1019. data = kmalloc(NPIV_CONFIG_SIZE, GFP_KERNEL);
  1020. if (!data) {
  1021. ql_log(ql_log_warn, vha, 0x7091,
  1022. "Unable to allocate memory for data.\n");
  1023. return;
  1024. }
  1025. ha->isp_ops->read_optrom(vha, data, ha->flt_region_npiv_conf << 2,
  1026. NPIV_CONFIG_SIZE);
  1027. cnt = (sizeof(hdr) + le16_to_cpu(hdr.entries) * sizeof(*entry)) >> 1;
  1028. for (wptr = data, chksum = 0; cnt--; wptr++)
  1029. chksum += le16_to_cpu(*wptr);
  1030. if (chksum) {
  1031. ql_dbg(ql_dbg_user, vha, 0x7092,
  1032. "Inconsistent NPIV-Config "
  1033. "detected: version=0x%x entries=0x%x checksum=0x%x.\n",
  1034. le16_to_cpu(hdr.version), le16_to_cpu(hdr.entries),
  1035. le16_to_cpu(hdr.checksum));
  1036. goto done;
  1037. }
  1038. entry = data + sizeof(struct qla_npiv_header);
  1039. cnt = le16_to_cpu(hdr.entries);
  1040. for (i = 0; cnt; cnt--, entry++, i++) {
  1041. uint16_t flags;
  1042. struct fc_vport_identifiers vid;
  1043. struct fc_vport *vport;
  1044. memcpy(&ha->npiv_info[i], entry, sizeof(struct qla_npiv_entry));
  1045. flags = le16_to_cpu(entry->flags);
  1046. if (flags == 0xffff)
  1047. continue;
  1048. if ((flags & BIT_0) == 0)
  1049. continue;
  1050. memset(&vid, 0, sizeof(vid));
  1051. vid.roles = FC_PORT_ROLE_FCP_INITIATOR;
  1052. vid.vport_type = FC_PORTTYPE_NPIV;
  1053. vid.disable = false;
  1054. vid.port_name = wwn_to_u64(entry->port_name);
  1055. vid.node_name = wwn_to_u64(entry->node_name);
  1056. ql_dbg(ql_dbg_user, vha, 0x7093,
  1057. "NPIV[%02x]: wwpn=%llx wwnn=%llx vf_id=%#x Q_qos=%#x F_qos=%#x.\n",
  1058. cnt, vid.port_name, vid.node_name,
  1059. le16_to_cpu(entry->vf_id),
  1060. entry->q_qos, entry->f_qos);
  1061. if (i < QLA_PRECONFIG_VPORTS) {
  1062. vport = fc_vport_create(vha->host, 0, &vid);
  1063. if (!vport)
  1064. ql_log(ql_log_warn, vha, 0x7094,
  1065. "NPIV-Config Failed to create vport [%02x]: wwpn=%llx wwnn=%llx.\n",
  1066. cnt, vid.port_name, vid.node_name);
  1067. }
  1068. }
  1069. done:
  1070. kfree(data);
  1071. }
  1072. static int
  1073. qla24xx_unprotect_flash(scsi_qla_host_t *vha)
  1074. {
  1075. struct qla_hw_data *ha = vha->hw;
  1076. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1077. if (ha->flags.fac_supported)
  1078. return qla81xx_fac_do_write_enable(vha, 1);
  1079. /* Enable flash write. */
  1080. wrt_reg_dword(&reg->ctrl_status,
  1081. rd_reg_dword(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  1082. rd_reg_dword(&reg->ctrl_status); /* PCI Posting. */
  1083. if (!ha->fdt_wrt_disable)
  1084. goto done;
  1085. /* Disable flash write-protection, first clear SR protection bit */
  1086. qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
  1087. /* Then write zero again to clear remaining SR bits.*/
  1088. qla24xx_write_flash_dword(ha, flash_conf_addr(ha, 0x101), 0);
  1089. done:
  1090. return QLA_SUCCESS;
  1091. }
  1092. static int
  1093. qla24xx_protect_flash(scsi_qla_host_t *vha)
  1094. {
  1095. struct qla_hw_data *ha = vha->hw;
  1096. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1097. ulong cnt = 300;
  1098. uint32_t faddr, dword;
  1099. if (ha->flags.fac_supported)
  1100. return qla81xx_fac_do_write_enable(vha, 0);
  1101. if (!ha->fdt_wrt_disable)
  1102. goto skip_wrt_protect;
  1103. /* Enable flash write-protection and wait for completion. */
  1104. faddr = flash_conf_addr(ha, 0x101);
  1105. qla24xx_write_flash_dword(ha, faddr, ha->fdt_wrt_disable);
  1106. faddr = flash_conf_addr(ha, 0x5);
  1107. while (cnt--) {
  1108. if (!qla24xx_read_flash_dword(ha, faddr, &dword)) {
  1109. if (!(dword & BIT_0))
  1110. break;
  1111. }
  1112. udelay(10);
  1113. }
  1114. skip_wrt_protect:
  1115. /* Disable flash write. */
  1116. wrt_reg_dword(&reg->ctrl_status,
  1117. rd_reg_dword(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  1118. return QLA_SUCCESS;
  1119. }
  1120. static int
  1121. qla24xx_erase_sector(scsi_qla_host_t *vha, uint32_t fdata)
  1122. {
  1123. struct qla_hw_data *ha = vha->hw;
  1124. uint32_t start, finish;
  1125. if (ha->flags.fac_supported) {
  1126. start = fdata >> 2;
  1127. finish = start + (ha->fdt_block_size >> 2) - 1;
  1128. return qla81xx_fac_erase_sector(vha, flash_data_addr(ha,
  1129. start), flash_data_addr(ha, finish));
  1130. }
  1131. return qla24xx_write_flash_dword(ha, ha->fdt_erase_cmd,
  1132. (fdata & 0xff00) | ((fdata << 16) & 0xff0000) |
  1133. ((fdata >> 16) & 0xff));
  1134. }
  1135. static int
  1136. qla24xx_write_flash_data(scsi_qla_host_t *vha, __le32 *dwptr, uint32_t faddr,
  1137. uint32_t dwords)
  1138. {
  1139. int ret;
  1140. ulong liter;
  1141. ulong dburst = OPTROM_BURST_DWORDS; /* burst size in dwords */
  1142. uint32_t sec_mask, rest_addr, fdata;
  1143. dma_addr_t optrom_dma;
  1144. void *optrom = NULL;
  1145. struct qla_hw_data *ha = vha->hw;
  1146. if (!IS_QLA25XX(ha) && !IS_QLA81XX(ha) && !IS_QLA83XX(ha) &&
  1147. !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
  1148. goto next;
  1149. /* Allocate dma buffer for burst write */
  1150. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  1151. &optrom_dma, GFP_KERNEL);
  1152. if (!optrom) {
  1153. ql_log(ql_log_warn, vha, 0x7095,
  1154. "Failed allocate burst (%x bytes)\n", OPTROM_BURST_SIZE);
  1155. }
  1156. next:
  1157. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0x7095,
  1158. "Unprotect flash...\n");
  1159. ret = qla24xx_unprotect_flash(vha);
  1160. if (ret) {
  1161. ql_log(ql_log_warn, vha, 0x7096,
  1162. "Failed to unprotect flash.\n");
  1163. goto done;
  1164. }
  1165. rest_addr = (ha->fdt_block_size >> 2) - 1;
  1166. sec_mask = ~rest_addr;
  1167. for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
  1168. fdata = (faddr & sec_mask) << 2;
  1169. /* Are we at the beginning of a sector? */
  1170. if (!(faddr & rest_addr)) {
  1171. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0x7095,
  1172. "Erase sector %#x...\n", faddr);
  1173. ret = qla24xx_erase_sector(vha, fdata);
  1174. if (ret) {
  1175. ql_dbg(ql_dbg_user, vha, 0x7007,
  1176. "Failed to erase sector %x.\n", faddr);
  1177. break;
  1178. }
  1179. }
  1180. if (optrom) {
  1181. /* If smaller than a burst remaining */
  1182. if (dwords - liter < dburst)
  1183. dburst = dwords - liter;
  1184. /* Copy to dma buffer */
  1185. memcpy(optrom, dwptr, dburst << 2);
  1186. /* Burst write */
  1187. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0x7095,
  1188. "Write burst (%#lx dwords)...\n", dburst);
  1189. ret = qla2x00_load_ram(vha, optrom_dma,
  1190. flash_data_addr(ha, faddr), dburst);
  1191. if (!ret) {
  1192. liter += dburst - 1;
  1193. faddr += dburst - 1;
  1194. dwptr += dburst - 1;
  1195. continue;
  1196. }
  1197. ql_log(ql_log_warn, vha, 0x7097,
  1198. "Failed burst-write at %x (%p/%#llx)....\n",
  1199. flash_data_addr(ha, faddr), optrom,
  1200. (u64)optrom_dma);
  1201. dma_free_coherent(&ha->pdev->dev,
  1202. OPTROM_BURST_SIZE, optrom, optrom_dma);
  1203. optrom = NULL;
  1204. if (IS_QLA27XX(ha) || IS_QLA28XX(ha))
  1205. break;
  1206. ql_log(ql_log_warn, vha, 0x7098,
  1207. "Reverting to slow write...\n");
  1208. }
  1209. /* Slow write */
  1210. ret = qla24xx_write_flash_dword(ha,
  1211. flash_data_addr(ha, faddr), le32_to_cpu(*dwptr));
  1212. if (ret) {
  1213. ql_dbg(ql_dbg_user, vha, 0x7006,
  1214. "Failed slow write %x (%x)\n", faddr, *dwptr);
  1215. break;
  1216. }
  1217. }
  1218. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0x7095,
  1219. "Protect flash...\n");
  1220. ret = qla24xx_protect_flash(vha);
  1221. if (ret)
  1222. ql_log(ql_log_warn, vha, 0x7099,
  1223. "Failed to protect flash\n");
  1224. done:
  1225. if (optrom)
  1226. dma_free_coherent(&ha->pdev->dev,
  1227. OPTROM_BURST_SIZE, optrom, optrom_dma);
  1228. return ret;
  1229. }
  1230. uint8_t *
  1231. qla2x00_read_nvram_data(scsi_qla_host_t *vha, void *buf, uint32_t naddr,
  1232. uint32_t bytes)
  1233. {
  1234. uint32_t i;
  1235. __le16 *wptr;
  1236. struct qla_hw_data *ha = vha->hw;
  1237. /* Word reads to NVRAM via registers. */
  1238. wptr = buf;
  1239. qla2x00_lock_nvram_access(ha);
  1240. for (i = 0; i < bytes >> 1; i++, naddr++)
  1241. wptr[i] = cpu_to_le16(qla2x00_get_nvram_word(ha,
  1242. naddr));
  1243. qla2x00_unlock_nvram_access(ha);
  1244. return buf;
  1245. }
  1246. uint8_t *
  1247. qla24xx_read_nvram_data(scsi_qla_host_t *vha, void *buf, uint32_t naddr,
  1248. uint32_t bytes)
  1249. {
  1250. struct qla_hw_data *ha = vha->hw;
  1251. uint32_t *dwptr = buf;
  1252. uint32_t i;
  1253. if (IS_P3P_TYPE(ha))
  1254. return buf;
  1255. /* Dword reads to flash. */
  1256. naddr = nvram_data_addr(ha, naddr);
  1257. bytes >>= 2;
  1258. for (i = 0; i < bytes; i++, naddr++, dwptr++) {
  1259. if (qla24xx_read_flash_dword(ha, naddr, dwptr))
  1260. break;
  1261. cpu_to_le32s(dwptr);
  1262. }
  1263. return buf;
  1264. }
  1265. int
  1266. qla2x00_write_nvram_data(scsi_qla_host_t *vha, void *buf, uint32_t naddr,
  1267. uint32_t bytes)
  1268. {
  1269. int ret, stat;
  1270. uint32_t i;
  1271. uint16_t *wptr;
  1272. unsigned long flags;
  1273. struct qla_hw_data *ha = vha->hw;
  1274. ret = QLA_SUCCESS;
  1275. spin_lock_irqsave(&ha->hardware_lock, flags);
  1276. qla2x00_lock_nvram_access(ha);
  1277. /* Disable NVRAM write-protection. */
  1278. stat = qla2x00_clear_nvram_protection(ha);
  1279. wptr = (uint16_t *)buf;
  1280. for (i = 0; i < bytes >> 1; i++, naddr++) {
  1281. qla2x00_write_nvram_word(ha, naddr,
  1282. cpu_to_le16(*wptr));
  1283. wptr++;
  1284. }
  1285. /* Enable NVRAM write-protection. */
  1286. qla2x00_set_nvram_protection(ha, stat);
  1287. qla2x00_unlock_nvram_access(ha);
  1288. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1289. return ret;
  1290. }
  1291. int
  1292. qla24xx_write_nvram_data(scsi_qla_host_t *vha, void *buf, uint32_t naddr,
  1293. uint32_t bytes)
  1294. {
  1295. struct qla_hw_data *ha = vha->hw;
  1296. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1297. __le32 *dwptr = buf;
  1298. uint32_t i;
  1299. int ret;
  1300. ret = QLA_SUCCESS;
  1301. if (IS_P3P_TYPE(ha))
  1302. return ret;
  1303. /* Enable flash write. */
  1304. wrt_reg_dword(&reg->ctrl_status,
  1305. rd_reg_dword(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  1306. rd_reg_dword(&reg->ctrl_status); /* PCI Posting. */
  1307. /* Disable NVRAM write-protection. */
  1308. qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
  1309. qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0);
  1310. /* Dword writes to flash. */
  1311. naddr = nvram_data_addr(ha, naddr);
  1312. bytes >>= 2;
  1313. for (i = 0; i < bytes; i++, naddr++, dwptr++) {
  1314. if (qla24xx_write_flash_dword(ha, naddr, le32_to_cpu(*dwptr))) {
  1315. ql_dbg(ql_dbg_user, vha, 0x709a,
  1316. "Unable to program nvram address=%x data=%x.\n",
  1317. naddr, *dwptr);
  1318. break;
  1319. }
  1320. }
  1321. /* Enable NVRAM write-protection. */
  1322. qla24xx_write_flash_dword(ha, nvram_conf_addr(ha, 0x101), 0x8c);
  1323. /* Disable flash write. */
  1324. wrt_reg_dword(&reg->ctrl_status,
  1325. rd_reg_dword(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  1326. rd_reg_dword(&reg->ctrl_status); /* PCI Posting. */
  1327. return ret;
  1328. }
  1329. uint8_t *
  1330. qla25xx_read_nvram_data(scsi_qla_host_t *vha, void *buf, uint32_t naddr,
  1331. uint32_t bytes)
  1332. {
  1333. struct qla_hw_data *ha = vha->hw;
  1334. uint32_t *dwptr = buf;
  1335. uint32_t i;
  1336. /* Dword reads to flash. */
  1337. naddr = flash_data_addr(ha, ha->flt_region_vpd_nvram | naddr);
  1338. bytes >>= 2;
  1339. for (i = 0; i < bytes; i++, naddr++, dwptr++) {
  1340. if (qla24xx_read_flash_dword(ha, naddr, dwptr))
  1341. break;
  1342. cpu_to_le32s(dwptr);
  1343. }
  1344. return buf;
  1345. }
  1346. #define RMW_BUFFER_SIZE (64 * 1024)
  1347. int
  1348. qla25xx_write_nvram_data(scsi_qla_host_t *vha, void *buf, uint32_t naddr,
  1349. uint32_t bytes)
  1350. {
  1351. struct qla_hw_data *ha = vha->hw;
  1352. uint8_t *dbuf = vmalloc(RMW_BUFFER_SIZE);
  1353. if (!dbuf)
  1354. return QLA_MEMORY_ALLOC_FAILED;
  1355. ha->isp_ops->read_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
  1356. RMW_BUFFER_SIZE);
  1357. memcpy(dbuf + (naddr << 2), buf, bytes);
  1358. ha->isp_ops->write_optrom(vha, dbuf, ha->flt_region_vpd_nvram << 2,
  1359. RMW_BUFFER_SIZE);
  1360. vfree(dbuf);
  1361. return QLA_SUCCESS;
  1362. }
  1363. static inline void
  1364. qla2x00_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
  1365. {
  1366. if (IS_QLA2322(ha)) {
  1367. /* Flip all colors. */
  1368. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  1369. /* Turn off. */
  1370. ha->beacon_color_state = 0;
  1371. *pflags = GPIO_LED_ALL_OFF;
  1372. } else {
  1373. /* Turn on. */
  1374. ha->beacon_color_state = QLA_LED_ALL_ON;
  1375. *pflags = GPIO_LED_RGA_ON;
  1376. }
  1377. } else {
  1378. /* Flip green led only. */
  1379. if (ha->beacon_color_state == QLA_LED_GRN_ON) {
  1380. /* Turn off. */
  1381. ha->beacon_color_state = 0;
  1382. *pflags = GPIO_LED_GREEN_OFF_AMBER_OFF;
  1383. } else {
  1384. /* Turn on. */
  1385. ha->beacon_color_state = QLA_LED_GRN_ON;
  1386. *pflags = GPIO_LED_GREEN_ON_AMBER_OFF;
  1387. }
  1388. }
  1389. }
  1390. #define PIO_REG(h, r) ((h)->pio_address + offsetof(struct device_reg_2xxx, r))
  1391. void
  1392. qla2x00_beacon_blink(struct scsi_qla_host *vha)
  1393. {
  1394. uint16_t gpio_enable;
  1395. uint16_t gpio_data;
  1396. uint16_t led_color = 0;
  1397. unsigned long flags;
  1398. struct qla_hw_data *ha = vha->hw;
  1399. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1400. if (IS_P3P_TYPE(ha))
  1401. return;
  1402. spin_lock_irqsave(&ha->hardware_lock, flags);
  1403. /* Save the Original GPIOE. */
  1404. if (ha->pio_address) {
  1405. gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
  1406. gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
  1407. } else {
  1408. gpio_enable = rd_reg_word(&reg->gpioe);
  1409. gpio_data = rd_reg_word(&reg->gpiod);
  1410. }
  1411. /* Set the modified gpio_enable values */
  1412. gpio_enable |= GPIO_LED_MASK;
  1413. if (ha->pio_address) {
  1414. WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
  1415. } else {
  1416. wrt_reg_word(&reg->gpioe, gpio_enable);
  1417. rd_reg_word(&reg->gpioe);
  1418. }
  1419. qla2x00_flip_colors(ha, &led_color);
  1420. /* Clear out any previously set LED color. */
  1421. gpio_data &= ~GPIO_LED_MASK;
  1422. /* Set the new input LED color to GPIOD. */
  1423. gpio_data |= led_color;
  1424. /* Set the modified gpio_data values */
  1425. if (ha->pio_address) {
  1426. WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
  1427. } else {
  1428. wrt_reg_word(&reg->gpiod, gpio_data);
  1429. rd_reg_word(&reg->gpiod);
  1430. }
  1431. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1432. }
  1433. int
  1434. qla2x00_beacon_on(struct scsi_qla_host *vha)
  1435. {
  1436. uint16_t gpio_enable;
  1437. uint16_t gpio_data;
  1438. unsigned long flags;
  1439. struct qla_hw_data *ha = vha->hw;
  1440. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1441. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  1442. ha->fw_options[1] |= FO1_DISABLE_GPIO6_7;
  1443. if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
  1444. ql_log(ql_log_warn, vha, 0x709b,
  1445. "Unable to update fw options (beacon on).\n");
  1446. return QLA_FUNCTION_FAILED;
  1447. }
  1448. /* Turn off LEDs. */
  1449. spin_lock_irqsave(&ha->hardware_lock, flags);
  1450. if (ha->pio_address) {
  1451. gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
  1452. gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
  1453. } else {
  1454. gpio_enable = rd_reg_word(&reg->gpioe);
  1455. gpio_data = rd_reg_word(&reg->gpiod);
  1456. }
  1457. gpio_enable |= GPIO_LED_MASK;
  1458. /* Set the modified gpio_enable values. */
  1459. if (ha->pio_address) {
  1460. WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
  1461. } else {
  1462. wrt_reg_word(&reg->gpioe, gpio_enable);
  1463. rd_reg_word(&reg->gpioe);
  1464. }
  1465. /* Clear out previously set LED colour. */
  1466. gpio_data &= ~GPIO_LED_MASK;
  1467. if (ha->pio_address) {
  1468. WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
  1469. } else {
  1470. wrt_reg_word(&reg->gpiod, gpio_data);
  1471. rd_reg_word(&reg->gpiod);
  1472. }
  1473. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1474. /*
  1475. * Let the per HBA timer kick off the blinking process based on
  1476. * the following flags. No need to do anything else now.
  1477. */
  1478. ha->beacon_blink_led = 1;
  1479. ha->beacon_color_state = 0;
  1480. return QLA_SUCCESS;
  1481. }
  1482. int
  1483. qla2x00_beacon_off(struct scsi_qla_host *vha)
  1484. {
  1485. int rval = QLA_SUCCESS;
  1486. struct qla_hw_data *ha = vha->hw;
  1487. ha->beacon_blink_led = 0;
  1488. /* Set the on flag so when it gets flipped it will be off. */
  1489. if (IS_QLA2322(ha))
  1490. ha->beacon_color_state = QLA_LED_ALL_ON;
  1491. else
  1492. ha->beacon_color_state = QLA_LED_GRN_ON;
  1493. ha->isp_ops->beacon_blink(vha); /* This turns green LED off */
  1494. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  1495. ha->fw_options[1] &= ~FO1_DISABLE_GPIO6_7;
  1496. rval = qla2x00_set_fw_options(vha, ha->fw_options);
  1497. if (rval != QLA_SUCCESS)
  1498. ql_log(ql_log_warn, vha, 0x709c,
  1499. "Unable to update fw options (beacon off).\n");
  1500. return rval;
  1501. }
  1502. static inline void
  1503. qla24xx_flip_colors(struct qla_hw_data *ha, uint16_t *pflags)
  1504. {
  1505. /* Flip all colors. */
  1506. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  1507. /* Turn off. */
  1508. ha->beacon_color_state = 0;
  1509. *pflags = 0;
  1510. } else {
  1511. /* Turn on. */
  1512. ha->beacon_color_state = QLA_LED_ALL_ON;
  1513. *pflags = GPDX_LED_YELLOW_ON | GPDX_LED_AMBER_ON;
  1514. }
  1515. }
  1516. void
  1517. qla24xx_beacon_blink(struct scsi_qla_host *vha)
  1518. {
  1519. uint16_t led_color = 0;
  1520. uint32_t gpio_data;
  1521. unsigned long flags;
  1522. struct qla_hw_data *ha = vha->hw;
  1523. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1524. /* Save the Original GPIOD. */
  1525. spin_lock_irqsave(&ha->hardware_lock, flags);
  1526. gpio_data = rd_reg_dword(&reg->gpiod);
  1527. /* Enable the gpio_data reg for update. */
  1528. gpio_data |= GPDX_LED_UPDATE_MASK;
  1529. wrt_reg_dword(&reg->gpiod, gpio_data);
  1530. gpio_data = rd_reg_dword(&reg->gpiod);
  1531. /* Set the color bits. */
  1532. qla24xx_flip_colors(ha, &led_color);
  1533. /* Clear out any previously set LED color. */
  1534. gpio_data &= ~GPDX_LED_COLOR_MASK;
  1535. /* Set the new input LED color to GPIOD. */
  1536. gpio_data |= led_color;
  1537. /* Set the modified gpio_data values. */
  1538. wrt_reg_dword(&reg->gpiod, gpio_data);
  1539. gpio_data = rd_reg_dword(&reg->gpiod);
  1540. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1541. }
  1542. static uint32_t
  1543. qla83xx_select_led_port(struct qla_hw_data *ha)
  1544. {
  1545. uint32_t led_select_value = 0;
  1546. if (!IS_QLA83XX(ha) && !IS_QLA27XX(ha) && !IS_QLA28XX(ha))
  1547. goto out;
  1548. if (ha->port_no == 0)
  1549. led_select_value = QLA83XX_LED_PORT0;
  1550. else
  1551. led_select_value = QLA83XX_LED_PORT1;
  1552. out:
  1553. return led_select_value;
  1554. }
  1555. void
  1556. qla83xx_beacon_blink(struct scsi_qla_host *vha)
  1557. {
  1558. uint32_t led_select_value;
  1559. struct qla_hw_data *ha = vha->hw;
  1560. uint16_t led_cfg[6];
  1561. uint16_t orig_led_cfg[6];
  1562. uint32_t led_10_value, led_43_value;
  1563. if (!IS_QLA83XX(ha) && !IS_QLA81XX(ha) && !IS_QLA27XX(ha) &&
  1564. !IS_QLA28XX(ha))
  1565. return;
  1566. if (!ha->beacon_blink_led)
  1567. return;
  1568. if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
  1569. qla2x00_write_ram_word(vha, 0x1003, 0x40000230);
  1570. qla2x00_write_ram_word(vha, 0x1004, 0x40000230);
  1571. } else if (IS_QLA2031(ha)) {
  1572. led_select_value = qla83xx_select_led_port(ha);
  1573. qla83xx_wr_reg(vha, led_select_value, 0x40000230);
  1574. qla83xx_wr_reg(vha, led_select_value + 4, 0x40000230);
  1575. } else if (IS_QLA8031(ha)) {
  1576. led_select_value = qla83xx_select_led_port(ha);
  1577. qla83xx_rd_reg(vha, led_select_value, &led_10_value);
  1578. qla83xx_rd_reg(vha, led_select_value + 0x10, &led_43_value);
  1579. qla83xx_wr_reg(vha, led_select_value, 0x01f44000);
  1580. msleep(500);
  1581. qla83xx_wr_reg(vha, led_select_value, 0x400001f4);
  1582. msleep(1000);
  1583. qla83xx_wr_reg(vha, led_select_value, led_10_value);
  1584. qla83xx_wr_reg(vha, led_select_value + 0x10, led_43_value);
  1585. } else if (IS_QLA81XX(ha)) {
  1586. int rval;
  1587. /* Save Current */
  1588. rval = qla81xx_get_led_config(vha, orig_led_cfg);
  1589. /* Do the blink */
  1590. if (rval == QLA_SUCCESS) {
  1591. if (IS_QLA81XX(ha)) {
  1592. led_cfg[0] = 0x4000;
  1593. led_cfg[1] = 0x2000;
  1594. led_cfg[2] = 0;
  1595. led_cfg[3] = 0;
  1596. led_cfg[4] = 0;
  1597. led_cfg[5] = 0;
  1598. } else {
  1599. led_cfg[0] = 0x4000;
  1600. led_cfg[1] = 0x4000;
  1601. led_cfg[2] = 0x4000;
  1602. led_cfg[3] = 0x2000;
  1603. led_cfg[4] = 0;
  1604. led_cfg[5] = 0x2000;
  1605. }
  1606. rval = qla81xx_set_led_config(vha, led_cfg);
  1607. msleep(1000);
  1608. if (IS_QLA81XX(ha)) {
  1609. led_cfg[0] = 0x4000;
  1610. led_cfg[1] = 0x2000;
  1611. led_cfg[2] = 0;
  1612. } else {
  1613. led_cfg[0] = 0x4000;
  1614. led_cfg[1] = 0x2000;
  1615. led_cfg[2] = 0x4000;
  1616. led_cfg[3] = 0x4000;
  1617. led_cfg[4] = 0;
  1618. led_cfg[5] = 0x2000;
  1619. }
  1620. rval = qla81xx_set_led_config(vha, led_cfg);
  1621. }
  1622. /* On exit, restore original (presumes no status change) */
  1623. qla81xx_set_led_config(vha, orig_led_cfg);
  1624. }
  1625. }
  1626. int
  1627. qla24xx_beacon_on(struct scsi_qla_host *vha)
  1628. {
  1629. uint32_t gpio_data;
  1630. unsigned long flags;
  1631. struct qla_hw_data *ha = vha->hw;
  1632. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1633. if (IS_P3P_TYPE(ha))
  1634. return QLA_SUCCESS;
  1635. if (IS_QLA8031(ha) || IS_QLA81XX(ha))
  1636. goto skip_gpio; /* let blink handle it */
  1637. if (ha->beacon_blink_led == 0) {
  1638. /* Enable firmware for update */
  1639. ha->fw_options[1] |= ADD_FO1_DISABLE_GPIO_LED_CTRL;
  1640. if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS)
  1641. return QLA_FUNCTION_FAILED;
  1642. if (qla2x00_get_fw_options(vha, ha->fw_options) !=
  1643. QLA_SUCCESS) {
  1644. ql_log(ql_log_warn, vha, 0x7009,
  1645. "Unable to update fw options (beacon on).\n");
  1646. return QLA_FUNCTION_FAILED;
  1647. }
  1648. if (IS_QLA2031(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha))
  1649. goto skip_gpio;
  1650. spin_lock_irqsave(&ha->hardware_lock, flags);
  1651. gpio_data = rd_reg_dword(&reg->gpiod);
  1652. /* Enable the gpio_data reg for update. */
  1653. gpio_data |= GPDX_LED_UPDATE_MASK;
  1654. wrt_reg_dword(&reg->gpiod, gpio_data);
  1655. rd_reg_dword(&reg->gpiod);
  1656. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1657. }
  1658. /* So all colors blink together. */
  1659. ha->beacon_color_state = 0;
  1660. skip_gpio:
  1661. /* Let the per HBA timer kick off the blinking process. */
  1662. ha->beacon_blink_led = 1;
  1663. return QLA_SUCCESS;
  1664. }
  1665. int
  1666. qla24xx_beacon_off(struct scsi_qla_host *vha)
  1667. {
  1668. uint32_t gpio_data;
  1669. unsigned long flags;
  1670. struct qla_hw_data *ha = vha->hw;
  1671. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1672. if (IS_P3P_TYPE(ha))
  1673. return QLA_SUCCESS;
  1674. if (!ha->flags.fw_started)
  1675. return QLA_SUCCESS;
  1676. ha->beacon_blink_led = 0;
  1677. if (IS_QLA2031(ha) || IS_QLA27XX(ha) || IS_QLA28XX(ha))
  1678. goto set_fw_options;
  1679. if (IS_QLA8031(ha) || IS_QLA81XX(ha))
  1680. return QLA_SUCCESS;
  1681. ha->beacon_color_state = QLA_LED_ALL_ON;
  1682. ha->isp_ops->beacon_blink(vha); /* Will flip to all off. */
  1683. /* Give control back to firmware. */
  1684. spin_lock_irqsave(&ha->hardware_lock, flags);
  1685. gpio_data = rd_reg_dword(&reg->gpiod);
  1686. /* Disable the gpio_data reg for update. */
  1687. gpio_data &= ~GPDX_LED_UPDATE_MASK;
  1688. wrt_reg_dword(&reg->gpiod, gpio_data);
  1689. rd_reg_dword(&reg->gpiod);
  1690. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1691. set_fw_options:
  1692. ha->fw_options[1] &= ~ADD_FO1_DISABLE_GPIO_LED_CTRL;
  1693. if (qla2x00_set_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
  1694. ql_log(ql_log_warn, vha, 0x704d,
  1695. "Unable to update fw options (beacon on).\n");
  1696. return QLA_FUNCTION_FAILED;
  1697. }
  1698. if (qla2x00_get_fw_options(vha, ha->fw_options) != QLA_SUCCESS) {
  1699. ql_log(ql_log_warn, vha, 0x704e,
  1700. "Unable to update fw options (beacon on).\n");
  1701. return QLA_FUNCTION_FAILED;
  1702. }
  1703. return QLA_SUCCESS;
  1704. }
  1705. /*
  1706. * Flash support routines
  1707. */
  1708. /**
  1709. * qla2x00_flash_enable() - Setup flash for reading and writing.
  1710. * @ha: HA context
  1711. */
  1712. static void
  1713. qla2x00_flash_enable(struct qla_hw_data *ha)
  1714. {
  1715. uint16_t data;
  1716. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1717. data = rd_reg_word(&reg->ctrl_status);
  1718. data |= CSR_FLASH_ENABLE;
  1719. wrt_reg_word(&reg->ctrl_status, data);
  1720. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1721. }
  1722. /**
  1723. * qla2x00_flash_disable() - Disable flash and allow RISC to run.
  1724. * @ha: HA context
  1725. */
  1726. static void
  1727. qla2x00_flash_disable(struct qla_hw_data *ha)
  1728. {
  1729. uint16_t data;
  1730. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1731. data = rd_reg_word(&reg->ctrl_status);
  1732. data &= ~(CSR_FLASH_ENABLE);
  1733. wrt_reg_word(&reg->ctrl_status, data);
  1734. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1735. }
  1736. /**
  1737. * qla2x00_read_flash_byte() - Reads a byte from flash
  1738. * @ha: HA context
  1739. * @addr: Address in flash to read
  1740. *
  1741. * A word is read from the chip, but, only the lower byte is valid.
  1742. *
  1743. * Returns the byte read from flash @addr.
  1744. */
  1745. static uint8_t
  1746. qla2x00_read_flash_byte(struct qla_hw_data *ha, uint32_t addr)
  1747. {
  1748. uint16_t data;
  1749. uint16_t bank_select;
  1750. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1751. bank_select = rd_reg_word(&reg->ctrl_status);
  1752. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1753. /* Specify 64K address range: */
  1754. /* clear out Module Select and Flash Address bits [19:16]. */
  1755. bank_select &= ~0xf8;
  1756. bank_select |= addr >> 12 & 0xf0;
  1757. bank_select |= CSR_FLASH_64K_BANK;
  1758. wrt_reg_word(&reg->ctrl_status, bank_select);
  1759. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1760. wrt_reg_word(&reg->flash_address, (uint16_t)addr);
  1761. data = rd_reg_word(&reg->flash_data);
  1762. return (uint8_t)data;
  1763. }
  1764. /* Setup bit 16 of flash address. */
  1765. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1766. bank_select |= CSR_FLASH_64K_BANK;
  1767. wrt_reg_word(&reg->ctrl_status, bank_select);
  1768. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1769. } else if (((addr & BIT_16) == 0) &&
  1770. (bank_select & CSR_FLASH_64K_BANK)) {
  1771. bank_select &= ~(CSR_FLASH_64K_BANK);
  1772. wrt_reg_word(&reg->ctrl_status, bank_select);
  1773. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1774. }
  1775. /* Always perform IO mapped accesses to the FLASH registers. */
  1776. if (ha->pio_address) {
  1777. uint16_t data2;
  1778. WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
  1779. do {
  1780. data = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
  1781. barrier();
  1782. cpu_relax();
  1783. data2 = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
  1784. } while (data != data2);
  1785. } else {
  1786. wrt_reg_word(&reg->flash_address, (uint16_t)addr);
  1787. data = qla2x00_debounce_register(&reg->flash_data);
  1788. }
  1789. return (uint8_t)data;
  1790. }
  1791. /**
  1792. * qla2x00_write_flash_byte() - Write a byte to flash
  1793. * @ha: HA context
  1794. * @addr: Address in flash to write
  1795. * @data: Data to write
  1796. */
  1797. static void
  1798. qla2x00_write_flash_byte(struct qla_hw_data *ha, uint32_t addr, uint8_t data)
  1799. {
  1800. uint16_t bank_select;
  1801. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1802. bank_select = rd_reg_word(&reg->ctrl_status);
  1803. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1804. /* Specify 64K address range: */
  1805. /* clear out Module Select and Flash Address bits [19:16]. */
  1806. bank_select &= ~0xf8;
  1807. bank_select |= addr >> 12 & 0xf0;
  1808. bank_select |= CSR_FLASH_64K_BANK;
  1809. wrt_reg_word(&reg->ctrl_status, bank_select);
  1810. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1811. wrt_reg_word(&reg->flash_address, (uint16_t)addr);
  1812. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1813. wrt_reg_word(&reg->flash_data, (uint16_t)data);
  1814. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1815. return;
  1816. }
  1817. /* Setup bit 16 of flash address. */
  1818. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1819. bank_select |= CSR_FLASH_64K_BANK;
  1820. wrt_reg_word(&reg->ctrl_status, bank_select);
  1821. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1822. } else if (((addr & BIT_16) == 0) &&
  1823. (bank_select & CSR_FLASH_64K_BANK)) {
  1824. bank_select &= ~(CSR_FLASH_64K_BANK);
  1825. wrt_reg_word(&reg->ctrl_status, bank_select);
  1826. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1827. }
  1828. /* Always perform IO mapped accesses to the FLASH registers. */
  1829. if (ha->pio_address) {
  1830. WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
  1831. WRT_REG_WORD_PIO(PIO_REG(ha, flash_data), (uint16_t)data);
  1832. } else {
  1833. wrt_reg_word(&reg->flash_address, (uint16_t)addr);
  1834. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1835. wrt_reg_word(&reg->flash_data, (uint16_t)data);
  1836. rd_reg_word(&reg->ctrl_status); /* PCI Posting. */
  1837. }
  1838. }
  1839. /**
  1840. * qla2x00_poll_flash() - Polls flash for completion.
  1841. * @ha: HA context
  1842. * @addr: Address in flash to poll
  1843. * @poll_data: Data to be polled
  1844. * @man_id: Flash manufacturer ID
  1845. * @flash_id: Flash ID
  1846. *
  1847. * This function polls the device until bit 7 of what is read matches data
  1848. * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
  1849. * out (a fatal error). The flash book recommeds reading bit 7 again after
  1850. * reading bit 5 as a 1.
  1851. *
  1852. * Returns 0 on success, else non-zero.
  1853. */
  1854. static int
  1855. qla2x00_poll_flash(struct qla_hw_data *ha, uint32_t addr, uint8_t poll_data,
  1856. uint8_t man_id, uint8_t flash_id)
  1857. {
  1858. int status;
  1859. uint8_t flash_data;
  1860. uint32_t cnt;
  1861. status = 1;
  1862. /* Wait for 30 seconds for command to finish. */
  1863. poll_data &= BIT_7;
  1864. for (cnt = 3000000; cnt; cnt--) {
  1865. flash_data = qla2x00_read_flash_byte(ha, addr);
  1866. if ((flash_data & BIT_7) == poll_data) {
  1867. status = 0;
  1868. break;
  1869. }
  1870. if (man_id != 0x40 && man_id != 0xda) {
  1871. if ((flash_data & BIT_5) && cnt > 2)
  1872. cnt = 2;
  1873. }
  1874. udelay(10);
  1875. barrier();
  1876. cond_resched();
  1877. }
  1878. return status;
  1879. }
  1880. /**
  1881. * qla2x00_program_flash_address() - Programs a flash address
  1882. * @ha: HA context
  1883. * @addr: Address in flash to program
  1884. * @data: Data to be written in flash
  1885. * @man_id: Flash manufacturer ID
  1886. * @flash_id: Flash ID
  1887. *
  1888. * Returns 0 on success, else non-zero.
  1889. */
  1890. static int
  1891. qla2x00_program_flash_address(struct qla_hw_data *ha, uint32_t addr,
  1892. uint8_t data, uint8_t man_id, uint8_t flash_id)
  1893. {
  1894. /* Write Program Command Sequence. */
  1895. if (IS_OEM_001(ha)) {
  1896. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1897. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1898. qla2x00_write_flash_byte(ha, 0xaaa, 0xa0);
  1899. qla2x00_write_flash_byte(ha, addr, data);
  1900. } else {
  1901. if (man_id == 0xda && flash_id == 0xc1) {
  1902. qla2x00_write_flash_byte(ha, addr, data);
  1903. if (addr & 0x7e)
  1904. return 0;
  1905. } else {
  1906. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1907. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1908. qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
  1909. qla2x00_write_flash_byte(ha, addr, data);
  1910. }
  1911. }
  1912. udelay(150);
  1913. /* Wait for write to complete. */
  1914. return qla2x00_poll_flash(ha, addr, data, man_id, flash_id);
  1915. }
  1916. /**
  1917. * qla2x00_erase_flash() - Erase the flash.
  1918. * @ha: HA context
  1919. * @man_id: Flash manufacturer ID
  1920. * @flash_id: Flash ID
  1921. *
  1922. * Returns 0 on success, else non-zero.
  1923. */
  1924. static int
  1925. qla2x00_erase_flash(struct qla_hw_data *ha, uint8_t man_id, uint8_t flash_id)
  1926. {
  1927. /* Individual Sector Erase Command Sequence */
  1928. if (IS_OEM_001(ha)) {
  1929. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1930. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1931. qla2x00_write_flash_byte(ha, 0xaaa, 0x80);
  1932. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1933. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1934. qla2x00_write_flash_byte(ha, 0xaaa, 0x10);
  1935. } else {
  1936. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1937. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1938. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1939. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1940. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1941. qla2x00_write_flash_byte(ha, 0x5555, 0x10);
  1942. }
  1943. udelay(150);
  1944. /* Wait for erase to complete. */
  1945. return qla2x00_poll_flash(ha, 0x00, 0x80, man_id, flash_id);
  1946. }
  1947. /**
  1948. * qla2x00_erase_flash_sector() - Erase a flash sector.
  1949. * @ha: HA context
  1950. * @addr: Flash sector to erase
  1951. * @sec_mask: Sector address mask
  1952. * @man_id: Flash manufacturer ID
  1953. * @flash_id: Flash ID
  1954. *
  1955. * Returns 0 on success, else non-zero.
  1956. */
  1957. static int
  1958. qla2x00_erase_flash_sector(struct qla_hw_data *ha, uint32_t addr,
  1959. uint32_t sec_mask, uint8_t man_id, uint8_t flash_id)
  1960. {
  1961. /* Individual Sector Erase Command Sequence */
  1962. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1963. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1964. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1965. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1966. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1967. if (man_id == 0x1f && flash_id == 0x13)
  1968. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
  1969. else
  1970. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
  1971. udelay(150);
  1972. /* Wait for erase to complete. */
  1973. return qla2x00_poll_flash(ha, addr, 0x80, man_id, flash_id);
  1974. }
  1975. /**
  1976. * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
  1977. * @ha: host adapter
  1978. * @man_id: Flash manufacturer ID
  1979. * @flash_id: Flash ID
  1980. */
  1981. static void
  1982. qla2x00_get_flash_manufacturer(struct qla_hw_data *ha, uint8_t *man_id,
  1983. uint8_t *flash_id)
  1984. {
  1985. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1986. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1987. qla2x00_write_flash_byte(ha, 0x5555, 0x90);
  1988. *man_id = qla2x00_read_flash_byte(ha, 0x0000);
  1989. *flash_id = qla2x00_read_flash_byte(ha, 0x0001);
  1990. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1991. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1992. qla2x00_write_flash_byte(ha, 0x5555, 0xf0);
  1993. }
  1994. static void
  1995. qla2x00_read_flash_data(struct qla_hw_data *ha, uint8_t *tmp_buf,
  1996. uint32_t saddr, uint32_t length)
  1997. {
  1998. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1999. uint32_t midpoint, ilength;
  2000. uint8_t data;
  2001. midpoint = length / 2;
  2002. wrt_reg_word(&reg->nvram, 0);
  2003. rd_reg_word(&reg->nvram);
  2004. for (ilength = 0; ilength < length; saddr++, ilength++, tmp_buf++) {
  2005. if (ilength == midpoint) {
  2006. wrt_reg_word(&reg->nvram, NVR_SELECT);
  2007. rd_reg_word(&reg->nvram);
  2008. }
  2009. data = qla2x00_read_flash_byte(ha, saddr);
  2010. if (saddr % 100)
  2011. udelay(10);
  2012. *tmp_buf = data;
  2013. cond_resched();
  2014. }
  2015. }
  2016. static inline void
  2017. qla2x00_suspend_hba(struct scsi_qla_host *vha)
  2018. {
  2019. int cnt;
  2020. unsigned long flags;
  2021. struct qla_hw_data *ha = vha->hw;
  2022. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  2023. /* Suspend HBA. */
  2024. scsi_block_requests(vha->host);
  2025. ha->isp_ops->disable_intrs(ha);
  2026. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  2027. /* Pause RISC. */
  2028. spin_lock_irqsave(&ha->hardware_lock, flags);
  2029. wrt_reg_word(&reg->hccr, HCCR_PAUSE_RISC);
  2030. rd_reg_word(&reg->hccr);
  2031. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  2032. for (cnt = 0; cnt < 30000; cnt++) {
  2033. if ((rd_reg_word(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
  2034. break;
  2035. udelay(100);
  2036. }
  2037. } else {
  2038. udelay(10);
  2039. }
  2040. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  2041. }
  2042. static inline void
  2043. qla2x00_resume_hba(struct scsi_qla_host *vha)
  2044. {
  2045. struct qla_hw_data *ha = vha->hw;
  2046. /* Resume HBA. */
  2047. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  2048. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  2049. qla2xxx_wake_dpc(vha);
  2050. qla2x00_wait_for_chip_reset(vha);
  2051. scsi_unblock_requests(vha->host);
  2052. }
  2053. void *
  2054. qla2x00_read_optrom_data(struct scsi_qla_host *vha, void *buf,
  2055. uint32_t offset, uint32_t length)
  2056. {
  2057. uint32_t addr, midpoint;
  2058. uint8_t *data;
  2059. struct qla_hw_data *ha = vha->hw;
  2060. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  2061. /* Suspend HBA. */
  2062. qla2x00_suspend_hba(vha);
  2063. /* Go with read. */
  2064. midpoint = ha->optrom_size / 2;
  2065. qla2x00_flash_enable(ha);
  2066. wrt_reg_word(&reg->nvram, 0);
  2067. rd_reg_word(&reg->nvram); /* PCI Posting. */
  2068. for (addr = offset, data = buf; addr < length; addr++, data++) {
  2069. if (addr == midpoint) {
  2070. wrt_reg_word(&reg->nvram, NVR_SELECT);
  2071. rd_reg_word(&reg->nvram); /* PCI Posting. */
  2072. }
  2073. *data = qla2x00_read_flash_byte(ha, addr);
  2074. }
  2075. qla2x00_flash_disable(ha);
  2076. /* Resume HBA. */
  2077. qla2x00_resume_hba(vha);
  2078. return buf;
  2079. }
  2080. int
  2081. qla2x00_write_optrom_data(struct scsi_qla_host *vha, void *buf,
  2082. uint32_t offset, uint32_t length)
  2083. {
  2084. int rval;
  2085. uint8_t man_id, flash_id, sec_number, *data;
  2086. uint16_t wd;
  2087. uint32_t addr, liter, sec_mask, rest_addr;
  2088. struct qla_hw_data *ha = vha->hw;
  2089. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  2090. /* Suspend HBA. */
  2091. qla2x00_suspend_hba(vha);
  2092. rval = QLA_SUCCESS;
  2093. sec_number = 0;
  2094. /* Reset ISP chip. */
  2095. wrt_reg_word(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  2096. pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
  2097. /* Go with write. */
  2098. qla2x00_flash_enable(ha);
  2099. do { /* Loop once to provide quick error exit */
  2100. /* Structure of flash memory based on manufacturer */
  2101. if (IS_OEM_001(ha)) {
  2102. /* OEM variant with special flash part. */
  2103. man_id = flash_id = 0;
  2104. rest_addr = 0xffff;
  2105. sec_mask = 0x10000;
  2106. goto update_flash;
  2107. }
  2108. qla2x00_get_flash_manufacturer(ha, &man_id, &flash_id);
  2109. switch (man_id) {
  2110. case 0x20: /* ST flash. */
  2111. if (flash_id == 0xd2 || flash_id == 0xe3) {
  2112. /*
  2113. * ST m29w008at part - 64kb sector size with
  2114. * 32kb,8kb,8kb,16kb sectors at memory address
  2115. * 0xf0000.
  2116. */
  2117. rest_addr = 0xffff;
  2118. sec_mask = 0x10000;
  2119. break;
  2120. }
  2121. /*
  2122. * ST m29w010b part - 16kb sector size
  2123. * Default to 16kb sectors
  2124. */
  2125. rest_addr = 0x3fff;
  2126. sec_mask = 0x1c000;
  2127. break;
  2128. case 0x40: /* Mostel flash. */
  2129. /* Mostel v29c51001 part - 512 byte sector size. */
  2130. rest_addr = 0x1ff;
  2131. sec_mask = 0x1fe00;
  2132. break;
  2133. case 0xbf: /* SST flash. */
  2134. /* SST39sf10 part - 4kb sector size. */
  2135. rest_addr = 0xfff;
  2136. sec_mask = 0x1f000;
  2137. break;
  2138. case 0xda: /* Winbond flash. */
  2139. /* Winbond W29EE011 part - 256 byte sector size. */
  2140. rest_addr = 0x7f;
  2141. sec_mask = 0x1ff80;
  2142. break;
  2143. case 0xc2: /* Macronix flash. */
  2144. /* 64k sector size. */
  2145. if (flash_id == 0x38 || flash_id == 0x4f) {
  2146. rest_addr = 0xffff;
  2147. sec_mask = 0x10000;
  2148. break;
  2149. }
  2150. fallthrough;
  2151. case 0x1f: /* Atmel flash. */
  2152. /* 512k sector size. */
  2153. if (flash_id == 0x13) {
  2154. rest_addr = 0x7fffffff;
  2155. sec_mask = 0x80000000;
  2156. break;
  2157. }
  2158. fallthrough;
  2159. case 0x01: /* AMD flash. */
  2160. if (flash_id == 0x38 || flash_id == 0x40 ||
  2161. flash_id == 0x4f) {
  2162. /* Am29LV081 part - 64kb sector size. */
  2163. /* Am29LV002BT part - 64kb sector size. */
  2164. rest_addr = 0xffff;
  2165. sec_mask = 0x10000;
  2166. break;
  2167. } else if (flash_id == 0x3e) {
  2168. /*
  2169. * Am29LV008b part - 64kb sector size with
  2170. * 32kb,8kb,8kb,16kb sector at memory address
  2171. * h0xf0000.
  2172. */
  2173. rest_addr = 0xffff;
  2174. sec_mask = 0x10000;
  2175. break;
  2176. } else if (flash_id == 0x20 || flash_id == 0x6e) {
  2177. /*
  2178. * Am29LV010 part or AM29f010 - 16kb sector
  2179. * size.
  2180. */
  2181. rest_addr = 0x3fff;
  2182. sec_mask = 0x1c000;
  2183. break;
  2184. } else if (flash_id == 0x6d) {
  2185. /* Am29LV001 part - 8kb sector size. */
  2186. rest_addr = 0x1fff;
  2187. sec_mask = 0x1e000;
  2188. break;
  2189. }
  2190. fallthrough;
  2191. default:
  2192. /* Default to 16 kb sector size. */
  2193. rest_addr = 0x3fff;
  2194. sec_mask = 0x1c000;
  2195. break;
  2196. }
  2197. update_flash:
  2198. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  2199. if (qla2x00_erase_flash(ha, man_id, flash_id)) {
  2200. rval = QLA_FUNCTION_FAILED;
  2201. break;
  2202. }
  2203. }
  2204. for (addr = offset, liter = 0; liter < length; liter++,
  2205. addr++) {
  2206. data = buf + liter;
  2207. /* Are we at the beginning of a sector? */
  2208. if ((addr & rest_addr) == 0) {
  2209. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  2210. if (addr >= 0x10000UL) {
  2211. if (((addr >> 12) & 0xf0) &&
  2212. ((man_id == 0x01 &&
  2213. flash_id == 0x3e) ||
  2214. (man_id == 0x20 &&
  2215. flash_id == 0xd2))) {
  2216. sec_number++;
  2217. if (sec_number == 1) {
  2218. rest_addr =
  2219. 0x7fff;
  2220. sec_mask =
  2221. 0x18000;
  2222. } else if (
  2223. sec_number == 2 ||
  2224. sec_number == 3) {
  2225. rest_addr =
  2226. 0x1fff;
  2227. sec_mask =
  2228. 0x1e000;
  2229. } else if (
  2230. sec_number == 4) {
  2231. rest_addr =
  2232. 0x3fff;
  2233. sec_mask =
  2234. 0x1c000;
  2235. }
  2236. }
  2237. }
  2238. } else if (addr == ha->optrom_size / 2) {
  2239. wrt_reg_word(&reg->nvram, NVR_SELECT);
  2240. rd_reg_word(&reg->nvram);
  2241. }
  2242. if (flash_id == 0xda && man_id == 0xc1) {
  2243. qla2x00_write_flash_byte(ha, 0x5555,
  2244. 0xaa);
  2245. qla2x00_write_flash_byte(ha, 0x2aaa,
  2246. 0x55);
  2247. qla2x00_write_flash_byte(ha, 0x5555,
  2248. 0xa0);
  2249. } else if (!IS_QLA2322(ha) && !IS_QLA6322(ha)) {
  2250. /* Then erase it */
  2251. if (qla2x00_erase_flash_sector(ha,
  2252. addr, sec_mask, man_id,
  2253. flash_id)) {
  2254. rval = QLA_FUNCTION_FAILED;
  2255. break;
  2256. }
  2257. if (man_id == 0x01 && flash_id == 0x6d)
  2258. sec_number++;
  2259. }
  2260. }
  2261. if (man_id == 0x01 && flash_id == 0x6d) {
  2262. if (sec_number == 1 &&
  2263. addr == (rest_addr - 1)) {
  2264. rest_addr = 0x0fff;
  2265. sec_mask = 0x1f000;
  2266. } else if (sec_number == 3 && (addr & 0x7ffe)) {
  2267. rest_addr = 0x3fff;
  2268. sec_mask = 0x1c000;
  2269. }
  2270. }
  2271. if (qla2x00_program_flash_address(ha, addr, *data,
  2272. man_id, flash_id)) {
  2273. rval = QLA_FUNCTION_FAILED;
  2274. break;
  2275. }
  2276. cond_resched();
  2277. }
  2278. } while (0);
  2279. qla2x00_flash_disable(ha);
  2280. /* Resume HBA. */
  2281. qla2x00_resume_hba(vha);
  2282. return rval;
  2283. }
  2284. void *
  2285. qla24xx_read_optrom_data(struct scsi_qla_host *vha, void *buf,
  2286. uint32_t offset, uint32_t length)
  2287. {
  2288. struct qla_hw_data *ha = vha->hw;
  2289. int rc;
  2290. /* Suspend HBA. */
  2291. scsi_block_requests(vha->host);
  2292. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  2293. /* Go with read. */
  2294. rc = qla24xx_read_flash_data(vha, buf, offset >> 2, length >> 2);
  2295. if (rc) {
  2296. ql_log(ql_log_info, vha, 0x01a0,
  2297. "Unable to perform optrom read(%x).\n", rc);
  2298. }
  2299. /* Resume HBA. */
  2300. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  2301. scsi_unblock_requests(vha->host);
  2302. return buf;
  2303. }
  2304. static int
  2305. qla28xx_extract_sfub_and_verify(struct scsi_qla_host *vha, __le32 *buf,
  2306. uint32_t len, uint32_t buf_size_without_sfub, uint8_t *sfub_buf)
  2307. {
  2308. uint32_t check_sum = 0;
  2309. __le32 *p;
  2310. int i;
  2311. p = buf + buf_size_without_sfub;
  2312. /* Extract SFUB from end of file */
  2313. memcpy(sfub_buf, (uint8_t *)p,
  2314. sizeof(struct secure_flash_update_block));
  2315. for (i = 0; i < (sizeof(struct secure_flash_update_block) >> 2); i++)
  2316. check_sum += le32_to_cpu(p[i]);
  2317. check_sum = (~check_sum) + 1;
  2318. if (check_sum != le32_to_cpu(p[i])) {
  2319. ql_log(ql_log_warn, vha, 0x7097,
  2320. "SFUB checksum failed, 0x%x, 0x%x\n",
  2321. check_sum, le32_to_cpu(p[i]));
  2322. return QLA_COMMAND_ERROR;
  2323. }
  2324. return QLA_SUCCESS;
  2325. }
  2326. static int
  2327. qla28xx_get_flash_region(struct scsi_qla_host *vha, uint32_t start,
  2328. struct qla_flt_region *region)
  2329. {
  2330. struct qla_hw_data *ha = vha->hw;
  2331. struct qla_flt_header *flt = ha->flt;
  2332. struct qla_flt_region *flt_reg = &flt->region[0];
  2333. uint16_t cnt;
  2334. int rval = QLA_FUNCTION_FAILED;
  2335. if (!ha->flt)
  2336. return QLA_FUNCTION_FAILED;
  2337. cnt = le16_to_cpu(flt->length) / sizeof(struct qla_flt_region);
  2338. for (; cnt; cnt--, flt_reg++) {
  2339. if (le32_to_cpu(flt_reg->start) == start) {
  2340. memcpy((uint8_t *)region, flt_reg,
  2341. sizeof(struct qla_flt_region));
  2342. rval = QLA_SUCCESS;
  2343. break;
  2344. }
  2345. }
  2346. return rval;
  2347. }
  2348. static int
  2349. qla28xx_write_flash_data(scsi_qla_host_t *vha, uint32_t *dwptr, uint32_t faddr,
  2350. uint32_t dwords)
  2351. {
  2352. struct qla_hw_data *ha = vha->hw;
  2353. ulong liter;
  2354. ulong dburst = OPTROM_BURST_DWORDS; /* burst size in dwords */
  2355. uint32_t sec_mask, rest_addr, fdata;
  2356. void *optrom = NULL;
  2357. dma_addr_t optrom_dma;
  2358. int rval, ret;
  2359. struct secure_flash_update_block *sfub;
  2360. dma_addr_t sfub_dma;
  2361. uint32_t offset = faddr << 2;
  2362. uint32_t buf_size_without_sfub = 0;
  2363. struct qla_flt_region region;
  2364. bool reset_to_rom = false;
  2365. uint32_t risc_size, risc_attr = 0;
  2366. __be32 *fw_array = NULL;
  2367. /* Retrieve region info - must be a start address passed in */
  2368. rval = qla28xx_get_flash_region(vha, offset, &region);
  2369. if (rval != QLA_SUCCESS) {
  2370. ql_log(ql_log_warn, vha, 0xffff,
  2371. "Invalid address %x - not a region start address\n",
  2372. offset);
  2373. goto done;
  2374. }
  2375. /* Allocate dma buffer for burst write */
  2376. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  2377. &optrom_dma, GFP_KERNEL);
  2378. if (!optrom) {
  2379. ql_log(ql_log_warn, vha, 0x7095,
  2380. "Failed allocate burst (%x bytes)\n", OPTROM_BURST_SIZE);
  2381. rval = QLA_COMMAND_ERROR;
  2382. goto done;
  2383. }
  2384. /*
  2385. * If adapter supports secure flash and region is secure
  2386. * extract secure flash update block (SFUB) and verify
  2387. */
  2388. if (ha->flags.secure_adapter && region.attribute) {
  2389. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0xffff,
  2390. "Region %x is secure\n", le16_to_cpu(region.code));
  2391. switch (le16_to_cpu(region.code)) {
  2392. case FLT_REG_FW:
  2393. case FLT_REG_FW_SEC_27XX:
  2394. case FLT_REG_MPI_PRI_28XX:
  2395. case FLT_REG_MPI_SEC_28XX:
  2396. fw_array = (__force __be32 *)dwptr;
  2397. /* 1st fw array */
  2398. risc_size = be32_to_cpu(fw_array[3]);
  2399. risc_attr = be32_to_cpu(fw_array[9]);
  2400. buf_size_without_sfub = risc_size;
  2401. fw_array += risc_size;
  2402. /* 2nd fw array */
  2403. risc_size = be32_to_cpu(fw_array[3]);
  2404. buf_size_without_sfub += risc_size;
  2405. fw_array += risc_size;
  2406. /* 1st dump template */
  2407. risc_size = be32_to_cpu(fw_array[2]);
  2408. /* skip header and ignore checksum */
  2409. buf_size_without_sfub += risc_size;
  2410. fw_array += risc_size;
  2411. if (risc_attr & BIT_9) {
  2412. /* 2nd dump template */
  2413. risc_size = be32_to_cpu(fw_array[2]);
  2414. /* skip header and ignore checksum */
  2415. buf_size_without_sfub += risc_size;
  2416. fw_array += risc_size;
  2417. }
  2418. break;
  2419. case FLT_REG_PEP_PRI_28XX:
  2420. case FLT_REG_PEP_SEC_28XX:
  2421. fw_array = (__force __be32 *)dwptr;
  2422. /* 1st fw array */
  2423. risc_size = be32_to_cpu(fw_array[3]);
  2424. risc_attr = be32_to_cpu(fw_array[9]);
  2425. buf_size_without_sfub = risc_size;
  2426. fw_array += risc_size;
  2427. break;
  2428. default:
  2429. ql_log(ql_log_warn + ql_dbg_verbose, vha,
  2430. 0xffff, "Secure region %x not supported\n",
  2431. le16_to_cpu(region.code));
  2432. rval = QLA_COMMAND_ERROR;
  2433. goto done;
  2434. }
  2435. sfub = dma_alloc_coherent(&ha->pdev->dev,
  2436. sizeof(struct secure_flash_update_block), &sfub_dma,
  2437. GFP_KERNEL);
  2438. if (!sfub) {
  2439. ql_log(ql_log_warn, vha, 0xffff,
  2440. "Unable to allocate memory for SFUB\n");
  2441. rval = QLA_COMMAND_ERROR;
  2442. goto done;
  2443. }
  2444. rval = qla28xx_extract_sfub_and_verify(vha, (__le32 *)dwptr,
  2445. dwords, buf_size_without_sfub, (uint8_t *)sfub);
  2446. if (rval != QLA_SUCCESS)
  2447. goto done;
  2448. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0xffff,
  2449. "SFUB extract and verify successful\n");
  2450. }
  2451. rest_addr = (ha->fdt_block_size >> 2) - 1;
  2452. sec_mask = ~rest_addr;
  2453. /* Lock semaphore */
  2454. rval = qla81xx_fac_semaphore_access(vha, FAC_SEMAPHORE_LOCK);
  2455. if (rval != QLA_SUCCESS) {
  2456. ql_log(ql_log_warn, vha, 0xffff,
  2457. "Unable to lock flash semaphore.");
  2458. goto done;
  2459. }
  2460. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0x7095,
  2461. "Unprotect flash...\n");
  2462. rval = qla24xx_unprotect_flash(vha);
  2463. if (rval) {
  2464. qla81xx_fac_semaphore_access(vha, FAC_SEMAPHORE_UNLOCK);
  2465. ql_log(ql_log_warn, vha, 0x7096, "Failed unprotect flash\n");
  2466. goto done;
  2467. }
  2468. for (liter = 0; liter < dwords; liter++, faddr++) {
  2469. fdata = (faddr & sec_mask) << 2;
  2470. /* If start of sector */
  2471. if (!(faddr & rest_addr)) {
  2472. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0x7095,
  2473. "Erase sector %#x...\n", faddr);
  2474. rval = qla24xx_erase_sector(vha, fdata);
  2475. if (rval) {
  2476. ql_dbg(ql_dbg_user, vha, 0x7007,
  2477. "Failed erase sector %#x\n", faddr);
  2478. goto write_protect;
  2479. }
  2480. }
  2481. }
  2482. if (ha->flags.secure_adapter) {
  2483. /*
  2484. * If adapter supports secure flash but FW doesn't,
  2485. * disable write protect, release semaphore and reset
  2486. * chip to execute ROM code in order to update region securely
  2487. */
  2488. if (!ha->flags.secure_fw) {
  2489. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0xffff,
  2490. "Disable Write and Release Semaphore.");
  2491. rval = qla24xx_protect_flash(vha);
  2492. if (rval != QLA_SUCCESS) {
  2493. qla81xx_fac_semaphore_access(vha,
  2494. FAC_SEMAPHORE_UNLOCK);
  2495. ql_log(ql_log_warn, vha, 0xffff,
  2496. "Unable to protect flash.");
  2497. goto done;
  2498. }
  2499. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0xffff,
  2500. "Reset chip to ROM.");
  2501. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  2502. set_bit(ISP_ABORT_TO_ROM, &vha->dpc_flags);
  2503. qla2xxx_wake_dpc(vha);
  2504. rval = qla2x00_wait_for_chip_reset(vha);
  2505. if (rval != QLA_SUCCESS) {
  2506. ql_log(ql_log_warn, vha, 0xffff,
  2507. "Unable to reset to ROM code.");
  2508. goto done;
  2509. }
  2510. reset_to_rom = true;
  2511. ha->flags.fac_supported = 0;
  2512. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0xffff,
  2513. "Lock Semaphore");
  2514. rval = qla2xxx_write_remote_register(vha,
  2515. FLASH_SEMAPHORE_REGISTER_ADDR, 0x00020002);
  2516. if (rval != QLA_SUCCESS) {
  2517. ql_log(ql_log_warn, vha, 0xffff,
  2518. "Unable to lock flash semaphore.");
  2519. goto done;
  2520. }
  2521. /* Unprotect flash */
  2522. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0xffff,
  2523. "Enable Write.");
  2524. rval = qla2x00_write_ram_word(vha, 0x7ffd0101, 0);
  2525. if (rval) {
  2526. ql_log(ql_log_warn, vha, 0x7096,
  2527. "Failed unprotect flash\n");
  2528. goto done;
  2529. }
  2530. }
  2531. /* If region is secure, send Secure Flash MB Cmd */
  2532. if (region.attribute && buf_size_without_sfub) {
  2533. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0xffff,
  2534. "Sending Secure Flash MB Cmd\n");
  2535. rval = qla28xx_secure_flash_update(vha, 0,
  2536. le16_to_cpu(region.code),
  2537. buf_size_without_sfub, sfub_dma,
  2538. sizeof(struct secure_flash_update_block) >> 2);
  2539. if (rval != QLA_SUCCESS) {
  2540. ql_log(ql_log_warn, vha, 0xffff,
  2541. "Secure Flash MB Cmd failed %x.", rval);
  2542. goto write_protect;
  2543. }
  2544. }
  2545. }
  2546. /* re-init flash offset */
  2547. faddr = offset >> 2;
  2548. for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
  2549. fdata = (faddr & sec_mask) << 2;
  2550. /* If smaller than a burst remaining */
  2551. if (dwords - liter < dburst)
  2552. dburst = dwords - liter;
  2553. /* Copy to dma buffer */
  2554. memcpy(optrom, dwptr, dburst << 2);
  2555. /* Burst write */
  2556. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0x7095,
  2557. "Write burst (%#lx dwords)...\n", dburst);
  2558. rval = qla2x00_load_ram(vha, optrom_dma,
  2559. flash_data_addr(ha, faddr), dburst);
  2560. if (rval != QLA_SUCCESS) {
  2561. ql_log(ql_log_warn, vha, 0x7097,
  2562. "Failed burst write at %x (%p/%#llx)...\n",
  2563. flash_data_addr(ha, faddr), optrom,
  2564. (u64)optrom_dma);
  2565. break;
  2566. }
  2567. liter += dburst - 1;
  2568. faddr += dburst - 1;
  2569. dwptr += dburst - 1;
  2570. }
  2571. write_protect:
  2572. ql_log(ql_log_warn + ql_dbg_verbose, vha, 0x7095,
  2573. "Protect flash...\n");
  2574. ret = qla24xx_protect_flash(vha);
  2575. if (ret) {
  2576. qla81xx_fac_semaphore_access(vha, FAC_SEMAPHORE_UNLOCK);
  2577. ql_log(ql_log_warn, vha, 0x7099,
  2578. "Failed protect flash\n");
  2579. rval = QLA_COMMAND_ERROR;
  2580. }
  2581. if (reset_to_rom == true) {
  2582. /* Schedule DPC to restart the RISC */
  2583. set_bit(ISP_ABORT_NEEDED, &vha->dpc_flags);
  2584. qla2xxx_wake_dpc(vha);
  2585. ret = qla2x00_wait_for_hba_online(vha);
  2586. if (ret != QLA_SUCCESS) {
  2587. ql_log(ql_log_warn, vha, 0xffff,
  2588. "Adapter did not come out of reset\n");
  2589. rval = QLA_COMMAND_ERROR;
  2590. }
  2591. }
  2592. done:
  2593. if (optrom)
  2594. dma_free_coherent(&ha->pdev->dev,
  2595. OPTROM_BURST_SIZE, optrom, optrom_dma);
  2596. return rval;
  2597. }
  2598. int
  2599. qla24xx_write_optrom_data(struct scsi_qla_host *vha, void *buf,
  2600. uint32_t offset, uint32_t length)
  2601. {
  2602. int rval;
  2603. struct qla_hw_data *ha = vha->hw;
  2604. /* Suspend HBA. */
  2605. scsi_block_requests(vha->host);
  2606. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  2607. /* Go with write. */
  2608. if (IS_QLA28XX(ha))
  2609. rval = qla28xx_write_flash_data(vha, buf, offset >> 2,
  2610. length >> 2);
  2611. else
  2612. rval = qla24xx_write_flash_data(vha, buf, offset >> 2,
  2613. length >> 2);
  2614. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  2615. scsi_unblock_requests(vha->host);
  2616. return rval;
  2617. }
  2618. void *
  2619. qla25xx_read_optrom_data(struct scsi_qla_host *vha, void *buf,
  2620. uint32_t offset, uint32_t length)
  2621. {
  2622. int rval;
  2623. dma_addr_t optrom_dma;
  2624. void *optrom;
  2625. uint8_t *pbuf;
  2626. uint32_t faddr, left, burst;
  2627. struct qla_hw_data *ha = vha->hw;
  2628. if (IS_QLA25XX(ha) || IS_QLA81XX(ha) || IS_QLA83XX(ha) ||
  2629. IS_QLA27XX(ha) || IS_QLA28XX(ha))
  2630. goto try_fast;
  2631. if (offset & 0xfff)
  2632. goto slow_read;
  2633. if (length < OPTROM_BURST_SIZE)
  2634. goto slow_read;
  2635. try_fast:
  2636. if (offset & 0xff)
  2637. goto slow_read;
  2638. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  2639. &optrom_dma, GFP_KERNEL);
  2640. if (!optrom) {
  2641. ql_log(ql_log_warn, vha, 0x00cc,
  2642. "Unable to allocate memory for optrom burst read (%x KB).\n",
  2643. OPTROM_BURST_SIZE / 1024);
  2644. goto slow_read;
  2645. }
  2646. pbuf = buf;
  2647. faddr = offset >> 2;
  2648. left = length >> 2;
  2649. burst = OPTROM_BURST_DWORDS;
  2650. while (left != 0) {
  2651. if (burst > left)
  2652. burst = left;
  2653. rval = qla2x00_dump_ram(vha, optrom_dma,
  2654. flash_data_addr(ha, faddr), burst);
  2655. if (rval) {
  2656. ql_log(ql_log_warn, vha, 0x00f5,
  2657. "Unable to burst-read optrom segment (%x/%x/%llx).\n",
  2658. rval, flash_data_addr(ha, faddr),
  2659. (unsigned long long)optrom_dma);
  2660. ql_log(ql_log_warn, vha, 0x00f6,
  2661. "Reverting to slow-read.\n");
  2662. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  2663. optrom, optrom_dma);
  2664. goto slow_read;
  2665. }
  2666. memcpy(pbuf, optrom, burst * 4);
  2667. left -= burst;
  2668. faddr += burst;
  2669. pbuf += burst * 4;
  2670. }
  2671. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE, optrom,
  2672. optrom_dma);
  2673. return buf;
  2674. slow_read:
  2675. return qla24xx_read_optrom_data(vha, buf, offset, length);
  2676. }
  2677. /**
  2678. * qla2x00_get_fcode_version() - Determine an FCODE image's version.
  2679. * @ha: HA context
  2680. * @pcids: Pointer to the FCODE PCI data structure
  2681. *
  2682. * The process of retrieving the FCODE version information is at best
  2683. * described as interesting.
  2684. *
  2685. * Within the first 100h bytes of the image an ASCII string is present
  2686. * which contains several pieces of information including the FCODE
  2687. * version. Unfortunately it seems the only reliable way to retrieve
  2688. * the version is by scanning for another sentinel within the string,
  2689. * the FCODE build date:
  2690. *
  2691. * ... 2.00.02 10/17/02 ...
  2692. *
  2693. * Returns QLA_SUCCESS on successful retrieval of version.
  2694. */
  2695. static void
  2696. qla2x00_get_fcode_version(struct qla_hw_data *ha, uint32_t pcids)
  2697. {
  2698. int ret = QLA_FUNCTION_FAILED;
  2699. uint32_t istart, iend, iter, vend;
  2700. uint8_t do_next, rbyte, *vbyte;
  2701. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  2702. /* Skip the PCI data structure. */
  2703. istart = pcids +
  2704. ((qla2x00_read_flash_byte(ha, pcids + 0x0B) << 8) |
  2705. qla2x00_read_flash_byte(ha, pcids + 0x0A));
  2706. iend = istart + 0x100;
  2707. do {
  2708. /* Scan for the sentinel date string...eeewww. */
  2709. do_next = 0;
  2710. iter = istart;
  2711. while ((iter < iend) && !do_next) {
  2712. iter++;
  2713. if (qla2x00_read_flash_byte(ha, iter) == '/') {
  2714. if (qla2x00_read_flash_byte(ha, iter + 2) ==
  2715. '/')
  2716. do_next++;
  2717. else if (qla2x00_read_flash_byte(ha,
  2718. iter + 3) == '/')
  2719. do_next++;
  2720. }
  2721. }
  2722. if (!do_next)
  2723. break;
  2724. /* Backtrack to previous ' ' (space). */
  2725. do_next = 0;
  2726. while ((iter > istart) && !do_next) {
  2727. iter--;
  2728. if (qla2x00_read_flash_byte(ha, iter) == ' ')
  2729. do_next++;
  2730. }
  2731. if (!do_next)
  2732. break;
  2733. /*
  2734. * Mark end of version tag, and find previous ' ' (space) or
  2735. * string length (recent FCODE images -- major hack ahead!!!).
  2736. */
  2737. vend = iter - 1;
  2738. do_next = 0;
  2739. while ((iter > istart) && !do_next) {
  2740. iter--;
  2741. rbyte = qla2x00_read_flash_byte(ha, iter);
  2742. if (rbyte == ' ' || rbyte == 0xd || rbyte == 0x10)
  2743. do_next++;
  2744. }
  2745. if (!do_next)
  2746. break;
  2747. /* Mark beginning of version tag, and copy data. */
  2748. iter++;
  2749. if ((vend - iter) &&
  2750. ((vend - iter) < sizeof(ha->fcode_revision))) {
  2751. vbyte = ha->fcode_revision;
  2752. while (iter <= vend) {
  2753. *vbyte++ = qla2x00_read_flash_byte(ha, iter);
  2754. iter++;
  2755. }
  2756. ret = QLA_SUCCESS;
  2757. }
  2758. } while (0);
  2759. if (ret != QLA_SUCCESS)
  2760. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  2761. }
  2762. int
  2763. qla2x00_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
  2764. {
  2765. int ret = QLA_SUCCESS;
  2766. uint8_t code_type, last_image;
  2767. uint32_t pcihdr, pcids;
  2768. uint8_t *dbyte;
  2769. uint16_t *dcode;
  2770. struct qla_hw_data *ha = vha->hw;
  2771. if (!ha->pio_address || !mbuf)
  2772. return QLA_FUNCTION_FAILED;
  2773. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  2774. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  2775. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  2776. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2777. qla2x00_flash_enable(ha);
  2778. /* Begin with first PCI expansion ROM header. */
  2779. pcihdr = 0;
  2780. last_image = 1;
  2781. do {
  2782. /* Verify PCI expansion ROM header. */
  2783. if (qla2x00_read_flash_byte(ha, pcihdr) != 0x55 ||
  2784. qla2x00_read_flash_byte(ha, pcihdr + 0x01) != 0xaa) {
  2785. /* No signature */
  2786. ql_log(ql_log_fatal, vha, 0x0050,
  2787. "No matching ROM signature.\n");
  2788. ret = QLA_FUNCTION_FAILED;
  2789. break;
  2790. }
  2791. /* Locate PCI data structure. */
  2792. pcids = pcihdr +
  2793. ((qla2x00_read_flash_byte(ha, pcihdr + 0x19) << 8) |
  2794. qla2x00_read_flash_byte(ha, pcihdr + 0x18));
  2795. /* Validate signature of PCI data structure. */
  2796. if (qla2x00_read_flash_byte(ha, pcids) != 'P' ||
  2797. qla2x00_read_flash_byte(ha, pcids + 0x1) != 'C' ||
  2798. qla2x00_read_flash_byte(ha, pcids + 0x2) != 'I' ||
  2799. qla2x00_read_flash_byte(ha, pcids + 0x3) != 'R') {
  2800. /* Incorrect header. */
  2801. ql_log(ql_log_fatal, vha, 0x0051,
  2802. "PCI data struct not found pcir_adr=%x.\n", pcids);
  2803. ret = QLA_FUNCTION_FAILED;
  2804. break;
  2805. }
  2806. /* Read version */
  2807. code_type = qla2x00_read_flash_byte(ha, pcids + 0x14);
  2808. switch (code_type) {
  2809. case ROM_CODE_TYPE_BIOS:
  2810. /* Intel x86, PC-AT compatible. */
  2811. ha->bios_revision[0] =
  2812. qla2x00_read_flash_byte(ha, pcids + 0x12);
  2813. ha->bios_revision[1] =
  2814. qla2x00_read_flash_byte(ha, pcids + 0x13);
  2815. ql_dbg(ql_dbg_init, vha, 0x0052,
  2816. "Read BIOS %d.%d.\n",
  2817. ha->bios_revision[1], ha->bios_revision[0]);
  2818. break;
  2819. case ROM_CODE_TYPE_FCODE:
  2820. /* Open Firmware standard for PCI (FCode). */
  2821. /* Eeeewww... */
  2822. qla2x00_get_fcode_version(ha, pcids);
  2823. break;
  2824. case ROM_CODE_TYPE_EFI:
  2825. /* Extensible Firmware Interface (EFI). */
  2826. ha->efi_revision[0] =
  2827. qla2x00_read_flash_byte(ha, pcids + 0x12);
  2828. ha->efi_revision[1] =
  2829. qla2x00_read_flash_byte(ha, pcids + 0x13);
  2830. ql_dbg(ql_dbg_init, vha, 0x0053,
  2831. "Read EFI %d.%d.\n",
  2832. ha->efi_revision[1], ha->efi_revision[0]);
  2833. break;
  2834. default:
  2835. ql_log(ql_log_warn, vha, 0x0054,
  2836. "Unrecognized code type %x at pcids %x.\n",
  2837. code_type, pcids);
  2838. break;
  2839. }
  2840. last_image = qla2x00_read_flash_byte(ha, pcids + 0x15) & BIT_7;
  2841. /* Locate next PCI expansion ROM. */
  2842. pcihdr += ((qla2x00_read_flash_byte(ha, pcids + 0x11) << 8) |
  2843. qla2x00_read_flash_byte(ha, pcids + 0x10)) * 512;
  2844. } while (!last_image);
  2845. if (IS_QLA2322(ha)) {
  2846. /* Read firmware image information. */
  2847. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2848. dbyte = mbuf;
  2849. memset(dbyte, 0, 8);
  2850. dcode = (uint16_t *)dbyte;
  2851. qla2x00_read_flash_data(ha, dbyte, ha->flt_region_fw * 4 + 10,
  2852. 8);
  2853. ql_dbg(ql_dbg_init + ql_dbg_buffer, vha, 0x010a,
  2854. "Dumping fw "
  2855. "ver from flash:.\n");
  2856. ql_dump_buffer(ql_dbg_init + ql_dbg_buffer, vha, 0x010b,
  2857. dbyte, 32);
  2858. if ((dcode[0] == 0xffff && dcode[1] == 0xffff &&
  2859. dcode[2] == 0xffff && dcode[3] == 0xffff) ||
  2860. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  2861. dcode[3] == 0)) {
  2862. ql_log(ql_log_warn, vha, 0x0057,
  2863. "Unrecognized fw revision at %x.\n",
  2864. ha->flt_region_fw * 4);
  2865. } else {
  2866. /* values are in big endian */
  2867. ha->fw_revision[0] = dbyte[0] << 16 | dbyte[1];
  2868. ha->fw_revision[1] = dbyte[2] << 16 | dbyte[3];
  2869. ha->fw_revision[2] = dbyte[4] << 16 | dbyte[5];
  2870. ql_dbg(ql_dbg_init, vha, 0x0058,
  2871. "FW Version: "
  2872. "%d.%d.%d.\n", ha->fw_revision[0],
  2873. ha->fw_revision[1], ha->fw_revision[2]);
  2874. }
  2875. }
  2876. qla2x00_flash_disable(ha);
  2877. return ret;
  2878. }
  2879. int
  2880. qla82xx_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
  2881. {
  2882. int ret = QLA_SUCCESS;
  2883. uint32_t pcihdr, pcids;
  2884. uint32_t *dcode = mbuf;
  2885. uint8_t *bcode = mbuf;
  2886. uint8_t code_type, last_image;
  2887. struct qla_hw_data *ha = vha->hw;
  2888. if (!mbuf)
  2889. return QLA_FUNCTION_FAILED;
  2890. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  2891. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  2892. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  2893. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2894. /* Begin with first PCI expansion ROM header. */
  2895. pcihdr = ha->flt_region_boot << 2;
  2896. last_image = 1;
  2897. do {
  2898. /* Verify PCI expansion ROM header. */
  2899. ha->isp_ops->read_optrom(vha, dcode, pcihdr, 0x20 * 4);
  2900. bcode = mbuf + (pcihdr % 4);
  2901. if (memcmp(bcode, "\x55\xaa", 2)) {
  2902. /* No signature */
  2903. ql_log(ql_log_fatal, vha, 0x0154,
  2904. "No matching ROM signature.\n");
  2905. ret = QLA_FUNCTION_FAILED;
  2906. break;
  2907. }
  2908. /* Locate PCI data structure. */
  2909. pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
  2910. ha->isp_ops->read_optrom(vha, dcode, pcids, 0x20 * 4);
  2911. bcode = mbuf + (pcihdr % 4);
  2912. /* Validate signature of PCI data structure. */
  2913. if (memcmp(bcode, "PCIR", 4)) {
  2914. /* Incorrect header. */
  2915. ql_log(ql_log_fatal, vha, 0x0155,
  2916. "PCI data struct not found pcir_adr=%x.\n", pcids);
  2917. ret = QLA_FUNCTION_FAILED;
  2918. break;
  2919. }
  2920. /* Read version */
  2921. code_type = bcode[0x14];
  2922. switch (code_type) {
  2923. case ROM_CODE_TYPE_BIOS:
  2924. /* Intel x86, PC-AT compatible. */
  2925. ha->bios_revision[0] = bcode[0x12];
  2926. ha->bios_revision[1] = bcode[0x13];
  2927. ql_dbg(ql_dbg_init, vha, 0x0156,
  2928. "Read BIOS %d.%d.\n",
  2929. ha->bios_revision[1], ha->bios_revision[0]);
  2930. break;
  2931. case ROM_CODE_TYPE_FCODE:
  2932. /* Open Firmware standard for PCI (FCode). */
  2933. ha->fcode_revision[0] = bcode[0x12];
  2934. ha->fcode_revision[1] = bcode[0x13];
  2935. ql_dbg(ql_dbg_init, vha, 0x0157,
  2936. "Read FCODE %d.%d.\n",
  2937. ha->fcode_revision[1], ha->fcode_revision[0]);
  2938. break;
  2939. case ROM_CODE_TYPE_EFI:
  2940. /* Extensible Firmware Interface (EFI). */
  2941. ha->efi_revision[0] = bcode[0x12];
  2942. ha->efi_revision[1] = bcode[0x13];
  2943. ql_dbg(ql_dbg_init, vha, 0x0158,
  2944. "Read EFI %d.%d.\n",
  2945. ha->efi_revision[1], ha->efi_revision[0]);
  2946. break;
  2947. default:
  2948. ql_log(ql_log_warn, vha, 0x0159,
  2949. "Unrecognized code type %x at pcids %x.\n",
  2950. code_type, pcids);
  2951. break;
  2952. }
  2953. last_image = bcode[0x15] & BIT_7;
  2954. /* Locate next PCI expansion ROM. */
  2955. pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
  2956. } while (!last_image);
  2957. /* Read firmware image information. */
  2958. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2959. dcode = mbuf;
  2960. ha->isp_ops->read_optrom(vha, dcode, ha->flt_region_fw << 2, 0x20);
  2961. bcode = mbuf + (pcihdr % 4);
  2962. /* Validate signature of PCI data structure. */
  2963. if (bcode[0x0] == 0x3 && bcode[0x1] == 0x0 &&
  2964. bcode[0x2] == 0x40 && bcode[0x3] == 0x40) {
  2965. ha->fw_revision[0] = bcode[0x4];
  2966. ha->fw_revision[1] = bcode[0x5];
  2967. ha->fw_revision[2] = bcode[0x6];
  2968. ql_dbg(ql_dbg_init, vha, 0x0153,
  2969. "Firmware revision %d.%d.%d\n",
  2970. ha->fw_revision[0], ha->fw_revision[1],
  2971. ha->fw_revision[2]);
  2972. }
  2973. return ret;
  2974. }
  2975. int
  2976. qla24xx_get_flash_version(scsi_qla_host_t *vha, void *mbuf)
  2977. {
  2978. int ret = QLA_SUCCESS;
  2979. uint32_t pcihdr = 0, pcids = 0;
  2980. uint32_t *dcode = mbuf;
  2981. uint8_t *bcode = mbuf;
  2982. uint8_t code_type, last_image;
  2983. int i;
  2984. struct qla_hw_data *ha = vha->hw;
  2985. uint32_t faddr = 0;
  2986. struct active_regions active_regions = { };
  2987. if (IS_P3P_TYPE(ha))
  2988. return QLA_SUCCESS;
  2989. if (!mbuf)
  2990. return QLA_FUNCTION_FAILED;
  2991. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  2992. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  2993. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  2994. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  2995. pcihdr = ha->flt_region_boot << 2;
  2996. if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
  2997. qla27xx_get_active_image(vha, &active_regions);
  2998. if (active_regions.global == QLA27XX_SECONDARY_IMAGE) {
  2999. pcihdr = ha->flt_region_boot_sec << 2;
  3000. }
  3001. }
  3002. do {
  3003. /* Verify PCI expansion ROM header. */
  3004. ret = qla24xx_read_flash_data(vha, dcode, pcihdr >> 2, 0x20);
  3005. if (ret) {
  3006. ql_log(ql_log_info, vha, 0x017d,
  3007. "Unable to read PCI EXP Rom Header(%x).\n", ret);
  3008. return QLA_FUNCTION_FAILED;
  3009. }
  3010. bcode = mbuf + (pcihdr % 4);
  3011. if (memcmp(bcode, "\x55\xaa", 2)) {
  3012. /* No signature */
  3013. ql_log(ql_log_fatal, vha, 0x0059,
  3014. "No matching ROM signature.\n");
  3015. return QLA_FUNCTION_FAILED;
  3016. }
  3017. /* Locate PCI data structure. */
  3018. pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
  3019. ret = qla24xx_read_flash_data(vha, dcode, pcids >> 2, 0x20);
  3020. if (ret) {
  3021. ql_log(ql_log_info, vha, 0x018e,
  3022. "Unable to read PCI Data Structure (%x).\n", ret);
  3023. return QLA_FUNCTION_FAILED;
  3024. }
  3025. bcode = mbuf + (pcihdr % 4);
  3026. /* Validate signature of PCI data structure. */
  3027. if (memcmp(bcode, "PCIR", 4)) {
  3028. /* Incorrect header. */
  3029. ql_log(ql_log_fatal, vha, 0x005a,
  3030. "PCI data struct not found pcir_adr=%x.\n", pcids);
  3031. ql_dump_buffer(ql_dbg_init, vha, 0x0059, dcode, 32);
  3032. return QLA_FUNCTION_FAILED;
  3033. }
  3034. /* Read version */
  3035. code_type = bcode[0x14];
  3036. switch (code_type) {
  3037. case ROM_CODE_TYPE_BIOS:
  3038. /* Intel x86, PC-AT compatible. */
  3039. ha->bios_revision[0] = bcode[0x12];
  3040. ha->bios_revision[1] = bcode[0x13];
  3041. ql_dbg(ql_dbg_init, vha, 0x005b,
  3042. "Read BIOS %d.%d.\n",
  3043. ha->bios_revision[1], ha->bios_revision[0]);
  3044. break;
  3045. case ROM_CODE_TYPE_FCODE:
  3046. /* Open Firmware standard for PCI (FCode). */
  3047. ha->fcode_revision[0] = bcode[0x12];
  3048. ha->fcode_revision[1] = bcode[0x13];
  3049. ql_dbg(ql_dbg_init, vha, 0x005c,
  3050. "Read FCODE %d.%d.\n",
  3051. ha->fcode_revision[1], ha->fcode_revision[0]);
  3052. break;
  3053. case ROM_CODE_TYPE_EFI:
  3054. /* Extensible Firmware Interface (EFI). */
  3055. ha->efi_revision[0] = bcode[0x12];
  3056. ha->efi_revision[1] = bcode[0x13];
  3057. ql_dbg(ql_dbg_init, vha, 0x005d,
  3058. "Read EFI %d.%d.\n",
  3059. ha->efi_revision[1], ha->efi_revision[0]);
  3060. break;
  3061. default:
  3062. ql_log(ql_log_warn, vha, 0x005e,
  3063. "Unrecognized code type %x at pcids %x.\n",
  3064. code_type, pcids);
  3065. break;
  3066. }
  3067. last_image = bcode[0x15] & BIT_7;
  3068. /* Locate next PCI expansion ROM. */
  3069. pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
  3070. } while (!last_image);
  3071. /* Read firmware image information. */
  3072. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  3073. faddr = ha->flt_region_fw;
  3074. if (IS_QLA27XX(ha) || IS_QLA28XX(ha)) {
  3075. qla27xx_get_active_image(vha, &active_regions);
  3076. if (active_regions.global == QLA27XX_SECONDARY_IMAGE)
  3077. faddr = ha->flt_region_fw_sec;
  3078. }
  3079. ret = qla24xx_read_flash_data(vha, dcode, faddr, 8);
  3080. if (ret) {
  3081. ql_log(ql_log_info, vha, 0x019e,
  3082. "Unable to read FW version (%x).\n", ret);
  3083. return ret;
  3084. } else {
  3085. if (qla24xx_risc_firmware_invalid(dcode)) {
  3086. ql_log(ql_log_warn, vha, 0x005f,
  3087. "Unrecognized fw revision at %x.\n",
  3088. ha->flt_region_fw * 4);
  3089. ql_dump_buffer(ql_dbg_init, vha, 0x005f, dcode, 32);
  3090. } else {
  3091. for (i = 0; i < 4; i++)
  3092. ha->fw_revision[i] =
  3093. be32_to_cpu((__force __be32)dcode[4+i]);
  3094. ql_dbg(ql_dbg_init, vha, 0x0060,
  3095. "Firmware revision (flash) %u.%u.%u (%x).\n",
  3096. ha->fw_revision[0], ha->fw_revision[1],
  3097. ha->fw_revision[2], ha->fw_revision[3]);
  3098. }
  3099. }
  3100. /* Check for golden firmware and get version if available */
  3101. if (!IS_QLA81XX(ha)) {
  3102. /* Golden firmware is not present in non 81XX adapters */
  3103. return ret;
  3104. }
  3105. memset(ha->gold_fw_version, 0, sizeof(ha->gold_fw_version));
  3106. faddr = ha->flt_region_gold_fw;
  3107. ret = qla24xx_read_flash_data(vha, dcode, ha->flt_region_gold_fw, 8);
  3108. if (ret) {
  3109. ql_log(ql_log_info, vha, 0x019f,
  3110. "Unable to read Gold FW version (%x).\n", ret);
  3111. return ret;
  3112. } else {
  3113. if (qla24xx_risc_firmware_invalid(dcode)) {
  3114. ql_log(ql_log_warn, vha, 0x0056,
  3115. "Unrecognized golden fw at %#x.\n", faddr);
  3116. ql_dump_buffer(ql_dbg_init, vha, 0x0056, dcode, 32);
  3117. return QLA_FUNCTION_FAILED;
  3118. }
  3119. for (i = 0; i < 4; i++)
  3120. ha->gold_fw_version[i] =
  3121. be32_to_cpu((__force __be32)dcode[4+i]);
  3122. }
  3123. return ret;
  3124. }
  3125. static int
  3126. qla2xxx_is_vpd_valid(uint8_t *pos, uint8_t *end)
  3127. {
  3128. if (pos >= end || *pos != 0x82)
  3129. return 0;
  3130. pos += 3 + pos[1];
  3131. if (pos >= end || *pos != 0x90)
  3132. return 0;
  3133. pos += 3 + pos[1];
  3134. if (pos >= end || *pos != 0x78)
  3135. return 0;
  3136. return 1;
  3137. }
  3138. int
  3139. qla2xxx_get_vpd_field(scsi_qla_host_t *vha, char *key, char *str, size_t size)
  3140. {
  3141. struct qla_hw_data *ha = vha->hw;
  3142. uint8_t *pos = ha->vpd;
  3143. uint8_t *end = pos + ha->vpd_size;
  3144. int len = 0;
  3145. if (!IS_FWI2_CAPABLE(ha) || !qla2xxx_is_vpd_valid(pos, end))
  3146. return 0;
  3147. while (pos < end && *pos != 0x78) {
  3148. len = (*pos == 0x82) ? pos[1] : pos[2];
  3149. if (!strncmp(pos, key, strlen(key)))
  3150. break;
  3151. if (*pos != 0x90 && *pos != 0x91)
  3152. pos += len;
  3153. pos += 3;
  3154. }
  3155. if (pos < end - len && *pos != 0x78)
  3156. return scnprintf(str, size, "%.*s", len, pos + 3);
  3157. return 0;
  3158. }
  3159. int
  3160. qla24xx_read_fcp_prio_cfg(scsi_qla_host_t *vha)
  3161. {
  3162. int len, max_len;
  3163. uint32_t fcp_prio_addr;
  3164. struct qla_hw_data *ha = vha->hw;
  3165. if (!ha->fcp_prio_cfg) {
  3166. ha->fcp_prio_cfg = vmalloc(FCP_PRIO_CFG_SIZE);
  3167. if (!ha->fcp_prio_cfg) {
  3168. ql_log(ql_log_warn, vha, 0x00d5,
  3169. "Unable to allocate memory for fcp priority data (%x).\n",
  3170. FCP_PRIO_CFG_SIZE);
  3171. return QLA_FUNCTION_FAILED;
  3172. }
  3173. }
  3174. memset(ha->fcp_prio_cfg, 0, FCP_PRIO_CFG_SIZE);
  3175. fcp_prio_addr = ha->flt_region_fcp_prio;
  3176. /* first read the fcp priority data header from flash */
  3177. ha->isp_ops->read_optrom(vha, ha->fcp_prio_cfg,
  3178. fcp_prio_addr << 2, FCP_PRIO_CFG_HDR_SIZE);
  3179. if (!qla24xx_fcp_prio_cfg_valid(vha, ha->fcp_prio_cfg, 0))
  3180. goto fail;
  3181. /* read remaining FCP CMD config data from flash */
  3182. fcp_prio_addr += (FCP_PRIO_CFG_HDR_SIZE >> 2);
  3183. len = ha->fcp_prio_cfg->num_entries * sizeof(struct qla_fcp_prio_entry);
  3184. max_len = FCP_PRIO_CFG_SIZE - FCP_PRIO_CFG_HDR_SIZE;
  3185. ha->isp_ops->read_optrom(vha, &ha->fcp_prio_cfg->entry[0],
  3186. fcp_prio_addr << 2, (len < max_len ? len : max_len));
  3187. /* revalidate the entire FCP priority config data, including entries */
  3188. if (!qla24xx_fcp_prio_cfg_valid(vha, ha->fcp_prio_cfg, 1))
  3189. goto fail;
  3190. ha->flags.fcp_prio_enabled = 1;
  3191. return QLA_SUCCESS;
  3192. fail:
  3193. vfree(ha->fcp_prio_cfg);
  3194. ha->fcp_prio_cfg = NULL;
  3195. return QLA_FUNCTION_FAILED;
  3196. }