qla_sup.c 63 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452
  1. /*
  2. * QLogic Fibre Channel HBA Driver
  3. * Copyright (c) 2003-2008 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/vmalloc.h>
  10. #include <asm/uaccess.h>
  11. static uint16_t qla2x00_nvram_request(scsi_qla_host_t *, uint32_t);
  12. static void qla2x00_nv_deselect(scsi_qla_host_t *);
  13. static void qla2x00_nv_write(scsi_qla_host_t *, uint16_t);
  14. /*
  15. * NVRAM support routines
  16. */
  17. /**
  18. * qla2x00_lock_nvram_access() -
  19. * @ha: HA context
  20. */
  21. static void
  22. qla2x00_lock_nvram_access(scsi_qla_host_t *ha)
  23. {
  24. uint16_t data;
  25. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  26. if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
  27. data = RD_REG_WORD(&reg->nvram);
  28. while (data & NVR_BUSY) {
  29. udelay(100);
  30. data = RD_REG_WORD(&reg->nvram);
  31. }
  32. /* Lock resource */
  33. WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0x1);
  34. RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  35. udelay(5);
  36. data = RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  37. while ((data & BIT_0) == 0) {
  38. /* Lock failed */
  39. udelay(100);
  40. WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0x1);
  41. RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  42. udelay(5);
  43. data = RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  44. }
  45. }
  46. }
  47. /**
  48. * qla2x00_unlock_nvram_access() -
  49. * @ha: HA context
  50. */
  51. static void
  52. qla2x00_unlock_nvram_access(scsi_qla_host_t *ha)
  53. {
  54. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  55. if (!IS_QLA2100(ha) && !IS_QLA2200(ha) && !IS_QLA2300(ha)) {
  56. WRT_REG_WORD(&reg->u.isp2300.host_semaphore, 0);
  57. RD_REG_WORD(&reg->u.isp2300.host_semaphore);
  58. }
  59. }
  60. /**
  61. * qla2x00_get_nvram_word() - Calculates word position in NVRAM and calls the
  62. * request routine to get the word from NVRAM.
  63. * @ha: HA context
  64. * @addr: Address in NVRAM to read
  65. *
  66. * Returns the word read from nvram @addr.
  67. */
  68. static uint16_t
  69. qla2x00_get_nvram_word(scsi_qla_host_t *ha, uint32_t addr)
  70. {
  71. uint16_t data;
  72. uint32_t nv_cmd;
  73. nv_cmd = addr << 16;
  74. nv_cmd |= NV_READ_OP;
  75. data = qla2x00_nvram_request(ha, nv_cmd);
  76. return (data);
  77. }
  78. /**
  79. * qla2x00_write_nvram_word() - Write NVRAM data.
  80. * @ha: HA context
  81. * @addr: Address in NVRAM to write
  82. * @data: word to program
  83. */
  84. static void
  85. qla2x00_write_nvram_word(scsi_qla_host_t *ha, uint32_t addr, uint16_t data)
  86. {
  87. int count;
  88. uint16_t word;
  89. uint32_t nv_cmd, wait_cnt;
  90. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  91. qla2x00_nv_write(ha, NVR_DATA_OUT);
  92. qla2x00_nv_write(ha, 0);
  93. qla2x00_nv_write(ha, 0);
  94. for (word = 0; word < 8; word++)
  95. qla2x00_nv_write(ha, NVR_DATA_OUT);
  96. qla2x00_nv_deselect(ha);
  97. /* Write data */
  98. nv_cmd = (addr << 16) | NV_WRITE_OP;
  99. nv_cmd |= data;
  100. nv_cmd <<= 5;
  101. for (count = 0; count < 27; count++) {
  102. if (nv_cmd & BIT_31)
  103. qla2x00_nv_write(ha, NVR_DATA_OUT);
  104. else
  105. qla2x00_nv_write(ha, 0);
  106. nv_cmd <<= 1;
  107. }
  108. qla2x00_nv_deselect(ha);
  109. /* Wait for NVRAM to become ready */
  110. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  111. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  112. wait_cnt = NVR_WAIT_CNT;
  113. do {
  114. if (!--wait_cnt) {
  115. DEBUG9_10(printk("%s(%ld): NVRAM didn't go ready...\n",
  116. __func__, ha->host_no));
  117. break;
  118. }
  119. NVRAM_DELAY();
  120. word = RD_REG_WORD(&reg->nvram);
  121. } while ((word & NVR_DATA_IN) == 0);
  122. qla2x00_nv_deselect(ha);
  123. /* Disable writes */
  124. qla2x00_nv_write(ha, NVR_DATA_OUT);
  125. for (count = 0; count < 10; count++)
  126. qla2x00_nv_write(ha, 0);
  127. qla2x00_nv_deselect(ha);
  128. }
  129. static int
  130. qla2x00_write_nvram_word_tmo(scsi_qla_host_t *ha, uint32_t addr, uint16_t data,
  131. uint32_t tmo)
  132. {
  133. int ret, count;
  134. uint16_t word;
  135. uint32_t nv_cmd;
  136. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  137. ret = QLA_SUCCESS;
  138. qla2x00_nv_write(ha, NVR_DATA_OUT);
  139. qla2x00_nv_write(ha, 0);
  140. qla2x00_nv_write(ha, 0);
  141. for (word = 0; word < 8; word++)
  142. qla2x00_nv_write(ha, NVR_DATA_OUT);
  143. qla2x00_nv_deselect(ha);
  144. /* Write data */
  145. nv_cmd = (addr << 16) | NV_WRITE_OP;
  146. nv_cmd |= data;
  147. nv_cmd <<= 5;
  148. for (count = 0; count < 27; count++) {
  149. if (nv_cmd & BIT_31)
  150. qla2x00_nv_write(ha, NVR_DATA_OUT);
  151. else
  152. qla2x00_nv_write(ha, 0);
  153. nv_cmd <<= 1;
  154. }
  155. qla2x00_nv_deselect(ha);
  156. /* Wait for NVRAM to become ready */
  157. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  158. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  159. do {
  160. NVRAM_DELAY();
  161. word = RD_REG_WORD(&reg->nvram);
  162. if (!--tmo) {
  163. ret = QLA_FUNCTION_FAILED;
  164. break;
  165. }
  166. } while ((word & NVR_DATA_IN) == 0);
  167. qla2x00_nv_deselect(ha);
  168. /* Disable writes */
  169. qla2x00_nv_write(ha, NVR_DATA_OUT);
  170. for (count = 0; count < 10; count++)
  171. qla2x00_nv_write(ha, 0);
  172. qla2x00_nv_deselect(ha);
  173. return ret;
  174. }
  175. /**
  176. * qla2x00_nvram_request() - Sends read command to NVRAM and gets data from
  177. * NVRAM.
  178. * @ha: HA context
  179. * @nv_cmd: NVRAM command
  180. *
  181. * Bit definitions for NVRAM command:
  182. *
  183. * Bit 26 = start bit
  184. * Bit 25, 24 = opcode
  185. * Bit 23-16 = address
  186. * Bit 15-0 = write data
  187. *
  188. * Returns the word read from nvram @addr.
  189. */
  190. static uint16_t
  191. qla2x00_nvram_request(scsi_qla_host_t *ha, uint32_t nv_cmd)
  192. {
  193. uint8_t cnt;
  194. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  195. uint16_t data = 0;
  196. uint16_t reg_data;
  197. /* Send command to NVRAM. */
  198. nv_cmd <<= 5;
  199. for (cnt = 0; cnt < 11; cnt++) {
  200. if (nv_cmd & BIT_31)
  201. qla2x00_nv_write(ha, NVR_DATA_OUT);
  202. else
  203. qla2x00_nv_write(ha, 0);
  204. nv_cmd <<= 1;
  205. }
  206. /* Read data from NVRAM. */
  207. for (cnt = 0; cnt < 16; cnt++) {
  208. WRT_REG_WORD(&reg->nvram, NVR_SELECT | NVR_CLOCK);
  209. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  210. NVRAM_DELAY();
  211. data <<= 1;
  212. reg_data = RD_REG_WORD(&reg->nvram);
  213. if (reg_data & NVR_DATA_IN)
  214. data |= BIT_0;
  215. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  216. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  217. NVRAM_DELAY();
  218. }
  219. /* Deselect chip. */
  220. WRT_REG_WORD(&reg->nvram, NVR_DESELECT);
  221. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  222. NVRAM_DELAY();
  223. return (data);
  224. }
  225. /**
  226. * qla2x00_nv_write() - Clean NVRAM operations.
  227. * @ha: HA context
  228. */
  229. static void
  230. qla2x00_nv_deselect(scsi_qla_host_t *ha)
  231. {
  232. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  233. WRT_REG_WORD(&reg->nvram, NVR_DESELECT);
  234. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  235. NVRAM_DELAY();
  236. }
  237. /**
  238. * qla2x00_nv_write() - Prepare for NVRAM read/write operation.
  239. * @ha: HA context
  240. * @data: Serial interface selector
  241. */
  242. static void
  243. qla2x00_nv_write(scsi_qla_host_t *ha, uint16_t data)
  244. {
  245. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  246. WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
  247. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  248. NVRAM_DELAY();
  249. WRT_REG_WORD(&reg->nvram, data | NVR_SELECT| NVR_CLOCK |
  250. NVR_WRT_ENABLE);
  251. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  252. NVRAM_DELAY();
  253. WRT_REG_WORD(&reg->nvram, data | NVR_SELECT | NVR_WRT_ENABLE);
  254. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  255. NVRAM_DELAY();
  256. }
  257. /**
  258. * qla2x00_clear_nvram_protection() -
  259. * @ha: HA context
  260. */
  261. static int
  262. qla2x00_clear_nvram_protection(scsi_qla_host_t *ha)
  263. {
  264. int ret, stat;
  265. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  266. uint32_t word, wait_cnt;
  267. uint16_t wprot, wprot_old;
  268. /* Clear NVRAM write protection. */
  269. ret = QLA_FUNCTION_FAILED;
  270. wprot_old = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
  271. stat = qla2x00_write_nvram_word_tmo(ha, ha->nvram_base,
  272. __constant_cpu_to_le16(0x1234), 100000);
  273. wprot = cpu_to_le16(qla2x00_get_nvram_word(ha, ha->nvram_base));
  274. if (stat != QLA_SUCCESS || wprot != 0x1234) {
  275. /* Write enable. */
  276. qla2x00_nv_write(ha, NVR_DATA_OUT);
  277. qla2x00_nv_write(ha, 0);
  278. qla2x00_nv_write(ha, 0);
  279. for (word = 0; word < 8; word++)
  280. qla2x00_nv_write(ha, NVR_DATA_OUT);
  281. qla2x00_nv_deselect(ha);
  282. /* Enable protection register. */
  283. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  284. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  285. qla2x00_nv_write(ha, NVR_PR_ENABLE);
  286. for (word = 0; word < 8; word++)
  287. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  288. qla2x00_nv_deselect(ha);
  289. /* Clear protection register (ffff is cleared). */
  290. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  291. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  292. qla2x00_nv_write(ha, NVR_PR_ENABLE | NVR_DATA_OUT);
  293. for (word = 0; word < 8; word++)
  294. qla2x00_nv_write(ha, NVR_DATA_OUT | NVR_PR_ENABLE);
  295. qla2x00_nv_deselect(ha);
  296. /* Wait for NVRAM to become ready. */
  297. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  298. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  299. wait_cnt = NVR_WAIT_CNT;
  300. do {
  301. if (!--wait_cnt) {
  302. DEBUG9_10(printk("%s(%ld): NVRAM didn't go "
  303. "ready...\n", __func__,
  304. ha->host_no));
  305. break;
  306. }
  307. NVRAM_DELAY();
  308. word = RD_REG_WORD(&reg->nvram);
  309. } while ((word & NVR_DATA_IN) == 0);
  310. if (wait_cnt)
  311. ret = QLA_SUCCESS;
  312. } else
  313. qla2x00_write_nvram_word(ha, ha->nvram_base, wprot_old);
  314. return ret;
  315. }
  316. static void
  317. qla2x00_set_nvram_protection(scsi_qla_host_t *ha, int stat)
  318. {
  319. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  320. uint32_t word, wait_cnt;
  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. DEBUG9_10(printk("%s(%ld): NVRAM didn't go ready...\n",
  352. __func__, ha->host_no));
  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. #define OPTROM_BURST_SIZE 0x1000
  363. #define OPTROM_BURST_DWORDS (OPTROM_BURST_SIZE / 4)
  364. static inline uint32_t
  365. flash_conf_to_access_addr(uint32_t faddr)
  366. {
  367. return FARX_ACCESS_FLASH_CONF | faddr;
  368. }
  369. static inline uint32_t
  370. flash_data_to_access_addr(uint32_t faddr)
  371. {
  372. return FARX_ACCESS_FLASH_DATA | faddr;
  373. }
  374. static inline uint32_t
  375. nvram_conf_to_access_addr(uint32_t naddr)
  376. {
  377. return FARX_ACCESS_NVRAM_CONF | naddr;
  378. }
  379. static inline uint32_t
  380. nvram_data_to_access_addr(uint32_t naddr)
  381. {
  382. return FARX_ACCESS_NVRAM_DATA | naddr;
  383. }
  384. static uint32_t
  385. qla24xx_read_flash_dword(scsi_qla_host_t *ha, uint32_t addr)
  386. {
  387. int rval;
  388. uint32_t cnt, data;
  389. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  390. WRT_REG_DWORD(&reg->flash_addr, addr & ~FARX_DATA_FLAG);
  391. /* Wait for READ cycle to complete. */
  392. rval = QLA_SUCCESS;
  393. for (cnt = 3000;
  394. (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) == 0 &&
  395. rval == QLA_SUCCESS; cnt--) {
  396. if (cnt)
  397. udelay(10);
  398. else
  399. rval = QLA_FUNCTION_TIMEOUT;
  400. cond_resched();
  401. }
  402. /* TODO: What happens if we time out? */
  403. data = 0xDEADDEAD;
  404. if (rval == QLA_SUCCESS)
  405. data = RD_REG_DWORD(&reg->flash_data);
  406. return data;
  407. }
  408. uint32_t *
  409. qla24xx_read_flash_data(scsi_qla_host_t *ha, uint32_t *dwptr, uint32_t faddr,
  410. uint32_t dwords)
  411. {
  412. uint32_t i;
  413. /* Dword reads to flash. */
  414. for (i = 0; i < dwords; i++, faddr++)
  415. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  416. flash_data_to_access_addr(faddr)));
  417. return dwptr;
  418. }
  419. static int
  420. qla24xx_write_flash_dword(scsi_qla_host_t *ha, uint32_t addr, uint32_t data)
  421. {
  422. int rval;
  423. uint32_t cnt;
  424. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  425. WRT_REG_DWORD(&reg->flash_data, data);
  426. RD_REG_DWORD(&reg->flash_data); /* PCI Posting. */
  427. WRT_REG_DWORD(&reg->flash_addr, addr | FARX_DATA_FLAG);
  428. /* Wait for Write cycle to complete. */
  429. rval = QLA_SUCCESS;
  430. for (cnt = 500000; (RD_REG_DWORD(&reg->flash_addr) & FARX_DATA_FLAG) &&
  431. rval == QLA_SUCCESS; cnt--) {
  432. if (cnt)
  433. udelay(10);
  434. else
  435. rval = QLA_FUNCTION_TIMEOUT;
  436. cond_resched();
  437. }
  438. return rval;
  439. }
  440. static void
  441. qla24xx_get_flash_manufacturer(scsi_qla_host_t *ha, uint8_t *man_id,
  442. uint8_t *flash_id)
  443. {
  444. uint32_t ids;
  445. ids = qla24xx_read_flash_dword(ha, flash_data_to_access_addr(0xd03ab));
  446. *man_id = LSB(ids);
  447. *flash_id = MSB(ids);
  448. /* Check if man_id and flash_id are valid. */
  449. if (ids != 0xDEADDEAD && (*man_id == 0 || *flash_id == 0)) {
  450. /* Read information using 0x9f opcode
  451. * Device ID, Mfg ID would be read in the format:
  452. * <Ext Dev Info><Device ID Part2><Device ID Part 1><Mfg ID>
  453. * Example: ATMEL 0x00 01 45 1F
  454. * Extract MFG and Dev ID from last two bytes.
  455. */
  456. ids = qla24xx_read_flash_dword(ha,
  457. flash_data_to_access_addr(0xd009f));
  458. *man_id = LSB(ids);
  459. *flash_id = MSB(ids);
  460. }
  461. }
  462. void
  463. qla2xxx_get_flash_info(scsi_qla_host_t *ha)
  464. {
  465. #define FLASH_BLK_SIZE_32K 0x8000
  466. #define FLASH_BLK_SIZE_64K 0x10000
  467. uint16_t cnt, chksum;
  468. uint16_t *wptr;
  469. struct qla_fdt_layout *fdt;
  470. uint8_t man_id, flash_id;
  471. if (!IS_QLA24XX_TYPE(ha) && !IS_QLA25XX(ha))
  472. return;
  473. wptr = (uint16_t *)ha->request_ring;
  474. fdt = (struct qla_fdt_layout *)ha->request_ring;
  475. ha->isp_ops->read_optrom(ha, (uint8_t *)ha->request_ring,
  476. FA_FLASH_DESCR_ADDR << 2, OPTROM_BURST_SIZE);
  477. if (*wptr == __constant_cpu_to_le16(0xffff))
  478. goto no_flash_data;
  479. if (fdt->sig[0] != 'Q' || fdt->sig[1] != 'L' || fdt->sig[2] != 'I' ||
  480. fdt->sig[3] != 'D')
  481. goto no_flash_data;
  482. for (cnt = 0, chksum = 0; cnt < sizeof(struct qla_fdt_layout) >> 1;
  483. cnt++)
  484. chksum += le16_to_cpu(*wptr++);
  485. if (chksum) {
  486. DEBUG2(qla_printk(KERN_INFO, ha, "Inconsistent FDT detected: "
  487. "checksum=0x%x id=%c version=0x%x.\n", chksum, fdt->sig[0],
  488. le16_to_cpu(fdt->version)));
  489. DEBUG9(qla2x00_dump_buffer((uint8_t *)fdt, sizeof(*fdt)));
  490. goto no_flash_data;
  491. }
  492. ha->fdt_odd_index = le16_to_cpu(fdt->man_id) == 0x1f;
  493. ha->fdt_wrt_disable = fdt->wrt_disable_bits;
  494. ha->fdt_erase_cmd = flash_conf_to_access_addr(0x0300 | fdt->erase_cmd);
  495. ha->fdt_block_size = le32_to_cpu(fdt->block_size);
  496. if (fdt->unprotect_sec_cmd) {
  497. ha->fdt_unprotect_sec_cmd = flash_conf_to_access_addr(0x0300 |
  498. fdt->unprotect_sec_cmd);
  499. ha->fdt_protect_sec_cmd = fdt->protect_sec_cmd ?
  500. flash_conf_to_access_addr(0x0300 | fdt->protect_sec_cmd):
  501. flash_conf_to_access_addr(0x0336);
  502. }
  503. DEBUG2(qla_printk(KERN_DEBUG, ha, "Flash[FDT]: (0x%x/0x%x) erase=0x%x "
  504. "pro=%x upro=%x idx=%d wrtd=0x%x blk=0x%x.\n",
  505. le16_to_cpu(fdt->man_id), le16_to_cpu(fdt->id), ha->fdt_erase_cmd,
  506. ha->fdt_protect_sec_cmd, ha->fdt_unprotect_sec_cmd,
  507. ha->fdt_odd_index, ha->fdt_wrt_disable, ha->fdt_block_size));
  508. return;
  509. no_flash_data:
  510. qla24xx_get_flash_manufacturer(ha, &man_id, &flash_id);
  511. ha->fdt_wrt_disable = 0x9c;
  512. ha->fdt_erase_cmd = flash_conf_to_access_addr(0x03d8);
  513. switch (man_id) {
  514. case 0xbf: /* STT flash. */
  515. if (flash_id == 0x8e)
  516. ha->fdt_block_size = FLASH_BLK_SIZE_64K;
  517. else
  518. ha->fdt_block_size = FLASH_BLK_SIZE_32K;
  519. if (flash_id == 0x80)
  520. ha->fdt_erase_cmd = flash_conf_to_access_addr(0x0352);
  521. break;
  522. case 0x13: /* ST M25P80. */
  523. ha->fdt_block_size = FLASH_BLK_SIZE_64K;
  524. break;
  525. case 0x1f: /* Atmel 26DF081A. */
  526. ha->fdt_odd_index = 1;
  527. ha->fdt_block_size = FLASH_BLK_SIZE_64K;
  528. ha->fdt_erase_cmd = flash_conf_to_access_addr(0x0320);
  529. ha->fdt_unprotect_sec_cmd = flash_conf_to_access_addr(0x0339);
  530. ha->fdt_protect_sec_cmd = flash_conf_to_access_addr(0x0336);
  531. break;
  532. default:
  533. /* Default to 64 kb sector size. */
  534. ha->fdt_block_size = FLASH_BLK_SIZE_64K;
  535. break;
  536. }
  537. DEBUG2(qla_printk(KERN_DEBUG, ha, "Flash[MID]: (0x%x/0x%x) erase=0x%x "
  538. "pro=%x upro=%x idx=%d wrtd=0x%x blk=0x%x.\n", man_id, flash_id,
  539. ha->fdt_erase_cmd, ha->fdt_protect_sec_cmd,
  540. ha->fdt_unprotect_sec_cmd, ha->fdt_odd_index, ha->fdt_wrt_disable,
  541. ha->fdt_block_size));
  542. }
  543. static void
  544. qla24xx_unprotect_flash(scsi_qla_host_t *ha)
  545. {
  546. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  547. /* Enable flash write. */
  548. WRT_REG_DWORD(&reg->ctrl_status,
  549. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  550. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  551. if (!ha->fdt_wrt_disable)
  552. return;
  553. /* Disable flash write-protection. */
  554. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0);
  555. /* Some flash parts need an additional zero-write to clear bits.*/
  556. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101), 0);
  557. }
  558. static void
  559. qla24xx_protect_flash(scsi_qla_host_t *ha)
  560. {
  561. uint32_t cnt;
  562. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  563. if (!ha->fdt_wrt_disable)
  564. goto skip_wrt_protect;
  565. /* Enable flash write-protection and wait for completion. */
  566. qla24xx_write_flash_dword(ha, flash_conf_to_access_addr(0x101),
  567. ha->fdt_wrt_disable);
  568. for (cnt = 300; cnt &&
  569. qla24xx_read_flash_dword(ha,
  570. flash_conf_to_access_addr(0x005)) & BIT_0;
  571. cnt--) {
  572. udelay(10);
  573. }
  574. skip_wrt_protect:
  575. /* Disable flash write. */
  576. WRT_REG_DWORD(&reg->ctrl_status,
  577. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  578. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  579. }
  580. static int
  581. qla24xx_write_flash_data(scsi_qla_host_t *ha, uint32_t *dwptr, uint32_t faddr,
  582. uint32_t dwords)
  583. {
  584. int ret;
  585. uint32_t liter, miter;
  586. uint32_t sec_mask, rest_addr;
  587. uint32_t fdata, findex;
  588. dma_addr_t optrom_dma;
  589. void *optrom = NULL;
  590. uint32_t *s, *d;
  591. ret = QLA_SUCCESS;
  592. /* Prepare burst-capable write on supported ISPs. */
  593. if (IS_QLA25XX(ha) && !(faddr & 0xfff) &&
  594. dwords > OPTROM_BURST_DWORDS) {
  595. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  596. &optrom_dma, GFP_KERNEL);
  597. if (!optrom) {
  598. qla_printk(KERN_DEBUG, ha,
  599. "Unable to allocate memory for optrom burst write "
  600. "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
  601. }
  602. }
  603. rest_addr = (ha->fdt_block_size >> 2) - 1;
  604. sec_mask = 0x80000 - (ha->fdt_block_size >> 2);
  605. qla24xx_unprotect_flash(ha);
  606. for (liter = 0; liter < dwords; liter++, faddr++, dwptr++) {
  607. if (ha->fdt_odd_index) {
  608. findex = faddr << 2;
  609. fdata = findex & sec_mask;
  610. } else {
  611. findex = faddr;
  612. fdata = (findex & sec_mask) << 2;
  613. }
  614. /* Are we at the beginning of a sector? */
  615. if ((findex & rest_addr) == 0) {
  616. /* Do sector unprotect. */
  617. if (ha->fdt_unprotect_sec_cmd)
  618. qla24xx_write_flash_dword(ha,
  619. ha->fdt_unprotect_sec_cmd,
  620. (fdata & 0xff00) | ((fdata << 16) &
  621. 0xff0000) | ((fdata >> 16) & 0xff));
  622. ret = qla24xx_write_flash_dword(ha, ha->fdt_erase_cmd,
  623. (fdata & 0xff00) |((fdata << 16) &
  624. 0xff0000) | ((fdata >> 16) & 0xff));
  625. if (ret != QLA_SUCCESS) {
  626. DEBUG9(printk("%s(%ld) Unable to flash "
  627. "sector: address=%x.\n", __func__,
  628. ha->host_no, faddr));
  629. break;
  630. }
  631. }
  632. /* Go with burst-write. */
  633. if (optrom && (liter + OPTROM_BURST_DWORDS) <= dwords) {
  634. /* Copy data to DMA'ble buffer. */
  635. for (miter = 0, s = optrom, d = dwptr;
  636. miter < OPTROM_BURST_DWORDS; miter++, s++, d++)
  637. *s = cpu_to_le32(*d);
  638. ret = qla2x00_load_ram(ha, optrom_dma,
  639. flash_data_to_access_addr(faddr),
  640. OPTROM_BURST_DWORDS);
  641. if (ret != QLA_SUCCESS) {
  642. qla_printk(KERN_WARNING, ha,
  643. "Unable to burst-write optrom segment "
  644. "(%x/%x/%llx).\n", ret,
  645. flash_data_to_access_addr(faddr),
  646. (unsigned long long)optrom_dma);
  647. qla_printk(KERN_WARNING, ha,
  648. "Reverting to slow-write.\n");
  649. dma_free_coherent(&ha->pdev->dev,
  650. OPTROM_BURST_SIZE, optrom, optrom_dma);
  651. optrom = NULL;
  652. } else {
  653. liter += OPTROM_BURST_DWORDS - 1;
  654. faddr += OPTROM_BURST_DWORDS - 1;
  655. dwptr += OPTROM_BURST_DWORDS - 1;
  656. continue;
  657. }
  658. }
  659. ret = qla24xx_write_flash_dword(ha,
  660. flash_data_to_access_addr(faddr), cpu_to_le32(*dwptr));
  661. if (ret != QLA_SUCCESS) {
  662. DEBUG9(printk("%s(%ld) Unable to program flash "
  663. "address=%x data=%x.\n", __func__,
  664. ha->host_no, faddr, *dwptr));
  665. break;
  666. }
  667. /* Do sector protect. */
  668. if (ha->fdt_unprotect_sec_cmd &&
  669. ((faddr & rest_addr) == rest_addr))
  670. qla24xx_write_flash_dword(ha,
  671. ha->fdt_protect_sec_cmd,
  672. (fdata & 0xff00) | ((fdata << 16) &
  673. 0xff0000) | ((fdata >> 16) & 0xff));
  674. }
  675. qla24xx_protect_flash(ha);
  676. if (optrom)
  677. dma_free_coherent(&ha->pdev->dev,
  678. OPTROM_BURST_SIZE, optrom, optrom_dma);
  679. return ret;
  680. }
  681. uint8_t *
  682. qla2x00_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  683. uint32_t bytes)
  684. {
  685. uint32_t i;
  686. uint16_t *wptr;
  687. /* Word reads to NVRAM via registers. */
  688. wptr = (uint16_t *)buf;
  689. qla2x00_lock_nvram_access(ha);
  690. for (i = 0; i < bytes >> 1; i++, naddr++)
  691. wptr[i] = cpu_to_le16(qla2x00_get_nvram_word(ha,
  692. naddr));
  693. qla2x00_unlock_nvram_access(ha);
  694. return buf;
  695. }
  696. uint8_t *
  697. qla24xx_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  698. uint32_t bytes)
  699. {
  700. uint32_t i;
  701. uint32_t *dwptr;
  702. /* Dword reads to flash. */
  703. dwptr = (uint32_t *)buf;
  704. for (i = 0; i < bytes >> 2; i++, naddr++)
  705. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  706. nvram_data_to_access_addr(naddr)));
  707. return buf;
  708. }
  709. int
  710. qla2x00_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  711. uint32_t bytes)
  712. {
  713. int ret, stat;
  714. uint32_t i;
  715. uint16_t *wptr;
  716. unsigned long flags;
  717. ret = QLA_SUCCESS;
  718. spin_lock_irqsave(&ha->hardware_lock, flags);
  719. qla2x00_lock_nvram_access(ha);
  720. /* Disable NVRAM write-protection. */
  721. stat = qla2x00_clear_nvram_protection(ha);
  722. wptr = (uint16_t *)buf;
  723. for (i = 0; i < bytes >> 1; i++, naddr++) {
  724. qla2x00_write_nvram_word(ha, naddr,
  725. cpu_to_le16(*wptr));
  726. wptr++;
  727. }
  728. /* Enable NVRAM write-protection. */
  729. qla2x00_set_nvram_protection(ha, stat);
  730. qla2x00_unlock_nvram_access(ha);
  731. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  732. return ret;
  733. }
  734. int
  735. qla24xx_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  736. uint32_t bytes)
  737. {
  738. int ret;
  739. uint32_t i;
  740. uint32_t *dwptr;
  741. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  742. ret = QLA_SUCCESS;
  743. /* Enable flash write. */
  744. WRT_REG_DWORD(&reg->ctrl_status,
  745. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  746. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  747. /* Disable NVRAM write-protection. */
  748. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  749. 0);
  750. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  751. 0);
  752. /* Dword writes to flash. */
  753. dwptr = (uint32_t *)buf;
  754. for (i = 0; i < bytes >> 2; i++, naddr++, dwptr++) {
  755. ret = qla24xx_write_flash_dword(ha,
  756. nvram_data_to_access_addr(naddr),
  757. cpu_to_le32(*dwptr));
  758. if (ret != QLA_SUCCESS) {
  759. DEBUG9(printk("%s(%ld) Unable to program "
  760. "nvram address=%x data=%x.\n", __func__,
  761. ha->host_no, naddr, *dwptr));
  762. break;
  763. }
  764. }
  765. /* Enable NVRAM write-protection. */
  766. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  767. 0x8c);
  768. /* Disable flash write. */
  769. WRT_REG_DWORD(&reg->ctrl_status,
  770. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  771. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  772. return ret;
  773. }
  774. uint8_t *
  775. qla25xx_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  776. uint32_t bytes)
  777. {
  778. uint32_t i;
  779. uint32_t *dwptr;
  780. /* Dword reads to flash. */
  781. dwptr = (uint32_t *)buf;
  782. for (i = 0; i < bytes >> 2; i++, naddr++)
  783. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  784. flash_data_to_access_addr(FA_VPD_NVRAM_ADDR | naddr)));
  785. return buf;
  786. }
  787. int
  788. qla25xx_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  789. uint32_t bytes)
  790. {
  791. #define RMW_BUFFER_SIZE (64 * 1024)
  792. uint8_t *dbuf;
  793. dbuf = vmalloc(RMW_BUFFER_SIZE);
  794. if (!dbuf)
  795. return QLA_MEMORY_ALLOC_FAILED;
  796. ha->isp_ops->read_optrom(ha, dbuf, FA_VPD_NVRAM_ADDR << 2,
  797. RMW_BUFFER_SIZE);
  798. memcpy(dbuf + (naddr << 2), buf, bytes);
  799. ha->isp_ops->write_optrom(ha, dbuf, FA_VPD_NVRAM_ADDR << 2,
  800. RMW_BUFFER_SIZE);
  801. vfree(dbuf);
  802. return QLA_SUCCESS;
  803. }
  804. static inline void
  805. qla2x00_flip_colors(scsi_qla_host_t *ha, uint16_t *pflags)
  806. {
  807. if (IS_QLA2322(ha)) {
  808. /* Flip all colors. */
  809. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  810. /* Turn off. */
  811. ha->beacon_color_state = 0;
  812. *pflags = GPIO_LED_ALL_OFF;
  813. } else {
  814. /* Turn on. */
  815. ha->beacon_color_state = QLA_LED_ALL_ON;
  816. *pflags = GPIO_LED_RGA_ON;
  817. }
  818. } else {
  819. /* Flip green led only. */
  820. if (ha->beacon_color_state == QLA_LED_GRN_ON) {
  821. /* Turn off. */
  822. ha->beacon_color_state = 0;
  823. *pflags = GPIO_LED_GREEN_OFF_AMBER_OFF;
  824. } else {
  825. /* Turn on. */
  826. ha->beacon_color_state = QLA_LED_GRN_ON;
  827. *pflags = GPIO_LED_GREEN_ON_AMBER_OFF;
  828. }
  829. }
  830. }
  831. #define PIO_REG(h, r) ((h)->pio_address + offsetof(struct device_reg_2xxx, r))
  832. void
  833. qla2x00_beacon_blink(struct scsi_qla_host *ha)
  834. {
  835. uint16_t gpio_enable;
  836. uint16_t gpio_data;
  837. uint16_t led_color = 0;
  838. unsigned long flags;
  839. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  840. spin_lock_irqsave(&ha->hardware_lock, flags);
  841. /* Save the Original GPIOE. */
  842. if (ha->pio_address) {
  843. gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
  844. gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
  845. } else {
  846. gpio_enable = RD_REG_WORD(&reg->gpioe);
  847. gpio_data = RD_REG_WORD(&reg->gpiod);
  848. }
  849. /* Set the modified gpio_enable values */
  850. gpio_enable |= GPIO_LED_MASK;
  851. if (ha->pio_address) {
  852. WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
  853. } else {
  854. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  855. RD_REG_WORD(&reg->gpioe);
  856. }
  857. qla2x00_flip_colors(ha, &led_color);
  858. /* Clear out any previously set LED color. */
  859. gpio_data &= ~GPIO_LED_MASK;
  860. /* Set the new input LED color to GPIOD. */
  861. gpio_data |= led_color;
  862. /* Set the modified gpio_data values */
  863. if (ha->pio_address) {
  864. WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
  865. } else {
  866. WRT_REG_WORD(&reg->gpiod, gpio_data);
  867. RD_REG_WORD(&reg->gpiod);
  868. }
  869. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  870. }
  871. int
  872. qla2x00_beacon_on(struct scsi_qla_host *ha)
  873. {
  874. uint16_t gpio_enable;
  875. uint16_t gpio_data;
  876. unsigned long flags;
  877. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  878. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  879. ha->fw_options[1] |= FO1_DISABLE_GPIO6_7;
  880. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  881. qla_printk(KERN_WARNING, ha,
  882. "Unable to update fw options (beacon on).\n");
  883. return QLA_FUNCTION_FAILED;
  884. }
  885. /* Turn off LEDs. */
  886. spin_lock_irqsave(&ha->hardware_lock, flags);
  887. if (ha->pio_address) {
  888. gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
  889. gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
  890. } else {
  891. gpio_enable = RD_REG_WORD(&reg->gpioe);
  892. gpio_data = RD_REG_WORD(&reg->gpiod);
  893. }
  894. gpio_enable |= GPIO_LED_MASK;
  895. /* Set the modified gpio_enable values. */
  896. if (ha->pio_address) {
  897. WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
  898. } else {
  899. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  900. RD_REG_WORD(&reg->gpioe);
  901. }
  902. /* Clear out previously set LED colour. */
  903. gpio_data &= ~GPIO_LED_MASK;
  904. if (ha->pio_address) {
  905. WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
  906. } else {
  907. WRT_REG_WORD(&reg->gpiod, gpio_data);
  908. RD_REG_WORD(&reg->gpiod);
  909. }
  910. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  911. /*
  912. * Let the per HBA timer kick off the blinking process based on
  913. * the following flags. No need to do anything else now.
  914. */
  915. ha->beacon_blink_led = 1;
  916. ha->beacon_color_state = 0;
  917. return QLA_SUCCESS;
  918. }
  919. int
  920. qla2x00_beacon_off(struct scsi_qla_host *ha)
  921. {
  922. int rval = QLA_SUCCESS;
  923. ha->beacon_blink_led = 0;
  924. /* Set the on flag so when it gets flipped it will be off. */
  925. if (IS_QLA2322(ha))
  926. ha->beacon_color_state = QLA_LED_ALL_ON;
  927. else
  928. ha->beacon_color_state = QLA_LED_GRN_ON;
  929. ha->isp_ops->beacon_blink(ha); /* This turns green LED off */
  930. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  931. ha->fw_options[1] &= ~FO1_DISABLE_GPIO6_7;
  932. rval = qla2x00_set_fw_options(ha, ha->fw_options);
  933. if (rval != QLA_SUCCESS)
  934. qla_printk(KERN_WARNING, ha,
  935. "Unable to update fw options (beacon off).\n");
  936. return rval;
  937. }
  938. static inline void
  939. qla24xx_flip_colors(scsi_qla_host_t *ha, uint16_t *pflags)
  940. {
  941. /* Flip all colors. */
  942. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  943. /* Turn off. */
  944. ha->beacon_color_state = 0;
  945. *pflags = 0;
  946. } else {
  947. /* Turn on. */
  948. ha->beacon_color_state = QLA_LED_ALL_ON;
  949. *pflags = GPDX_LED_YELLOW_ON | GPDX_LED_AMBER_ON;
  950. }
  951. }
  952. void
  953. qla24xx_beacon_blink(struct scsi_qla_host *ha)
  954. {
  955. uint16_t led_color = 0;
  956. uint32_t gpio_data;
  957. unsigned long flags;
  958. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  959. /* Save the Original GPIOD. */
  960. spin_lock_irqsave(&ha->hardware_lock, flags);
  961. gpio_data = RD_REG_DWORD(&reg->gpiod);
  962. /* Enable the gpio_data reg for update. */
  963. gpio_data |= GPDX_LED_UPDATE_MASK;
  964. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  965. gpio_data = RD_REG_DWORD(&reg->gpiod);
  966. /* Set the color bits. */
  967. qla24xx_flip_colors(ha, &led_color);
  968. /* Clear out any previously set LED color. */
  969. gpio_data &= ~GPDX_LED_COLOR_MASK;
  970. /* Set the new input LED color to GPIOD. */
  971. gpio_data |= led_color;
  972. /* Set the modified gpio_data values. */
  973. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  974. gpio_data = RD_REG_DWORD(&reg->gpiod);
  975. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  976. }
  977. int
  978. qla24xx_beacon_on(struct scsi_qla_host *ha)
  979. {
  980. uint32_t gpio_data;
  981. unsigned long flags;
  982. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  983. if (ha->beacon_blink_led == 0) {
  984. /* Enable firmware for update */
  985. ha->fw_options[1] |= ADD_FO1_DISABLE_GPIO_LED_CTRL;
  986. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS)
  987. return QLA_FUNCTION_FAILED;
  988. if (qla2x00_get_fw_options(ha, ha->fw_options) !=
  989. QLA_SUCCESS) {
  990. qla_printk(KERN_WARNING, ha,
  991. "Unable to update fw options (beacon on).\n");
  992. return QLA_FUNCTION_FAILED;
  993. }
  994. spin_lock_irqsave(&ha->hardware_lock, flags);
  995. gpio_data = RD_REG_DWORD(&reg->gpiod);
  996. /* Enable the gpio_data reg for update. */
  997. gpio_data |= GPDX_LED_UPDATE_MASK;
  998. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  999. RD_REG_DWORD(&reg->gpiod);
  1000. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1001. }
  1002. /* So all colors blink together. */
  1003. ha->beacon_color_state = 0;
  1004. /* Let the per HBA timer kick off the blinking process. */
  1005. ha->beacon_blink_led = 1;
  1006. return QLA_SUCCESS;
  1007. }
  1008. int
  1009. qla24xx_beacon_off(struct scsi_qla_host *ha)
  1010. {
  1011. uint32_t gpio_data;
  1012. unsigned long flags;
  1013. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1014. ha->beacon_blink_led = 0;
  1015. ha->beacon_color_state = QLA_LED_ALL_ON;
  1016. ha->isp_ops->beacon_blink(ha); /* Will flip to all off. */
  1017. /* Give control back to firmware. */
  1018. spin_lock_irqsave(&ha->hardware_lock, flags);
  1019. gpio_data = RD_REG_DWORD(&reg->gpiod);
  1020. /* Disable the gpio_data reg for update. */
  1021. gpio_data &= ~GPDX_LED_UPDATE_MASK;
  1022. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  1023. RD_REG_DWORD(&reg->gpiod);
  1024. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1025. ha->fw_options[1] &= ~ADD_FO1_DISABLE_GPIO_LED_CTRL;
  1026. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  1027. qla_printk(KERN_WARNING, ha,
  1028. "Unable to update fw options (beacon off).\n");
  1029. return QLA_FUNCTION_FAILED;
  1030. }
  1031. if (qla2x00_get_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  1032. qla_printk(KERN_WARNING, ha,
  1033. "Unable to get fw options (beacon off).\n");
  1034. return QLA_FUNCTION_FAILED;
  1035. }
  1036. return QLA_SUCCESS;
  1037. }
  1038. /*
  1039. * Flash support routines
  1040. */
  1041. /**
  1042. * qla2x00_flash_enable() - Setup flash for reading and writing.
  1043. * @ha: HA context
  1044. */
  1045. static void
  1046. qla2x00_flash_enable(scsi_qla_host_t *ha)
  1047. {
  1048. uint16_t data;
  1049. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1050. data = RD_REG_WORD(&reg->ctrl_status);
  1051. data |= CSR_FLASH_ENABLE;
  1052. WRT_REG_WORD(&reg->ctrl_status, data);
  1053. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1054. }
  1055. /**
  1056. * qla2x00_flash_disable() - Disable flash and allow RISC to run.
  1057. * @ha: HA context
  1058. */
  1059. static void
  1060. qla2x00_flash_disable(scsi_qla_host_t *ha)
  1061. {
  1062. uint16_t data;
  1063. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1064. data = RD_REG_WORD(&reg->ctrl_status);
  1065. data &= ~(CSR_FLASH_ENABLE);
  1066. WRT_REG_WORD(&reg->ctrl_status, data);
  1067. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1068. }
  1069. /**
  1070. * qla2x00_read_flash_byte() - Reads a byte from flash
  1071. * @ha: HA context
  1072. * @addr: Address in flash to read
  1073. *
  1074. * A word is read from the chip, but, only the lower byte is valid.
  1075. *
  1076. * Returns the byte read from flash @addr.
  1077. */
  1078. static uint8_t
  1079. qla2x00_read_flash_byte(scsi_qla_host_t *ha, uint32_t addr)
  1080. {
  1081. uint16_t data;
  1082. uint16_t bank_select;
  1083. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1084. bank_select = RD_REG_WORD(&reg->ctrl_status);
  1085. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1086. /* Specify 64K address range: */
  1087. /* clear out Module Select and Flash Address bits [19:16]. */
  1088. bank_select &= ~0xf8;
  1089. bank_select |= addr >> 12 & 0xf0;
  1090. bank_select |= CSR_FLASH_64K_BANK;
  1091. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1092. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1093. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1094. data = RD_REG_WORD(&reg->flash_data);
  1095. return (uint8_t)data;
  1096. }
  1097. /* Setup bit 16 of flash address. */
  1098. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1099. bank_select |= CSR_FLASH_64K_BANK;
  1100. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1101. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1102. } else if (((addr & BIT_16) == 0) &&
  1103. (bank_select & CSR_FLASH_64K_BANK)) {
  1104. bank_select &= ~(CSR_FLASH_64K_BANK);
  1105. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1106. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1107. }
  1108. /* Always perform IO mapped accesses to the FLASH registers. */
  1109. if (ha->pio_address) {
  1110. uint16_t data2;
  1111. WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
  1112. do {
  1113. data = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
  1114. barrier();
  1115. cpu_relax();
  1116. data2 = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
  1117. } while (data != data2);
  1118. } else {
  1119. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1120. data = qla2x00_debounce_register(&reg->flash_data);
  1121. }
  1122. return (uint8_t)data;
  1123. }
  1124. /**
  1125. * qla2x00_write_flash_byte() - Write a byte to flash
  1126. * @ha: HA context
  1127. * @addr: Address in flash to write
  1128. * @data: Data to write
  1129. */
  1130. static void
  1131. qla2x00_write_flash_byte(scsi_qla_host_t *ha, uint32_t addr, uint8_t data)
  1132. {
  1133. uint16_t bank_select;
  1134. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1135. bank_select = RD_REG_WORD(&reg->ctrl_status);
  1136. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1137. /* Specify 64K address range: */
  1138. /* clear out Module Select and Flash Address bits [19:16]. */
  1139. bank_select &= ~0xf8;
  1140. bank_select |= addr >> 12 & 0xf0;
  1141. bank_select |= CSR_FLASH_64K_BANK;
  1142. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1143. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1144. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1145. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1146. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  1147. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1148. return;
  1149. }
  1150. /* Setup bit 16 of flash address. */
  1151. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1152. bank_select |= CSR_FLASH_64K_BANK;
  1153. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1154. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1155. } else if (((addr & BIT_16) == 0) &&
  1156. (bank_select & CSR_FLASH_64K_BANK)) {
  1157. bank_select &= ~(CSR_FLASH_64K_BANK);
  1158. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1159. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1160. }
  1161. /* Always perform IO mapped accesses to the FLASH registers. */
  1162. if (ha->pio_address) {
  1163. WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
  1164. WRT_REG_WORD_PIO(PIO_REG(ha, flash_data), (uint16_t)data);
  1165. } else {
  1166. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1167. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1168. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  1169. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1170. }
  1171. }
  1172. /**
  1173. * qla2x00_poll_flash() - Polls flash for completion.
  1174. * @ha: HA context
  1175. * @addr: Address in flash to poll
  1176. * @poll_data: Data to be polled
  1177. * @man_id: Flash manufacturer ID
  1178. * @flash_id: Flash ID
  1179. *
  1180. * This function polls the device until bit 7 of what is read matches data
  1181. * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
  1182. * out (a fatal error). The flash book recommeds reading bit 7 again after
  1183. * reading bit 5 as a 1.
  1184. *
  1185. * Returns 0 on success, else non-zero.
  1186. */
  1187. static int
  1188. qla2x00_poll_flash(scsi_qla_host_t *ha, uint32_t addr, uint8_t poll_data,
  1189. uint8_t man_id, uint8_t flash_id)
  1190. {
  1191. int status;
  1192. uint8_t flash_data;
  1193. uint32_t cnt;
  1194. status = 1;
  1195. /* Wait for 30 seconds for command to finish. */
  1196. poll_data &= BIT_7;
  1197. for (cnt = 3000000; cnt; cnt--) {
  1198. flash_data = qla2x00_read_flash_byte(ha, addr);
  1199. if ((flash_data & BIT_7) == poll_data) {
  1200. status = 0;
  1201. break;
  1202. }
  1203. if (man_id != 0x40 && man_id != 0xda) {
  1204. if ((flash_data & BIT_5) && cnt > 2)
  1205. cnt = 2;
  1206. }
  1207. udelay(10);
  1208. barrier();
  1209. cond_resched();
  1210. }
  1211. return status;
  1212. }
  1213. /**
  1214. * qla2x00_program_flash_address() - Programs a flash address
  1215. * @ha: HA context
  1216. * @addr: Address in flash to program
  1217. * @data: Data to be written in flash
  1218. * @man_id: Flash manufacturer ID
  1219. * @flash_id: Flash ID
  1220. *
  1221. * Returns 0 on success, else non-zero.
  1222. */
  1223. static int
  1224. qla2x00_program_flash_address(scsi_qla_host_t *ha, uint32_t addr, uint8_t data,
  1225. uint8_t man_id, uint8_t flash_id)
  1226. {
  1227. /* Write Program Command Sequence. */
  1228. if (IS_OEM_001(ha)) {
  1229. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1230. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1231. qla2x00_write_flash_byte(ha, 0xaaa, 0xa0);
  1232. qla2x00_write_flash_byte(ha, addr, data);
  1233. } else {
  1234. if (man_id == 0xda && flash_id == 0xc1) {
  1235. qla2x00_write_flash_byte(ha, addr, data);
  1236. if (addr & 0x7e)
  1237. return 0;
  1238. } else {
  1239. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1240. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1241. qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
  1242. qla2x00_write_flash_byte(ha, addr, data);
  1243. }
  1244. }
  1245. udelay(150);
  1246. /* Wait for write to complete. */
  1247. return qla2x00_poll_flash(ha, addr, data, man_id, flash_id);
  1248. }
  1249. /**
  1250. * qla2x00_erase_flash() - Erase the flash.
  1251. * @ha: HA context
  1252. * @man_id: Flash manufacturer ID
  1253. * @flash_id: Flash ID
  1254. *
  1255. * Returns 0 on success, else non-zero.
  1256. */
  1257. static int
  1258. qla2x00_erase_flash(scsi_qla_host_t *ha, uint8_t man_id, uint8_t flash_id)
  1259. {
  1260. /* Individual Sector Erase Command Sequence */
  1261. if (IS_OEM_001(ha)) {
  1262. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1263. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1264. qla2x00_write_flash_byte(ha, 0xaaa, 0x80);
  1265. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1266. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1267. qla2x00_write_flash_byte(ha, 0xaaa, 0x10);
  1268. } else {
  1269. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1270. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1271. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1272. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1273. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1274. qla2x00_write_flash_byte(ha, 0x5555, 0x10);
  1275. }
  1276. udelay(150);
  1277. /* Wait for erase to complete. */
  1278. return qla2x00_poll_flash(ha, 0x00, 0x80, man_id, flash_id);
  1279. }
  1280. /**
  1281. * qla2x00_erase_flash_sector() - Erase a flash sector.
  1282. * @ha: HA context
  1283. * @addr: Flash sector to erase
  1284. * @sec_mask: Sector address mask
  1285. * @man_id: Flash manufacturer ID
  1286. * @flash_id: Flash ID
  1287. *
  1288. * Returns 0 on success, else non-zero.
  1289. */
  1290. static int
  1291. qla2x00_erase_flash_sector(scsi_qla_host_t *ha, uint32_t addr,
  1292. uint32_t sec_mask, uint8_t man_id, uint8_t flash_id)
  1293. {
  1294. /* Individual Sector Erase Command Sequence */
  1295. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1296. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1297. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1298. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1299. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1300. if (man_id == 0x1f && flash_id == 0x13)
  1301. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
  1302. else
  1303. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
  1304. udelay(150);
  1305. /* Wait for erase to complete. */
  1306. return qla2x00_poll_flash(ha, addr, 0x80, man_id, flash_id);
  1307. }
  1308. /**
  1309. * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
  1310. * @man_id: Flash manufacturer ID
  1311. * @flash_id: Flash ID
  1312. */
  1313. static void
  1314. qla2x00_get_flash_manufacturer(scsi_qla_host_t *ha, uint8_t *man_id,
  1315. uint8_t *flash_id)
  1316. {
  1317. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1318. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1319. qla2x00_write_flash_byte(ha, 0x5555, 0x90);
  1320. *man_id = qla2x00_read_flash_byte(ha, 0x0000);
  1321. *flash_id = qla2x00_read_flash_byte(ha, 0x0001);
  1322. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1323. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1324. qla2x00_write_flash_byte(ha, 0x5555, 0xf0);
  1325. }
  1326. static void
  1327. qla2x00_read_flash_data(scsi_qla_host_t *ha, uint8_t *tmp_buf, uint32_t saddr,
  1328. uint32_t length)
  1329. {
  1330. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1331. uint32_t midpoint, ilength;
  1332. uint8_t data;
  1333. midpoint = length / 2;
  1334. WRT_REG_WORD(&reg->nvram, 0);
  1335. RD_REG_WORD(&reg->nvram);
  1336. for (ilength = 0; ilength < length; saddr++, ilength++, tmp_buf++) {
  1337. if (ilength == midpoint) {
  1338. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1339. RD_REG_WORD(&reg->nvram);
  1340. }
  1341. data = qla2x00_read_flash_byte(ha, saddr);
  1342. if (saddr % 100)
  1343. udelay(10);
  1344. *tmp_buf = data;
  1345. cond_resched();
  1346. }
  1347. }
  1348. static inline void
  1349. qla2x00_suspend_hba(struct scsi_qla_host *ha)
  1350. {
  1351. int cnt;
  1352. unsigned long flags;
  1353. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1354. /* Suspend HBA. */
  1355. scsi_block_requests(ha->host);
  1356. ha->isp_ops->disable_intrs(ha);
  1357. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1358. /* Pause RISC. */
  1359. spin_lock_irqsave(&ha->hardware_lock, flags);
  1360. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  1361. RD_REG_WORD(&reg->hccr);
  1362. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  1363. for (cnt = 0; cnt < 30000; cnt++) {
  1364. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
  1365. break;
  1366. udelay(100);
  1367. }
  1368. } else {
  1369. udelay(10);
  1370. }
  1371. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1372. }
  1373. static inline void
  1374. qla2x00_resume_hba(struct scsi_qla_host *ha)
  1375. {
  1376. /* Resume HBA. */
  1377. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1378. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1379. qla2xxx_wake_dpc(ha);
  1380. qla2x00_wait_for_hba_online(ha);
  1381. scsi_unblock_requests(ha->host);
  1382. }
  1383. uint8_t *
  1384. qla2x00_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1385. uint32_t offset, uint32_t length)
  1386. {
  1387. uint32_t addr, midpoint;
  1388. uint8_t *data;
  1389. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1390. /* Suspend HBA. */
  1391. qla2x00_suspend_hba(ha);
  1392. /* Go with read. */
  1393. midpoint = ha->optrom_size / 2;
  1394. qla2x00_flash_enable(ha);
  1395. WRT_REG_WORD(&reg->nvram, 0);
  1396. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1397. for (addr = offset, data = buf; addr < length; addr++, data++) {
  1398. if (addr == midpoint) {
  1399. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1400. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1401. }
  1402. *data = qla2x00_read_flash_byte(ha, addr);
  1403. }
  1404. qla2x00_flash_disable(ha);
  1405. /* Resume HBA. */
  1406. qla2x00_resume_hba(ha);
  1407. return buf;
  1408. }
  1409. int
  1410. qla2x00_write_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1411. uint32_t offset, uint32_t length)
  1412. {
  1413. int rval;
  1414. uint8_t man_id, flash_id, sec_number, data;
  1415. uint16_t wd;
  1416. uint32_t addr, liter, sec_mask, rest_addr;
  1417. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1418. /* Suspend HBA. */
  1419. qla2x00_suspend_hba(ha);
  1420. rval = QLA_SUCCESS;
  1421. sec_number = 0;
  1422. /* Reset ISP chip. */
  1423. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  1424. pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
  1425. /* Go with write. */
  1426. qla2x00_flash_enable(ha);
  1427. do { /* Loop once to provide quick error exit */
  1428. /* Structure of flash memory based on manufacturer */
  1429. if (IS_OEM_001(ha)) {
  1430. /* OEM variant with special flash part. */
  1431. man_id = flash_id = 0;
  1432. rest_addr = 0xffff;
  1433. sec_mask = 0x10000;
  1434. goto update_flash;
  1435. }
  1436. qla2x00_get_flash_manufacturer(ha, &man_id, &flash_id);
  1437. switch (man_id) {
  1438. case 0x20: /* ST flash. */
  1439. if (flash_id == 0xd2 || flash_id == 0xe3) {
  1440. /*
  1441. * ST m29w008at part - 64kb sector size with
  1442. * 32kb,8kb,8kb,16kb sectors at memory address
  1443. * 0xf0000.
  1444. */
  1445. rest_addr = 0xffff;
  1446. sec_mask = 0x10000;
  1447. break;
  1448. }
  1449. /*
  1450. * ST m29w010b part - 16kb sector size
  1451. * Default to 16kb sectors
  1452. */
  1453. rest_addr = 0x3fff;
  1454. sec_mask = 0x1c000;
  1455. break;
  1456. case 0x40: /* Mostel flash. */
  1457. /* Mostel v29c51001 part - 512 byte sector size. */
  1458. rest_addr = 0x1ff;
  1459. sec_mask = 0x1fe00;
  1460. break;
  1461. case 0xbf: /* SST flash. */
  1462. /* SST39sf10 part - 4kb sector size. */
  1463. rest_addr = 0xfff;
  1464. sec_mask = 0x1f000;
  1465. break;
  1466. case 0xda: /* Winbond flash. */
  1467. /* Winbond W29EE011 part - 256 byte sector size. */
  1468. rest_addr = 0x7f;
  1469. sec_mask = 0x1ff80;
  1470. break;
  1471. case 0xc2: /* Macronix flash. */
  1472. /* 64k sector size. */
  1473. if (flash_id == 0x38 || flash_id == 0x4f) {
  1474. rest_addr = 0xffff;
  1475. sec_mask = 0x10000;
  1476. break;
  1477. }
  1478. /* Fall through... */
  1479. case 0x1f: /* Atmel flash. */
  1480. /* 512k sector size. */
  1481. if (flash_id == 0x13) {
  1482. rest_addr = 0x7fffffff;
  1483. sec_mask = 0x80000000;
  1484. break;
  1485. }
  1486. /* Fall through... */
  1487. case 0x01: /* AMD flash. */
  1488. if (flash_id == 0x38 || flash_id == 0x40 ||
  1489. flash_id == 0x4f) {
  1490. /* Am29LV081 part - 64kb sector size. */
  1491. /* Am29LV002BT part - 64kb sector size. */
  1492. rest_addr = 0xffff;
  1493. sec_mask = 0x10000;
  1494. break;
  1495. } else if (flash_id == 0x3e) {
  1496. /*
  1497. * Am29LV008b part - 64kb sector size with
  1498. * 32kb,8kb,8kb,16kb sector at memory address
  1499. * h0xf0000.
  1500. */
  1501. rest_addr = 0xffff;
  1502. sec_mask = 0x10000;
  1503. break;
  1504. } else if (flash_id == 0x20 || flash_id == 0x6e) {
  1505. /*
  1506. * Am29LV010 part or AM29f010 - 16kb sector
  1507. * size.
  1508. */
  1509. rest_addr = 0x3fff;
  1510. sec_mask = 0x1c000;
  1511. break;
  1512. } else if (flash_id == 0x6d) {
  1513. /* Am29LV001 part - 8kb sector size. */
  1514. rest_addr = 0x1fff;
  1515. sec_mask = 0x1e000;
  1516. break;
  1517. }
  1518. default:
  1519. /* Default to 16 kb sector size. */
  1520. rest_addr = 0x3fff;
  1521. sec_mask = 0x1c000;
  1522. break;
  1523. }
  1524. update_flash:
  1525. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1526. if (qla2x00_erase_flash(ha, man_id, flash_id)) {
  1527. rval = QLA_FUNCTION_FAILED;
  1528. break;
  1529. }
  1530. }
  1531. for (addr = offset, liter = 0; liter < length; liter++,
  1532. addr++) {
  1533. data = buf[liter];
  1534. /* Are we at the beginning of a sector? */
  1535. if ((addr & rest_addr) == 0) {
  1536. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1537. if (addr >= 0x10000UL) {
  1538. if (((addr >> 12) & 0xf0) &&
  1539. ((man_id == 0x01 &&
  1540. flash_id == 0x3e) ||
  1541. (man_id == 0x20 &&
  1542. flash_id == 0xd2))) {
  1543. sec_number++;
  1544. if (sec_number == 1) {
  1545. rest_addr =
  1546. 0x7fff;
  1547. sec_mask =
  1548. 0x18000;
  1549. } else if (
  1550. sec_number == 2 ||
  1551. sec_number == 3) {
  1552. rest_addr =
  1553. 0x1fff;
  1554. sec_mask =
  1555. 0x1e000;
  1556. } else if (
  1557. sec_number == 4) {
  1558. rest_addr =
  1559. 0x3fff;
  1560. sec_mask =
  1561. 0x1c000;
  1562. }
  1563. }
  1564. }
  1565. } else if (addr == ha->optrom_size / 2) {
  1566. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1567. RD_REG_WORD(&reg->nvram);
  1568. }
  1569. if (flash_id == 0xda && man_id == 0xc1) {
  1570. qla2x00_write_flash_byte(ha, 0x5555,
  1571. 0xaa);
  1572. qla2x00_write_flash_byte(ha, 0x2aaa,
  1573. 0x55);
  1574. qla2x00_write_flash_byte(ha, 0x5555,
  1575. 0xa0);
  1576. } else if (!IS_QLA2322(ha) && !IS_QLA6322(ha)) {
  1577. /* Then erase it */
  1578. if (qla2x00_erase_flash_sector(ha,
  1579. addr, sec_mask, man_id,
  1580. flash_id)) {
  1581. rval = QLA_FUNCTION_FAILED;
  1582. break;
  1583. }
  1584. if (man_id == 0x01 && flash_id == 0x6d)
  1585. sec_number++;
  1586. }
  1587. }
  1588. if (man_id == 0x01 && flash_id == 0x6d) {
  1589. if (sec_number == 1 &&
  1590. addr == (rest_addr - 1)) {
  1591. rest_addr = 0x0fff;
  1592. sec_mask = 0x1f000;
  1593. } else if (sec_number == 3 && (addr & 0x7ffe)) {
  1594. rest_addr = 0x3fff;
  1595. sec_mask = 0x1c000;
  1596. }
  1597. }
  1598. if (qla2x00_program_flash_address(ha, addr, data,
  1599. man_id, flash_id)) {
  1600. rval = QLA_FUNCTION_FAILED;
  1601. break;
  1602. }
  1603. cond_resched();
  1604. }
  1605. } while (0);
  1606. qla2x00_flash_disable(ha);
  1607. /* Resume HBA. */
  1608. qla2x00_resume_hba(ha);
  1609. return rval;
  1610. }
  1611. uint8_t *
  1612. qla24xx_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1613. uint32_t offset, uint32_t length)
  1614. {
  1615. /* Suspend HBA. */
  1616. scsi_block_requests(ha->host);
  1617. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1618. /* Go with read. */
  1619. qla24xx_read_flash_data(ha, (uint32_t *)buf, offset >> 2, length >> 2);
  1620. /* Resume HBA. */
  1621. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1622. scsi_unblock_requests(ha->host);
  1623. return buf;
  1624. }
  1625. int
  1626. qla24xx_write_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1627. uint32_t offset, uint32_t length)
  1628. {
  1629. int rval;
  1630. /* Suspend HBA. */
  1631. scsi_block_requests(ha->host);
  1632. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1633. /* Go with write. */
  1634. rval = qla24xx_write_flash_data(ha, (uint32_t *)buf, offset >> 2,
  1635. length >> 2);
  1636. /* Resume HBA -- RISC reset needed. */
  1637. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1638. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1639. qla2xxx_wake_dpc(ha);
  1640. qla2x00_wait_for_hba_online(ha);
  1641. scsi_unblock_requests(ha->host);
  1642. return rval;
  1643. }
  1644. uint8_t *
  1645. qla25xx_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1646. uint32_t offset, uint32_t length)
  1647. {
  1648. int rval;
  1649. dma_addr_t optrom_dma;
  1650. void *optrom;
  1651. uint8_t *pbuf;
  1652. uint32_t faddr, left, burst;
  1653. if (offset & 0xfff)
  1654. goto slow_read;
  1655. if (length < OPTROM_BURST_SIZE)
  1656. goto slow_read;
  1657. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  1658. &optrom_dma, GFP_KERNEL);
  1659. if (!optrom) {
  1660. qla_printk(KERN_DEBUG, ha,
  1661. "Unable to allocate memory for optrom burst read "
  1662. "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
  1663. goto slow_read;
  1664. }
  1665. pbuf = buf;
  1666. faddr = offset >> 2;
  1667. left = length >> 2;
  1668. burst = OPTROM_BURST_DWORDS;
  1669. while (left != 0) {
  1670. if (burst > left)
  1671. burst = left;
  1672. rval = qla2x00_dump_ram(ha, optrom_dma,
  1673. flash_data_to_access_addr(faddr), burst);
  1674. if (rval) {
  1675. qla_printk(KERN_WARNING, ha,
  1676. "Unable to burst-read optrom segment "
  1677. "(%x/%x/%llx).\n", rval,
  1678. flash_data_to_access_addr(faddr),
  1679. (unsigned long long)optrom_dma);
  1680. qla_printk(KERN_WARNING, ha,
  1681. "Reverting to slow-read.\n");
  1682. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  1683. optrom, optrom_dma);
  1684. goto slow_read;
  1685. }
  1686. memcpy(pbuf, optrom, burst * 4);
  1687. left -= burst;
  1688. faddr += burst;
  1689. pbuf += burst * 4;
  1690. }
  1691. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE, optrom,
  1692. optrom_dma);
  1693. return buf;
  1694. slow_read:
  1695. return qla24xx_read_optrom_data(ha, buf, offset, length);
  1696. }
  1697. /**
  1698. * qla2x00_get_fcode_version() - Determine an FCODE image's version.
  1699. * @ha: HA context
  1700. * @pcids: Pointer to the FCODE PCI data structure
  1701. *
  1702. * The process of retrieving the FCODE version information is at best
  1703. * described as interesting.
  1704. *
  1705. * Within the first 100h bytes of the image an ASCII string is present
  1706. * which contains several pieces of information including the FCODE
  1707. * version. Unfortunately it seems the only reliable way to retrieve
  1708. * the version is by scanning for another sentinel within the string,
  1709. * the FCODE build date:
  1710. *
  1711. * ... 2.00.02 10/17/02 ...
  1712. *
  1713. * Returns QLA_SUCCESS on successful retrieval of version.
  1714. */
  1715. static void
  1716. qla2x00_get_fcode_version(scsi_qla_host_t *ha, uint32_t pcids)
  1717. {
  1718. int ret = QLA_FUNCTION_FAILED;
  1719. uint32_t istart, iend, iter, vend;
  1720. uint8_t do_next, rbyte, *vbyte;
  1721. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1722. /* Skip the PCI data structure. */
  1723. istart = pcids +
  1724. ((qla2x00_read_flash_byte(ha, pcids + 0x0B) << 8) |
  1725. qla2x00_read_flash_byte(ha, pcids + 0x0A));
  1726. iend = istart + 0x100;
  1727. do {
  1728. /* Scan for the sentinel date string...eeewww. */
  1729. do_next = 0;
  1730. iter = istart;
  1731. while ((iter < iend) && !do_next) {
  1732. iter++;
  1733. if (qla2x00_read_flash_byte(ha, iter) == '/') {
  1734. if (qla2x00_read_flash_byte(ha, iter + 2) ==
  1735. '/')
  1736. do_next++;
  1737. else if (qla2x00_read_flash_byte(ha,
  1738. iter + 3) == '/')
  1739. do_next++;
  1740. }
  1741. }
  1742. if (!do_next)
  1743. break;
  1744. /* Backtrack to previous ' ' (space). */
  1745. do_next = 0;
  1746. while ((iter > istart) && !do_next) {
  1747. iter--;
  1748. if (qla2x00_read_flash_byte(ha, iter) == ' ')
  1749. do_next++;
  1750. }
  1751. if (!do_next)
  1752. break;
  1753. /*
  1754. * Mark end of version tag, and find previous ' ' (space) or
  1755. * string length (recent FCODE images -- major hack ahead!!!).
  1756. */
  1757. vend = iter - 1;
  1758. do_next = 0;
  1759. while ((iter > istart) && !do_next) {
  1760. iter--;
  1761. rbyte = qla2x00_read_flash_byte(ha, iter);
  1762. if (rbyte == ' ' || rbyte == 0xd || rbyte == 0x10)
  1763. do_next++;
  1764. }
  1765. if (!do_next)
  1766. break;
  1767. /* Mark beginning of version tag, and copy data. */
  1768. iter++;
  1769. if ((vend - iter) &&
  1770. ((vend - iter) < sizeof(ha->fcode_revision))) {
  1771. vbyte = ha->fcode_revision;
  1772. while (iter <= vend) {
  1773. *vbyte++ = qla2x00_read_flash_byte(ha, iter);
  1774. iter++;
  1775. }
  1776. ret = QLA_SUCCESS;
  1777. }
  1778. } while (0);
  1779. if (ret != QLA_SUCCESS)
  1780. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1781. }
  1782. int
  1783. qla2x00_get_flash_version(scsi_qla_host_t *ha, void *mbuf)
  1784. {
  1785. int ret = QLA_SUCCESS;
  1786. uint8_t code_type, last_image;
  1787. uint32_t pcihdr, pcids;
  1788. uint8_t *dbyte;
  1789. uint16_t *dcode;
  1790. if (!ha->pio_address || !mbuf)
  1791. return QLA_FUNCTION_FAILED;
  1792. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  1793. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  1794. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1795. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1796. qla2x00_flash_enable(ha);
  1797. /* Begin with first PCI expansion ROM header. */
  1798. pcihdr = 0;
  1799. last_image = 1;
  1800. do {
  1801. /* Verify PCI expansion ROM header. */
  1802. if (qla2x00_read_flash_byte(ha, pcihdr) != 0x55 ||
  1803. qla2x00_read_flash_byte(ha, pcihdr + 0x01) != 0xaa) {
  1804. /* No signature */
  1805. DEBUG2(printk("scsi(%ld): No matching ROM "
  1806. "signature.\n", ha->host_no));
  1807. ret = QLA_FUNCTION_FAILED;
  1808. break;
  1809. }
  1810. /* Locate PCI data structure. */
  1811. pcids = pcihdr +
  1812. ((qla2x00_read_flash_byte(ha, pcihdr + 0x19) << 8) |
  1813. qla2x00_read_flash_byte(ha, pcihdr + 0x18));
  1814. /* Validate signature of PCI data structure. */
  1815. if (qla2x00_read_flash_byte(ha, pcids) != 'P' ||
  1816. qla2x00_read_flash_byte(ha, pcids + 0x1) != 'C' ||
  1817. qla2x00_read_flash_byte(ha, pcids + 0x2) != 'I' ||
  1818. qla2x00_read_flash_byte(ha, pcids + 0x3) != 'R') {
  1819. /* Incorrect header. */
  1820. DEBUG2(printk("%s(): PCI data struct not found "
  1821. "pcir_adr=%x.\n", __func__, pcids));
  1822. ret = QLA_FUNCTION_FAILED;
  1823. break;
  1824. }
  1825. /* Read version */
  1826. code_type = qla2x00_read_flash_byte(ha, pcids + 0x14);
  1827. switch (code_type) {
  1828. case ROM_CODE_TYPE_BIOS:
  1829. /* Intel x86, PC-AT compatible. */
  1830. ha->bios_revision[0] =
  1831. qla2x00_read_flash_byte(ha, pcids + 0x12);
  1832. ha->bios_revision[1] =
  1833. qla2x00_read_flash_byte(ha, pcids + 0x13);
  1834. DEBUG3(printk("%s(): read BIOS %d.%d.\n", __func__,
  1835. ha->bios_revision[1], ha->bios_revision[0]));
  1836. break;
  1837. case ROM_CODE_TYPE_FCODE:
  1838. /* Open Firmware standard for PCI (FCode). */
  1839. /* Eeeewww... */
  1840. qla2x00_get_fcode_version(ha, pcids);
  1841. break;
  1842. case ROM_CODE_TYPE_EFI:
  1843. /* Extensible Firmware Interface (EFI). */
  1844. ha->efi_revision[0] =
  1845. qla2x00_read_flash_byte(ha, pcids + 0x12);
  1846. ha->efi_revision[1] =
  1847. qla2x00_read_flash_byte(ha, pcids + 0x13);
  1848. DEBUG3(printk("%s(): read EFI %d.%d.\n", __func__,
  1849. ha->efi_revision[1], ha->efi_revision[0]));
  1850. break;
  1851. default:
  1852. DEBUG2(printk("%s(): Unrecognized code type %x at "
  1853. "pcids %x.\n", __func__, code_type, pcids));
  1854. break;
  1855. }
  1856. last_image = qla2x00_read_flash_byte(ha, pcids + 0x15) & BIT_7;
  1857. /* Locate next PCI expansion ROM. */
  1858. pcihdr += ((qla2x00_read_flash_byte(ha, pcids + 0x11) << 8) |
  1859. qla2x00_read_flash_byte(ha, pcids + 0x10)) * 512;
  1860. } while (!last_image);
  1861. if (IS_QLA2322(ha)) {
  1862. /* Read firmware image information. */
  1863. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1864. dbyte = mbuf;
  1865. memset(dbyte, 0, 8);
  1866. dcode = (uint16_t *)dbyte;
  1867. qla2x00_read_flash_data(ha, dbyte, FA_RISC_CODE_ADDR * 4 + 10,
  1868. 8);
  1869. DEBUG3(printk("%s(%ld): dumping fw ver from flash:\n",
  1870. __func__, ha->host_no));
  1871. DEBUG3(qla2x00_dump_buffer((uint8_t *)dbyte, 8));
  1872. if ((dcode[0] == 0xffff && dcode[1] == 0xffff &&
  1873. dcode[2] == 0xffff && dcode[3] == 0xffff) ||
  1874. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  1875. dcode[3] == 0)) {
  1876. DEBUG2(printk("%s(): Unrecognized fw revision at "
  1877. "%x.\n", __func__, FA_RISC_CODE_ADDR * 4));
  1878. } else {
  1879. /* values are in big endian */
  1880. ha->fw_revision[0] = dbyte[0] << 16 | dbyte[1];
  1881. ha->fw_revision[1] = dbyte[2] << 16 | dbyte[3];
  1882. ha->fw_revision[2] = dbyte[4] << 16 | dbyte[5];
  1883. }
  1884. }
  1885. qla2x00_flash_disable(ha);
  1886. return ret;
  1887. }
  1888. int
  1889. qla24xx_get_flash_version(scsi_qla_host_t *ha, void *mbuf)
  1890. {
  1891. int ret = QLA_SUCCESS;
  1892. uint32_t pcihdr, pcids;
  1893. uint32_t *dcode;
  1894. uint8_t *bcode;
  1895. uint8_t code_type, last_image;
  1896. int i;
  1897. if (!mbuf)
  1898. return QLA_FUNCTION_FAILED;
  1899. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  1900. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  1901. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1902. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1903. dcode = mbuf;
  1904. /* Begin with first PCI expansion ROM header. */
  1905. pcihdr = 0;
  1906. last_image = 1;
  1907. do {
  1908. /* Verify PCI expansion ROM header. */
  1909. qla24xx_read_flash_data(ha, dcode, pcihdr >> 2, 0x20);
  1910. bcode = mbuf + (pcihdr % 4);
  1911. if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa) {
  1912. /* No signature */
  1913. DEBUG2(printk("scsi(%ld): No matching ROM "
  1914. "signature.\n", ha->host_no));
  1915. ret = QLA_FUNCTION_FAILED;
  1916. break;
  1917. }
  1918. /* Locate PCI data structure. */
  1919. pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
  1920. qla24xx_read_flash_data(ha, dcode, pcids >> 2, 0x20);
  1921. bcode = mbuf + (pcihdr % 4);
  1922. /* Validate signature of PCI data structure. */
  1923. if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
  1924. bcode[0x2] != 'I' || bcode[0x3] != 'R') {
  1925. /* Incorrect header. */
  1926. DEBUG2(printk("%s(): PCI data struct not found "
  1927. "pcir_adr=%x.\n", __func__, pcids));
  1928. ret = QLA_FUNCTION_FAILED;
  1929. break;
  1930. }
  1931. /* Read version */
  1932. code_type = bcode[0x14];
  1933. switch (code_type) {
  1934. case ROM_CODE_TYPE_BIOS:
  1935. /* Intel x86, PC-AT compatible. */
  1936. ha->bios_revision[0] = bcode[0x12];
  1937. ha->bios_revision[1] = bcode[0x13];
  1938. DEBUG3(printk("%s(): read BIOS %d.%d.\n", __func__,
  1939. ha->bios_revision[1], ha->bios_revision[0]));
  1940. break;
  1941. case ROM_CODE_TYPE_FCODE:
  1942. /* Open Firmware standard for PCI (FCode). */
  1943. ha->fcode_revision[0] = bcode[0x12];
  1944. ha->fcode_revision[1] = bcode[0x13];
  1945. DEBUG3(printk("%s(): read FCODE %d.%d.\n", __func__,
  1946. ha->fcode_revision[1], ha->fcode_revision[0]));
  1947. break;
  1948. case ROM_CODE_TYPE_EFI:
  1949. /* Extensible Firmware Interface (EFI). */
  1950. ha->efi_revision[0] = bcode[0x12];
  1951. ha->efi_revision[1] = bcode[0x13];
  1952. DEBUG3(printk("%s(): read EFI %d.%d.\n", __func__,
  1953. ha->efi_revision[1], ha->efi_revision[0]));
  1954. break;
  1955. default:
  1956. DEBUG2(printk("%s(): Unrecognized code type %x at "
  1957. "pcids %x.\n", __func__, code_type, pcids));
  1958. break;
  1959. }
  1960. last_image = bcode[0x15] & BIT_7;
  1961. /* Locate next PCI expansion ROM. */
  1962. pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
  1963. } while (!last_image);
  1964. /* Read firmware image information. */
  1965. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1966. dcode = mbuf;
  1967. qla24xx_read_flash_data(ha, dcode, FA_RISC_CODE_ADDR + 4, 4);
  1968. for (i = 0; i < 4; i++)
  1969. dcode[i] = be32_to_cpu(dcode[i]);
  1970. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  1971. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  1972. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  1973. dcode[3] == 0)) {
  1974. DEBUG2(printk("%s(): Unrecognized fw version at %x.\n",
  1975. __func__, FA_RISC_CODE_ADDR));
  1976. } else {
  1977. ha->fw_revision[0] = dcode[0];
  1978. ha->fw_revision[1] = dcode[1];
  1979. ha->fw_revision[2] = dcode[2];
  1980. ha->fw_revision[3] = dcode[3];
  1981. }
  1982. return ret;
  1983. }
  1984. static int
  1985. qla2xxx_is_vpd_valid(uint8_t *pos, uint8_t *end)
  1986. {
  1987. if (pos >= end || *pos != 0x82)
  1988. return 0;
  1989. pos += 3 + pos[1];
  1990. if (pos >= end || *pos != 0x90)
  1991. return 0;
  1992. pos += 3 + pos[1];
  1993. if (pos >= end || *pos != 0x78)
  1994. return 0;
  1995. return 1;
  1996. }
  1997. int
  1998. qla2xxx_get_vpd_field(scsi_qla_host_t *ha, char *key, char *str, size_t size)
  1999. {
  2000. uint8_t *pos = ha->vpd;
  2001. uint8_t *end = pos + ha->vpd_size;
  2002. int len = 0;
  2003. if (!IS_FWI2_CAPABLE(ha) || !qla2xxx_is_vpd_valid(pos, end))
  2004. return 0;
  2005. while (pos < end && *pos != 0x78) {
  2006. len = (*pos == 0x82) ? pos[1] : pos[2];
  2007. if (!strncmp(pos, key, strlen(key)))
  2008. break;
  2009. if (*pos != 0x90 && *pos != 0x91)
  2010. pos += len;
  2011. pos += 3;
  2012. }
  2013. if (pos < end - len && *pos != 0x78)
  2014. return snprintf(str, size, "%.*s", len, pos + 3);
  2015. return 0;
  2016. }
  2017. static int
  2018. qla2xxx_hw_event_store(scsi_qla_host_t *ha, uint32_t *fdata)
  2019. {
  2020. uint32_t d[2], faddr;
  2021. /* Locate first empty entry. */
  2022. for (;;) {
  2023. if (ha->hw_event_ptr >=
  2024. ha->hw_event_start + FA_HW_EVENT_SIZE) {
  2025. DEBUG2(qla_printk(KERN_WARNING, ha,
  2026. "HW event -- Log Full!\n"));
  2027. return QLA_MEMORY_ALLOC_FAILED;
  2028. }
  2029. qla24xx_read_flash_data(ha, d, ha->hw_event_ptr, 2);
  2030. faddr = flash_data_to_access_addr(ha->hw_event_ptr);
  2031. ha->hw_event_ptr += FA_HW_EVENT_ENTRY_SIZE;
  2032. if (d[0] == __constant_cpu_to_le32(0xffffffff) &&
  2033. d[1] == __constant_cpu_to_le32(0xffffffff)) {
  2034. qla24xx_unprotect_flash(ha);
  2035. qla24xx_write_flash_dword(ha, faddr++,
  2036. cpu_to_le32(jiffies));
  2037. qla24xx_write_flash_dword(ha, faddr++, 0);
  2038. qla24xx_write_flash_dword(ha, faddr++, *fdata++);
  2039. qla24xx_write_flash_dword(ha, faddr++, *fdata);
  2040. qla24xx_protect_flash(ha);
  2041. break;
  2042. }
  2043. }
  2044. return QLA_SUCCESS;
  2045. }
  2046. int
  2047. qla2xxx_hw_event_log(scsi_qla_host_t *ha, uint16_t code, uint16_t d1,
  2048. uint16_t d2, uint16_t d3)
  2049. {
  2050. #define QMARK(a, b, c, d) \
  2051. cpu_to_le32(LSB(a) << 24 | LSB(b) << 16 | LSB(c) << 8 | LSB(d))
  2052. int rval;
  2053. uint32_t marker[2], fdata[4];
  2054. if (ha->hw_event_start == 0)
  2055. return QLA_FUNCTION_FAILED;
  2056. DEBUG2(qla_printk(KERN_WARNING, ha,
  2057. "HW event -- code=%x, d1=%x, d2=%x, d3=%x.\n", code, d1, d2, d3));
  2058. /* If marker not already found, locate or write. */
  2059. if (!ha->flags.hw_event_marker_found) {
  2060. /* Create marker. */
  2061. marker[0] = QMARK('L', ha->fw_major_version,
  2062. ha->fw_minor_version, ha->fw_subminor_version);
  2063. marker[1] = QMARK(QLA_DRIVER_MAJOR_VER, QLA_DRIVER_MINOR_VER,
  2064. QLA_DRIVER_PATCH_VER, QLA_DRIVER_BETA_VER);
  2065. /* Locate marker. */
  2066. ha->hw_event_ptr = ha->hw_event_start;
  2067. for (;;) {
  2068. qla24xx_read_flash_data(ha, fdata, ha->hw_event_ptr,
  2069. 4);
  2070. if (fdata[0] == __constant_cpu_to_le32(0xffffffff) &&
  2071. fdata[1] == __constant_cpu_to_le32(0xffffffff))
  2072. break;
  2073. ha->hw_event_ptr += FA_HW_EVENT_ENTRY_SIZE;
  2074. if (ha->hw_event_ptr >=
  2075. ha->hw_event_start + FA_HW_EVENT_SIZE) {
  2076. DEBUG2(qla_printk(KERN_WARNING, ha,
  2077. "HW event -- Log Full!\n"));
  2078. return QLA_MEMORY_ALLOC_FAILED;
  2079. }
  2080. if (fdata[2] == marker[0] && fdata[3] == marker[1]) {
  2081. ha->flags.hw_event_marker_found = 1;
  2082. break;
  2083. }
  2084. }
  2085. /* No marker, write it. */
  2086. if (!ha->flags.hw_event_marker_found) {
  2087. rval = qla2xxx_hw_event_store(ha, marker);
  2088. if (rval != QLA_SUCCESS) {
  2089. DEBUG2(qla_printk(KERN_WARNING, ha,
  2090. "HW event -- Failed marker write=%x.!\n",
  2091. rval));
  2092. return rval;
  2093. }
  2094. ha->flags.hw_event_marker_found = 1;
  2095. }
  2096. }
  2097. /* Store error. */
  2098. fdata[0] = cpu_to_le32(code << 16 | d1);
  2099. fdata[1] = cpu_to_le32(d2 << 16 | d3);
  2100. rval = qla2xxx_hw_event_store(ha, fdata);
  2101. if (rval != QLA_SUCCESS) {
  2102. DEBUG2(qla_printk(KERN_WARNING, ha,
  2103. "HW event -- Failed error write=%x.!\n",
  2104. rval));
  2105. }
  2106. return rval;
  2107. }