1 /* ldc.c: Logical Domain Channel link-layer protocol driver.
3 * Copyright (C) 2007 David S. Miller <davem@davemloft.net>
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>
22 #include <asm/mdesc.h>
24 #define DRV_MODULE_NAME "ldc"
25 #define PFX DRV_MODULE_NAME ": "
26 #define DRV_MODULE_VERSION "1.0"
27 #define DRV_MODULE_RELDATE "June 25, 2007"
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
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
57 #define LDC_FRAG_MASK 0xc0
58 #define LDC_START 0x40
64 u8 u_data[LDC_PACKET_SIZE - 8];
68 u8 r_data[LDC_PACKET_SIZE - 8 - 8];
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)
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;
100 /* Protects arena alloc/free. */
102 struct iommu_arena arena;
103 struct ldc_mtable_entry *page_table;
107 /* Protects all operations that depend upon channel state. */
116 struct ldc_packet *tx_base;
117 unsigned long tx_head;
118 unsigned long tx_tail;
119 unsigned long tx_num_entries;
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;
133 unsigned long chan_state;
135 struct ldc_channel_config cfg;
138 const struct ldc_mode_ops *mops;
140 struct ldc_iommu iommu;
142 struct ldc_version ver;
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
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
161 struct hlist_head mh_list;
163 struct hlist_node list;
166 #define ldcdbg(TYPE, f, a...) \
167 do { if (lp->cfg.debug & LDC_DEBUG_##TYPE) \
168 printk(KERN_INFO PFX "ID[%lu] " f, lp->id, ## a); \
171 static const char *state_to_str(u8 state)
174 case LDC_STATE_INVALID:
178 case LDC_STATE_BOUND:
180 case LDC_STATE_READY:
182 case LDC_STATE_CONNECTED:
189 static void ldc_set_state(struct ldc_channel *lp, u8 state)
191 ldcdbg(STATE, "STATE (%s) --> (%s)\n",
192 state_to_str(lp->state),
193 state_to_str(state));
198 static unsigned long __advance(unsigned long off, unsigned long num_entries)
200 off += LDC_PACKET_SIZE;
201 if (off == (num_entries * LDC_PACKET_SIZE))
207 static unsigned long rx_advance(struct ldc_channel *lp, unsigned long off)
209 return __advance(off, lp->rx_num_entries);
212 static unsigned long tx_advance(struct ldc_channel *lp, unsigned long off)
214 return __advance(off, lp->tx_num_entries);
217 static struct ldc_packet *handshake_get_tx_packet(struct ldc_channel *lp,
218 unsigned long *new_tail)
220 struct ldc_packet *p;
223 t = tx_advance(lp, lp->tx_tail);
224 if (t == lp->tx_head)
230 return p + (lp->tx_tail / LDC_PACKET_SIZE);
233 /* When we are in reliable or stream mode, have to track the next packet
234 * we haven't gotten an ACK for in the TX queue using tx_acked. We have
235 * to be careful not to stomp over the queue past that point. During
236 * the handshake, we don't have TX data packets pending in the queue
237 * and that's why handshake_get_tx_packet() need not be mindful of
240 static unsigned long head_for_data(struct ldc_channel *lp)
242 if (lp->cfg.mode == LDC_MODE_RELIABLE ||
243 lp->cfg.mode == LDC_MODE_STREAM)
248 static int tx_has_space_for(struct ldc_channel *lp, unsigned int size)
250 unsigned long limit, tail, new_tail, diff;
253 limit = head_for_data(lp);
255 new_tail = tx_advance(lp, tail);
256 if (new_tail == limit)
259 if (limit > new_tail)
260 diff = limit - new_tail;
263 ((lp->tx_num_entries * LDC_PACKET_SIZE) - new_tail));
264 diff /= LDC_PACKET_SIZE;
267 if (diff * mss < size)
273 static struct ldc_packet *data_get_tx_packet(struct ldc_channel *lp,
274 unsigned long *new_tail)
276 struct ldc_packet *p;
279 h = head_for_data(lp);
280 t = tx_advance(lp, lp->tx_tail);
287 return p + (lp->tx_tail / LDC_PACKET_SIZE);
290 static int set_tx_tail(struct ldc_channel *lp, unsigned long tail)
292 unsigned long orig_tail = lp->tx_tail;
296 while (limit-- > 0) {
299 err = sun4v_ldc_tx_set_qtail(lp->id, tail);
303 if (err != HV_EWOULDBLOCK) {
304 lp->tx_tail = orig_tail;
310 lp->tx_tail = orig_tail;
314 /* This just updates the head value in the hypervisor using
315 * a polling loop with a timeout. The caller takes care of
316 * upating software state representing the head change, if any.
318 static int __set_rx_head(struct ldc_channel *lp, unsigned long head)
322 while (limit-- > 0) {
325 err = sun4v_ldc_rx_set_qhead(lp->id, head);
329 if (err != HV_EWOULDBLOCK)
338 static int send_tx_packet(struct ldc_channel *lp,
339 struct ldc_packet *p,
340 unsigned long new_tail)
342 BUG_ON(p != (lp->tx_base + (lp->tx_tail / LDC_PACKET_SIZE)));
344 return set_tx_tail(lp, new_tail);
347 static struct ldc_packet *handshake_compose_ctrl(struct ldc_channel *lp,
349 void *data, int dlen,
350 unsigned long *new_tail)
352 struct ldc_packet *p = handshake_get_tx_packet(lp, new_tail);
355 memset(p, 0, sizeof(*p));
360 memcpy(p->u.u_data, data, dlen);
365 static int start_handshake(struct ldc_channel *lp)
367 struct ldc_packet *p;
368 struct ldc_version *ver;
369 unsigned long new_tail;
373 ldcdbg(HS, "SEND VER INFO maj[%u] min[%u]\n",
374 ver->major, ver->minor);
376 p = handshake_compose_ctrl(lp, LDC_INFO, LDC_VERS,
377 ver, sizeof(*ver), &new_tail);
379 int err = send_tx_packet(lp, p, new_tail);
381 lp->flags &= ~LDC_FLAG_RESET;
387 static int send_version_nack(struct ldc_channel *lp,
388 u16 major, u16 minor)
390 struct ldc_packet *p;
391 struct ldc_version ver;
392 unsigned long new_tail;
397 p = handshake_compose_ctrl(lp, LDC_NACK, LDC_VERS,
398 &ver, sizeof(ver), &new_tail);
400 ldcdbg(HS, "SEND VER NACK maj[%u] min[%u]\n",
401 ver.major, ver.minor);
403 return send_tx_packet(lp, p, new_tail);
408 static int send_version_ack(struct ldc_channel *lp,
409 struct ldc_version *vp)
411 struct ldc_packet *p;
412 unsigned long new_tail;
414 p = handshake_compose_ctrl(lp, LDC_ACK, LDC_VERS,
415 vp, sizeof(*vp), &new_tail);
417 ldcdbg(HS, "SEND VER ACK maj[%u] min[%u]\n",
418 vp->major, vp->minor);
420 return send_tx_packet(lp, p, new_tail);
425 static int send_rts(struct ldc_channel *lp)
427 struct ldc_packet *p;
428 unsigned long new_tail;
430 p = handshake_compose_ctrl(lp, LDC_INFO, LDC_RTS, NULL, 0,
433 p->env = lp->cfg.mode;
437 ldcdbg(HS, "SEND RTS env[0x%x] seqid[0x%x]\n",
440 return send_tx_packet(lp, p, new_tail);
445 static int send_rtr(struct ldc_channel *lp)
447 struct ldc_packet *p;
448 unsigned long new_tail;
450 p = handshake_compose_ctrl(lp, LDC_INFO, LDC_RTR, NULL, 0,
453 p->env = lp->cfg.mode;
456 ldcdbg(HS, "SEND RTR env[0x%x] seqid[0x%x]\n",
459 return send_tx_packet(lp, p, new_tail);
464 static int send_rdx(struct ldc_channel *lp)
466 struct ldc_packet *p;
467 unsigned long new_tail;
469 p = handshake_compose_ctrl(lp, LDC_INFO, LDC_RDX, NULL, 0,
473 p->seqid = ++lp->snd_nxt;
474 p->u.r.ackid = lp->rcv_nxt;
476 ldcdbg(HS, "SEND RDX env[0x%x] seqid[0x%x] ackid[0x%x]\n",
477 p->env, p->seqid, p->u.r.ackid);
479 return send_tx_packet(lp, p, new_tail);
484 static int send_data_nack(struct ldc_channel *lp, struct ldc_packet *data_pkt)
486 struct ldc_packet *p;
487 unsigned long new_tail;
490 p = data_get_tx_packet(lp, &new_tail);
493 memset(p, 0, sizeof(*p));
494 p->type = data_pkt->type;
496 p->ctrl = data_pkt->ctrl & LDC_CTRL_MSK;
497 p->seqid = lp->snd_nxt;
498 p->u.r.ackid = lp->rcv_nxt;
500 ldcdbg(HS, "SEND DATA NACK type[0x%x] ctl[0x%x] seq[0x%x] ack[0x%x]\n",
501 p->type, p->ctrl, p->seqid, p->u.r.ackid);
503 err = send_tx_packet(lp, p, new_tail);
510 static int ldc_abort(struct ldc_channel *lp)
512 unsigned long hv_err;
514 ldcdbg(STATE, "ABORT\n");
516 /* We report but do not act upon the hypervisor errors because
517 * there really isn't much we can do if they fail at this point.
519 hv_err = sun4v_ldc_tx_qconf(lp->id, lp->tx_ra, lp->tx_num_entries);
521 printk(KERN_ERR PFX "ldc_abort: "
522 "sun4v_ldc_tx_qconf(%lx,%lx,%lx) failed, err=%lu\n",
523 lp->id, lp->tx_ra, lp->tx_num_entries, hv_err);
525 hv_err = sun4v_ldc_tx_get_state(lp->id,
530 printk(KERN_ERR PFX "ldc_abort: "
531 "sun4v_ldc_tx_get_state(%lx,...) failed, err=%lu\n",
534 hv_err = sun4v_ldc_rx_qconf(lp->id, lp->rx_ra, lp->rx_num_entries);
536 printk(KERN_ERR PFX "ldc_abort: "
537 "sun4v_ldc_rx_qconf(%lx,%lx,%lx) failed, err=%lu\n",
538 lp->id, lp->rx_ra, lp->rx_num_entries, hv_err);
540 /* Refetch the RX queue state as well, because we could be invoked
541 * here in the queue processing context.
543 hv_err = sun4v_ldc_rx_get_state(lp->id,
548 printk(KERN_ERR PFX "ldc_abort: "
549 "sun4v_ldc_rx_get_state(%lx,...) failed, err=%lu\n",
555 static struct ldc_version *find_by_major(u16 major)
557 struct ldc_version *ret = NULL;
560 for (i = 0; i < ARRAY_SIZE(ver_arr); i++) {
561 struct ldc_version *v = &ver_arr[i];
562 if (v->major <= major) {
570 static int process_ver_info(struct ldc_channel *lp, struct ldc_version *vp)
572 struct ldc_version *vap;
575 ldcdbg(HS, "GOT VERSION INFO major[%x] minor[%x]\n",
576 vp->major, vp->minor);
578 if (lp->hs_state == LDC_HS_GOTVERS) {
579 lp->hs_state = LDC_HS_OPEN;
580 memset(&lp->ver, 0, sizeof(lp->ver));
583 vap = find_by_major(vp->major);
585 err = send_version_nack(lp, 0, 0);
586 } else if (vap->major != vp->major) {
587 err = send_version_nack(lp, vap->major, vap->minor);
589 struct ldc_version ver = *vp;
590 if (ver.minor > vap->minor)
591 ver.minor = vap->minor;
592 err = send_version_ack(lp, &ver);
595 lp->hs_state = LDC_HS_GOTVERS;
599 return ldc_abort(lp);
604 static int process_ver_ack(struct ldc_channel *lp, struct ldc_version *vp)
606 ldcdbg(HS, "GOT VERSION ACK major[%x] minor[%x]\n",
607 vp->major, vp->minor);
609 if (lp->hs_state == LDC_HS_GOTVERS) {
610 if (lp->ver.major != vp->major ||
611 lp->ver.minor != vp->minor)
612 return ldc_abort(lp);
615 lp->hs_state = LDC_HS_GOTVERS;
618 return ldc_abort(lp);
622 static int process_ver_nack(struct ldc_channel *lp, struct ldc_version *vp)
624 struct ldc_version *vap;
626 if ((vp->major == 0 && vp->minor == 0) ||
627 !(vap = find_by_major(vp->major))) {
628 return ldc_abort(lp);
630 struct ldc_packet *p;
631 unsigned long new_tail;
633 p = handshake_compose_ctrl(lp, LDC_INFO, LDC_VERS,
637 return send_tx_packet(lp, p, new_tail);
639 return ldc_abort(lp);
643 static int process_version(struct ldc_channel *lp,
644 struct ldc_packet *p)
646 struct ldc_version *vp;
648 vp = (struct ldc_version *) p->u.u_data;
652 return process_ver_info(lp, vp);
655 return process_ver_ack(lp, vp);
658 return process_ver_nack(lp, vp);
661 return ldc_abort(lp);
665 static int process_rts(struct ldc_channel *lp,
666 struct ldc_packet *p)
668 ldcdbg(HS, "GOT RTS stype[%x] seqid[%x] env[%x]\n",
669 p->stype, p->seqid, p->env);
671 if (p->stype != LDC_INFO ||
672 lp->hs_state != LDC_HS_GOTVERS ||
673 p->env != lp->cfg.mode)
674 return ldc_abort(lp);
676 lp->snd_nxt = p->seqid;
677 lp->rcv_nxt = p->seqid;
678 lp->hs_state = LDC_HS_SENTRTR;
680 return ldc_abort(lp);
685 static int process_rtr(struct ldc_channel *lp,
686 struct ldc_packet *p)
688 ldcdbg(HS, "GOT RTR stype[%x] seqid[%x] env[%x]\n",
689 p->stype, p->seqid, p->env);
691 if (p->stype != LDC_INFO ||
692 p->env != lp->cfg.mode)
693 return ldc_abort(lp);
695 lp->snd_nxt = p->seqid;
696 lp->hs_state = LDC_HS_COMPLETE;
697 ldc_set_state(lp, LDC_STATE_CONNECTED);
703 static int rx_seq_ok(struct ldc_channel *lp, u32 seqid)
705 return lp->rcv_nxt + 1 == seqid;
708 static int process_rdx(struct ldc_channel *lp,
709 struct ldc_packet *p)
711 ldcdbg(HS, "GOT RDX stype[%x] seqid[%x] env[%x] ackid[%x]\n",
712 p->stype, p->seqid, p->env, p->u.r.ackid);
714 if (p->stype != LDC_INFO ||
715 !(rx_seq_ok(lp, p->seqid)))
716 return ldc_abort(lp);
718 lp->rcv_nxt = p->seqid;
720 lp->hs_state = LDC_HS_COMPLETE;
721 ldc_set_state(lp, LDC_STATE_CONNECTED);
726 static int process_control_frame(struct ldc_channel *lp,
727 struct ldc_packet *p)
731 return process_version(lp, p);
734 return process_rts(lp, p);
737 return process_rtr(lp, p);
740 return process_rdx(lp, p);
743 return ldc_abort(lp);
747 static int process_error_frame(struct ldc_channel *lp,
748 struct ldc_packet *p)
750 return ldc_abort(lp);
753 static int process_data_ack(struct ldc_channel *lp,
754 struct ldc_packet *ack)
756 unsigned long head = lp->tx_acked;
757 u32 ackid = ack->u.r.ackid;
760 struct ldc_packet *p = lp->tx_base + (head / LDC_PACKET_SIZE);
762 head = tx_advance(lp, head);
764 if (p->seqid == ackid) {
768 if (head == lp->tx_head)
769 return ldc_abort(lp);
775 static void send_events(struct ldc_channel *lp, unsigned int event_mask)
777 if (event_mask & LDC_EVENT_RESET)
778 lp->cfg.event(lp->event_arg, LDC_EVENT_RESET);
779 if (event_mask & LDC_EVENT_UP)
780 lp->cfg.event(lp->event_arg, LDC_EVENT_UP);
781 if (event_mask & LDC_EVENT_DATA_READY)
782 lp->cfg.event(lp->event_arg, LDC_EVENT_DATA_READY);
785 static irqreturn_t ldc_rx(int irq, void *dev_id)
787 struct ldc_channel *lp = dev_id;
788 unsigned long orig_state, hv_err, flags;
789 unsigned int event_mask;
791 spin_lock_irqsave(&lp->lock, flags);
793 orig_state = lp->chan_state;
794 hv_err = sun4v_ldc_rx_get_state(lp->id,
799 ldcdbg(RX, "RX state[0x%02lx:0x%02lx] head[0x%04lx] tail[0x%04lx]\n",
800 orig_state, lp->chan_state, lp->rx_head, lp->rx_tail);
804 if (lp->cfg.mode == LDC_MODE_RAW &&
805 lp->chan_state == LDC_CHANNEL_UP) {
806 lp->hs_state = LDC_HS_COMPLETE;
807 ldc_set_state(lp, LDC_STATE_CONNECTED);
809 event_mask |= LDC_EVENT_UP;
811 orig_state = lp->chan_state;
814 /* If we are in reset state, flush the RX queue and ignore
817 if (lp->flags & LDC_FLAG_RESET) {
818 (void) __set_rx_head(lp, lp->rx_tail);
822 /* Once we finish the handshake, we let the ldc_read()
823 * paths do all of the control frame and state management.
824 * Just trigger the callback.
826 if (lp->hs_state == LDC_HS_COMPLETE) {
828 if (lp->chan_state != orig_state) {
829 unsigned int event = LDC_EVENT_RESET;
831 if (lp->chan_state == LDC_CHANNEL_UP)
832 event = LDC_EVENT_UP;
836 if (lp->rx_head != lp->rx_tail)
837 event_mask |= LDC_EVENT_DATA_READY;
842 if (lp->chan_state != orig_state)
845 while (lp->rx_head != lp->rx_tail) {
846 struct ldc_packet *p;
850 p = lp->rx_base + (lp->rx_head / LDC_PACKET_SIZE);
854 err = process_control_frame(lp, p);
860 event_mask |= LDC_EVENT_DATA_READY;
865 err = process_error_frame(lp, p);
877 new += LDC_PACKET_SIZE;
878 if (new == (lp->rx_num_entries * LDC_PACKET_SIZE))
882 err = __set_rx_head(lp, new);
884 (void) ldc_abort(lp);
887 if (lp->hs_state == LDC_HS_COMPLETE)
888 goto handshake_complete;
892 spin_unlock_irqrestore(&lp->lock, flags);
894 send_events(lp, event_mask);
899 static irqreturn_t ldc_tx(int irq, void *dev_id)
901 struct ldc_channel *lp = dev_id;
902 unsigned long flags, hv_err, orig_state;
903 unsigned int event_mask = 0;
905 spin_lock_irqsave(&lp->lock, flags);
907 orig_state = lp->chan_state;
908 hv_err = sun4v_ldc_tx_get_state(lp->id,
913 ldcdbg(TX, " TX state[0x%02lx:0x%02lx] head[0x%04lx] tail[0x%04lx]\n",
914 orig_state, lp->chan_state, lp->tx_head, lp->tx_tail);
916 if (lp->cfg.mode == LDC_MODE_RAW &&
917 lp->chan_state == LDC_CHANNEL_UP) {
918 lp->hs_state = LDC_HS_COMPLETE;
919 ldc_set_state(lp, LDC_STATE_CONNECTED);
921 event_mask |= LDC_EVENT_UP;
924 spin_unlock_irqrestore(&lp->lock, flags);
926 send_events(lp, event_mask);
931 /* XXX ldc_alloc() and ldc_free() needs to run under a mutex so
932 * XXX that addition and removal from the ldc_channel_list has
933 * XXX atomicity, otherwise the __ldc_channel_exists() check is
934 * XXX totally pointless as another thread can slip into ldc_alloc()
935 * XXX and add a channel with the same ID. There also needs to be
936 * XXX a spinlock for ldc_channel_list.
938 static HLIST_HEAD(ldc_channel_list);
940 static int __ldc_channel_exists(unsigned long id)
942 struct ldc_channel *lp;
943 struct hlist_node *n;
945 hlist_for_each_entry(lp, n, &ldc_channel_list, list) {
952 static int alloc_queue(const char *name, unsigned long num_entries,
953 struct ldc_packet **base, unsigned long *ra)
955 unsigned long size, order;
958 size = num_entries * LDC_PACKET_SIZE;
959 order = get_order(size);
961 q = (void *) __get_free_pages(GFP_KERNEL, order);
963 printk(KERN_ERR PFX "Alloc of %s queue failed with "
964 "size=%lu order=%lu\n", name, size, order);
968 memset(q, 0, PAGE_SIZE << order);
976 static void free_queue(unsigned long num_entries, struct ldc_packet *q)
978 unsigned long size, order;
983 size = num_entries * LDC_PACKET_SIZE;
984 order = get_order(size);
986 free_pages((unsigned long)q, order);
989 /* XXX Make this configurable... XXX */
990 #define LDC_IOTABLE_SIZE (8 * 1024)
992 static int ldc_iommu_init(struct ldc_channel *lp)
994 unsigned long sz, num_tsb_entries, tsbsize, order;
995 struct ldc_iommu *iommu = &lp->iommu;
996 struct ldc_mtable_entry *table;
997 unsigned long hv_err;
1000 num_tsb_entries = LDC_IOTABLE_SIZE;
1001 tsbsize = num_tsb_entries * sizeof(struct ldc_mtable_entry);
1003 spin_lock_init(&iommu->lock);
1005 sz = num_tsb_entries / 8;
1006 sz = (sz + 7UL) & ~7UL;
1007 iommu->arena.map = kzalloc(sz, GFP_KERNEL);
1008 if (!iommu->arena.map) {
1009 printk(KERN_ERR PFX "Alloc of arena map failed, sz=%lu\n", sz);
1013 iommu->arena.limit = num_tsb_entries;
1015 order = get_order(tsbsize);
1017 table = (struct ldc_mtable_entry *)
1018 __get_free_pages(GFP_KERNEL, order);
1021 printk(KERN_ERR PFX "Alloc of MTE table failed, "
1022 "size=%lu order=%lu\n", tsbsize, order);
1026 memset(table, 0, PAGE_SIZE << order);
1028 iommu->page_table = table;
1030 hv_err = sun4v_ldc_set_map_table(lp->id, __pa(table),
1034 goto out_free_table;
1039 free_pages((unsigned long) table, order);
1040 iommu->page_table = NULL;
1043 kfree(iommu->arena.map);
1044 iommu->arena.map = NULL;
1049 static void ldc_iommu_release(struct ldc_channel *lp)
1051 struct ldc_iommu *iommu = &lp->iommu;
1052 unsigned long num_tsb_entries, tsbsize, order;
1054 (void) sun4v_ldc_set_map_table(lp->id, 0, 0);
1056 num_tsb_entries = iommu->arena.limit;
1057 tsbsize = num_tsb_entries * sizeof(struct ldc_mtable_entry);
1058 order = get_order(tsbsize);
1060 free_pages((unsigned long) iommu->page_table, order);
1061 iommu->page_table = NULL;
1063 kfree(iommu->arena.map);
1064 iommu->arena.map = NULL;
1067 struct ldc_channel *ldc_alloc(unsigned long id,
1068 const struct ldc_channel_config *cfgp,
1071 struct ldc_channel *lp;
1072 const struct ldc_mode_ops *mops;
1073 unsigned long dummy1, dummy2, hv_err;
1078 if (!ldom_domaining_enabled)
1085 switch (cfgp->mode) {
1088 mss = LDC_PACKET_SIZE;
1091 case LDC_MODE_UNRELIABLE:
1093 mss = LDC_PACKET_SIZE - 8;
1096 case LDC_MODE_RELIABLE:
1098 mss = LDC_PACKET_SIZE - 8 - 8;
1101 case LDC_MODE_STREAM:
1103 mss = LDC_PACKET_SIZE - 8 - 8;
1110 if (!cfgp->event || !event_arg || !cfgp->rx_irq || !cfgp->tx_irq)
1113 hv_err = sun4v_ldc_tx_qinfo(id, &dummy1, &dummy2);
1115 if (hv_err == HV_ECHANNEL)
1119 if (__ldc_channel_exists(id))
1124 lp = kzalloc(sizeof(*lp), GFP_KERNEL);
1129 spin_lock_init(&lp->lock);
1133 err = ldc_iommu_init(lp);
1142 lp->cfg.mtu = LDC_DEFAULT_MTU;
1144 if (lp->cfg.mode == LDC_MODE_STREAM) {
1145 mssbuf = kzalloc(lp->cfg.mtu, GFP_KERNEL);
1148 goto out_free_iommu;
1150 lp->mssbuf = mssbuf;
1153 lp->event_arg = event_arg;
1155 /* XXX allow setting via ldc_channel_config to override defaults
1156 * XXX or use some formula based upon mtu
1158 lp->tx_num_entries = LDC_DEFAULT_NUM_ENTRIES;
1159 lp->rx_num_entries = LDC_DEFAULT_NUM_ENTRIES;
1161 err = alloc_queue("TX", lp->tx_num_entries,
1162 &lp->tx_base, &lp->tx_ra);
1164 goto out_free_mssbuf;
1166 err = alloc_queue("RX", lp->rx_num_entries,
1167 &lp->rx_base, &lp->rx_ra);
1171 lp->flags |= LDC_FLAG_ALLOCED_QUEUES;
1173 lp->hs_state = LDC_HS_CLOSED;
1174 ldc_set_state(lp, LDC_STATE_INIT);
1176 INIT_HLIST_NODE(&lp->list);
1177 hlist_add_head(&lp->list, &ldc_channel_list);
1179 INIT_HLIST_HEAD(&lp->mh_list);
1184 free_queue(lp->tx_num_entries, lp->tx_base);
1191 ldc_iommu_release(lp);
1197 return ERR_PTR(err);
1199 EXPORT_SYMBOL(ldc_alloc);
1201 void ldc_free(struct ldc_channel *lp)
1203 if (lp->flags & LDC_FLAG_REGISTERED_IRQS) {
1204 free_irq(lp->cfg.rx_irq, lp);
1205 free_irq(lp->cfg.tx_irq, lp);
1208 if (lp->flags & LDC_FLAG_REGISTERED_QUEUES) {
1209 sun4v_ldc_tx_qconf(lp->id, 0, 0);
1210 sun4v_ldc_rx_qconf(lp->id, 0, 0);
1211 lp->flags &= ~LDC_FLAG_REGISTERED_QUEUES;
1213 if (lp->flags & LDC_FLAG_ALLOCED_QUEUES) {
1214 free_queue(lp->tx_num_entries, lp->tx_base);
1215 free_queue(lp->rx_num_entries, lp->rx_base);
1216 lp->flags &= ~LDC_FLAG_ALLOCED_QUEUES;
1219 hlist_del(&lp->list);
1224 ldc_iommu_release(lp);
1228 EXPORT_SYMBOL(ldc_free);
1230 /* Bind the channel. This registers the LDC queues with
1231 * the hypervisor and puts the channel into a pseudo-listening
1232 * state. This does not initiate a handshake, ldc_connect() does
1235 int ldc_bind(struct ldc_channel *lp)
1237 unsigned long hv_err, flags;
1240 spin_lock_irqsave(&lp->lock, flags);
1242 if (lp->state != LDC_STATE_INIT)
1245 err = request_irq(lp->cfg.rx_irq, ldc_rx,
1246 IRQF_SAMPLE_RANDOM | IRQF_SHARED,
1251 err = request_irq(lp->cfg.tx_irq, ldc_tx,
1252 IRQF_SAMPLE_RANDOM | IRQF_SHARED,
1255 goto out_free_rx_irq;
1258 lp->flags |= LDC_FLAG_REGISTERED_IRQS;
1261 hv_err = sun4v_ldc_tx_qconf(lp->id, 0, 0);
1263 goto out_free_tx_irq;
1265 hv_err = sun4v_ldc_tx_qconf(lp->id, lp->tx_ra, lp->tx_num_entries);
1267 goto out_free_tx_irq;
1269 hv_err = sun4v_ldc_rx_qconf(lp->id, 0, 0);
1273 hv_err = sun4v_ldc_rx_qconf(lp->id, lp->rx_ra, lp->rx_num_entries);
1277 lp->flags |= LDC_FLAG_REGISTERED_QUEUES;
1279 hv_err = sun4v_ldc_tx_get_state(lp->id,
1287 lp->tx_acked = lp->tx_head;
1289 lp->hs_state = LDC_HS_OPEN;
1290 ldc_set_state(lp, LDC_STATE_BOUND);
1292 spin_unlock_irqrestore(&lp->lock, flags);
1297 lp->flags &= ~LDC_FLAG_REGISTERED_QUEUES;
1298 sun4v_ldc_rx_qconf(lp->id, 0, 0);
1301 sun4v_ldc_tx_qconf(lp->id, 0, 0);
1304 lp->flags &= ~LDC_FLAG_REGISTERED_IRQS;
1305 free_irq(lp->cfg.tx_irq, lp);
1308 free_irq(lp->cfg.rx_irq, lp);
1311 spin_unlock_irqrestore(&lp->lock, flags);
1315 EXPORT_SYMBOL(ldc_bind);
1317 int ldc_connect(struct ldc_channel *lp)
1319 unsigned long flags;
1322 if (lp->cfg.mode == LDC_MODE_RAW)
1325 spin_lock_irqsave(&lp->lock, flags);
1327 if (!(lp->flags & LDC_FLAG_ALLOCED_QUEUES) ||
1328 !(lp->flags & LDC_FLAG_REGISTERED_QUEUES) ||
1329 lp->hs_state != LDC_HS_OPEN)
1332 err = start_handshake(lp);
1334 spin_unlock_irqrestore(&lp->lock, flags);
1338 EXPORT_SYMBOL(ldc_connect);
1340 int ldc_disconnect(struct ldc_channel *lp)
1342 unsigned long hv_err, flags;
1345 if (lp->cfg.mode == LDC_MODE_RAW)
1348 if (!(lp->flags & LDC_FLAG_ALLOCED_QUEUES) ||
1349 !(lp->flags & LDC_FLAG_REGISTERED_QUEUES))
1352 spin_lock_irqsave(&lp->lock, flags);
1355 hv_err = sun4v_ldc_tx_qconf(lp->id, 0, 0);
1359 hv_err = sun4v_ldc_tx_qconf(lp->id, lp->tx_ra, lp->tx_num_entries);
1363 hv_err = sun4v_ldc_rx_qconf(lp->id, 0, 0);
1367 hv_err = sun4v_ldc_rx_qconf(lp->id, lp->rx_ra, lp->rx_num_entries);
1371 ldc_set_state(lp, LDC_STATE_BOUND);
1372 lp->hs_state = LDC_HS_OPEN;
1373 lp->flags |= LDC_FLAG_RESET;
1375 spin_unlock_irqrestore(&lp->lock, flags);
1380 sun4v_ldc_tx_qconf(lp->id, 0, 0);
1381 sun4v_ldc_rx_qconf(lp->id, 0, 0);
1382 free_irq(lp->cfg.tx_irq, lp);
1383 free_irq(lp->cfg.rx_irq, lp);
1384 lp->flags &= ~(LDC_FLAG_REGISTERED_IRQS |
1385 LDC_FLAG_REGISTERED_QUEUES);
1386 ldc_set_state(lp, LDC_STATE_INIT);
1388 spin_unlock_irqrestore(&lp->lock, flags);
1392 EXPORT_SYMBOL(ldc_disconnect);
1394 int ldc_state(struct ldc_channel *lp)
1398 EXPORT_SYMBOL(ldc_state);
1400 static int write_raw(struct ldc_channel *lp, const void *buf, unsigned int size)
1402 struct ldc_packet *p;
1403 unsigned long new_tail;
1406 if (size > LDC_PACKET_SIZE)
1409 p = data_get_tx_packet(lp, &new_tail);
1413 memcpy(p, buf, size);
1415 err = send_tx_packet(lp, p, new_tail);
1422 static int read_raw(struct ldc_channel *lp, void *buf, unsigned int size)
1424 struct ldc_packet *p;
1425 unsigned long hv_err, new;
1428 if (size < LDC_PACKET_SIZE)
1431 hv_err = sun4v_ldc_rx_get_state(lp->id,
1436 return ldc_abort(lp);
1438 if (lp->chan_state == LDC_CHANNEL_DOWN ||
1439 lp->chan_state == LDC_CHANNEL_RESETTING)
1442 if (lp->rx_head == lp->rx_tail)
1445 p = lp->rx_base + (lp->rx_head / LDC_PACKET_SIZE);
1446 memcpy(buf, p, LDC_PACKET_SIZE);
1448 new = rx_advance(lp, lp->rx_head);
1451 err = __set_rx_head(lp, new);
1455 err = LDC_PACKET_SIZE;
1460 static const struct ldc_mode_ops raw_ops = {
1465 static int write_nonraw(struct ldc_channel *lp, const void *buf,
1468 unsigned long hv_err, tail;
1469 unsigned int copied;
1473 hv_err = sun4v_ldc_tx_get_state(lp->id, &lp->tx_head, &lp->tx_tail,
1475 if (unlikely(hv_err))
1478 if (unlikely(lp->chan_state != LDC_CHANNEL_UP))
1479 return ldc_abort(lp);
1481 if (!tx_has_space_for(lp, size))
1487 while (copied < size) {
1488 struct ldc_packet *p = lp->tx_base + (tail / LDC_PACKET_SIZE);
1489 u8 *data = ((lp->cfg.mode == LDC_MODE_UNRELIABLE) ?
1495 p->stype = LDC_INFO;
1498 data_len = size - copied;
1499 if (data_len > lp->mss)
1502 BUG_ON(data_len > LDC_LEN);
1504 p->env = (data_len |
1505 (copied == 0 ? LDC_START : 0) |
1506 (data_len == size - copied ? LDC_STOP : 0));
1510 ldcdbg(DATA, "SENT DATA [%02x:%02x:%02x:%02x:%08x]\n",
1517 memcpy(data, buf, data_len);
1521 tail = tx_advance(lp, tail);
1524 err = set_tx_tail(lp, tail);
1533 static int rx_bad_seq(struct ldc_channel *lp, struct ldc_packet *p,
1534 struct ldc_packet *first_frag)
1539 lp->rcv_nxt = first_frag->seqid - 1;
1541 err = send_data_nack(lp, p);
1545 err = __set_rx_head(lp, lp->rx_tail);
1547 return ldc_abort(lp);
1552 static int data_ack_nack(struct ldc_channel *lp, struct ldc_packet *p)
1554 if (p->stype & LDC_ACK) {
1555 int err = process_data_ack(lp, p);
1559 if (p->stype & LDC_NACK)
1560 return ldc_abort(lp);
1565 static int rx_data_wait(struct ldc_channel *lp, unsigned long cur_head)
1567 unsigned long dummy;
1570 ldcdbg(DATA, "DATA WAIT cur_head[%lx] rx_head[%lx] rx_tail[%lx]\n",
1571 cur_head, lp->rx_head, lp->rx_tail);
1572 while (limit-- > 0) {
1573 unsigned long hv_err;
1575 hv_err = sun4v_ldc_rx_get_state(lp->id,
1580 return ldc_abort(lp);
1582 ldcdbg(DATA, "REREAD head[%lx] tail[%lx] chan_state[%lx]\n",
1583 dummy, lp->rx_tail, lp->chan_state);
1585 if (lp->chan_state == LDC_CHANNEL_DOWN ||
1586 lp->chan_state == LDC_CHANNEL_RESETTING)
1589 if (cur_head != lp->rx_tail) {
1590 ldcdbg(DATA, "DATA WAIT DONE\n");
1599 static int rx_set_head(struct ldc_channel *lp, unsigned long head)
1601 int err = __set_rx_head(lp, head);
1604 return ldc_abort(lp);
1610 static int read_nonraw(struct ldc_channel *lp, void *buf, unsigned int size)
1612 struct ldc_packet *first_frag;
1613 unsigned long hv_err, new;
1616 hv_err = sun4v_ldc_rx_get_state(lp->id,
1621 return ldc_abort(lp);
1623 if (lp->chan_state == LDC_CHANNEL_DOWN ||
1624 lp->chan_state == LDC_CHANNEL_RESETTING)
1627 if (lp->rx_head == lp->rx_tail)
1634 struct ldc_packet *p;
1637 BUG_ON(new == lp->rx_tail);
1638 p = lp->rx_base + (new / LDC_PACKET_SIZE);
1640 ldcdbg(RX, "RX read pkt[%02x:%02x:%02x:%02x:%08x] "
1649 if (unlikely(!rx_seq_ok(lp, p->seqid))) {
1650 err = rx_bad_seq(lp, p, first_frag);
1655 if (p->type & LDC_CTRL) {
1656 err = process_control_frame(lp, p);
1662 lp->rcv_nxt = p->seqid;
1664 if (!(p->type & LDC_DATA)) {
1665 new = rx_advance(lp, new);
1668 if (p->stype & (LDC_ACK | LDC_NACK)) {
1669 err = data_ack_nack(lp, p);
1673 if (!(p->stype & LDC_INFO)) {
1674 new = rx_advance(lp, new);
1678 pkt_len = p->env & LDC_LEN;
1680 /* Every initial packet starts with the START bit set.
1682 * Singleton packets will have both START+STOP set.
1684 * Fragments will have START set in the first frame, STOP
1685 * set in the last frame, and neither bit set in middle
1686 * frames of the packet.
1688 * Therefore if we are at the beginning of a packet and
1689 * we don't see START, or we are in the middle of a fragmented
1690 * packet and do see START, we are unsynchronized and should
1691 * flush the RX queue.
1693 if ((first_frag == NULL && !(p->env & LDC_START)) ||
1694 (first_frag != NULL && (p->env & LDC_START))) {
1696 new = rx_advance(lp, new);
1698 err = rx_set_head(lp, new);
1708 if (pkt_len > size - copied) {
1709 /* User didn't give us a big enough buffer,
1710 * what to do? This is a pretty serious error.
1712 * Since we haven't updated the RX ring head to
1713 * consume any of the packets, signal the error
1714 * to the user and just leave the RX ring alone.
1716 * This seems the best behavior because this allows
1717 * a user of the LDC layer to start with a small
1718 * RX buffer for ldc_read() calls and use -EMSGSIZE
1719 * as a cue to enlarge it's read buffer.
1725 /* Ok, we are gonna eat this one. */
1726 new = rx_advance(lp, new);
1729 (lp->cfg.mode == LDC_MODE_UNRELIABLE ?
1730 p->u.u_data : p->u.r.r_data), pkt_len);
1734 if (p->env & LDC_STOP)
1738 if (new == lp->rx_tail) {
1739 err = rx_data_wait(lp, new);
1746 err = rx_set_head(lp, new);
1748 if (err && first_frag)
1749 lp->rcv_nxt = first_frag->seqid - 1;
1757 static const struct ldc_mode_ops nonraw_ops = {
1758 .write = write_nonraw,
1759 .read = read_nonraw,
1762 static int write_stream(struct ldc_channel *lp, const void *buf,
1765 if (size > lp->cfg.mtu)
1767 return write_nonraw(lp, buf, size);
1770 static int read_stream(struct ldc_channel *lp, void *buf, unsigned int size)
1772 if (!lp->mssbuf_len) {
1773 int err = read_nonraw(lp, lp->mssbuf,
1774 (size > lp->cfg.mtu ?
1775 lp->cfg.mtu : size));
1779 lp->mssbuf_len = err;
1783 if (size > lp->mssbuf_len)
1784 size = lp->mssbuf_len;
1785 memcpy(buf, lp->mssbuf + lp->mssbuf_off, size);
1787 lp->mssbuf_off += size;
1788 lp->mssbuf_len -= size;
1793 static const struct ldc_mode_ops stream_ops = {
1794 .write = write_stream,
1795 .read = read_stream,
1798 int ldc_write(struct ldc_channel *lp, const void *buf, unsigned int size)
1800 unsigned long flags;
1809 spin_lock_irqsave(&lp->lock, flags);
1811 if (lp->hs_state != LDC_HS_COMPLETE)
1814 err = lp->mops->write(lp, buf, size);
1816 spin_unlock_irqrestore(&lp->lock, flags);
1820 EXPORT_SYMBOL(ldc_write);
1822 int ldc_read(struct ldc_channel *lp, void *buf, unsigned int size)
1824 unsigned long flags;
1833 spin_lock_irqsave(&lp->lock, flags);
1835 if (lp->hs_state != LDC_HS_COMPLETE)
1838 err = lp->mops->read(lp, buf, size);
1840 spin_unlock_irqrestore(&lp->lock, flags);
1844 EXPORT_SYMBOL(ldc_read);
1846 static long arena_alloc(struct ldc_iommu *iommu, unsigned long npages)
1848 struct iommu_arena *arena = &iommu->arena;
1849 unsigned long n, i, start, end, limit;
1852 limit = arena->limit;
1853 start = arena->hint;
1857 n = find_next_zero_bit(arena->map, limit, start);
1859 if (unlikely(end >= limit)) {
1860 if (likely(pass < 1)) {
1866 /* Scanned the whole thing, give up. */
1871 for (i = n; i < end; i++) {
1872 if (test_bit(i, arena->map)) {
1878 for (i = n; i < end; i++)
1879 __set_bit(i, arena->map);
1886 #define COOKIE_PGSZ_CODE 0xf000000000000000ULL
1887 #define COOKIE_PGSZ_CODE_SHIFT 60ULL
1889 static u64 pagesize_code(void)
1891 switch (PAGE_SIZE) {
1893 case (8ULL * 1024ULL):
1895 case (64ULL * 1024ULL):
1897 case (512ULL * 1024ULL):
1899 case (4ULL * 1024ULL * 1024ULL):
1901 case (32ULL * 1024ULL * 1024ULL):
1903 case (256ULL * 1024ULL * 1024ULL):
1908 static u64 make_cookie(u64 index, u64 pgsz_code, u64 page_offset)
1910 return ((pgsz_code << COOKIE_PGSZ_CODE_SHIFT) |
1911 (index << PAGE_SHIFT) |
1915 static u64 cookie_to_index(u64 cookie, unsigned long *shift)
1917 u64 szcode = cookie >> COOKIE_PGSZ_CODE_SHIFT;
1919 cookie &= ~COOKIE_PGSZ_CODE;
1921 *shift = szcode * 3;
1923 return (cookie >> (13ULL + (szcode * 3ULL)));
1926 static struct ldc_mtable_entry *alloc_npages(struct ldc_iommu *iommu,
1927 unsigned long npages)
1931 entry = arena_alloc(iommu, npages);
1932 if (unlikely(entry < 0))
1935 return iommu->page_table + entry;
1938 static u64 perm_to_mte(unsigned int map_perm)
1942 mte_base = pagesize_code();
1944 if (map_perm & LDC_MAP_SHADOW) {
1945 if (map_perm & LDC_MAP_R)
1946 mte_base |= LDC_MTE_COPY_R;
1947 if (map_perm & LDC_MAP_W)
1948 mte_base |= LDC_MTE_COPY_W;
1950 if (map_perm & LDC_MAP_DIRECT) {
1951 if (map_perm & LDC_MAP_R)
1952 mte_base |= LDC_MTE_READ;
1953 if (map_perm & LDC_MAP_W)
1954 mte_base |= LDC_MTE_WRITE;
1955 if (map_perm & LDC_MAP_X)
1956 mte_base |= LDC_MTE_EXEC;
1958 if (map_perm & LDC_MAP_IO) {
1959 if (map_perm & LDC_MAP_R)
1960 mte_base |= LDC_MTE_IOMMU_R;
1961 if (map_perm & LDC_MAP_W)
1962 mte_base |= LDC_MTE_IOMMU_W;
1968 static int pages_in_region(unsigned long base, long len)
1973 unsigned long new = (base + PAGE_SIZE) & PAGE_MASK;
1975 len -= (new - base);
1983 struct cookie_state {
1984 struct ldc_mtable_entry *page_table;
1985 struct ldc_trans_cookie *cookies;
1992 static void fill_cookies(struct cookie_state *sp, unsigned long pa,
1993 unsigned long off, unsigned long len)
1996 unsigned long tlen, new = pa + PAGE_SIZE;
1999 sp->page_table[sp->pte_idx].mte = sp->mte_base | pa;
2003 tlen = PAGE_SIZE - off;
2007 this_cookie = make_cookie(sp->pte_idx,
2008 pagesize_code(), off);
2012 if (this_cookie == sp->prev_cookie) {
2013 sp->cookies[sp->nc - 1].cookie_size += tlen;
2015 sp->cookies[sp->nc].cookie_addr = this_cookie;
2016 sp->cookies[sp->nc].cookie_size = tlen;
2019 sp->prev_cookie = this_cookie + tlen;
2028 static int sg_count_one(struct scatterlist *sg)
2030 unsigned long base = page_to_pfn(sg->page) << PAGE_SHIFT;
2031 long len = sg->length;
2033 if ((sg->offset | len) & (8UL - 1))
2036 return pages_in_region(base + sg->offset, len);
2039 static int sg_count_pages(struct scatterlist *sg, int num_sg)
2045 for (i = 0; i < num_sg; i++) {
2046 int err = sg_count_one(sg + i);
2055 int ldc_map_sg(struct ldc_channel *lp,
2056 struct scatterlist *sg, int num_sg,
2057 struct ldc_trans_cookie *cookies, int ncookies,
2058 unsigned int map_perm)
2060 unsigned long i, npages, flags;
2061 struct ldc_mtable_entry *base;
2062 struct cookie_state state;
2063 struct ldc_iommu *iommu;
2066 if (map_perm & ~LDC_MAP_ALL)
2069 err = sg_count_pages(sg, num_sg);
2079 spin_lock_irqsave(&iommu->lock, flags);
2080 base = alloc_npages(iommu, npages);
2081 spin_unlock_irqrestore(&iommu->lock, flags);
2086 state.page_table = iommu->page_table;
2087 state.cookies = cookies;
2088 state.mte_base = perm_to_mte(map_perm);
2089 state.prev_cookie = ~(u64)0;
2090 state.pte_idx = (base - iommu->page_table);
2093 for (i = 0; i < num_sg; i++)
2094 fill_cookies(&state, page_to_pfn(sg[i].page) << PAGE_SHIFT,
2095 sg[i].offset, sg[i].length);
2099 EXPORT_SYMBOL(ldc_map_sg);
2101 int ldc_map_single(struct ldc_channel *lp,
2102 void *buf, unsigned int len,
2103 struct ldc_trans_cookie *cookies, int ncookies,
2104 unsigned int map_perm)
2106 unsigned long npages, pa, flags;
2107 struct ldc_mtable_entry *base;
2108 struct cookie_state state;
2109 struct ldc_iommu *iommu;
2111 if ((map_perm & ~LDC_MAP_ALL) || (ncookies < 1))
2115 if ((pa | len) & (8UL - 1))
2118 npages = pages_in_region(pa, len);
2122 spin_lock_irqsave(&iommu->lock, flags);
2123 base = alloc_npages(iommu, npages);
2124 spin_unlock_irqrestore(&iommu->lock, flags);
2129 state.page_table = iommu->page_table;
2130 state.cookies = cookies;
2131 state.mte_base = perm_to_mte(map_perm);
2132 state.prev_cookie = ~(u64)0;
2133 state.pte_idx = (base - iommu->page_table);
2135 fill_cookies(&state, (pa & PAGE_MASK), (pa & ~PAGE_MASK), len);
2136 BUG_ON(state.nc != 1);
2140 EXPORT_SYMBOL(ldc_map_single);
2142 static void free_npages(unsigned long id, struct ldc_iommu *iommu,
2143 u64 cookie, u64 size)
2145 struct iommu_arena *arena = &iommu->arena;
2146 unsigned long i, shift, index, npages;
2147 struct ldc_mtable_entry *base;
2149 npages = PAGE_ALIGN(((cookie & ~PAGE_MASK) + size)) >> PAGE_SHIFT;
2150 index = cookie_to_index(cookie, &shift);
2151 base = iommu->page_table + index;
2153 BUG_ON(index > arena->limit ||
2154 (index + npages) > arena->limit);
2156 for (i = 0; i < npages; i++) {
2158 sun4v_ldc_revoke(id, cookie + (i << shift),
2161 __clear_bit(index + i, arena->map);
2165 void ldc_unmap(struct ldc_channel *lp, struct ldc_trans_cookie *cookies,
2168 struct ldc_iommu *iommu = &lp->iommu;
2169 unsigned long flags;
2172 spin_lock_irqsave(&iommu->lock, flags);
2173 for (i = 0; i < ncookies; i++) {
2174 u64 addr = cookies[i].cookie_addr;
2175 u64 size = cookies[i].cookie_size;
2177 free_npages(lp->id, iommu, addr, size);
2179 spin_unlock_irqrestore(&iommu->lock, flags);
2181 EXPORT_SYMBOL(ldc_unmap);
2183 int ldc_copy(struct ldc_channel *lp, int copy_dir,
2184 void *buf, unsigned int len, unsigned long offset,
2185 struct ldc_trans_cookie *cookies, int ncookies)
2187 unsigned int orig_len;
2191 if (copy_dir != LDC_COPY_IN && copy_dir != LDC_COPY_OUT) {
2192 printk(KERN_ERR PFX "ldc_copy: ID[%lu] Bad copy_dir[%d]\n",
2198 if ((ra | len | offset) & (8UL - 1)) {
2199 printk(KERN_ERR PFX "ldc_copy: ID[%lu] Unaligned buffer "
2200 "ra[%lx] len[%x] offset[%lx]\n",
2201 lp->id, ra, len, offset);
2205 if (lp->hs_state != LDC_HS_COMPLETE ||
2206 (lp->flags & LDC_FLAG_RESET)) {
2207 printk(KERN_ERR PFX "ldc_copy: ID[%lu] Link down hs_state[%x] "
2208 "flags[%x]\n", lp->id, lp->hs_state, lp->flags);
2213 for (i = 0; i < ncookies; i++) {
2214 unsigned long cookie_raddr = cookies[i].cookie_addr;
2215 unsigned long this_len = cookies[i].cookie_size;
2216 unsigned long actual_len;
2218 if (unlikely(offset)) {
2219 unsigned long this_off = offset;
2221 if (this_off > this_len)
2222 this_off = this_len;
2225 this_len -= this_off;
2228 cookie_raddr += this_off;
2235 unsigned long hv_err;
2237 hv_err = sun4v_ldc_copy(lp->id, copy_dir,
2239 this_len, &actual_len);
2240 if (unlikely(hv_err)) {
2241 printk(KERN_ERR PFX "ldc_copy: ID[%lu] "
2244 if (lp->hs_state != LDC_HS_COMPLETE ||
2245 (lp->flags & LDC_FLAG_RESET))
2251 cookie_raddr += actual_len;
2254 if (actual_len == this_len)
2257 this_len -= actual_len;
2264 /* It is caller policy what to do about short copies.
2265 * For example, a networking driver can declare the
2266 * packet a runt and drop it.
2269 return orig_len - len;
2271 EXPORT_SYMBOL(ldc_copy);
2273 void *ldc_alloc_exp_dring(struct ldc_channel *lp, unsigned int len,
2274 struct ldc_trans_cookie *cookies, int *ncookies,
2275 unsigned int map_perm)
2280 if (len & (8UL - 1))
2281 return ERR_PTR(-EINVAL);
2283 buf = kzalloc(len, GFP_KERNEL);
2285 return ERR_PTR(-ENOMEM);
2287 err = ldc_map_single(lp, buf, len, cookies, *ncookies, map_perm);
2290 return ERR_PTR(err);
2296 EXPORT_SYMBOL(ldc_alloc_exp_dring);
2298 void ldc_free_exp_dring(struct ldc_channel *lp, void *buf, unsigned int len,
2299 struct ldc_trans_cookie *cookies, int ncookies)
2301 ldc_unmap(lp, cookies, ncookies);
2304 EXPORT_SYMBOL(ldc_free_exp_dring);
2306 static int __init ldc_init(void)
2308 struct mdesc_node *mp;
2309 unsigned long major, minor;
2312 mp = md_find_node_by_name(NULL, "platform");
2316 v = md_get_property(mp, "domaining-enabled", NULL);
2322 if (sun4v_hvapi_register(HV_GRP_LDOM, major, &minor)) {
2323 printk(KERN_INFO PFX "Could not register LDOM hvapi.\n");
2327 printk(KERN_INFO "%s", version);
2330 printk(KERN_INFO PFX "Domaining disabled.\n");
2333 ldom_domaining_enabled = 1;
2338 core_initcall(ldc_init);