]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ntb/if_ntb/if_ntb.c
Re-enable write combining, disabled by default at r295486.
[FreeBSD/FreeBSD.git] / sys / dev / ntb / if_ntb / if_ntb.c
1 /*-
2  * Copyright (C) 2013 Intel Corporation
3  * Copyright (C) 2015 EMC Corporation
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27
28 #include <sys/cdefs.h>
29 __FBSDID("$FreeBSD$");
30
31 #include <sys/param.h>
32 #include <sys/kernel.h>
33 #include <sys/systm.h>
34 #include <sys/bitset.h>
35 #include <sys/bus.h>
36 #include <sys/ktr.h>
37 #include <sys/limits.h>
38 #include <sys/lock.h>
39 #include <sys/malloc.h>
40 #include <sys/module.h>
41 #include <sys/mutex.h>
42 #include <sys/queue.h>
43 #include <sys/socket.h>
44 #include <sys/sockio.h>
45 #include <sys/sysctl.h>
46 #include <sys/taskqueue.h>
47
48 #include <net/if.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51 #include <net/if_var.h>
52 #include <net/bpf.h>
53 #include <net/ethernet.h>
54
55 #include <vm/vm.h>
56 #include <vm/pmap.h>
57
58 #include <machine/bus.h>
59 #include <machine/cpufunc.h>
60
61 #include <netinet/in.h>
62 #include <netinet/ip.h>
63
64 #include "../ntb_hw/ntb_hw.h"
65
66 /*
67  * The Non-Transparent Bridge (NTB) is a device on some Intel processors that
68  * allows you to connect two systems using a PCI-e link.
69  *
70  * This module contains a protocol for sending and receiving messages, and
71  * exposes that protocol through a simulated ethernet device called ntb.
72  *
73  * NOTE: Much of the code in this module is shared with Linux. Any patches may
74  * be picked up and redistributed in Linux with a dual GPL/BSD license.
75  */
76
77 #define QP_SETSIZE      64
78 BITSET_DEFINE(_qpset, QP_SETSIZE);
79 #define test_bit(pos, addr)     BIT_ISSET(QP_SETSIZE, (pos), (addr))
80 #define set_bit(pos, addr)      BIT_SET(QP_SETSIZE, (pos), (addr))
81 #define clear_bit(pos, addr)    BIT_CLR(QP_SETSIZE, (pos), (addr))
82 #define ffs_bit(addr)           BIT_FFS(QP_SETSIZE, (addr))
83
84 #define KTR_NTB KTR_SPARE3
85
86 #define NTB_TRANSPORT_VERSION   4
87 #define NTB_RX_MAX_PKTS         64
88 #define NTB_RXQ_SIZE            300
89
90 enum ntb_link_event {
91         NTB_LINK_DOWN = 0,
92         NTB_LINK_UP,
93 };
94
95 static SYSCTL_NODE(_hw, OID_AUTO, if_ntb, CTLFLAG_RW, 0, "if_ntb");
96
97 static unsigned g_if_ntb_debug_level;
98 SYSCTL_UINT(_hw_if_ntb, OID_AUTO, debug_level, CTLFLAG_RWTUN,
99     &g_if_ntb_debug_level, 0, "if_ntb log level -- higher is more verbose");
100 #define ntb_printf(lvl, ...) do {                       \
101         if ((lvl) <= g_if_ntb_debug_level) {            \
102                 if_printf(nt->ifp, __VA_ARGS__);        \
103         }                                               \
104 } while (0)
105
106 static unsigned transport_mtu = IP_MAXPACKET + ETHER_HDR_LEN + ETHER_CRC_LEN;
107
108 static uint64_t max_mw_size;
109 SYSCTL_UQUAD(_hw_if_ntb, OID_AUTO, max_mw_size, CTLFLAG_RDTUN, &max_mw_size, 0,
110     "If enabled (non-zero), limit the size of large memory windows. "
111     "Both sides of the NTB MUST set the same value here.");
112
113 static unsigned max_num_clients;
114 SYSCTL_UINT(_hw_if_ntb, OID_AUTO, max_num_clients, CTLFLAG_RDTUN,
115     &max_num_clients, 0, "Maximum number of NTB transport clients.  "
116     "0 (default) - use all available NTB memory windows; "
117     "positive integer N - Limit to N memory windows.");
118
119 static unsigned enable_xeon_watchdog;
120 SYSCTL_UINT(_hw_if_ntb, OID_AUTO, enable_xeon_watchdog, CTLFLAG_RDTUN,
121     &enable_xeon_watchdog, 0, "If non-zero, write a register every second to "
122     "keep a watchdog from tearing down the NTB link");
123
124 STAILQ_HEAD(ntb_queue_list, ntb_queue_entry);
125
126 typedef uint32_t ntb_q_idx_t;
127
128 struct ntb_queue_entry {
129         /* ntb_queue list reference */
130         STAILQ_ENTRY(ntb_queue_entry) entry;
131
132         /* info on data to be transferred */
133         void            *cb_data;
134         void            *buf;
135         uint32_t        len;
136         uint32_t        flags;
137
138         struct ntb_transport_qp         *qp;
139         struct ntb_payload_header       *x_hdr;
140         ntb_q_idx_t     index;
141 };
142
143 struct ntb_rx_info {
144         ntb_q_idx_t     entry;
145 };
146
147 struct ntb_transport_qp {
148         struct ntb_transport_ctx        *transport;
149         struct ntb_softc        *ntb;
150
151         void                    *cb_data;
152
153         bool                    client_ready;
154         volatile bool           link_is_up;
155         uint8_t                 qp_num; /* Only 64 QPs are allowed.  0-63 */
156
157         struct ntb_rx_info      *rx_info;
158         struct ntb_rx_info      *remote_rx_info;
159
160         void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
161             void *data, int len);
162         struct ntb_queue_list   tx_free_q;
163         struct mtx              ntb_tx_free_q_lock;
164         caddr_t                 tx_mw;
165         bus_addr_t              tx_mw_phys;
166         ntb_q_idx_t             tx_index;
167         ntb_q_idx_t             tx_max_entry;
168         uint64_t                tx_max_frame;
169
170         void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
171             void *data, int len);
172         struct ntb_queue_list   rx_post_q;
173         struct ntb_queue_list   rx_pend_q;
174         /* ntb_rx_q_lock: synchronize access to rx_XXXX_q */
175         struct mtx              ntb_rx_q_lock;
176         struct task             rx_completion_task;
177         struct task             rxc_db_work;
178         caddr_t                 rx_buff;
179         ntb_q_idx_t             rx_index;
180         ntb_q_idx_t             rx_max_entry;
181         uint64_t                rx_max_frame;
182
183         void (*event_handler)(void *data, enum ntb_link_event status);
184         struct callout          link_work;
185         struct callout          queue_full;
186         struct callout          rx_full;
187
188         uint64_t                last_rx_no_buf;
189
190         /* Stats */
191         uint64_t                rx_bytes;
192         uint64_t                rx_pkts;
193         uint64_t                rx_ring_empty;
194         uint64_t                rx_err_no_buf;
195         uint64_t                rx_err_oflow;
196         uint64_t                rx_err_ver;
197         uint64_t                tx_bytes;
198         uint64_t                tx_pkts;
199         uint64_t                tx_ring_full;
200         uint64_t                tx_err_no_buf;
201 };
202
203 struct ntb_queue_handlers {
204         void (*rx_handler)(struct ntb_transport_qp *qp, void *qp_data,
205             void *data, int len);
206         void (*tx_handler)(struct ntb_transport_qp *qp, void *qp_data,
207             void *data, int len);
208         void (*event_handler)(void *data, enum ntb_link_event status);
209 };
210
211 struct ntb_transport_mw {
212         vm_paddr_t      phys_addr;
213         size_t          phys_size;
214         size_t          xlat_align;
215         size_t          xlat_align_size;
216         bus_addr_t      addr_limit;
217         /* Tx buff is off vbase / phys_addr */
218         caddr_t         vbase;
219         size_t          xlat_size;
220         size_t          buff_size;
221         /* Rx buff is off virt_addr / dma_addr */
222         caddr_t         virt_addr;
223         bus_addr_t      dma_addr;
224 };
225
226 struct ntb_transport_ctx {
227         struct ntb_softc        *ntb;
228         struct ifnet            *ifp;
229         struct ntb_transport_mw mw_vec[NTB_MAX_NUM_MW];
230         struct ntb_transport_qp *qp_vec;
231         struct _qpset           qp_bitmap;
232         struct _qpset           qp_bitmap_free;
233         unsigned                mw_count;
234         unsigned                qp_count;
235         volatile bool           link_is_up;
236         struct callout          link_work;
237         struct callout          link_watchdog;
238         struct task             link_cleanup;
239         uint64_t                bufsize;
240         u_char                  eaddr[ETHER_ADDR_LEN];
241         struct mtx              tx_lock;
242         struct mtx              rx_lock;
243
244         /* The hardcoded single queuepair in ntb_setup_interface() */
245         struct ntb_transport_qp *qp;
246 };
247
248 static struct ntb_transport_ctx net_softc;
249
250 enum {
251         IF_NTB_DESC_DONE_FLAG = 1 << 0,
252         IF_NTB_LINK_DOWN_FLAG = 1 << 1,
253 };
254
255 struct ntb_payload_header {
256         ntb_q_idx_t ver;
257         uint32_t len;
258         uint32_t flags;
259 };
260
261 enum {
262         /*
263          * The order of this enum is part of the if_ntb remote protocol.  Do
264          * not reorder without bumping protocol version (and it's probably best
265          * to keep the protocol in lock-step with the Linux NTB driver.
266          */
267         IF_NTB_VERSION = 0,
268         IF_NTB_QP_LINKS,
269         IF_NTB_NUM_QPS,
270         IF_NTB_NUM_MWS,
271         /*
272          * N.B.: transport_link_work assumes MW1 enums = MW0 + 2.
273          */
274         IF_NTB_MW0_SZ_HIGH,
275         IF_NTB_MW0_SZ_LOW,
276         IF_NTB_MW1_SZ_HIGH,
277         IF_NTB_MW1_SZ_LOW,
278         IF_NTB_MAX_SPAD,
279
280         /*
281          * Some NTB-using hardware have a watchdog to work around NTB hangs; if
282          * a register or doorbell isn't written every few seconds, the link is
283          * torn down.  Write an otherwise unused register every few seconds to
284          * work around this watchdog.
285          */
286         IF_NTB_WATCHDOG_SPAD = 15
287 };
288 CTASSERT(IF_NTB_WATCHDOG_SPAD < XEON_SPAD_COUNT &&
289     IF_NTB_WATCHDOG_SPAD < ATOM_SPAD_COUNT);
290
291 #define QP_TO_MW(nt, qp)        ((qp) % nt->mw_count)
292 #define NTB_QP_DEF_NUM_ENTRIES  100
293 #define NTB_LINK_DOWN_TIMEOUT   10
294
295 static int ntb_handle_module_events(struct module *m, int what, void *arg);
296 static int ntb_setup_interface(void);
297 static int ntb_teardown_interface(void);
298 static void ntb_net_init(void *arg);
299 static int ntb_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
300 static void ntb_start(struct ifnet *ifp);
301 static void ntb_net_tx_handler(struct ntb_transport_qp *qp, void *qp_data,
302     void *data, int len);
303 static void ntb_net_rx_handler(struct ntb_transport_qp *qp, void *qp_data,
304     void *data, int len);
305 static void ntb_net_event_handler(void *data, enum ntb_link_event status);
306 static int ntb_transport_probe(struct ntb_softc *ntb);
307 static void ntb_transport_free(struct ntb_transport_ctx *);
308 static void ntb_transport_init_queue(struct ntb_transport_ctx *nt,
309     unsigned int qp_num);
310 static void ntb_transport_free_queue(struct ntb_transport_qp *qp);
311 static struct ntb_transport_qp *ntb_transport_create_queue(void *data,
312     struct ntb_softc *pdev, const struct ntb_queue_handlers *handlers);
313 static void ntb_transport_link_up(struct ntb_transport_qp *qp);
314 static int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb,
315     void *data, unsigned int len);
316 static int ntb_process_tx(struct ntb_transport_qp *qp,
317     struct ntb_queue_entry *entry);
318 static void ntb_memcpy_tx(struct ntb_transport_qp *qp,
319     struct ntb_queue_entry *entry, void *offset);
320 static void ntb_qp_full(void *arg);
321 static void ntb_transport_rxc_db(void *arg, int pending);
322 static int ntb_process_rxc(struct ntb_transport_qp *qp);
323 static void ntb_memcpy_rx(struct ntb_transport_qp *qp,
324     struct ntb_queue_entry *entry, void *offset);
325 static inline void ntb_rx_copy_callback(struct ntb_transport_qp *qp,
326     void *data);
327 static void ntb_complete_rxc(void *arg, int pending);
328 static void ntb_transport_doorbell_callback(void *data, uint32_t vector);
329 static void ntb_transport_event_callback(void *data);
330 static void ntb_transport_link_work(void *arg);
331 static int ntb_set_mw(struct ntb_transport_ctx *, int num_mw, size_t size);
332 static void ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw);
333 static int ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt,
334     unsigned int qp_num);
335 static void ntb_qp_link_work(void *arg);
336 static void ntb_transport_link_cleanup(struct ntb_transport_ctx *nt);
337 static void ntb_transport_link_cleanup_work(void *, int);
338 static void ntb_qp_link_down(struct ntb_transport_qp *qp);
339 static void ntb_qp_link_down_reset(struct ntb_transport_qp *qp);
340 static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp);
341 static void ntb_transport_link_down(struct ntb_transport_qp *qp);
342 static void ntb_send_link_down(struct ntb_transport_qp *qp);
343 static void ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry,
344     struct ntb_queue_list *list);
345 static struct ntb_queue_entry *ntb_list_rm(struct mtx *lock,
346     struct ntb_queue_list *list);
347 static struct ntb_queue_entry *ntb_list_mv(struct mtx *lock,
348     struct ntb_queue_list *from, struct ntb_queue_list *to);
349 static void create_random_local_eui48(u_char *eaddr);
350 static unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp);
351 static void xeon_link_watchdog_hb(void *);
352
353 static const struct ntb_ctx_ops ntb_transport_ops = {
354         .link_event = ntb_transport_event_callback,
355         .db_event = ntb_transport_doorbell_callback,
356 };
357
358 MALLOC_DEFINE(M_NTB_IF, "if_ntb", "ntb network driver");
359
360 static inline void
361 iowrite32(uint32_t val, void *addr)
362 {
363
364         bus_space_write_4(X86_BUS_SPACE_MEM, 0/* HACK */, (uintptr_t)addr,
365             val);
366 }
367
368 /* Module setup and teardown */
369 static int
370 ntb_handle_module_events(struct module *m, int what, void *arg)
371 {
372         int err = 0;
373
374         switch (what) {
375         case MOD_LOAD:
376                 err = ntb_setup_interface();
377                 break;
378         case MOD_UNLOAD:
379                 err = ntb_teardown_interface();
380                 break;
381         default:
382                 err = EOPNOTSUPP;
383                 break;
384         }
385         return (err);
386 }
387
388 static moduledata_t if_ntb_mod = {
389         "if_ntb",
390         ntb_handle_module_events,
391         NULL
392 };
393
394 DECLARE_MODULE(if_ntb, if_ntb_mod, SI_SUB_KLD, SI_ORDER_ANY);
395 MODULE_DEPEND(if_ntb, ntb_hw, 1, 1, 1);
396
397 static int
398 ntb_setup_interface(void)
399 {
400         struct ifnet *ifp;
401         struct ntb_queue_handlers handlers = { ntb_net_rx_handler,
402             ntb_net_tx_handler, ntb_net_event_handler };
403         int rc;
404
405         net_softc.ntb = devclass_get_softc(devclass_find("ntb_hw"), 0);
406         if (net_softc.ntb == NULL) {
407                 printf("ntb: Cannot find devclass\n");
408                 return (ENXIO);
409         }
410
411         ifp = net_softc.ifp = if_alloc(IFT_ETHER);
412         if (ifp == NULL) {
413                 ntb_transport_free(&net_softc);
414                 printf("ntb: Cannot allocate ifnet structure\n");
415                 return (ENOMEM);
416         }
417         if_initname(ifp, "ntb", 0);
418
419         rc = ntb_transport_probe(net_softc.ntb);
420         if (rc != 0) {
421                 printf("ntb: Cannot init transport: %d\n", rc);
422                 if_free(net_softc.ifp);
423                 return (rc);
424         }
425
426         net_softc.qp = ntb_transport_create_queue(ifp, net_softc.ntb,
427             &handlers);
428         ifp->if_init = ntb_net_init;
429         ifp->if_softc = &net_softc;
430         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
431         ifp->if_ioctl = ntb_ioctl;
432         ifp->if_start = ntb_start;
433         IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
434         ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
435         IFQ_SET_READY(&ifp->if_snd);
436         create_random_local_eui48(net_softc.eaddr);
437         ether_ifattach(ifp, net_softc.eaddr);
438         ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_JUMBO_MTU;
439         ifp->if_capenable = ifp->if_capabilities;
440         ifp->if_mtu = ntb_transport_max_size(net_softc.qp) - ETHER_HDR_LEN -
441             ETHER_CRC_LEN;
442
443         ntb_transport_link_up(net_softc.qp);
444         net_softc.bufsize = ntb_transport_max_size(net_softc.qp) +
445             sizeof(struct ether_header);
446         return (0);
447 }
448
449 static int
450 ntb_teardown_interface(void)
451 {
452
453         if (net_softc.qp != NULL) {
454                 ntb_transport_link_down(net_softc.qp);
455
456                 ntb_transport_free_queue(net_softc.qp);
457                 ntb_transport_free(&net_softc);
458         }
459
460         if (net_softc.ifp != NULL) {
461                 ether_ifdetach(net_softc.ifp);
462                 if_free(net_softc.ifp);
463                 net_softc.ifp = NULL;
464         }
465
466         return (0);
467 }
468
469 /* Network device interface */
470
471 static void
472 ntb_net_init(void *arg)
473 {
474         struct ntb_transport_ctx *ntb_softc = arg;
475         struct ifnet *ifp = ntb_softc->ifp;
476
477         ifp->if_drv_flags |= IFF_DRV_RUNNING;
478         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
479         ifp->if_flags |= IFF_UP;
480         if_link_state_change(ifp, LINK_STATE_UP);
481 }
482
483 static int
484 ntb_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
485 {
486         struct ntb_transport_ctx *nt = ifp->if_softc;
487         struct ifreq *ifr = (struct ifreq *)data;
488         int error = 0;
489
490         switch (command) {
491         case SIOCSIFMTU:
492             {
493                 if (ifr->ifr_mtu > ntb_transport_max_size(nt->qp) -
494                     ETHER_HDR_LEN - ETHER_CRC_LEN) {
495                         error = EINVAL;
496                         break;
497                 }
498
499                 ifp->if_mtu = ifr->ifr_mtu;
500                 break;
501             }
502         default:
503                 error = ether_ioctl(ifp, command, data);
504                 break;
505         }
506
507         return (error);
508 }
509
510
511 static void
512 ntb_start(struct ifnet *ifp)
513 {
514         struct mbuf *m_head;
515         struct ntb_transport_ctx *nt = ifp->if_softc;
516         int rc;
517
518         mtx_lock(&nt->tx_lock);
519         ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
520         CTR0(KTR_NTB, "TX: ntb_start");
521         while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
522                 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
523                 CTR1(KTR_NTB, "TX: start mbuf %p", m_head);
524                 rc = ntb_transport_tx_enqueue(nt->qp, m_head, m_head,
525                              m_length(m_head, NULL));
526                 if (rc != 0) {
527                         CTR1(KTR_NTB,
528                             "TX: could not tx mbuf %p. Returning to snd q",
529                             m_head);
530                         if (rc == EAGAIN) {
531                                 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
532                                 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
533                                 callout_reset(&nt->qp->queue_full, hz / 1000,
534                                     ntb_qp_full, ifp);
535                         }
536                         break;
537                 }
538
539         }
540         mtx_unlock(&nt->tx_lock);
541 }
542
543 /* Network Device Callbacks */
544 static void
545 ntb_net_tx_handler(struct ntb_transport_qp *qp, void *qp_data, void *data,
546     int len)
547 {
548
549         m_freem(data);
550         CTR1(KTR_NTB, "TX: tx_handler freeing mbuf %p", data);
551 }
552
553 static void
554 ntb_net_rx_handler(struct ntb_transport_qp *qp, void *qp_data, void *data,
555     int len)
556 {
557         struct mbuf *m = data;
558         struct ifnet *ifp = qp_data;
559
560         CTR0(KTR_NTB, "RX: rx handler");
561         (*ifp->if_input)(ifp, m);
562 }
563
564 static void
565 ntb_net_event_handler(void *data, enum ntb_link_event status)
566 {
567         struct ifnet *ifp;
568
569         ifp = data;
570         (void)ifp;
571
572         /* XXX The Linux driver munges with the carrier status here. */
573
574         switch (status) {
575         case NTB_LINK_DOWN:
576                 break;
577         case NTB_LINK_UP:
578                 break;
579         default:
580                 panic("Bogus ntb_link_event %u\n", status);
581         }
582 }
583
584 /* Transport Init and teardown */
585
586 static void
587 xeon_link_watchdog_hb(void *arg)
588 {
589         struct ntb_transport_ctx *nt;
590
591         nt = arg;
592         ntb_spad_write(nt->ntb, IF_NTB_WATCHDOG_SPAD, 0);
593         callout_reset(&nt->link_watchdog, 1 * hz, xeon_link_watchdog_hb, nt);
594 }
595
596 static int
597 ntb_transport_probe(struct ntb_softc *ntb)
598 {
599         struct ntb_transport_ctx *nt = &net_softc;
600         struct ntb_transport_mw *mw;
601         uint64_t qp_bitmap;
602         int rc;
603         unsigned i;
604
605         nt->mw_count = ntb_mw_count(ntb);
606         for (i = 0; i < nt->mw_count; i++) {
607                 mw = &nt->mw_vec[i];
608
609                 rc = ntb_mw_get_range(ntb, i, &mw->phys_addr, &mw->vbase,
610                     &mw->phys_size, &mw->xlat_align, &mw->xlat_align_size,
611                     &mw->addr_limit);
612                 if (rc != 0)
613                         goto err;
614
615                 mw->buff_size = 0;
616                 mw->xlat_size = 0;
617                 mw->virt_addr = NULL;
618                 mw->dma_addr = 0;
619
620                 rc = ntb_mw_set_wc(nt->ntb, i, VM_MEMATTR_WRITE_COMBINING);
621                 if (rc)
622                         ntb_printf(0, "Unable to set mw%d caching\n", i);
623         }
624
625         qp_bitmap = ntb_db_valid_mask(ntb);
626         nt->qp_count = flsll(qp_bitmap);
627         KASSERT(nt->qp_count != 0, ("bogus db bitmap"));
628         nt->qp_count -= 1;
629
630         if (max_num_clients != 0 && max_num_clients < nt->qp_count)
631                 nt->qp_count = max_num_clients;
632         else if (nt->mw_count < nt->qp_count)
633                 nt->qp_count = nt->mw_count;
634         KASSERT(nt->qp_count <= QP_SETSIZE, ("invalid qp_count"));
635
636         mtx_init(&nt->tx_lock, "ntb transport tx", NULL, MTX_DEF);
637         mtx_init(&nt->rx_lock, "ntb transport rx", NULL, MTX_DEF);
638
639         nt->qp_vec = malloc(nt->qp_count * sizeof(*nt->qp_vec), M_NTB_IF,
640             M_WAITOK | M_ZERO);
641
642         for (i = 0; i < nt->qp_count; i++) {
643                 set_bit(i, &nt->qp_bitmap);
644                 set_bit(i, &nt->qp_bitmap_free);
645                 ntb_transport_init_queue(nt, i);
646         }
647
648         callout_init(&nt->link_work, 0);
649         callout_init(&nt->link_watchdog, 0);
650         TASK_INIT(&nt->link_cleanup, 0, ntb_transport_link_cleanup_work, nt);
651
652         rc = ntb_set_ctx(ntb, nt, &ntb_transport_ops);
653         if (rc != 0)
654                 goto err;
655
656         nt->link_is_up = false;
657         ntb_link_enable(ntb, NTB_SPEED_AUTO, NTB_WIDTH_AUTO);
658         ntb_link_event(ntb);
659
660         callout_reset(&nt->link_work, 0, ntb_transport_link_work, nt);
661         if (enable_xeon_watchdog != 0)
662                 callout_reset(&nt->link_watchdog, 0, xeon_link_watchdog_hb, nt);
663         return (0);
664
665 err:
666         free(nt->qp_vec, M_NTB_IF);
667         nt->qp_vec = NULL;
668         return (rc);
669 }
670
671 static void
672 ntb_transport_free(struct ntb_transport_ctx *nt)
673 {
674         struct ntb_softc *ntb = nt->ntb;
675         struct _qpset qp_bitmap_alloc;
676         uint8_t i;
677
678         ntb_transport_link_cleanup(nt);
679         taskqueue_drain(taskqueue_swi, &nt->link_cleanup);
680         callout_drain(&nt->link_work);
681         callout_drain(&nt->link_watchdog);
682
683         BIT_COPY(QP_SETSIZE, &nt->qp_bitmap, &qp_bitmap_alloc);
684         BIT_NAND(QP_SETSIZE, &qp_bitmap_alloc, &nt->qp_bitmap_free);
685
686         /* Verify that all the QPs are freed */
687         for (i = 0; i < nt->qp_count; i++)
688                 if (test_bit(i, &qp_bitmap_alloc))
689                         ntb_transport_free_queue(&nt->qp_vec[i]);
690
691         ntb_link_disable(ntb);
692         ntb_clear_ctx(ntb);
693
694         for (i = 0; i < nt->mw_count; i++)
695                 ntb_free_mw(nt, i);
696
697         free(nt->qp_vec, M_NTB_IF);
698 }
699
700 static void
701 ntb_transport_init_queue(struct ntb_transport_ctx *nt, unsigned int qp_num)
702 {
703         struct ntb_transport_mw *mw;
704         struct ntb_transport_qp *qp;
705         vm_paddr_t mw_base;
706         uint64_t mw_size, qp_offset;
707         size_t tx_size;
708         unsigned num_qps_mw, mw_num, mw_count;
709
710         mw_count = nt->mw_count;
711         mw_num = QP_TO_MW(nt, qp_num);
712         mw = &nt->mw_vec[mw_num];
713
714         qp = &nt->qp_vec[qp_num];
715         qp->qp_num = qp_num;
716         qp->transport = nt;
717         qp->ntb = nt->ntb;
718         qp->client_ready = false;
719         qp->event_handler = NULL;
720         ntb_qp_link_down_reset(qp);
721
722         if (nt->qp_count % mw_count && mw_num + 1 < nt->qp_count / mw_count)
723                 num_qps_mw = nt->qp_count / mw_count + 1;
724         else
725                 num_qps_mw = nt->qp_count / mw_count;
726
727         mw_base = mw->phys_addr;
728         mw_size = mw->phys_size;
729
730         tx_size = mw_size / num_qps_mw;
731         qp_offset = tx_size * (qp_num / mw_count);
732
733         qp->tx_mw = mw->vbase + qp_offset;
734         KASSERT(qp->tx_mw != NULL, ("uh oh?"));
735
736         /* XXX Assumes that a vm_paddr_t is equivalent to bus_addr_t */
737         qp->tx_mw_phys = mw_base + qp_offset;
738         KASSERT(qp->tx_mw_phys != 0, ("uh oh?"));
739
740         tx_size -= sizeof(struct ntb_rx_info);
741         qp->rx_info = (void *)(qp->tx_mw + tx_size);
742
743         /* Due to house-keeping, there must be at least 2 buffs */
744         qp->tx_max_frame = qmin(tx_size / 2,
745             transport_mtu + sizeof(struct ntb_payload_header));
746         qp->tx_max_entry = tx_size / qp->tx_max_frame;
747
748         callout_init(&qp->link_work, 0);
749         callout_init(&qp->queue_full, 1);
750         callout_init(&qp->rx_full, 1);
751
752         mtx_init(&qp->ntb_rx_q_lock, "ntb rx q", NULL, MTX_SPIN);
753         mtx_init(&qp->ntb_tx_free_q_lock, "ntb tx free q", NULL, MTX_SPIN);
754         TASK_INIT(&qp->rx_completion_task, 0, ntb_complete_rxc, qp);
755         TASK_INIT(&qp->rxc_db_work, 0, ntb_transport_rxc_db, qp);
756
757         STAILQ_INIT(&qp->rx_post_q);
758         STAILQ_INIT(&qp->rx_pend_q);
759         STAILQ_INIT(&qp->tx_free_q);
760
761         callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
762 }
763
764 static void
765 ntb_transport_free_queue(struct ntb_transport_qp *qp)
766 {
767         struct ntb_queue_entry *entry;
768
769         if (qp == NULL)
770                 return;
771
772         callout_drain(&qp->link_work);
773
774         ntb_db_set_mask(qp->ntb, 1ull << qp->qp_num);
775         taskqueue_drain(taskqueue_swi, &qp->rxc_db_work);
776         taskqueue_drain(taskqueue_swi, &qp->rx_completion_task);
777
778         qp->cb_data = NULL;
779         qp->rx_handler = NULL;
780         qp->tx_handler = NULL;
781         qp->event_handler = NULL;
782
783         while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_pend_q)))
784                 free(entry, M_NTB_IF);
785
786         while ((entry = ntb_list_rm(&qp->ntb_rx_q_lock, &qp->rx_post_q)))
787                 free(entry, M_NTB_IF);
788
789         while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
790                 free(entry, M_NTB_IF);
791
792         set_bit(qp->qp_num, &qp->transport->qp_bitmap_free);
793 }
794
795 /**
796  * ntb_transport_create_queue - Create a new NTB transport layer queue
797  * @rx_handler: receive callback function
798  * @tx_handler: transmit callback function
799  * @event_handler: event callback function
800  *
801  * Create a new NTB transport layer queue and provide the queue with a callback
802  * routine for both transmit and receive.  The receive callback routine will be
803  * used to pass up data when the transport has received it on the queue.   The
804  * transmit callback routine will be called when the transport has completed the
805  * transmission of the data on the queue and the data is ready to be freed.
806  *
807  * RETURNS: pointer to newly created ntb_queue, NULL on error.
808  */
809 static struct ntb_transport_qp *
810 ntb_transport_create_queue(void *data, struct ntb_softc *ntb,
811     const struct ntb_queue_handlers *handlers)
812 {
813         struct ntb_queue_entry *entry;
814         struct ntb_transport_qp *qp;
815         struct ntb_transport_ctx *nt;
816         unsigned int free_queue;
817         int i;
818
819         nt = ntb_get_ctx(ntb, NULL);
820         KASSERT(nt != NULL, ("bogus"));
821
822         free_queue = ffs_bit(&nt->qp_bitmap);
823         if (free_queue == 0)
824                 return (NULL);
825
826         /* decrement free_queue to make it zero based */
827         free_queue--;
828
829         qp = &nt->qp_vec[free_queue];
830         clear_bit(qp->qp_num, &nt->qp_bitmap_free);
831         qp->cb_data = data;
832         qp->rx_handler = handlers->rx_handler;
833         qp->tx_handler = handlers->tx_handler;
834         qp->event_handler = handlers->event_handler;
835
836         for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
837                 entry = malloc(sizeof(*entry), M_NTB_IF, M_WAITOK | M_ZERO);
838                 entry->cb_data = nt->ifp;
839                 entry->buf = NULL;
840                 entry->len = transport_mtu;
841                 ntb_list_add(&qp->ntb_rx_q_lock, entry, &qp->rx_pend_q);
842         }
843
844         for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
845                 entry = malloc(sizeof(*entry), M_NTB_IF, M_WAITOK | M_ZERO);
846                 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
847         }
848
849         ntb_db_clear(ntb, 1ull << qp->qp_num);
850         ntb_db_clear_mask(ntb, 1ull << qp->qp_num);
851         return (qp);
852 }
853
854 /**
855  * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
856  * @qp: NTB transport layer queue to be enabled
857  *
858  * Notify NTB transport layer of client readiness to use queue
859  */
860 static void
861 ntb_transport_link_up(struct ntb_transport_qp *qp)
862 {
863         struct ntb_transport_ctx *nt;
864
865         if (qp == NULL)
866                 return;
867
868         qp->client_ready = true;
869
870         nt = qp->transport;
871         ntb_printf(2, "qp client ready\n");
872
873         if (qp->transport->link_is_up)
874                 callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
875 }
876
877
878
879 /* Transport Tx */
880
881 /**
882  * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
883  * @qp: NTB transport layer queue the entry is to be enqueued on
884  * @cb: per buffer pointer for callback function to use
885  * @data: pointer to data buffer that will be sent
886  * @len: length of the data buffer
887  *
888  * Enqueue a new transmit buffer onto the transport queue from which a NTB
889  * payload will be transmitted.  This assumes that a lock is being held to
890  * serialize access to the qp.
891  *
892  * RETURNS: An appropriate ERRNO error value on error, or zero for success.
893  */
894 static int
895 ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
896     unsigned int len)
897 {
898         struct ntb_queue_entry *entry;
899         int rc;
900
901         if (qp == NULL || !qp->link_is_up || len == 0) {
902                 CTR0(KTR_NTB, "TX: link not up");
903                 return (EINVAL);
904         }
905
906         entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
907         if (entry == NULL) {
908                 CTR0(KTR_NTB, "TX: could not get entry from tx_free_q");
909                 qp->tx_err_no_buf++;
910                 return (EBUSY);
911         }
912         CTR1(KTR_NTB, "TX: got entry %p from tx_free_q", entry);
913
914         entry->cb_data = cb;
915         entry->buf = data;
916         entry->len = len;
917         entry->flags = 0;
918
919         rc = ntb_process_tx(qp, entry);
920         if (rc != 0) {
921                 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
922                 CTR1(KTR_NTB,
923                     "TX: process_tx failed. Returning entry %p to tx_free_q",
924                     entry);
925         }
926         return (rc);
927 }
928
929 static int
930 ntb_process_tx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry)
931 {
932         void *offset;
933
934         offset = qp->tx_mw + qp->tx_max_frame * qp->tx_index;
935         CTR3(KTR_NTB,
936             "TX: process_tx: tx_pkts=%lu, tx_index=%u, remote entry=%u",
937             qp->tx_pkts, qp->tx_index, qp->remote_rx_info->entry);
938         if (qp->tx_index == qp->remote_rx_info->entry) {
939                 CTR0(KTR_NTB, "TX: ring full");
940                 qp->tx_ring_full++;
941                 return (EAGAIN);
942         }
943
944         if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
945                 if (qp->tx_handler != NULL)
946                         qp->tx_handler(qp, qp->cb_data, entry->buf,
947                             EIO);
948                 else
949                         m_freem(entry->buf);
950
951                 entry->buf = NULL;
952                 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
953                 CTR1(KTR_NTB,
954                     "TX: frame too big. returning entry %p to tx_free_q",
955                     entry);
956                 return (0);
957         }
958         CTR2(KTR_NTB, "TX: copying entry %p to offset %p", entry, offset);
959         ntb_memcpy_tx(qp, entry, offset);
960
961         qp->tx_index++;
962         qp->tx_index %= qp->tx_max_entry;
963
964         qp->tx_pkts++;
965
966         return (0);
967 }
968
969 static void
970 ntb_memcpy_tx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry,
971     void *offset)
972 {
973         struct ntb_payload_header *hdr;
974
975         /* This piece is from Linux' ntb_async_tx() */
976         hdr = (struct ntb_payload_header *)((char *)offset + qp->tx_max_frame -
977             sizeof(struct ntb_payload_header));
978         entry->x_hdr = hdr;
979         iowrite32(entry->len, &hdr->len);
980         iowrite32(qp->tx_pkts, &hdr->ver);
981
982         /* This piece is ntb_memcpy_tx() */
983         CTR2(KTR_NTB, "TX: copying %d bytes to offset %p", entry->len, offset);
984         if (entry->buf != NULL) {
985                 m_copydata((struct mbuf *)entry->buf, 0, entry->len, offset);
986
987                 /*
988                  * Ensure that the data is fully copied before setting the
989                  * flags
990                  */
991                 wmb();
992         }
993
994         /* The rest is ntb_tx_copy_callback() */
995         iowrite32(entry->flags | IF_NTB_DESC_DONE_FLAG, &hdr->flags);
996         CTR1(KTR_NTB, "TX: hdr %p set DESC_DONE", hdr);
997
998         ntb_peer_db_set(qp->ntb, 1ull << qp->qp_num);
999
1000         /*
1001          * The entry length can only be zero if the packet is intended to be a
1002          * "link down" or similar.  Since no payload is being sent in these
1003          * cases, there is nothing to add to the completion queue.
1004          */
1005         if (entry->len > 0) {
1006                 qp->tx_bytes += entry->len;
1007
1008                 if (qp->tx_handler)
1009                         qp->tx_handler(qp, qp->cb_data, entry->buf,
1010                             entry->len);
1011                 else
1012                         m_freem(entry->buf);
1013                 entry->buf = NULL;
1014         }
1015
1016         CTR3(KTR_NTB,
1017             "TX: entry %p sent. hdr->ver = %u, hdr->flags = 0x%x, Returning "
1018             "to tx_free_q", entry, hdr->ver, hdr->flags);
1019         ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
1020 }
1021
1022 static void
1023 ntb_qp_full(void *arg)
1024 {
1025
1026         CTR0(KTR_NTB, "TX: qp_full callout");
1027         ntb_start(arg);
1028 }
1029
1030 /* Transport Rx */
1031 static void
1032 ntb_transport_rxc_db(void *arg, int pending __unused)
1033 {
1034         struct ntb_transport_qp *qp = arg;
1035         ntb_q_idx_t i;
1036         int rc;
1037
1038         /*
1039          * Limit the number of packets processed in a single interrupt to
1040          * provide fairness to others
1041          */
1042         CTR0(KTR_NTB, "RX: transport_rx");
1043         mtx_lock(&qp->transport->rx_lock);
1044         for (i = 0; i < qp->rx_max_entry; i++) {
1045                 rc = ntb_process_rxc(qp);
1046                 if (rc != 0) {
1047                         CTR0(KTR_NTB, "RX: process_rxc failed");
1048                         break;
1049                 }
1050         }
1051         mtx_unlock(&qp->transport->rx_lock);
1052
1053         if (i == qp->rx_max_entry)
1054                 taskqueue_enqueue(taskqueue_swi, &qp->rxc_db_work);
1055         else if ((ntb_db_read(qp->ntb) & (1ull << qp->qp_num)) != 0) {
1056                 /* If db is set, clear it and read it back to commit clear. */
1057                 ntb_db_clear(qp->ntb, 1ull << qp->qp_num);
1058                 (void)ntb_db_read(qp->ntb);
1059
1060                 /*
1061                  * An interrupt may have arrived between finishing
1062                  * ntb_process_rxc and clearing the doorbell bit: there might
1063                  * be some more work to do.
1064                  */
1065                 taskqueue_enqueue(taskqueue_swi, &qp->rxc_db_work);
1066         }
1067 }
1068
1069 static int
1070 ntb_process_rxc(struct ntb_transport_qp *qp)
1071 {
1072         struct ntb_payload_header *hdr;
1073         struct ntb_queue_entry *entry;
1074         caddr_t offset;
1075
1076         offset = qp->rx_buff + qp->rx_max_frame * qp->rx_index;
1077         hdr = (void *)(offset + qp->rx_max_frame -
1078             sizeof(struct ntb_payload_header));
1079
1080         CTR1(KTR_NTB, "RX: process_rxc rx_index = %u", qp->rx_index);
1081         if ((hdr->flags & IF_NTB_DESC_DONE_FLAG) == 0) {
1082                 CTR0(KTR_NTB, "RX: hdr not done");
1083                 qp->rx_ring_empty++;
1084                 return (EAGAIN);
1085         }
1086
1087         if ((hdr->flags & IF_NTB_LINK_DOWN_FLAG) != 0) {
1088                 CTR0(KTR_NTB, "RX: link down");
1089                 ntb_qp_link_down(qp);
1090                 hdr->flags = 0;
1091                 return (EAGAIN);
1092         }
1093
1094         if (hdr->ver != (uint32_t)qp->rx_pkts) {
1095                 CTR2(KTR_NTB,"RX: ver != rx_pkts (%x != %lx). "
1096                     "Returning entry to rx_pend_q", hdr->ver, qp->rx_pkts);
1097                 qp->rx_err_ver++;
1098                 return (EIO);
1099         }
1100
1101         entry = ntb_list_mv(&qp->ntb_rx_q_lock, &qp->rx_pend_q, &qp->rx_post_q);
1102         if (entry == NULL) {
1103                 qp->rx_err_no_buf++;
1104                 CTR0(KTR_NTB, "RX: No entries in rx_pend_q");
1105                 return (EAGAIN);
1106         }
1107         callout_stop(&qp->rx_full);
1108         CTR1(KTR_NTB, "RX: rx entry %p from rx_pend_q", entry);
1109
1110         entry->x_hdr = hdr;
1111         entry->index = qp->rx_index;
1112
1113         if (hdr->len > entry->len) {
1114                 CTR2(KTR_NTB, "RX: len too long. Wanted %ju got %ju",
1115                     (uintmax_t)hdr->len, (uintmax_t)entry->len);
1116                 qp->rx_err_oflow++;
1117
1118                 entry->len = -EIO;
1119                 entry->flags |= IF_NTB_DESC_DONE_FLAG;
1120
1121                 taskqueue_enqueue(taskqueue_swi, &qp->rx_completion_task);
1122         } else {
1123                 qp->rx_bytes += hdr->len;
1124                 qp->rx_pkts++;
1125
1126                 CTR1(KTR_NTB, "RX: received %ld rx_pkts", qp->rx_pkts);
1127
1128                 entry->len = hdr->len;
1129
1130                 ntb_memcpy_rx(qp, entry, offset);
1131         }
1132
1133         qp->rx_index++;
1134         qp->rx_index %= qp->rx_max_entry;
1135         return (0);
1136 }
1137
1138 static void
1139 ntb_memcpy_rx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry,
1140     void *offset)
1141 {
1142         struct ifnet *ifp = entry->cb_data;
1143         unsigned int len = entry->len;
1144         struct mbuf *m;
1145
1146         CTR2(KTR_NTB, "RX: copying %d bytes from offset %p", len, offset);
1147         m = m_devget(offset, len, 0, ifp, NULL);
1148         m->m_pkthdr.csum_flags = CSUM_IP_CHECKED | CSUM_IP_VALID;
1149
1150         entry->buf = (void *)m;
1151
1152         /* Ensure that the data is globally visible before clearing the flag */
1153         wmb();
1154
1155         CTR2(KTR_NTB, "RX: copied entry %p to mbuf %p.", entry, m);
1156         ntb_rx_copy_callback(qp, entry);
1157 }
1158
1159 static inline void
1160 ntb_rx_copy_callback(struct ntb_transport_qp *qp, void *data)
1161 {
1162         struct ntb_queue_entry *entry;
1163
1164         entry = data;
1165         entry->flags |= IF_NTB_DESC_DONE_FLAG;
1166         taskqueue_enqueue(taskqueue_swi, &qp->rx_completion_task);
1167 }
1168
1169 static void
1170 ntb_complete_rxc(void *arg, int pending)
1171 {
1172         struct ntb_transport_qp *qp = arg;
1173         struct ntb_queue_entry *entry;
1174         struct mbuf *m;
1175         unsigned len;
1176
1177         CTR0(KTR_NTB, "RX: rx_completion_task");
1178
1179         mtx_lock_spin(&qp->ntb_rx_q_lock);
1180
1181         while (!STAILQ_EMPTY(&qp->rx_post_q)) {
1182                 entry = STAILQ_FIRST(&qp->rx_post_q);
1183                 if ((entry->flags & IF_NTB_DESC_DONE_FLAG) == 0)
1184                         break;
1185
1186                 entry->x_hdr->flags = 0;
1187                 iowrite32(entry->index, &qp->rx_info->entry);
1188
1189                 STAILQ_REMOVE_HEAD(&qp->rx_post_q, entry);
1190
1191                 len = entry->len;
1192                 m = entry->buf;
1193
1194                 /*
1195                  * Re-initialize queue_entry for reuse; rx_handler takes
1196                  * ownership of the mbuf.
1197                  */
1198                 entry->buf = NULL;
1199                 entry->len = transport_mtu;
1200                 entry->cb_data = qp->transport->ifp;
1201
1202                 STAILQ_INSERT_TAIL(&qp->rx_pend_q, entry, entry);
1203
1204                 mtx_unlock_spin(&qp->ntb_rx_q_lock);
1205
1206                 CTR2(KTR_NTB, "RX: completing entry %p, mbuf %p", entry, m);
1207                 if (qp->rx_handler != NULL && qp->client_ready)
1208                         qp->rx_handler(qp, qp->cb_data, m, len);
1209                 else
1210                         m_freem(m);
1211
1212                 mtx_lock_spin(&qp->ntb_rx_q_lock);
1213         }
1214
1215         mtx_unlock_spin(&qp->ntb_rx_q_lock);
1216 }
1217
1218 static void
1219 ntb_transport_doorbell_callback(void *data, uint32_t vector)
1220 {
1221         struct ntb_transport_ctx *nt = data;
1222         struct ntb_transport_qp *qp;
1223         struct _qpset db_bits;
1224         uint64_t vec_mask;
1225         unsigned qp_num;
1226
1227         BIT_COPY(QP_SETSIZE, &nt->qp_bitmap, &db_bits);
1228         BIT_NAND(QP_SETSIZE, &db_bits, &nt->qp_bitmap_free);
1229
1230         vec_mask = ntb_db_vector_mask(nt->ntb, vector);
1231         while (vec_mask != 0) {
1232                 qp_num = ffsll(vec_mask) - 1;
1233
1234                 if (test_bit(qp_num, &db_bits)) {
1235                         qp = &nt->qp_vec[qp_num];
1236                         taskqueue_enqueue(taskqueue_swi, &qp->rxc_db_work);
1237                 }
1238
1239                 vec_mask &= ~(1ull << qp_num);
1240         }
1241 }
1242
1243 /* Link Event handler */
1244 static void
1245 ntb_transport_event_callback(void *data)
1246 {
1247         struct ntb_transport_ctx *nt = data;
1248
1249         if (ntb_link_is_up(nt->ntb, NULL, NULL)) {
1250                 ntb_printf(1, "HW link up\n");
1251                 callout_reset(&nt->link_work, 0, ntb_transport_link_work, nt);
1252         } else {
1253                 ntb_printf(1, "HW link down\n");
1254                 taskqueue_enqueue(taskqueue_swi, &nt->link_cleanup);
1255         }
1256 }
1257
1258 /* Link bring up */
1259 static void
1260 ntb_transport_link_work(void *arg)
1261 {
1262         struct ntb_transport_ctx *nt = arg;
1263         struct ntb_softc *ntb = nt->ntb;
1264         struct ntb_transport_qp *qp;
1265         uint64_t val64, size;
1266         uint32_t val;
1267         unsigned i;
1268         int rc;
1269
1270         /* send the local info, in the opposite order of the way we read it */
1271         for (i = 0; i < nt->mw_count; i++) {
1272                 size = nt->mw_vec[i].phys_size;
1273
1274                 if (max_mw_size != 0 && size > max_mw_size)
1275                         size = max_mw_size;
1276
1277                 ntb_peer_spad_write(ntb, IF_NTB_MW0_SZ_HIGH + (i * 2),
1278                     size >> 32);
1279                 ntb_peer_spad_write(ntb, IF_NTB_MW0_SZ_LOW + (i * 2), size);
1280         }
1281
1282         ntb_peer_spad_write(ntb, IF_NTB_NUM_MWS, nt->mw_count);
1283
1284         ntb_peer_spad_write(ntb, IF_NTB_NUM_QPS, nt->qp_count);
1285
1286         ntb_peer_spad_write(ntb, IF_NTB_VERSION, NTB_TRANSPORT_VERSION);
1287
1288         /* Query the remote side for its info */
1289         val = 0;
1290         ntb_spad_read(ntb, IF_NTB_VERSION, &val);
1291         if (val != NTB_TRANSPORT_VERSION)
1292                 goto out;
1293
1294         ntb_spad_read(ntb, IF_NTB_NUM_QPS, &val);
1295         if (val != nt->qp_count)
1296                 goto out;
1297
1298         ntb_spad_read(ntb, IF_NTB_NUM_MWS, &val);
1299         if (val != nt->mw_count)
1300                 goto out;
1301
1302         for (i = 0; i < nt->mw_count; i++) {
1303                 ntb_spad_read(ntb, IF_NTB_MW0_SZ_HIGH + (i * 2), &val);
1304                 val64 = (uint64_t)val << 32;
1305
1306                 ntb_spad_read(ntb, IF_NTB_MW0_SZ_LOW + (i * 2), &val);
1307                 val64 |= val;
1308
1309                 rc = ntb_set_mw(nt, i, val64);
1310                 if (rc != 0)
1311                         goto free_mws;
1312         }
1313
1314         nt->link_is_up = true;
1315         ntb_printf(1, "transport link up\n");
1316
1317         for (i = 0; i < nt->qp_count; i++) {
1318                 qp = &nt->qp_vec[i];
1319
1320                 ntb_transport_setup_qp_mw(nt, i);
1321
1322                 if (qp->client_ready)
1323                         callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
1324         }
1325
1326         return;
1327
1328 free_mws:
1329         for (i = 0; i < nt->mw_count; i++)
1330                 ntb_free_mw(nt, i);
1331 out:
1332         if (ntb_link_is_up(ntb, NULL, NULL))
1333                 callout_reset(&nt->link_work,
1334                     NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_transport_link_work, nt);
1335 }
1336
1337 static int
1338 ntb_set_mw(struct ntb_transport_ctx *nt, int num_mw, size_t size)
1339 {
1340         struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
1341         size_t xlat_size, buff_size;
1342         int rc;
1343
1344         if (size == 0)
1345                 return (EINVAL);
1346
1347         xlat_size = roundup(size, mw->xlat_align_size);
1348         buff_size = xlat_size;
1349
1350         /* No need to re-setup */
1351         if (mw->xlat_size == xlat_size)
1352                 return (0);
1353
1354         if (mw->buff_size != 0)
1355                 ntb_free_mw(nt, num_mw);
1356
1357         /* Alloc memory for receiving data.  Must be aligned */
1358         mw->xlat_size = xlat_size;
1359         mw->buff_size = buff_size;
1360
1361         mw->virt_addr = contigmalloc(mw->buff_size, M_NTB_IF, M_ZERO, 0,
1362             mw->addr_limit, mw->xlat_align, 0);
1363         if (mw->virt_addr == NULL) {
1364                 ntb_printf(0, "Unable to allocate MW buffer of size %zu/%zu\n",
1365                     mw->buff_size, mw->xlat_size);
1366                 mw->xlat_size = 0;
1367                 mw->buff_size = 0;
1368                 return (ENOMEM);
1369         }
1370         /* TODO: replace with bus_space_* functions */
1371         mw->dma_addr = vtophys(mw->virt_addr);
1372
1373         /*
1374          * Ensure that the allocation from contigmalloc is aligned as
1375          * requested.  XXX: This may not be needed -- brought in for parity
1376          * with the Linux driver.
1377          */
1378         if (mw->dma_addr % mw->xlat_align != 0) {
1379                 ntb_printf(0,
1380                     "DMA memory 0x%jx not aligned to BAR size 0x%zx\n",
1381                     (uintmax_t)mw->dma_addr, size);
1382                 ntb_free_mw(nt, num_mw);
1383                 return (ENOMEM);
1384         }
1385
1386         /* Notify HW the memory location of the receive buffer */
1387         rc = ntb_mw_set_trans(nt->ntb, num_mw, mw->dma_addr, mw->xlat_size);
1388         if (rc) {
1389                 ntb_printf(0, "Unable to set mw%d translation\n", num_mw);
1390                 ntb_free_mw(nt, num_mw);
1391                 return (rc);
1392         }
1393
1394         return (0);
1395 }
1396
1397 static void
1398 ntb_free_mw(struct ntb_transport_ctx *nt, int num_mw)
1399 {
1400         struct ntb_transport_mw *mw = &nt->mw_vec[num_mw];
1401
1402         if (mw->virt_addr == NULL)
1403                 return;
1404
1405         ntb_mw_clear_trans(nt->ntb, num_mw);
1406         contigfree(mw->virt_addr, mw->xlat_size, M_NTB_IF);
1407         mw->xlat_size = 0;
1408         mw->buff_size = 0;
1409         mw->virt_addr = NULL;
1410 }
1411
1412 static int
1413 ntb_transport_setup_qp_mw(struct ntb_transport_ctx *nt, unsigned int qp_num)
1414 {
1415         struct ntb_transport_qp *qp = &nt->qp_vec[qp_num];
1416         struct ntb_transport_mw *mw;
1417         void *offset;
1418         ntb_q_idx_t i;
1419         size_t rx_size;
1420         unsigned num_qps_mw, mw_num, mw_count;
1421
1422         mw_count = nt->mw_count;
1423         mw_num = QP_TO_MW(nt, qp_num);
1424         mw = &nt->mw_vec[mw_num];
1425
1426         if (mw->virt_addr == NULL)
1427                 return (ENOMEM);
1428
1429         if (nt->qp_count % mw_count && mw_num + 1 < nt->qp_count / mw_count)
1430                 num_qps_mw = nt->qp_count / mw_count + 1;
1431         else
1432                 num_qps_mw = nt->qp_count / mw_count;
1433
1434         rx_size = mw->xlat_size / num_qps_mw;
1435         qp->rx_buff = mw->virt_addr + rx_size * (qp_num / mw_count);
1436         rx_size -= sizeof(struct ntb_rx_info);
1437
1438         qp->remote_rx_info = (void*)(qp->rx_buff + rx_size);
1439
1440         /* Due to house-keeping, there must be at least 2 buffs */
1441         qp->rx_max_frame = qmin(rx_size / 2,
1442             transport_mtu + sizeof(struct ntb_payload_header));
1443         qp->rx_max_entry = rx_size / qp->rx_max_frame;
1444         qp->rx_index = 0;
1445
1446         qp->remote_rx_info->entry = qp->rx_max_entry - 1;
1447
1448         /* Set up the hdr offsets with 0s */
1449         for (i = 0; i < qp->rx_max_entry; i++) {
1450                 offset = (void *)(qp->rx_buff + qp->rx_max_frame * (i + 1) -
1451                     sizeof(struct ntb_payload_header));
1452                 memset(offset, 0, sizeof(struct ntb_payload_header));
1453         }
1454
1455         qp->rx_pkts = 0;
1456         qp->tx_pkts = 0;
1457         qp->tx_index = 0;
1458
1459         return (0);
1460 }
1461
1462 static void
1463 ntb_qp_link_work(void *arg)
1464 {
1465         struct ntb_transport_qp *qp = arg;
1466         struct ntb_softc *ntb = qp->ntb;
1467         struct ntb_transport_ctx *nt = qp->transport;
1468         uint32_t val, dummy;
1469
1470         ntb_spad_read(ntb, IF_NTB_QP_LINKS, &val);
1471
1472         ntb_peer_spad_write(ntb, IF_NTB_QP_LINKS, val | (1ull << qp->qp_num));
1473
1474         /* query remote spad for qp ready bits */
1475         ntb_peer_spad_read(ntb, IF_NTB_QP_LINKS, &dummy);
1476
1477         /* See if the remote side is up */
1478         if ((val & (1ull << qp->qp_num)) != 0) {
1479                 ntb_printf(2, "qp link up\n");
1480                 qp->link_is_up = true;
1481
1482                 if (qp->event_handler != NULL)
1483                         qp->event_handler(qp->cb_data, NTB_LINK_UP);
1484
1485                 taskqueue_enqueue(taskqueue_swi, &qp->rxc_db_work);
1486         } else if (nt->link_is_up)
1487                 callout_reset(&qp->link_work,
1488                     NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_qp_link_work, qp);
1489 }
1490
1491 /* Link down event*/
1492 static void
1493 ntb_transport_link_cleanup(struct ntb_transport_ctx *nt)
1494 {
1495         struct ntb_transport_qp *qp;
1496         struct _qpset qp_bitmap_alloc;
1497         unsigned i;
1498
1499         BIT_COPY(QP_SETSIZE, &nt->qp_bitmap, &qp_bitmap_alloc);
1500         BIT_NAND(QP_SETSIZE, &qp_bitmap_alloc, &nt->qp_bitmap_free);
1501
1502         /* Pass along the info to any clients */
1503         for (i = 0; i < nt->qp_count; i++)
1504                 if (test_bit(i, &qp_bitmap_alloc)) {
1505                         qp = &nt->qp_vec[i];
1506                         ntb_qp_link_cleanup(qp);
1507                         callout_drain(&qp->link_work);
1508                 }
1509
1510         if (!nt->link_is_up)
1511                 callout_drain(&nt->link_work);
1512
1513         /*
1514          * The scratchpad registers keep the values if the remote side
1515          * goes down, blast them now to give them a sane value the next
1516          * time they are accessed
1517          */
1518         for (i = 0; i < IF_NTB_MAX_SPAD; i++)
1519                 ntb_spad_write(nt->ntb, i, 0);
1520 }
1521
1522 static void
1523 ntb_transport_link_cleanup_work(void *arg, int pending __unused)
1524 {
1525
1526         ntb_transport_link_cleanup(arg);
1527 }
1528
1529 static void
1530 ntb_qp_link_down(struct ntb_transport_qp *qp)
1531 {
1532
1533         ntb_qp_link_cleanup(qp);
1534 }
1535
1536 static void
1537 ntb_qp_link_down_reset(struct ntb_transport_qp *qp)
1538 {
1539
1540         qp->link_is_up = false;
1541
1542         qp->tx_index = qp->rx_index = 0;
1543         qp->tx_bytes = qp->rx_bytes = 0;
1544         qp->tx_pkts = qp->rx_pkts = 0;
1545
1546         qp->rx_ring_empty = 0;
1547         qp->tx_ring_full = 0;
1548
1549         qp->rx_err_no_buf = qp->tx_err_no_buf = 0;
1550         qp->rx_err_oflow = qp->rx_err_ver = 0;
1551 }
1552
1553 static void
1554 ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
1555 {
1556         struct ntb_transport_ctx *nt = qp->transport;
1557
1558         callout_drain(&qp->link_work);
1559         ntb_qp_link_down_reset(qp);
1560
1561         if (qp->event_handler != NULL)
1562                 qp->event_handler(qp->cb_data, NTB_LINK_DOWN);
1563
1564         if (nt->link_is_up)
1565                 callout_reset(&qp->link_work,
1566                     NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_qp_link_work, qp);
1567 }
1568
1569 /* Link commanded down */
1570 /**
1571  * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1572  * @qp: NTB transport layer queue to be disabled
1573  *
1574  * Notify NTB transport layer of client's desire to no longer receive data on
1575  * transport queue specified.  It is the client's responsibility to ensure all
1576  * entries on queue are purged or otherwise handled appropriately.
1577  */
1578 static void
1579 ntb_transport_link_down(struct ntb_transport_qp *qp)
1580 {
1581         uint32_t val;
1582
1583         if (qp == NULL)
1584                 return;
1585
1586         qp->client_ready = false;
1587
1588         ntb_spad_read(qp->ntb, IF_NTB_QP_LINKS, &val);
1589
1590         ntb_peer_spad_write(qp->ntb, IF_NTB_QP_LINKS,
1591            val & ~(1 << qp->qp_num));
1592
1593         if (qp->link_is_up)
1594                 ntb_send_link_down(qp);
1595         else
1596                 callout_drain(&qp->link_work);
1597 }
1598
1599 static void
1600 ntb_send_link_down(struct ntb_transport_qp *qp)
1601 {
1602         struct ntb_queue_entry *entry;
1603         int i, rc;
1604
1605         if (!qp->link_is_up)
1606                 return;
1607
1608         for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1609                 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1610                 if (entry != NULL)
1611                         break;
1612                 pause("NTB Wait for link down", hz / 10);
1613         }
1614
1615         if (entry == NULL)
1616                 return;
1617
1618         entry->cb_data = NULL;
1619         entry->buf = NULL;
1620         entry->len = 0;
1621         entry->flags = IF_NTB_LINK_DOWN_FLAG;
1622
1623         mtx_lock(&qp->transport->tx_lock);
1624         rc = ntb_process_tx(qp, entry);
1625         if (rc != 0)
1626                 printf("ntb: Failed to send link down\n");
1627         mtx_unlock(&qp->transport->tx_lock);
1628
1629         ntb_qp_link_down_reset(qp);
1630 }
1631
1632
1633 /* List Management */
1634
1635 static void
1636 ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry,
1637     struct ntb_queue_list *list)
1638 {
1639
1640         mtx_lock_spin(lock);
1641         STAILQ_INSERT_TAIL(list, entry, entry);
1642         mtx_unlock_spin(lock);
1643 }
1644
1645 static struct ntb_queue_entry *
1646 ntb_list_rm(struct mtx *lock, struct ntb_queue_list *list)
1647 {
1648         struct ntb_queue_entry *entry;
1649
1650         mtx_lock_spin(lock);
1651         if (STAILQ_EMPTY(list)) {
1652                 entry = NULL;
1653                 goto out;
1654         }
1655         entry = STAILQ_FIRST(list);
1656         STAILQ_REMOVE_HEAD(list, entry);
1657 out:
1658         mtx_unlock_spin(lock);
1659
1660         return (entry);
1661 }
1662
1663 static struct ntb_queue_entry *
1664 ntb_list_mv(struct mtx *lock, struct ntb_queue_list *from,
1665     struct ntb_queue_list *to)
1666 {
1667         struct ntb_queue_entry *entry;
1668
1669         mtx_lock_spin(lock);
1670         if (STAILQ_EMPTY(from)) {
1671                 entry = NULL;
1672                 goto out;
1673         }
1674         entry = STAILQ_FIRST(from);
1675         STAILQ_REMOVE_HEAD(from, entry);
1676         STAILQ_INSERT_TAIL(to, entry, entry);
1677
1678 out:
1679         mtx_unlock_spin(lock);
1680         return (entry);
1681 }
1682
1683 /* Helper functions */
1684 /* TODO: This too should really be part of the kernel */
1685 #define EUI48_MULTICAST                 1 << 0
1686 #define EUI48_LOCALLY_ADMINISTERED      1 << 1
1687 static void
1688 create_random_local_eui48(u_char *eaddr)
1689 {
1690         static uint8_t counter = 0;
1691         uint32_t seed = ticks;
1692
1693         eaddr[0] = EUI48_LOCALLY_ADMINISTERED;
1694         memcpy(&eaddr[1], &seed, sizeof(uint32_t));
1695         eaddr[5] = counter++;
1696 }
1697
1698 /**
1699  * ntb_transport_max_size - Query the max payload size of a qp
1700  * @qp: NTB transport layer queue to be queried
1701  *
1702  * Query the maximum payload size permissible on the given qp
1703  *
1704  * RETURNS: the max payload size of a qp
1705  */
1706 static unsigned int
1707 ntb_transport_max_size(struct ntb_transport_qp *qp)
1708 {
1709
1710         if (qp == NULL)
1711                 return (0);
1712
1713         return (qp->tx_max_frame - sizeof(struct ntb_payload_header));
1714 }