ntb_transport.c revision 289154
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: head/sys/dev/ntb/if_ntb/if_ntb.c 289154 2015-10-11 20:59:02Z cem $");
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/taskqueue.h>
43#include <net/if.h>
44#include <net/if_media.h>
45#include <net/if_types.h>
46#include <net/if_var.h>
47#include <net/bpf.h>
48#include <net/ethernet.h>
49#include <vm/vm.h>
50#include <vm/pmap.h>
51#include <machine/bus.h>
52#include <machine/cpufunc.h>
53#include <machine/pmap.h>
54
55#include "../ntb_hw/ntb_hw.h"
56
57/*
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.
60 *
61 * This module contains a protocol for sending and receiving messages, and
62 * exposes that protocol through a simulated ethernet device called ntb.
63 *
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.
66 */
67
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))
72
73#define KTR_NTB KTR_SPARE3
74
75#define NTB_TRANSPORT_VERSION	3
76#define NTB_RX_MAX_PKTS		64
77#define	NTB_RXQ_SIZE		300
78
79static unsigned int transport_mtu = 0x4000 + ETHER_HDR_LEN + ETHER_CRC_LEN;
80static unsigned int max_num_clients = 1;
81
82STAILQ_HEAD(ntb_queue_list, ntb_queue_entry);
83
84struct ntb_queue_entry {
85	/* ntb_queue list reference */
86	STAILQ_ENTRY(ntb_queue_entry) entry;
87
88	/* info on data to be transfered */
89	void		*cb_data;
90	void		*buf;
91	uint64_t	len;
92	uint64_t	flags;
93};
94
95struct ntb_rx_info {
96	unsigned int entry;
97};
98
99struct ntb_transport_qp {
100	struct ntb_netdev	*transport;
101	struct ntb_softc	*ntb;
102
103	void			*cb_data;
104
105	bool			client_ready;
106	bool			qp_link;
107	uint8_t			qp_num;	/* Only 64 QPs are allowed.  0-63 */
108
109	struct ntb_rx_info	*rx_info;
110	struct ntb_rx_info	*remote_rx_info;
111
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;
116	void			*tx_mw;
117	uint64_t		tx_index;
118	uint64_t		tx_max_entry;
119	uint64_t		tx_max_frame;
120
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;
128	void			*rx_buff;
129	uint64_t		rx_index;
130	uint64_t		rx_max_entry;
131	uint64_t		rx_max_frame;
132
133	void (*event_handler) (void *data, int status);
134	struct callout		link_work;
135	struct callout		queue_full;
136	struct callout		rx_full;
137
138	uint64_t		last_rx_no_buf;
139
140	/* Stats */
141	uint64_t		rx_bytes;
142	uint64_t		rx_pkts;
143	uint64_t		rx_ring_empty;
144	uint64_t		rx_err_no_buf;
145	uint64_t		rx_err_oflow;
146	uint64_t		rx_err_ver;
147	uint64_t		tx_bytes;
148	uint64_t		tx_pkts;
149	uint64_t		tx_ring_full;
150};
151
152struct 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);
158};
159
160
161struct ntb_transport_mw {
162	size_t		size;
163	void		*virt_addr;
164	vm_paddr_t	dma_addr;
165};
166
167struct ntb_netdev {
168	struct ntb_softc	*ntb;
169	struct ifnet		*ifp;
170	struct ntb_transport_mw	mw[NTB_NUM_MW];
171	struct ntb_transport_qp	*qps;
172	uint64_t		max_qps;
173	uint64_t		qp_bitmap;
174	bool			transport_link;
175	struct callout		link_work;
176	struct ntb_transport_qp *qp;
177	uint64_t		bufsize;
178	u_char			eaddr[ETHER_ADDR_LEN];
179	struct mtx		tx_lock;
180	struct mtx		rx_lock;
181};
182
183static struct ntb_netdev net_softc;
184
185enum {
186	IF_NTB_DESC_DONE_FLAG = 1 << 0,
187	IF_NTB_LINK_DOWN_FLAG = 1 << 1,
188};
189
190struct ntb_payload_header {
191	uint64_t ver;
192	uint64_t len;
193	uint64_t flags;
194};
195
196enum {
197	/*
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.
201	 */
202	IF_NTB_VERSION = 0,
203	IF_NTB_QP_LINKS,
204	IF_NTB_NUM_QPS,
205	IF_NTB_NUM_MWS,
206	/*
207	 * N.B.: transport_link_work assumes MW1 enums = MW0 + 2.
208	 */
209	IF_NTB_MW0_SZ_HIGH,
210	IF_NTB_MW0_SZ_LOW,
211	IF_NTB_MW1_SZ_HIGH,
212	IF_NTB_MW1_SZ_LOW,
213	IF_NTB_MAX_SPAD,
214};
215
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
219
220static int ntb_handle_module_events(struct module *m, int what, void *arg);
221static int ntb_setup_interface(void);
222static int ntb_teardown_interface(void);
223static void ntb_net_init(void *arg);
224static int ntb_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
225static void ntb_start(struct ifnet *ifp);
226static void ntb_net_tx_handler(struct ntb_transport_qp *qp, void *qp_data,
227    void *data, int len);
228static void ntb_net_rx_handler(struct ntb_transport_qp *qp, void *qp_data,
229    void *data, int len);
230static void ntb_net_event_handler(void *data, int status);
231static int ntb_transport_init(struct ntb_softc *ntb);
232static void ntb_transport_free(void *transport);
233static void ntb_transport_init_queue(struct ntb_netdev *nt,
234    unsigned int qp_num);
235static void ntb_transport_free_queue(struct ntb_transport_qp *qp);
236static struct ntb_transport_qp * ntb_transport_create_queue(void *data,
237    struct ntb_softc *pdev, const struct ntb_queue_handlers *handlers);
238static void ntb_transport_link_up(struct ntb_transport_qp *qp);
239static int ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb,
240    void *data, unsigned int len);
241static int ntb_process_tx(struct ntb_transport_qp *qp,
242    struct ntb_queue_entry *entry);
243static void ntb_tx_copy_task(struct ntb_transport_qp *qp,
244    struct ntb_queue_entry *entry, void *offset);
245static void ntb_qp_full(void *arg);
246static void ntb_transport_rxc_db(void *data, int db_num);
247static void ntb_rx_pendq_full(void *arg);
248static void ntb_transport_rx(struct ntb_transport_qp *qp);
249static int ntb_process_rxc(struct ntb_transport_qp *qp);
250static void ntb_rx_copy_task(struct ntb_transport_qp *qp,
251    struct ntb_queue_entry *entry, void *offset);
252static void ntb_rx_completion_task(void *arg, int pending);
253static void ntb_transport_event_callback(void *data, enum ntb_hw_event event);
254static void ntb_transport_link_work(void *arg);
255static int ntb_set_mw(struct ntb_netdev *nt, int num_mw, unsigned int size);
256static void ntb_free_mw(struct ntb_netdev *nt, int num_mw);
257static void ntb_transport_setup_qp_mw(struct ntb_netdev *nt,
258    unsigned int qp_num);
259static void ntb_qp_link_work(void *arg);
260static void ntb_transport_link_cleanup(struct ntb_netdev *nt);
261static void ntb_qp_link_down(struct ntb_transport_qp *qp);
262static void ntb_qp_link_cleanup(struct ntb_transport_qp *qp);
263static void ntb_transport_link_down(struct ntb_transport_qp *qp);
264static void ntb_send_link_down(struct ntb_transport_qp *qp);
265static void ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry,
266    struct ntb_queue_list *list);
267static struct ntb_queue_entry *ntb_list_rm(struct mtx *lock,
268    struct ntb_queue_list *list);
269static void create_random_local_eui48(u_char *eaddr);
270static unsigned int ntb_transport_max_size(struct ntb_transport_qp *qp);
271
272MALLOC_DEFINE(M_NTB_IF, "if_ntb", "ntb network driver");
273
274/* Module setup and teardown */
275static int
276ntb_handle_module_events(struct module *m, int what, void *arg)
277{
278	int err = 0;
279
280	switch (what) {
281	case MOD_LOAD:
282		err = ntb_setup_interface();
283		break;
284	case MOD_UNLOAD:
285		err = ntb_teardown_interface();
286		break;
287	default:
288		err = EOPNOTSUPP;
289		break;
290	}
291	return (err);
292}
293
294static moduledata_t if_ntb_mod = {
295	"if_ntb",
296	ntb_handle_module_events,
297	NULL
298};
299
300DECLARE_MODULE(if_ntb, if_ntb_mod, SI_SUB_KLD, SI_ORDER_ANY);
301MODULE_DEPEND(if_ntb, ntb_hw, 1, 1, 1);
302
303static int
304ntb_setup_interface()
305{
306	struct ifnet *ifp;
307	struct ntb_queue_handlers handlers = { ntb_net_rx_handler,
308	    ntb_net_tx_handler, ntb_net_event_handler };
309
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");
313		return (ENXIO);
314	}
315
316	ntb_transport_init(net_softc.ntb);
317
318	ifp = net_softc.ifp = if_alloc(IFT_ETHER);
319	if (ifp == NULL) {
320		printf("ntb: cannot allocate ifnet structure\n");
321		return (ENOMEM);
322	}
323
324	net_softc.qp = ntb_transport_create_queue(ifp, net_softc.ntb,
325	    &handlers);
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;
339
340	ntb_transport_link_up(net_softc.qp);
341	net_softc.bufsize = ntb_transport_max_size(net_softc.qp) +
342	    sizeof(struct ether_header);
343	return (0);
344}
345
346static int
347ntb_teardown_interface()
348{
349
350	if (net_softc.qp != NULL)
351		ntb_transport_link_down(net_softc.qp);
352
353	if (net_softc.ifp != NULL) {
354		ether_ifdetach(net_softc.ifp);
355		if_free(net_softc.ifp);
356	}
357
358	if (net_softc.qp != NULL) {
359		ntb_transport_free_queue(net_softc.qp);
360		ntb_transport_free(&net_softc);
361	}
362
363	return (0);
364}
365
366/* Network device interface */
367
368static void
369ntb_net_init(void *arg)
370{
371	struct ntb_netdev *ntb_softc = arg;
372	struct ifnet *ifp = ntb_softc->ifp;
373
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);
378}
379
380static int
381ntb_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
382{
383	struct ntb_netdev *nt = ifp->if_softc;
384	struct ifreq *ifr = (struct ifreq *)data;
385	int error = 0;
386
387	switch (command) {
388	case SIOCSIFMTU:
389	    {
390		if (ifr->ifr_mtu > ntb_transport_max_size(nt->qp) -
391		    ETHER_HDR_LEN - ETHER_CRC_LEN) {
392			error = EINVAL;
393			break;
394		}
395
396		ifp->if_mtu = ifr->ifr_mtu;
397		break;
398	    }
399	default:
400		error = ether_ioctl(ifp, command, data);
401		break;
402	}
403
404	return (error);
405}
406
407
408static void
409ntb_start(struct ifnet *ifp)
410{
411	struct mbuf *m_head;
412	struct ntb_netdev *nt = ifp->if_softc;
413	int rc;
414
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));
423		if (rc != 0) {
424			CTR1(KTR_NTB,
425			    "TX: could not tx mbuf %p. Returning to snd q",
426			    m_head);
427			if (rc == EAGAIN) {
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,
431				    ntb_qp_full, ifp);
432			}
433			break;
434		}
435
436	}
437	mtx_unlock(&nt->tx_lock);
438}
439
440/* Network Device Callbacks */
441static void
442ntb_net_tx_handler(struct ntb_transport_qp *qp, void *qp_data, void *data,
443    int len)
444{
445
446	m_freem(data);
447	CTR1(KTR_NTB, "TX: tx_handler freeing mbuf %p", data);
448}
449
450static void
451ntb_net_rx_handler(struct ntb_transport_qp *qp, void *qp_data, void *data,
452    int len)
453{
454	struct mbuf *m = data;
455	struct ifnet *ifp = qp_data;
456
457	CTR0(KTR_NTB, "RX: rx handler");
458	(*ifp->if_input)(ifp, m);
459}
460
461static void
462ntb_net_event_handler(void *data, int status)
463{
464
465}
466
467/* Transport Init and teardown */
468
469static int
470ntb_transport_init(struct ntb_softc *ntb)
471{
472	struct ntb_netdev *nt = &net_softc;
473	int rc, i;
474
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);
479
480	nt->qps = malloc(nt->max_qps * sizeof(struct ntb_transport_qp),
481			  M_NTB_IF, M_WAITOK|M_ZERO);
482
483	nt->qp_bitmap = ((uint64_t) 1 << nt->max_qps) - 1;
484
485	for (i = 0; i < nt->max_qps; i++)
486		ntb_transport_init_queue(nt, i);
487
488	callout_init(&nt->link_work, 0);
489
490	rc = ntb_register_event_callback(ntb,
491					 ntb_transport_event_callback);
492	if (rc != 0)
493		goto err;
494
495	if (ntb_query_link_status(ntb)) {
496		if (bootverbose)
497			device_printf(ntb_get_device(ntb), "link up\n");
498		callout_reset(&nt->link_work, 0, ntb_transport_link_work, nt);
499	}
500
501	return (0);
502
503err:
504	free(nt->qps, M_NTB_IF);
505	ntb_unregister_transport(ntb);
506	return (rc);
507}
508
509static void
510ntb_transport_free(void *transport)
511{
512	struct ntb_netdev *nt = transport;
513	struct ntb_softc *ntb = nt->ntb;
514	int i;
515
516	nt->transport_link = NTB_LINK_DOWN;
517
518	callout_drain(&nt->link_work);
519
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]);
524
525
526	ntb_unregister_event_callback(ntb);
527
528	for (i = 0; i < NTB_NUM_MW; i++)
529		ntb_free_mw(nt, i);
530
531	free(nt->qps, M_NTB_IF);
532	ntb_unregister_transport(ntb);
533}
534
535static void
536ntb_transport_init_queue(struct ntb_netdev *nt, unsigned int qp_num)
537{
538	struct ntb_transport_qp *qp;
539	unsigned int num_qps_mw, tx_size;
540	uint8_t mw_num = QP_TO_MW(qp_num);
541
542	qp = &nt->qps[qp_num];
543	qp->qp_num = qp_num;
544	qp->transport = nt;
545	qp->ntb = nt->ntb;
546	qp->qp_link = NTB_LINK_DOWN;
547	qp->client_ready = NTB_LINK_DOWN;
548	qp->event_handler = NULL;
549
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;
552	else
553		num_qps_mw = nt->max_qps / NTB_NUM_MW;
554
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);
560
561	qp->tx_mw = qp->rx_info + 1;
562	qp->tx_max_frame = min(transport_mtu + sizeof(struct ntb_payload_header),
563	    tx_size);
564	qp->tx_max_entry = tx_size / qp->tx_max_frame;
565	qp->tx_index = 0;
566
567	callout_init(&qp->link_work, 0);
568	callout_init(&qp->queue_full, 1);
569	callout_init(&qp->rx_full, 1);
570
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);
575
576	STAILQ_INIT(&qp->rx_pend_q);
577	STAILQ_INIT(&qp->rx_free_q);
578	STAILQ_INIT(&qp->tx_free_q);
579}
580
581static void
582ntb_transport_free_queue(struct ntb_transport_qp *qp)
583{
584	struct ntb_queue_entry *entry;
585
586	if (qp == NULL)
587		return;
588
589	callout_drain(&qp->link_work);
590
591	ntb_unregister_db_callback(qp->ntb, qp->qp_num);
592
593	while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q)))
594		free(entry, M_NTB_IF);
595
596	while ((entry = ntb_list_rm(&qp->ntb_rx_pend_q_lock, &qp->rx_pend_q)))
597		free(entry, M_NTB_IF);
598
599	while ((entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q)))
600		free(entry, M_NTB_IF);
601
602	set_bit(qp->qp_num, &qp->transport->qp_bitmap);
603}
604
605/**
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
610 *
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.
616 *
617 * RETURNS: pointer to newly created ntb_queue, NULL on error.
618 */
619static struct ntb_transport_qp *
620ntb_transport_create_queue(void *data, struct ntb_softc *pdev,
621    const struct ntb_queue_handlers *handlers)
622{
623	struct ntb_queue_entry *entry;
624	struct ntb_transport_qp *qp;
625	struct ntb_netdev *nt;
626	unsigned int free_queue;
627	int rc, i;
628
629	nt = ntb_find_transport(pdev);
630	if (nt == NULL)
631		goto err;
632
633	free_queue = ffs(nt->qp_bitmap);
634	if (free_queue == 0)
635		goto err;
636
637	/* decrement free_queue to make it zero based */
638	free_queue--;
639
640	clear_bit(free_queue, &nt->qp_bitmap);
641
642	qp = &nt->qps[free_queue];
643	qp->cb_data = data;
644	qp->rx_handler = handlers->rx_handler;
645	qp->tx_handler = handlers->tx_handler;
646	qp->event_handler = handlers->event_handler;
647
648	for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
649		entry = malloc(sizeof(struct ntb_queue_entry), M_NTB_IF,
650		    M_WAITOK|M_ZERO);
651		entry->cb_data = nt->ifp;
652		entry->buf = NULL;
653		entry->len = transport_mtu;
654		ntb_list_add(&qp->ntb_rx_pend_q_lock, entry, &qp->rx_pend_q);
655	}
656
657	for (i = 0; i < NTB_QP_DEF_NUM_ENTRIES; i++) {
658		entry = malloc(sizeof(struct ntb_queue_entry), M_NTB_IF,
659		    M_WAITOK|M_ZERO);
660		ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
661	}
662
663	rc = ntb_register_db_callback(qp->ntb, free_queue, qp,
664				      ntb_transport_rxc_db);
665	if (rc != 0)
666		goto err1;
667
668	return (qp);
669
670err1:
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);
676err:
677	return (NULL);
678}
679
680/**
681 * ntb_transport_link_up - Notify NTB transport of client readiness to use queue
682 * @qp: NTB transport layer queue to be enabled
683 *
684 * Notify NTB transport layer of client readiness to use queue
685 */
686static void
687ntb_transport_link_up(struct ntb_transport_qp *qp)
688{
689
690	if (qp == NULL)
691		return;
692
693	qp->client_ready = NTB_LINK_UP;
694	if (bootverbose)
695		device_printf(ntb_get_device(qp->ntb), "qp client ready\n");
696
697	if (qp->transport->transport_link == NTB_LINK_UP)
698		callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
699}
700
701
702
703/* Transport Tx */
704
705/**
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
711 *
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.
715 *
716 * RETURNS: An appropriate ERRNO error value on error, or zero for success.
717 */
718static int
719ntb_transport_tx_enqueue(struct ntb_transport_qp *qp, void *cb, void *data,
720    unsigned int len)
721{
722	struct ntb_queue_entry *entry;
723	int rc;
724
725	if (qp == NULL || qp->qp_link != NTB_LINK_UP || len == 0) {
726		CTR0(KTR_NTB, "TX: link not up");
727		return (EINVAL);
728	}
729
730	entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
731	if (entry == NULL) {
732		CTR0(KTR_NTB, "TX: could not get entry from tx_free_q");
733		return (ENOMEM);
734	}
735	CTR1(KTR_NTB, "TX: got entry %p from tx_free_q", entry);
736
737	entry->cb_data = cb;
738	entry->buf = data;
739	entry->len = len;
740	entry->flags = 0;
741
742	rc = ntb_process_tx(qp, entry);
743	if (rc != 0) {
744		ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
745		CTR1(KTR_NTB,
746		    "TX: process_tx failed. Returning entry %p to tx_free_q",
747		    entry);
748	}
749	return (rc);
750}
751
752static int
753ntb_process_tx(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry)
754{
755	void *offset;
756
757	offset = (char *)qp->tx_mw + qp->tx_max_frame * qp->tx_index;
758	CTR3(KTR_NTB,
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");
763		qp->tx_ring_full++;
764		return (EAGAIN);
765	}
766
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,
770				       EIO);
771
772		ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
773		CTR1(KTR_NTB,
774		    "TX: frame too big. returning entry %p to tx_free_q",
775		    entry);
776		return (0);
777	}
778	CTR2(KTR_NTB, "TX: copying entry %p to offset %p", entry, offset);
779	ntb_tx_copy_task(qp, entry, offset);
780
781	qp->tx_index++;
782	qp->tx_index %= qp->tx_max_entry;
783
784	qp->tx_pkts++;
785
786	return (0);
787}
788
789static void
790ntb_tx_copy_task(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry,
791    void *offset)
792{
793	struct ntb_payload_header *hdr;
794
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);
798
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 */
803	wmb();
804	/* TODO: replace with bus_space_write */
805	hdr->flags = entry->flags | IF_NTB_DESC_DONE_FLAG;
806
807	ntb_ring_sdb(qp->ntb, qp->qp_num);
808
809	/*
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.
813	 */
814	if (entry->len > 0) {
815		qp->tx_bytes += entry->len;
816
817		if (qp->tx_handler)
818			qp->tx_handler(qp, qp->cb_data, entry->cb_data,
819				       entry->len);
820	}
821
822	CTR2(KTR_NTB,
823	    "TX: entry %p sent. hdr->ver = %d, Returning to tx_free_q", entry,
824	    hdr->ver);
825	ntb_list_add(&qp->ntb_tx_free_q_lock, entry, &qp->tx_free_q);
826}
827
828static void
829ntb_qp_full(void *arg)
830{
831
832	CTR0(KTR_NTB, "TX: qp_full callout");
833	ntb_start(arg);
834}
835
836/* Transport Rx */
837static void
838ntb_transport_rxc_db(void *data, int db_num)
839{
840	struct ntb_transport_qp *qp = data;
841
842	ntb_transport_rx(qp);
843}
844
845static void
846ntb_rx_pendq_full(void *arg)
847{
848
849	CTR0(KTR_NTB, "RX: ntb_rx_pendq_full callout");
850	ntb_transport_rx(arg);
851}
852
853static void
854ntb_transport_rx(struct ntb_transport_qp *qp)
855{
856	int rc, i;
857
858	/*
859	 * Limit the number of packets processed in a single interrupt to
860	 * provide fairness to others
861	 */
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);
866		if (rc != 0) {
867			CTR0(KTR_NTB, "RX: process_rxc failed");
868			break;
869		}
870	}
871	mtx_unlock(&qp->transport->rx_lock);
872}
873
874static int
875ntb_process_rxc(struct ntb_transport_qp *qp)
876{
877	struct ntb_payload_header *hdr;
878	struct ntb_queue_entry *entry;
879	void *offset;
880
881	offset = (void *)
882	    ((char *)qp->rx_buff + qp->rx_max_frame * qp->rx_index);
883	hdr = (void *)
884	    ((char *)offset + qp->rx_max_frame -
885		sizeof(struct ntb_payload_header));
886
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);
889	if (entry == NULL) {
890		qp->rx_err_no_buf++;
891		CTR0(KTR_NTB, "RX: No entries in rx_pend_q");
892		return (ENOMEM);
893	}
894	callout_stop(&qp->rx_full);
895	CTR1(KTR_NTB, "RX: rx entry %p from rx_pend_q", entry);
896
897	if ((hdr->flags & IF_NTB_DESC_DONE_FLAG) == 0) {
898		CTR1(KTR_NTB,
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);
901		qp->rx_ring_empty++;
902		return (EAGAIN);
903	}
904
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,
908		    entry);
909		ntb_list_add(&qp->ntb_rx_pend_q_lock, entry, &qp->rx_pend_q);
910		qp->rx_err_ver++;
911		return (EIO);
912	}
913
914	if ((hdr->flags & IF_NTB_LINK_DOWN_FLAG) != 0) {
915		ntb_qp_link_down(qp);
916		CTR1(KTR_NTB,
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);
919		goto out;
920	}
921
922	if (hdr->len <= entry->len) {
923		entry->len = hdr->len;
924		ntb_rx_copy_task(qp, entry, offset);
925	} else {
926		CTR1(KTR_NTB,
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);
929
930		qp->rx_err_oflow++;
931	}
932
933	qp->rx_bytes += hdr->len;
934	qp->rx_pkts++;
935	CTR1(KTR_NTB, "RX: received %ld rx_pkts", qp->rx_pkts);
936
937
938out:
939	/* Ensure that the data is globally visible before clearing the flag */
940	wmb();
941	hdr->flags = 0;
942	/* TODO: replace with bus_space_write */
943	qp->rx_info->entry = qp->rx_index;
944
945	qp->rx_index++;
946	qp->rx_index %= qp->rx_max_entry;
947
948	return (0);
949}
950
951static void
952ntb_rx_copy_task(struct ntb_transport_qp *qp, struct ntb_queue_entry *entry,
953    void *offset)
954{
955	struct ifnet *ifp = entry->cb_data;
956	unsigned int len = entry->len;
957	struct mbuf *m;
958
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;
962
963	entry->buf = (void *)m;
964
965	CTR2(KTR_NTB,
966	    "RX: copied entry %p to mbuf %p. Adding entry to rx_free_q", entry,
967	    m);
968	ntb_list_add(&qp->ntb_rx_free_q_lock, entry, &qp->rx_free_q);
969
970	taskqueue_enqueue(taskqueue_swi, &qp->rx_completion_task);
971}
972
973static void
974ntb_rx_completion_task(void *arg, int pending)
975{
976	struct ntb_transport_qp *qp = arg;
977	struct mbuf *m;
978	struct ntb_queue_entry *entry;
979
980	CTR0(KTR_NTB, "RX: rx_completion_task");
981
982	while ((entry = ntb_list_rm(&qp->ntb_rx_free_q_lock, &qp->rx_free_q))) {
983		m = entry->buf;
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);
987
988		entry->buf = NULL;
989		entry->len = qp->transport->bufsize;
990
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,
998			    qp);
999		}
1000	}
1001}
1002
1003/* Link Event handler */
1004static void
1005ntb_transport_event_callback(void *data, enum ntb_hw_event event)
1006{
1007	struct ntb_netdev *nt = data;
1008
1009	switch (event) {
1010	case NTB_EVENT_HW_LINK_UP:
1011		if (bootverbose)
1012			device_printf(ntb_get_device(nt->ntb), "HW link up\n");
1013		callout_reset(&nt->link_work, 0, ntb_transport_link_work, nt);
1014		break;
1015	case NTB_EVENT_HW_LINK_DOWN:
1016		if (bootverbose)
1017			device_printf(ntb_get_device(nt->ntb), "HW link down\n");
1018		ntb_transport_link_cleanup(nt);
1019		break;
1020	default:
1021		panic("ntb: Unknown NTB event");
1022	}
1023}
1024
1025/* Link bring up */
1026static void
1027ntb_transport_link_work(void *arg)
1028{
1029	struct ntb_netdev *nt = arg;
1030	struct ntb_softc *ntb = nt->ntb;
1031	struct ntb_transport_qp *qp;
1032	uint64_t val64;
1033	uint32_t val;
1034	int rc, i;
1035
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);
1040		if (rc != 0)
1041			goto out;
1042
1043		rc = ntb_write_remote_spad(ntb, IF_NTB_MW0_SZ_LOW + (i * 2),
1044		    (uint32_t)ntb_get_mw_size(ntb, i));
1045		if (rc != 0)
1046			goto out;
1047	}
1048
1049	rc = ntb_write_remote_spad(ntb, IF_NTB_NUM_MWS, NTB_NUM_MW);
1050	if (rc != 0)
1051		goto out;
1052
1053	rc = ntb_write_remote_spad(ntb, IF_NTB_NUM_QPS, nt->max_qps);
1054	if (rc != 0)
1055		goto out;
1056
1057	rc = ntb_write_remote_spad(ntb, IF_NTB_VERSION, NTB_TRANSPORT_VERSION);
1058	if (rc != 0)
1059		goto out;
1060
1061	/* Query the remote side for its info */
1062	rc = ntb_read_local_spad(ntb, IF_NTB_VERSION, &val);
1063	if (rc != 0)
1064		goto out;
1065
1066	if (val != NTB_TRANSPORT_VERSION)
1067		goto out;
1068
1069	rc = ntb_read_local_spad(ntb, IF_NTB_NUM_QPS, &val);
1070	if (rc != 0)
1071		goto out;
1072
1073	if (val != nt->max_qps)
1074		goto out;
1075
1076	rc = ntb_read_local_spad(ntb, IF_NTB_NUM_MWS, &val);
1077	if (rc != 0)
1078		goto out;
1079
1080	if (val != NTB_NUM_MW)
1081		goto out;
1082
1083	for (i = 0; i < NTB_NUM_MW; i++) {
1084		rc = ntb_read_local_spad(ntb, IF_NTB_MW0_SZ_HIGH + (i * 2),
1085		    &val);
1086		if (rc != 0)
1087			goto free_mws;
1088
1089		val64 = (uint64_t)val << 32;
1090
1091		rc = ntb_read_local_spad(ntb, IF_NTB_MW0_SZ_LOW + (i * 2),
1092		    &val);
1093		if (rc != 0)
1094			goto free_mws;
1095
1096		val64 |= val;
1097
1098		rc = ntb_set_mw(nt, i, val64);
1099		if (rc != 0)
1100			goto free_mws;
1101	}
1102
1103	nt->transport_link = NTB_LINK_UP;
1104	if (bootverbose)
1105		device_printf(ntb_get_device(ntb), "transport link up\n");
1106
1107	for (i = 0; i < nt->max_qps; i++) {
1108		qp = &nt->qps[i];
1109
1110		ntb_transport_setup_qp_mw(nt, i);
1111
1112		if (qp->client_ready == NTB_LINK_UP)
1113			callout_reset(&qp->link_work, 0, ntb_qp_link_work, qp);
1114	}
1115
1116	return;
1117
1118free_mws:
1119	for (i = 0; i < NTB_NUM_MW; i++)
1120		ntb_free_mw(nt, i);
1121out:
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);
1125}
1126
1127static int
1128ntb_set_mw(struct ntb_netdev *nt, int num_mw, unsigned int size)
1129{
1130	struct ntb_transport_mw *mw = &nt->mw[num_mw];
1131
1132	/* No need to re-setup */
1133	if (mw->size == size)
1134		return (0);
1135
1136	if (mw->size != 0)
1137		ntb_free_mw(nt, num_mw);
1138
1139	/* Alloc memory for receiving data.  Must be 4k aligned */
1140	mw->size = size;
1141
1142	mw->virt_addr = contigmalloc(mw->size, M_NTB_IF, M_ZERO, 0,
1143	    BUS_SPACE_MAXADDR, mw->size, 0);
1144	if (mw->virt_addr == NULL) {
1145		mw->size = 0;
1146		printf("ntb: Unable to allocate MW buffer of size %d\n",
1147		    (int)mw->size);
1148		return (ENOMEM);
1149	}
1150	/* TODO: replace with bus_space_* functions */
1151	mw->dma_addr = vtophys(mw->virt_addr);
1152
1153	/* Notify HW the memory location of the receive buffer */
1154	ntb_set_mw_addr(nt->ntb, num_mw, mw->dma_addr);
1155
1156	return (0);
1157}
1158
1159static void
1160ntb_free_mw(struct ntb_netdev *nt, int num_mw)
1161{
1162	struct ntb_transport_mw *mw = &nt->mw[num_mw];
1163
1164	if (mw->virt_addr == NULL)
1165		return;
1166
1167	contigfree(mw->virt_addr, mw->size, M_NTB_IF);
1168	mw->virt_addr = NULL;
1169}
1170
1171static void
1172ntb_transport_setup_qp_mw(struct ntb_netdev *nt, unsigned int qp_num)
1173{
1174	struct ntb_transport_qp *qp = &nt->qps[qp_num];
1175	void *offset;
1176	unsigned int rx_size, num_qps_mw;
1177	uint8_t mw_num = QP_TO_MW(qp_num);
1178	unsigned int i;
1179
1180	if (nt->max_qps % NTB_NUM_MW && mw_num < nt->max_qps % NTB_NUM_MW)
1181		num_qps_mw = nt->max_qps / NTB_NUM_MW + 1;
1182	else
1183		num_qps_mw = nt->max_qps / NTB_NUM_MW;
1184
1185	rx_size = (unsigned int) nt->mw[mw_num].size / num_qps_mw;
1186	qp->remote_rx_info = (void *)((uint8_t *)nt->mw[mw_num].virt_addr +
1187			     (qp_num / NTB_NUM_MW * rx_size));
1188	rx_size -= sizeof(struct ntb_rx_info);
1189
1190	qp->rx_buff = qp->remote_rx_info + 1;
1191	qp->rx_max_frame = min(transport_mtu + sizeof(struct ntb_payload_header),
1192	    rx_size);
1193	qp->rx_max_entry = rx_size / qp->rx_max_frame;
1194	qp->rx_index = 0;
1195	qp->tx_index = 0;
1196
1197	qp->remote_rx_info->entry = qp->rx_max_entry;
1198
1199	/* setup the hdr offsets with 0's */
1200	for (i = 0; i < qp->rx_max_entry; i++) {
1201		offset = (void *)((uint8_t *)qp->rx_buff +
1202		    qp->rx_max_frame * (i + 1) -
1203		    sizeof(struct ntb_payload_header));
1204		memset(offset, 0, sizeof(struct ntb_payload_header));
1205	}
1206
1207	qp->rx_pkts = 0;
1208	qp->tx_pkts = 0;
1209}
1210
1211static void
1212ntb_qp_link_work(void *arg)
1213{
1214	struct ntb_transport_qp *qp = arg;
1215	struct ntb_softc *ntb = qp->ntb;
1216	struct ntb_netdev *nt = qp->transport;
1217	int rc, val;
1218
1219
1220	rc = ntb_read_remote_spad(ntb, IF_NTB_QP_LINKS, &val);
1221	if (rc != 0)
1222		return;
1223
1224	rc = ntb_write_remote_spad(ntb, IF_NTB_QP_LINKS, val | 1 << qp->qp_num);
1225
1226	/* query remote spad for qp ready bits */
1227	rc = ntb_read_local_spad(ntb, IF_NTB_QP_LINKS, &val);
1228
1229	/* See if the remote side is up */
1230	if ((1 << qp->qp_num & val) != 0) {
1231		qp->qp_link = NTB_LINK_UP;
1232		if (qp->event_handler != NULL)
1233			qp->event_handler(qp->cb_data, NTB_LINK_UP);
1234		if (bootverbose)
1235			device_printf(ntb_get_device(ntb), "qp link up\n");
1236	} else if (nt->transport_link == NTB_LINK_UP) {
1237		callout_reset(&qp->link_work,
1238		    NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_qp_link_work, qp);
1239	}
1240}
1241
1242/* Link down event*/
1243static void
1244ntb_transport_link_cleanup(struct ntb_netdev *nt)
1245{
1246	int i;
1247
1248	if (nt->transport_link == NTB_LINK_DOWN)
1249		callout_drain(&nt->link_work);
1250	else
1251		nt->transport_link = NTB_LINK_DOWN;
1252
1253	/* Pass along the info to any clients */
1254	for (i = 0; i < nt->max_qps; i++)
1255		if (!test_bit(i, &nt->qp_bitmap))
1256			ntb_qp_link_down(&nt->qps[i]);
1257
1258	/*
1259	 * The scratchpad registers keep the values if the remote side
1260	 * goes down, blast them now to give them a sane value the next
1261	 * time they are accessed
1262	 */
1263	for (i = 0; i < IF_NTB_MAX_SPAD; i++)
1264		ntb_write_local_spad(nt->ntb, i, 0);
1265}
1266
1267
1268static void
1269ntb_qp_link_down(struct ntb_transport_qp *qp)
1270{
1271
1272	ntb_qp_link_cleanup(qp);
1273}
1274
1275static void
1276ntb_qp_link_cleanup(struct ntb_transport_qp *qp)
1277{
1278	struct ntb_netdev *nt = qp->transport;
1279
1280	if (qp->qp_link == NTB_LINK_DOWN) {
1281		callout_drain(&qp->link_work);
1282		return;
1283	}
1284
1285	if (qp->event_handler != NULL)
1286		qp->event_handler(qp->cb_data, NTB_LINK_DOWN);
1287
1288	qp->qp_link = NTB_LINK_DOWN;
1289
1290	if (nt->transport_link == NTB_LINK_UP)
1291		callout_reset(&qp->link_work,
1292		    NTB_LINK_DOWN_TIMEOUT * hz / 1000, ntb_qp_link_work, qp);
1293}
1294
1295/* Link commanded down */
1296/**
1297 * ntb_transport_link_down - Notify NTB transport to no longer enqueue data
1298 * @qp: NTB transport layer queue to be disabled
1299 *
1300 * Notify NTB transport layer of client's desire to no longer receive data on
1301 * transport queue specified.  It is the client's responsibility to ensure all
1302 * entries on queue are purged or otherwise handled appropraitely.
1303 */
1304static void
1305ntb_transport_link_down(struct ntb_transport_qp *qp)
1306{
1307	int rc, val;
1308
1309	if (qp == NULL)
1310		return;
1311
1312	qp->client_ready = NTB_LINK_DOWN;
1313
1314	rc = ntb_read_remote_spad(qp->ntb, IF_NTB_QP_LINKS, &val);
1315	if (rc != 0)
1316		return;
1317
1318	rc = ntb_write_remote_spad(qp->ntb, IF_NTB_QP_LINKS,
1319	   val & ~(1 << qp->qp_num));
1320
1321	if (qp->qp_link == NTB_LINK_UP)
1322		ntb_send_link_down(qp);
1323	else
1324		callout_drain(&qp->link_work);
1325
1326}
1327
1328static void
1329ntb_send_link_down(struct ntb_transport_qp *qp)
1330{
1331	struct ntb_queue_entry *entry;
1332	int i, rc;
1333
1334	if (qp->qp_link == NTB_LINK_DOWN)
1335		return;
1336
1337	qp->qp_link = NTB_LINK_DOWN;
1338
1339	for (i = 0; i < NTB_LINK_DOWN_TIMEOUT; i++) {
1340		entry = ntb_list_rm(&qp->ntb_tx_free_q_lock, &qp->tx_free_q);
1341		if (entry != NULL)
1342			break;
1343		pause("NTB Wait for link down", hz / 10);
1344	}
1345
1346	if (entry == NULL)
1347		return;
1348
1349	entry->cb_data = NULL;
1350	entry->buf = NULL;
1351	entry->len = 0;
1352	entry->flags = IF_NTB_LINK_DOWN_FLAG;
1353
1354	mtx_lock(&qp->transport->tx_lock);
1355	rc = ntb_process_tx(qp, entry);
1356	if (rc != 0)
1357		printf("ntb: Failed to send link down\n");
1358	mtx_unlock(&qp->transport->tx_lock);
1359}
1360
1361
1362/* List Management */
1363
1364static void
1365ntb_list_add(struct mtx *lock, struct ntb_queue_entry *entry,
1366    struct ntb_queue_list *list)
1367{
1368
1369	mtx_lock_spin(lock);
1370	STAILQ_INSERT_TAIL(list, entry, entry);
1371	mtx_unlock_spin(lock);
1372}
1373
1374static struct ntb_queue_entry *
1375ntb_list_rm(struct mtx *lock, struct ntb_queue_list *list)
1376{
1377	struct ntb_queue_entry *entry;
1378
1379	mtx_lock_spin(lock);
1380	if (STAILQ_EMPTY(list)) {
1381		entry = NULL;
1382		goto out;
1383	}
1384	entry = STAILQ_FIRST(list);
1385	STAILQ_REMOVE_HEAD(list, entry);
1386out:
1387	mtx_unlock_spin(lock);
1388
1389	return (entry);
1390}
1391
1392/* Helper functions */
1393/* TODO: This too should really be part of the kernel */
1394#define EUI48_MULTICAST			1 << 0
1395#define EUI48_LOCALLY_ADMINISTERED	1 << 1
1396static void
1397create_random_local_eui48(u_char *eaddr)
1398{
1399	static uint8_t counter = 0;
1400	uint32_t seed = ticks;
1401
1402	eaddr[0] = EUI48_LOCALLY_ADMINISTERED;
1403	memcpy(&eaddr[1], &seed, sizeof(uint32_t));
1404	eaddr[5] = counter++;
1405}
1406
1407/**
1408 * ntb_transport_max_size - Query the max payload size of a qp
1409 * @qp: NTB transport layer queue to be queried
1410 *
1411 * Query the maximum payload size permissible on the given qp
1412 *
1413 * RETURNS: the max payload size of a qp
1414 */
1415static unsigned int
1416ntb_transport_max_size(struct ntb_transport_qp *qp)
1417{
1418
1419	if (qp == NULL)
1420		return (0);
1421
1422	return (qp->tx_max_frame - sizeof(struct ntb_payload_header));
1423}
1424