2 * Copyright (C) 2013 Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/systm.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/taskqueue.h>
44 #include <net/if_media.h>
45 #include <net/if_types.h>
46 #include <net/if_var.h>
48 #include <net/ethernet.h>
51 #include <machine/bus.h>
52 #include <machine/cpufunc.h>
53 #include <machine/pmap.h>
55 #include "../ntb_hw/ntb_hw.h"
58 * The Non-Transparent Bridge (NTB) is a device on some Intel processors that
59 * allows you to connect two systems using a PCI-e link.
61 * This module contains a protocol for sending and receiving messages, and
62 * exposes that protocol through a simulated ethernet device called ntb.
64 * NOTE: Much of the code in this module is shared with Linux. Any patches may
65 * be picked up and redistributed in Linux with a dual GPL/BSD license.
68 /* TODO: These functions should really be part of the kernel */
69 #define test_bit(pos, bitmap_addr) (*(bitmap_addr) & 1UL << (pos))
70 #define set_bit(pos, bitmap_addr) *(bitmap_addr) |= 1UL << (pos)
71 #define clear_bit(pos, bitmap_addr) *(bitmap_addr) &= ~(1UL << (pos))
73 #define KTR_NTB KTR_SPARE3
75 #define NTB_TRANSPORT_VERSION 3
76 #define NTB_RX_MAX_PKTS 64
77 #define NTB_RXQ_SIZE 300
79 static unsigned int transport_mtu = 0x4000 + ETHER_HDR_LEN + ETHER_CRC_LEN;
80 static unsigned int max_num_clients = 1;
82 STAILQ_HEAD(ntb_queue_list, ntb_queue_entry);
84 struct ntb_queue_entry {
85 /* ntb_queue list reference */
86 STAILQ_ENTRY(ntb_queue_entry) entry;
88 /* info on data to be transfered */
99 struct ntb_transport_qp {
100 struct ntb_netdev *transport;
101 struct ntb_softc *ntb;
107 uint8_t qp_num; /* Only 64 QPs are allowed. 0-63 */
109 struct ntb_rx_info *rx_info;
110 struct ntb_rx_info *remote_rx_info;
112 void (*tx_handler) (struct ntb_transport_qp *qp, void *qp_data,
113 void *data, int len);
114 struct ntb_queue_list tx_free_q;
115 struct mtx ntb_tx_free_q_lock;
118 uint64_t tx_max_entry;
119 uint64_t tx_max_frame;
121 void (*rx_handler) (struct ntb_transport_qp *qp, void *qp_data,
122 void *data, int len);
123 struct ntb_queue_list rx_pend_q;
124 struct ntb_queue_list rx_free_q;
125 struct mtx ntb_rx_pend_q_lock;
126 struct mtx ntb_rx_free_q_lock;
127 struct task rx_completion_task;
130 uint64_t rx_max_entry;
131 uint64_t rx_max_frame;
133 void (*event_handler) (void *data, int status);
134 struct callout link_work;
135 struct callout queue_full;
136 struct callout rx_full;
138 uint64_t last_rx_no_buf;
143 uint64_t rx_ring_empty;
144 uint64_t rx_err_no_buf;
145 uint64_t rx_err_oflow;
149 uint64_t tx_ring_full;
152 struct ntb_queue_handlers {
153 void (*rx_handler) (struct ntb_transport_qp *qp, void *qp_data,
154 void *data, int len);
155 void (*tx_handler) (struct ntb_transport_qp *qp, void *qp_data,
156 void *data, int len);
157 void (*event_handler) (void *data, int status);
161 struct ntb_transport_mw {
168 struct ntb_softc *ntb;
170 struct ntb_transport_mw mw[NTB_NUM_MW];
171 struct ntb_transport_qp *qps;
175 struct callout link_work;
176 struct ntb_transport_qp *qp;
178 u_char eaddr[ETHER_ADDR_LEN];
183 static struct ntb_netdev net_softc;
186 IF_NTB_DESC_DONE_FLAG = 1 << 0,
187 IF_NTB_LINK_DOWN_FLAG = 1 << 1,
190 struct ntb_payload_header {
198 * The order of this enum is part of the if_ntb remote protocol. Do
199 * not reorder without bumping protocol version (and it's probably best
200 * to keep the protocol in lock-step with the Linux NTB driver.
207 * N.B.: transport_link_work assumes MW1 enums = MW0 + 2.
216 #define QP_TO_MW(qp) ((qp) % NTB_NUM_MW)
217 #define NTB_QP_DEF_NUM_ENTRIES 100
218 #define NTB_LINK_DOWN_TIMEOUT 10
220 static int ntb_handle_module_events(struct module *m, int what, void *arg);
221 static int ntb_setup_interface(void);
222 static int ntb_teardown_interface(void);
223 static void ntb_net_init(void *arg);
224 static int ntb_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
225 static void ntb_start(struct ifnet *ifp);
226 static void ntb_net_tx_handler(struct ntb_transport_qp *qp, void *qp_data,
227 void *data, int len);
228 static void ntb_net_rx_handler(struct ntb_transport_qp *qp, void *qp_data,
229 void *data, int len);
230 static void ntb_net_event_handler(void *data, int status);
231 static int ntb_transport_init(struct ntb_softc *ntb);
232 static void ntb_transport_free(void *transport);
233 static void ntb_transport_init_queue(struct ntb_netdev *nt,
234 unsigned int qp_num);
235 static void ntb_transport_free_queue(struct ntb_transport_qp *qp);
236 static struct ntb_transport_qp * ntb_transport_create_queue(void *data,
237 struct ntb_softc *pdev, const struct ntb_queue_handlers *handlers);
238 static void ntb_transport_link_up(struct ntb_transport_qp *qp);
239 static int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb,
240 void *data, unsigned int len);
241 static int ntb_process_tx(struct ntb_transport_qp *qp,
242 struct ntb_queue_entry *entry);
243 static void ntb_tx_copy_task(struct ntb_transport_qp *qp,
244 struct ntb_queue_entry *entry, void *offset);
245 static void ntb_qp_full(void *arg);
246 static void ntb_transport_rxc_db(void *data, int db_num);
247 static void ntb_rx_pendq_full(void *arg);
248 static void ntb_transport_rx(struct ntb_transport_qp *qp);
249 static int ntb_process_rxc(struct ntb_transport_qp *qp);
250 static void ntb_rx_copy_task(struct ntb_transport_qp *qp,
251 struct ntb_queue_entry *entry, void *offset);
252 static void ntb_rx_completion_task(void *arg, int pending);
253 static void ntb_transport_event_callback(void *data, enum ntb_hw_event event);
254 static void ntb_transport_link_work(void *arg);
255 static int ntb_set_mw(struct ntb_netdev *nt, int num_mw, unsigned int size);
256 static void ntb_free_mw(struct ntb_netdev *nt, int num_mw);
257 static void ntb_transport_setup_qp_mw(struct ntb_netdev *nt,
258 unsigned int qp_num);
259 static void ntb_qp_link_work(void *arg);
260 static void ntb_transport_link_cleanup(struct ntb_netdev *nt);
261 static void ntb_qp_link_down(struct ntb_transport_qp *qp);
262 static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp);
263 static void ntb_transport_link_down(struct ntb_transport_qp *qp);
264 static void ntb_send_link_down(struct ntb_transport_qp *qp);
265 static void ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry,
266 struct ntb_queue_list *list);
267 static struct ntb_queue_entry *ntb_list_rm(struct mtx *lock,
268 struct ntb_queue_list *list);
269 static void create_random_local_eui48(u_char *eaddr);
270 static unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp);
272 MALLOC_DEFINE(M_NTB_IF, "if_ntb", "ntb network driver");
274 /* Module setup and teardown */
276 ntb_handle_module_events(struct module *m, int what, void *arg)
282 err = ntb_setup_interface();
285 err = ntb_teardown_interface();
294 static moduledata_t if_ntb_mod = {
296 ntb_handle_module_events,
300 DECLARE_MODULE(if_ntb, if_ntb_mod, SI_SUB_KLD, SI_ORDER_ANY);
301 MODULE_DEPEND(if_ntb, ntb_hw, 1, 1, 1);
304 ntb_setup_interface()
307 struct ntb_queue_handlers handlers = { ntb_net_rx_handler,
308 ntb_net_tx_handler, ntb_net_event_handler };
310 net_softc.ntb = devclass_get_softc(devclass_find("ntb_hw"), 0);
311 if (net_softc.ntb == NULL) {
312 printf("ntb: Cannot find devclass\n");
316 ntb_transport_init(net_softc.ntb);
318 ifp = net_softc.ifp = if_alloc(IFT_ETHER);
320 printf("ntb: cannot allocate ifnet structure\n");
324 net_softc.qp = ntb_transport_create_queue(ifp, net_softc.ntb,
326 if_initname(ifp, "ntb", 0);
327 ifp->if_init = ntb_net_init;
328 ifp->if_softc = &net_softc;
329 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX;
330 ifp->if_ioctl = ntb_ioctl;
331 ifp->if_start = ntb_start;
332 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN);
333 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN;
334 IFQ_SET_READY(&ifp->if_snd);
335 create_random_local_eui48(net_softc.eaddr);
336 ether_ifattach(ifp, net_softc.eaddr);
337 ifp->if_capabilities = IFCAP_HWCSUM | IFCAP_JUMBO_MTU;
338 ifp->if_capenable = ifp->if_capabilities;
340 ntb_transport_link_up(net_softc.qp);
341 net_softc.bufsize = ntb_transport_max_size(net_softc.qp) +
342 sizeof(struct ether_header);
347 ntb_teardown_interface()
350 if (net_softc.qp != NULL)
351 ntb_transport_link_down(net_softc.qp);
353 if (net_softc.ifp != NULL) {
354 ether_ifdetach(net_softc.ifp);
355 if_free(net_softc.ifp);
358 if (net_softc.qp != NULL) {
359 ntb_transport_free_queue(net_softc.qp);
360 ntb_transport_free(&net_softc);
366 /* Network device interface */
369 ntb_net_init(void *arg)
371 struct ntb_netdev *ntb_softc = arg;
372 struct ifnet *ifp = ntb_softc->ifp;
374 ifp->if_drv_flags |= IFF_DRV_RUNNING;
375 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
376 ifp->if_flags |= IFF_UP;
377 if_link_state_change(ifp, LINK_STATE_UP);
381 ntb_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
383 struct ntb_netdev *nt = ifp->if_softc;
384 struct ifreq *ifr = (struct ifreq *)data;
390 if (ifr->ifr_mtu > ntb_transport_max_size(nt->qp) -
391 ETHER_HDR_LEN - ETHER_CRC_LEN) {
396 ifp->if_mtu = ifr->ifr_mtu;
400 error = ether_ioctl(ifp, command, data);
409 ntb_start(struct ifnet *ifp)
412 struct ntb_netdev *nt = ifp->if_softc;
415 mtx_lock(&nt->tx_lock);
416 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE;
417 CTR0(KTR_NTB, "TX: ntb_start");
418 while (!IFQ_DRV_IS_EMPTY(&ifp->if_snd)) {
419 IFQ_DRV_DEQUEUE(&ifp->if_snd, m_head);
420 CTR1(KTR_NTB, "TX: start mbuf %p", m_head);
421 rc = ntb_transport_tx_enqueue(nt->qp, m_head, m_head,
422 m_length(m_head, NULL));
425 "TX: could not tx mbuf %p. Returning to snd q",
428 ifp->if_drv_flags |= IFF_DRV_OACTIVE;
429 IFQ_DRV_PREPEND(&ifp->if_snd, m_head);
430 callout_reset(&nt->qp->queue_full, hz / 1000,
437 mtx_unlock(&nt->tx_lock);
440 /* Network Device Callbacks */
442 ntb_net_tx_handler(struct ntb_transport_qp *qp, void *qp_data, void *data,
447 CTR1(KTR_NTB, "TX: tx_handler freeing mbuf %p", data);
451 ntb_net_rx_handler(struct ntb_transport_qp *qp, void *qp_data, void *data,
454 struct mbuf *m = data;
455 struct ifnet *ifp = qp_data;
457 CTR0(KTR_NTB, "RX: rx handler");
458 (*ifp->if_input)(ifp, m);
462 ntb_net_event_handler(void *data, int status)
467 /* Transport Init and teardown */
470 ntb_transport_init(struct ntb_softc *ntb)
472 struct ntb_netdev *nt = &net_softc;
475 nt->max_qps = max_num_clients;
476 ntb_register_transport(ntb, nt);
477 mtx_init(&nt->tx_lock, "ntb transport tx", NULL, MTX_DEF);
478 mtx_init(&nt->rx_lock, "ntb transport rx", NULL, MTX_DEF);
480 nt->qps = malloc(nt->max_qps * sizeof(struct ntb_transport_qp),
481 M_NTB_IF, M_WAITOK|M_ZERO);
483 nt->qp_bitmap = ((uint64_t) 1 << nt->max_qps) - 1;
485 for (i = 0; i < nt->max_qps; i++)
486 ntb_transport_init_queue(nt, i);
488 callout_init(&nt->link_work, 0);
490 rc = ntb_register_event_callback(ntb,
491 ntb_transport_event_callback);
495 if (ntb_query_link_status(ntb)) {
497 device_printf(ntb_get_device(ntb), "link up\n");
498 callout_reset(&nt->link_work, 0, ntb_transport_link_work, nt);
504 free(nt->qps, M_NTB_IF);
505 ntb_unregister_transport(ntb);
510 ntb_transport_free(void *transport)
512 struct ntb_netdev *nt = transport;
513 struct ntb_softc *ntb = nt->ntb;
516 nt->transport_link = NTB_LINK_DOWN;
518 callout_drain(&nt->link_work);
520 /* verify that all the qps are freed */
521 for (i = 0; i < nt->max_qps; i++)
522 if (!test_bit(i, &nt->qp_bitmap))
523 ntb_transport_free_queue(&nt->qps[i]);
526 ntb_unregister_event_callback(ntb);
528 for (i = 0; i < NTB_NUM_MW; i++)
531 free(nt->qps, M_NTB_IF);
532 ntb_unregister_transport(ntb);
536 ntb_transport_init_queue(struct ntb_netdev *nt, unsigned int qp_num)
538 struct ntb_transport_qp *qp;
539 unsigned int num_qps_mw, tx_size;
540 uint8_t mw_num = QP_TO_MW(qp_num);
542 qp = &nt->qps[qp_num];
546 qp->qp_link = NTB_LINK_DOWN;
547 qp->client_ready = NTB_LINK_DOWN;
548 qp->event_handler = NULL;
550 if (nt->max_qps % NTB_NUM_MW && mw_num < nt->max_qps % NTB_NUM_MW)
551 num_qps_mw = nt->max_qps / NTB_NUM_MW + 1;
553 num_qps_mw = nt->max_qps / NTB_NUM_MW;
555 tx_size = (unsigned int) ntb_get_mw_size(qp->ntb, mw_num) / num_qps_mw;
556 qp->rx_info = (struct ntb_rx_info *)
557 ((char *)ntb_get_mw_vbase(qp->ntb, mw_num) +
558 (qp_num / NTB_NUM_MW * tx_size));
559 tx_size -= sizeof(struct ntb_rx_info);
561 qp->tx_mw = qp->rx_info + 1;
562 qp->tx_max_frame = min(transport_mtu + sizeof(struct ntb_payload_header),
564 qp->tx_max_entry = tx_size / qp->tx_max_frame;
567 callout_init(&qp->link_work, 0);
568 callout_init(&qp->queue_full, 1);
569 callout_init(&qp->rx_full, 1);
571 mtx_init(&qp->ntb_rx_pend_q_lock, "ntb rx pend q", NULL, MTX_SPIN);
572 mtx_init(&qp->ntb_rx_free_q_lock, "ntb rx free q", NULL, MTX_SPIN);
573 mtx_init(&qp->ntb_tx_free_q_lock, "ntb tx free q", NULL, MTX_SPIN);
574 TASK_INIT(&qp->rx_completion_task, 0, ntb_rx_completion_task, qp);
576 STAILQ_INIT(&qp->rx_pend_q);
577 STAILQ_INIT(&qp->rx_free_q);
578 STAILQ_INIT(&qp->tx_free_q);
582 ntb_transport_free_queue(struct ntb_transport_qp *qp)
584 struct ntb_queue_entry *entry;
589 callout_drain(&qp->link_work);
591 ntb_unregister_db_callback(qp->ntb, qp->qp_num);
593 while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
594 free(entry, M_NTB_IF);
596 while ((entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q)))
597 free(entry, M_NTB_IF);
599 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
600 free(entry, M_NTB_IF);
602 set_bit(qp->qp_num, &qp->transport->qp_bitmap);
606 * ntb_transport_create_queue - Create a new NTB transport layer queue
607 * @rx_handler: receive callback function
608 * @tx_handler: transmit callback function
609 * @event_handler: event callback function
611 * Create a new NTB transport layer queue and provide the queue with a callback
612 * routine for both transmit and receive. The receive callback routine will be
613 * used to pass up data when the transport has received it on the queue. The
614 * transmit callback routine will be called when the transport has completed the
615 * transmission of the data on the queue and the data is ready to be freed.
617 * RETURNS: pointer to newly created ntb_queue, NULL on error.
619 static struct ntb_transport_qp *
620 ntb_transport_create_queue(void *data, struct ntb_softc *pdev,
621 const struct ntb_queue_handlers *handlers)
623 struct ntb_queue_entry *entry;
624 struct ntb_transport_qp *qp;
625 struct ntb_netdev *nt;
626 unsigned int free_queue;
629 nt = ntb_find_transport(pdev);
633 free_queue = ffs(nt->qp_bitmap);
637 /* decrement free_queue to make it zero based */
640 clear_bit(free_queue, &nt->qp_bitmap);
642 qp = &nt->qps[free_queue];
644 qp->rx_handler = handlers->rx_handler;
645 qp->tx_handler = handlers->tx_handler;
646 qp->event_handler = handlers->event_handler;
648 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
649 entry = malloc(sizeof(struct ntb_queue_entry), M_NTB_IF,
651 entry->cb_data = nt->ifp;
653 entry->len = transport_mtu;
654 ntb_list_add(&qp->ntb_rx_pend_q_lock, entry, &qp->rx_pend_q);
657 for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
658 entry = malloc(sizeof(struct ntb_queue_entry), M_NTB_IF,
660 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
663 rc = ntb_register_db_callback(qp->ntb, free_queue, qp,
664 ntb_transport_rxc_db);
671 while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
672 free(entry, M_NTB_IF);
673 while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
674 free(entry, M_NTB_IF);
675 set_bit(free_queue, &nt->qp_bitmap);
681 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
682 * @qp: NTB transport layer queue to be enabled
684 * Notify NTB transport layer of client readiness to use queue
687 ntb_transport_link_up(struct ntb_transport_qp *qp)
693 qp->client_ready = NTB_LINK_UP;
695 device_printf(ntb_get_device(qp->ntb), "qp client ready\n");
697 if (qp->transport->transport_link == NTB_LINK_UP)
698 callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
706 * ntb_transport_tx_enqueue - Enqueue a new NTB queue entry
707 * @qp: NTB transport layer queue the entry is to be enqueued on
708 * @cb: per buffer pointer for callback function to use
709 * @data: pointer to data buffer that will be sent
710 * @len: length of the data buffer
712 * Enqueue a new transmit buffer onto the transport queue from which a NTB
713 * payload will be transmitted. This assumes that a lock is behing held to
714 * serialize access to the qp.
716 * RETURNS: An appropriate ERRNO error value on error, or zero for success.
719 ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
722 struct ntb_queue_entry *entry;
725 if (qp == NULL || qp->qp_link != NTB_LINK_UP || len == 0) {
726 CTR0(KTR_NTB, "TX: link not up");
730 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
732 CTR0(KTR_NTB, "TX: could not get entry from tx_free_q");
735 CTR1(KTR_NTB, "TX: got entry %p from tx_free_q", entry);
742 rc = ntb_process_tx(qp, entry);
744 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
746 "TX: process_tx failed. Returning entry %p to tx_free_q",
753 ntb_process_tx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry)
757 offset = (char *)qp->tx_mw + qp->tx_max_frame * qp->tx_index;
759 "TX: process_tx: tx_pkts=%u, tx_index=%u, remote entry=%u",
760 qp->tx_pkts, qp->tx_index, qp->remote_rx_info->entry);
761 if (qp->tx_index == qp->remote_rx_info->entry) {
762 CTR0(KTR_NTB, "TX: ring full");
767 if (entry->len > qp->tx_max_frame - sizeof(struct ntb_payload_header)) {
768 if (qp->tx_handler != NULL)
769 qp->tx_handler(qp, qp->cb_data, entry->buf,
772 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
774 "TX: frame too big. returning entry %p to tx_free_q",
778 CTR2(KTR_NTB, "TX: copying entry %p to offset %p", entry, offset);
779 ntb_tx_copy_task(qp, entry, offset);
782 qp->tx_index %= qp->tx_max_entry;
790 ntb_tx_copy_task(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry,
793 struct ntb_payload_header *hdr;
795 CTR2(KTR_NTB, "TX: copying %d bytes to offset %p", entry->len, offset);
796 if (entry->buf != NULL)
797 m_copydata((struct mbuf *)entry->buf, 0, entry->len, offset);
799 hdr = (struct ntb_payload_header *)((char *)offset + qp->tx_max_frame -
800 sizeof(struct ntb_payload_header));
801 hdr->len = entry->len; /* TODO: replace with bus_space_write */
802 hdr->ver = qp->tx_pkts; /* TODO: replace with bus_space_write */
804 /* TODO: replace with bus_space_write */
805 hdr->flags = entry->flags | IF_NTB_DESC_DONE_FLAG;
807 ntb_ring_sdb(qp->ntb, qp->qp_num);
810 * The entry length can only be zero if the packet is intended to be a
811 * "link down" or similar. Since no payload is being sent in these
812 * cases, there is nothing to add to the completion queue.
814 if (entry->len > 0) {
815 qp->tx_bytes += entry->len;
818 qp->tx_handler(qp, qp->cb_data, entry->cb_data,
823 "TX: entry %p sent. hdr->ver = %d, Returning to tx_free_q", entry,
825 ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
829 ntb_qp_full(void *arg)
832 CTR0(KTR_NTB, "TX: qp_full callout");
838 ntb_transport_rxc_db(void *data, int db_num)
840 struct ntb_transport_qp *qp = data;
842 ntb_transport_rx(qp);
846 ntb_rx_pendq_full(void *arg)
849 CTR0(KTR_NTB, "RX: ntb_rx_pendq_full callout");
850 ntb_transport_rx(arg);
854 ntb_transport_rx(struct ntb_transport_qp *qp)
859 * Limit the number of packets processed in a single interrupt to
860 * provide fairness to others
862 mtx_lock(&qp->transport->rx_lock);
863 CTR0(KTR_NTB, "RX: transport_rx");
864 for (i = 0; i < NTB_RX_MAX_PKTS; i++) {
865 rc = ntb_process_rxc(qp);
867 CTR0(KTR_NTB, "RX: process_rxc failed");
871 mtx_unlock(&qp->transport->rx_lock);
875 ntb_process_rxc(struct ntb_transport_qp *qp)
877 struct ntb_payload_header *hdr;
878 struct ntb_queue_entry *entry;
882 ((char *)qp->rx_buff + qp->rx_max_frame * qp->rx_index);
884 ((char *)offset + qp->rx_max_frame -
885 sizeof(struct ntb_payload_header));
887 CTR1(KTR_NTB, "RX: process_rxc rx_index = %u", qp->rx_index);
888 entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q);
891 CTR0(KTR_NTB, "RX: No entries in rx_pend_q");
894 callout_stop(&qp->rx_full);
895 CTR1(KTR_NTB, "RX: rx entry %p from rx_pend_q", entry);
897 if ((hdr->flags & IF_NTB_DESC_DONE_FLAG) == 0) {
899 "RX: hdr not done. Returning entry %p to rx_pend_q", entry);
900 ntb_list_add(&qp->ntb_rx_pend_q_lock, entry, &qp->rx_pend_q);
905 if (hdr->ver != (uint32_t) qp->rx_pkts) {
906 CTR3(KTR_NTB,"RX: ver != rx_pkts (%x != %lx). "
907 "Returning entry %p to rx_pend_q", hdr->ver, qp->rx_pkts,
909 ntb_list_add(&qp->ntb_rx_pend_q_lock, entry, &qp->rx_pend_q);
914 if ((hdr->flags & IF_NTB_LINK_DOWN_FLAG) != 0) {
915 ntb_qp_link_down(qp);
917 "RX: link down. adding entry %p back to rx_pend_q", entry);
918 ntb_list_add(&qp->ntb_rx_pend_q_lock, entry, &qp->rx_pend_q);
922 if (hdr->len <= entry->len) {
923 entry->len = hdr->len;
924 ntb_rx_copy_task(qp, entry, offset);
927 "RX: len too long. Returning entry %p to rx_pend_q", entry);
928 ntb_list_add(&qp->ntb_rx_pend_q_lock, entry, &qp->rx_pend_q);
933 qp->rx_bytes += hdr->len;
935 CTR1(KTR_NTB, "RX: received %ld rx_pkts", qp->rx_pkts);
939 /* Ensure that the data is globally visible before clearing the flag */
942 /* TODO: replace with bus_space_write */
943 qp->rx_info->entry = qp->rx_index;
946 qp->rx_index %= qp->rx_max_entry;
952 ntb_rx_copy_task(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry,
955 struct ifnet *ifp = entry->cb_data;
956 unsigned int len = entry->len;
959 CTR2(KTR_NTB, "RX: copying %d bytes from offset %p", len, offset);
960 m = m_devget(offset, len, 0, ifp, NULL);
961 m->m_pkthdr.csum_flags = CSUM_IP_CHECKED | CSUM_IP_VALID;
963 entry->buf = (void *)m;
966 "RX: copied entry %p to mbuf %p. Adding entry to rx_free_q", entry,
968 ntb_list_add(&qp->ntb_rx_free_q_lock, entry, &qp->rx_free_q);
970 taskqueue_enqueue(taskqueue_swi, &qp->rx_completion_task);
974 ntb_rx_completion_task(void *arg, int pending)
976 struct ntb_transport_qp *qp = arg;
978 struct ntb_queue_entry *entry;
980 CTR0(KTR_NTB, "RX: rx_completion_task");
982 while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q))) {
984 CTR2(KTR_NTB, "RX: completing entry %p, mbuf %p", entry, m);
985 if (qp->rx_handler && qp->client_ready == NTB_LINK_UP)
986 qp->rx_handler(qp, qp->cb_data, m, entry->len);
989 entry->len = qp->transport->bufsize;
991 CTR1(KTR_NTB,"RX: entry %p removed from rx_free_q "
992 "and added to rx_pend_q", entry);
993 ntb_list_add(&qp->ntb_rx_pend_q_lock, entry, &qp->rx_pend_q);
994 if (qp->rx_err_no_buf > qp->last_rx_no_buf) {
995 qp->last_rx_no_buf = qp->rx_err_no_buf;
996 CTR0(KTR_NTB, "RX: could spawn rx task");
997 callout_reset(&qp->rx_full, hz / 1000, ntb_rx_pendq_full,
1003 /* Link Event handler */
1005 ntb_transport_event_callback(void *data, enum ntb_hw_event event)
1007 struct ntb_netdev *nt = data;
1010 case NTB_EVENT_HW_LINK_UP:
1012 device_printf(ntb_get_device(nt->ntb), "HW link up\n");
1013 callout_reset(&nt->link_work, 0, ntb_transport_link_work, nt);
1015 case NTB_EVENT_HW_LINK_DOWN:
1017 device_printf(ntb_get_device(nt->ntb), "HW link down\n");
1018 ntb_transport_link_cleanup(nt);
1021 panic("ntb: Unknown NTB event");
1027 ntb_transport_link_work(void *arg)
1029 struct ntb_netdev *nt = arg;
1030 struct ntb_softc *ntb = nt->ntb;
1031 struct ntb_transport_qp *qp;
1036 /* send the local info, in the opposite order of the way we read it */
1037 for (i = 0; i < NTB_NUM_MW; i++) {
1038 rc = ntb_write_remote_spad(ntb, IF_NTB_MW0_SZ_HIGH + (i * 2),
1039 ntb_get_mw_size(ntb, i) >> 32);
1043 rc = ntb_write_remote_spad(ntb, IF_NTB_MW0_SZ_LOW + (i * 2),
1044 (uint32_t)ntb_get_mw_size(ntb, i));
1049 rc = ntb_write_remote_spad(ntb, IF_NTB_NUM_MWS, NTB_NUM_MW);
1053 rc = ntb_write_remote_spad(ntb, IF_NTB_NUM_QPS, nt->max_qps);
1057 rc = ntb_write_remote_spad(ntb, IF_NTB_VERSION, NTB_TRANSPORT_VERSION);
1061 /* Query the remote side for its info */
1062 rc = ntb_read_local_spad(ntb, IF_NTB_VERSION, &val);
1066 if (val != NTB_TRANSPORT_VERSION)
1069 rc = ntb_read_local_spad(ntb, IF_NTB_NUM_QPS, &val);
1073 if (val != nt->max_qps)
1076 rc = ntb_read_local_spad(ntb, IF_NTB_NUM_MWS, &val);
1080 if (val != NTB_NUM_MW)
1083 for (i = 0; i < NTB_NUM_MW; i++) {
1084 rc = ntb_read_local_spad(ntb, IF_NTB_MW0_SZ_HIGH + (i * 2),
1089 val64 = (uint64_t)val << 32;
1091 rc = ntb_read_local_spad(ntb, IF_NTB_MW0_SZ_LOW + (i * 2),
1098 rc = ntb_set_mw(nt, i, val64);
1103 nt->transport_link = NTB_LINK_UP;
1105 device_printf(ntb_get_device(ntb), "transport link up\n");
1107 for (i = 0; i < nt->max_qps; i++) {
1110 ntb_transport_setup_qp_mw(nt, i);
1112 if (qp->client_ready == NTB_LINK_UP)
1113 callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
1119 for (i = 0; i < NTB_NUM_MW; i++)
1122 if (ntb_query_link_status(ntb))
1123 callout_reset(&nt->link_work,
1124 NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_transport_link_work, nt);
1128 ntb_set_mw(struct ntb_netdev *nt, int num_mw, unsigned int size)
1130 struct ntb_transport_mw *mw = &nt->mw[num_mw];
1132 /* Alloc memory for receiving data. Must be 4k aligned */
1135 mw->virt_addr = contigmalloc(mw->size, M_NTB_IF, M_ZERO, 0,
1136 BUS_SPACE_MAXADDR, mw->size, 0);
1137 if (mw->virt_addr == NULL) {
1138 printf("ntb: Unable to allocate MW buffer of size %d\n",
1142 /* TODO: replace with bus_space_* functions */
1143 mw->dma_addr = vtophys(mw->virt_addr);
1145 /* Notify HW the memory location of the receive buffer */
1146 ntb_set_mw_addr(nt->ntb, num_mw, mw->dma_addr);
1152 ntb_free_mw(struct ntb_netdev *nt, int num_mw)
1154 struct ntb_transport_mw *mw = &nt->mw[num_mw];
1156 if (mw->virt_addr == NULL)
1159 contigfree(mw->virt_addr, mw->size, M_NTB_IF);
1160 mw->virt_addr = NULL;
1164 ntb_transport_setup_qp_mw(struct ntb_netdev *nt, unsigned int qp_num)
1166 struct ntb_transport_qp *qp = &nt->qps[qp_num];
1168 unsigned int rx_size, num_qps_mw;
1169 uint8_t mw_num = QP_TO_MW(qp_num);
1172 if (nt->max_qps % NTB_NUM_MW && mw_num < nt->max_qps % NTB_NUM_MW)
1173 num_qps_mw = nt->max_qps / NTB_NUM_MW + 1;
1175 num_qps_mw = nt->max_qps / NTB_NUM_MW;
1177 rx_size = (unsigned int) nt->mw[mw_num].size / num_qps_mw;
1178 qp->remote_rx_info = (void *)((uint8_t *)nt->mw[mw_num].virt_addr +
1179 (qp_num / NTB_NUM_MW * rx_size));
1180 rx_size -= sizeof(struct ntb_rx_info);
1182 qp->rx_buff = qp->remote_rx_info + 1;
1183 qp->rx_max_frame = min(transport_mtu + sizeof(struct ntb_payload_header),
1185 qp->rx_max_entry = rx_size / qp->rx_max_frame;
1189 qp->remote_rx_info->entry = qp->rx_max_entry;
1191 /* setup the hdr offsets with 0's */
1192 for (i = 0; i < qp->rx_max_entry; i++) {
1193 offset = (void *)((uint8_t *)qp->rx_buff +
1194 qp->rx_max_frame * (i + 1) -
1195 sizeof(struct ntb_payload_header));
1196 memset(offset, 0, sizeof(struct ntb_payload_header));
1204 ntb_qp_link_work(void *arg)
1206 struct ntb_transport_qp *qp = arg;
1207 struct ntb_softc *ntb = qp->ntb;
1208 struct ntb_netdev *nt = qp->transport;
1212 rc = ntb_read_remote_spad(ntb, IF_NTB_QP_LINKS, &val);
1216 rc = ntb_write_remote_spad(ntb, IF_NTB_QP_LINKS, val | 1 << qp->qp_num);
1218 /* query remote spad for qp ready bits */
1219 rc = ntb_read_local_spad(ntb, IF_NTB_QP_LINKS, &val);
1221 /* See if the remote side is up */
1222 if ((1 << qp->qp_num & val) != 0) {
1223 qp->qp_link = NTB_LINK_UP;
1224 if (qp->event_handler != NULL)
1225 qp->event_handler(qp->cb_data, NTB_LINK_UP);
1227 device_printf(ntb_get_device(ntb), "qp link up\n");
1228 } else if (nt->transport_link == NTB_LINK_UP) {
1229 callout_reset(&qp->link_work,
1230 NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_qp_link_work, qp);
1234 /* Link down event*/
1236 ntb_transport_link_cleanup(struct ntb_netdev *nt)
1240 if (nt->transport_link == NTB_LINK_DOWN)
1241 callout_drain(&nt->link_work);
1243 nt->transport_link = NTB_LINK_DOWN;
1245 /* Pass along the info to any clients */
1246 for (i = 0; i < nt->max_qps; i++)
1247 if (!test_bit(i, &nt->qp_bitmap))
1248 ntb_qp_link_down(&nt->qps[i]);
1251 * The scratchpad registers keep the values if the remote side
1252 * goes down, blast them now to give them a sane value the next
1253 * time they are accessed
1255 for (i = 0; i < IF_NTB_MAX_SPAD; i++)
1256 ntb_write_local_spad(nt->ntb, i, 0);
1261 ntb_qp_link_down(struct ntb_transport_qp *qp)
1264 ntb_qp_link_cleanup(qp);
1268 ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
1270 struct ntb_netdev *nt = qp->transport;
1272 if (qp->qp_link == NTB_LINK_DOWN) {
1273 callout_drain(&qp->link_work);
1277 if (qp->event_handler != NULL)
1278 qp->event_handler(qp->cb_data, NTB_LINK_DOWN);
1280 qp->qp_link = NTB_LINK_DOWN;
1282 if (nt->transport_link == NTB_LINK_UP)
1283 callout_reset(&qp->link_work,
1284 NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_qp_link_work, qp);
1287 /* Link commanded down */
1289 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1290 * @qp: NTB transport layer queue to be disabled
1292 * Notify NTB transport layer of client's desire to no longer receive data on
1293 * transport queue specified. It is the client's responsibility to ensure all
1294 * entries on queue are purged or otherwise handled appropraitely.
1297 ntb_transport_link_down(struct ntb_transport_qp *qp)
1304 qp->client_ready = NTB_LINK_DOWN;
1306 rc = ntb_read_remote_spad(qp->ntb, IF_NTB_QP_LINKS, &val);
1310 rc = ntb_write_remote_spad(qp->ntb, IF_NTB_QP_LINKS,
1311 val & ~(1 << qp->qp_num));
1313 if (qp->qp_link == NTB_LINK_UP)
1314 ntb_send_link_down(qp);
1316 callout_drain(&qp->link_work);
1321 ntb_send_link_down(struct ntb_transport_qp *qp)
1323 struct ntb_queue_entry *entry;
1326 if (qp->qp_link == NTB_LINK_DOWN)
1329 qp->qp_link = NTB_LINK_DOWN;
1331 for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1332 entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1335 pause("NTB Wait for link down", hz / 10);
1341 entry->cb_data = NULL;
1344 entry->flags = IF_NTB_LINK_DOWN_FLAG;
1346 mtx_lock(&qp->transport->tx_lock);
1347 rc = ntb_process_tx(qp, entry);
1349 printf("ntb: Failed to send link down\n");
1350 mtx_unlock(&qp->transport->tx_lock);
1354 /* List Management */
1357 ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry,
1358 struct ntb_queue_list *list)
1361 mtx_lock_spin(lock);
1362 STAILQ_INSERT_TAIL(list, entry, entry);
1363 mtx_unlock_spin(lock);
1366 static struct ntb_queue_entry *
1367 ntb_list_rm(struct mtx *lock, struct ntb_queue_list *list)
1369 struct ntb_queue_entry *entry;
1371 mtx_lock_spin(lock);
1372 if (STAILQ_EMPTY(list)) {
1376 entry = STAILQ_FIRST(list);
1377 STAILQ_REMOVE_HEAD(list, entry);
1379 mtx_unlock_spin(lock);
1384 /* Helper functions */
1385 /* TODO: This too should really be part of the kernel */
1386 #define EUI48_MULTICAST 1 << 0
1387 #define EUI48_LOCALLY_ADMINISTERED 1 << 1
1389 create_random_local_eui48(u_char *eaddr)
1391 static uint8_t counter = 0;
1392 uint32_t seed = ticks;
1394 eaddr[0] = EUI48_LOCALLY_ADMINISTERED;
1395 memcpy(&eaddr[1], &seed, sizeof(uint32_t));
1396 eaddr[5] = counter++;
1400 * ntb_transport_max_size - Query the max payload size of a qp
1401 * @qp: NTB transport layer queue to be queried
1403 * Query the maximum payload size permissible on the given qp
1405 * RETURNS: the max payload size of a qp
1408 ntb_transport_max_size(struct ntb_transport_qp *qp)
1414 return (qp->tx_max_frame - sizeof(struct ntb_payload_header));