]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ntb/if_ntb/if_ntb.c
NTB: MFV 948d3a65: Xeon Errata Workaround
[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/lock.h>
36 #include <sys/malloc.h>
37 #include <sys/module.h>
38 #include <sys/mutex.h>
39 #include <sys/queue.h>
40 #include <sys/socket.h>
41 #include <sys/sockio.h>
42 #include <sys/sysctl.h>
43 #include <sys/taskqueue.h>
44 #include <net/if.h>
45 #include <net/if_media.h>
46 #include <net/if_types.h>
47 #include <net/if_var.h>
48 #include <net/bpf.h>
49 #include <net/ethernet.h>
50 #include <vm/vm.h>
51 #include <vm/pmap.h>
52 #include <machine/bus.h>
53 #include <machine/cpufunc.h>
54 #include <machine/pmap.h>
55
56 #include "../ntb_hw/ntb_hw.h"
57
58 /*
59  * The Non-Transparent Bridge (NTB) is a device on some Intel processors that
60  * allows you to connect two systems using a PCI-e link.
61  *
62  * This module contains a protocol for sending and receiving messages, and
63  * exposes that protocol through a simulated ethernet device called ntb.
64  *
65  * NOTE: Much of the code in this module is shared with Linux. Any patches may
66  * be picked up and redistributed in Linux with a dual GPL/BSD license.
67  */
68
69 /* TODO: These functions should really be part of the kernel */
70 #define test_bit(pos, bitmap_addr)  (*(bitmap_addr) & 1UL << (pos))
71 #define set_bit(pos, bitmap_addr)   *(bitmap_addr) |= 1UL << (pos)
72 #define clear_bit(pos, bitmap_addr) *(bitmap_addr) &= ~(1UL << (pos))
73
74 #define KTR_NTB KTR_SPARE3
75
76 #define NTB_TRANSPORT_VERSION   3
77 #define NTB_RX_MAX_PKTS         64
78 #define NTB_RXQ_SIZE            300
79
80 static unsigned int transport_mtu = 0x4000 + ETHER_HDR_LEN + ETHER_CRC_LEN;
81
82 /*
83  * This is an oversimplification to work around Xeon Errata.  The second client
84  * may be usable for unidirectional traffic.
85  */
86 static unsigned int max_num_clients = 1;
87
88 STAILQ_HEAD(ntb_queue_list, ntb_queue_entry);
89
90 struct ntb_queue_entry {
91         /* ntb_queue list reference */
92         STAILQ_ENTRY(ntb_queue_entry) entry;
93
94         /* info on data to be transfered */
95         void            *cb_data;
96         void            *buf;
97         uint64_t        len;
98         uint64_t        flags;
99 };
100
101 struct ntb_rx_info {
102         unsigned int entry;
103 };
104
105 struct ntb_transport_qp {
106         struct ntb_netdev       *transport;
107         struct ntb_softc        *ntb;
108
109         void                    *cb_data;
110
111         bool                    client_ready;
112         bool                    qp_link;
113         uint8_t                 qp_num; /* Only 64 QPs are allowed.  0-63 */
114
115         struct ntb_rx_info      *rx_info;
116         struct ntb_rx_info      *remote_rx_info;
117
118         void (*tx_handler) (struct ntb_transport_qp *qp, void *qp_data,
119             void *data, int len);
120         struct ntb_queue_list   tx_free_q;
121         struct mtx              ntb_tx_free_q_lock;
122         void                    *tx_mw;
123         uint64_t                tx_index;
124         uint64_t                tx_max_entry;
125         uint64_t                tx_max_frame;
126
127         void (*rx_handler) (struct ntb_transport_qp *qp, void *qp_data,
128             void *data, int len);
129         struct ntb_queue_list   rx_pend_q;
130         struct ntb_queue_list   rx_free_q;
131         struct mtx              ntb_rx_pend_q_lock;
132         struct mtx              ntb_rx_free_q_lock;
133         struct task             rx_completion_task;
134         void                    *rx_buff;
135         uint64_t                rx_index;
136         uint64_t                rx_max_entry;
137         uint64_t                rx_max_frame;
138
139         void (*event_handler) (void *data, int status);
140         struct callout          link_work;
141         struct callout          queue_full;
142         struct callout          rx_full;
143
144         uint64_t                last_rx_no_buf;
145
146         /* Stats */
147         uint64_t                rx_bytes;
148         uint64_t                rx_pkts;
149         uint64_t                rx_ring_empty;
150         uint64_t                rx_err_no_buf;
151         uint64_t                rx_err_oflow;
152         uint64_t                rx_err_ver;
153         uint64_t                tx_bytes;
154         uint64_t                tx_pkts;
155         uint64_t                tx_ring_full;
156 };
157
158 struct ntb_queue_handlers {
159         void (*rx_handler) (struct ntb_transport_qp *qp, void *qp_data,
160             void *data, int len);
161         void (*tx_handler) (struct ntb_transport_qp *qp, void *qp_data,
162             void *data, int len);
163         void (*event_handler) (void *data, int status);
164 };
165
166
167 struct ntb_transport_mw {
168         size_t          size;
169         void            *virt_addr;
170         vm_paddr_t      dma_addr;
171 };
172
173 struct ntb_netdev {
174         struct ntb_softc        *ntb;
175         struct ifnet            *ifp;
176         struct ntb_transport_mw mw[NTB_NUM_MW];
177         struct ntb_transport_qp *qps;
178         uint64_t                max_qps;
179         uint64_t                qp_bitmap;
180         bool                    transport_link;
181         struct callout          link_work;
182         struct ntb_transport_qp *qp;
183         uint64_t                bufsize;
184         u_char                  eaddr[ETHER_ADDR_LEN];
185         struct mtx              tx_lock;
186         struct mtx              rx_lock;
187 };
188
189 static struct ntb_netdev net_softc;
190
191 enum {
192         IF_NTB_DESC_DONE_FLAG = 1 << 0,
193         IF_NTB_LINK_DOWN_FLAG = 1 << 1,
194 };
195
196 struct ntb_payload_header {
197         uint64_t ver;
198         uint64_t len;
199         uint64_t flags;
200 };
201
202 enum {
203         /*
204          * The order of this enum is part of the if_ntb remote protocol.  Do
205          * not reorder without bumping protocol version (and it's probably best
206          * to keep the protocol in lock-step with the Linux NTB driver.
207          */
208         IF_NTB_VERSION = 0,
209         IF_NTB_QP_LINKS,
210         IF_NTB_NUM_QPS,
211         IF_NTB_NUM_MWS,
212         /*
213          * N.B.: transport_link_work assumes MW1 enums = MW0 + 2.
214          */
215         IF_NTB_MW0_SZ_HIGH,
216         IF_NTB_MW0_SZ_LOW,
217         IF_NTB_MW1_SZ_HIGH,
218         IF_NTB_MW1_SZ_LOW,
219         IF_NTB_MAX_SPAD,
220 };
221
222 #define QP_TO_MW(qp)            ((qp) % NTB_NUM_MW)
223 #define NTB_QP_DEF_NUM_ENTRIES  100
224 #define NTB_LINK_DOWN_TIMEOUT   10
225
226 static int ntb_handle_module_events(struct module *m, int what, void *arg);
227 static int ntb_setup_interface(void);
228 static int ntb_teardown_interface(void);
229 static void ntb_net_init(void *arg);
230 static int ntb_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
231 static void ntb_start(struct ifnet *ifp);
232 static void ntb_net_tx_handler(struct ntb_transport_qp *qp, void *qp_data,
233     void *data, int len);
234 static void ntb_net_rx_handler(struct ntb_transport_qp *qp, void *qp_data,
235     void *data, int len);
236 static void ntb_net_event_handler(void *data, int status);
237 static int ntb_transport_init(struct ntb_softc *ntb);
238 static void ntb_transport_free(void *transport);
239 static void ntb_transport_init_queue(struct ntb_netdev *nt,
240     unsigned int qp_num);
241 static void ntb_transport_free_queue(struct ntb_transport_qp *qp);
242 static struct ntb_transport_qp * ntb_transport_create_queue(void *data,
243     struct ntb_softc *pdev, const struct ntb_queue_handlers *handlers);
244 static void ntb_transport_link_up(struct ntb_transport_qp *qp);
245 static int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb,
246     void *data, unsigned int len);
247 static int ntb_process_tx(struct ntb_transport_qp *qp,
248     struct ntb_queue_entry *entry);
249 static void ntb_tx_copy_task(struct ntb_transport_qp *qp,
250     struct ntb_queue_entry *entry, void *offset);
251 static void ntb_qp_full(void *arg);
252 static void ntb_transport_rxc_db(void *data, int db_num);
253 static void ntb_rx_pendq_full(void *arg);
254 static void ntb_transport_rx(struct ntb_transport_qp *qp);
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()
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()
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, int status)
469 {
470
471 }
472
473 /* Transport Init and teardown */
474
475 static int
476 ntb_transport_init(struct ntb_softc *ntb)
477 {
478         struct ntb_netdev *nt = &net_softc;
479         int rc, i;
480
481         nt->max_qps = max_num_clients;
482         ntb_register_transport(ntb, nt);
483         mtx_init(&nt->tx_lock, "ntb transport tx", NULL, MTX_DEF);
484         mtx_init(&nt->rx_lock, "ntb transport rx", NULL, MTX_DEF);
485
486         nt->qps = malloc(nt->max_qps * sizeof(struct ntb_transport_qp),
487                           M_NTB_IF, M_WAITOK|M_ZERO);
488
489         nt->qp_bitmap = ((uint64_t) 1 << nt->max_qps) - 1;
490
491         for (i = 0; i < nt->max_qps; i++)
492                 ntb_transport_init_queue(nt, i);
493
494         callout_init(&nt->link_work, 0);
495
496         rc = ntb_register_event_callback(ntb,
497                                          ntb_transport_event_callback);
498         if (rc != 0)
499                 goto err;
500
501         if (ntb_query_link_status(ntb)) {
502                 if (bootverbose)
503                         device_printf(ntb_get_device(ntb), "link up\n");
504                 callout_reset(&nt->link_work, 0, ntb_transport_link_work, nt);
505         }
506
507         return (0);
508
509 err:
510         free(nt->qps, M_NTB_IF);
511         ntb_unregister_transport(ntb);
512         return (rc);
513 }
514
515 static void
516 ntb_transport_free(void *transport)
517 {
518         struct ntb_netdev *nt = transport;
519         struct ntb_softc *ntb = nt->ntb;
520         int i;
521
522         nt->transport_link = NTB_LINK_DOWN;
523
524         callout_drain(&nt->link_work);
525
526         /* verify that all the qps are freed */
527         for (i = 0; i < nt->max_qps; i++)
528                 if (!test_bit(i, &nt->qp_bitmap))
529                         ntb_transport_free_queue(&nt->qps[i]);
530
531
532         ntb_unregister_event_callback(ntb);
533
534         for (i = 0; i < NTB_NUM_MW; i++)
535                 ntb_free_mw(nt, i);
536
537         free(nt->qps, M_NTB_IF);
538         ntb_unregister_transport(ntb);
539 }
540
541 static void
542 ntb_transport_init_queue(struct ntb_netdev *nt, unsigned int qp_num)
543 {
544         struct ntb_transport_qp *qp;
545         unsigned int num_qps_mw, tx_size;
546         uint8_t mw_num = QP_TO_MW(qp_num);
547
548         qp = &nt->qps[qp_num];
549         qp->qp_num = qp_num;
550         qp->transport = nt;
551         qp->ntb = nt->ntb;
552         qp->qp_link = NTB_LINK_DOWN;
553         qp->client_ready = NTB_LINK_DOWN;
554         qp->event_handler = NULL;
555
556         if (nt->max_qps % NTB_NUM_MW && mw_num < nt->max_qps % NTB_NUM_MW)
557                 num_qps_mw = nt->max_qps / NTB_NUM_MW + 1;
558         else
559                 num_qps_mw = nt->max_qps / NTB_NUM_MW;
560
561         tx_size = (unsigned int) ntb_get_mw_size(qp->ntb, mw_num) / num_qps_mw;
562         qp->rx_info = (struct ntb_rx_info *)
563             ((char *)ntb_get_mw_vbase(qp->ntb, mw_num) +
564             (qp_num / NTB_NUM_MW * tx_size));
565         tx_size -= sizeof(struct ntb_rx_info);
566
567         qp->tx_mw = qp->rx_info + 1;
568         /* Due to house-keeping, there must be at least 2 buffs */
569         qp->tx_max_frame = min(transport_mtu + sizeof(struct ntb_payload_header),
570             tx_size / 2);
571         qp->tx_max_entry = tx_size / qp->tx_max_frame;
572
573         callout_init(&qp->link_work, 0);
574         callout_init(&qp->queue_full, 1);
575         callout_init(&qp->rx_full, 1);
576
577         mtx_init(&qp->ntb_rx_pend_q_lock, "ntb rx pend q", NULL, MTX_SPIN);
578         mtx_init(&qp->ntb_rx_free_q_lock, "ntb rx free q", NULL, MTX_SPIN);
579         mtx_init(&qp->ntb_tx_free_q_lock, "ntb tx free q", NULL, MTX_SPIN);
580         TASK_INIT(&qp->rx_completion_task, 0, ntb_rx_completion_task, qp);
581
582         STAILQ_INIT(&qp->rx_pend_q);
583         STAILQ_INIT(&qp->rx_free_q);
584         STAILQ_INIT(&qp->tx_free_q);
585 }
586
587 static void
588 ntb_transport_free_queue(struct ntb_transport_qp *qp)
589 {
590         struct ntb_queue_entry *entry;
591
592         if (qp == NULL)
593                 return;
594
595         callout_drain(&qp->link_work);
596
597         ntb_unregister_db_callback(qp->ntb, qp->qp_num);
598
599         while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
600                 free(entry, M_NTB_IF);
601
602         while ((entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q)))
603                 free(entry, M_NTB_IF);
604
605         while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
606                 free(entry, M_NTB_IF);
607
608         set_bit(qp->qp_num, &qp->transport->qp_bitmap);
609 }
610
611 /**
612  * ntb_transport_create_queue - Create a new NTB transport layer queue
613  * @rx_handler: receive callback function
614  * @tx_handler: transmit callback function
615  * @event_handler: event callback function
616  *
617  * Create a new NTB transport layer queue and provide the queue with a callback
618  * routine for both transmit and receive.  The receive callback routine will be
619  * used to pass up data when the transport has received it on the queue.   The
620  * transmit callback routine will be called when the transport has completed the
621  * transmission of the data on the queue and the data is ready to be freed.
622  *
623  * RETURNS: pointer to newly created ntb_queue, NULL on error.
624  */
625 static struct ntb_transport_qp *
626 ntb_transport_create_queue(void *data, struct ntb_softc *pdev,
627     const struct ntb_queue_handlers *handlers)
628 {
629         struct ntb_queue_entry *entry;
630         struct ntb_transport_qp *qp;
631         struct ntb_netdev *nt;
632         unsigned int free_queue;
633         int rc, i;
634
635         nt = ntb_find_transport(pdev);
636         if (nt == NULL)
637                 goto err;
638
639         free_queue = ffs(nt->qp_bitmap);
640         if (free_queue == 0)
641                 goto err;
642
643         /* decrement free_queue to make it zero based */
644         free_queue--;
645
646         clear_bit(free_queue, &nt->qp_bitmap);
647
648         qp = &nt->qps[free_queue];
649         qp->cb_data = data;
650         qp->rx_handler = handlers->rx_handler;
651         qp->tx_handler = handlers->tx_handler;
652         qp->event_handler = handlers->event_handler;
653
654         for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
655                 entry = malloc(sizeof(struct ntb_queue_entry), M_NTB_IF,
656                     M_WAITOK|M_ZERO);
657                 entry->cb_data = nt->ifp;
658                 entry->buf = NULL;
659                 entry->len = transport_mtu;
660                 ntb_list_add(&qp->ntb_rx_pend_q_lock, entry, &qp->rx_pend_q);
661         }
662
663         for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
664                 entry = malloc(sizeof(struct ntb_queue_entry), M_NTB_IF,
665                     M_WAITOK|M_ZERO);
666                 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
667         }
668
669         rc = ntb_register_db_callback(qp->ntb, free_queue, qp,
670                                       ntb_transport_rxc_db);
671         if (rc != 0)
672                 goto err1;
673
674         return (qp);
675
676 err1:
677         while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
678                 free(entry, M_NTB_IF);
679         while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
680                 free(entry, M_NTB_IF);
681         set_bit(free_queue, &nt->qp_bitmap);
682 err:
683         return (NULL);
684 }
685
686 /**
687  * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
688  * @qp: NTB transport layer queue to be enabled
689  *
690  * Notify NTB transport layer of client readiness to use queue
691  */
692 static void
693 ntb_transport_link_up(struct ntb_transport_qp *qp)
694 {
695
696         if (qp == NULL)
697                 return;
698
699         qp->client_ready = NTB_LINK_UP;
700         if (bootverbose)
701                 device_printf(ntb_get_device(qp->ntb), "qp client ready\n");
702
703         if (qp->transport->transport_link == NTB_LINK_UP)
704                 callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
705 }
706
707
708
709 /* Transport Tx */
710
711 /**
712  * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
713  * @qp: NTB transport layer queue the entry is to be enqueued on
714  * @cb: per buffer pointer for callback function to use
715  * @data: pointer to data buffer that will be sent
716  * @len: length of the data buffer
717  *
718  * Enqueue a new transmit buffer onto the transport queue from which a NTB
719  * payload will be transmitted.  This assumes that a lock is behing held to
720  * serialize access to the qp.
721  *
722  * RETURNS: An appropriate ERRNO error value on error, or zero for success.
723  */
724 static int
725 ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
726     unsigned int len)
727 {
728         struct ntb_queue_entry *entry;
729         int rc;
730
731         if (qp == NULL || qp->qp_link != NTB_LINK_UP || len == 0) {
732                 CTR0(KTR_NTB, "TX: link not up");
733                 return (EINVAL);
734         }
735
736         entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
737         if (entry == NULL) {
738                 CTR0(KTR_NTB, "TX: could not get entry from tx_free_q");
739                 return (ENOMEM);
740         }
741         CTR1(KTR_NTB, "TX: got entry %p from tx_free_q", entry);
742
743         entry->cb_data = cb;
744         entry->buf = data;
745         entry->len = len;
746         entry->flags = 0;
747
748         rc = ntb_process_tx(qp, entry);
749         if (rc != 0) {
750                 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
751                 CTR1(KTR_NTB,
752                     "TX: process_tx failed. Returning entry %p to tx_free_q",
753                     entry);
754         }
755         return (rc);
756 }
757
758 static int
759 ntb_process_tx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry)
760 {
761         void *offset;
762
763         offset = (char *)qp->tx_mw + qp->tx_max_frame * qp->tx_index;
764         CTR3(KTR_NTB,
765             "TX: process_tx: tx_pkts=%u, tx_index=%u, remote entry=%u",
766             qp->tx_pkts, qp->tx_index, qp->remote_rx_info->entry);
767         if (qp->tx_index == qp->remote_rx_info->entry) {
768                 CTR0(KTR_NTB, "TX: ring full");
769                 qp->tx_ring_full++;
770                 return (EAGAIN);
771         }
772
773         if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
774                 if (qp->tx_handler != NULL)
775                         qp->tx_handler(qp, qp->cb_data, entry->buf,
776                                        EIO);
777
778                 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
779                 CTR1(KTR_NTB,
780                     "TX: frame too big. returning entry %p to tx_free_q",
781                     entry);
782                 return (0);
783         }
784         CTR2(KTR_NTB, "TX: copying entry %p to offset %p", entry, offset);
785         ntb_tx_copy_task(qp, entry, offset);
786
787         qp->tx_index++;
788         qp->tx_index %= qp->tx_max_entry;
789
790         qp->tx_pkts++;
791
792         return (0);
793 }
794
795 static void
796 ntb_tx_copy_task(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry,
797     void *offset)
798 {
799         struct ntb_payload_header *hdr;
800
801         CTR2(KTR_NTB, "TX: copying %d bytes to offset %p", entry->len, offset);
802         if (entry->buf != NULL)
803                 m_copydata((struct mbuf *)entry->buf, 0, entry->len, offset);
804
805         hdr = (struct ntb_payload_header *)((char *)offset + qp->tx_max_frame -
806             sizeof(struct ntb_payload_header));
807         hdr->len = entry->len; /* TODO: replace with bus_space_write */
808         hdr->ver = qp->tx_pkts; /* TODO: replace with bus_space_write */
809         wmb();
810         /* TODO: replace with bus_space_write */
811         hdr->flags = entry->flags | IF_NTB_DESC_DONE_FLAG;
812
813         ntb_ring_sdb(qp->ntb, qp->qp_num);
814
815         /* 
816          * The entry length can only be zero if the packet is intended to be a
817          * "link down" or similar.  Since no payload is being sent in these
818          * cases, there is nothing to add to the completion queue.
819          */
820         if (entry->len > 0) {
821                 qp->tx_bytes += entry->len;
822
823                 if (qp->tx_handler)
824                         qp->tx_handler(qp, qp->cb_data, entry->cb_data,
825                                        entry->len);
826         }
827
828         CTR2(KTR_NTB,
829             "TX: entry %p sent. hdr->ver = %d, Returning to tx_free_q", entry,
830             hdr->ver);
831         ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
832 }
833
834 static void
835 ntb_qp_full(void *arg)
836 {
837
838         CTR0(KTR_NTB, "TX: qp_full callout");
839         ntb_start(arg);
840 }
841
842 /* Transport Rx */
843 static void
844 ntb_transport_rxc_db(void *data, int db_num)
845 {
846         struct ntb_transport_qp *qp = data;
847
848         ntb_transport_rx(qp);
849 }
850
851 static void
852 ntb_rx_pendq_full(void *arg)
853 {
854
855         CTR0(KTR_NTB, "RX: ntb_rx_pendq_full callout");
856         ntb_transport_rx(arg);
857 }
858
859 static void
860 ntb_transport_rx(struct ntb_transport_qp *qp)
861 {
862         uint64_t i;
863         int rc;
864
865         /* 
866          * Limit the number of packets processed in a single interrupt to
867          * provide fairness to others
868          */
869         mtx_lock(&qp->transport->rx_lock);
870         CTR0(KTR_NTB, "RX: transport_rx");
871         for (i = 0; i < qp->rx_max_entry; i++) {
872                 rc = ntb_process_rxc(qp);
873                 if (rc != 0) {
874                         CTR0(KTR_NTB, "RX: process_rxc failed");
875                         break;
876                 }
877         }
878         mtx_unlock(&qp->transport->rx_lock);
879 }
880
881 static int
882 ntb_process_rxc(struct ntb_transport_qp *qp)
883 {
884         struct ntb_payload_header *hdr;
885         struct ntb_queue_entry *entry;
886         void *offset;
887
888         offset = (void *)
889             ((char *)qp->rx_buff + qp->rx_max_frame * qp->rx_index);
890         hdr = (void *)
891             ((char *)offset + qp->rx_max_frame -
892                 sizeof(struct ntb_payload_header));
893
894         CTR1(KTR_NTB, "RX: process_rxc rx_index = %u", qp->rx_index);
895         entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
896         if (entry == NULL) {
897                 qp->rx_err_no_buf++;
898                 CTR0(KTR_NTB, "RX: No entries in rx_pend_q");
899                 return (ENOMEM);
900         }
901         callout_stop(&qp->rx_full);
902         CTR1(KTR_NTB, "RX: rx entry %p from rx_pend_q", entry);
903
904         if ((hdr->flags & IF_NTB_DESC_DONE_FLAG) == 0) {
905                 CTR1(KTR_NTB,
906                     "RX: hdr not done. Returning entry %p to rx_pend_q", entry);
907                 ntb_list_add(&qp->ntb_rx_pend_q_lock, entry, &qp->rx_pend_q);
908                 qp->rx_ring_empty++;
909                 return (EAGAIN);
910         }
911
912         if (hdr->ver != (uint32_t) qp->rx_pkts) {
913                 CTR3(KTR_NTB,"RX: ver != rx_pkts (%x != %lx). "
914                     "Returning entry %p to rx_pend_q", hdr->ver, qp->rx_pkts,
915                     entry);
916                 ntb_list_add(&qp->ntb_rx_pend_q_lock, entry, &qp->rx_pend_q);
917                 qp->rx_err_ver++;
918                 return (EIO);
919         }
920
921         if ((hdr->flags & IF_NTB_LINK_DOWN_FLAG) != 0) {
922                 ntb_qp_link_down(qp);
923                 CTR1(KTR_NTB,
924                     "RX: link down. adding entry %p back to rx_pend_q", entry);
925                 ntb_list_add(&qp->ntb_rx_pend_q_lock, entry, &qp->rx_pend_q);
926                 goto out;
927         }
928
929         if (hdr->len <= entry->len) {
930                 entry->len = hdr->len;
931                 ntb_rx_copy_task(qp, entry, offset);
932         } else {
933                 CTR1(KTR_NTB,
934                     "RX: len too long. Returning entry %p to rx_pend_q", entry);
935                 ntb_list_add(&qp->ntb_rx_pend_q_lock, entry, &qp->rx_pend_q);
936
937                 qp->rx_err_oflow++;
938         }
939
940         qp->rx_bytes += hdr->len;
941         qp->rx_pkts++;
942         CTR1(KTR_NTB, "RX: received %ld rx_pkts", qp->rx_pkts);
943
944
945 out:
946         /* Ensure that the data is globally visible before clearing the flag */
947         wmb();
948         hdr->flags = 0;
949         /* TODO: replace with bus_space_write */
950         qp->rx_info->entry = qp->rx_index;
951
952         qp->rx_index++;
953         qp->rx_index %= qp->rx_max_entry;
954
955         return (0);
956 }
957
958 static void
959 ntb_rx_copy_task(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry,
960     void *offset)
961 {
962         struct ifnet *ifp = entry->cb_data;
963         unsigned int len = entry->len;
964         struct mbuf *m;
965
966         CTR2(KTR_NTB, "RX: copying %d bytes from offset %p", len, offset);
967         m = m_devget(offset, len, 0, ifp, NULL);
968         m->m_pkthdr.csum_flags = CSUM_IP_CHECKED | CSUM_IP_VALID;
969
970         entry->buf = (void *)m;
971
972         CTR2(KTR_NTB,
973             "RX: copied entry %p to mbuf %p. Adding entry to rx_free_q", entry,
974             m);
975         ntb_list_add(&qp->ntb_rx_free_q_lock, entry, &qp->rx_free_q);
976
977         taskqueue_enqueue(taskqueue_swi, &qp->rx_completion_task);
978 }
979
980 static void
981 ntb_rx_completion_task(void *arg, int pending)
982 {
983         struct ntb_transport_qp *qp = arg;
984         struct mbuf *m;
985         struct ntb_queue_entry *entry;
986
987         CTR0(KTR_NTB, "RX: rx_completion_task");
988
989         while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q))) {
990                 m = entry->buf;
991                 CTR2(KTR_NTB, "RX: completing entry %p, mbuf %p", entry, m);
992                 if (qp->rx_handler && qp->client_ready == NTB_LINK_UP)
993                         qp->rx_handler(qp, qp->cb_data, m, entry->len);
994
995                 entry->buf = NULL;
996                 entry->len = qp->transport->bufsize;
997
998                 CTR1(KTR_NTB,"RX: entry %p removed from rx_free_q "
999                     "and added to rx_pend_q", entry);
1000                 ntb_list_add(&qp->ntb_rx_pend_q_lock, entry, &qp->rx_pend_q);
1001                 if (qp->rx_err_no_buf > qp->last_rx_no_buf) {
1002                         qp->last_rx_no_buf = qp->rx_err_no_buf;
1003                         CTR0(KTR_NTB, "RX: could spawn rx task");
1004                         callout_reset(&qp->rx_full, hz / 1000, ntb_rx_pendq_full,
1005                             qp);
1006                 }
1007         }
1008 }
1009
1010 /* Link Event handler */
1011 static void
1012 ntb_transport_event_callback(void *data, enum ntb_hw_event event)
1013 {
1014         struct ntb_netdev *nt = data;
1015
1016         switch (event) {
1017         case NTB_EVENT_HW_LINK_UP:
1018                 if (bootverbose)
1019                         device_printf(ntb_get_device(nt->ntb), "HW link up\n");
1020                 callout_reset(&nt->link_work, 0, ntb_transport_link_work, nt);
1021                 break;
1022         case NTB_EVENT_HW_LINK_DOWN:
1023                 if (bootverbose)
1024                         device_printf(ntb_get_device(nt->ntb), "HW link down\n");
1025                 ntb_transport_link_cleanup(nt);
1026                 break;
1027         default:
1028                 panic("ntb: Unknown NTB event");
1029         }
1030 }
1031
1032 /* Link bring up */
1033 static void
1034 ntb_transport_link_work(void *arg)
1035 {
1036         struct ntb_netdev *nt = arg;
1037         struct ntb_softc *ntb = nt->ntb;
1038         struct ntb_transport_qp *qp;
1039         uint64_t val64;
1040         uint32_t val, i, num_mw;
1041         int rc;
1042
1043         if (ntb_has_feature(ntb, NTB_REGS_THRU_MW))
1044                 num_mw = NTB_NUM_MW - 1;
1045         else
1046                 num_mw = NTB_NUM_MW;
1047
1048         /* send the local info, in the opposite order of the way we read it */
1049         for (i = 0; i < num_mw; i++) {
1050                 rc = ntb_write_remote_spad(ntb, IF_NTB_MW0_SZ_HIGH + (i * 2),
1051                     ntb_get_mw_size(ntb, i) >> 32);
1052                 if (rc != 0)
1053                         goto out;
1054
1055                 rc = ntb_write_remote_spad(ntb, IF_NTB_MW0_SZ_LOW + (i * 2),
1056                     (uint32_t)ntb_get_mw_size(ntb, i));
1057                 if (rc != 0)
1058                         goto out;
1059         }
1060
1061         rc = ntb_write_remote_spad(ntb, IF_NTB_NUM_MWS, num_mw);
1062         if (rc != 0)
1063                 goto out;
1064
1065         rc = ntb_write_remote_spad(ntb, IF_NTB_NUM_QPS, nt->max_qps);
1066         if (rc != 0)
1067                 goto out;
1068
1069         rc = ntb_write_remote_spad(ntb, IF_NTB_VERSION, NTB_TRANSPORT_VERSION);
1070         if (rc != 0)
1071                 goto out;
1072
1073         /* Query the remote side for its info */
1074         rc = ntb_read_local_spad(ntb, IF_NTB_VERSION, &val);
1075         if (rc != 0)
1076                 goto out;
1077
1078         if (val != NTB_TRANSPORT_VERSION)
1079                 goto out;
1080
1081         rc = ntb_read_local_spad(ntb, IF_NTB_NUM_QPS, &val);
1082         if (rc != 0)
1083                 goto out;
1084
1085         if (val != nt->max_qps)
1086                 goto out;
1087
1088         rc = ntb_read_local_spad(ntb, IF_NTB_NUM_MWS, &val);
1089         if (rc != 0)
1090                 goto out;
1091
1092         if (val != num_mw)
1093                 goto out;
1094
1095         for (i = 0; i < num_mw; i++) {
1096                 rc = ntb_read_local_spad(ntb, IF_NTB_MW0_SZ_HIGH + (i * 2),
1097                     &val);
1098                 if (rc != 0)
1099                         goto free_mws;
1100
1101                 val64 = (uint64_t)val << 32;
1102
1103                 rc = ntb_read_local_spad(ntb, IF_NTB_MW0_SZ_LOW + (i * 2),
1104                     &val);
1105                 if (rc != 0)
1106                         goto free_mws;
1107
1108                 val64 |= val;
1109
1110                 rc = ntb_set_mw(nt, i, val64);
1111                 if (rc != 0)
1112                         goto free_mws;
1113         }
1114
1115         nt->transport_link = NTB_LINK_UP;
1116         if (bootverbose)
1117                 device_printf(ntb_get_device(ntb), "transport link up\n");
1118
1119         for (i = 0; i < nt->max_qps; i++) {
1120                 qp = &nt->qps[i];
1121
1122                 ntb_transport_setup_qp_mw(nt, i);
1123
1124                 if (qp->client_ready == NTB_LINK_UP)
1125                         callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
1126         }
1127
1128         return;
1129
1130 free_mws:
1131         for (i = 0; i < NTB_NUM_MW; i++)
1132                 ntb_free_mw(nt, i);
1133 out:
1134         if (ntb_query_link_status(ntb))
1135                 callout_reset(&nt->link_work,
1136                     NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_transport_link_work, nt);
1137 }
1138
1139 static int
1140 ntb_set_mw(struct ntb_netdev *nt, int num_mw, unsigned int size)
1141 {
1142         struct ntb_transport_mw *mw = &nt->mw[num_mw];
1143
1144         /* No need to re-setup */
1145         if (mw->size == size)
1146                 return (0);
1147
1148         if (mw->size != 0)
1149                 ntb_free_mw(nt, num_mw);
1150
1151         /* Alloc memory for receiving data.  Must be 4k aligned */
1152         mw->size = size;
1153
1154         mw->virt_addr = contigmalloc(mw->size, M_NTB_IF, M_ZERO, 0,
1155             BUS_SPACE_MAXADDR, mw->size, 0);
1156         if (mw->virt_addr == NULL) {
1157                 mw->size = 0;
1158                 printf("ntb: Unable to allocate MW buffer of size %d\n",
1159                     (int)mw->size);
1160                 return (ENOMEM);
1161         }
1162         /* TODO: replace with bus_space_* functions */
1163         mw->dma_addr = vtophys(mw->virt_addr);
1164
1165         /* Notify HW the memory location of the receive buffer */
1166         ntb_set_mw_addr(nt->ntb, num_mw, mw->dma_addr);
1167
1168         return (0);
1169 }
1170
1171 static void
1172 ntb_free_mw(struct ntb_netdev *nt, int num_mw)
1173 {
1174         struct ntb_transport_mw *mw = &nt->mw[num_mw];
1175
1176         if (mw->virt_addr == NULL)
1177                 return;
1178
1179         contigfree(mw->virt_addr, mw->size, M_NTB_IF);
1180         mw->virt_addr = NULL;
1181 }
1182
1183 static void
1184 ntb_transport_setup_qp_mw(struct ntb_netdev *nt, unsigned int qp_num)
1185 {
1186         struct ntb_transport_qp *qp = &nt->qps[qp_num];
1187         void *offset;
1188         unsigned int rx_size, num_qps_mw;
1189         uint8_t mw_num = QP_TO_MW(qp_num);
1190         unsigned int i;
1191
1192         if (nt->max_qps % NTB_NUM_MW && mw_num < nt->max_qps % NTB_NUM_MW)
1193                 num_qps_mw = nt->max_qps / NTB_NUM_MW + 1;
1194         else
1195                 num_qps_mw = nt->max_qps / NTB_NUM_MW;
1196
1197         rx_size = (unsigned int) nt->mw[mw_num].size / num_qps_mw;
1198         qp->remote_rx_info = (void *)((uint8_t *)nt->mw[mw_num].virt_addr +
1199                              (qp_num / NTB_NUM_MW * rx_size));
1200         rx_size -= sizeof(struct ntb_rx_info);
1201
1202         qp->rx_buff = qp->remote_rx_info + 1;
1203         /* Due to house-keeping, there must be at least 2 buffs */
1204         qp->rx_max_frame = min(transport_mtu + sizeof(struct ntb_payload_header),
1205             rx_size / 2);
1206         qp->rx_max_entry = rx_size / qp->rx_max_frame;
1207         qp->rx_index = 0;
1208
1209         qp->remote_rx_info->entry = qp->rx_max_entry - 1;
1210
1211         /* setup the hdr offsets with 0's */
1212         for (i = 0; i < qp->rx_max_entry; i++) {
1213                 offset = (void *)((uint8_t *)qp->rx_buff +
1214                     qp->rx_max_frame * (i + 1) -
1215                     sizeof(struct ntb_payload_header));
1216                 memset(offset, 0, sizeof(struct ntb_payload_header));
1217         }
1218
1219         qp->rx_pkts = 0;
1220         qp->tx_pkts = 0;
1221         qp->tx_index = 0;
1222 }
1223
1224 static void
1225 ntb_qp_link_work(void *arg)
1226 {
1227         struct ntb_transport_qp *qp = arg;
1228         struct ntb_softc *ntb = qp->ntb;
1229         struct ntb_netdev *nt = qp->transport;
1230         int rc, val;
1231
1232
1233         rc = ntb_read_remote_spad(ntb, IF_NTB_QP_LINKS, &val);
1234         if (rc != 0)
1235                 return;
1236
1237         rc = ntb_write_remote_spad(ntb, IF_NTB_QP_LINKS, val | 1 << qp->qp_num);
1238
1239         /* query remote spad for qp ready bits */
1240         rc = ntb_read_local_spad(ntb, IF_NTB_QP_LINKS, &val);
1241
1242         /* See if the remote side is up */
1243         if ((1 << qp->qp_num & val) != 0) {
1244                 qp->qp_link = NTB_LINK_UP;
1245                 if (qp->event_handler != NULL)
1246                         qp->event_handler(qp->cb_data, NTB_LINK_UP);
1247                 if (bootverbose)
1248                         device_printf(ntb_get_device(ntb), "qp link up\n");
1249         } else if (nt->transport_link == NTB_LINK_UP) {
1250                 callout_reset(&qp->link_work,
1251                     NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_qp_link_work, qp);
1252         }
1253 }
1254
1255 /* Link down event*/
1256 static void
1257 ntb_transport_link_cleanup(struct ntb_netdev *nt)
1258 {
1259         int i;
1260
1261         if (nt->transport_link == NTB_LINK_DOWN)
1262                 callout_drain(&nt->link_work);
1263         else
1264                 nt->transport_link = NTB_LINK_DOWN;
1265
1266         /* Pass along the info to any clients */
1267         for (i = 0; i < nt->max_qps; i++)
1268                 if (!test_bit(i, &nt->qp_bitmap))
1269                         ntb_qp_link_down(&nt->qps[i]);
1270
1271         /* 
1272          * The scratchpad registers keep the values if the remote side
1273          * goes down, blast them now to give them a sane value the next
1274          * time they are accessed
1275          */
1276         for (i = 0; i < IF_NTB_MAX_SPAD; i++)
1277                 ntb_write_local_spad(nt->ntb, i, 0);
1278 }
1279
1280
1281 static void
1282 ntb_qp_link_down(struct ntb_transport_qp *qp)
1283 {
1284
1285         ntb_qp_link_cleanup(qp);
1286 }
1287
1288 static void
1289 ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
1290 {
1291         struct ntb_netdev *nt = qp->transport;
1292
1293         if (qp->qp_link == NTB_LINK_DOWN) {
1294                 callout_drain(&qp->link_work);
1295                 return;
1296         }
1297
1298         if (qp->event_handler != NULL)
1299                 qp->event_handler(qp->cb_data, NTB_LINK_DOWN);
1300
1301         qp->qp_link = NTB_LINK_DOWN;
1302
1303         if (nt->transport_link == NTB_LINK_UP)
1304                 callout_reset(&qp->link_work,
1305                     NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_qp_link_work, qp);
1306 }
1307
1308 /* Link commanded down */
1309 /**
1310  * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1311  * @qp: NTB transport layer queue to be disabled
1312  *
1313  * Notify NTB transport layer of client's desire to no longer receive data on
1314  * transport queue specified.  It is the client's responsibility to ensure all
1315  * entries on queue are purged or otherwise handled appropraitely.
1316  */
1317 static void
1318 ntb_transport_link_down(struct ntb_transport_qp *qp)
1319 {
1320         int rc, val;
1321
1322         if (qp == NULL)
1323                 return;
1324
1325         qp->client_ready = NTB_LINK_DOWN;
1326
1327         rc = ntb_read_remote_spad(qp->ntb, IF_NTB_QP_LINKS, &val);
1328         if (rc != 0)
1329                 return;
1330
1331         rc = ntb_write_remote_spad(qp->ntb, IF_NTB_QP_LINKS,
1332            val & ~(1 << qp->qp_num));
1333
1334         if (qp->qp_link == NTB_LINK_UP)
1335                 ntb_send_link_down(qp);
1336         else
1337                 callout_drain(&qp->link_work);
1338
1339 }
1340
1341 static void
1342 ntb_send_link_down(struct ntb_transport_qp *qp)
1343 {
1344         struct ntb_queue_entry *entry;
1345         int i, rc;
1346
1347         if (qp->qp_link == NTB_LINK_DOWN)
1348                 return;
1349
1350         qp->qp_link = NTB_LINK_DOWN;
1351
1352         for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1353                 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1354                 if (entry != NULL)
1355                         break;
1356                 pause("NTB Wait for link down", hz / 10);
1357         }
1358
1359         if (entry == NULL)
1360                 return;
1361
1362         entry->cb_data = NULL;
1363         entry->buf = NULL;
1364         entry->len = 0;
1365         entry->flags = IF_NTB_LINK_DOWN_FLAG;
1366
1367         mtx_lock(&qp->transport->tx_lock);
1368         rc = ntb_process_tx(qp, entry);
1369         if (rc != 0)
1370                 printf("ntb: Failed to send link down\n");
1371         mtx_unlock(&qp->transport->tx_lock);
1372 }
1373
1374
1375 /* List Management */
1376
1377 static void
1378 ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry,
1379     struct ntb_queue_list *list)
1380 {
1381
1382         mtx_lock_spin(lock);
1383         STAILQ_INSERT_TAIL(list, entry, entry);
1384         mtx_unlock_spin(lock);
1385 }
1386
1387 static struct ntb_queue_entry *
1388 ntb_list_rm(struct mtx *lock, struct ntb_queue_list *list)
1389 {
1390         struct ntb_queue_entry *entry;
1391
1392         mtx_lock_spin(lock);
1393         if (STAILQ_EMPTY(list)) {
1394                 entry = NULL;
1395                 goto out;
1396         }
1397         entry = STAILQ_FIRST(list);
1398         STAILQ_REMOVE_HEAD(list, entry);
1399 out:
1400         mtx_unlock_spin(lock);
1401
1402         return (entry);
1403 }
1404
1405 /* Helper functions */
1406 /* TODO: This too should really be part of the kernel */
1407 #define EUI48_MULTICAST                 1 << 0
1408 #define EUI48_LOCALLY_ADMINISTERED      1 << 1
1409 static void
1410 create_random_local_eui48(u_char *eaddr)
1411 {
1412         static uint8_t counter = 0;
1413         uint32_t seed = ticks;
1414
1415         eaddr[0] = EUI48_LOCALLY_ADMINISTERED;
1416         memcpy(&eaddr[1], &seed, sizeof(uint32_t));
1417         eaddr[5] = counter++;
1418 }
1419
1420 /**
1421  * ntb_transport_max_size - Query the max payload size of a qp
1422  * @qp: NTB transport layer queue to be queried
1423  *
1424  * Query the maximum payload size permissible on the given qp
1425  *
1426  * RETURNS: the max payload size of a qp
1427  */
1428 static unsigned int
1429 ntb_transport_max_size(struct ntb_transport_qp *qp)
1430 {
1431
1432         if (qp == NULL)
1433                 return (0);
1434
1435         return (qp->tx_max_frame - sizeof(struct ntb_payload_header));
1436 }