sparc: Fix bus type probing for ESP and LE devices.
[linux-2.6/mini2440.git] / arch / sparc / kernel / ldc.c
blob6ce5d2598a09f938596fa2c74072e6bdb2d0e7f7
1 /* ldc.c: Logical Domain Channel link-layer protocol driver.
3 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
4 */
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>
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>
24 #define DRV_MODULE_NAME "ldc"
25 #define PFX DRV_MODULE_NAME ": "
26 #define DRV_MODULE_VERSION "1.1"
27 #define DRV_MODULE_RELDATE "July 22, 2008"
29 static char version[] __devinitdata =
30 DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
31 #define LDC_PACKET_SIZE 64
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.
37 struct ldc_packet {
38 u8 type;
39 #define LDC_CTRL 0x01
40 #define LDC_DATA 0x02
41 #define LDC_ERR 0x10
43 u8 stype;
44 #define LDC_INFO 0x01
45 #define LDC_ACK 0x02
46 #define LDC_NACK 0x04
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
55 u8 env;
56 #define LDC_LEN 0x3f
57 #define LDC_FRAG_MASK 0xc0
58 #define LDC_START 0x40
59 #define LDC_STOP 0x80
61 u32 seqid;
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;
73 struct ldc_version {
74 u16 major;
75 u16 minor;
78 /* Ordered from largest major to lowest. */
79 static struct ldc_version ver_arr[] = {
80 { .major = 1, .minor = 0 },
83 #define LDC_DEFAULT_MTU (4 * LDC_PACKET_SIZE)
84 #define LDC_DEFAULT_NUM_ENTRIES (PAGE_SIZE / LDC_PACKET_SIZE)
86 struct ldc_channel;
88 struct ldc_mode_ops {
89 int (*write)(struct ldc_channel *, const void *, unsigned int);
90 int (*read)(struct ldc_channel *, void *, unsigned int);
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;
97 int ldom_domaining_enabled;
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;
106 struct ldc_channel {
107 /* Protects all operations that depend upon channel state. */
108 spinlock_t lock;
110 unsigned long id;
112 u8 *mssbuf;
113 u32 mssbuf_len;
114 u32 mssbuf_off;
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;
122 unsigned long tx_acked;
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;
130 u32 rcv_nxt;
131 u32 snd_nxt;
133 unsigned long chan_state;
135 struct ldc_channel_config cfg;
136 void *event_arg;
138 const struct ldc_mode_ops *mops;
140 struct ldc_iommu iommu;
142 struct ldc_version ver;
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
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
158 u8 mss;
159 u8 state;
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];
165 struct hlist_head mh_list;
167 struct hlist_node list;
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)
175 static const char *state_to_str(u8 state)
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>";
193 static void ldc_set_state(struct ldc_channel *lp, u8 state)
195 ldcdbg(STATE, "STATE (%s) --> (%s)\n",
196 state_to_str(lp->state),
197 state_to_str(state));
199 lp->state = state;
202 static unsigned long __advance(unsigned long off, unsigned long num_entries)
204 off += LDC_PACKET_SIZE;
205 if (off == (num_entries * LDC_PACKET_SIZE))
206 off = 0;
208 return off;
211 static unsigned long rx_advance(struct ldc_channel *lp, unsigned long off)
213 return __advance(off, lp->rx_num_entries);
216 static unsigned long tx_advance(struct ldc_channel *lp, unsigned long off)
218 return __advance(off, lp->tx_num_entries);
221 static struct ldc_packet *handshake_get_tx_packet(struct ldc_channel *lp,
222 unsigned long *new_tail)
224 struct ldc_packet *p;
225 unsigned long t;
227 t = tx_advance(lp, lp->tx_tail);
228 if (t == lp->tx_head)
229 return NULL;
231 *new_tail = t;
233 p = lp->tx_base;
234 return p + (lp->tx_tail / LDC_PACKET_SIZE);
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.
244 static unsigned long head_for_data(struct ldc_channel *lp)
246 if (lp->cfg.mode == LDC_MODE_STREAM)
247 return lp->tx_acked;
248 return lp->tx_head;
251 static int tx_has_space_for(struct ldc_channel *lp, unsigned int size)
253 unsigned long limit, tail, new_tail, diff;
254 unsigned int mss;
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;
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;
270 if (diff * mss < size)
271 return 0;
273 return 1;
276 static struct ldc_packet *data_get_tx_packet(struct ldc_channel *lp,
277 unsigned long *new_tail)
279 struct ldc_packet *p;
280 unsigned long h, t;
282 h = head_for_data(lp);
283 t = tx_advance(lp, lp->tx_tail);
284 if (t == h)
285 return NULL;
287 *new_tail = t;
289 p = lp->tx_base;
290 return p + (lp->tx_tail / LDC_PACKET_SIZE);
293 static int set_tx_tail(struct ldc_channel *lp, unsigned long tail)
295 unsigned long orig_tail = lp->tx_tail;
296 int limit = 1000;
298 lp->tx_tail = tail;
299 while (limit-- > 0) {
300 unsigned long err;
302 err = sun4v_ldc_tx_set_qtail(lp->id, tail);
303 if (!err)
304 return 0;
306 if (err != HV_EWOULDBLOCK) {
307 lp->tx_tail = orig_tail;
308 return -EINVAL;
310 udelay(1);
313 lp->tx_tail = orig_tail;
314 return -EBUSY;
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.
321 static int __set_rx_head(struct ldc_channel *lp, unsigned long head)
323 int limit = 1000;
325 while (limit-- > 0) {
326 unsigned long err;
328 err = sun4v_ldc_rx_set_qhead(lp->id, head);
329 if (!err)
330 return 0;
332 if (err != HV_EWOULDBLOCK)
333 return -EINVAL;
335 udelay(1);
338 return -EBUSY;
341 static int send_tx_packet(struct ldc_channel *lp,
342 struct ldc_packet *p,
343 unsigned long new_tail)
345 BUG_ON(p != (lp->tx_base + (lp->tx_tail / LDC_PACKET_SIZE)));
347 return set_tx_tail(lp, new_tail);
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)
355 struct ldc_packet *p = handshake_get_tx_packet(lp, new_tail);
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);
365 return p;
368 static int start_handshake(struct ldc_channel *lp)
370 struct ldc_packet *p;
371 struct ldc_version *ver;
372 unsigned long new_tail;
374 ver = &ver_arr[0];
376 ldcdbg(HS, "SEND VER INFO maj[%u] min[%u]\n",
377 ver->major, ver->minor);
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;
387 return -EBUSY;
390 static int send_version_nack(struct ldc_channel *lp,
391 u16 major, u16 minor)
393 struct ldc_packet *p;
394 struct ldc_version ver;
395 unsigned long new_tail;
397 ver.major = major;
398 ver.minor = minor;
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);
406 return send_tx_packet(lp, p, new_tail);
408 return -EBUSY;
411 static int send_version_ack(struct ldc_channel *lp,
412 struct ldc_version *vp)
414 struct ldc_packet *p;
415 unsigned long new_tail;
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);
423 return send_tx_packet(lp, p, new_tail);
425 return -EBUSY;
428 static int send_rts(struct ldc_channel *lp)
430 struct ldc_packet *p;
431 unsigned long new_tail;
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;
440 ldcdbg(HS, "SEND RTS env[0x%x] seqid[0x%x]\n",
441 p->env, p->seqid);
443 return send_tx_packet(lp, p, new_tail);
445 return -EBUSY;
448 static int send_rtr(struct ldc_channel *lp)
450 struct ldc_packet *p;
451 unsigned long new_tail;
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;
459 ldcdbg(HS, "SEND RTR env[0x%x] seqid[0x%x]\n",
460 p->env, p->seqid);
462 return send_tx_packet(lp, p, new_tail);
464 return -EBUSY;
467 static int send_rdx(struct ldc_channel *lp)
469 struct ldc_packet *p;
470 unsigned long new_tail;
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;
479 ldcdbg(HS, "SEND RDX env[0x%x] seqid[0x%x] ackid[0x%x]\n",
480 p->env, p->seqid, p->u.r.ackid);
482 return send_tx_packet(lp, p, new_tail);
484 return -EBUSY;
487 static int send_data_nack(struct ldc_channel *lp, struct ldc_packet *data_pkt)
489 struct ldc_packet *p;
490 unsigned long new_tail;
491 int err;
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;
500 p->seqid = lp->snd_nxt + 1;
501 p->u.r.ackid = lp->rcv_nxt;
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);
506 err = send_tx_packet(lp, p, new_tail);
507 if (!err)
508 lp->snd_nxt++;
510 return err;
513 static int ldc_abort(struct ldc_channel *lp)
515 unsigned long hv_err;
517 ldcdbg(STATE, "ABORT\n");
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.
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);
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);
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);
543 /* Refetch the RX queue state as well, because we could be invoked
544 * here in the queue processing context.
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);
555 return -ECONNRESET;
558 static struct ldc_version *find_by_major(u16 major)
560 struct ldc_version *ret = NULL;
561 int i;
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;
570 return ret;
573 static int process_ver_info(struct ldc_channel *lp, struct ldc_version *vp)
575 struct ldc_version *vap;
576 int err;
578 ldcdbg(HS, "GOT VERSION INFO major[%x] minor[%x]\n",
579 vp->major, vp->minor);
581 if (lp->hs_state == LDC_HS_GOTVERS) {
582 lp->hs_state = LDC_HS_OPEN;
583 memset(&lp->ver, 0, sizeof(lp->ver));
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;
601 if (err)
602 return ldc_abort(lp);
604 return 0;
607 static int process_ver_ack(struct ldc_channel *lp, struct ldc_version *vp)
609 ldcdbg(HS, "GOT VERSION ACK major[%x] minor[%x]\n",
610 vp->major, vp->minor);
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;
620 if (send_rts(lp))
621 return ldc_abort(lp);
622 return 0;
625 static int process_ver_nack(struct ldc_channel *lp, struct ldc_version *vp)
627 struct ldc_version *vap;
628 struct ldc_packet *p;
629 unsigned long new_tail;
631 if (vp->major == 0 && vp->minor == 0)
632 return ldc_abort(lp);
634 vap = find_by_major(vp->major);
635 if (!vap)
636 return ldc_abort(lp);
638 p = handshake_compose_ctrl(lp, LDC_INFO, LDC_VERS,
639 vap, sizeof(*vap),
640 &new_tail);
641 if (!p)
642 return ldc_abort(lp);
644 return send_tx_packet(lp, p, new_tail);
647 static int process_version(struct ldc_channel *lp,
648 struct ldc_packet *p)
650 struct ldc_version *vp;
652 vp = (struct ldc_version *) p->u.u_data;
654 switch (p->stype) {
655 case LDC_INFO:
656 return process_ver_info(lp, vp);
658 case LDC_ACK:
659 return process_ver_ack(lp, vp);
661 case LDC_NACK:
662 return process_ver_nack(lp, vp);
664 default:
665 return ldc_abort(lp);
669 static int process_rts(struct ldc_channel *lp,
670 struct ldc_packet *p)
672 ldcdbg(HS, "GOT RTS stype[%x] seqid[%x] env[%x]\n",
673 p->stype, p->seqid, p->env);
675 if (p->stype != LDC_INFO ||
676 lp->hs_state != LDC_HS_GOTVERS ||
677 p->env != lp->cfg.mode)
678 return ldc_abort(lp);
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);
686 return 0;
689 static int process_rtr(struct ldc_channel *lp,
690 struct ldc_packet *p)
692 ldcdbg(HS, "GOT RTR stype[%x] seqid[%x] env[%x]\n",
693 p->stype, p->seqid, p->env);
695 if (p->stype != LDC_INFO ||
696 p->env != lp->cfg.mode)
697 return ldc_abort(lp);
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);
704 return LDC_EVENT_UP;
707 static int rx_seq_ok(struct ldc_channel *lp, u32 seqid)
709 return lp->rcv_nxt + 1 == seqid;
712 static int process_rdx(struct ldc_channel *lp,
713 struct ldc_packet *p)
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);
718 if (p->stype != LDC_INFO ||
719 !(rx_seq_ok(lp, p->seqid)))
720 return ldc_abort(lp);
722 lp->rcv_nxt = p->seqid;
724 lp->hs_state = LDC_HS_COMPLETE;
725 ldc_set_state(lp, LDC_STATE_CONNECTED);
727 return LDC_EVENT_UP;
730 static int process_control_frame(struct ldc_channel *lp,
731 struct ldc_packet *p)
733 switch (p->ctrl) {
734 case LDC_VERS:
735 return process_version(lp, p);
737 case LDC_RTS:
738 return process_rts(lp, p);
740 case LDC_RTR:
741 return process_rtr(lp, p);
743 case LDC_RDX:
744 return process_rdx(lp, p);
746 default:
747 return ldc_abort(lp);
751 static int process_error_frame(struct ldc_channel *lp,
752 struct ldc_packet *p)
754 return ldc_abort(lp);
757 static int process_data_ack(struct ldc_channel *lp,
758 struct ldc_packet *ack)
760 unsigned long head = lp->tx_acked;
761 u32 ackid = ack->u.r.ackid;
763 while (1) {
764 struct ldc_packet *p = lp->tx_base + (head / LDC_PACKET_SIZE);
766 head = tx_advance(lp, head);
768 if (p->seqid == ackid) {
769 lp->tx_acked = head;
770 return 0;
772 if (head == lp->tx_tail)
773 return ldc_abort(lp);
776 return 0;
779 static void send_events(struct ldc_channel *lp, unsigned int event_mask)
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);
789 static irqreturn_t ldc_rx(int irq, void *dev_id)
791 struct ldc_channel *lp = dev_id;
792 unsigned long orig_state, hv_err, flags;
793 unsigned int event_mask;
795 spin_lock_irqsave(&lp->lock, flags);
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);
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);
806 event_mask = 0;
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);
813 event_mask |= LDC_EVENT_UP;
815 orig_state = lp->chan_state;
818 /* If we are in reset state, flush the RX queue and ignore
819 * everything.
821 if (lp->flags & LDC_FLAG_RESET) {
822 (void) __set_rx_head(lp, lp->rx_tail);
823 goto out;
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.
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;
835 if (lp->chan_state == LDC_CHANNEL_UP)
836 event = LDC_EVENT_UP;
838 event_mask |= event;
840 if (lp->rx_head != lp->rx_tail)
841 event_mask |= LDC_EVENT_DATA_READY;
843 goto out;
846 if (lp->chan_state != orig_state)
847 goto out;
849 while (lp->rx_head != lp->rx_tail) {
850 struct ldc_packet *p;
851 unsigned long new;
852 int err;
854 p = lp->rx_base + (lp->rx_head / LDC_PACKET_SIZE);
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;
863 case LDC_DATA:
864 event_mask |= LDC_EVENT_DATA_READY;
865 err = 0;
866 break;
868 case LDC_ERR:
869 err = process_error_frame(lp, p);
870 break;
872 default:
873 err = ldc_abort(lp);
874 break;
877 if (err < 0)
878 break;
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;
886 err = __set_rx_head(lp, new);
887 if (err < 0) {
888 (void) ldc_abort(lp);
889 break;
891 if (lp->hs_state == LDC_HS_COMPLETE)
892 goto handshake_complete;
895 out:
896 spin_unlock_irqrestore(&lp->lock, flags);
898 send_events(lp, event_mask);
900 return IRQ_HANDLED;
903 static irqreturn_t ldc_tx(int irq, void *dev_id)
905 struct ldc_channel *lp = dev_id;
906 unsigned long flags, hv_err, orig_state;
907 unsigned int event_mask = 0;
909 spin_lock_irqsave(&lp->lock, flags);
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);
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);
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);
925 event_mask |= LDC_EVENT_UP;
928 spin_unlock_irqrestore(&lp->lock, flags);
930 send_events(lp, event_mask);
932 return IRQ_HANDLED;
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.
942 static HLIST_HEAD(ldc_channel_list);
944 static int __ldc_channel_exists(unsigned long id)
946 struct ldc_channel *lp;
947 struct hlist_node *n;
949 hlist_for_each_entry(lp, n, &ldc_channel_list, list) {
950 if (lp->id == id)
951 return 1;
953 return 0;
956 static int alloc_queue(const char *name, unsigned long num_entries,
957 struct ldc_packet **base, unsigned long *ra)
959 unsigned long size, order;
960 void *q;
962 size = num_entries * LDC_PACKET_SIZE;
963 order = get_order(size);
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;
972 memset(q, 0, PAGE_SIZE << order);
974 *base = q;
975 *ra = __pa(q);
977 return 0;
980 static void free_queue(unsigned long num_entries, struct ldc_packet *q)
982 unsigned long size, order;
984 if (!q)
985 return;
987 size = num_entries * LDC_PACKET_SIZE;
988 order = get_order(size);
990 free_pages((unsigned long)q, order);
993 /* XXX Make this configurable... XXX */
994 #define LDC_IOTABLE_SIZE (8 * 1024)
996 static int ldc_iommu_init(struct ldc_channel *lp)
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;
1004 num_tsb_entries = LDC_IOTABLE_SIZE;
1005 tsbsize = num_tsb_entries * sizeof(struct ldc_mtable_entry);
1007 spin_lock_init(&iommu->lock);
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;
1017 iommu->arena.limit = num_tsb_entries;
1019 order = get_order(tsbsize);
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;
1030 memset(table, 0, PAGE_SIZE << order);
1032 iommu->page_table = table;
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;
1040 return 0;
1042 out_free_table:
1043 free_pages((unsigned long) table, order);
1044 iommu->page_table = NULL;
1046 out_free_map:
1047 kfree(iommu->arena.map);
1048 iommu->arena.map = NULL;
1050 return err;
1053 static void ldc_iommu_release(struct ldc_channel *lp)
1055 struct ldc_iommu *iommu = &lp->iommu;
1056 unsigned long num_tsb_entries, tsbsize, order;
1058 (void) sun4v_ldc_set_map_table(lp->id, 0, 0);
1060 num_tsb_entries = iommu->arena.limit;
1061 tsbsize = num_tsb_entries * sizeof(struct ldc_mtable_entry);
1062 order = get_order(tsbsize);
1064 free_pages((unsigned long) iommu->page_table, order);
1065 iommu->page_table = NULL;
1067 kfree(iommu->arena.map);
1068 iommu->arena.map = NULL;
1071 struct ldc_channel *ldc_alloc(unsigned long id,
1072 const struct ldc_channel_config *cfgp,
1073 void *event_arg)
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;
1081 err = -ENODEV;
1082 if (!ldom_domaining_enabled)
1083 goto out_err;
1085 err = -EINVAL;
1086 if (!cfgp)
1087 goto out_err;
1089 switch (cfgp->mode) {
1090 case LDC_MODE_RAW:
1091 mops = &raw_ops;
1092 mss = LDC_PACKET_SIZE;
1093 break;
1095 case LDC_MODE_UNRELIABLE:
1096 mops = &nonraw_ops;
1097 mss = LDC_PACKET_SIZE - 8;
1098 break;
1100 case LDC_MODE_STREAM:
1101 mops = &stream_ops;
1102 mss = LDC_PACKET_SIZE - 8 - 8;
1103 break;
1105 default:
1106 goto out_err;
1109 if (!cfgp->event || !event_arg || !cfgp->rx_irq || !cfgp->tx_irq)
1110 goto out_err;
1112 hv_err = sun4v_ldc_tx_qinfo(id, &dummy1, &dummy2);
1113 err = -ENODEV;
1114 if (hv_err == HV_ECHANNEL)
1115 goto out_err;
1117 err = -EEXIST;
1118 if (__ldc_channel_exists(id))
1119 goto out_err;
1121 mssbuf = NULL;
1123 lp = kzalloc(sizeof(*lp), GFP_KERNEL);
1124 err = -ENOMEM;
1125 if (!lp)
1126 goto out_err;
1128 spin_lock_init(&lp->lock);
1130 lp->id = id;
1132 err = ldc_iommu_init(lp);
1133 if (err)
1134 goto out_free_ldc;
1136 lp->mops = mops;
1137 lp->mss = mss;
1139 lp->cfg = *cfgp;
1140 if (!lp->cfg.mtu)
1141 lp->cfg.mtu = LDC_DEFAULT_MTU;
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;
1149 lp->mssbuf = mssbuf;
1152 lp->event_arg = event_arg;
1154 /* XXX allow setting via ldc_channel_config to override defaults
1155 * XXX or use some formula based upon mtu
1157 lp->tx_num_entries = LDC_DEFAULT_NUM_ENTRIES;
1158 lp->rx_num_entries = LDC_DEFAULT_NUM_ENTRIES;
1160 err = alloc_queue("TX", lp->tx_num_entries,
1161 &lp->tx_base, &lp->tx_ra);
1162 if (err)
1163 goto out_free_mssbuf;
1165 err = alloc_queue("RX", lp->rx_num_entries,
1166 &lp->rx_base, &lp->rx_ra);
1167 if (err)
1168 goto out_free_txq;
1170 lp->flags |= LDC_FLAG_ALLOCED_QUEUES;
1172 lp->hs_state = LDC_HS_CLOSED;
1173 ldc_set_state(lp, LDC_STATE_INIT);
1175 INIT_HLIST_NODE(&lp->list);
1176 hlist_add_head(&lp->list, &ldc_channel_list);
1178 INIT_HLIST_HEAD(&lp->mh_list);
1180 return lp;
1182 out_free_txq:
1183 free_queue(lp->tx_num_entries, lp->tx_base);
1185 out_free_mssbuf:
1186 if (mssbuf)
1187 kfree(mssbuf);
1189 out_free_iommu:
1190 ldc_iommu_release(lp);
1192 out_free_ldc:
1193 kfree(lp);
1195 out_err:
1196 return ERR_PTR(err);
1198 EXPORT_SYMBOL(ldc_alloc);
1200 void ldc_free(struct ldc_channel *lp)
1202 if (lp->flags & LDC_FLAG_REGISTERED_IRQS) {
1203 free_irq(lp->cfg.rx_irq, lp);
1204 free_irq(lp->cfg.tx_irq, lp);
1207 if (lp->flags & LDC_FLAG_REGISTERED_QUEUES) {
1208 sun4v_ldc_tx_qconf(lp->id, 0, 0);
1209 sun4v_ldc_rx_qconf(lp->id, 0, 0);
1210 lp->flags &= ~LDC_FLAG_REGISTERED_QUEUES;
1212 if (lp->flags & LDC_FLAG_ALLOCED_QUEUES) {
1213 free_queue(lp->tx_num_entries, lp->tx_base);
1214 free_queue(lp->rx_num_entries, lp->rx_base);
1215 lp->flags &= ~LDC_FLAG_ALLOCED_QUEUES;
1218 hlist_del(&lp->list);
1220 if (lp->mssbuf)
1221 kfree(lp->mssbuf);
1223 ldc_iommu_release(lp);
1225 kfree(lp);
1227 EXPORT_SYMBOL(ldc_free);
1229 /* Bind the channel. This registers the LDC queues with
1230 * the hypervisor and puts the channel into a pseudo-listening
1231 * state. This does not initiate a handshake, ldc_connect() does
1232 * that.
1234 int ldc_bind(struct ldc_channel *lp, const char *name)
1236 unsigned long hv_err, flags;
1237 int err = -EINVAL;
1239 if (!name ||
1240 (lp->state != LDC_STATE_INIT))
1241 return -EINVAL;
1243 snprintf(lp->rx_irq_name, LDC_IRQ_NAME_MAX, "%s RX", name);
1244 snprintf(lp->tx_irq_name, LDC_IRQ_NAME_MAX, "%s TX", name);
1246 err = request_irq(lp->cfg.rx_irq, ldc_rx,
1247 IRQF_SAMPLE_RANDOM | IRQF_SHARED,
1248 lp->rx_irq_name, lp);
1249 if (err)
1250 return err;
1252 err = request_irq(lp->cfg.tx_irq, ldc_tx,
1253 IRQF_SAMPLE_RANDOM | IRQF_SHARED,
1254 lp->tx_irq_name, lp);
1255 if (err) {
1256 free_irq(lp->cfg.rx_irq, lp);
1257 return err;
1261 spin_lock_irqsave(&lp->lock, flags);
1263 enable_irq(lp->cfg.rx_irq);
1264 enable_irq(lp->cfg.tx_irq);
1266 lp->flags |= LDC_FLAG_REGISTERED_IRQS;
1268 err = -ENODEV;
1269 hv_err = sun4v_ldc_tx_qconf(lp->id, 0, 0);
1270 if (hv_err)
1271 goto out_free_irqs;
1273 hv_err = sun4v_ldc_tx_qconf(lp->id, lp->tx_ra, lp->tx_num_entries);
1274 if (hv_err)
1275 goto out_free_irqs;
1277 hv_err = sun4v_ldc_rx_qconf(lp->id, 0, 0);
1278 if (hv_err)
1279 goto out_unmap_tx;
1281 hv_err = sun4v_ldc_rx_qconf(lp->id, lp->rx_ra, lp->rx_num_entries);
1282 if (hv_err)
1283 goto out_unmap_tx;
1285 lp->flags |= LDC_FLAG_REGISTERED_QUEUES;
1287 hv_err = sun4v_ldc_tx_get_state(lp->id,
1288 &lp->tx_head,
1289 &lp->tx_tail,
1290 &lp->chan_state);
1291 err = -EBUSY;
1292 if (hv_err)
1293 goto out_unmap_rx;
1295 lp->tx_acked = lp->tx_head;
1297 lp->hs_state = LDC_HS_OPEN;
1298 ldc_set_state(lp, LDC_STATE_BOUND);
1300 spin_unlock_irqrestore(&lp->lock, flags);
1302 return 0;
1304 out_unmap_rx:
1305 lp->flags &= ~LDC_FLAG_REGISTERED_QUEUES;
1306 sun4v_ldc_rx_qconf(lp->id, 0, 0);
1308 out_unmap_tx:
1309 sun4v_ldc_tx_qconf(lp->id, 0, 0);
1311 out_free_irqs:
1312 lp->flags &= ~LDC_FLAG_REGISTERED_IRQS;
1313 free_irq(lp->cfg.tx_irq, lp);
1314 free_irq(lp->cfg.rx_irq, lp);
1316 spin_unlock_irqrestore(&lp->lock, flags);
1318 return err;
1320 EXPORT_SYMBOL(ldc_bind);
1322 int ldc_connect(struct ldc_channel *lp)
1324 unsigned long flags;
1325 int err;
1327 if (lp->cfg.mode == LDC_MODE_RAW)
1328 return -EINVAL;
1330 spin_lock_irqsave(&lp->lock, flags);
1332 if (!(lp->flags & LDC_FLAG_ALLOCED_QUEUES) ||
1333 !(lp->flags & LDC_FLAG_REGISTERED_QUEUES) ||
1334 lp->hs_state != LDC_HS_OPEN)
1335 err = -EINVAL;
1336 else
1337 err = start_handshake(lp);
1339 spin_unlock_irqrestore(&lp->lock, flags);
1341 return err;
1343 EXPORT_SYMBOL(ldc_connect);
1345 int ldc_disconnect(struct ldc_channel *lp)
1347 unsigned long hv_err, flags;
1348 int err;
1350 if (lp->cfg.mode == LDC_MODE_RAW)
1351 return -EINVAL;
1353 if (!(lp->flags & LDC_FLAG_ALLOCED_QUEUES) ||
1354 !(lp->flags & LDC_FLAG_REGISTERED_QUEUES))
1355 return -EINVAL;
1357 spin_lock_irqsave(&lp->lock, flags);
1359 err = -ENODEV;
1360 hv_err = sun4v_ldc_tx_qconf(lp->id, 0, 0);
1361 if (hv_err)
1362 goto out_err;
1364 hv_err = sun4v_ldc_tx_qconf(lp->id, lp->tx_ra, lp->tx_num_entries);
1365 if (hv_err)
1366 goto out_err;
1368 hv_err = sun4v_ldc_rx_qconf(lp->id, 0, 0);
1369 if (hv_err)
1370 goto out_err;
1372 hv_err = sun4v_ldc_rx_qconf(lp->id, lp->rx_ra, lp->rx_num_entries);
1373 if (hv_err)
1374 goto out_err;
1376 ldc_set_state(lp, LDC_STATE_BOUND);
1377 lp->hs_state = LDC_HS_OPEN;
1378 lp->flags |= LDC_FLAG_RESET;
1380 spin_unlock_irqrestore(&lp->lock, flags);
1382 return 0;
1384 out_err:
1385 sun4v_ldc_tx_qconf(lp->id, 0, 0);
1386 sun4v_ldc_rx_qconf(lp->id, 0, 0);
1387 free_irq(lp->cfg.tx_irq, lp);
1388 free_irq(lp->cfg.rx_irq, lp);
1389 lp->flags &= ~(LDC_FLAG_REGISTERED_IRQS |
1390 LDC_FLAG_REGISTERED_QUEUES);
1391 ldc_set_state(lp, LDC_STATE_INIT);
1393 spin_unlock_irqrestore(&lp->lock, flags);
1395 return err;
1397 EXPORT_SYMBOL(ldc_disconnect);
1399 int ldc_state(struct ldc_channel *lp)
1401 return lp->state;
1403 EXPORT_SYMBOL(ldc_state);
1405 static int write_raw(struct ldc_channel *lp, const void *buf, unsigned int size)
1407 struct ldc_packet *p;
1408 unsigned long new_tail;
1409 int err;
1411 if (size > LDC_PACKET_SIZE)
1412 return -EMSGSIZE;
1414 p = data_get_tx_packet(lp, &new_tail);
1415 if (!p)
1416 return -EAGAIN;
1418 memcpy(p, buf, size);
1420 err = send_tx_packet(lp, p, new_tail);
1421 if (!err)
1422 err = size;
1424 return err;
1427 static int read_raw(struct ldc_channel *lp, void *buf, unsigned int size)
1429 struct ldc_packet *p;
1430 unsigned long hv_err, new;
1431 int err;
1433 if (size < LDC_PACKET_SIZE)
1434 return -EINVAL;
1436 hv_err = sun4v_ldc_rx_get_state(lp->id,
1437 &lp->rx_head,
1438 &lp->rx_tail,
1439 &lp->chan_state);
1440 if (hv_err)
1441 return ldc_abort(lp);
1443 if (lp->chan_state == LDC_CHANNEL_DOWN ||
1444 lp->chan_state == LDC_CHANNEL_RESETTING)
1445 return -ECONNRESET;
1447 if (lp->rx_head == lp->rx_tail)
1448 return 0;
1450 p = lp->rx_base + (lp->rx_head / LDC_PACKET_SIZE);
1451 memcpy(buf, p, LDC_PACKET_SIZE);
1453 new = rx_advance(lp, lp->rx_head);
1454 lp->rx_head = new;
1456 err = __set_rx_head(lp, new);
1457 if (err < 0)
1458 err = -ECONNRESET;
1459 else
1460 err = LDC_PACKET_SIZE;
1462 return err;
1465 static const struct ldc_mode_ops raw_ops = {
1466 .write = write_raw,
1467 .read = read_raw,
1470 static int write_nonraw(struct ldc_channel *lp, const void *buf,
1471 unsigned int size)
1473 unsigned long hv_err, tail;
1474 unsigned int copied;
1475 u32 seq;
1476 int err;
1478 hv_err = sun4v_ldc_tx_get_state(lp->id, &lp->tx_head, &lp->tx_tail,
1479 &lp->chan_state);
1480 if (unlikely(hv_err))
1481 return -EBUSY;
1483 if (unlikely(lp->chan_state != LDC_CHANNEL_UP))
1484 return ldc_abort(lp);
1486 if (!tx_has_space_for(lp, size))
1487 return -EAGAIN;
1489 seq = lp->snd_nxt;
1490 copied = 0;
1491 tail = lp->tx_tail;
1492 while (copied < size) {
1493 struct ldc_packet *p = lp->tx_base + (tail / LDC_PACKET_SIZE);
1494 u8 *data = ((lp->cfg.mode == LDC_MODE_UNRELIABLE) ?
1495 p->u.u_data :
1496 p->u.r.r_data);
1497 int data_len;
1499 p->type = LDC_DATA;
1500 p->stype = LDC_INFO;
1501 p->ctrl = 0;
1503 data_len = size - copied;
1504 if (data_len > lp->mss)
1505 data_len = lp->mss;
1507 BUG_ON(data_len > LDC_LEN);
1509 p->env = (data_len |
1510 (copied == 0 ? LDC_START : 0) |
1511 (data_len == size - copied ? LDC_STOP : 0));
1513 p->seqid = ++seq;
1515 ldcdbg(DATA, "SENT DATA [%02x:%02x:%02x:%02x:%08x]\n",
1516 p->type,
1517 p->stype,
1518 p->ctrl,
1519 p->env,
1520 p->seqid);
1522 memcpy(data, buf, data_len);
1523 buf += data_len;
1524 copied += data_len;
1526 tail = tx_advance(lp, tail);
1529 err = set_tx_tail(lp, tail);
1530 if (!err) {
1531 lp->snd_nxt = seq;
1532 err = size;
1535 return err;
1538 static int rx_bad_seq(struct ldc_channel *lp, struct ldc_packet *p,
1539 struct ldc_packet *first_frag)
1541 int err;
1543 if (first_frag)
1544 lp->rcv_nxt = first_frag->seqid - 1;
1546 err = send_data_nack(lp, p);
1547 if (err)
1548 return err;
1550 err = __set_rx_head(lp, lp->rx_tail);
1551 if (err < 0)
1552 return ldc_abort(lp);
1554 return 0;
1557 static int data_ack_nack(struct ldc_channel *lp, struct ldc_packet *p)
1559 if (p->stype & LDC_ACK) {
1560 int err = process_data_ack(lp, p);
1561 if (err)
1562 return err;
1564 if (p->stype & LDC_NACK)
1565 return ldc_abort(lp);
1567 return 0;
1570 static int rx_data_wait(struct ldc_channel *lp, unsigned long cur_head)
1572 unsigned long dummy;
1573 int limit = 1000;
1575 ldcdbg(DATA, "DATA WAIT cur_head[%lx] rx_head[%lx] rx_tail[%lx]\n",
1576 cur_head, lp->rx_head, lp->rx_tail);
1577 while (limit-- > 0) {
1578 unsigned long hv_err;
1580 hv_err = sun4v_ldc_rx_get_state(lp->id,
1581 &dummy,
1582 &lp->rx_tail,
1583 &lp->chan_state);
1584 if (hv_err)
1585 return ldc_abort(lp);
1587 if (lp->chan_state == LDC_CHANNEL_DOWN ||
1588 lp->chan_state == LDC_CHANNEL_RESETTING)
1589 return -ECONNRESET;
1591 if (cur_head != lp->rx_tail) {
1592 ldcdbg(DATA, "DATA WAIT DONE "
1593 "head[%lx] tail[%lx] chan_state[%lx]\n",
1594 dummy, lp->rx_tail, lp->chan_state);
1595 return 0;
1598 udelay(1);
1600 return -EAGAIN;
1603 static int rx_set_head(struct ldc_channel *lp, unsigned long head)
1605 int err = __set_rx_head(lp, head);
1607 if (err < 0)
1608 return ldc_abort(lp);
1610 lp->rx_head = head;
1611 return 0;
1614 static void send_data_ack(struct ldc_channel *lp)
1616 unsigned long new_tail;
1617 struct ldc_packet *p;
1619 p = data_get_tx_packet(lp, &new_tail);
1620 if (likely(p)) {
1621 int err;
1623 memset(p, 0, sizeof(*p));
1624 p->type = LDC_DATA;
1625 p->stype = LDC_ACK;
1626 p->ctrl = 0;
1627 p->seqid = lp->snd_nxt + 1;
1628 p->u.r.ackid = lp->rcv_nxt;
1630 err = send_tx_packet(lp, p, new_tail);
1631 if (!err)
1632 lp->snd_nxt++;
1636 static int read_nonraw(struct ldc_channel *lp, void *buf, unsigned int size)
1638 struct ldc_packet *first_frag;
1639 unsigned long hv_err, new;
1640 int err, copied;
1642 hv_err = sun4v_ldc_rx_get_state(lp->id,
1643 &lp->rx_head,
1644 &lp->rx_tail,
1645 &lp->chan_state);
1646 if (hv_err)
1647 return ldc_abort(lp);
1649 if (lp->chan_state == LDC_CHANNEL_DOWN ||
1650 lp->chan_state == LDC_CHANNEL_RESETTING)
1651 return -ECONNRESET;
1653 if (lp->rx_head == lp->rx_tail)
1654 return 0;
1656 first_frag = NULL;
1657 copied = err = 0;
1658 new = lp->rx_head;
1659 while (1) {
1660 struct ldc_packet *p;
1661 int pkt_len;
1663 BUG_ON(new == lp->rx_tail);
1664 p = lp->rx_base + (new / LDC_PACKET_SIZE);
1666 ldcdbg(RX, "RX read pkt[%02x:%02x:%02x:%02x:%08x:%08x] "
1667 "rcv_nxt[%08x]\n",
1668 p->type,
1669 p->stype,
1670 p->ctrl,
1671 p->env,
1672 p->seqid,
1673 p->u.r.ackid,
1674 lp->rcv_nxt);
1676 if (unlikely(!rx_seq_ok(lp, p->seqid))) {
1677 err = rx_bad_seq(lp, p, first_frag);
1678 copied = 0;
1679 break;
1682 if (p->type & LDC_CTRL) {
1683 err = process_control_frame(lp, p);
1684 if (err < 0)
1685 break;
1686 err = 0;
1689 lp->rcv_nxt = p->seqid;
1691 if (!(p->type & LDC_DATA)) {
1692 new = rx_advance(lp, new);
1693 goto no_data;
1695 if (p->stype & (LDC_ACK | LDC_NACK)) {
1696 err = data_ack_nack(lp, p);
1697 if (err)
1698 break;
1700 if (!(p->stype & LDC_INFO)) {
1701 new = rx_advance(lp, new);
1702 err = rx_set_head(lp, new);
1703 if (err)
1704 break;
1705 goto no_data;
1708 pkt_len = p->env & LDC_LEN;
1710 /* Every initial packet starts with the START bit set.
1712 * Singleton packets will have both START+STOP set.
1714 * Fragments will have START set in the first frame, STOP
1715 * set in the last frame, and neither bit set in middle
1716 * frames of the packet.
1718 * Therefore if we are at the beginning of a packet and
1719 * we don't see START, or we are in the middle of a fragmented
1720 * packet and do see START, we are unsynchronized and should
1721 * flush the RX queue.
1723 if ((first_frag == NULL && !(p->env & LDC_START)) ||
1724 (first_frag != NULL && (p->env & LDC_START))) {
1725 if (!first_frag)
1726 new = rx_advance(lp, new);
1728 err = rx_set_head(lp, new);
1729 if (err)
1730 break;
1732 if (!first_frag)
1733 goto no_data;
1735 if (!first_frag)
1736 first_frag = p;
1738 if (pkt_len > size - copied) {
1739 /* User didn't give us a big enough buffer,
1740 * what to do? This is a pretty serious error.
1742 * Since we haven't updated the RX ring head to
1743 * consume any of the packets, signal the error
1744 * to the user and just leave the RX ring alone.
1746 * This seems the best behavior because this allows
1747 * a user of the LDC layer to start with a small
1748 * RX buffer for ldc_read() calls and use -EMSGSIZE
1749 * as a cue to enlarge it's read buffer.
1751 err = -EMSGSIZE;
1752 break;
1755 /* Ok, we are gonna eat this one. */
1756 new = rx_advance(lp, new);
1758 memcpy(buf,
1759 (lp->cfg.mode == LDC_MODE_UNRELIABLE ?
1760 p->u.u_data : p->u.r.r_data), pkt_len);
1761 buf += pkt_len;
1762 copied += pkt_len;
1764 if (p->env & LDC_STOP)
1765 break;
1767 no_data:
1768 if (new == lp->rx_tail) {
1769 err = rx_data_wait(lp, new);
1770 if (err)
1771 break;
1775 if (!err)
1776 err = rx_set_head(lp, new);
1778 if (err && first_frag)
1779 lp->rcv_nxt = first_frag->seqid - 1;
1781 if (!err) {
1782 err = copied;
1783 if (err > 0 && lp->cfg.mode != LDC_MODE_UNRELIABLE)
1784 send_data_ack(lp);
1787 return err;
1790 static const struct ldc_mode_ops nonraw_ops = {
1791 .write = write_nonraw,
1792 .read = read_nonraw,
1795 static int write_stream(struct ldc_channel *lp, const void *buf,
1796 unsigned int size)
1798 if (size > lp->cfg.mtu)
1799 size = lp->cfg.mtu;
1800 return write_nonraw(lp, buf, size);
1803 static int read_stream(struct ldc_channel *lp, void *buf, unsigned int size)
1805 if (!lp->mssbuf_len) {
1806 int err = read_nonraw(lp, lp->mssbuf, lp->cfg.mtu);
1807 if (err < 0)
1808 return err;
1810 lp->mssbuf_len = err;
1811 lp->mssbuf_off = 0;
1814 if (size > lp->mssbuf_len)
1815 size = lp->mssbuf_len;
1816 memcpy(buf, lp->mssbuf + lp->mssbuf_off, size);
1818 lp->mssbuf_off += size;
1819 lp->mssbuf_len -= size;
1821 return size;
1824 static const struct ldc_mode_ops stream_ops = {
1825 .write = write_stream,
1826 .read = read_stream,
1829 int ldc_write(struct ldc_channel *lp, const void *buf, unsigned int size)
1831 unsigned long flags;
1832 int err;
1834 if (!buf)
1835 return -EINVAL;
1837 if (!size)
1838 return 0;
1840 spin_lock_irqsave(&lp->lock, flags);
1842 if (lp->hs_state != LDC_HS_COMPLETE)
1843 err = -ENOTCONN;
1844 else
1845 err = lp->mops->write(lp, buf, size);
1847 spin_unlock_irqrestore(&lp->lock, flags);
1849 return err;
1851 EXPORT_SYMBOL(ldc_write);
1853 int ldc_read(struct ldc_channel *lp, void *buf, unsigned int size)
1855 unsigned long flags;
1856 int err;
1858 if (!buf)
1859 return -EINVAL;
1861 if (!size)
1862 return 0;
1864 spin_lock_irqsave(&lp->lock, flags);
1866 if (lp->hs_state != LDC_HS_COMPLETE)
1867 err = -ENOTCONN;
1868 else
1869 err = lp->mops->read(lp, buf, size);
1871 spin_unlock_irqrestore(&lp->lock, flags);
1873 return err;
1875 EXPORT_SYMBOL(ldc_read);
1877 static long arena_alloc(struct ldc_iommu *iommu, unsigned long npages)
1879 struct iommu_arena *arena = &iommu->arena;
1880 unsigned long n, i, start, end, limit;
1881 int pass;
1883 limit = arena->limit;
1884 start = arena->hint;
1885 pass = 0;
1887 again:
1888 n = find_next_zero_bit(arena->map, limit, start);
1889 end = n + npages;
1890 if (unlikely(end >= limit)) {
1891 if (likely(pass < 1)) {
1892 limit = start;
1893 start = 0;
1894 pass++;
1895 goto again;
1896 } else {
1897 /* Scanned the whole thing, give up. */
1898 return -1;
1902 for (i = n; i < end; i++) {
1903 if (test_bit(i, arena->map)) {
1904 start = i + 1;
1905 goto again;
1909 for (i = n; i < end; i++)
1910 __set_bit(i, arena->map);
1912 arena->hint = end;
1914 return n;
1917 #define COOKIE_PGSZ_CODE 0xf000000000000000ULL
1918 #define COOKIE_PGSZ_CODE_SHIFT 60ULL
1920 static u64 pagesize_code(void)
1922 switch (PAGE_SIZE) {
1923 default:
1924 case (8ULL * 1024ULL):
1925 return 0;
1926 case (64ULL * 1024ULL):
1927 return 1;
1928 case (512ULL * 1024ULL):
1929 return 2;
1930 case (4ULL * 1024ULL * 1024ULL):
1931 return 3;
1932 case (32ULL * 1024ULL * 1024ULL):
1933 return 4;
1934 case (256ULL * 1024ULL * 1024ULL):
1935 return 5;
1939 static u64 make_cookie(u64 index, u64 pgsz_code, u64 page_offset)
1941 return ((pgsz_code << COOKIE_PGSZ_CODE_SHIFT) |
1942 (index << PAGE_SHIFT) |
1943 page_offset);
1946 static u64 cookie_to_index(u64 cookie, unsigned long *shift)
1948 u64 szcode = cookie >> COOKIE_PGSZ_CODE_SHIFT;
1950 cookie &= ~COOKIE_PGSZ_CODE;
1952 *shift = szcode * 3;
1954 return (cookie >> (13ULL + (szcode * 3ULL)));
1957 static struct ldc_mtable_entry *alloc_npages(struct ldc_iommu *iommu,
1958 unsigned long npages)
1960 long entry;
1962 entry = arena_alloc(iommu, npages);
1963 if (unlikely(entry < 0))
1964 return NULL;
1966 return iommu->page_table + entry;
1969 static u64 perm_to_mte(unsigned int map_perm)
1971 u64 mte_base;
1973 mte_base = pagesize_code();
1975 if (map_perm & LDC_MAP_SHADOW) {
1976 if (map_perm & LDC_MAP_R)
1977 mte_base |= LDC_MTE_COPY_R;
1978 if (map_perm & LDC_MAP_W)
1979 mte_base |= LDC_MTE_COPY_W;
1981 if (map_perm & LDC_MAP_DIRECT) {
1982 if (map_perm & LDC_MAP_R)
1983 mte_base |= LDC_MTE_READ;
1984 if (map_perm & LDC_MAP_W)
1985 mte_base |= LDC_MTE_WRITE;
1986 if (map_perm & LDC_MAP_X)
1987 mte_base |= LDC_MTE_EXEC;
1989 if (map_perm & LDC_MAP_IO) {
1990 if (map_perm & LDC_MAP_R)
1991 mte_base |= LDC_MTE_IOMMU_R;
1992 if (map_perm & LDC_MAP_W)
1993 mte_base |= LDC_MTE_IOMMU_W;
1996 return mte_base;
1999 static int pages_in_region(unsigned long base, long len)
2001 int count = 0;
2003 do {
2004 unsigned long new = (base + PAGE_SIZE) & PAGE_MASK;
2006 len -= (new - base);
2007 base = new;
2008 count++;
2009 } while (len > 0);
2011 return count;
2014 struct cookie_state {
2015 struct ldc_mtable_entry *page_table;
2016 struct ldc_trans_cookie *cookies;
2017 u64 mte_base;
2018 u64 prev_cookie;
2019 u32 pte_idx;
2020 u32 nc;
2023 static void fill_cookies(struct cookie_state *sp, unsigned long pa,
2024 unsigned long off, unsigned long len)
2026 do {
2027 unsigned long tlen, new = pa + PAGE_SIZE;
2028 u64 this_cookie;
2030 sp->page_table[sp->pte_idx].mte = sp->mte_base | pa;
2032 tlen = PAGE_SIZE;
2033 if (off)
2034 tlen = PAGE_SIZE - off;
2035 if (tlen > len)
2036 tlen = len;
2038 this_cookie = make_cookie(sp->pte_idx,
2039 pagesize_code(), off);
2041 off = 0;
2043 if (this_cookie == sp->prev_cookie) {
2044 sp->cookies[sp->nc - 1].cookie_size += tlen;
2045 } else {
2046 sp->cookies[sp->nc].cookie_addr = this_cookie;
2047 sp->cookies[sp->nc].cookie_size = tlen;
2048 sp->nc++;
2050 sp->prev_cookie = this_cookie + tlen;
2052 sp->pte_idx++;
2054 len -= tlen;
2055 pa = new;
2056 } while (len > 0);
2059 static int sg_count_one(struct scatterlist *sg)
2061 unsigned long base = page_to_pfn(sg_page(sg)) << PAGE_SHIFT;
2062 long len = sg->length;
2064 if ((sg->offset | len) & (8UL - 1))
2065 return -EFAULT;
2067 return pages_in_region(base + sg->offset, len);
2070 static int sg_count_pages(struct scatterlist *sg, int num_sg)
2072 int count;
2073 int i;
2075 count = 0;
2076 for (i = 0; i < num_sg; i++) {
2077 int err = sg_count_one(sg + i);
2078 if (err < 0)
2079 return err;
2080 count += err;
2083 return count;
2086 int ldc_map_sg(struct ldc_channel *lp,
2087 struct scatterlist *sg, int num_sg,
2088 struct ldc_trans_cookie *cookies, int ncookies,
2089 unsigned int map_perm)
2091 unsigned long i, npages, flags;
2092 struct ldc_mtable_entry *base;
2093 struct cookie_state state;
2094 struct ldc_iommu *iommu;
2095 int err;
2097 if (map_perm & ~LDC_MAP_ALL)
2098 return -EINVAL;
2100 err = sg_count_pages(sg, num_sg);
2101 if (err < 0)
2102 return err;
2104 npages = err;
2105 if (err > ncookies)
2106 return -EMSGSIZE;
2108 iommu = &lp->iommu;
2110 spin_lock_irqsave(&iommu->lock, flags);
2111 base = alloc_npages(iommu, npages);
2112 spin_unlock_irqrestore(&iommu->lock, flags);
2114 if (!base)
2115 return -ENOMEM;
2117 state.page_table = iommu->page_table;
2118 state.cookies = cookies;
2119 state.mte_base = perm_to_mte(map_perm);
2120 state.prev_cookie = ~(u64)0;
2121 state.pte_idx = (base - iommu->page_table);
2122 state.nc = 0;
2124 for (i = 0; i < num_sg; i++)
2125 fill_cookies(&state, page_to_pfn(sg_page(&sg[i])) << PAGE_SHIFT,
2126 sg[i].offset, sg[i].length);
2128 return state.nc;
2130 EXPORT_SYMBOL(ldc_map_sg);
2132 int ldc_map_single(struct ldc_channel *lp,
2133 void *buf, unsigned int len,
2134 struct ldc_trans_cookie *cookies, int ncookies,
2135 unsigned int map_perm)
2137 unsigned long npages, pa, flags;
2138 struct ldc_mtable_entry *base;
2139 struct cookie_state state;
2140 struct ldc_iommu *iommu;
2142 if ((map_perm & ~LDC_MAP_ALL) || (ncookies < 1))
2143 return -EINVAL;
2145 pa = __pa(buf);
2146 if ((pa | len) & (8UL - 1))
2147 return -EFAULT;
2149 npages = pages_in_region(pa, len);
2151 iommu = &lp->iommu;
2153 spin_lock_irqsave(&iommu->lock, flags);
2154 base = alloc_npages(iommu, npages);
2155 spin_unlock_irqrestore(&iommu->lock, flags);
2157 if (!base)
2158 return -ENOMEM;
2160 state.page_table = iommu->page_table;
2161 state.cookies = cookies;
2162 state.mte_base = perm_to_mte(map_perm);
2163 state.prev_cookie = ~(u64)0;
2164 state.pte_idx = (base - iommu->page_table);
2165 state.nc = 0;
2166 fill_cookies(&state, (pa & PAGE_MASK), (pa & ~PAGE_MASK), len);
2167 BUG_ON(state.nc != 1);
2169 return state.nc;
2171 EXPORT_SYMBOL(ldc_map_single);
2173 static void free_npages(unsigned long id, struct ldc_iommu *iommu,
2174 u64 cookie, u64 size)
2176 struct iommu_arena *arena = &iommu->arena;
2177 unsigned long i, shift, index, npages;
2178 struct ldc_mtable_entry *base;
2180 npages = PAGE_ALIGN(((cookie & ~PAGE_MASK) + size)) >> PAGE_SHIFT;
2181 index = cookie_to_index(cookie, &shift);
2182 base = iommu->page_table + index;
2184 BUG_ON(index > arena->limit ||
2185 (index + npages) > arena->limit);
2187 for (i = 0; i < npages; i++) {
2188 if (base->cookie)
2189 sun4v_ldc_revoke(id, cookie + (i << shift),
2190 base->cookie);
2191 base->mte = 0;
2192 __clear_bit(index + i, arena->map);
2196 void ldc_unmap(struct ldc_channel *lp, struct ldc_trans_cookie *cookies,
2197 int ncookies)
2199 struct ldc_iommu *iommu = &lp->iommu;
2200 unsigned long flags;
2201 int i;
2203 spin_lock_irqsave(&iommu->lock, flags);
2204 for (i = 0; i < ncookies; i++) {
2205 u64 addr = cookies[i].cookie_addr;
2206 u64 size = cookies[i].cookie_size;
2208 free_npages(lp->id, iommu, addr, size);
2210 spin_unlock_irqrestore(&iommu->lock, flags);
2212 EXPORT_SYMBOL(ldc_unmap);
2214 int ldc_copy(struct ldc_channel *lp, int copy_dir,
2215 void *buf, unsigned int len, unsigned long offset,
2216 struct ldc_trans_cookie *cookies, int ncookies)
2218 unsigned int orig_len;
2219 unsigned long ra;
2220 int i;
2222 if (copy_dir != LDC_COPY_IN && copy_dir != LDC_COPY_OUT) {
2223 printk(KERN_ERR PFX "ldc_copy: ID[%lu] Bad copy_dir[%d]\n",
2224 lp->id, copy_dir);
2225 return -EINVAL;
2228 ra = __pa(buf);
2229 if ((ra | len | offset) & (8UL - 1)) {
2230 printk(KERN_ERR PFX "ldc_copy: ID[%lu] Unaligned buffer "
2231 "ra[%lx] len[%x] offset[%lx]\n",
2232 lp->id, ra, len, offset);
2233 return -EFAULT;
2236 if (lp->hs_state != LDC_HS_COMPLETE ||
2237 (lp->flags & LDC_FLAG_RESET)) {
2238 printk(KERN_ERR PFX "ldc_copy: ID[%lu] Link down hs_state[%x] "
2239 "flags[%x]\n", lp->id, lp->hs_state, lp->flags);
2240 return -ECONNRESET;
2243 orig_len = len;
2244 for (i = 0; i < ncookies; i++) {
2245 unsigned long cookie_raddr = cookies[i].cookie_addr;
2246 unsigned long this_len = cookies[i].cookie_size;
2247 unsigned long actual_len;
2249 if (unlikely(offset)) {
2250 unsigned long this_off = offset;
2252 if (this_off > this_len)
2253 this_off = this_len;
2255 offset -= this_off;
2256 this_len -= this_off;
2257 if (!this_len)
2258 continue;
2259 cookie_raddr += this_off;
2262 if (this_len > len)
2263 this_len = len;
2265 while (1) {
2266 unsigned long hv_err;
2268 hv_err = sun4v_ldc_copy(lp->id, copy_dir,
2269 cookie_raddr, ra,
2270 this_len, &actual_len);
2271 if (unlikely(hv_err)) {
2272 printk(KERN_ERR PFX "ldc_copy: ID[%lu] "
2273 "HV error %lu\n",
2274 lp->id, hv_err);
2275 if (lp->hs_state != LDC_HS_COMPLETE ||
2276 (lp->flags & LDC_FLAG_RESET))
2277 return -ECONNRESET;
2278 else
2279 return -EFAULT;
2282 cookie_raddr += actual_len;
2283 ra += actual_len;
2284 len -= actual_len;
2285 if (actual_len == this_len)
2286 break;
2288 this_len -= actual_len;
2291 if (!len)
2292 break;
2295 /* It is caller policy what to do about short copies.
2296 * For example, a networking driver can declare the
2297 * packet a runt and drop it.
2300 return orig_len - len;
2302 EXPORT_SYMBOL(ldc_copy);
2304 void *ldc_alloc_exp_dring(struct ldc_channel *lp, unsigned int len,
2305 struct ldc_trans_cookie *cookies, int *ncookies,
2306 unsigned int map_perm)
2308 void *buf;
2309 int err;
2311 if (len & (8UL - 1))
2312 return ERR_PTR(-EINVAL);
2314 buf = kzalloc(len, GFP_KERNEL);
2315 if (!buf)
2316 return ERR_PTR(-ENOMEM);
2318 err = ldc_map_single(lp, buf, len, cookies, *ncookies, map_perm);
2319 if (err < 0) {
2320 kfree(buf);
2321 return ERR_PTR(err);
2323 *ncookies = err;
2325 return buf;
2327 EXPORT_SYMBOL(ldc_alloc_exp_dring);
2329 void ldc_free_exp_dring(struct ldc_channel *lp, void *buf, unsigned int len,
2330 struct ldc_trans_cookie *cookies, int ncookies)
2332 ldc_unmap(lp, cookies, ncookies);
2333 kfree(buf);
2335 EXPORT_SYMBOL(ldc_free_exp_dring);
2337 static int __init ldc_init(void)
2339 unsigned long major, minor;
2340 struct mdesc_handle *hp;
2341 const u64 *v;
2342 int err;
2343 u64 mp;
2345 hp = mdesc_grab();
2346 if (!hp)
2347 return -ENODEV;
2349 mp = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
2350 err = -ENODEV;
2351 if (mp == MDESC_NODE_NULL)
2352 goto out;
2354 v = mdesc_get_property(hp, mp, "domaining-enabled", NULL);
2355 if (!v)
2356 goto out;
2358 major = 1;
2359 minor = 0;
2360 if (sun4v_hvapi_register(HV_GRP_LDOM, major, &minor)) {
2361 printk(KERN_INFO PFX "Could not register LDOM hvapi.\n");
2362 goto out;
2365 printk(KERN_INFO "%s", version);
2367 if (!*v) {
2368 printk(KERN_INFO PFX "Domaining disabled.\n");
2369 goto out;
2371 ldom_domaining_enabled = 1;
2372 err = 0;
2374 out:
2375 mdesc_release(hp);
2376 return err;
2379 core_initcall(ldc_init);