Branch data Line data Source code
1 : : /* SPDX-License-Identifier: BSD-3-Clause
2 : : * Copyright(c) 2019 Intel Corporation.
3 : : */
4 : : #include <stdint.h>
5 : : #include <stdio.h>
6 : : #include <errno.h>
7 : :
8 : : #include <rte_io.h>
9 : : #include <rte_eal.h>
10 : : #include <rte_pci.h>
11 : : #include <bus_pci_driver.h>
12 : : #include <rte_rawdev.h>
13 : : #include <rte_rawdev_pmd.h>
14 : : #include <rte_memzone.h>
15 : :
16 : : #include "ntb.h"
17 : : #include "ntb_hw_intel.h"
18 : :
19 : : enum xeon_ntb_bar {
20 : : XEON_NTB_BAR23 = 2,
21 : : XEON_NTB_BAR45 = 4,
22 : : };
23 : :
24 : : static enum xeon_ntb_bar intel_ntb_bar[] = {
25 : : XEON_NTB_BAR23,
26 : : XEON_NTB_BAR45,
27 : : };
28 : :
29 : : static inline int
30 : : is_gen3_ntb(const struct ntb_hw *hw)
31 : : {
32 [ # # # # : 0 : if (hw->pci_dev->id.device_id == NTB_INTEL_DEV_ID_B2B_SKX)
# # # # #
# ]
33 : : return 1;
34 : :
35 : : return 0;
36 : : }
37 : :
38 : : static inline int
39 : : is_gen4_ntb(const struct ntb_hw *hw)
40 : : {
41 [ # # # # : 0 : if (hw->pci_dev->id.device_id == NTB_INTEL_DEV_ID_B2B_ICX ||
# # # # #
# # # #
# ]
42 : : hw->pci_dev->id.device_id == NTB_INTEL_DEV_ID_B2B_SPR)
43 : : return 1;
44 : :
45 : : return 0;
46 : : }
47 : :
48 : : static int
49 : 0 : intel_ntb3_check_ppd(struct ntb_hw *hw)
50 : : {
51 : : uint8_t reg_val;
52 : : int ret;
53 : :
54 : 0 : ret = rte_pci_read_config(hw->pci_dev, ®_val,
55 : : sizeof(reg_val), XEON_PPD_OFFSET);
56 [ # # ]: 0 : if (ret < 0) {
57 : 0 : NTB_LOG(ERR, "Cannot get NTB PPD (PCIe port definition).");
58 : 0 : return -EIO;
59 : : }
60 : :
61 : : /* Check connection topo type. Only support B2B. */
62 [ # # ]: 0 : switch (reg_val & XEON_PPD_CONN_MASK) {
63 : 0 : case XEON_PPD_CONN_B2B:
64 : 0 : NTB_LOG(INFO, "Topo B2B (back to back) is using.");
65 : : break;
66 : 0 : case XEON_PPD_CONN_TRANSPARENT:
67 : : case XEON_PPD_CONN_RP:
68 : : default:
69 : 0 : NTB_LOG(ERR, "Not supported conn topo. Please use B2B.");
70 : 0 : return -EINVAL;
71 : : }
72 : :
73 : : /* Check device type. */
74 [ # # ]: 0 : if (reg_val & XEON_PPD_DEV_DSD) {
75 : 0 : NTB_LOG(INFO, "DSD, Downstream Device.");
76 : 0 : hw->topo = NTB_TOPO_B2B_DSD;
77 : : } else {
78 : 0 : NTB_LOG(INFO, "USD, Upstream device.");
79 : 0 : hw->topo = NTB_TOPO_B2B_USD;
80 : : }
81 : :
82 : : /* Check if bar4 is split. Do not support split bar. */
83 [ # # ]: 0 : if (reg_val & XEON_PPD_SPLIT_BAR_MASK) {
84 : 0 : NTB_LOG(ERR, "Do not support split bar.");
85 : 0 : return -EINVAL;
86 : : }
87 : :
88 : : return 0;
89 : : }
90 : :
91 : : static int
92 : 0 : intel_ntb4_check_ppd_for_ICX(struct ntb_hw *hw, uint32_t reg_val)
93 : : {
94 : : /* Check connection topo type. Only support B2B. */
95 [ # # ]: 0 : switch (reg_val & XEON_GEN4_PPD_CONN_MASK) {
96 : 0 : case XEON_GEN4_PPD_CONN_B2B:
97 : 0 : NTB_LOG(INFO, "Topo B2B (back to back) is using.");
98 : : break;
99 : 0 : default:
100 : 0 : NTB_LOG(ERR, "Not supported conn topo. Please use B2B.");
101 : 0 : return -EINVAL;
102 : : }
103 : :
104 : : /* Check device type. */
105 [ # # ]: 0 : if (reg_val & XEON_GEN4_PPD_DEV_DSD) {
106 : 0 : NTB_LOG(INFO, "DSD, Downstream Device.");
107 : 0 : hw->topo = NTB_TOPO_B2B_DSD;
108 : : } else {
109 : 0 : NTB_LOG(INFO, "USD, Upstream device.");
110 : 0 : hw->topo = NTB_TOPO_B2B_USD;
111 : : }
112 : :
113 : : return 0;
114 : : }
115 : :
116 : : static int
117 : 0 : intel_ntb4_check_ppd_for_SPR(struct ntb_hw *hw, uint32_t reg_val)
118 : : {
119 : : /* Check connection topo type. Only support B2B. */
120 [ # # ]: 0 : switch (reg_val & XEON_SPR_PPD_CONN_MASK) {
121 : 0 : case XEON_SPR_PPD_CONN_B2B:
122 : 0 : NTB_LOG(INFO, "Topo B2B (back to back) is using.");
123 : : break;
124 : 0 : default:
125 : 0 : NTB_LOG(ERR, "Not supported conn topo. Please use B2B.");
126 : 0 : return -EINVAL;
127 : : }
128 : :
129 : : /* Check device type. */
130 [ # # ]: 0 : if (reg_val & XEON_SPR_PPD_DEV_DSD) {
131 : 0 : NTB_LOG(INFO, "DSD, Downstream Device.");
132 : 0 : hw->topo = NTB_TOPO_B2B_DSD;
133 : : } else {
134 : 0 : NTB_LOG(INFO, "USD, Upstream device.");
135 : 0 : hw->topo = NTB_TOPO_B2B_USD;
136 : : }
137 : :
138 : : return 0;
139 : : }
140 : :
141 : : static int
142 : 0 : intel_ntb4_check_ppd(struct ntb_hw *hw)
143 : : {
144 : : uint8_t revision_id;
145 : : uint32_t reg_val;
146 : : int ret;
147 : :
148 : 0 : ret = rte_pci_read_config(hw->pci_dev, &revision_id,
149 : : 1, RTE_PCI_REVISION_ID);
150 [ # # ]: 0 : if (ret != 1) {
151 : 0 : NTB_LOG(ERR, "Cannot get NTB PCI Device Revision ID.");
152 : 0 : return -EIO;
153 : : }
154 : :
155 : 0 : reg_val = rte_read32(hw->hw_addr + XEON_GEN4_PPD1_OFFSET);
156 : :
157 : : /* Distinguish HW platform (ICX/SPR) via PCI Revision ID */
158 [ # # ]: 0 : if (revision_id > NTB_PCI_DEV_REVISION_ICX_MAX)
159 : 0 : ret = intel_ntb4_check_ppd_for_SPR(hw, reg_val);
160 [ # # ]: 0 : else if (revision_id >= NTB_PCI_DEV_REVISION_ICX_MIN)
161 : 0 : ret = intel_ntb4_check_ppd_for_ICX(hw, reg_val);
162 : : else {
163 : 0 : NTB_LOG(ERR, "Invalid NTB PCI Device Revision ID.");
164 : 0 : return -EIO;
165 : : }
166 : :
167 : : return ret;
168 : : }
169 : :
170 : : static int
171 : 0 : intel_ntb_dev_init(const struct rte_rawdev *dev)
172 : : {
173 : 0 : struct ntb_hw *hw = dev->dev_private;
174 : : uint8_t bar;
175 : : int ret, i;
176 : :
177 [ # # ]: 0 : if (hw == NULL) {
178 : 0 : NTB_LOG(ERR, "Invalid device.");
179 : 0 : return -EINVAL;
180 : : }
181 : :
182 [ # # ]: 0 : hw->hw_addr = (char *)hw->pci_dev->mem_resource[0].addr;
183 : :
184 : : if (is_gen3_ntb(hw))
185 : 0 : ret = intel_ntb3_check_ppd(hw);
186 : : else if (is_gen4_ntb(hw))
187 : : /* PPD is in MMIO but not config space for NTB Gen4 */
188 : 0 : ret = intel_ntb4_check_ppd(hw);
189 : : else {
190 : 0 : NTB_LOG(ERR, "Cannot init device for unsupported device.");
191 : 0 : return -ENOTSUP;
192 : : }
193 : :
194 [ # # ]: 0 : if (ret)
195 : : return ret;
196 : :
197 : 0 : hw->mw_cnt = XEON_MW_COUNT;
198 : 0 : hw->db_cnt = XEON_DB_COUNT;
199 : 0 : hw->spad_cnt = XEON_SPAD_COUNT;
200 : :
201 : 0 : hw->mw_size = rte_zmalloc("ntb_mw_size",
202 : : hw->mw_cnt * sizeof(uint64_t), 0);
203 [ # # ]: 0 : if (hw->mw_size == NULL) {
204 : 0 : NTB_LOG(ERR, "Cannot allocate memory for mw size.");
205 : 0 : return -ENOMEM;
206 : : }
207 : :
208 [ # # ]: 0 : for (i = 0; i < hw->mw_cnt; i++) {
209 : 0 : bar = intel_ntb_bar[i];
210 : 0 : hw->mw_size[i] = hw->pci_dev->mem_resource[bar].len;
211 : : }
212 : :
213 : : /* Reserve the last 2 spad registers for users. */
214 [ # # ]: 0 : for (i = 0; i < NTB_SPAD_USER_MAX_NUM; i++)
215 : 0 : hw->spad_user_list[i] = hw->spad_cnt;
216 : 0 : hw->spad_user_list[0] = hw->spad_cnt - 2;
217 : 0 : hw->spad_user_list[1] = hw->spad_cnt - 1;
218 : :
219 : 0 : return 0;
220 : : }
221 : :
222 : : static void *
223 : 0 : intel_ntb_get_peer_mw_addr(const struct rte_rawdev *dev, int mw_idx)
224 : : {
225 : 0 : struct ntb_hw *hw = dev->dev_private;
226 : : uint8_t bar;
227 : :
228 [ # # ]: 0 : if (hw == NULL) {
229 : 0 : NTB_LOG(ERR, "Invalid device.");
230 : 0 : return 0;
231 : : }
232 : :
233 [ # # # # ]: 0 : if (mw_idx < 0 || mw_idx >= hw->mw_cnt) {
234 : 0 : NTB_LOG(ERR, "Invalid memory window index (0 - %u).",
235 : : hw->mw_cnt - 1);
236 : 0 : return 0;
237 : : }
238 : :
239 : 0 : bar = intel_ntb_bar[mw_idx];
240 : :
241 : 0 : return hw->pci_dev->mem_resource[bar].addr;
242 : : }
243 : :
244 : : static int
245 : 0 : intel_ntb_mw_set_trans(const struct rte_rawdev *dev, int mw_idx,
246 : : uint64_t addr, uint64_t size)
247 : : {
248 : 0 : struct ntb_hw *hw = dev->dev_private;
249 : : void *xlat_addr, *limit_addr;
250 : : uint64_t xlat_off, limit_off;
251 : : uint64_t base, limit;
252 : : uint8_t bar;
253 : :
254 [ # # ]: 0 : if (hw == NULL) {
255 : 0 : NTB_LOG(ERR, "Invalid device.");
256 : 0 : return -EINVAL;
257 : : }
258 : :
259 [ # # # # ]: 0 : if (mw_idx < 0 || mw_idx >= hw->mw_cnt) {
260 : 0 : NTB_LOG(ERR, "Invalid memory window index (0 - %u).",
261 : : hw->mw_cnt - 1);
262 : 0 : return -EINVAL;
263 : : }
264 : :
265 : 0 : bar = intel_ntb_bar[mw_idx];
266 : :
267 : 0 : xlat_off = XEON_IMBAR1XBASE_OFFSET + mw_idx * XEON_BAR_INTERVAL_OFFSET;
268 : 0 : limit_off = XEON_IMBAR1XLMT_OFFSET + mw_idx * XEON_BAR_INTERVAL_OFFSET;
269 : 0 : xlat_addr = hw->hw_addr + xlat_off;
270 : 0 : limit_addr = hw->hw_addr + limit_off;
271 : :
272 : : /* Limit reg val should be EMBAR base address plus MW size. */
273 : : base = addr;
274 : 0 : limit = hw->pci_dev->mem_resource[bar].phys_addr + size;
275 : : rte_write64(base, xlat_addr);
276 : : rte_write64(limit, limit_addr);
277 : :
278 : : if (is_gen3_ntb(hw)) {
279 : : /* Setup the external point so that remote can access. */
280 : 0 : xlat_off = XEON_EMBAR1_OFFSET + 8 * mw_idx;
281 : 0 : xlat_addr = hw->hw_addr + xlat_off;
282 : 0 : limit_off = XEON_EMBAR1XLMT_OFFSET +
283 : : mw_idx * XEON_BAR_INTERVAL_OFFSET;
284 : 0 : limit_addr = hw->hw_addr + limit_off;
285 : : base = rte_read64(xlat_addr);
286 : 0 : base &= ~0xf;
287 : 0 : limit = base + size;
288 : : rte_write64(limit, limit_addr);
289 : : } else if (is_gen4_ntb(hw)) {
290 : : /* Set translate base address index register */
291 : 0 : xlat_off = XEON_GEN4_IM1XBASEIDX_OFFSET +
292 : : mw_idx * XEON_GEN4_XBASEIDX_INTERVAL;
293 [ # # ]: 0 : xlat_addr = hw->hw_addr + xlat_off;
294 : 0 : rte_write16(rte_log2_u64(size), xlat_addr);
295 : : } else {
296 : 0 : NTB_LOG(ERR, "Cannot set translation of memory windows for unsupported device.");
297 : : rte_write64(base, limit_addr);
298 : : rte_write64(0, xlat_addr);
299 : 0 : return -ENOTSUP;
300 : : }
301 : :
302 : : return 0;
303 : : }
304 : :
305 : : static void *
306 : 0 : intel_ntb_ioremap(const struct rte_rawdev *dev, uint64_t addr)
307 : : {
308 : 0 : struct ntb_hw *hw = dev->dev_private;
309 : : void *mapped = NULL;
310 : : void *base;
311 : : int i;
312 : :
313 [ # # ]: 0 : for (i = 0; i < hw->peer_used_mws; i++) {
314 [ # # ]: 0 : if (addr >= hw->peer_mw_base[i] &&
315 [ # # ]: 0 : addr <= hw->peer_mw_base[i] + hw->mw_size[i]) {
316 : 0 : base = intel_ntb_get_peer_mw_addr(dev, i);
317 : 0 : mapped = (void *)(size_t)(addr - hw->peer_mw_base[i] +
318 : 0 : (size_t)base);
319 : 0 : break;
320 : : }
321 : : }
322 : :
323 : 0 : return mapped;
324 : : }
325 : :
326 : : static int
327 : 0 : intel_ntb_get_link_status(const struct rte_rawdev *dev)
328 : : {
329 : 0 : struct ntb_hw *hw = dev->dev_private;
330 : : uint16_t reg_val, reg_off;
331 : : int ret;
332 : :
333 [ # # ]: 0 : if (hw == NULL) {
334 : 0 : NTB_LOG(ERR, "Invalid device.");
335 : 0 : return -EINVAL;
336 : : }
337 : :
338 : : if (is_gen3_ntb(hw)) {
339 : : reg_off = XEON_GEN3_LINK_STATUS_OFFSET;
340 : 0 : ret = rte_pci_read_config(hw->pci_dev, ®_val,
341 : : sizeof(reg_val), reg_off);
342 [ # # ]: 0 : if (ret < 0) {
343 : 0 : NTB_LOG(ERR, "Unable to get link status.");
344 : 0 : return -EIO;
345 : : }
346 : : } else if (is_gen4_ntb(hw)) {
347 : : reg_off = XEON_GEN4_LINK_STATUS_OFFSET;
348 : 0 : reg_val = rte_read16(hw->hw_addr + reg_off);
349 : : } else {
350 : 0 : NTB_LOG(ERR, "Cannot get link status for unsupported device.");
351 : 0 : return -ENOTSUP;
352 : : }
353 : :
354 : 0 : hw->link_status = NTB_LNK_STA_ACTIVE(reg_val);
355 : :
356 [ # # ]: 0 : if (hw->link_status) {
357 : 0 : hw->link_speed = NTB_LNK_STA_SPEED(reg_val);
358 : 0 : hw->link_width = NTB_LNK_STA_WIDTH(reg_val);
359 : : } else {
360 : 0 : hw->link_speed = NTB_SPEED_NONE;
361 : 0 : hw->link_width = NTB_WIDTH_NONE;
362 : : }
363 : :
364 : : return 0;
365 : : }
366 : :
367 : : static int
368 : : intel_ntb_gen3_set_link(const struct ntb_hw *hw, bool up)
369 : : {
370 : : uint32_t ntb_ctrl, reg_off;
371 : : void *reg_addr;
372 : :
373 : : reg_off = XEON_NTBCNTL_OFFSET;
374 : 0 : reg_addr = hw->hw_addr + reg_off;
375 : : ntb_ctrl = rte_read32(reg_addr);
376 : :
377 [ # # ]: 0 : if (up) {
378 : 0 : ntb_ctrl &= ~(NTB_CTL_DISABLE | NTB_CTL_CFG_LOCK);
379 : : ntb_ctrl |= NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP;
380 : 0 : ntb_ctrl |= NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP;
381 : : } else {
382 : : ntb_ctrl &= ~(NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP);
383 : 0 : ntb_ctrl &= ~(NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP);
384 : 0 : ntb_ctrl |= NTB_CTL_DISABLE | NTB_CTL_CFG_LOCK;
385 : : }
386 : :
387 : : rte_write32(ntb_ctrl, reg_addr);
388 : :
389 : : return 0;
390 : : }
391 : :
392 : : static int
393 : 0 : intel_ntb_gen4_set_link(const struct ntb_hw *hw, bool up)
394 : : {
395 : : uint32_t ntb_ctrl, ppd0;
396 : : uint16_t link_ctrl;
397 : : void *reg_addr;
398 : :
399 [ # # ]: 0 : if (up) {
400 : 0 : reg_addr = hw->hw_addr + XEON_NTBCNTL_OFFSET;
401 : : ntb_ctrl = NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP;
402 : : ntb_ctrl |= NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP;
403 : : rte_write32(ntb_ctrl, reg_addr);
404 : :
405 : 0 : reg_addr = hw->hw_addr + XEON_GEN4_LINK_CTRL_OFFSET;
406 : : link_ctrl = rte_read16(reg_addr);
407 : 0 : link_ctrl &= ~XEON_GEN4_LINK_CTRL_LINK_DIS;
408 : : rte_write16(link_ctrl, reg_addr);
409 : :
410 : : /* start link training */
411 : 0 : reg_addr = hw->hw_addr + XEON_GEN4_PPD0_OFFSET;
412 : : ppd0 = rte_read32(reg_addr);
413 : 0 : ppd0 |= XEON_GEN4_PPD_LINKTRN;
414 : : rte_write32(ppd0, reg_addr);
415 : :
416 : : /* make sure link training has started */
417 : : ppd0 = rte_read32(reg_addr);
418 [ # # ]: 0 : if (!(ppd0 & XEON_GEN4_PPD_LINKTRN)) {
419 : 0 : NTB_LOG(ERR, "Link is not training.");
420 : 0 : return -EINVAL;
421 : : }
422 : : } else {
423 : 0 : reg_addr = hw->hw_addr + XEON_NTBCNTL_OFFSET;
424 : : ntb_ctrl = rte_read32(reg_addr);
425 : : ntb_ctrl &= ~(NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP);
426 : 0 : ntb_ctrl &= ~(NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP);
427 : : rte_write32(ntb_ctrl, reg_addr);
428 : :
429 : 0 : reg_addr = hw->hw_addr + XEON_GEN4_LINK_CTRL_OFFSET;
430 : : link_ctrl = rte_read16(reg_addr);
431 : 0 : link_ctrl |= XEON_GEN4_LINK_CTRL_LINK_DIS;
432 : : rte_write16(link_ctrl, reg_addr);
433 : : }
434 : :
435 : : return 0;
436 : : }
437 : :
438 : : static int
439 : 0 : intel_ntb_set_link(const struct rte_rawdev *dev, bool up)
440 : : {
441 [ # # ]: 0 : struct ntb_hw *hw = dev->dev_private;
442 : : int ret = 0;
443 : :
444 : : if (is_gen3_ntb(hw))
445 : : ret = intel_ntb_gen3_set_link(hw, up);
446 : : else if (is_gen4_ntb(hw))
447 : 0 : ret = intel_ntb_gen4_set_link(hw, up);
448 : : else {
449 : 0 : NTB_LOG(ERR, "Cannot set link for unsupported device.");
450 : : ret = -ENOTSUP;
451 : : }
452 : :
453 : 0 : return ret;
454 : : }
455 : :
456 : : static uint32_t
457 : 0 : intel_ntb_spad_read(const struct rte_rawdev *dev, int spad, bool peer)
458 : : {
459 : 0 : struct ntb_hw *hw = dev->dev_private;
460 : : uint32_t spad_v, reg_off;
461 : : void *reg_addr;
462 : :
463 [ # # # # ]: 0 : if (spad < 0 || spad >= hw->spad_cnt) {
464 : 0 : NTB_LOG(ERR, "Invalid spad reg index.");
465 : 0 : return 0;
466 : : }
467 : :
468 : : /* When peer is true, read peer spad reg */
469 : : if (is_gen3_ntb(hw))
470 [ # # ]: 0 : reg_off = peer ? XEON_GEN3_B2B_SPAD_OFFSET :
471 : : XEON_IM_SPAD_OFFSET;
472 : : else if (is_gen4_ntb(hw))
473 [ # # ]: 0 : reg_off = peer ? XEON_GEN4_B2B_SPAD_OFFSET :
474 : : XEON_IM_SPAD_OFFSET;
475 : : else {
476 : 0 : NTB_LOG(ERR, "Cannot read spad for unsupported device.");
477 : 0 : return -ENOTSUP;
478 : : }
479 : 0 : reg_addr = hw->hw_addr + reg_off + (spad << 2);
480 : : spad_v = rte_read32(reg_addr);
481 : :
482 : 0 : return spad_v;
483 : : }
484 : :
485 : : static int
486 : 0 : intel_ntb_spad_write(const struct rte_rawdev *dev, int spad,
487 : : bool peer, uint32_t spad_v)
488 : : {
489 : 0 : struct ntb_hw *hw = dev->dev_private;
490 : : uint32_t reg_off;
491 : : void *reg_addr;
492 : :
493 [ # # # # ]: 0 : if (spad < 0 || spad >= hw->spad_cnt) {
494 : 0 : NTB_LOG(ERR, "Invalid spad reg index.");
495 : 0 : return -EINVAL;
496 : : }
497 : :
498 : : /* When peer is true, write peer spad reg */
499 : : if (is_gen3_ntb(hw))
500 [ # # ]: 0 : reg_off = peer ? XEON_GEN3_B2B_SPAD_OFFSET :
501 : : XEON_IM_SPAD_OFFSET;
502 : : else if (is_gen4_ntb(hw))
503 [ # # ]: 0 : reg_off = peer ? XEON_GEN4_B2B_SPAD_OFFSET :
504 : : XEON_IM_SPAD_OFFSET;
505 : : else {
506 : 0 : NTB_LOG(ERR, "Cannot write spad for unsupported device.");
507 : 0 : return -ENOTSUP;
508 : : }
509 : 0 : reg_addr = hw->hw_addr + reg_off + (spad << 2);
510 : :
511 : : rte_write32(spad_v, reg_addr);
512 : :
513 : 0 : return 0;
514 : : }
515 : :
516 : : static uint64_t
517 : 0 : intel_ntb_db_read(const struct rte_rawdev *dev)
518 : : {
519 : 0 : struct ntb_hw *hw = dev->dev_private;
520 : : uint64_t db_off, db_bits;
521 : : void *db_addr;
522 : :
523 : : db_off = XEON_IM_INT_STATUS_OFFSET;
524 : 0 : db_addr = hw->hw_addr + db_off;
525 : :
526 : : db_bits = rte_read64(db_addr);
527 : :
528 : 0 : return db_bits;
529 : : }
530 : :
531 : : static int
532 : 0 : intel_ntb_db_clear(const struct rte_rawdev *dev, uint64_t db_bits)
533 : : {
534 : 0 : struct ntb_hw *hw = dev->dev_private;
535 : : uint64_t db_off;
536 : : void *db_addr;
537 : :
538 : : db_off = XEON_IM_INT_STATUS_OFFSET;
539 [ # # ]: 0 : db_addr = hw->hw_addr + db_off;
540 : :
541 : : if (is_gen4_ntb(hw))
542 : : rte_write16(XEON_GEN4_SLOTSTS_DLLSCS,
543 : : hw->hw_addr + XEON_GEN4_SLOTSTS);
544 : : rte_write64(db_bits, db_addr);
545 : :
546 : 0 : return 0;
547 : : }
548 : :
549 : : static int
550 : 0 : intel_ntb_db_set_mask(const struct rte_rawdev *dev, uint64_t db_mask)
551 : : {
552 : 0 : struct ntb_hw *hw = dev->dev_private;
553 : : uint64_t db_m_off;
554 : : void *db_m_addr;
555 : :
556 : : db_m_off = XEON_IM_INT_DISABLE_OFFSET;
557 : 0 : db_m_addr = hw->hw_addr + db_m_off;
558 : :
559 : 0 : db_mask |= hw->db_mask;
560 : :
561 : : rte_write64(db_mask, db_m_addr);
562 : :
563 : 0 : hw->db_mask = db_mask;
564 : :
565 : 0 : return 0;
566 : : }
567 : :
568 : : static int
569 : 0 : intel_ntb_peer_db_set(const struct rte_rawdev *dev, uint8_t db_idx)
570 : : {
571 : 0 : struct ntb_hw *hw = dev->dev_private;
572 : : uint32_t db_off;
573 : : void *db_addr;
574 : :
575 [ # # ]: 0 : if (((uint64_t)1 << db_idx) & ~hw->db_valid_mask) {
576 : 0 : NTB_LOG(ERR, "Invalid doorbell.");
577 : 0 : return -EINVAL;
578 : : }
579 : :
580 : 0 : db_off = XEON_IM_DOORBELL_OFFSET + db_idx * 4;
581 : 0 : db_addr = hw->hw_addr + db_off;
582 : :
583 : : rte_write32(1, db_addr);
584 : :
585 : 0 : return 0;
586 : : }
587 : :
588 : : static int
589 : 0 : intel_ntb_vector_bind(const struct rte_rawdev *dev, uint8_t intr, uint8_t msix)
590 : : {
591 : 0 : struct ntb_hw *hw = dev->dev_private;
592 : : uint8_t reg_off;
593 : : void *reg_addr;
594 : :
595 [ # # ]: 0 : if (intr >= hw->db_cnt) {
596 : 0 : NTB_LOG(ERR, "Invalid intr source.");
597 : 0 : return -EINVAL;
598 : : }
599 : :
600 : : /* Bind intr source to msix vector */
601 : : if (is_gen3_ntb(hw))
602 : : reg_off = XEON_GEN3_INTVEC_OFFSET;
603 : : else if (is_gen4_ntb(hw))
604 : : reg_off = XEON_GEN4_INTVEC_OFFSET;
605 : : else {
606 : 0 : NTB_LOG(ERR, "Cannot bind vectors for unsupported device.");
607 : 0 : return -ENOTSUP;
608 : : }
609 : 0 : reg_addr = hw->hw_addr + reg_off + intr;
610 : :
611 : : rte_write8(msix, reg_addr);
612 : :
613 : 0 : return 0;
614 : : }
615 : :
616 : : /* Advertise the local configuration to the peer and program the memory
617 : : * windows. Scratchpad writes and translations only take effect once both
618 : : * sides are up, so this is also re-run from the ready doorbell.
619 : : */
620 : : static int
621 : 0 : intel_ntb_dev_handshake(const struct rte_rawdev *dev)
622 : : {
623 : 0 : struct ntb_hw *hw = dev->dev_private;
624 : : uint32_t val;
625 : : int ret, i;
626 : :
627 : : /* Tell peer the mw info of local side. */
628 : 0 : ret = intel_ntb_spad_write(dev, SPAD_NUM_MWS, 1, hw->mw_cnt);
629 [ # # ]: 0 : if (ret < 0)
630 : : return ret;
631 [ # # ]: 0 : for (i = 0; i < hw->mw_cnt; i++) {
632 : 0 : NTB_LOG(INFO, "Local %u mw size: 0x%"PRIx64"", i, hw->mw_size[i]);
633 : 0 : val = hw->mw_size[i] >> 32;
634 : 0 : ret = intel_ntb_spad_write(dev, SPAD_MW0_SZ_H + 2 * i, 1, val);
635 [ # # ]: 0 : if (ret < 0)
636 : 0 : return ret;
637 : 0 : val = hw->mw_size[i];
638 : 0 : ret = intel_ntb_spad_write(dev, SPAD_MW0_SZ_L + 2 * i, 1, val);
639 [ # # ]: 0 : if (ret < 0)
640 : 0 : return ret;
641 : : }
642 : :
643 : : /* Tell peer about the queue info and map memory to the peer. */
644 : 0 : ret = intel_ntb_spad_write(dev, SPAD_Q_SZ, 1, hw->queue_size);
645 [ # # ]: 0 : if (ret < 0)
646 : : return ret;
647 : 0 : ret = intel_ntb_spad_write(dev, SPAD_NUM_QPS, 1, hw->queue_pairs);
648 [ # # ]: 0 : if (ret < 0)
649 : : return ret;
650 : 0 : ret = intel_ntb_spad_write(dev, SPAD_USED_MWS, 1, hw->used_mw_num);
651 [ # # ]: 0 : if (ret < 0)
652 : : return ret;
653 [ # # ]: 0 : for (i = 0; i < hw->used_mw_num; i++) {
654 : 0 : val = (uint64_t)(size_t)(hw->mz[i]->addr) >> 32;
655 : 0 : ret = intel_ntb_spad_write(dev, SPAD_MW0_BA_H + 2 * i, 1, val);
656 [ # # ]: 0 : if (ret < 0)
657 : 0 : return ret;
658 : 0 : val = (uint64_t)(size_t)(hw->mz[i]->addr);
659 : 0 : ret = intel_ntb_spad_write(dev, SPAD_MW0_BA_L + 2 * i, 1, val);
660 [ # # ]: 0 : if (ret < 0)
661 : 0 : return ret;
662 : : }
663 : :
664 [ # # ]: 0 : for (i = 0; i < hw->used_mw_num; i++) {
665 : 0 : ret = intel_ntb_mw_set_trans(dev, i, hw->mz[i]->iova, hw->mz[i]->len);
666 [ # # ]: 0 : if (ret < 0)
667 : 0 : return ret;
668 : : }
669 : :
670 : : /* Ring doorbell 0 to tell peer the device is ready. */
671 : 0 : return intel_ntb_peer_db_set(dev, 0);
672 : : }
673 : :
674 : : /* Check that the peer advertises the same memory-window layout as local. */
675 : : static int
676 : 0 : intel_ntb_peer_mw_validate(const struct rte_rawdev *dev)
677 : : {
678 : 0 : struct ntb_hw *hw = dev->dev_private;
679 : : uint64_t peer_mw_size;
680 : : uint32_t val_h, val_l;
681 : : uint8_t peer_mw_cnt;
682 : : int i;
683 : :
684 : 0 : peer_mw_cnt = intel_ntb_spad_read(dev, SPAD_NUM_MWS, 0);
685 [ # # ]: 0 : if (peer_mw_cnt != hw->mw_cnt) {
686 : 0 : NTB_LOG(ERR, "Both mw cnt must be the same.");
687 : 0 : return -EINVAL;
688 : : }
689 : :
690 [ # # ]: 0 : for (i = 0; i < hw->mw_cnt; i++) {
691 : 0 : val_h = intel_ntb_spad_read(dev, SPAD_MW0_SZ_H + 2 * i, 0);
692 : 0 : val_l = intel_ntb_spad_read(dev, SPAD_MW0_SZ_L + 2 * i, 0);
693 : 0 : peer_mw_size = ((uint64_t)val_h << 32) | val_l;
694 : 0 : NTB_LOG(DEBUG, "Peer %u mw size: 0x%"PRIx64"", i, peer_mw_size);
695 [ # # ]: 0 : if (peer_mw_size != hw->mw_size[i]) {
696 : 0 : NTB_LOG(ERR, "Mw config must be the same.");
697 : 0 : return -EINVAL;
698 : : }
699 : : }
700 : :
701 : : return 0;
702 : : }
703 : :
704 : : /* Read and validate the peer configuration recorded in the scratchpad. */
705 : : static int
706 : 0 : intel_ntb_read_peer_config(const struct rte_rawdev *dev)
707 : : {
708 : 0 : struct ntb_hw *hw = dev->dev_private;
709 : : uint32_t peer_base_l, peer_val;
710 : : uint64_t peer_base_h;
711 : : int i;
712 : :
713 : 0 : peer_val = intel_ntb_spad_read(dev, SPAD_Q_SZ, 0);
714 [ # # ]: 0 : if (peer_val != hw->queue_size) {
715 : 0 : NTB_LOG(ERR, "Inconsistent queue size! (local: %u peer: %u)",
716 : : hw->queue_size, peer_val);
717 : 0 : return -EINVAL;
718 : : }
719 : :
720 : 0 : peer_val = intel_ntb_spad_read(dev, SPAD_NUM_QPS, 0);
721 [ # # ]: 0 : if (peer_val != hw->queue_pairs) {
722 : 0 : NTB_LOG(ERR, "Inconsistent number of queues! (local: %u peer: %u)",
723 : : hw->queue_pairs, peer_val);
724 : 0 : return -EINVAL;
725 : : }
726 : :
727 : 0 : hw->peer_used_mws = intel_ntb_spad_read(dev, SPAD_USED_MWS, 0);
728 : :
729 [ # # ]: 0 : for (i = 0; i < hw->peer_used_mws; i++) {
730 : 0 : peer_base_h = intel_ntb_spad_read(dev, SPAD_MW0_BA_H + 2 * i, 0);
731 : 0 : peer_base_l = intel_ntb_spad_read(dev, SPAD_MW0_BA_L + 2 * i, 0);
732 : 0 : hw->peer_mw_base[i] = (peer_base_h << 32) + peer_base_l;
733 : : }
734 : :
735 : : return 0;
736 : : }
737 : :
738 : : /* Tear down the local scratchpad and translations so the peer can no
739 : : * longer reach local memory.
740 : : */
741 : : static void
742 : 0 : intel_ntb_link_cleanup(const struct rte_rawdev *dev)
743 : : {
744 : 0 : struct ntb_hw *hw = dev->dev_private;
745 : : int status, i;
746 : :
747 : : /* Clean spad registers. */
748 [ # # ]: 0 : for (i = 0; i < hw->spad_cnt; i++) {
749 : 0 : status = intel_ntb_spad_write(dev, i, 0, 0);
750 [ # # ]: 0 : if (status)
751 : 0 : NTB_LOG(ERR, "Failed to clean local spad.");
752 : : }
753 : :
754 : : /* Clear mw so that peer cannot access local memory. */
755 [ # # ]: 0 : for (i = 0; i < hw->used_mw_num; i++) {
756 : 0 : status = intel_ntb_mw_set_trans(dev, i, 0, 0);
757 [ # # ]: 0 : if (status)
758 : 0 : NTB_LOG(ERR, "Failed to clean mw.");
759 : : }
760 : 0 : }
761 : :
762 : : static uint64_t
763 : 0 : intel_ntb_get_mem_align(const struct rte_rawdev *dev, uint32_t mw_id, uint64_t mw_len __rte_unused)
764 : : {
765 : 0 : struct ntb_hw *hw = dev->dev_private;
766 : :
767 : : /* The memzone base must be aligned to the memory window size. */
768 : 0 : return hw->mw_size[mw_id];
769 : : }
770 : :
771 : : /* operations for primary side of local ntb */
772 : : const struct ntb_dev_ops intel_ntb_ops = {
773 : : .ntb_dev_init = intel_ntb_dev_init,
774 : : .get_peer_mw_addr = intel_ntb_get_peer_mw_addr,
775 : : .ioremap = intel_ntb_ioremap,
776 : : .get_link_status = intel_ntb_get_link_status,
777 : : .set_link = intel_ntb_set_link,
778 : : .spad_read = intel_ntb_spad_read,
779 : : .spad_write = intel_ntb_spad_write,
780 : : .db_read = intel_ntb_db_read,
781 : : .db_clear = intel_ntb_db_clear,
782 : : .db_set_mask = intel_ntb_db_set_mask,
783 : : .peer_db_set = intel_ntb_peer_db_set,
784 : : .vector_bind = intel_ntb_vector_bind,
785 : : .dev_handshake = intel_ntb_dev_handshake,
786 : : .peer_mw_validate = intel_ntb_peer_mw_validate,
787 : : .read_peer_config = intel_ntb_read_peer_config,
788 : : .link_cleanup = intel_ntb_link_cleanup,
789 : : .mem_align = intel_ntb_get_mem_align,
790 : : };
|