summaryrefslogtreecommitdiff
path: root/usr/src/uts/common/io/sfxge/sfxge_nvram.c
blob: 9dc052d9c433724529707f9adad08c62ad563cfe (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
/*
 * Copyright (c) 2009-2016 Solarflare Communications Inc.
 * All rights reserved.
 *
 * 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.
 *
 * The views and conclusions contained in the software and documentation are
 * those of the authors and should not be interpreted as representing official
 * policies, either expressed or implied, of the FreeBSD Project.
 */

#include <sys/types.h>
#include <sys/ddi.h>
#include <sys/sunddi.h>
#include <sys/stream.h>
#include <sys/dlpi.h>

#include "sfxge.h"

static int
sfxge_nvram_rw(sfxge_t *sp, sfxge_nvram_ioc_t *snip, efx_nvram_type_t type,
    boolean_t write)
{
	int (*op)(efx_nic_t *, efx_nvram_type_t, unsigned int, caddr_t, size_t);
	efx_nic_t *enp = sp->s_enp;
	size_t chunk_size;
	off_t off;
	int rc;

	op = (write) ? efx_nvram_write_chunk : efx_nvram_read_chunk;

	if ((rc = efx_nvram_rw_start(enp, type, &chunk_size)) != 0)
		goto fail1;

	off = 0;
	while (snip->sni_size) {
		size_t len = MIN(chunk_size, snip->sni_size);
		caddr_t buf = (caddr_t)(&snip->sni_data[off]);

		if ((rc = op(enp, type, snip->sni_offset + off, buf, len)) != 0)
			goto fail2;

		snip->sni_size -= len;
		off += len;
	}

	efx_nvram_rw_finish(enp, type);
	return (0);

fail2:
	DTRACE_PROBE(fail2);
	efx_nvram_rw_finish(enp, type);
fail1:
	DTRACE_PROBE1(fail1, int, rc);
	return (rc);
}


static int
sfxge_nvram_erase(sfxge_t *sp, sfxge_nvram_ioc_t *snip, efx_nvram_type_t type)
{
	efx_nic_t *enp = sp->s_enp;
	size_t chunk_size;
	int rc;
	_NOTE(ARGUNUSED(snip));

	if ((rc = efx_nvram_rw_start(enp, type, &chunk_size)) != 0)
		goto fail1;

	if ((rc = efx_nvram_erase(enp, type)) != 0)
		goto fail2;

	efx_nvram_rw_finish(enp, type);
	return (0);

fail2:
	DTRACE_PROBE(fail2);
	efx_nvram_rw_finish(enp, type);
fail1:
	DTRACE_PROBE1(fail1, int, rc);
	return (rc);
}

int
sfxge_nvram_ioctl(sfxge_t *sp, sfxge_nvram_ioc_t *snip)
{
	efx_nic_t *enp = sp->s_enp;
	efx_nvram_type_t type;
	int rc;

	switch (snip->sni_type) {
	case SFXGE_NVRAM_TYPE_BOOTROM:
		type = EFX_NVRAM_BOOTROM;
		break;
	case SFXGE_NVRAM_TYPE_BOOTROM_CFG:
		type = EFX_NVRAM_BOOTROM_CFG;
		break;
	case SFXGE_NVRAM_TYPE_MC:
		type = EFX_NVRAM_MC_FIRMWARE;
		break;
	case SFXGE_NVRAM_TYPE_MC_GOLDEN:
		type = EFX_NVRAM_MC_GOLDEN;
		if (snip->sni_op == SFXGE_NVRAM_OP_WRITE ||
		    snip->sni_op == SFXGE_NVRAM_OP_ERASE ||
		    snip->sni_op == SFXGE_NVRAM_OP_SET_VER) {
			rc = ENOTSUP;
			goto fail1;
		}
		break;
	case SFXGE_NVRAM_TYPE_PHY:
		type = EFX_NVRAM_PHY;
		break;
	case SFXGE_NVRAM_TYPE_NULL_PHY:
		type = EFX_NVRAM_NULLPHY;
		break;
	case SFXGE_NVRAM_TYPE_FPGA: /* PTP timestamping FPGA */
		type = EFX_NVRAM_FPGA;
		break;
	case SFXGE_NVRAM_TYPE_FCFW:
		type = EFX_NVRAM_FCFW;
		break;
	case SFXGE_NVRAM_TYPE_CPLD:
		type = EFX_NVRAM_CPLD;
		break;
	case SFXGE_NVRAM_TYPE_FPGA_BACKUP:
		type = EFX_NVRAM_FPGA_BACKUP;
		break;
	case SFXGE_NVRAM_TYPE_DYNAMIC_CFG:
		type = EFX_NVRAM_DYNAMIC_CFG;
		break;
	default:
		rc = EINVAL;
		goto fail2;
	}

	if (snip->sni_size > sizeof (snip->sni_data)) {
		rc = ENOSPC;
		goto fail3;
	}

	switch (snip->sni_op) {
	case SFXGE_NVRAM_OP_SIZE:
	{
		size_t size;
		if ((rc = efx_nvram_size(enp, type, &size)) != 0)
			goto fail4;
		snip->sni_size = (uint32_t)size;
		break;
	}
	case SFXGE_NVRAM_OP_READ:
		if ((rc = sfxge_nvram_rw(sp, snip, type, B_FALSE)) != 0)
			goto fail4;
		break;
	case SFXGE_NVRAM_OP_WRITE:
		if ((rc = sfxge_nvram_rw(sp, snip, type, B_TRUE)) != 0)
			goto fail4;
		break;
	case SFXGE_NVRAM_OP_ERASE:
		if ((rc = sfxge_nvram_erase(sp, snip, type)) != 0)
			goto fail4;
		break;
	case SFXGE_NVRAM_OP_GET_VER:
		if ((rc = efx_nvram_get_version(enp, type, &snip->sni_subtype,
		    &snip->sni_version[0])) != 0)
			goto fail4;
		break;
	case SFXGE_NVRAM_OP_SET_VER:
		if ((rc = efx_nvram_set_version(enp, type,
		    &snip->sni_version[0])) != 0)
			goto fail4;
		break;
	default:
		rc = ENOTSUP;
		goto fail5;
	}

	return (0);

fail5:
	DTRACE_PROBE(fail5);
fail4:
	DTRACE_PROBE(fail4);
fail3:
	DTRACE_PROBE(fail3);
fail2:
	DTRACE_PROBE(fail2);
fail1:
	DTRACE_PROBE1(fail1, int, rc);

	return (rc);
}