2 * Copyright (c) 2013, Cisco Systems, Inc. All rights reserved.
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
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <linux/bug.h>
34 #include <linux/errno.h>
35 #include <linux/spinlock.h>
37 #include "usnic_log.h"
38 #include "usnic_vnic.h"
39 #include "usnic_fwd.h"
40 #include "usnic_uiom.h"
41 #include "usnic_debugfs.h"
42 #include "usnic_ib_qp_grp.h"
43 #include "usnic_ib_sysfs.h"
44 #include "usnic_transport.h"
48 const char *usnic_ib_qp_grp_state_to_string(enum ib_qp_state state)
66 return "UNKNOWN STATE";
71 int usnic_ib_qp_grp_dump_hdr(char *buf, int buf_sz)
73 return scnprintf(buf, buf_sz, "|QPN\t|State\t|PID\t|VF Idx\t|Fil ID");
76 int usnic_ib_qp_grp_dump_rows(void *obj, char *buf, int buf_sz)
78 struct usnic_ib_qp_grp *qp_grp = obj;
79 struct usnic_ib_qp_grp_flow *default_flow;
81 default_flow = list_first_entry(&qp_grp->flows_lst,
82 struct usnic_ib_qp_grp_flow, link);
83 return scnprintf(buf, buf_sz, "|%d\t|%s\t|%d\t|%hu\t|%d",
85 usnic_ib_qp_grp_state_to_string(
88 usnic_vnic_get_index(qp_grp->vf->vnic),
89 default_flow->flow->flow_id);
91 return scnprintf(buf, buf_sz, "|N/A\t|N/A\t|N/A\t|N/A\t|N/A");
95 static struct usnic_vnic_res_chunk *
96 get_qp_res_chunk(struct usnic_ib_qp_grp *qp_grp)
98 lockdep_assert_held(&qp_grp->lock);
100 * The QP res chunk, used to derive qp indices,
101 * are just indices of the RQs
103 return usnic_ib_qp_grp_get_chunk(qp_grp, USNIC_VNIC_RES_TYPE_RQ);
106 static int enable_qp_grp(struct usnic_ib_qp_grp *qp_grp)
111 struct usnic_vnic_res_chunk *res_chunk;
112 struct usnic_vnic_res *res;
114 lockdep_assert_held(&qp_grp->lock);
116 vnic_idx = usnic_vnic_get_index(qp_grp->vf->vnic);
118 res_chunk = get_qp_res_chunk(qp_grp);
119 if (IS_ERR(res_chunk)) {
120 usnic_err("Unable to get qp res with err %ld\n",
122 return PTR_ERR(res_chunk);
125 for (i = 0; i < res_chunk->cnt; i++) {
126 res = res_chunk->res[i];
127 status = usnic_fwd_enable_qp(qp_grp->ufdev, vnic_idx,
130 usnic_err("Failed to enable qp %d of %s:%d\n with err %d\n",
131 res->vnic_idx, qp_grp->ufdev->name,
140 for (i--; i >= 0; i--) {
141 res = res_chunk->res[i];
142 usnic_fwd_disable_qp(qp_grp->ufdev, vnic_idx,
149 static int disable_qp_grp(struct usnic_ib_qp_grp *qp_grp)
152 struct usnic_vnic_res_chunk *res_chunk;
153 struct usnic_vnic_res *res;
156 lockdep_assert_held(&qp_grp->lock);
157 vnic_idx = usnic_vnic_get_index(qp_grp->vf->vnic);
159 res_chunk = get_qp_res_chunk(qp_grp);
160 if (IS_ERR(res_chunk)) {
161 usnic_err("Unable to get qp res with err %ld\n",
163 return PTR_ERR(res_chunk);
166 for (i = 0; i < res_chunk->cnt; i++) {
167 res = res_chunk->res[i];
168 status = usnic_fwd_disable_qp(qp_grp->ufdev, vnic_idx,
171 usnic_err("Failed to disable rq %d of %s:%d\n with err %d\n",
182 static int init_filter_action(struct usnic_ib_qp_grp *qp_grp,
183 struct usnic_filter_action *uaction)
185 struct usnic_vnic_res_chunk *res_chunk;
187 res_chunk = usnic_ib_qp_grp_get_chunk(qp_grp, USNIC_VNIC_RES_TYPE_RQ);
188 if (IS_ERR(res_chunk)) {
189 usnic_err("Unable to get %s with err %ld\n",
190 usnic_vnic_res_type_to_str(USNIC_VNIC_RES_TYPE_RQ),
192 return PTR_ERR(res_chunk);
195 uaction->vnic_idx = usnic_vnic_get_index(qp_grp->vf->vnic);
196 uaction->action.type = FILTER_ACTION_RQ_STEERING;
197 uaction->action.u.rq_idx = res_chunk->res[DFLT_RQ_IDX]->vnic_idx;
202 static struct usnic_ib_qp_grp_flow*
203 create_roce_custom_flow(struct usnic_ib_qp_grp *qp_grp,
204 struct usnic_transport_spec *trans_spec)
208 struct filter filter;
209 struct usnic_filter_action uaction;
210 struct usnic_ib_qp_grp_flow *qp_flow;
211 struct usnic_fwd_flow *flow;
212 enum usnic_transport_type trans_type;
214 trans_type = trans_spec->trans_type;
215 port_num = trans_spec->usnic_roce.port_num;
218 port_num = usnic_transport_rsrv_port(trans_type, port_num);
220 return ERR_PTR(-EINVAL);
223 usnic_fwd_init_usnic_filter(&filter, port_num);
224 err = init_filter_action(qp_grp, &uaction);
226 goto out_unreserve_port;
228 flow = usnic_fwd_alloc_flow(qp_grp->ufdev, &filter, &uaction);
229 if (IS_ERR_OR_NULL(flow)) {
230 err = flow ? PTR_ERR(flow) : -EFAULT;
231 goto out_unreserve_port;
234 /* Create Flow Handle */
235 qp_flow = kzalloc(sizeof(*qp_flow), GFP_ATOMIC);
238 goto out_dealloc_flow;
240 qp_flow->flow = flow;
241 qp_flow->trans_type = trans_type;
242 qp_flow->usnic_roce.port_num = port_num;
243 qp_flow->qp_grp = qp_grp;
247 usnic_fwd_dealloc_flow(flow);
249 usnic_transport_unrsrv_port(trans_type, port_num);
253 static void release_roce_custom_flow(struct usnic_ib_qp_grp_flow *qp_flow)
255 usnic_fwd_dealloc_flow(qp_flow->flow);
256 usnic_transport_unrsrv_port(qp_flow->trans_type,
257 qp_flow->usnic_roce.port_num);
261 static struct usnic_ib_qp_grp_flow*
262 create_udp_flow(struct usnic_ib_qp_grp *qp_grp,
263 struct usnic_transport_spec *trans_spec)
268 struct filter filter;
269 struct usnic_filter_action uaction;
270 struct usnic_ib_qp_grp_flow *qp_flow;
271 struct usnic_fwd_flow *flow;
272 enum usnic_transport_type trans_type;
277 trans_type = trans_spec->trans_type;
278 sock_fd = trans_spec->udp.sock_fd;
280 /* Get and check socket */
281 sock = usnic_transport_get_socket(sock_fd);
282 if (IS_ERR_OR_NULL(sock))
283 return ERR_CAST(sock);
285 err = usnic_transport_sock_get_addr(sock, &proto, &addr, &port_num);
289 if (proto != IPPROTO_UDP) {
290 usnic_err("Protocol for fd %d is not UDP", sock_fd);
296 usnic_fwd_init_udp_filter(&filter, addr, port_num);
297 err = init_filter_action(qp_grp, &uaction);
301 flow = usnic_fwd_alloc_flow(qp_grp->ufdev, &filter, &uaction);
302 if (IS_ERR_OR_NULL(flow)) {
303 err = flow ? PTR_ERR(flow) : -EFAULT;
308 qp_flow = kzalloc(sizeof(*qp_flow), GFP_ATOMIC);
311 goto out_dealloc_flow;
313 qp_flow->flow = flow;
314 qp_flow->trans_type = trans_type;
315 qp_flow->udp.sock = sock;
316 qp_flow->qp_grp = qp_grp;
320 usnic_fwd_dealloc_flow(flow);
322 usnic_transport_put_socket(sock);
326 static void release_udp_flow(struct usnic_ib_qp_grp_flow *qp_flow)
328 usnic_fwd_dealloc_flow(qp_flow->flow);
329 usnic_transport_put_socket(qp_flow->udp.sock);
333 static struct usnic_ib_qp_grp_flow*
334 create_and_add_flow(struct usnic_ib_qp_grp *qp_grp,
335 struct usnic_transport_spec *trans_spec)
337 struct usnic_ib_qp_grp_flow *qp_flow;
338 enum usnic_transport_type trans_type;
340 trans_type = trans_spec->trans_type;
341 switch (trans_type) {
342 case USNIC_TRANSPORT_ROCE_CUSTOM:
343 qp_flow = create_roce_custom_flow(qp_grp, trans_spec);
345 case USNIC_TRANSPORT_IPV4_UDP:
346 qp_flow = create_udp_flow(qp_grp, trans_spec);
349 usnic_err("Unsupported transport %u\n",
350 trans_spec->trans_type);
351 return ERR_PTR(-EINVAL);
354 if (!IS_ERR_OR_NULL(qp_flow)) {
355 list_add_tail(&qp_flow->link, &qp_grp->flows_lst);
356 usnic_debugfs_flow_add(qp_flow);
363 static void release_and_remove_flow(struct usnic_ib_qp_grp_flow *qp_flow)
365 usnic_debugfs_flow_remove(qp_flow);
366 list_del(&qp_flow->link);
368 switch (qp_flow->trans_type) {
369 case USNIC_TRANSPORT_ROCE_CUSTOM:
370 release_roce_custom_flow(qp_flow);
372 case USNIC_TRANSPORT_IPV4_UDP:
373 release_udp_flow(qp_flow);
376 WARN(1, "Unsupported transport %u\n",
377 qp_flow->trans_type);
382 static void release_and_remove_all_flows(struct usnic_ib_qp_grp *qp_grp)
384 struct usnic_ib_qp_grp_flow *qp_flow, *tmp;
385 list_for_each_entry_safe(qp_flow, tmp, &qp_grp->flows_lst, link)
386 release_and_remove_flow(qp_flow);
389 int usnic_ib_qp_grp_modify(struct usnic_ib_qp_grp *qp_grp,
390 enum ib_qp_state new_state,
394 struct ib_event ib_event;
395 enum ib_qp_state old_state;
396 struct usnic_transport_spec *trans_spec;
397 struct usnic_ib_qp_grp_flow *qp_flow;
399 old_state = qp_grp->state;
400 trans_spec = (struct usnic_transport_spec *) data;
402 spin_lock(&qp_grp->lock);
410 release_and_remove_all_flows(qp_grp);
416 status = disable_qp_grp(qp_grp);
417 release_and_remove_all_flows(qp_grp);
427 qp_flow = create_and_add_flow(qp_grp,
429 if (IS_ERR_OR_NULL(qp_flow)) {
430 status = qp_flow ? PTR_ERR(qp_flow) : -EFAULT;
435 * Optional to specify filters.
442 qp_flow = create_and_add_flow(qp_grp,
444 if (IS_ERR_OR_NULL(qp_flow)) {
445 status = qp_flow ? PTR_ERR(qp_flow) : -EFAULT;
450 * Doesn't make sense to go into INIT state
451 * from INIT state w/o adding filters.
457 status = disable_qp_grp(qp_grp);
460 status = disable_qp_grp(qp_grp);
469 status = enable_qp_grp(qp_grp);
485 ib_event.device = &qp_grp->vf->pf->ib_dev;
486 ib_event.element.qp = &qp_grp->ibqp;
487 ib_event.event = IB_EVENT_QP_FATAL;
491 qp_grp->ibqp.event_handler(&ib_event,
492 qp_grp->ibqp.qp_context);
495 release_and_remove_all_flows(qp_grp);
496 qp_grp->ibqp.event_handler(&ib_event,
497 qp_grp->ibqp.qp_context);
501 status = disable_qp_grp(qp_grp);
502 release_and_remove_all_flows(qp_grp);
503 qp_grp->ibqp.event_handler(&ib_event,
504 qp_grp->ibqp.qp_context);
513 spin_unlock(&qp_grp->lock);
516 qp_grp->state = new_state;
517 usnic_info("Transitioned %u from %s to %s",
519 usnic_ib_qp_grp_state_to_string(old_state),
520 usnic_ib_qp_grp_state_to_string(new_state));
522 usnic_err("Failed to transition %u from %s to %s",
524 usnic_ib_qp_grp_state_to_string(old_state),
525 usnic_ib_qp_grp_state_to_string(new_state));
531 static struct usnic_vnic_res_chunk**
532 alloc_res_chunk_list(struct usnic_vnic *vnic,
533 struct usnic_vnic_res_spec *res_spec, void *owner_obj)
535 enum usnic_vnic_res_type res_type;
536 struct usnic_vnic_res_chunk **res_chunk_list;
537 int err, i, res_cnt, res_lst_sz;
540 res_spec->resources[res_lst_sz].type != USNIC_VNIC_RES_TYPE_EOL;
545 res_chunk_list = kcalloc(res_lst_sz + 1, sizeof(*res_chunk_list),
548 return ERR_PTR(-ENOMEM);
550 for (i = 0; res_spec->resources[i].type != USNIC_VNIC_RES_TYPE_EOL;
552 res_type = res_spec->resources[i].type;
553 res_cnt = res_spec->resources[i].cnt;
555 res_chunk_list[i] = usnic_vnic_get_resources(vnic, res_type,
557 if (IS_ERR_OR_NULL(res_chunk_list[i])) {
558 err = res_chunk_list[i] ?
559 PTR_ERR(res_chunk_list[i]) : -ENOMEM;
560 usnic_err("Failed to get %s from %s with err %d\n",
561 usnic_vnic_res_type_to_str(res_type),
562 usnic_vnic_pci_name(vnic),
568 return res_chunk_list;
571 for (i--; i >= 0; i--)
572 usnic_vnic_put_resources(res_chunk_list[i]);
573 kfree(res_chunk_list);
577 static void free_qp_grp_res(struct usnic_vnic_res_chunk **res_chunk_list)
580 for (i = 0; res_chunk_list[i]; i++)
581 usnic_vnic_put_resources(res_chunk_list[i]);
582 kfree(res_chunk_list);
585 static int qp_grp_and_vf_bind(struct usnic_ib_vf *vf,
586 struct usnic_ib_pd *pd,
587 struct usnic_ib_qp_grp *qp_grp)
590 struct pci_dev *pdev;
592 lockdep_assert_held(&vf->lock);
594 pdev = usnic_vnic_get_pdev(vf->vnic);
595 if (vf->qp_grp_ref_cnt == 0) {
596 err = usnic_uiom_attach_dev_to_pd(pd->umem_pd, &pdev->dev);
598 usnic_err("Failed to attach %s to domain\n",
604 vf->qp_grp_ref_cnt++;
606 WARN_ON(vf->pd != pd);
612 static void qp_grp_and_vf_unbind(struct usnic_ib_qp_grp *qp_grp)
614 struct pci_dev *pdev;
615 struct usnic_ib_pd *pd;
617 lockdep_assert_held(&qp_grp->vf->lock);
620 pdev = usnic_vnic_get_pdev(qp_grp->vf->vnic);
621 if (--qp_grp->vf->qp_grp_ref_cnt == 0) {
622 qp_grp->vf->pd = NULL;
623 usnic_uiom_detach_dev_from_pd(pd->umem_pd, &pdev->dev);
628 static void log_spec(struct usnic_vnic_res_spec *res_spec)
631 usnic_vnic_spec_dump(buf, sizeof(buf), res_spec);
632 usnic_dbg("%s\n", buf);
635 static int qp_grp_id_from_flow(struct usnic_ib_qp_grp_flow *qp_flow,
638 enum usnic_transport_type trans_type = qp_flow->trans_type;
640 uint16_t port_num = 0;
642 switch (trans_type) {
643 case USNIC_TRANSPORT_ROCE_CUSTOM:
644 *id = qp_flow->usnic_roce.port_num;
646 case USNIC_TRANSPORT_IPV4_UDP:
647 err = usnic_transport_sock_get_addr(qp_flow->udp.sock,
653 * Copy port_num to stack first and then to *id,
654 * so that the short to int cast works for little
655 * and big endian systems.
660 usnic_err("Unsupported transport %u\n", trans_type);
667 int usnic_ib_qp_grp_create(struct usnic_ib_qp_grp *qp_grp,
668 struct usnic_fwd_dev *ufdev, struct usnic_ib_vf *vf,
669 struct usnic_ib_pd *pd,
670 struct usnic_vnic_res_spec *res_spec,
671 struct usnic_transport_spec *transport_spec)
674 enum usnic_transport_type transport = transport_spec->trans_type;
675 struct usnic_ib_qp_grp_flow *qp_flow;
677 lockdep_assert_held(&vf->lock);
679 err = usnic_vnic_res_spec_satisfied(&min_transport_spec[transport],
682 usnic_err("Spec does not meet minimum req for transport %d\n",
688 qp_grp->res_chunk_list = alloc_res_chunk_list(vf->vnic, res_spec,
690 if (IS_ERR_OR_NULL(qp_grp->res_chunk_list))
691 return qp_grp->res_chunk_list ?
692 PTR_ERR(qp_grp->res_chunk_list) :
695 err = qp_grp_and_vf_bind(vf, pd, qp_grp);
699 INIT_LIST_HEAD(&qp_grp->flows_lst);
700 spin_lock_init(&qp_grp->lock);
701 qp_grp->ufdev = ufdev;
702 qp_grp->state = IB_QPS_RESET;
703 qp_grp->owner_pid = current->pid;
705 qp_flow = create_and_add_flow(qp_grp, transport_spec);
706 if (IS_ERR_OR_NULL(qp_flow)) {
707 usnic_err("Unable to create and add flow with err %ld\n",
709 err = qp_flow ? PTR_ERR(qp_flow) : -EFAULT;
710 goto out_qp_grp_vf_unbind;
713 err = qp_grp_id_from_flow(qp_flow, &qp_grp->grp_id);
715 goto out_release_flow;
716 qp_grp->ibqp.qp_num = qp_grp->grp_id;
718 usnic_ib_sysfs_qpn_add(qp_grp);
723 release_and_remove_flow(qp_flow);
724 out_qp_grp_vf_unbind:
725 qp_grp_and_vf_unbind(qp_grp);
727 free_qp_grp_res(qp_grp->res_chunk_list);
731 void usnic_ib_qp_grp_destroy(struct usnic_ib_qp_grp *qp_grp)
734 WARN_ON(qp_grp->state != IB_QPS_RESET);
735 lockdep_assert_held(&qp_grp->vf->lock);
737 release_and_remove_all_flows(qp_grp);
738 usnic_ib_sysfs_qpn_remove(qp_grp);
739 qp_grp_and_vf_unbind(qp_grp);
740 free_qp_grp_res(qp_grp->res_chunk_list);
743 struct usnic_vnic_res_chunk*
744 usnic_ib_qp_grp_get_chunk(struct usnic_ib_qp_grp *qp_grp,
745 enum usnic_vnic_res_type res_type)
749 for (i = 0; qp_grp->res_chunk_list[i]; i++) {
750 if (qp_grp->res_chunk_list[i]->type == res_type)
751 return qp_grp->res_chunk_list[i];
754 return ERR_PTR(-EINVAL);