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