qlcnic_hw.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365
  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, consumer;
  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. consumer = tx_ring->sw_consumer;
  353. if (nr_desc >= qlcnic_tx_avail(tx_ring)) {
  354. netif_tx_stop_queue(tx_ring->txq);
  355. smp_mb();
  356. if (qlcnic_tx_avail(tx_ring) > nr_desc) {
  357. if (qlcnic_tx_avail(tx_ring) > TX_STOP_THRESH)
  358. netif_tx_wake_queue(tx_ring->txq);
  359. } else {
  360. adapter->stats.xmit_off++;
  361. __netif_tx_unlock_bh(tx_ring->txq);
  362. return -EBUSY;
  363. }
  364. }
  365. do {
  366. cmd_desc = &cmd_desc_arr[i];
  367. pbuf = &tx_ring->cmd_buf_arr[producer];
  368. pbuf->skb = NULL;
  369. pbuf->frag_count = 0;
  370. memcpy(&tx_ring->desc_head[producer],
  371. &cmd_desc_arr[i], sizeof(struct cmd_desc_type0));
  372. producer = get_next_index(producer, tx_ring->num_desc);
  373. i++;
  374. } while (i != nr_desc);
  375. tx_ring->producer = producer;
  376. qlcnic_update_cmd_producer(tx_ring);
  377. __netif_tx_unlock_bh(tx_ring->txq);
  378. return 0;
  379. }
  380. static int
  381. qlcnic_sre_macaddr_change(struct qlcnic_adapter *adapter, u8 *addr,
  382. __le16 vlan_id, unsigned op)
  383. {
  384. struct qlcnic_nic_req req;
  385. struct qlcnic_mac_req *mac_req;
  386. struct qlcnic_vlan_req *vlan_req;
  387. u64 word;
  388. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  389. req.qhdr = cpu_to_le64(QLCNIC_REQUEST << 23);
  390. word = QLCNIC_MAC_EVENT | ((u64)adapter->portnum << 16);
  391. req.req_hdr = cpu_to_le64(word);
  392. mac_req = (struct qlcnic_mac_req *)&req.words[0];
  393. mac_req->op = op;
  394. memcpy(mac_req->mac_addr, addr, 6);
  395. vlan_req = (struct qlcnic_vlan_req *)&req.words[1];
  396. vlan_req->vlan_id = vlan_id;
  397. return qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  398. }
  399. static int qlcnic_nic_add_mac(struct qlcnic_adapter *adapter, const u8 *addr)
  400. {
  401. struct list_head *head;
  402. struct qlcnic_mac_list_s *cur;
  403. /* look up if already exists */
  404. list_for_each(head, &adapter->mac_list) {
  405. cur = list_entry(head, struct qlcnic_mac_list_s, list);
  406. if (memcmp(addr, cur->mac_addr, ETH_ALEN) == 0)
  407. return 0;
  408. }
  409. cur = kzalloc(sizeof(struct qlcnic_mac_list_s), GFP_ATOMIC);
  410. if (cur == NULL) {
  411. dev_err(&adapter->netdev->dev,
  412. "failed to add mac address filter\n");
  413. return -ENOMEM;
  414. }
  415. memcpy(cur->mac_addr, addr, ETH_ALEN);
  416. if (qlcnic_sre_macaddr_change(adapter,
  417. cur->mac_addr, 0, QLCNIC_MAC_ADD)) {
  418. kfree(cur);
  419. return -EIO;
  420. }
  421. list_add_tail(&cur->list, &adapter->mac_list);
  422. return 0;
  423. }
  424. void qlcnic_set_multi(struct net_device *netdev)
  425. {
  426. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  427. struct netdev_hw_addr *ha;
  428. static const u8 bcast_addr[ETH_ALEN] = {
  429. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
  430. };
  431. u32 mode = VPORT_MISS_MODE_DROP;
  432. if (!test_bit(__QLCNIC_FW_ATTACHED, &adapter->state))
  433. return;
  434. qlcnic_nic_add_mac(adapter, adapter->mac_addr);
  435. qlcnic_nic_add_mac(adapter, bcast_addr);
  436. if (netdev->flags & IFF_PROMISC) {
  437. if (!(adapter->flags & QLCNIC_PROMISC_DISABLED))
  438. mode = VPORT_MISS_MODE_ACCEPT_ALL;
  439. goto send_fw_cmd;
  440. }
  441. if ((netdev->flags & IFF_ALLMULTI) ||
  442. (netdev_mc_count(netdev) > adapter->ahw->max_mc_count)) {
  443. mode = VPORT_MISS_MODE_ACCEPT_MULTI;
  444. goto send_fw_cmd;
  445. }
  446. if (!netdev_mc_empty(netdev)) {
  447. netdev_for_each_mc_addr(ha, netdev) {
  448. qlcnic_nic_add_mac(adapter, ha->addr);
  449. }
  450. }
  451. send_fw_cmd:
  452. if (mode == VPORT_MISS_MODE_ACCEPT_ALL) {
  453. qlcnic_alloc_lb_filters_mem(adapter);
  454. adapter->mac_learn = 1;
  455. } else {
  456. adapter->mac_learn = 0;
  457. }
  458. qlcnic_nic_set_promisc(adapter, mode);
  459. }
  460. int qlcnic_nic_set_promisc(struct qlcnic_adapter *adapter, u32 mode)
  461. {
  462. struct qlcnic_nic_req req;
  463. u64 word;
  464. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  465. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  466. word = QLCNIC_H2C_OPCODE_SET_MAC_RECEIVE_MODE |
  467. ((u64)adapter->portnum << 16);
  468. req.req_hdr = cpu_to_le64(word);
  469. req.words[0] = cpu_to_le64(mode);
  470. return qlcnic_send_cmd_descs(adapter,
  471. (struct cmd_desc_type0 *)&req, 1);
  472. }
  473. void qlcnic_free_mac_list(struct qlcnic_adapter *adapter)
  474. {
  475. struct qlcnic_mac_list_s *cur;
  476. struct list_head *head = &adapter->mac_list;
  477. while (!list_empty(head)) {
  478. cur = list_entry(head->next, struct qlcnic_mac_list_s, list);
  479. qlcnic_sre_macaddr_change(adapter,
  480. cur->mac_addr, 0, QLCNIC_MAC_DEL);
  481. list_del(&cur->list);
  482. kfree(cur);
  483. }
  484. }
  485. void qlcnic_prune_lb_filters(struct qlcnic_adapter *adapter)
  486. {
  487. struct qlcnic_filter *tmp_fil;
  488. struct hlist_node *tmp_hnode, *n;
  489. struct hlist_head *head;
  490. int i;
  491. for (i = 0; i < adapter->fhash.fmax; i++) {
  492. head = &(adapter->fhash.fhead[i]);
  493. hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode)
  494. {
  495. if (jiffies >
  496. (QLCNIC_FILTER_AGE * HZ + tmp_fil->ftime)) {
  497. qlcnic_sre_macaddr_change(adapter,
  498. tmp_fil->faddr, tmp_fil->vlan_id,
  499. tmp_fil->vlan_id ? QLCNIC_MAC_VLAN_DEL :
  500. QLCNIC_MAC_DEL);
  501. spin_lock_bh(&adapter->mac_learn_lock);
  502. adapter->fhash.fnum--;
  503. hlist_del(&tmp_fil->fnode);
  504. spin_unlock_bh(&adapter->mac_learn_lock);
  505. kfree(tmp_fil);
  506. }
  507. }
  508. }
  509. }
  510. void qlcnic_delete_lb_filters(struct qlcnic_adapter *adapter)
  511. {
  512. struct qlcnic_filter *tmp_fil;
  513. struct hlist_node *tmp_hnode, *n;
  514. struct hlist_head *head;
  515. int i;
  516. for (i = 0; i < adapter->fhash.fmax; i++) {
  517. head = &(adapter->fhash.fhead[i]);
  518. hlist_for_each_entry_safe(tmp_fil, tmp_hnode, n, head, fnode) {
  519. qlcnic_sre_macaddr_change(adapter, tmp_fil->faddr,
  520. tmp_fil->vlan_id, tmp_fil->vlan_id ?
  521. QLCNIC_MAC_VLAN_DEL : QLCNIC_MAC_DEL);
  522. spin_lock_bh(&adapter->mac_learn_lock);
  523. adapter->fhash.fnum--;
  524. hlist_del(&tmp_fil->fnode);
  525. spin_unlock_bh(&adapter->mac_learn_lock);
  526. kfree(tmp_fil);
  527. }
  528. }
  529. }
  530. static int qlcnic_set_fw_loopback(struct qlcnic_adapter *adapter, u8 flag)
  531. {
  532. struct qlcnic_nic_req req;
  533. int rv;
  534. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  535. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  536. req.req_hdr = cpu_to_le64(QLCNIC_H2C_OPCODE_CONFIG_LOOPBACK |
  537. ((u64) adapter->portnum << 16) | ((u64) 0x1 << 32));
  538. req.words[0] = cpu_to_le64(flag);
  539. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  540. if (rv != 0)
  541. dev_err(&adapter->pdev->dev, "%sting loopback mode failed\n",
  542. flag ? "Set" : "Reset");
  543. return rv;
  544. }
  545. int qlcnic_set_lb_mode(struct qlcnic_adapter *adapter, u8 mode)
  546. {
  547. if (qlcnic_set_fw_loopback(adapter, mode))
  548. return -EIO;
  549. if (qlcnic_nic_set_promisc(adapter, VPORT_MISS_MODE_ACCEPT_ALL)) {
  550. qlcnic_set_fw_loopback(adapter, 0);
  551. return -EIO;
  552. }
  553. msleep(1000);
  554. return 0;
  555. }
  556. void qlcnic_clear_lb_mode(struct qlcnic_adapter *adapter)
  557. {
  558. int mode = VPORT_MISS_MODE_DROP;
  559. struct net_device *netdev = adapter->netdev;
  560. qlcnic_set_fw_loopback(adapter, 0);
  561. if (netdev->flags & IFF_PROMISC)
  562. mode = VPORT_MISS_MODE_ACCEPT_ALL;
  563. else if (netdev->flags & IFF_ALLMULTI)
  564. mode = VPORT_MISS_MODE_ACCEPT_MULTI;
  565. qlcnic_nic_set_promisc(adapter, mode);
  566. msleep(1000);
  567. }
  568. /*
  569. * Send the interrupt coalescing parameter set by ethtool to the card.
  570. */
  571. int qlcnic_config_intr_coalesce(struct qlcnic_adapter *adapter)
  572. {
  573. struct qlcnic_nic_req req;
  574. int rv;
  575. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  576. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  577. req.req_hdr = cpu_to_le64(QLCNIC_CONFIG_INTR_COALESCE |
  578. ((u64) adapter->portnum << 16));
  579. req.words[0] = cpu_to_le64(((u64) adapter->ahw->coal.flag) << 32);
  580. req.words[2] = cpu_to_le64(adapter->ahw->coal.rx_packets |
  581. ((u64) adapter->ahw->coal.rx_time_us) << 16);
  582. req.words[5] = cpu_to_le64(adapter->ahw->coal.timer_out |
  583. ((u64) adapter->ahw->coal.type) << 32 |
  584. ((u64) adapter->ahw->coal.sts_ring_mask) << 40);
  585. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  586. if (rv != 0)
  587. dev_err(&adapter->netdev->dev,
  588. "Could not send interrupt coalescing parameters\n");
  589. return rv;
  590. }
  591. int qlcnic_config_hw_lro(struct qlcnic_adapter *adapter, int enable)
  592. {
  593. struct qlcnic_nic_req req;
  594. u64 word;
  595. int rv;
  596. if (!test_bit(__QLCNIC_FW_ATTACHED, &adapter->state))
  597. return 0;
  598. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  599. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  600. word = QLCNIC_H2C_OPCODE_CONFIG_HW_LRO | ((u64)adapter->portnum << 16);
  601. req.req_hdr = cpu_to_le64(word);
  602. req.words[0] = cpu_to_le64(enable);
  603. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  604. if (rv != 0)
  605. dev_err(&adapter->netdev->dev,
  606. "Could not send configure hw lro request\n");
  607. return rv;
  608. }
  609. int qlcnic_config_bridged_mode(struct qlcnic_adapter *adapter, u32 enable)
  610. {
  611. struct qlcnic_nic_req req;
  612. u64 word;
  613. int rv;
  614. if (!!(adapter->flags & QLCNIC_BRIDGE_ENABLED) == enable)
  615. return 0;
  616. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  617. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  618. word = QLCNIC_H2C_OPCODE_CONFIG_BRIDGING |
  619. ((u64)adapter->portnum << 16);
  620. req.req_hdr = cpu_to_le64(word);
  621. req.words[0] = cpu_to_le64(enable);
  622. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  623. if (rv != 0)
  624. dev_err(&adapter->netdev->dev,
  625. "Could not send configure bridge mode request\n");
  626. adapter->flags ^= QLCNIC_BRIDGE_ENABLED;
  627. return rv;
  628. }
  629. #define RSS_HASHTYPE_IP_TCP 0x3
  630. int qlcnic_config_rss(struct qlcnic_adapter *adapter, int enable)
  631. {
  632. struct qlcnic_nic_req req;
  633. u64 word;
  634. int i, rv;
  635. static const u64 key[] = {
  636. 0xbeac01fa6a42b73bULL, 0x8030f20c77cb2da3ULL,
  637. 0xae7b30b4d0ca2bcbULL, 0x43a38fb04167253dULL,
  638. 0x255b0ec26d5a56daULL
  639. };
  640. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  641. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  642. word = QLCNIC_H2C_OPCODE_CONFIG_RSS | ((u64)adapter->portnum << 16);
  643. req.req_hdr = cpu_to_le64(word);
  644. /*
  645. * RSS request:
  646. * bits 3-0: hash_method
  647. * 5-4: hash_type_ipv4
  648. * 7-6: hash_type_ipv6
  649. * 8: enable
  650. * 9: use indirection table
  651. * 47-10: reserved
  652. * 63-48: indirection table mask
  653. */
  654. word = ((u64)(RSS_HASHTYPE_IP_TCP & 0x3) << 4) |
  655. ((u64)(RSS_HASHTYPE_IP_TCP & 0x3) << 6) |
  656. ((u64)(enable & 0x1) << 8) |
  657. ((0x7ULL) << 48);
  658. req.words[0] = cpu_to_le64(word);
  659. for (i = 0; i < 5; i++)
  660. req.words[i+1] = cpu_to_le64(key[i]);
  661. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  662. if (rv != 0)
  663. dev_err(&adapter->netdev->dev, "could not configure RSS\n");
  664. return rv;
  665. }
  666. int qlcnic_config_ipaddr(struct qlcnic_adapter *adapter, __be32 ip, int cmd)
  667. {
  668. struct qlcnic_nic_req req;
  669. struct qlcnic_ipaddr *ipa;
  670. u64 word;
  671. int rv;
  672. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  673. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  674. word = QLCNIC_H2C_OPCODE_CONFIG_IPADDR | ((u64)adapter->portnum << 16);
  675. req.req_hdr = cpu_to_le64(word);
  676. req.words[0] = cpu_to_le64(cmd);
  677. ipa = (struct qlcnic_ipaddr *)&req.words[1];
  678. ipa->ipv4 = ip;
  679. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  680. if (rv != 0)
  681. dev_err(&adapter->netdev->dev,
  682. "could not notify %s IP 0x%x reuqest\n",
  683. (cmd == QLCNIC_IP_UP) ? "Add" : "Remove", ip);
  684. return rv;
  685. }
  686. int qlcnic_linkevent_request(struct qlcnic_adapter *adapter, int enable)
  687. {
  688. struct qlcnic_nic_req req;
  689. u64 word;
  690. int rv;
  691. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  692. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  693. word = QLCNIC_H2C_OPCODE_GET_LINKEVENT | ((u64)adapter->portnum << 16);
  694. req.req_hdr = cpu_to_le64(word);
  695. req.words[0] = cpu_to_le64(enable | (enable << 8));
  696. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  697. if (rv != 0)
  698. dev_err(&adapter->netdev->dev,
  699. "could not configure link notification\n");
  700. return rv;
  701. }
  702. int qlcnic_send_lro_cleanup(struct qlcnic_adapter *adapter)
  703. {
  704. struct qlcnic_nic_req req;
  705. u64 word;
  706. int rv;
  707. if (!test_bit(__QLCNIC_FW_ATTACHED, &adapter->state))
  708. return 0;
  709. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  710. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  711. word = QLCNIC_H2C_OPCODE_LRO_REQUEST |
  712. ((u64)adapter->portnum << 16) |
  713. ((u64)QLCNIC_LRO_REQUEST_CLEANUP << 56) ;
  714. req.req_hdr = cpu_to_le64(word);
  715. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  716. if (rv != 0)
  717. dev_err(&adapter->netdev->dev,
  718. "could not cleanup lro flows\n");
  719. return rv;
  720. }
  721. /*
  722. * qlcnic_change_mtu - Change the Maximum Transfer Unit
  723. * @returns 0 on success, negative on failure
  724. */
  725. int qlcnic_change_mtu(struct net_device *netdev, int mtu)
  726. {
  727. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  728. int rc = 0;
  729. if (mtu < P3P_MIN_MTU || mtu > P3P_MAX_MTU) {
  730. dev_err(&adapter->netdev->dev, "%d bytes < mtu < %d bytes"
  731. " not supported\n", P3P_MAX_MTU, P3P_MIN_MTU);
  732. return -EINVAL;
  733. }
  734. rc = qlcnic_fw_cmd_set_mtu(adapter, mtu);
  735. if (!rc)
  736. netdev->mtu = mtu;
  737. return rc;
  738. }
  739. netdev_features_t qlcnic_fix_features(struct net_device *netdev,
  740. netdev_features_t features)
  741. {
  742. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  743. if ((adapter->flags & QLCNIC_ESWITCH_ENABLED)) {
  744. netdev_features_t changed = features ^ netdev->features;
  745. features ^= changed & (NETIF_F_ALL_CSUM | NETIF_F_RXCSUM);
  746. }
  747. if (!(features & NETIF_F_RXCSUM))
  748. features &= ~NETIF_F_LRO;
  749. return features;
  750. }
  751. int qlcnic_set_features(struct net_device *netdev, netdev_features_t features)
  752. {
  753. struct qlcnic_adapter *adapter = netdev_priv(netdev);
  754. netdev_features_t changed = netdev->features ^ features;
  755. int hw_lro = (features & NETIF_F_LRO) ? QLCNIC_LRO_ENABLED : 0;
  756. if (!(changed & NETIF_F_LRO))
  757. return 0;
  758. netdev->features = features ^ NETIF_F_LRO;
  759. if (qlcnic_config_hw_lro(adapter, hw_lro))
  760. return -EIO;
  761. if ((hw_lro == 0) && qlcnic_send_lro_cleanup(adapter))
  762. return -EIO;
  763. return 0;
  764. }
  765. /*
  766. * Changes the CRB window to the specified window.
  767. */
  768. /* Returns < 0 if off is not valid,
  769. * 1 if window access is needed. 'off' is set to offset from
  770. * CRB space in 128M pci map
  771. * 0 if no window access is needed. 'off' is set to 2M addr
  772. * In: 'off' is offset from base in 128M pci map
  773. */
  774. static int qlcnic_pci_get_crb_addr_2M(struct qlcnic_hardware_context *ahw,
  775. ulong off, void __iomem **addr)
  776. {
  777. const struct crb_128M_2M_sub_block_map *m;
  778. if ((off >= QLCNIC_CRB_MAX) || (off < QLCNIC_PCI_CRBSPACE))
  779. return -EINVAL;
  780. off -= QLCNIC_PCI_CRBSPACE;
  781. /*
  782. * Try direct map
  783. */
  784. m = &crb_128M_2M_map[CRB_BLK(off)].sub_block[CRB_SUBBLK(off)];
  785. if (m->valid && (m->start_128M <= off) && (m->end_128M > off)) {
  786. *addr = ahw->pci_base0 + m->start_2M +
  787. (off - m->start_128M);
  788. return 0;
  789. }
  790. /*
  791. * Not in direct map, use crb window
  792. */
  793. *addr = ahw->pci_base0 + CRB_INDIRECT_2M + (off & MASK(16));
  794. return 1;
  795. }
  796. /*
  797. * In: 'off' is offset from CRB space in 128M pci map
  798. * Out: 'off' is 2M pci map addr
  799. * side effect: lock crb window
  800. */
  801. static int
  802. qlcnic_pci_set_crbwindow_2M(struct qlcnic_adapter *adapter, ulong off)
  803. {
  804. u32 window;
  805. void __iomem *addr = adapter->ahw->pci_base0 + CRB_WINDOW_2M;
  806. off -= QLCNIC_PCI_CRBSPACE;
  807. window = CRB_HI(off);
  808. if (window == 0) {
  809. dev_err(&adapter->pdev->dev, "Invalid offset 0x%lx\n", off);
  810. return -EIO;
  811. }
  812. writel(window, addr);
  813. if (readl(addr) != window) {
  814. if (printk_ratelimit())
  815. dev_warn(&adapter->pdev->dev,
  816. "failed to set CRB window to %d off 0x%lx\n",
  817. window, off);
  818. return -EIO;
  819. }
  820. return 0;
  821. }
  822. int
  823. qlcnic_hw_write_wx_2M(struct qlcnic_adapter *adapter, ulong off, u32 data)
  824. {
  825. unsigned long flags;
  826. int rv;
  827. void __iomem *addr = NULL;
  828. rv = qlcnic_pci_get_crb_addr_2M(adapter->ahw, off, &addr);
  829. if (rv == 0) {
  830. writel(data, addr);
  831. return 0;
  832. }
  833. if (rv > 0) {
  834. /* indirect access */
  835. write_lock_irqsave(&adapter->ahw->crb_lock, flags);
  836. crb_win_lock(adapter);
  837. rv = qlcnic_pci_set_crbwindow_2M(adapter, off);
  838. if (!rv)
  839. writel(data, addr);
  840. crb_win_unlock(adapter);
  841. write_unlock_irqrestore(&adapter->ahw->crb_lock, flags);
  842. return rv;
  843. }
  844. dev_err(&adapter->pdev->dev,
  845. "%s: invalid offset: 0x%016lx\n", __func__, off);
  846. dump_stack();
  847. return -EIO;
  848. }
  849. int qlcnic_hw_read_wx_2M(struct qlcnic_adapter *adapter, ulong off)
  850. {
  851. unsigned long flags;
  852. int rv;
  853. u32 data = -1;
  854. void __iomem *addr = NULL;
  855. rv = qlcnic_pci_get_crb_addr_2M(adapter->ahw, off, &addr);
  856. if (rv == 0)
  857. return readl(addr);
  858. if (rv > 0) {
  859. /* indirect access */
  860. write_lock_irqsave(&adapter->ahw->crb_lock, flags);
  861. crb_win_lock(adapter);
  862. if (!qlcnic_pci_set_crbwindow_2M(adapter, off))
  863. data = readl(addr);
  864. crb_win_unlock(adapter);
  865. write_unlock_irqrestore(&adapter->ahw->crb_lock, flags);
  866. return data;
  867. }
  868. dev_err(&adapter->pdev->dev,
  869. "%s: invalid offset: 0x%016lx\n", __func__, off);
  870. dump_stack();
  871. return -1;
  872. }
  873. void __iomem *qlcnic_get_ioaddr(struct qlcnic_hardware_context *ahw,
  874. u32 offset)
  875. {
  876. void __iomem *addr = NULL;
  877. WARN_ON(qlcnic_pci_get_crb_addr_2M(ahw, offset, &addr));
  878. return addr;
  879. }
  880. static int qlcnic_pci_mem_access_direct(struct qlcnic_adapter *adapter,
  881. u32 window, u64 off, u64 *data, int op)
  882. {
  883. void __iomem *addr;
  884. u32 start;
  885. mutex_lock(&adapter->ahw->mem_lock);
  886. writel(window, adapter->ahw->ocm_win_crb);
  887. /* read back to flush */
  888. readl(adapter->ahw->ocm_win_crb);
  889. start = QLCNIC_PCI_OCM0_2M + off;
  890. addr = adapter->ahw->pci_base0 + start;
  891. if (op == 0) /* read */
  892. *data = readq(addr);
  893. else /* write */
  894. writeq(*data, addr);
  895. /* Set window to 0 */
  896. writel(0, adapter->ahw->ocm_win_crb);
  897. readl(adapter->ahw->ocm_win_crb);
  898. mutex_unlock(&adapter->ahw->mem_lock);
  899. return 0;
  900. }
  901. void
  902. qlcnic_pci_camqm_read_2M(struct qlcnic_adapter *adapter, u64 off, u64 *data)
  903. {
  904. void __iomem *addr = adapter->ahw->pci_base0 +
  905. QLCNIC_PCI_CAMQM_2M_BASE + (off - QLCNIC_PCI_CAMQM);
  906. mutex_lock(&adapter->ahw->mem_lock);
  907. *data = readq(addr);
  908. mutex_unlock(&adapter->ahw->mem_lock);
  909. }
  910. void
  911. qlcnic_pci_camqm_write_2M(struct qlcnic_adapter *adapter, u64 off, u64 data)
  912. {
  913. void __iomem *addr = adapter->ahw->pci_base0 +
  914. QLCNIC_PCI_CAMQM_2M_BASE + (off - QLCNIC_PCI_CAMQM);
  915. mutex_lock(&adapter->ahw->mem_lock);
  916. writeq(data, addr);
  917. mutex_unlock(&adapter->ahw->mem_lock);
  918. }
  919. /* Set MS memory control data for different adapters */
  920. static void qlcnic_set_ms_controls(struct qlcnic_adapter *adapter, u64 off,
  921. struct qlcnic_ms_reg_ctrl *ms)
  922. {
  923. ms->control = QLCNIC_MS_CTRL;
  924. ms->low = QLCNIC_MS_ADDR_LO;
  925. ms->hi = QLCNIC_MS_ADDR_HI;
  926. if (off & 0xf) {
  927. ms->wd[0] = QLCNIC_MS_WRTDATA_LO;
  928. ms->rd[0] = QLCNIC_MS_RDDATA_LO;
  929. ms->wd[1] = QLCNIC_MS_WRTDATA_HI;
  930. ms->rd[1] = QLCNIC_MS_RDDATA_HI;
  931. ms->wd[2] = QLCNIC_MS_WRTDATA_ULO;
  932. ms->wd[3] = QLCNIC_MS_WRTDATA_UHI;
  933. ms->rd[2] = QLCNIC_MS_RDDATA_ULO;
  934. ms->rd[3] = QLCNIC_MS_RDDATA_UHI;
  935. } else {
  936. ms->wd[0] = QLCNIC_MS_WRTDATA_ULO;
  937. ms->rd[0] = QLCNIC_MS_RDDATA_ULO;
  938. ms->wd[1] = QLCNIC_MS_WRTDATA_UHI;
  939. ms->rd[1] = QLCNIC_MS_RDDATA_UHI;
  940. ms->wd[2] = QLCNIC_MS_WRTDATA_LO;
  941. ms->wd[3] = QLCNIC_MS_WRTDATA_HI;
  942. ms->rd[2] = QLCNIC_MS_RDDATA_LO;
  943. ms->rd[3] = QLCNIC_MS_RDDATA_HI;
  944. }
  945. ms->ocm_window = OCM_WIN_P3P(off);
  946. ms->off = GET_MEM_OFFS_2M(off);
  947. }
  948. int qlcnic_pci_mem_write_2M(struct qlcnic_adapter *adapter, u64 off, u64 data)
  949. {
  950. int j, ret = 0;
  951. u32 temp, off8;
  952. struct qlcnic_ms_reg_ctrl ms;
  953. /* Only 64-bit aligned access */
  954. if (off & 7)
  955. return -EIO;
  956. memset(&ms, 0, sizeof(struct qlcnic_ms_reg_ctrl));
  957. if (!(ADDR_IN_RANGE(off, QLCNIC_ADDR_QDR_NET,
  958. QLCNIC_ADDR_QDR_NET_MAX) ||
  959. ADDR_IN_RANGE(off, QLCNIC_ADDR_DDR_NET,
  960. QLCNIC_ADDR_DDR_NET_MAX)))
  961. return -EIO;
  962. qlcnic_set_ms_controls(adapter, off, &ms);
  963. if (ADDR_IN_RANGE(off, QLCNIC_ADDR_OCM0, QLCNIC_ADDR_OCM0_MAX))
  964. return qlcnic_pci_mem_access_direct(adapter, ms.ocm_window,
  965. ms.off, &data, 1);
  966. off8 = off & ~0xf;
  967. mutex_lock(&adapter->ahw->mem_lock);
  968. qlcnic_ind_wr(adapter, ms.low, off8);
  969. qlcnic_ind_wr(adapter, ms.hi, 0);
  970. qlcnic_ind_wr(adapter, ms.control, TA_CTL_ENABLE);
  971. qlcnic_ind_wr(adapter, ms.control, QLCNIC_TA_START_ENABLE);
  972. for (j = 0; j < MAX_CTL_CHECK; j++) {
  973. temp = qlcnic_ind_rd(adapter, ms.control);
  974. if ((temp & TA_CTL_BUSY) == 0)
  975. break;
  976. }
  977. if (j >= MAX_CTL_CHECK) {
  978. ret = -EIO;
  979. goto done;
  980. }
  981. /* This is the modify part of read-modify-write */
  982. qlcnic_ind_wr(adapter, ms.wd[0], qlcnic_ind_rd(adapter, ms.rd[0]));
  983. qlcnic_ind_wr(adapter, ms.wd[1], qlcnic_ind_rd(adapter, ms.rd[1]));
  984. /* This is the write part of read-modify-write */
  985. qlcnic_ind_wr(adapter, ms.wd[2], data & 0xffffffff);
  986. qlcnic_ind_wr(adapter, ms.wd[3], (data >> 32) & 0xffffffff);
  987. qlcnic_ind_wr(adapter, ms.control, QLCNIC_TA_WRITE_ENABLE);
  988. qlcnic_ind_wr(adapter, ms.control, QLCNIC_TA_WRITE_START);
  989. for (j = 0; j < MAX_CTL_CHECK; j++) {
  990. temp = qlcnic_ind_rd(adapter, ms.control);
  991. if ((temp & TA_CTL_BUSY) == 0)
  992. break;
  993. }
  994. if (j >= MAX_CTL_CHECK) {
  995. if (printk_ratelimit())
  996. dev_err(&adapter->pdev->dev,
  997. "failed to write through agent\n");
  998. ret = -EIO;
  999. } else
  1000. ret = 0;
  1001. done:
  1002. mutex_unlock(&adapter->ahw->mem_lock);
  1003. return ret;
  1004. }
  1005. int qlcnic_pci_mem_read_2M(struct qlcnic_adapter *adapter, u64 off, u64 *data)
  1006. {
  1007. int j, ret;
  1008. u32 temp, off8;
  1009. u64 val;
  1010. struct qlcnic_ms_reg_ctrl ms;
  1011. /* Only 64-bit aligned access */
  1012. if (off & 7)
  1013. return -EIO;
  1014. if (!(ADDR_IN_RANGE(off, QLCNIC_ADDR_QDR_NET,
  1015. QLCNIC_ADDR_QDR_NET_MAX) ||
  1016. ADDR_IN_RANGE(off, QLCNIC_ADDR_DDR_NET,
  1017. QLCNIC_ADDR_DDR_NET_MAX)))
  1018. return -EIO;
  1019. memset(&ms, 0, sizeof(struct qlcnic_ms_reg_ctrl));
  1020. qlcnic_set_ms_controls(adapter, off, &ms);
  1021. if (ADDR_IN_RANGE(off, QLCNIC_ADDR_OCM0, QLCNIC_ADDR_OCM0_MAX))
  1022. return qlcnic_pci_mem_access_direct(adapter, ms.ocm_window,
  1023. ms.off, data, 0);
  1024. mutex_lock(&adapter->ahw->mem_lock);
  1025. off8 = off & ~0xf;
  1026. qlcnic_ind_wr(adapter, ms.low, off8);
  1027. qlcnic_ind_wr(adapter, ms.hi, 0);
  1028. qlcnic_ind_wr(adapter, ms.control, TA_CTL_ENABLE);
  1029. qlcnic_ind_wr(adapter, ms.control, QLCNIC_TA_START_ENABLE);
  1030. for (j = 0; j < MAX_CTL_CHECK; j++) {
  1031. temp = qlcnic_ind_rd(adapter, ms.control);
  1032. if ((temp & TA_CTL_BUSY) == 0)
  1033. break;
  1034. }
  1035. if (j >= MAX_CTL_CHECK) {
  1036. if (printk_ratelimit())
  1037. dev_err(&adapter->pdev->dev,
  1038. "failed to read through agent\n");
  1039. ret = -EIO;
  1040. } else {
  1041. temp = qlcnic_ind_rd(adapter, ms.rd[3]);
  1042. val = (u64)temp << 32;
  1043. val |= qlcnic_ind_rd(adapter, ms.rd[2]);
  1044. *data = val;
  1045. ret = 0;
  1046. }
  1047. mutex_unlock(&adapter->ahw->mem_lock);
  1048. return ret;
  1049. }
  1050. int qlcnic_get_board_info(struct qlcnic_adapter *adapter)
  1051. {
  1052. int offset, board_type, magic;
  1053. struct pci_dev *pdev = adapter->pdev;
  1054. offset = QLCNIC_FW_MAGIC_OFFSET;
  1055. if (qlcnic_rom_fast_read(adapter, offset, &magic))
  1056. return -EIO;
  1057. if (magic != QLCNIC_BDINFO_MAGIC) {
  1058. dev_err(&pdev->dev, "invalid board config, magic=%08x\n",
  1059. magic);
  1060. return -EIO;
  1061. }
  1062. offset = QLCNIC_BRDTYPE_OFFSET;
  1063. if (qlcnic_rom_fast_read(adapter, offset, &board_type))
  1064. return -EIO;
  1065. adapter->ahw->board_type = board_type;
  1066. if (board_type == QLCNIC_BRDTYPE_P3P_4_GB_MM) {
  1067. u32 gpio = QLCRD32(adapter, QLCNIC_ROMUSB_GLB_PAD_GPIO_I);
  1068. if ((gpio & 0x8000) == 0)
  1069. board_type = QLCNIC_BRDTYPE_P3P_10G_TP;
  1070. }
  1071. switch (board_type) {
  1072. case QLCNIC_BRDTYPE_P3P_HMEZ:
  1073. case QLCNIC_BRDTYPE_P3P_XG_LOM:
  1074. case QLCNIC_BRDTYPE_P3P_10G_CX4:
  1075. case QLCNIC_BRDTYPE_P3P_10G_CX4_LP:
  1076. case QLCNIC_BRDTYPE_P3P_IMEZ:
  1077. case QLCNIC_BRDTYPE_P3P_10G_SFP_PLUS:
  1078. case QLCNIC_BRDTYPE_P3P_10G_SFP_CT:
  1079. case QLCNIC_BRDTYPE_P3P_10G_SFP_QT:
  1080. case QLCNIC_BRDTYPE_P3P_10G_XFP:
  1081. case QLCNIC_BRDTYPE_P3P_10000_BASE_T:
  1082. adapter->ahw->port_type = QLCNIC_XGBE;
  1083. break;
  1084. case QLCNIC_BRDTYPE_P3P_REF_QG:
  1085. case QLCNIC_BRDTYPE_P3P_4_GB:
  1086. case QLCNIC_BRDTYPE_P3P_4_GB_MM:
  1087. adapter->ahw->port_type = QLCNIC_GBE;
  1088. break;
  1089. case QLCNIC_BRDTYPE_P3P_10G_TP:
  1090. adapter->ahw->port_type = (adapter->portnum < 2) ?
  1091. QLCNIC_XGBE : QLCNIC_GBE;
  1092. break;
  1093. default:
  1094. dev_err(&pdev->dev, "unknown board type %x\n", board_type);
  1095. adapter->ahw->port_type = QLCNIC_XGBE;
  1096. break;
  1097. }
  1098. return 0;
  1099. }
  1100. int
  1101. qlcnic_wol_supported(struct qlcnic_adapter *adapter)
  1102. {
  1103. u32 wol_cfg;
  1104. wol_cfg = QLCRD32(adapter, QLCNIC_WOL_CONFIG_NV);
  1105. if (wol_cfg & (1UL << adapter->portnum)) {
  1106. wol_cfg = QLCRD32(adapter, QLCNIC_WOL_CONFIG);
  1107. if (wol_cfg & (1 << adapter->portnum))
  1108. return 1;
  1109. }
  1110. return 0;
  1111. }
  1112. int qlcnic_config_led(struct qlcnic_adapter *adapter, u32 state, u32 rate)
  1113. {
  1114. struct qlcnic_nic_req req;
  1115. int rv;
  1116. u64 word;
  1117. memset(&req, 0, sizeof(struct qlcnic_nic_req));
  1118. req.qhdr = cpu_to_le64(QLCNIC_HOST_REQUEST << 23);
  1119. word = QLCNIC_H2C_OPCODE_CONFIG_LED | ((u64)adapter->portnum << 16);
  1120. req.req_hdr = cpu_to_le64(word);
  1121. req.words[0] = cpu_to_le64((u64)rate << 32);
  1122. req.words[1] = cpu_to_le64(state);
  1123. rv = qlcnic_send_cmd_descs(adapter, (struct cmd_desc_type0 *)&req, 1);
  1124. if (rv)
  1125. dev_err(&adapter->pdev->dev, "LED configuration failed.\n");
  1126. return rv;
  1127. }