usnic_fwd.c 8.0 KB

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