1 /*
2  * ssi_protocol.c
3  *
4  * Implementation of the SSI McSAAB improved protocol.
5  *
6  * Copyright (C) 2010 Nokia Corporation. All rights reserved.
7  * Copyright (C) 2013 Sebastian Reichel <sre@kernel.org>
8  *
9  * Contact: Carlos Chinea <carlos.chinea@nokia.com>
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * version 2 as published by the Free Software Foundation.
14  *
15  * This program is distributed in the hope that it will be useful, but
16  * WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
23  * 02110-1301 USA
24  */
25 
26 #include <linux/atomic.h>
27 #include <linux/clk.h>
28 #include <linux/device.h>
29 #include <linux/err.h>
30 #include <linux/gpio.h>
31 #include <linux/if_ether.h>
32 #include <linux/if_arp.h>
33 #include <linux/if_phonet.h>
34 #include <linux/init.h>
35 #include <linux/irq.h>
36 #include <linux/list.h>
37 #include <linux/module.h>
38 #include <linux/netdevice.h>
39 #include <linux/notifier.h>
40 #include <linux/scatterlist.h>
41 #include <linux/skbuff.h>
42 #include <linux/slab.h>
43 #include <linux/spinlock.h>
44 #include <linux/timer.h>
45 #include <linux/hsi/hsi.h>
46 #include <linux/hsi/ssi_protocol.h>
47 
48 void ssi_waketest(struct hsi_client *cl, unsigned int enable);
49 
50 #define SSIP_TXQUEUE_LEN	100
51 #define SSIP_MAX_MTU		65535
52 #define SSIP_DEFAULT_MTU	4000
53 #define PN_MEDIA_SOS		21
54 #define SSIP_MIN_PN_HDR		6	/* FIXME: Revisit */
55 #define SSIP_WDTOUT		2000	/* FIXME: has to be 500 msecs */
56 #define SSIP_KATOUT		15	/* 15 msecs */
57 #define SSIP_MAX_CMDS		5 /* Number of pre-allocated commands buffers */
58 #define SSIP_BYTES_TO_FRAMES(x) ((((x) - 1) >> 2) + 1)
59 #define SSIP_CMT_LOADER_SYNC	0x11223344
60 /*
61  * SSI protocol command definitions
62  */
63 #define SSIP_COMMAND(data)	((data) >> 28)
64 #define SSIP_PAYLOAD(data)	((data) & 0xfffffff)
65 /* Commands */
66 #define SSIP_SW_BREAK		0
67 #define SSIP_BOOTINFO_REQ	1
68 #define SSIP_BOOTINFO_RESP	2
69 #define SSIP_WAKETEST_RESULT	3
70 #define SSIP_START_TRANS	4
71 #define SSIP_READY		5
72 /* Payloads */
73 #define SSIP_DATA_VERSION(data)	((data) & 0xff)
74 #define SSIP_LOCAL_VERID	1
75 #define SSIP_WAKETEST_OK	0
76 #define SSIP_WAKETEST_FAILED	1
77 #define SSIP_PDU_LENGTH(data)	(((data) >> 8) & 0xffff)
78 #define SSIP_MSG_ID(data)	((data) & 0xff)
79 /* Generic Command */
80 #define SSIP_CMD(cmd, payload)	(((cmd) << 28) | ((payload) & 0xfffffff))
81 /* Commands for the control channel */
82 #define SSIP_BOOTINFO_REQ_CMD(ver) \
83 		SSIP_CMD(SSIP_BOOTINFO_REQ, SSIP_DATA_VERSION(ver))
84 #define SSIP_BOOTINFO_RESP_CMD(ver) \
85 		SSIP_CMD(SSIP_BOOTINFO_RESP, SSIP_DATA_VERSION(ver))
86 #define SSIP_START_TRANS_CMD(pdulen, id) \
87 		SSIP_CMD(SSIP_START_TRANS, (((pdulen) << 8) | SSIP_MSG_ID(id)))
88 #define SSIP_READY_CMD		SSIP_CMD(SSIP_READY, 0)
89 #define SSIP_SWBREAK_CMD	SSIP_CMD(SSIP_SW_BREAK, 0)
90 
91 #define SSIP_WAKETEST_FLAG 0
92 
93 /* Main state machine states */
94 enum {
95 	INIT,
96 	HANDSHAKE,
97 	ACTIVE,
98 };
99 
100 /* Send state machine states */
101 enum {
102 	SEND_IDLE,
103 	WAIT4READY,
104 	SEND_READY,
105 	SENDING,
106 	SENDING_SWBREAK,
107 };
108 
109 /* Receive state machine states */
110 enum {
111 	RECV_IDLE,
112 	RECV_READY,
113 	RECEIVING,
114 };
115 
116 /**
117  * struct ssi_protocol - SSI protocol (McSAAB) data
118  * @main_state: Main state machine
119  * @send_state: TX state machine
120  * @recv_state: RX state machine
121  * @flags: Flags, currently only used to follow wake line test
122  * @rxid: RX data id
123  * @txid: TX data id
124  * @txqueue_len: TX queue length
125  * @tx_wd: TX watchdog
126  * @rx_wd: RX watchdog
127  * @keep_alive: Workaround for SSI HW bug
128  * @lock: To serialize access to this struct
129  * @netdev: Phonet network device
130  * @txqueue: TX data queue
131  * @cmdqueue: Queue of free commands
132  * @cl: HSI client own reference
133  * @link: Link for ssip_list
134  * @tx_usecount: Refcount to keep track the slaves that use the wake line
135  * @channel_id_cmd: HSI channel id for command stream
136  * @channel_id_data: HSI channel id for data stream
137  */
138 struct ssi_protocol {
139 	unsigned int		main_state;
140 	unsigned int		send_state;
141 	unsigned int		recv_state;
142 	unsigned long		flags;
143 	u8			rxid;
144 	u8			txid;
145 	unsigned int		txqueue_len;
146 	struct timer_list	tx_wd;
147 	struct timer_list	rx_wd;
148 	struct timer_list	keep_alive; /* wake-up workaround */
149 	spinlock_t		lock;
150 	struct net_device	*netdev;
151 	struct list_head	txqueue;
152 	struct list_head	cmdqueue;
153 	struct work_struct	work;
154 	struct hsi_client	*cl;
155 	struct list_head	link;
156 	atomic_t		tx_usecnt;
157 	int			channel_id_cmd;
158 	int			channel_id_data;
159 };
160 
161 /* List of ssi protocol instances */
162 static LIST_HEAD(ssip_list);
163 
164 static void ssip_rxcmd_complete(struct hsi_msg *msg);
165 
166 static inline void ssip_set_cmd(struct hsi_msg *msg, u32 cmd)
167 {
168 	u32 *data;
169 
170 	data = sg_virt(msg->sgt.sgl);
171 	*data = cmd;
172 }
173 
174 static inline u32 ssip_get_cmd(struct hsi_msg *msg)
175 {
176 	u32 *data;
177 
178 	data = sg_virt(msg->sgt.sgl);
179 
180 	return *data;
181 }
182 
183 static void ssip_skb_to_msg(struct sk_buff *skb, struct hsi_msg *msg)
184 {
185 	skb_frag_t *frag;
186 	struct scatterlist *sg;
187 	int i;
188 
189 	BUG_ON(msg->sgt.nents != (unsigned int)(skb_shinfo(skb)->nr_frags + 1));
190 
191 	sg = msg->sgt.sgl;
192 	sg_set_buf(sg, skb->data, skb_headlen(skb));
193 	for (i = 0; i < skb_shinfo(skb)->nr_frags; i++) {
194 		sg = sg_next(sg);
195 		BUG_ON(!sg);
196 		frag = &skb_shinfo(skb)->frags[i];
197 		sg_set_page(sg, frag->page.p, frag->size, frag->page_offset);
198 	}
199 }
200 
201 static void ssip_free_data(struct hsi_msg *msg)
202 {
203 	struct sk_buff *skb;
204 
205 	skb = msg->context;
206 	pr_debug("free data: msg %p context %p skb %p\n", msg, msg->context,
207 								skb);
208 	msg->destructor = NULL;
209 	dev_kfree_skb(skb);
210 	hsi_free_msg(msg);
211 }
212 
213 static struct hsi_msg *ssip_alloc_data(struct ssi_protocol *ssi,
214 					struct sk_buff *skb, gfp_t flags)
215 {
216 	struct hsi_msg *msg;
217 
218 	msg = hsi_alloc_msg(skb_shinfo(skb)->nr_frags + 1, flags);
219 	if (!msg)
220 		return NULL;
221 	ssip_skb_to_msg(skb, msg);
222 	msg->destructor = ssip_free_data;
223 	msg->channel = ssi->channel_id_data;
224 	msg->context = skb;
225 
226 	return msg;
227 }
228 
229 static inline void ssip_release_cmd(struct hsi_msg *msg)
230 {
231 	struct ssi_protocol *ssi = hsi_client_drvdata(msg->cl);
232 
233 	dev_dbg(&msg->cl->device, "Release cmd 0x%08x\n", ssip_get_cmd(msg));
234 	spin_lock_bh(&ssi->lock);
235 	list_add_tail(&msg->link, &ssi->cmdqueue);
236 	spin_unlock_bh(&ssi->lock);
237 }
238 
239 static struct hsi_msg *ssip_claim_cmd(struct ssi_protocol *ssi)
240 {
241 	struct hsi_msg *msg;
242 
243 	BUG_ON(list_empty(&ssi->cmdqueue));
244 
245 	spin_lock_bh(&ssi->lock);
246 	msg = list_first_entry(&ssi->cmdqueue, struct hsi_msg, link);
247 	list_del(&msg->link);
248 	spin_unlock_bh(&ssi->lock);
249 	msg->destructor = ssip_release_cmd;
250 
251 	return msg;
252 }
253 
254 static void ssip_free_cmds(struct ssi_protocol *ssi)
255 {
256 	struct hsi_msg *msg, *tmp;
257 
258 	list_for_each_entry_safe(msg, tmp, &ssi->cmdqueue, link) {
259 		list_del(&msg->link);
260 		msg->destructor = NULL;
261 		kfree(sg_virt(msg->sgt.sgl));
262 		hsi_free_msg(msg);
263 	}
264 }
265 
266 static int ssip_alloc_cmds(struct ssi_protocol *ssi)
267 {
268 	struct hsi_msg *msg;
269 	u32 *buf;
270 	unsigned int i;
271 
272 	for (i = 0; i < SSIP_MAX_CMDS; i++) {
273 		msg = hsi_alloc_msg(1, GFP_KERNEL);
274 		if (!msg)
275 			goto out;
276 		buf = kmalloc(sizeof(*buf), GFP_KERNEL);
277 		if (!buf) {
278 			hsi_free_msg(msg);
279 			goto out;
280 		}
281 		sg_init_one(msg->sgt.sgl, buf, sizeof(*buf));
282 		msg->channel = ssi->channel_id_cmd;
283 		list_add_tail(&msg->link, &ssi->cmdqueue);
284 	}
285 
286 	return 0;
287 out:
288 	ssip_free_cmds(ssi);
289 
290 	return -ENOMEM;
291 }
292 
293 static void ssip_set_rxstate(struct ssi_protocol *ssi, unsigned int state)
294 {
295 	ssi->recv_state = state;
296 	switch (state) {
297 	case RECV_IDLE:
298 		del_timer(&ssi->rx_wd);
299 		if (ssi->send_state == SEND_IDLE)
300 			del_timer(&ssi->keep_alive);
301 		break;
302 	case RECV_READY:
303 		/* CMT speech workaround */
304 		if (atomic_read(&ssi->tx_usecnt))
305 			break;
306 		/* Otherwise fall through */
307 	case RECEIVING:
308 		mod_timer(&ssi->keep_alive, jiffies +
309 						msecs_to_jiffies(SSIP_KATOUT));
310 		mod_timer(&ssi->rx_wd, jiffies + msecs_to_jiffies(SSIP_WDTOUT));
311 		break;
312 	default:
313 		break;
314 	}
315 }
316 
317 static void ssip_set_txstate(struct ssi_protocol *ssi, unsigned int state)
318 {
319 	ssi->send_state = state;
320 	switch (state) {
321 	case SEND_IDLE:
322 	case SEND_READY:
323 		del_timer(&ssi->tx_wd);
324 		if (ssi->recv_state == RECV_IDLE)
325 			del_timer(&ssi->keep_alive);
326 		break;
327 	case WAIT4READY:
328 	case SENDING:
329 	case SENDING_SWBREAK:
330 		mod_timer(&ssi->keep_alive,
331 				jiffies + msecs_to_jiffies(SSIP_KATOUT));
332 		mod_timer(&ssi->tx_wd, jiffies + msecs_to_jiffies(SSIP_WDTOUT));
333 		break;
334 	default:
335 		break;
336 	}
337 }
338 
339 struct hsi_client *ssip_slave_get_master(struct hsi_client *slave)
340 {
341 	struct hsi_client *master = ERR_PTR(-ENODEV);
342 	struct ssi_protocol *ssi;
343 
344 	list_for_each_entry(ssi, &ssip_list, link)
345 		if (slave->device.parent == ssi->cl->device.parent) {
346 			master = ssi->cl;
347 			break;
348 		}
349 
350 	return master;
351 }
352 EXPORT_SYMBOL_GPL(ssip_slave_get_master);
353 
354 int ssip_slave_start_tx(struct hsi_client *master)
355 {
356 	struct ssi_protocol *ssi = hsi_client_drvdata(master);
357 
358 	dev_dbg(&master->device, "start TX %d\n", atomic_read(&ssi->tx_usecnt));
359 	spin_lock_bh(&ssi->lock);
360 	if (ssi->send_state == SEND_IDLE) {
361 		ssip_set_txstate(ssi, WAIT4READY);
362 		hsi_start_tx(master);
363 	}
364 	spin_unlock_bh(&ssi->lock);
365 	atomic_inc(&ssi->tx_usecnt);
366 
367 	return 0;
368 }
369 EXPORT_SYMBOL_GPL(ssip_slave_start_tx);
370 
371 int ssip_slave_stop_tx(struct hsi_client *master)
372 {
373 	struct ssi_protocol *ssi = hsi_client_drvdata(master);
374 
375 	WARN_ON_ONCE(atomic_read(&ssi->tx_usecnt) == 0);
376 
377 	if (atomic_dec_and_test(&ssi->tx_usecnt)) {
378 		spin_lock_bh(&ssi->lock);
379 		if ((ssi->send_state == SEND_READY) ||
380 			(ssi->send_state == WAIT4READY)) {
381 			ssip_set_txstate(ssi, SEND_IDLE);
382 			hsi_stop_tx(master);
383 		}
384 		spin_unlock_bh(&ssi->lock);
385 	}
386 	dev_dbg(&master->device, "stop TX %d\n", atomic_read(&ssi->tx_usecnt));
387 
388 	return 0;
389 }
390 EXPORT_SYMBOL_GPL(ssip_slave_stop_tx);
391 
392 int ssip_slave_running(struct hsi_client *master)
393 {
394 	struct ssi_protocol *ssi = hsi_client_drvdata(master);
395 	return netif_running(ssi->netdev);
396 }
397 EXPORT_SYMBOL_GPL(ssip_slave_running);
398 
399 static void ssip_reset(struct hsi_client *cl)
400 {
401 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
402 	struct list_head *head, *tmp;
403 	struct hsi_msg *msg;
404 
405 	if (netif_running(ssi->netdev))
406 		netif_carrier_off(ssi->netdev);
407 	hsi_flush(cl);
408 	spin_lock_bh(&ssi->lock);
409 	if (ssi->send_state != SEND_IDLE)
410 		hsi_stop_tx(cl);
411 	spin_unlock_bh(&ssi->lock);
412 	if (test_and_clear_bit(SSIP_WAKETEST_FLAG, &ssi->flags))
413 		ssi_waketest(cl, 0); /* FIXME: To be removed */
414 	spin_lock_bh(&ssi->lock);
415 	del_timer(&ssi->rx_wd);
416 	del_timer(&ssi->tx_wd);
417 	del_timer(&ssi->keep_alive);
418 	ssi->main_state = 0;
419 	ssi->send_state = 0;
420 	ssi->recv_state = 0;
421 	ssi->flags = 0;
422 	ssi->rxid = 0;
423 	ssi->txid = 0;
424 	list_for_each_safe(head, tmp, &ssi->txqueue) {
425 		msg = list_entry(head, struct hsi_msg, link);
426 		dev_dbg(&cl->device, "Pending TX data\n");
427 		list_del(head);
428 		ssip_free_data(msg);
429 	}
430 	ssi->txqueue_len = 0;
431 	spin_unlock_bh(&ssi->lock);
432 }
433 
434 static void ssip_dump_state(struct hsi_client *cl)
435 {
436 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
437 	struct hsi_msg *msg;
438 
439 	spin_lock_bh(&ssi->lock);
440 	dev_err(&cl->device, "Main state: %d\n", ssi->main_state);
441 	dev_err(&cl->device, "Recv state: %d\n", ssi->recv_state);
442 	dev_err(&cl->device, "Send state: %d\n", ssi->send_state);
443 	dev_err(&cl->device, "CMT %s\n", (ssi->main_state == ACTIVE) ?
444 							"Online" : "Offline");
445 	dev_err(&cl->device, "Wake test %d\n",
446 				test_bit(SSIP_WAKETEST_FLAG, &ssi->flags));
447 	dev_err(&cl->device, "Data RX id: %d\n", ssi->rxid);
448 	dev_err(&cl->device, "Data TX id: %d\n", ssi->txid);
449 
450 	list_for_each_entry(msg, &ssi->txqueue, link)
451 		dev_err(&cl->device, "pending TX data (%p)\n", msg);
452 	spin_unlock_bh(&ssi->lock);
453 }
454 
455 static void ssip_error(struct hsi_client *cl)
456 {
457 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
458 	struct hsi_msg *msg;
459 
460 	ssip_dump_state(cl);
461 	ssip_reset(cl);
462 	msg = ssip_claim_cmd(ssi);
463 	msg->complete = ssip_rxcmd_complete;
464 	hsi_async_read(cl, msg);
465 }
466 
467 static void ssip_keep_alive(unsigned long data)
468 {
469 	struct hsi_client *cl = (struct hsi_client *)data;
470 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
471 
472 	dev_dbg(&cl->device, "Keep alive kick in: m(%d) r(%d) s(%d)\n",
473 		ssi->main_state, ssi->recv_state, ssi->send_state);
474 
475 	spin_lock(&ssi->lock);
476 	if (ssi->recv_state == RECV_IDLE)
477 		switch (ssi->send_state) {
478 		case SEND_READY:
479 			if (atomic_read(&ssi->tx_usecnt) == 0)
480 				break;
481 			/*
482 			 * Fall through. Workaround for cmt-speech
483 			 * in that case we relay on audio timers.
484 			 */
485 		case SEND_IDLE:
486 			spin_unlock(&ssi->lock);
487 			return;
488 		}
489 	mod_timer(&ssi->keep_alive, jiffies + msecs_to_jiffies(SSIP_KATOUT));
490 	spin_unlock(&ssi->lock);
491 }
492 
493 static void ssip_wd(unsigned long data)
494 {
495 	struct hsi_client *cl = (struct hsi_client *)data;
496 
497 	dev_err(&cl->device, "Watchdog trigerred\n");
498 	ssip_error(cl);
499 }
500 
501 static void ssip_send_bootinfo_req_cmd(struct hsi_client *cl)
502 {
503 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
504 	struct hsi_msg *msg;
505 
506 	dev_dbg(&cl->device, "Issuing BOOT INFO REQ command\n");
507 	msg = ssip_claim_cmd(ssi);
508 	ssip_set_cmd(msg, SSIP_BOOTINFO_REQ_CMD(SSIP_LOCAL_VERID));
509 	msg->complete = ssip_release_cmd;
510 	hsi_async_write(cl, msg);
511 	dev_dbg(&cl->device, "Issuing RX command\n");
512 	msg = ssip_claim_cmd(ssi);
513 	msg->complete = ssip_rxcmd_complete;
514 	hsi_async_read(cl, msg);
515 }
516 
517 static void ssip_start_rx(struct hsi_client *cl)
518 {
519 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
520 	struct hsi_msg *msg;
521 
522 	dev_dbg(&cl->device, "RX start M(%d) R(%d)\n", ssi->main_state,
523 						ssi->recv_state);
524 	spin_lock_bh(&ssi->lock);
525 	/*
526 	 * We can have two UP events in a row due to a short low
527 	 * high transition. Therefore we need to ignore the sencond UP event.
528 	 */
529 	if ((ssi->main_state != ACTIVE) || (ssi->recv_state == RECV_READY)) {
530 		spin_unlock_bh(&ssi->lock);
531 		return;
532 	}
533 	ssip_set_rxstate(ssi, RECV_READY);
534 	spin_unlock_bh(&ssi->lock);
535 
536 	msg = ssip_claim_cmd(ssi);
537 	ssip_set_cmd(msg, SSIP_READY_CMD);
538 	msg->complete = ssip_release_cmd;
539 	dev_dbg(&cl->device, "Send READY\n");
540 	hsi_async_write(cl, msg);
541 }
542 
543 static void ssip_stop_rx(struct hsi_client *cl)
544 {
545 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
546 
547 	dev_dbg(&cl->device, "RX stop M(%d)\n", ssi->main_state);
548 	spin_lock_bh(&ssi->lock);
549 	if (likely(ssi->main_state == ACTIVE))
550 		ssip_set_rxstate(ssi, RECV_IDLE);
551 	spin_unlock_bh(&ssi->lock);
552 }
553 
554 static void ssip_free_strans(struct hsi_msg *msg)
555 {
556 	ssip_free_data(msg->context);
557 	ssip_release_cmd(msg);
558 }
559 
560 static void ssip_strans_complete(struct hsi_msg *msg)
561 {
562 	struct hsi_client *cl = msg->cl;
563 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
564 	struct hsi_msg *data;
565 
566 	data = msg->context;
567 	ssip_release_cmd(msg);
568 	spin_lock_bh(&ssi->lock);
569 	ssip_set_txstate(ssi, SENDING);
570 	spin_unlock_bh(&ssi->lock);
571 	hsi_async_write(cl, data);
572 }
573 
574 static int ssip_xmit(struct hsi_client *cl)
575 {
576 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
577 	struct hsi_msg *msg, *dmsg;
578 	struct sk_buff *skb;
579 
580 	spin_lock_bh(&ssi->lock);
581 	if (list_empty(&ssi->txqueue)) {
582 		spin_unlock_bh(&ssi->lock);
583 		return 0;
584 	}
585 	dmsg = list_first_entry(&ssi->txqueue, struct hsi_msg, link);
586 	list_del(&dmsg->link);
587 	ssi->txqueue_len--;
588 	spin_unlock_bh(&ssi->lock);
589 
590 	msg = ssip_claim_cmd(ssi);
591 	skb = dmsg->context;
592 	msg->context = dmsg;
593 	msg->complete = ssip_strans_complete;
594 	msg->destructor = ssip_free_strans;
595 
596 	spin_lock_bh(&ssi->lock);
597 	ssip_set_cmd(msg, SSIP_START_TRANS_CMD(SSIP_BYTES_TO_FRAMES(skb->len),
598 								ssi->txid));
599 	ssi->txid++;
600 	ssip_set_txstate(ssi, SENDING);
601 	spin_unlock_bh(&ssi->lock);
602 
603 	dev_dbg(&cl->device, "Send STRANS (%d frames)\n",
604 						SSIP_BYTES_TO_FRAMES(skb->len));
605 
606 	return hsi_async_write(cl, msg);
607 }
608 
609 /* In soft IRQ context */
610 static void ssip_pn_rx(struct sk_buff *skb)
611 {
612 	struct net_device *dev = skb->dev;
613 
614 	if (unlikely(!netif_running(dev))) {
615 		dev_dbg(&dev->dev, "Drop RX packet\n");
616 		dev->stats.rx_dropped++;
617 		dev_kfree_skb(skb);
618 		return;
619 	}
620 	if (unlikely(!pskb_may_pull(skb, SSIP_MIN_PN_HDR))) {
621 		dev_dbg(&dev->dev, "Error drop RX packet\n");
622 		dev->stats.rx_errors++;
623 		dev->stats.rx_length_errors++;
624 		dev_kfree_skb(skb);
625 		return;
626 	}
627 	dev->stats.rx_packets++;
628 	dev->stats.rx_bytes += skb->len;
629 
630 	/* length field is exchanged in network byte order */
631 	((u16 *)skb->data)[2] = ntohs(((u16 *)skb->data)[2]);
632 	dev_dbg(&dev->dev, "RX length fixed (%04x -> %u)\n",
633 			((u16 *)skb->data)[2], ntohs(((u16 *)skb->data)[2]));
634 
635 	skb->protocol = htons(ETH_P_PHONET);
636 	skb_reset_mac_header(skb);
637 	__skb_pull(skb, 1);
638 	netif_rx(skb);
639 }
640 
641 static void ssip_rx_data_complete(struct hsi_msg *msg)
642 {
643 	struct hsi_client *cl = msg->cl;
644 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
645 	struct sk_buff *skb;
646 
647 	if (msg->status == HSI_STATUS_ERROR) {
648 		dev_err(&cl->device, "RX data error\n");
649 		ssip_free_data(msg);
650 		ssip_error(cl);
651 		return;
652 	}
653 	del_timer(&ssi->rx_wd); /* FIXME: Revisit */
654 	skb = msg->context;
655 	ssip_pn_rx(skb);
656 	hsi_free_msg(msg);
657 }
658 
659 static void ssip_rx_bootinforeq(struct hsi_client *cl, u32 cmd)
660 {
661 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
662 	struct hsi_msg *msg;
663 
664 	/* Workaroud: Ignore CMT Loader message leftover */
665 	if (cmd == SSIP_CMT_LOADER_SYNC)
666 		return;
667 
668 	switch (ssi->main_state) {
669 	case ACTIVE:
670 		dev_err(&cl->device, "Boot info req on active state\n");
671 		ssip_error(cl);
672 		/* Fall through */
673 	case INIT:
674 	case HANDSHAKE:
675 		spin_lock_bh(&ssi->lock);
676 		ssi->main_state = HANDSHAKE;
677 		spin_unlock_bh(&ssi->lock);
678 
679 		if (!test_and_set_bit(SSIP_WAKETEST_FLAG, &ssi->flags))
680 			ssi_waketest(cl, 1); /* FIXME: To be removed */
681 
682 		spin_lock_bh(&ssi->lock);
683 		/* Start boot handshake watchdog */
684 		mod_timer(&ssi->tx_wd, jiffies + msecs_to_jiffies(SSIP_WDTOUT));
685 		spin_unlock_bh(&ssi->lock);
686 		dev_dbg(&cl->device, "Send BOOTINFO_RESP\n");
687 		if (SSIP_DATA_VERSION(cmd) != SSIP_LOCAL_VERID)
688 			dev_warn(&cl->device, "boot info req verid mismatch\n");
689 		msg = ssip_claim_cmd(ssi);
690 		ssip_set_cmd(msg, SSIP_BOOTINFO_RESP_CMD(SSIP_LOCAL_VERID));
691 		msg->complete = ssip_release_cmd;
692 		hsi_async_write(cl, msg);
693 		break;
694 	default:
695 		dev_dbg(&cl->device, "Wrong state M(%d)\n", ssi->main_state);
696 		break;
697 	}
698 }
699 
700 static void ssip_rx_bootinforesp(struct hsi_client *cl, u32 cmd)
701 {
702 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
703 
704 	if (SSIP_DATA_VERSION(cmd) != SSIP_LOCAL_VERID)
705 		dev_warn(&cl->device, "boot info resp verid mismatch\n");
706 
707 	spin_lock_bh(&ssi->lock);
708 	if (ssi->main_state != ACTIVE)
709 		/* Use tx_wd as a boot watchdog in non ACTIVE state */
710 		mod_timer(&ssi->tx_wd, jiffies + msecs_to_jiffies(SSIP_WDTOUT));
711 	else
712 		dev_dbg(&cl->device, "boot info resp ignored M(%d)\n",
713 							ssi->main_state);
714 	spin_unlock_bh(&ssi->lock);
715 }
716 
717 static void ssip_rx_waketest(struct hsi_client *cl, u32 cmd)
718 {
719 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
720 	unsigned int wkres = SSIP_PAYLOAD(cmd);
721 
722 	spin_lock_bh(&ssi->lock);
723 	if (ssi->main_state != HANDSHAKE) {
724 		dev_dbg(&cl->device, "wake lines test ignored M(%d)\n",
725 							ssi->main_state);
726 		spin_unlock_bh(&ssi->lock);
727 		return;
728 	}
729 	spin_unlock_bh(&ssi->lock);
730 
731 	if (test_and_clear_bit(SSIP_WAKETEST_FLAG, &ssi->flags))
732 		ssi_waketest(cl, 0); /* FIXME: To be removed */
733 
734 	spin_lock_bh(&ssi->lock);
735 	ssi->main_state = ACTIVE;
736 	del_timer(&ssi->tx_wd); /* Stop boot handshake timer */
737 	spin_unlock_bh(&ssi->lock);
738 
739 	dev_notice(&cl->device, "WAKELINES TEST %s\n",
740 				wkres & SSIP_WAKETEST_FAILED ? "FAILED" : "OK");
741 	if (wkres & SSIP_WAKETEST_FAILED) {
742 		ssip_error(cl);
743 		return;
744 	}
745 	dev_dbg(&cl->device, "CMT is ONLINE\n");
746 	netif_wake_queue(ssi->netdev);
747 	netif_carrier_on(ssi->netdev);
748 }
749 
750 static void ssip_rx_ready(struct hsi_client *cl)
751 {
752 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
753 
754 	spin_lock_bh(&ssi->lock);
755 	if (unlikely(ssi->main_state != ACTIVE)) {
756 		dev_dbg(&cl->device, "READY on wrong state: S(%d) M(%d)\n",
757 					ssi->send_state, ssi->main_state);
758 		spin_unlock_bh(&ssi->lock);
759 		return;
760 	}
761 	if (ssi->send_state != WAIT4READY) {
762 		dev_dbg(&cl->device, "Ignore spurious READY command\n");
763 		spin_unlock_bh(&ssi->lock);
764 		return;
765 	}
766 	ssip_set_txstate(ssi, SEND_READY);
767 	spin_unlock_bh(&ssi->lock);
768 	ssip_xmit(cl);
769 }
770 
771 static void ssip_rx_strans(struct hsi_client *cl, u32 cmd)
772 {
773 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
774 	struct sk_buff *skb;
775 	struct hsi_msg *msg;
776 	int len = SSIP_PDU_LENGTH(cmd);
777 
778 	dev_dbg(&cl->device, "RX strans: %d frames\n", len);
779 	spin_lock_bh(&ssi->lock);
780 	if (unlikely(ssi->main_state != ACTIVE)) {
781 		dev_err(&cl->device, "START TRANS wrong state: S(%d) M(%d)\n",
782 					ssi->send_state, ssi->main_state);
783 		spin_unlock_bh(&ssi->lock);
784 		return;
785 	}
786 	ssip_set_rxstate(ssi, RECEIVING);
787 	if (unlikely(SSIP_MSG_ID(cmd) != ssi->rxid)) {
788 		dev_err(&cl->device, "START TRANS id %d expected %d\n",
789 					SSIP_MSG_ID(cmd), ssi->rxid);
790 		spin_unlock_bh(&ssi->lock);
791 		goto out1;
792 	}
793 	ssi->rxid++;
794 	spin_unlock_bh(&ssi->lock);
795 	skb = netdev_alloc_skb(ssi->netdev, len * 4);
796 	if (unlikely(!skb)) {
797 		dev_err(&cl->device, "No memory for rx skb\n");
798 		goto out1;
799 	}
800 	skb->dev = ssi->netdev;
801 	skb_put(skb, len * 4);
802 	msg = ssip_alloc_data(ssi, skb, GFP_ATOMIC);
803 	if (unlikely(!msg)) {
804 		dev_err(&cl->device, "No memory for RX data msg\n");
805 		goto out2;
806 	}
807 	msg->complete = ssip_rx_data_complete;
808 	hsi_async_read(cl, msg);
809 
810 	return;
811 out2:
812 	dev_kfree_skb(skb);
813 out1:
814 	ssip_error(cl);
815 }
816 
817 static void ssip_rxcmd_complete(struct hsi_msg *msg)
818 {
819 	struct hsi_client *cl = msg->cl;
820 	u32 cmd = ssip_get_cmd(msg);
821 	unsigned int cmdid = SSIP_COMMAND(cmd);
822 
823 	if (msg->status == HSI_STATUS_ERROR) {
824 		dev_err(&cl->device, "RX error detected\n");
825 		ssip_release_cmd(msg);
826 		ssip_error(cl);
827 		return;
828 	}
829 	hsi_async_read(cl, msg);
830 	dev_dbg(&cl->device, "RX cmd: 0x%08x\n", cmd);
831 	switch (cmdid) {
832 	case SSIP_SW_BREAK:
833 		/* Ignored */
834 		break;
835 	case SSIP_BOOTINFO_REQ:
836 		ssip_rx_bootinforeq(cl, cmd);
837 		break;
838 	case SSIP_BOOTINFO_RESP:
839 		ssip_rx_bootinforesp(cl, cmd);
840 		break;
841 	case SSIP_WAKETEST_RESULT:
842 		ssip_rx_waketest(cl, cmd);
843 		break;
844 	case SSIP_START_TRANS:
845 		ssip_rx_strans(cl, cmd);
846 		break;
847 	case SSIP_READY:
848 		ssip_rx_ready(cl);
849 		break;
850 	default:
851 		dev_warn(&cl->device, "command 0x%08x not supported\n", cmd);
852 		break;
853 	}
854 }
855 
856 static void ssip_swbreak_complete(struct hsi_msg *msg)
857 {
858 	struct hsi_client *cl = msg->cl;
859 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
860 
861 	ssip_release_cmd(msg);
862 	spin_lock_bh(&ssi->lock);
863 	if (list_empty(&ssi->txqueue)) {
864 		if (atomic_read(&ssi->tx_usecnt)) {
865 			ssip_set_txstate(ssi, SEND_READY);
866 		} else {
867 			ssip_set_txstate(ssi, SEND_IDLE);
868 			hsi_stop_tx(cl);
869 		}
870 		spin_unlock_bh(&ssi->lock);
871 	} else {
872 		spin_unlock_bh(&ssi->lock);
873 		ssip_xmit(cl);
874 	}
875 	netif_wake_queue(ssi->netdev);
876 }
877 
878 static void ssip_tx_data_complete(struct hsi_msg *msg)
879 {
880 	struct hsi_client *cl = msg->cl;
881 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
882 	struct hsi_msg *cmsg;
883 
884 	if (msg->status == HSI_STATUS_ERROR) {
885 		dev_err(&cl->device, "TX data error\n");
886 		ssip_error(cl);
887 		goto out;
888 	}
889 	spin_lock_bh(&ssi->lock);
890 	if (list_empty(&ssi->txqueue)) {
891 		ssip_set_txstate(ssi, SENDING_SWBREAK);
892 		spin_unlock_bh(&ssi->lock);
893 		cmsg = ssip_claim_cmd(ssi);
894 		ssip_set_cmd(cmsg, SSIP_SWBREAK_CMD);
895 		cmsg->complete = ssip_swbreak_complete;
896 		dev_dbg(&cl->device, "Send SWBREAK\n");
897 		hsi_async_write(cl, cmsg);
898 	} else {
899 		spin_unlock_bh(&ssi->lock);
900 		ssip_xmit(cl);
901 	}
902 out:
903 	ssip_free_data(msg);
904 }
905 
906 static void ssip_port_event(struct hsi_client *cl, unsigned long event)
907 {
908 	switch (event) {
909 	case HSI_EVENT_START_RX:
910 		ssip_start_rx(cl);
911 		break;
912 	case HSI_EVENT_STOP_RX:
913 		ssip_stop_rx(cl);
914 		break;
915 	default:
916 		return;
917 	}
918 }
919 
920 static int ssip_pn_open(struct net_device *dev)
921 {
922 	struct hsi_client *cl = to_hsi_client(dev->dev.parent);
923 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
924 	int err;
925 
926 	err = hsi_claim_port(cl, 1);
927 	if (err < 0) {
928 		dev_err(&cl->device, "SSI port already claimed\n");
929 		return err;
930 	}
931 	err = hsi_register_port_event(cl, ssip_port_event);
932 	if (err < 0) {
933 		dev_err(&cl->device, "Register HSI port event failed (%d)\n",
934 			err);
935 		return err;
936 	}
937 	dev_dbg(&cl->device, "Configuring SSI port\n");
938 	hsi_setup(cl);
939 
940 	if (!test_and_set_bit(SSIP_WAKETEST_FLAG, &ssi->flags))
941 		ssi_waketest(cl, 1); /* FIXME: To be removed */
942 
943 	spin_lock_bh(&ssi->lock);
944 	ssi->main_state = HANDSHAKE;
945 	spin_unlock_bh(&ssi->lock);
946 
947 	ssip_send_bootinfo_req_cmd(cl);
948 
949 	return 0;
950 }
951 
952 static int ssip_pn_stop(struct net_device *dev)
953 {
954 	struct hsi_client *cl = to_hsi_client(dev->dev.parent);
955 
956 	ssip_reset(cl);
957 	hsi_unregister_port_event(cl);
958 	hsi_release_port(cl);
959 
960 	return 0;
961 }
962 
963 static void ssip_xmit_work(struct work_struct *work)
964 {
965 	struct ssi_protocol *ssi =
966 				container_of(work, struct ssi_protocol, work);
967 	struct hsi_client *cl = ssi->cl;
968 
969 	ssip_xmit(cl);
970 }
971 
972 static int ssip_pn_xmit(struct sk_buff *skb, struct net_device *dev)
973 {
974 	struct hsi_client *cl = to_hsi_client(dev->dev.parent);
975 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
976 	struct hsi_msg *msg;
977 
978 	if ((skb->protocol != htons(ETH_P_PHONET)) ||
979 					(skb->len < SSIP_MIN_PN_HDR))
980 		goto drop;
981 	/* Pad to 32-bits - FIXME: Revisit*/
982 	if ((skb->len & 3) && skb_pad(skb, 4 - (skb->len & 3)))
983 		goto drop;
984 
985 	/*
986 	 * Modem sends Phonet messages over SSI with its own endianess...
987 	 * Assume that modem has the same endianess as we do.
988 	 */
989 	if (skb_cow_head(skb, 0))
990 		goto drop;
991 
992 	/* length field is exchanged in network byte order */
993 	((u16 *)skb->data)[2] = htons(((u16 *)skb->data)[2]);
994 
995 	msg = ssip_alloc_data(ssi, skb, GFP_ATOMIC);
996 	if (!msg) {
997 		dev_dbg(&cl->device, "Dropping tx data: No memory\n");
998 		goto drop;
999 	}
1000 	msg->complete = ssip_tx_data_complete;
1001 
1002 	spin_lock_bh(&ssi->lock);
1003 	if (unlikely(ssi->main_state != ACTIVE)) {
1004 		spin_unlock_bh(&ssi->lock);
1005 		dev_dbg(&cl->device, "Dropping tx data: CMT is OFFLINE\n");
1006 		goto drop2;
1007 	}
1008 	list_add_tail(&msg->link, &ssi->txqueue);
1009 	ssi->txqueue_len++;
1010 	if (dev->tx_queue_len < ssi->txqueue_len) {
1011 		dev_info(&cl->device, "TX queue full %d\n", ssi->txqueue_len);
1012 		netif_stop_queue(dev);
1013 	}
1014 	if (ssi->send_state == SEND_IDLE) {
1015 		ssip_set_txstate(ssi, WAIT4READY);
1016 		spin_unlock_bh(&ssi->lock);
1017 		dev_dbg(&cl->device, "Start TX qlen %d\n", ssi->txqueue_len);
1018 		hsi_start_tx(cl);
1019 	} else if (ssi->send_state == SEND_READY) {
1020 		/* Needed for cmt-speech workaround */
1021 		dev_dbg(&cl->device, "Start TX on SEND READY qlen %d\n",
1022 							ssi->txqueue_len);
1023 		spin_unlock_bh(&ssi->lock);
1024 		schedule_work(&ssi->work);
1025 	} else {
1026 		spin_unlock_bh(&ssi->lock);
1027 	}
1028 	dev->stats.tx_packets++;
1029 	dev->stats.tx_bytes += skb->len;
1030 
1031 	return 0;
1032 drop2:
1033 	hsi_free_msg(msg);
1034 drop:
1035 	dev->stats.tx_dropped++;
1036 	dev_kfree_skb(skb);
1037 
1038 	return 0;
1039 }
1040 
1041 /* CMT reset event handler */
1042 void ssip_reset_event(struct hsi_client *master)
1043 {
1044 	struct ssi_protocol *ssi = hsi_client_drvdata(master);
1045 	dev_err(&ssi->cl->device, "CMT reset detected!\n");
1046 	ssip_error(ssi->cl);
1047 }
1048 EXPORT_SYMBOL_GPL(ssip_reset_event);
1049 
1050 static const struct net_device_ops ssip_pn_ops = {
1051 	.ndo_open	= ssip_pn_open,
1052 	.ndo_stop	= ssip_pn_stop,
1053 	.ndo_start_xmit	= ssip_pn_xmit,
1054 };
1055 
1056 static void ssip_pn_setup(struct net_device *dev)
1057 {
1058 	dev->features		= 0;
1059 	dev->netdev_ops		= &ssip_pn_ops;
1060 	dev->type		= ARPHRD_PHONET;
1061 	dev->flags		= IFF_POINTOPOINT | IFF_NOARP;
1062 	dev->mtu		= SSIP_DEFAULT_MTU;
1063 	dev->hard_header_len	= 1;
1064 	dev->dev_addr[0]	= PN_MEDIA_SOS;
1065 	dev->addr_len		= 1;
1066 	dev->tx_queue_len	= SSIP_TXQUEUE_LEN;
1067 
1068 	dev->destructor		= free_netdev;
1069 	dev->header_ops		= &phonet_header_ops;
1070 }
1071 
1072 static int ssi_protocol_probe(struct device *dev)
1073 {
1074 	static const char ifname[] = "phonet%d";
1075 	struct hsi_client *cl = to_hsi_client(dev);
1076 	struct ssi_protocol *ssi;
1077 	int err;
1078 
1079 	ssi = kzalloc(sizeof(*ssi), GFP_KERNEL);
1080 	if (!ssi) {
1081 		dev_err(dev, "No memory for ssi protocol\n");
1082 		return -ENOMEM;
1083 	}
1084 
1085 	spin_lock_init(&ssi->lock);
1086 	init_timer_deferrable(&ssi->rx_wd);
1087 	init_timer_deferrable(&ssi->tx_wd);
1088 	init_timer(&ssi->keep_alive);
1089 	ssi->rx_wd.data = (unsigned long)cl;
1090 	ssi->rx_wd.function = ssip_wd;
1091 	ssi->tx_wd.data = (unsigned long)cl;
1092 	ssi->tx_wd.function = ssip_wd;
1093 	ssi->keep_alive.data = (unsigned long)cl;
1094 	ssi->keep_alive.function = ssip_keep_alive;
1095 	INIT_LIST_HEAD(&ssi->txqueue);
1096 	INIT_LIST_HEAD(&ssi->cmdqueue);
1097 	atomic_set(&ssi->tx_usecnt, 0);
1098 	hsi_client_set_drvdata(cl, ssi);
1099 	ssi->cl = cl;
1100 	INIT_WORK(&ssi->work, ssip_xmit_work);
1101 
1102 	ssi->channel_id_cmd = hsi_get_channel_id_by_name(cl, "mcsaab-control");
1103 	if (ssi->channel_id_cmd < 0) {
1104 		err = ssi->channel_id_cmd;
1105 		dev_err(dev, "Could not get cmd channel (%d)\n", err);
1106 		goto out;
1107 	}
1108 
1109 	ssi->channel_id_data = hsi_get_channel_id_by_name(cl, "mcsaab-data");
1110 	if (ssi->channel_id_data < 0) {
1111 		err = ssi->channel_id_data;
1112 		dev_err(dev, "Could not get data channel (%d)\n", err);
1113 		goto out;
1114 	}
1115 
1116 	err = ssip_alloc_cmds(ssi);
1117 	if (err < 0) {
1118 		dev_err(dev, "No memory for commands\n");
1119 		goto out;
1120 	}
1121 
1122 	ssi->netdev = alloc_netdev(0, ifname, NET_NAME_UNKNOWN, ssip_pn_setup);
1123 	if (!ssi->netdev) {
1124 		dev_err(dev, "No memory for netdev\n");
1125 		err = -ENOMEM;
1126 		goto out1;
1127 	}
1128 
1129 	/* MTU range: 6 - 65535 */
1130 	ssi->netdev->min_mtu = PHONET_MIN_MTU;
1131 	ssi->netdev->max_mtu = SSIP_MAX_MTU;
1132 
1133 	SET_NETDEV_DEV(ssi->netdev, dev);
1134 	netif_carrier_off(ssi->netdev);
1135 	err = register_netdev(ssi->netdev);
1136 	if (err < 0) {
1137 		dev_err(dev, "Register netdev failed (%d)\n", err);
1138 		goto out2;
1139 	}
1140 
1141 	list_add(&ssi->link, &ssip_list);
1142 
1143 	dev_dbg(dev, "channel configuration: cmd=%d, data=%d\n",
1144 		ssi->channel_id_cmd, ssi->channel_id_data);
1145 
1146 	return 0;
1147 out2:
1148 	free_netdev(ssi->netdev);
1149 out1:
1150 	ssip_free_cmds(ssi);
1151 out:
1152 	kfree(ssi);
1153 
1154 	return err;
1155 }
1156 
1157 static int ssi_protocol_remove(struct device *dev)
1158 {
1159 	struct hsi_client *cl = to_hsi_client(dev);
1160 	struct ssi_protocol *ssi = hsi_client_drvdata(cl);
1161 
1162 	list_del(&ssi->link);
1163 	unregister_netdev(ssi->netdev);
1164 	ssip_free_cmds(ssi);
1165 	hsi_client_set_drvdata(cl, NULL);
1166 	kfree(ssi);
1167 
1168 	return 0;
1169 }
1170 
1171 static struct hsi_client_driver ssip_driver = {
1172 	.driver = {
1173 		.name	= "ssi-protocol",
1174 		.owner	= THIS_MODULE,
1175 		.probe	= ssi_protocol_probe,
1176 		.remove	= ssi_protocol_remove,
1177 	},
1178 };
1179 
1180 static int __init ssip_init(void)
1181 {
1182 	pr_info("SSI protocol aka McSAAB added\n");
1183 
1184 	return hsi_register_client_driver(&ssip_driver);
1185 }
1186 module_init(ssip_init);
1187 
1188 static void __exit ssip_exit(void)
1189 {
1190 	hsi_unregister_client_driver(&ssip_driver);
1191 	pr_info("SSI protocol driver removed\n");
1192 }
1193 module_exit(ssip_exit);
1194 
1195 MODULE_ALIAS("hsi:ssi-protocol");
1196 MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>");
1197 MODULE_AUTHOR("Remi Denis-Courmont <remi.denis-courmont@nokia.com>");
1198 MODULE_DESCRIPTION("SSI protocol improved aka McSAAB");
1199 MODULE_LICENSE("GPL");
1200