xref: /linux/arch/sparc/kernel/viohs.c (revision 066bcaca51946c8305e3d637a795e8ccf8dbd3cf)
1a88b5ba8SSam Ravnborg /* viohs.c: LDOM Virtual I/O handshake helper layer.
2a88b5ba8SSam Ravnborg  *
3a88b5ba8SSam Ravnborg  * Copyright (C) 2007 David S. Miller <davem@davemloft.net>
4a88b5ba8SSam Ravnborg  */
5a88b5ba8SSam Ravnborg 
6a88b5ba8SSam Ravnborg #include <linux/kernel.h>
7*066bcacaSPaul Gortmaker #include <linux/export.h>
8a88b5ba8SSam Ravnborg #include <linux/string.h>
9a88b5ba8SSam Ravnborg #include <linux/delay.h>
10a88b5ba8SSam Ravnborg #include <linux/sched.h>
11a88b5ba8SSam Ravnborg #include <linux/slab.h>
12a88b5ba8SSam Ravnborg 
13a88b5ba8SSam Ravnborg #include <asm/ldc.h>
14a88b5ba8SSam Ravnborg #include <asm/vio.h>
15a88b5ba8SSam Ravnborg 
16a88b5ba8SSam Ravnborg int vio_ldc_send(struct vio_driver_state *vio, void *data, int len)
17a88b5ba8SSam Ravnborg {
18a88b5ba8SSam Ravnborg 	int err, limit = 1000;
19a88b5ba8SSam Ravnborg 
20a88b5ba8SSam Ravnborg 	err = -EINVAL;
21a88b5ba8SSam Ravnborg 	while (limit-- > 0) {
22a88b5ba8SSam Ravnborg 		err = ldc_write(vio->lp, data, len);
23a88b5ba8SSam Ravnborg 		if (!err || (err != -EAGAIN))
24a88b5ba8SSam Ravnborg 			break;
25a88b5ba8SSam Ravnborg 		udelay(1);
26a88b5ba8SSam Ravnborg 	}
27a88b5ba8SSam Ravnborg 
28a88b5ba8SSam Ravnborg 	return err;
29a88b5ba8SSam Ravnborg }
30a88b5ba8SSam Ravnborg EXPORT_SYMBOL(vio_ldc_send);
31a88b5ba8SSam Ravnborg 
32a88b5ba8SSam Ravnborg static int send_ctrl(struct vio_driver_state *vio,
33a88b5ba8SSam Ravnborg 		     struct vio_msg_tag *tag, int len)
34a88b5ba8SSam Ravnborg {
35a88b5ba8SSam Ravnborg 	tag->sid = vio_send_sid(vio);
36a88b5ba8SSam Ravnborg 	return vio_ldc_send(vio, tag, len);
37a88b5ba8SSam Ravnborg }
38a88b5ba8SSam Ravnborg 
39a88b5ba8SSam Ravnborg static void init_tag(struct vio_msg_tag *tag, u8 type, u8 stype, u16 stype_env)
40a88b5ba8SSam Ravnborg {
41a88b5ba8SSam Ravnborg 	tag->type = type;
42a88b5ba8SSam Ravnborg 	tag->stype = stype;
43a88b5ba8SSam Ravnborg 	tag->stype_env = stype_env;
44a88b5ba8SSam Ravnborg }
45a88b5ba8SSam Ravnborg 
46a88b5ba8SSam Ravnborg static int send_version(struct vio_driver_state *vio, u16 major, u16 minor)
47a88b5ba8SSam Ravnborg {
48a88b5ba8SSam Ravnborg 	struct vio_ver_info pkt;
49a88b5ba8SSam Ravnborg 
50a88b5ba8SSam Ravnborg 	vio->_local_sid = (u32) sched_clock();
51a88b5ba8SSam Ravnborg 
52a88b5ba8SSam Ravnborg 	memset(&pkt, 0, sizeof(pkt));
53a88b5ba8SSam Ravnborg 	init_tag(&pkt.tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_VER_INFO);
54a88b5ba8SSam Ravnborg 	pkt.major = major;
55a88b5ba8SSam Ravnborg 	pkt.minor = minor;
56a88b5ba8SSam Ravnborg 	pkt.dev_class = vio->dev_class;
57a88b5ba8SSam Ravnborg 
58a88b5ba8SSam Ravnborg 	viodbg(HS, "SEND VERSION INFO maj[%u] min[%u] devclass[%u]\n",
59a88b5ba8SSam Ravnborg 	       major, minor, vio->dev_class);
60a88b5ba8SSam Ravnborg 
61a88b5ba8SSam Ravnborg 	return send_ctrl(vio, &pkt.tag, sizeof(pkt));
62a88b5ba8SSam Ravnborg }
63a88b5ba8SSam Ravnborg 
64a88b5ba8SSam Ravnborg static int start_handshake(struct vio_driver_state *vio)
65a88b5ba8SSam Ravnborg {
66a88b5ba8SSam Ravnborg 	int err;
67a88b5ba8SSam Ravnborg 
68a88b5ba8SSam Ravnborg 	viodbg(HS, "START HANDSHAKE\n");
69a88b5ba8SSam Ravnborg 
70a88b5ba8SSam Ravnborg 	vio->hs_state = VIO_HS_INVALID;
71a88b5ba8SSam Ravnborg 
72a88b5ba8SSam Ravnborg 	err = send_version(vio,
73a88b5ba8SSam Ravnborg 			   vio->ver_table[0].major,
74a88b5ba8SSam Ravnborg 			   vio->ver_table[0].minor);
75a88b5ba8SSam Ravnborg 	if (err < 0)
76a88b5ba8SSam Ravnborg 		return err;
77a88b5ba8SSam Ravnborg 
78a88b5ba8SSam Ravnborg 	return 0;
79a88b5ba8SSam Ravnborg }
80a88b5ba8SSam Ravnborg 
81a88b5ba8SSam Ravnborg static void flush_rx_dring(struct vio_driver_state *vio)
82a88b5ba8SSam Ravnborg {
83a88b5ba8SSam Ravnborg 	struct vio_dring_state *dr;
84a88b5ba8SSam Ravnborg 	u64 ident;
85a88b5ba8SSam Ravnborg 
86a88b5ba8SSam Ravnborg 	BUG_ON(!(vio->dr_state & VIO_DR_STATE_RXREG));
87a88b5ba8SSam Ravnborg 
88a88b5ba8SSam Ravnborg 	dr = &vio->drings[VIO_DRIVER_RX_RING];
89a88b5ba8SSam Ravnborg 	ident = dr->ident;
90a88b5ba8SSam Ravnborg 
91a88b5ba8SSam Ravnborg 	BUG_ON(!vio->desc_buf);
92a88b5ba8SSam Ravnborg 	kfree(vio->desc_buf);
93a88b5ba8SSam Ravnborg 	vio->desc_buf = NULL;
94a88b5ba8SSam Ravnborg 
95a88b5ba8SSam Ravnborg 	memset(dr, 0, sizeof(*dr));
96a88b5ba8SSam Ravnborg 	dr->ident = ident;
97a88b5ba8SSam Ravnborg }
98a88b5ba8SSam Ravnborg 
99a88b5ba8SSam Ravnborg void vio_link_state_change(struct vio_driver_state *vio, int event)
100a88b5ba8SSam Ravnborg {
101a88b5ba8SSam Ravnborg 	if (event == LDC_EVENT_UP) {
102a88b5ba8SSam Ravnborg 		vio->hs_state = VIO_HS_INVALID;
103a88b5ba8SSam Ravnborg 
104a88b5ba8SSam Ravnborg 		switch (vio->dev_class) {
105a88b5ba8SSam Ravnborg 		case VDEV_NETWORK:
106a88b5ba8SSam Ravnborg 		case VDEV_NETWORK_SWITCH:
107a88b5ba8SSam Ravnborg 			vio->dr_state = (VIO_DR_STATE_TXREQ |
108a88b5ba8SSam Ravnborg 					 VIO_DR_STATE_RXREQ);
109a88b5ba8SSam Ravnborg 			break;
110a88b5ba8SSam Ravnborg 
111a88b5ba8SSam Ravnborg 		case VDEV_DISK:
112a88b5ba8SSam Ravnborg 			vio->dr_state = VIO_DR_STATE_TXREQ;
113a88b5ba8SSam Ravnborg 			break;
114a88b5ba8SSam Ravnborg 		case VDEV_DISK_SERVER:
115a88b5ba8SSam Ravnborg 			vio->dr_state = VIO_DR_STATE_RXREQ;
116a88b5ba8SSam Ravnborg 			break;
117a88b5ba8SSam Ravnborg 		}
118a88b5ba8SSam Ravnborg 		start_handshake(vio);
119a88b5ba8SSam Ravnborg 	} else if (event == LDC_EVENT_RESET) {
120a88b5ba8SSam Ravnborg 		vio->hs_state = VIO_HS_INVALID;
121a88b5ba8SSam Ravnborg 
122a88b5ba8SSam Ravnborg 		if (vio->dr_state & VIO_DR_STATE_RXREG)
123a88b5ba8SSam Ravnborg 			flush_rx_dring(vio);
124a88b5ba8SSam Ravnborg 
125a88b5ba8SSam Ravnborg 		vio->dr_state = 0x00;
126a88b5ba8SSam Ravnborg 		memset(&vio->ver, 0, sizeof(vio->ver));
127a88b5ba8SSam Ravnborg 
128a88b5ba8SSam Ravnborg 		ldc_disconnect(vio->lp);
129a88b5ba8SSam Ravnborg 	}
130a88b5ba8SSam Ravnborg }
131a88b5ba8SSam Ravnborg EXPORT_SYMBOL(vio_link_state_change);
132a88b5ba8SSam Ravnborg 
133a88b5ba8SSam Ravnborg static int handshake_failure(struct vio_driver_state *vio)
134a88b5ba8SSam Ravnborg {
135a88b5ba8SSam Ravnborg 	struct vio_dring_state *dr;
136a88b5ba8SSam Ravnborg 
137a88b5ba8SSam Ravnborg 	/* XXX Put policy here...  Perhaps start a timer to fire
138a88b5ba8SSam Ravnborg 	 * XXX in 100 ms, which will bring the link up and retry
139a88b5ba8SSam Ravnborg 	 * XXX the handshake.
140a88b5ba8SSam Ravnborg 	 */
141a88b5ba8SSam Ravnborg 
142a88b5ba8SSam Ravnborg 	viodbg(HS, "HANDSHAKE FAILURE\n");
143a88b5ba8SSam Ravnborg 
144a88b5ba8SSam Ravnborg 	vio->dr_state &= ~(VIO_DR_STATE_TXREG |
145a88b5ba8SSam Ravnborg 			   VIO_DR_STATE_RXREG);
146a88b5ba8SSam Ravnborg 
147a88b5ba8SSam Ravnborg 	dr = &vio->drings[VIO_DRIVER_RX_RING];
148a88b5ba8SSam Ravnborg 	memset(dr, 0, sizeof(*dr));
149a88b5ba8SSam Ravnborg 
150a88b5ba8SSam Ravnborg 	kfree(vio->desc_buf);
151a88b5ba8SSam Ravnborg 	vio->desc_buf = NULL;
152a88b5ba8SSam Ravnborg 	vio->desc_buf_len = 0;
153a88b5ba8SSam Ravnborg 
154a88b5ba8SSam Ravnborg 	vio->hs_state = VIO_HS_INVALID;
155a88b5ba8SSam Ravnborg 
156a88b5ba8SSam Ravnborg 	return -ECONNRESET;
157a88b5ba8SSam Ravnborg }
158a88b5ba8SSam Ravnborg 
159a88b5ba8SSam Ravnborg static int process_unknown(struct vio_driver_state *vio, void *arg)
160a88b5ba8SSam Ravnborg {
161a88b5ba8SSam Ravnborg 	struct vio_msg_tag *pkt = arg;
162a88b5ba8SSam Ravnborg 
163a88b5ba8SSam Ravnborg 	viodbg(HS, "UNKNOWN CONTROL [%02x:%02x:%04x:%08x]\n",
164a88b5ba8SSam Ravnborg 	       pkt->type, pkt->stype, pkt->stype_env, pkt->sid);
165a88b5ba8SSam Ravnborg 
166a88b5ba8SSam Ravnborg 	printk(KERN_ERR "vio: ID[%lu] Resetting connection.\n",
167a88b5ba8SSam Ravnborg 	       vio->vdev->channel_id);
168a88b5ba8SSam Ravnborg 
169a88b5ba8SSam Ravnborg 	ldc_disconnect(vio->lp);
170a88b5ba8SSam Ravnborg 
171a88b5ba8SSam Ravnborg 	return -ECONNRESET;
172a88b5ba8SSam Ravnborg }
173a88b5ba8SSam Ravnborg 
174a88b5ba8SSam Ravnborg static int send_dreg(struct vio_driver_state *vio)
175a88b5ba8SSam Ravnborg {
176a88b5ba8SSam Ravnborg 	struct vio_dring_state *dr = &vio->drings[VIO_DRIVER_TX_RING];
177a88b5ba8SSam Ravnborg 	union {
178a88b5ba8SSam Ravnborg 		struct vio_dring_register pkt;
179a88b5ba8SSam Ravnborg 		char all[sizeof(struct vio_dring_register) +
180a88b5ba8SSam Ravnborg 			 (sizeof(struct ldc_trans_cookie) *
181a88b5ba8SSam Ravnborg 			  dr->ncookies)];
182a88b5ba8SSam Ravnborg 	} u;
183a88b5ba8SSam Ravnborg 	int i;
184a88b5ba8SSam Ravnborg 
185a88b5ba8SSam Ravnborg 	memset(&u, 0, sizeof(u));
186a88b5ba8SSam Ravnborg 	init_tag(&u.pkt.tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_DRING_REG);
187a88b5ba8SSam Ravnborg 	u.pkt.dring_ident = 0;
188a88b5ba8SSam Ravnborg 	u.pkt.num_descr = dr->num_entries;
189a88b5ba8SSam Ravnborg 	u.pkt.descr_size = dr->entry_size;
190a88b5ba8SSam Ravnborg 	u.pkt.options = VIO_TX_DRING;
191a88b5ba8SSam Ravnborg 	u.pkt.num_cookies = dr->ncookies;
192a88b5ba8SSam Ravnborg 
193a88b5ba8SSam Ravnborg 	viodbg(HS, "SEND DRING_REG INFO ndesc[%u] dsz[%u] opt[0x%x] "
194a88b5ba8SSam Ravnborg 	       "ncookies[%u]\n",
195a88b5ba8SSam Ravnborg 	       u.pkt.num_descr, u.pkt.descr_size, u.pkt.options,
196a88b5ba8SSam Ravnborg 	       u.pkt.num_cookies);
197a88b5ba8SSam Ravnborg 
198a88b5ba8SSam Ravnborg 	for (i = 0; i < dr->ncookies; i++) {
199a88b5ba8SSam Ravnborg 		u.pkt.cookies[i] = dr->cookies[i];
200a88b5ba8SSam Ravnborg 
201a88b5ba8SSam Ravnborg 		viodbg(HS, "DRING COOKIE(%d) [%016llx:%016llx]\n",
202a88b5ba8SSam Ravnborg 		       i,
203a88b5ba8SSam Ravnborg 		       (unsigned long long) u.pkt.cookies[i].cookie_addr,
204a88b5ba8SSam Ravnborg 		       (unsigned long long) u.pkt.cookies[i].cookie_size);
205a88b5ba8SSam Ravnborg 	}
206a88b5ba8SSam Ravnborg 
207a88b5ba8SSam Ravnborg 	return send_ctrl(vio, &u.pkt.tag, sizeof(u));
208a88b5ba8SSam Ravnborg }
209a88b5ba8SSam Ravnborg 
210a88b5ba8SSam Ravnborg static int send_rdx(struct vio_driver_state *vio)
211a88b5ba8SSam Ravnborg {
212a88b5ba8SSam Ravnborg 	struct vio_rdx pkt;
213a88b5ba8SSam Ravnborg 
214a88b5ba8SSam Ravnborg 	memset(&pkt, 0, sizeof(pkt));
215a88b5ba8SSam Ravnborg 
216a88b5ba8SSam Ravnborg 	init_tag(&pkt.tag, VIO_TYPE_CTRL, VIO_SUBTYPE_INFO, VIO_RDX);
217a88b5ba8SSam Ravnborg 
218a88b5ba8SSam Ravnborg 	viodbg(HS, "SEND RDX INFO\n");
219a88b5ba8SSam Ravnborg 
220a88b5ba8SSam Ravnborg 	return send_ctrl(vio, &pkt.tag, sizeof(pkt));
221a88b5ba8SSam Ravnborg }
222a88b5ba8SSam Ravnborg 
223a88b5ba8SSam Ravnborg static int send_attr(struct vio_driver_state *vio)
224a88b5ba8SSam Ravnborg {
225a88b5ba8SSam Ravnborg 	return vio->ops->send_attr(vio);
226a88b5ba8SSam Ravnborg }
227a88b5ba8SSam Ravnborg 
228a88b5ba8SSam Ravnborg static struct vio_version *find_by_major(struct vio_driver_state *vio,
229a88b5ba8SSam Ravnborg 					 u16 major)
230a88b5ba8SSam Ravnborg {
231a88b5ba8SSam Ravnborg 	struct vio_version *ret = NULL;
232a88b5ba8SSam Ravnborg 	int i;
233a88b5ba8SSam Ravnborg 
234a88b5ba8SSam Ravnborg 	for (i = 0; i < vio->ver_table_entries; i++) {
235a88b5ba8SSam Ravnborg 		struct vio_version *v = &vio->ver_table[i];
236a88b5ba8SSam Ravnborg 		if (v->major <= major) {
237a88b5ba8SSam Ravnborg 			ret = v;
238a88b5ba8SSam Ravnborg 			break;
239a88b5ba8SSam Ravnborg 		}
240a88b5ba8SSam Ravnborg 	}
241a88b5ba8SSam Ravnborg 	return ret;
242a88b5ba8SSam Ravnborg }
243a88b5ba8SSam Ravnborg 
244a88b5ba8SSam Ravnborg static int process_ver_info(struct vio_driver_state *vio,
245a88b5ba8SSam Ravnborg 			    struct vio_ver_info *pkt)
246a88b5ba8SSam Ravnborg {
247a88b5ba8SSam Ravnborg 	struct vio_version *vap;
248a88b5ba8SSam Ravnborg 	int err;
249a88b5ba8SSam Ravnborg 
250a88b5ba8SSam Ravnborg 	viodbg(HS, "GOT VERSION INFO maj[%u] min[%u] devclass[%u]\n",
251a88b5ba8SSam Ravnborg 	       pkt->major, pkt->minor, pkt->dev_class);
252a88b5ba8SSam Ravnborg 
253a88b5ba8SSam Ravnborg 	if (vio->hs_state != VIO_HS_INVALID) {
254a88b5ba8SSam Ravnborg 		/* XXX Perhaps invoke start_handshake? XXX */
255a88b5ba8SSam Ravnborg 		memset(&vio->ver, 0, sizeof(vio->ver));
256a88b5ba8SSam Ravnborg 		vio->hs_state = VIO_HS_INVALID;
257a88b5ba8SSam Ravnborg 	}
258a88b5ba8SSam Ravnborg 
259a88b5ba8SSam Ravnborg 	vap = find_by_major(vio, pkt->major);
260a88b5ba8SSam Ravnborg 
261a88b5ba8SSam Ravnborg 	vio->_peer_sid = pkt->tag.sid;
262a88b5ba8SSam Ravnborg 
263a88b5ba8SSam Ravnborg 	if (!vap) {
264a88b5ba8SSam Ravnborg 		pkt->tag.stype = VIO_SUBTYPE_NACK;
265a88b5ba8SSam Ravnborg 		pkt->major = 0;
266a88b5ba8SSam Ravnborg 		pkt->minor = 0;
267a88b5ba8SSam Ravnborg 		viodbg(HS, "SEND VERSION NACK maj[0] min[0]\n");
268a88b5ba8SSam Ravnborg 		err = send_ctrl(vio, &pkt->tag, sizeof(*pkt));
269a88b5ba8SSam Ravnborg 	} else if (vap->major != pkt->major) {
270a88b5ba8SSam Ravnborg 		pkt->tag.stype = VIO_SUBTYPE_NACK;
271a88b5ba8SSam Ravnborg 		pkt->major = vap->major;
272a88b5ba8SSam Ravnborg 		pkt->minor = vap->minor;
273a88b5ba8SSam Ravnborg 		viodbg(HS, "SEND VERSION NACK maj[%u] min[%u]\n",
274a88b5ba8SSam Ravnborg 		       pkt->major, pkt->minor);
275a88b5ba8SSam Ravnborg 		err = send_ctrl(vio, &pkt->tag, sizeof(*pkt));
276a88b5ba8SSam Ravnborg 	} else {
277a88b5ba8SSam Ravnborg 		struct vio_version ver = {
278a88b5ba8SSam Ravnborg 			.major = pkt->major,
279a88b5ba8SSam Ravnborg 			.minor = pkt->minor,
280a88b5ba8SSam Ravnborg 		};
281a88b5ba8SSam Ravnborg 		if (ver.minor > vap->minor)
282a88b5ba8SSam Ravnborg 			ver.minor = vap->minor;
283a88b5ba8SSam Ravnborg 		pkt->minor = ver.minor;
284a88b5ba8SSam Ravnborg 		pkt->tag.stype = VIO_SUBTYPE_ACK;
285a88b5ba8SSam Ravnborg 		viodbg(HS, "SEND VERSION ACK maj[%u] min[%u]\n",
286a88b5ba8SSam Ravnborg 		       pkt->major, pkt->minor);
287a88b5ba8SSam Ravnborg 		err = send_ctrl(vio, &pkt->tag, sizeof(*pkt));
288a88b5ba8SSam Ravnborg 		if (err > 0) {
289a88b5ba8SSam Ravnborg 			vio->ver = ver;
290a88b5ba8SSam Ravnborg 			vio->hs_state = VIO_HS_GOTVERS;
291a88b5ba8SSam Ravnborg 		}
292a88b5ba8SSam Ravnborg 	}
293a88b5ba8SSam Ravnborg 	if (err < 0)
294a88b5ba8SSam Ravnborg 		return handshake_failure(vio);
295a88b5ba8SSam Ravnborg 
296a88b5ba8SSam Ravnborg 	return 0;
297a88b5ba8SSam Ravnborg }
298a88b5ba8SSam Ravnborg 
299a88b5ba8SSam Ravnborg static int process_ver_ack(struct vio_driver_state *vio,
300a88b5ba8SSam Ravnborg 			   struct vio_ver_info *pkt)
301a88b5ba8SSam Ravnborg {
302a88b5ba8SSam Ravnborg 	viodbg(HS, "GOT VERSION ACK maj[%u] min[%u] devclass[%u]\n",
303a88b5ba8SSam Ravnborg 	       pkt->major, pkt->minor, pkt->dev_class);
304a88b5ba8SSam Ravnborg 
305a88b5ba8SSam Ravnborg 	if (vio->hs_state & VIO_HS_GOTVERS) {
306a88b5ba8SSam Ravnborg 		if (vio->ver.major != pkt->major ||
307a88b5ba8SSam Ravnborg 		    vio->ver.minor != pkt->minor) {
308a88b5ba8SSam Ravnborg 			pkt->tag.stype = VIO_SUBTYPE_NACK;
309a88b5ba8SSam Ravnborg 			(void) send_ctrl(vio, &pkt->tag, sizeof(*pkt));
310a88b5ba8SSam Ravnborg 			return handshake_failure(vio);
311a88b5ba8SSam Ravnborg 		}
312a88b5ba8SSam Ravnborg 	} else {
313a88b5ba8SSam Ravnborg 		vio->ver.major = pkt->major;
314a88b5ba8SSam Ravnborg 		vio->ver.minor = pkt->minor;
315a88b5ba8SSam Ravnborg 		vio->hs_state = VIO_HS_GOTVERS;
316a88b5ba8SSam Ravnborg 	}
317a88b5ba8SSam Ravnborg 
318a88b5ba8SSam Ravnborg 	switch (vio->dev_class) {
319a88b5ba8SSam Ravnborg 	case VDEV_NETWORK:
320a88b5ba8SSam Ravnborg 	case VDEV_DISK:
321a88b5ba8SSam Ravnborg 		if (send_attr(vio) < 0)
322a88b5ba8SSam Ravnborg 			return handshake_failure(vio);
323a88b5ba8SSam Ravnborg 		break;
324a88b5ba8SSam Ravnborg 
325a88b5ba8SSam Ravnborg 	default:
326a88b5ba8SSam Ravnborg 		break;
327a88b5ba8SSam Ravnborg 	}
328a88b5ba8SSam Ravnborg 
329a88b5ba8SSam Ravnborg 	return 0;
330a88b5ba8SSam Ravnborg }
331a88b5ba8SSam Ravnborg 
332a88b5ba8SSam Ravnborg static int process_ver_nack(struct vio_driver_state *vio,
333a88b5ba8SSam Ravnborg 			    struct vio_ver_info *pkt)
334a88b5ba8SSam Ravnborg {
335a88b5ba8SSam Ravnborg 	struct vio_version *nver;
336a88b5ba8SSam Ravnborg 
337a88b5ba8SSam Ravnborg 	viodbg(HS, "GOT VERSION NACK maj[%u] min[%u] devclass[%u]\n",
338a88b5ba8SSam Ravnborg 	       pkt->major, pkt->minor, pkt->dev_class);
339a88b5ba8SSam Ravnborg 
3402eac5a0dSSam Ravnborg 	if (pkt->major == 0 && pkt->minor == 0)
3412eac5a0dSSam Ravnborg 		return handshake_failure(vio);
3422eac5a0dSSam Ravnborg 	nver = find_by_major(vio, pkt->major);
3432eac5a0dSSam Ravnborg 	if (!nver)
344a88b5ba8SSam Ravnborg 		return handshake_failure(vio);
345a88b5ba8SSam Ravnborg 
346a88b5ba8SSam Ravnborg 	if (send_version(vio, nver->major, nver->minor) < 0)
347a88b5ba8SSam Ravnborg 		return handshake_failure(vio);
348a88b5ba8SSam Ravnborg 
349a88b5ba8SSam Ravnborg 	return 0;
350a88b5ba8SSam Ravnborg }
351a88b5ba8SSam Ravnborg 
352a88b5ba8SSam Ravnborg static int process_ver(struct vio_driver_state *vio, struct vio_ver_info *pkt)
353a88b5ba8SSam Ravnborg {
354a88b5ba8SSam Ravnborg 	switch (pkt->tag.stype) {
355a88b5ba8SSam Ravnborg 	case VIO_SUBTYPE_INFO:
356a88b5ba8SSam Ravnborg 		return process_ver_info(vio, pkt);
357a88b5ba8SSam Ravnborg 
358a88b5ba8SSam Ravnborg 	case VIO_SUBTYPE_ACK:
359a88b5ba8SSam Ravnborg 		return process_ver_ack(vio, pkt);
360a88b5ba8SSam Ravnborg 
361a88b5ba8SSam Ravnborg 	case VIO_SUBTYPE_NACK:
362a88b5ba8SSam Ravnborg 		return process_ver_nack(vio, pkt);
363a88b5ba8SSam Ravnborg 
364a88b5ba8SSam Ravnborg 	default:
365a88b5ba8SSam Ravnborg 		return handshake_failure(vio);
3666cb79b3fSJoe Perches 	}
367a88b5ba8SSam Ravnborg }
368a88b5ba8SSam Ravnborg 
369a88b5ba8SSam Ravnborg static int process_attr(struct vio_driver_state *vio, void *pkt)
370a88b5ba8SSam Ravnborg {
371a88b5ba8SSam Ravnborg 	int err;
372a88b5ba8SSam Ravnborg 
373a88b5ba8SSam Ravnborg 	if (!(vio->hs_state & VIO_HS_GOTVERS))
374a88b5ba8SSam Ravnborg 		return handshake_failure(vio);
375a88b5ba8SSam Ravnborg 
376a88b5ba8SSam Ravnborg 	err = vio->ops->handle_attr(vio, pkt);
377a88b5ba8SSam Ravnborg 	if (err < 0) {
378a88b5ba8SSam Ravnborg 		return handshake_failure(vio);
379a88b5ba8SSam Ravnborg 	} else {
380a88b5ba8SSam Ravnborg 		vio->hs_state |= VIO_HS_GOT_ATTR;
381a88b5ba8SSam Ravnborg 
382a88b5ba8SSam Ravnborg 		if ((vio->dr_state & VIO_DR_STATE_TXREQ) &&
383a88b5ba8SSam Ravnborg 		    !(vio->hs_state & VIO_HS_SENT_DREG)) {
384a88b5ba8SSam Ravnborg 			if (send_dreg(vio) < 0)
385a88b5ba8SSam Ravnborg 				return handshake_failure(vio);
386a88b5ba8SSam Ravnborg 
387a88b5ba8SSam Ravnborg 			vio->hs_state |= VIO_HS_SENT_DREG;
388a88b5ba8SSam Ravnborg 		}
389a88b5ba8SSam Ravnborg 	}
390a88b5ba8SSam Ravnborg 	return 0;
391a88b5ba8SSam Ravnborg }
392a88b5ba8SSam Ravnborg 
393a88b5ba8SSam Ravnborg static int all_drings_registered(struct vio_driver_state *vio)
394a88b5ba8SSam Ravnborg {
395a88b5ba8SSam Ravnborg 	int need_rx, need_tx;
396a88b5ba8SSam Ravnborg 
397a88b5ba8SSam Ravnborg 	need_rx = (vio->dr_state & VIO_DR_STATE_RXREQ);
398a88b5ba8SSam Ravnborg 	need_tx = (vio->dr_state & VIO_DR_STATE_TXREQ);
399a88b5ba8SSam Ravnborg 
400a88b5ba8SSam Ravnborg 	if (need_rx &&
401a88b5ba8SSam Ravnborg 	    !(vio->dr_state & VIO_DR_STATE_RXREG))
402a88b5ba8SSam Ravnborg 		return 0;
403a88b5ba8SSam Ravnborg 
404a88b5ba8SSam Ravnborg 	if (need_tx &&
405a88b5ba8SSam Ravnborg 	    !(vio->dr_state & VIO_DR_STATE_TXREG))
406a88b5ba8SSam Ravnborg 		return 0;
407a88b5ba8SSam Ravnborg 
408a88b5ba8SSam Ravnborg 	return 1;
409a88b5ba8SSam Ravnborg }
410a88b5ba8SSam Ravnborg 
411a88b5ba8SSam Ravnborg static int process_dreg_info(struct vio_driver_state *vio,
412a88b5ba8SSam Ravnborg 			     struct vio_dring_register *pkt)
413a88b5ba8SSam Ravnborg {
414a88b5ba8SSam Ravnborg 	struct vio_dring_state *dr;
415a88b5ba8SSam Ravnborg 	int i, len;
416a88b5ba8SSam Ravnborg 
417a88b5ba8SSam Ravnborg 	viodbg(HS, "GOT DRING_REG INFO ident[%llx] "
418a88b5ba8SSam Ravnborg 	       "ndesc[%u] dsz[%u] opt[0x%x] ncookies[%u]\n",
419a88b5ba8SSam Ravnborg 	       (unsigned long long) pkt->dring_ident,
420a88b5ba8SSam Ravnborg 	       pkt->num_descr, pkt->descr_size, pkt->options,
421a88b5ba8SSam Ravnborg 	       pkt->num_cookies);
422a88b5ba8SSam Ravnborg 
423a88b5ba8SSam Ravnborg 	if (!(vio->dr_state & VIO_DR_STATE_RXREQ))
424a88b5ba8SSam Ravnborg 		goto send_nack;
425a88b5ba8SSam Ravnborg 
426a88b5ba8SSam Ravnborg 	if (vio->dr_state & VIO_DR_STATE_RXREG)
427a88b5ba8SSam Ravnborg 		goto send_nack;
428a88b5ba8SSam Ravnborg 
429a88b5ba8SSam Ravnborg 	BUG_ON(vio->desc_buf);
430a88b5ba8SSam Ravnborg 
431a88b5ba8SSam Ravnborg 	vio->desc_buf = kzalloc(pkt->descr_size, GFP_ATOMIC);
432a88b5ba8SSam Ravnborg 	if (!vio->desc_buf)
433a88b5ba8SSam Ravnborg 		goto send_nack;
434a88b5ba8SSam Ravnborg 
435a88b5ba8SSam Ravnborg 	vio->desc_buf_len = pkt->descr_size;
436a88b5ba8SSam Ravnborg 
437a88b5ba8SSam Ravnborg 	dr = &vio->drings[VIO_DRIVER_RX_RING];
438a88b5ba8SSam Ravnborg 
439a88b5ba8SSam Ravnborg 	dr->num_entries = pkt->num_descr;
440a88b5ba8SSam Ravnborg 	dr->entry_size = pkt->descr_size;
441a88b5ba8SSam Ravnborg 	dr->ncookies = pkt->num_cookies;
442a88b5ba8SSam Ravnborg 	for (i = 0; i < dr->ncookies; i++) {
443a88b5ba8SSam Ravnborg 		dr->cookies[i] = pkt->cookies[i];
444a88b5ba8SSam Ravnborg 
445a88b5ba8SSam Ravnborg 		viodbg(HS, "DRING COOKIE(%d) [%016llx:%016llx]\n",
446a88b5ba8SSam Ravnborg 		       i,
447a88b5ba8SSam Ravnborg 		       (unsigned long long)
448a88b5ba8SSam Ravnborg 		       pkt->cookies[i].cookie_addr,
449a88b5ba8SSam Ravnborg 		       (unsigned long long)
450a88b5ba8SSam Ravnborg 		       pkt->cookies[i].cookie_size);
451a88b5ba8SSam Ravnborg 	}
452a88b5ba8SSam Ravnborg 
453a88b5ba8SSam Ravnborg 	pkt->tag.stype = VIO_SUBTYPE_ACK;
454a88b5ba8SSam Ravnborg 	pkt->dring_ident = ++dr->ident;
455a88b5ba8SSam Ravnborg 
456a88b5ba8SSam Ravnborg 	viodbg(HS, "SEND DRING_REG ACK ident[%llx]\n",
457a88b5ba8SSam Ravnborg 	       (unsigned long long) pkt->dring_ident);
458a88b5ba8SSam Ravnborg 
459a88b5ba8SSam Ravnborg 	len = (sizeof(*pkt) +
460a88b5ba8SSam Ravnborg 	       (dr->ncookies * sizeof(struct ldc_trans_cookie)));
461a88b5ba8SSam Ravnborg 	if (send_ctrl(vio, &pkt->tag, len) < 0)
462a88b5ba8SSam Ravnborg 		goto send_nack;
463a88b5ba8SSam Ravnborg 
464a88b5ba8SSam Ravnborg 	vio->dr_state |= VIO_DR_STATE_RXREG;
465a88b5ba8SSam Ravnborg 
466a88b5ba8SSam Ravnborg 	return 0;
467a88b5ba8SSam Ravnborg 
468a88b5ba8SSam Ravnborg send_nack:
469a88b5ba8SSam Ravnborg 	pkt->tag.stype = VIO_SUBTYPE_NACK;
470a88b5ba8SSam Ravnborg 	viodbg(HS, "SEND DRING_REG NACK\n");
471a88b5ba8SSam Ravnborg 	(void) send_ctrl(vio, &pkt->tag, sizeof(*pkt));
472a88b5ba8SSam Ravnborg 
473a88b5ba8SSam Ravnborg 	return handshake_failure(vio);
474a88b5ba8SSam Ravnborg }
475a88b5ba8SSam Ravnborg 
476a88b5ba8SSam Ravnborg static int process_dreg_ack(struct vio_driver_state *vio,
477a88b5ba8SSam Ravnborg 			    struct vio_dring_register *pkt)
478a88b5ba8SSam Ravnborg {
479a88b5ba8SSam Ravnborg 	struct vio_dring_state *dr;
480a88b5ba8SSam Ravnborg 
481a88b5ba8SSam Ravnborg 	viodbg(HS, "GOT DRING_REG ACK ident[%llx] "
482a88b5ba8SSam Ravnborg 	       "ndesc[%u] dsz[%u] opt[0x%x] ncookies[%u]\n",
483a88b5ba8SSam Ravnborg 	       (unsigned long long) pkt->dring_ident,
484a88b5ba8SSam Ravnborg 	       pkt->num_descr, pkt->descr_size, pkt->options,
485a88b5ba8SSam Ravnborg 	       pkt->num_cookies);
486a88b5ba8SSam Ravnborg 
487a88b5ba8SSam Ravnborg 	dr = &vio->drings[VIO_DRIVER_TX_RING];
488a88b5ba8SSam Ravnborg 
489a88b5ba8SSam Ravnborg 	if (!(vio->dr_state & VIO_DR_STATE_TXREQ))
490a88b5ba8SSam Ravnborg 		return handshake_failure(vio);
491a88b5ba8SSam Ravnborg 
492a88b5ba8SSam Ravnborg 	dr->ident = pkt->dring_ident;
493a88b5ba8SSam Ravnborg 	vio->dr_state |= VIO_DR_STATE_TXREG;
494a88b5ba8SSam Ravnborg 
495a88b5ba8SSam Ravnborg 	if (all_drings_registered(vio)) {
496a88b5ba8SSam Ravnborg 		if (send_rdx(vio) < 0)
497a88b5ba8SSam Ravnborg 			return handshake_failure(vio);
498a88b5ba8SSam Ravnborg 		vio->hs_state = VIO_HS_SENT_RDX;
499a88b5ba8SSam Ravnborg 	}
500a88b5ba8SSam Ravnborg 	return 0;
501a88b5ba8SSam Ravnborg }
502a88b5ba8SSam Ravnborg 
503a88b5ba8SSam Ravnborg static int process_dreg_nack(struct vio_driver_state *vio,
504a88b5ba8SSam Ravnborg 			     struct vio_dring_register *pkt)
505a88b5ba8SSam Ravnborg {
506a88b5ba8SSam Ravnborg 	viodbg(HS, "GOT DRING_REG NACK ident[%llx] "
507a88b5ba8SSam Ravnborg 	       "ndesc[%u] dsz[%u] opt[0x%x] ncookies[%u]\n",
508a88b5ba8SSam Ravnborg 	       (unsigned long long) pkt->dring_ident,
509a88b5ba8SSam Ravnborg 	       pkt->num_descr, pkt->descr_size, pkt->options,
510a88b5ba8SSam Ravnborg 	       pkt->num_cookies);
511a88b5ba8SSam Ravnborg 
512a88b5ba8SSam Ravnborg 	return handshake_failure(vio);
513a88b5ba8SSam Ravnborg }
514a88b5ba8SSam Ravnborg 
515a88b5ba8SSam Ravnborg static int process_dreg(struct vio_driver_state *vio,
516a88b5ba8SSam Ravnborg 			struct vio_dring_register *pkt)
517a88b5ba8SSam Ravnborg {
518a88b5ba8SSam Ravnborg 	if (!(vio->hs_state & VIO_HS_GOTVERS))
519a88b5ba8SSam Ravnborg 		return handshake_failure(vio);
520a88b5ba8SSam Ravnborg 
521a88b5ba8SSam Ravnborg 	switch (pkt->tag.stype) {
522a88b5ba8SSam Ravnborg 	case VIO_SUBTYPE_INFO:
523a88b5ba8SSam Ravnborg 		return process_dreg_info(vio, pkt);
524a88b5ba8SSam Ravnborg 
525a88b5ba8SSam Ravnborg 	case VIO_SUBTYPE_ACK:
526a88b5ba8SSam Ravnborg 		return process_dreg_ack(vio, pkt);
527a88b5ba8SSam Ravnborg 
528a88b5ba8SSam Ravnborg 	case VIO_SUBTYPE_NACK:
529a88b5ba8SSam Ravnborg 		return process_dreg_nack(vio, pkt);
530a88b5ba8SSam Ravnborg 
531a88b5ba8SSam Ravnborg 	default:
532a88b5ba8SSam Ravnborg 		return handshake_failure(vio);
533a88b5ba8SSam Ravnborg 	}
534a88b5ba8SSam Ravnborg }
535a88b5ba8SSam Ravnborg 
536a88b5ba8SSam Ravnborg static int process_dunreg(struct vio_driver_state *vio,
537a88b5ba8SSam Ravnborg 			  struct vio_dring_unregister *pkt)
538a88b5ba8SSam Ravnborg {
539a88b5ba8SSam Ravnborg 	struct vio_dring_state *dr = &vio->drings[VIO_DRIVER_RX_RING];
540a88b5ba8SSam Ravnborg 
541a88b5ba8SSam Ravnborg 	viodbg(HS, "GOT DRING_UNREG\n");
542a88b5ba8SSam Ravnborg 
543a88b5ba8SSam Ravnborg 	if (pkt->dring_ident != dr->ident)
544a88b5ba8SSam Ravnborg 		return 0;
545a88b5ba8SSam Ravnborg 
546a88b5ba8SSam Ravnborg 	vio->dr_state &= ~VIO_DR_STATE_RXREG;
547a88b5ba8SSam Ravnborg 
548a88b5ba8SSam Ravnborg 	memset(dr, 0, sizeof(*dr));
549a88b5ba8SSam Ravnborg 
550a88b5ba8SSam Ravnborg 	kfree(vio->desc_buf);
551a88b5ba8SSam Ravnborg 	vio->desc_buf = NULL;
552a88b5ba8SSam Ravnborg 	vio->desc_buf_len = 0;
553a88b5ba8SSam Ravnborg 
554a88b5ba8SSam Ravnborg 	return 0;
555a88b5ba8SSam Ravnborg }
556a88b5ba8SSam Ravnborg 
557a88b5ba8SSam Ravnborg static int process_rdx_info(struct vio_driver_state *vio, struct vio_rdx *pkt)
558a88b5ba8SSam Ravnborg {
559a88b5ba8SSam Ravnborg 	viodbg(HS, "GOT RDX INFO\n");
560a88b5ba8SSam Ravnborg 
561a88b5ba8SSam Ravnborg 	pkt->tag.stype = VIO_SUBTYPE_ACK;
562a88b5ba8SSam Ravnborg 	viodbg(HS, "SEND RDX ACK\n");
563a88b5ba8SSam Ravnborg 	if (send_ctrl(vio, &pkt->tag, sizeof(*pkt)) < 0)
564a88b5ba8SSam Ravnborg 		return handshake_failure(vio);
565a88b5ba8SSam Ravnborg 
566a88b5ba8SSam Ravnborg 	vio->hs_state |= VIO_HS_SENT_RDX_ACK;
567a88b5ba8SSam Ravnborg 	return 0;
568a88b5ba8SSam Ravnborg }
569a88b5ba8SSam Ravnborg 
570a88b5ba8SSam Ravnborg static int process_rdx_ack(struct vio_driver_state *vio, struct vio_rdx *pkt)
571a88b5ba8SSam Ravnborg {
572a88b5ba8SSam Ravnborg 	viodbg(HS, "GOT RDX ACK\n");
573a88b5ba8SSam Ravnborg 
574a88b5ba8SSam Ravnborg 	if (!(vio->hs_state & VIO_HS_SENT_RDX))
575a88b5ba8SSam Ravnborg 		return handshake_failure(vio);
576a88b5ba8SSam Ravnborg 
577a88b5ba8SSam Ravnborg 	vio->hs_state |= VIO_HS_GOT_RDX_ACK;
578a88b5ba8SSam Ravnborg 	return 0;
579a88b5ba8SSam Ravnborg }
580a88b5ba8SSam Ravnborg 
581a88b5ba8SSam Ravnborg static int process_rdx_nack(struct vio_driver_state *vio, struct vio_rdx *pkt)
582a88b5ba8SSam Ravnborg {
583a88b5ba8SSam Ravnborg 	viodbg(HS, "GOT RDX NACK\n");
584a88b5ba8SSam Ravnborg 
585a88b5ba8SSam Ravnborg 	return handshake_failure(vio);
586a88b5ba8SSam Ravnborg }
587a88b5ba8SSam Ravnborg 
588a88b5ba8SSam Ravnborg static int process_rdx(struct vio_driver_state *vio, struct vio_rdx *pkt)
589a88b5ba8SSam Ravnborg {
590a88b5ba8SSam Ravnborg 	if (!all_drings_registered(vio))
591a88b5ba8SSam Ravnborg 		handshake_failure(vio);
592a88b5ba8SSam Ravnborg 
593a88b5ba8SSam Ravnborg 	switch (pkt->tag.stype) {
594a88b5ba8SSam Ravnborg 	case VIO_SUBTYPE_INFO:
595a88b5ba8SSam Ravnborg 		return process_rdx_info(vio, pkt);
596a88b5ba8SSam Ravnborg 
597a88b5ba8SSam Ravnborg 	case VIO_SUBTYPE_ACK:
598a88b5ba8SSam Ravnborg 		return process_rdx_ack(vio, pkt);
599a88b5ba8SSam Ravnborg 
600a88b5ba8SSam Ravnborg 	case VIO_SUBTYPE_NACK:
601a88b5ba8SSam Ravnborg 		return process_rdx_nack(vio, pkt);
602a88b5ba8SSam Ravnborg 
603a88b5ba8SSam Ravnborg 	default:
604a88b5ba8SSam Ravnborg 		return handshake_failure(vio);
605a88b5ba8SSam Ravnborg 	}
606a88b5ba8SSam Ravnborg }
607a88b5ba8SSam Ravnborg 
608a88b5ba8SSam Ravnborg int vio_control_pkt_engine(struct vio_driver_state *vio, void *pkt)
609a88b5ba8SSam Ravnborg {
610a88b5ba8SSam Ravnborg 	struct vio_msg_tag *tag = pkt;
611a88b5ba8SSam Ravnborg 	u8 prev_state = vio->hs_state;
612a88b5ba8SSam Ravnborg 	int err;
613a88b5ba8SSam Ravnborg 
614a88b5ba8SSam Ravnborg 	switch (tag->stype_env) {
615a88b5ba8SSam Ravnborg 	case VIO_VER_INFO:
616a88b5ba8SSam Ravnborg 		err = process_ver(vio, pkt);
617a88b5ba8SSam Ravnborg 		break;
618a88b5ba8SSam Ravnborg 
619a88b5ba8SSam Ravnborg 	case VIO_ATTR_INFO:
620a88b5ba8SSam Ravnborg 		err = process_attr(vio, pkt);
621a88b5ba8SSam Ravnborg 		break;
622a88b5ba8SSam Ravnborg 
623a88b5ba8SSam Ravnborg 	case VIO_DRING_REG:
624a88b5ba8SSam Ravnborg 		err = process_dreg(vio, pkt);
625a88b5ba8SSam Ravnborg 		break;
626a88b5ba8SSam Ravnborg 
627a88b5ba8SSam Ravnborg 	case VIO_DRING_UNREG:
628a88b5ba8SSam Ravnborg 		err = process_dunreg(vio, pkt);
629a88b5ba8SSam Ravnborg 		break;
630a88b5ba8SSam Ravnborg 
631a88b5ba8SSam Ravnborg 	case VIO_RDX:
632a88b5ba8SSam Ravnborg 		err = process_rdx(vio, pkt);
633a88b5ba8SSam Ravnborg 		break;
634a88b5ba8SSam Ravnborg 
635a88b5ba8SSam Ravnborg 	default:
636a88b5ba8SSam Ravnborg 		err = process_unknown(vio, pkt);
637a88b5ba8SSam Ravnborg 		break;
638a88b5ba8SSam Ravnborg 	}
639a88b5ba8SSam Ravnborg 	if (!err &&
640a88b5ba8SSam Ravnborg 	    vio->hs_state != prev_state &&
641a88b5ba8SSam Ravnborg 	    (vio->hs_state & VIO_HS_COMPLETE))
642a88b5ba8SSam Ravnborg 		vio->ops->handshake_complete(vio);
643a88b5ba8SSam Ravnborg 
644a88b5ba8SSam Ravnborg 	return err;
645a88b5ba8SSam Ravnborg }
646a88b5ba8SSam Ravnborg EXPORT_SYMBOL(vio_control_pkt_engine);
647a88b5ba8SSam Ravnborg 
648a88b5ba8SSam Ravnborg void vio_conn_reset(struct vio_driver_state *vio)
649a88b5ba8SSam Ravnborg {
650a88b5ba8SSam Ravnborg }
651a88b5ba8SSam Ravnborg EXPORT_SYMBOL(vio_conn_reset);
652a88b5ba8SSam Ravnborg 
653a88b5ba8SSam Ravnborg /* The issue is that the Solaris virtual disk server just mirrors the
654a88b5ba8SSam Ravnborg  * SID values it gets from the client peer.  So we work around that
655a88b5ba8SSam Ravnborg  * here in vio_{validate,send}_sid() so that the drivers don't need
656a88b5ba8SSam Ravnborg  * to be aware of this crap.
657a88b5ba8SSam Ravnborg  */
658a88b5ba8SSam Ravnborg int vio_validate_sid(struct vio_driver_state *vio, struct vio_msg_tag *tp)
659a88b5ba8SSam Ravnborg {
660a88b5ba8SSam Ravnborg 	u32 sid;
661a88b5ba8SSam Ravnborg 
662a88b5ba8SSam Ravnborg 	/* Always let VERSION+INFO packets through unchecked, they
663a88b5ba8SSam Ravnborg 	 * define the new SID.
664a88b5ba8SSam Ravnborg 	 */
665a88b5ba8SSam Ravnborg 	if (tp->type == VIO_TYPE_CTRL &&
666a88b5ba8SSam Ravnborg 	    tp->stype == VIO_SUBTYPE_INFO &&
667a88b5ba8SSam Ravnborg 	    tp->stype_env == VIO_VER_INFO)
668a88b5ba8SSam Ravnborg 		return 0;
669a88b5ba8SSam Ravnborg 
670a88b5ba8SSam Ravnborg 	/* Ok, now figure out which SID to use.  */
671a88b5ba8SSam Ravnborg 	switch (vio->dev_class) {
672a88b5ba8SSam Ravnborg 	case VDEV_NETWORK:
673a88b5ba8SSam Ravnborg 	case VDEV_NETWORK_SWITCH:
674a88b5ba8SSam Ravnborg 	case VDEV_DISK_SERVER:
675a88b5ba8SSam Ravnborg 	default:
676a88b5ba8SSam Ravnborg 		sid = vio->_peer_sid;
677a88b5ba8SSam Ravnborg 		break;
678a88b5ba8SSam Ravnborg 
679a88b5ba8SSam Ravnborg 	case VDEV_DISK:
680a88b5ba8SSam Ravnborg 		sid = vio->_local_sid;
681a88b5ba8SSam Ravnborg 		break;
682a88b5ba8SSam Ravnborg 	}
683a88b5ba8SSam Ravnborg 
684a88b5ba8SSam Ravnborg 	if (sid == tp->sid)
685a88b5ba8SSam Ravnborg 		return 0;
686a88b5ba8SSam Ravnborg 	viodbg(DATA, "BAD SID tag->sid[%08x] peer_sid[%08x] local_sid[%08x]\n",
687a88b5ba8SSam Ravnborg 	       tp->sid, vio->_peer_sid, vio->_local_sid);
688a88b5ba8SSam Ravnborg 	return -EINVAL;
689a88b5ba8SSam Ravnborg }
690a88b5ba8SSam Ravnborg EXPORT_SYMBOL(vio_validate_sid);
691a88b5ba8SSam Ravnborg 
692a88b5ba8SSam Ravnborg u32 vio_send_sid(struct vio_driver_state *vio)
693a88b5ba8SSam Ravnborg {
694a88b5ba8SSam Ravnborg 	switch (vio->dev_class) {
695a88b5ba8SSam Ravnborg 	case VDEV_NETWORK:
696a88b5ba8SSam Ravnborg 	case VDEV_NETWORK_SWITCH:
697a88b5ba8SSam Ravnborg 	case VDEV_DISK:
698a88b5ba8SSam Ravnborg 	default:
699a88b5ba8SSam Ravnborg 		return vio->_local_sid;
700a88b5ba8SSam Ravnborg 
701a88b5ba8SSam Ravnborg 	case VDEV_DISK_SERVER:
702a88b5ba8SSam Ravnborg 		return vio->_peer_sid;
703a88b5ba8SSam Ravnborg 	}
704a88b5ba8SSam Ravnborg }
705a88b5ba8SSam Ravnborg EXPORT_SYMBOL(vio_send_sid);
706a88b5ba8SSam Ravnborg 
707a88b5ba8SSam Ravnborg int vio_ldc_alloc(struct vio_driver_state *vio,
708a88b5ba8SSam Ravnborg 			 struct ldc_channel_config *base_cfg,
709a88b5ba8SSam Ravnborg 			 void *event_arg)
710a88b5ba8SSam Ravnborg {
711a88b5ba8SSam Ravnborg 	struct ldc_channel_config cfg = *base_cfg;
712a88b5ba8SSam Ravnborg 	struct ldc_channel *lp;
713a88b5ba8SSam Ravnborg 
714a88b5ba8SSam Ravnborg 	cfg.tx_irq = vio->vdev->tx_irq;
715a88b5ba8SSam Ravnborg 	cfg.rx_irq = vio->vdev->rx_irq;
716a88b5ba8SSam Ravnborg 
717a88b5ba8SSam Ravnborg 	lp = ldc_alloc(vio->vdev->channel_id, &cfg, event_arg);
718a88b5ba8SSam Ravnborg 	if (IS_ERR(lp))
719a88b5ba8SSam Ravnborg 		return PTR_ERR(lp);
720a88b5ba8SSam Ravnborg 
721a88b5ba8SSam Ravnborg 	vio->lp = lp;
722a88b5ba8SSam Ravnborg 
723a88b5ba8SSam Ravnborg 	return 0;
724a88b5ba8SSam Ravnborg }
725a88b5ba8SSam Ravnborg EXPORT_SYMBOL(vio_ldc_alloc);
726a88b5ba8SSam Ravnborg 
727a88b5ba8SSam Ravnborg void vio_ldc_free(struct vio_driver_state *vio)
728a88b5ba8SSam Ravnborg {
729a88b5ba8SSam Ravnborg 	ldc_free(vio->lp);
730a88b5ba8SSam Ravnborg 	vio->lp = NULL;
731a88b5ba8SSam Ravnborg 
732a88b5ba8SSam Ravnborg 	kfree(vio->desc_buf);
733a88b5ba8SSam Ravnborg 	vio->desc_buf = NULL;
734a88b5ba8SSam Ravnborg 	vio->desc_buf_len = 0;
735a88b5ba8SSam Ravnborg }
736a88b5ba8SSam Ravnborg EXPORT_SYMBOL(vio_ldc_free);
737a88b5ba8SSam Ravnborg 
738a88b5ba8SSam Ravnborg void vio_port_up(struct vio_driver_state *vio)
739a88b5ba8SSam Ravnborg {
740a88b5ba8SSam Ravnborg 	unsigned long flags;
741a88b5ba8SSam Ravnborg 	int err, state;
742a88b5ba8SSam Ravnborg 
743a88b5ba8SSam Ravnborg 	spin_lock_irqsave(&vio->lock, flags);
744a88b5ba8SSam Ravnborg 
745a88b5ba8SSam Ravnborg 	state = ldc_state(vio->lp);
746a88b5ba8SSam Ravnborg 
747a88b5ba8SSam Ravnborg 	err = 0;
748a88b5ba8SSam Ravnborg 	if (state == LDC_STATE_INIT) {
749a88b5ba8SSam Ravnborg 		err = ldc_bind(vio->lp, vio->name);
750a88b5ba8SSam Ravnborg 		if (err)
751a88b5ba8SSam Ravnborg 			printk(KERN_WARNING "%s: Port %lu bind failed, "
752a88b5ba8SSam Ravnborg 			       "err=%d\n",
753a88b5ba8SSam Ravnborg 			       vio->name, vio->vdev->channel_id, err);
754a88b5ba8SSam Ravnborg 	}
755a88b5ba8SSam Ravnborg 
756a88b5ba8SSam Ravnborg 	if (!err) {
757a88b5ba8SSam Ravnborg 		err = ldc_connect(vio->lp);
758a88b5ba8SSam Ravnborg 		if (err)
759a88b5ba8SSam Ravnborg 			printk(KERN_WARNING "%s: Port %lu connect failed, "
760a88b5ba8SSam Ravnborg 			       "err=%d\n",
761a88b5ba8SSam Ravnborg 			       vio->name, vio->vdev->channel_id, err);
762a88b5ba8SSam Ravnborg 	}
763a88b5ba8SSam Ravnborg 	if (err) {
764a88b5ba8SSam Ravnborg 		unsigned long expires = jiffies + HZ;
765a88b5ba8SSam Ravnborg 
766a88b5ba8SSam Ravnborg 		expires = round_jiffies(expires);
767a88b5ba8SSam Ravnborg 		mod_timer(&vio->timer, expires);
768a88b5ba8SSam Ravnborg 	}
769a88b5ba8SSam Ravnborg 
770a88b5ba8SSam Ravnborg 	spin_unlock_irqrestore(&vio->lock, flags);
771a88b5ba8SSam Ravnborg }
772a88b5ba8SSam Ravnborg EXPORT_SYMBOL(vio_port_up);
773a88b5ba8SSam Ravnborg 
774a88b5ba8SSam Ravnborg static void vio_port_timer(unsigned long _arg)
775a88b5ba8SSam Ravnborg {
776a88b5ba8SSam Ravnborg 	struct vio_driver_state *vio = (struct vio_driver_state *) _arg;
777a88b5ba8SSam Ravnborg 
778a88b5ba8SSam Ravnborg 	vio_port_up(vio);
779a88b5ba8SSam Ravnborg }
780a88b5ba8SSam Ravnborg 
781a88b5ba8SSam Ravnborg int vio_driver_init(struct vio_driver_state *vio, struct vio_dev *vdev,
782a88b5ba8SSam Ravnborg 		    u8 dev_class, struct vio_version *ver_table,
783a88b5ba8SSam Ravnborg 		    int ver_table_size, struct vio_driver_ops *ops,
784a88b5ba8SSam Ravnborg 		    char *name)
785a88b5ba8SSam Ravnborg {
786a88b5ba8SSam Ravnborg 	switch (dev_class) {
787a88b5ba8SSam Ravnborg 	case VDEV_NETWORK:
788a88b5ba8SSam Ravnborg 	case VDEV_NETWORK_SWITCH:
789a88b5ba8SSam Ravnborg 	case VDEV_DISK:
790a88b5ba8SSam Ravnborg 	case VDEV_DISK_SERVER:
791a88b5ba8SSam Ravnborg 		break;
792a88b5ba8SSam Ravnborg 
793a88b5ba8SSam Ravnborg 	default:
794a88b5ba8SSam Ravnborg 		return -EINVAL;
795a88b5ba8SSam Ravnborg 	}
796a88b5ba8SSam Ravnborg 
797a88b5ba8SSam Ravnborg 	if (!ops->send_attr ||
798a88b5ba8SSam Ravnborg 	    !ops->handle_attr ||
799a88b5ba8SSam Ravnborg 	    !ops->handshake_complete)
800a88b5ba8SSam Ravnborg 		return -EINVAL;
801a88b5ba8SSam Ravnborg 
802a88b5ba8SSam Ravnborg 	if (!ver_table || ver_table_size < 0)
803a88b5ba8SSam Ravnborg 		return -EINVAL;
804a88b5ba8SSam Ravnborg 
805a88b5ba8SSam Ravnborg 	if (!name)
806a88b5ba8SSam Ravnborg 		return -EINVAL;
807a88b5ba8SSam Ravnborg 
808a88b5ba8SSam Ravnborg 	spin_lock_init(&vio->lock);
809a88b5ba8SSam Ravnborg 
810a88b5ba8SSam Ravnborg 	vio->name = name;
811a88b5ba8SSam Ravnborg 
812a88b5ba8SSam Ravnborg 	vio->dev_class = dev_class;
813a88b5ba8SSam Ravnborg 	vio->vdev = vdev;
814a88b5ba8SSam Ravnborg 
815a88b5ba8SSam Ravnborg 	vio->ver_table = ver_table;
816a88b5ba8SSam Ravnborg 	vio->ver_table_entries = ver_table_size;
817a88b5ba8SSam Ravnborg 
818a88b5ba8SSam Ravnborg 	vio->ops = ops;
819a88b5ba8SSam Ravnborg 
820a88b5ba8SSam Ravnborg 	setup_timer(&vio->timer, vio_port_timer, (unsigned long) vio);
821a88b5ba8SSam Ravnborg 
822a88b5ba8SSam Ravnborg 	return 0;
823a88b5ba8SSam Ravnborg }
824a88b5ba8SSam Ravnborg EXPORT_SYMBOL(vio_driver_init);
825