usnic_fwd.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
  3. *
  4. * This software is available to you under a choice of one of two
  5. * licenses. You may choose to be licensed under the terms of the GNU
  6. * General Public License (GPL) Version 2, available from the file
  7. * COPYING in the main directory of this source tree, or the
  8. * BSD license below:
  9. *
  10. * Redistribution and use in source and binary forms, with or
  11. * without modification, are permitted provided that the following
  12. * conditions are met:
  13. *
  14. * - Redistributions of source code must retain the above
  15. * copyright notice, this list of conditions and the following
  16. * disclaimer.
  17. *
  18. * - Redistributions in binary form must reproduce the above
  19. * copyright notice, this list of conditions and the following
  20. * disclaimer in the documentation and/or other materials
  21. * provided with the distribution.
  22. *
  23. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  27. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  28. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  29. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  30. * SOFTWARE.
  31. *
  32. */
  33. #include <linux/netdevice.h>
  34. #include <linux/pci.h>
  35. #include "enic_api.h"
  36. #include "usnic_common_pkt_hdr.h"
  37. #include "usnic_fwd.h"
  38. #include "usnic_log.h"
  39. static int usnic_fwd_devcmd_locked(struct usnic_fwd_dev *ufdev, int vnic_idx,
  40. enum vnic_devcmd_cmd cmd, u64 *a0,
  41. u64 *a1)
  42. {
  43. int status;
  44. struct net_device *netdev = ufdev->netdev;
  45. lockdep_assert_held(&ufdev->lock);
  46. status = enic_api_devcmd_proxy_by_index(netdev,
  47. vnic_idx,
  48. cmd,
  49. a0, a1,
  50. 1000);
  51. if (status) {
  52. if (status == ERR_EINVAL && cmd == CMD_DEL_FILTER) {
  53. usnic_dbg("Dev %s vnic idx %u cmd %u already deleted",
  54. ufdev->name, vnic_idx, cmd);
  55. } else {
  56. usnic_err("Dev %s vnic idx %u cmd %u failed with status %d\n",
  57. ufdev->name, vnic_idx, cmd,
  58. status);
  59. }
  60. } else {
  61. usnic_dbg("Dev %s vnic idx %u cmd %u success",
  62. ufdev->name, vnic_idx, cmd);
  63. }
  64. return status;
  65. }
  66. static int usnic_fwd_devcmd(struct usnic_fwd_dev *ufdev, int vnic_idx,
  67. enum vnic_devcmd_cmd cmd, u64 *a0, u64 *a1)
  68. {
  69. int status;
  70. spin_lock(&ufdev->lock);
  71. status = usnic_fwd_devcmd_locked(ufdev, vnic_idx, cmd, a0, a1);
  72. spin_unlock(&ufdev->lock);
  73. return status;
  74. }
  75. struct usnic_fwd_dev *usnic_fwd_dev_alloc(struct pci_dev *pdev)
  76. {
  77. struct usnic_fwd_dev *ufdev;
  78. ufdev = kzalloc(sizeof(*ufdev), GFP_KERNEL);
  79. if (!ufdev)
  80. return NULL;
  81. ufdev->pdev = pdev;
  82. ufdev->netdev = pci_get_drvdata(pdev);
  83. spin_lock_init(&ufdev->lock);
  84. BUILD_BUG_ON(sizeof(ufdev->name) != sizeof(ufdev->netdev->name));
  85. strcpy(ufdev->name, ufdev->netdev->name);
  86. return ufdev;
  87. }
  88. void usnic_fwd_dev_free(struct usnic_fwd_dev *ufdev)
  89. {
  90. kfree(ufdev);
  91. }
  92. void usnic_fwd_set_mac(struct usnic_fwd_dev *ufdev, char mac[ETH_ALEN])
  93. {
  94. spin_lock(&ufdev->lock);
  95. memcpy(&ufdev->mac, mac, sizeof(ufdev->mac));
  96. spin_unlock(&ufdev->lock);
  97. }
  98. void usnic_fwd_add_ipaddr(struct usnic_fwd_dev *ufdev, __be32 inaddr)
  99. {
  100. spin_lock(&ufdev->lock);
  101. if (!ufdev->inaddr)
  102. ufdev->inaddr = inaddr;
  103. spin_unlock(&ufdev->lock);
  104. }
  105. void usnic_fwd_del_ipaddr(struct usnic_fwd_dev *ufdev)
  106. {
  107. spin_lock(&ufdev->lock);
  108. ufdev->inaddr = 0;
  109. spin_unlock(&ufdev->lock);
  110. }
  111. void usnic_fwd_carrier_up(struct usnic_fwd_dev *ufdev)
  112. {
  113. spin_lock(&ufdev->lock);
  114. ufdev->link_up = 1;
  115. spin_unlock(&ufdev->lock);
  116. }
  117. void usnic_fwd_carrier_down(struct usnic_fwd_dev *ufdev)
  118. {
  119. spin_lock(&ufdev->lock);
  120. ufdev->link_up = 0;
  121. spin_unlock(&ufdev->lock);
  122. }
  123. void usnic_fwd_set_mtu(struct usnic_fwd_dev *ufdev, unsigned int mtu)
  124. {
  125. spin_lock(&ufdev->lock);
  126. ufdev->mtu = mtu;
  127. spin_unlock(&ufdev->lock);
  128. }
  129. static int usnic_fwd_dev_ready_locked(struct usnic_fwd_dev *ufdev)
  130. {
  131. lockdep_assert_held(&ufdev->lock);
  132. if (!ufdev->link_up)
  133. return -EPERM;
  134. return 0;
  135. }
  136. static int validate_filter_locked(struct usnic_fwd_dev *ufdev,
  137. struct filter *filter)
  138. {
  139. lockdep_assert_held(&ufdev->lock);
  140. if (filter->type == FILTER_IPV4_5TUPLE) {
  141. if (!(filter->u.ipv4.flags & FILTER_FIELD_5TUP_DST_AD))
  142. return -EACCES;
  143. if (!(filter->u.ipv4.flags & FILTER_FIELD_5TUP_DST_PT))
  144. return -EBUSY;
  145. else if (ufdev->inaddr == 0)
  146. return -EINVAL;
  147. else if (filter->u.ipv4.dst_port == 0)
  148. return -ERANGE;
  149. else if (ntohl(ufdev->inaddr) != filter->u.ipv4.dst_addr)
  150. return -EFAULT;
  151. else
  152. return 0;
  153. }
  154. return 0;
  155. }
  156. static void fill_tlv(struct filter_tlv *tlv, struct filter *filter,
  157. struct filter_action *action)
  158. {
  159. tlv->type = CLSF_TLV_FILTER;
  160. tlv->length = sizeof(struct filter);
  161. *((struct filter *)&tlv->val) = *filter;
  162. tlv = (struct filter_tlv *)((char *)tlv + sizeof(struct filter_tlv) +
  163. sizeof(struct filter));
  164. tlv->type = CLSF_TLV_ACTION;
  165. tlv->length = sizeof(struct filter_action);
  166. *((struct filter_action *)&tlv->val) = *action;
  167. }
  168. struct usnic_fwd_flow*
  169. usnic_fwd_alloc_flow(struct usnic_fwd_dev *ufdev, struct filter *filter,
  170. struct usnic_filter_action *uaction)
  171. {
  172. struct filter_tlv *tlv;
  173. struct pci_dev *pdev;
  174. struct usnic_fwd_flow *flow;
  175. uint64_t a0, a1;
  176. uint64_t tlv_size;
  177. dma_addr_t tlv_pa;
  178. int status;
  179. pdev = ufdev->pdev;
  180. tlv_size = (2*sizeof(struct filter_tlv) + sizeof(struct filter) +
  181. sizeof(struct filter_action));
  182. flow = kzalloc(sizeof(*flow), GFP_ATOMIC);
  183. if (!flow)
  184. return ERR_PTR(-ENOMEM);
  185. tlv = pci_alloc_consistent(pdev, tlv_size, &tlv_pa);
  186. if (!tlv) {
  187. usnic_err("Failed to allocate memory\n");
  188. status = -ENOMEM;
  189. goto out_free_flow;
  190. }
  191. fill_tlv(tlv, filter, &uaction->action);
  192. spin_lock(&ufdev->lock);
  193. status = usnic_fwd_dev_ready_locked(ufdev);
  194. if (status) {
  195. usnic_err("Forwarding dev %s not ready with status %d\n",
  196. ufdev->name, status);
  197. goto out_free_tlv;
  198. }
  199. status = validate_filter_locked(ufdev, filter);
  200. if (status) {
  201. usnic_err("Failed to validate filter with status %d\n",
  202. status);
  203. goto out_free_tlv;
  204. }
  205. /* Issue Devcmd */
  206. a0 = tlv_pa;
  207. a1 = tlv_size;
  208. status = usnic_fwd_devcmd_locked(ufdev, uaction->vnic_idx,
  209. CMD_ADD_FILTER, &a0, &a1);
  210. if (status) {
  211. usnic_err("VF %s Filter add failed with status:%d",
  212. ufdev->name, status);
  213. status = -EFAULT;
  214. goto out_free_tlv;
  215. } else {
  216. usnic_dbg("VF %s FILTER ID:%llu", ufdev->name, a0);
  217. }
  218. flow->flow_id = (uint32_t) a0;
  219. flow->vnic_idx = uaction->vnic_idx;
  220. flow->ufdev = ufdev;
  221. out_free_tlv:
  222. spin_unlock(&ufdev->lock);
  223. pci_free_consistent(pdev, tlv_size, tlv, tlv_pa);
  224. if (!status)
  225. return flow;
  226. out_free_flow:
  227. kfree(flow);
  228. return ERR_PTR(status);
  229. }
  230. int usnic_fwd_dealloc_flow(struct usnic_fwd_flow *flow)
  231. {
  232. int status;
  233. u64 a0, a1;
  234. a0 = flow->flow_id;
  235. status = usnic_fwd_devcmd(flow->ufdev, flow->vnic_idx,
  236. CMD_DEL_FILTER, &a0, &a1);
  237. if (status) {
  238. if (status == ERR_EINVAL) {
  239. usnic_dbg("Filter %u already deleted for VF Idx %u pf: %s status: %d",
  240. flow->flow_id, flow->vnic_idx,
  241. flow->ufdev->name, status);
  242. } else {
  243. usnic_err("PF %s VF Idx %u Filter: %u FILTER DELETE failed with status %d",
  244. flow->ufdev->name, flow->vnic_idx,
  245. flow->flow_id, status);
  246. }
  247. status = 0;
  248. /*
  249. * Log the error and fake success to the caller because if
  250. * a flow fails to be deleted in the firmware, it is an
  251. * unrecoverable error.
  252. */
  253. } else {
  254. usnic_dbg("PF %s VF Idx %u Filter: %u FILTER DELETED",
  255. flow->ufdev->name, flow->vnic_idx,
  256. flow->flow_id);
  257. }
  258. kfree(flow);
  259. return status;
  260. }
  261. int usnic_fwd_enable_qp(struct usnic_fwd_dev *ufdev, int vnic_idx, int qp_idx)
  262. {
  263. int status;
  264. struct net_device *pf_netdev;
  265. u64 a0, a1;
  266. pf_netdev = ufdev->netdev;
  267. a0 = qp_idx;
  268. a1 = CMD_QP_RQWQ;
  269. status = usnic_fwd_devcmd(ufdev, vnic_idx, CMD_QP_ENABLE,
  270. &a0, &a1);
  271. if (status) {
  272. usnic_err("PF %s VNIC Index %u RQ Index: %u ENABLE Failed with status %d",
  273. netdev_name(pf_netdev),
  274. vnic_idx,
  275. qp_idx,
  276. status);
  277. } else {
  278. usnic_dbg("PF %s VNIC Index %u RQ Index: %u ENABLED",
  279. netdev_name(pf_netdev),
  280. vnic_idx, qp_idx);
  281. }
  282. return status;
  283. }
  284. int usnic_fwd_disable_qp(struct usnic_fwd_dev *ufdev, int vnic_idx, int qp_idx)
  285. {
  286. int status;
  287. u64 a0, a1;
  288. struct net_device *pf_netdev;
  289. pf_netdev = ufdev->netdev;
  290. a0 = qp_idx;
  291. a1 = CMD_QP_RQWQ;
  292. status = usnic_fwd_devcmd(ufdev, vnic_idx, CMD_QP_DISABLE,
  293. &a0, &a1);
  294. if (status) {
  295. usnic_err("PF %s VNIC Index %u RQ Index: %u DISABLE Failed with status %d",
  296. netdev_name(pf_netdev),
  297. vnic_idx,
  298. qp_idx,
  299. status);
  300. } else {
  301. usnic_dbg("PF %s VNIC Index %u RQ Index: %u DISABLED",
  302. netdev_name(pf_netdev),
  303. vnic_idx,
  304. qp_idx);
  305. }
  306. return status;
  307. }