4 * Implementation of the SSI McSAAB improved protocol.
6 * Copyright (C) 2010 Nokia Corporation. All rights reserved.
7 * Copyright (C) 2013 Sebastian Reichel <sre@kernel.org>
9 * Contact: Carlos Chinea <carlos.chinea@nokia.com>
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.
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.
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
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>
48 void ssi_waketest(struct hsi_client
*cl
, unsigned int enable
);
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
61 * SSI protocol command definitions
63 #define SSIP_COMMAND(data) ((data) >> 28)
64 #define SSIP_PAYLOAD(data) ((data) & 0xfffffff)
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
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)
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)
91 #define SSIP_WAKETEST_FLAG 0
93 /* Main state machine states */
100 /* Send state machine states */
109 /* Receive state machine states */
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
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
138 struct ssi_protocol
{
139 unsigned int main_state
;
140 unsigned int send_state
;
141 unsigned int recv_state
;
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 */
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
;
161 /* List of ssi protocol instances */
162 static LIST_HEAD(ssip_list
);
164 static void ssip_rxcmd_complete(struct hsi_msg
*msg
);
166 static inline void ssip_set_cmd(struct hsi_msg
*msg
, u32 cmd
)
170 data
= sg_virt(msg
->sgt
.sgl
);
174 static inline u32
ssip_get_cmd(struct hsi_msg
*msg
)
178 data
= sg_virt(msg
->sgt
.sgl
);
183 static void ssip_skb_to_msg(struct sk_buff
*skb
, struct hsi_msg
*msg
)
186 struct scatterlist
*sg
;
189 BUG_ON(msg
->sgt
.nents
!= (unsigned int)(skb_shinfo(skb
)->nr_frags
+ 1));
192 sg_set_buf(sg
, skb
->data
, skb_headlen(skb
));
193 for (i
= 0; i
< skb_shinfo(skb
)->nr_frags
; i
++) {
196 frag
= &skb_shinfo(skb
)->frags
[i
];
197 sg_set_page(sg
, frag
->page
.p
, frag
->size
, frag
->page_offset
);
201 static void ssip_free_data(struct hsi_msg
*msg
)
206 pr_debug("free data: msg %p context %p skb %p\n", msg
, msg
->context
,
208 msg
->destructor
= NULL
;
213 static struct hsi_msg
*ssip_alloc_data(struct ssi_protocol
*ssi
,
214 struct sk_buff
*skb
, gfp_t flags
)
218 msg
= hsi_alloc_msg(skb_shinfo(skb
)->nr_frags
+ 1, flags
);
221 ssip_skb_to_msg(skb
, msg
);
222 msg
->destructor
= ssip_free_data
;
223 msg
->channel
= ssi
->channel_id_data
;
229 static inline void ssip_release_cmd(struct hsi_msg
*msg
)
231 struct ssi_protocol
*ssi
= hsi_client_drvdata(msg
->cl
);
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
);
239 static struct hsi_msg
*ssip_claim_cmd(struct ssi_protocol
*ssi
)
243 BUG_ON(list_empty(&ssi
->cmdqueue
));
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
;
254 static void ssip_free_cmds(struct ssi_protocol
*ssi
)
256 struct hsi_msg
*msg
, *tmp
;
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
));
266 static int ssip_alloc_cmds(struct ssi_protocol
*ssi
)
272 for (i
= 0; i
< SSIP_MAX_CMDS
; i
++) {
273 msg
= hsi_alloc_msg(1, GFP_KERNEL
);
276 buf
= kmalloc(sizeof(*buf
), GFP_KERNEL
);
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
);
293 static void ssip_set_rxstate(struct ssi_protocol
*ssi
, unsigned int state
)
295 ssi
->recv_state
= state
;
298 del_timer(&ssi
->rx_wd
);
299 if (ssi
->send_state
== SEND_IDLE
)
300 del_timer(&ssi
->keep_alive
);
303 /* CMT speech workaround */
304 if (atomic_read(&ssi
->tx_usecnt
))
306 /* Otherwise fall through */
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
));
317 static void ssip_set_txstate(struct ssi_protocol
*ssi
, unsigned int state
)
319 ssi
->send_state
= state
;
323 del_timer(&ssi
->tx_wd
);
324 if (ssi
->recv_state
== RECV_IDLE
)
325 del_timer(&ssi
->keep_alive
);
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
));
339 struct hsi_client
*ssip_slave_get_master(struct hsi_client
*slave
)
341 struct hsi_client
*master
= ERR_PTR(-ENODEV
);
342 struct ssi_protocol
*ssi
;
344 list_for_each_entry(ssi
, &ssip_list
, link
)
345 if (slave
->device
.parent
== ssi
->cl
->device
.parent
) {
352 EXPORT_SYMBOL_GPL(ssip_slave_get_master
);
354 int ssip_slave_start_tx(struct hsi_client
*master
)
356 struct ssi_protocol
*ssi
= hsi_client_drvdata(master
);
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
);
364 spin_unlock_bh(&ssi
->lock
);
365 atomic_inc(&ssi
->tx_usecnt
);
369 EXPORT_SYMBOL_GPL(ssip_slave_start_tx
);
371 int ssip_slave_stop_tx(struct hsi_client
*master
)
373 struct ssi_protocol
*ssi
= hsi_client_drvdata(master
);
375 WARN_ON_ONCE(atomic_read(&ssi
->tx_usecnt
) == 0);
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
);
384 spin_unlock_bh(&ssi
->lock
);
386 dev_dbg(&master
->device
, "stop TX %d\n", atomic_read(&ssi
->tx_usecnt
));
390 EXPORT_SYMBOL_GPL(ssip_slave_stop_tx
);
392 int ssip_slave_running(struct hsi_client
*master
)
394 struct ssi_protocol
*ssi
= hsi_client_drvdata(master
);
395 return netif_running(ssi
->netdev
);
397 EXPORT_SYMBOL_GPL(ssip_slave_running
);
399 static void ssip_reset(struct hsi_client
*cl
)
401 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
402 struct list_head
*head
, *tmp
;
405 if (netif_running(ssi
->netdev
))
406 netif_carrier_off(ssi
->netdev
);
408 spin_lock_bh(&ssi
->lock
);
409 if (ssi
->send_state
!= SEND_IDLE
)
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
);
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");
430 ssi
->txqueue_len
= 0;
431 spin_unlock_bh(&ssi
->lock
);
434 static void ssip_dump_state(struct hsi_client
*cl
)
436 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
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
);
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
);
455 static void ssip_error(struct hsi_client
*cl
)
457 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
462 msg
= ssip_claim_cmd(ssi
);
463 msg
->complete
= ssip_rxcmd_complete
;
464 hsi_async_read(cl
, msg
);
467 static void ssip_keep_alive(struct timer_list
*t
)
469 struct ssi_protocol
*ssi
= from_timer(ssi
, t
, keep_alive
);
470 struct hsi_client
*cl
= ssi
->cl
;
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
);
475 spin_lock(&ssi
->lock
);
476 if (ssi
->recv_state
== RECV_IDLE
)
477 switch (ssi
->send_state
) {
479 if (atomic_read(&ssi
->tx_usecnt
) == 0)
482 * Fall through. Workaround for cmt-speech
483 * in that case we relay on audio timers.
486 spin_unlock(&ssi
->lock
);
489 mod_timer(&ssi
->keep_alive
, jiffies
+ msecs_to_jiffies(SSIP_KATOUT
));
490 spin_unlock(&ssi
->lock
);
493 static void ssip_rx_wd(struct timer_list
*t
)
495 struct ssi_protocol
*ssi
= from_timer(ssi
, t
, rx_wd
);
496 struct hsi_client
*cl
= ssi
->cl
;
498 dev_err(&cl
->device
, "Watchdog trigerred\n");
502 static void ssip_tx_wd(struct timer_list
*t
)
504 struct ssi_protocol
*ssi
= from_timer(ssi
, t
, tx_wd
);
505 struct hsi_client
*cl
= ssi
->cl
;
507 dev_err(&cl
->device
, "Watchdog trigerred\n");
511 static void ssip_send_bootinfo_req_cmd(struct hsi_client
*cl
)
513 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
516 dev_dbg(&cl
->device
, "Issuing BOOT INFO REQ command\n");
517 msg
= ssip_claim_cmd(ssi
);
518 ssip_set_cmd(msg
, SSIP_BOOTINFO_REQ_CMD(SSIP_LOCAL_VERID
));
519 msg
->complete
= ssip_release_cmd
;
520 hsi_async_write(cl
, msg
);
521 dev_dbg(&cl
->device
, "Issuing RX command\n");
522 msg
= ssip_claim_cmd(ssi
);
523 msg
->complete
= ssip_rxcmd_complete
;
524 hsi_async_read(cl
, msg
);
527 static void ssip_start_rx(struct hsi_client
*cl
)
529 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
532 dev_dbg(&cl
->device
, "RX start M(%d) R(%d)\n", ssi
->main_state
,
534 spin_lock_bh(&ssi
->lock
);
536 * We can have two UP events in a row due to a short low
537 * high transition. Therefore we need to ignore the sencond UP event.
539 if ((ssi
->main_state
!= ACTIVE
) || (ssi
->recv_state
== RECV_READY
)) {
540 spin_unlock_bh(&ssi
->lock
);
543 ssip_set_rxstate(ssi
, RECV_READY
);
544 spin_unlock_bh(&ssi
->lock
);
546 msg
= ssip_claim_cmd(ssi
);
547 ssip_set_cmd(msg
, SSIP_READY_CMD
);
548 msg
->complete
= ssip_release_cmd
;
549 dev_dbg(&cl
->device
, "Send READY\n");
550 hsi_async_write(cl
, msg
);
553 static void ssip_stop_rx(struct hsi_client
*cl
)
555 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
557 dev_dbg(&cl
->device
, "RX stop M(%d)\n", ssi
->main_state
);
558 spin_lock_bh(&ssi
->lock
);
559 if (likely(ssi
->main_state
== ACTIVE
))
560 ssip_set_rxstate(ssi
, RECV_IDLE
);
561 spin_unlock_bh(&ssi
->lock
);
564 static void ssip_free_strans(struct hsi_msg
*msg
)
566 ssip_free_data(msg
->context
);
567 ssip_release_cmd(msg
);
570 static void ssip_strans_complete(struct hsi_msg
*msg
)
572 struct hsi_client
*cl
= msg
->cl
;
573 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
574 struct hsi_msg
*data
;
577 ssip_release_cmd(msg
);
578 spin_lock_bh(&ssi
->lock
);
579 ssip_set_txstate(ssi
, SENDING
);
580 spin_unlock_bh(&ssi
->lock
);
581 hsi_async_write(cl
, data
);
584 static int ssip_xmit(struct hsi_client
*cl
)
586 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
587 struct hsi_msg
*msg
, *dmsg
;
590 spin_lock_bh(&ssi
->lock
);
591 if (list_empty(&ssi
->txqueue
)) {
592 spin_unlock_bh(&ssi
->lock
);
595 dmsg
= list_first_entry(&ssi
->txqueue
, struct hsi_msg
, link
);
596 list_del(&dmsg
->link
);
598 spin_unlock_bh(&ssi
->lock
);
600 msg
= ssip_claim_cmd(ssi
);
603 msg
->complete
= ssip_strans_complete
;
604 msg
->destructor
= ssip_free_strans
;
606 spin_lock_bh(&ssi
->lock
);
607 ssip_set_cmd(msg
, SSIP_START_TRANS_CMD(SSIP_BYTES_TO_FRAMES(skb
->len
),
610 ssip_set_txstate(ssi
, SENDING
);
611 spin_unlock_bh(&ssi
->lock
);
613 dev_dbg(&cl
->device
, "Send STRANS (%d frames)\n",
614 SSIP_BYTES_TO_FRAMES(skb
->len
));
616 return hsi_async_write(cl
, msg
);
619 /* In soft IRQ context */
620 static void ssip_pn_rx(struct sk_buff
*skb
)
622 struct net_device
*dev
= skb
->dev
;
624 if (unlikely(!netif_running(dev
))) {
625 dev_dbg(&dev
->dev
, "Drop RX packet\n");
626 dev
->stats
.rx_dropped
++;
630 if (unlikely(!pskb_may_pull(skb
, SSIP_MIN_PN_HDR
))) {
631 dev_dbg(&dev
->dev
, "Error drop RX packet\n");
632 dev
->stats
.rx_errors
++;
633 dev
->stats
.rx_length_errors
++;
637 dev
->stats
.rx_packets
++;
638 dev
->stats
.rx_bytes
+= skb
->len
;
640 /* length field is exchanged in network byte order */
641 ((u16
*)skb
->data
)[2] = ntohs(((u16
*)skb
->data
)[2]);
642 dev_dbg(&dev
->dev
, "RX length fixed (%04x -> %u)\n",
643 ((u16
*)skb
->data
)[2], ntohs(((u16
*)skb
->data
)[2]));
645 skb
->protocol
= htons(ETH_P_PHONET
);
646 skb_reset_mac_header(skb
);
651 static void ssip_rx_data_complete(struct hsi_msg
*msg
)
653 struct hsi_client
*cl
= msg
->cl
;
654 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
657 if (msg
->status
== HSI_STATUS_ERROR
) {
658 dev_err(&cl
->device
, "RX data error\n");
663 del_timer(&ssi
->rx_wd
); /* FIXME: Revisit */
669 static void ssip_rx_bootinforeq(struct hsi_client
*cl
, u32 cmd
)
671 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
674 /* Workaroud: Ignore CMT Loader message leftover */
675 if (cmd
== SSIP_CMT_LOADER_SYNC
)
678 switch (ssi
->main_state
) {
680 dev_err(&cl
->device
, "Boot info req on active state\n");
685 spin_lock_bh(&ssi
->lock
);
686 ssi
->main_state
= HANDSHAKE
;
687 spin_unlock_bh(&ssi
->lock
);
689 if (!test_and_set_bit(SSIP_WAKETEST_FLAG
, &ssi
->flags
))
690 ssi_waketest(cl
, 1); /* FIXME: To be removed */
692 spin_lock_bh(&ssi
->lock
);
693 /* Start boot handshake watchdog */
694 mod_timer(&ssi
->tx_wd
, jiffies
+ msecs_to_jiffies(SSIP_WDTOUT
));
695 spin_unlock_bh(&ssi
->lock
);
696 dev_dbg(&cl
->device
, "Send BOOTINFO_RESP\n");
697 if (SSIP_DATA_VERSION(cmd
) != SSIP_LOCAL_VERID
)
698 dev_warn(&cl
->device
, "boot info req verid mismatch\n");
699 msg
= ssip_claim_cmd(ssi
);
700 ssip_set_cmd(msg
, SSIP_BOOTINFO_RESP_CMD(SSIP_LOCAL_VERID
));
701 msg
->complete
= ssip_release_cmd
;
702 hsi_async_write(cl
, msg
);
705 dev_dbg(&cl
->device
, "Wrong state M(%d)\n", ssi
->main_state
);
710 static void ssip_rx_bootinforesp(struct hsi_client
*cl
, u32 cmd
)
712 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
714 if (SSIP_DATA_VERSION(cmd
) != SSIP_LOCAL_VERID
)
715 dev_warn(&cl
->device
, "boot info resp verid mismatch\n");
717 spin_lock_bh(&ssi
->lock
);
718 if (ssi
->main_state
!= ACTIVE
)
719 /* Use tx_wd as a boot watchdog in non ACTIVE state */
720 mod_timer(&ssi
->tx_wd
, jiffies
+ msecs_to_jiffies(SSIP_WDTOUT
));
722 dev_dbg(&cl
->device
, "boot info resp ignored M(%d)\n",
724 spin_unlock_bh(&ssi
->lock
);
727 static void ssip_rx_waketest(struct hsi_client
*cl
, u32 cmd
)
729 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
730 unsigned int wkres
= SSIP_PAYLOAD(cmd
);
732 spin_lock_bh(&ssi
->lock
);
733 if (ssi
->main_state
!= HANDSHAKE
) {
734 dev_dbg(&cl
->device
, "wake lines test ignored M(%d)\n",
736 spin_unlock_bh(&ssi
->lock
);
739 spin_unlock_bh(&ssi
->lock
);
741 if (test_and_clear_bit(SSIP_WAKETEST_FLAG
, &ssi
->flags
))
742 ssi_waketest(cl
, 0); /* FIXME: To be removed */
744 spin_lock_bh(&ssi
->lock
);
745 ssi
->main_state
= ACTIVE
;
746 del_timer(&ssi
->tx_wd
); /* Stop boot handshake timer */
747 spin_unlock_bh(&ssi
->lock
);
749 dev_notice(&cl
->device
, "WAKELINES TEST %s\n",
750 wkres
& SSIP_WAKETEST_FAILED
? "FAILED" : "OK");
751 if (wkres
& SSIP_WAKETEST_FAILED
) {
755 dev_dbg(&cl
->device
, "CMT is ONLINE\n");
756 netif_wake_queue(ssi
->netdev
);
757 netif_carrier_on(ssi
->netdev
);
760 static void ssip_rx_ready(struct hsi_client
*cl
)
762 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
764 spin_lock_bh(&ssi
->lock
);
765 if (unlikely(ssi
->main_state
!= ACTIVE
)) {
766 dev_dbg(&cl
->device
, "READY on wrong state: S(%d) M(%d)\n",
767 ssi
->send_state
, ssi
->main_state
);
768 spin_unlock_bh(&ssi
->lock
);
771 if (ssi
->send_state
!= WAIT4READY
) {
772 dev_dbg(&cl
->device
, "Ignore spurious READY command\n");
773 spin_unlock_bh(&ssi
->lock
);
776 ssip_set_txstate(ssi
, SEND_READY
);
777 spin_unlock_bh(&ssi
->lock
);
781 static void ssip_rx_strans(struct hsi_client
*cl
, u32 cmd
)
783 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
786 int len
= SSIP_PDU_LENGTH(cmd
);
788 dev_dbg(&cl
->device
, "RX strans: %d frames\n", len
);
789 spin_lock_bh(&ssi
->lock
);
790 if (unlikely(ssi
->main_state
!= ACTIVE
)) {
791 dev_err(&cl
->device
, "START TRANS wrong state: S(%d) M(%d)\n",
792 ssi
->send_state
, ssi
->main_state
);
793 spin_unlock_bh(&ssi
->lock
);
796 ssip_set_rxstate(ssi
, RECEIVING
);
797 if (unlikely(SSIP_MSG_ID(cmd
) != ssi
->rxid
)) {
798 dev_err(&cl
->device
, "START TRANS id %d expected %d\n",
799 SSIP_MSG_ID(cmd
), ssi
->rxid
);
800 spin_unlock_bh(&ssi
->lock
);
804 spin_unlock_bh(&ssi
->lock
);
805 skb
= netdev_alloc_skb(ssi
->netdev
, len
* 4);
806 if (unlikely(!skb
)) {
807 dev_err(&cl
->device
, "No memory for rx skb\n");
810 skb
->dev
= ssi
->netdev
;
811 skb_put(skb
, len
* 4);
812 msg
= ssip_alloc_data(ssi
, skb
, GFP_ATOMIC
);
813 if (unlikely(!msg
)) {
814 dev_err(&cl
->device
, "No memory for RX data msg\n");
817 msg
->complete
= ssip_rx_data_complete
;
818 hsi_async_read(cl
, msg
);
827 static void ssip_rxcmd_complete(struct hsi_msg
*msg
)
829 struct hsi_client
*cl
= msg
->cl
;
830 u32 cmd
= ssip_get_cmd(msg
);
831 unsigned int cmdid
= SSIP_COMMAND(cmd
);
833 if (msg
->status
== HSI_STATUS_ERROR
) {
834 dev_err(&cl
->device
, "RX error detected\n");
835 ssip_release_cmd(msg
);
839 hsi_async_read(cl
, msg
);
840 dev_dbg(&cl
->device
, "RX cmd: 0x%08x\n", cmd
);
845 case SSIP_BOOTINFO_REQ
:
846 ssip_rx_bootinforeq(cl
, cmd
);
848 case SSIP_BOOTINFO_RESP
:
849 ssip_rx_bootinforesp(cl
, cmd
);
851 case SSIP_WAKETEST_RESULT
:
852 ssip_rx_waketest(cl
, cmd
);
854 case SSIP_START_TRANS
:
855 ssip_rx_strans(cl
, cmd
);
861 dev_warn(&cl
->device
, "command 0x%08x not supported\n", cmd
);
866 static void ssip_swbreak_complete(struct hsi_msg
*msg
)
868 struct hsi_client
*cl
= msg
->cl
;
869 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
871 ssip_release_cmd(msg
);
872 spin_lock_bh(&ssi
->lock
);
873 if (list_empty(&ssi
->txqueue
)) {
874 if (atomic_read(&ssi
->tx_usecnt
)) {
875 ssip_set_txstate(ssi
, SEND_READY
);
877 ssip_set_txstate(ssi
, SEND_IDLE
);
880 spin_unlock_bh(&ssi
->lock
);
882 spin_unlock_bh(&ssi
->lock
);
885 netif_wake_queue(ssi
->netdev
);
888 static void ssip_tx_data_complete(struct hsi_msg
*msg
)
890 struct hsi_client
*cl
= msg
->cl
;
891 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
892 struct hsi_msg
*cmsg
;
894 if (msg
->status
== HSI_STATUS_ERROR
) {
895 dev_err(&cl
->device
, "TX data error\n");
899 spin_lock_bh(&ssi
->lock
);
900 if (list_empty(&ssi
->txqueue
)) {
901 ssip_set_txstate(ssi
, SENDING_SWBREAK
);
902 spin_unlock_bh(&ssi
->lock
);
903 cmsg
= ssip_claim_cmd(ssi
);
904 ssip_set_cmd(cmsg
, SSIP_SWBREAK_CMD
);
905 cmsg
->complete
= ssip_swbreak_complete
;
906 dev_dbg(&cl
->device
, "Send SWBREAK\n");
907 hsi_async_write(cl
, cmsg
);
909 spin_unlock_bh(&ssi
->lock
);
916 static void ssip_port_event(struct hsi_client
*cl
, unsigned long event
)
919 case HSI_EVENT_START_RX
:
922 case HSI_EVENT_STOP_RX
:
930 static int ssip_pn_open(struct net_device
*dev
)
932 struct hsi_client
*cl
= to_hsi_client(dev
->dev
.parent
);
933 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
936 err
= hsi_claim_port(cl
, 1);
938 dev_err(&cl
->device
, "SSI port already claimed\n");
941 err
= hsi_register_port_event(cl
, ssip_port_event
);
943 dev_err(&cl
->device
, "Register HSI port event failed (%d)\n",
947 dev_dbg(&cl
->device
, "Configuring SSI port\n");
950 if (!test_and_set_bit(SSIP_WAKETEST_FLAG
, &ssi
->flags
))
951 ssi_waketest(cl
, 1); /* FIXME: To be removed */
953 spin_lock_bh(&ssi
->lock
);
954 ssi
->main_state
= HANDSHAKE
;
955 spin_unlock_bh(&ssi
->lock
);
957 ssip_send_bootinfo_req_cmd(cl
);
962 static int ssip_pn_stop(struct net_device
*dev
)
964 struct hsi_client
*cl
= to_hsi_client(dev
->dev
.parent
);
967 hsi_unregister_port_event(cl
);
968 hsi_release_port(cl
);
973 static void ssip_xmit_work(struct work_struct
*work
)
975 struct ssi_protocol
*ssi
=
976 container_of(work
, struct ssi_protocol
, work
);
977 struct hsi_client
*cl
= ssi
->cl
;
982 static int ssip_pn_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
984 struct hsi_client
*cl
= to_hsi_client(dev
->dev
.parent
);
985 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
988 if ((skb
->protocol
!= htons(ETH_P_PHONET
)) ||
989 (skb
->len
< SSIP_MIN_PN_HDR
))
991 /* Pad to 32-bits - FIXME: Revisit*/
992 if ((skb
->len
& 3) && skb_pad(skb
, 4 - (skb
->len
& 3)))
996 * Modem sends Phonet messages over SSI with its own endianess...
997 * Assume that modem has the same endianess as we do.
999 if (skb_cow_head(skb
, 0))
1002 /* length field is exchanged in network byte order */
1003 ((u16
*)skb
->data
)[2] = htons(((u16
*)skb
->data
)[2]);
1005 msg
= ssip_alloc_data(ssi
, skb
, GFP_ATOMIC
);
1007 dev_dbg(&cl
->device
, "Dropping tx data: No memory\n");
1010 msg
->complete
= ssip_tx_data_complete
;
1012 spin_lock_bh(&ssi
->lock
);
1013 if (unlikely(ssi
->main_state
!= ACTIVE
)) {
1014 spin_unlock_bh(&ssi
->lock
);
1015 dev_dbg(&cl
->device
, "Dropping tx data: CMT is OFFLINE\n");
1018 list_add_tail(&msg
->link
, &ssi
->txqueue
);
1020 if (dev
->tx_queue_len
< ssi
->txqueue_len
) {
1021 dev_info(&cl
->device
, "TX queue full %d\n", ssi
->txqueue_len
);
1022 netif_stop_queue(dev
);
1024 if (ssi
->send_state
== SEND_IDLE
) {
1025 ssip_set_txstate(ssi
, WAIT4READY
);
1026 spin_unlock_bh(&ssi
->lock
);
1027 dev_dbg(&cl
->device
, "Start TX qlen %d\n", ssi
->txqueue_len
);
1029 } else if (ssi
->send_state
== SEND_READY
) {
1030 /* Needed for cmt-speech workaround */
1031 dev_dbg(&cl
->device
, "Start TX on SEND READY qlen %d\n",
1033 spin_unlock_bh(&ssi
->lock
);
1034 schedule_work(&ssi
->work
);
1036 spin_unlock_bh(&ssi
->lock
);
1038 dev
->stats
.tx_packets
++;
1039 dev
->stats
.tx_bytes
+= skb
->len
;
1047 dev
->stats
.tx_dropped
++;
1052 /* CMT reset event handler */
1053 void ssip_reset_event(struct hsi_client
*master
)
1055 struct ssi_protocol
*ssi
= hsi_client_drvdata(master
);
1056 dev_err(&ssi
->cl
->device
, "CMT reset detected!\n");
1057 ssip_error(ssi
->cl
);
1059 EXPORT_SYMBOL_GPL(ssip_reset_event
);
1061 static const struct net_device_ops ssip_pn_ops
= {
1062 .ndo_open
= ssip_pn_open
,
1063 .ndo_stop
= ssip_pn_stop
,
1064 .ndo_start_xmit
= ssip_pn_xmit
,
1067 static void ssip_pn_setup(struct net_device
*dev
)
1070 dev
->netdev_ops
= &ssip_pn_ops
;
1071 dev
->type
= ARPHRD_PHONET
;
1072 dev
->flags
= IFF_POINTOPOINT
| IFF_NOARP
;
1073 dev
->mtu
= SSIP_DEFAULT_MTU
;
1074 dev
->hard_header_len
= 1;
1075 dev
->dev_addr
[0] = PN_MEDIA_SOS
;
1077 dev
->tx_queue_len
= SSIP_TXQUEUE_LEN
;
1079 dev
->needs_free_netdev
= true;
1080 dev
->header_ops
= &phonet_header_ops
;
1083 static int ssi_protocol_probe(struct device
*dev
)
1085 static const char ifname
[] = "phonet%d";
1086 struct hsi_client
*cl
= to_hsi_client(dev
);
1087 struct ssi_protocol
*ssi
;
1090 ssi
= kzalloc(sizeof(*ssi
), GFP_KERNEL
);
1092 dev_err(dev
, "No memory for ssi protocol\n");
1096 spin_lock_init(&ssi
->lock
);
1097 timer_setup(&ssi
->rx_wd
, ssip_rx_wd
, TIMER_DEFERRABLE
);
1098 timer_setup(&ssi
->tx_wd
, ssip_tx_wd
, TIMER_DEFERRABLE
);
1099 timer_setup(&ssi
->keep_alive
, ssip_keep_alive
, 0);
1100 INIT_LIST_HEAD(&ssi
->txqueue
);
1101 INIT_LIST_HEAD(&ssi
->cmdqueue
);
1102 atomic_set(&ssi
->tx_usecnt
, 0);
1103 hsi_client_set_drvdata(cl
, ssi
);
1105 INIT_WORK(&ssi
->work
, ssip_xmit_work
);
1107 ssi
->channel_id_cmd
= hsi_get_channel_id_by_name(cl
, "mcsaab-control");
1108 if (ssi
->channel_id_cmd
< 0) {
1109 err
= ssi
->channel_id_cmd
;
1110 dev_err(dev
, "Could not get cmd channel (%d)\n", err
);
1114 ssi
->channel_id_data
= hsi_get_channel_id_by_name(cl
, "mcsaab-data");
1115 if (ssi
->channel_id_data
< 0) {
1116 err
= ssi
->channel_id_data
;
1117 dev_err(dev
, "Could not get data channel (%d)\n", err
);
1121 err
= ssip_alloc_cmds(ssi
);
1123 dev_err(dev
, "No memory for commands\n");
1127 ssi
->netdev
= alloc_netdev(0, ifname
, NET_NAME_UNKNOWN
, ssip_pn_setup
);
1129 dev_err(dev
, "No memory for netdev\n");
1134 /* MTU range: 6 - 65535 */
1135 ssi
->netdev
->min_mtu
= PHONET_MIN_MTU
;
1136 ssi
->netdev
->max_mtu
= SSIP_MAX_MTU
;
1138 SET_NETDEV_DEV(ssi
->netdev
, dev
);
1139 netif_carrier_off(ssi
->netdev
);
1140 err
= register_netdev(ssi
->netdev
);
1142 dev_err(dev
, "Register netdev failed (%d)\n", err
);
1146 list_add(&ssi
->link
, &ssip_list
);
1148 dev_dbg(dev
, "channel configuration: cmd=%d, data=%d\n",
1149 ssi
->channel_id_cmd
, ssi
->channel_id_data
);
1153 free_netdev(ssi
->netdev
);
1155 ssip_free_cmds(ssi
);
1162 static int ssi_protocol_remove(struct device
*dev
)
1164 struct hsi_client
*cl
= to_hsi_client(dev
);
1165 struct ssi_protocol
*ssi
= hsi_client_drvdata(cl
);
1167 list_del(&ssi
->link
);
1168 unregister_netdev(ssi
->netdev
);
1169 ssip_free_cmds(ssi
);
1170 hsi_client_set_drvdata(cl
, NULL
);
1176 static struct hsi_client_driver ssip_driver
= {
1178 .name
= "ssi-protocol",
1179 .owner
= THIS_MODULE
,
1180 .probe
= ssi_protocol_probe
,
1181 .remove
= ssi_protocol_remove
,
1185 static int __init
ssip_init(void)
1187 pr_info("SSI protocol aka McSAAB added\n");
1189 return hsi_register_client_driver(&ssip_driver
);
1191 module_init(ssip_init
);
1193 static void __exit
ssip_exit(void)
1195 hsi_unregister_client_driver(&ssip_driver
);
1196 pr_info("SSI protocol driver removed\n");
1198 module_exit(ssip_exit
);
1200 MODULE_ALIAS("hsi:ssi-protocol");
1201 MODULE_AUTHOR("Carlos Chinea <carlos.chinea@nokia.com>");
1202 MODULE_AUTHOR("Remi Denis-Courmont <remi.denis-courmont@nokia.com>");
1203 MODULE_DESCRIPTION("SSI protocol improved aka McSAAB");
1204 MODULE_LICENSE("GPL");