xref: /openbmc/linux/drivers/infiniband/hw/qedr/main.c (revision 884caada)
1 /* QLogic qedr NIC Driver
2  * Copyright (c) 2015-2016  QLogic Corporation
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  * OpenIB.org 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 #include <linux/module.h>
33 #include <rdma/ib_verbs.h>
34 #include <rdma/ib_addr.h>
35 #include <rdma/ib_user_verbs.h>
36 #include <rdma/iw_cm.h>
37 #include <rdma/ib_mad.h>
38 #include <linux/netdevice.h>
39 #include <linux/iommu.h>
40 #include <linux/pci.h>
41 #include <net/addrconf.h>
42 
43 #include <linux/qed/qed_chain.h>
44 #include <linux/qed/qed_if.h>
45 #include "qedr.h"
46 #include "verbs.h"
47 #include <rdma/qedr-abi.h>
48 #include "qedr_iw_cm.h"
49 
50 MODULE_DESCRIPTION("QLogic 40G/100G ROCE Driver");
51 MODULE_AUTHOR("QLogic Corporation");
52 MODULE_LICENSE("Dual BSD/GPL");
53 
54 #define QEDR_WQ_MULTIPLIER_DFT	(3)
55 
56 static void qedr_ib_dispatch_event(struct qedr_dev *dev, u8 port_num,
57 				   enum ib_event_type type)
58 {
59 	struct ib_event ibev;
60 
61 	ibev.device = &dev->ibdev;
62 	ibev.element.port_num = port_num;
63 	ibev.event = type;
64 
65 	ib_dispatch_event(&ibev);
66 }
67 
68 static enum rdma_link_layer qedr_link_layer(struct ib_device *device,
69 					    u8 port_num)
70 {
71 	return IB_LINK_LAYER_ETHERNET;
72 }
73 
74 static void qedr_get_dev_fw_str(struct ib_device *ibdev, char *str)
75 {
76 	struct qedr_dev *qedr = get_qedr_dev(ibdev);
77 	u32 fw_ver = (u32)qedr->attr.fw_ver;
78 
79 	snprintf(str, IB_FW_VERSION_NAME_MAX, "%d. %d. %d. %d",
80 		 (fw_ver >> 24) & 0xFF, (fw_ver >> 16) & 0xFF,
81 		 (fw_ver >> 8) & 0xFF, fw_ver & 0xFF);
82 }
83 
84 static int qedr_roce_port_immutable(struct ib_device *ibdev, u8 port_num,
85 				    struct ib_port_immutable *immutable)
86 {
87 	struct ib_port_attr attr;
88 	int err;
89 
90 	err = qedr_query_port(ibdev, port_num, &attr);
91 	if (err)
92 		return err;
93 
94 	immutable->pkey_tbl_len = attr.pkey_tbl_len;
95 	immutable->gid_tbl_len = attr.gid_tbl_len;
96 	immutable->core_cap_flags = RDMA_CORE_PORT_IBA_ROCE |
97 	    RDMA_CORE_PORT_IBA_ROCE_UDP_ENCAP;
98 	immutable->max_mad_size = IB_MGMT_MAD_SIZE;
99 
100 	return 0;
101 }
102 
103 static int qedr_iw_port_immutable(struct ib_device *ibdev, u8 port_num,
104 				  struct ib_port_immutable *immutable)
105 {
106 	struct ib_port_attr attr;
107 	int err;
108 
109 	err = qedr_query_port(ibdev, port_num, &attr);
110 	if (err)
111 		return err;
112 
113 	immutable->pkey_tbl_len = 1;
114 	immutable->gid_tbl_len = 1;
115 	immutable->core_cap_flags = RDMA_CORE_PORT_IWARP;
116 	immutable->max_mad_size = 0;
117 
118 	return 0;
119 }
120 
121 /* QEDR sysfs interface */
122 static ssize_t hw_rev_show(struct device *device, struct device_attribute *attr,
123 			   char *buf)
124 {
125 	struct qedr_dev *dev =
126 		rdma_device_to_drv_device(device, struct qedr_dev, ibdev);
127 
128 	return scnprintf(buf, PAGE_SIZE, "0x%x\n", dev->attr.hw_ver);
129 }
130 static DEVICE_ATTR_RO(hw_rev);
131 
132 static ssize_t hca_type_show(struct device *device,
133 			     struct device_attribute *attr, char *buf)
134 {
135 	struct qedr_dev *dev =
136 		rdma_device_to_drv_device(device, struct qedr_dev, ibdev);
137 
138 	return scnprintf(buf, PAGE_SIZE, "FastLinQ QL%x %s\n",
139 			 dev->pdev->device,
140 			 rdma_protocol_iwarp(&dev->ibdev, 1) ?
141 			 "iWARP" : "RoCE");
142 }
143 static DEVICE_ATTR_RO(hca_type);
144 
145 static struct attribute *qedr_attributes[] = {
146 	&dev_attr_hw_rev.attr,
147 	&dev_attr_hca_type.attr,
148 	NULL
149 };
150 
151 static const struct attribute_group qedr_attr_group = {
152 	.attrs = qedr_attributes,
153 };
154 
155 static const struct ib_device_ops qedr_iw_dev_ops = {
156 	.get_port_immutable = qedr_iw_port_immutable,
157 	.iw_accept = qedr_iw_accept,
158 	.iw_add_ref = qedr_iw_qp_add_ref,
159 	.iw_connect = qedr_iw_connect,
160 	.iw_create_listen = qedr_iw_create_listen,
161 	.iw_destroy_listen = qedr_iw_destroy_listen,
162 	.iw_get_qp = qedr_iw_get_qp,
163 	.iw_reject = qedr_iw_reject,
164 	.iw_rem_ref = qedr_iw_qp_rem_ref,
165 	.query_gid = qedr_iw_query_gid,
166 };
167 
168 static int qedr_iw_register_device(struct qedr_dev *dev)
169 {
170 	dev->ibdev.node_type = RDMA_NODE_RNIC;
171 
172 	ib_set_device_ops(&dev->ibdev, &qedr_iw_dev_ops);
173 
174 	memcpy(dev->ibdev.iw_ifname,
175 	       dev->ndev->name, sizeof(dev->ibdev.iw_ifname));
176 
177 	return 0;
178 }
179 
180 static const struct ib_device_ops qedr_roce_dev_ops = {
181 	.get_port_immutable = qedr_roce_port_immutable,
182 };
183 
184 static void qedr_roce_register_device(struct qedr_dev *dev)
185 {
186 	dev->ibdev.node_type = RDMA_NODE_IB_CA;
187 
188 	ib_set_device_ops(&dev->ibdev, &qedr_roce_dev_ops);
189 }
190 
191 static const struct ib_device_ops qedr_dev_ops = {
192 	.owner = THIS_MODULE,
193 	.driver_id = RDMA_DRIVER_QEDR,
194 	.uverbs_abi_ver = QEDR_ABI_VERSION,
195 
196 	.alloc_mr = qedr_alloc_mr,
197 	.alloc_pd = qedr_alloc_pd,
198 	.alloc_ucontext = qedr_alloc_ucontext,
199 	.create_ah = qedr_create_ah,
200 	.create_cq = qedr_create_cq,
201 	.create_qp = qedr_create_qp,
202 	.create_srq = qedr_create_srq,
203 	.dealloc_pd = qedr_dealloc_pd,
204 	.dealloc_ucontext = qedr_dealloc_ucontext,
205 	.dereg_mr = qedr_dereg_mr,
206 	.destroy_ah = qedr_destroy_ah,
207 	.destroy_cq = qedr_destroy_cq,
208 	.destroy_qp = qedr_destroy_qp,
209 	.destroy_srq = qedr_destroy_srq,
210 	.get_dev_fw_str = qedr_get_dev_fw_str,
211 	.get_dma_mr = qedr_get_dma_mr,
212 	.get_link_layer = qedr_link_layer,
213 	.map_mr_sg = qedr_map_mr_sg,
214 	.mmap = qedr_mmap,
215 	.modify_port = qedr_modify_port,
216 	.modify_qp = qedr_modify_qp,
217 	.modify_srq = qedr_modify_srq,
218 	.poll_cq = qedr_poll_cq,
219 	.post_recv = qedr_post_recv,
220 	.post_send = qedr_post_send,
221 	.post_srq_recv = qedr_post_srq_recv,
222 	.process_mad = qedr_process_mad,
223 	.query_device = qedr_query_device,
224 	.query_pkey = qedr_query_pkey,
225 	.query_port = qedr_query_port,
226 	.query_qp = qedr_query_qp,
227 	.query_srq = qedr_query_srq,
228 	.reg_user_mr = qedr_reg_user_mr,
229 	.req_notify_cq = qedr_arm_cq,
230 	.resize_cq = qedr_resize_cq,
231 
232 	INIT_RDMA_OBJ_SIZE(ib_ah, qedr_ah, ibah),
233 	INIT_RDMA_OBJ_SIZE(ib_cq, qedr_cq, ibcq),
234 	INIT_RDMA_OBJ_SIZE(ib_pd, qedr_pd, ibpd),
235 	INIT_RDMA_OBJ_SIZE(ib_srq, qedr_srq, ibsrq),
236 	INIT_RDMA_OBJ_SIZE(ib_ucontext, qedr_ucontext, ibucontext),
237 };
238 
239 static int qedr_register_device(struct qedr_dev *dev)
240 {
241 	int rc;
242 
243 	dev->ibdev.node_guid = dev->attr.node_guid;
244 	memcpy(dev->ibdev.node_desc, QEDR_NODE_DESC, sizeof(QEDR_NODE_DESC));
245 
246 	dev->ibdev.uverbs_cmd_mask = QEDR_UVERBS(GET_CONTEXT) |
247 				     QEDR_UVERBS(QUERY_DEVICE) |
248 				     QEDR_UVERBS(QUERY_PORT) |
249 				     QEDR_UVERBS(ALLOC_PD) |
250 				     QEDR_UVERBS(DEALLOC_PD) |
251 				     QEDR_UVERBS(CREATE_COMP_CHANNEL) |
252 				     QEDR_UVERBS(CREATE_CQ) |
253 				     QEDR_UVERBS(RESIZE_CQ) |
254 				     QEDR_UVERBS(DESTROY_CQ) |
255 				     QEDR_UVERBS(REQ_NOTIFY_CQ) |
256 				     QEDR_UVERBS(CREATE_QP) |
257 				     QEDR_UVERBS(MODIFY_QP) |
258 				     QEDR_UVERBS(QUERY_QP) |
259 				     QEDR_UVERBS(DESTROY_QP) |
260 				     QEDR_UVERBS(CREATE_SRQ) |
261 				     QEDR_UVERBS(DESTROY_SRQ) |
262 				     QEDR_UVERBS(QUERY_SRQ) |
263 				     QEDR_UVERBS(MODIFY_SRQ) |
264 				     QEDR_UVERBS(POST_SRQ_RECV) |
265 				     QEDR_UVERBS(REG_MR) |
266 				     QEDR_UVERBS(DEREG_MR) |
267 				     QEDR_UVERBS(POLL_CQ) |
268 				     QEDR_UVERBS(POST_SEND) |
269 				     QEDR_UVERBS(POST_RECV);
270 
271 	if (IS_IWARP(dev)) {
272 		rc = qedr_iw_register_device(dev);
273 		if (rc)
274 			return rc;
275 	} else {
276 		qedr_roce_register_device(dev);
277 	}
278 
279 	dev->ibdev.phys_port_cnt = 1;
280 	dev->ibdev.num_comp_vectors = dev->num_cnq;
281 	dev->ibdev.dev.parent = &dev->pdev->dev;
282 
283 	rdma_set_device_sysfs_group(&dev->ibdev, &qedr_attr_group);
284 	ib_set_device_ops(&dev->ibdev, &qedr_dev_ops);
285 
286 	rc = ib_device_set_netdev(&dev->ibdev, dev->ndev, 1);
287 	if (rc)
288 		return rc;
289 
290 	return ib_register_device(&dev->ibdev, "qedr%d");
291 }
292 
293 /* This function allocates fast-path status block memory */
294 static int qedr_alloc_mem_sb(struct qedr_dev *dev,
295 			     struct qed_sb_info *sb_info, u16 sb_id)
296 {
297 	struct status_block_e4 *sb_virt;
298 	dma_addr_t sb_phys;
299 	int rc;
300 
301 	sb_virt = dma_alloc_coherent(&dev->pdev->dev,
302 				     sizeof(*sb_virt), &sb_phys, GFP_KERNEL);
303 	if (!sb_virt)
304 		return -ENOMEM;
305 
306 	rc = dev->ops->common->sb_init(dev->cdev, sb_info,
307 				       sb_virt, sb_phys, sb_id,
308 				       QED_SB_TYPE_CNQ);
309 	if (rc) {
310 		pr_err("Status block initialization failed\n");
311 		dma_free_coherent(&dev->pdev->dev, sizeof(*sb_virt),
312 				  sb_virt, sb_phys);
313 		return rc;
314 	}
315 
316 	return 0;
317 }
318 
319 static void qedr_free_mem_sb(struct qedr_dev *dev,
320 			     struct qed_sb_info *sb_info, int sb_id)
321 {
322 	if (sb_info->sb_virt) {
323 		dev->ops->common->sb_release(dev->cdev, sb_info, sb_id,
324 					     QED_SB_TYPE_CNQ);
325 		dma_free_coherent(&dev->pdev->dev, sizeof(*sb_info->sb_virt),
326 				  (void *)sb_info->sb_virt, sb_info->sb_phys);
327 	}
328 }
329 
330 static void qedr_free_resources(struct qedr_dev *dev)
331 {
332 	int i;
333 
334 	if (IS_IWARP(dev))
335 		destroy_workqueue(dev->iwarp_wq);
336 
337 	for (i = 0; i < dev->num_cnq; i++) {
338 		qedr_free_mem_sb(dev, &dev->sb_array[i], dev->sb_start + i);
339 		dev->ops->common->chain_free(dev->cdev, &dev->cnq_array[i].pbl);
340 	}
341 
342 	kfree(dev->cnq_array);
343 	kfree(dev->sb_array);
344 	kfree(dev->sgid_tbl);
345 }
346 
347 static int qedr_alloc_resources(struct qedr_dev *dev)
348 {
349 	struct qedr_cnq *cnq;
350 	__le16 *cons_pi;
351 	u16 n_entries;
352 	int i, rc;
353 
354 	dev->sgid_tbl = kcalloc(QEDR_MAX_SGID, sizeof(union ib_gid),
355 				GFP_KERNEL);
356 	if (!dev->sgid_tbl)
357 		return -ENOMEM;
358 
359 	spin_lock_init(&dev->sgid_lock);
360 
361 	if (IS_IWARP(dev)) {
362 		xa_init_flags(&dev->qps, XA_FLAGS_LOCK_IRQ);
363 		dev->iwarp_wq = create_singlethread_workqueue("qedr_iwarpq");
364 	}
365 
366 	/* Allocate Status blocks for CNQ */
367 	dev->sb_array = kcalloc(dev->num_cnq, sizeof(*dev->sb_array),
368 				GFP_KERNEL);
369 	if (!dev->sb_array) {
370 		rc = -ENOMEM;
371 		goto err1;
372 	}
373 
374 	dev->cnq_array = kcalloc(dev->num_cnq,
375 				 sizeof(*dev->cnq_array), GFP_KERNEL);
376 	if (!dev->cnq_array) {
377 		rc = -ENOMEM;
378 		goto err2;
379 	}
380 
381 	dev->sb_start = dev->ops->rdma_get_start_sb(dev->cdev);
382 
383 	/* Allocate CNQ PBLs */
384 	n_entries = min_t(u32, QED_RDMA_MAX_CNQ_SIZE, QEDR_ROCE_MAX_CNQ_SIZE);
385 	for (i = 0; i < dev->num_cnq; i++) {
386 		cnq = &dev->cnq_array[i];
387 
388 		rc = qedr_alloc_mem_sb(dev, &dev->sb_array[i],
389 				       dev->sb_start + i);
390 		if (rc)
391 			goto err3;
392 
393 		rc = dev->ops->common->chain_alloc(dev->cdev,
394 						   QED_CHAIN_USE_TO_CONSUME,
395 						   QED_CHAIN_MODE_PBL,
396 						   QED_CHAIN_CNT_TYPE_U16,
397 						   n_entries,
398 						   sizeof(struct regpair *),
399 						   &cnq->pbl, NULL);
400 		if (rc)
401 			goto err4;
402 
403 		cnq->dev = dev;
404 		cnq->sb = &dev->sb_array[i];
405 		cons_pi = dev->sb_array[i].sb_virt->pi_array;
406 		cnq->hw_cons_ptr = &cons_pi[QED_ROCE_PROTOCOL_INDEX];
407 		cnq->index = i;
408 		sprintf(cnq->name, "qedr%d@pci:%s", i, pci_name(dev->pdev));
409 
410 		DP_DEBUG(dev, QEDR_MSG_INIT, "cnq[%d].cons=%d\n",
411 			 i, qed_chain_get_cons_idx(&cnq->pbl));
412 	}
413 
414 	return 0;
415 err4:
416 	qedr_free_mem_sb(dev, &dev->sb_array[i], dev->sb_start + i);
417 err3:
418 	for (--i; i >= 0; i--) {
419 		dev->ops->common->chain_free(dev->cdev, &dev->cnq_array[i].pbl);
420 		qedr_free_mem_sb(dev, &dev->sb_array[i], dev->sb_start + i);
421 	}
422 	kfree(dev->cnq_array);
423 err2:
424 	kfree(dev->sb_array);
425 err1:
426 	kfree(dev->sgid_tbl);
427 	return rc;
428 }
429 
430 static void qedr_pci_set_atomic(struct qedr_dev *dev, struct pci_dev *pdev)
431 {
432 	int rc = pci_enable_atomic_ops_to_root(pdev,
433 					       PCI_EXP_DEVCAP2_ATOMIC_COMP64);
434 
435 	if (rc) {
436 		dev->atomic_cap = IB_ATOMIC_NONE;
437 		DP_DEBUG(dev, QEDR_MSG_INIT, "Atomic capability disabled\n");
438 	} else {
439 		dev->atomic_cap = IB_ATOMIC_GLOB;
440 		DP_DEBUG(dev, QEDR_MSG_INIT, "Atomic capability enabled\n");
441 	}
442 }
443 
444 static const struct qed_rdma_ops *qed_ops;
445 
446 #define HILO_U64(hi, lo)		((((u64)(hi)) << 32) + (lo))
447 
448 static irqreturn_t qedr_irq_handler(int irq, void *handle)
449 {
450 	u16 hw_comp_cons, sw_comp_cons;
451 	struct qedr_cnq *cnq = handle;
452 	struct regpair *cq_handle;
453 	struct qedr_cq *cq;
454 
455 	qed_sb_ack(cnq->sb, IGU_INT_DISABLE, 0);
456 
457 	qed_sb_update_sb_idx(cnq->sb);
458 
459 	hw_comp_cons = le16_to_cpu(*cnq->hw_cons_ptr);
460 	sw_comp_cons = qed_chain_get_cons_idx(&cnq->pbl);
461 
462 	/* Align protocol-index and chain reads */
463 	rmb();
464 
465 	while (sw_comp_cons != hw_comp_cons) {
466 		cq_handle = (struct regpair *)qed_chain_consume(&cnq->pbl);
467 		cq = (struct qedr_cq *)(uintptr_t)HILO_U64(cq_handle->hi,
468 				cq_handle->lo);
469 
470 		if (cq == NULL) {
471 			DP_ERR(cnq->dev,
472 			       "Received NULL CQ cq_handle->hi=%d cq_handle->lo=%d sw_comp_cons=%d hw_comp_cons=%d\n",
473 			       cq_handle->hi, cq_handle->lo, sw_comp_cons,
474 			       hw_comp_cons);
475 
476 			break;
477 		}
478 
479 		if (cq->sig != QEDR_CQ_MAGIC_NUMBER) {
480 			DP_ERR(cnq->dev,
481 			       "Problem with cq signature, cq_handle->hi=%d ch_handle->lo=%d cq=%p\n",
482 			       cq_handle->hi, cq_handle->lo, cq);
483 			break;
484 		}
485 
486 		cq->arm_flags = 0;
487 
488 		if (!cq->destroyed && cq->ibcq.comp_handler)
489 			(*cq->ibcq.comp_handler)
490 				(&cq->ibcq, cq->ibcq.cq_context);
491 
492 		/* The CQ's CNQ notification counter is checked before
493 		 * destroying the CQ in a busy-wait loop that waits for all of
494 		 * the CQ's CNQ interrupts to be processed. It is increased
495 		 * here, only after the completion handler, to ensure that the
496 		 * the handler is not running when the CQ is destroyed.
497 		 */
498 		cq->cnq_notif++;
499 
500 		sw_comp_cons = qed_chain_get_cons_idx(&cnq->pbl);
501 
502 		cnq->n_comp++;
503 	}
504 
505 	qed_ops->rdma_cnq_prod_update(cnq->dev->rdma_ctx, cnq->index,
506 				      sw_comp_cons);
507 
508 	qed_sb_ack(cnq->sb, IGU_INT_ENABLE, 1);
509 
510 	return IRQ_HANDLED;
511 }
512 
513 static void qedr_sync_free_irqs(struct qedr_dev *dev)
514 {
515 	u32 vector;
516 	u16 idx;
517 	int i;
518 
519 	for (i = 0; i < dev->int_info.used_cnt; i++) {
520 		if (dev->int_info.msix_cnt) {
521 			idx = i * dev->num_hwfns + dev->affin_hwfn_idx;
522 			vector = dev->int_info.msix[idx].vector;
523 			synchronize_irq(vector);
524 			free_irq(vector, &dev->cnq_array[i]);
525 		}
526 	}
527 
528 	dev->int_info.used_cnt = 0;
529 }
530 
531 static int qedr_req_msix_irqs(struct qedr_dev *dev)
532 {
533 	int i, rc = 0;
534 	u16 idx;
535 
536 	if (dev->num_cnq > dev->int_info.msix_cnt) {
537 		DP_ERR(dev,
538 		       "Interrupt mismatch: %d CNQ queues > %d MSI-x vectors\n",
539 		       dev->num_cnq, dev->int_info.msix_cnt);
540 		return -EINVAL;
541 	}
542 
543 	for (i = 0; i < dev->num_cnq; i++) {
544 		idx = i * dev->num_hwfns + dev->affin_hwfn_idx;
545 		rc = request_irq(dev->int_info.msix[idx].vector,
546 				 qedr_irq_handler, 0, dev->cnq_array[i].name,
547 				 &dev->cnq_array[i]);
548 		if (rc) {
549 			DP_ERR(dev, "Request cnq %d irq failed\n", i);
550 			qedr_sync_free_irqs(dev);
551 		} else {
552 			DP_DEBUG(dev, QEDR_MSG_INIT,
553 				 "Requested cnq irq for %s [entry %d]. Cookie is at %p\n",
554 				 dev->cnq_array[i].name, i,
555 				 &dev->cnq_array[i]);
556 			dev->int_info.used_cnt++;
557 		}
558 	}
559 
560 	return rc;
561 }
562 
563 static int qedr_setup_irqs(struct qedr_dev *dev)
564 {
565 	int rc;
566 
567 	DP_DEBUG(dev, QEDR_MSG_INIT, "qedr_setup_irqs\n");
568 
569 	/* Learn Interrupt configuration */
570 	rc = dev->ops->rdma_set_rdma_int(dev->cdev, dev->num_cnq);
571 	if (rc < 0)
572 		return rc;
573 
574 	rc = dev->ops->rdma_get_rdma_int(dev->cdev, &dev->int_info);
575 	if (rc) {
576 		DP_DEBUG(dev, QEDR_MSG_INIT, "get_rdma_int failed\n");
577 		return rc;
578 	}
579 
580 	if (dev->int_info.msix_cnt) {
581 		DP_DEBUG(dev, QEDR_MSG_INIT, "rdma msix_cnt = %d\n",
582 			 dev->int_info.msix_cnt);
583 		rc = qedr_req_msix_irqs(dev);
584 		if (rc)
585 			return rc;
586 	}
587 
588 	DP_DEBUG(dev, QEDR_MSG_INIT, "qedr_setup_irqs succeeded\n");
589 
590 	return 0;
591 }
592 
593 static int qedr_set_device_attr(struct qedr_dev *dev)
594 {
595 	struct qed_rdma_device *qed_attr;
596 	struct qedr_device_attr *attr;
597 	u32 page_size;
598 
599 	/* Part 1 - query core capabilities */
600 	qed_attr = dev->ops->rdma_query_device(dev->rdma_ctx);
601 
602 	/* Part 2 - check capabilities */
603 	page_size = ~dev->attr.page_size_caps + 1;
604 	if (page_size > PAGE_SIZE) {
605 		DP_ERR(dev,
606 		       "Kernel PAGE_SIZE is %ld which is smaller than minimum page size (%d) required by qedr\n",
607 		       PAGE_SIZE, page_size);
608 		return -ENODEV;
609 	}
610 
611 	/* Part 3 - copy and update capabilities */
612 	attr = &dev->attr;
613 	attr->vendor_id = qed_attr->vendor_id;
614 	attr->vendor_part_id = qed_attr->vendor_part_id;
615 	attr->hw_ver = qed_attr->hw_ver;
616 	attr->fw_ver = qed_attr->fw_ver;
617 	attr->node_guid = qed_attr->node_guid;
618 	attr->sys_image_guid = qed_attr->sys_image_guid;
619 	attr->max_cnq = qed_attr->max_cnq;
620 	attr->max_sge = qed_attr->max_sge;
621 	attr->max_inline = qed_attr->max_inline;
622 	attr->max_sqe = min_t(u32, qed_attr->max_wqe, QEDR_MAX_SQE);
623 	attr->max_rqe = min_t(u32, qed_attr->max_wqe, QEDR_MAX_RQE);
624 	attr->max_qp_resp_rd_atomic_resc = qed_attr->max_qp_resp_rd_atomic_resc;
625 	attr->max_qp_req_rd_atomic_resc = qed_attr->max_qp_req_rd_atomic_resc;
626 	attr->max_dev_resp_rd_atomic_resc =
627 	    qed_attr->max_dev_resp_rd_atomic_resc;
628 	attr->max_cq = qed_attr->max_cq;
629 	attr->max_qp = qed_attr->max_qp;
630 	attr->max_mr = qed_attr->max_mr;
631 	attr->max_mr_size = qed_attr->max_mr_size;
632 	attr->max_cqe = min_t(u64, qed_attr->max_cqe, QEDR_MAX_CQES);
633 	attr->max_mw = qed_attr->max_mw;
634 	attr->max_fmr = qed_attr->max_fmr;
635 	attr->max_mr_mw_fmr_pbl = qed_attr->max_mr_mw_fmr_pbl;
636 	attr->max_mr_mw_fmr_size = qed_attr->max_mr_mw_fmr_size;
637 	attr->max_pd = qed_attr->max_pd;
638 	attr->max_ah = qed_attr->max_ah;
639 	attr->max_pkey = qed_attr->max_pkey;
640 	attr->max_srq = qed_attr->max_srq;
641 	attr->max_srq_wr = qed_attr->max_srq_wr;
642 	attr->dev_caps = qed_attr->dev_caps;
643 	attr->page_size_caps = qed_attr->page_size_caps;
644 	attr->dev_ack_delay = qed_attr->dev_ack_delay;
645 	attr->reserved_lkey = qed_attr->reserved_lkey;
646 	attr->bad_pkey_counter = qed_attr->bad_pkey_counter;
647 	attr->max_stats_queues = qed_attr->max_stats_queues;
648 
649 	return 0;
650 }
651 
652 static void qedr_unaffiliated_event(void *context, u8 event_code)
653 {
654 	pr_err("unaffiliated event not implemented yet\n");
655 }
656 
657 static void qedr_affiliated_event(void *context, u8 e_code, void *fw_handle)
658 {
659 #define EVENT_TYPE_NOT_DEFINED	0
660 #define EVENT_TYPE_CQ		1
661 #define EVENT_TYPE_QP		2
662 #define EVENT_TYPE_SRQ		3
663 	struct qedr_dev *dev = (struct qedr_dev *)context;
664 	struct regpair *async_handle = (struct regpair *)fw_handle;
665 	u64 roce_handle64 = ((u64) async_handle->hi << 32) + async_handle->lo;
666 	u8 event_type = EVENT_TYPE_NOT_DEFINED;
667 	struct ib_event event;
668 	struct ib_srq *ibsrq;
669 	struct qedr_srq *srq;
670 	unsigned long flags;
671 	struct ib_cq *ibcq;
672 	struct ib_qp *ibqp;
673 	struct qedr_cq *cq;
674 	struct qedr_qp *qp;
675 	u16 srq_id;
676 
677 	if (IS_ROCE(dev)) {
678 		switch (e_code) {
679 		case ROCE_ASYNC_EVENT_CQ_OVERFLOW_ERR:
680 			event.event = IB_EVENT_CQ_ERR;
681 			event_type = EVENT_TYPE_CQ;
682 			break;
683 		case ROCE_ASYNC_EVENT_SQ_DRAINED:
684 			event.event = IB_EVENT_SQ_DRAINED;
685 			event_type = EVENT_TYPE_QP;
686 			break;
687 		case ROCE_ASYNC_EVENT_QP_CATASTROPHIC_ERR:
688 			event.event = IB_EVENT_QP_FATAL;
689 			event_type = EVENT_TYPE_QP;
690 			break;
691 		case ROCE_ASYNC_EVENT_LOCAL_INVALID_REQUEST_ERR:
692 			event.event = IB_EVENT_QP_REQ_ERR;
693 			event_type = EVENT_TYPE_QP;
694 			break;
695 		case ROCE_ASYNC_EVENT_LOCAL_ACCESS_ERR:
696 			event.event = IB_EVENT_QP_ACCESS_ERR;
697 			event_type = EVENT_TYPE_QP;
698 			break;
699 		case ROCE_ASYNC_EVENT_SRQ_LIMIT:
700 			event.event = IB_EVENT_SRQ_LIMIT_REACHED;
701 			event_type = EVENT_TYPE_SRQ;
702 			break;
703 		case ROCE_ASYNC_EVENT_SRQ_EMPTY:
704 			event.event = IB_EVENT_SRQ_ERR;
705 			event_type = EVENT_TYPE_SRQ;
706 			break;
707 		default:
708 			DP_ERR(dev, "unsupported event %d on handle=%llx\n",
709 			       e_code, roce_handle64);
710 		}
711 	} else {
712 		switch (e_code) {
713 		case QED_IWARP_EVENT_SRQ_LIMIT:
714 			event.event = IB_EVENT_SRQ_LIMIT_REACHED;
715 			event_type = EVENT_TYPE_SRQ;
716 			break;
717 		case QED_IWARP_EVENT_SRQ_EMPTY:
718 			event.event = IB_EVENT_SRQ_ERR;
719 			event_type = EVENT_TYPE_SRQ;
720 			break;
721 		default:
722 		DP_ERR(dev, "unsupported event %d on handle=%llx\n", e_code,
723 		       roce_handle64);
724 		}
725 	}
726 	switch (event_type) {
727 	case EVENT_TYPE_CQ:
728 		cq = (struct qedr_cq *)(uintptr_t)roce_handle64;
729 		if (cq) {
730 			ibcq = &cq->ibcq;
731 			if (ibcq->event_handler) {
732 				event.device = ibcq->device;
733 				event.element.cq = ibcq;
734 				ibcq->event_handler(&event, ibcq->cq_context);
735 			}
736 		} else {
737 			WARN(1,
738 			     "Error: CQ event with NULL pointer ibcq. Handle=%llx\n",
739 			     roce_handle64);
740 		}
741 		DP_ERR(dev, "CQ event %d on handle %p\n", e_code, cq);
742 		break;
743 	case EVENT_TYPE_QP:
744 		qp = (struct qedr_qp *)(uintptr_t)roce_handle64;
745 		if (qp) {
746 			ibqp = &qp->ibqp;
747 			if (ibqp->event_handler) {
748 				event.device = ibqp->device;
749 				event.element.qp = ibqp;
750 				ibqp->event_handler(&event, ibqp->qp_context);
751 			}
752 		} else {
753 			WARN(1,
754 			     "Error: QP event with NULL pointer ibqp. Handle=%llx\n",
755 			     roce_handle64);
756 		}
757 		DP_ERR(dev, "QP event %d on handle %p\n", e_code, qp);
758 		break;
759 	case EVENT_TYPE_SRQ:
760 		srq_id = (u16)roce_handle64;
761 		xa_lock_irqsave(&dev->srqs, flags);
762 		srq = xa_load(&dev->srqs, srq_id);
763 		if (srq) {
764 			ibsrq = &srq->ibsrq;
765 			if (ibsrq->event_handler) {
766 				event.device = ibsrq->device;
767 				event.element.srq = ibsrq;
768 				ibsrq->event_handler(&event,
769 						     ibsrq->srq_context);
770 			}
771 		} else {
772 			DP_NOTICE(dev,
773 				  "SRQ event with NULL pointer ibsrq. Handle=%llx\n",
774 				  roce_handle64);
775 		}
776 		xa_unlock_irqrestore(&dev->srqs, flags);
777 		DP_NOTICE(dev, "SRQ event %d on handle %p\n", e_code, srq);
778 	default:
779 		break;
780 	}
781 }
782 
783 static int qedr_init_hw(struct qedr_dev *dev)
784 {
785 	struct qed_rdma_add_user_out_params out_params;
786 	struct qed_rdma_start_in_params *in_params;
787 	struct qed_rdma_cnq_params *cur_pbl;
788 	struct qed_rdma_events events;
789 	dma_addr_t p_phys_table;
790 	u32 page_cnt;
791 	int rc = 0;
792 	int i;
793 
794 	in_params =  kzalloc(sizeof(*in_params), GFP_KERNEL);
795 	if (!in_params) {
796 		rc = -ENOMEM;
797 		goto out;
798 	}
799 
800 	in_params->desired_cnq = dev->num_cnq;
801 	for (i = 0; i < dev->num_cnq; i++) {
802 		cur_pbl = &in_params->cnq_pbl_list[i];
803 
804 		page_cnt = qed_chain_get_page_cnt(&dev->cnq_array[i].pbl);
805 		cur_pbl->num_pbl_pages = page_cnt;
806 
807 		p_phys_table = qed_chain_get_pbl_phys(&dev->cnq_array[i].pbl);
808 		cur_pbl->pbl_ptr = (u64)p_phys_table;
809 	}
810 
811 	events.affiliated_event = qedr_affiliated_event;
812 	events.unaffiliated_event = qedr_unaffiliated_event;
813 	events.context = dev;
814 
815 	in_params->events = &events;
816 	in_params->cq_mode = QED_RDMA_CQ_MODE_32_BITS;
817 	in_params->max_mtu = dev->ndev->mtu;
818 	dev->iwarp_max_mtu = dev->ndev->mtu;
819 	ether_addr_copy(&in_params->mac_addr[0], dev->ndev->dev_addr);
820 
821 	rc = dev->ops->rdma_init(dev->cdev, in_params);
822 	if (rc)
823 		goto out;
824 
825 	rc = dev->ops->rdma_add_user(dev->rdma_ctx, &out_params);
826 	if (rc)
827 		goto out;
828 
829 	dev->db_addr = out_params.dpi_addr;
830 	dev->db_phys_addr = out_params.dpi_phys_addr;
831 	dev->db_size = out_params.dpi_size;
832 	dev->dpi = out_params.dpi;
833 
834 	rc = qedr_set_device_attr(dev);
835 out:
836 	kfree(in_params);
837 	if (rc)
838 		DP_ERR(dev, "Init HW Failed rc = %d\n", rc);
839 
840 	return rc;
841 }
842 
843 static void qedr_stop_hw(struct qedr_dev *dev)
844 {
845 	dev->ops->rdma_remove_user(dev->rdma_ctx, dev->dpi);
846 	dev->ops->rdma_stop(dev->rdma_ctx);
847 }
848 
849 static struct qedr_dev *qedr_add(struct qed_dev *cdev, struct pci_dev *pdev,
850 				 struct net_device *ndev)
851 {
852 	struct qed_dev_rdma_info dev_info;
853 	struct qedr_dev *dev;
854 	int rc = 0;
855 
856 	dev = ib_alloc_device(qedr_dev, ibdev);
857 	if (!dev) {
858 		pr_err("Unable to allocate ib device\n");
859 		return NULL;
860 	}
861 
862 	DP_DEBUG(dev, QEDR_MSG_INIT, "qedr add device called\n");
863 
864 	dev->pdev = pdev;
865 	dev->ndev = ndev;
866 	dev->cdev = cdev;
867 
868 	qed_ops = qed_get_rdma_ops();
869 	if (!qed_ops) {
870 		DP_ERR(dev, "Failed to get qed roce operations\n");
871 		goto init_err;
872 	}
873 
874 	dev->ops = qed_ops;
875 	rc = qed_ops->fill_dev_info(cdev, &dev_info);
876 	if (rc)
877 		goto init_err;
878 
879 	dev->user_dpm_enabled = dev_info.user_dpm_enabled;
880 	dev->rdma_type = dev_info.rdma_type;
881 	dev->num_hwfns = dev_info.common.num_hwfns;
882 
883 	if (IS_IWARP(dev) && QEDR_IS_CMT(dev)) {
884 		rc = dev->ops->iwarp_set_engine_affin(cdev, false);
885 		if (rc) {
886 			DP_ERR(dev, "iWARP is disabled over a 100g device Enabling it may impact L2 performance. To enable it run devlink dev param set <dev> name iwarp_cmt value true cmode runtime\n");
887 			goto init_err;
888 		}
889 	}
890 	dev->affin_hwfn_idx = dev->ops->common->get_affin_hwfn_idx(cdev);
891 
892 	dev->rdma_ctx = dev->ops->rdma_get_rdma_ctx(cdev);
893 
894 	dev->num_cnq = dev->ops->rdma_get_min_cnq_msix(cdev);
895 	if (!dev->num_cnq) {
896 		DP_ERR(dev, "Failed. At least one CNQ is required.\n");
897 		rc = -ENOMEM;
898 		goto init_err;
899 	}
900 
901 	dev->wq_multiplier = QEDR_WQ_MULTIPLIER_DFT;
902 
903 	qedr_pci_set_atomic(dev, pdev);
904 
905 	rc = qedr_alloc_resources(dev);
906 	if (rc)
907 		goto init_err;
908 
909 	rc = qedr_init_hw(dev);
910 	if (rc)
911 		goto alloc_err;
912 
913 	rc = qedr_setup_irqs(dev);
914 	if (rc)
915 		goto irq_err;
916 
917 	rc = qedr_register_device(dev);
918 	if (rc) {
919 		DP_ERR(dev, "Unable to allocate register device\n");
920 		goto reg_err;
921 	}
922 
923 	if (!test_and_set_bit(QEDR_ENET_STATE_BIT, &dev->enet_state))
924 		qedr_ib_dispatch_event(dev, QEDR_PORT, IB_EVENT_PORT_ACTIVE);
925 
926 	DP_DEBUG(dev, QEDR_MSG_INIT, "qedr driver loaded successfully\n");
927 	return dev;
928 
929 reg_err:
930 	qedr_sync_free_irqs(dev);
931 irq_err:
932 	qedr_stop_hw(dev);
933 alloc_err:
934 	qedr_free_resources(dev);
935 init_err:
936 	ib_dealloc_device(&dev->ibdev);
937 	DP_ERR(dev, "qedr driver load failed rc=%d\n", rc);
938 
939 	return NULL;
940 }
941 
942 static void qedr_remove(struct qedr_dev *dev)
943 {
944 	/* First unregister with stack to stop all the active traffic
945 	 * of the registered clients.
946 	 */
947 	ib_unregister_device(&dev->ibdev);
948 
949 	qedr_stop_hw(dev);
950 	qedr_sync_free_irqs(dev);
951 	qedr_free_resources(dev);
952 
953 	if (IS_IWARP(dev) && QEDR_IS_CMT(dev))
954 		dev->ops->iwarp_set_engine_affin(dev->cdev, true);
955 
956 	ib_dealloc_device(&dev->ibdev);
957 }
958 
959 static void qedr_close(struct qedr_dev *dev)
960 {
961 	if (test_and_clear_bit(QEDR_ENET_STATE_BIT, &dev->enet_state))
962 		qedr_ib_dispatch_event(dev, QEDR_PORT, IB_EVENT_PORT_ERR);
963 }
964 
965 static void qedr_shutdown(struct qedr_dev *dev)
966 {
967 	qedr_close(dev);
968 	qedr_remove(dev);
969 }
970 
971 static void qedr_open(struct qedr_dev *dev)
972 {
973 	if (!test_and_set_bit(QEDR_ENET_STATE_BIT, &dev->enet_state))
974 		qedr_ib_dispatch_event(dev, QEDR_PORT, IB_EVENT_PORT_ACTIVE);
975 }
976 
977 static void qedr_mac_address_change(struct qedr_dev *dev)
978 {
979 	union ib_gid *sgid = &dev->sgid_tbl[0];
980 	u8 guid[8], mac_addr[6];
981 	int rc;
982 
983 	/* Update SGID */
984 	ether_addr_copy(&mac_addr[0], dev->ndev->dev_addr);
985 	guid[0] = mac_addr[0] ^ 2;
986 	guid[1] = mac_addr[1];
987 	guid[2] = mac_addr[2];
988 	guid[3] = 0xff;
989 	guid[4] = 0xfe;
990 	guid[5] = mac_addr[3];
991 	guid[6] = mac_addr[4];
992 	guid[7] = mac_addr[5];
993 	sgid->global.subnet_prefix = cpu_to_be64(0xfe80000000000000LL);
994 	memcpy(&sgid->raw[8], guid, sizeof(guid));
995 
996 	/* Update LL2 */
997 	rc = dev->ops->ll2_set_mac_filter(dev->cdev,
998 					  dev->gsi_ll2_mac_address,
999 					  dev->ndev->dev_addr);
1000 
1001 	ether_addr_copy(dev->gsi_ll2_mac_address, dev->ndev->dev_addr);
1002 
1003 	qedr_ib_dispatch_event(dev, QEDR_PORT, IB_EVENT_GID_CHANGE);
1004 
1005 	if (rc)
1006 		DP_ERR(dev, "Error updating mac filter\n");
1007 }
1008 
1009 /* event handling via NIC driver ensures that all the NIC specific
1010  * initialization done before RoCE driver notifies
1011  * event to stack.
1012  */
1013 static void qedr_notify(struct qedr_dev *dev, enum qede_rdma_event event)
1014 {
1015 	switch (event) {
1016 	case QEDE_UP:
1017 		qedr_open(dev);
1018 		break;
1019 	case QEDE_DOWN:
1020 		qedr_close(dev);
1021 		break;
1022 	case QEDE_CLOSE:
1023 		qedr_shutdown(dev);
1024 		break;
1025 	case QEDE_CHANGE_ADDR:
1026 		qedr_mac_address_change(dev);
1027 		break;
1028 	default:
1029 		pr_err("Event not supported\n");
1030 	}
1031 }
1032 
1033 static struct qedr_driver qedr_drv = {
1034 	.name = "qedr_driver",
1035 	.add = qedr_add,
1036 	.remove = qedr_remove,
1037 	.notify = qedr_notify,
1038 };
1039 
1040 static int __init qedr_init_module(void)
1041 {
1042 	return qede_rdma_register_driver(&qedr_drv);
1043 }
1044 
1045 static void __exit qedr_exit_module(void)
1046 {
1047 	qede_rdma_unregister_driver(&qedr_drv);
1048 }
1049 
1050 module_init(qedr_init_module);
1051 module_exit(qedr_exit_module);
1052