summaryrefslogtreecommitdiffstats
path: root/drivers/net/sfc/sfc_intr.c
blob: ee59cb1c05aa42c964129a8d6a2f79657ad09036 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*-
 *   BSD LICENSE
 *
 * Copyright (c) 2016-2017 Solarflare Communications Inc.
 * All rights reserved.
 *
 * This software was jointly developed between OKTET Labs (under contract
 * for Solarflare) and Solarflare Communications, Inc.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 * At the momemt of writing DPDK v16.07 has notion of two types of
 * interrupts: LSC (link status change) and RXQ (receive indication).
 * It allows to register interrupt callback for entire device which is
 * not intended to be used for receive indication (i.e. link status
 * change indication only). The handler has no information which HW
 * interrupt has triggered it, so we don't know which event queue should
 * be polled/reprimed (except qmask in the case of legacy line interrupt).
 */

#include <rte_common.h>
#include <rte_interrupts.h>

#include "efx.h"

#include "sfc.h"
#include "sfc_log.h"
#include "sfc_ev.h"

static void
sfc_intr_handle_mgmt_evq(struct sfc_adapter *sa)
{
	struct sfc_evq *evq;

	rte_spinlock_lock(&sa->mgmt_evq_lock);

	evq = sa->mgmt_evq;

	if (evq->init_state != SFC_EVQ_STARTED) {
		sfc_log_init(sa, "interrupt on stopped EVQ %u", evq->evq_index);
	} else {
		sfc_ev_qpoll(evq);

		if (sfc_ev_qprime(evq) != 0)
			sfc_err(sa, "cannot prime EVQ %u", evq->evq_index);
	}

	rte_spinlock_unlock(&sa->mgmt_evq_lock);
}

static void
sfc_intr_line_handler(void *cb_arg)
{
	struct sfc_adapter *sa = (struct sfc_adapter *)cb_arg;
	efx_nic_t *enp = sa->nic;
	boolean_t fatal;
	uint32_t qmask;
	unsigned int lsc_seq = sa->port.lsc_seq;
	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(sa->eth_dev);

	sfc_log_init(sa, "entry");

	if (sa->state != SFC_ADAPTER_STARTED &&
	    sa->state != SFC_ADAPTER_STARTING &&
	    sa->state != SFC_ADAPTER_STOPPING) {
		sfc_log_init(sa,
			     "interrupt on stopped adapter, don't reenable");
		goto exit;
	}

	efx_intr_status_line(enp, &fatal, &qmask);
	if (fatal) {
		(void)efx_intr_disable(enp);
		(void)efx_intr_fatal(enp);
		sfc_err(sa, "fatal, interrupts disabled");
		goto exit;
	}

	if (qmask & (1 << sa->mgmt_evq_index))
		sfc_intr_handle_mgmt_evq(sa);

	if (rte_intr_enable(&pci_dev->intr_handle) != 0)
		sfc_err(sa, "cannot reenable interrupts");

	sfc_log_init(sa, "done");

exit:
	if (lsc_seq != sa->port.lsc_seq) {
		sfc_info(sa, "link status change event: link %s",
			 sa->eth_dev->data->dev_link.link_status ?
			 "UP" : "DOWN");
		_rte_eth_dev_callback_process(sa->eth_dev,
					      RTE_ETH_EVENT_INTR_LSC,
					      NULL, NULL);
	}
}

static void
sfc_intr_message_handler(void *cb_arg)
{
	struct sfc_adapter *sa = (struct sfc_adapter *)cb_arg;
	efx_nic_t *enp = sa->nic;
	boolean_t fatal;
	unsigned int lsc_seq = sa->port.lsc_seq;
	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(sa->eth_dev);

	sfc_log_init(sa, "entry");

	if (sa->state != SFC_ADAPTER_STARTED &&
	    sa->state != SFC_ADAPTER_STARTING &&
	    sa->state != SFC_ADAPTER_STOPPING) {
		sfc_log_init(sa, "adapter not-started, don't reenable");
		goto exit;
	}

	efx_intr_status_message(enp, sa->mgmt_evq_index, &fatal);
	if (fatal) {
		(void)efx_intr_disable(enp);
		(void)efx_intr_fatal(enp);
		sfc_err(sa, "fatal, interrupts disabled");
		goto exit;
	}

	sfc_intr_handle_mgmt_evq(sa);

	if (rte_intr_enable(&pci_dev->intr_handle) != 0)
		sfc_err(sa, "cannot reenable interrupts");

	sfc_log_init(sa, "done");

exit:
	if (lsc_seq != sa->port.lsc_seq) {
		sfc_info(sa, "link status change event");
		_rte_eth_dev_callback_process(sa->eth_dev,
					      RTE_ETH_EVENT_INTR_LSC,
					      NULL, NULL);
	}
}

