qlcnic_hw.c 37 KB

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