enic_res.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright 2008-2010 Cisco Systems, Inc. All rights reserved.
  3. * Copyright 2007 Nuova Systems, Inc. All rights reserved.
  4. *
  5. * This program is free software; you may redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; version 2 of the License.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  10. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  11. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  12. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  13. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  14. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  15. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  16. * SOFTWARE.
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/errno.h>
  21. #include <linux/types.h>
  22. #include <linux/pci.h>
  23. #include <linux/netdevice.h>
  24. #include "wq_enet_desc.h"
  25. #include "rq_enet_desc.h"
  26. #include "cq_enet_desc.h"
  27. #include "vnic_resource.h"
  28. #include "vnic_enet.h"
  29. #include "vnic_dev.h"
  30. #include "vnic_wq.h"
  31. #include "vnic_rq.h"
  32. #include "vnic_cq.h"
  33. #include "vnic_intr.h"
  34. #include "vnic_stats.h"
  35. #include "vnic_nic.h"
  36. #include "vnic_rss.h"
  37. #include "enic_res.h"
  38. #include "enic.h"
  39. int enic_get_vnic_config(struct enic *enic)
  40. {
  41. struct vnic_enet_config *c = &enic->config;
  42. int err;
  43. err = vnic_dev_get_mac_addr(enic->vdev, enic->mac_addr);
  44. if (err) {
  45. dev_err(enic_get_dev(enic),
  46. "Error getting MAC addr, %d\n", err);
  47. return err;
  48. }
  49. #define GET_CONFIG(m) \
  50. do { \
  51. err = vnic_dev_spec(enic->vdev, \
  52. offsetof(struct vnic_enet_config, m), \
  53. sizeof(c->m), &c->m); \
  54. if (err) { \
  55. dev_err(enic_get_dev(enic), \
  56. "Error getting %s, %d\n", #m, err); \
  57. return err; \
  58. } \
  59. } while (0)
  60. GET_CONFIG(flags);
  61. GET_CONFIG(wq_desc_count);
  62. GET_CONFIG(rq_desc_count);
  63. GET_CONFIG(mtu);
  64. GET_CONFIG(intr_timer_type);
  65. GET_CONFIG(intr_mode);
  66. GET_CONFIG(intr_timer_usec);
  67. GET_CONFIG(loop_tag);
  68. GET_CONFIG(num_arfs);
  69. c->wq_desc_count =
  70. min_t(u32, ENIC_MAX_WQ_DESCS,
  71. max_t(u32, ENIC_MIN_WQ_DESCS,
  72. c->wq_desc_count));
  73. c->wq_desc_count &= 0xffffffe0; /* must be aligned to groups of 32 */
  74. c->rq_desc_count =
  75. min_t(u32, ENIC_MAX_RQ_DESCS,
  76. max_t(u32, ENIC_MIN_RQ_DESCS,
  77. c->rq_desc_count));
  78. c->rq_desc_count &= 0xffffffe0; /* must be aligned to groups of 32 */
  79. if (c->mtu == 0)
  80. c->mtu = 1500;
  81. c->mtu = min_t(u16, ENIC_MAX_MTU,
  82. max_t(u16, ENIC_MIN_MTU,
  83. c->mtu));
  84. c->intr_timer_usec = min_t(u32, c->intr_timer_usec,
  85. vnic_dev_get_intr_coal_timer_max(enic->vdev));
  86. dev_info(enic_get_dev(enic),
  87. "vNIC MAC addr %pM wq/rq %d/%d mtu %d\n",
  88. enic->mac_addr, c->wq_desc_count, c->rq_desc_count, c->mtu);
  89. dev_info(enic_get_dev(enic), "vNIC csum tx/rx %s/%s "
  90. "tso/lro %s/%s rss %s intr mode %s type %s timer %d usec "
  91. "loopback tag 0x%04x\n",
  92. ENIC_SETTING(enic, TXCSUM) ? "yes" : "no",
  93. ENIC_SETTING(enic, RXCSUM) ? "yes" : "no",
  94. ENIC_SETTING(enic, TSO) ? "yes" : "no",
  95. ENIC_SETTING(enic, LRO) ? "yes" : "no",
  96. ENIC_SETTING(enic, RSS) ? "yes" : "no",
  97. c->intr_mode == VENET_INTR_MODE_INTX ? "INTx" :
  98. c->intr_mode == VENET_INTR_MODE_MSI ? "MSI" :
  99. c->intr_mode == VENET_INTR_MODE_ANY ? "any" :
  100. "unknown",
  101. c->intr_timer_type == VENET_INTR_TYPE_MIN ? "min" :
  102. c->intr_timer_type == VENET_INTR_TYPE_IDLE ? "idle" :
  103. "unknown",
  104. c->intr_timer_usec,
  105. c->loop_tag);
  106. return 0;
  107. }
  108. int enic_add_vlan(struct enic *enic, u16 vlanid)
  109. {
  110. u64 a0 = vlanid, a1 = 0;
  111. int wait = 1000;
  112. int err;
  113. err = vnic_dev_cmd(enic->vdev, CMD_VLAN_ADD, &a0, &a1, wait);
  114. if (err)
  115. dev_err(enic_get_dev(enic), "Can't add vlan id, %d\n", err);
  116. return err;
  117. }
  118. int enic_del_vlan(struct enic *enic, u16 vlanid)
  119. {
  120. u64 a0 = vlanid, a1 = 0;
  121. int wait = 1000;
  122. int err;
  123. err = vnic_dev_cmd(enic->vdev, CMD_VLAN_DEL, &a0, &a1, wait);
  124. if (err)
  125. dev_err(enic_get_dev(enic), "Can't delete vlan id, %d\n", err);
  126. return err;
  127. }
  128. int enic_set_nic_cfg(struct enic *enic, u8 rss_default_cpu, u8 rss_hash_type,
  129. u8 rss_hash_bits, u8 rss_base_cpu, u8 rss_enable, u8 tso_ipid_split_en,
  130. u8 ig_vlan_strip_en)
  131. {
  132. enum vnic_devcmd_cmd cmd = CMD_NIC_CFG;
  133. u64 a0, a1;
  134. u32 nic_cfg;
  135. int wait = 1000;
  136. vnic_set_nic_cfg(&nic_cfg, rss_default_cpu,
  137. rss_hash_type, rss_hash_bits, rss_base_cpu,
  138. rss_enable, tso_ipid_split_en, ig_vlan_strip_en);
  139. a0 = nic_cfg;
  140. a1 = 0;
  141. if (rss_hash_type & (NIC_CFG_RSS_HASH_TYPE_UDP_IPV4 |
  142. NIC_CFG_RSS_HASH_TYPE_UDP_IPV6))
  143. cmd = CMD_NIC_CFG_CHK;
  144. return vnic_dev_cmd(enic->vdev, cmd, &a0, &a1, wait);
  145. }
  146. int enic_set_rss_key(struct enic *enic, dma_addr_t key_pa, u64 len)
  147. {
  148. u64 a0 = (u64)key_pa, a1 = len;
  149. int wait = 1000;
  150. return vnic_dev_cmd(enic->vdev, CMD_RSS_KEY, &a0, &a1, wait);
  151. }
  152. int enic_set_rss_cpu(struct enic *enic, dma_addr_t cpu_pa, u64 len)
  153. {
  154. u64 a0 = (u64)cpu_pa, a1 = len;
  155. int wait = 1000;
  156. return vnic_dev_cmd(enic->vdev, CMD_RSS_CPU, &a0, &a1, wait);
  157. }
  158. void enic_free_vnic_resources(struct enic *enic)
  159. {
  160. unsigned int i;
  161. for (i = 0; i < enic->wq_count; i++)
  162. vnic_wq_free(&enic->wq[i]);
  163. for (i = 0; i < enic->rq_count; i++)
  164. vnic_rq_free(&enic->rq[i]);
  165. for (i = 0; i < enic->cq_count; i++)
  166. vnic_cq_free(&enic->cq[i]);
  167. for (i = 0; i < enic->intr_count; i++)
  168. vnic_intr_free(&enic->intr[i]);
  169. }
  170. void enic_get_res_counts(struct enic *enic)
  171. {
  172. enic->wq_count = vnic_dev_get_res_count(enic->vdev, RES_TYPE_WQ);
  173. enic->rq_count = vnic_dev_get_res_count(enic->vdev, RES_TYPE_RQ);
  174. enic->cq_count = vnic_dev_get_res_count(enic->vdev, RES_TYPE_CQ);
  175. enic->intr_count = vnic_dev_get_res_count(enic->vdev,
  176. RES_TYPE_INTR_CTRL);
  177. dev_info(enic_get_dev(enic),
  178. "vNIC resources avail: wq %d rq %d cq %d intr %d\n",
  179. enic->wq_count, enic->rq_count,
  180. enic->cq_count, enic->intr_count);
  181. }
  182. void enic_init_vnic_resources(struct enic *enic)
  183. {
  184. enum vnic_dev_intr_mode intr_mode;
  185. unsigned int mask_on_assertion;
  186. unsigned int interrupt_offset;
  187. unsigned int error_interrupt_enable;
  188. unsigned int error_interrupt_offset;
  189. unsigned int cq_index;
  190. unsigned int i;
  191. intr_mode = vnic_dev_get_intr_mode(enic->vdev);
  192. /* Init RQ/WQ resources.
  193. *
  194. * RQ[0 - n-1] point to CQ[0 - n-1]
  195. * WQ[0 - m-1] point to CQ[n - n+m-1]
  196. *
  197. * Error interrupt is not enabled for MSI.
  198. */
  199. switch (intr_mode) {
  200. case VNIC_DEV_INTR_MODE_INTX:
  201. case VNIC_DEV_INTR_MODE_MSIX:
  202. error_interrupt_enable = 1;
  203. error_interrupt_offset = enic->intr_count - 2;
  204. break;
  205. default:
  206. error_interrupt_enable = 0;
  207. error_interrupt_offset = 0;
  208. break;
  209. }
  210. for (i = 0; i < enic->rq_count; i++) {
  211. cq_index = i;
  212. vnic_rq_init(&enic->rq[i],
  213. cq_index,
  214. error_interrupt_enable,
  215. error_interrupt_offset);
  216. }
  217. for (i = 0; i < enic->wq_count; i++) {
  218. cq_index = enic->rq_count + i;
  219. vnic_wq_init(&enic->wq[i],
  220. cq_index,
  221. error_interrupt_enable,
  222. error_interrupt_offset);
  223. }
  224. /* Init CQ resources
  225. *
  226. * CQ[0 - n+m-1] point to INTR[0] for INTx, MSI
  227. * CQ[0 - n+m-1] point to INTR[0 - n+m-1] for MSI-X
  228. */
  229. for (i = 0; i < enic->cq_count; i++) {
  230. switch (intr_mode) {
  231. case VNIC_DEV_INTR_MODE_MSIX:
  232. interrupt_offset = i;
  233. break;
  234. default:
  235. interrupt_offset = 0;
  236. break;
  237. }
  238. vnic_cq_init(&enic->cq[i],
  239. 0 /* flow_control_enable */,
  240. 1 /* color_enable */,
  241. 0 /* cq_head */,
  242. 0 /* cq_tail */,
  243. 1 /* cq_tail_color */,
  244. 1 /* interrupt_enable */,
  245. 1 /* cq_entry_enable */,
  246. 0 /* cq_message_enable */,
  247. interrupt_offset,
  248. 0 /* cq_message_addr */);
  249. }
  250. /* Init INTR resources
  251. *
  252. * mask_on_assertion is not used for INTx due to the level-
  253. * triggered nature of INTx
  254. */
  255. switch (intr_mode) {
  256. case VNIC_DEV_INTR_MODE_MSI:
  257. case VNIC_DEV_INTR_MODE_MSIX:
  258. mask_on_assertion = 1;
  259. break;
  260. default:
  261. mask_on_assertion = 0;
  262. break;
  263. }
  264. for (i = 0; i < enic->intr_count; i++) {
  265. vnic_intr_init(&enic->intr[i],
  266. enic->config.intr_timer_usec,
  267. enic->config.intr_timer_type,
  268. mask_on_assertion);
  269. }
  270. }
  271. int enic_alloc_vnic_resources(struct enic *enic)
  272. {
  273. enum vnic_dev_intr_mode intr_mode;
  274. unsigned int i;
  275. int err;
  276. intr_mode = vnic_dev_get_intr_mode(enic->vdev);
  277. dev_info(enic_get_dev(enic), "vNIC resources used: "
  278. "wq %d rq %d cq %d intr %d intr mode %s\n",
  279. enic->wq_count, enic->rq_count,
  280. enic->cq_count, enic->intr_count,
  281. intr_mode == VNIC_DEV_INTR_MODE_INTX ? "legacy PCI INTx" :
  282. intr_mode == VNIC_DEV_INTR_MODE_MSI ? "MSI" :
  283. intr_mode == VNIC_DEV_INTR_MODE_MSIX ? "MSI-X" :
  284. "unknown");
  285. /* Allocate queue resources
  286. */
  287. for (i = 0; i < enic->wq_count; i++) {
  288. err = vnic_wq_alloc(enic->vdev, &enic->wq[i], i,
  289. enic->config.wq_desc_count,
  290. sizeof(struct wq_enet_desc));
  291. if (err)
  292. goto err_out_cleanup;
  293. }
  294. for (i = 0; i < enic->rq_count; i++) {
  295. err = vnic_rq_alloc(enic->vdev, &enic->rq[i], i,
  296. enic->config.rq_desc_count,
  297. sizeof(struct rq_enet_desc));
  298. if (err)
  299. goto err_out_cleanup;
  300. }
  301. for (i = 0; i < enic->cq_count; i++) {
  302. if (i < enic->rq_count)
  303. err = vnic_cq_alloc(enic->vdev, &enic->cq[i], i,
  304. enic->config.rq_desc_count,
  305. sizeof(struct cq_enet_rq_desc));
  306. else
  307. err = vnic_cq_alloc(enic->vdev, &enic->cq[i], i,
  308. enic->config.wq_desc_count,
  309. sizeof(struct cq_enet_wq_desc));
  310. if (err)
  311. goto err_out_cleanup;
  312. }
  313. for (i = 0; i < enic->intr_count; i++) {
  314. err = vnic_intr_alloc(enic->vdev, &enic->intr[i], i);
  315. if (err)
  316. goto err_out_cleanup;
  317. }
  318. /* Hook remaining resource
  319. */
  320. enic->legacy_pba = vnic_dev_get_res(enic->vdev,
  321. RES_TYPE_INTR_PBA_LEGACY, 0);
  322. if (!enic->legacy_pba && intr_mode == VNIC_DEV_INTR_MODE_INTX) {
  323. dev_err(enic_get_dev(enic),
  324. "Failed to hook legacy pba resource\n");
  325. err = -ENODEV;
  326. goto err_out_cleanup;
  327. }
  328. return 0;
  329. err_out_cleanup:
  330. enic_free_vnic_resources(enic);
  331. return err;
  332. }