blob: 4c53b857cc1ce2bf19e70dca883cc1a6774b0605 [file] [log] [blame]
Ram Amrani51ff1722016-10-01 21:59:57 +03001/* QLogic qed 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/types.h>
33#include <asm/byteorder.h>
34#include <linux/bitops.h>
35#include <linux/delay.h>
36#include <linux/dma-mapping.h>
37#include <linux/errno.h>
38#include <linux/etherdevice.h>
39#include <linux/if_ether.h>
40#include <linux/if_vlan.h>
41#include <linux/io.h>
42#include <linux/ip.h>
43#include <linux/ipv6.h>
44#include <linux/kernel.h>
45#include <linux/list.h>
46#include <linux/module.h>
47#include <linux/mutex.h>
48#include <linux/pci.h>
49#include <linux/slab.h>
50#include <linux/spinlock.h>
51#include <linux/string.h>
52#include <linux/tcp.h>
53#include <linux/bitops.h>
54#include <linux/qed/qed_roce_if.h>
55#include <linux/qed/qed_roce_if.h>
56#include "qed.h"
57#include "qed_cxt.h"
58#include "qed_hsi.h"
59#include "qed_hw.h"
60#include "qed_init_ops.h"
61#include "qed_int.h"
62#include "qed_ll2.h"
63#include "qed_mcp.h"
64#include "qed_reg_addr.h"
65#include "qed_sp.h"
66#include "qed_roce.h"
67
68void qed_async_roce_event(struct qed_hwfn *p_hwfn,
69 struct event_ring_entry *p_eqe)
70{
71 struct qed_rdma_info *p_rdma_info = p_hwfn->p_rdma_info;
72
73 p_rdma_info->events.affiliated_event(p_rdma_info->events.context,
74 p_eqe->opcode, &p_eqe->data);
75}
76
77static int qed_rdma_bmap_alloc(struct qed_hwfn *p_hwfn,
78 struct qed_bmap *bmap, u32 max_count)
79{
80 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "max_count = %08x\n", max_count);
81
82 bmap->max_count = max_count;
83
84 bmap->bitmap = kzalloc(BITS_TO_LONGS(max_count) * sizeof(long),
85 GFP_KERNEL);
86 if (!bmap->bitmap) {
87 DP_NOTICE(p_hwfn,
88 "qed bmap alloc failed: cannot allocate memory (bitmap)\n");
89 return -ENOMEM;
90 }
91
92 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Allocated bitmap %p\n",
93 bmap->bitmap);
94 return 0;
95}
96
97static int qed_rdma_bmap_alloc_id(struct qed_hwfn *p_hwfn,
98 struct qed_bmap *bmap, u32 *id_num)
99{
100 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "bmap = %p\n", bmap);
101
102 *id_num = find_first_zero_bit(bmap->bitmap, bmap->max_count);
103
104 if (*id_num >= bmap->max_count) {
105 DP_NOTICE(p_hwfn, "no id available max_count=%d\n",
106 bmap->max_count);
107 return -EINVAL;
108 }
109
110 __set_bit(*id_num, bmap->bitmap);
111
112 return 0;
113}
114
115static void qed_bmap_release_id(struct qed_hwfn *p_hwfn,
116 struct qed_bmap *bmap, u32 id_num)
117{
118 bool b_acquired;
119
120 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "id_num = %08x", id_num);
121 if (id_num >= bmap->max_count)
122 return;
123
124 b_acquired = test_and_clear_bit(id_num, bmap->bitmap);
125 if (!b_acquired) {
126 DP_NOTICE(p_hwfn, "ID %d already released\n", id_num);
127 return;
128 }
129}
130
131u32 qed_rdma_get_sb_id(void *p_hwfn, u32 rel_sb_id)
132{
133 /* First sb id for RoCE is after all the l2 sb */
134 return FEAT_NUM((struct qed_hwfn *)p_hwfn, QED_PF_L2_QUE) + rel_sb_id;
135}
136
137u32 qed_rdma_query_cau_timer_res(void *rdma_cxt)
138{
139 return QED_CAU_DEF_RX_TIMER_RES;
140}
141
142static int qed_rdma_alloc(struct qed_hwfn *p_hwfn,
143 struct qed_ptt *p_ptt,
144 struct qed_rdma_start_in_params *params)
145{
146 struct qed_rdma_info *p_rdma_info;
147 u32 num_cons, num_tasks;
148 int rc = -ENOMEM;
149
150 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Allocating RDMA\n");
151
152 /* Allocate a struct with current pf rdma info */
153 p_rdma_info = kzalloc(sizeof(*p_rdma_info), GFP_KERNEL);
154 if (!p_rdma_info) {
155 DP_NOTICE(p_hwfn,
156 "qed rdma alloc failed: cannot allocate memory (rdma info). rc = %d\n",
157 rc);
158 return rc;
159 }
160
161 p_hwfn->p_rdma_info = p_rdma_info;
162 p_rdma_info->proto = PROTOCOLID_ROCE;
163
164 num_cons = qed_cxt_get_proto_cid_count(p_hwfn, p_rdma_info->proto, 0);
165
166 p_rdma_info->num_qps = num_cons / 2;
167
168 num_tasks = qed_cxt_get_proto_tid_count(p_hwfn, PROTOCOLID_ROCE);
169
170 /* Each MR uses a single task */
171 p_rdma_info->num_mrs = num_tasks;
172
173 /* Queue zone lines are shared between RoCE and L2 in such a way that
174 * they can be used by each without obstructing the other.
175 */
176 p_rdma_info->queue_zone_base = (u16)FEAT_NUM(p_hwfn, QED_L2_QUEUE);
177
178 /* Allocate a struct with device params and fill it */
179 p_rdma_info->dev = kzalloc(sizeof(*p_rdma_info->dev), GFP_KERNEL);
180 if (!p_rdma_info->dev) {
181 DP_NOTICE(p_hwfn,
182 "qed rdma alloc failed: cannot allocate memory (rdma info dev). rc = %d\n",
183 rc);
184 goto free_rdma_info;
185 }
186
187 /* Allocate a struct with port params and fill it */
188 p_rdma_info->port = kzalloc(sizeof(*p_rdma_info->port), GFP_KERNEL);
189 if (!p_rdma_info->port) {
190 DP_NOTICE(p_hwfn,
191 "qed rdma alloc failed: cannot allocate memory (rdma info port). rc = %d\n",
192 rc);
193 goto free_rdma_dev;
194 }
195
196 /* Allocate bit map for pd's */
197 rc = qed_rdma_bmap_alloc(p_hwfn, &p_rdma_info->pd_map, RDMA_MAX_PDS);
198 if (rc) {
199 DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
200 "Failed to allocate pd_map, rc = %d\n",
201 rc);
202 goto free_rdma_port;
203 }
204
205 /* Allocate DPI bitmap */
206 rc = qed_rdma_bmap_alloc(p_hwfn, &p_rdma_info->dpi_map,
207 p_hwfn->dpi_count);
208 if (rc) {
209 DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
210 "Failed to allocate DPI bitmap, rc = %d\n", rc);
211 goto free_pd_map;
212 }
213
214 /* Allocate bitmap for cq's. The maximum number of CQs is bounded to
215 * twice the number of QPs.
216 */
217 rc = qed_rdma_bmap_alloc(p_hwfn, &p_rdma_info->cq_map,
218 p_rdma_info->num_qps * 2);
219 if (rc) {
220 DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
221 "Failed to allocate cq bitmap, rc = %d\n", rc);
222 goto free_dpi_map;
223 }
224
225 /* Allocate bitmap for toggle bit for cq icids
226 * We toggle the bit every time we create or resize cq for a given icid.
227 * The maximum number of CQs is bounded to twice the number of QPs.
228 */
229 rc = qed_rdma_bmap_alloc(p_hwfn, &p_rdma_info->toggle_bits,
230 p_rdma_info->num_qps * 2);
231 if (rc) {
232 DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
233 "Failed to allocate toogle bits, rc = %d\n", rc);
234 goto free_cq_map;
235 }
236
237 /* Allocate bitmap for itids */
238 rc = qed_rdma_bmap_alloc(p_hwfn, &p_rdma_info->tid_map,
239 p_rdma_info->num_mrs);
240 if (rc) {
241 DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
242 "Failed to allocate itids bitmaps, rc = %d\n", rc);
243 goto free_toggle_map;
244 }
245
246 /* Allocate bitmap for cids used for qps. */
247 rc = qed_rdma_bmap_alloc(p_hwfn, &p_rdma_info->cid_map, num_cons);
248 if (rc) {
249 DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
250 "Failed to allocate cid bitmap, rc = %d\n", rc);
251 goto free_tid_map;
252 }
253
254 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Allocation successful\n");
255 return 0;
256
257free_tid_map:
258 kfree(p_rdma_info->tid_map.bitmap);
259free_toggle_map:
260 kfree(p_rdma_info->toggle_bits.bitmap);
261free_cq_map:
262 kfree(p_rdma_info->cq_map.bitmap);
263free_dpi_map:
264 kfree(p_rdma_info->dpi_map.bitmap);
265free_pd_map:
266 kfree(p_rdma_info->pd_map.bitmap);
267free_rdma_port:
268 kfree(p_rdma_info->port);
269free_rdma_dev:
270 kfree(p_rdma_info->dev);
271free_rdma_info:
272 kfree(p_rdma_info);
273
274 return rc;
275}
276
277void qed_rdma_resc_free(struct qed_hwfn *p_hwfn)
278{
279 struct qed_rdma_info *p_rdma_info = p_hwfn->p_rdma_info;
280
281 kfree(p_rdma_info->cid_map.bitmap);
282 kfree(p_rdma_info->tid_map.bitmap);
283 kfree(p_rdma_info->toggle_bits.bitmap);
284 kfree(p_rdma_info->cq_map.bitmap);
285 kfree(p_rdma_info->dpi_map.bitmap);
286 kfree(p_rdma_info->pd_map.bitmap);
287
288 kfree(p_rdma_info->port);
289 kfree(p_rdma_info->dev);
290
291 kfree(p_rdma_info);
292}
293
294static void qed_rdma_free(struct qed_hwfn *p_hwfn)
295{
296 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Freeing RDMA\n");
297
298 qed_rdma_resc_free(p_hwfn);
299}
300
301static void qed_rdma_get_guid(struct qed_hwfn *p_hwfn, u8 *guid)
302{
303 guid[0] = p_hwfn->hw_info.hw_mac_addr[0] ^ 2;
304 guid[1] = p_hwfn->hw_info.hw_mac_addr[1];
305 guid[2] = p_hwfn->hw_info.hw_mac_addr[2];
306 guid[3] = 0xff;
307 guid[4] = 0xfe;
308 guid[5] = p_hwfn->hw_info.hw_mac_addr[3];
309 guid[6] = p_hwfn->hw_info.hw_mac_addr[4];
310 guid[7] = p_hwfn->hw_info.hw_mac_addr[5];
311}
312
313static void qed_rdma_init_events(struct qed_hwfn *p_hwfn,
314 struct qed_rdma_start_in_params *params)
315{
316 struct qed_rdma_events *events;
317
318 events = &p_hwfn->p_rdma_info->events;
319
320 events->unaffiliated_event = params->events->unaffiliated_event;
321 events->affiliated_event = params->events->affiliated_event;
322 events->context = params->events->context;
323}
324
325static void qed_rdma_init_devinfo(struct qed_hwfn *p_hwfn,
326 struct qed_rdma_start_in_params *params)
327{
328 struct qed_rdma_device *dev = p_hwfn->p_rdma_info->dev;
329 struct qed_dev *cdev = p_hwfn->cdev;
330 u32 pci_status_control;
331 u32 num_qps;
332
333 /* Vendor specific information */
334 dev->vendor_id = cdev->vendor_id;
335 dev->vendor_part_id = cdev->device_id;
336 dev->hw_ver = 0;
337 dev->fw_ver = (FW_MAJOR_VERSION << 24) | (FW_MINOR_VERSION << 16) |
338 (FW_REVISION_VERSION << 8) | (FW_ENGINEERING_VERSION);
339
340 qed_rdma_get_guid(p_hwfn, (u8 *)&dev->sys_image_guid);
341 dev->node_guid = dev->sys_image_guid;
342
343 dev->max_sge = min_t(u32, RDMA_MAX_SGE_PER_SQ_WQE,
344 RDMA_MAX_SGE_PER_RQ_WQE);
345
346 if (cdev->rdma_max_sge)
347 dev->max_sge = min_t(u32, cdev->rdma_max_sge, dev->max_sge);
348
349 dev->max_inline = ROCE_REQ_MAX_INLINE_DATA_SIZE;
350
351 dev->max_inline = (cdev->rdma_max_inline) ?
352 min_t(u32, cdev->rdma_max_inline, dev->max_inline) :
353 dev->max_inline;
354
355 dev->max_wqe = QED_RDMA_MAX_WQE;
356 dev->max_cnq = (u8)FEAT_NUM(p_hwfn, QED_RDMA_CNQ);
357
358 /* The number of QPs may be higher than QED_ROCE_MAX_QPS, because
359 * it is up-aligned to 16 and then to ILT page size within qed cxt.
360 * This is OK in terms of ILT but we don't want to configure the FW
361 * above its abilities
362 */
363 num_qps = ROCE_MAX_QPS;
364 num_qps = min_t(u64, num_qps, p_hwfn->p_rdma_info->num_qps);
365 dev->max_qp = num_qps;
366
367 /* CQs uses the same icids that QPs use hence they are limited by the
368 * number of icids. There are two icids per QP.
369 */
370 dev->max_cq = num_qps * 2;
371
372 /* The number of mrs is smaller by 1 since the first is reserved */
373 dev->max_mr = p_hwfn->p_rdma_info->num_mrs - 1;
374 dev->max_mr_size = QED_RDMA_MAX_MR_SIZE;
375
376 /* The maximum CQE capacity per CQ supported.
377 * max number of cqes will be in two layer pbl,
378 * 8 is the pointer size in bytes
379 * 32 is the size of cq element in bytes
380 */
381 if (params->cq_mode == QED_RDMA_CQ_MODE_32_BITS)
382 dev->max_cqe = QED_RDMA_MAX_CQE_32_BIT;
383 else
384 dev->max_cqe = QED_RDMA_MAX_CQE_16_BIT;
385
386 dev->max_mw = 0;
387 dev->max_fmr = QED_RDMA_MAX_FMR;
388 dev->max_mr_mw_fmr_pbl = (PAGE_SIZE / 8) * (PAGE_SIZE / 8);
389 dev->max_mr_mw_fmr_size = dev->max_mr_mw_fmr_pbl * PAGE_SIZE;
390 dev->max_pkey = QED_RDMA_MAX_P_KEY;
391
392 dev->max_qp_resp_rd_atomic_resc = RDMA_RING_PAGE_SIZE /
393 (RDMA_RESP_RD_ATOMIC_ELM_SIZE * 2);
394 dev->max_qp_req_rd_atomic_resc = RDMA_RING_PAGE_SIZE /
395 RDMA_REQ_RD_ATOMIC_ELM_SIZE;
396 dev->max_dev_resp_rd_atomic_resc = dev->max_qp_resp_rd_atomic_resc *
397 p_hwfn->p_rdma_info->num_qps;
398 dev->page_size_caps = QED_RDMA_PAGE_SIZE_CAPS;
399 dev->dev_ack_delay = QED_RDMA_ACK_DELAY;
400 dev->max_pd = RDMA_MAX_PDS;
401 dev->max_ah = p_hwfn->p_rdma_info->num_qps;
402 dev->max_stats_queues = (u8)RESC_NUM(p_hwfn, QED_RDMA_STATS_QUEUE);
403
404 /* Set capablities */
405 dev->dev_caps = 0;
406 SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_RNR_NAK, 1);
407 SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_PORT_ACTIVE_EVENT, 1);
408 SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_PORT_CHANGE_EVENT, 1);
409 SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_RESIZE_CQ, 1);
410 SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_BASE_MEMORY_EXT, 1);
411 SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_BASE_QUEUE_EXT, 1);
412 SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_ZBVA, 1);
413 SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_LOCAL_INV_FENCE, 1);
414
415 /* Check atomic operations support in PCI configuration space. */
416 pci_read_config_dword(cdev->pdev,
417 cdev->pdev->pcie_cap + PCI_EXP_DEVCTL2,
418 &pci_status_control);
419
420 if (pci_status_control & PCI_EXP_DEVCTL2_LTR_EN)
421 SET_FIELD(dev->dev_caps, QED_RDMA_DEV_CAP_ATOMIC_OP, 1);
422}
423
424static void qed_rdma_init_port(struct qed_hwfn *p_hwfn)
425{
426 struct qed_rdma_port *port = p_hwfn->p_rdma_info->port;
427 struct qed_rdma_device *dev = p_hwfn->p_rdma_info->dev;
428
429 port->port_state = p_hwfn->mcp_info->link_output.link_up ?
430 QED_RDMA_PORT_UP : QED_RDMA_PORT_DOWN;
431
432 port->max_msg_size = min_t(u64,
433 (dev->max_mr_mw_fmr_size *
434 p_hwfn->cdev->rdma_max_sge),
435 BIT(31));
436
437 port->pkey_bad_counter = 0;
438}
439
440static int qed_rdma_init_hw(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
441{
442 u32 ll2_ethertype_en;
443
444 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Initializing HW\n");
445 p_hwfn->b_rdma_enabled_in_prs = false;
446
447 qed_wr(p_hwfn, p_ptt, PRS_REG_ROCE_DEST_QP_MAX_PF, 0);
448
449 p_hwfn->rdma_prs_search_reg = PRS_REG_SEARCH_ROCE;
450
451 /* We delay writing to this reg until first cid is allocated. See
452 * qed_cxt_dynamic_ilt_alloc function for more details
453 */
454 ll2_ethertype_en = qed_rd(p_hwfn, p_ptt, PRS_REG_LIGHT_L2_ETHERTYPE_EN);
455 qed_wr(p_hwfn, p_ptt, PRS_REG_LIGHT_L2_ETHERTYPE_EN,
456 (ll2_ethertype_en | 0x01));
457
458 if (qed_cxt_get_proto_cid_start(p_hwfn, PROTOCOLID_ROCE) % 2) {
459 DP_NOTICE(p_hwfn, "The first RoCE's cid should be even\n");
460 return -EINVAL;
461 }
462
463 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Initializing HW - Done\n");
464 return 0;
465}
466
467static int qed_rdma_start_fw(struct qed_hwfn *p_hwfn,
468 struct qed_rdma_start_in_params *params,
469 struct qed_ptt *p_ptt)
470{
471 struct rdma_init_func_ramrod_data *p_ramrod;
472 struct qed_rdma_cnq_params *p_cnq_pbl_list;
473 struct rdma_init_func_hdr *p_params_header;
474 struct rdma_cnq_params *p_cnq_params;
475 struct qed_sp_init_data init_data;
476 struct qed_spq_entry *p_ent;
477 u32 cnq_id, sb_id;
478 int rc;
479
480 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Starting FW\n");
481
482 /* Save the number of cnqs for the function close ramrod */
483 p_hwfn->p_rdma_info->num_cnqs = params->desired_cnq;
484
485 /* Get SPQ entry */
486 memset(&init_data, 0, sizeof(init_data));
487 init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
488 init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
489
490 rc = qed_sp_init_request(p_hwfn, &p_ent, RDMA_RAMROD_FUNC_INIT,
491 p_hwfn->p_rdma_info->proto, &init_data);
492 if (rc)
493 return rc;
494
495 p_ramrod = &p_ent->ramrod.roce_init_func.rdma;
496
497 p_params_header = &p_ramrod->params_header;
498 p_params_header->cnq_start_offset = (u8)RESC_START(p_hwfn,
499 QED_RDMA_CNQ_RAM);
500 p_params_header->num_cnqs = params->desired_cnq;
501
502 if (params->cq_mode == QED_RDMA_CQ_MODE_16_BITS)
503 p_params_header->cq_ring_mode = 1;
504 else
505 p_params_header->cq_ring_mode = 0;
506
507 for (cnq_id = 0; cnq_id < params->desired_cnq; cnq_id++) {
508 sb_id = qed_rdma_get_sb_id(p_hwfn, cnq_id);
509 p_cnq_params = &p_ramrod->cnq_params[cnq_id];
510 p_cnq_pbl_list = &params->cnq_pbl_list[cnq_id];
511 p_cnq_params->sb_num =
512 cpu_to_le16(p_hwfn->sbs_info[sb_id]->igu_sb_id);
513
514 p_cnq_params->sb_index = p_hwfn->pf_params.rdma_pf_params.gl_pi;
515 p_cnq_params->num_pbl_pages = p_cnq_pbl_list->num_pbl_pages;
516
517 DMA_REGPAIR_LE(p_cnq_params->pbl_base_addr,
518 p_cnq_pbl_list->pbl_ptr);
519
520 /* we assume here that cnq_id and qz_offset are the same */
521 p_cnq_params->queue_zone_num =
522 cpu_to_le16(p_hwfn->p_rdma_info->queue_zone_base +
523 cnq_id);
524 }
525
526 return qed_spq_post(p_hwfn, p_ent, NULL);
527}
528
529static int qed_rdma_reserve_lkey(struct qed_hwfn *p_hwfn)
530{
531 struct qed_rdma_device *dev = p_hwfn->p_rdma_info->dev;
532
533 /* The first DPI is reserved for the Kernel */
534 __set_bit(0, p_hwfn->p_rdma_info->dpi_map.bitmap);
535
536 /* Tid 0 will be used as the key for "reserved MR".
537 * The driver should allocate memory for it so it can be loaded but no
538 * ramrod should be passed on it.
539 */
540 qed_rdma_alloc_tid(p_hwfn, &dev->reserved_lkey);
541 if (dev->reserved_lkey != RDMA_RESERVED_LKEY) {
542 DP_NOTICE(p_hwfn,
543 "Reserved lkey should be equal to RDMA_RESERVED_LKEY\n");
544 return -EINVAL;
545 }
546
547 return 0;
548}
549
550static int qed_rdma_setup(struct qed_hwfn *p_hwfn,
551 struct qed_ptt *p_ptt,
552 struct qed_rdma_start_in_params *params)
553{
554 int rc;
555
556 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "RDMA setup\n");
557
558 spin_lock_init(&p_hwfn->p_rdma_info->lock);
559
560 qed_rdma_init_devinfo(p_hwfn, params);
561 qed_rdma_init_port(p_hwfn);
562 qed_rdma_init_events(p_hwfn, params);
563
564 rc = qed_rdma_reserve_lkey(p_hwfn);
565 if (rc)
566 return rc;
567
568 rc = qed_rdma_init_hw(p_hwfn, p_ptt);
569 if (rc)
570 return rc;
571
572 return qed_rdma_start_fw(p_hwfn, params, p_ptt);
573}
574
575int qed_rdma_stop(void *rdma_cxt)
576{
577 struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
578 struct rdma_close_func_ramrod_data *p_ramrod;
579 struct qed_sp_init_data init_data;
580 struct qed_spq_entry *p_ent;
581 struct qed_ptt *p_ptt;
582 u32 ll2_ethertype_en;
583 int rc = -EBUSY;
584
585 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "RDMA stop\n");
586
587 p_ptt = qed_ptt_acquire(p_hwfn);
588 if (!p_ptt) {
589 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Failed to acquire PTT\n");
590 return rc;
591 }
592
593 /* Disable RoCE search */
594 qed_wr(p_hwfn, p_ptt, p_hwfn->rdma_prs_search_reg, 0);
595 p_hwfn->b_rdma_enabled_in_prs = false;
596
597 qed_wr(p_hwfn, p_ptt, PRS_REG_ROCE_DEST_QP_MAX_PF, 0);
598
599 ll2_ethertype_en = qed_rd(p_hwfn, p_ptt, PRS_REG_LIGHT_L2_ETHERTYPE_EN);
600
601 qed_wr(p_hwfn, p_ptt, PRS_REG_LIGHT_L2_ETHERTYPE_EN,
602 (ll2_ethertype_en & 0xFFFE));
603
604 qed_ptt_release(p_hwfn, p_ptt);
605
606 /* Get SPQ entry */
607 memset(&init_data, 0, sizeof(init_data));
608 init_data.opaque_fid = p_hwfn->hw_info.opaque_fid;
609 init_data.comp_mode = QED_SPQ_MODE_EBLOCK;
610
611 /* Stop RoCE */
612 rc = qed_sp_init_request(p_hwfn, &p_ent, RDMA_RAMROD_FUNC_CLOSE,
613 p_hwfn->p_rdma_info->proto, &init_data);
614 if (rc)
615 goto out;
616
617 p_ramrod = &p_ent->ramrod.rdma_close_func;
618
619 p_ramrod->num_cnqs = p_hwfn->p_rdma_info->num_cnqs;
620 p_ramrod->cnq_start_offset = (u8)RESC_START(p_hwfn, QED_RDMA_CNQ_RAM);
621
622 rc = qed_spq_post(p_hwfn, p_ent, NULL);
623
624out:
625 qed_rdma_free(p_hwfn);
626
627 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "RDMA stop done, rc = %d\n", rc);
628 return rc;
629}
630
631int qed_rdma_add_user(void *rdma_cxt,
632 struct qed_rdma_add_user_out_params *out_params)
633{
634 struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
635 u32 dpi_start_offset;
636 u32 returned_id = 0;
637 int rc;
638
639 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Adding User\n");
640
641 /* Allocate DPI */
642 spin_lock_bh(&p_hwfn->p_rdma_info->lock);
643 rc = qed_rdma_bmap_alloc_id(p_hwfn, &p_hwfn->p_rdma_info->dpi_map,
644 &returned_id);
645 spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
646
647 out_params->dpi = (u16)returned_id;
648
649 /* Calculate the corresponding DPI address */
650 dpi_start_offset = p_hwfn->dpi_start_offset;
651
652 out_params->dpi_addr = (u64)((u8 __iomem *)p_hwfn->doorbells +
653 dpi_start_offset +
654 ((out_params->dpi) * p_hwfn->dpi_size));
655
656 out_params->dpi_phys_addr = p_hwfn->cdev->db_phys_addr +
657 dpi_start_offset +
658 ((out_params->dpi) * p_hwfn->dpi_size);
659
660 out_params->dpi_size = p_hwfn->dpi_size;
661
662 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Adding user - done, rc = %d\n", rc);
663 return rc;
664}
665
666struct qed_rdma_device *qed_rdma_query_device(void *rdma_cxt)
667{
668 struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
669
670 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Query device\n");
671
672 /* Return struct with device parameters */
673 return p_hwfn->p_rdma_info->dev;
674}
675
676int qed_rdma_alloc_tid(void *rdma_cxt, u32 *itid)
677{
678 struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
679 int rc;
680
681 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Allocate TID\n");
682
683 spin_lock_bh(&p_hwfn->p_rdma_info->lock);
684 rc = qed_rdma_bmap_alloc_id(p_hwfn,
685 &p_hwfn->p_rdma_info->tid_map, itid);
686 spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
687 if (rc)
688 goto out;
689
690 rc = qed_cxt_dynamic_ilt_alloc(p_hwfn, QED_ELEM_TASK, *itid);
691out:
692 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "Allocate TID - done, rc = %d\n", rc);
693 return rc;
694}
695
696void qed_rdma_cnq_prod_update(void *rdma_cxt, u8 qz_offset, u16 prod)
697{
698 struct qed_hwfn *p_hwfn;
699 u16 qz_num;
700 u32 addr;
701
702 p_hwfn = (struct qed_hwfn *)rdma_cxt;
703 qz_num = p_hwfn->p_rdma_info->queue_zone_base + qz_offset;
704 addr = GTT_BAR0_MAP_REG_USDM_RAM +
705 USTORM_COMMON_QUEUE_CONS_OFFSET(qz_num);
706
707 REG_WR16(p_hwfn, addr, prod);
708
709 /* keep prod updates ordered */
710 wmb();
711}
712
713static int qed_fill_rdma_dev_info(struct qed_dev *cdev,
714 struct qed_dev_rdma_info *info)
715{
716 memset(info, 0, sizeof(*info));
717
718 info->rdma_type = QED_RDMA_TYPE_ROCE;
719
720 qed_fill_dev_info(cdev, &info->common);
721
722 return 0;
723}
724
725static int qed_rdma_get_sb_start(struct qed_dev *cdev)
726{
727 int feat_num;
728
729 if (cdev->num_hwfns > 1)
730 feat_num = FEAT_NUM(QED_LEADING_HWFN(cdev), QED_PF_L2_QUE);
731 else
732 feat_num = FEAT_NUM(QED_LEADING_HWFN(cdev), QED_PF_L2_QUE) *
733 cdev->num_hwfns;
734
735 return feat_num;
736}
737
738static int qed_rdma_get_min_cnq_msix(struct qed_dev *cdev)
739{
740 int n_cnq = FEAT_NUM(QED_LEADING_HWFN(cdev), QED_RDMA_CNQ);
741 int n_msix = cdev->int_params.rdma_msix_cnt;
742
743 return min_t(int, n_cnq, n_msix);
744}
745
746static int qed_rdma_set_int(struct qed_dev *cdev, u16 cnt)
747{
748 int limit = 0;
749
750 /* Mark the fastpath as free/used */
751 cdev->int_params.fp_initialized = cnt ? true : false;
752
753 if (cdev->int_params.out.int_mode != QED_INT_MODE_MSIX) {
754 DP_ERR(cdev,
755 "qed roce supports only MSI-X interrupts (detected %d).\n",
756 cdev->int_params.out.int_mode);
757 return -EINVAL;
758 } else if (cdev->int_params.fp_msix_cnt) {
759 limit = cdev->int_params.rdma_msix_cnt;
760 }
761
762 if (!limit)
763 return -ENOMEM;
764
765 return min_t(int, cnt, limit);
766}
767
768static int qed_rdma_get_int(struct qed_dev *cdev, struct qed_int_info *info)
769{
770 memset(info, 0, sizeof(*info));
771
772 if (!cdev->int_params.fp_initialized) {
773 DP_INFO(cdev,
774 "Protocol driver requested interrupt information, but its support is not yet configured\n");
775 return -EINVAL;
776 }
777
778 if (cdev->int_params.out.int_mode == QED_INT_MODE_MSIX) {
779 int msix_base = cdev->int_params.rdma_msix_base;
780
781 info->msix_cnt = cdev->int_params.rdma_msix_cnt;
782 info->msix = &cdev->int_params.msix_table[msix_base];
783
784 DP_VERBOSE(cdev, QED_MSG_RDMA, "msix_cnt = %d msix_base=%d\n",
785 info->msix_cnt, msix_base);
786 }
787
788 return 0;
789}
790
791static void *qed_rdma_get_rdma_ctx(struct qed_dev *cdev)
792{
793 return QED_LEADING_HWFN(cdev);
794}
795
796static void qed_rdma_dpm_conf(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
797{
798 u32 val;
799
800 val = (p_hwfn->dcbx_no_edpm || p_hwfn->db_bar_no_edpm) ? 0 : 1;
801
802 qed_wr(p_hwfn, p_ptt, DORQ_REG_PF_DPM_ENABLE, val);
803 DP_VERBOSE(p_hwfn, (QED_MSG_DCB | QED_MSG_RDMA),
804 "Changing DPM_EN state to %d (DCBX=%d, DB_BAR=%d)\n",
805 val, p_hwfn->dcbx_no_edpm, p_hwfn->db_bar_no_edpm);
806}
807
808void qed_rdma_dpm_bar(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
809{
810 p_hwfn->db_bar_no_edpm = true;
811
812 qed_rdma_dpm_conf(p_hwfn, p_ptt);
813}
814
815int qed_rdma_start(void *rdma_cxt, struct qed_rdma_start_in_params *params)
816{
817 struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
818 struct qed_ptt *p_ptt;
819 int rc = -EBUSY;
820
821 DP_VERBOSE(p_hwfn, QED_MSG_RDMA,
822 "desired_cnq = %08x\n", params->desired_cnq);
823
824 p_ptt = qed_ptt_acquire(p_hwfn);
825 if (!p_ptt)
826 goto err;
827
828 rc = qed_rdma_alloc(p_hwfn, p_ptt, params);
829 if (rc)
830 goto err1;
831
832 rc = qed_rdma_setup(p_hwfn, p_ptt, params);
833 if (rc)
834 goto err2;
835
836 qed_ptt_release(p_hwfn, p_ptt);
837
838 return rc;
839
840err2:
841 qed_rdma_free(p_hwfn);
842err1:
843 qed_ptt_release(p_hwfn, p_ptt);
844err:
845 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "RDMA start - error, rc = %d\n", rc);
846 return rc;
847}
848
849static int qed_rdma_init(struct qed_dev *cdev,
850 struct qed_rdma_start_in_params *params)
851{
852 return qed_rdma_start(QED_LEADING_HWFN(cdev), params);
853}
854
855void qed_rdma_remove_user(void *rdma_cxt, u16 dpi)
856{
857 struct qed_hwfn *p_hwfn = (struct qed_hwfn *)rdma_cxt;
858
859 DP_VERBOSE(p_hwfn, QED_MSG_RDMA, "dpi = %08x\n", dpi);
860
861 spin_lock_bh(&p_hwfn->p_rdma_info->lock);
862 qed_bmap_release_id(p_hwfn, &p_hwfn->p_rdma_info->dpi_map, dpi);
863 spin_unlock_bh(&p_hwfn->p_rdma_info->lock);
864}
865
866static const struct qed_rdma_ops qed_rdma_ops_pass = {
867 .common = &qed_common_ops_pass,
868 .fill_dev_info = &qed_fill_rdma_dev_info,
869 .rdma_get_rdma_ctx = &qed_rdma_get_rdma_ctx,
870 .rdma_init = &qed_rdma_init,
871 .rdma_add_user = &qed_rdma_add_user,
872 .rdma_remove_user = &qed_rdma_remove_user,
873 .rdma_stop = &qed_rdma_stop,
874 .rdma_query_device = &qed_rdma_query_device,
875 .rdma_get_start_sb = &qed_rdma_get_sb_start,
876 .rdma_get_rdma_int = &qed_rdma_get_int,
877 .rdma_set_rdma_int = &qed_rdma_set_int,
878 .rdma_get_min_cnq_msix = &qed_rdma_get_min_cnq_msix,
879 .rdma_cnq_prod_update = &qed_rdma_cnq_prod_update,
880};
881
882const struct qed_rdma_ops *qed_get_rdma_ops()
883{
884 return &qed_rdma_ops_pass;
885}
886EXPORT_SYMBOL(qed_get_rdma_ops);