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