Commit | Line | Data |
---|---|---|
e53e97ce DM |
1 | /* ldc.c: Logical Domain Channel link-layer protocol driver. |
2 | * | |
b7c2a757 | 3 | * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net> |
e53e97ce DM |
4 | */ |
5 | ||
6 | #include <linux/kernel.h> | |
7 | #include <linux/module.h> | |
8 | #include <linux/slab.h> | |
9 | #include <linux/spinlock.h> | |
10 | #include <linux/delay.h> | |
11 | #include <linux/errno.h> | |
12 | #include <linux/string.h> | |
13 | #include <linux/scatterlist.h> | |
14 | #include <linux/interrupt.h> | |
15 | #include <linux/list.h> | |
16 | #include <linux/init.h> | |
17 | ||
18 | #include <asm/hypervisor.h> | |
19 | #include <asm/iommu.h> | |
20 | #include <asm/page.h> | |
21 | #include <asm/ldc.h> | |
22 | #include <asm/mdesc.h> | |
23 | ||
24 | #define DRV_MODULE_NAME "ldc" | |
25 | #define PFX DRV_MODULE_NAME ": " | |
b7c2a757 DM |
26 | #define DRV_MODULE_VERSION "1.1" |
27 | #define DRV_MODULE_RELDATE "July 22, 2008" | |
e53e97ce DM |
28 | |
29 | static char version[] __devinitdata = | |
30 | DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n"; | |
31 | #define LDC_PACKET_SIZE 64 | |
32 | ||
33 | /* Packet header layout for unreliable and reliable mode frames. | |
34 | * When in RAW mode, packets are simply straight 64-byte payloads | |
35 | * with no headers. | |
36 | */ | |
37 | struct ldc_packet { | |
38 | u8 type; | |
39 | #define LDC_CTRL 0x01 | |
40 | #define LDC_DATA 0x02 | |
41 | #define LDC_ERR 0x10 | |
42 | ||
43 | u8 stype; | |
44 | #define LDC_INFO 0x01 | |
45 | #define LDC_ACK 0x02 | |
46 | #define LDC_NACK 0x04 | |
47 | ||
48 | u8 ctrl; | |
49 | #define LDC_VERS 0x01 /* Link Version */ | |
50 | #define LDC_RTS 0x02 /* Request To Send */ | |
51 | #define LDC_RTR 0x03 /* Ready To Receive */ | |
52 | #define LDC_RDX 0x04 /* Ready for Data eXchange */ | |
53 | #define LDC_CTRL_MSK 0x0f | |
54 | ||
55 | u8 env; | |
56 | #define LDC_LEN 0x3f | |
57 | #define LDC_FRAG_MASK 0xc0 | |
58 | #define LDC_START 0x40 | |
59 | #define LDC_STOP 0x80 | |
60 | ||
61 | u32 seqid; | |
62 | ||
63 | union { | |
64 | u8 u_data[LDC_PACKET_SIZE - 8]; | |
65 | struct { | |
66 | u32 pad; | |
67 | u32 ackid; | |
68 | u8 r_data[LDC_PACKET_SIZE - 8 - 8]; | |
69 | } r; | |
70 | } u; | |
71 | }; | |
72 | ||
73 | struct ldc_version { | |
74 | u16 major; | |
75 | u16 minor; | |
76 | }; | |
77 | ||
78 | /* Ordered from largest major to lowest. */ | |
79 | static struct ldc_version ver_arr[] = { | |
80 | { .major = 1, .minor = 0 }, | |
81 | }; | |
82 | ||
83 | #define LDC_DEFAULT_MTU (4 * LDC_PACKET_SIZE) | |
84 | #define LDC_DEFAULT_NUM_ENTRIES (PAGE_SIZE / LDC_PACKET_SIZE) | |
85 | ||
86 | struct ldc_channel; | |
87 | ||
88 | struct ldc_mode_ops { | |
89 | int (*write)(struct ldc_channel *, const void *, unsigned int); | |
90 | int (*read)(struct ldc_channel *, void *, unsigned int); | |
91 | }; | |
92 | ||
93 | static const struct ldc_mode_ops raw_ops; | |
94 | static const struct ldc_mode_ops nonraw_ops; | |
95 | static const struct ldc_mode_ops stream_ops; | |
96 | ||
97 | int ldom_domaining_enabled; | |
98 | ||
99 | struct ldc_iommu { | |
100 | /* Protects arena alloc/free. */ | |
101 | spinlock_t lock; | |
102 | struct iommu_arena arena; | |
103 | struct ldc_mtable_entry *page_table; | |
104 | }; | |
105 | ||
106 | struct ldc_channel { | |
107 | /* Protects all operations that depend upon channel state. */ | |
108 | spinlock_t lock; | |
109 | ||
110 | unsigned long id; | |
111 | ||
112 | u8 *mssbuf; | |
113 | u32 mssbuf_len; | |
114 | u32 mssbuf_off; | |
115 | ||
116 | struct ldc_packet *tx_base; | |
117 | unsigned long tx_head; | |
118 | unsigned long tx_tail; | |
119 | unsigned long tx_num_entries; | |
120 | unsigned long tx_ra; | |
121 | ||
122 | unsigned long tx_acked; | |
123 | ||
124 | struct ldc_packet *rx_base; | |
125 | unsigned long rx_head; | |
126 | unsigned long rx_tail; | |
127 | unsigned long rx_num_entries; | |
128 | unsigned long rx_ra; | |
129 | ||
130 | u32 rcv_nxt; | |
131 | u32 snd_nxt; | |
132 | ||
133 | unsigned long chan_state; | |
134 | ||
135 | struct ldc_channel_config cfg; | |
136 | void *event_arg; | |
137 | ||
138 | const struct ldc_mode_ops *mops; | |
139 | ||
140 | struct ldc_iommu iommu; | |
141 | ||
142 | struct ldc_version ver; | |
143 | ||
144 | u8 hs_state; | |
145 | #define LDC_HS_CLOSED 0x00 | |
146 | #define LDC_HS_OPEN 0x01 | |
147 | #define LDC_HS_GOTVERS 0x02 | |
148 | #define LDC_HS_SENTRTR 0x03 | |
149 | #define LDC_HS_GOTRTR 0x04 | |
150 | #define LDC_HS_COMPLETE 0x10 | |
151 | ||
152 | u8 flags; | |
153 | #define LDC_FLAG_ALLOCED_QUEUES 0x01 | |
154 | #define LDC_FLAG_REGISTERED_QUEUES 0x02 | |
155 | #define LDC_FLAG_REGISTERED_IRQS 0x04 | |
156 | #define LDC_FLAG_RESET 0x10 | |
157 | ||
158 | u8 mss; | |
159 | u8 state; | |
160 | ||
133f09a1 DM |
161 | #define LDC_IRQ_NAME_MAX 32 |
162 | char rx_irq_name[LDC_IRQ_NAME_MAX]; | |
163 | char tx_irq_name[LDC_IRQ_NAME_MAX]; | |
164 | ||
e53e97ce DM |
165 | struct hlist_head mh_list; |
166 | ||
167 | struct hlist_node list; | |
168 | }; | |
169 | ||
170 | #define ldcdbg(TYPE, f, a...) \ | |
171 | do { if (lp->cfg.debug & LDC_DEBUG_##TYPE) \ | |
172 | printk(KERN_INFO PFX "ID[%lu] " f, lp->id, ## a); \ | |
173 | } while (0) | |
174 | ||
175 | static const char *state_to_str(u8 state) | |
176 | { | |
177 | switch (state) { | |
178 | case LDC_STATE_INVALID: | |
179 | return "INVALID"; | |
180 | case LDC_STATE_INIT: | |
181 | return "INIT"; | |
182 | case LDC_STATE_BOUND: | |
183 | return "BOUND"; | |
184 | case LDC_STATE_READY: | |
185 | return "READY"; | |
186 | case LDC_STATE_CONNECTED: | |
187 | return "CONNECTED"; | |
188 | default: | |
189 | return "<UNKNOWN>"; | |
190 | } | |
191 | } | |
192 | ||
193 | static void ldc_set_state(struct ldc_channel *lp, u8 state) | |
194 | { | |
195 | ldcdbg(STATE, "STATE (%s) --> (%s)\n", | |
196 | state_to_str(lp->state), | |
197 | state_to_str(state)); | |
198 | ||
199 | lp->state = state; | |
200 | } | |
201 | ||
202 | static unsigned long __advance(unsigned long off, unsigned long num_entries) | |
203 | { | |
204 | off += LDC_PACKET_SIZE; | |
205 | if (off == (num_entries * LDC_PACKET_SIZE)) | |
206 | off = 0; | |
207 | ||
208 | return off; | |
209 | } | |
210 | ||
211 | static unsigned long rx_advance(struct ldc_channel *lp, unsigned long off) | |
212 | { | |
213 | return __advance(off, lp->rx_num_entries); | |
214 | } | |
215 | ||
216 | static unsigned long tx_advance(struct ldc_channel *lp, unsigned long off) | |
217 | { | |
218 | return __advance(off, lp->tx_num_entries); | |
219 | } | |
220 | ||
221 | static struct ldc_packet *handshake_get_tx_packet(struct ldc_channel *lp, | |
222 | unsigned long *new_tail) | |
223 | { | |
224 | struct ldc_packet *p; | |
225 | unsigned long t; | |
226 | ||
227 | t = tx_advance(lp, lp->tx_tail); | |
228 | if (t == lp->tx_head) | |
229 | return NULL; | |
230 | ||
231 | *new_tail = t; | |
232 | ||
233 | p = lp->tx_base; | |
234 | return p + (lp->tx_tail / LDC_PACKET_SIZE); | |
235 | } | |
236 | ||
237 | /* When we are in reliable or stream mode, have to track the next packet | |
238 | * we haven't gotten an ACK for in the TX queue using tx_acked. We have | |
239 | * to be careful not to stomp over the queue past that point. During | |
240 | * the handshake, we don't have TX data packets pending in the queue | |
241 | * and that's why handshake_get_tx_packet() need not be mindful of | |
242 | * lp->tx_acked. | |
243 | */ | |
244 | static unsigned long head_for_data(struct ldc_channel *lp) | |
245 | { | |
cb481235 | 246 | if (lp->cfg.mode == LDC_MODE_STREAM) |
e53e97ce DM |
247 | return lp->tx_acked; |
248 | return lp->tx_head; | |
249 | } | |
250 | ||
251 | static int tx_has_space_for(struct ldc_channel *lp, unsigned int size) | |
252 | { | |
253 | unsigned long limit, tail, new_tail, diff; | |
254 | unsigned int mss; | |
255 | ||
256 | limit = head_for_data(lp); | |
257 | tail = lp->tx_tail; | |
258 | new_tail = tx_advance(lp, tail); | |
259 | if (new_tail == limit) | |
260 | return 0; | |
261 | ||
262 | if (limit > new_tail) | |
263 | diff = limit - new_tail; | |
264 | else | |
265 | diff = (limit + | |
266 | ((lp->tx_num_entries * LDC_PACKET_SIZE) - new_tail)); | |
267 | diff /= LDC_PACKET_SIZE; | |
268 | mss = lp->mss; | |
269 | ||
270 | if (diff * mss < size) | |
271 | return 0; | |
272 | ||
273 | return 1; | |
274 | } | |
275 | ||
276 | static struct ldc_packet *data_get_tx_packet(struct ldc_channel *lp, | |
277 | unsigned long *new_tail) | |
278 | { | |
279 | struct ldc_packet *p; | |
280 | unsigned long h, t; | |
281 | ||
282 | h = head_for_data(lp); | |
283 | t = tx_advance(lp, lp->tx_tail); | |
284 | if (t == h) | |
285 | return NULL; | |
286 | ||
287 | *new_tail = t; | |
288 | ||
289 | p = lp->tx_base; | |
290 | return p + (lp->tx_tail / LDC_PACKET_SIZE); | |
291 | } | |
292 | ||
293 | static int set_tx_tail(struct ldc_channel *lp, unsigned long tail) | |
294 | { | |
295 | unsigned long orig_tail = lp->tx_tail; | |
296 | int limit = 1000; | |
297 | ||
298 | lp->tx_tail = tail; | |
299 | while (limit-- > 0) { | |
300 | unsigned long err; | |
301 | ||
302 | err = sun4v_ldc_tx_set_qtail(lp->id, tail); | |
303 | if (!err) | |
304 | return 0; | |
305 | ||
306 | if (err != HV_EWOULDBLOCK) { | |
307 | lp->tx_tail = orig_tail; | |
308 | return -EINVAL; | |
309 | } | |
310 | udelay(1); | |
311 | } | |
312 | ||
313 | lp->tx_tail = orig_tail; | |
314 | return -EBUSY; | |
315 | } | |
316 | ||
317 | /* This just updates the head value in the hypervisor using | |
318 | * a polling loop with a timeout. The caller takes care of | |
319 | * upating software state representing the head change, if any. | |
320 | */ | |
321 | static int __set_rx_head(struct ldc_channel *lp, unsigned long head) | |
322 | { | |
323 | int limit = 1000; | |
324 | ||
325 | while (limit-- > 0) { | |
326 | unsigned long err; | |
327 | ||
328 | err = sun4v_ldc_rx_set_qhead(lp->id, head); | |
329 | if (!err) | |
330 | return 0; | |
331 | ||
332 | if (err != HV_EWOULDBLOCK) | |
333 | return -EINVAL; | |
334 | ||
335 | udelay(1); | |
336 | } | |
337 | ||
338 | return -EBUSY; | |
339 | } | |
340 | ||
341 | static int send_tx_packet(struct ldc_channel *lp, | |
342 | struct ldc_packet *p, | |
343 | unsigned long new_tail) | |
344 | { | |
345 | BUG_ON(p != (lp->tx_base + (lp->tx_tail / LDC_PACKET_SIZE))); | |
346 | ||
347 | return set_tx_tail(lp, new_tail); | |
348 | } | |
349 | ||
350 | static struct ldc_packet *handshake_compose_ctrl(struct ldc_channel *lp, | |
351 | u8 stype, u8 ctrl, | |
352 | void *data, int dlen, | |
353 | unsigned long *new_tail) | |
354 | { | |
355 | struct ldc_packet *p = handshake_get_tx_packet(lp, new_tail); | |
356 | ||
357 | if (p) { | |
358 | memset(p, 0, sizeof(*p)); | |
359 | p->type = LDC_CTRL; | |
360 | p->stype = stype; | |
361 | p->ctrl = ctrl; | |
362 | if (data) | |
363 | memcpy(p->u.u_data, data, dlen); | |
364 | } | |
365 | return p; | |
366 | } | |
367 | ||
368 | static int start_handshake(struct ldc_channel *lp) | |
369 | { | |
370 | struct ldc_packet *p; | |
371 | struct ldc_version *ver; | |
372 | unsigned long new_tail; | |
373 | ||
374 | ver = &ver_arr[0]; | |
375 | ||
376 | ldcdbg(HS, "SEND VER INFO maj[%u] min[%u]\n", | |
377 | ver->major, ver->minor); | |
378 | ||
379 | p = handshake_compose_ctrl(lp, LDC_INFO, LDC_VERS, | |
380 | ver, sizeof(*ver), &new_tail); | |
381 | if (p) { | |
382 | int err = send_tx_packet(lp, p, new_tail); | |
383 | if (!err) | |
384 | lp->flags &= ~LDC_FLAG_RESET; | |
385 | return err; | |
386 | } | |
387 | return -EBUSY; | |
388 | } | |
389 | ||
390 | static int send_version_nack(struct ldc_channel *lp, | |
391 | u16 major, u16 minor) | |
392 | { | |
393 | struct ldc_packet *p; | |
394 | struct ldc_version ver; | |
395 | unsigned long new_tail; | |
396 | ||
397 | ver.major = major; | |
398 | ver.minor = minor; | |
399 | ||
400 | p = handshake_compose_ctrl(lp, LDC_NACK, LDC_VERS, | |
401 | &ver, sizeof(ver), &new_tail); | |
402 | if (p) { | |
403 | ldcdbg(HS, "SEND VER NACK maj[%u] min[%u]\n", | |
404 | ver.major, ver.minor); | |
405 | ||
406 | return send_tx_packet(lp, p, new_tail); | |
407 | } | |
408 | return -EBUSY; | |
409 | } | |
410 | ||
411 | static int send_version_ack(struct ldc_channel *lp, | |
412 | struct ldc_version *vp) | |
413 | { | |
414 | struct ldc_packet *p; | |
415 | unsigned long new_tail; | |
416 | ||
417 | p = handshake_compose_ctrl(lp, LDC_ACK, LDC_VERS, | |
418 | vp, sizeof(*vp), &new_tail); | |
419 | if (p) { | |
420 | ldcdbg(HS, "SEND VER ACK maj[%u] min[%u]\n", | |
421 | vp->major, vp->minor); | |
422 | ||
423 | return send_tx_packet(lp, p, new_tail); | |
424 | } | |
425 | return -EBUSY; | |
426 | } | |
427 | ||
428 | static int send_rts(struct ldc_channel *lp) | |
429 | { | |
430 | struct ldc_packet *p; | |
431 | unsigned long new_tail; | |
432 | ||
433 | p = handshake_compose_ctrl(lp, LDC_INFO, LDC_RTS, NULL, 0, | |
434 | &new_tail); | |
435 | if (p) { | |
436 | p->env = lp->cfg.mode; | |
437 | p->seqid = 0; | |
438 | lp->rcv_nxt = 0; | |
439 | ||
440 | ldcdbg(HS, "SEND RTS env[0x%x] seqid[0x%x]\n", | |
441 | p->env, p->seqid); | |
442 | ||
443 | return send_tx_packet(lp, p, new_tail); | |
444 | } | |
445 | return -EBUSY; | |
446 | } | |
447 | ||
448 | static int send_rtr(struct ldc_channel *lp) | |
449 | { | |
450 | struct ldc_packet *p; | |
451 | unsigned long new_tail; | |
452 | ||
453 | p = handshake_compose_ctrl(lp, LDC_INFO, LDC_RTR, NULL, 0, | |
454 | &new_tail); | |
455 | if (p) { | |
456 | p->env = lp->cfg.mode; | |
457 | p->seqid = 0; | |
458 | ||
459 | ldcdbg(HS, "SEND RTR env[0x%x] seqid[0x%x]\n", | |
460 | p->env, p->seqid); | |
461 | ||
462 | return send_tx_packet(lp, p, new_tail); | |
463 | } | |
464 | return -EBUSY; | |
465 | } | |
466 | ||
467 | static int send_rdx(struct ldc_channel *lp) | |
468 | { | |
469 | struct ldc_packet *p; | |
470 | unsigned long new_tail; | |
471 | ||
472 | p = handshake_compose_ctrl(lp, LDC_INFO, LDC_RDX, NULL, 0, | |
473 | &new_tail); | |
474 | if (p) { | |
475 | p->env = 0; | |
476 | p->seqid = ++lp->snd_nxt; | |
477 | p->u.r.ackid = lp->rcv_nxt; | |
478 | ||
479 | ldcdbg(HS, "SEND RDX env[0x%x] seqid[0x%x] ackid[0x%x]\n", | |
480 | p->env, p->seqid, p->u.r.ackid); | |
481 | ||
482 | return send_tx_packet(lp, p, new_tail); | |
483 | } | |
484 | return -EBUSY; | |
485 | } | |
486 | ||
487 | static int send_data_nack(struct ldc_channel *lp, struct ldc_packet *data_pkt) | |
488 | { | |
489 | struct ldc_packet *p; | |
490 | unsigned long new_tail; | |
491 | int err; | |
492 | ||
493 | p = data_get_tx_packet(lp, &new_tail); | |
494 | if (!p) | |
495 | return -EBUSY; | |
496 | memset(p, 0, sizeof(*p)); | |
497 | p->type = data_pkt->type; | |
498 | p->stype = LDC_NACK; | |
499 | p->ctrl = data_pkt->ctrl & LDC_CTRL_MSK; | |
cb481235 | 500 | p->seqid = lp->snd_nxt + 1; |
e53e97ce DM |
501 | p->u.r.ackid = lp->rcv_nxt; |
502 | ||
503 | ldcdbg(HS, "SEND DATA NACK type[0x%x] ctl[0x%x] seq[0x%x] ack[0x%x]\n", | |
504 | p->type, p->ctrl, p->seqid, p->u.r.ackid); | |
505 | ||
506 | err = send_tx_packet(lp, p, new_tail); | |
507 | if (!err) | |
508 | lp->snd_nxt++; | |
509 | ||
510 | return err; | |
511 | } | |
512 | ||
513 | static int ldc_abort(struct ldc_channel *lp) | |
514 | { | |
515 | unsigned long hv_err; | |
516 | ||
517 | ldcdbg(STATE, "ABORT\n"); | |
518 | ||
519 | /* We report but do not act upon the hypervisor errors because | |
520 | * there really isn't much we can do if they fail at this point. | |
521 | */ | |
522 | hv_err = sun4v_ldc_tx_qconf(lp->id, lp->tx_ra, lp->tx_num_entries); | |
523 | if (hv_err) | |
524 | printk(KERN_ERR PFX "ldc_abort: " | |
525 | "sun4v_ldc_tx_qconf(%lx,%lx,%lx) failed, err=%lu\n", | |
526 | lp->id, lp->tx_ra, lp->tx_num_entries, hv_err); | |
527 | ||
528 | hv_err = sun4v_ldc_tx_get_state(lp->id, | |
529 | &lp->tx_head, | |
530 | &lp->tx_tail, | |
531 | &lp->chan_state); | |
532 | if (hv_err) | |
533 | printk(KERN_ERR PFX "ldc_abort: " | |
534 | "sun4v_ldc_tx_get_state(%lx,...) failed, err=%lu\n", | |
535 | lp->id, hv_err); | |
536 | ||
537 | hv_err = sun4v_ldc_rx_qconf(lp->id, lp->rx_ra, lp->rx_num_entries); | |
538 | if (hv_err) | |
539 | printk(KERN_ERR PFX "ldc_abort: " | |
540 | "sun4v_ldc_rx_qconf(%lx,%lx,%lx) failed, err=%lu\n", | |
541 | lp->id, lp->rx_ra, lp->rx_num_entries, hv_err); | |
542 | ||
543 | /* Refetch the RX queue state as well, because we could be invoked | |
544 | * here in the queue processing context. | |
545 | */ | |
546 | hv_err = sun4v_ldc_rx_get_state(lp->id, | |
547 | &lp->rx_head, | |
548 | &lp->rx_tail, | |
549 | &lp->chan_state); | |
550 | if (hv_err) | |
551 | printk(KERN_ERR PFX "ldc_abort: " | |
552 | "sun4v_ldc_rx_get_state(%lx,...) failed, err=%lu\n", | |
553 | lp->id, hv_err); | |
554 | ||
555 | return -ECONNRESET; | |
556 | } | |
557 | ||
558 | static struct ldc_version *find_by_major(u16 major) | |
559 | { | |
560 | struct ldc_version *ret = NULL; | |
561 | int i; | |
562 | ||
563 | for (i = 0; i < ARRAY_SIZE(ver_arr); i++) { | |
564 | struct ldc_version *v = &ver_arr[i]; | |
565 | if (v->major <= major) { | |
566 | ret = v; | |
567 | break; | |
568 | } | |
569 | } | |
570 | return ret; | |
571 | } | |
572 | ||
573 | static int process_ver_info(struct ldc_channel *lp, struct ldc_version *vp) | |
574 | { | |
575 | struct ldc_version *vap; | |
576 | int err; | |
577 | ||
578 | ldcdbg(HS, "GOT VERSION INFO major[%x] minor[%x]\n", | |
579 | vp->major, vp->minor); | |
580 | ||
581 | if (lp->hs_state == LDC_HS_GOTVERS) { | |
582 | lp->hs_state = LDC_HS_OPEN; | |
583 | memset(&lp->ver, 0, sizeof(lp->ver)); | |
584 | } | |
585 | ||
586 | vap = find_by_major(vp->major); | |
587 | if (!vap) { | |
588 | err = send_version_nack(lp, 0, 0); | |
589 | } else if (vap->major != vp->major) { | |
590 | err = send_version_nack(lp, vap->major, vap->minor); | |
591 | } else { | |
592 | struct ldc_version ver = *vp; | |
593 | if (ver.minor > vap->minor) | |
594 | ver.minor = vap->minor; | |
595 | err = send_version_ack(lp, &ver); | |
596 | if (!err) { | |
597 | lp->ver = ver; | |
598 | lp->hs_state = LDC_HS_GOTVERS; | |
599 | } | |
600 | } | |
601 | if (err) | |
602 | return ldc_abort(lp); | |
603 | ||
604 | return 0; | |
605 | } | |
606 | ||
607 | static int process_ver_ack(struct ldc_channel *lp, struct ldc_version *vp) | |
608 | { | |
609 | ldcdbg(HS, "GOT VERSION ACK major[%x] minor[%x]\n", | |
610 | vp->major, vp->minor); | |
611 | ||
612 | if (lp->hs_state == LDC_HS_GOTVERS) { | |
613 | if (lp->ver.major != vp->major || | |
614 | lp->ver.minor != vp->minor) | |
615 | return ldc_abort(lp); | |
616 | } else { | |
617 | lp->ver = *vp; | |
618 | lp->hs_state = LDC_HS_GOTVERS; | |
619 | } | |
620 | if (send_rts(lp)) | |
621 | return ldc_abort(lp); | |
622 | return 0; | |
623 | } | |
624 | ||
625 | static int process_ver_nack(struct ldc_channel *lp, struct ldc_version *vp) | |
626 | { | |
627 | struct ldc_version *vap; | |
b2c0805f SR |
628 | struct ldc_packet *p; |
629 | unsigned long new_tail; | |
e53e97ce | 630 | |
b2c0805f SR |
631 | if (vp->major == 0 && vp->minor == 0) |
632 | return ldc_abort(lp); | |
633 | ||
634 | vap = find_by_major(vp->major); | |
635 | if (!vap) | |
e53e97ce | 636 | return ldc_abort(lp); |
e53e97ce | 637 | |
b2c0805f | 638 | p = handshake_compose_ctrl(lp, LDC_INFO, LDC_VERS, |
e53e97ce DM |
639 | vap, sizeof(*vap), |
640 | &new_tail); | |
b2c0805f SR |
641 | if (!p) |
642 | return ldc_abort(lp); | |
643 | ||
644 | return send_tx_packet(lp, p, new_tail); | |
e53e97ce DM |
645 | } |
646 | ||
647 | static int process_version(struct ldc_channel *lp, | |
648 | struct ldc_packet *p) | |
649 | { | |
650 | struct ldc_version *vp; | |
651 | ||
652 | vp = (struct ldc_version *) p->u.u_data; | |
653 | ||
654 | switch (p->stype) { | |
655 | case LDC_INFO: | |
656 | return process_ver_info(lp, vp); | |
657 | ||
658 | case LDC_ACK: | |
659 | return process_ver_ack(lp, vp); | |
660 | ||
661 | case LDC_NACK: | |
662 | return process_ver_nack(lp, vp); | |
663 | ||
664 | default: | |
665 | return ldc_abort(lp); | |
666 | } | |
667 | } | |
668 | ||
669 | static int process_rts(struct ldc_channel *lp, | |
670 | struct ldc_packet *p) | |
671 | { | |
672 | ldcdbg(HS, "GOT RTS stype[%x] seqid[%x] env[%x]\n", | |
673 | p->stype, p->seqid, p->env); | |
674 | ||
675 | if (p->stype != LDC_INFO || | |
676 | lp->hs_state != LDC_HS_GOTVERS || | |
677 | p->env != lp->cfg.mode) | |
678 | return ldc_abort(lp); | |
679 | ||
680 | lp->snd_nxt = p->seqid; | |
681 | lp->rcv_nxt = p->seqid; | |
682 | lp->hs_state = LDC_HS_SENTRTR; | |
683 | if (send_rtr(lp)) | |
684 | return ldc_abort(lp); | |
685 | ||
686 | return 0; | |
687 | } | |
688 | ||
689 | static int process_rtr(struct ldc_channel *lp, | |
690 | struct ldc_packet *p) | |
691 | { | |
692 | ldcdbg(HS, "GOT RTR stype[%x] seqid[%x] env[%x]\n", | |
693 | p->stype, p->seqid, p->env); | |
694 | ||
695 | if (p->stype != LDC_INFO || | |
696 | p->env != lp->cfg.mode) | |
697 | return ldc_abort(lp); | |
698 | ||
699 | lp->snd_nxt = p->seqid; | |
700 | lp->hs_state = LDC_HS_COMPLETE; | |
701 | ldc_set_state(lp, LDC_STATE_CONNECTED); | |
702 | send_rdx(lp); | |
703 | ||
704 | return LDC_EVENT_UP; | |
705 | } | |
706 | ||
707 | static int rx_seq_ok(struct ldc_channel *lp, u32 seqid) | |
708 | { | |
709 | return lp->rcv_nxt + 1 == seqid; | |
710 | } | |
711 | ||
712 | static int process_rdx(struct ldc_channel *lp, | |
713 | struct ldc_packet *p) | |
714 | { | |
715 | ldcdbg(HS, "GOT RDX stype[%x] seqid[%x] env[%x] ackid[%x]\n", | |
716 | p->stype, p->seqid, p->env, p->u.r.ackid); | |
717 | ||
718 | if (p->stype != LDC_INFO || | |
719 | !(rx_seq_ok(lp, p->seqid))) | |
720 | return ldc_abort(lp); | |
721 | ||
722 | lp->rcv_nxt = p->seqid; | |
723 | ||
724 | lp->hs_state = LDC_HS_COMPLETE; | |
725 | ldc_set_state(lp, LDC_STATE_CONNECTED); | |
726 | ||
727 | return LDC_EVENT_UP; | |
728 | } | |
729 | ||
730 | static int process_control_frame(struct ldc_channel *lp, | |
731 | struct ldc_packet *p) | |
732 | { | |
733 | switch (p->ctrl) { | |
734 | case LDC_VERS: | |
735 | return process_version(lp, p); | |
736 | ||
737 | case LDC_RTS: | |
738 | return process_rts(lp, p); | |
739 | ||
740 | case LDC_RTR: | |
741 | return process_rtr(lp, p); | |
742 | ||
743 | case LDC_RDX: | |
744 | return process_rdx(lp, p); | |
745 | ||
746 | default: | |
747 | return ldc_abort(lp); | |
748 | } | |
749 | } | |
750 | ||
751 | static int process_error_frame(struct ldc_channel *lp, | |
752 | struct ldc_packet *p) | |
753 | { | |
754 | return ldc_abort(lp); | |
755 | } | |
756 | ||
757 | static int process_data_ack(struct ldc_channel *lp, | |
758 | struct ldc_packet *ack) | |
759 | { | |
760 | unsigned long head = lp->tx_acked; | |
761 | u32 ackid = ack->u.r.ackid; | |
762 | ||
763 | while (1) { | |
764 | struct ldc_packet *p = lp->tx_base + (head / LDC_PACKET_SIZE); | |
765 | ||
766 | head = tx_advance(lp, head); | |
767 | ||
768 | if (p->seqid == ackid) { | |
769 | lp->tx_acked = head; | |
770 | return 0; | |
771 | } | |
cb481235 | 772 | if (head == lp->tx_tail) |
e53e97ce DM |
773 | return ldc_abort(lp); |
774 | } | |
775 | ||
776 | return 0; | |
777 | } | |
778 | ||
779 | static void send_events(struct ldc_channel *lp, unsigned int event_mask) | |
780 | { | |
781 | if (event_mask & LDC_EVENT_RESET) | |
782 | lp->cfg.event(lp->event_arg, LDC_EVENT_RESET); | |
783 | if (event_mask & LDC_EVENT_UP) | |
784 | lp->cfg.event(lp->event_arg, LDC_EVENT_UP); | |
785 | if (event_mask & LDC_EVENT_DATA_READY) | |
786 | lp->cfg.event(lp->event_arg, LDC_EVENT_DATA_READY); | |
787 | } | |
788 | ||
789 | static irqreturn_t ldc_rx(int irq, void *dev_id) | |
790 | { | |
791 | struct ldc_channel *lp = dev_id; | |
792 | unsigned long orig_state, hv_err, flags; | |
793 | unsigned int event_mask; | |
794 | ||
795 | spin_lock_irqsave(&lp->lock, flags); | |
796 | ||
797 | orig_state = lp->chan_state; | |
798 | hv_err = sun4v_ldc_rx_get_state(lp->id, | |
799 | &lp->rx_head, | |
800 | &lp->rx_tail, | |
801 | &lp->chan_state); | |
802 | ||
803 | ldcdbg(RX, "RX state[0x%02lx:0x%02lx] head[0x%04lx] tail[0x%04lx]\n", | |
804 | orig_state, lp->chan_state, lp->rx_head, lp->rx_tail); | |
805 | ||
806 | event_mask = 0; | |
807 | ||
808 | if (lp->cfg.mode == LDC_MODE_RAW && | |
809 | lp->chan_state == LDC_CHANNEL_UP) { | |
810 | lp->hs_state = LDC_HS_COMPLETE; | |
811 | ldc_set_state(lp, LDC_STATE_CONNECTED); | |
812 | ||
813 | event_mask |= LDC_EVENT_UP; | |
814 | ||
815 | orig_state = lp->chan_state; | |
816 | } | |
817 | ||
818 | /* If we are in reset state, flush the RX queue and ignore | |
819 | * everything. | |
820 | */ | |
821 | if (lp->flags & LDC_FLAG_RESET) { | |
822 | (void) __set_rx_head(lp, lp->rx_tail); | |
823 | goto out; | |
824 | } | |
825 | ||
826 | /* Once we finish the handshake, we let the ldc_read() | |
827 | * paths do all of the control frame and state management. | |
828 | * Just trigger the callback. | |
829 | */ | |
830 | if (lp->hs_state == LDC_HS_COMPLETE) { | |
831 | handshake_complete: | |
832 | if (lp->chan_state != orig_state) { | |
833 | unsigned int event = LDC_EVENT_RESET; | |
834 | ||
835 | if (lp->chan_state == LDC_CHANNEL_UP) | |
836 | event = LDC_EVENT_UP; | |
837 | ||
838 | event_mask |= event; | |
839 | } | |
840 | if (lp->rx_head != lp->rx_tail) | |
841 | event_mask |= LDC_EVENT_DATA_READY; | |
842 | ||
843 | goto out; | |
844 | } | |
845 | ||
846 | if (lp->chan_state != orig_state) | |
847 | goto out; | |
848 | ||
849 | while (lp->rx_head != lp->rx_tail) { | |
850 | struct ldc_packet *p; | |
851 | unsigned long new; | |
852 | int err; | |
853 | ||
854 | p = lp->rx_base + (lp->rx_head / LDC_PACKET_SIZE); | |
855 | ||
856 | switch (p->type) { | |
857 | case LDC_CTRL: | |
858 | err = process_control_frame(lp, p); | |
859 | if (err > 0) | |
860 | event_mask |= err; | |
861 | break; | |
862 | ||
863 | case LDC_DATA: | |
864 | event_mask |= LDC_EVENT_DATA_READY; | |
865 | err = 0; | |
866 | break; | |
867 | ||
868 | case LDC_ERR: | |
869 | err = process_error_frame(lp, p); | |
870 | break; | |
871 | ||
872 | default: | |
873 | err = ldc_abort(lp); | |
874 | break; | |
875 | } | |
876 | ||
877 | if (err < 0) | |
878 | break; | |
879 | ||
880 | new = lp->rx_head; | |
881 | new += LDC_PACKET_SIZE; | |
882 | if (new == (lp->rx_num_entries * LDC_PACKET_SIZE)) | |
883 | new = 0; | |
884 | lp->rx_head = new; | |
885 | ||
886 | err = __set_rx_head(lp, new); | |
887 | if (err < 0) { | |
888 | (void) ldc_abort(lp); | |
889 | break; | |
890 | } | |
891 | if (lp->hs_state == LDC_HS_COMPLETE) | |
892 | goto handshake_complete; | |
893 | } | |
894 | ||
895 | out: | |
896 | spin_unlock_irqrestore(&lp->lock, flags); | |
897 | ||
898 | send_events(lp, event_mask); | |
899 | ||
900 | return IRQ_HANDLED; | |
901 | } | |
902 | ||
903 | static irqreturn_t ldc_tx(int irq, void *dev_id) | |
904 | { | |
905 | struct ldc_channel *lp = dev_id; | |
906 | unsigned long flags, hv_err, orig_state; | |
907 | unsigned int event_mask = 0; | |
908 | ||
909 | spin_lock_irqsave(&lp->lock, flags); | |
910 | ||
911 | orig_state = lp->chan_state; | |
912 | hv_err = sun4v_ldc_tx_get_state(lp->id, | |
913 | &lp->tx_head, | |
914 | &lp->tx_tail, | |
915 | &lp->chan_state); | |
916 | ||
917 | ldcdbg(TX, " TX state[0x%02lx:0x%02lx] head[0x%04lx] tail[0x%04lx]\n", | |
918 | orig_state, lp->chan_state, lp->tx_head, lp->tx_tail); | |
919 | ||
920 | if (lp->cfg.mode == LDC_MODE_RAW && | |
921 | lp->chan_state == LDC_CHANNEL_UP) { | |
922 | lp->hs_state = LDC_HS_COMPLETE; | |
923 | ldc_set_state(lp, LDC_STATE_CONNECTED); | |
924 | ||
925 | event_mask |= LDC_EVENT_UP; | |
926 | } | |
927 | ||
928 | spin_unlock_irqrestore(&lp->lock, flags); | |
929 | ||
930 | send_events(lp, event_mask); | |
931 | ||
932 | return IRQ_HANDLED; | |
933 | } | |
934 | ||
935 | /* XXX ldc_alloc() and ldc_free() needs to run under a mutex so | |
936 | * XXX that addition and removal from the ldc_channel_list has | |
937 | * XXX atomicity, otherwise the __ldc_channel_exists() check is | |
938 | * XXX totally pointless as another thread can slip into ldc_alloc() | |
939 | * XXX and add a channel with the same ID. There also needs to be | |
940 | * XXX a spinlock for ldc_channel_list. | |
941 | */ | |
942 | static HLIST_HEAD(ldc_channel_list); | |
943 | ||
944 | static int __ldc_channel_exists(unsigned long id) | |
945 | { | |
946 | struct ldc_channel *lp; | |
947 | struct hlist_node *n; | |
948 | ||
949 | hlist_for_each_entry(lp, n, &ldc_channel_list, list) { | |
950 | if (lp->id == id) | |
951 | return 1; | |
952 | } | |
953 | return 0; | |
954 | } | |
955 | ||
956 | static int alloc_queue(const char *name, unsigned long num_entries, | |
957 | struct ldc_packet **base, unsigned long *ra) | |
958 | { | |
959 | unsigned long size, order; | |
960 | void *q; | |
961 | ||
962 | size = num_entries * LDC_PACKET_SIZE; | |
963 | order = get_order(size); | |
964 | ||
965 | q = (void *) __get_free_pages(GFP_KERNEL, order); | |
966 | if (!q) { | |
967 | printk(KERN_ERR PFX "Alloc of %s queue failed with " | |
968 | "size=%lu order=%lu\n", name, size, order); | |
969 | return -ENOMEM; | |
970 | } | |
971 | ||
972 | memset(q, 0, PAGE_SIZE << order); | |
973 | ||
974 | *base = q; | |
975 | *ra = __pa(q); | |
976 | ||
977 | return 0; | |
978 | } | |
979 | ||
980 | static void free_queue(unsigned long num_entries, struct ldc_packet *q) | |
981 | { | |
982 | unsigned long size, order; | |
983 | ||
984 | if (!q) | |
985 | return; | |
986 | ||
987 | size = num_entries * LDC_PACKET_SIZE; | |
988 | order = get_order(size); | |
989 | ||
990 | free_pages((unsigned long)q, order); | |
991 | } | |
992 | ||
993 | /* XXX Make this configurable... XXX */ | |
994 | #define LDC_IOTABLE_SIZE (8 * 1024) | |
995 | ||
996 | static int ldc_iommu_init(struct ldc_channel *lp) | |
997 | { | |
998 | unsigned long sz, num_tsb_entries, tsbsize, order; | |
999 | struct ldc_iommu *iommu = &lp->iommu; | |
1000 | struct ldc_mtable_entry *table; | |
1001 | unsigned long hv_err; | |
1002 | int err; | |
1003 | ||
1004 | num_tsb_entries = LDC_IOTABLE_SIZE; | |
1005 | tsbsize = num_tsb_entries * sizeof(struct ldc_mtable_entry); | |
1006 | ||
1007 | spin_lock_init(&iommu->lock); | |
1008 | ||
1009 | sz = num_tsb_entries / 8; | |
1010 | sz = (sz + 7UL) & ~7UL; | |
1011 | iommu->arena.map = kzalloc(sz, GFP_KERNEL); | |
1012 | if (!iommu->arena.map) { | |
1013 | printk(KERN_ERR PFX "Alloc of arena map failed, sz=%lu\n", sz); | |
1014 | return -ENOMEM; | |
1015 | } | |
1016 | ||
1017 | iommu->arena.limit = num_tsb_entries; | |
1018 | ||
1019 | order = get_order(tsbsize); | |
1020 | ||
1021 | table = (struct ldc_mtable_entry *) | |
1022 | __get_free_pages(GFP_KERNEL, order); | |
1023 | err = -ENOMEM; | |
1024 | if (!table) { | |
1025 | printk(KERN_ERR PFX "Alloc of MTE table failed, " | |
1026 | "size=%lu order=%lu\n", tsbsize, order); | |
1027 | goto out_free_map; | |
1028 | } | |
1029 | ||
1030 | memset(table, 0, PAGE_SIZE << order); | |
1031 | ||
1032 | iommu->page_table = table; | |
1033 | ||
1034 | hv_err = sun4v_ldc_set_map_table(lp->id, __pa(table), | |
1035 | num_tsb_entries); | |
1036 | err = -EINVAL; | |
1037 | if (hv_err) | |
1038 | goto out_free_table; | |
1039 | ||
1040 | return 0; | |
1041 | ||
1042 | out_free_table: | |
1043 | free_pages((unsigned long) table, order); | |
1044 | iommu->page_table = NULL; | |
1045 | ||
1046 | out_free_map: | |
1047 | kfree(iommu->arena.map); | |
1048 | iommu->arena.map = NULL; | |
1049 | ||
1050 | return err; | |
1051 | } | |
1052 | ||
1053 | static void ldc_iommu_release(struct ldc_channel *lp) | |
1054 | { | |
1055 | struct ldc_iommu *iommu = &lp->iommu; | |
1056 | unsigned long num_tsb_entries, tsbsize, order; | |
1057 | ||
1058 | (void) sun4v_ldc_set_map_table(lp->id, 0, 0); | |
1059 | ||
1060 | num_tsb_entries = iommu->arena.limit; | |
1061 | tsbsize = num_tsb_entries * sizeof(struct ldc_mtable_entry); | |
1062 | order = get_order(tsbsize); | |
1063 | ||
1064 | free_pages((unsigned long) iommu->page_table, order); | |
1065 | iommu->page_table = NULL; | |
1066 | ||
1067 | kfree(iommu->arena.map); | |
1068 | iommu->arena.map = NULL; | |
1069 | } | |
1070 | ||
1071 | struct ldc_channel *ldc_alloc(unsigned long id, | |
1072 | const struct ldc_channel_config *cfgp, | |
1073 | void *event_arg) | |
1074 | { | |
1075 | struct ldc_channel *lp; | |
1076 | const struct ldc_mode_ops *mops; | |
1077 | unsigned long dummy1, dummy2, hv_err; | |
1078 | u8 mss, *mssbuf; | |
1079 | int err; | |
1080 | ||
1081 | err = -ENODEV; | |
1082 | if (!ldom_domaining_enabled) | |
1083 | goto out_err; | |
1084 | ||
1085 | err = -EINVAL; | |
1086 | if (!cfgp) | |
1087 | goto out_err; | |
1088 | ||
1089 | switch (cfgp->mode) { | |
1090 | case LDC_MODE_RAW: | |
1091 | mops = &raw_ops; | |
1092 | mss = LDC_PACKET_SIZE; | |
1093 | break; | |
1094 | ||
1095 | case LDC_MODE_UNRELIABLE: | |
1096 | mops = &nonraw_ops; | |
1097 | mss = LDC_PACKET_SIZE - 8; | |
1098 | break; | |
1099 | ||
e53e97ce DM |
1100 | case LDC_MODE_STREAM: |
1101 | mops = &stream_ops; | |
1102 | mss = LDC_PACKET_SIZE - 8 - 8; | |
1103 | break; | |
1104 | ||
1105 | default: | |
1106 | goto out_err; | |
1107 | } | |
1108 | ||
1109 | if (!cfgp->event || !event_arg || !cfgp->rx_irq || !cfgp->tx_irq) | |
1110 | goto out_err; | |
1111 | ||
1112 | hv_err = sun4v_ldc_tx_qinfo(id, &dummy1, &dummy2); | |
1113 | err = -ENODEV; | |
1114 | if (hv_err == HV_ECHANNEL) | |
1115 | goto out_err; | |
1116 | ||
1117 | err = -EEXIST; | |
1118 | if (__ldc_channel_exists(id)) | |
1119 | goto out_err; | |
1120 | ||
1121 | mssbuf = NULL; | |
1122 | ||
1123 | lp = kzalloc(sizeof(*lp), GFP_KERNEL); | |
1124 | err = -ENOMEM; | |
1125 | if (!lp) | |
1126 | goto out_err; | |
1127 | ||
1128 | spin_lock_init(&lp->lock); | |
1129 | ||
1130 | lp->id = id; | |
1131 | ||
1132 | err = ldc_iommu_init(lp); | |
1133 | if (err) | |
1134 | goto out_free_ldc; | |
1135 | ||
1136 | lp->mops = mops; | |
1137 | lp->mss = mss; | |
1138 | ||
1139 | lp->cfg = *cfgp; | |
1140 | if (!lp->cfg.mtu) | |
1141 | lp->cfg.mtu = LDC_DEFAULT_MTU; | |
1142 | ||
1143 | if (lp->cfg.mode == LDC_MODE_STREAM) { | |
1144 | mssbuf = kzalloc(lp->cfg.mtu, GFP_KERNEL); | |
1145 | if (!mssbuf) { | |
1146 | err = -ENOMEM; | |
1147 | goto out_free_iommu; | |
1148 | } | |
1149 | lp->mssbuf = mssbuf; | |
1150 | } | |
1151 | ||
1152 | lp->event_arg = event_arg; | |
1153 | ||
1154 | /* XXX allow setting via ldc_channel_config to override defaults | |
1155 | * XXX or use some formula based upon mtu | |
1156 | */ | |
1157 | lp->tx_num_entries = LDC_DEFAULT_NUM_ENTRIES; | |
1158 | lp->rx_num_entries = LDC_DEFAULT_NUM_ENTRIES; | |
1159 | ||
1160 | err = alloc_queue("TX", lp->tx_num_entries, | |
1161 | &lp->tx_base, &lp->tx_ra); | |
1162 | if (err) | |
1163 | goto out_free_mssbuf; | |
1164 | ||
1165 | err = alloc_queue("RX", lp->rx_num_entries, | |
1166 | &lp->rx_base, &lp->rx_ra); | |
1167 | if (err) | |
1168 | goto out_free_txq; | |
1169 | ||
1170 | lp->flags |= LDC_FLAG_ALLOCED_QUEUES; | |
1171 | ||
1172 | lp->hs_state = LDC_HS_CLOSED; | |
1173 | ldc_set_state(lp, LDC_STATE_INIT); | |
1174 | ||
1175 | INIT_HLIST_NODE(&lp->list); | |
1176 | hlist_add_head(&lp->list, &ldc_channel_list); | |
1177 | ||
1178 | INIT_HLIST_HEAD(&lp->mh_list); | |
1179 | ||
1180 | return lp; | |
1181 | ||
1182 | out_free_txq: | |
1183 | free_queue(lp->tx_num_entries, lp->tx_base); | |
1184 | ||
1185 | out_free_mssbuf: | |
ed247e12 | 1186 | kfree(mssbuf); |
e53e97ce DM |
1187 | |
1188 | out_free_iommu: | |
1189 | ldc_iommu_release(lp); | |
1190 | ||
1191 | out_free_ldc: | |
1192 | kfree(lp); | |
1193 | ||
1194 | out_err: | |
1195 | return ERR_PTR(err); | |
1196 | } | |
1197 | EXPORT_SYMBOL(ldc_alloc); | |
1198 | ||
1199 | void ldc_free(struct ldc_channel *lp) | |
1200 | { | |
1201 | if (lp->flags & LDC_FLAG_REGISTERED_IRQS) { | |
1202 | free_irq(lp->cfg.rx_irq, lp); | |
1203 | free_irq(lp->cfg.tx_irq, lp); | |
1204 | } | |
1205 | ||
1206 | if (lp->flags & LDC_FLAG_REGISTERED_QUEUES) { | |
1207 | sun4v_ldc_tx_qconf(lp->id, 0, 0); | |
1208 | sun4v_ldc_rx_qconf(lp->id, 0, 0); | |
1209 | lp->flags &= ~LDC_FLAG_REGISTERED_QUEUES; | |
1210 | } | |
1211 | if (lp->flags & LDC_FLAG_ALLOCED_QUEUES) { | |
1212 | free_queue(lp->tx_num_entries, lp->tx_base); | |
1213 | free_queue(lp->rx_num_entries, lp->rx_base); | |
1214 | lp->flags &= ~LDC_FLAG_ALLOCED_QUEUES; | |
1215 | } | |
1216 | ||
1217 | hlist_del(&lp->list); | |
1218 | ||
ed247e12 | 1219 | kfree(lp->mssbuf); |
e53e97ce DM |
1220 | |
1221 | ldc_iommu_release(lp); | |
1222 | ||
1223 | kfree(lp); | |
1224 | } | |
1225 | EXPORT_SYMBOL(ldc_free); | |
1226 | ||
1227 | /* Bind the channel. This registers the LDC queues with | |
1228 | * the hypervisor and puts the channel into a pseudo-listening | |
1229 | * state. This does not initiate a handshake, ldc_connect() does | |
1230 | * that. | |
1231 | */ | |
133f09a1 | 1232 | int ldc_bind(struct ldc_channel *lp, const char *name) |
e53e97ce DM |
1233 | { |
1234 | unsigned long hv_err, flags; | |
1235 | int err = -EINVAL; | |
1236 | ||
b7c2a757 DM |
1237 | if (!name || |
1238 | (lp->state != LDC_STATE_INIT)) | |
1239 | return -EINVAL; | |
e53e97ce | 1240 | |
133f09a1 DM |
1241 | snprintf(lp->rx_irq_name, LDC_IRQ_NAME_MAX, "%s RX", name); |
1242 | snprintf(lp->tx_irq_name, LDC_IRQ_NAME_MAX, "%s TX", name); | |
1243 | ||
e53e97ce DM |
1244 | err = request_irq(lp->cfg.rx_irq, ldc_rx, |
1245 | IRQF_SAMPLE_RANDOM | IRQF_SHARED, | |
133f09a1 | 1246 | lp->rx_irq_name, lp); |
e53e97ce | 1247 | if (err) |
b7c2a757 | 1248 | return err; |
e53e97ce DM |
1249 | |
1250 | err = request_irq(lp->cfg.tx_irq, ldc_tx, | |
1251 | IRQF_SAMPLE_RANDOM | IRQF_SHARED, | |
133f09a1 | 1252 | lp->tx_irq_name, lp); |
b7c2a757 DM |
1253 | if (err) { |
1254 | free_irq(lp->cfg.rx_irq, lp); | |
1255 | return err; | |
1256 | } | |
1257 | ||
e53e97ce | 1258 | |
b7c2a757 DM |
1259 | spin_lock_irqsave(&lp->lock, flags); |
1260 | ||
1261 | enable_irq(lp->cfg.rx_irq); | |
1262 | enable_irq(lp->cfg.tx_irq); | |
e53e97ce DM |
1263 | |
1264 | lp->flags |= LDC_FLAG_REGISTERED_IRQS; | |
1265 | ||
1266 | err = -ENODEV; | |
1267 | hv_err = sun4v_ldc_tx_qconf(lp->id, 0, 0); | |
1268 | if (hv_err) | |
b7c2a757 | 1269 | goto out_free_irqs; |
e53e97ce DM |
1270 | |
1271 | hv_err = sun4v_ldc_tx_qconf(lp->id, lp->tx_ra, lp->tx_num_entries); | |
1272 | if (hv_err) | |
b7c2a757 | 1273 | goto out_free_irqs; |
e53e97ce DM |
1274 | |
1275 | hv_err = sun4v_ldc_rx_qconf(lp->id, 0, 0); | |
1276 | if (hv_err) | |
1277 | goto out_unmap_tx; | |
1278 | ||
1279 | hv_err = sun4v_ldc_rx_qconf(lp->id, lp->rx_ra, lp->rx_num_entries); | |
1280 | if (hv_err) | |
1281 | goto out_unmap_tx; | |
1282 | ||
1283 | lp->flags |= LDC_FLAG_REGISTERED_QUEUES; | |
1284 | ||
1285 | hv_err = sun4v_ldc_tx_get_state(lp->id, | |
1286 | &lp->tx_head, | |
1287 | &lp->tx_tail, | |
1288 | &lp->chan_state); | |
1289 | err = -EBUSY; | |
1290 | if (hv_err) | |
1291 | goto out_unmap_rx; | |
1292 | ||
1293 | lp->tx_acked = lp->tx_head; | |
1294 | ||
1295 | lp->hs_state = LDC_HS_OPEN; | |
1296 | ldc_set_state(lp, LDC_STATE_BOUND); | |
1297 | ||
1298 | spin_unlock_irqrestore(&lp->lock, flags); | |
1299 | ||
1300 | return 0; | |
1301 | ||
1302 | out_unmap_rx: | |
1303 | lp->flags &= ~LDC_FLAG_REGISTERED_QUEUES; | |
1304 | sun4v_ldc_rx_qconf(lp->id, 0, 0); | |
1305 | ||
1306 | out_unmap_tx: | |
1307 | sun4v_ldc_tx_qconf(lp->id, 0, 0); | |
1308 | ||
b7c2a757 | 1309 | out_free_irqs: |
e53e97ce DM |
1310 | lp->flags &= ~LDC_FLAG_REGISTERED_IRQS; |
1311 | free_irq(lp->cfg.tx_irq, lp); | |
e53e97ce DM |
1312 | free_irq(lp->cfg.rx_irq, lp); |
1313 | ||
e53e97ce DM |
1314 | spin_unlock_irqrestore(&lp->lock, flags); |
1315 | ||
1316 | return err; | |
1317 | } | |
1318 | EXPORT_SYMBOL(ldc_bind); | |
1319 | ||
1320 | int ldc_connect(struct ldc_channel *lp) | |
1321 | { | |
1322 | unsigned long flags; | |
1323 | int err; | |
1324 | ||
1325 | if (lp->cfg.mode == LDC_MODE_RAW) | |
1326 | return -EINVAL; | |
1327 | ||
1328 | spin_lock_irqsave(&lp->lock, flags); | |
1329 | ||
1330 | if (!(lp->flags & LDC_FLAG_ALLOCED_QUEUES) || | |
1331 | !(lp->flags & LDC_FLAG_REGISTERED_QUEUES) || | |
1332 | lp->hs_state != LDC_HS_OPEN) | |
1333 | err = -EINVAL; | |
1334 | else | |
1335 | err = start_handshake(lp); | |
1336 | ||
1337 | spin_unlock_irqrestore(&lp->lock, flags); | |
1338 | ||
1339 | return err; | |
1340 | } | |
1341 | EXPORT_SYMBOL(ldc_connect); | |
1342 | ||
1343 | int ldc_disconnect(struct ldc_channel *lp) | |
1344 | { | |
1345 | unsigned long hv_err, flags; | |
1346 | int err; | |
1347 | ||
1348 | if (lp->cfg.mode == LDC_MODE_RAW) | |
1349 | return -EINVAL; | |
1350 | ||
1351 | if (!(lp->flags & LDC_FLAG_ALLOCED_QUEUES) || | |
1352 | !(lp->flags & LDC_FLAG_REGISTERED_QUEUES)) | |
1353 | return -EINVAL; | |
1354 | ||
1355 | spin_lock_irqsave(&lp->lock, flags); | |
1356 | ||
1357 | err = -ENODEV; | |
1358 | hv_err = sun4v_ldc_tx_qconf(lp->id, 0, 0); | |
1359 | if (hv_err) | |
1360 | goto out_err; | |
1361 | ||
1362 | hv_err = sun4v_ldc_tx_qconf(lp->id, lp->tx_ra, lp->tx_num_entries); | |
1363 | if (hv_err) | |
1364 | goto out_err; | |
1365 | ||
1366 | hv_err = sun4v_ldc_rx_qconf(lp->id, 0, 0); | |
1367 | if (hv_err) | |
1368 | goto out_err; | |
1369 | ||
1370 | hv_err = sun4v_ldc_rx_qconf(lp->id, lp->rx_ra, lp->rx_num_entries); | |
1371 | if (hv_err) | |
1372 | goto out_err; | |
1373 | ||
1374 | ldc_set_state(lp, LDC_STATE_BOUND); | |
1375 | lp->hs_state = LDC_HS_OPEN; | |
1376 | lp->flags |= LDC_FLAG_RESET; | |
1377 | ||
1378 | spin_unlock_irqrestore(&lp->lock, flags); | |
1379 | ||
1380 | return 0; | |
1381 | ||
1382 | out_err: | |
1383 | sun4v_ldc_tx_qconf(lp->id, 0, 0); | |
1384 | sun4v_ldc_rx_qconf(lp->id, 0, 0); | |
1385 | free_irq(lp->cfg.tx_irq, lp); | |
1386 | free_irq(lp->cfg.rx_irq, lp); | |
1387 | lp->flags &= ~(LDC_FLAG_REGISTERED_IRQS | | |
1388 | LDC_FLAG_REGISTERED_QUEUES); | |
1389 | ldc_set_state(lp, LDC_STATE_INIT); | |
1390 | ||
1391 | spin_unlock_irqrestore(&lp->lock, flags); | |
1392 | ||
1393 | return err; | |
1394 | } | |
1395 | EXPORT_SYMBOL(ldc_disconnect); | |
1396 | ||
1397 | int ldc_state(struct ldc_channel *lp) | |
1398 | { | |
1399 | return lp->state; | |
1400 | } | |
1401 | EXPORT_SYMBOL(ldc_state); | |
1402 | ||
1403 | static int write_raw(struct ldc_channel *lp, const void *buf, unsigned int size) | |
1404 | { | |
1405 | struct ldc_packet *p; | |
1406 | unsigned long new_tail; | |
1407 | int err; | |
1408 | ||
1409 | if (size > LDC_PACKET_SIZE) | |
1410 | return -EMSGSIZE; | |
1411 | ||
1412 | p = data_get_tx_packet(lp, &new_tail); | |
1413 | if (!p) | |
1414 | return -EAGAIN; | |
1415 | ||
1416 | memcpy(p, buf, size); | |
1417 | ||
1418 | err = send_tx_packet(lp, p, new_tail); | |
1419 | if (!err) | |
1420 | err = size; | |
1421 | ||
1422 | return err; | |
1423 | } | |
1424 | ||
1425 | static int read_raw(struct ldc_channel *lp, void *buf, unsigned int size) | |
1426 | { | |
1427 | struct ldc_packet *p; | |
1428 | unsigned long hv_err, new; | |
1429 | int err; | |
1430 | ||
1431 | if (size < LDC_PACKET_SIZE) | |
1432 | return -EINVAL; | |
1433 | ||
1434 | hv_err = sun4v_ldc_rx_get_state(lp->id, | |
1435 | &lp->rx_head, | |
1436 | &lp->rx_tail, | |
1437 | &lp->chan_state); | |
1438 | if (hv_err) | |
1439 | return ldc_abort(lp); | |
1440 | ||
1441 | if (lp->chan_state == LDC_CHANNEL_DOWN || | |
1442 | lp->chan_state == LDC_CHANNEL_RESETTING) | |
1443 | return -ECONNRESET; | |
1444 | ||
1445 | if (lp->rx_head == lp->rx_tail) | |
1446 | return 0; | |
1447 | ||
1448 | p = lp->rx_base + (lp->rx_head / LDC_PACKET_SIZE); | |
1449 | memcpy(buf, p, LDC_PACKET_SIZE); | |
1450 | ||
1451 | new = rx_advance(lp, lp->rx_head); | |
1452 | lp->rx_head = new; | |
1453 | ||
1454 | err = __set_rx_head(lp, new); | |
1455 | if (err < 0) | |
1456 | err = -ECONNRESET; | |
1457 | else | |
1458 | err = LDC_PACKET_SIZE; | |
1459 | ||
1460 | return err; | |
1461 | } | |
1462 | ||
1463 | static const struct ldc_mode_ops raw_ops = { | |
1464 | .write = write_raw, | |
1465 | .read = read_raw, | |
1466 | }; | |
1467 | ||
1468 | static int write_nonraw(struct ldc_channel *lp, const void *buf, | |
1469 | unsigned int size) | |
1470 | { | |
1471 | unsigned long hv_err, tail; | |
1472 | unsigned int copied; | |
1473 | u32 seq; | |
1474 | int err; | |
1475 | ||
1476 | hv_err = sun4v_ldc_tx_get_state(lp->id, &lp->tx_head, &lp->tx_tail, | |
1477 | &lp->chan_state); | |
1478 | if (unlikely(hv_err)) | |
1479 | return -EBUSY; | |
1480 | ||
1481 | if (unlikely(lp->chan_state != LDC_CHANNEL_UP)) | |
1482 | return ldc_abort(lp); | |
1483 | ||
1484 | if (!tx_has_space_for(lp, size)) | |
1485 | return -EAGAIN; | |
1486 | ||
1487 | seq = lp->snd_nxt; | |
1488 | copied = 0; | |
1489 | tail = lp->tx_tail; | |
1490 | while (copied < size) { | |
1491 | struct ldc_packet *p = lp->tx_base + (tail / LDC_PACKET_SIZE); | |
1492 | u8 *data = ((lp->cfg.mode == LDC_MODE_UNRELIABLE) ? | |
1493 | p->u.u_data : | |
1494 | p->u.r.r_data); | |
1495 | int data_len; | |
1496 | ||
1497 | p->type = LDC_DATA; | |
1498 | p->stype = LDC_INFO; | |
1499 | p->ctrl = 0; | |
1500 | ||
1501 | data_len = size - copied; | |
1502 | if (data_len > lp->mss) | |
1503 | data_len = lp->mss; | |
1504 | ||
1505 | BUG_ON(data_len > LDC_LEN); | |
1506 | ||
1507 | p->env = (data_len | | |
1508 | (copied == 0 ? LDC_START : 0) | | |
1509 | (data_len == size - copied ? LDC_STOP : 0)); | |
1510 | ||
1511 | p->seqid = ++seq; | |
1512 | ||
1513 | ldcdbg(DATA, "SENT DATA [%02x:%02x:%02x:%02x:%08x]\n", | |
1514 | p->type, | |
1515 | p->stype, | |
1516 | p->ctrl, | |
1517 | p->env, | |
1518 | p->seqid); | |
1519 | ||
1520 | memcpy(data, buf, data_len); | |
1521 | buf += data_len; | |
1522 | copied += data_len; | |
1523 | ||
1524 | tail = tx_advance(lp, tail); | |
1525 | } | |
1526 | ||
1527 | err = set_tx_tail(lp, tail); | |
1528 | if (!err) { | |
1529 | lp->snd_nxt = seq; | |
1530 | err = size; | |
1531 | } | |
1532 | ||
1533 | return err; | |
1534 | } | |
1535 | ||
1536 | static int rx_bad_seq(struct ldc_channel *lp, struct ldc_packet *p, | |
1537 | struct ldc_packet *first_frag) | |
1538 | { | |
1539 | int err; | |
1540 | ||
1541 | if (first_frag) | |
1542 | lp->rcv_nxt = first_frag->seqid - 1; | |
1543 | ||
1544 | err = send_data_nack(lp, p); | |
1545 | if (err) | |
1546 | return err; | |
1547 | ||
1548 | err = __set_rx_head(lp, lp->rx_tail); | |
1549 | if (err < 0) | |
1550 | return ldc_abort(lp); | |
1551 | ||
1552 | return 0; | |
1553 | } | |
1554 | ||
1555 | static int data_ack_nack(struct ldc_channel *lp, struct ldc_packet *p) | |
1556 | { | |
1557 | if (p->stype & LDC_ACK) { | |
1558 | int err = process_data_ack(lp, p); | |
1559 | if (err) | |
1560 | return err; | |
1561 | } | |
1562 | if (p->stype & LDC_NACK) | |
1563 | return ldc_abort(lp); | |
1564 | ||
1565 | return 0; | |
1566 | } | |
1567 | ||
1568 | static int rx_data_wait(struct ldc_channel *lp, unsigned long cur_head) | |
1569 | { | |
1570 | unsigned long dummy; | |
1571 | int limit = 1000; | |
1572 | ||
1573 | ldcdbg(DATA, "DATA WAIT cur_head[%lx] rx_head[%lx] rx_tail[%lx]\n", | |
1574 | cur_head, lp->rx_head, lp->rx_tail); | |
1575 | while (limit-- > 0) { | |
1576 | unsigned long hv_err; | |
1577 | ||
1578 | hv_err = sun4v_ldc_rx_get_state(lp->id, | |
1579 | &dummy, | |
1580 | &lp->rx_tail, | |
1581 | &lp->chan_state); | |
1582 | if (hv_err) | |
1583 | return ldc_abort(lp); | |
1584 | ||
e53e97ce DM |
1585 | if (lp->chan_state == LDC_CHANNEL_DOWN || |
1586 | lp->chan_state == LDC_CHANNEL_RESETTING) | |
1587 | return -ECONNRESET; | |
1588 | ||
1589 | if (cur_head != lp->rx_tail) { | |
cb481235 DM |
1590 | ldcdbg(DATA, "DATA WAIT DONE " |
1591 | "head[%lx] tail[%lx] chan_state[%lx]\n", | |
1592 | dummy, lp->rx_tail, lp->chan_state); | |
e53e97ce DM |
1593 | return 0; |
1594 | } | |
1595 | ||
1596 | udelay(1); | |
1597 | } | |
1598 | return -EAGAIN; | |
1599 | } | |
1600 | ||
1601 | static int rx_set_head(struct ldc_channel *lp, unsigned long head) | |
1602 | { | |
1603 | int err = __set_rx_head(lp, head); | |
1604 | ||
1605 | if (err < 0) | |
1606 | return ldc_abort(lp); | |
1607 | ||
1608 | lp->rx_head = head; | |
1609 | return 0; | |
1610 | } | |
1611 | ||
cb481235 DM |
1612 | static void send_data_ack(struct ldc_channel *lp) |
1613 | { | |
1614 | unsigned long new_tail; | |
1615 | struct ldc_packet *p; | |
1616 | ||
1617 | p = data_get_tx_packet(lp, &new_tail); | |
1618 | if (likely(p)) { | |
1619 | int err; | |
1620 | ||
1621 | memset(p, 0, sizeof(*p)); | |
1622 | p->type = LDC_DATA; | |
1623 | p->stype = LDC_ACK; | |
1624 | p->ctrl = 0; | |
1625 | p->seqid = lp->snd_nxt + 1; | |
1626 | p->u.r.ackid = lp->rcv_nxt; | |
1627 | ||
1628 | err = send_tx_packet(lp, p, new_tail); | |
1629 | if (!err) | |
1630 | lp->snd_nxt++; | |
1631 | } | |
1632 | } | |
1633 | ||
e53e97ce DM |
1634 | static int read_nonraw(struct ldc_channel *lp, void *buf, unsigned int size) |
1635 | { | |
1636 | struct ldc_packet *first_frag; | |
1637 | unsigned long hv_err, new; | |
1638 | int err, copied; | |
1639 | ||
1640 | hv_err = sun4v_ldc_rx_get_state(lp->id, | |
1641 | &lp->rx_head, | |
1642 | &lp->rx_tail, | |
1643 | &lp->chan_state); | |
1644 | if (hv_err) | |
1645 | return ldc_abort(lp); | |
1646 | ||
1647 | if (lp->chan_state == LDC_CHANNEL_DOWN || | |
1648 | lp->chan_state == LDC_CHANNEL_RESETTING) | |
1649 | return -ECONNRESET; | |
1650 | ||
1651 | if (lp->rx_head == lp->rx_tail) | |
1652 | return 0; | |
1653 | ||
1654 | first_frag = NULL; | |
1655 | copied = err = 0; | |
1656 | new = lp->rx_head; | |
1657 | while (1) { | |
1658 | struct ldc_packet *p; | |
1659 | int pkt_len; | |
1660 | ||
1661 | BUG_ON(new == lp->rx_tail); | |
1662 | p = lp->rx_base + (new / LDC_PACKET_SIZE); | |
1663 | ||
cb481235 | 1664 | ldcdbg(RX, "RX read pkt[%02x:%02x:%02x:%02x:%08x:%08x] " |
e53e97ce DM |
1665 | "rcv_nxt[%08x]\n", |
1666 | p->type, | |
1667 | p->stype, | |
1668 | p->ctrl, | |
1669 | p->env, | |
1670 | p->seqid, | |
cb481235 | 1671 | p->u.r.ackid, |
e53e97ce DM |
1672 | lp->rcv_nxt); |
1673 | ||
1674 | if (unlikely(!rx_seq_ok(lp, p->seqid))) { | |
1675 | err = rx_bad_seq(lp, p, first_frag); | |
1676 | copied = 0; | |
1677 | break; | |
1678 | } | |
1679 | ||
1680 | if (p->type & LDC_CTRL) { | |
1681 | err = process_control_frame(lp, p); | |
1682 | if (err < 0) | |
1683 | break; | |
1684 | err = 0; | |
1685 | } | |
1686 | ||
1687 | lp->rcv_nxt = p->seqid; | |
1688 | ||
1689 | if (!(p->type & LDC_DATA)) { | |
1690 | new = rx_advance(lp, new); | |
1691 | goto no_data; | |
1692 | } | |
1693 | if (p->stype & (LDC_ACK | LDC_NACK)) { | |
1694 | err = data_ack_nack(lp, p); | |
1695 | if (err) | |
1696 | break; | |
1697 | } | |
1698 | if (!(p->stype & LDC_INFO)) { | |
1699 | new = rx_advance(lp, new); | |
cb481235 DM |
1700 | err = rx_set_head(lp, new); |
1701 | if (err) | |
1702 | break; | |
e53e97ce DM |
1703 | goto no_data; |
1704 | } | |
1705 | ||
1706 | pkt_len = p->env & LDC_LEN; | |
1707 | ||
1708 | /* Every initial packet starts with the START bit set. | |
1709 | * | |
1710 | * Singleton packets will have both START+STOP set. | |
1711 | * | |
1712 | * Fragments will have START set in the first frame, STOP | |
1713 | * set in the last frame, and neither bit set in middle | |
1714 | * frames of the packet. | |
1715 | * | |
1716 | * Therefore if we are at the beginning of a packet and | |
1717 | * we don't see START, or we are in the middle of a fragmented | |
1718 | * packet and do see START, we are unsynchronized and should | |
1719 | * flush the RX queue. | |
1720 | */ | |
1721 | if ((first_frag == NULL && !(p->env & LDC_START)) || | |
1722 | (first_frag != NULL && (p->env & LDC_START))) { | |
1723 | if (!first_frag) | |
1724 | new = rx_advance(lp, new); | |
1725 | ||
1726 | err = rx_set_head(lp, new); | |
1727 | if (err) | |
1728 | break; | |
1729 | ||
1730 | if (!first_frag) | |
1731 | goto no_data; | |
1732 | } | |
1733 | if (!first_frag) | |
1734 | first_frag = p; | |
1735 | ||
1736 | if (pkt_len > size - copied) { | |
1737 | /* User didn't give us a big enough buffer, | |
1738 | * what to do? This is a pretty serious error. | |
1739 | * | |
1740 | * Since we haven't updated the RX ring head to | |
1741 | * consume any of the packets, signal the error | |
1742 | * to the user and just leave the RX ring alone. | |
1743 | * | |
1744 | * This seems the best behavior because this allows | |
1745 | * a user of the LDC layer to start with a small | |
1746 | * RX buffer for ldc_read() calls and use -EMSGSIZE | |
1747 | * as a cue to enlarge it's read buffer. | |
1748 | */ | |
1749 | err = -EMSGSIZE; | |
1750 | break; | |
1751 | } | |
1752 | ||
1753 | /* Ok, we are gonna eat this one. */ | |
1754 | new = rx_advance(lp, new); | |
1755 | ||
1756 | memcpy(buf, | |
1757 | (lp->cfg.mode == LDC_MODE_UNRELIABLE ? | |
1758 | p->u.u_data : p->u.r.r_data), pkt_len); | |
1759 | buf += pkt_len; | |
1760 | copied += pkt_len; | |
1761 | ||
1762 | if (p->env & LDC_STOP) | |
1763 | break; | |
1764 | ||
1765 | no_data: | |
1766 | if (new == lp->rx_tail) { | |
1767 | err = rx_data_wait(lp, new); | |
1768 | if (err) | |
1769 | break; | |
1770 | } | |
1771 | } | |
1772 | ||
1773 | if (!err) | |
1774 | err = rx_set_head(lp, new); | |
1775 | ||
1776 | if (err && first_frag) | |
1777 | lp->rcv_nxt = first_frag->seqid - 1; | |
1778 | ||
cb481235 | 1779 | if (!err) { |
e53e97ce | 1780 | err = copied; |
cb481235 DM |
1781 | if (err > 0 && lp->cfg.mode != LDC_MODE_UNRELIABLE) |
1782 | send_data_ack(lp); | |
1783 | } | |
e53e97ce DM |
1784 | |
1785 | return err; | |
1786 | } | |
1787 | ||
1788 | static const struct ldc_mode_ops nonraw_ops = { | |
1789 | .write = write_nonraw, | |
1790 | .read = read_nonraw, | |
1791 | }; | |
1792 | ||
1793 | static int write_stream(struct ldc_channel *lp, const void *buf, | |
1794 | unsigned int size) | |
1795 | { | |
1796 | if (size > lp->cfg.mtu) | |
1797 | size = lp->cfg.mtu; | |
1798 | return write_nonraw(lp, buf, size); | |
1799 | } | |
1800 | ||
1801 | static int read_stream(struct ldc_channel *lp, void *buf, unsigned int size) | |
1802 | { | |
1803 | if (!lp->mssbuf_len) { | |
cb481235 | 1804 | int err = read_nonraw(lp, lp->mssbuf, lp->cfg.mtu); |
e53e97ce DM |
1805 | if (err < 0) |
1806 | return err; | |
1807 | ||
1808 | lp->mssbuf_len = err; | |
1809 | lp->mssbuf_off = 0; | |
1810 | } | |
1811 | ||
1812 | if (size > lp->mssbuf_len) | |
1813 | size = lp->mssbuf_len; | |
1814 | memcpy(buf, lp->mssbuf + lp->mssbuf_off, size); | |
1815 | ||
1816 | lp->mssbuf_off += size; | |
1817 | lp->mssbuf_len -= size; | |
1818 | ||
1819 | return size; | |
1820 | } | |
1821 | ||
1822 | static const struct ldc_mode_ops stream_ops = { | |
1823 | .write = write_stream, | |
1824 | .read = read_stream, | |
1825 | }; | |
1826 | ||
1827 | int ldc_write(struct ldc_channel *lp, const void *buf, unsigned int size) | |
1828 | { | |
1829 | unsigned long flags; | |
1830 | int err; | |
1831 | ||
1832 | if (!buf) | |
1833 | return -EINVAL; | |
1834 | ||
1835 | if (!size) | |
1836 | return 0; | |
1837 | ||
1838 | spin_lock_irqsave(&lp->lock, flags); | |
1839 | ||
1840 | if (lp->hs_state != LDC_HS_COMPLETE) | |
1841 | err = -ENOTCONN; | |
1842 | else | |
1843 | err = lp->mops->write(lp, buf, size); | |
1844 | ||
1845 | spin_unlock_irqrestore(&lp->lock, flags); | |
1846 | ||
1847 | return err; | |
1848 | } | |
1849 | EXPORT_SYMBOL(ldc_write); | |
1850 | ||
1851 | int ldc_read(struct ldc_channel *lp, void *buf, unsigned int size) | |
1852 | { | |
1853 | unsigned long flags; | |
1854 | int err; | |
1855 | ||
1856 | if (!buf) | |
1857 | return -EINVAL; | |
1858 | ||
1859 | if (!size) | |
1860 | return 0; | |
1861 | ||
1862 | spin_lock_irqsave(&lp->lock, flags); | |
1863 | ||
1864 | if (lp->hs_state != LDC_HS_COMPLETE) | |
1865 | err = -ENOTCONN; | |
1866 | else | |
1867 | err = lp->mops->read(lp, buf, size); | |
1868 | ||
1869 | spin_unlock_irqrestore(&lp->lock, flags); | |
1870 | ||
1871 | return err; | |
1872 | } | |
1873 | EXPORT_SYMBOL(ldc_read); | |
1874 | ||
1875 | static long arena_alloc(struct ldc_iommu *iommu, unsigned long npages) | |
1876 | { | |
1877 | struct iommu_arena *arena = &iommu->arena; | |
1878 | unsigned long n, i, start, end, limit; | |
1879 | int pass; | |
1880 | ||
1881 | limit = arena->limit; | |
1882 | start = arena->hint; | |
1883 | pass = 0; | |
1884 | ||
1885 | again: | |
1886 | n = find_next_zero_bit(arena->map, limit, start); | |
1887 | end = n + npages; | |
1888 | if (unlikely(end >= limit)) { | |
1889 | if (likely(pass < 1)) { | |
1890 | limit = start; | |
1891 | start = 0; | |
1892 | pass++; | |
1893 | goto again; | |
1894 | } else { | |
1895 | /* Scanned the whole thing, give up. */ | |
1896 | return -1; | |
1897 | } | |
1898 | } | |
1899 | ||
1900 | for (i = n; i < end; i++) { | |
1901 | if (test_bit(i, arena->map)) { | |
1902 | start = i + 1; | |
1903 | goto again; | |
1904 | } | |
1905 | } | |
1906 | ||
1907 | for (i = n; i < end; i++) | |
1908 | __set_bit(i, arena->map); | |
1909 | ||
1910 | arena->hint = end; | |
1911 | ||
1912 | return n; | |
1913 | } | |
1914 | ||
1915 | #define COOKIE_PGSZ_CODE 0xf000000000000000ULL | |
1916 | #define COOKIE_PGSZ_CODE_SHIFT 60ULL | |
1917 | ||
1918 | static u64 pagesize_code(void) | |
1919 | { | |
1920 | switch (PAGE_SIZE) { | |
1921 | default: | |
1922 | case (8ULL * 1024ULL): | |
1923 | return 0; | |
1924 | case (64ULL * 1024ULL): | |
1925 | return 1; | |
1926 | case (512ULL * 1024ULL): | |
1927 | return 2; | |
1928 | case (4ULL * 1024ULL * 1024ULL): | |
1929 | return 3; | |
1930 | case (32ULL * 1024ULL * 1024ULL): | |
1931 | return 4; | |
1932 | case (256ULL * 1024ULL * 1024ULL): | |
1933 | return 5; | |
1934 | } | |
1935 | } | |
1936 | ||
1937 | static u64 make_cookie(u64 index, u64 pgsz_code, u64 page_offset) | |
1938 | { | |
1939 | return ((pgsz_code << COOKIE_PGSZ_CODE_SHIFT) | | |
1940 | (index << PAGE_SHIFT) | | |
1941 | page_offset); | |
1942 | } | |
1943 | ||
1944 | static u64 cookie_to_index(u64 cookie, unsigned long *shift) | |
1945 | { | |
1946 | u64 szcode = cookie >> COOKIE_PGSZ_CODE_SHIFT; | |
1947 | ||
1948 | cookie &= ~COOKIE_PGSZ_CODE; | |
1949 | ||
1950 | *shift = szcode * 3; | |
1951 | ||
1952 | return (cookie >> (13ULL + (szcode * 3ULL))); | |
1953 | } | |
1954 | ||
1955 | static struct ldc_mtable_entry *alloc_npages(struct ldc_iommu *iommu, | |
1956 | unsigned long npages) | |
1957 | { | |
1958 | long entry; | |
1959 | ||
1960 | entry = arena_alloc(iommu, npages); | |
1961 | if (unlikely(entry < 0)) | |
1962 | return NULL; | |
1963 | ||
1964 | return iommu->page_table + entry; | |
1965 | } | |
1966 | ||
1967 | static u64 perm_to_mte(unsigned int map_perm) | |
1968 | { | |
1969 | u64 mte_base; | |
1970 | ||
1971 | mte_base = pagesize_code(); | |
1972 | ||
1973 | if (map_perm & LDC_MAP_SHADOW) { | |
1974 | if (map_perm & LDC_MAP_R) | |
1975 | mte_base |= LDC_MTE_COPY_R; | |
1976 | if (map_perm & LDC_MAP_W) | |
1977 | mte_base |= LDC_MTE_COPY_W; | |
1978 | } | |
1979 | if (map_perm & LDC_MAP_DIRECT) { | |
1980 | if (map_perm & LDC_MAP_R) | |
1981 | mte_base |= LDC_MTE_READ; | |
1982 | if (map_perm & LDC_MAP_W) | |
1983 | mte_base |= LDC_MTE_WRITE; | |
1984 | if (map_perm & LDC_MAP_X) | |
1985 | mte_base |= LDC_MTE_EXEC; | |
1986 | } | |
1987 | if (map_perm & LDC_MAP_IO) { | |
1988 | if (map_perm & LDC_MAP_R) | |
1989 | mte_base |= LDC_MTE_IOMMU_R; | |
1990 | if (map_perm & LDC_MAP_W) | |
1991 | mte_base |= LDC_MTE_IOMMU_W; | |
1992 | } | |
1993 | ||
1994 | return mte_base; | |
1995 | } | |
1996 | ||
1997 | static int pages_in_region(unsigned long base, long len) | |
1998 | { | |
1999 | int count = 0; | |
2000 | ||
2001 | do { | |
2002 | unsigned long new = (base + PAGE_SIZE) & PAGE_MASK; | |
2003 | ||
2004 | len -= (new - base); | |
2005 | base = new; | |
2006 | count++; | |
2007 | } while (len > 0); | |
2008 | ||
2009 | return count; | |
2010 | } | |
2011 | ||
2012 | struct cookie_state { | |
2013 | struct ldc_mtable_entry *page_table; | |
2014 | struct ldc_trans_cookie *cookies; | |
2015 | u64 mte_base; | |
2016 | u64 prev_cookie; | |
2017 | u32 pte_idx; | |
2018 | u32 nc; | |
2019 | }; | |
2020 | ||
2021 | static void fill_cookies(struct cookie_state *sp, unsigned long pa, | |
2022 | unsigned long off, unsigned long len) | |
2023 | { | |
2024 | do { | |
2025 | unsigned long tlen, new = pa + PAGE_SIZE; | |
2026 | u64 this_cookie; | |
2027 | ||
2028 | sp->page_table[sp->pte_idx].mte = sp->mte_base | pa; | |
2029 | ||
2030 | tlen = PAGE_SIZE; | |
2031 | if (off) | |
2032 | tlen = PAGE_SIZE - off; | |
2033 | if (tlen > len) | |
2034 | tlen = len; | |
2035 | ||
2036 | this_cookie = make_cookie(sp->pte_idx, | |
2037 | pagesize_code(), off); | |
2038 | ||
2039 | off = 0; | |
2040 | ||
2041 | if (this_cookie == sp->prev_cookie) { | |
2042 | sp->cookies[sp->nc - 1].cookie_size += tlen; | |
2043 | } else { | |
2044 | sp->cookies[sp->nc].cookie_addr = this_cookie; | |
2045 | sp->cookies[sp->nc].cookie_size = tlen; | |
2046 | sp->nc++; | |
2047 | } | |
2048 | sp->prev_cookie = this_cookie + tlen; | |
2049 | ||
2050 | sp->pte_idx++; | |
2051 | ||
2052 | len -= tlen; | |
2053 | pa = new; | |
2054 | } while (len > 0); | |
2055 | } | |
2056 | ||
2057 | static int sg_count_one(struct scatterlist *sg) | |
2058 | { | |
58b053e4 | 2059 | unsigned long base = page_to_pfn(sg_page(sg)) << PAGE_SHIFT; |
e53e97ce DM |
2060 | long len = sg->length; |
2061 | ||
2062 | if ((sg->offset | len) & (8UL - 1)) | |
2063 | return -EFAULT; | |
2064 | ||
2065 | return pages_in_region(base + sg->offset, len); | |
2066 | } | |
2067 | ||
2068 | static int sg_count_pages(struct scatterlist *sg, int num_sg) | |
2069 | { | |
2070 | int count; | |
2071 | int i; | |
2072 | ||
2073 | count = 0; | |
2074 | for (i = 0; i < num_sg; i++) { | |
2075 | int err = sg_count_one(sg + i); | |
2076 | if (err < 0) | |
2077 | return err; | |
2078 | count += err; | |
2079 | } | |
2080 | ||
2081 | return count; | |
2082 | } | |
2083 | ||
2084 | int ldc_map_sg(struct ldc_channel *lp, | |
2085 | struct scatterlist *sg, int num_sg, | |
2086 | struct ldc_trans_cookie *cookies, int ncookies, | |
2087 | unsigned int map_perm) | |
2088 | { | |
2089 | unsigned long i, npages, flags; | |
2090 | struct ldc_mtable_entry *base; | |
2091 | struct cookie_state state; | |
2092 | struct ldc_iommu *iommu; | |
2093 | int err; | |
2094 | ||
2095 | if (map_perm & ~LDC_MAP_ALL) | |
2096 | return -EINVAL; | |
2097 | ||
2098 | err = sg_count_pages(sg, num_sg); | |
2099 | if (err < 0) | |
2100 | return err; | |
2101 | ||
2102 | npages = err; | |
2103 | if (err > ncookies) | |
2104 | return -EMSGSIZE; | |
2105 | ||
2106 | iommu = &lp->iommu; | |
2107 | ||
2108 | spin_lock_irqsave(&iommu->lock, flags); | |
2109 | base = alloc_npages(iommu, npages); | |
2110 | spin_unlock_irqrestore(&iommu->lock, flags); | |
2111 | ||
2112 | if (!base) | |
2113 | return -ENOMEM; | |
2114 | ||
2115 | state.page_table = iommu->page_table; | |
2116 | state.cookies = cookies; | |
2117 | state.mte_base = perm_to_mte(map_perm); | |
2118 | state.prev_cookie = ~(u64)0; | |
2119 | state.pte_idx = (base - iommu->page_table); | |
2120 | state.nc = 0; | |
2121 | ||
2122 | for (i = 0; i < num_sg; i++) | |
d91c5e88 | 2123 | fill_cookies(&state, page_to_pfn(sg_page(&sg[i])) << PAGE_SHIFT, |
e53e97ce DM |
2124 | sg[i].offset, sg[i].length); |
2125 | ||
2126 | return state.nc; | |
2127 | } | |
2128 | EXPORT_SYMBOL(ldc_map_sg); | |
2129 | ||
2130 | int ldc_map_single(struct ldc_channel *lp, | |
2131 | void *buf, unsigned int len, | |
2132 | struct ldc_trans_cookie *cookies, int ncookies, | |
2133 | unsigned int map_perm) | |
2134 | { | |
2135 | unsigned long npages, pa, flags; | |
2136 | struct ldc_mtable_entry *base; | |
2137 | struct cookie_state state; | |
2138 | struct ldc_iommu *iommu; | |
2139 | ||
2140 | if ((map_perm & ~LDC_MAP_ALL) || (ncookies < 1)) | |
2141 | return -EINVAL; | |
2142 | ||
2143 | pa = __pa(buf); | |
2144 | if ((pa | len) & (8UL - 1)) | |
2145 | return -EFAULT; | |
2146 | ||
2147 | npages = pages_in_region(pa, len); | |
2148 | ||
2149 | iommu = &lp->iommu; | |
2150 | ||
2151 | spin_lock_irqsave(&iommu->lock, flags); | |
2152 | base = alloc_npages(iommu, npages); | |
2153 | spin_unlock_irqrestore(&iommu->lock, flags); | |
2154 | ||
2155 | if (!base) | |
2156 | return -ENOMEM; | |
2157 | ||
2158 | state.page_table = iommu->page_table; | |
2159 | state.cookies = cookies; | |
2160 | state.mte_base = perm_to_mte(map_perm); | |
2161 | state.prev_cookie = ~(u64)0; | |
2162 | state.pte_idx = (base - iommu->page_table); | |
2163 | state.nc = 0; | |
2164 | fill_cookies(&state, (pa & PAGE_MASK), (pa & ~PAGE_MASK), len); | |
2165 | BUG_ON(state.nc != 1); | |
2166 | ||
2167 | return state.nc; | |
2168 | } | |
2169 | EXPORT_SYMBOL(ldc_map_single); | |
2170 | ||
2171 | static void free_npages(unsigned long id, struct ldc_iommu *iommu, | |
2172 | u64 cookie, u64 size) | |
2173 | { | |
2174 | struct iommu_arena *arena = &iommu->arena; | |
2175 | unsigned long i, shift, index, npages; | |
2176 | struct ldc_mtable_entry *base; | |
2177 | ||
2178 | npages = PAGE_ALIGN(((cookie & ~PAGE_MASK) + size)) >> PAGE_SHIFT; | |
2179 | index = cookie_to_index(cookie, &shift); | |
2180 | base = iommu->page_table + index; | |
2181 | ||
2182 | BUG_ON(index > arena->limit || | |
2183 | (index + npages) > arena->limit); | |
2184 | ||
2185 | for (i = 0; i < npages; i++) { | |
2186 | if (base->cookie) | |
2187 | sun4v_ldc_revoke(id, cookie + (i << shift), | |
2188 | base->cookie); | |
2189 | base->mte = 0; | |
2190 | __clear_bit(index + i, arena->map); | |
2191 | } | |
2192 | } | |
2193 | ||
2194 | void ldc_unmap(struct ldc_channel *lp, struct ldc_trans_cookie *cookies, | |
2195 | int ncookies) | |
2196 | { | |
2197 | struct ldc_iommu *iommu = &lp->iommu; | |
2198 | unsigned long flags; | |
2199 | int i; | |
2200 | ||
2201 | spin_lock_irqsave(&iommu->lock, flags); | |
2202 | for (i = 0; i < ncookies; i++) { | |
2203 | u64 addr = cookies[i].cookie_addr; | |
2204 | u64 size = cookies[i].cookie_size; | |
2205 | ||
2206 | free_npages(lp->id, iommu, addr, size); | |
2207 | } | |
2208 | spin_unlock_irqrestore(&iommu->lock, flags); | |
2209 | } | |
2210 | EXPORT_SYMBOL(ldc_unmap); | |
2211 | ||
2212 | int ldc_copy(struct ldc_channel *lp, int copy_dir, | |
2213 | void *buf, unsigned int len, unsigned long offset, | |
2214 | struct ldc_trans_cookie *cookies, int ncookies) | |
2215 | { | |
2216 | unsigned int orig_len; | |
2217 | unsigned long ra; | |
2218 | int i; | |
2219 | ||
2220 | if (copy_dir != LDC_COPY_IN && copy_dir != LDC_COPY_OUT) { | |
2221 | printk(KERN_ERR PFX "ldc_copy: ID[%lu] Bad copy_dir[%d]\n", | |
2222 | lp->id, copy_dir); | |
2223 | return -EINVAL; | |
2224 | } | |
2225 | ||
2226 | ra = __pa(buf); | |
2227 | if ((ra | len | offset) & (8UL - 1)) { | |
2228 | printk(KERN_ERR PFX "ldc_copy: ID[%lu] Unaligned buffer " | |
2229 | "ra[%lx] len[%x] offset[%lx]\n", | |
2230 | lp->id, ra, len, offset); | |
2231 | return -EFAULT; | |
2232 | } | |
2233 | ||
2234 | if (lp->hs_state != LDC_HS_COMPLETE || | |
2235 | (lp->flags & LDC_FLAG_RESET)) { | |
2236 | printk(KERN_ERR PFX "ldc_copy: ID[%lu] Link down hs_state[%x] " | |
2237 | "flags[%x]\n", lp->id, lp->hs_state, lp->flags); | |
2238 | return -ECONNRESET; | |
2239 | } | |
2240 | ||
2241 | orig_len = len; | |
2242 | for (i = 0; i < ncookies; i++) { | |
2243 | unsigned long cookie_raddr = cookies[i].cookie_addr; | |
2244 | unsigned long this_len = cookies[i].cookie_size; | |
2245 | unsigned long actual_len; | |
2246 | ||
2247 | if (unlikely(offset)) { | |
2248 | unsigned long this_off = offset; | |
2249 | ||
2250 | if (this_off > this_len) | |
2251 | this_off = this_len; | |
2252 | ||
2253 | offset -= this_off; | |
2254 | this_len -= this_off; | |
2255 | if (!this_len) | |
2256 | continue; | |
2257 | cookie_raddr += this_off; | |
2258 | } | |
2259 | ||
2260 | if (this_len > len) | |
2261 | this_len = len; | |
2262 | ||
2263 | while (1) { | |
2264 | unsigned long hv_err; | |
2265 | ||
2266 | hv_err = sun4v_ldc_copy(lp->id, copy_dir, | |
2267 | cookie_raddr, ra, | |
2268 | this_len, &actual_len); | |
2269 | if (unlikely(hv_err)) { | |
2270 | printk(KERN_ERR PFX "ldc_copy: ID[%lu] " | |
2271 | "HV error %lu\n", | |
2272 | lp->id, hv_err); | |
2273 | if (lp->hs_state != LDC_HS_COMPLETE || | |
2274 | (lp->flags & LDC_FLAG_RESET)) | |
2275 | return -ECONNRESET; | |
2276 | else | |
2277 | return -EFAULT; | |
2278 | } | |
2279 | ||
2280 | cookie_raddr += actual_len; | |
2281 | ra += actual_len; | |
2282 | len -= actual_len; | |
2283 | if (actual_len == this_len) | |
2284 | break; | |
2285 | ||
2286 | this_len -= actual_len; | |
2287 | } | |
2288 | ||
2289 | if (!len) | |
2290 | break; | |
2291 | } | |
2292 | ||
2293 | /* It is caller policy what to do about short copies. | |
2294 | * For example, a networking driver can declare the | |
2295 | * packet a runt and drop it. | |
2296 | */ | |
2297 | ||
2298 | return orig_len - len; | |
2299 | } | |
2300 | EXPORT_SYMBOL(ldc_copy); | |
2301 | ||
2302 | void *ldc_alloc_exp_dring(struct ldc_channel *lp, unsigned int len, | |
2303 | struct ldc_trans_cookie *cookies, int *ncookies, | |
2304 | unsigned int map_perm) | |
2305 | { | |
2306 | void *buf; | |
2307 | int err; | |
2308 | ||
2309 | if (len & (8UL - 1)) | |
2310 | return ERR_PTR(-EINVAL); | |
2311 | ||
2312 | buf = kzalloc(len, GFP_KERNEL); | |
2313 | if (!buf) | |
2314 | return ERR_PTR(-ENOMEM); | |
2315 | ||
2316 | err = ldc_map_single(lp, buf, len, cookies, *ncookies, map_perm); | |
2317 | if (err < 0) { | |
2318 | kfree(buf); | |
2319 | return ERR_PTR(err); | |
2320 | } | |
2321 | *ncookies = err; | |
2322 | ||
2323 | return buf; | |
2324 | } | |
2325 | EXPORT_SYMBOL(ldc_alloc_exp_dring); | |
2326 | ||
2327 | void ldc_free_exp_dring(struct ldc_channel *lp, void *buf, unsigned int len, | |
2328 | struct ldc_trans_cookie *cookies, int ncookies) | |
2329 | { | |
2330 | ldc_unmap(lp, cookies, ncookies); | |
2331 | kfree(buf); | |
2332 | } | |
2333 | EXPORT_SYMBOL(ldc_free_exp_dring); | |
2334 | ||
2335 | static int __init ldc_init(void) | |
2336 | { | |
e53e97ce | 2337 | unsigned long major, minor; |
43fdf274 | 2338 | struct mdesc_handle *hp; |
e53e97ce | 2339 | const u64 *v; |
6fab2600 | 2340 | int err; |
43fdf274 | 2341 | u64 mp; |
e53e97ce | 2342 | |
43fdf274 DM |
2343 | hp = mdesc_grab(); |
2344 | if (!hp) | |
e53e97ce DM |
2345 | return -ENODEV; |
2346 | ||
43fdf274 | 2347 | mp = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform"); |
6fab2600 | 2348 | err = -ENODEV; |
43fdf274 | 2349 | if (mp == MDESC_NODE_NULL) |
6fab2600 | 2350 | goto out; |
43fdf274 DM |
2351 | |
2352 | v = mdesc_get_property(hp, mp, "domaining-enabled", NULL); | |
e53e97ce | 2353 | if (!v) |
6fab2600 | 2354 | goto out; |
e53e97ce DM |
2355 | |
2356 | major = 1; | |
2357 | minor = 0; | |
2358 | if (sun4v_hvapi_register(HV_GRP_LDOM, major, &minor)) { | |
2359 | printk(KERN_INFO PFX "Could not register LDOM hvapi.\n"); | |
6fab2600 | 2360 | goto out; |
e53e97ce DM |
2361 | } |
2362 | ||
2363 | printk(KERN_INFO "%s", version); | |
2364 | ||
2365 | if (!*v) { | |
2366 | printk(KERN_INFO PFX "Domaining disabled.\n"); | |
6fab2600 | 2367 | goto out; |
e53e97ce DM |
2368 | } |
2369 | ldom_domaining_enabled = 1; | |
6fab2600 | 2370 | err = 0; |
e53e97ce | 2371 | |
6fab2600 DM |
2372 | out: |
2373 | mdesc_release(hp); | |
2374 | return err; | |
e53e97ce DM |
2375 | } |
2376 | ||
2377 | core_initcall(ldc_init); |