aboutsummaryrefslogtreecommitdiffstats
path: root/gpxe/src/drivers/net/mlx_ipoib/ib_driver.c
blob: a46db7fc23c392c305d94687518910b2c5c40f33 (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
/*
  This software is available to you under a choice of one of two
  licenses.  You may choose to be licensed under the terms of the GNU
  General Public License (GPL) Version 2, available at
  <http://www.fsf.org/copyleft/gpl.html>, or the OpenIB.org BSD
  license, available in the LICENSE.TXT file accompanying this
  software.  These details are also available at
  <http://openib.org/license.html>.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  SOFTWARE.

  Copyright (c) 2004 Mellanox Technologies Ltd.  All rights reserved.
*/

#include "ib_driver.h"

static const __u8 ipv4_bcast_gid[] = {
	0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
	0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
};

static int wait_logic_link_up(__u8 port)
{
	unsigned int relax_time, max_time;
	relax_time = 500;
	max_time = 30000;	/* 30 seconds */
	int rc;
	unsigned int i, error = 1;
	__u16 status;
	struct port_info_st pi_var;
	__u8 port_state;

	for (i = 0; i < max_time; i += relax_time) {
		rc = get_port_info(port, &pi_var, &status);
		if (rc) {
			eprintf("");
			return rc;
		} else {
			if (status == 0) {
				port_state = (pi_var.combined4 >> 24) & 0xf;
				//port_state= pi_var.port_state;
				if (port_state == 4) {
					error = 0;
					break;
				}
			}
		}
		printf("+");
		mdelay(relax_time);
	}

	if (i >= max_time)
		return -1;

	return 0;
}

static int ib_driver_init(struct pci_device *pci, udqp_t * ipoib_qph_p)
{
	int rc;
	__u8 port;
	__u16 status;
	__u32 qkey;
	__u16 mlid;
	ud_av_t av;
	struct ib_eqe_st ib_eqe;
	__u8 num_eqe;

	tprintf("");
	rc = ib_device_init(pci);
	if (rc)
		return rc;

	tprintf("");

	memcpy(ib_data.bcast_gid.raw, ipv4_bcast_gid, sizeof(ipv4_bcast_gid));

	port = PXE_IB_PORT;
	rc = setup_hca(port, &ib_data.eq);
	if (rc)
		return rc;
	tprintf("setup_hca() success");

	ib_data.port = port;

	if(print_info)
		printf("boot port = %d\n", ib_data.port);

	rc = wait_logic_link_up(port);
	if (rc)
		return rc;

	tprintf("wait_logic_link_up() success");

	rc = get_guid_info(&status);
	if (rc) {
		eprintf("");
		return rc;
	} else if (status) {
		eprintf("");
		return rc;
	}

	tprintf("get_guid_info() success");

	/* this to flush stdout that contains previous chars */
	printf("    \n");
	if(print_info) {
		__u8 *gid=ib_data.port_gid.raw;

		printf("\n");
		printf("port GID=%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:"
		       "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\n",
		       gid[0],gid[1],gid[2],gid[3],gid[4],gid[5],gid[6],gid[7],
		       gid[8],gid[9],gid[10],gid[11],gid[12],gid[13],gid[14],gid[15]);
	}

	rc = get_pkey_tbl(NULL, &status);
	if (rc) {
		eprintf("");
		return rc;
	} else if (status) {
		eprintf("");
		return rc;
	}
	rc = create_mads_qp(&ib_data.mads_qp,
			    &ib_data.mads_snd_cq, &ib_data.mads_rcv_cq);
	if (rc) {
		eprintf("");
		return rc;
	}

	tprintf("attempt to join mcast group ...");
	rc = join_mc_group(&qkey, &mlid, 1);
	if (rc) {
		eprintf("");
		return rc;
	} else {
		tprintf("join_mc_group() successfull qkey=0x%lx, mlid=0x%x",
			qkey, mlid);
	}

	rc = create_ipoib_qp(&ib_data.ipoib_qp,
			     &ib_data.ipoib_snd_cq,
			     &ib_data.ipoib_rcv_cq, qkey);
	if (rc) {
		eprintf("");
		return rc;
	}

	tprintf("create_ipoib_qp() success");
	*ipoib_qph_p = ib_data.ipoib_qp;

	tprintf("register qp to receive mcast...");
	rc = add_qp_to_mcast_group(ib_data.bcast_gid, 1);
	if (rc) {
		eprintf("");
		return rc;
	} else {
		tprintf("add_qp_to_mcast_group() success");
	}

	/* create a broadcast group ud AV */
	av = alloc_ud_av();
	if (!av) {
		eprintf("");
		return -1;
	}
	tprintf("alloc_ud_av() success");
	modify_av_params(av, mlid, 1, 0, 0, &ib_data.bcast_gid, BCAST_QPN);
	tprintf("modify_av_params() success");
	ib_data.bcast_av = av;

	do {
		rc = poll_eq(&ib_eqe, &num_eqe);
		if (rc) {
			eprintf("");
			return -1;
		}
		if (num_eqe) {
			tprintf("num_eqe=%d", num_eqe);
		}
		tprintf("num_eqe=%d", num_eqe);
	} while (num_eqe);
	tprintf("eq is drained");

	clear_interrupt();

	return rc;
}

static int ib_driver_close(int fw_fatal)
{
	int rc, ret = 0;
	__u32 qkey;
	__u16 mlid;

	rc = ib_device_close();
	if (rc) {
		eprintf("ib_device_close() failed");
		ret = 1;
	}

	tprintf("");
	if (!fw_fatal) {
		rc = join_mc_group(&qkey, &mlid, 0);
		if (rc) {
			eprintf("");
			ret = 1;
		}
		tprintf("join_mc_group(leave) success");

		rc = add_qp_to_mcast_group(ib_data.bcast_gid, 0);
		if (rc) {
			eprintf("");
			ret = 1;
		}
		tprintf("add_qp_to_mcast_group(remove) success");

		rc = cmd_close_ib(ib_data.port);
		if (rc) {
			eprintf("");
			ret = 1;
		}
		tprintf("cmd_close_ib(%d) success", ib_data.port);

		if (destroy_udqp(ib_data.mads_qp)) {
			eprintf("");
			ret = 1;
		}

		if (destroy_udqp(ib_data.ipoib_qp)) {
			eprintf("");
			ret = 1;
		}
	}

	rc = cmd_close_hca(fw_fatal);
	if (rc) {
		eprintf("");
		ret = 1;
	}

	if (!fw_fatal) {
		rc = cmd_sys_dis();
		if (rc) {
			eprintf("");
			ret = 1;
		}
	}

	return ret;
}

static int poll_cqe_tout(cq_t cqh, __u16 tout, void **wqe, int *good_p)
{
	int rc;
	struct ib_cqe_st ib_cqe;
	__u8 num_cqes;
	unsigned long end;

	end = currticks() + tout;
	do {
		rc = ib_poll_cq(cqh, &ib_cqe, &num_cqes);
		if (rc)
			return rc;

		if (num_cqes == 1) {
			if (good_p) {
				*good_p = ib_cqe.is_error ? 0 : 1;
			}
			if (wqe)
				*wqe = ib_cqe.wqe;
			return 0;
		}
	}
	while (currticks() < end);

	return -1;
}

static u8 *get_port_gid(void)
{
	return ib_data.port_gid.raw;
}

static __u32 ib_get_qpn(udqp_t qph)
{
	__u32 qpn;

	qpn = dev_get_qpn(qph);

	return qpn;
}

static int drain_eq(void)
{
	__u8 num_eqe = 0, tot_eqe = 0;
	int rc;

	do {
		tot_eqe += num_eqe;
		rc = poll_eq(ib_data.eq, &num_eqe);
		if (rc) {
			eprintf("");
			return -1;
		}

		tprintf("num_eqe=%d", num_eqe);
	} while (num_eqe);
	tprintf("eq is drained");
	if (tot_eqe) {
		tprintf("got %d eqes", tot_eqe);
		return -1;
	}

	return 0;
}


static int poll_error_buf(void)
{
	__u32 *ptr= dev_ib_data.error_buf_addr;
	__u32 i;

	for (i=0; i<dev_ib_data.error_buf_size; ++i, ptr++) {
		if ( readl(ptr) ) {
			return -1;
		}
	}

	return 0;
}