summaryrefslogtreecommitdiff
path: root/drivers/net/ethernet/marvell/octeon_ep_vf/octep_vf_mbox.c
blob: 2eab21e43048a1149745874bfddbc053ccb2b6af (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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
// SPDX-License-Identifier: GPL-2.0
/* Marvell Octeon EP (EndPoint) VF Ethernet Driver
 *
 * Copyright (C) 2020 Marvell.
 *
 */
#include <linux/types.h>
#include <linux/pci.h>
#include <linux/netdevice.h>
#include "octep_vf_config.h"
#include "octep_vf_main.h"

/* When a new command is implemented, the below table should be updated
 * with new command and it's version info.
 */
static u32 pfvf_cmd_versions[OCTEP_PFVF_MBOX_CMD_MAX] = {
	[0 ... OCTEP_PFVF_MBOX_CMD_DEV_REMOVE] = OCTEP_PFVF_MBOX_VERSION_V1,
	[OCTEP_PFVF_MBOX_CMD_GET_FW_INFO ... OCTEP_PFVF_MBOX_NOTIF_LINK_STATUS] =
		OCTEP_PFVF_MBOX_VERSION_V2
};

int octep_vf_setup_mbox(struct octep_vf_device *oct)
{
	int ring = 0;

	oct->mbox = vzalloc(sizeof(*oct->mbox));
	if (!oct->mbox)
		return -1;

	mutex_init(&oct->mbox->lock);

	oct->hw_ops.setup_mbox_regs(oct, ring);
	INIT_WORK(&oct->mbox->wk.work, octep_vf_mbox_work);
	oct->mbox->wk.ctxptr = oct;
	oct->mbox_neg_ver = OCTEP_PFVF_MBOX_VERSION_CURRENT;
	dev_info(&oct->pdev->dev, "setup vf mbox successfully\n");
	return 0;
}

void octep_vf_delete_mbox(struct octep_vf_device *oct)
{
	if (oct->mbox) {
		if (work_pending(&oct->mbox->wk.work))
			cancel_work_sync(&oct->mbox->wk.work);

		mutex_destroy(&oct->mbox->lock);
		vfree(oct->mbox);
		oct->mbox = NULL;
		dev_info(&oct->pdev->dev, "Deleted vf mbox successfully\n");
	}
}

int octep_vf_mbox_version_check(struct octep_vf_device *oct)
{
	union octep_pfvf_mbox_word cmd;
	union octep_pfvf_mbox_word rsp;
	int ret;

	cmd.u64 = 0;
	cmd.s_version.opcode = OCTEP_PFVF_MBOX_CMD_VERSION;
	cmd.s_version.version = OCTEP_PFVF_MBOX_VERSION_CURRENT;
	ret = octep_vf_mbox_send_cmd(oct, cmd, &rsp);
	if (ret == OCTEP_PFVF_MBOX_CMD_STATUS_NACK) {
		dev_err(&oct->pdev->dev,
			"VF Mbox version is incompatible with PF\n");
		return -EINVAL;
	}
	oct->mbox_neg_ver = (u32)rsp.s_version.version;
	dev_dbg(&oct->pdev->dev,
		"VF Mbox version:%u Negotiated VF version with PF:%u\n",
		 (u32)cmd.s_version.version,
		 (u32)rsp.s_version.version);
	return 0;
}

void octep_vf_mbox_work(struct work_struct *work)
{
	struct octep_vf_mbox_wk *wk = container_of(work, struct octep_vf_mbox_wk, work);
	struct octep_vf_iface_link_info *link_info;
	struct octep_vf_device *oct = NULL;
	struct octep_vf_mbox *mbox = NULL;
	union octep_pfvf_mbox_word *notif;
	u64 pf_vf_data;

	oct = (struct octep_vf_device *)wk->ctxptr;
	link_info = &oct->link_info;
	mbox = oct->mbox;
	pf_vf_data = readq(mbox->mbox_read_reg);

	notif = (union octep_pfvf_mbox_word *)&pf_vf_data;

	switch (notif->s.opcode) {
	case OCTEP_PFVF_MBOX_NOTIF_LINK_STATUS:
		if (notif->s_link_status.status) {
			link_info->oper_up = OCTEP_PFVF_LINK_STATUS_UP;
			netif_carrier_on(oct->netdev);
			dev_info(&oct->pdev->dev, "netif_carrier_on\n");
		} else {
			link_info->oper_up = OCTEP_PFVF_LINK_STATUS_DOWN;
			netif_carrier_off(oct->netdev);
			dev_info(&oct->pdev->dev, "netif_carrier_off\n");
		}
		break;
	default:
		dev_err(&oct->pdev->dev,
			"Received unsupported notif %d\n", notif->s.opcode);
		break;
	}
}

static int __octep_vf_mbox_send_cmd(struct octep_vf_device *oct,
				    union octep_pfvf_mbox_word cmd,
				    union octep_pfvf_mbox_word *rsp)
{
	struct octep_vf_mbox *mbox = oct->mbox;
	u64 reg_val = 0ull;
	int count;

	if (!mbox)
		return OCTEP_PFVF_MBOX_CMD_STATUS_NOT_SETUP;

	cmd.s.type = OCTEP_PFVF_MBOX_TYPE_CMD;
	writeq(cmd.u64, mbox->mbox_write_reg);

	/* No response for notification messages */
	if (!rsp)
		return 0;

	for (count = 0; count < OCTEP_PFVF_MBOX_TIMEOUT_WAIT_COUNT; count++) {
		usleep_range(1000, 1500);
		reg_val = readq(mbox->mbox_write_reg);
		if (reg_val != cmd.u64) {
			rsp->u64 = reg_val;
			break;
		}
	}
	if (count == OCTEP_PFVF_MBOX_TIMEOUT_WAIT_COUNT) {
		dev_err(&oct->pdev->dev, "mbox send command timed out\n");
		return OCTEP_PFVF_MBOX_CMD_STATUS_TIMEDOUT;
	}
	if (rsp->s.type != OCTEP_PFVF_MBOX_TYPE_RSP_ACK) {
		dev_err(&oct->pdev->dev, "mbox_send: Received NACK\n");
		return OCTEP_PFVF_MBOX_CMD_STATUS_NACK;
	}
	rsp->u64 = reg_val;
	return 0;
}

int octep_vf_mbox_send_cmd(struct octep_vf_device *oct, union octep_pfvf_mbox_word cmd,
			   union octep_pfvf_mbox_word *rsp)
{
	struct octep_vf_mbox *mbox = oct->mbox;
	int ret;

	if (!mbox)
		return OCTEP_PFVF_MBOX_CMD_STATUS_NOT_SETUP;
	mutex_lock(&mbox->lock);
	if (pfvf_cmd_versions[cmd.s.opcode] > oct->mbox_neg_ver) {
		dev_dbg(&oct->pdev->dev, "CMD:%d not supported in Version:%d\n",
			cmd.s.opcode, oct->mbox_neg_ver);
		mutex_unlock(&mbox->lock);
		return -EOPNOTSUPP;
	}
	ret = __octep_vf_mbox_send_cmd(oct, cmd, rsp);
	mutex_unlock(&mbox->lock);
	return ret;
}

int octep_vf_mbox_bulk_read(struct octep_vf_device *oct, enum octep_pfvf_mbox_opcode opcode,
			    u8 *data, int *size)
{
	struct octep_vf_mbox *mbox = oct->mbox;
	union octep_pfvf_mbox_word cmd;
	union octep_pfvf_mbox_word rsp;
	int data_len = 0, tmp_len = 0;
	int read_cnt, i = 0, ret;

	if (!mbox)
		return OCTEP_PFVF_MBOX_CMD_STATUS_NOT_SETUP;

	mutex_lock(&mbox->lock);
	cmd.u64 = 0;
	cmd.s_data.opcode = opcode;
	cmd.s_data.frag = 0;
	/* Send cmd to read data from PF */
	ret = __octep_vf_mbox_send_cmd(oct, cmd, &rsp);
	if (ret) {
		dev_err(&oct->pdev->dev, "send mbox cmd fail for data request\n");
		mutex_unlock(&mbox->lock);
		return ret;
	}
	/*  PF sends the data length of requested CMD
	 *  in  ACK
	 */
	data_len = *((int32_t *)rsp.s_data.data);
	tmp_len = data_len;
	cmd.u64 = 0;
	rsp.u64 = 0;
	cmd.s_data.opcode = opcode;
	cmd.s_data.frag = 1;
	while (data_len) {
		ret = __octep_vf_mbox_send_cmd(oct, cmd, &rsp);
		if (ret) {
			dev_err(&oct->pdev->dev, "send mbox cmd fail for data request\n");
			mutex_unlock(&mbox->lock);
			mbox->mbox_data.data_index = 0;
			memset(mbox->mbox_data.recv_data, 0, OCTEP_PFVF_MBOX_MAX_DATA_BUF_SIZE);
			return ret;
		}
		if (data_len > OCTEP_PFVF_MBOX_MAX_DATA_SIZE) {
			data_len -= OCTEP_PFVF_MBOX_MAX_DATA_SIZE;
			read_cnt = OCTEP_PFVF_MBOX_MAX_DATA_SIZE;
		} else {
			read_cnt = data_len;
			data_len = 0;
		}
		for (i = 0; i < read_cnt; i++) {
			mbox->mbox_data.recv_data[mbox->mbox_data.data_index] =
				rsp.s_data.data[i];
			mbox->mbox_data.data_index++;
		}
		cmd.u64 = 0;
		rsp.u64 = 0;
		cmd.s_data.opcode = opcode;
		cmd.s_data.frag = 1;
	}
	memcpy(data, mbox->mbox_data.recv_data, tmp_len);
	*size = tmp_len;
	mbox->mbox_data.data_index = 0;
	memset(mbox->mbox_data.recv_data, 0, OCTEP_PFVF_MBOX_MAX_DATA_BUF_SIZE);
	mutex_unlock(&mbox->lock);
	return 0;
}

int octep_vf_mbox_set_mtu(struct octep_vf_device *oct, int mtu)
{
	int frame_size = mtu + ETH_HLEN + ETH_FCS_LEN;
	union octep_pfvf_mbox_word cmd;
	union octep_pfvf_mbox_word rsp;
	int ret = 0;

	if (mtu < ETH_MIN_MTU || frame_size > ETH_MAX_MTU) {
		dev_err(&oct->pdev->dev,
			"Failed to set MTU to %d MIN MTU:%d MAX MTU:%d\n",
			mtu, ETH_MIN_MTU, ETH_MAX_MTU);
		return -EINVAL;
	}

	cmd.u64 = 0;
	cmd.s_set_mtu.opcode = OCTEP_PFVF_MBOX_CMD_SET_MTU;
	cmd.s_set_mtu.mtu = mtu;

	ret = octep_vf_mbox_send_cmd(oct, cmd, &rsp);
	if (ret) {
		dev_err(&oct->pdev->dev, "Mbox send failed; err=%d\n", ret);
		return ret;
	}
	if (rsp.s_set_mtu.type != OCTEP_PFVF_MBOX_TYPE_RSP_ACK) {
		dev_err(&oct->pdev->dev, "Received Mbox NACK from PF for MTU:%d\n", mtu);
		return -EINVAL;
	}

	return 0;
}

int octep_vf_mbox_set_mac_addr(struct octep_vf_device *oct, char *mac_addr)
{
	union octep_pfvf_mbox_word cmd;
	union octep_pfvf_mbox_word rsp;
	int i, ret;

	cmd.u64 = 0;
	cmd.s_set_mac.opcode = OCTEP_PFVF_MBOX_CMD_SET_MAC_ADDR;
	for (i = 0; i < ETH_ALEN; i++)
		cmd.s_set_mac.mac_addr[i] = mac_addr[i];
	ret = octep_vf_mbox_send_cmd(oct, cmd, &rsp);
	if (ret) {
		dev_err(&oct->pdev->dev, "Mbox send failed; err = %d\n", ret);
		return ret;
	}
	if (rsp.s_set_mac.type != OCTEP_PFVF_MBOX_TYPE_RSP_ACK) {
		dev_err(&oct->pdev->dev, "received NACK\n");
		return -EINVAL;
	}
	return 0;
}

int octep_vf_mbox_get_mac_addr(struct octep_vf_device *oct, char *mac_addr)
{
	union octep_pfvf_mbox_word cmd;
	union octep_pfvf_mbox_word rsp;
	int i, ret;

	cmd.u64 = 0;
	cmd.s_set_mac.opcode = OCTEP_PFVF_MBOX_CMD_GET_MAC_ADDR;
	ret = octep_vf_mbox_send_cmd(oct, cmd, &rsp);
	if (ret) {
		dev_err(&oct->pdev->dev, "get_mac: mbox send failed; err = %d\n", ret);
		return ret;
	}
	if (rsp.s_set_mac.type != OCTEP_PFVF_MBOX_TYPE_RSP_ACK) {
		dev_err(&oct->pdev->dev, "get_mac: received NACK\n");
		return -EINVAL;
	}
	for (i = 0; i < ETH_ALEN; i++)
		mac_addr[i] = rsp.s_set_mac.mac_addr[i];
	return 0;
}

int octep_vf_mbox_set_rx_state(struct octep_vf_device *oct, bool state)
{
	union octep_pfvf_mbox_word cmd;
	union octep_pfvf_mbox_word rsp;
	int ret;

	cmd.u64 = 0;
	cmd.s_link_state.opcode = OCTEP_PFVF_MBOX_CMD_SET_RX_STATE;
	cmd.s_link_state.state = state;
	ret = octep_vf_mbox_send_cmd(oct, cmd, &rsp);
	if (ret) {
		dev_err(&oct->pdev->dev, "Set Rx state via VF Mbox send failed\n");
		return ret;
	}
	if (rsp.s_link_state.type != OCTEP_PFVF_MBOX_TYPE_RSP_ACK) {
		dev_err(&oct->pdev->dev, "Set Rx state received NACK\n");
		return -EINVAL;
	}
	return 0;
}

int octep_vf_mbox_set_link_status(struct octep_vf_device *oct, bool status)
{
	union octep_pfvf_mbox_word cmd;
	union octep_pfvf_mbox_word rsp;
	int ret;

	cmd.u64 = 0;
	cmd.s_link_status.opcode = OCTEP_PFVF_MBOX_CMD_SET_LINK_STATUS;
	cmd.s_link_status.status = status;
	ret = octep_vf_mbox_send_cmd(oct, cmd, &rsp);
	if (ret) {
		dev_err(&oct->pdev->dev, "Set link status via VF Mbox send failed\n");
		return ret;
	}
	if (rsp.s_link_status.type != OCTEP_PFVF_MBOX_TYPE_RSP_ACK) {
		dev_err(&oct->pdev->dev, "Set link status received NACK\n");
		return -EINVAL;
	}
	return 0;
}

int octep_vf_mbox_get_link_status(struct octep_vf_device *oct, u8 *oper_up)
{
	union octep_pfvf_mbox_word cmd;
	union octep_pfvf_mbox_word rsp;
	int ret;

	cmd.u64 = 0;
	cmd.s_link_status.opcode = OCTEP_PFVF_MBOX_CMD_GET_LINK_STATUS;
	ret = octep_vf_mbox_send_cmd(oct, cmd, &rsp);
	if (ret) {
		dev_err(&oct->pdev->dev, "Get link status via VF Mbox send failed\n");
		return ret;
	}
	if (rsp.s_link_status.type != OCTEP_PFVF_MBOX_TYPE_RSP_ACK) {
		dev_err(&oct->pdev->dev, "Get link status received NACK\n");
		return -EINVAL;
	}
	*oper_up = rsp.s_link_status.status;
	return 0;
}

int octep_vf_mbox_dev_remove(struct octep_vf_device *oct)
{
	union octep_pfvf_mbox_word cmd;
	int ret;

	cmd.u64 = 0;
	cmd.s.opcode = OCTEP_PFVF_MBOX_CMD_DEV_REMOVE;
	ret = octep_vf_mbox_send_cmd(oct, cmd, NULL);
	return ret;
}

int octep_vf_mbox_get_fw_info(struct octep_vf_device *oct)
{
	union octep_pfvf_mbox_word cmd;
	union octep_pfvf_mbox_word rsp;
	int ret;

	cmd.u64 = 0;
	cmd.s_fw_info.opcode = OCTEP_PFVF_MBOX_CMD_GET_FW_INFO;
	ret = octep_vf_mbox_send_cmd(oct, cmd, &rsp);
	if (ret) {
		dev_err(&oct->pdev->dev, "Get link status via VF Mbox send failed\n");
		return ret;
	}
	if (rsp.s_fw_info.type != OCTEP_PFVF_MBOX_TYPE_RSP_ACK) {
		dev_err(&oct->pdev->dev, "Get link status received NACK\n");
		return -EINVAL;
	}
	oct->fw_info.pkind = rsp.s_fw_info.pkind;
	oct->fw_info.fsz = rsp.s_fw_info.fsz;
	oct->fw_info.rx_ol_flags = rsp.s_fw_info.rx_ol_flags;
	oct->fw_info.tx_ol_flags = rsp.s_fw_info.tx_ol_flags;

	return 0;
}

int octep_vf_mbox_set_offloads(struct octep_vf_device *oct, u16 tx_offloads,
			       u16 rx_offloads)
{
	union octep_pfvf_mbox_word cmd;
	union octep_pfvf_mbox_word rsp;
	int ret;

	cmd.u64 = 0;
	cmd.s_offloads.opcode = OCTEP_PFVF_MBOX_CMD_SET_OFFLOADS;
	cmd.s_offloads.rx_ol_flags = rx_offloads;
	cmd.s_offloads.tx_ol_flags = tx_offloads;
	ret = octep_vf_mbox_send_cmd(oct, cmd, &rsp);
	if (ret) {
		dev_err(&oct->pdev->dev, "Set offloads via VF Mbox send failed\n");
		return ret;
	}
	if (rsp.s_link_state.type != OCTEP_PFVF_MBOX_TYPE_RSP_ACK) {
		dev_err(&oct->pdev->dev, "Set offloads received NACK\n");
		return -EINVAL;
	}
	return 0;
}