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
431
432
433
434
435
436
437
|
/* $OpenBSD: ldc.c,v 1.2 2009/01/12 19:34:08 kettenis Exp $ */
/*
* Copyright (c) 2009 Mark Kettenis
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/malloc.h>
#include <sys/systm.h>
#include <machine/bus.h>
#include <machine/hypervisor.h>
#include <sparc64/dev/ldcvar.h>
#ifdef LDC_DEBUG
#define DPRINTF(x) printf x
#else
#define DPRINTF(x)
#endif
void ldc_rx_ctrl_vers(struct ldc_conn *, struct ldc_pkt *);
void ldc_rx_ctrl_rtr(struct ldc_conn *, struct ldc_pkt *);
void ldc_rx_ctrl_rts(struct ldc_conn *, struct ldc_pkt *);
void ldc_send_rtr(struct ldc_conn *);
void ldc_send_rts(struct ldc_conn *);
void ldc_send_rdx(struct ldc_conn *);
void
ldc_rx_ctrl(struct ldc_conn *lc, struct ldc_pkt *lp)
{
switch (lp->ctrl) {
case LDC_VERS:
ldc_rx_ctrl_vers(lc, lp);
break;
case LDC_RTS:
ldc_rx_ctrl_rts(lc, lp);
break;
case LDC_RTR:
ldc_rx_ctrl_rtr(lc, lp);
break;
default:
DPRINTF(("CTRL/0x%02x/0x%02x\n", lp->stype, lp->ctrl));
ldc_reset(lc);
break;
}
}
void
ldc_rx_ctrl_vers(struct ldc_conn *lc, struct ldc_pkt *lp)
{
switch (lp->stype) {
case LDC_INFO:
/* XXX do nothing for now. */
break;
case LDC_ACK:
if (lc->lc_state != LDC_SND_VERS) {
DPRINTF(("Spurious CTRL/ACK/VERS: state %d\n",
lc->lc_state));
ldc_reset(lc);
return;
}
DPRINTF(("CTRL/ACK/VERS\n"));
ldc_send_rts(lc);
break;
case LDC_NACK:
DPRINTF(("CTRL/NACK/VERS\n"));
ldc_reset(lc);
break;
default:
DPRINTF(("CTRL/0x%02x/VERS\n", lp->stype));
ldc_reset(lc);
break;
}
}
void
ldc_rx_ctrl_rts(struct ldc_conn *lc, struct ldc_pkt *lp)
{
switch (lp->stype) {
case LDC_INFO:
if (lc->lc_state != LDC_RCV_VERS) {
DPRINTF(("Suprious CTRL/INFO/RTS: state %d\n",
lc->lc_state));
ldc_reset(lc);
return;
}
DPRINTF(("CTRL/INFO/RTS\n"));
ldc_send_rtr(lc);
break;
case LDC_ACK:
DPRINTF(("CTRL/ACK/RTS\n"));
ldc_reset(lc);
break;
case LDC_NACK:
DPRINTF(("CTRL/NACK/RTS\n"));
ldc_reset(lc);
break;
default:
DPRINTF(("CTRL/0x%02x/RTS\n", lp->stype));
ldc_reset(lc);
break;
}
}
void
ldc_rx_ctrl_rtr(struct ldc_conn *lc, struct ldc_pkt *lp)
{
switch (lp->stype) {
case LDC_INFO:
if (lc->lc_state != LDC_SND_RTS) {
DPRINTF(("Spurious CTRL/INFO/RTR: state %d\n",
lc->lc_state));
ldc_reset(lc);
return;
}
DPRINTF(("CTRL/INFO/RTR\n"));
ldc_send_rdx(lc);
if (lc->lc_start)
lc->lc_start(lc);
break;
case LDC_ACK:
DPRINTF(("CTRL/ACK/RTR\n"));
ldc_reset(lc);
break;
case LDC_NACK:
DPRINTF(("CTRL/NACK/RTR\n"));
ldc_reset(lc);
break;
default:
DPRINTF(("CTRL/0x%02x/RTR\n", lp->stype));
ldc_reset(lc);
break;
}
}
void
ldc_rx_data(struct ldc_conn *lc, struct ldc_pkt *lp)
{
if (lp->stype != LDC_INFO) {
DPRINTF(("DATA/0x%02x\n", lp->stype));
ldc_reset(lc);
return;
}
if (lc->lc_state != LDC_SND_RTR &&
lc->lc_state != LDC_SND_RDX) {
DPRINTF(("Spurious DATA/INFO: state %d\n", lc->lc_state));
ldc_reset(lc);
return;
}
lc->lc_rx_data(lc, lp);
}
void
ldc_send_vers(struct ldc_conn *lc)
{
struct ldc_pkt *lp;
uint64_t tx_head, tx_tail, tx_state;
int err;
err = hv_ldc_tx_get_state(lc->lc_id, &tx_head, &tx_tail, &tx_state);
if (err != H_EOK || tx_state != LDC_CHANNEL_UP)
return;
lp = (struct ldc_pkt *)(lc->lc_txq->lq_va + tx_tail);
bzero(lp, sizeof(struct ldc_pkt));
lp->type = LDC_CTRL;
lp->stype = LDC_INFO;
lp->ctrl = LDC_VERS;
lp->major = 1;
lp->minor = 0;
tx_tail += sizeof(*lp);
tx_tail &= ((lc->lc_txq->lq_nentries * sizeof(*lp)) - 1);
err = hv_ldc_tx_set_qtail(lc->lc_id, tx_tail);
if (err != H_EOK) {
printf("%s: hv_ldc_tx_set_qtail: %d\n", __func__, err);
return;
}
lc->lc_state = LDC_SND_VERS;
}
void
ldc_send_rts(struct ldc_conn *lc)
{
struct ldc_pkt *lp;
uint64_t tx_head, tx_tail, tx_state;
int err;
err = hv_ldc_tx_get_state(lc->lc_id, &tx_head, &tx_tail, &tx_state);
if (err != H_EOK || tx_state != LDC_CHANNEL_UP)
return;
lp = (struct ldc_pkt *)(lc->lc_txq->lq_va + tx_tail);
bzero(lp, sizeof(struct ldc_pkt));
lp->type = LDC_CTRL;
lp->stype = LDC_INFO;
lp->ctrl = LDC_RTS;
lp->env = LDC_MODE_UNRELIABLE;
lp->seqid = lc->lc_tx_seqid++;
tx_tail += sizeof(*lp);
tx_tail &= ((lc->lc_txq->lq_nentries * sizeof(*lp)) - 1);
err = hv_ldc_tx_set_qtail(lc->lc_id, tx_tail);
if (err != H_EOK) {
printf("%s: hv_ldc_tx_set_qtail: %d\n", __func__, err);
return;
}
lc->lc_state = LDC_SND_RTS;
}
void
ldc_send_rtr(struct ldc_conn *lc)
{
struct ldc_pkt *lp;
uint64_t tx_head, tx_tail, tx_state;
int err;
err = hv_ldc_tx_get_state(lc->lc_id, &tx_head, &tx_tail, &tx_state);
if (err != H_EOK || tx_state != LDC_CHANNEL_UP)
return;
lp = (struct ldc_pkt *)(lc->lc_txq->lq_va + tx_tail);
bzero(lp, sizeof(struct ldc_pkt));
lp->type = LDC_CTRL;
lp->stype = LDC_INFO;
lp->ctrl = LDC_RTR;
lp->env = LDC_MODE_UNRELIABLE;
lp->seqid = lc->lc_tx_seqid++;
tx_tail += sizeof(*lp);
tx_tail &= ((lc->lc_txq->lq_nentries * sizeof(*lp)) - 1);
err = hv_ldc_tx_set_qtail(lc->lc_id, tx_tail);
if (err != H_EOK)
printf("%s: hv_ldc_tx_set_qtail: %d\n", __func__, err);
lc->lc_state = LDC_SND_RTR;
}
void
ldc_send_rdx(struct ldc_conn *lc)
{
struct ldc_pkt *lp;
uint64_t tx_head, tx_tail, tx_state;
int err;
err = hv_ldc_tx_get_state(lc->lc_id, &tx_head, &tx_tail, &tx_state);
if (err != H_EOK || tx_state != LDC_CHANNEL_UP)
return;
lp = (struct ldc_pkt *)(lc->lc_txq->lq_va + tx_tail);
bzero(lp, sizeof(struct ldc_pkt));
lp->type = LDC_CTRL;
lp->stype = LDC_INFO;
lp->ctrl = LDC_RDX;
lp->env = LDC_MODE_UNRELIABLE;
lp->seqid = lc->lc_tx_seqid++;
tx_tail += sizeof(*lp);
tx_tail &= ((lc->lc_txq->lq_nentries * sizeof(*lp)) - 1);
err = hv_ldc_tx_set_qtail(lc->lc_id, tx_tail);
if (err != H_EOK)
printf("%s: hv_ldc_tx_set_qtail: %d\n", __func__, err);
lc->lc_state = LDC_SND_RDX;
}
void
ldc_reset(struct ldc_conn *lc)
{
int err;
DPRINTF(("Resetting connection\n"));
hv_ldc_tx_qconf(lc->lc_id, 0, 0);
hv_ldc_rx_qconf(lc->lc_id, 0, 0);
lc->lc_tx_state = lc->lc_rx_state = LDC_CHANNEL_DOWN;
err = hv_ldc_tx_qconf(lc->lc_id,
lc->lc_txq->lq_map->dm_segs[0].ds_addr, lc->lc_txq->lq_nentries);
if (err != H_EOK)
printf("%s: hv_ldc_tx_qconf %d\n", __func__, err);
err = hv_ldc_rx_qconf(lc->lc_id,
lc->lc_rxq->lq_map->dm_segs[0].ds_addr, lc->lc_rxq->lq_nentries);
if (err != H_EOK)
printf("%s: hv_ldc_rx_qconf %d\n", __func__, err);
lc->lc_tx_seqid = 0;
lc->lc_state = 0;
lc->lc_reset(lc);
}
struct ldc_queue *
ldc_queue_alloc(bus_dma_tag_t t, int nentries)
{
struct ldc_queue *lq;
bus_size_t size;
caddr_t va;
int nsegs;
lq = malloc(sizeof(struct ldc_queue), M_DEVBUF, M_NOWAIT);
if (lq == NULL)
return NULL;
size = roundup(nentries * sizeof(struct ldc_pkt), PAGE_SIZE);
if (bus_dmamap_create(t, size, 1, size, 0,
BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &lq->lq_map) != 0)
return (NULL);
if (bus_dmamem_alloc(t, size, PAGE_SIZE, 0, &lq->lq_seg, 1,
&nsegs, BUS_DMA_NOWAIT) != 0)
goto destroy;
if (bus_dmamem_map(t, &lq->lq_seg, 1, size, &va,
BUS_DMA_NOWAIT) != 0)
goto free;
if (bus_dmamap_load(t, lq->lq_map, va, size, NULL,
BUS_DMA_NOWAIT) != 0)
goto unmap;
lq->lq_va = va;
lq->lq_nentries = nentries;
return (lq);
unmap:
bus_dmamem_unmap(t, va, size);
free:
bus_dmamem_free(t, &lq->lq_seg, 1);
destroy:
bus_dmamap_destroy(t, lq->lq_map);
return (NULL);
}
void
ldc_queue_free(bus_dma_tag_t t, struct ldc_queue *lq)
{
bus_size_t size;
size = roundup(lq->lq_nentries * sizeof(struct ldc_pkt), PAGE_SIZE);
bus_dmamap_unload(t, lq->lq_map);
bus_dmamem_unmap(t, lq->lq_va, size);
bus_dmamem_free(t, &lq->lq_seg, 1);
bus_dmamap_destroy(t, lq->lq_map);
free(lq, M_DEVBUF);
}
struct ldc_map *
ldc_map_alloc(bus_dma_tag_t t, int nentries)
{
struct ldc_map *lm;
bus_size_t size;
caddr_t va;
int nsegs;
lm = malloc(sizeof(struct ldc_map), M_DEVBUF, M_NOWAIT);
if (lm == NULL)
return NULL;
size = roundup(nentries * sizeof(struct ldc_map_slot), PAGE_SIZE);
if (bus_dmamap_create(t, size, 1, size, 0,
BUS_DMA_NOWAIT | BUS_DMA_ALLOCNOW, &lm->lm_map) != 0)
return (NULL);
if (bus_dmamem_alloc(t, size, PAGE_SIZE, 0, &lm->lm_seg, 1,
&nsegs, BUS_DMA_NOWAIT) != 0)
goto destroy;
if (bus_dmamem_map(t, &lm->lm_seg, 1, size, &va,
BUS_DMA_NOWAIT) != 0)
goto free;
if (bus_dmamap_load(t, lm->lm_map, va, size, NULL,
BUS_DMA_NOWAIT) != 0)
goto unmap;
lm->lm_slot = (struct ldc_map_slot *)va;
lm->lm_nentries = nentries;
bzero(lm->lm_slot, nentries * sizeof(struct ldc_map_slot));
return (lm);
unmap:
bus_dmamem_unmap(t, va, size);
free:
bus_dmamem_free(t, &lm->lm_seg, 1);
destroy:
bus_dmamap_destroy(t, lm->lm_map);
return (NULL);
}
void
ldc_map_free(bus_dma_tag_t t, struct ldc_map *lm)
{
bus_size_t size;
size = lm->lm_nentries * sizeof(struct ldc_map_slot);
size = roundup(size, PAGE_SIZE);
bus_dmamap_unload(t, lm->lm_map);
bus_dmamem_unmap(t, (caddr_t)lm->lm_slot, size);
bus_dmamem_free(t, &lm->lm_seg, 1);
bus_dmamap_destroy(t, lm->lm_map);
free(lm, M_DEVBUF);
}
|