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 : : RTE_BUILD_BUG_ON(XEON_MW_COUNT > NTB_MAX_MW);
198 : 0 : hw->mw_cnt = XEON_MW_COUNT;
199 : 0 : hw->db_cnt = XEON_DB_COUNT;
200 : 0 : hw->spad_cnt = XEON_SPAD_COUNT;
201 : :
202 [ # # ]: 0 : for (i = 0; i < hw->mw_cnt; i++) {
203 : 0 : bar = intel_ntb_bar[i];
204 : 0 : hw->mw_size[i] = hw->pci_dev->mem_resource[bar].len;
205 : : }
206 : :
207 : : /* Reserve the last 2 spad registers for users. */
208 [ # # ]: 0 : for (i = 0; i < NTB_SPAD_USER_MAX_NUM; i++)
209 : 0 : hw->spad_user_list[i] = hw->spad_cnt;
210 : 0 : hw->spad_user_list[0] = hw->spad_cnt - 2;
211 : 0 : hw->spad_user_list[1] = hw->spad_cnt - 1;
212 : :
213 : 0 : return 0;
214 : : }
215 : :
216 : : static void *
217 : 0 : intel_ntb_get_peer_mw_addr(const struct rte_rawdev *dev, int mw_idx)
218 : : {
219 : 0 : struct ntb_hw *hw = dev->dev_private;
220 : : uint8_t bar;
221 : :
222 [ # # ]: 0 : if (hw == NULL) {
223 : 0 : NTB_LOG(ERR, "Invalid device.");
224 : 0 : return 0;
225 : : }
226 : :
227 [ # # # # ]: 0 : if (mw_idx < 0 || mw_idx >= hw->mw_cnt) {
228 : 0 : NTB_LOG(ERR, "Invalid memory window index (0 - %u).",
229 : : hw->mw_cnt - 1);
230 : 0 : return 0;
231 : : }
232 : :
233 : 0 : bar = intel_ntb_bar[mw_idx];
234 : :
235 : 0 : return hw->pci_dev->mem_resource[bar].addr;
236 : : }
237 : :
238 : : static int
239 : 0 : intel_ntb_mw_set_trans(const struct rte_rawdev *dev, int mw_idx,
240 : : uint64_t addr, uint64_t size)
241 : : {
242 : 0 : struct ntb_hw *hw = dev->dev_private;
243 : : void *xlat_addr, *limit_addr;
244 : : uint64_t xlat_off, limit_off;
245 : : uint64_t base, limit;
246 : : uint8_t bar;
247 : :
248 [ # # ]: 0 : if (hw == NULL) {
249 : 0 : NTB_LOG(ERR, "Invalid device.");
250 : 0 : return -EINVAL;
251 : : }
252 : :
253 [ # # # # ]: 0 : if (mw_idx < 0 || mw_idx >= hw->mw_cnt) {
254 : 0 : NTB_LOG(ERR, "Invalid memory window index (0 - %u).",
255 : : hw->mw_cnt - 1);
256 : 0 : return -EINVAL;
257 : : }
258 : :
259 : 0 : bar = intel_ntb_bar[mw_idx];
260 : :
261 : 0 : xlat_off = XEON_IMBAR1XBASE_OFFSET + mw_idx * XEON_BAR_INTERVAL_OFFSET;
262 : 0 : limit_off = XEON_IMBAR1XLMT_OFFSET + mw_idx * XEON_BAR_INTERVAL_OFFSET;
263 : 0 : xlat_addr = hw->hw_addr + xlat_off;
264 : 0 : limit_addr = hw->hw_addr + limit_off;
265 : :
266 : : /* Limit reg val should be EMBAR base address plus MW size. */
267 : : base = addr;
268 : 0 : limit = hw->pci_dev->mem_resource[bar].phys_addr + size;
269 : : rte_write64(base, xlat_addr);
270 : : rte_write64(limit, limit_addr);
271 : :
272 : : if (is_gen3_ntb(hw)) {
273 : : /* Setup the external point so that remote can access. */
274 : 0 : xlat_off = XEON_EMBAR1_OFFSET + 8 * mw_idx;
275 : 0 : xlat_addr = hw->hw_addr + xlat_off;
276 : 0 : limit_off = XEON_EMBAR1XLMT_OFFSET +
277 : : mw_idx * XEON_BAR_INTERVAL_OFFSET;
278 : 0 : limit_addr = hw->hw_addr + limit_off;
279 : : base = rte_read64(xlat_addr);
280 : 0 : base &= ~0xf;
281 : 0 : limit = base + size;
282 : : rte_write64(limit, limit_addr);
283 : : } else if (is_gen4_ntb(hw)) {
284 : : /* Set translate base address index register */
285 : 0 : xlat_off = XEON_GEN4_IM1XBASEIDX_OFFSET +
286 : : mw_idx * XEON_GEN4_XBASEIDX_INTERVAL;
287 [ # # ]: 0 : xlat_addr = hw->hw_addr + xlat_off;
288 : 0 : rte_write16(rte_log2_u64(size), xlat_addr);
289 : : } else {
290 : 0 : NTB_LOG(ERR, "Cannot set translation of memory windows for unsupported device.");
291 : : rte_write64(base, limit_addr);
292 : : rte_write64(0, xlat_addr);
293 : 0 : return -ENOTSUP;
294 : : }
295 : :
296 : : return 0;
297 : : }
298 : :
299 : : static void *
300 : 0 : intel_ntb_ioremap(const struct rte_rawdev *dev, uint64_t addr)
301 : : {
302 : 0 : struct ntb_hw *hw = dev->dev_private;
303 : : void *mapped = NULL;
304 : : void *base;
305 : : int i;
306 : :
307 [ # # ]: 0 : for (i = 0; i < hw->peer_used_mws; i++) {
308 [ # # ]: 0 : if (addr >= hw->peer_mw_base[i] &&
309 [ # # ]: 0 : addr < hw->peer_mw_base[i] + hw->mw_size[i]) {
310 : 0 : base = intel_ntb_get_peer_mw_addr(dev, i);
311 : 0 : mapped = (void *)(size_t)(addr - hw->peer_mw_base[i] +
312 : 0 : (size_t)base);
313 : 0 : break;
314 : : }
315 : : }
316 : :
317 : 0 : return mapped;
318 : : }
319 : :
320 : : static int
321 : 0 : intel_ntb_get_link_status(const struct rte_rawdev *dev)
322 : : {
323 : 0 : struct ntb_hw *hw = dev->dev_private;
324 : : uint16_t reg_val, reg_off;
325 : : int ret;
326 : :
327 [ # # ]: 0 : if (hw == NULL) {
328 : 0 : NTB_LOG(ERR, "Invalid device.");
329 : 0 : return -EINVAL;
330 : : }
331 : :
332 : : if (is_gen3_ntb(hw)) {
333 : : reg_off = XEON_GEN3_LINK_STATUS_OFFSET;
334 : 0 : ret = rte_pci_read_config(hw->pci_dev, ®_val,
335 : : sizeof(reg_val), reg_off);
336 [ # # ]: 0 : if (ret < 0) {
337 : 0 : NTB_LOG(ERR, "Unable to get link status.");
338 : 0 : return -EIO;
339 : : }
340 : : } else if (is_gen4_ntb(hw)) {
341 : : reg_off = XEON_GEN4_LINK_STATUS_OFFSET;
342 : 0 : reg_val = rte_read16(hw->hw_addr + reg_off);
343 : : } else {
344 : 0 : NTB_LOG(ERR, "Cannot get link status for unsupported device.");
345 : 0 : return -ENOTSUP;
346 : : }
347 : :
348 : 0 : hw->link_status = NTB_LNK_STA_ACTIVE(reg_val);
349 : :
350 [ # # ]: 0 : if (hw->link_status) {
351 : 0 : hw->link_speed = NTB_LNK_STA_SPEED(reg_val);
352 : 0 : hw->link_width = NTB_LNK_STA_WIDTH(reg_val);
353 : : } else {
354 : 0 : hw->link_speed = NTB_SPEED_NONE;
355 : 0 : hw->link_width = NTB_WIDTH_NONE;
356 : : }
357 : :
358 : : return 0;
359 : : }
360 : :
361 : : static int
362 : : intel_ntb_gen3_set_link(const struct ntb_hw *hw, bool up)
363 : : {
364 : : uint32_t ntb_ctrl, reg_off;
365 : : void *reg_addr;
366 : :
367 : : reg_off = XEON_NTBCNTL_OFFSET;
368 : 0 : reg_addr = hw->hw_addr + reg_off;
369 : : ntb_ctrl = rte_read32(reg_addr);
370 : :
371 [ # # ]: 0 : if (up) {
372 : 0 : ntb_ctrl &= ~(NTB_CTL_DISABLE | NTB_CTL_CFG_LOCK);
373 : : ntb_ctrl |= NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP;
374 : 0 : ntb_ctrl |= NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP;
375 : : } else {
376 : : ntb_ctrl &= ~(NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP);
377 : 0 : ntb_ctrl &= ~(NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP);
378 : 0 : ntb_ctrl |= NTB_CTL_DISABLE | NTB_CTL_CFG_LOCK;
379 : : }
380 : :
381 : : rte_write32(ntb_ctrl, reg_addr);
382 : :
383 : : return 0;
384 : : }
385 : :
386 : : static int
387 : 0 : intel_ntb_gen4_set_link(const struct ntb_hw *hw, bool up)
388 : : {
389 : : uint32_t ntb_ctrl, ppd0;
390 : : uint16_t link_ctrl;
391 : : void *reg_addr;
392 : :
393 [ # # ]: 0 : if (up) {
394 : 0 : reg_addr = hw->hw_addr + XEON_NTBCNTL_OFFSET;
395 : : ntb_ctrl = NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP;
396 : : ntb_ctrl |= NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP;
397 : : rte_write32(ntb_ctrl, reg_addr);
398 : :
399 : 0 : reg_addr = hw->hw_addr + XEON_GEN4_LINK_CTRL_OFFSET;
400 : : link_ctrl = rte_read16(reg_addr);
401 : 0 : link_ctrl &= ~XEON_GEN4_LINK_CTRL_LINK_DIS;
402 : : rte_write16(link_ctrl, reg_addr);
403 : :
404 : : /* start link training */
405 : 0 : reg_addr = hw->hw_addr + XEON_GEN4_PPD0_OFFSET;
406 : : ppd0 = rte_read32(reg_addr);
407 : 0 : ppd0 |= XEON_GEN4_PPD_LINKTRN;
408 : : rte_write32(ppd0, reg_addr);
409 : :
410 : : /* make sure link training has started */
411 : : ppd0 = rte_read32(reg_addr);
412 [ # # ]: 0 : if (!(ppd0 & XEON_GEN4_PPD_LINKTRN)) {
413 : 0 : NTB_LOG(ERR, "Link is not training.");
414 : 0 : return -EINVAL;
415 : : }
416 : : } else {
417 : 0 : reg_addr = hw->hw_addr + XEON_NTBCNTL_OFFSET;
418 : : ntb_ctrl = rte_read32(reg_addr);
419 : : ntb_ctrl &= ~(NTB_CTL_P2S_BAR2_SNOOP | NTB_CTL_S2P_BAR2_SNOOP);
420 : 0 : ntb_ctrl &= ~(NTB_CTL_P2S_BAR4_SNOOP | NTB_CTL_S2P_BAR4_SNOOP);
421 : : rte_write32(ntb_ctrl, reg_addr);
422 : :
423 : 0 : reg_addr = hw->hw_addr + XEON_GEN4_LINK_CTRL_OFFSET;
424 : : link_ctrl = rte_read16(reg_addr);
425 : 0 : link_ctrl |= XEON_GEN4_LINK_CTRL_LINK_DIS;
426 : : rte_write16(link_ctrl, reg_addr);
427 : : }
428 : :
429 : : return 0;
430 : : }
431 : :
432 : : static int
433 : 0 : intel_ntb_set_link(const struct rte_rawdev *dev, bool up)
434 : : {
435 [ # # ]: 0 : struct ntb_hw *hw = dev->dev_private;
436 : : int ret = 0;
437 : :
438 : : if (is_gen3_ntb(hw))
439 : : ret = intel_ntb_gen3_set_link(hw, up);
440 : : else if (is_gen4_ntb(hw))
441 : 0 : ret = intel_ntb_gen4_set_link(hw, up);
442 : : else {
443 : 0 : NTB_LOG(ERR, "Cannot set link for unsupported device.");
444 : : ret = -ENOTSUP;
445 : : }
446 : :
447 : 0 : return ret;
448 : : }
449 : :
450 : : static uint32_t
451 : 0 : intel_ntb_spad_read(const struct rte_rawdev *dev, int spad, bool peer)
452 : : {
453 : 0 : struct ntb_hw *hw = dev->dev_private;
454 : : uint32_t spad_v, reg_off;
455 : : void *reg_addr;
456 : :
457 [ # # # # ]: 0 : if (spad < 0 || spad >= hw->spad_cnt) {
458 : 0 : NTB_LOG(ERR, "Invalid spad reg index.");
459 : 0 : return 0;
460 : : }
461 : :
462 : : /* When peer is true, read peer spad reg */
463 : : if (is_gen3_ntb(hw))
464 [ # # ]: 0 : reg_off = peer ? XEON_GEN3_B2B_SPAD_OFFSET :
465 : : XEON_IM_SPAD_OFFSET;
466 : : else if (is_gen4_ntb(hw))
467 [ # # ]: 0 : reg_off = peer ? XEON_GEN4_B2B_SPAD_OFFSET :
468 : : XEON_IM_SPAD_OFFSET;
469 : : else {
470 : 0 : NTB_LOG(ERR, "Cannot read spad for unsupported device.");
471 : 0 : return -ENOTSUP;
472 : : }
473 : 0 : reg_addr = hw->hw_addr + reg_off + (spad << 2);
474 : : spad_v = rte_read32(reg_addr);
475 : :
476 : 0 : return spad_v;
477 : : }
478 : :
479 : : static int
480 : 0 : intel_ntb_spad_write(const struct rte_rawdev *dev, int spad,
481 : : bool peer, uint32_t spad_v)
482 : : {
483 : 0 : struct ntb_hw *hw = dev->dev_private;
484 : : uint32_t reg_off;
485 : : void *reg_addr;
486 : :
487 [ # # # # ]: 0 : if (spad < 0 || spad >= hw->spad_cnt) {
488 : 0 : NTB_LOG(ERR, "Invalid spad reg index.");
489 : 0 : return -EINVAL;
490 : : }
491 : :
492 : : /* When peer is true, write peer spad reg */
493 : : if (is_gen3_ntb(hw))
494 [ # # ]: 0 : reg_off = peer ? XEON_GEN3_B2B_SPAD_OFFSET :
495 : : XEON_IM_SPAD_OFFSET;
496 : : else if (is_gen4_ntb(hw))
497 [ # # ]: 0 : reg_off = peer ? XEON_GEN4_B2B_SPAD_OFFSET :
498 : : XEON_IM_SPAD_OFFSET;
499 : : else {
500 : 0 : NTB_LOG(ERR, "Cannot write spad for unsupported device.");
501 : 0 : return -ENOTSUP;
502 : : }
503 : 0 : reg_addr = hw->hw_addr + reg_off + (spad << 2);
504 : :
505 : : rte_write32(spad_v, reg_addr);
506 : :
507 : 0 : return 0;
508 : : }
509 : :
510 : : static uint64_t
511 : 0 : intel_ntb_db_read(const struct rte_rawdev *dev)
512 : : {
513 : 0 : struct ntb_hw *hw = dev->dev_private;
514 : : uint64_t db_off, db_bits;
515 : : void *db_addr;
516 : :
517 : : db_off = XEON_IM_INT_STATUS_OFFSET;
518 : 0 : db_addr = hw->hw_addr + db_off;
519 : :
520 : : db_bits = rte_read64(db_addr);
521 : :
522 : 0 : return db_bits;
523 : : }
524 : :
525 : : static int
526 : 0 : intel_ntb_db_clear(const struct rte_rawdev *dev, uint64_t db_bits)
527 : : {
528 : 0 : struct ntb_hw *hw = dev->dev_private;
529 : : uint64_t db_off;
530 : : void *db_addr;
531 : :
532 : : db_off = XEON_IM_INT_STATUS_OFFSET;
533 [ # # ]: 0 : db_addr = hw->hw_addr + db_off;
534 : :
535 : : if (is_gen4_ntb(hw))
536 : : rte_write16(XEON_GEN4_SLOTSTS_DLLSCS,
537 : : hw->hw_addr + XEON_GEN4_SLOTSTS);
538 : : rte_write64(db_bits, db_addr);
539 : :
540 : 0 : return 0;
541 : : }
542 : :
543 : : static int
544 : 0 : intel_ntb_db_set_mask(const struct rte_rawdev *dev, uint64_t db_mask)
545 : : {
546 : 0 : struct ntb_hw *hw = dev->dev_private;
547 : : uint64_t db_m_off;
548 : : void *db_m_addr;
549 : :
550 : : db_m_off = XEON_IM_INT_DISABLE_OFFSET;
551 : 0 : db_m_addr = hw->hw_addr + db_m_off;
552 : :
553 : 0 : db_mask |= hw->db_mask;
554 : :
555 : : rte_write64(db_mask, db_m_addr);
556 : :
557 : 0 : hw->db_mask = db_mask;
558 : :
559 : 0 : return 0;
560 : : }
561 : :
562 : : static int
563 : 0 : intel_ntb_peer_db_set(const struct rte_rawdev *dev, uint8_t db_idx)
564 : : {
565 : 0 : struct ntb_hw *hw = dev->dev_private;
566 : : uint32_t db_off;
567 : : void *db_addr;
568 : :
569 [ # # ]: 0 : if (((uint64_t)1 << db_idx) & ~hw->db_valid_mask) {
570 : 0 : NTB_LOG(ERR, "Invalid doorbell.");
571 : 0 : return -EINVAL;
572 : : }
573 : :
574 : 0 : db_off = XEON_IM_DOORBELL_OFFSET + db_idx * 4;
575 : 0 : db_addr = hw->hw_addr + db_off;
576 : :
577 : : rte_write32(1, db_addr);
578 : :
579 : 0 : return 0;
580 : : }
581 : :
582 : : static int
583 : 0 : intel_ntb_vector_bind(const struct rte_rawdev *dev, uint8_t intr, uint8_t msix)
584 : : {
585 : 0 : struct ntb_hw *hw = dev->dev_private;
586 : : uint8_t reg_off;
587 : : void *reg_addr;
588 : :
589 [ # # ]: 0 : if (intr >= hw->db_cnt) {
590 : 0 : NTB_LOG(ERR, "Invalid intr source.");
591 : 0 : return -EINVAL;
592 : : }
593 : :
594 : : /* Bind intr source to msix vector */
595 : : if (is_gen3_ntb(hw))
596 : : reg_off = XEON_GEN3_INTVEC_OFFSET;
597 : : else if (is_gen4_ntb(hw))
598 : : reg_off = XEON_GEN4_INTVEC_OFFSET;
599 : : else {
600 : 0 : NTB_LOG(ERR, "Cannot bind vectors for unsupported device.");
601 : 0 : return -ENOTSUP;
602 : : }
603 : 0 : reg_addr = hw->hw_addr + reg_off + intr;
604 : :
605 : : rte_write8(msix, reg_addr);
606 : :
607 : 0 : return 0;
608 : : }
609 : :
610 : : /* Advertise the local configuration to the peer and program the memory
611 : : * windows. Scratchpad writes and translations only take effect once both
612 : : * sides are up, so this is also re-run from the ready doorbell.
613 : : */
614 : : static int
615 : 0 : intel_ntb_dev_handshake(const struct rte_rawdev *dev)
616 : : {
617 : 0 : struct ntb_hw *hw = dev->dev_private;
618 : : uint32_t val;
619 : : int ret, i;
620 : :
621 : : /* Tell peer the mw info of local side. */
622 : 0 : ret = intel_ntb_spad_write(dev, SPAD_NUM_MWS, 1, hw->mw_cnt);
623 [ # # ]: 0 : if (ret < 0)
624 : : return ret;
625 [ # # ]: 0 : for (i = 0; i < hw->mw_cnt; i++) {
626 : 0 : NTB_LOG(INFO, "Local %u mw size: 0x%"PRIx64"", i, hw->mw_size[i]);
627 : 0 : val = hw->mw_size[i] >> 32;
628 : 0 : ret = intel_ntb_spad_write(dev, SPAD_MW0_SZ_H + 2 * i, 1, val);
629 [ # # ]: 0 : if (ret < 0)
630 : 0 : return ret;
631 : 0 : val = hw->mw_size[i];
632 : 0 : ret = intel_ntb_spad_write(dev, SPAD_MW0_SZ_L + 2 * i, 1, val);
633 [ # # ]: 0 : if (ret < 0)
634 : 0 : return ret;
635 : : }
636 : :
637 : : /* Tell peer about the queue info and map memory to the peer. */
638 : 0 : ret = intel_ntb_spad_write(dev, SPAD_Q_SZ, 1, hw->queue_size);
639 [ # # ]: 0 : if (ret < 0)
640 : : return ret;
641 : 0 : ret = intel_ntb_spad_write(dev, SPAD_NUM_QPS, 1, hw->queue_pairs);
642 [ # # ]: 0 : if (ret < 0)
643 : : return ret;
644 : 0 : ret = intel_ntb_spad_write(dev, SPAD_USED_MWS, 1, hw->used_mw_num);
645 [ # # ]: 0 : if (ret < 0)
646 : : return ret;
647 [ # # ]: 0 : for (i = 0; i < hw->used_mw_num; i++) {
648 : 0 : val = (uint64_t)(size_t)(hw->mz[i]->addr) >> 32;
649 : 0 : ret = intel_ntb_spad_write(dev, SPAD_MW0_BA_H + 2 * i, 1, val);
650 [ # # ]: 0 : if (ret < 0)
651 : 0 : return ret;
652 : 0 : val = (uint64_t)(size_t)(hw->mz[i]->addr);
653 : 0 : ret = intel_ntb_spad_write(dev, SPAD_MW0_BA_L + 2 * i, 1, val);
654 [ # # ]: 0 : if (ret < 0)
655 : 0 : return ret;
656 : : }
657 : :
658 [ # # ]: 0 : for (i = 0; i < hw->used_mw_num; i++) {
659 : 0 : ret = intel_ntb_mw_set_trans(dev, i, hw->mz[i]->iova, hw->mz[i]->len);
660 [ # # ]: 0 : if (ret < 0)
661 : 0 : return ret;
662 : : }
663 : :
664 : : /* Ring doorbell 0 to tell peer the device is ready. */
665 : 0 : return intel_ntb_peer_db_set(dev, 0);
666 : : }
667 : :
668 : : /* Check that the peer advertises the same memory-window layout as local. */
669 : : static int
670 : 0 : intel_ntb_peer_mw_validate(const struct rte_rawdev *dev)
671 : : {
672 : 0 : struct ntb_hw *hw = dev->dev_private;
673 : : uint64_t peer_mw_size;
674 : : uint32_t val_h, val_l;
675 : : uint8_t peer_mw_cnt;
676 : : int i;
677 : :
678 : 0 : peer_mw_cnt = intel_ntb_spad_read(dev, SPAD_NUM_MWS, 0);
679 [ # # ]: 0 : if (peer_mw_cnt != hw->mw_cnt) {
680 : 0 : NTB_LOG(ERR, "Both mw cnt must be the same.");
681 : 0 : return -EINVAL;
682 : : }
683 : :
684 [ # # ]: 0 : for (i = 0; i < hw->mw_cnt; i++) {
685 : 0 : val_h = intel_ntb_spad_read(dev, SPAD_MW0_SZ_H + 2 * i, 0);
686 : 0 : val_l = intel_ntb_spad_read(dev, SPAD_MW0_SZ_L + 2 * i, 0);
687 : 0 : peer_mw_size = ((uint64_t)val_h << 32) | val_l;
688 : 0 : NTB_LOG(DEBUG, "Peer %u mw size: 0x%"PRIx64"", i, peer_mw_size);
689 [ # # ]: 0 : if (peer_mw_size != hw->mw_size[i]) {
690 : 0 : NTB_LOG(ERR, "Mw config must be the same.");
691 : 0 : return -EINVAL;
692 : : }
693 : : }
694 : :
695 : : return 0;
696 : : }
697 : :
698 : : /* Read and validate the peer configuration recorded in the scratchpad. */
699 : : static int
700 : 0 : intel_ntb_read_peer_config(const struct rte_rawdev *dev)
701 : : {
702 : 0 : struct ntb_hw *hw = dev->dev_private;
703 : : uint32_t peer_base_l, peer_val;
704 : : uint64_t peer_base_h;
705 : : int i;
706 : :
707 : 0 : peer_val = intel_ntb_spad_read(dev, SPAD_Q_SZ, 0);
708 [ # # ]: 0 : if (peer_val != hw->queue_size) {
709 : 0 : NTB_LOG(ERR, "Inconsistent queue size! (local: %u peer: %u)",
710 : : hw->queue_size, peer_val);
711 : 0 : return -EINVAL;
712 : : }
713 : :
714 : 0 : peer_val = intel_ntb_spad_read(dev, SPAD_NUM_QPS, 0);
715 [ # # ]: 0 : if (peer_val != hw->queue_pairs) {
716 : 0 : NTB_LOG(ERR, "Inconsistent number of queues! (local: %u peer: %u)",
717 : : hw->queue_pairs, peer_val);
718 : 0 : return -EINVAL;
719 : : }
720 : :
721 : 0 : peer_val = intel_ntb_spad_read(dev, SPAD_USED_MWS, 0);
722 [ # # ]: 0 : if (peer_val > hw->mw_cnt) {
723 : 0 : NTB_LOG(ERR, "Peer used mws %u > mw cnt %u.", peer_val, hw->mw_cnt);
724 : 0 : return -EINVAL;
725 : : }
726 : 0 : hw->peer_used_mws = peer_val;
727 : :
728 [ # # ]: 0 : for (i = 0; i < hw->peer_used_mws; i++) {
729 : 0 : peer_base_h = intel_ntb_spad_read(dev, SPAD_MW0_BA_H + 2 * i, 0);
730 : 0 : peer_base_l = intel_ntb_spad_read(dev, SPAD_MW0_BA_L + 2 * i, 0);
731 : 0 : hw->peer_mw_base[i] = (peer_base_h << 32) + peer_base_l;
732 : : }
733 : :
734 : : return 0;
735 : : }
736 : :
737 : : /* Tear down the local scratchpad and translations so the peer can no
738 : : * longer reach local memory.
739 : : */
740 : : static void
741 : 0 : intel_ntb_link_cleanup(const struct rte_rawdev *dev)
742 : : {
743 : 0 : struct ntb_hw *hw = dev->dev_private;
744 : : int status, i;
745 : :
746 : : /* Clean spad registers. */
747 [ # # ]: 0 : for (i = 0; i < hw->spad_cnt; i++) {
748 : 0 : status = intel_ntb_spad_write(dev, i, 0, 0);
749 [ # # ]: 0 : if (status)
750 : 0 : NTB_LOG(ERR, "Failed to clean local spad.");
751 : : }
752 : :
753 : : /* Clear mw so that peer cannot access local memory. */
754 [ # # ]: 0 : for (i = 0; i < hw->used_mw_num; i++) {
755 : 0 : status = intel_ntb_mw_set_trans(dev, i, 0, 0);
756 [ # # ]: 0 : if (status)
757 : 0 : NTB_LOG(ERR, "Failed to clean mw.");
758 : : }
759 : 0 : }
760 : :
761 : : static uint64_t
762 : 0 : intel_ntb_get_mem_align(const struct rte_rawdev *dev, uint32_t mw_id, uint64_t mw_len __rte_unused)
763 : : {
764 : 0 : struct ntb_hw *hw = dev->dev_private;
765 : :
766 : : /* The memzone base must be aligned to the memory window size. */
767 : 0 : return hw->mw_size[mw_id];
768 : : }
769 : :
770 : : /* operations for primary side of local ntb */
771 : : const struct ntb_dev_ops intel_ntb_ops = {
772 : : .ntb_dev_init = intel_ntb_dev_init,
773 : : .get_peer_mw_addr = intel_ntb_get_peer_mw_addr,
774 : : .ioremap = intel_ntb_ioremap,
775 : : .get_link_status = intel_ntb_get_link_status,
776 : : .set_link = intel_ntb_set_link,
777 : : .spad_read = intel_ntb_spad_read,
778 : : .spad_write = intel_ntb_spad_write,
779 : : .db_read = intel_ntb_db_read,
780 : : .db_clear = intel_ntb_db_clear,
781 : : .db_set_mask = intel_ntb_db_set_mask,
782 : : .peer_db_set = intel_ntb_peer_db_set,
783 : : .vector_bind = intel_ntb_vector_bind,
784 : : .dev_handshake = intel_ntb_dev_handshake,
785 : : .peer_mw_validate = intel_ntb_peer_mw_validate,
786 : : .read_peer_config = intel_ntb_read_peer_config,
787 : : .link_cleanup = intel_ntb_link_cleanup,
788 : : .mem_align = intel_ntb_get_mem_align,
789 : : };
|