xref: /openbmc/linux/arch/sparc/kernel/ldc.c (revision 6c95483b)
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 		/*
817 		 * Generate an LDC_EVENT_UP event if the channel
818 		 * was not already up.
819 		 */
820 		if (orig_state != LDC_CHANNEL_UP) {
821 			event_mask |= LDC_EVENT_UP;
822 			orig_state = lp->chan_state;
823 		}
824 	}
825 
826 	/* If we are in reset state, flush the RX queue and ignore
827 	 * everything.
828 	 */
829 	if (lp->flags & LDC_FLAG_RESET) {
830 		(void) ldc_rx_reset(lp);
831 		goto out;
832 	}
833 
834 	/* Once we finish the handshake, we let the ldc_read()
835 	 * paths do all of the control frame and state management.
836 	 * Just trigger the callback.
837 	 */
838 	if (lp->hs_state == LDC_HS_COMPLETE) {
839 handshake_complete:
840 		if (lp->chan_state != orig_state) {
841 			unsigned int event = LDC_EVENT_RESET;
842 
843 			if (lp->chan_state == LDC_CHANNEL_UP)
844 				event = LDC_EVENT_UP;
845 
846 			event_mask |= event;
847 		}
848 		if (lp->rx_head != lp->rx_tail)
849 			event_mask |= LDC_EVENT_DATA_READY;
850 
851 		goto out;
852 	}
853 
854 	if (lp->chan_state != orig_state)
855 		goto out;
856 
857 	while (lp->rx_head != lp->rx_tail) {
858 		struct ldc_packet *p;
859 		unsigned long new;
860 		int err;
861 
862 		p = lp->rx_base + (lp->rx_head / LDC_PACKET_SIZE);
863 
864 		switch (p->type) {
865 		case LDC_CTRL:
866 			err = process_control_frame(lp, p);
867 			if (err > 0)
868 				event_mask |= err;
869 			break;
870 
871 		case LDC_DATA:
872 			event_mask |= LDC_EVENT_DATA_READY;
873 			err = 0;
874 			break;
875 
876 		case LDC_ERR:
877 			err = process_error_frame(lp, p);
878 			break;
879 
880 		default:
881 			err = LDC_ABORT(lp);
882 			break;
883 		}
884 
885 		if (err < 0)
886 			break;
887 
888 		new = lp->rx_head;
889 		new += LDC_PACKET_SIZE;
890 		if (new == (lp->rx_num_entries * LDC_PACKET_SIZE))
891 			new = 0;
892 		lp->rx_head = new;
893 
894 		err = __set_rx_head(lp, new);
895 		if (err < 0) {
896 			(void) LDC_ABORT(lp);
897 			break;
898 		}
899 		if (lp->hs_state == LDC_HS_COMPLETE)
900 			goto handshake_complete;
901 	}
902 
903 out:
904 	spin_unlock_irqrestore(&lp->lock, flags);
905 
906 	send_events(lp, event_mask);
907 
908 	return IRQ_HANDLED;
909 }
910 
911 static irqreturn_t ldc_tx(int irq, void *dev_id)
912 {
913 	struct ldc_channel *lp = dev_id;
914 	unsigned long flags, orig_state;
915 	unsigned int event_mask = 0;
916 
917 	spin_lock_irqsave(&lp->lock, flags);
918 
919 	orig_state = lp->chan_state;
920 
921 	/* We should probably check for hypervisor errors here and
922 	 * reset the LDC channel if we get one.
923 	 */
924 	sun4v_ldc_tx_get_state(lp->id,
925 			       &lp->tx_head,
926 			       &lp->tx_tail,
927 			       &lp->chan_state);
928 
929 	ldcdbg(TX, " TX state[0x%02lx:0x%02lx] head[0x%04lx] tail[0x%04lx]\n",
930 	       orig_state, lp->chan_state, lp->tx_head, lp->tx_tail);
931 
932 	if (lp->cfg.mode == LDC_MODE_RAW &&
933 	    lp->chan_state == LDC_CHANNEL_UP) {
934 		lp->hs_state = LDC_HS_COMPLETE;
935 		ldc_set_state(lp, LDC_STATE_CONNECTED);
936 
937 		/*
938 		 * Generate an LDC_EVENT_UP event if the channel
939 		 * was not already up.
940 		 */
941 		if (orig_state != LDC_CHANNEL_UP) {
942 			event_mask |= LDC_EVENT_UP;
943 			orig_state = lp->chan_state;
944 		}
945 	}
946 
947 	spin_unlock_irqrestore(&lp->lock, flags);
948 
949 	send_events(lp, event_mask);
950 
951 	return IRQ_HANDLED;
952 }
953 
954 /* XXX ldc_alloc() and ldc_free() needs to run under a mutex so
955  * XXX that addition and removal from the ldc_channel_list has
956  * XXX atomicity, otherwise the __ldc_channel_exists() check is
957  * XXX totally pointless as another thread can slip into ldc_alloc()
958  * XXX and add a channel with the same ID.  There also needs to be
959  * XXX a spinlock for ldc_channel_list.
960  */
961 static HLIST_HEAD(ldc_channel_list);
962 
963 static int __ldc_channel_exists(unsigned long id)
964 {
965 	struct ldc_channel *lp;
966 
967 	hlist_for_each_entry(lp, &ldc_channel_list, list) {
968 		if (lp->id == id)
969 			return 1;
970 	}
971 	return 0;
972 }
973 
974 static int alloc_queue(const char *name, unsigned long num_entries,
975 		       struct ldc_packet **base, unsigned long *ra)
976 {
977 	unsigned long size, order;
978 	void *q;
979 
980 	size = num_entries * LDC_PACKET_SIZE;
981 	order = get_order(size);
982 
983 	q = (void *) __get_free_pages(GFP_KERNEL, order);
984 	if (!q) {
985 		printk(KERN_ERR PFX "Alloc of %s queue failed with "
986 		       "size=%lu order=%lu\n", name, size, order);
987 		return -ENOMEM;
988 	}
989 
990 	memset(q, 0, PAGE_SIZE << order);
991 
992 	*base = q;
993 	*ra = __pa(q);
994 
995 	return 0;
996 }
997 
998 static void free_queue(unsigned long num_entries, struct ldc_packet *q)
999 {
1000 	unsigned long size, order;
1001 
1002 	if (!q)
1003 		return;
1004 
1005 	size = num_entries * LDC_PACKET_SIZE;
1006 	order = get_order(size);
1007 
1008 	free_pages((unsigned long)q, order);
1009 }
1010 
1011 static unsigned long ldc_cookie_to_index(u64 cookie, void *arg)
1012 {
1013 	u64 szcode = cookie >> COOKIE_PGSZ_CODE_SHIFT;
1014 	/* struct ldc_iommu *ldc_iommu = (struct ldc_iommu *)arg; */
1015 
1016 	cookie &= ~COOKIE_PGSZ_CODE;
1017 
1018 	return (cookie >> (13ULL + (szcode * 3ULL)));
1019 }
1020 
1021 static void ldc_demap(struct ldc_iommu *iommu, unsigned long id, u64 cookie,
1022 		      unsigned long entry, unsigned long npages)
1023 {
1024 	struct ldc_mtable_entry *base;
1025 	unsigned long i, shift;
1026 
1027 	shift = (cookie >> COOKIE_PGSZ_CODE_SHIFT) * 3;
1028 	base = iommu->page_table + entry;
1029 	for (i = 0; i < npages; i++) {
1030 		if (base->cookie)
1031 			sun4v_ldc_revoke(id, cookie + (i << shift),
1032 					 base->cookie);
1033 		base->mte = 0;
1034 	}
1035 }
1036 
1037 /* XXX Make this configurable... XXX */
1038 #define LDC_IOTABLE_SIZE	(8 * 1024)
1039 
1040 static int ldc_iommu_init(const char *name, struct ldc_channel *lp)
1041 {
1042 	unsigned long sz, num_tsb_entries, tsbsize, order;
1043 	struct ldc_iommu *ldc_iommu = &lp->iommu;
1044 	struct iommu_map_table *iommu = &ldc_iommu->iommu_map_table;
1045 	struct ldc_mtable_entry *table;
1046 	unsigned long hv_err;
1047 	int err;
1048 
1049 	num_tsb_entries = LDC_IOTABLE_SIZE;
1050 	tsbsize = num_tsb_entries * sizeof(struct ldc_mtable_entry);
1051 	spin_lock_init(&ldc_iommu->lock);
1052 
1053 	sz = num_tsb_entries / 8;
1054 	sz = (sz + 7UL) & ~7UL;
1055 	iommu->map = kzalloc(sz, GFP_KERNEL);
1056 	if (!iommu->map) {
1057 		printk(KERN_ERR PFX "Alloc of arena map failed, sz=%lu\n", sz);
1058 		return -ENOMEM;
1059 	}
1060 	iommu_tbl_pool_init(iommu, num_tsb_entries, PAGE_SHIFT,
1061 			    NULL, false /* no large pool */,
1062 			    1 /* npools */,
1063 			    true /* skip span boundary check */);
1064 
1065 	order = get_order(tsbsize);
1066 
1067 	table = (struct ldc_mtable_entry *)
1068 		__get_free_pages(GFP_KERNEL, order);
1069 	err = -ENOMEM;
1070 	if (!table) {
1071 		printk(KERN_ERR PFX "Alloc of MTE table failed, "
1072 		       "size=%lu order=%lu\n", tsbsize, order);
1073 		goto out_free_map;
1074 	}
1075 
1076 	memset(table, 0, PAGE_SIZE << order);
1077 
1078 	ldc_iommu->page_table = table;
1079 
1080 	hv_err = sun4v_ldc_set_map_table(lp->id, __pa(table),
1081 					 num_tsb_entries);
1082 	err = -EINVAL;
1083 	if (hv_err)
1084 		goto out_free_table;
1085 
1086 	return 0;
1087 
1088 out_free_table:
1089 	free_pages((unsigned long) table, order);
1090 	ldc_iommu->page_table = NULL;
1091 
1092 out_free_map:
1093 	kfree(iommu->map);
1094 	iommu->map = NULL;
1095 
1096 	return err;
1097 }
1098 
1099 static void ldc_iommu_release(struct ldc_channel *lp)
1100 {
1101 	struct ldc_iommu *ldc_iommu = &lp->iommu;
1102 	struct iommu_map_table *iommu = &ldc_iommu->iommu_map_table;
1103 	unsigned long num_tsb_entries, tsbsize, order;
1104 
1105 	(void) sun4v_ldc_set_map_table(lp->id, 0, 0);
1106 
1107 	num_tsb_entries = iommu->poolsize * iommu->nr_pools;
1108 	tsbsize = num_tsb_entries * sizeof(struct ldc_mtable_entry);
1109 	order = get_order(tsbsize);
1110 
1111 	free_pages((unsigned long) ldc_iommu->page_table, order);
1112 	ldc_iommu->page_table = NULL;
1113 
1114 	kfree(iommu->map);
1115 	iommu->map = NULL;
1116 }
1117 
1118 struct ldc_channel *ldc_alloc(unsigned long id,
1119 			      const struct ldc_channel_config *cfgp,
1120 			      void *event_arg,
1121 			      const char *name)
1122 {
1123 	struct ldc_channel *lp;
1124 	const struct ldc_mode_ops *mops;
1125 	unsigned long dummy1, dummy2, hv_err;
1126 	u8 mss, *mssbuf;
1127 	int err;
1128 
1129 	err = -ENODEV;
1130 	if (!ldom_domaining_enabled)
1131 		goto out_err;
1132 
1133 	err = -EINVAL;
1134 	if (!cfgp)
1135 		goto out_err;
1136 	if (!name)
1137 		goto out_err;
1138 
1139 	switch (cfgp->mode) {
1140 	case LDC_MODE_RAW:
1141 		mops = &raw_ops;
1142 		mss = LDC_PACKET_SIZE;
1143 		break;
1144 
1145 	case LDC_MODE_UNRELIABLE:
1146 		mops = &nonraw_ops;
1147 		mss = LDC_PACKET_SIZE - 8;
1148 		break;
1149 
1150 	case LDC_MODE_STREAM:
1151 		mops = &stream_ops;
1152 		mss = LDC_PACKET_SIZE - 8 - 8;
1153 		break;
1154 
1155 	default:
1156 		goto out_err;
1157 	}
1158 
1159 	if (!cfgp->event || !event_arg || !cfgp->rx_irq || !cfgp->tx_irq)
1160 		goto out_err;
1161 
1162 	hv_err = sun4v_ldc_tx_qinfo(id, &dummy1, &dummy2);
1163 	err = -ENODEV;
1164 	if (hv_err == HV_ECHANNEL)
1165 		goto out_err;
1166 
1167 	err = -EEXIST;
1168 	if (__ldc_channel_exists(id))
1169 		goto out_err;
1170 
1171 	mssbuf = NULL;
1172 
1173 	lp = kzalloc(sizeof(*lp), GFP_KERNEL);
1174 	err = -ENOMEM;
1175 	if (!lp)
1176 		goto out_err;
1177 
1178 	spin_lock_init(&lp->lock);
1179 
1180 	lp->id = id;
1181 
1182 	err = ldc_iommu_init(name, lp);
1183 	if (err)
1184 		goto out_free_ldc;
1185 
1186 	lp->mops = mops;
1187 	lp->mss = mss;
1188 
1189 	lp->cfg = *cfgp;
1190 	if (!lp->cfg.mtu)
1191 		lp->cfg.mtu = LDC_DEFAULT_MTU;
1192 
1193 	if (lp->cfg.mode == LDC_MODE_STREAM) {
1194 		mssbuf = kzalloc(lp->cfg.mtu, GFP_KERNEL);
1195 		if (!mssbuf) {
1196 			err = -ENOMEM;
1197 			goto out_free_iommu;
1198 		}
1199 		lp->mssbuf = mssbuf;
1200 	}
1201 
1202 	lp->event_arg = event_arg;
1203 
1204 	/* XXX allow setting via ldc_channel_config to override defaults
1205 	 * XXX or use some formula based upon mtu
1206 	 */
1207 	lp->tx_num_entries = LDC_DEFAULT_NUM_ENTRIES;
1208 	lp->rx_num_entries = LDC_DEFAULT_NUM_ENTRIES;
1209 
1210 	err = alloc_queue("TX", lp->tx_num_entries,
1211 			  &lp->tx_base, &lp->tx_ra);
1212 	if (err)
1213 		goto out_free_mssbuf;
1214 
1215 	err = alloc_queue("RX", lp->rx_num_entries,
1216 			  &lp->rx_base, &lp->rx_ra);
1217 	if (err)
1218 		goto out_free_txq;
1219 
1220 	lp->flags |= LDC_FLAG_ALLOCED_QUEUES;
1221 
1222 	lp->hs_state = LDC_HS_CLOSED;
1223 	ldc_set_state(lp, LDC_STATE_INIT);
1224 
1225 	INIT_HLIST_NODE(&lp->list);
1226 	hlist_add_head(&lp->list, &ldc_channel_list);
1227 
1228 	INIT_HLIST_HEAD(&lp->mh_list);
1229 
1230 	snprintf(lp->rx_irq_name, LDC_IRQ_NAME_MAX, "%s RX", name);
1231 	snprintf(lp->tx_irq_name, LDC_IRQ_NAME_MAX, "%s TX", name);
1232 
1233 	err = request_irq(lp->cfg.rx_irq, ldc_rx, 0,
1234 			  lp->rx_irq_name, lp);
1235 	if (err)
1236 		goto out_free_txq;
1237 
1238 	err = request_irq(lp->cfg.tx_irq, ldc_tx, 0,
1239 			  lp->tx_irq_name, lp);
1240 	if (err) {
1241 		free_irq(lp->cfg.rx_irq, lp);
1242 		goto out_free_txq;
1243 	}
1244 
1245 	return lp;
1246 
1247 out_free_txq:
1248 	free_queue(lp->tx_num_entries, lp->tx_base);
1249 
1250 out_free_mssbuf:
1251 	kfree(mssbuf);
1252 
1253 out_free_iommu:
1254 	ldc_iommu_release(lp);
1255 
1256 out_free_ldc:
1257 	kfree(lp);
1258 
1259 out_err:
1260 	return ERR_PTR(err);
1261 }
1262 EXPORT_SYMBOL(ldc_alloc);
1263 
1264 void ldc_unbind(struct ldc_channel *lp)
1265 {
1266 	if (lp->flags & LDC_FLAG_REGISTERED_IRQS) {
1267 		free_irq(lp->cfg.rx_irq, lp);
1268 		free_irq(lp->cfg.tx_irq, lp);
1269 		lp->flags &= ~LDC_FLAG_REGISTERED_IRQS;
1270 	}
1271 
1272 	if (lp->flags & LDC_FLAG_REGISTERED_QUEUES) {
1273 		sun4v_ldc_tx_qconf(lp->id, 0, 0);
1274 		sun4v_ldc_rx_qconf(lp->id, 0, 0);
1275 		lp->flags &= ~LDC_FLAG_REGISTERED_QUEUES;
1276 	}
1277 	if (lp->flags & LDC_FLAG_ALLOCED_QUEUES) {
1278 		free_queue(lp->tx_num_entries, lp->tx_base);
1279 		free_queue(lp->rx_num_entries, lp->rx_base);
1280 		lp->flags &= ~LDC_FLAG_ALLOCED_QUEUES;
1281 	}
1282 
1283 	ldc_set_state(lp, LDC_STATE_INIT);
1284 }
1285 EXPORT_SYMBOL(ldc_unbind);
1286 
1287 void ldc_free(struct ldc_channel *lp)
1288 {
1289 	ldc_unbind(lp);
1290 	hlist_del(&lp->list);
1291 	kfree(lp->mssbuf);
1292 	ldc_iommu_release(lp);
1293 
1294 	kfree(lp);
1295 }
1296 EXPORT_SYMBOL(ldc_free);
1297 
1298 /* Bind the channel.  This registers the LDC queues with
1299  * the hypervisor and puts the channel into a pseudo-listening
1300  * state.  This does not initiate a handshake, ldc_connect() does
1301  * that.
1302  */
1303 int ldc_bind(struct ldc_channel *lp)
1304 {
1305 	unsigned long hv_err, flags;
1306 	int err = -EINVAL;
1307 
1308 	if (lp->state != LDC_STATE_INIT)
1309 		return -EINVAL;
1310 
1311 	spin_lock_irqsave(&lp->lock, flags);
1312 
1313 	enable_irq(lp->cfg.rx_irq);
1314 	enable_irq(lp->cfg.tx_irq);
1315 
1316 	lp->flags |= LDC_FLAG_REGISTERED_IRQS;
1317 
1318 	err = -ENODEV;
1319 	hv_err = sun4v_ldc_tx_qconf(lp->id, 0, 0);
1320 	if (hv_err)
1321 		goto out_free_irqs;
1322 
1323 	hv_err = sun4v_ldc_tx_qconf(lp->id, lp->tx_ra, lp->tx_num_entries);
1324 	if (hv_err)
1325 		goto out_free_irqs;
1326 
1327 	hv_err = sun4v_ldc_rx_qconf(lp->id, 0, 0);
1328 	if (hv_err)
1329 		goto out_unmap_tx;
1330 
1331 	hv_err = sun4v_ldc_rx_qconf(lp->id, lp->rx_ra, lp->rx_num_entries);
1332 	if (hv_err)
1333 		goto out_unmap_tx;
1334 
1335 	lp->flags |= LDC_FLAG_REGISTERED_QUEUES;
1336 
1337 	hv_err = sun4v_ldc_tx_get_state(lp->id,
1338 					&lp->tx_head,
1339 					&lp->tx_tail,
1340 					&lp->chan_state);
1341 	err = -EBUSY;
1342 	if (hv_err)
1343 		goto out_unmap_rx;
1344 
1345 	lp->tx_acked = lp->tx_head;
1346 
1347 	lp->hs_state = LDC_HS_OPEN;
1348 	ldc_set_state(lp, LDC_STATE_BOUND);
1349 
1350 	spin_unlock_irqrestore(&lp->lock, flags);
1351 
1352 	return 0;
1353 
1354 out_unmap_rx:
1355 	lp->flags &= ~LDC_FLAG_REGISTERED_QUEUES;
1356 	sun4v_ldc_rx_qconf(lp->id, 0, 0);
1357 
1358 out_unmap_tx:
1359 	sun4v_ldc_tx_qconf(lp->id, 0, 0);
1360 
1361 out_free_irqs:
1362 	lp->flags &= ~LDC_FLAG_REGISTERED_IRQS;
1363 	free_irq(lp->cfg.tx_irq, lp);
1364 	free_irq(lp->cfg.rx_irq, lp);
1365 
1366 	spin_unlock_irqrestore(&lp->lock, flags);
1367 
1368 	return err;
1369 }
1370 EXPORT_SYMBOL(ldc_bind);
1371 
1372 int ldc_connect(struct ldc_channel *lp)
1373 {
1374 	unsigned long flags;
1375 	int err;
1376 
1377 	if (lp->cfg.mode == LDC_MODE_RAW)
1378 		return -EINVAL;
1379 
1380 	spin_lock_irqsave(&lp->lock, flags);
1381 
1382 	if (!(lp->flags & LDC_FLAG_ALLOCED_QUEUES) ||
1383 	    !(lp->flags & LDC_FLAG_REGISTERED_QUEUES) ||
1384 	    lp->hs_state != LDC_HS_OPEN)
1385 		err = ((lp->hs_state > LDC_HS_OPEN) ? 0 : -EINVAL);
1386 	else
1387 		err = start_handshake(lp);
1388 
1389 	spin_unlock_irqrestore(&lp->lock, flags);
1390 
1391 	return err;
1392 }
1393 EXPORT_SYMBOL(ldc_connect);
1394 
1395 int ldc_disconnect(struct ldc_channel *lp)
1396 {
1397 	unsigned long hv_err, flags;
1398 	int err;
1399 
1400 	if (lp->cfg.mode == LDC_MODE_RAW)
1401 		return -EINVAL;
1402 
1403 	if (!(lp->flags & LDC_FLAG_ALLOCED_QUEUES) ||
1404 	    !(lp->flags & LDC_FLAG_REGISTERED_QUEUES))
1405 		return -EINVAL;
1406 
1407 	spin_lock_irqsave(&lp->lock, flags);
1408 
1409 	err = -ENODEV;
1410 	hv_err = sun4v_ldc_tx_qconf(lp->id, 0, 0);
1411 	if (hv_err)
1412 		goto out_err;
1413 
1414 	hv_err = sun4v_ldc_tx_qconf(lp->id, lp->tx_ra, lp->tx_num_entries);
1415 	if (hv_err)
1416 		goto out_err;
1417 
1418 	hv_err = sun4v_ldc_rx_qconf(lp->id, 0, 0);
1419 	if (hv_err)
1420 		goto out_err;
1421 
1422 	hv_err = sun4v_ldc_rx_qconf(lp->id, lp->rx_ra, lp->rx_num_entries);
1423 	if (hv_err)
1424 		goto out_err;
1425 
1426 	ldc_set_state(lp, LDC_STATE_BOUND);
1427 	lp->hs_state = LDC_HS_OPEN;
1428 	lp->flags |= LDC_FLAG_RESET;
1429 
1430 	spin_unlock_irqrestore(&lp->lock, flags);
1431 
1432 	return 0;
1433 
1434 out_err:
1435 	sun4v_ldc_tx_qconf(lp->id, 0, 0);
1436 	sun4v_ldc_rx_qconf(lp->id, 0, 0);
1437 	free_irq(lp->cfg.tx_irq, lp);
1438 	free_irq(lp->cfg.rx_irq, lp);
1439 	lp->flags &= ~(LDC_FLAG_REGISTERED_IRQS |
1440 		       LDC_FLAG_REGISTERED_QUEUES);
1441 	ldc_set_state(lp, LDC_STATE_INIT);
1442 
1443 	spin_unlock_irqrestore(&lp->lock, flags);
1444 
1445 	return err;
1446 }
1447 EXPORT_SYMBOL(ldc_disconnect);
1448 
1449 int ldc_state(struct ldc_channel *lp)
1450 {
1451 	return lp->state;
1452 }
1453 EXPORT_SYMBOL(ldc_state);
1454 
1455 void ldc_set_state(struct ldc_channel *lp, u8 state)
1456 {
1457 	ldcdbg(STATE, "STATE (%s) --> (%s)\n",
1458 	       state_to_str(lp->state),
1459 	       state_to_str(state));
1460 
1461 	lp->state = state;
1462 }
1463 
1464 int ldc_mode(struct ldc_channel *lp)
1465 {
1466 	return lp->cfg.mode;
1467 }
1468 
1469 int ldc_rx_reset(struct ldc_channel *lp)
1470 {
1471 	return __set_rx_head(lp, lp->rx_tail);
1472 }
1473 
1474 void __ldc_print(struct ldc_channel *lp, const char *caller)
1475 {
1476 	pr_info("%s: id=0x%lx flags=0x%x state=%s cstate=0x%lx hsstate=0x%x\n"
1477 		"\trx_h=0x%lx rx_t=0x%lx rx_n=%ld\n"
1478 		"\ttx_h=0x%lx tx_t=0x%lx tx_n=%ld\n"
1479 		"\trcv_nxt=%u snd_nxt=%u\n",
1480 		caller, lp->id, lp->flags, state_to_str(lp->state),
1481 		lp->chan_state, lp->hs_state,
1482 		lp->rx_head, lp->rx_tail, lp->rx_num_entries,
1483 		lp->tx_head, lp->tx_tail, lp->tx_num_entries,
1484 		lp->rcv_nxt, lp->snd_nxt);
1485 }
1486 
1487 static int write_raw(struct ldc_channel *lp, const void *buf, unsigned int size)
1488 {
1489 	struct ldc_packet *p;
1490 	unsigned long new_tail, hv_err;
1491 	int err;
1492 
1493 	hv_err = sun4v_ldc_tx_get_state(lp->id, &lp->tx_head, &lp->tx_tail,
1494 					&lp->chan_state);
1495 	if (unlikely(hv_err))
1496 		return -EBUSY;
1497 
1498 	if (unlikely(lp->chan_state != LDC_CHANNEL_UP))
1499 		return LDC_ABORT(lp);
1500 
1501 	if (size > LDC_PACKET_SIZE)
1502 		return -EMSGSIZE;
1503 
1504 	p = data_get_tx_packet(lp, &new_tail);
1505 	if (!p)
1506 		return -EAGAIN;
1507 
1508 	memcpy(p, buf, size);
1509 
1510 	err = send_tx_packet(lp, p, new_tail);
1511 	if (!err)
1512 		err = size;
1513 
1514 	return err;
1515 }
1516 
1517 static int read_raw(struct ldc_channel *lp, void *buf, unsigned int size)
1518 {
1519 	struct ldc_packet *p;
1520 	unsigned long hv_err, new;
1521 	int err;
1522 
1523 	if (size < LDC_PACKET_SIZE)
1524 		return -EINVAL;
1525 
1526 	hv_err = sun4v_ldc_rx_get_state(lp->id,
1527 					&lp->rx_head,
1528 					&lp->rx_tail,
1529 					&lp->chan_state);
1530 	if (hv_err)
1531 		return LDC_ABORT(lp);
1532 
1533 	if (lp->chan_state == LDC_CHANNEL_DOWN ||
1534 	    lp->chan_state == LDC_CHANNEL_RESETTING)
1535 		return -ECONNRESET;
1536 
1537 	if (lp->rx_head == lp->rx_tail)
1538 		return 0;
1539 
1540 	p = lp->rx_base + (lp->rx_head / LDC_PACKET_SIZE);
1541 	memcpy(buf, p, LDC_PACKET_SIZE);
1542 
1543 	new = rx_advance(lp, lp->rx_head);
1544 	lp->rx_head = new;
1545 
1546 	err = __set_rx_head(lp, new);
1547 	if (err < 0)
1548 		err = -ECONNRESET;
1549 	else
1550 		err = LDC_PACKET_SIZE;
1551 
1552 	return err;
1553 }
1554 
1555 static const struct ldc_mode_ops raw_ops = {
1556 	.write		=	write_raw,
1557 	.read		=	read_raw,
1558 };
1559 
1560 static int write_nonraw(struct ldc_channel *lp, const void *buf,
1561 			unsigned int size)
1562 {
1563 	unsigned long hv_err, tail;
1564 	unsigned int copied;
1565 	u32 seq;
1566 	int err;
1567 
1568 	hv_err = sun4v_ldc_tx_get_state(lp->id, &lp->tx_head, &lp->tx_tail,
1569 					&lp->chan_state);
1570 	if (unlikely(hv_err))
1571 		return -EBUSY;
1572 
1573 	if (unlikely(lp->chan_state != LDC_CHANNEL_UP))
1574 		return LDC_ABORT(lp);
1575 
1576 	if (!tx_has_space_for(lp, size))
1577 		return -EAGAIN;
1578 
1579 	seq = lp->snd_nxt;
1580 	copied = 0;
1581 	tail = lp->tx_tail;
1582 	while (copied < size) {
1583 		struct ldc_packet *p = lp->tx_base + (tail / LDC_PACKET_SIZE);
1584 		u8 *data = ((lp->cfg.mode == LDC_MODE_UNRELIABLE) ?
1585 			    p->u.u_data :
1586 			    p->u.r.r_data);
1587 		int data_len;
1588 
1589 		p->type = LDC_DATA;
1590 		p->stype = LDC_INFO;
1591 		p->ctrl = 0;
1592 
1593 		data_len = size - copied;
1594 		if (data_len > lp->mss)
1595 			data_len = lp->mss;
1596 
1597 		BUG_ON(data_len > LDC_LEN);
1598 
1599 		p->env = (data_len |
1600 			  (copied == 0 ? LDC_START : 0) |
1601 			  (data_len == size - copied ? LDC_STOP : 0));
1602 
1603 		p->seqid = ++seq;
1604 
1605 		ldcdbg(DATA, "SENT DATA [%02x:%02x:%02x:%02x:%08x]\n",
1606 		       p->type,
1607 		       p->stype,
1608 		       p->ctrl,
1609 		       p->env,
1610 		       p->seqid);
1611 
1612 		memcpy(data, buf, data_len);
1613 		buf += data_len;
1614 		copied += data_len;
1615 
1616 		tail = tx_advance(lp, tail);
1617 	}
1618 
1619 	err = set_tx_tail(lp, tail);
1620 	if (!err) {
1621 		lp->snd_nxt = seq;
1622 		err = size;
1623 	}
1624 
1625 	return err;
1626 }
1627 
1628 static int rx_bad_seq(struct ldc_channel *lp, struct ldc_packet *p,
1629 		      struct ldc_packet *first_frag)
1630 {
1631 	int err;
1632 
1633 	if (first_frag)
1634 		lp->rcv_nxt = first_frag->seqid - 1;
1635 
1636 	err = send_data_nack(lp, p);
1637 	if (err)
1638 		return err;
1639 
1640 	err = ldc_rx_reset(lp);
1641 	if (err < 0)
1642 		return LDC_ABORT(lp);
1643 
1644 	return 0;
1645 }
1646 
1647 static int data_ack_nack(struct ldc_channel *lp, struct ldc_packet *p)
1648 {
1649 	if (p->stype & LDC_ACK) {
1650 		int err = process_data_ack(lp, p);
1651 		if (err)
1652 			return err;
1653 	}
1654 	if (p->stype & LDC_NACK)
1655 		return LDC_ABORT(lp);
1656 
1657 	return 0;
1658 }
1659 
1660 static int rx_data_wait(struct ldc_channel *lp, unsigned long cur_head)
1661 {
1662 	unsigned long dummy;
1663 	int limit = 1000;
1664 
1665 	ldcdbg(DATA, "DATA WAIT cur_head[%lx] rx_head[%lx] rx_tail[%lx]\n",
1666 	       cur_head, lp->rx_head, lp->rx_tail);
1667 	while (limit-- > 0) {
1668 		unsigned long hv_err;
1669 
1670 		hv_err = sun4v_ldc_rx_get_state(lp->id,
1671 						&dummy,
1672 						&lp->rx_tail,
1673 						&lp->chan_state);
1674 		if (hv_err)
1675 			return LDC_ABORT(lp);
1676 
1677 		if (lp->chan_state == LDC_CHANNEL_DOWN ||
1678 		    lp->chan_state == LDC_CHANNEL_RESETTING)
1679 			return -ECONNRESET;
1680 
1681 		if (cur_head != lp->rx_tail) {
1682 			ldcdbg(DATA, "DATA WAIT DONE "
1683 			       "head[%lx] tail[%lx] chan_state[%lx]\n",
1684 			       dummy, lp->rx_tail, lp->chan_state);
1685 			return 0;
1686 		}
1687 
1688 		udelay(1);
1689 	}
1690 	return -EAGAIN;
1691 }
1692 
1693 static int rx_set_head(struct ldc_channel *lp, unsigned long head)
1694 {
1695 	int err = __set_rx_head(lp, head);
1696 
1697 	if (err < 0)
1698 		return LDC_ABORT(lp);
1699 
1700 	lp->rx_head = head;
1701 	return 0;
1702 }
1703 
1704 static void send_data_ack(struct ldc_channel *lp)
1705 {
1706 	unsigned long new_tail;
1707 	struct ldc_packet *p;
1708 
1709 	p = data_get_tx_packet(lp, &new_tail);
1710 	if (likely(p)) {
1711 		int err;
1712 
1713 		memset(p, 0, sizeof(*p));
1714 		p->type = LDC_DATA;
1715 		p->stype = LDC_ACK;
1716 		p->ctrl = 0;
1717 		p->seqid = lp->snd_nxt + 1;
1718 		p->u.r.ackid = lp->rcv_nxt;
1719 
1720 		err = send_tx_packet(lp, p, new_tail);
1721 		if (!err)
1722 			lp->snd_nxt++;
1723 	}
1724 }
1725 
1726 static int read_nonraw(struct ldc_channel *lp, void *buf, unsigned int size)
1727 {
1728 	struct ldc_packet *first_frag;
1729 	unsigned long hv_err, new;
1730 	int err, copied;
1731 
1732 	hv_err = sun4v_ldc_rx_get_state(lp->id,
1733 					&lp->rx_head,
1734 					&lp->rx_tail,
1735 					&lp->chan_state);
1736 	if (hv_err)
1737 		return LDC_ABORT(lp);
1738 
1739 	if (lp->chan_state == LDC_CHANNEL_DOWN ||
1740 	    lp->chan_state == LDC_CHANNEL_RESETTING)
1741 		return -ECONNRESET;
1742 
1743 	if (lp->rx_head == lp->rx_tail)
1744 		return 0;
1745 
1746 	first_frag = NULL;
1747 	copied = err = 0;
1748 	new = lp->rx_head;
1749 	while (1) {
1750 		struct ldc_packet *p;
1751 		int pkt_len;
1752 
1753 		BUG_ON(new == lp->rx_tail);
1754 		p = lp->rx_base + (new / LDC_PACKET_SIZE);
1755 
1756 		ldcdbg(RX, "RX read pkt[%02x:%02x:%02x:%02x:%08x:%08x] "
1757 		       "rcv_nxt[%08x]\n",
1758 		       p->type,
1759 		       p->stype,
1760 		       p->ctrl,
1761 		       p->env,
1762 		       p->seqid,
1763 		       p->u.r.ackid,
1764 		       lp->rcv_nxt);
1765 
1766 		if (unlikely(!rx_seq_ok(lp, p->seqid))) {
1767 			err = rx_bad_seq(lp, p, first_frag);
1768 			copied = 0;
1769 			break;
1770 		}
1771 
1772 		if (p->type & LDC_CTRL) {
1773 			err = process_control_frame(lp, p);
1774 			if (err < 0)
1775 				break;
1776 			err = 0;
1777 		}
1778 
1779 		lp->rcv_nxt = p->seqid;
1780 
1781 		/*
1782 		 * If this is a control-only packet, there is nothing
1783 		 * else to do but advance the rx queue since the packet
1784 		 * was already processed above.
1785 		 */
1786 		if (!(p->type & LDC_DATA)) {
1787 			new = rx_advance(lp, new);
1788 			break;
1789 		}
1790 		if (p->stype & (LDC_ACK | LDC_NACK)) {
1791 			err = data_ack_nack(lp, p);
1792 			if (err)
1793 				break;
1794 		}
1795 		if (!(p->stype & LDC_INFO)) {
1796 			new = rx_advance(lp, new);
1797 			err = rx_set_head(lp, new);
1798 			if (err)
1799 				break;
1800 			goto no_data;
1801 		}
1802 
1803 		pkt_len = p->env & LDC_LEN;
1804 
1805 		/* Every initial packet starts with the START bit set.
1806 		 *
1807 		 * Singleton packets will have both START+STOP set.
1808 		 *
1809 		 * Fragments will have START set in the first frame, STOP
1810 		 * set in the last frame, and neither bit set in middle
1811 		 * frames of the packet.
1812 		 *
1813 		 * Therefore if we are at the beginning of a packet and
1814 		 * we don't see START, or we are in the middle of a fragmented
1815 		 * packet and do see START, we are unsynchronized and should
1816 		 * flush the RX queue.
1817 		 */
1818 		if ((first_frag == NULL && !(p->env & LDC_START)) ||
1819 		    (first_frag != NULL &&  (p->env & LDC_START))) {
1820 			if (!first_frag)
1821 				new = rx_advance(lp, new);
1822 
1823 			err = rx_set_head(lp, new);
1824 			if (err)
1825 				break;
1826 
1827 			if (!first_frag)
1828 				goto no_data;
1829 		}
1830 		if (!first_frag)
1831 			first_frag = p;
1832 
1833 		if (pkt_len > size - copied) {
1834 			/* User didn't give us a big enough buffer,
1835 			 * what to do?  This is a pretty serious error.
1836 			 *
1837 			 * Since we haven't updated the RX ring head to
1838 			 * consume any of the packets, signal the error
1839 			 * to the user and just leave the RX ring alone.
1840 			 *
1841 			 * This seems the best behavior because this allows
1842 			 * a user of the LDC layer to start with a small
1843 			 * RX buffer for ldc_read() calls and use -EMSGSIZE
1844 			 * as a cue to enlarge it's read buffer.
1845 			 */
1846 			err = -EMSGSIZE;
1847 			break;
1848 		}
1849 
1850 		/* Ok, we are gonna eat this one.  */
1851 		new = rx_advance(lp, new);
1852 
1853 		memcpy(buf,
1854 		       (lp->cfg.mode == LDC_MODE_UNRELIABLE ?
1855 			p->u.u_data : p->u.r.r_data), pkt_len);
1856 		buf += pkt_len;
1857 		copied += pkt_len;
1858 
1859 		if (p->env & LDC_STOP)
1860 			break;
1861 
1862 no_data:
1863 		if (new == lp->rx_tail) {
1864 			err = rx_data_wait(lp, new);
1865 			if (err)
1866 				break;
1867 		}
1868 	}
1869 
1870 	if (!err)
1871 		err = rx_set_head(lp, new);
1872 
1873 	if (err && first_frag)
1874 		lp->rcv_nxt = first_frag->seqid - 1;
1875 
1876 	if (!err) {
1877 		err = copied;
1878 		if (err > 0 && lp->cfg.mode != LDC_MODE_UNRELIABLE)
1879 			send_data_ack(lp);
1880 	}
1881 
1882 	return err;
1883 }
1884 
1885 static const struct ldc_mode_ops nonraw_ops = {
1886 	.write		=	write_nonraw,
1887 	.read		=	read_nonraw,
1888 };
1889 
1890 static int write_stream(struct ldc_channel *lp, const void *buf,
1891 			unsigned int size)
1892 {
1893 	if (size > lp->cfg.mtu)
1894 		size = lp->cfg.mtu;
1895 	return write_nonraw(lp, buf, size);
1896 }
1897 
1898 static int read_stream(struct ldc_channel *lp, void *buf, unsigned int size)
1899 {
1900 	if (!lp->mssbuf_len) {
1901 		int err = read_nonraw(lp, lp->mssbuf, lp->cfg.mtu);
1902 		if (err < 0)
1903 			return err;
1904 
1905 		lp->mssbuf_len = err;
1906 		lp->mssbuf_off = 0;
1907 	}
1908 
1909 	if (size > lp->mssbuf_len)
1910 		size = lp->mssbuf_len;
1911 	memcpy(buf, lp->mssbuf + lp->mssbuf_off, size);
1912 
1913 	lp->mssbuf_off += size;
1914 	lp->mssbuf_len -= size;
1915 
1916 	return size;
1917 }
1918 
1919 static const struct ldc_mode_ops stream_ops = {
1920 	.write		=	write_stream,
1921 	.read		=	read_stream,
1922 };
1923 
1924 int ldc_write(struct ldc_channel *lp, const void *buf, unsigned int size)
1925 {
1926 	unsigned long flags;
1927 	int err;
1928 
1929 	if (!buf)
1930 		return -EINVAL;
1931 
1932 	if (!size)
1933 		return 0;
1934 
1935 	spin_lock_irqsave(&lp->lock, flags);
1936 
1937 	if (lp->hs_state != LDC_HS_COMPLETE)
1938 		err = -ENOTCONN;
1939 	else
1940 		err = lp->mops->write(lp, buf, size);
1941 
1942 	spin_unlock_irqrestore(&lp->lock, flags);
1943 
1944 	return err;
1945 }
1946 EXPORT_SYMBOL(ldc_write);
1947 
1948 int ldc_read(struct ldc_channel *lp, void *buf, unsigned int size)
1949 {
1950 	unsigned long flags;
1951 	int err;
1952 
1953 	if (!buf)
1954 		return -EINVAL;
1955 
1956 	if (!size)
1957 		return 0;
1958 
1959 	spin_lock_irqsave(&lp->lock, flags);
1960 
1961 	if (lp->hs_state != LDC_HS_COMPLETE)
1962 		err = -ENOTCONN;
1963 	else
1964 		err = lp->mops->read(lp, buf, size);
1965 
1966 	spin_unlock_irqrestore(&lp->lock, flags);
1967 
1968 	return err;
1969 }
1970 EXPORT_SYMBOL(ldc_read);
1971 
1972 static u64 pagesize_code(void)
1973 {
1974 	switch (PAGE_SIZE) {
1975 	default:
1976 	case (8ULL * 1024ULL):
1977 		return 0;
1978 	case (64ULL * 1024ULL):
1979 		return 1;
1980 	case (512ULL * 1024ULL):
1981 		return 2;
1982 	case (4ULL * 1024ULL * 1024ULL):
1983 		return 3;
1984 	case (32ULL * 1024ULL * 1024ULL):
1985 		return 4;
1986 	case (256ULL * 1024ULL * 1024ULL):
1987 		return 5;
1988 	}
1989 }
1990 
1991 static u64 make_cookie(u64 index, u64 pgsz_code, u64 page_offset)
1992 {
1993 	return ((pgsz_code << COOKIE_PGSZ_CODE_SHIFT) |
1994 		(index << PAGE_SHIFT) |
1995 		page_offset);
1996 }
1997 
1998 
1999 static struct ldc_mtable_entry *alloc_npages(struct ldc_iommu *iommu,
2000 					     unsigned long npages)
2001 {
2002 	long entry;
2003 
2004 	entry = iommu_tbl_range_alloc(NULL, &iommu->iommu_map_table,
2005 				      npages, NULL, (unsigned long)-1, 0);
2006 	if (unlikely(entry == IOMMU_ERROR_CODE))
2007 		return NULL;
2008 
2009 	return iommu->page_table + entry;
2010 }
2011 
2012 static u64 perm_to_mte(unsigned int map_perm)
2013 {
2014 	u64 mte_base;
2015 
2016 	mte_base = pagesize_code();
2017 
2018 	if (map_perm & LDC_MAP_SHADOW) {
2019 		if (map_perm & LDC_MAP_R)
2020 			mte_base |= LDC_MTE_COPY_R;
2021 		if (map_perm & LDC_MAP_W)
2022 			mte_base |= LDC_MTE_COPY_W;
2023 	}
2024 	if (map_perm & LDC_MAP_DIRECT) {
2025 		if (map_perm & LDC_MAP_R)
2026 			mte_base |= LDC_MTE_READ;
2027 		if (map_perm & LDC_MAP_W)
2028 			mte_base |= LDC_MTE_WRITE;
2029 		if (map_perm & LDC_MAP_X)
2030 			mte_base |= LDC_MTE_EXEC;
2031 	}
2032 	if (map_perm & LDC_MAP_IO) {
2033 		if (map_perm & LDC_MAP_R)
2034 			mte_base |= LDC_MTE_IOMMU_R;
2035 		if (map_perm & LDC_MAP_W)
2036 			mte_base |= LDC_MTE_IOMMU_W;
2037 	}
2038 
2039 	return mte_base;
2040 }
2041 
2042 static int pages_in_region(unsigned long base, long len)
2043 {
2044 	int count = 0;
2045 
2046 	do {
2047 		unsigned long new = (base + PAGE_SIZE) & PAGE_MASK;
2048 
2049 		len -= (new - base);
2050 		base = new;
2051 		count++;
2052 	} while (len > 0);
2053 
2054 	return count;
2055 }
2056 
2057 struct cookie_state {
2058 	struct ldc_mtable_entry		*page_table;
2059 	struct ldc_trans_cookie		*cookies;
2060 	u64				mte_base;
2061 	u64				prev_cookie;
2062 	u32				pte_idx;
2063 	u32				nc;
2064 };
2065 
2066 static void fill_cookies(struct cookie_state *sp, unsigned long pa,
2067 			 unsigned long off, unsigned long len)
2068 {
2069 	do {
2070 		unsigned long tlen, new = pa + PAGE_SIZE;
2071 		u64 this_cookie;
2072 
2073 		sp->page_table[sp->pte_idx].mte = sp->mte_base | pa;
2074 
2075 		tlen = PAGE_SIZE;
2076 		if (off)
2077 			tlen = PAGE_SIZE - off;
2078 		if (tlen > len)
2079 			tlen = len;
2080 
2081 		this_cookie = make_cookie(sp->pte_idx,
2082 					  pagesize_code(), off);
2083 
2084 		off = 0;
2085 
2086 		if (this_cookie == sp->prev_cookie) {
2087 			sp->cookies[sp->nc - 1].cookie_size += tlen;
2088 		} else {
2089 			sp->cookies[sp->nc].cookie_addr = this_cookie;
2090 			sp->cookies[sp->nc].cookie_size = tlen;
2091 			sp->nc++;
2092 		}
2093 		sp->prev_cookie = this_cookie + tlen;
2094 
2095 		sp->pte_idx++;
2096 
2097 		len -= tlen;
2098 		pa = new;
2099 	} while (len > 0);
2100 }
2101 
2102 static int sg_count_one(struct scatterlist *sg)
2103 {
2104 	unsigned long base = page_to_pfn(sg_page(sg)) << PAGE_SHIFT;
2105 	long len = sg->length;
2106 
2107 	if ((sg->offset | len) & (8UL - 1))
2108 		return -EFAULT;
2109 
2110 	return pages_in_region(base + sg->offset, len);
2111 }
2112 
2113 static int sg_count_pages(struct scatterlist *sg, int num_sg)
2114 {
2115 	int count;
2116 	int i;
2117 
2118 	count = 0;
2119 	for (i = 0; i < num_sg; i++) {
2120 		int err = sg_count_one(sg + i);
2121 		if (err < 0)
2122 			return err;
2123 		count += err;
2124 	}
2125 
2126 	return count;
2127 }
2128 
2129 int ldc_map_sg(struct ldc_channel *lp,
2130 	       struct scatterlist *sg, int num_sg,
2131 	       struct ldc_trans_cookie *cookies, int ncookies,
2132 	       unsigned int map_perm)
2133 {
2134 	unsigned long i, npages;
2135 	struct ldc_mtable_entry *base;
2136 	struct cookie_state state;
2137 	struct ldc_iommu *iommu;
2138 	int err;
2139 	struct scatterlist *s;
2140 
2141 	if (map_perm & ~LDC_MAP_ALL)
2142 		return -EINVAL;
2143 
2144 	err = sg_count_pages(sg, num_sg);
2145 	if (err < 0)
2146 		return err;
2147 
2148 	npages = err;
2149 	if (err > ncookies)
2150 		return -EMSGSIZE;
2151 
2152 	iommu = &lp->iommu;
2153 
2154 	base = alloc_npages(iommu, npages);
2155 
2156 	if (!base)
2157 		return -ENOMEM;
2158 
2159 	state.page_table = iommu->page_table;
2160 	state.cookies = cookies;
2161 	state.mte_base = perm_to_mte(map_perm);
2162 	state.prev_cookie = ~(u64)0;
2163 	state.pte_idx = (base - iommu->page_table);
2164 	state.nc = 0;
2165 
2166 	for_each_sg(sg, s, num_sg, i) {
2167 		fill_cookies(&state, page_to_pfn(sg_page(s)) << PAGE_SHIFT,
2168 			     s->offset, s->length);
2169 	}
2170 
2171 	return state.nc;
2172 }
2173 EXPORT_SYMBOL(ldc_map_sg);
2174 
2175 int ldc_map_single(struct ldc_channel *lp,
2176 		   void *buf, unsigned int len,
2177 		   struct ldc_trans_cookie *cookies, int ncookies,
2178 		   unsigned int map_perm)
2179 {
2180 	unsigned long npages, pa;
2181 	struct ldc_mtable_entry *base;
2182 	struct cookie_state state;
2183 	struct ldc_iommu *iommu;
2184 
2185 	if ((map_perm & ~LDC_MAP_ALL) || (ncookies < 1))
2186 		return -EINVAL;
2187 
2188 	pa = __pa(buf);
2189 	if ((pa | len) & (8UL - 1))
2190 		return -EFAULT;
2191 
2192 	npages = pages_in_region(pa, len);
2193 
2194 	iommu = &lp->iommu;
2195 
2196 	base = alloc_npages(iommu, npages);
2197 
2198 	if (!base)
2199 		return -ENOMEM;
2200 
2201 	state.page_table = iommu->page_table;
2202 	state.cookies = cookies;
2203 	state.mte_base = perm_to_mte(map_perm);
2204 	state.prev_cookie = ~(u64)0;
2205 	state.pte_idx = (base - iommu->page_table);
2206 	state.nc = 0;
2207 	fill_cookies(&state, (pa & PAGE_MASK), (pa & ~PAGE_MASK), len);
2208 	BUG_ON(state.nc > ncookies);
2209 
2210 	return state.nc;
2211 }
2212 EXPORT_SYMBOL(ldc_map_single);
2213 
2214 
2215 static void free_npages(unsigned long id, struct ldc_iommu *iommu,
2216 			u64 cookie, u64 size)
2217 {
2218 	unsigned long npages, entry;
2219 
2220 	npages = PAGE_ALIGN(((cookie & ~PAGE_MASK) + size)) >> PAGE_SHIFT;
2221 
2222 	entry = ldc_cookie_to_index(cookie, iommu);
2223 	ldc_demap(iommu, id, cookie, entry, npages);
2224 	iommu_tbl_range_free(&iommu->iommu_map_table, cookie, npages, entry);
2225 }
2226 
2227 void ldc_unmap(struct ldc_channel *lp, struct ldc_trans_cookie *cookies,
2228 	       int ncookies)
2229 {
2230 	struct ldc_iommu *iommu = &lp->iommu;
2231 	int i;
2232 	unsigned long flags;
2233 
2234 	spin_lock_irqsave(&iommu->lock, flags);
2235 	for (i = 0; i < ncookies; i++) {
2236 		u64 addr = cookies[i].cookie_addr;
2237 		u64 size = cookies[i].cookie_size;
2238 
2239 		free_npages(lp->id, iommu, addr, size);
2240 	}
2241 	spin_unlock_irqrestore(&iommu->lock, flags);
2242 }
2243 EXPORT_SYMBOL(ldc_unmap);
2244 
2245 int ldc_copy(struct ldc_channel *lp, int copy_dir,
2246 	     void *buf, unsigned int len, unsigned long offset,
2247 	     struct ldc_trans_cookie *cookies, int ncookies)
2248 {
2249 	unsigned int orig_len;
2250 	unsigned long ra;
2251 	int i;
2252 
2253 	if (copy_dir != LDC_COPY_IN && copy_dir != LDC_COPY_OUT) {
2254 		printk(KERN_ERR PFX "ldc_copy: ID[%lu] Bad copy_dir[%d]\n",
2255 		       lp->id, copy_dir);
2256 		return -EINVAL;
2257 	}
2258 
2259 	ra = __pa(buf);
2260 	if ((ra | len | offset) & (8UL - 1)) {
2261 		printk(KERN_ERR PFX "ldc_copy: ID[%lu] Unaligned buffer "
2262 		       "ra[%lx] len[%x] offset[%lx]\n",
2263 		       lp->id, ra, len, offset);
2264 		return -EFAULT;
2265 	}
2266 
2267 	if (lp->hs_state != LDC_HS_COMPLETE ||
2268 	    (lp->flags & LDC_FLAG_RESET)) {
2269 		printk(KERN_ERR PFX "ldc_copy: ID[%lu] Link down hs_state[%x] "
2270 		       "flags[%x]\n", lp->id, lp->hs_state, lp->flags);
2271 		return -ECONNRESET;
2272 	}
2273 
2274 	orig_len = len;
2275 	for (i = 0; i < ncookies; i++) {
2276 		unsigned long cookie_raddr = cookies[i].cookie_addr;
2277 		unsigned long this_len = cookies[i].cookie_size;
2278 		unsigned long actual_len;
2279 
2280 		if (unlikely(offset)) {
2281 			unsigned long this_off = offset;
2282 
2283 			if (this_off > this_len)
2284 				this_off = this_len;
2285 
2286 			offset -= this_off;
2287 			this_len -= this_off;
2288 			if (!this_len)
2289 				continue;
2290 			cookie_raddr += this_off;
2291 		}
2292 
2293 		if (this_len > len)
2294 			this_len = len;
2295 
2296 		while (1) {
2297 			unsigned long hv_err;
2298 
2299 			hv_err = sun4v_ldc_copy(lp->id, copy_dir,
2300 						cookie_raddr, ra,
2301 						this_len, &actual_len);
2302 			if (unlikely(hv_err)) {
2303 				printk(KERN_ERR PFX "ldc_copy: ID[%lu] "
2304 				       "HV error %lu\n",
2305 				       lp->id, hv_err);
2306 				if (lp->hs_state != LDC_HS_COMPLETE ||
2307 				    (lp->flags & LDC_FLAG_RESET))
2308 					return -ECONNRESET;
2309 				else
2310 					return -EFAULT;
2311 			}
2312 
2313 			cookie_raddr += actual_len;
2314 			ra += actual_len;
2315 			len -= actual_len;
2316 			if (actual_len == this_len)
2317 				break;
2318 
2319 			this_len -= actual_len;
2320 		}
2321 
2322 		if (!len)
2323 			break;
2324 	}
2325 
2326 	/* It is caller policy what to do about short copies.
2327 	 * For example, a networking driver can declare the
2328 	 * packet a runt and drop it.
2329 	 */
2330 
2331 	return orig_len - len;
2332 }
2333 EXPORT_SYMBOL(ldc_copy);
2334 
2335 void *ldc_alloc_exp_dring(struct ldc_channel *lp, unsigned int len,
2336 			  struct ldc_trans_cookie *cookies, int *ncookies,
2337 			  unsigned int map_perm)
2338 {
2339 	void *buf;
2340 	int err;
2341 
2342 	if (len & (8UL - 1))
2343 		return ERR_PTR(-EINVAL);
2344 
2345 	buf = kzalloc(len, GFP_ATOMIC);
2346 	if (!buf)
2347 		return ERR_PTR(-ENOMEM);
2348 
2349 	err = ldc_map_single(lp, buf, len, cookies, *ncookies, map_perm);
2350 	if (err < 0) {
2351 		kfree(buf);
2352 		return ERR_PTR(err);
2353 	}
2354 	*ncookies = err;
2355 
2356 	return buf;
2357 }
2358 EXPORT_SYMBOL(ldc_alloc_exp_dring);
2359 
2360 void ldc_free_exp_dring(struct ldc_channel *lp, void *buf, unsigned int len,
2361 			struct ldc_trans_cookie *cookies, int ncookies)
2362 {
2363 	ldc_unmap(lp, cookies, ncookies);
2364 	kfree(buf);
2365 }
2366 EXPORT_SYMBOL(ldc_free_exp_dring);
2367 
2368 static int __init ldc_init(void)
2369 {
2370 	unsigned long major, minor;
2371 	struct mdesc_handle *hp;
2372 	const u64 *v;
2373 	int err;
2374 	u64 mp;
2375 
2376 	hp = mdesc_grab();
2377 	if (!hp)
2378 		return -ENODEV;
2379 
2380 	mp = mdesc_node_by_name(hp, MDESC_NODE_NULL, "platform");
2381 	err = -ENODEV;
2382 	if (mp == MDESC_NODE_NULL)
2383 		goto out;
2384 
2385 	v = mdesc_get_property(hp, mp, "domaining-enabled", NULL);
2386 	if (!v)
2387 		goto out;
2388 
2389 	major = 1;
2390 	minor = 0;
2391 	if (sun4v_hvapi_register(HV_GRP_LDOM, major, &minor)) {
2392 		printk(KERN_INFO PFX "Could not register LDOM hvapi.\n");
2393 		goto out;
2394 	}
2395 
2396 	printk(KERN_INFO "%s", version);
2397 
2398 	if (!*v) {
2399 		printk(KERN_INFO PFX "Domaining disabled.\n");
2400 		goto out;
2401 	}
2402 	ldom_domaining_enabled = 1;
2403 	err = 0;
2404 
2405 out:
2406 	mdesc_release(hp);
2407 	return err;
2408 }
2409 
2410 core_initcall(ldc_init);
2411