qlcnic_hw.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364
  1. /*
  2. * QLogic qlcnic NIC Driver
  3. * Copyright (c) 2009-2010 QLogic Corporation
  4. *
  5. * See LICENSE.qlcnic for copyright and licensing details.
  6. */
  7. #include "qlcnic.h"
  8. #include "qlcnic_hdr.h"
  9. #include <linux/slab.h>
  10. #include <net/ip.h>
  11. #include <linux/bitops.h>
  12. #define MASK(n) ((1ULL<<(n))-1)
  13. #define OCM_WIN_P3P(addr) (addr & 0xffc0000)
  14. #define GET_MEM_OFFS_2M(addr) (addr & MASK(18))
  15. #define CRB_BLK(off) ((off >> 20) & 0x3f)
  16. #define CRB_SUBBLK(off) ((off >> 16) & 0xf)
  17. #define CRB_WINDOW_2M (0x130060)
  18. #define CRB_HI(off) ((crb_hub_agt[CRB_BLK(off)] << 20) | ((off) & 0xf0000))
  19. #define CRB_INDIRECT_2M (0x1e0000UL)
  20. struct qlcnic_ms_reg_ctrl {
  21. u32 ocm_window;
  22. u32 control;
  23. u32 hi;
  24. u32 low;
  25. u32 rd[4];
  26. u32 wd[4];
  27. u64 off;
  28. };
  29. #ifndef readq
  30. static inline u64 readq(void __iomem *addr)
  31. {
  32. return readl(addr) | (((u64) readl(addr + 4)) << 32LL);
  33. }
  34. #endif
  35. #ifndef writeq
  36. static inline void writeq(u64 val, void __iomem *addr)
  37. {
  38. writel(((u32) (val)), (addr));
  39. writel(((u32) (val >> 32)), (addr + 4));
  40. }
  41. #endif
  42. static struct crb_128M_2M_block_map
  43. crb_128M_2M_map[64] __cacheline_aligned_in_smp = {
  44. {{{0, 0, 0, 0} } }, /* 0: PCI */
  45. {{{1, 0x0100000, 0x0102000, 0x120000}, /* 1: PCIE */
  46. {1, 0x0110000, 0x0120000, 0x130000},
  47. {1, 0x0120000, 0x0122000, 0x124000},
  48. {1, 0x0130000, 0x0132000, 0x126000},
  49. {1, 0x0140000, 0x0142000, 0x128000},
  50. {1, 0x0150000, 0x0152000, 0x12a000},
  51. {1, 0x0160000, 0x0170000, 0x110000},
  52. {1, 0x0170000, 0x0172000, 0x12e000},
  53. {0, 0x0000000, 0x0000000, 0x000000},
  54. {0, 0x0000000, 0x0000000, 0x000000},
  55. {0, 0x0000000, 0x0000000, 0x000000},
  56. {0, 0x0000000, 0x0000000, 0x000000},
  57. {0, 0x0000000, 0x0000000, 0x000000},
  58. {0, 0x0000000, 0x0000000, 0x000000},
  59. {1, 0x01e0000, 0x01e0800, 0x122000},
  60. {0, 0x0000000, 0x0000000, 0x000000} } },
  61. {{{1, 0x0200000, 0x0210000, 0x180000} } },/* 2: MN */
  62. {{{0, 0, 0, 0} } }, /* 3: */
  63. {{{1, 0x0400000, 0x0401000, 0x169000} } },/* 4: P2NR1 */
  64. {{{1, 0x0500000, 0x0510000, 0x140000} } },/* 5: SRE */
  65. {{{1, 0x0600000, 0x0610000, 0x1c0000} } },/* 6: NIU */
  66. {{{1, 0x0700000, 0x0704000, 0x1b8000} } },/* 7: QM */
  67. {{{1, 0x0800000, 0x0802000, 0x170000}, /* 8: SQM0 */
  68. {0, 0x0000000, 0x0000000, 0x000000},
  69. {0, 0x0000000, 0x0000000, 0x000000},
  70. {0, 0x0000000, 0x0000000, 0x000000},
  71. {0, 0x0000000, 0x0000000, 0x000000},
  72. {0, 0x0000000, 0x0000000, 0x000000},
  73. {0, 0x0000000, 0x0000000, 0x000000},
  74. {0, 0x0000000, 0x0000000, 0x000000},
  75. {0, 0x0000000, 0x0000000, 0x000000},
  76. {0, 0x0000000, 0x0000000, 0x000000},
  77. {0, 0x0000000, 0x0000000, 0x000000},
  78. {0, 0x0000000, 0x0000000, 0x000000},
  79. {0, 0x0000000, 0x0000000, 0x000000},
  80. {0, 0x0000000, 0x0000000, 0x000000},
  81. {0, 0x0000000, 0x0000000, 0x000000},
  82. {1, 0x08f0000, 0x08f2000, 0x172000} } },
  83. {{{1, 0x0900000, 0x0902000, 0x174000}, /* 9: SQM1*/
  84. {0, 0x0000000, 0x0000000, 0x000000},
  85. {0, 0x0000000, 0x0000000, 0x000000},
  86. {0, 0x0000000, 0x0000000, 0x000000},
  87. {0, 0x0000000, 0x0000000, 0x000000},
  88. {0, 0x0000000, 0x0000000, 0x000000},
  89. {0, 0x0000000, 0x0000000, 0x000000},
  90. {0, 0x0000000, 0x0000000, 0x000000},
  91. {0, 0x0000000, 0x0000000, 0x000000},
  92. {0, 0x0000000, 0x0000000, 0x000000},
  93. {0, 0x0000000, 0x0000000, 0x000000},
  94. {0, 0x0000000, 0x0000000, 0x000000},
  95. {0, 0x0000000, 0x0000000, 0x000000},
  96. {0, 0x0000000, 0x0000000, 0x000000},
  97. {0, 0x0000000, 0x0000000, 0x000000},
  98. {1, 0x09f0000, 0x09f2000, 0x176000} } },
  99. {{{0, 0x0a00000, 0x0a02000, 0x178000}, /* 10: SQM2*/
  100. {0, 0x0000000, 0x0000000, 0x000000},
  101. {0, 0x0000000, 0x0000000, 0x000000},
  102. {0, 0x0000000, 0x0000000, 0x000000},
  103. {0, 0x0000000, 0x0000000, 0x000000},
  104. {0, 0x0000000, 0x0000000, 0x000000},
  105. {0, 0x0000000, 0x0000000, 0x000000},
  106. {0, 0x0000000, 0x0000000, 0x000000},
  107. {0, 0x0000000, 0x0000000, 0x000000},
  108. {0, 0x0000000, 0x0000000, 0x000000},
  109. {0, 0x0000000, 0x0000000, 0x000000},
  110. {0, 0x0000000, 0x0000000, 0x000000},
  111. {0, 0x0000000, 0x0000000, 0x000000},
  112. {0, 0x0000000, 0x0000000, 0x000000},
  113. {0, 0x0000000, 0x0000000, 0x000000},
  114. {1, 0x0af0000, 0x0af2000, 0x17a000} } },
  115. {{{0, 0x0b00000, 0x0b02000, 0x17c000}, /* 11: SQM3*/
  116. {0, 0x0000000, 0x0000000, 0x000000},
  117. {0, 0x0000000, 0x0000000, 0x000000},
  118. {0, 0x0000000, 0x0000000, 0x000000},
  119. {0, 0x0000000, 0x0000000, 0x000000},
  120. {0, 0x0000000, 0x0000000, 0x000000},
  121. {0, 0x0000000, 0x0000000, 0x000000},
  122. {0, 0x0000000, 0x0000000, 0x000000},
  123. {0, 0x0000000, 0x0000000, 0x000000},
  124. {0, 0x0000000, 0x0000000, 0x000000},
  125. {0, 0x0000000, 0x0000000, 0x000000},
  126. {0, 0x0000000, 0x0000000, 0x000000},
  127. {0, 0x0000000, 0x0000000, 0x000000},
  128. {0, 0x0000000, 0x0000000, 0x000000},
  129. {0, 0x0000000, 0x0000000, 0x000000},
  130. {1, 0x0bf0000, 0x0bf2000, 0x17e000} } },
  131. {{{1, 0x0c00000, 0x0c04000, 0x1d4000} } },/* 12: I2Q */
  132. {{{1, 0x0d00000, 0x0d04000, 0x1a4000} } },/* 13: TMR */
  133. {{{1, 0x0e00000, 0x0e04000, 0x1a0000} } },/* 14: ROMUSB */
  134. {{{1, 0x0f00000, 0x0f01000, 0x164000} } },/* 15: PEG4 */
  135. {{{0, 0x1000000, 0x1004000, 0x1a8000} } },/* 16: XDMA */
  136. {{{1, 0x1100000, 0x1101000, 0x160000} } },/* 17: PEG0 */
  137. {{{1, 0x1200000, 0x1201000, 0x161000} } },/* 18: PEG1 */
  138. {{{1, 0x1300000, 0x1301000, 0x162000} } },/* 19: PEG2 */
  139. {{{1, 0x1400000, 0x1401000, 0x163000} } },/* 20: PEG3 */
  140. {{{1, 0x1500000, 0x1501000, 0x165000} } },/* 21: P2ND */
  141. {{{1, 0x1600000, 0x1601000, 0x166000} } },/* 22: P2NI */
  142. {{{0, 0, 0, 0} } }, /* 23: */
  143. {{{0, 0, 0, 0} } }, /* 24: */
  144. {{{0, 0, 0, 0} } }, /* 25: */
  145. {{{0, 0, 0, 0} } }, /* 26: */
  146. {{{0, 0, 0, 0} } }, /* 27: */
  147. {{{0, 0, 0, 0} } }, /* 28: */
  148. {{{1, 0x1d00000, 0x1d10000, 0x190000} } },/* 29: MS */
  149. {{{1, 0x1e00000, 0x1e01000, 0x16a000} } },/* 30: P2NR2 */
  150. {{{1, 0x1f00000, 0x1f10000, 0x150000} } },/* 31: EPG */
  151. {{{0} } }, /* 32: PCI */
  152. {{{1, 0x2100000, 0x2102000, 0x120000}, /* 33: PCIE */
  153. {1, 0x2110000, 0x2120000, 0x130000},
  154. {1, 0x2120000, 0x2122000, 0x124000},
  155. {1, 0x2130000, 0x2132000, 0x126000},
  156. {1, 0x2140000, 0x2142000, 0x128000},
  157. {1, 0x2150000, 0x2152000, 0x12a000},
  158. {1, 0x2160000, 0x2170000, 0x110000},
  159. {1, 0x2170000, 0x2172000, 0x12e000},
  160. {0, 0x0000000, 0x0000000, 0x000000},
  161. {0, 0x0000000, 0x0000000, 0x000000},
  162. {0, 0x0000000, 0x0000000, 0x000000},
  163. {0, 0x0000000, 0x0000000, 0x000000},
  164. {0, 0x0000000, 0x0000000, 0x000000},
  165. {0, 0x0000000, 0x0000000, 0x000000},
  166. {0, 0x0000000, 0x0000000, 0x000000},
  167. {0, 0x0000000, 0x0000000, 0x000000} } },
  168. {{{1, 0x2200000, 0x2204000, 0x1b0000} } },/* 34: CAM */
  169. {{{0} } }, /* 35: */
  170. {{{0} } }, /* 36: */
  171. {{{0} } }, /* 37: */
  172. {{{0} } }, /* 38: */
  173. {{{0} } }, /* 39: */
  174. {{{1, 0x2800000, 0x2804000, 0x1a4000} } },/* 40: TMR */
  175. {{{1, 0x2900000, 0x2901000, 0x16b000} } },/* 41: P2NR3 */
  176. {{{1, 0x2a00000, 0x2a00400, 0x1ac400} } },/* 42: RPMX1 */
  177. {{{1, 0x2b00000, 0x2b00400, 0x1ac800} } },/* 43: RPMX2 */
  178. {{{1, 0x2c00000, 0x2c00400, 0x1acc00} } },/* 44: RPMX3 */
  179. {{{1, 0x2d00000, 0x2d00400, 0x1ad000} } },/* 45: RPMX4 */
  180. {{{1, 0x2e00000, 0x2e00400, 0x1ad400} } },/* 46: RPMX5 */
  181. {{{1, 0x2f00000, 0x2f00400, 0x1ad800} } },/* 47: RPMX6 */
  182. {{{1, 0x3000000, 0x3000400, 0x1adc00} } },/* 48: RPMX7 */
  183. {{{0, 0x3100000, 0x3104000, 0x1a8000} } },/* 49: XDMA */
  184. {{{1, 0x3200000, 0x3204000, 0x1d4000} } },/* 50: I2Q */
  185. {{{1, 0x3300000, 0x3304000, 0x1a0000} } },/* 51: ROMUSB */
  186. {{{0} } }, /* 52: */
  187. {{{1, 0x3500000, 0x3500400, 0x1ac000} } },/* 53: RPMX0 */
  188. {{{1, 0x3600000, 0x3600400, 0x1ae000} } },/* 54: RPMX8 */
  189. {{{1, 0x3700000, 0x3700400, 0x1ae400} } },/* 55: RPMX9 */
  190. {{{1, 0x3800000, 0x3804000, 0x1d0000} } },/* 56: OCM0 */
  191. {{{1, 0x3900000, 0x3904000, 0x1b4000} } },/* 57: CRYPTO */
  192. {{{1, 0x3a00000, 0x3a04000, 0x1d8000} } },/* 58: SMB */
  193. {{{0} } }, /* 59: I2C0 */
  194. {{{0} } }, /* 60: I2C1 */
  195. {{{1, 0x3d00000, 0x3d04000, 0x1d8000} } },/* 61: LPC */
  196. {{{1, 0x3e00000, 0x3e01000, 0x167000} } },/* 62: P2NC */
  197. {{{1, 0x3f00000, 0x3f01000, 0x168000} } } /* 63: P2NR0 */
  198. };
  199. /*
  200. * top 12 bits of crb internal address (hub, agent)
  201. */
  202. static const unsigned crb_hub_agt[64] = {
  203. 0,
  204. QLCNIC_HW_CRB_HUB_AGT_ADR_PS,
  205. QLCNIC_HW_CRB_HUB_AGT_ADR_MN,
  206. QLCNIC_HW_CRB_HUB_AGT_ADR_MS,
  207. 0,
  208. QLCNIC_HW_CRB_HUB_AGT_ADR_SRE,
  209. QLCNIC_HW_CRB_HUB_AGT_ADR_NIU,
  210. QLCNIC_HW_CRB_HUB_AGT_ADR_QMN,
  211. QLCNIC_HW_CRB_HUB_AGT_ADR_SQN0,
  212. QLCNIC_HW_CRB_HUB_AGT_ADR_SQN1,
  213. QLCNIC_HW_CRB_HUB_AGT_ADR_SQN2,
  214. QLCNIC_HW_CRB_HUB_AGT_ADR_SQN3,
  215. QLCNIC_HW_CRB_HUB_AGT_ADR_I2Q,
  216. QLCNIC_HW_CRB_HUB_AGT_ADR_TIMR,
  217. QLCNIC_HW_CRB_HUB_AGT_ADR_ROMUSB,
  218. QLCNIC_HW_CRB_HUB_AGT_ADR_PGN4,
  219. QLCNIC_HW_CRB_HUB_AGT_ADR_XDMA,
  220. QLCNIC_HW_CRB_HUB_AGT_ADR_PGN0,
  221. QLCNIC_HW_CRB_HUB_AGT_ADR_PGN1,
  222. QLCNIC_HW_CRB_HUB_AGT_ADR_PGN2,
  223. QLCNIC_HW_CRB_HUB_AGT_ADR_PGN3,
  224. QLCNIC_HW_CRB_HUB_AGT_ADR_PGND,
  225. QLCNIC_HW_CRB_HUB_AGT_ADR_PGNI,
  226. QLCNIC_HW_CRB_HUB_AGT_ADR_PGS0,
  227. QLCNIC_HW_CRB_HUB_AGT_ADR_PGS1,
  228. QLCNIC_HW_CRB_HUB_AGT_ADR_PGS2,
  229. QLCNIC_HW_CRB_HUB_AGT_ADR_PGS3,
  230. 0,
  231. QLCNIC_HW_CRB_HUB_AGT_ADR_PGSI,
  232. QLCNIC_HW_CRB_HUB_AGT_ADR_SN,
  233. 0,
  234. QLCNIC_HW_CRB_HUB_AGT_ADR_EG,
  235. 0,
  236. QLCNIC_HW_CRB_HUB_AGT_ADR_PS,
  237. QLCNIC_HW_CRB_HUB_AGT_ADR_CAM,
  238. 0,
  239. 0,
  240. 0,
  241. 0,
  242. 0,
  243. QLCNIC_HW_CRB_HUB_AGT_ADR_TIMR,
  244. 0,
  245. QLCNIC_HW_CRB_HUB_AGT_ADR_RPMX1,
  246. QLCNIC_HW_CRB_HUB_AGT_ADR_RPMX2,
  247. QLCNIC_HW_CRB_HUB_AGT_ADR_RPMX3,
  248. QLCNIC_HW_CRB_HUB_AGT_ADR_RPMX4,
  249. QLCNIC_HW_CRB_HUB_AGT_ADR_RPMX5,
  250. QLCNIC_HW_CRB_HUB_AGT_ADR_RPMX6,
  251. QLCNIC_HW_CRB_HUB_AGT_ADR_RPMX7,
  252. QLCNIC_HW_CRB_HUB_AGT_ADR_XDMA,
  253. QLCNIC_HW_CRB_HUB_AGT_ADR_I2Q,
  254. QLCNIC_HW_CRB_HUB_AGT_ADR_ROMUSB,
  255. 0,
  256. QLCNIC_HW_CRB_HUB_AGT_ADR_RPMX0,
  257. QLCNIC_HW_CRB_HUB_AGT_ADR_RPMX8,
  258. QLCNIC_HW_CRB_HUB_AGT_ADR_RPMX9,
  259. QLCNIC_HW_CRB_HUB_AGT_ADR_OCM0,
  260. 0,
  261. QLCNIC_HW_CRB_HUB_AGT_ADR_SMB,
  262. QLCNIC_HW_CRB_HUB_AGT_ADR_I2C0,
  263. QLCNIC_HW_CRB_HUB_AGT_ADR_I2C1,
  264. 0,
  265. QLCNIC_HW_CRB_HUB_AGT_ADR_PGNC,
  266. 0,
  267. };
  268. static const u32 msi_tgt_status[8] = {
  269. ISR_INT_TARGET_STATUS, ISR_INT_TARGET_STATUS_F1,
  270. ISR_INT_TARGET_STATUS_F2, ISR_INT_TARGET_STATUS_F3,
  271. ISR_INT_TARGET_STATUS_F4, ISR_INT_TARGET_STATUS_F5,
  272. ISR_INT_TARGET_STATUS_F6, ISR_INT_TARGET_STATUS_F7
  273. };
  274. /* PCI Windowing for DDR regions. */
  275. #define QLCNIC_PCIE_SEM_TIMEOUT 10000
  276. static void qlcnic_read_window_reg(u32 addr, void __iomem *bar0, u32 *data)
  277. {
  278. u32 dest;
  279. void __iomem *val;
  280. dest = addr & 0xFFFF0000;
  281. val = bar0 + QLCNIC_FW_DUMP_REG1;
  282. writel(dest, val);
  283. readl(val);
  284. val = bar0 + QLCNIC_FW_DUMP_REG2 + LSW(addr);
  285. *data = readl(val);
  286. }
  287. static void qlcnic_write_window_reg(u32 addr, void __iomem *bar0, u32 data)
  288. {
  289. u32 dest;
  290. void __iomem *val;
  291. dest = addr & 0xFFFF0000;
  292. val = bar0 + QLCNIC_FW_DUMP_REG1;
  293. writel(dest, val);
  294. readl(val);
  295. val = bar0 + QLCNIC_FW_DUMP_REG2 + LSW(addr);
  296. writel(data, val);
  297. readl(val);
  298. }
  299. int
  300. qlcnic_pcie_sem_lock(struct qlcnic_adapter *adapter, int sem, u32 id_reg)
  301. {
  302. int done = 0, timeout = 0;
  303. while (!done) {
  304. done = QLCRD32(adapter, QLCNIC_PCIE_REG(PCIE_SEM_LOCK(sem)));
  305. if (done == 1)
  306. break;
  307. if (++timeout >= QLCNIC_PCIE_SEM_TIMEOUT) {
  308. dev_err(&adapter->pdev->dev,
  309. "Failed to acquire sem=%d lock; holdby=%d\n",
  310. sem, id_reg ? QLCRD32(adapter, id_reg) : -1);
  311. return -EIO;
  312. }
  313. msleep(1);
  314. }
  315. if (id_reg)
  316. QLCWR32(adapter, id_reg, adapter->portnum);
  317. return 0;
  318. }
  319. void
  320. qlcnic_pcie_sem_unlock(struct qlcnic_adapter *adapter, int sem)
  321. {
  322. QLCRD32(adapter, QLCNIC_PCIE_REG(PCIE_SEM_UNLOCK(sem)));
  323. }
  324. static int qlcnic_ind_rd(struct qlcnic_adapter *adapter, u32 addr)
  325. {
  326. u32 data;
  327. if (qlcnic_82xx_check(adapter))
  328. qlcnic_read_window_reg(addr, adapter->ahw->pci_base0, &data);
  329. else
  330. return -EIO;
  331. return data;
  332. }
  333. static void qlcnic_ind_wr(struct qlcnic_adapter *adapter, u32 addr, u32 data)
  334. {
  335. if (qlcnic_82xx_check(adapter))
  336. qlcnic_write_window_reg(addr, adapter->ahw->pci_base0, data);
  337. }
  338. static int
  339. qlcnic_send_cmd_descs(struct qlcnic_adapter *adapter,
  340. struct cmd_desc_type0 *cmd_desc_arr, int nr_desc)
  341. {
  342. u32 i, producer;
  343. struct qlcnic_cmd_buffer *pbuf;
  344. struct cmd_desc_type0 *cmd_desc;
  345. struct qlcnic_host_tx_ring *tx_ring;
  346. i = 0;
  347. if (!test_bit(__QLCNIC_FW_ATTACHED, &adapter->state))
  348. return -EIO;
  349. tx_ring = adapter->tx_ring;
  350. __netif_tx_lock_bh(tx_ring->txq);
  351. producer = tx_ring->producer;
  352. if (nr_desc >= qlcnic_tx_avail(tx_ring)) {
  353. netif_tx_stop_queue(tx_ring->txq);
  354. smp_mb();
  355. if (qlcnic_tx_avail(tx_ring) > nr_desc) {
  356. if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
  357. netif_tx_wake_queue(tx_ring->txq);
  358. } else {
  359. adapter->stats.xmit_off++;
  360. __netif_tx_unlock_bh(tx_ring->txq);
  361. return -EBUSY;
  362. }
  363. }
  364. do {
  365. cmd_desc = &cmd_desc_arr[i];
  366. pbuf = &tx_ring->cmd_buf_arr[producer];
  367. pbuf->skb = NULL;
  368. pbuf->frag_count = 0;
  369. memcpy(&tx_ring->desc_head[producer],
  370. cmd_desc, sizeof(struct cmd_desc_type0));
  371. producer = get_next_index(producer, tx_ring->num_desc);
  372. i++;
  373. } while (i != nr_desc);
  374. tx_ring->producer = producer;
  375. qlcnic_update_cmd_producer(tx_ring);
  376. __netif_tx_unlock_bh(tx_ring->txq);
  377. return 0;
  378. }
  379. static int
  380. qlcnic_sre_macaddr_change(struct qlcnic_adapter *adapter, u8 *addr,
  381. __le16 vlan_id, unsigned op)
  382. {
  383. struct qlcnic_nic_req req;
  384. struct qlcnic_mac_req *mac_req;
  385. struct qlcnic_vlan_req *vlan_req;
  386. u64 word;
  387. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  388. req.qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
  389. word = QLCNIC_MAC_EVENT | ((u64)adapter->portnum << 16);
  390. req.req_hdr = cpu_to_le64(word);
  391. mac_req = (struct qlcnic_mac_req *)&req.words[0];
  392. mac_req->op = op;
  393. memcpy(mac_req->mac_addr, addr, 6);
  394. vlan_req = (struct qlcnic_vlan_req *)&req.words[1];
  395. vlan_req->vlan_id = vlan_id;
  396. return qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  397. }
  398. static int qlcnic_nic_add_mac(struct qlcnic_adapter *adapter, const u8 *addr)
  399. {
  400. struct list_head *head;
  401. struct qlcnic_mac_list_s *cur;
  402. /* look up if already exists */
  403. list_for_each(head, &adapter->mac_list) {
  404. cur = list_entry(head, struct qlcnic_mac_list_s, list);
  405. if (memcmp(addr, cur->mac_addr, ETH_ALEN) == 0)
  406. return 0;
  407. }
  408. cur = kzalloc(sizeof(struct qlcnic_mac_list_s), GFP_ATOMIC);
  409. if (cur == NULL) {
  410. dev_err(&adapter->netdev->dev,
  411. "failed to add mac address filter\n");
  412. return -ENOMEM;
  413. }
  414. memcpy(cur->mac_addr, addr, ETH_ALEN);
  415. if (qlcnic_sre_macaddr_change(adapter,
  416. cur->mac_addr, 0, QLCNIC_MAC_ADD)) {
  417. kfree(cur);
  418. return -EIO;
  419. }
  420. list_add_tail(&cur->list, &adapter->mac_list);
  421. return 0;
  422. }
  423. void qlcnic_set_multi(struct net_device *netdev)
  424. {
  425. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  426. struct netdev_hw_addr *ha;
  427. static const u8 bcast_addr[ETH_ALEN] = {
  428. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  429. };
  430. u32 mode = VPORT_MISS_MODE_DROP;
  431. if (!test_bit(__QLCNIC_FW_ATTACHED, &adapter->state))
  432. return;
  433. qlcnic_nic_add_mac(adapter, adapter->mac_addr);
  434. qlcnic_nic_add_mac(adapter, bcast_addr);
  435. if (netdev->flags & IFF_PROMISC) {
  436. if (!(adapter->flags & QLCNIC_PROMISC_DISABLED))
  437. mode = VPORT_MISS_MODE_ACCEPT_ALL;
  438. goto send_fw_cmd;
  439. }
  440. if ((netdev->flags & IFF_ALLMULTI) ||
  441. (netdev_mc_count(netdev) > adapter->ahw->max_mc_count)) {
  442. mode = VPORT_MISS_MODE_ACCEPT_MULTI;
  443. goto send_fw_cmd;
  444. }
  445. if (!netdev_mc_empty(netdev)) {
  446. netdev_for_each_mc_addr(ha, netdev) {
  447. qlcnic_nic_add_mac(adapter, ha->addr);
  448. }
  449. }
  450. send_fw_cmd:
  451. if (mode == VPORT_MISS_MODE_ACCEPT_ALL) {
  452. qlcnic_alloc_lb_filters_mem(adapter);
  453. adapter->mac_learn = 1;
  454. } else {
  455. adapter->mac_learn = 0;
  456. }
  457. qlcnic_nic_set_promisc(adapter, mode);
  458. }
  459. int qlcnic_nic_set_promisc(struct qlcnic_adapter *adapter, u32 mode)
  460. {
  461. struct qlcnic_nic_req req;
  462. u64 word;
  463. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  464. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  465. word = QLCNIC_H2C_OPCODE_SET_MAC_RECEIVE_MODE |
  466. ((u64)adapter->portnum << 16);
  467. req.req_hdr = cpu_to_le64(word);
  468. req.words[0] = cpu_to_le64(mode);
  469. return qlcnic_send_cmd_descs(adapter,
  470. (struct cmd_desc_type0 *)&req, 1);
  471. }
  472. void qlcnic_free_mac_list(struct qlcnic_adapter *adapter)
  473. {
  474. struct qlcnic_mac_list_s *cur;
  475. struct list_head *head = &adapter->mac_list;
  476. while (!list_empty(head)) {
  477. cur = list_entry(head->next, struct qlcnic_mac_list_s, list);
  478. qlcnic_sre_macaddr_change(adapter,
  479. cur->mac_addr, 0, QLCNIC_MAC_DEL);
  480. list_del(&cur->list);
  481. kfree(cur);
  482. }
  483. }
  484. void qlcnic_prune_lb_filters(struct qlcnic_adapter *adapter)
  485. {
  486. struct qlcnic_filter *tmp_fil;
  487. struct hlist_node *tmp_hnode, *n;
  488. struct hlist_head *head;
  489. int i;
  490. for (i = 0; i < adapter->fhash.fmax; i++) {
  491. head = &(adapter->fhash.fhead[i]);
  492. hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode)
  493. {
  494. if (jiffies >
  495. (QLCNIC_FILTER_AGE * HZ + tmp_fil->ftime)) {
  496. qlcnic_sre_macaddr_change(adapter,
  497. tmp_fil->faddr, tmp_fil->vlan_id,
  498. tmp_fil->vlan_id ? QLCNIC_MAC_VLAN_DEL :
  499. QLCNIC_MAC_DEL);
  500. spin_lock_bh(&adapter->mac_learn_lock);
  501. adapter->fhash.fnum--;
  502. hlist_del(&tmp_fil->fnode);
  503. spin_unlock_bh(&adapter->mac_learn_lock);
  504. kfree(tmp_fil);
  505. }
  506. }
  507. }
  508. }
  509. void qlcnic_delete_lb_filters(struct qlcnic_adapter *adapter)
  510. {
  511. struct qlcnic_filter *tmp_fil;
  512. struct hlist_node *tmp_hnode, *n;
  513. struct hlist_head *head;
  514. int i;
  515. for (i = 0; i < adapter->fhash.fmax; i++) {
  516. head = &(adapter->fhash.fhead[i]);
  517. hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
  518. qlcnic_sre_macaddr_change(adapter, tmp_fil->faddr,
  519. tmp_fil->vlan_id, tmp_fil->vlan_id ?
  520. QLCNIC_MAC_VLAN_DEL : QLCNIC_MAC_DEL);
  521. spin_lock_bh(&adapter->mac_learn_lock);
  522. adapter->fhash.fnum--;
  523. hlist_del(&tmp_fil->fnode);
  524. spin_unlock_bh(&adapter->mac_learn_lock);
  525. kfree(tmp_fil);
  526. }
  527. }
  528. }
  529. static int qlcnic_set_fw_loopback(struct qlcnic_adapter *adapter, u8 flag)
  530. {
  531. struct qlcnic_nic_req req;
  532. int rv;
  533. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  534. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  535. req.req_hdr = cpu_to_le64(QLCNIC_H2C_OPCODE_CONFIG_LOOPBACK |
  536. ((u64) adapter->portnum << 16) | ((u64) 0x1 << 32));
  537. req.words[0] = cpu_to_le64(flag);
  538. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  539. if (rv != 0)
  540. dev_err(&adapter->pdev->dev, "%sting loopback mode failed\n",
  541. flag ? "Set" : "Reset");
  542. return rv;
  543. }
  544. int qlcnic_set_lb_mode(struct qlcnic_adapter *adapter, u8 mode)
  545. {
  546. if (qlcnic_set_fw_loopback(adapter, mode))
  547. return -EIO;
  548. if (qlcnic_nic_set_promisc(adapter, VPORT_MISS_MODE_ACCEPT_ALL)) {
  549. qlcnic_set_fw_loopback(adapter, 0);
  550. return -EIO;
  551. }
  552. msleep(1000);
  553. return 0;
  554. }
  555. void qlcnic_clear_lb_mode(struct qlcnic_adapter *adapter)
  556. {
  557. int mode = VPORT_MISS_MODE_DROP;
  558. struct net_device *netdev = adapter->netdev;
  559. qlcnic_set_fw_loopback(adapter, 0);
  560. if (netdev->flags & IFF_PROMISC)
  561. mode = VPORT_MISS_MODE_ACCEPT_ALL;
  562. else if (netdev->flags & IFF_ALLMULTI)
  563. mode = VPORT_MISS_MODE_ACCEPT_MULTI;
  564. qlcnic_nic_set_promisc(adapter, mode);
  565. msleep(1000);
  566. }
  567. /*
  568. * Send the interrupt coalescing parameter set by ethtool to the card.
  569. */
  570. int qlcnic_config_intr_coalesce(struct qlcnic_adapter *adapter)
  571. {
  572. struct qlcnic_nic_req req;
  573. int rv;
  574. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  575. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  576. req.req_hdr = cpu_to_le64(QLCNIC_CONFIG_INTR_COALESCE |
  577. ((u64) adapter->portnum << 16));
  578. req.words[0] = cpu_to_le64(((u64) adapter->ahw->coal.flag) << 32);
  579. req.words[2] = cpu_to_le64(adapter->ahw->coal.rx_packets |
  580. ((u64) adapter->ahw->coal.rx_time_us) << 16);
  581. req.words[5] = cpu_to_le64(adapter->ahw->coal.timer_out |
  582. ((u64) adapter->ahw->coal.type) << 32 |
  583. ((u64) adapter->ahw->coal.sts_ring_mask) << 40);
  584. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  585. if (rv != 0)
  586. dev_err(&adapter->netdev->dev,
  587. "Could not send interrupt coalescing parameters\n");
  588. return rv;
  589. }
  590. int qlcnic_config_hw_lro(struct qlcnic_adapter *adapter, int enable)
  591. {
  592. struct qlcnic_nic_req req;
  593. u64 word;
  594. int rv;
  595. if (!test_bit(__QLCNIC_FW_ATTACHED, &adapter->state))
  596. return 0;
  597. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  598. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  599. word = QLCNIC_H2C_OPCODE_CONFIG_HW_LRO | ((u64)adapter->portnum << 16);
  600. req.req_hdr = cpu_to_le64(word);
  601. req.words[0] = cpu_to_le64(enable);
  602. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  603. if (rv != 0)
  604. dev_err(&adapter->netdev->dev,
  605. "Could not send configure hw lro request\n");
  606. return rv;
  607. }
  608. int qlcnic_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
  609. {
  610. struct qlcnic_nic_req req;
  611. u64 word;
  612. int rv;
  613. if (!!(adapter->flags & QLCNIC_BRIDGE_ENABLED) == enable)
  614. return 0;
  615. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  616. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  617. word = QLCNIC_H2C_OPCODE_CONFIG_BRIDGING |
  618. ((u64)adapter->portnum << 16);
  619. req.req_hdr = cpu_to_le64(word);
  620. req.words[0] = cpu_to_le64(enable);
  621. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  622. if (rv != 0)
  623. dev_err(&adapter->netdev->dev,
  624. "Could not send configure bridge mode request\n");
  625. adapter->flags ^= QLCNIC_BRIDGE_ENABLED;
  626. return rv;
  627. }
  628. #define RSS_HASHTYPE_IP_TCP 0x3
  629. int qlcnic_config_rss(struct qlcnic_adapter *adapter, int enable)
  630. {
  631. struct qlcnic_nic_req req;
  632. u64 word;
  633. int i, rv;
  634. static const u64 key[] = {
  635. 0xbeac01fa6a42b73bULL, 0x8030f20c77cb2da3ULL,
  636. 0xae7b30b4d0ca2bcbULL, 0x43a38fb04167253dULL,
  637. 0x255b0ec26d5a56daULL
  638. };
  639. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  640. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  641. word = QLCNIC_H2C_OPCODE_CONFIG_RSS | ((u64)adapter->portnum << 16);
  642. req.req_hdr = cpu_to_le64(word);
  643. /*
  644. * RSS request:
  645. * bits 3-0: hash_method
  646. * 5-4: hash_type_ipv4
  647. * 7-6: hash_type_ipv6
  648. * 8: enable
  649. * 9: use indirection table
  650. * 47-10: reserved
  651. * 63-48: indirection table mask
  652. */
  653. word = ((u64)(RSS_HASHTYPE_IP_TCP & 0x3) << 4) |
  654. ((u64)(RSS_HASHTYPE_IP_TCP & 0x3) << 6) |
  655. ((u64)(enable & 0x1) << 8) |
  656. ((0x7ULL) << 48);
  657. req.words[0] = cpu_to_le64(word);
  658. for (i = 0; i < 5; i++)
  659. req.words[i+1] = cpu_to_le64(key[i]);
  660. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  661. if (rv != 0)
  662. dev_err(&adapter->netdev->dev, "could not configure RSS\n");
  663. return rv;
  664. }
  665. int qlcnic_config_ipaddr(struct qlcnic_adapter *adapter, __be32 ip, int cmd)
  666. {
  667. struct qlcnic_nic_req req;
  668. struct qlcnic_ipaddr *ipa;
  669. u64 word;
  670. int rv;
  671. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  672. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  673. word = QLCNIC_H2C_OPCODE_CONFIG_IPADDR | ((u64)adapter->portnum << 16);
  674. req.req_hdr = cpu_to_le64(word);
  675. req.words[0] = cpu_to_le64(cmd);
  676. ipa = (struct qlcnic_ipaddr *)&req.words[1];
  677. ipa->ipv4 = ip;
  678. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  679. if (rv != 0)
  680. dev_err(&adapter->netdev->dev,
  681. "could not notify %s IP 0x%x reuqest\n",
  682. (cmd == QLCNIC_IP_UP) ? "Add" : "Remove", ip);
  683. return rv;
  684. }
  685. int qlcnic_linkevent_request(struct qlcnic_adapter *adapter, int enable)
  686. {
  687. struct qlcnic_nic_req req;
  688. u64 word;
  689. int rv;
  690. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  691. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  692. word = QLCNIC_H2C_OPCODE_GET_LINKEVENT | ((u64)adapter->portnum << 16);
  693. req.req_hdr = cpu_to_le64(word);
  694. req.words[0] = cpu_to_le64(enable | (enable << 8));
  695. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  696. if (rv != 0)
  697. dev_err(&adapter->netdev->dev,
  698. "could not configure link notification\n");
  699. return rv;
  700. }
  701. int qlcnic_send_lro_cleanup(struct qlcnic_adapter *adapter)
  702. {
  703. struct qlcnic_nic_req req;
  704. u64 word;
  705. int rv;
  706. if (!test_bit(__QLCNIC_FW_ATTACHED, &adapter->state))
  707. return 0;
  708. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  709. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  710. word = QLCNIC_H2C_OPCODE_LRO_REQUEST |
  711. ((u64)adapter->portnum << 16) |
  712. ((u64)QLCNIC_LRO_REQUEST_CLEANUP << 56) ;
  713. req.req_hdr = cpu_to_le64(word);
  714. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  715. if (rv != 0)
  716. dev_err(&adapter->netdev->dev,
  717. "could not cleanup lro flows\n");
  718. return rv;
  719. }
  720. /*
  721. * qlcnic_change_mtu - Change the Maximum Transfer Unit
  722. * @returns 0 on success, negative on failure
  723. */
  724. int qlcnic_change_mtu(struct net_device *netdev, int mtu)
  725. {
  726. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  727. int rc = 0;
  728. if (mtu < P3P_MIN_MTU || mtu > P3P_MAX_MTU) {
  729. dev_err(&adapter->netdev->dev, "%d bytes < mtu < %d bytes"
  730. " not supported\n", P3P_MAX_MTU, P3P_MIN_MTU);
  731. return -EINVAL;
  732. }
  733. rc = qlcnic_fw_cmd_set_mtu(adapter, mtu);
  734. if (!rc)
  735. netdev->mtu = mtu;
  736. return rc;
  737. }
  738. netdev_features_t qlcnic_fix_features(struct net_device *netdev,
  739. netdev_features_t features)
  740. {
  741. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  742. if ((adapter->flags & QLCNIC_ESWITCH_ENABLED)) {
  743. netdev_features_t changed = features ^ netdev->features;
  744. features ^= changed & (NETIF_F_ALL_CSUM | NETIF_F_RXCSUM);
  745. }
  746. if (!(features & NETIF_F_RXCSUM))
  747. features &= ~NETIF_F_LRO;
  748. return features;
  749. }
  750. int qlcnic_set_features(struct net_device *netdev, netdev_features_t features)
  751. {
  752. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  753. netdev_features_t changed = netdev->features ^ features;
  754. int hw_lro = (features & NETIF_F_LRO) ? QLCNIC_LRO_ENABLED : 0;
  755. if (!(changed & NETIF_F_LRO))
  756. return 0;
  757. netdev->features = features ^ NETIF_F_LRO;
  758. if (qlcnic_config_hw_lro(adapter, hw_lro))
  759. return -EIO;
  760. if ((hw_lro == 0) && qlcnic_send_lro_cleanup(adapter))
  761. return -EIO;
  762. return 0;
  763. }
  764. /*
  765. * Changes the CRB window to the specified window.
  766. */
  767. /* Returns < 0 if off is not valid,
  768. * 1 if window access is needed. 'off' is set to offset from
  769. * CRB space in 128M pci map
  770. * 0 if no window access is needed. 'off' is set to 2M addr
  771. * In: 'off' is offset from base in 128M pci map
  772. */
  773. static int qlcnic_pci_get_crb_addr_2M(struct qlcnic_hardware_context *ahw,
  774. ulong off, void __iomem **addr)
  775. {
  776. const struct crb_128M_2M_sub_block_map *m;
  777. if ((off >= QLCNIC_CRB_MAX) || (off < QLCNIC_PCI_CRBSPACE))
  778. return -EINVAL;
  779. off -= QLCNIC_PCI_CRBSPACE;
  780. /*
  781. * Try direct map
  782. */
  783. m = &crb_128M_2M_map[CRB_BLK(off)].sub_block[CRB_SUBBLK(off)];
  784. if (m->valid && (m->start_128M <= off) && (m->end_128M > off)) {
  785. *addr = ahw->pci_base0 + m->start_2M +
  786. (off - m->start_128M);
  787. return 0;
  788. }
  789. /*
  790. * Not in direct map, use crb window
  791. */
  792. *addr = ahw->pci_base0 + CRB_INDIRECT_2M + (off & MASK(16));
  793. return 1;
  794. }
  795. /*
  796. * In: 'off' is offset from CRB space in 128M pci map
  797. * Out: 'off' is 2M pci map addr
  798. * side effect: lock crb window
  799. */
  800. static int
  801. qlcnic_pci_set_crbwindow_2M(struct qlcnic_adapter *adapter, ulong off)
  802. {
  803. u32 window;
  804. void __iomem *addr = adapter->ahw->pci_base0 + CRB_WINDOW_2M;
  805. off -= QLCNIC_PCI_CRBSPACE;
  806. window = CRB_HI(off);
  807. if (window == 0) {
  808. dev_err(&adapter->pdev->dev, "Invalid offset 0x%lx\n", off);
  809. return -EIO;
  810. }
  811. writel(window, addr);
  812. if (readl(addr) != window) {
  813. if (printk_ratelimit())
  814. dev_warn(&adapter->pdev->dev,
  815. "failed to set CRB window to %d off 0x%lx\n",
  816. window, off);
  817. return -EIO;
  818. }
  819. return 0;
  820. }
  821. int
  822. qlcnic_hw_write_wx_2M(struct qlcnic_adapter *adapter, ulong off, u32 data)
  823. {
  824. unsigned long flags;
  825. int rv;
  826. void __iomem *addr = NULL;
  827. rv = qlcnic_pci_get_crb_addr_2M(adapter->ahw, off, &addr);
  828. if (rv == 0) {
  829. writel(data, addr);
  830. return 0;
  831. }
  832. if (rv > 0) {
  833. /* indirect access */
  834. write_lock_irqsave(&adapter->ahw->crb_lock, flags);
  835. crb_win_lock(adapter);
  836. rv = qlcnic_pci_set_crbwindow_2M(adapter, off);
  837. if (!rv)
  838. writel(data, addr);
  839. crb_win_unlock(adapter);
  840. write_unlock_irqrestore(&adapter->ahw->crb_lock, flags);
  841. return rv;
  842. }
  843. dev_err(&adapter->pdev->dev,
  844. "%s: invalid offset: 0x%016lx\n", __func__, off);
  845. dump_stack();
  846. return -EIO;
  847. }
  848. int qlcnic_hw_read_wx_2M(struct qlcnic_adapter *adapter, ulong off)
  849. {
  850. unsigned long flags;
  851. int rv;
  852. u32 data = -1;
  853. void __iomem *addr = NULL;
  854. rv = qlcnic_pci_get_crb_addr_2M(adapter->ahw, off, &addr);
  855. if (rv == 0)
  856. return readl(addr);
  857. if (rv > 0) {
  858. /* indirect access */
  859. write_lock_irqsave(&adapter->ahw->crb_lock, flags);
  860. crb_win_lock(adapter);
  861. if (!qlcnic_pci_set_crbwindow_2M(adapter, off))
  862. data = readl(addr);
  863. crb_win_unlock(adapter);
  864. write_unlock_irqrestore(&adapter->ahw->crb_lock, flags);
  865. return data;
  866. }
  867. dev_err(&adapter->pdev->dev,
  868. "%s: invalid offset: 0x%016lx\n", __func__, off);
  869. dump_stack();
  870. return -1;
  871. }
  872. void __iomem *qlcnic_get_ioaddr(struct qlcnic_hardware_context *ahw,
  873. u32 offset)
  874. {
  875. void __iomem *addr = NULL;
  876. WARN_ON(qlcnic_pci_get_crb_addr_2M(ahw, offset, &addr));
  877. return addr;
  878. }
  879. static int qlcnic_pci_mem_access_direct(struct qlcnic_adapter *adapter,
  880. u32 window, u64 off, u64 *data, int op)
  881. {
  882. void __iomem *addr;
  883. u32 start;
  884. mutex_lock(&adapter->ahw->mem_lock);
  885. writel(window, adapter->ahw->ocm_win_crb);
  886. /* read back to flush */
  887. readl(adapter->ahw->ocm_win_crb);
  888. start = QLCNIC_PCI_OCM0_2M + off;
  889. addr = adapter->ahw->pci_base0 + start;
  890. if (op == 0) /* read */
  891. *data = readq(addr);
  892. else /* write */
  893. writeq(*data, addr);
  894. /* Set window to 0 */
  895. writel(0, adapter->ahw->ocm_win_crb);
  896. readl(adapter->ahw->ocm_win_crb);
  897. mutex_unlock(&adapter->ahw->mem_lock);
  898. return 0;
  899. }
  900. void
  901. qlcnic_pci_camqm_read_2M(struct qlcnic_adapter *adapter, u64 off, u64 *data)
  902. {
  903. void __iomem *addr = adapter->ahw->pci_base0 +
  904. QLCNIC_PCI_CAMQM_2M_BASE + (off - QLCNIC_PCI_CAMQM);
  905. mutex_lock(&adapter->ahw->mem_lock);
  906. *data = readq(addr);
  907. mutex_unlock(&adapter->ahw->mem_lock);
  908. }
  909. void
  910. qlcnic_pci_camqm_write_2M(struct qlcnic_adapter *adapter, u64 off, u64 data)
  911. {
  912. void __iomem *addr = adapter->ahw->pci_base0 +
  913. QLCNIC_PCI_CAMQM_2M_BASE + (off - QLCNIC_PCI_CAMQM);
  914. mutex_lock(&adapter->ahw->mem_lock);
  915. writeq(data, addr);
  916. mutex_unlock(&adapter->ahw->mem_lock);
  917. }
  918. /* Set MS memory control data for different adapters */
  919. static void qlcnic_set_ms_controls(struct qlcnic_adapter *adapter, u64 off,
  920. struct qlcnic_ms_reg_ctrl *ms)
  921. {
  922. ms->control = QLCNIC_MS_CTRL;
  923. ms->low = QLCNIC_MS_ADDR_LO;
  924. ms->hi = QLCNIC_MS_ADDR_HI;
  925. if (off & 0xf) {
  926. ms->wd[0] = QLCNIC_MS_WRTDATA_LO;
  927. ms->rd[0] = QLCNIC_MS_RDDATA_LO;
  928. ms->wd[1] = QLCNIC_MS_WRTDATA_HI;
  929. ms->rd[1] = QLCNIC_MS_RDDATA_HI;
  930. ms->wd[2] = QLCNIC_MS_WRTDATA_ULO;
  931. ms->wd[3] = QLCNIC_MS_WRTDATA_UHI;
  932. ms->rd[2] = QLCNIC_MS_RDDATA_ULO;
  933. ms->rd[3] = QLCNIC_MS_RDDATA_UHI;
  934. } else {
  935. ms->wd[0] = QLCNIC_MS_WRTDATA_ULO;
  936. ms->rd[0] = QLCNIC_MS_RDDATA_ULO;
  937. ms->wd[1] = QLCNIC_MS_WRTDATA_UHI;
  938. ms->rd[1] = QLCNIC_MS_RDDATA_UHI;
  939. ms->wd[2] = QLCNIC_MS_WRTDATA_LO;
  940. ms->wd[3] = QLCNIC_MS_WRTDATA_HI;
  941. ms->rd[2] = QLCNIC_MS_RDDATA_LO;
  942. ms->rd[3] = QLCNIC_MS_RDDATA_HI;
  943. }
  944. ms->ocm_window = OCM_WIN_P3P(off);
  945. ms->off = GET_MEM_OFFS_2M(off);
  946. }
  947. int qlcnic_pci_mem_write_2M(struct qlcnic_adapter *adapter, u64 off, u64 data)
  948. {
  949. int j, ret = 0;
  950. u32 temp, off8;
  951. struct qlcnic_ms_reg_ctrl ms;
  952. /* Only 64-bit aligned access */
  953. if (off & 7)
  954. return -EIO;
  955. memset(&ms, 0, sizeof(struct qlcnic_ms_reg_ctrl));
  956. if (!(ADDR_IN_RANGE(off, QLCNIC_ADDR_QDR_NET,
  957. QLCNIC_ADDR_QDR_NET_MAX) ||
  958. ADDR_IN_RANGE(off, QLCNIC_ADDR_DDR_NET,
  959. QLCNIC_ADDR_DDR_NET_MAX)))
  960. return -EIO;
  961. qlcnic_set_ms_controls(adapter, off, &ms);
  962. if (ADDR_IN_RANGE(off, QLCNIC_ADDR_OCM0, QLCNIC_ADDR_OCM0_MAX))
  963. return qlcnic_pci_mem_access_direct(adapter, ms.ocm_window,
  964. ms.off, &data, 1);
  965. off8 = off & ~0xf;
  966. mutex_lock(&adapter->ahw->mem_lock);
  967. qlcnic_ind_wr(adapter, ms.low, off8);
  968. qlcnic_ind_wr(adapter, ms.hi, 0);
  969. qlcnic_ind_wr(adapter, ms.control, TA_CTL_ENABLE);
  970. qlcnic_ind_wr(adapter, ms.control, QLCNIC_TA_START_ENABLE);
  971. for (j = 0; j < MAX_CTL_CHECK; j++) {
  972. temp = qlcnic_ind_rd(adapter, ms.control);
  973. if ((temp & TA_CTL_BUSY) == 0)
  974. break;
  975. }
  976. if (j >= MAX_CTL_CHECK) {
  977. ret = -EIO;
  978. goto done;
  979. }
  980. /* This is the modify part of read-modify-write */
  981. qlcnic_ind_wr(adapter, ms.wd[0], qlcnic_ind_rd(adapter, ms.rd[0]));
  982. qlcnic_ind_wr(adapter, ms.wd[1], qlcnic_ind_rd(adapter, ms.rd[1]));
  983. /* This is the write part of read-modify-write */
  984. qlcnic_ind_wr(adapter, ms.wd[2], data & 0xffffffff);
  985. qlcnic_ind_wr(adapter, ms.wd[3], (data >> 32) & 0xffffffff);
  986. qlcnic_ind_wr(adapter, ms.control, QLCNIC_TA_WRITE_ENABLE);
  987. qlcnic_ind_wr(adapter, ms.control, QLCNIC_TA_WRITE_START);
  988. for (j = 0; j < MAX_CTL_CHECK; j++) {
  989. temp = qlcnic_ind_rd(adapter, ms.control);
  990. if ((temp & TA_CTL_BUSY) == 0)
  991. break;
  992. }
  993. if (j >= MAX_CTL_CHECK) {
  994. if (printk_ratelimit())
  995. dev_err(&adapter->pdev->dev,
  996. "failed to write through agent\n");
  997. ret = -EIO;
  998. } else
  999. ret = 0;
  1000. done:
  1001. mutex_unlock(&adapter->ahw->mem_lock);
  1002. return ret;
  1003. }
  1004. int qlcnic_pci_mem_read_2M(struct qlcnic_adapter *adapter, u64 off, u64 *data)
  1005. {
  1006. int j, ret;
  1007. u32 temp, off8;
  1008. u64 val;
  1009. struct qlcnic_ms_reg_ctrl ms;
  1010. /* Only 64-bit aligned access */
  1011. if (off & 7)
  1012. return -EIO;
  1013. if (!(ADDR_IN_RANGE(off, QLCNIC_ADDR_QDR_NET,
  1014. QLCNIC_ADDR_QDR_NET_MAX) ||
  1015. ADDR_IN_RANGE(off, QLCNIC_ADDR_DDR_NET,
  1016. QLCNIC_ADDR_DDR_NET_MAX)))
  1017. return -EIO;
  1018. memset(&ms, 0, sizeof(struct qlcnic_ms_reg_ctrl));
  1019. qlcnic_set_ms_controls(adapter, off, &ms);
  1020. if (ADDR_IN_RANGE(off, QLCNIC_ADDR_OCM0, QLCNIC_ADDR_OCM0_MAX))
  1021. return qlcnic_pci_mem_access_direct(adapter, ms.ocm_window,
  1022. ms.off, data, 0);
  1023. mutex_lock(&adapter->ahw->mem_lock);
  1024. off8 = off & ~0xf;
  1025. qlcnic_ind_wr(adapter, ms.low, off8);
  1026. qlcnic_ind_wr(adapter, ms.hi, 0);
  1027. qlcnic_ind_wr(adapter, ms.control, TA_CTL_ENABLE);
  1028. qlcnic_ind_wr(adapter, ms.control, QLCNIC_TA_START_ENABLE);
  1029. for (j = 0; j < MAX_CTL_CHECK; j++) {
  1030. temp = qlcnic_ind_rd(adapter, ms.control);
  1031. if ((temp & TA_CTL_BUSY) == 0)
  1032. break;
  1033. }
  1034. if (j >= MAX_CTL_CHECK) {
  1035. if (printk_ratelimit())
  1036. dev_err(&adapter->pdev->dev,
  1037. "failed to read through agent\n");
  1038. ret = -EIO;
  1039. } else {
  1040. temp = qlcnic_ind_rd(adapter, ms.rd[3]);
  1041. val = (u64)temp << 32;
  1042. val |= qlcnic_ind_rd(adapter, ms.rd[2]);
  1043. *data = val;
  1044. ret = 0;
  1045. }
  1046. mutex_unlock(&adapter->ahw->mem_lock);
  1047. return ret;
  1048. }
  1049. int qlcnic_get_board_info(struct qlcnic_adapter *adapter)
  1050. {
  1051. int offset, board_type, magic;
  1052. struct pci_dev *pdev = adapter->pdev;
  1053. offset = QLCNIC_FW_MAGIC_OFFSET;
  1054. if (qlcnic_rom_fast_read(adapter, offset, &magic))
  1055. return -EIO;
  1056. if (magic != QLCNIC_BDINFO_MAGIC) {
  1057. dev_err(&pdev->dev, "invalid board config, magic=%08x\n",
  1058. magic);
  1059. return -EIO;
  1060. }
  1061. offset = QLCNIC_BRDTYPE_OFFSET;
  1062. if (qlcnic_rom_fast_read(adapter, offset, &board_type))
  1063. return -EIO;
  1064. adapter->ahw->board_type = board_type;
  1065. if (board_type == QLCNIC_BRDTYPE_P3P_4_GB_MM) {
  1066. u32 gpio = QLCRD32(adapter, QLCNIC_ROMUSB_GLB_PAD_GPIO_I);
  1067. if ((gpio & 0x8000) == 0)
  1068. board_type = QLCNIC_BRDTYPE_P3P_10G_TP;
  1069. }
  1070. switch (board_type) {
  1071. case QLCNIC_BRDTYPE_P3P_HMEZ:
  1072. case QLCNIC_BRDTYPE_P3P_XG_LOM:
  1073. case QLCNIC_BRDTYPE_P3P_10G_CX4:
  1074. case QLCNIC_BRDTYPE_P3P_10G_CX4_LP:
  1075. case QLCNIC_BRDTYPE_P3P_IMEZ:
  1076. case QLCNIC_BRDTYPE_P3P_10G_SFP_PLUS:
  1077. case QLCNIC_BRDTYPE_P3P_10G_SFP_CT:
  1078. case QLCNIC_BRDTYPE_P3P_10G_SFP_QT:
  1079. case QLCNIC_BRDTYPE_P3P_10G_XFP:
  1080. case QLCNIC_BRDTYPE_P3P_10000_BASE_T:
  1081. adapter->ahw->port_type = QLCNIC_XGBE;
  1082. break;
  1083. case QLCNIC_BRDTYPE_P3P_REF_QG:
  1084. case QLCNIC_BRDTYPE_P3P_4_GB:
  1085. case QLCNIC_BRDTYPE_P3P_4_GB_MM:
  1086. adapter->ahw->port_type = QLCNIC_GBE;
  1087. break;
  1088. case QLCNIC_BRDTYPE_P3P_10G_TP:
  1089. adapter->ahw->port_type = (adapter->portnum < 2) ?
  1090. QLCNIC_XGBE : QLCNIC_GBE;
  1091. break;
  1092. default:
  1093. dev_err(&pdev->dev, "unknown board type %x\n", board_type);
  1094. adapter->ahw->port_type = QLCNIC_XGBE;
  1095. break;
  1096. }
  1097. return 0;
  1098. }
  1099. int
  1100. qlcnic_wol_supported(struct qlcnic_adapter *adapter)
  1101. {
  1102. u32 wol_cfg;
  1103. wol_cfg = QLCRD32(adapter, QLCNIC_WOL_CONFIG_NV);
  1104. if (wol_cfg & (1UL << adapter->portnum)) {
  1105. wol_cfg = QLCRD32(adapter, QLCNIC_WOL_CONFIG);
  1106. if (wol_cfg & (1 << adapter->portnum))
  1107. return 1;
  1108. }
  1109. return 0;
  1110. }
  1111. int qlcnic_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
  1112. {
  1113. struct qlcnic_nic_req req;
  1114. int rv;
  1115. u64 word;
  1116. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  1117. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  1118. word = QLCNIC_H2C_OPCODE_CONFIG_LED | ((u64)adapter->portnum << 16);
  1119. req.req_hdr = cpu_to_le64(word);
  1120. req.words[0] = cpu_to_le64((u64)rate << 32);
  1121. req.words[1] = cpu_to_le64(state);
  1122. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  1123. if (rv)
  1124. dev_err(&adapter->pdev->dev, "LED configuration failed.\n");
  1125. return rv;
  1126. }