int
sfc_intr_start(struct sfc_adapter *sa)
{
	struct sfc_intr *intr = &sa->intr;
	struct rte_intr_handle *intr_handle;
	struct rte_pci_device *pci_dev;
	int rc;

	sfc_log_init(sa, "entry");

	/*
	 * The EFX common code event queue module depends on the interrupt
	 * module. Ensure that the interrupt module is always initialized
	 * (even if interrupts are not used).  Status memory is required
	 * for Siena only and may be NULL for EF10.
	 */
	sfc_log_init(sa, "efx_intr_init");
	rc = efx_intr_init(sa->nic, intr->type, NULL);
	if (rc != 0)
		goto fail_intr_init;

	pci_dev = RTE_ETH_DEV_TO_PCI(sa->eth_dev);
	intr_handle = &pci_dev->intr_handle;

	if (intr->handler != NULL) {
		sfc_log_init(sa, "rte_intr_callback_register");
		rc = rte_intr_callback_register(intr_handle, intr->handler,
						(void *)sa);
		if (rc != 0) {
			sfc_err(sa,
				"cannot register interrupt handler (rc=%d)",
				rc);
			/*
			 * Convert error code from negative returned by RTE API
			 * to positive used in the driver.
			 */
			rc = -rc;
			goto fail_rte_intr_cb_reg;
		}

		sfc_log_init(sa, "rte_intr_enable");
		rc = rte_intr_enable(intr_handle);
		if (rc != 0) {
			sfc_err(sa, "cannot enable interrupts (rc=%d)", rc);
			/*
			 * Convert error code from negative returned by RTE API
			 * to positive used in the driver.
			 */
			rc = -rc;
			goto fail_rte_intr_enable;
		}

		sfc_log_init(sa, "efx_intr_enable");
		efx_intr_enable(sa->nic);
	}

	sfc_log_init(sa, "done type=%u max_intr=%d nb_efd=%u vec=%p",
		     intr_handle->type, intr_handle->max_intr,
		     intr_handle->nb_efd, intr_handle->intr_vec);
	return 0;

fail_rte_intr_enable:
	rte_intr_callback_unregister(intr_handle, intr->handler, (void *)sa);

fail_rte_intr_cb_reg:
	efx_intr_fini(sa->nic);

fail_intr_init:
	sfc_log_init(sa, "failed %d", rc);
	return rc;
}

void
sfc_intr_stop(struct sfc_adapter *sa)
{
	struct sfc_intr *intr = &sa->intr;
	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(sa->eth_dev);

	sfc_log_init(sa, "entry");

	if (intr->handler != NULL) {
		struct rte_intr_handle *intr_handle;
		int rc;

		efx_intr_disable(sa->nic);

		intr_handle = &pci_dev->intr_handle;
		if (rte_intr_disable(intr_handle) != 0)
			sfc_err(sa, "cannot disable interrupts");

		while ((rc = rte_intr_callback_unregister(intr_handle,
				intr->handler, (void *)sa)) == -EAGAIN)
			;
		if (rc != 1)
			sfc_err(sa,
				"cannot unregister interrupt handler %d",
				rc);
	}

	efx_intr_fini(sa->nic);

	sfc_log_init(sa, "done");
}

int
sfc_intr_configure(struct sfc_adapter *sa)
{
	struct sfc_intr *intr = &sa->intr;

	sfc_log_init(sa, "entry");

	intr->handler = NULL;
	intr->lsc_intr = (sa->eth_dev->data->dev_conf.intr_conf.lsc != 0);
	if (!intr->lsc_intr) {
		sfc_info(sa, "LSC tracking using interrupts is disabled");
		goto done;
	}

	switch (intr->type) {
	case EFX_INTR_MESSAGE:
		intr->handler = sfc_intr_message_handler;
		break;
	case EFX_INTR_LINE:
		intr->handler = sfc_intr_line_handler;
		break;
	case EFX_INTR_INVALID:
		sfc_warn(sa, "interrupts are not supported");
		break;
	default:
		sfc_panic(sa, "unexpected EFX interrupt type %u\n", intr->type);
		break;
	}

done:
	sfc_log_init(sa, "done");
	return 0;
}

void
sfc_intr_close(struct sfc_adapter *sa)
{
	sfc_log_init(sa, "entry");

	sfc_log_init(sa, "done");
}

int
sfc_intr_attach(struct sfc_adapter *sa)
{
	struct sfc_intr *intr = &sa->intr;
	struct rte_pci_device *pci_dev = RTE_ETH_DEV_TO_PCI(sa->eth_dev);

	sfc_log_init(sa, "entry");

	switch (pci_dev->intr_handle.type) {
#ifdef RTE_EXEC_ENV_LINUXAPP
	case RTE_INTR_HANDLE_UIO_INTX:
	case RTE_INTR_HANDLE_VFIO_LEGACY:
		intr->type = EFX_INTR_LINE;
		break;
	case RTE_INTR_HANDLE_UIO:
	case RTE_INTR_HANDLE_VFIO_MSI:
	case RTE_INTR_HANDLE_VFIO_MSIX:
		intr->type = EFX_INTR_MESSAGE;
		break;
#endif
	default:
		intr->type = EFX_INTR_INVALID;
		break;
	}

	sfc_log_init(sa, "done");
	return 0;
}

void
sfc_intr_detach(struct sfc_adapter *sa)
{
	sfc_log_init(sa, "entry");

	sa->intr.type = EFX_INTR_INVALID;

	sfc_log_init(sa, "done");
}