qla_sup.c 62 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410
  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. unsigned long flags;
  743. ret = QLA_SUCCESS;
  744. spin_lock_irqsave(&ha->hardware_lock, flags);
  745. /* Enable flash write. */
  746. WRT_REG_DWORD(&reg->ctrl_status,
  747. RD_REG_DWORD(&reg->ctrl_status) | CSRX_FLASH_ENABLE);
  748. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  749. /* Disable NVRAM write-protection. */
  750. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  751. 0);
  752. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  753. 0);
  754. /* Dword writes to flash. */
  755. dwptr = (uint32_t *)buf;
  756. for (i = 0; i < bytes >> 2; i++, naddr++, dwptr++) {
  757. ret = qla24xx_write_flash_dword(ha,
  758. nvram_data_to_access_addr(naddr),
  759. cpu_to_le32(*dwptr));
  760. if (ret != QLA_SUCCESS) {
  761. DEBUG9(printk("%s(%ld) Unable to program "
  762. "nvram address=%x data=%x.\n", __func__,
  763. ha->host_no, naddr, *dwptr));
  764. break;
  765. }
  766. }
  767. /* Enable NVRAM write-protection. */
  768. qla24xx_write_flash_dword(ha, nvram_conf_to_access_addr(0x101),
  769. 0x8c);
  770. /* Disable flash write. */
  771. WRT_REG_DWORD(&reg->ctrl_status,
  772. RD_REG_DWORD(&reg->ctrl_status) & ~CSRX_FLASH_ENABLE);
  773. RD_REG_DWORD(&reg->ctrl_status); /* PCI Posting. */
  774. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  775. return ret;
  776. }
  777. uint8_t *
  778. qla25xx_read_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  779. uint32_t bytes)
  780. {
  781. uint32_t i;
  782. uint32_t *dwptr;
  783. /* Dword reads to flash. */
  784. dwptr = (uint32_t *)buf;
  785. for (i = 0; i < bytes >> 2; i++, naddr++)
  786. dwptr[i] = cpu_to_le32(qla24xx_read_flash_dword(ha,
  787. flash_data_to_access_addr(FA_VPD_NVRAM_ADDR | naddr)));
  788. return buf;
  789. }
  790. int
  791. qla25xx_write_nvram_data(scsi_qla_host_t *ha, uint8_t *buf, uint32_t naddr,
  792. uint32_t bytes)
  793. {
  794. #define RMW_BUFFER_SIZE (64 * 1024)
  795. uint8_t *dbuf;
  796. dbuf = vmalloc(RMW_BUFFER_SIZE);
  797. if (!dbuf)
  798. return QLA_MEMORY_ALLOC_FAILED;
  799. ha->isp_ops->read_optrom(ha, dbuf, FA_VPD_NVRAM_ADDR << 2,
  800. RMW_BUFFER_SIZE);
  801. memcpy(dbuf + (naddr << 2), buf, bytes);
  802. ha->isp_ops->write_optrom(ha, dbuf, FA_VPD_NVRAM_ADDR << 2,
  803. RMW_BUFFER_SIZE);
  804. vfree(dbuf);
  805. return QLA_SUCCESS;
  806. }
  807. static inline void
  808. qla2x00_flip_colors(scsi_qla_host_t *ha, uint16_t *pflags)
  809. {
  810. if (IS_QLA2322(ha)) {
  811. /* Flip all colors. */
  812. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  813. /* Turn off. */
  814. ha->beacon_color_state = 0;
  815. *pflags = GPIO_LED_ALL_OFF;
  816. } else {
  817. /* Turn on. */
  818. ha->beacon_color_state = QLA_LED_ALL_ON;
  819. *pflags = GPIO_LED_RGA_ON;
  820. }
  821. } else {
  822. /* Flip green led only. */
  823. if (ha->beacon_color_state == QLA_LED_GRN_ON) {
  824. /* Turn off. */
  825. ha->beacon_color_state = 0;
  826. *pflags = GPIO_LED_GREEN_OFF_AMBER_OFF;
  827. } else {
  828. /* Turn on. */
  829. ha->beacon_color_state = QLA_LED_GRN_ON;
  830. *pflags = GPIO_LED_GREEN_ON_AMBER_OFF;
  831. }
  832. }
  833. }
  834. #define PIO_REG(h, r) ((h)->pio_address + offsetof(struct device_reg_2xxx, r))
  835. void
  836. qla2x00_beacon_blink(struct scsi_qla_host *ha)
  837. {
  838. uint16_t gpio_enable;
  839. uint16_t gpio_data;
  840. uint16_t led_color = 0;
  841. unsigned long flags;
  842. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  843. spin_lock_irqsave(&ha->hardware_lock, flags);
  844. /* Save the Original GPIOE. */
  845. if (ha->pio_address) {
  846. gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
  847. gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
  848. } else {
  849. gpio_enable = RD_REG_WORD(&reg->gpioe);
  850. gpio_data = RD_REG_WORD(&reg->gpiod);
  851. }
  852. /* Set the modified gpio_enable values */
  853. gpio_enable |= GPIO_LED_MASK;
  854. if (ha->pio_address) {
  855. WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
  856. } else {
  857. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  858. RD_REG_WORD(&reg->gpioe);
  859. }
  860. qla2x00_flip_colors(ha, &led_color);
  861. /* Clear out any previously set LED color. */
  862. gpio_data &= ~GPIO_LED_MASK;
  863. /* Set the new input LED color to GPIOD. */
  864. gpio_data |= led_color;
  865. /* Set the modified gpio_data values */
  866. if (ha->pio_address) {
  867. WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
  868. } else {
  869. WRT_REG_WORD(&reg->gpiod, gpio_data);
  870. RD_REG_WORD(&reg->gpiod);
  871. }
  872. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  873. }
  874. int
  875. qla2x00_beacon_on(struct scsi_qla_host *ha)
  876. {
  877. uint16_t gpio_enable;
  878. uint16_t gpio_data;
  879. unsigned long flags;
  880. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  881. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  882. ha->fw_options[1] |= FO1_DISABLE_GPIO6_7;
  883. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  884. qla_printk(KERN_WARNING, ha,
  885. "Unable to update fw options (beacon on).\n");
  886. return QLA_FUNCTION_FAILED;
  887. }
  888. /* Turn off LEDs. */
  889. spin_lock_irqsave(&ha->hardware_lock, flags);
  890. if (ha->pio_address) {
  891. gpio_enable = RD_REG_WORD_PIO(PIO_REG(ha, gpioe));
  892. gpio_data = RD_REG_WORD_PIO(PIO_REG(ha, gpiod));
  893. } else {
  894. gpio_enable = RD_REG_WORD(&reg->gpioe);
  895. gpio_data = RD_REG_WORD(&reg->gpiod);
  896. }
  897. gpio_enable |= GPIO_LED_MASK;
  898. /* Set the modified gpio_enable values. */
  899. if (ha->pio_address) {
  900. WRT_REG_WORD_PIO(PIO_REG(ha, gpioe), gpio_enable);
  901. } else {
  902. WRT_REG_WORD(&reg->gpioe, gpio_enable);
  903. RD_REG_WORD(&reg->gpioe);
  904. }
  905. /* Clear out previously set LED colour. */
  906. gpio_data &= ~GPIO_LED_MASK;
  907. if (ha->pio_address) {
  908. WRT_REG_WORD_PIO(PIO_REG(ha, gpiod), gpio_data);
  909. } else {
  910. WRT_REG_WORD(&reg->gpiod, gpio_data);
  911. RD_REG_WORD(&reg->gpiod);
  912. }
  913. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  914. /*
  915. * Let the per HBA timer kick off the blinking process based on
  916. * the following flags. No need to do anything else now.
  917. */
  918. ha->beacon_blink_led = 1;
  919. ha->beacon_color_state = 0;
  920. return QLA_SUCCESS;
  921. }
  922. int
  923. qla2x00_beacon_off(struct scsi_qla_host *ha)
  924. {
  925. int rval = QLA_SUCCESS;
  926. ha->beacon_blink_led = 0;
  927. /* Set the on flag so when it gets flipped it will be off. */
  928. if (IS_QLA2322(ha))
  929. ha->beacon_color_state = QLA_LED_ALL_ON;
  930. else
  931. ha->beacon_color_state = QLA_LED_GRN_ON;
  932. ha->isp_ops->beacon_blink(ha); /* This turns green LED off */
  933. ha->fw_options[1] &= ~FO1_SET_EMPHASIS_SWING;
  934. ha->fw_options[1] &= ~FO1_DISABLE_GPIO6_7;
  935. rval = qla2x00_set_fw_options(ha, ha->fw_options);
  936. if (rval != QLA_SUCCESS)
  937. qla_printk(KERN_WARNING, ha,
  938. "Unable to update fw options (beacon off).\n");
  939. return rval;
  940. }
  941. static inline void
  942. qla24xx_flip_colors(scsi_qla_host_t *ha, uint16_t *pflags)
  943. {
  944. /* Flip all colors. */
  945. if (ha->beacon_color_state == QLA_LED_ALL_ON) {
  946. /* Turn off. */
  947. ha->beacon_color_state = 0;
  948. *pflags = 0;
  949. } else {
  950. /* Turn on. */
  951. ha->beacon_color_state = QLA_LED_ALL_ON;
  952. *pflags = GPDX_LED_YELLOW_ON | GPDX_LED_AMBER_ON;
  953. }
  954. }
  955. void
  956. qla24xx_beacon_blink(struct scsi_qla_host *ha)
  957. {
  958. uint16_t led_color = 0;
  959. uint32_t gpio_data;
  960. unsigned long flags;
  961. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  962. /* Save the Original GPIOD. */
  963. spin_lock_irqsave(&ha->hardware_lock, flags);
  964. gpio_data = RD_REG_DWORD(&reg->gpiod);
  965. /* Enable the gpio_data reg for update. */
  966. gpio_data |= GPDX_LED_UPDATE_MASK;
  967. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  968. gpio_data = RD_REG_DWORD(&reg->gpiod);
  969. /* Set the color bits. */
  970. qla24xx_flip_colors(ha, &led_color);
  971. /* Clear out any previously set LED color. */
  972. gpio_data &= ~GPDX_LED_COLOR_MASK;
  973. /* Set the new input LED color to GPIOD. */
  974. gpio_data |= led_color;
  975. /* Set the modified gpio_data values. */
  976. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  977. gpio_data = RD_REG_DWORD(&reg->gpiod);
  978. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  979. }
  980. int
  981. qla24xx_beacon_on(struct scsi_qla_host *ha)
  982. {
  983. uint32_t gpio_data;
  984. unsigned long flags;
  985. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  986. if (ha->beacon_blink_led == 0) {
  987. /* Enable firmware for update */
  988. ha->fw_options[1] |= ADD_FO1_DISABLE_GPIO_LED_CTRL;
  989. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS)
  990. return QLA_FUNCTION_FAILED;
  991. if (qla2x00_get_fw_options(ha, ha->fw_options) !=
  992. QLA_SUCCESS) {
  993. qla_printk(KERN_WARNING, ha,
  994. "Unable to update fw options (beacon on).\n");
  995. return QLA_FUNCTION_FAILED;
  996. }
  997. spin_lock_irqsave(&ha->hardware_lock, flags);
  998. gpio_data = RD_REG_DWORD(&reg->gpiod);
  999. /* Enable the gpio_data reg for update. */
  1000. gpio_data |= GPDX_LED_UPDATE_MASK;
  1001. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  1002. RD_REG_DWORD(&reg->gpiod);
  1003. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1004. }
  1005. /* So all colors blink together. */
  1006. ha->beacon_color_state = 0;
  1007. /* Let the per HBA timer kick off the blinking process. */
  1008. ha->beacon_blink_led = 1;
  1009. return QLA_SUCCESS;
  1010. }
  1011. int
  1012. qla24xx_beacon_off(struct scsi_qla_host *ha)
  1013. {
  1014. uint32_t gpio_data;
  1015. unsigned long flags;
  1016. struct device_reg_24xx __iomem *reg = &ha->iobase->isp24;
  1017. ha->beacon_blink_led = 0;
  1018. ha->beacon_color_state = QLA_LED_ALL_ON;
  1019. ha->isp_ops->beacon_blink(ha); /* Will flip to all off. */
  1020. /* Give control back to firmware. */
  1021. spin_lock_irqsave(&ha->hardware_lock, flags);
  1022. gpio_data = RD_REG_DWORD(&reg->gpiod);
  1023. /* Disable the gpio_data reg for update. */
  1024. gpio_data &= ~GPDX_LED_UPDATE_MASK;
  1025. WRT_REG_DWORD(&reg->gpiod, gpio_data);
  1026. RD_REG_DWORD(&reg->gpiod);
  1027. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1028. ha->fw_options[1] &= ~ADD_FO1_DISABLE_GPIO_LED_CTRL;
  1029. if (qla2x00_set_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  1030. qla_printk(KERN_WARNING, ha,
  1031. "Unable to update fw options (beacon off).\n");
  1032. return QLA_FUNCTION_FAILED;
  1033. }
  1034. if (qla2x00_get_fw_options(ha, ha->fw_options) != QLA_SUCCESS) {
  1035. qla_printk(KERN_WARNING, ha,
  1036. "Unable to get fw options (beacon off).\n");
  1037. return QLA_FUNCTION_FAILED;
  1038. }
  1039. return QLA_SUCCESS;
  1040. }
  1041. /*
  1042. * Flash support routines
  1043. */
  1044. /**
  1045. * qla2x00_flash_enable() - Setup flash for reading and writing.
  1046. * @ha: HA context
  1047. */
  1048. static void
  1049. qla2x00_flash_enable(scsi_qla_host_t *ha)
  1050. {
  1051. uint16_t data;
  1052. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1053. data = RD_REG_WORD(&reg->ctrl_status);
  1054. data |= CSR_FLASH_ENABLE;
  1055. WRT_REG_WORD(&reg->ctrl_status, data);
  1056. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1057. }
  1058. /**
  1059. * qla2x00_flash_disable() - Disable flash and allow RISC to run.
  1060. * @ha: HA context
  1061. */
  1062. static void
  1063. qla2x00_flash_disable(scsi_qla_host_t *ha)
  1064. {
  1065. uint16_t data;
  1066. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1067. data = RD_REG_WORD(&reg->ctrl_status);
  1068. data &= ~(CSR_FLASH_ENABLE);
  1069. WRT_REG_WORD(&reg->ctrl_status, data);
  1070. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1071. }
  1072. /**
  1073. * qla2x00_read_flash_byte() - Reads a byte from flash
  1074. * @ha: HA context
  1075. * @addr: Address in flash to read
  1076. *
  1077. * A word is read from the chip, but, only the lower byte is valid.
  1078. *
  1079. * Returns the byte read from flash @addr.
  1080. */
  1081. static uint8_t
  1082. qla2x00_read_flash_byte(scsi_qla_host_t *ha, uint32_t addr)
  1083. {
  1084. uint16_t data;
  1085. uint16_t bank_select;
  1086. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1087. bank_select = RD_REG_WORD(&reg->ctrl_status);
  1088. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1089. /* Specify 64K address range: */
  1090. /* clear out Module Select and Flash Address bits [19:16]. */
  1091. bank_select &= ~0xf8;
  1092. bank_select |= addr >> 12 & 0xf0;
  1093. bank_select |= CSR_FLASH_64K_BANK;
  1094. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1095. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1096. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1097. data = RD_REG_WORD(&reg->flash_data);
  1098. return (uint8_t)data;
  1099. }
  1100. /* Setup bit 16 of flash address. */
  1101. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1102. bank_select |= CSR_FLASH_64K_BANK;
  1103. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1104. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1105. } else if (((addr & BIT_16) == 0) &&
  1106. (bank_select & CSR_FLASH_64K_BANK)) {
  1107. bank_select &= ~(CSR_FLASH_64K_BANK);
  1108. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1109. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1110. }
  1111. /* Always perform IO mapped accesses to the FLASH registers. */
  1112. if (ha->pio_address) {
  1113. uint16_t data2;
  1114. WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
  1115. do {
  1116. data = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
  1117. barrier();
  1118. cpu_relax();
  1119. data2 = RD_REG_WORD_PIO(PIO_REG(ha, flash_data));
  1120. } while (data != data2);
  1121. } else {
  1122. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1123. data = qla2x00_debounce_register(&reg->flash_data);
  1124. }
  1125. return (uint8_t)data;
  1126. }
  1127. /**
  1128. * qla2x00_write_flash_byte() - Write a byte to flash
  1129. * @ha: HA context
  1130. * @addr: Address in flash to write
  1131. * @data: Data to write
  1132. */
  1133. static void
  1134. qla2x00_write_flash_byte(scsi_qla_host_t *ha, uint32_t addr, uint8_t data)
  1135. {
  1136. uint16_t bank_select;
  1137. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1138. bank_select = RD_REG_WORD(&reg->ctrl_status);
  1139. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1140. /* Specify 64K address range: */
  1141. /* clear out Module Select and Flash Address bits [19:16]. */
  1142. bank_select &= ~0xf8;
  1143. bank_select |= addr >> 12 & 0xf0;
  1144. bank_select |= CSR_FLASH_64K_BANK;
  1145. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1146. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1147. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1148. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1149. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  1150. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1151. return;
  1152. }
  1153. /* Setup bit 16 of flash address. */
  1154. if ((addr & BIT_16) && ((bank_select & CSR_FLASH_64K_BANK) == 0)) {
  1155. bank_select |= CSR_FLASH_64K_BANK;
  1156. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1157. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1158. } else if (((addr & BIT_16) == 0) &&
  1159. (bank_select & CSR_FLASH_64K_BANK)) {
  1160. bank_select &= ~(CSR_FLASH_64K_BANK);
  1161. WRT_REG_WORD(&reg->ctrl_status, bank_select);
  1162. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1163. }
  1164. /* Always perform IO mapped accesses to the FLASH registers. */
  1165. if (ha->pio_address) {
  1166. WRT_REG_WORD_PIO(PIO_REG(ha, flash_address), (uint16_t)addr);
  1167. WRT_REG_WORD_PIO(PIO_REG(ha, flash_data), (uint16_t)data);
  1168. } else {
  1169. WRT_REG_WORD(&reg->flash_address, (uint16_t)addr);
  1170. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1171. WRT_REG_WORD(&reg->flash_data, (uint16_t)data);
  1172. RD_REG_WORD(&reg->ctrl_status); /* PCI Posting. */
  1173. }
  1174. }
  1175. /**
  1176. * qla2x00_poll_flash() - Polls flash for completion.
  1177. * @ha: HA context
  1178. * @addr: Address in flash to poll
  1179. * @poll_data: Data to be polled
  1180. * @man_id: Flash manufacturer ID
  1181. * @flash_id: Flash ID
  1182. *
  1183. * This function polls the device until bit 7 of what is read matches data
  1184. * bit 7 or until data bit 5 becomes a 1. If that hapens, the flash ROM timed
  1185. * out (a fatal error). The flash book recommeds reading bit 7 again after
  1186. * reading bit 5 as a 1.
  1187. *
  1188. * Returns 0 on success, else non-zero.
  1189. */
  1190. static int
  1191. qla2x00_poll_flash(scsi_qla_host_t *ha, uint32_t addr, uint8_t poll_data,
  1192. uint8_t man_id, uint8_t flash_id)
  1193. {
  1194. int status;
  1195. uint8_t flash_data;
  1196. uint32_t cnt;
  1197. status = 1;
  1198. /* Wait for 30 seconds for command to finish. */
  1199. poll_data &= BIT_7;
  1200. for (cnt = 3000000; cnt; cnt--) {
  1201. flash_data = qla2x00_read_flash_byte(ha, addr);
  1202. if ((flash_data & BIT_7) == poll_data) {
  1203. status = 0;
  1204. break;
  1205. }
  1206. if (man_id != 0x40 && man_id != 0xda) {
  1207. if ((flash_data & BIT_5) && cnt > 2)
  1208. cnt = 2;
  1209. }
  1210. udelay(10);
  1211. barrier();
  1212. cond_resched();
  1213. }
  1214. return status;
  1215. }
  1216. /**
  1217. * qla2x00_program_flash_address() - Programs a flash address
  1218. * @ha: HA context
  1219. * @addr: Address in flash to program
  1220. * @data: Data to be written in flash
  1221. * @man_id: Flash manufacturer ID
  1222. * @flash_id: Flash ID
  1223. *
  1224. * Returns 0 on success, else non-zero.
  1225. */
  1226. static int
  1227. qla2x00_program_flash_address(scsi_qla_host_t *ha, uint32_t addr, uint8_t data,
  1228. uint8_t man_id, uint8_t flash_id)
  1229. {
  1230. /* Write Program Command Sequence. */
  1231. if (IS_OEM_001(ha)) {
  1232. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1233. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1234. qla2x00_write_flash_byte(ha, 0xaaa, 0xa0);
  1235. qla2x00_write_flash_byte(ha, addr, data);
  1236. } else {
  1237. if (man_id == 0xda && flash_id == 0xc1) {
  1238. qla2x00_write_flash_byte(ha, addr, data);
  1239. if (addr & 0x7e)
  1240. return 0;
  1241. } else {
  1242. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1243. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1244. qla2x00_write_flash_byte(ha, 0x5555, 0xa0);
  1245. qla2x00_write_flash_byte(ha, addr, data);
  1246. }
  1247. }
  1248. udelay(150);
  1249. /* Wait for write to complete. */
  1250. return qla2x00_poll_flash(ha, addr, data, man_id, flash_id);
  1251. }
  1252. /**
  1253. * qla2x00_erase_flash() - Erase the flash.
  1254. * @ha: HA context
  1255. * @man_id: Flash manufacturer ID
  1256. * @flash_id: Flash ID
  1257. *
  1258. * Returns 0 on success, else non-zero.
  1259. */
  1260. static int
  1261. qla2x00_erase_flash(scsi_qla_host_t *ha, uint8_t man_id, uint8_t flash_id)
  1262. {
  1263. /* Individual Sector Erase Command Sequence */
  1264. if (IS_OEM_001(ha)) {
  1265. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1266. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1267. qla2x00_write_flash_byte(ha, 0xaaa, 0x80);
  1268. qla2x00_write_flash_byte(ha, 0xaaa, 0xaa);
  1269. qla2x00_write_flash_byte(ha, 0x555, 0x55);
  1270. qla2x00_write_flash_byte(ha, 0xaaa, 0x10);
  1271. } else {
  1272. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1273. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1274. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1275. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1276. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1277. qla2x00_write_flash_byte(ha, 0x5555, 0x10);
  1278. }
  1279. udelay(150);
  1280. /* Wait for erase to complete. */
  1281. return qla2x00_poll_flash(ha, 0x00, 0x80, man_id, flash_id);
  1282. }
  1283. /**
  1284. * qla2x00_erase_flash_sector() - Erase a flash sector.
  1285. * @ha: HA context
  1286. * @addr: Flash sector to erase
  1287. * @sec_mask: Sector address mask
  1288. * @man_id: Flash manufacturer ID
  1289. * @flash_id: Flash ID
  1290. *
  1291. * Returns 0 on success, else non-zero.
  1292. */
  1293. static int
  1294. qla2x00_erase_flash_sector(scsi_qla_host_t *ha, uint32_t addr,
  1295. uint32_t sec_mask, uint8_t man_id, uint8_t flash_id)
  1296. {
  1297. /* Individual Sector Erase Command Sequence */
  1298. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1299. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1300. qla2x00_write_flash_byte(ha, 0x5555, 0x80);
  1301. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1302. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1303. if (man_id == 0x1f && flash_id == 0x13)
  1304. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x10);
  1305. else
  1306. qla2x00_write_flash_byte(ha, addr & sec_mask, 0x30);
  1307. udelay(150);
  1308. /* Wait for erase to complete. */
  1309. return qla2x00_poll_flash(ha, addr, 0x80, man_id, flash_id);
  1310. }
  1311. /**
  1312. * qla2x00_get_flash_manufacturer() - Read manufacturer ID from flash chip.
  1313. * @man_id: Flash manufacturer ID
  1314. * @flash_id: Flash ID
  1315. */
  1316. static void
  1317. qla2x00_get_flash_manufacturer(scsi_qla_host_t *ha, uint8_t *man_id,
  1318. uint8_t *flash_id)
  1319. {
  1320. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1321. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1322. qla2x00_write_flash_byte(ha, 0x5555, 0x90);
  1323. *man_id = qla2x00_read_flash_byte(ha, 0x0000);
  1324. *flash_id = qla2x00_read_flash_byte(ha, 0x0001);
  1325. qla2x00_write_flash_byte(ha, 0x5555, 0xaa);
  1326. qla2x00_write_flash_byte(ha, 0x2aaa, 0x55);
  1327. qla2x00_write_flash_byte(ha, 0x5555, 0xf0);
  1328. }
  1329. static void
  1330. qla2x00_read_flash_data(scsi_qla_host_t *ha, uint8_t *tmp_buf, uint32_t saddr,
  1331. uint32_t length)
  1332. {
  1333. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1334. uint32_t midpoint, ilength;
  1335. uint8_t data;
  1336. midpoint = length / 2;
  1337. WRT_REG_WORD(&reg->nvram, 0);
  1338. RD_REG_WORD(&reg->nvram);
  1339. for (ilength = 0; ilength < length; saddr++, ilength++, tmp_buf++) {
  1340. if (ilength == midpoint) {
  1341. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1342. RD_REG_WORD(&reg->nvram);
  1343. }
  1344. data = qla2x00_read_flash_byte(ha, saddr);
  1345. if (saddr % 100)
  1346. udelay(10);
  1347. *tmp_buf = data;
  1348. cond_resched();
  1349. }
  1350. }
  1351. static inline void
  1352. qla2x00_suspend_hba(struct scsi_qla_host *ha)
  1353. {
  1354. int cnt;
  1355. unsigned long flags;
  1356. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1357. /* Suspend HBA. */
  1358. scsi_block_requests(ha->host);
  1359. ha->isp_ops->disable_intrs(ha);
  1360. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1361. /* Pause RISC. */
  1362. spin_lock_irqsave(&ha->hardware_lock, flags);
  1363. WRT_REG_WORD(&reg->hccr, HCCR_PAUSE_RISC);
  1364. RD_REG_WORD(&reg->hccr);
  1365. if (IS_QLA2100(ha) || IS_QLA2200(ha) || IS_QLA2300(ha)) {
  1366. for (cnt = 0; cnt < 30000; cnt++) {
  1367. if ((RD_REG_WORD(&reg->hccr) & HCCR_RISC_PAUSE) != 0)
  1368. break;
  1369. udelay(100);
  1370. }
  1371. } else {
  1372. udelay(10);
  1373. }
  1374. spin_unlock_irqrestore(&ha->hardware_lock, flags);
  1375. }
  1376. static inline void
  1377. qla2x00_resume_hba(struct scsi_qla_host *ha)
  1378. {
  1379. /* Resume HBA. */
  1380. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1381. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1382. qla2xxx_wake_dpc(ha);
  1383. qla2x00_wait_for_hba_online(ha);
  1384. scsi_unblock_requests(ha->host);
  1385. }
  1386. uint8_t *
  1387. qla2x00_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1388. uint32_t offset, uint32_t length)
  1389. {
  1390. uint32_t addr, midpoint;
  1391. uint8_t *data;
  1392. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1393. /* Suspend HBA. */
  1394. qla2x00_suspend_hba(ha);
  1395. /* Go with read. */
  1396. midpoint = ha->optrom_size / 2;
  1397. qla2x00_flash_enable(ha);
  1398. WRT_REG_WORD(&reg->nvram, 0);
  1399. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1400. for (addr = offset, data = buf; addr < length; addr++, data++) {
  1401. if (addr == midpoint) {
  1402. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1403. RD_REG_WORD(&reg->nvram); /* PCI Posting. */
  1404. }
  1405. *data = qla2x00_read_flash_byte(ha, addr);
  1406. }
  1407. qla2x00_flash_disable(ha);
  1408. /* Resume HBA. */
  1409. qla2x00_resume_hba(ha);
  1410. return buf;
  1411. }
  1412. int
  1413. qla2x00_write_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1414. uint32_t offset, uint32_t length)
  1415. {
  1416. int rval;
  1417. uint8_t man_id, flash_id, sec_number, data;
  1418. uint16_t wd;
  1419. uint32_t addr, liter, sec_mask, rest_addr;
  1420. struct device_reg_2xxx __iomem *reg = &ha->iobase->isp;
  1421. /* Suspend HBA. */
  1422. qla2x00_suspend_hba(ha);
  1423. rval = QLA_SUCCESS;
  1424. sec_number = 0;
  1425. /* Reset ISP chip. */
  1426. WRT_REG_WORD(&reg->ctrl_status, CSR_ISP_SOFT_RESET);
  1427. pci_read_config_word(ha->pdev, PCI_COMMAND, &wd);
  1428. /* Go with write. */
  1429. qla2x00_flash_enable(ha);
  1430. do { /* Loop once to provide quick error exit */
  1431. /* Structure of flash memory based on manufacturer */
  1432. if (IS_OEM_001(ha)) {
  1433. /* OEM variant with special flash part. */
  1434. man_id = flash_id = 0;
  1435. rest_addr = 0xffff;
  1436. sec_mask = 0x10000;
  1437. goto update_flash;
  1438. }
  1439. qla2x00_get_flash_manufacturer(ha, &man_id, &flash_id);
  1440. switch (man_id) {
  1441. case 0x20: /* ST flash. */
  1442. if (flash_id == 0xd2 || flash_id == 0xe3) {
  1443. /*
  1444. * ST m29w008at part - 64kb sector size with
  1445. * 32kb,8kb,8kb,16kb sectors at memory address
  1446. * 0xf0000.
  1447. */
  1448. rest_addr = 0xffff;
  1449. sec_mask = 0x10000;
  1450. break;
  1451. }
  1452. /*
  1453. * ST m29w010b part - 16kb sector size
  1454. * Default to 16kb sectors
  1455. */
  1456. rest_addr = 0x3fff;
  1457. sec_mask = 0x1c000;
  1458. break;
  1459. case 0x40: /* Mostel flash. */
  1460. /* Mostel v29c51001 part - 512 byte sector size. */
  1461. rest_addr = 0x1ff;
  1462. sec_mask = 0x1fe00;
  1463. break;
  1464. case 0xbf: /* SST flash. */
  1465. /* SST39sf10 part - 4kb sector size. */
  1466. rest_addr = 0xfff;
  1467. sec_mask = 0x1f000;
  1468. break;
  1469. case 0xda: /* Winbond flash. */
  1470. /* Winbond W29EE011 part - 256 byte sector size. */
  1471. rest_addr = 0x7f;
  1472. sec_mask = 0x1ff80;
  1473. break;
  1474. case 0xc2: /* Macronix flash. */
  1475. /* 64k sector size. */
  1476. if (flash_id == 0x38 || flash_id == 0x4f) {
  1477. rest_addr = 0xffff;
  1478. sec_mask = 0x10000;
  1479. break;
  1480. }
  1481. /* Fall through... */
  1482. case 0x1f: /* Atmel flash. */
  1483. /* 512k sector size. */
  1484. if (flash_id == 0x13) {
  1485. rest_addr = 0x7fffffff;
  1486. sec_mask = 0x80000000;
  1487. break;
  1488. }
  1489. /* Fall through... */
  1490. case 0x01: /* AMD flash. */
  1491. if (flash_id == 0x38 || flash_id == 0x40 ||
  1492. flash_id == 0x4f) {
  1493. /* Am29LV081 part - 64kb sector size. */
  1494. /* Am29LV002BT part - 64kb sector size. */
  1495. rest_addr = 0xffff;
  1496. sec_mask = 0x10000;
  1497. break;
  1498. } else if (flash_id == 0x3e) {
  1499. /*
  1500. * Am29LV008b part - 64kb sector size with
  1501. * 32kb,8kb,8kb,16kb sector at memory address
  1502. * h0xf0000.
  1503. */
  1504. rest_addr = 0xffff;
  1505. sec_mask = 0x10000;
  1506. break;
  1507. } else if (flash_id == 0x20 || flash_id == 0x6e) {
  1508. /*
  1509. * Am29LV010 part or AM29f010 - 16kb sector
  1510. * size.
  1511. */
  1512. rest_addr = 0x3fff;
  1513. sec_mask = 0x1c000;
  1514. break;
  1515. } else if (flash_id == 0x6d) {
  1516. /* Am29LV001 part - 8kb sector size. */
  1517. rest_addr = 0x1fff;
  1518. sec_mask = 0x1e000;
  1519. break;
  1520. }
  1521. default:
  1522. /* Default to 16 kb sector size. */
  1523. rest_addr = 0x3fff;
  1524. sec_mask = 0x1c000;
  1525. break;
  1526. }
  1527. update_flash:
  1528. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1529. if (qla2x00_erase_flash(ha, man_id, flash_id)) {
  1530. rval = QLA_FUNCTION_FAILED;
  1531. break;
  1532. }
  1533. }
  1534. for (addr = offset, liter = 0; liter < length; liter++,
  1535. addr++) {
  1536. data = buf[liter];
  1537. /* Are we at the beginning of a sector? */
  1538. if ((addr & rest_addr) == 0) {
  1539. if (IS_QLA2322(ha) || IS_QLA6322(ha)) {
  1540. if (addr >= 0x10000UL) {
  1541. if (((addr >> 12) & 0xf0) &&
  1542. ((man_id == 0x01 &&
  1543. flash_id == 0x3e) ||
  1544. (man_id == 0x20 &&
  1545. flash_id == 0xd2))) {
  1546. sec_number++;
  1547. if (sec_number == 1) {
  1548. rest_addr =
  1549. 0x7fff;
  1550. sec_mask =
  1551. 0x18000;
  1552. } else if (
  1553. sec_number == 2 ||
  1554. sec_number == 3) {
  1555. rest_addr =
  1556. 0x1fff;
  1557. sec_mask =
  1558. 0x1e000;
  1559. } else if (
  1560. sec_number == 4) {
  1561. rest_addr =
  1562. 0x3fff;
  1563. sec_mask =
  1564. 0x1c000;
  1565. }
  1566. }
  1567. }
  1568. } else if (addr == ha->optrom_size / 2) {
  1569. WRT_REG_WORD(&reg->nvram, NVR_SELECT);
  1570. RD_REG_WORD(&reg->nvram);
  1571. }
  1572. if (flash_id == 0xda && man_id == 0xc1) {
  1573. qla2x00_write_flash_byte(ha, 0x5555,
  1574. 0xaa);
  1575. qla2x00_write_flash_byte(ha, 0x2aaa,
  1576. 0x55);
  1577. qla2x00_write_flash_byte(ha, 0x5555,
  1578. 0xa0);
  1579. } else if (!IS_QLA2322(ha) && !IS_QLA6322(ha)) {
  1580. /* Then erase it */
  1581. if (qla2x00_erase_flash_sector(ha,
  1582. addr, sec_mask, man_id,
  1583. flash_id)) {
  1584. rval = QLA_FUNCTION_FAILED;
  1585. break;
  1586. }
  1587. if (man_id == 0x01 && flash_id == 0x6d)
  1588. sec_number++;
  1589. }
  1590. }
  1591. if (man_id == 0x01 && flash_id == 0x6d) {
  1592. if (sec_number == 1 &&
  1593. addr == (rest_addr - 1)) {
  1594. rest_addr = 0x0fff;
  1595. sec_mask = 0x1f000;
  1596. } else if (sec_number == 3 && (addr & 0x7ffe)) {
  1597. rest_addr = 0x3fff;
  1598. sec_mask = 0x1c000;
  1599. }
  1600. }
  1601. if (qla2x00_program_flash_address(ha, addr, data,
  1602. man_id, flash_id)) {
  1603. rval = QLA_FUNCTION_FAILED;
  1604. break;
  1605. }
  1606. cond_resched();
  1607. }
  1608. } while (0);
  1609. qla2x00_flash_disable(ha);
  1610. /* Resume HBA. */
  1611. qla2x00_resume_hba(ha);
  1612. return rval;
  1613. }
  1614. uint8_t *
  1615. qla24xx_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1616. uint32_t offset, uint32_t length)
  1617. {
  1618. /* Suspend HBA. */
  1619. scsi_block_requests(ha->host);
  1620. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1621. /* Go with read. */
  1622. qla24xx_read_flash_data(ha, (uint32_t *)buf, offset >> 2, length >> 2);
  1623. /* Resume HBA. */
  1624. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1625. scsi_unblock_requests(ha->host);
  1626. return buf;
  1627. }
  1628. int
  1629. qla24xx_write_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1630. uint32_t offset, uint32_t length)
  1631. {
  1632. int rval;
  1633. /* Suspend HBA. */
  1634. scsi_block_requests(ha->host);
  1635. set_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1636. /* Go with write. */
  1637. rval = qla24xx_write_flash_data(ha, (uint32_t *)buf, offset >> 2,
  1638. length >> 2);
  1639. /* Resume HBA -- RISC reset needed. */
  1640. clear_bit(MBX_UPDATE_FLASH_ACTIVE, &ha->mbx_cmd_flags);
  1641. set_bit(ISP_ABORT_NEEDED, &ha->dpc_flags);
  1642. qla2xxx_wake_dpc(ha);
  1643. qla2x00_wait_for_hba_online(ha);
  1644. scsi_unblock_requests(ha->host);
  1645. return rval;
  1646. }
  1647. uint8_t *
  1648. qla25xx_read_optrom_data(struct scsi_qla_host *ha, uint8_t *buf,
  1649. uint32_t offset, uint32_t length)
  1650. {
  1651. int rval;
  1652. dma_addr_t optrom_dma;
  1653. void *optrom;
  1654. uint8_t *pbuf;
  1655. uint32_t faddr, left, burst;
  1656. if (offset & 0xfff)
  1657. goto slow_read;
  1658. if (length < OPTROM_BURST_SIZE)
  1659. goto slow_read;
  1660. optrom = dma_alloc_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  1661. &optrom_dma, GFP_KERNEL);
  1662. if (!optrom) {
  1663. qla_printk(KERN_DEBUG, ha,
  1664. "Unable to allocate memory for optrom burst read "
  1665. "(%x KB).\n", OPTROM_BURST_SIZE / 1024);
  1666. goto slow_read;
  1667. }
  1668. pbuf = buf;
  1669. faddr = offset >> 2;
  1670. left = length >> 2;
  1671. burst = OPTROM_BURST_DWORDS;
  1672. while (left != 0) {
  1673. if (burst > left)
  1674. burst = left;
  1675. rval = qla2x00_dump_ram(ha, optrom_dma,
  1676. flash_data_to_access_addr(faddr), burst);
  1677. if (rval) {
  1678. qla_printk(KERN_WARNING, ha,
  1679. "Unable to burst-read optrom segment "
  1680. "(%x/%x/%llx).\n", rval,
  1681. flash_data_to_access_addr(faddr),
  1682. (unsigned long long)optrom_dma);
  1683. qla_printk(KERN_WARNING, ha,
  1684. "Reverting to slow-read.\n");
  1685. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE,
  1686. optrom, optrom_dma);
  1687. goto slow_read;
  1688. }
  1689. memcpy(pbuf, optrom, burst * 4);
  1690. left -= burst;
  1691. faddr += burst;
  1692. pbuf += burst * 4;
  1693. }
  1694. dma_free_coherent(&ha->pdev->dev, OPTROM_BURST_SIZE, optrom,
  1695. optrom_dma);
  1696. return buf;
  1697. slow_read:
  1698. return qla24xx_read_optrom_data(ha, buf, offset, length);
  1699. }
  1700. /**
  1701. * qla2x00_get_fcode_version() - Determine an FCODE image's version.
  1702. * @ha: HA context
  1703. * @pcids: Pointer to the FCODE PCI data structure
  1704. *
  1705. * The process of retrieving the FCODE version information is at best
  1706. * described as interesting.
  1707. *
  1708. * Within the first 100h bytes of the image an ASCII string is present
  1709. * which contains several pieces of information including the FCODE
  1710. * version. Unfortunately it seems the only reliable way to retrieve
  1711. * the version is by scanning for another sentinel within the string,
  1712. * the FCODE build date:
  1713. *
  1714. * ... 2.00.02 10/17/02 ...
  1715. *
  1716. * Returns QLA_SUCCESS on successful retrieval of version.
  1717. */
  1718. static void
  1719. qla2x00_get_fcode_version(scsi_qla_host_t *ha, uint32_t pcids)
  1720. {
  1721. int ret = QLA_FUNCTION_FAILED;
  1722. uint32_t istart, iend, iter, vend;
  1723. uint8_t do_next, rbyte, *vbyte;
  1724. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1725. /* Skip the PCI data structure. */
  1726. istart = pcids +
  1727. ((qla2x00_read_flash_byte(ha, pcids + 0x0B) << 8) |
  1728. qla2x00_read_flash_byte(ha, pcids + 0x0A));
  1729. iend = istart + 0x100;
  1730. do {
  1731. /* Scan for the sentinel date string...eeewww. */
  1732. do_next = 0;
  1733. iter = istart;
  1734. while ((iter < iend) && !do_next) {
  1735. iter++;
  1736. if (qla2x00_read_flash_byte(ha, iter) == '/') {
  1737. if (qla2x00_read_flash_byte(ha, iter + 2) ==
  1738. '/')
  1739. do_next++;
  1740. else if (qla2x00_read_flash_byte(ha,
  1741. iter + 3) == '/')
  1742. do_next++;
  1743. }
  1744. }
  1745. if (!do_next)
  1746. break;
  1747. /* Backtrack to previous ' ' (space). */
  1748. do_next = 0;
  1749. while ((iter > istart) && !do_next) {
  1750. iter--;
  1751. if (qla2x00_read_flash_byte(ha, iter) == ' ')
  1752. do_next++;
  1753. }
  1754. if (!do_next)
  1755. break;
  1756. /*
  1757. * Mark end of version tag, and find previous ' ' (space) or
  1758. * string length (recent FCODE images -- major hack ahead!!!).
  1759. */
  1760. vend = iter - 1;
  1761. do_next = 0;
  1762. while ((iter > istart) && !do_next) {
  1763. iter--;
  1764. rbyte = qla2x00_read_flash_byte(ha, iter);
  1765. if (rbyte == ' ' || rbyte == 0xd || rbyte == 0x10)
  1766. do_next++;
  1767. }
  1768. if (!do_next)
  1769. break;
  1770. /* Mark beginning of version tag, and copy data. */
  1771. iter++;
  1772. if ((vend - iter) &&
  1773. ((vend - iter) < sizeof(ha->fcode_revision))) {
  1774. vbyte = ha->fcode_revision;
  1775. while (iter <= vend) {
  1776. *vbyte++ = qla2x00_read_flash_byte(ha, iter);
  1777. iter++;
  1778. }
  1779. ret = QLA_SUCCESS;
  1780. }
  1781. } while (0);
  1782. if (ret != QLA_SUCCESS)
  1783. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1784. }
  1785. int
  1786. qla2x00_get_flash_version(scsi_qla_host_t *ha, void *mbuf)
  1787. {
  1788. int ret = QLA_SUCCESS;
  1789. uint8_t code_type, last_image;
  1790. uint32_t pcihdr, pcids;
  1791. uint8_t *dbyte;
  1792. uint16_t *dcode;
  1793. if (!ha->pio_address || !mbuf)
  1794. return QLA_FUNCTION_FAILED;
  1795. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  1796. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  1797. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1798. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1799. qla2x00_flash_enable(ha);
  1800. /* Begin with first PCI expansion ROM header. */
  1801. pcihdr = 0;
  1802. last_image = 1;
  1803. do {
  1804. /* Verify PCI expansion ROM header. */
  1805. if (qla2x00_read_flash_byte(ha, pcihdr) != 0x55 ||
  1806. qla2x00_read_flash_byte(ha, pcihdr + 0x01) != 0xaa) {
  1807. /* No signature */
  1808. DEBUG2(printk("scsi(%ld): No matching ROM "
  1809. "signature.\n", ha->host_no));
  1810. ret = QLA_FUNCTION_FAILED;
  1811. break;
  1812. }
  1813. /* Locate PCI data structure. */
  1814. pcids = pcihdr +
  1815. ((qla2x00_read_flash_byte(ha, pcihdr + 0x19) << 8) |
  1816. qla2x00_read_flash_byte(ha, pcihdr + 0x18));
  1817. /* Validate signature of PCI data structure. */
  1818. if (qla2x00_read_flash_byte(ha, pcids) != 'P' ||
  1819. qla2x00_read_flash_byte(ha, pcids + 0x1) != 'C' ||
  1820. qla2x00_read_flash_byte(ha, pcids + 0x2) != 'I' ||
  1821. qla2x00_read_flash_byte(ha, pcids + 0x3) != 'R') {
  1822. /* Incorrect header. */
  1823. DEBUG2(printk("%s(): PCI data struct not found "
  1824. "pcir_adr=%x.\n", __func__, pcids));
  1825. ret = QLA_FUNCTION_FAILED;
  1826. break;
  1827. }
  1828. /* Read version */
  1829. code_type = qla2x00_read_flash_byte(ha, pcids + 0x14);
  1830. switch (code_type) {
  1831. case ROM_CODE_TYPE_BIOS:
  1832. /* Intel x86, PC-AT compatible. */
  1833. ha->bios_revision[0] =
  1834. qla2x00_read_flash_byte(ha, pcids + 0x12);
  1835. ha->bios_revision[1] =
  1836. qla2x00_read_flash_byte(ha, pcids + 0x13);
  1837. DEBUG3(printk("%s(): read BIOS %d.%d.\n", __func__,
  1838. ha->bios_revision[1], ha->bios_revision[0]));
  1839. break;
  1840. case ROM_CODE_TYPE_FCODE:
  1841. /* Open Firmware standard for PCI (FCode). */
  1842. /* Eeeewww... */
  1843. qla2x00_get_fcode_version(ha, pcids);
  1844. break;
  1845. case ROM_CODE_TYPE_EFI:
  1846. /* Extensible Firmware Interface (EFI). */
  1847. ha->efi_revision[0] =
  1848. qla2x00_read_flash_byte(ha, pcids + 0x12);
  1849. ha->efi_revision[1] =
  1850. qla2x00_read_flash_byte(ha, pcids + 0x13);
  1851. DEBUG3(printk("%s(): read EFI %d.%d.\n", __func__,
  1852. ha->efi_revision[1], ha->efi_revision[0]));
  1853. break;
  1854. default:
  1855. DEBUG2(printk("%s(): Unrecognized code type %x at "
  1856. "pcids %x.\n", __func__, code_type, pcids));
  1857. break;
  1858. }
  1859. last_image = qla2x00_read_flash_byte(ha, pcids + 0x15) & BIT_7;
  1860. /* Locate next PCI expansion ROM. */
  1861. pcihdr += ((qla2x00_read_flash_byte(ha, pcids + 0x11) << 8) |
  1862. qla2x00_read_flash_byte(ha, pcids + 0x10)) * 512;
  1863. } while (!last_image);
  1864. if (IS_QLA2322(ha)) {
  1865. /* Read firmware image information. */
  1866. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1867. dbyte = mbuf;
  1868. memset(dbyte, 0, 8);
  1869. dcode = (uint16_t *)dbyte;
  1870. qla2x00_read_flash_data(ha, dbyte, FA_RISC_CODE_ADDR * 4 + 10,
  1871. 8);
  1872. DEBUG3(printk("%s(%ld): dumping fw ver from flash:\n",
  1873. __func__, ha->host_no));
  1874. DEBUG3(qla2x00_dump_buffer((uint8_t *)dbyte, 8));
  1875. if ((dcode[0] == 0xffff && dcode[1] == 0xffff &&
  1876. dcode[2] == 0xffff && dcode[3] == 0xffff) ||
  1877. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  1878. dcode[3] == 0)) {
  1879. DEBUG2(printk("%s(): Unrecognized fw revision at "
  1880. "%x.\n", __func__, FA_RISC_CODE_ADDR * 4));
  1881. } else {
  1882. /* values are in big endian */
  1883. ha->fw_revision[0] = dbyte[0] << 16 | dbyte[1];
  1884. ha->fw_revision[1] = dbyte[2] << 16 | dbyte[3];
  1885. ha->fw_revision[2] = dbyte[4] << 16 | dbyte[5];
  1886. }
  1887. }
  1888. qla2x00_flash_disable(ha);
  1889. return ret;
  1890. }
  1891. int
  1892. qla24xx_get_flash_version(scsi_qla_host_t *ha, void *mbuf)
  1893. {
  1894. int ret = QLA_SUCCESS;
  1895. uint32_t pcihdr, pcids;
  1896. uint32_t *dcode;
  1897. uint8_t *bcode;
  1898. uint8_t code_type, last_image;
  1899. int i;
  1900. if (!mbuf)
  1901. return QLA_FUNCTION_FAILED;
  1902. memset(ha->bios_revision, 0, sizeof(ha->bios_revision));
  1903. memset(ha->efi_revision, 0, sizeof(ha->efi_revision));
  1904. memset(ha->fcode_revision, 0, sizeof(ha->fcode_revision));
  1905. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1906. dcode = mbuf;
  1907. /* Begin with first PCI expansion ROM header. */
  1908. pcihdr = 0;
  1909. last_image = 1;
  1910. do {
  1911. /* Verify PCI expansion ROM header. */
  1912. qla24xx_read_flash_data(ha, dcode, pcihdr >> 2, 0x20);
  1913. bcode = mbuf + (pcihdr % 4);
  1914. if (bcode[0x0] != 0x55 || bcode[0x1] != 0xaa) {
  1915. /* No signature */
  1916. DEBUG2(printk("scsi(%ld): No matching ROM "
  1917. "signature.\n", ha->host_no));
  1918. ret = QLA_FUNCTION_FAILED;
  1919. break;
  1920. }
  1921. /* Locate PCI data structure. */
  1922. pcids = pcihdr + ((bcode[0x19] << 8) | bcode[0x18]);
  1923. qla24xx_read_flash_data(ha, dcode, pcids >> 2, 0x20);
  1924. bcode = mbuf + (pcihdr % 4);
  1925. /* Validate signature of PCI data structure. */
  1926. if (bcode[0x0] != 'P' || bcode[0x1] != 'C' ||
  1927. bcode[0x2] != 'I' || bcode[0x3] != 'R') {
  1928. /* Incorrect header. */
  1929. DEBUG2(printk("%s(): PCI data struct not found "
  1930. "pcir_adr=%x.\n", __func__, pcids));
  1931. ret = QLA_FUNCTION_FAILED;
  1932. break;
  1933. }
  1934. /* Read version */
  1935. code_type = bcode[0x14];
  1936. switch (code_type) {
  1937. case ROM_CODE_TYPE_BIOS:
  1938. /* Intel x86, PC-AT compatible. */
  1939. ha->bios_revision[0] = bcode[0x12];
  1940. ha->bios_revision[1] = bcode[0x13];
  1941. DEBUG3(printk("%s(): read BIOS %d.%d.\n", __func__,
  1942. ha->bios_revision[1], ha->bios_revision[0]));
  1943. break;
  1944. case ROM_CODE_TYPE_FCODE:
  1945. /* Open Firmware standard for PCI (FCode). */
  1946. ha->fcode_revision[0] = bcode[0x12];
  1947. ha->fcode_revision[1] = bcode[0x13];
  1948. DEBUG3(printk("%s(): read FCODE %d.%d.\n", __func__,
  1949. ha->fcode_revision[1], ha->fcode_revision[0]));
  1950. break;
  1951. case ROM_CODE_TYPE_EFI:
  1952. /* Extensible Firmware Interface (EFI). */
  1953. ha->efi_revision[0] = bcode[0x12];
  1954. ha->efi_revision[1] = bcode[0x13];
  1955. DEBUG3(printk("%s(): read EFI %d.%d.\n", __func__,
  1956. ha->efi_revision[1], ha->efi_revision[0]));
  1957. break;
  1958. default:
  1959. DEBUG2(printk("%s(): Unrecognized code type %x at "
  1960. "pcids %x.\n", __func__, code_type, pcids));
  1961. break;
  1962. }
  1963. last_image = bcode[0x15] & BIT_7;
  1964. /* Locate next PCI expansion ROM. */
  1965. pcihdr += ((bcode[0x11] << 8) | bcode[0x10]) * 512;
  1966. } while (!last_image);
  1967. /* Read firmware image information. */
  1968. memset(ha->fw_revision, 0, sizeof(ha->fw_revision));
  1969. dcode = mbuf;
  1970. qla24xx_read_flash_data(ha, dcode, FA_RISC_CODE_ADDR + 4, 4);
  1971. for (i = 0; i < 4; i++)
  1972. dcode[i] = be32_to_cpu(dcode[i]);
  1973. if ((dcode[0] == 0xffffffff && dcode[1] == 0xffffffff &&
  1974. dcode[2] == 0xffffffff && dcode[3] == 0xffffffff) ||
  1975. (dcode[0] == 0 && dcode[1] == 0 && dcode[2] == 0 &&
  1976. dcode[3] == 0)) {
  1977. DEBUG2(printk("%s(): Unrecognized fw version at %x.\n",
  1978. __func__, FA_RISC_CODE_ADDR));
  1979. } else {
  1980. ha->fw_revision[0] = dcode[0];
  1981. ha->fw_revision[1] = dcode[1];
  1982. ha->fw_revision[2] = dcode[2];
  1983. ha->fw_revision[3] = dcode[3];
  1984. }
  1985. return ret;
  1986. }
  1987. static int
  1988. qla2xxx_hw_event_store(scsi_qla_host_t *ha, uint32_t *fdata)
  1989. {
  1990. uint32_t d[2], faddr;
  1991. /* Locate first empty entry. */
  1992. for (;;) {
  1993. if (ha->hw_event_ptr >=
  1994. ha->hw_event_start + FA_HW_EVENT_SIZE) {
  1995. DEBUG2(qla_printk(KERN_WARNING, ha,
  1996. "HW event -- Log Full!\n"));
  1997. return QLA_MEMORY_ALLOC_FAILED;
  1998. }
  1999. qla24xx_read_flash_data(ha, d, ha->hw_event_ptr, 2);
  2000. faddr = flash_data_to_access_addr(ha->hw_event_ptr);
  2001. ha->hw_event_ptr += FA_HW_EVENT_ENTRY_SIZE;
  2002. if (d[0] == __constant_cpu_to_le32(0xffffffff) &&
  2003. d[1] == __constant_cpu_to_le32(0xffffffff)) {
  2004. qla24xx_unprotect_flash(ha);
  2005. qla24xx_write_flash_dword(ha, faddr++,
  2006. cpu_to_le32(jiffies));
  2007. qla24xx_write_flash_dword(ha, faddr++, 0);
  2008. qla24xx_write_flash_dword(ha, faddr++, *fdata++);
  2009. qla24xx_write_flash_dword(ha, faddr++, *fdata);
  2010. qla24xx_protect_flash(ha);
  2011. break;
  2012. }
  2013. }
  2014. return QLA_SUCCESS;
  2015. }
  2016. int
  2017. qla2xxx_hw_event_log(scsi_qla_host_t *ha, uint16_t code, uint16_t d1,
  2018. uint16_t d2, uint16_t d3)
  2019. {
  2020. #define QMARK(a, b, c, d) \
  2021. cpu_to_le32(LSB(a) << 24 | LSB(b) << 16 | LSB(c) << 8 | LSB(d))
  2022. int rval;
  2023. uint32_t marker[2], fdata[4];
  2024. if (ha->hw_event_start == 0)
  2025. return QLA_FUNCTION_FAILED;
  2026. DEBUG2(qla_printk(KERN_WARNING, ha,
  2027. "HW event -- code=%x, d1=%x, d2=%x, d3=%x.\n", code, d1, d2, d3));
  2028. /* If marker not already found, locate or write. */
  2029. if (!ha->flags.hw_event_marker_found) {
  2030. /* Create marker. */
  2031. marker[0] = QMARK('L', ha->fw_major_version,
  2032. ha->fw_minor_version, ha->fw_subminor_version);
  2033. marker[1] = QMARK(QLA_DRIVER_MAJOR_VER, QLA_DRIVER_MINOR_VER,
  2034. QLA_DRIVER_PATCH_VER, QLA_DRIVER_BETA_VER);
  2035. /* Locate marker. */
  2036. ha->hw_event_ptr = ha->hw_event_start;
  2037. for (;;) {
  2038. qla24xx_read_flash_data(ha, fdata, ha->hw_event_ptr,
  2039. 4);
  2040. if (fdata[0] == __constant_cpu_to_le32(0xffffffff) &&
  2041. fdata[1] == __constant_cpu_to_le32(0xffffffff))
  2042. break;
  2043. ha->hw_event_ptr += FA_HW_EVENT_ENTRY_SIZE;
  2044. if (ha->hw_event_ptr >=
  2045. ha->hw_event_start + FA_HW_EVENT_SIZE) {
  2046. DEBUG2(qla_printk(KERN_WARNING, ha,
  2047. "HW event -- Log Full!\n"));
  2048. return QLA_MEMORY_ALLOC_FAILED;
  2049. }
  2050. if (fdata[2] == marker[0] && fdata[3] == marker[1]) {
  2051. ha->flags.hw_event_marker_found = 1;
  2052. break;
  2053. }
  2054. }
  2055. /* No marker, write it. */
  2056. if (!ha->flags.hw_event_marker_found) {
  2057. rval = qla2xxx_hw_event_store(ha, marker);
  2058. if (rval != QLA_SUCCESS) {
  2059. DEBUG2(qla_printk(KERN_WARNING, ha,
  2060. "HW event -- Failed marker write=%x.!\n",
  2061. rval));
  2062. return rval;
  2063. }
  2064. ha->flags.hw_event_marker_found = 1;
  2065. }
  2066. }
  2067. /* Store error. */
  2068. fdata[0] = cpu_to_le32(code << 16 | d1);
  2069. fdata[1] = cpu_to_le32(d2 << 16 | d3);
  2070. rval = qla2xxx_hw_event_store(ha, fdata);
  2071. if (rval != QLA_SUCCESS) {
  2072. DEBUG2(qla_printk(KERN_WARNING, ha,
  2073. "HW event -- Failed error write=%x.!\n",
  2074. rval));
  2075. }
  2076. return rval;
  2077. }