qlcnic_hw.c 37 KB

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