qla_sup.c 83 KB

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