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