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