blob: 74bbdbe9f6641caf39de041f8742652ea9384157 [file] [log] [blame]
Arun Kumar Neelakantam406e5692013-01-17 18:58:04 +05301/* Copyright (c) 2011-2013, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002 *
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License version 2 and
5 * only version 2 as published by the Free Software Foundation.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 */
13
14/*
15 * BAM DMUX module.
16 */
17
18#define DEBUG
19
20#include <linux/delay.h>
21#include <linux/module.h>
22#include <linux/netdevice.h>
23#include <linux/platform_device.h>
24#include <linux/sched.h>
25#include <linux/skbuff.h>
26#include <linux/debugfs.h>
Jeff Hugoaab7ebc2011-09-07 16:46:04 -060027#include <linux/clk.h>
Jeff Hugoae3a85e2011-12-02 17:10:18 -070028#include <linux/wakelock.h>
Eric Holmberg878923a2012-01-10 14:28:19 -070029#include <linux/kfifo.h>
Jeff Hugo3910ee12012-08-21 14:08:20 -060030#include <linux/of.h>
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +053031#include <mach/msm_ipc_logging.h>
Arun Kumar Neelakantame7c0d622013-10-11 14:34:02 +053032#include <linux/srcu.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070033#include <mach/sps.h>
34#include <mach/bam_dmux.h>
Jeff Hugoade1f842011-08-03 15:53:59 -060035#include <mach/msm_smsm.h>
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060036#include <mach/subsystem_notif.h>
Jeff Hugo75913c82011-12-05 15:59:01 -070037#include <mach/socinfo.h>
Jeff Hugo4838f412012-01-20 11:19:37 -070038#include <mach/subsystem_restart.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070039
Brent Hronik89c96ba2013-08-27 14:34:22 -060040#include "bam_dmux_private.h"
41
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042#define BAM_CH_LOCAL_OPEN 0x1
43#define BAM_CH_REMOTE_OPEN 0x2
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060044#define BAM_CH_IN_RESET 0x4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070045
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -070046#define LOW_WATERMARK 2
47#define HIGH_WATERMARK 4
Anurag Singhdcd8b4e2012-07-30 16:46:37 -070048#define DEFAULT_POLLING_MIN_SLEEP (950)
49#define MAX_POLLING_SLEEP (6050)
50#define MIN_POLLING_SLEEP (950)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051
52static int msm_bam_dmux_debug_enable;
53module_param_named(debug_enable, msm_bam_dmux_debug_enable,
54 int, S_IRUGO | S_IWUSR | S_IWGRP);
Anurag Singh308c3862013-08-13 17:22:41 -070055static int POLLING_MIN_SLEEP = 2950;
Anurag Singhdcd8b4e2012-07-30 16:46:37 -070056module_param_named(min_sleep, POLLING_MIN_SLEEP,
57 int, S_IRUGO | S_IWUSR | S_IWGRP);
Anurag Singh308c3862013-08-13 17:22:41 -070058static int POLLING_MAX_SLEEP = 3050;
Anurag Singhdcd8b4e2012-07-30 16:46:37 -070059module_param_named(max_sleep, POLLING_MAX_SLEEP,
60 int, S_IRUGO | S_IWUSR | S_IWGRP);
Anurag Singh308c3862013-08-13 17:22:41 -070061static int POLLING_INACTIVITY = 1;
Anurag Singhdcd8b4e2012-07-30 16:46:37 -070062module_param_named(inactivity, POLLING_INACTIVITY,
63 int, S_IRUGO | S_IWUSR | S_IWGRP);
Anurag Singh308c3862013-08-13 17:22:41 -070064static int bam_adaptive_timer_enabled;
Anurag Singhdcd8b4e2012-07-30 16:46:37 -070065module_param_named(adaptive_timer_enabled,
66 bam_adaptive_timer_enabled,
67 int, S_IRUGO | S_IWUSR | S_IWGRP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070068
Brent Hronik89c96ba2013-08-27 14:34:22 -060069static struct bam_ops_if bam_default_ops = {
70 /* smsm */
71 .smsm_change_state_ptr = &smsm_change_state,
72 .smsm_get_state_ptr = &smsm_get_state,
73 .smsm_state_cb_register_ptr = &smsm_state_cb_register,
74 .smsm_state_cb_deregister_ptr = &smsm_state_cb_deregister,
75
76 /* sps */
77 .sps_connect_ptr = &sps_connect,
78 .sps_disconnect_ptr = &sps_disconnect,
79 .sps_register_bam_device_ptr = &sps_register_bam_device,
80 .sps_deregister_bam_device_ptr = &sps_deregister_bam_device,
81 .sps_alloc_endpoint_ptr = &sps_alloc_endpoint,
82 .sps_free_endpoint_ptr = &sps_free_endpoint,
83 .sps_set_config_ptr = &sps_set_config,
84 .sps_get_config_ptr = &sps_get_config,
85 .sps_device_reset_ptr = &sps_device_reset,
86 .sps_register_event_ptr = &sps_register_event,
87 .sps_transfer_one_ptr = &sps_transfer_one,
88 .sps_get_iovec_ptr = &sps_get_iovec,
89 .sps_get_unused_desc_num_ptr = &sps_get_unused_desc_num,
90
91 .dma_to = DMA_TO_DEVICE,
92 .dma_from = DMA_FROM_DEVICE,
93};
94static struct bam_ops_if *bam_ops = &bam_default_ops;
95
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070096#if defined(DEBUG)
97static uint32_t bam_dmux_read_cnt;
98static uint32_t bam_dmux_write_cnt;
99static uint32_t bam_dmux_write_cpy_cnt;
100static uint32_t bam_dmux_write_cpy_bytes;
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700101static uint32_t bam_dmux_tx_sps_failure_cnt;
Eric Holmberg6074aba2012-01-18 17:59:44 -0700102static uint32_t bam_dmux_tx_stall_cnt;
Eric Holmberg1f1255d2012-02-22 13:37:21 -0700103static atomic_t bam_dmux_ack_out_cnt = ATOMIC_INIT(0);
104static atomic_t bam_dmux_ack_in_cnt = ATOMIC_INIT(0);
105static atomic_t bam_dmux_a2_pwr_cntl_in_cnt = ATOMIC_INIT(0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700106
107#define DBG(x...) do { \
108 if (msm_bam_dmux_debug_enable) \
109 pr_debug(x); \
110 } while (0)
111
112#define DBG_INC_READ_CNT(x) do { \
113 bam_dmux_read_cnt += (x); \
114 if (msm_bam_dmux_debug_enable) \
115 pr_debug("%s: total read bytes %u\n", \
116 __func__, bam_dmux_read_cnt); \
117 } while (0)
118
119#define DBG_INC_WRITE_CNT(x) do { \
120 bam_dmux_write_cnt += (x); \
121 if (msm_bam_dmux_debug_enable) \
122 pr_debug("%s: total written bytes %u\n", \
123 __func__, bam_dmux_write_cnt); \
124 } while (0)
125
126#define DBG_INC_WRITE_CPY(x) do { \
127 bam_dmux_write_cpy_bytes += (x); \
128 bam_dmux_write_cpy_cnt++; \
129 if (msm_bam_dmux_debug_enable) \
130 pr_debug("%s: total write copy cnt %u, bytes %u\n", \
131 __func__, bam_dmux_write_cpy_cnt, \
132 bam_dmux_write_cpy_bytes); \
133 } while (0)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700134
135#define DBG_INC_TX_SPS_FAILURE_CNT() do { \
136 bam_dmux_tx_sps_failure_cnt++; \
137} while (0)
138
Eric Holmberg6074aba2012-01-18 17:59:44 -0700139#define DBG_INC_TX_STALL_CNT() do { \
140 bam_dmux_tx_stall_cnt++; \
141} while (0)
142
Eric Holmberg1f1255d2012-02-22 13:37:21 -0700143#define DBG_INC_ACK_OUT_CNT() \
144 atomic_inc(&bam_dmux_ack_out_cnt)
145
146#define DBG_INC_A2_POWER_CONTROL_IN_CNT() \
147 atomic_inc(&bam_dmux_a2_pwr_cntl_in_cnt)
148
149#define DBG_INC_ACK_IN_CNT() \
150 atomic_inc(&bam_dmux_ack_in_cnt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700151#else
152#define DBG(x...) do { } while (0)
153#define DBG_INC_READ_CNT(x...) do { } while (0)
154#define DBG_INC_WRITE_CNT(x...) do { } while (0)
155#define DBG_INC_WRITE_CPY(x...) do { } while (0)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700156#define DBG_INC_TX_SPS_FAILURE_CNT() do { } while (0)
Eric Holmberg6074aba2012-01-18 17:59:44 -0700157#define DBG_INC_TX_STALL_CNT() do { } while (0)
Eric Holmberg1f1255d2012-02-22 13:37:21 -0700158#define DBG_INC_ACK_OUT_CNT() do { } while (0)
159#define DBG_INC_A2_POWER_CONTROL_IN_CNT() \
160 do { } while (0)
161#define DBG_INC_ACK_IN_CNT() do { } while (0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700162#endif
163
164struct bam_ch_info {
165 uint32_t status;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600166 void (*notify)(void *, int, unsigned long);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700167 void *priv;
168 spinlock_t lock;
Jeff Hugo7960abd2011-08-02 15:39:38 -0600169 struct platform_device *pdev;
170 char name[BAM_DMUX_CH_NAME_MAX_LEN];
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700171 int num_tx_pkts;
172 int use_wm;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700173};
174
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700175#define A2_NUM_PIPES 6
176#define A2_SUMMING_THRESHOLD 4096
177#define A2_DEFAULT_DESCRIPTORS 32
178#define A2_PHYS_BASE 0x124C2000
179#define A2_PHYS_SIZE 0x2000
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700180#define NUM_BUFFERS 32
Jeff Hugo3910ee12012-08-21 14:08:20 -0600181
182#ifndef A2_BAM_IRQ
183#define A2_BAM_IRQ -1
184#endif
185
186static void *a2_phys_base;
187static uint32_t a2_phys_size;
188static int a2_bam_irq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189static struct sps_bam_props a2_props;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600190static u32 a2_device_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700191static struct sps_pipe *bam_tx_pipe;
192static struct sps_pipe *bam_rx_pipe;
193static struct sps_connect tx_connection;
194static struct sps_connect rx_connection;
195static struct sps_mem_buffer tx_desc_mem_buf;
196static struct sps_mem_buffer rx_desc_mem_buf;
197static struct sps_register_event tx_register_event;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600198static struct sps_register_event rx_register_event;
Jeff Hugo7c185602013-09-11 17:39:54 -0600199static unsigned long long last_rx_pkt_timestamp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700200
201static struct bam_ch_info bam_ch[BAM_DMUX_NUM_CHANNELS];
202static int bam_mux_initialized;
203
Jeff Hugo949080a2011-08-30 11:58:56 -0600204static int polling_mode;
Anurag Singhdcd8b4e2012-07-30 16:46:37 -0700205static unsigned long rx_timer_interval;
Jeff Hugo949080a2011-08-30 11:58:56 -0600206
207static LIST_HEAD(bam_rx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600208static DEFINE_MUTEX(bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700209static int bam_rx_pool_len;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600210static LIST_HEAD(bam_tx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600211static DEFINE_SPINLOCK(bam_tx_pool_spinlock);
Eric Holmberga623da82012-07-12 09:37:09 -0600212static DEFINE_MUTEX(bam_pdev_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600213
Jeff Hugod98b1082011-10-24 10:30:23 -0600214static void notify_all(int event, unsigned long data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700215static void bam_mux_write_done(struct work_struct *work);
216static void handle_bam_mux_cmd(struct work_struct *work);
Jeff Hugo949080a2011-08-30 11:58:56 -0600217static void rx_timer_work_func(struct work_struct *work);
Jeff Hugo0f03ddc2013-12-15 16:36:11 -0700218static void queue_rx_work_func(struct work_struct *work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700219
Jeff Hugo949080a2011-08-30 11:58:56 -0600220static DECLARE_WORK(rx_timer_work, rx_timer_work_func);
Jeff Hugo0f03ddc2013-12-15 16:36:11 -0700221static DECLARE_WORK(queue_rx_work, queue_rx_work_func);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700222
223static struct workqueue_struct *bam_mux_rx_workqueue;
224static struct workqueue_struct *bam_mux_tx_workqueue;
225
Arun Kumar Neelakantame7c0d622013-10-11 14:34:02 +0530226static struct srcu_struct bam_dmux_srcu;
227
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600228/* A2 power collaspe */
229#define UL_TIMEOUT_DELAY 1000 /* in ms */
Jeff Hugo0b13a352012-03-17 23:18:30 -0600230#define ENABLE_DISCONNECT_ACK 0x1
Brent Hronik096f7d32013-06-28 15:43:08 -0600231#define SHUTDOWN_TIMEOUT_MS 500
Jeff Hugo1f317392013-07-24 16:28:52 -0600232#define UL_WAKEUP_TIMEOUT_MS 2000
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600233static void toggle_apps_ack(void);
234static void reconnect_to_bam(void);
235static void disconnect_to_bam(void);
236static void ul_wakeup(void);
237static void ul_timeout(struct work_struct *work);
238static void vote_dfab(void);
239static void unvote_dfab(void);
Jeff Hugod98b1082011-10-24 10:30:23 -0600240static void kickoff_ul_wakeup_func(struct work_struct *work);
Eric Holmberg006057d2012-01-11 10:10:42 -0700241static void grab_wakelock(void);
242static void release_wakelock(void);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600243
244static int bam_is_connected;
245static DEFINE_MUTEX(wakeup_lock);
246static struct completion ul_wakeup_ack_completion;
247static struct completion bam_connection_completion;
248static struct delayed_work ul_timeout_work;
249static int ul_packet_written;
Eric Holmbergbc9f21c2012-01-18 11:33:33 -0700250static atomic_t ul_ondemand_vote = ATOMIC_INIT(0);
Stephen Boyd69d35e32012-02-14 15:33:30 -0800251static struct clk *dfab_clk, *xo_clk;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600252static DEFINE_RWLOCK(ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600253static DECLARE_WORK(kickoff_ul_wakeup, kickoff_ul_wakeup_func);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600254static int bam_connection_is_active;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -0700255static int wait_for_ack;
Jeff Hugoae3a85e2011-12-02 17:10:18 -0700256static struct wake_lock bam_wakelock;
Eric Holmberg006057d2012-01-11 10:10:42 -0700257static int a2_pc_disabled;
258static DEFINE_MUTEX(dfab_status_lock);
259static int dfab_is_on;
260static int wait_for_dfab;
261static struct completion dfab_unvote_completion;
262static DEFINE_SPINLOCK(wakelock_reference_lock);
263static int wakelock_reference_count;
Jeff Hugo583a6da2012-02-03 11:37:30 -0700264static int a2_pc_disabled_wakelock_skipped;
Jeff Hugob1e7c582012-06-20 15:02:11 -0600265static int disconnect_ack = 1;
Jeff Hugocb798022012-04-09 14:55:40 -0600266static LIST_HEAD(bam_other_notify_funcs);
Jeff Hugo4b7c7b32012-04-18 16:25:14 -0600267static DEFINE_MUTEX(smsm_cb_lock);
Jeff Hugoc2696142012-05-03 11:42:13 -0600268static DEFINE_MUTEX(delayed_ul_vote_lock);
269static int need_delayed_ul_vote;
Jeff Hugo18792a32012-06-20 15:25:55 -0600270static int power_management_only_mode;
Jeff Hugoa82a95c2012-12-14 17:56:19 -0700271static int in_ssr;
272static int ssr_skipped_disconnect;
Brent Hronik096f7d32013-06-28 15:43:08 -0600273static struct completion shutdown_completion;
Jeff Hugocb798022012-04-09 14:55:40 -0600274
275struct outside_notify_func {
276 void (*notify)(void *, int, unsigned long);
277 void *priv;
278 struct list_head list_node;
279};
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600280/* End A2 power collaspe */
281
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600282/* subsystem restart */
283static int restart_notifier_cb(struct notifier_block *this,
284 unsigned long code,
285 void *data);
286
287static struct notifier_block restart_notifier = {
288 .notifier_call = restart_notifier_cb,
289};
290static int in_global_reset;
291/* end subsystem restart */
292
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700293#define bam_ch_is_open(x) \
294 (bam_ch[(x)].status == (BAM_CH_LOCAL_OPEN | BAM_CH_REMOTE_OPEN))
295
296#define bam_ch_is_local_open(x) \
297 (bam_ch[(x)].status & BAM_CH_LOCAL_OPEN)
298
299#define bam_ch_is_remote_open(x) \
300 (bam_ch[(x)].status & BAM_CH_REMOTE_OPEN)
301
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600302#define bam_ch_is_in_reset(x) \
303 (bam_ch[(x)].status & BAM_CH_IN_RESET)
304
Eric Holmberg878923a2012-01-10 14:28:19 -0700305struct kfifo bam_dmux_state_log;
Eric Holmberg878923a2012-01-10 14:28:19 -0700306static int bam_dmux_uplink_vote;
307static int bam_dmux_power_state;
308
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530309static void *bam_ipc_log_txt;
310
311#define BAM_IPC_LOG_PAGES 5
312
Eric Holmberg878923a2012-01-10 14:28:19 -0700313/**
314 * Log a state change along with a small message.
Eric Holmberg878923a2012-01-10 14:28:19 -0700315 * Complete size of messsage is limited to @todo.
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530316 * Logging is done using IPC Logging infrastructure.
317 *
318 * States
319 * D: 1 = Power collapse disabled
320 * R: 1 = in global reset
321 * P: 1 = BAM is powered up
322 * A: 1 = BAM initialized and ready for data
323 * V: 1 = Uplink vote for power
324 * U: 1 = Uplink active
325 * W: 1 = Uplink Wait-for-ack
326 * A: 1 = Uplink ACK received
327 * #: >=1 On-demand uplink vote
328 * D: 1 = Disconnect ACK active
Eric Holmberg878923a2012-01-10 14:28:19 -0700329 */
Eric Holmberg878923a2012-01-10 14:28:19 -0700330
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530331#define BAM_DMUX_LOG(fmt, args...) \
332do { \
333 if (bam_ipc_log_txt) { \
334 ipc_log_string(bam_ipc_log_txt, \
335 "<DMUX> %c%c%c%c %c%c%c%c%d%c " fmt, \
336 a2_pc_disabled ? 'D' : 'd', \
337 in_global_reset ? 'R' : 'r', \
338 bam_dmux_power_state ? 'P' : 'p', \
339 bam_connection_is_active ? 'A' : 'a', \
340 bam_dmux_uplink_vote ? 'V' : 'v', \
341 bam_is_connected ? 'U' : 'u', \
342 wait_for_ack ? 'W' : 'w', \
343 ul_wakeup_ack_completion.done ? 'A' : 'a', \
344 atomic_read(&ul_ondemand_vote), \
345 disconnect_ack ? 'D' : 'd', \
346 args); \
347 } \
348} while (0)
Eric Holmberg878923a2012-01-10 14:28:19 -0700349
Zaheerulla Meerf800bba2013-02-13 15:49:14 +0530350#define DMUX_LOG_KERR(fmt, args...) \
351do { \
352 BAM_DMUX_LOG(fmt, args); \
353 pr_err(fmt, args); \
354} while (0)
355
Eric Holmberg878923a2012-01-10 14:28:19 -0700356static inline void set_tx_timestamp(struct tx_pkt_info *pkt)
357{
358 unsigned long long t_now;
359
360 t_now = sched_clock();
361 pkt->ts_nsec = do_div(t_now, 1000000000U);
362 pkt->ts_sec = (unsigned)t_now;
363}
364
365static inline void verify_tx_queue_is_empty(const char *func)
366{
367 unsigned long flags;
368 struct tx_pkt_info *info;
369 int reported = 0;
370
371 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
372 list_for_each_entry(info, &bam_tx_pool, list_node) {
373 if (!reported) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530374 BAM_DMUX_LOG("%s: tx pool not empty\n", func);
Eric Holmberg454d9da2012-01-12 09:37:14 -0700375 if (!in_global_reset)
376 pr_err("%s: tx pool not empty\n", func);
Eric Holmberg878923a2012-01-10 14:28:19 -0700377 reported = 1;
378 }
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530379 BAM_DMUX_LOG("%s: node=%p ts=%u.%09lu\n", __func__,
Eric Holmberg454d9da2012-01-12 09:37:14 -0700380 &info->list_node, info->ts_sec, info->ts_nsec);
381 if (!in_global_reset)
382 pr_err("%s: node=%p ts=%u.%09lu\n", __func__,
383 &info->list_node, info->ts_sec, info->ts_nsec);
Eric Holmberg878923a2012-01-10 14:28:19 -0700384 }
385 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
386}
387
Jeff Hugo0f03ddc2013-12-15 16:36:11 -0700388static void __queue_rx(gfp_t alloc_flags)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700389{
390 void *ptr;
391 struct rx_pkt_info *info;
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700392 int ret;
393 int rx_len_cached;
Jeff Hugo949080a2011-08-30 11:58:56 -0600394
Jeff Hugoc9749932011-11-02 17:50:40 -0600395 mutex_lock(&bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700396 rx_len_cached = bam_rx_pool_len;
Jeff Hugoc9749932011-11-02 17:50:40 -0600397 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600398
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600399 while (bam_connection_is_active && rx_len_cached < NUM_BUFFERS) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700400 if (in_global_reset)
401 goto fail;
402
Jeff Hugo0f03ddc2013-12-15 16:36:11 -0700403 info = kmalloc(sizeof(struct rx_pkt_info), alloc_flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700404 if (!info) {
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600405 DMUX_LOG_KERR(
Jeff Hugo0f03ddc2013-12-15 16:36:11 -0700406 "%s: unable to alloc rx_pkt_info w/ flags %x, will retry later\n",
407 __func__,
408 alloc_flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700409 goto fail;
410 }
411
412 INIT_WORK(&info->work, handle_bam_mux_cmd);
413
Jeff Hugo0f03ddc2013-12-15 16:36:11 -0700414 info->skb = __dev_alloc_skb(BUFFER_SIZE, alloc_flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700415 if (info->skb == NULL) {
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600416 DMUX_LOG_KERR(
Jeff Hugo0f03ddc2013-12-15 16:36:11 -0700417 "%s: unable to alloc skb w/ flags %x, will retry later\n",
418 __func__,
419 alloc_flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700420 goto fail_info;
421 }
422 ptr = skb_put(info->skb, BUFFER_SIZE);
423
424 info->dma_address = dma_map_single(NULL, ptr, BUFFER_SIZE,
Brent Hronik89c96ba2013-08-27 14:34:22 -0600425 bam_ops->dma_from);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700426 if (info->dma_address == 0 || info->dma_address == ~0) {
427 DMUX_LOG_KERR("%s: dma_map_single failure %p for %p\n",
428 __func__, (void *)info->dma_address, ptr);
429 goto fail_skb;
430 }
431
432 mutex_lock(&bam_rx_pool_mutexlock);
433 list_add_tail(&info->list_node, &bam_rx_pool);
434 rx_len_cached = ++bam_rx_pool_len;
Brent Hronik89c96ba2013-08-27 14:34:22 -0600435 ret = bam_ops->sps_transfer_one_ptr(bam_rx_pipe,
436 info->dma_address, BUFFER_SIZE, info, 0);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700437 if (ret) {
Eric Holmberg00cf8692012-07-16 14:21:19 -0600438 list_del(&info->list_node);
439 rx_len_cached = --bam_rx_pool_len;
440 mutex_unlock(&bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700441 DMUX_LOG_KERR("%s: sps_transfer_one failed %d\n",
442 __func__, ret);
Eric Holmberg00cf8692012-07-16 14:21:19 -0600443
444 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
Brent Hronik89c96ba2013-08-27 14:34:22 -0600445 bam_ops->dma_from);
Eric Holmberg00cf8692012-07-16 14:21:19 -0600446
447 goto fail_skb;
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700448 }
Eric Holmberg00cf8692012-07-16 14:21:19 -0600449 mutex_unlock(&bam_rx_pool_mutexlock);
450
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700451 }
452 return;
453
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700454fail_skb:
455 dev_kfree_skb_any(info->skb);
456
457fail_info:
458 kfree(info);
459
460fail:
Jeff Hugo0f03ddc2013-12-15 16:36:11 -0700461 if (!in_global_reset) {
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600462 DMUX_LOG_KERR("%s: rescheduling\n", __func__);
Jeff Hugo0f03ddc2013-12-15 16:36:11 -0700463 schedule_work(&queue_rx_work);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700464 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700465}
466
Jeff Hugo0f03ddc2013-12-15 16:36:11 -0700467static void queue_rx(void)
468{
469 /*
470 * Hot path. Delays waiting for the allocation to find memory if its
471 * not immediately available, and delays from logging allocation
472 * failures which cannot be tolerated at this time.
473 */
474 __queue_rx(GFP_NOWAIT | __GFP_NOWARN);
475}
476
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600477static void queue_rx_work_func(struct work_struct *work)
478{
Jeff Hugo0f03ddc2013-12-15 16:36:11 -0700479 /*
480 * Cold path. Delays can be tolerated. Use of GFP_KERNEL should
481 * guarentee the requested memory will be found, after some ammount of
482 * delay.
483 */
484 __queue_rx(GFP_KERNEL);
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600485}
486
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487static void bam_mux_process_data(struct sk_buff *rx_skb)
488{
489 unsigned long flags;
490 struct bam_mux_hdr *rx_hdr;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600491 unsigned long event_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492
493 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
494
495 rx_skb->data = (unsigned char *)(rx_hdr + 1);
496 rx_skb->tail = rx_skb->data + rx_hdr->pkt_len;
497 rx_skb->len = rx_hdr->pkt_len;
Jeff Hugoee88f672011-10-04 17:14:52 -0600498 rx_skb->truesize = rx_hdr->pkt_len + sizeof(struct sk_buff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700499
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600500 event_data = (unsigned long)(rx_skb);
501
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700502 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600503 if (bam_ch[rx_hdr->ch_id].notify)
504 bam_ch[rx_hdr->ch_id].notify(
505 bam_ch[rx_hdr->ch_id].priv, BAM_DMUX_RECEIVE,
506 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700507 else
508 dev_kfree_skb_any(rx_skb);
509 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
510
511 queue_rx();
512}
513
Eric Holmberg006057d2012-01-11 10:10:42 -0700514static inline void handle_bam_mux_cmd_open(struct bam_mux_hdr *rx_hdr)
515{
516 unsigned long flags;
517 int ret;
518
Eric Holmberga623da82012-07-12 09:37:09 -0600519 mutex_lock(&bam_pdev_mutexlock);
520 if (in_global_reset) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530521 BAM_DMUX_LOG("%s: open cid %d aborted due to ssr\n",
Eric Holmberga623da82012-07-12 09:37:09 -0600522 __func__, rx_hdr->ch_id);
523 mutex_unlock(&bam_pdev_mutexlock);
524 queue_rx();
525 return;
526 }
Eric Holmberg006057d2012-01-11 10:10:42 -0700527 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
528 bam_ch[rx_hdr->ch_id].status |= BAM_CH_REMOTE_OPEN;
529 bam_ch[rx_hdr->ch_id].num_tx_pkts = 0;
530 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Eric Holmberg006057d2012-01-11 10:10:42 -0700531 ret = platform_device_add(bam_ch[rx_hdr->ch_id].pdev);
532 if (ret)
533 pr_err("%s: platform_device_add() error: %d\n",
534 __func__, ret);
Eric Holmberga623da82012-07-12 09:37:09 -0600535 mutex_unlock(&bam_pdev_mutexlock);
536 queue_rx();
Eric Holmberg006057d2012-01-11 10:10:42 -0700537}
538
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700539static void handle_bam_mux_cmd(struct work_struct *work)
540{
541 unsigned long flags;
542 struct bam_mux_hdr *rx_hdr;
543 struct rx_pkt_info *info;
544 struct sk_buff *rx_skb;
545
546 info = container_of(work, struct rx_pkt_info, work);
547 rx_skb = info->skb;
Brent Hronik89c96ba2013-08-27 14:34:22 -0600548 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
549 bam_ops->dma_from);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700550 kfree(info);
551
552 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
553
554 DBG_INC_READ_CNT(sizeof(struct bam_mux_hdr));
555 DBG("%s: magic %x reserved %d cmd %d pad %d ch %d len %d\n", __func__,
556 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
557 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
558 if (rx_hdr->magic_num != BAM_MUX_HDR_MAGIC_NO) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700559 DMUX_LOG_KERR("%s: dropping invalid hdr. magic %x"
560 " reserved %d cmd %d"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700561 " pad %d ch %d len %d\n", __func__,
562 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
563 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
564 dev_kfree_skb_any(rx_skb);
565 queue_rx();
566 return;
567 }
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700568
569 if (rx_hdr->ch_id >= BAM_DMUX_NUM_CHANNELS) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700570 DMUX_LOG_KERR("%s: dropping invalid LCID %d"
571 " reserved %d cmd %d"
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700572 " pad %d ch %d len %d\n", __func__,
573 rx_hdr->ch_id, rx_hdr->reserved, rx_hdr->cmd,
574 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
575 dev_kfree_skb_any(rx_skb);
576 queue_rx();
577 return;
578 }
579
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700580 switch (rx_hdr->cmd) {
581 case BAM_MUX_HDR_CMD_DATA:
582 DBG_INC_READ_CNT(rx_hdr->pkt_len);
583 bam_mux_process_data(rx_skb);
584 break;
585 case BAM_MUX_HDR_CMD_OPEN:
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530586 BAM_DMUX_LOG("%s: opening cid %d PC enabled\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -0700587 rx_hdr->ch_id);
Eric Holmberg006057d2012-01-11 10:10:42 -0700588 handle_bam_mux_cmd_open(rx_hdr);
Jeff Hugob1e7c582012-06-20 15:02:11 -0600589 if (!(rx_hdr->reserved & ENABLE_DISCONNECT_ACK)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530590 BAM_DMUX_LOG("%s: deactivating disconnect ack\n",
Jeff Hugod7d2b062012-07-24 14:29:56 -0600591 __func__);
Jeff Hugob1e7c582012-06-20 15:02:11 -0600592 disconnect_ack = 0;
Jeff Hugo0b13a352012-03-17 23:18:30 -0600593 }
Eric Holmberg006057d2012-01-11 10:10:42 -0700594 dev_kfree_skb_any(rx_skb);
595 break;
596 case BAM_MUX_HDR_CMD_OPEN_NO_A2_PC:
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530597 BAM_DMUX_LOG("%s: opening cid %d PC disabled\n", __func__,
Eric Holmberg006057d2012-01-11 10:10:42 -0700598 rx_hdr->ch_id);
599
600 if (!a2_pc_disabled) {
601 a2_pc_disabled = 1;
Jeff Hugo322179f2012-02-29 10:52:34 -0700602 ul_wakeup();
Eric Holmberg006057d2012-01-11 10:10:42 -0700603 }
604
605 handle_bam_mux_cmd_open(rx_hdr);
Eric Holmberge779dba2011-11-04 18:22:01 -0600606 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607 break;
608 case BAM_MUX_HDR_CMD_CLOSE:
609 /* probably should drop pending write */
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530610 BAM_DMUX_LOG("%s: closing cid %d\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -0700611 rx_hdr->ch_id);
Eric Holmberga623da82012-07-12 09:37:09 -0600612 mutex_lock(&bam_pdev_mutexlock);
613 if (in_global_reset) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530614 BAM_DMUX_LOG("%s: close cid %d aborted due to ssr\n",
Eric Holmberga623da82012-07-12 09:37:09 -0600615 __func__, rx_hdr->ch_id);
616 mutex_unlock(&bam_pdev_mutexlock);
617 break;
618 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700619 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
620 bam_ch[rx_hdr->ch_id].status &= ~BAM_CH_REMOTE_OPEN;
621 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo7960abd2011-08-02 15:39:38 -0600622 platform_device_unregister(bam_ch[rx_hdr->ch_id].pdev);
623 bam_ch[rx_hdr->ch_id].pdev =
624 platform_device_alloc(bam_ch[rx_hdr->ch_id].name, 2);
625 if (!bam_ch[rx_hdr->ch_id].pdev)
626 pr_err("%s: platform_device_alloc failed\n", __func__);
Eric Holmberga623da82012-07-12 09:37:09 -0600627 mutex_unlock(&bam_pdev_mutexlock);
Eric Holmberge779dba2011-11-04 18:22:01 -0600628 dev_kfree_skb_any(rx_skb);
Eric Holmberga623da82012-07-12 09:37:09 -0600629 queue_rx();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700630 break;
631 default:
Eric Holmberg878923a2012-01-10 14:28:19 -0700632 DMUX_LOG_KERR("%s: dropping invalid hdr. magic %x"
633 " reserved %d cmd %d pad %d ch %d len %d\n",
634 __func__, rx_hdr->magic_num, rx_hdr->reserved,
635 rx_hdr->cmd, rx_hdr->pad_len, rx_hdr->ch_id,
636 rx_hdr->pkt_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700637 dev_kfree_skb_any(rx_skb);
638 queue_rx();
639 return;
640 }
641}
642
643static int bam_mux_write_cmd(void *data, uint32_t len)
644{
645 int rc;
646 struct tx_pkt_info *pkt;
647 dma_addr_t dma_address;
Jeff Hugo626303bf2011-11-21 11:43:28 -0700648 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649
Eric Holmbergd83cd2b2011-11-04 15:54:17 -0600650 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651 if (pkt == NULL) {
652 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
653 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700654 return rc;
655 }
656
657 dma_address = dma_map_single(NULL, data, len,
Brent Hronik89c96ba2013-08-27 14:34:22 -0600658 bam_ops->dma_to);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700659 if (!dma_address) {
660 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugo96cb7482011-12-07 13:28:31 -0700661 kfree(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700662 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700663 return rc;
664 }
665 pkt->skb = (struct sk_buff *)(data);
666 pkt->len = len;
667 pkt->dma_address = dma_address;
668 pkt->is_cmd = 1;
Eric Holmberg878923a2012-01-10 14:28:19 -0700669 set_tx_timestamp(pkt);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600670 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700671 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600672 list_add_tail(&pkt->list_node, &bam_tx_pool);
Brent Hronik89c96ba2013-08-27 14:34:22 -0600673 rc = bam_ops->sps_transfer_one_ptr(bam_tx_pipe, dma_address, len,
Jeff Hugoc85df962013-04-05 13:22:48 -0600674 pkt, SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600675 if (rc) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700676 DMUX_LOG_KERR("%s sps_transfer_one failed rc=%d\n",
677 __func__, rc);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600678 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700679 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700680 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700681 dma_unmap_single(NULL, pkt->dma_address,
682 pkt->len,
Brent Hronik89c96ba2013-08-27 14:34:22 -0600683 bam_ops->dma_to);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600684 kfree(pkt);
Jeff Hugobb6da952012-01-16 15:02:42 -0700685 } else {
686 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600687 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700688
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600689 ul_packet_written = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700690 return rc;
691}
692
693static void bam_mux_write_done(struct work_struct *work)
694{
695 struct sk_buff *skb;
696 struct bam_mux_hdr *hdr;
697 struct tx_pkt_info *info;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700698 struct tx_pkt_info *info_expected;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600699 unsigned long event_data;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700700 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700701
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600702 if (in_global_reset)
703 return;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700704
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700705 info = container_of(work, struct tx_pkt_info, work);
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700706
707 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
708 info_expected = list_first_entry(&bam_tx_pool,
709 struct tx_pkt_info, list_node);
710 if (unlikely(info != info_expected)) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700711 struct tx_pkt_info *errant_pkt;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700712
Eric Holmberg878923a2012-01-10 14:28:19 -0700713 DMUX_LOG_KERR("%s: bam_tx_pool mismatch .next=%p,"
714 " list_node=%p, ts=%u.%09lu\n",
715 __func__, bam_tx_pool.next, &info->list_node,
716 info->ts_sec, info->ts_nsec
717 );
718
719 list_for_each_entry(errant_pkt, &bam_tx_pool, list_node) {
720 DMUX_LOG_KERR("%s: node=%p ts=%u.%09lu\n", __func__,
721 &errant_pkt->list_node, errant_pkt->ts_sec,
722 errant_pkt->ts_nsec);
723
724 }
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700725 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
726 BUG();
727 }
728 list_del(&info->list_node);
729 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
730
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600731 if (info->is_cmd) {
732 kfree(info->skb);
733 kfree(info);
734 return;
735 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700736 skb = info->skb;
737 kfree(info);
738 hdr = (struct bam_mux_hdr *)skb->data;
Eric Holmberg9fdef262012-02-14 11:46:05 -0700739 DBG_INC_WRITE_CNT(skb->len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600740 event_data = (unsigned long)(skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700741 spin_lock_irqsave(&bam_ch[hdr->ch_id].lock, flags);
742 bam_ch[hdr->ch_id].num_tx_pkts--;
743 spin_unlock_irqrestore(&bam_ch[hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600744 if (bam_ch[hdr->ch_id].notify)
745 bam_ch[hdr->ch_id].notify(
746 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
747 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748 else
749 dev_kfree_skb_any(skb);
750}
751
752int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
753{
754 int rc = 0;
755 struct bam_mux_hdr *hdr;
756 unsigned long flags;
757 struct sk_buff *new_skb = NULL;
758 dma_addr_t dma_address;
759 struct tx_pkt_info *pkt;
Arun Kumar Neelakantame7c0d622013-10-11 14:34:02 +0530760 int rcu_id;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700761
762 if (id >= BAM_DMUX_NUM_CHANNELS)
763 return -EINVAL;
764 if (!skb)
765 return -EINVAL;
766 if (!bam_mux_initialized)
767 return -ENODEV;
768
Arun Kumar Neelakantame7c0d622013-10-11 14:34:02 +0530769 rcu_id = srcu_read_lock(&bam_dmux_srcu);
770 if (in_global_reset) {
771 BAM_DMUX_LOG("%s: In SSR... ch_id[%d]\n", __func__, id);
772 srcu_read_unlock(&bam_dmux_srcu, rcu_id);
773 return -EFAULT;
774 }
775
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700776 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
777 spin_lock_irqsave(&bam_ch[id].lock, flags);
778 if (!bam_ch_is_open(id)) {
779 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
780 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
Arun Kumar Neelakantame7c0d622013-10-11 14:34:02 +0530781 srcu_read_unlock(&bam_dmux_srcu, rcu_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782 return -ENODEV;
783 }
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700784
785 if (bam_ch[id].use_wm &&
786 (bam_ch[id].num_tx_pkts >= HIGH_WATERMARK)) {
787 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
788 pr_err("%s: watermark exceeded: %d\n", __func__, id);
Arun Kumar Neelakantame7c0d622013-10-11 14:34:02 +0530789 srcu_read_unlock(&bam_dmux_srcu, rcu_id);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700790 return -EAGAIN;
791 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700792 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
793
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600794 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600795 if (!bam_is_connected) {
796 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600797 ul_wakeup();
Arun Kumar Neelakantame7c0d622013-10-11 14:34:02 +0530798 if (unlikely(in_global_reset == 1)) {
799 srcu_read_unlock(&bam_dmux_srcu, rcu_id);
Jeff Hugo4838f412012-01-20 11:19:37 -0700800 return -EFAULT;
Arun Kumar Neelakantame7c0d622013-10-11 14:34:02 +0530801 }
Jeff Hugo061ce672011-10-21 17:15:32 -0600802 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600803 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600804 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600805
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700806 /* if skb do not have any tailroom for padding,
807 copy the skb into a new expanded skb */
808 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
809 /* revisit, probably dev_alloc_skb and memcpy is effecient */
810 new_skb = skb_copy_expand(skb, skb_headroom(skb),
811 4 - (skb->len & 0x3), GFP_ATOMIC);
812 if (new_skb == NULL) {
813 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600814 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815 }
816 dev_kfree_skb_any(skb);
817 skb = new_skb;
818 DBG_INC_WRITE_CPY(skb->len);
819 }
820
821 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
822
823 /* caller should allocate for hdr and padding
824 hdr is fine, padding is tricky */
825 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
826 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
827 hdr->reserved = 0;
828 hdr->ch_id = id;
829 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
830 if (skb->len & 0x3)
831 skb_put(skb, 4 - (skb->len & 0x3));
832
833 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
834
835 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
836 __func__, skb->data, skb->tail, skb->len,
837 hdr->pkt_len, hdr->pad_len);
838
839 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
840 if (pkt == NULL) {
841 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600842 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700843 }
844
845 dma_address = dma_map_single(NULL, skb->data, skb->len,
Brent Hronik89c96ba2013-08-27 14:34:22 -0600846 bam_ops->dma_to);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700847 if (!dma_address) {
848 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600849 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700850 }
851 pkt->skb = skb;
852 pkt->dma_address = dma_address;
853 pkt->is_cmd = 0;
Eric Holmberg878923a2012-01-10 14:28:19 -0700854 set_tx_timestamp(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700856 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600857 list_add_tail(&pkt->list_node, &bam_tx_pool);
Brent Hronik89c96ba2013-08-27 14:34:22 -0600858 rc = bam_ops->sps_transfer_one_ptr(bam_tx_pipe, dma_address, skb->len,
Jeff Hugoc85df962013-04-05 13:22:48 -0600859 pkt, SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600860 if (rc) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700861 DMUX_LOG_KERR("%s sps_transfer_one failed rc=%d\n",
862 __func__, rc);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600863 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700864 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700865 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700866 dma_unmap_single(NULL, pkt->dma_address,
Brent Hronik89c96ba2013-08-27 14:34:22 -0600867 pkt->skb->len, bam_ops->dma_to);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600868 kfree(pkt);
Jeff Hugo872bd062011-11-15 17:47:21 -0700869 if (new_skb)
870 dev_kfree_skb_any(new_skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700871 } else {
Jeff Hugobb6da952012-01-16 15:02:42 -0700872 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700873 spin_lock_irqsave(&bam_ch[id].lock, flags);
874 bam_ch[id].num_tx_pkts++;
875 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600876 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600877 ul_packet_written = 1;
878 read_unlock(&ul_wakeup_lock);
Arun Kumar Neelakantame7c0d622013-10-11 14:34:02 +0530879 srcu_read_unlock(&bam_dmux_srcu, rcu_id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700880 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600881
882write_fail3:
883 kfree(pkt);
884write_fail2:
Arun Kumar Neelakantam406e5692013-01-17 18:58:04 +0530885 skb_pull(skb, sizeof(struct bam_mux_hdr));
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600886 if (new_skb)
887 dev_kfree_skb_any(new_skb);
888write_fail:
889 read_unlock(&ul_wakeup_lock);
Arun Kumar Neelakantame7c0d622013-10-11 14:34:02 +0530890 srcu_read_unlock(&bam_dmux_srcu, rcu_id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600891 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700892}
893
894int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600895 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700896{
897 struct bam_mux_hdr *hdr;
898 unsigned long flags;
899 int rc = 0;
900
901 DBG("%s: opening ch %d\n", __func__, id);
Eric Holmberg5d775432011-11-09 10:23:35 -0700902 if (!bam_mux_initialized) {
903 DBG("%s: not inititialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700904 return -ENODEV;
Eric Holmberg5d775432011-11-09 10:23:35 -0700905 }
906 if (id >= BAM_DMUX_NUM_CHANNELS) {
907 pr_err("%s: invalid channel id %d\n", __func__, id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700909 }
910 if (notify == NULL) {
911 pr_err("%s: notify function is NULL\n", __func__);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600912 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700913 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700914
915 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
916 if (hdr == NULL) {
917 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
918 return -ENOMEM;
919 }
920 spin_lock_irqsave(&bam_ch[id].lock, flags);
921 if (bam_ch_is_open(id)) {
922 DBG("%s: Already opened %d\n", __func__, id);
923 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
924 kfree(hdr);
925 goto open_done;
926 }
927 if (!bam_ch_is_remote_open(id)) {
928 DBG("%s: Remote not open; ch: %d\n", __func__, id);
929 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
930 kfree(hdr);
Eric Holmberg5d775432011-11-09 10:23:35 -0700931 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700932 }
933
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600934 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700935 bam_ch[id].priv = priv;
936 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700937 bam_ch[id].num_tx_pkts = 0;
938 bam_ch[id].use_wm = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700939 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
940
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600941 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600942 if (!bam_is_connected) {
943 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600944 ul_wakeup();
Brent Hronik96630422013-05-01 16:38:43 -0600945 if (unlikely(in_global_reset == 1)) {
946 kfree(hdr);
Jeff Hugo4838f412012-01-20 11:19:37 -0700947 return -EFAULT;
Brent Hronik96630422013-05-01 16:38:43 -0600948 }
Jeff Hugo061ce672011-10-21 17:15:32 -0600949 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600950 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600951 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600952
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
954 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
955 hdr->reserved = 0;
956 hdr->ch_id = id;
957 hdr->pkt_len = 0;
958 hdr->pad_len = 0;
959
960 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600961 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700962
963open_done:
964 DBG("%s: opened ch %d\n", __func__, id);
965 return rc;
966}
967
968int msm_bam_dmux_close(uint32_t id)
969{
970 struct bam_mux_hdr *hdr;
971 unsigned long flags;
972 int rc;
973
974 if (id >= BAM_DMUX_NUM_CHANNELS)
975 return -EINVAL;
976 DBG("%s: closing ch %d\n", __func__, id);
977 if (!bam_mux_initialized)
978 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700979
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600980 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600981 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600982 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600983 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700984 if (unlikely(in_global_reset == 1))
985 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600986 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600987 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600988 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600989
Jeff Hugo061ce672011-10-21 17:15:32 -0600990 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600991 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700992 bam_ch[id].priv = NULL;
993 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
994 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
995
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600996 if (bam_ch_is_in_reset(id)) {
997 read_unlock(&ul_wakeup_lock);
998 bam_ch[id].status &= ~BAM_CH_IN_RESET;
999 return 0;
1000 }
1001
Jeff Hugobb5802f2011-11-02 17:10:29 -06001002 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001003 if (hdr == NULL) {
1004 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -06001005 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001006 return -ENOMEM;
1007 }
1008 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
1009 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
1010 hdr->reserved = 0;
1011 hdr->ch_id = id;
1012 hdr->pkt_len = 0;
1013 hdr->pad_len = 0;
1014
1015 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001016 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001017
1018 DBG("%s: closed ch %d\n", __func__, id);
1019 return rc;
1020}
1021
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001022int msm_bam_dmux_is_ch_full(uint32_t id)
1023{
1024 unsigned long flags;
1025 int ret;
1026
1027 if (id >= BAM_DMUX_NUM_CHANNELS)
1028 return -EINVAL;
1029
1030 spin_lock_irqsave(&bam_ch[id].lock, flags);
1031 bam_ch[id].use_wm = 1;
1032 ret = bam_ch[id].num_tx_pkts >= HIGH_WATERMARK;
1033 DBG("%s: ch %d num tx pkts=%d, HWM=%d\n", __func__,
1034 id, bam_ch[id].num_tx_pkts, ret);
1035 if (!bam_ch_is_local_open(id)) {
1036 ret = -ENODEV;
1037 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
1038 }
1039 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
1040
1041 return ret;
1042}
1043
1044int msm_bam_dmux_is_ch_low(uint32_t id)
1045{
Eric Holmberged3ca0a2012-04-09 15:44:58 -06001046 unsigned long flags;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001047 int ret;
1048
1049 if (id >= BAM_DMUX_NUM_CHANNELS)
1050 return -EINVAL;
1051
Eric Holmberged3ca0a2012-04-09 15:44:58 -06001052 spin_lock_irqsave(&bam_ch[id].lock, flags);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001053 bam_ch[id].use_wm = 1;
1054 ret = bam_ch[id].num_tx_pkts <= LOW_WATERMARK;
1055 DBG("%s: ch %d num tx pkts=%d, LWM=%d\n", __func__,
1056 id, bam_ch[id].num_tx_pkts, ret);
1057 if (!bam_ch_is_local_open(id)) {
1058 ret = -ENODEV;
1059 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
1060 }
Eric Holmberged3ca0a2012-04-09 15:44:58 -06001061 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001062
1063 return ret;
1064}
1065
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001066static void rx_switch_to_interrupt_mode(void)
1067{
1068 struct sps_connect cur_rx_conn;
1069 struct sps_iovec iov;
1070 struct rx_pkt_info *info;
1071 int ret;
1072
1073 /*
1074 * Attempt to enable interrupts - if this fails,
1075 * continue polling and we will retry later.
1076 */
Brent Hronik89c96ba2013-08-27 14:34:22 -06001077 ret = bam_ops->sps_get_config_ptr(bam_rx_pipe, &cur_rx_conn);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001078 if (ret) {
1079 pr_err("%s: sps_get_config() failed %d\n", __func__, ret);
1080 goto fail;
1081 }
1082
1083 rx_register_event.options = SPS_O_EOT;
Brent Hronik89c96ba2013-08-27 14:34:22 -06001084 ret = bam_ops->sps_register_event_ptr(bam_rx_pipe, &rx_register_event);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001085 if (ret) {
1086 pr_err("%s: sps_register_event() failed %d\n", __func__, ret);
1087 goto fail;
1088 }
1089
1090 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
1091 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
Brent Hronik89c96ba2013-08-27 14:34:22 -06001092 ret = bam_ops->sps_set_config_ptr(bam_rx_pipe, &cur_rx_conn);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001093 if (ret) {
1094 pr_err("%s: sps_set_config() failed %d\n", __func__, ret);
1095 goto fail;
1096 }
1097 polling_mode = 0;
Brent Hronik096f7d32013-06-28 15:43:08 -06001098 complete_all(&shutdown_completion);
Eric Holmberg006057d2012-01-11 10:10:42 -07001099 release_wakelock();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001100
1101 /* handle any rx packets before interrupt was enabled */
1102 while (bam_connection_is_active && !polling_mode) {
Brent Hronik89c96ba2013-08-27 14:34:22 -06001103 ret = bam_ops->sps_get_iovec_ptr(bam_rx_pipe, &iov);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001104 if (ret) {
1105 pr_err("%s: sps_get_iovec failed %d\n",
1106 __func__, ret);
1107 break;
1108 }
1109 if (iov.addr == 0)
1110 break;
1111
1112 mutex_lock(&bam_rx_pool_mutexlock);
1113 if (unlikely(list_empty(&bam_rx_pool))) {
Eric Holmberg00cf8692012-07-16 14:21:19 -06001114 DMUX_LOG_KERR("%s: have iovec %p but rx pool empty\n",
1115 __func__, (void *)iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001116 mutex_unlock(&bam_rx_pool_mutexlock);
1117 continue;
1118 }
1119 info = list_first_entry(&bam_rx_pool, struct rx_pkt_info,
1120 list_node);
Eric Holmberg00cf8692012-07-16 14:21:19 -06001121 if (info->dma_address != iov.addr) {
1122 DMUX_LOG_KERR("%s: iovec %p != dma %p\n",
1123 __func__,
1124 (void *)iov.addr,
1125 (void *)info->dma_address);
1126 list_for_each_entry(info, &bam_rx_pool, list_node) {
1127 DMUX_LOG_KERR("%s: dma %p\n", __func__,
1128 (void *)info->dma_address);
1129 if (iov.addr == info->dma_address)
1130 break;
1131 }
1132 }
1133 BUG_ON(info->dma_address != iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001134 list_del(&info->list_node);
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001135 --bam_rx_pool_len;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001136 mutex_unlock(&bam_rx_pool_mutexlock);
1137 handle_bam_mux_cmd(&info->work);
1138 }
1139 return;
1140
1141fail:
1142 pr_err("%s: reverting to polling\n", __func__);
Jeff Hugofff43af92012-03-29 17:54:52 -06001143 queue_work_on(0, bam_mux_rx_workqueue, &rx_timer_work);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001144}
1145
Jeff Hugo7c185602013-09-11 17:39:54 -06001146/**
1147 * store_rx_timestamp() - store the current raw time as as a timestamp for when
1148 * the last rx packet was processed
1149 */
1150static void store_rx_timestamp(void)
1151{
1152 last_rx_pkt_timestamp = sched_clock();
1153}
1154
1155/**
1156 * log_rx_timestamp() - Log the stored rx pkt timestamp in a human readable
1157 * format
1158 */
1159static void log_rx_timestamp(void)
1160{
1161 unsigned long long t = last_rx_pkt_timestamp;
1162 unsigned long nanosec_rem;
1163
1164 nanosec_rem = do_div(t, 1000000000U);
1165 BAM_DMUX_LOG("Last rx pkt processed at [%6u.%09lu]\n", (unsigned)t,
1166 nanosec_rem);
1167}
1168
Jeff Hugo949080a2011-08-30 11:58:56 -06001169static void rx_timer_work_func(struct work_struct *work)
1170{
1171 struct sps_iovec iov;
Jeff Hugo949080a2011-08-30 11:58:56 -06001172 struct rx_pkt_info *info;
1173 int inactive_cycles = 0;
1174 int ret;
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001175 u32 buffs_unused, buffs_used;
Jeff Hugo949080a2011-08-30 11:58:56 -06001176
Jeff Hugo7c185602013-09-11 17:39:54 -06001177 BAM_DMUX_LOG("%s: polling start\n", __func__);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001178 while (bam_connection_is_active) { /* timer loop */
Jeff Hugo949080a2011-08-30 11:58:56 -06001179 ++inactive_cycles;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001180 while (bam_connection_is_active) { /* deplete queue loop */
Jeff Hugo7c185602013-09-11 17:39:54 -06001181 if (in_global_reset) {
1182 BAM_DMUX_LOG(
1183 "%s: polling exit, global reset detected\n",
1184 __func__);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001185 return;
Jeff Hugo7c185602013-09-11 17:39:54 -06001186 }
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001187
Brent Hronik89c96ba2013-08-27 14:34:22 -06001188 ret = bam_ops->sps_get_iovec_ptr(bam_rx_pipe, &iov);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001189 if (ret) {
Jeff Hugo7c185602013-09-11 17:39:54 -06001190 DMUX_LOG_KERR("%s: sps_get_iovec failed %d\n",
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001191 __func__, ret);
1192 break;
1193 }
Jeff Hugo949080a2011-08-30 11:58:56 -06001194 if (iov.addr == 0)
1195 break;
Jeff Hugo7c185602013-09-11 17:39:54 -06001196 store_rx_timestamp();
Jeff Hugo949080a2011-08-30 11:58:56 -06001197 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -06001198 mutex_lock(&bam_rx_pool_mutexlock);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001199 if (unlikely(list_empty(&bam_rx_pool))) {
Eric Holmberg00cf8692012-07-16 14:21:19 -06001200 DMUX_LOG_KERR(
1201 "%s: have iovec %p but rx pool empty\n",
1202 __func__, (void *)iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001203 mutex_unlock(&bam_rx_pool_mutexlock);
1204 continue;
1205 }
1206 info = list_first_entry(&bam_rx_pool,
1207 struct rx_pkt_info, list_node);
Eric Holmberg00cf8692012-07-16 14:21:19 -06001208 if (info->dma_address != iov.addr) {
1209 DMUX_LOG_KERR("%s: iovec %p != dma %p\n",
1210 __func__,
1211 (void *)iov.addr,
1212 (void *)info->dma_address);
1213 list_for_each_entry(info, &bam_rx_pool,
1214 list_node) {
1215 DMUX_LOG_KERR("%s: dma %p\n", __func__,
1216 (void *)info->dma_address);
1217 if (iov.addr == info->dma_address)
1218 break;
1219 }
1220 }
1221 BUG_ON(info->dma_address != iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001222 list_del(&info->list_node);
Eric Holmberg00cf8692012-07-16 14:21:19 -06001223 --bam_rx_pool_len;
Jeff Hugoc9749932011-11-02 17:50:40 -06001224 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -06001225 handle_bam_mux_cmd(&info->work);
1226 }
1227
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001228 if (inactive_cycles >= POLLING_INACTIVITY) {
Jeff Hugo7c185602013-09-11 17:39:54 -06001229 BAM_DMUX_LOG("%s: polling exit, no data\n", __func__);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001230 rx_switch_to_interrupt_mode();
1231 break;
Jeff Hugo949080a2011-08-30 11:58:56 -06001232 }
1233
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001234 if (bam_adaptive_timer_enabled) {
1235 usleep_range(rx_timer_interval, rx_timer_interval + 50);
1236
Brent Hronik89c96ba2013-08-27 14:34:22 -06001237 ret = bam_ops->sps_get_unused_desc_num_ptr(bam_rx_pipe,
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001238 &buffs_unused);
1239
1240 if (ret) {
Jeff Hugo7c185602013-09-11 17:39:54 -06001241 DMUX_LOG_KERR(
1242 "%s: error getting num buffers unused after sleep\n",
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001243 __func__);
1244
1245 break;
1246 }
1247
1248 buffs_used = NUM_BUFFERS - buffs_unused;
1249
1250 if (buffs_unused == 0) {
1251 rx_timer_interval = MIN_POLLING_SLEEP;
1252 } else {
1253 if (buffs_used > 0) {
1254 rx_timer_interval =
1255 (2 * NUM_BUFFERS *
1256 rx_timer_interval)/
1257 (3 * buffs_used);
1258 } else {
1259 rx_timer_interval =
1260 MAX_POLLING_SLEEP;
1261 }
1262 }
1263
1264 if (rx_timer_interval > MAX_POLLING_SLEEP)
1265 rx_timer_interval = MAX_POLLING_SLEEP;
1266 else if (rx_timer_interval < MIN_POLLING_SLEEP)
1267 rx_timer_interval = MIN_POLLING_SLEEP;
1268 } else {
1269 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
1270 }
Jeff Hugo949080a2011-08-30 11:58:56 -06001271 }
1272}
1273
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001274static void bam_mux_tx_notify(struct sps_event_notify *notify)
1275{
1276 struct tx_pkt_info *pkt;
1277
1278 DBG("%s: event %d notified\n", __func__, notify->event_id);
1279
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001280 if (in_global_reset)
1281 return;
1282
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001283 switch (notify->event_id) {
1284 case SPS_EVENT_EOT:
1285 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001286 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001287 dma_unmap_single(NULL, pkt->dma_address,
1288 pkt->skb->len,
Brent Hronik89c96ba2013-08-27 14:34:22 -06001289 bam_ops->dma_to);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001290 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001291 dma_unmap_single(NULL, pkt->dma_address,
1292 pkt->len,
Brent Hronik89c96ba2013-08-27 14:34:22 -06001293 bam_ops->dma_to);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001294 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001295 break;
1296 default:
1297 pr_err("%s: recieved unexpected event id %d\n", __func__,
1298 notify->event_id);
1299 }
1300}
1301
Jeff Hugo33dbc002011-08-25 15:52:53 -06001302static void bam_mux_rx_notify(struct sps_event_notify *notify)
1303{
Jeff Hugo949080a2011-08-30 11:58:56 -06001304 int ret;
1305 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -06001306
1307 DBG("%s: event %d notified\n", __func__, notify->event_id);
1308
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001309 if (in_global_reset)
1310 return;
1311
Jeff Hugo33dbc002011-08-25 15:52:53 -06001312 switch (notify->event_id) {
1313 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -06001314 /* attempt to disable interrupts in this pipe */
1315 if (!polling_mode) {
Brent Hronik89c96ba2013-08-27 14:34:22 -06001316 ret = bam_ops->sps_get_config_ptr(bam_rx_pipe,
1317 &cur_rx_conn);
Jeff Hugo949080a2011-08-30 11:58:56 -06001318 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001319 pr_err("%s: sps_get_config() failed %d, interrupts"
1320 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001321 break;
1322 }
Jeff Hugoa9d32ba2011-11-21 14:59:48 -07001323 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
Jeff Hugo949080a2011-08-30 11:58:56 -06001324 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
Brent Hronik89c96ba2013-08-27 14:34:22 -06001325 ret = bam_ops->sps_set_config_ptr(bam_rx_pipe,
1326 &cur_rx_conn);
Jeff Hugo949080a2011-08-30 11:58:56 -06001327 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001328 pr_err("%s: sps_set_config() failed %d, interrupts"
1329 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001330 break;
1331 }
Brent Hronik096f7d32013-06-28 15:43:08 -06001332 INIT_COMPLETION(shutdown_completion);
Eric Holmberg006057d2012-01-11 10:10:42 -07001333 grab_wakelock();
Jeff Hugo949080a2011-08-30 11:58:56 -06001334 polling_mode = 1;
Jeff Hugofff43af92012-03-29 17:54:52 -06001335 /*
1336 * run on core 0 so that netif_rx() in rmnet uses only
1337 * one queue
1338 */
1339 queue_work_on(0, bam_mux_rx_workqueue, &rx_timer_work);
Jeff Hugo949080a2011-08-30 11:58:56 -06001340 }
Jeff Hugo33dbc002011-08-25 15:52:53 -06001341 break;
1342 default:
1343 pr_err("%s: recieved unexpected event id %d\n", __func__,
1344 notify->event_id);
1345 }
1346}
1347
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001348#ifdef CONFIG_DEBUG_FS
1349
1350static int debug_tbl(char *buf, int max)
1351{
1352 int i = 0;
1353 int j;
1354
1355 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
1356 i += scnprintf(buf + i, max - i,
1357 "ch%02d local open=%s remote open=%s\n",
1358 j, bam_ch_is_local_open(j) ? "Y" : "N",
1359 bam_ch_is_remote_open(j) ? "Y" : "N");
1360 }
1361
1362 return i;
1363}
1364
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001365static int debug_ul_pkt_cnt(char *buf, int max)
1366{
1367 struct list_head *p;
1368 unsigned long flags;
1369 int n = 0;
1370
1371 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
1372 __list_for_each(p, &bam_tx_pool) {
1373 ++n;
1374 }
1375 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
1376
1377 return scnprintf(buf, max, "Number of UL packets in flight: %d\n", n);
1378}
1379
1380static int debug_stats(char *buf, int max)
1381{
1382 int i = 0;
1383
1384 i += scnprintf(buf + i, max - i,
Eric Holmberg9fdef262012-02-14 11:46:05 -07001385 "skb read cnt: %u\n"
1386 "skb write cnt: %u\n"
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001387 "skb copy cnt: %u\n"
1388 "skb copy bytes: %u\n"
Eric Holmberg6074aba2012-01-18 17:59:44 -07001389 "sps tx failures: %u\n"
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001390 "sps tx stalls: %u\n"
Eric Holmberg1f1255d2012-02-22 13:37:21 -07001391 "rx queue len: %d\n"
1392 "a2 ack out cnt: %d\n"
1393 "a2 ack in cnt: %d\n"
1394 "a2 pwr cntl in: %d\n",
Eric Holmberg9fdef262012-02-14 11:46:05 -07001395 bam_dmux_read_cnt,
1396 bam_dmux_write_cnt,
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001397 bam_dmux_write_cpy_cnt,
1398 bam_dmux_write_cpy_bytes,
Eric Holmberg6074aba2012-01-18 17:59:44 -07001399 bam_dmux_tx_sps_failure_cnt,
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001400 bam_dmux_tx_stall_cnt,
Eric Holmberg1f1255d2012-02-22 13:37:21 -07001401 bam_rx_pool_len,
1402 atomic_read(&bam_dmux_ack_out_cnt),
1403 atomic_read(&bam_dmux_ack_in_cnt),
1404 atomic_read(&bam_dmux_a2_pwr_cntl_in_cnt)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001405 );
1406
1407 return i;
1408}
1409
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001410#define DEBUG_BUFMAX 4096
1411static char debug_buffer[DEBUG_BUFMAX];
1412
1413static ssize_t debug_read(struct file *file, char __user *buf,
1414 size_t count, loff_t *ppos)
1415{
1416 int (*fill)(char *buf, int max) = file->private_data;
1417 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
1418 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
1419}
1420
1421static int debug_open(struct inode *inode, struct file *file)
1422{
1423 file->private_data = inode->i_private;
1424 return 0;
1425}
1426
1427
1428static const struct file_operations debug_ops = {
1429 .read = debug_read,
1430 .open = debug_open,
1431};
1432
1433static void debug_create(const char *name, mode_t mode,
1434 struct dentry *dent,
1435 int (*fill)(char *buf, int max))
1436{
Eric Holmberge4ac80b2012-01-12 09:21:59 -07001437 struct dentry *file;
1438
1439 file = debugfs_create_file(name, mode, dent, fill, &debug_ops);
1440 if (IS_ERR(file))
1441 pr_err("%s: debugfs create failed %d\n", __func__,
1442 (int)PTR_ERR(file));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001443}
1444
1445#endif
1446
Jeff Hugod98b1082011-10-24 10:30:23 -06001447static void notify_all(int event, unsigned long data)
1448{
1449 int i;
Jeff Hugocb798022012-04-09 14:55:40 -06001450 struct list_head *temp;
1451 struct outside_notify_func *func;
Jeff Hugod98b1082011-10-24 10:30:23 -06001452
Jeff Hugoac8152a2013-04-19 11:05:19 -06001453 BAM_DMUX_LOG("%s: event=%d, data=%lu\n", __func__, event, data);
1454
Jeff Hugod98b1082011-10-24 10:30:23 -06001455 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
Jeff Hugoac8152a2013-04-19 11:05:19 -06001456 if (bam_ch_is_open(i))
Jeff Hugod98b1082011-10-24 10:30:23 -06001457 bam_ch[i].notify(bam_ch[i].priv, event, data);
1458 }
Jeff Hugocb798022012-04-09 14:55:40 -06001459
1460 __list_for_each(temp, &bam_other_notify_funcs) {
1461 func = container_of(temp, struct outside_notify_func,
1462 list_node);
1463 func->notify(func->priv, event, data);
1464 }
Jeff Hugod98b1082011-10-24 10:30:23 -06001465}
1466
1467static void kickoff_ul_wakeup_func(struct work_struct *work)
1468{
1469 read_lock(&ul_wakeup_lock);
1470 if (!bam_is_connected) {
1471 read_unlock(&ul_wakeup_lock);
1472 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -07001473 if (unlikely(in_global_reset == 1))
1474 return;
Jeff Hugod98b1082011-10-24 10:30:23 -06001475 read_lock(&ul_wakeup_lock);
1476 ul_packet_written = 1;
1477 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
1478 }
1479 read_unlock(&ul_wakeup_lock);
1480}
1481
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001482int msm_bam_dmux_kickoff_ul_wakeup(void)
Jeff Hugod98b1082011-10-24 10:30:23 -06001483{
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001484 int is_connected;
1485
1486 read_lock(&ul_wakeup_lock);
1487 ul_packet_written = 1;
1488 is_connected = bam_is_connected;
1489 if (!is_connected)
1490 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
1491 read_unlock(&ul_wakeup_lock);
1492
1493 return is_connected;
Jeff Hugod98b1082011-10-24 10:30:23 -06001494}
1495
Eric Holmberg878923a2012-01-10 14:28:19 -07001496static void power_vote(int vote)
1497{
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301498 BAM_DMUX_LOG("%s: curr=%d, vote=%d\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -07001499 bam_dmux_uplink_vote, vote);
1500
1501 if (bam_dmux_uplink_vote == vote)
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301502 BAM_DMUX_LOG("%s: warning - duplicate power vote\n", __func__);
Eric Holmberg878923a2012-01-10 14:28:19 -07001503
1504 bam_dmux_uplink_vote = vote;
1505 if (vote)
Brent Hronik89c96ba2013-08-27 14:34:22 -06001506 bam_ops->smsm_change_state_ptr(SMSM_APPS_STATE,
1507 0, SMSM_A2_POWER_CONTROL);
Eric Holmberg878923a2012-01-10 14:28:19 -07001508 else
Brent Hronik89c96ba2013-08-27 14:34:22 -06001509 bam_ops->smsm_change_state_ptr(SMSM_APPS_STATE,
1510 SMSM_A2_POWER_CONTROL, 0);
Eric Holmberg878923a2012-01-10 14:28:19 -07001511}
1512
Eric Holmberg454d9da2012-01-12 09:37:14 -07001513/*
1514 * @note: Must be called with ul_wakeup_lock locked.
1515 */
1516static inline void ul_powerdown(void)
1517{
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301518 BAM_DMUX_LOG("%s: powerdown\n", __func__);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001519 verify_tx_queue_is_empty(__func__);
1520
1521 if (a2_pc_disabled) {
1522 wait_for_dfab = 1;
1523 INIT_COMPLETION(dfab_unvote_completion);
1524 release_wakelock();
1525 } else {
1526 wait_for_ack = 1;
1527 INIT_COMPLETION(ul_wakeup_ack_completion);
1528 power_vote(0);
1529 }
1530 bam_is_connected = 0;
1531 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
1532}
1533
1534static inline void ul_powerdown_finish(void)
1535{
1536 if (a2_pc_disabled && wait_for_dfab) {
1537 unvote_dfab();
1538 complete_all(&dfab_unvote_completion);
1539 wait_for_dfab = 0;
1540 }
1541}
1542
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001543/*
1544 * Votes for UL power and returns current power state.
1545 *
1546 * @returns true if currently connected
1547 */
1548int msm_bam_dmux_ul_power_vote(void)
1549{
1550 int is_connected;
1551
1552 read_lock(&ul_wakeup_lock);
1553 atomic_inc(&ul_ondemand_vote);
1554 is_connected = bam_is_connected;
1555 if (!is_connected)
1556 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
1557 read_unlock(&ul_wakeup_lock);
1558
1559 return is_connected;
1560}
1561
1562/*
1563 * Unvotes for UL power.
1564 *
1565 * @returns true if vote count is 0 (UL shutdown possible)
1566 */
1567int msm_bam_dmux_ul_power_unvote(void)
1568{
1569 int vote;
1570
1571 read_lock(&ul_wakeup_lock);
1572 vote = atomic_dec_return(&ul_ondemand_vote);
1573 if (unlikely(vote) < 0)
1574 DMUX_LOG_KERR("%s: invalid power vote %d\n", __func__, vote);
1575 read_unlock(&ul_wakeup_lock);
1576
1577 return vote == 0;
1578}
1579
Jeff Hugocb798022012-04-09 14:55:40 -06001580int msm_bam_dmux_reg_notify(void *priv,
1581 void (*notify)(void *priv, int event_type,
1582 unsigned long data))
1583{
1584 struct outside_notify_func *func;
1585
1586 if (!notify)
1587 return -EINVAL;
1588
1589 func = kmalloc(sizeof(struct outside_notify_func), GFP_KERNEL);
1590 if (!func)
1591 return -ENOMEM;
1592
1593 func->notify = notify;
1594 func->priv = priv;
1595 list_add(&func->list_node, &bam_other_notify_funcs);
1596
1597 return 0;
1598}
1599
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001600static void ul_timeout(struct work_struct *work)
1601{
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001602 unsigned long flags;
1603 int ret;
1604
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001605 if (in_global_reset)
1606 return;
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001607 ret = write_trylock_irqsave(&ul_wakeup_lock, flags);
1608 if (!ret) { /* failed to grab lock, reschedule and bail */
1609 schedule_delayed_work(&ul_timeout_work,
1610 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1611 return;
1612 }
Eric Holmberg454d9da2012-01-12 09:37:14 -07001613 if (bam_is_connected) {
Eric Holmberg6074aba2012-01-18 17:59:44 -07001614 if (!ul_packet_written) {
1615 spin_lock(&bam_tx_pool_spinlock);
1616 if (!list_empty(&bam_tx_pool)) {
1617 struct tx_pkt_info *info;
1618
1619 info = list_first_entry(&bam_tx_pool,
1620 struct tx_pkt_info, list_node);
1621 DMUX_LOG_KERR("%s: UL delayed ts=%u.%09lu\n",
1622 __func__, info->ts_sec, info->ts_nsec);
1623 DBG_INC_TX_STALL_CNT();
1624 ul_packet_written = 1;
1625 }
1626 spin_unlock(&bam_tx_pool_spinlock);
1627 }
1628
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001629 if (ul_packet_written || atomic_read(&ul_ondemand_vote)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301630 BAM_DMUX_LOG("%s: pkt written %d\n",
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001631 __func__, ul_packet_written);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001632 ul_packet_written = 0;
1633 schedule_delayed_work(&ul_timeout_work,
1634 msecs_to_jiffies(UL_TIMEOUT_DELAY));
Eric Holmberg006057d2012-01-11 10:10:42 -07001635 } else {
Eric Holmberg454d9da2012-01-12 09:37:14 -07001636 ul_powerdown();
Eric Holmberg006057d2012-01-11 10:10:42 -07001637 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001638 }
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001639 write_unlock_irqrestore(&ul_wakeup_lock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001640 ul_powerdown_finish();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001641}
Jeff Hugo4838f412012-01-20 11:19:37 -07001642
1643static int ssrestart_check(void)
1644{
Jeff Hugob8156d72013-06-04 12:51:10 -06001645 int ret = 0;
1646
Eric Holmberg7614a7f2013-07-29 15:47:12 -06001647 if (in_global_reset) {
1648 DMUX_LOG_KERR("%s: modem timeout: already in SSR\n",
1649 __func__);
1650 return 1;
1651 }
1652
Jeff Hugob8156d72013-06-04 12:51:10 -06001653 DMUX_LOG_KERR("%s: modem timeout: BAM DMUX disabled for SSR\n",
1654 __func__);
Eric Holmberg90285e22012-02-22 12:33:05 -07001655 in_global_reset = 1;
Jeff Hugob8156d72013-06-04 12:51:10 -06001656 ret = subsystem_restart("modem");
1657 if (ret == -ENODEV)
1658 panic("modem subsystem restart failed\n");
Eric Holmberg90285e22012-02-22 12:33:05 -07001659 return 1;
Jeff Hugo4838f412012-01-20 11:19:37 -07001660}
1661
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001662static void ul_wakeup(void)
1663{
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001664 int ret;
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001665 int do_vote_dfab = 0;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001666
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001667 mutex_lock(&wakeup_lock);
1668 if (bam_is_connected) { /* bam got connected before lock grabbed */
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301669 BAM_DMUX_LOG("%s Already awake\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001670 mutex_unlock(&wakeup_lock);
1671 return;
1672 }
Eric Holmberg878923a2012-01-10 14:28:19 -07001673
Jeff Hugoc2696142012-05-03 11:42:13 -06001674 /*
Jeff Hugof5001732012-08-27 13:19:09 -06001675 * if this gets hit, that means restart_notifier_cb() has started
1676 * but probably not finished, thus we know SSR has happened, but
1677 * haven't been able to send that info to our clients yet.
1678 * in that case, abort the ul_wakeup() so that we don't undo any
1679 * work restart_notifier_cb() has done. The clients will be notified
1680 * shortly. No cleanup necessary (reschedule the wakeup) as our and
1681 * their SSR handling will cover it
1682 */
1683 if (unlikely(in_global_reset == 1)) {
1684 mutex_unlock(&wakeup_lock);
1685 return;
1686 }
1687
1688 /*
Jeff Hugoc2696142012-05-03 11:42:13 -06001689 * if someone is voting for UL before bam is inited (modem up first
1690 * time), set flag for init to kickoff ul wakeup once bam is inited
1691 */
1692 mutex_lock(&delayed_ul_vote_lock);
1693 if (unlikely(!bam_mux_initialized)) {
1694 need_delayed_ul_vote = 1;
1695 mutex_unlock(&delayed_ul_vote_lock);
1696 mutex_unlock(&wakeup_lock);
1697 return;
1698 }
1699 mutex_unlock(&delayed_ul_vote_lock);
1700
Eric Holmberg006057d2012-01-11 10:10:42 -07001701 if (a2_pc_disabled) {
1702 /*
1703 * don't grab the wakelock the first time because it is
1704 * already grabbed when a2 powers on
1705 */
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001706 if (likely(a2_pc_disabled_wakelock_skipped)) {
Eric Holmberg006057d2012-01-11 10:10:42 -07001707 grab_wakelock();
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001708 do_vote_dfab = 1; /* vote must occur after wait */
1709 } else {
Jeff Hugo583a6da2012-02-03 11:37:30 -07001710 a2_pc_disabled_wakelock_skipped = 1;
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001711 }
Eric Holmberg006057d2012-01-11 10:10:42 -07001712 if (wait_for_dfab) {
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001713 ret = wait_for_completion_timeout(
Eric Holmberg006057d2012-01-11 10:10:42 -07001714 &dfab_unvote_completion, HZ);
1715 BUG_ON(ret == 0);
1716 }
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001717 if (likely(do_vote_dfab))
1718 vote_dfab();
Eric Holmberg006057d2012-01-11 10:10:42 -07001719 schedule_delayed_work(&ul_timeout_work,
1720 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1721 bam_is_connected = 1;
1722 mutex_unlock(&wakeup_lock);
1723 return;
1724 }
1725
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001726 /*
1727 * must wait for the previous power down request to have been acked
1728 * chances are it already came in and this will just fall through
1729 * instead of waiting
1730 */
1731 if (wait_for_ack) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301732 BAM_DMUX_LOG("%s waiting for previous ack\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001733 ret = wait_for_completion_timeout(
Jeff Hugo1f317392013-07-24 16:28:52 -06001734 &ul_wakeup_ack_completion,
1735 msecs_to_jiffies(UL_WAKEUP_TIMEOUT_MS));
Eric Holmberg006057d2012-01-11 10:10:42 -07001736 wait_for_ack = 0;
Jeff Hugo4838f412012-01-20 11:19:37 -07001737 if (unlikely(ret == 0) && ssrestart_check()) {
1738 mutex_unlock(&wakeup_lock);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301739 BAM_DMUX_LOG("%s timeout previous ack\n", __func__);
Jeff Hugo4838f412012-01-20 11:19:37 -07001740 return;
1741 }
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001742 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001743 INIT_COMPLETION(ul_wakeup_ack_completion);
Eric Holmberg878923a2012-01-10 14:28:19 -07001744 power_vote(1);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301745 BAM_DMUX_LOG("%s waiting for wakeup ack\n", __func__);
Jeff Hugo1f317392013-07-24 16:28:52 -06001746 ret = wait_for_completion_timeout(&ul_wakeup_ack_completion,
1747 msecs_to_jiffies(UL_WAKEUP_TIMEOUT_MS));
Jeff Hugo4838f412012-01-20 11:19:37 -07001748 if (unlikely(ret == 0) && ssrestart_check()) {
1749 mutex_unlock(&wakeup_lock);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301750 BAM_DMUX_LOG("%s timeout wakeup ack\n", __func__);
Jeff Hugo4838f412012-01-20 11:19:37 -07001751 return;
1752 }
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301753 BAM_DMUX_LOG("%s waiting completion\n", __func__);
Jeff Hugo1f317392013-07-24 16:28:52 -06001754 ret = wait_for_completion_timeout(&bam_connection_completion,
1755 msecs_to_jiffies(UL_WAKEUP_TIMEOUT_MS));
Jeff Hugo4838f412012-01-20 11:19:37 -07001756 if (unlikely(ret == 0) && ssrestart_check()) {
1757 mutex_unlock(&wakeup_lock);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301758 BAM_DMUX_LOG("%s timeout power on\n", __func__);
Jeff Hugo4838f412012-01-20 11:19:37 -07001759 return;
1760 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001761
1762 bam_is_connected = 1;
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301763 BAM_DMUX_LOG("%s complete\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001764 schedule_delayed_work(&ul_timeout_work,
1765 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1766 mutex_unlock(&wakeup_lock);
1767}
1768
1769static void reconnect_to_bam(void)
1770{
1771 int i;
1772
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001773 in_global_reset = 0;
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001774 in_ssr = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001775 vote_dfab();
Jeff Hugo18792a32012-06-20 15:25:55 -06001776 if (!power_management_only_mode) {
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001777 if (ssr_skipped_disconnect) {
1778 /* delayed to here to prevent bus stall */
Brent Hronik89c96ba2013-08-27 14:34:22 -06001779 bam_ops->sps_disconnect_ptr(bam_tx_pipe);
1780 bam_ops->sps_disconnect_ptr(bam_rx_pipe);
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001781 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1782 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
1783 }
1784 ssr_skipped_disconnect = 0;
Brent Hronik89c96ba2013-08-27 14:34:22 -06001785 i = bam_ops->sps_device_reset_ptr(a2_device_handle);
Jeff Hugo18792a32012-06-20 15:25:55 -06001786 if (i)
1787 pr_err("%s: device reset failed rc = %d\n", __func__,
1788 i);
Brent Hronik89c96ba2013-08-27 14:34:22 -06001789 i = bam_ops->sps_connect_ptr(bam_tx_pipe, &tx_connection);
Jeff Hugo18792a32012-06-20 15:25:55 -06001790 if (i)
1791 pr_err("%s: tx connection failed rc = %d\n", __func__,
1792 i);
Brent Hronik89c96ba2013-08-27 14:34:22 -06001793 i = bam_ops->sps_connect_ptr(bam_rx_pipe, &rx_connection);
Jeff Hugo18792a32012-06-20 15:25:55 -06001794 if (i)
1795 pr_err("%s: rx connection failed rc = %d\n", __func__,
1796 i);
Brent Hronik89c96ba2013-08-27 14:34:22 -06001797 i = bam_ops->sps_register_event_ptr(bam_tx_pipe,
1798 &tx_register_event);
Jeff Hugo18792a32012-06-20 15:25:55 -06001799 if (i)
1800 pr_err("%s: tx event reg failed rc = %d\n", __func__,
1801 i);
Brent Hronik89c96ba2013-08-27 14:34:22 -06001802 i = bam_ops->sps_register_event_ptr(bam_rx_pipe,
1803 &rx_register_event);
Jeff Hugo18792a32012-06-20 15:25:55 -06001804 if (i)
1805 pr_err("%s: rx event reg failed rc = %d\n", __func__,
1806 i);
1807 }
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001808
1809 bam_connection_is_active = 1;
1810
1811 if (polling_mode)
1812 rx_switch_to_interrupt_mode();
1813
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001814 toggle_apps_ack();
1815 complete_all(&bam_connection_completion);
Jeff Hugo18792a32012-06-20 15:25:55 -06001816 if (!power_management_only_mode)
1817 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001818}
1819
1820static void disconnect_to_bam(void)
1821{
1822 struct list_head *node;
1823 struct rx_pkt_info *info;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001824 unsigned long flags;
Brent Hronik096f7d32013-06-28 15:43:08 -06001825 unsigned long time_remaining;
1826
Eric Holmberg7614a7f2013-07-29 15:47:12 -06001827 if (!in_global_reset) {
1828 time_remaining = wait_for_completion_timeout(
1829 &shutdown_completion,
1830 msecs_to_jiffies(SHUTDOWN_TIMEOUT_MS));
1831 if (time_remaining == 0) {
1832 DMUX_LOG_KERR("%s: shutdown completion timed out\n",
1833 __func__);
Jeff Hugo7c185602013-09-11 17:39:54 -06001834 log_rx_timestamp();
Eric Holmberg7614a7f2013-07-29 15:47:12 -06001835 ssrestart_check();
1836 }
Brent Hronik096f7d32013-06-28 15:43:08 -06001837 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001838
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001839 bam_connection_is_active = 0;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001840
1841 /* handle disconnect during active UL */
1842 write_lock_irqsave(&ul_wakeup_lock, flags);
1843 if (bam_is_connected) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301844 BAM_DMUX_LOG("%s: UL active - forcing powerdown\n", __func__);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001845 ul_powerdown();
1846 }
1847 write_unlock_irqrestore(&ul_wakeup_lock, flags);
1848 ul_powerdown_finish();
1849
1850 /* tear down BAM connection */
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001851 INIT_COMPLETION(bam_connection_completion);
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001852
1853 /* in_ssr documentation/assumptions found in restart_notifier_cb */
Jeff Hugo18792a32012-06-20 15:25:55 -06001854 if (!power_management_only_mode) {
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001855 if (likely(!in_ssr)) {
Jeff Hugof7ae7a62013-04-19 11:18:32 -06001856 BAM_DMUX_LOG("%s: disconnect tx\n", __func__);
Brent Hronik89c96ba2013-08-27 14:34:22 -06001857 bam_ops->sps_disconnect_ptr(bam_tx_pipe);
Jeff Hugof7ae7a62013-04-19 11:18:32 -06001858 BAM_DMUX_LOG("%s: disconnect rx\n", __func__);
Brent Hronik89c96ba2013-08-27 14:34:22 -06001859 bam_ops->sps_disconnect_ptr(bam_rx_pipe);
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001860 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1861 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
Jeff Hugof7ae7a62013-04-19 11:18:32 -06001862 BAM_DMUX_LOG("%s: device reset\n", __func__);
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001863 sps_device_reset(a2_device_handle);
1864 } else {
1865 ssr_skipped_disconnect = 1;
1866 }
Jeff Hugo18792a32012-06-20 15:25:55 -06001867 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001868 unvote_dfab();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001869
1870 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001871 while (!list_empty(&bam_rx_pool)) {
1872 node = bam_rx_pool.next;
1873 list_del(node);
1874 info = container_of(node, struct rx_pkt_info, list_node);
1875 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
Brent Hronik89c96ba2013-08-27 14:34:22 -06001876 bam_ops->dma_from);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001877 dev_kfree_skb_any(info->skb);
1878 kfree(info);
1879 }
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001880 bam_rx_pool_len = 0;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001881 mutex_unlock(&bam_rx_pool_mutexlock);
Eric Holmberg878923a2012-01-10 14:28:19 -07001882
Jeff Hugo0b13a352012-03-17 23:18:30 -06001883 if (disconnect_ack)
1884 toggle_apps_ack();
1885
Eric Holmberg878923a2012-01-10 14:28:19 -07001886 verify_tx_queue_is_empty(__func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001887}
1888
1889static void vote_dfab(void)
1890{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001891 int rc;
1892
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301893 BAM_DMUX_LOG("%s\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001894 mutex_lock(&dfab_status_lock);
1895 if (dfab_is_on) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301896 BAM_DMUX_LOG("%s: dfab is already on\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001897 mutex_unlock(&dfab_status_lock);
1898 return;
1899 }
Jeff Hugod0befde2012-08-09 15:32:49 -06001900 if (dfab_clk) {
1901 rc = clk_prepare_enable(dfab_clk);
1902 if (rc)
1903 DMUX_LOG_KERR("bam_dmux vote for dfab failed rc = %d\n",
1904 rc);
1905 }
1906 if (xo_clk) {
1907 rc = clk_prepare_enable(xo_clk);
1908 if (rc)
1909 DMUX_LOG_KERR("bam_dmux vote for xo failed rc = %d\n",
1910 rc);
1911 }
Eric Holmberg006057d2012-01-11 10:10:42 -07001912 dfab_is_on = 1;
1913 mutex_unlock(&dfab_status_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001914}
1915
1916static void unvote_dfab(void)
1917{
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301918 BAM_DMUX_LOG("%s\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001919 mutex_lock(&dfab_status_lock);
1920 if (!dfab_is_on) {
1921 DMUX_LOG_KERR("%s: dfab is already off\n", __func__);
1922 dump_stack();
1923 mutex_unlock(&dfab_status_lock);
1924 return;
1925 }
Jeff Hugod0befde2012-08-09 15:32:49 -06001926 if (dfab_clk)
1927 clk_disable_unprepare(dfab_clk);
1928 if (xo_clk)
1929 clk_disable_unprepare(xo_clk);
Eric Holmberg006057d2012-01-11 10:10:42 -07001930 dfab_is_on = 0;
1931 mutex_unlock(&dfab_status_lock);
1932}
1933
1934/* reference counting wrapper around wakelock */
1935static void grab_wakelock(void)
1936{
1937 unsigned long flags;
1938
1939 spin_lock_irqsave(&wakelock_reference_lock, flags);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301940 BAM_DMUX_LOG("%s: ref count = %d\n", __func__,
Eric Holmberg006057d2012-01-11 10:10:42 -07001941 wakelock_reference_count);
1942 if (wakelock_reference_count == 0)
1943 wake_lock(&bam_wakelock);
1944 ++wakelock_reference_count;
1945 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1946}
1947
1948static void release_wakelock(void)
1949{
1950 unsigned long flags;
1951
1952 spin_lock_irqsave(&wakelock_reference_lock, flags);
1953 if (wakelock_reference_count == 0) {
1954 DMUX_LOG_KERR("%s: bam_dmux wakelock not locked\n", __func__);
1955 dump_stack();
1956 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1957 return;
1958 }
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301959 BAM_DMUX_LOG("%s: ref count = %d\n", __func__,
Eric Holmberg006057d2012-01-11 10:10:42 -07001960 wakelock_reference_count);
1961 --wakelock_reference_count;
1962 if (wakelock_reference_count == 0)
1963 wake_unlock(&bam_wakelock);
1964 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001965}
1966
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001967static int restart_notifier_cb(struct notifier_block *this,
1968 unsigned long code,
1969 void *data)
1970{
1971 int i;
1972 struct list_head *node;
1973 struct tx_pkt_info *info;
1974 int temp_remote_status;
Jeff Hugo626303bf2011-11-21 11:43:28 -07001975 unsigned long flags;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001976
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001977 /*
1978 * Bam_dmux counts on the fact that the BEFORE_SHUTDOWN level of
1979 * notifications are guarenteed to execute before the AFTER_SHUTDOWN
1980 * level of notifications, and that BEFORE_SHUTDOWN always occurs in
1981 * all SSR events, no matter what triggered the SSR. Also, bam_dmux
1982 * assumes that SMD does its SSR processing in the AFTER_SHUTDOWN level
1983 * thus bam_dmux is guarenteed to detect SSR before SMD, since the
1984 * callbacks for all the drivers within the AFTER_SHUTDOWN level could
1985 * occur in any order. Bam_dmux uses this knowledge to skip accessing
1986 * the bam hardware when disconnect_to_bam() is triggered by SMD's SSR
1987 * processing. We do not wat to access the bam hardware during SSR
1988 * because a watchdog crash from a bus stall would likely occur.
1989 */
Jeff Hugo199294b2013-02-25 13:46:56 -07001990 if (code == SUBSYS_BEFORE_SHUTDOWN) {
Arun Kumar Neelakantame7c0d622013-10-11 14:34:02 +05301991 BAM_DMUX_LOG("%s: begin\n", __func__);
Jeff Hugo199294b2013-02-25 13:46:56 -07001992 in_global_reset = 1;
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001993 in_ssr = 1;
Arun Kumar Neelakantame7c0d622013-10-11 14:34:02 +05301994 /* wait till all bam_dmux writes completes */
1995 synchronize_srcu(&bam_dmux_srcu);
1996 BAM_DMUX_LOG("%s: ssr signaling complete\n", __func__);
Jeff Hugo199294b2013-02-25 13:46:56 -07001997 flush_workqueue(bam_mux_rx_workqueue);
1998 }
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001999 if (code != SUBSYS_AFTER_SHUTDOWN)
2000 return NOTIFY_DONE;
2001
Eric Holmberg454d9da2012-01-12 09:37:14 -07002002 /* Handle uplink Powerdown */
2003 write_lock_irqsave(&ul_wakeup_lock, flags);
2004 if (bam_is_connected) {
2005 ul_powerdown();
2006 wait_for_ack = 0;
2007 }
Jeff Hugo4838f412012-01-20 11:19:37 -07002008 /*
2009 * if modem crash during ul_wakeup(), power_vote is 1, needs to be
2010 * reset to 0. harmless if bam_is_connected check above passes
2011 */
2012 power_vote(0);
Eric Holmberg454d9da2012-01-12 09:37:14 -07002013 write_unlock_irqrestore(&ul_wakeup_lock, flags);
2014 ul_powerdown_finish();
Eric Holmberg006057d2012-01-11 10:10:42 -07002015 a2_pc_disabled = 0;
Jeff Hugo583a6da2012-02-03 11:37:30 -07002016 a2_pc_disabled_wakelock_skipped = 0;
Jeff Hugof62029d2012-07-17 13:39:53 -06002017 disconnect_ack = 1;
Eric Holmberg454d9da2012-01-12 09:37:14 -07002018
2019 /* Cleanup Channel States */
Eric Holmberga623da82012-07-12 09:37:09 -06002020 mutex_lock(&bam_pdev_mutexlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002021 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
2022 temp_remote_status = bam_ch_is_remote_open(i);
2023 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07002024 bam_ch[i].num_tx_pkts = 0;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002025 if (bam_ch_is_local_open(i))
2026 bam_ch[i].status |= BAM_CH_IN_RESET;
2027 if (temp_remote_status) {
2028 platform_device_unregister(bam_ch[i].pdev);
2029 bam_ch[i].pdev = platform_device_alloc(
2030 bam_ch[i].name, 2);
2031 }
2032 }
Eric Holmberga623da82012-07-12 09:37:09 -06002033 mutex_unlock(&bam_pdev_mutexlock);
Eric Holmberg454d9da2012-01-12 09:37:14 -07002034
2035 /* Cleanup pending UL data */
Jeff Hugo626303bf2011-11-21 11:43:28 -07002036 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002037 while (!list_empty(&bam_tx_pool)) {
2038 node = bam_tx_pool.next;
2039 list_del(node);
2040 info = container_of(node, struct tx_pkt_info,
2041 list_node);
2042 if (!info->is_cmd) {
2043 dma_unmap_single(NULL, info->dma_address,
2044 info->skb->len,
Brent Hronik89c96ba2013-08-27 14:34:22 -06002045 bam_ops->dma_to);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002046 dev_kfree_skb_any(info->skb);
2047 } else {
2048 dma_unmap_single(NULL, info->dma_address,
2049 info->len,
Brent Hronik89c96ba2013-08-27 14:34:22 -06002050 bam_ops->dma_to);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002051 kfree(info->skb);
2052 }
2053 kfree(info);
2054 }
Jeff Hugo626303bf2011-11-21 11:43:28 -07002055 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07002056
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302057 BAM_DMUX_LOG("%s: complete\n", __func__);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002058 return NOTIFY_DONE;
2059}
2060
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002061static int bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002062{
2063 u32 h;
2064 dma_addr_t dma_addr;
2065 int ret;
2066 void *a2_virt_addr;
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002067 int skip_iounmap = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002068
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002069 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002070 /* init BAM */
Jeff Hugo3910ee12012-08-21 14:08:20 -06002071 a2_virt_addr = ioremap_nocache((unsigned long)(a2_phys_base),
2072 a2_phys_size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002073 if (!a2_virt_addr) {
2074 pr_err("%s: ioremap failed\n", __func__);
2075 ret = -ENOMEM;
Jeff Hugo994a92d2012-01-05 13:25:21 -07002076 goto ioremap_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002077 }
Jeff Hugo3910ee12012-08-21 14:08:20 -06002078 a2_props.phys_addr = (u32)(a2_phys_base);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002079 a2_props.virt_addr = a2_virt_addr;
Jeff Hugo3910ee12012-08-21 14:08:20 -06002080 a2_props.virt_size = a2_phys_size;
2081 a2_props.irq = a2_bam_irq;
Jeff Hugo927cba62011-11-11 11:49:52 -07002082 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002083 a2_props.num_pipes = A2_NUM_PIPES;
2084 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
Jeff Hugo75913c82011-12-05 15:59:01 -07002085 if (cpu_is_msm9615())
2086 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002087 /* need to free on tear down */
Brent Hronik89c96ba2013-08-27 14:34:22 -06002088 ret = bam_ops->sps_register_bam_device_ptr(&a2_props, &h);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002089 if (ret < 0) {
2090 pr_err("%s: register bam error %d\n", __func__, ret);
2091 goto register_bam_failed;
2092 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002093 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002094
Brent Hronik89c96ba2013-08-27 14:34:22 -06002095 bam_tx_pipe = bam_ops->sps_alloc_endpoint_ptr();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002096 if (bam_tx_pipe == NULL) {
2097 pr_err("%s: tx alloc endpoint failed\n", __func__);
2098 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002099 goto tx_alloc_endpoint_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002100 }
Brent Hronik89c96ba2013-08-27 14:34:22 -06002101 ret = bam_ops->sps_get_config_ptr(bam_tx_pipe, &tx_connection);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002102 if (ret) {
2103 pr_err("%s: tx get config failed %d\n", __func__, ret);
2104 goto tx_get_config_failed;
2105 }
2106
2107 tx_connection.source = SPS_DEV_HANDLE_MEM;
2108 tx_connection.src_pipe_index = 0;
2109 tx_connection.destination = h;
2110 tx_connection.dest_pipe_index = 4;
2111 tx_connection.mode = SPS_MODE_DEST;
2112 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
2113 tx_desc_mem_buf.size = 0x800; /* 2k */
2114 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
2115 &dma_addr, 0);
2116 if (tx_desc_mem_buf.base == NULL) {
2117 pr_err("%s: tx memory alloc failed\n", __func__);
2118 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002119 goto tx_get_config_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002120 }
2121 tx_desc_mem_buf.phys_base = dma_addr;
2122 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
2123 tx_connection.desc = tx_desc_mem_buf;
2124 tx_connection.event_thresh = 0x10;
2125
Brent Hronik89c96ba2013-08-27 14:34:22 -06002126 ret = bam_ops->sps_connect_ptr(bam_tx_pipe, &tx_connection);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002127 if (ret < 0) {
2128 pr_err("%s: tx connect error %d\n", __func__, ret);
2129 goto tx_connect_failed;
2130 }
2131
Brent Hronik89c96ba2013-08-27 14:34:22 -06002132 bam_rx_pipe = bam_ops->sps_alloc_endpoint_ptr();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002133 if (bam_rx_pipe == NULL) {
2134 pr_err("%s: rx alloc endpoint failed\n", __func__);
2135 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002136 goto rx_alloc_endpoint_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002137 }
Brent Hronik89c96ba2013-08-27 14:34:22 -06002138 ret = bam_ops->sps_get_config_ptr(bam_rx_pipe, &rx_connection);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002139 if (ret) {
2140 pr_err("%s: rx get config failed %d\n", __func__, ret);
2141 goto rx_get_config_failed;
2142 }
2143
2144 rx_connection.source = h;
2145 rx_connection.src_pipe_index = 5;
2146 rx_connection.destination = SPS_DEV_HANDLE_MEM;
2147 rx_connection.dest_pipe_index = 1;
2148 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06002149 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
2150 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002151 rx_desc_mem_buf.size = 0x800; /* 2k */
2152 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
2153 &dma_addr, 0);
2154 if (rx_desc_mem_buf.base == NULL) {
2155 pr_err("%s: rx memory alloc failed\n", __func__);
2156 ret = -ENOMEM;
2157 goto rx_mem_failed;
2158 }
2159 rx_desc_mem_buf.phys_base = dma_addr;
2160 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
2161 rx_connection.desc = rx_desc_mem_buf;
2162 rx_connection.event_thresh = 0x10;
2163
Brent Hronik89c96ba2013-08-27 14:34:22 -06002164 ret = bam_ops->sps_connect_ptr(bam_rx_pipe, &rx_connection);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002165 if (ret < 0) {
2166 pr_err("%s: rx connect error %d\n", __func__, ret);
2167 goto rx_connect_failed;
2168 }
2169
2170 tx_register_event.options = SPS_O_EOT;
2171 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
2172 tx_register_event.xfer_done = NULL;
2173 tx_register_event.callback = bam_mux_tx_notify;
2174 tx_register_event.user = NULL;
Brent Hronik89c96ba2013-08-27 14:34:22 -06002175 ret = bam_ops->sps_register_event_ptr(bam_tx_pipe, &tx_register_event);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002176 if (ret < 0) {
2177 pr_err("%s: tx register event error %d\n", __func__, ret);
2178 goto rx_event_reg_failed;
2179 }
2180
Jeff Hugo33dbc002011-08-25 15:52:53 -06002181 rx_register_event.options = SPS_O_EOT;
2182 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
2183 rx_register_event.xfer_done = NULL;
2184 rx_register_event.callback = bam_mux_rx_notify;
2185 rx_register_event.user = NULL;
Brent Hronik89c96ba2013-08-27 14:34:22 -06002186 ret = bam_ops->sps_register_event_ptr(bam_rx_pipe, &rx_register_event);
Jeff Hugo33dbc002011-08-25 15:52:53 -06002187 if (ret < 0) {
2188 pr_err("%s: tx register event error %d\n", __func__, ret);
2189 goto rx_event_reg_failed;
2190 }
2191
Jeff Hugoc2696142012-05-03 11:42:13 -06002192 mutex_lock(&delayed_ul_vote_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002193 bam_mux_initialized = 1;
Jeff Hugoc2696142012-05-03 11:42:13 -06002194 if (need_delayed_ul_vote) {
2195 need_delayed_ul_vote = 0;
2196 msm_bam_dmux_kickoff_ul_wakeup();
2197 }
2198 mutex_unlock(&delayed_ul_vote_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002199 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002200 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002201 complete_all(&bam_connection_completion);
Jeff Hugo2fb555e2012-03-14 16:33:47 -06002202 queue_rx();
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002203 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002204
2205rx_event_reg_failed:
Brent Hronik89c96ba2013-08-27 14:34:22 -06002206 bam_ops->sps_disconnect_ptr(bam_rx_pipe);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002207rx_connect_failed:
2208 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
2209 rx_desc_mem_buf.phys_base);
2210rx_mem_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002211rx_get_config_failed:
Brent Hronik89c96ba2013-08-27 14:34:22 -06002212 bam_ops->sps_free_endpoint_ptr(bam_rx_pipe);
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002213rx_alloc_endpoint_failed:
Brent Hronik89c96ba2013-08-27 14:34:22 -06002214 bam_ops->sps_disconnect_ptr(bam_tx_pipe);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002215tx_connect_failed:
2216 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
2217 tx_desc_mem_buf.phys_base);
2218tx_get_config_failed:
Brent Hronik89c96ba2013-08-27 14:34:22 -06002219 bam_ops->sps_free_endpoint_ptr(bam_tx_pipe);
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002220tx_alloc_endpoint_failed:
Brent Hronik89c96ba2013-08-27 14:34:22 -06002221 bam_ops->sps_deregister_bam_device_ptr(h);
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002222 /*
2223 * sps_deregister_bam_device() calls iounmap. calling iounmap on the
2224 * same handle below will cause a crash, so skip it if we've freed
2225 * the handle here.
2226 */
2227 skip_iounmap = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002228register_bam_failed:
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002229 if (!skip_iounmap)
2230 iounmap(a2_virt_addr);
Jeff Hugo994a92d2012-01-05 13:25:21 -07002231ioremap_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002232 /*destroy_workqueue(bam_mux_workqueue);*/
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002233 return ret;
2234}
2235
2236static int bam_init_fallback(void)
2237{
2238 u32 h;
2239 int ret;
2240 void *a2_virt_addr;
2241
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002242 /* init BAM */
Jeff Hugo3910ee12012-08-21 14:08:20 -06002243 a2_virt_addr = ioremap_nocache((unsigned long)(a2_phys_base),
2244 a2_phys_size);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002245 if (!a2_virt_addr) {
2246 pr_err("%s: ioremap failed\n", __func__);
2247 ret = -ENOMEM;
2248 goto ioremap_failed;
2249 }
Jeff Hugo3910ee12012-08-21 14:08:20 -06002250 a2_props.phys_addr = (u32)(a2_phys_base);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002251 a2_props.virt_addr = a2_virt_addr;
Jeff Hugo3910ee12012-08-21 14:08:20 -06002252 a2_props.virt_size = a2_phys_size;
2253 a2_props.irq = a2_bam_irq;
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002254 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
2255 a2_props.num_pipes = A2_NUM_PIPES;
2256 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
2257 if (cpu_is_msm9615())
2258 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
Brent Hronik89c96ba2013-08-27 14:34:22 -06002259 ret = bam_ops->sps_register_bam_device_ptr(&a2_props, &h);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002260 if (ret < 0) {
2261 pr_err("%s: register bam error %d\n", __func__, ret);
2262 goto register_bam_failed;
2263 }
2264 a2_device_handle = h;
Jeff Hugoc2696142012-05-03 11:42:13 -06002265
2266 mutex_lock(&delayed_ul_vote_lock);
2267 bam_mux_initialized = 1;
2268 if (need_delayed_ul_vote) {
2269 need_delayed_ul_vote = 0;
2270 msm_bam_dmux_kickoff_ul_wakeup();
2271 }
2272 mutex_unlock(&delayed_ul_vote_lock);
Jeff Hugo2bec9772012-04-05 12:25:16 -06002273 toggle_apps_ack();
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002274
Jeff Hugo18792a32012-06-20 15:25:55 -06002275 power_management_only_mode = 1;
2276 bam_connection_is_active = 1;
2277 complete_all(&bam_connection_completion);
2278
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002279 return 0;
2280
2281register_bam_failed:
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002282 iounmap(a2_virt_addr);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002283ioremap_failed:
2284 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002285}
Jeff Hugoade1f842011-08-03 15:53:59 -06002286
Jeff Hugoa670b762012-03-15 15:58:28 -06002287static void msm9615_bam_init(void)
Eric Holmberg604ab252012-01-15 00:01:18 -07002288{
2289 int ret = 0;
2290
2291 ret = bam_init();
2292 if (ret) {
2293 ret = bam_init_fallback();
2294 if (ret)
2295 pr_err("%s: bam init fallback failed: %d",
2296 __func__, ret);
2297 }
2298}
2299
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002300static void toggle_apps_ack(void)
2301{
2302 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
Eric Holmberg878923a2012-01-10 14:28:19 -07002303
Eric Holmberg7614a7f2013-07-29 15:47:12 -06002304 if (in_global_reset) {
2305 BAM_DMUX_LOG("%s: skipped due to SSR\n", __func__);
2306 return;
2307 }
2308
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302309 BAM_DMUX_LOG("%s: apps ack %d->%d\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -07002310 clear_bit & 0x1, ~clear_bit & 0x1);
Brent Hronik89c96ba2013-08-27 14:34:22 -06002311 bam_ops->smsm_change_state_ptr(SMSM_APPS_STATE,
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002312 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
2313 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
2314 clear_bit = ~clear_bit;
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002315 DBG_INC_ACK_OUT_CNT();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002316}
2317
Jeff Hugoade1f842011-08-03 15:53:59 -06002318static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
2319{
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002320 static int last_processed_state;
2321
2322 mutex_lock(&smsm_cb_lock);
Eric Holmberg878923a2012-01-10 14:28:19 -07002323 bam_dmux_power_state = new_state & SMSM_A2_POWER_CONTROL ? 1 : 0;
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002324 DBG_INC_A2_POWER_CONTROL_IN_CNT();
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302325 BAM_DMUX_LOG("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
Eric Holmberg878923a2012-01-10 14:28:19 -07002326 new_state);
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002327 if (last_processed_state == (new_state & SMSM_A2_POWER_CONTROL)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302328 BAM_DMUX_LOG("%s: already processed this state\n", __func__);
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002329 mutex_unlock(&smsm_cb_lock);
2330 return;
2331 }
2332
2333 last_processed_state = new_state & SMSM_A2_POWER_CONTROL;
Eric Holmberg878923a2012-01-10 14:28:19 -07002334
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002335 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302336 BAM_DMUX_LOG("%s: reconnect\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07002337 grab_wakelock();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002338 reconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002339 } else if (bam_mux_initialized &&
2340 !(new_state & SMSM_A2_POWER_CONTROL)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302341 BAM_DMUX_LOG("%s: disconnect\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002342 disconnect_to_bam();
Eric Holmberg006057d2012-01-11 10:10:42 -07002343 release_wakelock();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002344 } else if (new_state & SMSM_A2_POWER_CONTROL) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302345 BAM_DMUX_LOG("%s: init\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07002346 grab_wakelock();
Jeff Hugoa670b762012-03-15 15:58:28 -06002347 if (cpu_is_msm9615())
2348 msm9615_bam_init();
2349 else
Eric Holmberg604ab252012-01-15 00:01:18 -07002350 bam_init();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002351 } else {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302352 BAM_DMUX_LOG("%s: bad state change\n", __func__);
Jeff Hugoade1f842011-08-03 15:53:59 -06002353 pr_err("%s: unsupported state change\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002354 }
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002355 mutex_unlock(&smsm_cb_lock);
Jeff Hugoade1f842011-08-03 15:53:59 -06002356
2357}
2358
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002359static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
2360 uint32_t new_state)
2361{
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002362 DBG_INC_ACK_IN_CNT();
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302363 BAM_DMUX_LOG("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
Eric Holmberg878923a2012-01-10 14:28:19 -07002364 new_state);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002365 complete_all(&ul_wakeup_ack_completion);
2366}
2367
Brent Hronik89c96ba2013-08-27 14:34:22 -06002368/**
2369 * msm_bam_dmux_set_bam_ops() - sets the bam_ops
2370 * @ops: bam_ops_if to set
2371 *
2372 * Sets bam_ops to allow switching of runtime behavior. Preconditon, bam dmux
2373 * must be in an idle state. If input ops is NULL, then bam_ops will be
2374 * restored to their default state.
2375 */
2376void msm_bam_dmux_set_bam_ops(struct bam_ops_if *ops)
2377{
2378 if (ops != NULL)
2379 bam_ops = ops;
2380 else
2381 bam_ops = &bam_default_ops;
2382}
2383EXPORT_SYMBOL(msm_bam_dmux_set_bam_ops);
2384
2385/**
2386 * msm_bam_dmux_deinit() - puts bam dmux into a deinited state
2387 *
2388 * Puts bam dmux into a deinitialized state by simulating an ssr.
2389 */
2390void msm_bam_dmux_deinit(void)
2391{
2392 restart_notifier_cb(NULL, SUBSYS_BEFORE_SHUTDOWN, NULL);
2393 restart_notifier_cb(NULL, SUBSYS_AFTER_SHUTDOWN, NULL);
2394}
2395EXPORT_SYMBOL(msm_bam_dmux_deinit);
2396
2397/**
2398 * msm_bam_dmux_reinit() - reinitializes bam dmux
2399 */
2400void msm_bam_dmux_reinit(void)
2401{
2402 bam_ops->smsm_state_cb_register_ptr(SMSM_MODEM_STATE,
2403 SMSM_A2_POWER_CONTROL,
2404 bam_dmux_smsm_cb, NULL);
2405 bam_ops->smsm_state_cb_register_ptr(SMSM_MODEM_STATE,
2406 SMSM_A2_POWER_CONTROL_ACK,
2407 bam_dmux_smsm_ack_cb, NULL);
2408 bam_mux_initialized = 0;
2409 bam_init();
2410}
2411EXPORT_SYMBOL(msm_bam_dmux_reinit);
2412
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002413static int bam_dmux_probe(struct platform_device *pdev)
2414{
2415 int rc;
Jeff Hugo3910ee12012-08-21 14:08:20 -06002416 struct resource *r;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002417
2418 DBG("%s probe called\n", __func__);
2419 if (bam_mux_initialized)
2420 return 0;
2421
Jeff Hugo3910ee12012-08-21 14:08:20 -06002422 if (pdev->dev.of_node) {
2423 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2424 if (!r) {
2425 pr_err("%s: reg field missing\n", __func__);
2426 return -ENODEV;
2427 }
2428 a2_phys_base = (void *)(r->start);
2429 a2_phys_size = (uint32_t)(resource_size(r));
2430 a2_bam_irq = platform_get_irq(pdev, 0);
2431 if (a2_bam_irq == -ENXIO) {
2432 pr_err("%s: irq field missing\n", __func__);
2433 return -ENODEV;
2434 }
2435 DBG("%s: base:%p size:%x irq:%d\n", __func__,
2436 a2_phys_base,
2437 a2_phys_size,
2438 a2_bam_irq);
2439 } else { /* fallback to default init data */
2440 a2_phys_base = (void *)(A2_PHYS_BASE);
2441 a2_phys_size = A2_PHYS_SIZE;
2442 a2_bam_irq = A2_BAM_IRQ;
2443 }
2444
Stephen Boyd69d35e32012-02-14 15:33:30 -08002445 xo_clk = clk_get(&pdev->dev, "xo");
2446 if (IS_ERR(xo_clk)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302447 BAM_DMUX_LOG("%s: did not get xo clock\n", __func__);
Jeff Hugod0befde2012-08-09 15:32:49 -06002448 xo_clk = NULL;
Stephen Boyd69d35e32012-02-14 15:33:30 -08002449 }
Stephen Boyd1c51a492011-10-26 12:11:47 -07002450 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002451 if (IS_ERR(dfab_clk)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302452 BAM_DMUX_LOG("%s: did not get dfab clock\n", __func__);
Jeff Hugod0befde2012-08-09 15:32:49 -06002453 dfab_clk = NULL;
2454 } else {
2455 rc = clk_set_rate(dfab_clk, 64000000);
2456 if (rc)
2457 pr_err("%s: unable to set dfab clock rate\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002458 }
2459
Jeff Hugofff43af92012-03-29 17:54:52 -06002460 /*
2461 * setup the workqueue so that it can be pinned to core 0 and not
2462 * block the watchdog pet function, so that netif_rx() in rmnet
2463 * only uses one queue.
2464 */
2465 bam_mux_rx_workqueue = alloc_workqueue("bam_dmux_rx",
2466 WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002467 if (!bam_mux_rx_workqueue)
2468 return -ENOMEM;
2469
2470 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
2471 if (!bam_mux_tx_workqueue) {
2472 destroy_workqueue(bam_mux_rx_workqueue);
2473 return -ENOMEM;
2474 }
2475
Jeff Hugo7960abd2011-08-02 15:39:38 -06002476 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002477 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06002478 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
2479 "bam_dmux_ch_%d", rc);
2480 /* bus 2, ie a2 stream 2 */
2481 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
2482 if (!bam_ch[rc].pdev) {
2483 pr_err("%s: platform device alloc failed\n", __func__);
2484 destroy_workqueue(bam_mux_rx_workqueue);
2485 destroy_workqueue(bam_mux_tx_workqueue);
2486 return -ENOMEM;
2487 }
2488 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002489
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002490 init_completion(&ul_wakeup_ack_completion);
2491 init_completion(&bam_connection_completion);
Eric Holmberg006057d2012-01-11 10:10:42 -07002492 init_completion(&dfab_unvote_completion);
Brent Hronik096f7d32013-06-28 15:43:08 -06002493 init_completion(&shutdown_completion);
2494 complete_all(&shutdown_completion);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002495 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002496 wake_lock_init(&bam_wakelock, WAKE_LOCK_SUSPEND, "bam_dmux_wakelock");
Arun Kumar Neelakantame7c0d622013-10-11 14:34:02 +05302497 init_srcu_struct(&bam_dmux_srcu);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002498
Brent Hronik89c96ba2013-08-27 14:34:22 -06002499 rc = bam_ops->smsm_state_cb_register_ptr(SMSM_MODEM_STATE,
2500 SMSM_A2_POWER_CONTROL,
2501 bam_dmux_smsm_cb, NULL);
Jeff Hugoade1f842011-08-03 15:53:59 -06002502
2503 if (rc) {
2504 destroy_workqueue(bam_mux_rx_workqueue);
2505 destroy_workqueue(bam_mux_tx_workqueue);
2506 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
2507 return -ENOMEM;
2508 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002509
Brent Hronik89c96ba2013-08-27 14:34:22 -06002510 rc = bam_ops->smsm_state_cb_register_ptr(SMSM_MODEM_STATE,
2511 SMSM_A2_POWER_CONTROL_ACK,
2512 bam_dmux_smsm_ack_cb, NULL);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002513
2514 if (rc) {
2515 destroy_workqueue(bam_mux_rx_workqueue);
2516 destroy_workqueue(bam_mux_tx_workqueue);
Brent Hronik89c96ba2013-08-27 14:34:22 -06002517 bam_ops->smsm_state_cb_deregister_ptr(SMSM_MODEM_STATE,
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002518 SMSM_A2_POWER_CONTROL,
2519 bam_dmux_smsm_cb, NULL);
2520 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
2521 rc);
2522 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
2523 platform_device_put(bam_ch[rc].pdev);
2524 return -ENOMEM;
2525 }
2526
Brent Hronik89c96ba2013-08-27 14:34:22 -06002527 if (bam_ops->smsm_get_state_ptr(SMSM_MODEM_STATE) &
2528 SMSM_A2_POWER_CONTROL)
2529 bam_dmux_smsm_cb(NULL, 0,
2530 bam_ops->smsm_get_state_ptr(SMSM_MODEM_STATE));
Eric Holmbergfd1e2ae2011-11-15 18:28:17 -07002531
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002532 return 0;
2533}
2534
Jeff Hugo3910ee12012-08-21 14:08:20 -06002535static struct of_device_id msm_match_table[] = {
2536 {.compatible = "qcom,bam_dmux"},
2537 {},
2538};
2539
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002540static struct platform_driver bam_dmux_driver = {
2541 .probe = bam_dmux_probe,
2542 .driver = {
2543 .name = "BAM_RMNT",
2544 .owner = THIS_MODULE,
Jeff Hugo3910ee12012-08-21 14:08:20 -06002545 .of_match_table = msm_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002546 },
2547};
2548
2549static int __init bam_dmux_init(void)
2550{
2551#ifdef CONFIG_DEBUG_FS
2552 struct dentry *dent;
2553
2554 dent = debugfs_create_dir("bam_dmux", 0);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002555 if (!IS_ERR(dent)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002556 debug_create("tbl", 0444, dent, debug_tbl);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002557 debug_create("ul_pkt_cnt", 0444, dent, debug_ul_pkt_cnt);
2558 debug_create("stats", 0444, dent, debug_stats);
2559 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002560#endif
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302561
2562 bam_ipc_log_txt = ipc_log_context_create(BAM_IPC_LOG_PAGES, "bam_dmux");
2563 if (!bam_ipc_log_txt) {
2564 pr_err("%s : unable to create IPC Logging Context", __func__);
Eric Holmberg878923a2012-01-10 14:28:19 -07002565 }
2566
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07002567 rx_timer_interval = DEFAULT_POLLING_MIN_SLEEP;
2568
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002569 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002570 return platform_driver_register(&bam_dmux_driver);
2571}
2572
Jeff Hugoade1f842011-08-03 15:53:59 -06002573late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002574MODULE_DESCRIPTION("MSM BAM DMUX");
2575MODULE_LICENSE("GPL v2");