blob: 11077dc3d70262af1712fc76a95b19bb174399f2 [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>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070032#include <mach/sps.h>
33#include <mach/bam_dmux.h>
Jeff Hugoade1f842011-08-03 15:53:59 -060034#include <mach/msm_smsm.h>
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060035#include <mach/subsystem_notif.h>
Jeff Hugo75913c82011-12-05 15:59:01 -070036#include <mach/socinfo.h>
Jeff Hugo4838f412012-01-20 11:19:37 -070037#include <mach/subsystem_restart.h>
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070038
39#define BAM_CH_LOCAL_OPEN 0x1
40#define BAM_CH_REMOTE_OPEN 0x2
Jeff Hugo6e7a92a2011-10-24 05:25:13 -060041#define BAM_CH_IN_RESET 0x4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070042
43#define BAM_MUX_HDR_MAGIC_NO 0x33fc
44
Eric Holmberg006057d2012-01-11 10:10:42 -070045#define BAM_MUX_HDR_CMD_DATA 0
46#define BAM_MUX_HDR_CMD_OPEN 1
47#define BAM_MUX_HDR_CMD_CLOSE 2
48#define BAM_MUX_HDR_CMD_STATUS 3 /* unused */
49#define BAM_MUX_HDR_CMD_OPEN_NO_A2_PC 4
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070050
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070051
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -070052#define LOW_WATERMARK 2
53#define HIGH_WATERMARK 4
Anurag Singhdcd8b4e2012-07-30 16:46:37 -070054#define DEFAULT_POLLING_MIN_SLEEP (950)
55#define MAX_POLLING_SLEEP (6050)
56#define MIN_POLLING_SLEEP (950)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070057
58static int msm_bam_dmux_debug_enable;
59module_param_named(debug_enable, msm_bam_dmux_debug_enable,
60 int, S_IRUGO | S_IWUSR | S_IWGRP);
Anurag Singhdcd8b4e2012-07-30 16:46:37 -070061static int POLLING_MIN_SLEEP = 950;
62module_param_named(min_sleep, POLLING_MIN_SLEEP,
63 int, S_IRUGO | S_IWUSR | S_IWGRP);
64static int POLLING_MAX_SLEEP = 1050;
65module_param_named(max_sleep, POLLING_MAX_SLEEP,
66 int, S_IRUGO | S_IWUSR | S_IWGRP);
67static int POLLING_INACTIVITY = 40;
68module_param_named(inactivity, POLLING_INACTIVITY,
69 int, S_IRUGO | S_IWUSR | S_IWGRP);
70static int bam_adaptive_timer_enabled = 1;
71module_param_named(adaptive_timer_enabled,
72 bam_adaptive_timer_enabled,
73 int, S_IRUGO | S_IWUSR | S_IWGRP);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070074
75#if defined(DEBUG)
76static uint32_t bam_dmux_read_cnt;
77static uint32_t bam_dmux_write_cnt;
78static uint32_t bam_dmux_write_cpy_cnt;
79static uint32_t bam_dmux_write_cpy_bytes;
Eric Holmberg2fddbcd2011-11-28 18:25:57 -070080static uint32_t bam_dmux_tx_sps_failure_cnt;
Eric Holmberg6074aba2012-01-18 17:59:44 -070081static uint32_t bam_dmux_tx_stall_cnt;
Eric Holmberg1f1255d2012-02-22 13:37:21 -070082static atomic_t bam_dmux_ack_out_cnt = ATOMIC_INIT(0);
83static atomic_t bam_dmux_ack_in_cnt = ATOMIC_INIT(0);
84static atomic_t bam_dmux_a2_pwr_cntl_in_cnt = ATOMIC_INIT(0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070085
86#define DBG(x...) do { \
87 if (msm_bam_dmux_debug_enable) \
88 pr_debug(x); \
89 } while (0)
90
91#define DBG_INC_READ_CNT(x) do { \
92 bam_dmux_read_cnt += (x); \
93 if (msm_bam_dmux_debug_enable) \
94 pr_debug("%s: total read bytes %u\n", \
95 __func__, bam_dmux_read_cnt); \
96 } while (0)
97
98#define DBG_INC_WRITE_CNT(x) do { \
99 bam_dmux_write_cnt += (x); \
100 if (msm_bam_dmux_debug_enable) \
101 pr_debug("%s: total written bytes %u\n", \
102 __func__, bam_dmux_write_cnt); \
103 } while (0)
104
105#define DBG_INC_WRITE_CPY(x) do { \
106 bam_dmux_write_cpy_bytes += (x); \
107 bam_dmux_write_cpy_cnt++; \
108 if (msm_bam_dmux_debug_enable) \
109 pr_debug("%s: total write copy cnt %u, bytes %u\n", \
110 __func__, bam_dmux_write_cpy_cnt, \
111 bam_dmux_write_cpy_bytes); \
112 } while (0)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700113
114#define DBG_INC_TX_SPS_FAILURE_CNT() do { \
115 bam_dmux_tx_sps_failure_cnt++; \
116} while (0)
117
Eric Holmberg6074aba2012-01-18 17:59:44 -0700118#define DBG_INC_TX_STALL_CNT() do { \
119 bam_dmux_tx_stall_cnt++; \
120} while (0)
121
Eric Holmberg1f1255d2012-02-22 13:37:21 -0700122#define DBG_INC_ACK_OUT_CNT() \
123 atomic_inc(&bam_dmux_ack_out_cnt)
124
125#define DBG_INC_A2_POWER_CONTROL_IN_CNT() \
126 atomic_inc(&bam_dmux_a2_pwr_cntl_in_cnt)
127
128#define DBG_INC_ACK_IN_CNT() \
129 atomic_inc(&bam_dmux_ack_in_cnt)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700130#else
131#define DBG(x...) do { } while (0)
132#define DBG_INC_READ_CNT(x...) do { } while (0)
133#define DBG_INC_WRITE_CNT(x...) do { } while (0)
134#define DBG_INC_WRITE_CPY(x...) do { } while (0)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700135#define DBG_INC_TX_SPS_FAILURE_CNT() do { } while (0)
Eric Holmberg6074aba2012-01-18 17:59:44 -0700136#define DBG_INC_TX_STALL_CNT() do { } while (0)
Eric Holmberg1f1255d2012-02-22 13:37:21 -0700137#define DBG_INC_ACK_OUT_CNT() do { } while (0)
138#define DBG_INC_A2_POWER_CONTROL_IN_CNT() \
139 do { } while (0)
140#define DBG_INC_ACK_IN_CNT() do { } while (0)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700141#endif
142
143struct bam_ch_info {
144 uint32_t status;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600145 void (*notify)(void *, int, unsigned long);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700146 void *priv;
147 spinlock_t lock;
Jeff Hugo7960abd2011-08-02 15:39:38 -0600148 struct platform_device *pdev;
149 char name[BAM_DMUX_CH_NAME_MAX_LEN];
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700150 int num_tx_pkts;
151 int use_wm;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700152};
153
154struct tx_pkt_info {
155 struct sk_buff *skb;
156 dma_addr_t dma_address;
157 char is_cmd;
158 uint32_t len;
159 struct work_struct work;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600160 struct list_head list_node;
Eric Holmberg878923a2012-01-10 14:28:19 -0700161 unsigned ts_sec;
162 unsigned long ts_nsec;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700163};
164
165struct rx_pkt_info {
166 struct sk_buff *skb;
167 dma_addr_t dma_address;
168 struct work_struct work;
Jeff Hugo949080a2011-08-30 11:58:56 -0600169 struct list_head list_node;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700170};
171
172#define A2_NUM_PIPES 6
173#define A2_SUMMING_THRESHOLD 4096
174#define A2_DEFAULT_DESCRIPTORS 32
175#define A2_PHYS_BASE 0x124C2000
176#define A2_PHYS_SIZE 0x2000
177#define BUFFER_SIZE 2048
178#define NUM_BUFFERS 32
Jeff Hugo3910ee12012-08-21 14:08:20 -0600179
180#ifndef A2_BAM_IRQ
181#define A2_BAM_IRQ -1
182#endif
183
184static void *a2_phys_base;
185static uint32_t a2_phys_size;
186static int a2_bam_irq;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700187static struct sps_bam_props a2_props;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600188static u32 a2_device_handle;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700189static struct sps_pipe *bam_tx_pipe;
190static struct sps_pipe *bam_rx_pipe;
191static struct sps_connect tx_connection;
192static struct sps_connect rx_connection;
193static struct sps_mem_buffer tx_desc_mem_buf;
194static struct sps_mem_buffer rx_desc_mem_buf;
195static struct sps_register_event tx_register_event;
Jeff Hugo33dbc002011-08-25 15:52:53 -0600196static struct sps_register_event rx_register_event;
Jeff Hugo7c185602013-09-11 17:39:54 -0600197static unsigned long long last_rx_pkt_timestamp;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700198
199static struct bam_ch_info bam_ch[BAM_DMUX_NUM_CHANNELS];
200static int bam_mux_initialized;
201
Jeff Hugo949080a2011-08-30 11:58:56 -0600202static int polling_mode;
Anurag Singhdcd8b4e2012-07-30 16:46:37 -0700203static unsigned long rx_timer_interval;
Jeff Hugo949080a2011-08-30 11:58:56 -0600204
205static LIST_HEAD(bam_rx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600206static DEFINE_MUTEX(bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700207static int bam_rx_pool_len;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600208static LIST_HEAD(bam_tx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600209static DEFINE_SPINLOCK(bam_tx_pool_spinlock);
Eric Holmberga623da82012-07-12 09:37:09 -0600210static DEFINE_MUTEX(bam_pdev_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600211
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700212struct bam_mux_hdr {
213 uint16_t magic_num;
214 uint8_t reserved;
215 uint8_t cmd;
216 uint8_t pad_len;
217 uint8_t ch_id;
218 uint16_t pkt_len;
219};
220
Jeff Hugod98b1082011-10-24 10:30:23 -0600221static void notify_all(int event, unsigned long data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700222static void bam_mux_write_done(struct work_struct *work);
223static void handle_bam_mux_cmd(struct work_struct *work);
Jeff Hugo949080a2011-08-30 11:58:56 -0600224static void rx_timer_work_func(struct work_struct *work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700225
Jeff Hugo949080a2011-08-30 11:58:56 -0600226static DECLARE_WORK(rx_timer_work, rx_timer_work_func);
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600227static struct delayed_work queue_rx_work;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700228
229static struct workqueue_struct *bam_mux_rx_workqueue;
230static struct workqueue_struct *bam_mux_tx_workqueue;
231
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600232/* A2 power collaspe */
233#define UL_TIMEOUT_DELAY 1000 /* in ms */
Jeff Hugo0b13a352012-03-17 23:18:30 -0600234#define ENABLE_DISCONNECT_ACK 0x1
Brent Hronik096f7d32013-06-28 15:43:08 -0600235#define SHUTDOWN_TIMEOUT_MS 500
Jeff Hugo1f317392013-07-24 16:28:52 -0600236#define UL_WAKEUP_TIMEOUT_MS 2000
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600237static void toggle_apps_ack(void);
238static void reconnect_to_bam(void);
239static void disconnect_to_bam(void);
240static void ul_wakeup(void);
241static void ul_timeout(struct work_struct *work);
242static void vote_dfab(void);
243static void unvote_dfab(void);
Jeff Hugod98b1082011-10-24 10:30:23 -0600244static void kickoff_ul_wakeup_func(struct work_struct *work);
Eric Holmberg006057d2012-01-11 10:10:42 -0700245static void grab_wakelock(void);
246static void release_wakelock(void);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600247
248static int bam_is_connected;
249static DEFINE_MUTEX(wakeup_lock);
250static struct completion ul_wakeup_ack_completion;
251static struct completion bam_connection_completion;
252static struct delayed_work ul_timeout_work;
253static int ul_packet_written;
Eric Holmbergbc9f21c2012-01-18 11:33:33 -0700254static atomic_t ul_ondemand_vote = ATOMIC_INIT(0);
Stephen Boyd69d35e32012-02-14 15:33:30 -0800255static struct clk *dfab_clk, *xo_clk;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600256static DEFINE_RWLOCK(ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600257static DECLARE_WORK(kickoff_ul_wakeup, kickoff_ul_wakeup_func);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600258static int bam_connection_is_active;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -0700259static int wait_for_ack;
Jeff Hugoae3a85e2011-12-02 17:10:18 -0700260static struct wake_lock bam_wakelock;
Eric Holmberg006057d2012-01-11 10:10:42 -0700261static int a2_pc_disabled;
262static DEFINE_MUTEX(dfab_status_lock);
263static int dfab_is_on;
264static int wait_for_dfab;
265static struct completion dfab_unvote_completion;
266static DEFINE_SPINLOCK(wakelock_reference_lock);
267static int wakelock_reference_count;
Jeff Hugo583a6da2012-02-03 11:37:30 -0700268static int a2_pc_disabled_wakelock_skipped;
Jeff Hugob1e7c582012-06-20 15:02:11 -0600269static int disconnect_ack = 1;
Jeff Hugocb798022012-04-09 14:55:40 -0600270static LIST_HEAD(bam_other_notify_funcs);
Jeff Hugo4b7c7b32012-04-18 16:25:14 -0600271static DEFINE_MUTEX(smsm_cb_lock);
Jeff Hugoc2696142012-05-03 11:42:13 -0600272static DEFINE_MUTEX(delayed_ul_vote_lock);
273static int need_delayed_ul_vote;
Jeff Hugo18792a32012-06-20 15:25:55 -0600274static int power_management_only_mode;
Jeff Hugoa82a95c2012-12-14 17:56:19 -0700275static int in_ssr;
276static int ssr_skipped_disconnect;
Brent Hronik096f7d32013-06-28 15:43:08 -0600277static struct completion shutdown_completion;
Jeff Hugocb798022012-04-09 14:55:40 -0600278
279struct outside_notify_func {
280 void (*notify)(void *, int, unsigned long);
281 void *priv;
282 struct list_head list_node;
283};
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600284/* End A2 power collaspe */
285
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600286/* subsystem restart */
287static int restart_notifier_cb(struct notifier_block *this,
288 unsigned long code,
289 void *data);
290
291static struct notifier_block restart_notifier = {
292 .notifier_call = restart_notifier_cb,
293};
294static int in_global_reset;
295/* end subsystem restart */
296
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700297#define bam_ch_is_open(x) \
298 (bam_ch[(x)].status == (BAM_CH_LOCAL_OPEN | BAM_CH_REMOTE_OPEN))
299
300#define bam_ch_is_local_open(x) \
301 (bam_ch[(x)].status & BAM_CH_LOCAL_OPEN)
302
303#define bam_ch_is_remote_open(x) \
304 (bam_ch[(x)].status & BAM_CH_REMOTE_OPEN)
305
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600306#define bam_ch_is_in_reset(x) \
307 (bam_ch[(x)].status & BAM_CH_IN_RESET)
308
Eric Holmberg878923a2012-01-10 14:28:19 -0700309struct kfifo bam_dmux_state_log;
Eric Holmberg878923a2012-01-10 14:28:19 -0700310static int bam_dmux_uplink_vote;
311static int bam_dmux_power_state;
312
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530313static void *bam_ipc_log_txt;
314
315#define BAM_IPC_LOG_PAGES 5
316
Eric Holmberg878923a2012-01-10 14:28:19 -0700317/**
318 * Log a state change along with a small message.
Eric Holmberg878923a2012-01-10 14:28:19 -0700319 * Complete size of messsage is limited to @todo.
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530320 * Logging is done using IPC Logging infrastructure.
321 *
322 * States
323 * D: 1 = Power collapse disabled
324 * R: 1 = in global reset
325 * P: 1 = BAM is powered up
326 * A: 1 = BAM initialized and ready for data
327 * V: 1 = Uplink vote for power
328 * U: 1 = Uplink active
329 * W: 1 = Uplink Wait-for-ack
330 * A: 1 = Uplink ACK received
331 * #: >=1 On-demand uplink vote
332 * D: 1 = Disconnect ACK active
Eric Holmberg878923a2012-01-10 14:28:19 -0700333 */
Eric Holmberg878923a2012-01-10 14:28:19 -0700334
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530335#define BAM_DMUX_LOG(fmt, args...) \
336do { \
337 if (bam_ipc_log_txt) { \
338 ipc_log_string(bam_ipc_log_txt, \
339 "<DMUX> %c%c%c%c %c%c%c%c%d%c " fmt, \
340 a2_pc_disabled ? 'D' : 'd', \
341 in_global_reset ? 'R' : 'r', \
342 bam_dmux_power_state ? 'P' : 'p', \
343 bam_connection_is_active ? 'A' : 'a', \
344 bam_dmux_uplink_vote ? 'V' : 'v', \
345 bam_is_connected ? 'U' : 'u', \
346 wait_for_ack ? 'W' : 'w', \
347 ul_wakeup_ack_completion.done ? 'A' : 'a', \
348 atomic_read(&ul_ondemand_vote), \
349 disconnect_ack ? 'D' : 'd', \
350 args); \
351 } \
352} while (0)
Eric Holmberg878923a2012-01-10 14:28:19 -0700353
Zaheerulla Meerf800bba2013-02-13 15:49:14 +0530354#define DMUX_LOG_KERR(fmt, args...) \
355do { \
356 BAM_DMUX_LOG(fmt, args); \
357 pr_err(fmt, args); \
358} while (0)
359
Eric Holmberg878923a2012-01-10 14:28:19 -0700360static inline void set_tx_timestamp(struct tx_pkt_info *pkt)
361{
362 unsigned long long t_now;
363
364 t_now = sched_clock();
365 pkt->ts_nsec = do_div(t_now, 1000000000U);
366 pkt->ts_sec = (unsigned)t_now;
367}
368
369static inline void verify_tx_queue_is_empty(const char *func)
370{
371 unsigned long flags;
372 struct tx_pkt_info *info;
373 int reported = 0;
374
375 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
376 list_for_each_entry(info, &bam_tx_pool, list_node) {
377 if (!reported) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530378 BAM_DMUX_LOG("%s: tx pool not empty\n", func);
Eric Holmberg454d9da2012-01-12 09:37:14 -0700379 if (!in_global_reset)
380 pr_err("%s: tx pool not empty\n", func);
Eric Holmberg878923a2012-01-10 14:28:19 -0700381 reported = 1;
382 }
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530383 BAM_DMUX_LOG("%s: node=%p ts=%u.%09lu\n", __func__,
Eric Holmberg454d9da2012-01-12 09:37:14 -0700384 &info->list_node, info->ts_sec, info->ts_nsec);
385 if (!in_global_reset)
386 pr_err("%s: node=%p ts=%u.%09lu\n", __func__,
387 &info->list_node, info->ts_sec, info->ts_nsec);
Eric Holmberg878923a2012-01-10 14:28:19 -0700388 }
389 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
390}
391
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700392static void queue_rx(void)
393{
394 void *ptr;
395 struct rx_pkt_info *info;
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700396 int ret;
397 int rx_len_cached;
Jeff Hugo949080a2011-08-30 11:58:56 -0600398
Jeff Hugoc9749932011-11-02 17:50:40 -0600399 mutex_lock(&bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700400 rx_len_cached = bam_rx_pool_len;
Jeff Hugoc9749932011-11-02 17:50:40 -0600401 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600402
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600403 while (bam_connection_is_active && rx_len_cached < NUM_BUFFERS) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700404 if (in_global_reset)
405 goto fail;
406
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600407 info = kmalloc(sizeof(struct rx_pkt_info),
408 GFP_NOWAIT | __GFP_NOWARN);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700409 if (!info) {
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600410 DMUX_LOG_KERR(
411 "%s: unable to alloc rx_pkt_info, will retry later\n",
412 __func__);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700413 goto fail;
414 }
415
416 INIT_WORK(&info->work, handle_bam_mux_cmd);
417
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600418 info->skb = __dev_alloc_skb(BUFFER_SIZE,
419 GFP_NOWAIT | __GFP_NOWARN);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700420 if (info->skb == NULL) {
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600421 DMUX_LOG_KERR(
422 "%s: unable to alloc skb, will retry later\n",
423 __func__);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700424 goto fail_info;
425 }
426 ptr = skb_put(info->skb, BUFFER_SIZE);
427
428 info->dma_address = dma_map_single(NULL, ptr, BUFFER_SIZE,
429 DMA_FROM_DEVICE);
430 if (info->dma_address == 0 || info->dma_address == ~0) {
431 DMUX_LOG_KERR("%s: dma_map_single failure %p for %p\n",
432 __func__, (void *)info->dma_address, ptr);
433 goto fail_skb;
434 }
435
436 mutex_lock(&bam_rx_pool_mutexlock);
437 list_add_tail(&info->list_node, &bam_rx_pool);
438 rx_len_cached = ++bam_rx_pool_len;
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700439 ret = sps_transfer_one(bam_rx_pipe, info->dma_address,
Jeff Hugoc85df962013-04-05 13:22:48 -0600440 BUFFER_SIZE, info, 0);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700441 if (ret) {
Eric Holmberg00cf8692012-07-16 14:21:19 -0600442 list_del(&info->list_node);
443 rx_len_cached = --bam_rx_pool_len;
444 mutex_unlock(&bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700445 DMUX_LOG_KERR("%s: sps_transfer_one failed %d\n",
446 __func__, ret);
Eric Holmberg00cf8692012-07-16 14:21:19 -0600447
448 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
449 DMA_FROM_DEVICE);
450
451 goto fail_skb;
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700452 }
Eric Holmberg00cf8692012-07-16 14:21:19 -0600453 mutex_unlock(&bam_rx_pool_mutexlock);
454
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700455 }
456 return;
457
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700458fail_skb:
459 dev_kfree_skb_any(info->skb);
460
461fail_info:
462 kfree(info);
463
464fail:
Arun Kumar Neelakantam799447f2012-12-13 18:06:49 +0530465 if (rx_len_cached == 0 && !in_global_reset) {
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600466 DMUX_LOG_KERR("%s: rescheduling\n", __func__);
467 schedule_delayed_work(&queue_rx_work, msecs_to_jiffies(100));
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700468 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700469}
470
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600471static void queue_rx_work_func(struct work_struct *work)
472{
473 queue_rx();
474}
475
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700476static void bam_mux_process_data(struct sk_buff *rx_skb)
477{
478 unsigned long flags;
479 struct bam_mux_hdr *rx_hdr;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600480 unsigned long event_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700481
482 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
483
484 rx_skb->data = (unsigned char *)(rx_hdr + 1);
485 rx_skb->tail = rx_skb->data + rx_hdr->pkt_len;
486 rx_skb->len = rx_hdr->pkt_len;
Jeff Hugoee88f672011-10-04 17:14:52 -0600487 rx_skb->truesize = rx_hdr->pkt_len + sizeof(struct sk_buff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700488
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600489 event_data = (unsigned long)(rx_skb);
490
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700491 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600492 if (bam_ch[rx_hdr->ch_id].notify)
493 bam_ch[rx_hdr->ch_id].notify(
494 bam_ch[rx_hdr->ch_id].priv, BAM_DMUX_RECEIVE,
495 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700496 else
497 dev_kfree_skb_any(rx_skb);
498 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
499
500 queue_rx();
501}
502
Eric Holmberg006057d2012-01-11 10:10:42 -0700503static inline void handle_bam_mux_cmd_open(struct bam_mux_hdr *rx_hdr)
504{
505 unsigned long flags;
506 int ret;
507
Eric Holmberga623da82012-07-12 09:37:09 -0600508 mutex_lock(&bam_pdev_mutexlock);
509 if (in_global_reset) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530510 BAM_DMUX_LOG("%s: open cid %d aborted due to ssr\n",
Eric Holmberga623da82012-07-12 09:37:09 -0600511 __func__, rx_hdr->ch_id);
512 mutex_unlock(&bam_pdev_mutexlock);
513 queue_rx();
514 return;
515 }
Eric Holmberg006057d2012-01-11 10:10:42 -0700516 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
517 bam_ch[rx_hdr->ch_id].status |= BAM_CH_REMOTE_OPEN;
518 bam_ch[rx_hdr->ch_id].num_tx_pkts = 0;
519 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Eric Holmberg006057d2012-01-11 10:10:42 -0700520 ret = platform_device_add(bam_ch[rx_hdr->ch_id].pdev);
521 if (ret)
522 pr_err("%s: platform_device_add() error: %d\n",
523 __func__, ret);
Eric Holmberga623da82012-07-12 09:37:09 -0600524 mutex_unlock(&bam_pdev_mutexlock);
525 queue_rx();
Eric Holmberg006057d2012-01-11 10:10:42 -0700526}
527
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700528static void handle_bam_mux_cmd(struct work_struct *work)
529{
530 unsigned long flags;
531 struct bam_mux_hdr *rx_hdr;
532 struct rx_pkt_info *info;
533 struct sk_buff *rx_skb;
534
535 info = container_of(work, struct rx_pkt_info, work);
536 rx_skb = info->skb;
Jeff Hugo949080a2011-08-30 11:58:56 -0600537 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE, DMA_FROM_DEVICE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700538 kfree(info);
539
540 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
541
542 DBG_INC_READ_CNT(sizeof(struct bam_mux_hdr));
543 DBG("%s: magic %x reserved %d cmd %d pad %d ch %d len %d\n", __func__,
544 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
545 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
546 if (rx_hdr->magic_num != BAM_MUX_HDR_MAGIC_NO) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700547 DMUX_LOG_KERR("%s: dropping invalid hdr. magic %x"
548 " reserved %d cmd %d"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700549 " pad %d ch %d len %d\n", __func__,
550 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
551 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
552 dev_kfree_skb_any(rx_skb);
553 queue_rx();
554 return;
555 }
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700556
557 if (rx_hdr->ch_id >= BAM_DMUX_NUM_CHANNELS) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700558 DMUX_LOG_KERR("%s: dropping invalid LCID %d"
559 " reserved %d cmd %d"
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700560 " pad %d ch %d len %d\n", __func__,
561 rx_hdr->ch_id, rx_hdr->reserved, rx_hdr->cmd,
562 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
563 dev_kfree_skb_any(rx_skb);
564 queue_rx();
565 return;
566 }
567
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700568 switch (rx_hdr->cmd) {
569 case BAM_MUX_HDR_CMD_DATA:
570 DBG_INC_READ_CNT(rx_hdr->pkt_len);
571 bam_mux_process_data(rx_skb);
572 break;
573 case BAM_MUX_HDR_CMD_OPEN:
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530574 BAM_DMUX_LOG("%s: opening cid %d PC enabled\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -0700575 rx_hdr->ch_id);
Eric Holmberg006057d2012-01-11 10:10:42 -0700576 handle_bam_mux_cmd_open(rx_hdr);
Jeff Hugob1e7c582012-06-20 15:02:11 -0600577 if (!(rx_hdr->reserved & ENABLE_DISCONNECT_ACK)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530578 BAM_DMUX_LOG("%s: deactivating disconnect ack\n",
Jeff Hugod7d2b062012-07-24 14:29:56 -0600579 __func__);
Jeff Hugob1e7c582012-06-20 15:02:11 -0600580 disconnect_ack = 0;
Jeff Hugo0b13a352012-03-17 23:18:30 -0600581 }
Eric Holmberg006057d2012-01-11 10:10:42 -0700582 dev_kfree_skb_any(rx_skb);
583 break;
584 case BAM_MUX_HDR_CMD_OPEN_NO_A2_PC:
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530585 BAM_DMUX_LOG("%s: opening cid %d PC disabled\n", __func__,
Eric Holmberg006057d2012-01-11 10:10:42 -0700586 rx_hdr->ch_id);
587
588 if (!a2_pc_disabled) {
589 a2_pc_disabled = 1;
Jeff Hugo322179f2012-02-29 10:52:34 -0700590 ul_wakeup();
Eric Holmberg006057d2012-01-11 10:10:42 -0700591 }
592
593 handle_bam_mux_cmd_open(rx_hdr);
Eric Holmberge779dba2011-11-04 18:22:01 -0600594 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700595 break;
596 case BAM_MUX_HDR_CMD_CLOSE:
597 /* probably should drop pending write */
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530598 BAM_DMUX_LOG("%s: closing cid %d\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -0700599 rx_hdr->ch_id);
Eric Holmberga623da82012-07-12 09:37:09 -0600600 mutex_lock(&bam_pdev_mutexlock);
601 if (in_global_reset) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530602 BAM_DMUX_LOG("%s: close cid %d aborted due to ssr\n",
Eric Holmberga623da82012-07-12 09:37:09 -0600603 __func__, rx_hdr->ch_id);
604 mutex_unlock(&bam_pdev_mutexlock);
605 break;
606 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700607 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
608 bam_ch[rx_hdr->ch_id].status &= ~BAM_CH_REMOTE_OPEN;
609 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo7960abd2011-08-02 15:39:38 -0600610 platform_device_unregister(bam_ch[rx_hdr->ch_id].pdev);
611 bam_ch[rx_hdr->ch_id].pdev =
612 platform_device_alloc(bam_ch[rx_hdr->ch_id].name, 2);
613 if (!bam_ch[rx_hdr->ch_id].pdev)
614 pr_err("%s: platform_device_alloc failed\n", __func__);
Eric Holmberga623da82012-07-12 09:37:09 -0600615 mutex_unlock(&bam_pdev_mutexlock);
Eric Holmberge779dba2011-11-04 18:22:01 -0600616 dev_kfree_skb_any(rx_skb);
Eric Holmberga623da82012-07-12 09:37:09 -0600617 queue_rx();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700618 break;
619 default:
Eric Holmberg878923a2012-01-10 14:28:19 -0700620 DMUX_LOG_KERR("%s: dropping invalid hdr. magic %x"
621 " reserved %d cmd %d pad %d ch %d len %d\n",
622 __func__, rx_hdr->magic_num, rx_hdr->reserved,
623 rx_hdr->cmd, rx_hdr->pad_len, rx_hdr->ch_id,
624 rx_hdr->pkt_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700625 dev_kfree_skb_any(rx_skb);
626 queue_rx();
627 return;
628 }
629}
630
631static int bam_mux_write_cmd(void *data, uint32_t len)
632{
633 int rc;
634 struct tx_pkt_info *pkt;
635 dma_addr_t dma_address;
Jeff Hugo626303bf2011-11-21 11:43:28 -0700636 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700637
Eric Holmbergd83cd2b2011-11-04 15:54:17 -0600638 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700639 if (pkt == NULL) {
640 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
641 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700642 return rc;
643 }
644
645 dma_address = dma_map_single(NULL, data, len,
646 DMA_TO_DEVICE);
647 if (!dma_address) {
648 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugo96cb7482011-12-07 13:28:31 -0700649 kfree(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700651 return rc;
652 }
653 pkt->skb = (struct sk_buff *)(data);
654 pkt->len = len;
655 pkt->dma_address = dma_address;
656 pkt->is_cmd = 1;
Eric Holmberg878923a2012-01-10 14:28:19 -0700657 set_tx_timestamp(pkt);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600658 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700659 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600660 list_add_tail(&pkt->list_node, &bam_tx_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700661 rc = sps_transfer_one(bam_tx_pipe, dma_address, len,
Jeff Hugoc85df962013-04-05 13:22:48 -0600662 pkt, SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600663 if (rc) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700664 DMUX_LOG_KERR("%s sps_transfer_one failed rc=%d\n",
665 __func__, rc);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600666 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700667 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700668 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700669 dma_unmap_single(NULL, pkt->dma_address,
670 pkt->len,
671 DMA_TO_DEVICE);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600672 kfree(pkt);
Jeff Hugobb6da952012-01-16 15:02:42 -0700673 } else {
674 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600675 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700676
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600677 ul_packet_written = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700678 return rc;
679}
680
681static void bam_mux_write_done(struct work_struct *work)
682{
683 struct sk_buff *skb;
684 struct bam_mux_hdr *hdr;
685 struct tx_pkt_info *info;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700686 struct tx_pkt_info *info_expected;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600687 unsigned long event_data;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700688 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700689
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600690 if (in_global_reset)
691 return;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700692
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700693 info = container_of(work, struct tx_pkt_info, work);
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700694
695 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
696 info_expected = list_first_entry(&bam_tx_pool,
697 struct tx_pkt_info, list_node);
698 if (unlikely(info != info_expected)) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700699 struct tx_pkt_info *errant_pkt;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700700
Eric Holmberg878923a2012-01-10 14:28:19 -0700701 DMUX_LOG_KERR("%s: bam_tx_pool mismatch .next=%p,"
702 " list_node=%p, ts=%u.%09lu\n",
703 __func__, bam_tx_pool.next, &info->list_node,
704 info->ts_sec, info->ts_nsec
705 );
706
707 list_for_each_entry(errant_pkt, &bam_tx_pool, list_node) {
708 DMUX_LOG_KERR("%s: node=%p ts=%u.%09lu\n", __func__,
709 &errant_pkt->list_node, errant_pkt->ts_sec,
710 errant_pkt->ts_nsec);
711
712 }
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700713 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
714 BUG();
715 }
716 list_del(&info->list_node);
717 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
718
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600719 if (info->is_cmd) {
720 kfree(info->skb);
721 kfree(info);
722 return;
723 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700724 skb = info->skb;
725 kfree(info);
726 hdr = (struct bam_mux_hdr *)skb->data;
Eric Holmberg9fdef262012-02-14 11:46:05 -0700727 DBG_INC_WRITE_CNT(skb->len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600728 event_data = (unsigned long)(skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700729 spin_lock_irqsave(&bam_ch[hdr->ch_id].lock, flags);
730 bam_ch[hdr->ch_id].num_tx_pkts--;
731 spin_unlock_irqrestore(&bam_ch[hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600732 if (bam_ch[hdr->ch_id].notify)
733 bam_ch[hdr->ch_id].notify(
734 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
735 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700736 else
737 dev_kfree_skb_any(skb);
738}
739
740int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
741{
742 int rc = 0;
743 struct bam_mux_hdr *hdr;
744 unsigned long flags;
745 struct sk_buff *new_skb = NULL;
746 dma_addr_t dma_address;
747 struct tx_pkt_info *pkt;
748
749 if (id >= BAM_DMUX_NUM_CHANNELS)
750 return -EINVAL;
751 if (!skb)
752 return -EINVAL;
753 if (!bam_mux_initialized)
754 return -ENODEV;
755
756 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
757 spin_lock_irqsave(&bam_ch[id].lock, flags);
758 if (!bam_ch_is_open(id)) {
759 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
760 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
761 return -ENODEV;
762 }
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700763
764 if (bam_ch[id].use_wm &&
765 (bam_ch[id].num_tx_pkts >= HIGH_WATERMARK)) {
766 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
767 pr_err("%s: watermark exceeded: %d\n", __func__, id);
768 return -EAGAIN;
769 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700770 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
771
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600772 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600773 if (!bam_is_connected) {
774 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600775 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700776 if (unlikely(in_global_reset == 1))
777 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600778 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600779 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600780 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600781
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700782 /* if skb do not have any tailroom for padding,
783 copy the skb into a new expanded skb */
784 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
785 /* revisit, probably dev_alloc_skb and memcpy is effecient */
786 new_skb = skb_copy_expand(skb, skb_headroom(skb),
787 4 - (skb->len & 0x3), GFP_ATOMIC);
788 if (new_skb == NULL) {
789 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600790 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700791 }
792 dev_kfree_skb_any(skb);
793 skb = new_skb;
794 DBG_INC_WRITE_CPY(skb->len);
795 }
796
797 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
798
799 /* caller should allocate for hdr and padding
800 hdr is fine, padding is tricky */
801 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
802 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
803 hdr->reserved = 0;
804 hdr->ch_id = id;
805 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
806 if (skb->len & 0x3)
807 skb_put(skb, 4 - (skb->len & 0x3));
808
809 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
810
811 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
812 __func__, skb->data, skb->tail, skb->len,
813 hdr->pkt_len, hdr->pad_len);
814
815 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
816 if (pkt == NULL) {
817 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600818 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700819 }
820
821 dma_address = dma_map_single(NULL, skb->data, skb->len,
822 DMA_TO_DEVICE);
823 if (!dma_address) {
824 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600825 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700826 }
827 pkt->skb = skb;
828 pkt->dma_address = dma_address;
829 pkt->is_cmd = 0;
Eric Holmberg878923a2012-01-10 14:28:19 -0700830 set_tx_timestamp(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700831 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700832 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600833 list_add_tail(&pkt->list_node, &bam_tx_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700834 rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len,
Jeff Hugoc85df962013-04-05 13:22:48 -0600835 pkt, SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600836 if (rc) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700837 DMUX_LOG_KERR("%s sps_transfer_one failed rc=%d\n",
838 __func__, rc);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600839 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700840 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700841 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700842 dma_unmap_single(NULL, pkt->dma_address,
843 pkt->skb->len, DMA_TO_DEVICE);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600844 kfree(pkt);
Jeff Hugo872bd062011-11-15 17:47:21 -0700845 if (new_skb)
846 dev_kfree_skb_any(new_skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700847 } else {
Jeff Hugobb6da952012-01-16 15:02:42 -0700848 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700849 spin_lock_irqsave(&bam_ch[id].lock, flags);
850 bam_ch[id].num_tx_pkts++;
851 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600852 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600853 ul_packet_written = 1;
854 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700855 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600856
857write_fail3:
858 kfree(pkt);
859write_fail2:
Arun Kumar Neelakantam406e5692013-01-17 18:58:04 +0530860 skb_pull(skb, sizeof(struct bam_mux_hdr));
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600861 if (new_skb)
862 dev_kfree_skb_any(new_skb);
863write_fail:
864 read_unlock(&ul_wakeup_lock);
865 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866}
867
868int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600869 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700870{
871 struct bam_mux_hdr *hdr;
872 unsigned long flags;
873 int rc = 0;
874
875 DBG("%s: opening ch %d\n", __func__, id);
Eric Holmberg5d775432011-11-09 10:23:35 -0700876 if (!bam_mux_initialized) {
877 DBG("%s: not inititialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878 return -ENODEV;
Eric Holmberg5d775432011-11-09 10:23:35 -0700879 }
880 if (id >= BAM_DMUX_NUM_CHANNELS) {
881 pr_err("%s: invalid channel id %d\n", __func__, id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700882 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700883 }
884 if (notify == NULL) {
885 pr_err("%s: notify function is NULL\n", __func__);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600886 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700887 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700888
889 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
890 if (hdr == NULL) {
891 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
892 return -ENOMEM;
893 }
894 spin_lock_irqsave(&bam_ch[id].lock, flags);
895 if (bam_ch_is_open(id)) {
896 DBG("%s: Already opened %d\n", __func__, id);
897 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
898 kfree(hdr);
899 goto open_done;
900 }
901 if (!bam_ch_is_remote_open(id)) {
902 DBG("%s: Remote not open; ch: %d\n", __func__, id);
903 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
904 kfree(hdr);
Eric Holmberg5d775432011-11-09 10:23:35 -0700905 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700906 }
907
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600908 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909 bam_ch[id].priv = priv;
910 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700911 bam_ch[id].num_tx_pkts = 0;
912 bam_ch[id].use_wm = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700913 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
914
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600915 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600916 if (!bam_is_connected) {
917 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600918 ul_wakeup();
Brent Hronik96630422013-05-01 16:38:43 -0600919 if (unlikely(in_global_reset == 1)) {
920 kfree(hdr);
Jeff Hugo4838f412012-01-20 11:19:37 -0700921 return -EFAULT;
Brent Hronik96630422013-05-01 16:38:43 -0600922 }
Jeff Hugo061ce672011-10-21 17:15:32 -0600923 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600924 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600925 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600926
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700927 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
928 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
929 hdr->reserved = 0;
930 hdr->ch_id = id;
931 hdr->pkt_len = 0;
932 hdr->pad_len = 0;
933
934 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600935 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700936
937open_done:
938 DBG("%s: opened ch %d\n", __func__, id);
939 return rc;
940}
941
942int msm_bam_dmux_close(uint32_t id)
943{
944 struct bam_mux_hdr *hdr;
945 unsigned long flags;
946 int rc;
947
948 if (id >= BAM_DMUX_NUM_CHANNELS)
949 return -EINVAL;
950 DBG("%s: closing ch %d\n", __func__, id);
951 if (!bam_mux_initialized)
952 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700953
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600954 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600955 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600956 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600957 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700958 if (unlikely(in_global_reset == 1))
959 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600960 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600961 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600962 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600963
Jeff Hugo061ce672011-10-21 17:15:32 -0600964 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600965 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700966 bam_ch[id].priv = NULL;
967 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
968 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
969
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600970 if (bam_ch_is_in_reset(id)) {
971 read_unlock(&ul_wakeup_lock);
972 bam_ch[id].status &= ~BAM_CH_IN_RESET;
973 return 0;
974 }
975
Jeff Hugobb5802f2011-11-02 17:10:29 -0600976 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700977 if (hdr == NULL) {
978 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600979 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700980 return -ENOMEM;
981 }
982 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
983 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
984 hdr->reserved = 0;
985 hdr->ch_id = id;
986 hdr->pkt_len = 0;
987 hdr->pad_len = 0;
988
989 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600990 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700991
992 DBG("%s: closed ch %d\n", __func__, id);
993 return rc;
994}
995
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700996int msm_bam_dmux_is_ch_full(uint32_t id)
997{
998 unsigned long flags;
999 int ret;
1000
1001 if (id >= BAM_DMUX_NUM_CHANNELS)
1002 return -EINVAL;
1003
1004 spin_lock_irqsave(&bam_ch[id].lock, flags);
1005 bam_ch[id].use_wm = 1;
1006 ret = bam_ch[id].num_tx_pkts >= HIGH_WATERMARK;
1007 DBG("%s: ch %d num tx pkts=%d, HWM=%d\n", __func__,
1008 id, bam_ch[id].num_tx_pkts, ret);
1009 if (!bam_ch_is_local_open(id)) {
1010 ret = -ENODEV;
1011 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
1012 }
1013 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
1014
1015 return ret;
1016}
1017
1018int msm_bam_dmux_is_ch_low(uint32_t id)
1019{
Eric Holmberged3ca0a2012-04-09 15:44:58 -06001020 unsigned long flags;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001021 int ret;
1022
1023 if (id >= BAM_DMUX_NUM_CHANNELS)
1024 return -EINVAL;
1025
Eric Holmberged3ca0a2012-04-09 15:44:58 -06001026 spin_lock_irqsave(&bam_ch[id].lock, flags);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001027 bam_ch[id].use_wm = 1;
1028 ret = bam_ch[id].num_tx_pkts <= LOW_WATERMARK;
1029 DBG("%s: ch %d num tx pkts=%d, LWM=%d\n", __func__,
1030 id, bam_ch[id].num_tx_pkts, ret);
1031 if (!bam_ch_is_local_open(id)) {
1032 ret = -ENODEV;
1033 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
1034 }
Eric Holmberged3ca0a2012-04-09 15:44:58 -06001035 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001036
1037 return ret;
1038}
1039
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001040static void rx_switch_to_interrupt_mode(void)
1041{
1042 struct sps_connect cur_rx_conn;
1043 struct sps_iovec iov;
1044 struct rx_pkt_info *info;
1045 int ret;
1046
1047 /*
1048 * Attempt to enable interrupts - if this fails,
1049 * continue polling and we will retry later.
1050 */
1051 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
1052 if (ret) {
1053 pr_err("%s: sps_get_config() failed %d\n", __func__, ret);
1054 goto fail;
1055 }
1056
1057 rx_register_event.options = SPS_O_EOT;
1058 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1059 if (ret) {
1060 pr_err("%s: sps_register_event() failed %d\n", __func__, ret);
1061 goto fail;
1062 }
1063
1064 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
1065 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
1066 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
1067 if (ret) {
1068 pr_err("%s: sps_set_config() failed %d\n", __func__, ret);
1069 goto fail;
1070 }
1071 polling_mode = 0;
Brent Hronik096f7d32013-06-28 15:43:08 -06001072 complete_all(&shutdown_completion);
Eric Holmberg006057d2012-01-11 10:10:42 -07001073 release_wakelock();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001074
1075 /* handle any rx packets before interrupt was enabled */
1076 while (bam_connection_is_active && !polling_mode) {
1077 ret = sps_get_iovec(bam_rx_pipe, &iov);
1078 if (ret) {
1079 pr_err("%s: sps_get_iovec failed %d\n",
1080 __func__, ret);
1081 break;
1082 }
1083 if (iov.addr == 0)
1084 break;
1085
1086 mutex_lock(&bam_rx_pool_mutexlock);
1087 if (unlikely(list_empty(&bam_rx_pool))) {
Eric Holmberg00cf8692012-07-16 14:21:19 -06001088 DMUX_LOG_KERR("%s: have iovec %p but rx pool empty\n",
1089 __func__, (void *)iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001090 mutex_unlock(&bam_rx_pool_mutexlock);
1091 continue;
1092 }
1093 info = list_first_entry(&bam_rx_pool, struct rx_pkt_info,
1094 list_node);
Eric Holmberg00cf8692012-07-16 14:21:19 -06001095 if (info->dma_address != iov.addr) {
1096 DMUX_LOG_KERR("%s: iovec %p != dma %p\n",
1097 __func__,
1098 (void *)iov.addr,
1099 (void *)info->dma_address);
1100 list_for_each_entry(info, &bam_rx_pool, list_node) {
1101 DMUX_LOG_KERR("%s: dma %p\n", __func__,
1102 (void *)info->dma_address);
1103 if (iov.addr == info->dma_address)
1104 break;
1105 }
1106 }
1107 BUG_ON(info->dma_address != iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001108 list_del(&info->list_node);
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001109 --bam_rx_pool_len;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001110 mutex_unlock(&bam_rx_pool_mutexlock);
1111 handle_bam_mux_cmd(&info->work);
1112 }
1113 return;
1114
1115fail:
1116 pr_err("%s: reverting to polling\n", __func__);
Jeff Hugofff43af92012-03-29 17:54:52 -06001117 queue_work_on(0, bam_mux_rx_workqueue, &rx_timer_work);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001118}
1119
Jeff Hugo7c185602013-09-11 17:39:54 -06001120/**
1121 * store_rx_timestamp() - store the current raw time as as a timestamp for when
1122 * the last rx packet was processed
1123 */
1124static void store_rx_timestamp(void)
1125{
1126 last_rx_pkt_timestamp = sched_clock();
1127}
1128
1129/**
1130 * log_rx_timestamp() - Log the stored rx pkt timestamp in a human readable
1131 * format
1132 */
1133static void log_rx_timestamp(void)
1134{
1135 unsigned long long t = last_rx_pkt_timestamp;
1136 unsigned long nanosec_rem;
1137
1138 nanosec_rem = do_div(t, 1000000000U);
1139 BAM_DMUX_LOG("Last rx pkt processed at [%6u.%09lu]\n", (unsigned)t,
1140 nanosec_rem);
1141}
1142
Jeff Hugo949080a2011-08-30 11:58:56 -06001143static void rx_timer_work_func(struct work_struct *work)
1144{
1145 struct sps_iovec iov;
Jeff Hugo949080a2011-08-30 11:58:56 -06001146 struct rx_pkt_info *info;
1147 int inactive_cycles = 0;
1148 int ret;
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001149 u32 buffs_unused, buffs_used;
Jeff Hugo949080a2011-08-30 11:58:56 -06001150
Jeff Hugo7c185602013-09-11 17:39:54 -06001151 BAM_DMUX_LOG("%s: polling start\n", __func__);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001152 while (bam_connection_is_active) { /* timer loop */
Jeff Hugo949080a2011-08-30 11:58:56 -06001153 ++inactive_cycles;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001154 while (bam_connection_is_active) { /* deplete queue loop */
Jeff Hugo7c185602013-09-11 17:39:54 -06001155 if (in_global_reset) {
1156 BAM_DMUX_LOG(
1157 "%s: polling exit, global reset detected\n",
1158 __func__);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001159 return;
Jeff Hugo7c185602013-09-11 17:39:54 -06001160 }
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001161
1162 ret = sps_get_iovec(bam_rx_pipe, &iov);
1163 if (ret) {
Jeff Hugo7c185602013-09-11 17:39:54 -06001164 DMUX_LOG_KERR("%s: sps_get_iovec failed %d\n",
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001165 __func__, ret);
1166 break;
1167 }
Jeff Hugo949080a2011-08-30 11:58:56 -06001168 if (iov.addr == 0)
1169 break;
Jeff Hugo7c185602013-09-11 17:39:54 -06001170 store_rx_timestamp();
Jeff Hugo949080a2011-08-30 11:58:56 -06001171 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -06001172 mutex_lock(&bam_rx_pool_mutexlock);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001173 if (unlikely(list_empty(&bam_rx_pool))) {
Eric Holmberg00cf8692012-07-16 14:21:19 -06001174 DMUX_LOG_KERR(
1175 "%s: have iovec %p but rx pool empty\n",
1176 __func__, (void *)iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001177 mutex_unlock(&bam_rx_pool_mutexlock);
1178 continue;
1179 }
1180 info = list_first_entry(&bam_rx_pool,
1181 struct rx_pkt_info, list_node);
Eric Holmberg00cf8692012-07-16 14:21:19 -06001182 if (info->dma_address != iov.addr) {
1183 DMUX_LOG_KERR("%s: iovec %p != dma %p\n",
1184 __func__,
1185 (void *)iov.addr,
1186 (void *)info->dma_address);
1187 list_for_each_entry(info, &bam_rx_pool,
1188 list_node) {
1189 DMUX_LOG_KERR("%s: dma %p\n", __func__,
1190 (void *)info->dma_address);
1191 if (iov.addr == info->dma_address)
1192 break;
1193 }
1194 }
1195 BUG_ON(info->dma_address != iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001196 list_del(&info->list_node);
Eric Holmberg00cf8692012-07-16 14:21:19 -06001197 --bam_rx_pool_len;
Jeff Hugoc9749932011-11-02 17:50:40 -06001198 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -06001199 handle_bam_mux_cmd(&info->work);
1200 }
1201
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001202 if (inactive_cycles >= POLLING_INACTIVITY) {
Jeff Hugo7c185602013-09-11 17:39:54 -06001203 BAM_DMUX_LOG("%s: polling exit, no data\n", __func__);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001204 rx_switch_to_interrupt_mode();
1205 break;
Jeff Hugo949080a2011-08-30 11:58:56 -06001206 }
1207
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001208 if (bam_adaptive_timer_enabled) {
1209 usleep_range(rx_timer_interval, rx_timer_interval + 50);
1210
1211 ret = sps_get_unused_desc_num(bam_rx_pipe,
1212 &buffs_unused);
1213
1214 if (ret) {
Jeff Hugo7c185602013-09-11 17:39:54 -06001215 DMUX_LOG_KERR(
1216 "%s: error getting num buffers unused after sleep\n",
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001217 __func__);
1218
1219 break;
1220 }
1221
1222 buffs_used = NUM_BUFFERS - buffs_unused;
1223
1224 if (buffs_unused == 0) {
1225 rx_timer_interval = MIN_POLLING_SLEEP;
1226 } else {
1227 if (buffs_used > 0) {
1228 rx_timer_interval =
1229 (2 * NUM_BUFFERS *
1230 rx_timer_interval)/
1231 (3 * buffs_used);
1232 } else {
1233 rx_timer_interval =
1234 MAX_POLLING_SLEEP;
1235 }
1236 }
1237
1238 if (rx_timer_interval > MAX_POLLING_SLEEP)
1239 rx_timer_interval = MAX_POLLING_SLEEP;
1240 else if (rx_timer_interval < MIN_POLLING_SLEEP)
1241 rx_timer_interval = MIN_POLLING_SLEEP;
1242 } else {
1243 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
1244 }
Jeff Hugo949080a2011-08-30 11:58:56 -06001245 }
1246}
1247
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001248static void bam_mux_tx_notify(struct sps_event_notify *notify)
1249{
1250 struct tx_pkt_info *pkt;
1251
1252 DBG("%s: event %d notified\n", __func__, notify->event_id);
1253
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001254 if (in_global_reset)
1255 return;
1256
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001257 switch (notify->event_id) {
1258 case SPS_EVENT_EOT:
1259 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001260 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001261 dma_unmap_single(NULL, pkt->dma_address,
1262 pkt->skb->len,
1263 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001264 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001265 dma_unmap_single(NULL, pkt->dma_address,
1266 pkt->len,
1267 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001268 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001269 break;
1270 default:
1271 pr_err("%s: recieved unexpected event id %d\n", __func__,
1272 notify->event_id);
1273 }
1274}
1275
Jeff Hugo33dbc002011-08-25 15:52:53 -06001276static void bam_mux_rx_notify(struct sps_event_notify *notify)
1277{
Jeff Hugo949080a2011-08-30 11:58:56 -06001278 int ret;
1279 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -06001280
1281 DBG("%s: event %d notified\n", __func__, notify->event_id);
1282
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001283 if (in_global_reset)
1284 return;
1285
Jeff Hugo33dbc002011-08-25 15:52:53 -06001286 switch (notify->event_id) {
1287 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -06001288 /* attempt to disable interrupts in this pipe */
1289 if (!polling_mode) {
1290 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
1291 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001292 pr_err("%s: sps_get_config() failed %d, interrupts"
1293 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001294 break;
1295 }
Jeff Hugoa9d32ba2011-11-21 14:59:48 -07001296 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
Jeff Hugo949080a2011-08-30 11:58:56 -06001297 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
1298 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
1299 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001300 pr_err("%s: sps_set_config() failed %d, interrupts"
1301 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001302 break;
1303 }
Brent Hronik096f7d32013-06-28 15:43:08 -06001304 INIT_COMPLETION(shutdown_completion);
Eric Holmberg006057d2012-01-11 10:10:42 -07001305 grab_wakelock();
Jeff Hugo949080a2011-08-30 11:58:56 -06001306 polling_mode = 1;
Jeff Hugofff43af92012-03-29 17:54:52 -06001307 /*
1308 * run on core 0 so that netif_rx() in rmnet uses only
1309 * one queue
1310 */
1311 queue_work_on(0, bam_mux_rx_workqueue, &rx_timer_work);
Jeff Hugo949080a2011-08-30 11:58:56 -06001312 }
Jeff Hugo33dbc002011-08-25 15:52:53 -06001313 break;
1314 default:
1315 pr_err("%s: recieved unexpected event id %d\n", __func__,
1316 notify->event_id);
1317 }
1318}
1319
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001320#ifdef CONFIG_DEBUG_FS
1321
1322static int debug_tbl(char *buf, int max)
1323{
1324 int i = 0;
1325 int j;
1326
1327 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
1328 i += scnprintf(buf + i, max - i,
1329 "ch%02d local open=%s remote open=%s\n",
1330 j, bam_ch_is_local_open(j) ? "Y" : "N",
1331 bam_ch_is_remote_open(j) ? "Y" : "N");
1332 }
1333
1334 return i;
1335}
1336
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001337static int debug_ul_pkt_cnt(char *buf, int max)
1338{
1339 struct list_head *p;
1340 unsigned long flags;
1341 int n = 0;
1342
1343 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
1344 __list_for_each(p, &bam_tx_pool) {
1345 ++n;
1346 }
1347 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
1348
1349 return scnprintf(buf, max, "Number of UL packets in flight: %d\n", n);
1350}
1351
1352static int debug_stats(char *buf, int max)
1353{
1354 int i = 0;
1355
1356 i += scnprintf(buf + i, max - i,
Eric Holmberg9fdef262012-02-14 11:46:05 -07001357 "skb read cnt: %u\n"
1358 "skb write cnt: %u\n"
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001359 "skb copy cnt: %u\n"
1360 "skb copy bytes: %u\n"
Eric Holmberg6074aba2012-01-18 17:59:44 -07001361 "sps tx failures: %u\n"
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001362 "sps tx stalls: %u\n"
Eric Holmberg1f1255d2012-02-22 13:37:21 -07001363 "rx queue len: %d\n"
1364 "a2 ack out cnt: %d\n"
1365 "a2 ack in cnt: %d\n"
1366 "a2 pwr cntl in: %d\n",
Eric Holmberg9fdef262012-02-14 11:46:05 -07001367 bam_dmux_read_cnt,
1368 bam_dmux_write_cnt,
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001369 bam_dmux_write_cpy_cnt,
1370 bam_dmux_write_cpy_bytes,
Eric Holmberg6074aba2012-01-18 17:59:44 -07001371 bam_dmux_tx_sps_failure_cnt,
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001372 bam_dmux_tx_stall_cnt,
Eric Holmberg1f1255d2012-02-22 13:37:21 -07001373 bam_rx_pool_len,
1374 atomic_read(&bam_dmux_ack_out_cnt),
1375 atomic_read(&bam_dmux_ack_in_cnt),
1376 atomic_read(&bam_dmux_a2_pwr_cntl_in_cnt)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001377 );
1378
1379 return i;
1380}
1381
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001382#define DEBUG_BUFMAX 4096
1383static char debug_buffer[DEBUG_BUFMAX];
1384
1385static ssize_t debug_read(struct file *file, char __user *buf,
1386 size_t count, loff_t *ppos)
1387{
1388 int (*fill)(char *buf, int max) = file->private_data;
1389 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
1390 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
1391}
1392
1393static int debug_open(struct inode *inode, struct file *file)
1394{
1395 file->private_data = inode->i_private;
1396 return 0;
1397}
1398
1399
1400static const struct file_operations debug_ops = {
1401 .read = debug_read,
1402 .open = debug_open,
1403};
1404
1405static void debug_create(const char *name, mode_t mode,
1406 struct dentry *dent,
1407 int (*fill)(char *buf, int max))
1408{
Eric Holmberge4ac80b2012-01-12 09:21:59 -07001409 struct dentry *file;
1410
1411 file = debugfs_create_file(name, mode, dent, fill, &debug_ops);
1412 if (IS_ERR(file))
1413 pr_err("%s: debugfs create failed %d\n", __func__,
1414 (int)PTR_ERR(file));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001415}
1416
1417#endif
1418
Jeff Hugod98b1082011-10-24 10:30:23 -06001419static void notify_all(int event, unsigned long data)
1420{
1421 int i;
Jeff Hugocb798022012-04-09 14:55:40 -06001422 struct list_head *temp;
1423 struct outside_notify_func *func;
Jeff Hugod98b1082011-10-24 10:30:23 -06001424
Jeff Hugoac8152a2013-04-19 11:05:19 -06001425 BAM_DMUX_LOG("%s: event=%d, data=%lu\n", __func__, event, data);
1426
Jeff Hugod98b1082011-10-24 10:30:23 -06001427 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
Jeff Hugoac8152a2013-04-19 11:05:19 -06001428 if (bam_ch_is_open(i))
Jeff Hugod98b1082011-10-24 10:30:23 -06001429 bam_ch[i].notify(bam_ch[i].priv, event, data);
1430 }
Jeff Hugocb798022012-04-09 14:55:40 -06001431
1432 __list_for_each(temp, &bam_other_notify_funcs) {
1433 func = container_of(temp, struct outside_notify_func,
1434 list_node);
1435 func->notify(func->priv, event, data);
1436 }
Jeff Hugod98b1082011-10-24 10:30:23 -06001437}
1438
1439static void kickoff_ul_wakeup_func(struct work_struct *work)
1440{
1441 read_lock(&ul_wakeup_lock);
1442 if (!bam_is_connected) {
1443 read_unlock(&ul_wakeup_lock);
1444 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -07001445 if (unlikely(in_global_reset == 1))
1446 return;
Jeff Hugod98b1082011-10-24 10:30:23 -06001447 read_lock(&ul_wakeup_lock);
1448 ul_packet_written = 1;
1449 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
1450 }
1451 read_unlock(&ul_wakeup_lock);
1452}
1453
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001454int msm_bam_dmux_kickoff_ul_wakeup(void)
Jeff Hugod98b1082011-10-24 10:30:23 -06001455{
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001456 int is_connected;
1457
1458 read_lock(&ul_wakeup_lock);
1459 ul_packet_written = 1;
1460 is_connected = bam_is_connected;
1461 if (!is_connected)
1462 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
1463 read_unlock(&ul_wakeup_lock);
1464
1465 return is_connected;
Jeff Hugod98b1082011-10-24 10:30:23 -06001466}
1467
Eric Holmberg878923a2012-01-10 14:28:19 -07001468static void power_vote(int vote)
1469{
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301470 BAM_DMUX_LOG("%s: curr=%d, vote=%d\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -07001471 bam_dmux_uplink_vote, vote);
1472
1473 if (bam_dmux_uplink_vote == vote)
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301474 BAM_DMUX_LOG("%s: warning - duplicate power vote\n", __func__);
Eric Holmberg878923a2012-01-10 14:28:19 -07001475
1476 bam_dmux_uplink_vote = vote;
1477 if (vote)
1478 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
1479 else
1480 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1481}
1482
Eric Holmberg454d9da2012-01-12 09:37:14 -07001483/*
1484 * @note: Must be called with ul_wakeup_lock locked.
1485 */
1486static inline void ul_powerdown(void)
1487{
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301488 BAM_DMUX_LOG("%s: powerdown\n", __func__);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001489 verify_tx_queue_is_empty(__func__);
1490
1491 if (a2_pc_disabled) {
1492 wait_for_dfab = 1;
1493 INIT_COMPLETION(dfab_unvote_completion);
1494 release_wakelock();
1495 } else {
1496 wait_for_ack = 1;
1497 INIT_COMPLETION(ul_wakeup_ack_completion);
1498 power_vote(0);
1499 }
1500 bam_is_connected = 0;
1501 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
1502}
1503
1504static inline void ul_powerdown_finish(void)
1505{
1506 if (a2_pc_disabled && wait_for_dfab) {
1507 unvote_dfab();
1508 complete_all(&dfab_unvote_completion);
1509 wait_for_dfab = 0;
1510 }
1511}
1512
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001513/*
1514 * Votes for UL power and returns current power state.
1515 *
1516 * @returns true if currently connected
1517 */
1518int msm_bam_dmux_ul_power_vote(void)
1519{
1520 int is_connected;
1521
1522 read_lock(&ul_wakeup_lock);
1523 atomic_inc(&ul_ondemand_vote);
1524 is_connected = bam_is_connected;
1525 if (!is_connected)
1526 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
1527 read_unlock(&ul_wakeup_lock);
1528
1529 return is_connected;
1530}
1531
1532/*
1533 * Unvotes for UL power.
1534 *
1535 * @returns true if vote count is 0 (UL shutdown possible)
1536 */
1537int msm_bam_dmux_ul_power_unvote(void)
1538{
1539 int vote;
1540
1541 read_lock(&ul_wakeup_lock);
1542 vote = atomic_dec_return(&ul_ondemand_vote);
1543 if (unlikely(vote) < 0)
1544 DMUX_LOG_KERR("%s: invalid power vote %d\n", __func__, vote);
1545 read_unlock(&ul_wakeup_lock);
1546
1547 return vote == 0;
1548}
1549
Jeff Hugocb798022012-04-09 14:55:40 -06001550int msm_bam_dmux_reg_notify(void *priv,
1551 void (*notify)(void *priv, int event_type,
1552 unsigned long data))
1553{
1554 struct outside_notify_func *func;
1555
1556 if (!notify)
1557 return -EINVAL;
1558
1559 func = kmalloc(sizeof(struct outside_notify_func), GFP_KERNEL);
1560 if (!func)
1561 return -ENOMEM;
1562
1563 func->notify = notify;
1564 func->priv = priv;
1565 list_add(&func->list_node, &bam_other_notify_funcs);
1566
1567 return 0;
1568}
1569
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001570static void ul_timeout(struct work_struct *work)
1571{
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001572 unsigned long flags;
1573 int ret;
1574
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001575 if (in_global_reset)
1576 return;
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001577 ret = write_trylock_irqsave(&ul_wakeup_lock, flags);
1578 if (!ret) { /* failed to grab lock, reschedule and bail */
1579 schedule_delayed_work(&ul_timeout_work,
1580 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1581 return;
1582 }
Eric Holmberg454d9da2012-01-12 09:37:14 -07001583 if (bam_is_connected) {
Eric Holmberg6074aba2012-01-18 17:59:44 -07001584 if (!ul_packet_written) {
1585 spin_lock(&bam_tx_pool_spinlock);
1586 if (!list_empty(&bam_tx_pool)) {
1587 struct tx_pkt_info *info;
1588
1589 info = list_first_entry(&bam_tx_pool,
1590 struct tx_pkt_info, list_node);
1591 DMUX_LOG_KERR("%s: UL delayed ts=%u.%09lu\n",
1592 __func__, info->ts_sec, info->ts_nsec);
1593 DBG_INC_TX_STALL_CNT();
1594 ul_packet_written = 1;
1595 }
1596 spin_unlock(&bam_tx_pool_spinlock);
1597 }
1598
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001599 if (ul_packet_written || atomic_read(&ul_ondemand_vote)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301600 BAM_DMUX_LOG("%s: pkt written %d\n",
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001601 __func__, ul_packet_written);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001602 ul_packet_written = 0;
1603 schedule_delayed_work(&ul_timeout_work,
1604 msecs_to_jiffies(UL_TIMEOUT_DELAY));
Eric Holmberg006057d2012-01-11 10:10:42 -07001605 } else {
Eric Holmberg454d9da2012-01-12 09:37:14 -07001606 ul_powerdown();
Eric Holmberg006057d2012-01-11 10:10:42 -07001607 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001608 }
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001609 write_unlock_irqrestore(&ul_wakeup_lock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001610 ul_powerdown_finish();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001611}
Jeff Hugo4838f412012-01-20 11:19:37 -07001612
1613static int ssrestart_check(void)
1614{
Jeff Hugob8156d72013-06-04 12:51:10 -06001615 int ret = 0;
1616
Eric Holmberg7614a7f2013-07-29 15:47:12 -06001617 if (in_global_reset) {
1618 DMUX_LOG_KERR("%s: modem timeout: already in SSR\n",
1619 __func__);
1620 return 1;
1621 }
1622
Jeff Hugob8156d72013-06-04 12:51:10 -06001623 DMUX_LOG_KERR("%s: modem timeout: BAM DMUX disabled for SSR\n",
1624 __func__);
Eric Holmberg90285e22012-02-22 12:33:05 -07001625 in_global_reset = 1;
Jeff Hugob8156d72013-06-04 12:51:10 -06001626 ret = subsystem_restart("modem");
1627 if (ret == -ENODEV)
1628 panic("modem subsystem restart failed\n");
Eric Holmberg90285e22012-02-22 12:33:05 -07001629 return 1;
Jeff Hugo4838f412012-01-20 11:19:37 -07001630}
1631
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001632static void ul_wakeup(void)
1633{
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001634 int ret;
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001635 int do_vote_dfab = 0;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001636
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001637 mutex_lock(&wakeup_lock);
1638 if (bam_is_connected) { /* bam got connected before lock grabbed */
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301639 BAM_DMUX_LOG("%s Already awake\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001640 mutex_unlock(&wakeup_lock);
1641 return;
1642 }
Eric Holmberg878923a2012-01-10 14:28:19 -07001643
Jeff Hugoc2696142012-05-03 11:42:13 -06001644 /*
Jeff Hugof5001732012-08-27 13:19:09 -06001645 * if this gets hit, that means restart_notifier_cb() has started
1646 * but probably not finished, thus we know SSR has happened, but
1647 * haven't been able to send that info to our clients yet.
1648 * in that case, abort the ul_wakeup() so that we don't undo any
1649 * work restart_notifier_cb() has done. The clients will be notified
1650 * shortly. No cleanup necessary (reschedule the wakeup) as our and
1651 * their SSR handling will cover it
1652 */
1653 if (unlikely(in_global_reset == 1)) {
1654 mutex_unlock(&wakeup_lock);
1655 return;
1656 }
1657
1658 /*
Jeff Hugoc2696142012-05-03 11:42:13 -06001659 * if someone is voting for UL before bam is inited (modem up first
1660 * time), set flag for init to kickoff ul wakeup once bam is inited
1661 */
1662 mutex_lock(&delayed_ul_vote_lock);
1663 if (unlikely(!bam_mux_initialized)) {
1664 need_delayed_ul_vote = 1;
1665 mutex_unlock(&delayed_ul_vote_lock);
1666 mutex_unlock(&wakeup_lock);
1667 return;
1668 }
1669 mutex_unlock(&delayed_ul_vote_lock);
1670
Eric Holmberg006057d2012-01-11 10:10:42 -07001671 if (a2_pc_disabled) {
1672 /*
1673 * don't grab the wakelock the first time because it is
1674 * already grabbed when a2 powers on
1675 */
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001676 if (likely(a2_pc_disabled_wakelock_skipped)) {
Eric Holmberg006057d2012-01-11 10:10:42 -07001677 grab_wakelock();
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001678 do_vote_dfab = 1; /* vote must occur after wait */
1679 } else {
Jeff Hugo583a6da2012-02-03 11:37:30 -07001680 a2_pc_disabled_wakelock_skipped = 1;
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001681 }
Eric Holmberg006057d2012-01-11 10:10:42 -07001682 if (wait_for_dfab) {
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001683 ret = wait_for_completion_timeout(
Eric Holmberg006057d2012-01-11 10:10:42 -07001684 &dfab_unvote_completion, HZ);
1685 BUG_ON(ret == 0);
1686 }
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001687 if (likely(do_vote_dfab))
1688 vote_dfab();
Eric Holmberg006057d2012-01-11 10:10:42 -07001689 schedule_delayed_work(&ul_timeout_work,
1690 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1691 bam_is_connected = 1;
1692 mutex_unlock(&wakeup_lock);
1693 return;
1694 }
1695
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001696 /*
1697 * must wait for the previous power down request to have been acked
1698 * chances are it already came in and this will just fall through
1699 * instead of waiting
1700 */
1701 if (wait_for_ack) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301702 BAM_DMUX_LOG("%s waiting for previous ack\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001703 ret = wait_for_completion_timeout(
Jeff Hugo1f317392013-07-24 16:28:52 -06001704 &ul_wakeup_ack_completion,
1705 msecs_to_jiffies(UL_WAKEUP_TIMEOUT_MS));
Eric Holmberg006057d2012-01-11 10:10:42 -07001706 wait_for_ack = 0;
Jeff Hugo4838f412012-01-20 11:19:37 -07001707 if (unlikely(ret == 0) && ssrestart_check()) {
1708 mutex_unlock(&wakeup_lock);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301709 BAM_DMUX_LOG("%s timeout previous ack\n", __func__);
Jeff Hugo4838f412012-01-20 11:19:37 -07001710 return;
1711 }
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001712 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001713 INIT_COMPLETION(ul_wakeup_ack_completion);
Eric Holmberg878923a2012-01-10 14:28:19 -07001714 power_vote(1);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301715 BAM_DMUX_LOG("%s waiting for wakeup ack\n", __func__);
Jeff Hugo1f317392013-07-24 16:28:52 -06001716 ret = wait_for_completion_timeout(&ul_wakeup_ack_completion,
1717 msecs_to_jiffies(UL_WAKEUP_TIMEOUT_MS));
Jeff Hugo4838f412012-01-20 11:19:37 -07001718 if (unlikely(ret == 0) && ssrestart_check()) {
1719 mutex_unlock(&wakeup_lock);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301720 BAM_DMUX_LOG("%s timeout wakeup ack\n", __func__);
Jeff Hugo4838f412012-01-20 11:19:37 -07001721 return;
1722 }
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301723 BAM_DMUX_LOG("%s waiting completion\n", __func__);
Jeff Hugo1f317392013-07-24 16:28:52 -06001724 ret = wait_for_completion_timeout(&bam_connection_completion,
1725 msecs_to_jiffies(UL_WAKEUP_TIMEOUT_MS));
Jeff Hugo4838f412012-01-20 11:19:37 -07001726 if (unlikely(ret == 0) && ssrestart_check()) {
1727 mutex_unlock(&wakeup_lock);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301728 BAM_DMUX_LOG("%s timeout power on\n", __func__);
Jeff Hugo4838f412012-01-20 11:19:37 -07001729 return;
1730 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001731
1732 bam_is_connected = 1;
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301733 BAM_DMUX_LOG("%s complete\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001734 schedule_delayed_work(&ul_timeout_work,
1735 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1736 mutex_unlock(&wakeup_lock);
1737}
1738
1739static void reconnect_to_bam(void)
1740{
1741 int i;
1742
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001743 in_global_reset = 0;
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001744 in_ssr = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001745 vote_dfab();
Jeff Hugo18792a32012-06-20 15:25:55 -06001746 if (!power_management_only_mode) {
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001747 if (ssr_skipped_disconnect) {
1748 /* delayed to here to prevent bus stall */
1749 sps_disconnect(bam_tx_pipe);
1750 sps_disconnect(bam_rx_pipe);
1751 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1752 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
1753 }
1754 ssr_skipped_disconnect = 0;
Jeff Hugo18792a32012-06-20 15:25:55 -06001755 i = sps_device_reset(a2_device_handle);
1756 if (i)
1757 pr_err("%s: device reset failed rc = %d\n", __func__,
1758 i);
1759 i = sps_connect(bam_tx_pipe, &tx_connection);
1760 if (i)
1761 pr_err("%s: tx connection failed rc = %d\n", __func__,
1762 i);
1763 i = sps_connect(bam_rx_pipe, &rx_connection);
1764 if (i)
1765 pr_err("%s: rx connection failed rc = %d\n", __func__,
1766 i);
1767 i = sps_register_event(bam_tx_pipe, &tx_register_event);
1768 if (i)
1769 pr_err("%s: tx event reg failed rc = %d\n", __func__,
1770 i);
1771 i = sps_register_event(bam_rx_pipe, &rx_register_event);
1772 if (i)
1773 pr_err("%s: rx event reg failed rc = %d\n", __func__,
1774 i);
1775 }
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001776
1777 bam_connection_is_active = 1;
1778
1779 if (polling_mode)
1780 rx_switch_to_interrupt_mode();
1781
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001782 toggle_apps_ack();
1783 complete_all(&bam_connection_completion);
Jeff Hugo18792a32012-06-20 15:25:55 -06001784 if (!power_management_only_mode)
1785 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001786}
1787
1788static void disconnect_to_bam(void)
1789{
1790 struct list_head *node;
1791 struct rx_pkt_info *info;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001792 unsigned long flags;
Brent Hronik096f7d32013-06-28 15:43:08 -06001793 unsigned long time_remaining;
1794
Eric Holmberg7614a7f2013-07-29 15:47:12 -06001795 if (!in_global_reset) {
1796 time_remaining = wait_for_completion_timeout(
1797 &shutdown_completion,
1798 msecs_to_jiffies(SHUTDOWN_TIMEOUT_MS));
1799 if (time_remaining == 0) {
1800 DMUX_LOG_KERR("%s: shutdown completion timed out\n",
1801 __func__);
Jeff Hugo7c185602013-09-11 17:39:54 -06001802 log_rx_timestamp();
Eric Holmberg7614a7f2013-07-29 15:47:12 -06001803 ssrestart_check();
1804 }
Brent Hronik096f7d32013-06-28 15:43:08 -06001805 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001806
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001807 bam_connection_is_active = 0;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001808
1809 /* handle disconnect during active UL */
1810 write_lock_irqsave(&ul_wakeup_lock, flags);
1811 if (bam_is_connected) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301812 BAM_DMUX_LOG("%s: UL active - forcing powerdown\n", __func__);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001813 ul_powerdown();
1814 }
1815 write_unlock_irqrestore(&ul_wakeup_lock, flags);
1816 ul_powerdown_finish();
1817
1818 /* tear down BAM connection */
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001819 INIT_COMPLETION(bam_connection_completion);
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001820
1821 /* in_ssr documentation/assumptions found in restart_notifier_cb */
Jeff Hugo18792a32012-06-20 15:25:55 -06001822 if (!power_management_only_mode) {
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001823 if (likely(!in_ssr)) {
Jeff Hugof7ae7a62013-04-19 11:18:32 -06001824 BAM_DMUX_LOG("%s: disconnect tx\n", __func__);
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001825 sps_disconnect(bam_tx_pipe);
Jeff Hugof7ae7a62013-04-19 11:18:32 -06001826 BAM_DMUX_LOG("%s: disconnect rx\n", __func__);
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001827 sps_disconnect(bam_rx_pipe);
1828 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1829 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
Jeff Hugof7ae7a62013-04-19 11:18:32 -06001830 BAM_DMUX_LOG("%s: device reset\n", __func__);
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001831 sps_device_reset(a2_device_handle);
1832 } else {
1833 ssr_skipped_disconnect = 1;
1834 }
Jeff Hugo18792a32012-06-20 15:25:55 -06001835 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001836 unvote_dfab();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001837
1838 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001839 while (!list_empty(&bam_rx_pool)) {
1840 node = bam_rx_pool.next;
1841 list_del(node);
1842 info = container_of(node, struct rx_pkt_info, list_node);
1843 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
1844 DMA_FROM_DEVICE);
1845 dev_kfree_skb_any(info->skb);
1846 kfree(info);
1847 }
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001848 bam_rx_pool_len = 0;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001849 mutex_unlock(&bam_rx_pool_mutexlock);
Eric Holmberg878923a2012-01-10 14:28:19 -07001850
Jeff Hugo0b13a352012-03-17 23:18:30 -06001851 if (disconnect_ack)
1852 toggle_apps_ack();
1853
Eric Holmberg878923a2012-01-10 14:28:19 -07001854 verify_tx_queue_is_empty(__func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001855}
1856
1857static void vote_dfab(void)
1858{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001859 int rc;
1860
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301861 BAM_DMUX_LOG("%s\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001862 mutex_lock(&dfab_status_lock);
1863 if (dfab_is_on) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301864 BAM_DMUX_LOG("%s: dfab is already on\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001865 mutex_unlock(&dfab_status_lock);
1866 return;
1867 }
Jeff Hugod0befde2012-08-09 15:32:49 -06001868 if (dfab_clk) {
1869 rc = clk_prepare_enable(dfab_clk);
1870 if (rc)
1871 DMUX_LOG_KERR("bam_dmux vote for dfab failed rc = %d\n",
1872 rc);
1873 }
1874 if (xo_clk) {
1875 rc = clk_prepare_enable(xo_clk);
1876 if (rc)
1877 DMUX_LOG_KERR("bam_dmux vote for xo failed rc = %d\n",
1878 rc);
1879 }
Eric Holmberg006057d2012-01-11 10:10:42 -07001880 dfab_is_on = 1;
1881 mutex_unlock(&dfab_status_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001882}
1883
1884static void unvote_dfab(void)
1885{
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301886 BAM_DMUX_LOG("%s\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001887 mutex_lock(&dfab_status_lock);
1888 if (!dfab_is_on) {
1889 DMUX_LOG_KERR("%s: dfab is already off\n", __func__);
1890 dump_stack();
1891 mutex_unlock(&dfab_status_lock);
1892 return;
1893 }
Jeff Hugod0befde2012-08-09 15:32:49 -06001894 if (dfab_clk)
1895 clk_disable_unprepare(dfab_clk);
1896 if (xo_clk)
1897 clk_disable_unprepare(xo_clk);
Eric Holmberg006057d2012-01-11 10:10:42 -07001898 dfab_is_on = 0;
1899 mutex_unlock(&dfab_status_lock);
1900}
1901
1902/* reference counting wrapper around wakelock */
1903static void grab_wakelock(void)
1904{
1905 unsigned long flags;
1906
1907 spin_lock_irqsave(&wakelock_reference_lock, flags);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301908 BAM_DMUX_LOG("%s: ref count = %d\n", __func__,
Eric Holmberg006057d2012-01-11 10:10:42 -07001909 wakelock_reference_count);
1910 if (wakelock_reference_count == 0)
1911 wake_lock(&bam_wakelock);
1912 ++wakelock_reference_count;
1913 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1914}
1915
1916static void release_wakelock(void)
1917{
1918 unsigned long flags;
1919
1920 spin_lock_irqsave(&wakelock_reference_lock, flags);
1921 if (wakelock_reference_count == 0) {
1922 DMUX_LOG_KERR("%s: bam_dmux wakelock not locked\n", __func__);
1923 dump_stack();
1924 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1925 return;
1926 }
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301927 BAM_DMUX_LOG("%s: ref count = %d\n", __func__,
Eric Holmberg006057d2012-01-11 10:10:42 -07001928 wakelock_reference_count);
1929 --wakelock_reference_count;
1930 if (wakelock_reference_count == 0)
1931 wake_unlock(&bam_wakelock);
1932 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001933}
1934
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001935static int restart_notifier_cb(struct notifier_block *this,
1936 unsigned long code,
1937 void *data)
1938{
1939 int i;
1940 struct list_head *node;
1941 struct tx_pkt_info *info;
1942 int temp_remote_status;
Jeff Hugo626303bf2011-11-21 11:43:28 -07001943 unsigned long flags;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001944
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001945 /*
1946 * Bam_dmux counts on the fact that the BEFORE_SHUTDOWN level of
1947 * notifications are guarenteed to execute before the AFTER_SHUTDOWN
1948 * level of notifications, and that BEFORE_SHUTDOWN always occurs in
1949 * all SSR events, no matter what triggered the SSR. Also, bam_dmux
1950 * assumes that SMD does its SSR processing in the AFTER_SHUTDOWN level
1951 * thus bam_dmux is guarenteed to detect SSR before SMD, since the
1952 * callbacks for all the drivers within the AFTER_SHUTDOWN level could
1953 * occur in any order. Bam_dmux uses this knowledge to skip accessing
1954 * the bam hardware when disconnect_to_bam() is triggered by SMD's SSR
1955 * processing. We do not wat to access the bam hardware during SSR
1956 * because a watchdog crash from a bus stall would likely occur.
1957 */
Jeff Hugo199294b2013-02-25 13:46:56 -07001958 if (code == SUBSYS_BEFORE_SHUTDOWN) {
1959 in_global_reset = 1;
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001960 in_ssr = 1;
Zaheerulla Meerf800bba2013-02-13 15:49:14 +05301961 BAM_DMUX_LOG("%s: begin\n", __func__);
Jeff Hugo199294b2013-02-25 13:46:56 -07001962 flush_workqueue(bam_mux_rx_workqueue);
1963 }
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001964 if (code != SUBSYS_AFTER_SHUTDOWN)
1965 return NOTIFY_DONE;
1966
Eric Holmberg454d9da2012-01-12 09:37:14 -07001967 /* Handle uplink Powerdown */
1968 write_lock_irqsave(&ul_wakeup_lock, flags);
1969 if (bam_is_connected) {
1970 ul_powerdown();
1971 wait_for_ack = 0;
1972 }
Jeff Hugo4838f412012-01-20 11:19:37 -07001973 /*
1974 * if modem crash during ul_wakeup(), power_vote is 1, needs to be
1975 * reset to 0. harmless if bam_is_connected check above passes
1976 */
1977 power_vote(0);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001978 write_unlock_irqrestore(&ul_wakeup_lock, flags);
1979 ul_powerdown_finish();
Eric Holmberg006057d2012-01-11 10:10:42 -07001980 a2_pc_disabled = 0;
Jeff Hugo583a6da2012-02-03 11:37:30 -07001981 a2_pc_disabled_wakelock_skipped = 0;
Jeff Hugof62029d2012-07-17 13:39:53 -06001982 disconnect_ack = 1;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001983
1984 /* Cleanup Channel States */
Eric Holmberga623da82012-07-12 09:37:09 -06001985 mutex_lock(&bam_pdev_mutexlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001986 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
1987 temp_remote_status = bam_ch_is_remote_open(i);
1988 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001989 bam_ch[i].num_tx_pkts = 0;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001990 if (bam_ch_is_local_open(i))
1991 bam_ch[i].status |= BAM_CH_IN_RESET;
1992 if (temp_remote_status) {
1993 platform_device_unregister(bam_ch[i].pdev);
1994 bam_ch[i].pdev = platform_device_alloc(
1995 bam_ch[i].name, 2);
1996 }
1997 }
Eric Holmberga623da82012-07-12 09:37:09 -06001998 mutex_unlock(&bam_pdev_mutexlock);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001999
2000 /* Cleanup pending UL data */
Jeff Hugo626303bf2011-11-21 11:43:28 -07002001 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002002 while (!list_empty(&bam_tx_pool)) {
2003 node = bam_tx_pool.next;
2004 list_del(node);
2005 info = container_of(node, struct tx_pkt_info,
2006 list_node);
2007 if (!info->is_cmd) {
2008 dma_unmap_single(NULL, info->dma_address,
2009 info->skb->len,
2010 DMA_TO_DEVICE);
2011 dev_kfree_skb_any(info->skb);
2012 } else {
2013 dma_unmap_single(NULL, info->dma_address,
2014 info->len,
2015 DMA_TO_DEVICE);
2016 kfree(info->skb);
2017 }
2018 kfree(info);
2019 }
Jeff Hugo626303bf2011-11-21 11:43:28 -07002020 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07002021
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302022 BAM_DMUX_LOG("%s: complete\n", __func__);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002023 return NOTIFY_DONE;
2024}
2025
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002026static int bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002027{
2028 u32 h;
2029 dma_addr_t dma_addr;
2030 int ret;
2031 void *a2_virt_addr;
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002032 int skip_iounmap = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002033
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002034 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002035 /* init BAM */
Jeff Hugo3910ee12012-08-21 14:08:20 -06002036 a2_virt_addr = ioremap_nocache((unsigned long)(a2_phys_base),
2037 a2_phys_size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002038 if (!a2_virt_addr) {
2039 pr_err("%s: ioremap failed\n", __func__);
2040 ret = -ENOMEM;
Jeff Hugo994a92d2012-01-05 13:25:21 -07002041 goto ioremap_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002042 }
Jeff Hugo3910ee12012-08-21 14:08:20 -06002043 a2_props.phys_addr = (u32)(a2_phys_base);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002044 a2_props.virt_addr = a2_virt_addr;
Jeff Hugo3910ee12012-08-21 14:08:20 -06002045 a2_props.virt_size = a2_phys_size;
2046 a2_props.irq = a2_bam_irq;
Jeff Hugo927cba62011-11-11 11:49:52 -07002047 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002048 a2_props.num_pipes = A2_NUM_PIPES;
2049 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
Jeff Hugo75913c82011-12-05 15:59:01 -07002050 if (cpu_is_msm9615())
2051 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002052 /* need to free on tear down */
2053 ret = sps_register_bam_device(&a2_props, &h);
2054 if (ret < 0) {
2055 pr_err("%s: register bam error %d\n", __func__, ret);
2056 goto register_bam_failed;
2057 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002058 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002059
2060 bam_tx_pipe = sps_alloc_endpoint();
2061 if (bam_tx_pipe == NULL) {
2062 pr_err("%s: tx alloc endpoint failed\n", __func__);
2063 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002064 goto tx_alloc_endpoint_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002065 }
2066 ret = sps_get_config(bam_tx_pipe, &tx_connection);
2067 if (ret) {
2068 pr_err("%s: tx get config failed %d\n", __func__, ret);
2069 goto tx_get_config_failed;
2070 }
2071
2072 tx_connection.source = SPS_DEV_HANDLE_MEM;
2073 tx_connection.src_pipe_index = 0;
2074 tx_connection.destination = h;
2075 tx_connection.dest_pipe_index = 4;
2076 tx_connection.mode = SPS_MODE_DEST;
2077 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
2078 tx_desc_mem_buf.size = 0x800; /* 2k */
2079 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
2080 &dma_addr, 0);
2081 if (tx_desc_mem_buf.base == NULL) {
2082 pr_err("%s: tx memory alloc failed\n", __func__);
2083 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002084 goto tx_get_config_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002085 }
2086 tx_desc_mem_buf.phys_base = dma_addr;
2087 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
2088 tx_connection.desc = tx_desc_mem_buf;
2089 tx_connection.event_thresh = 0x10;
2090
2091 ret = sps_connect(bam_tx_pipe, &tx_connection);
2092 if (ret < 0) {
2093 pr_err("%s: tx connect error %d\n", __func__, ret);
2094 goto tx_connect_failed;
2095 }
2096
2097 bam_rx_pipe = sps_alloc_endpoint();
2098 if (bam_rx_pipe == NULL) {
2099 pr_err("%s: rx alloc endpoint failed\n", __func__);
2100 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002101 goto rx_alloc_endpoint_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002102 }
2103 ret = sps_get_config(bam_rx_pipe, &rx_connection);
2104 if (ret) {
2105 pr_err("%s: rx get config failed %d\n", __func__, ret);
2106 goto rx_get_config_failed;
2107 }
2108
2109 rx_connection.source = h;
2110 rx_connection.src_pipe_index = 5;
2111 rx_connection.destination = SPS_DEV_HANDLE_MEM;
2112 rx_connection.dest_pipe_index = 1;
2113 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06002114 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
2115 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002116 rx_desc_mem_buf.size = 0x800; /* 2k */
2117 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
2118 &dma_addr, 0);
2119 if (rx_desc_mem_buf.base == NULL) {
2120 pr_err("%s: rx memory alloc failed\n", __func__);
2121 ret = -ENOMEM;
2122 goto rx_mem_failed;
2123 }
2124 rx_desc_mem_buf.phys_base = dma_addr;
2125 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
2126 rx_connection.desc = rx_desc_mem_buf;
2127 rx_connection.event_thresh = 0x10;
2128
2129 ret = sps_connect(bam_rx_pipe, &rx_connection);
2130 if (ret < 0) {
2131 pr_err("%s: rx connect error %d\n", __func__, ret);
2132 goto rx_connect_failed;
2133 }
2134
2135 tx_register_event.options = SPS_O_EOT;
2136 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
2137 tx_register_event.xfer_done = NULL;
2138 tx_register_event.callback = bam_mux_tx_notify;
2139 tx_register_event.user = NULL;
2140 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
2141 if (ret < 0) {
2142 pr_err("%s: tx register event error %d\n", __func__, ret);
2143 goto rx_event_reg_failed;
2144 }
2145
Jeff Hugo33dbc002011-08-25 15:52:53 -06002146 rx_register_event.options = SPS_O_EOT;
2147 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
2148 rx_register_event.xfer_done = NULL;
2149 rx_register_event.callback = bam_mux_rx_notify;
2150 rx_register_event.user = NULL;
2151 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
2152 if (ret < 0) {
2153 pr_err("%s: tx register event error %d\n", __func__, ret);
2154 goto rx_event_reg_failed;
2155 }
2156
Jeff Hugoc2696142012-05-03 11:42:13 -06002157 mutex_lock(&delayed_ul_vote_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002158 bam_mux_initialized = 1;
Jeff Hugoc2696142012-05-03 11:42:13 -06002159 if (need_delayed_ul_vote) {
2160 need_delayed_ul_vote = 0;
2161 msm_bam_dmux_kickoff_ul_wakeup();
2162 }
2163 mutex_unlock(&delayed_ul_vote_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002164 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002165 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002166 complete_all(&bam_connection_completion);
Jeff Hugo2fb555e2012-03-14 16:33:47 -06002167 queue_rx();
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002168 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002169
2170rx_event_reg_failed:
2171 sps_disconnect(bam_rx_pipe);
2172rx_connect_failed:
2173 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
2174 rx_desc_mem_buf.phys_base);
2175rx_mem_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002176rx_get_config_failed:
2177 sps_free_endpoint(bam_rx_pipe);
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002178rx_alloc_endpoint_failed:
2179 sps_disconnect(bam_tx_pipe);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002180tx_connect_failed:
2181 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
2182 tx_desc_mem_buf.phys_base);
2183tx_get_config_failed:
2184 sps_free_endpoint(bam_tx_pipe);
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002185tx_alloc_endpoint_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002186 sps_deregister_bam_device(h);
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002187 /*
2188 * sps_deregister_bam_device() calls iounmap. calling iounmap on the
2189 * same handle below will cause a crash, so skip it if we've freed
2190 * the handle here.
2191 */
2192 skip_iounmap = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002193register_bam_failed:
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002194 if (!skip_iounmap)
2195 iounmap(a2_virt_addr);
Jeff Hugo994a92d2012-01-05 13:25:21 -07002196ioremap_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002197 /*destroy_workqueue(bam_mux_workqueue);*/
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002198 return ret;
2199}
2200
2201static int bam_init_fallback(void)
2202{
2203 u32 h;
2204 int ret;
2205 void *a2_virt_addr;
2206
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002207 /* init BAM */
Jeff Hugo3910ee12012-08-21 14:08:20 -06002208 a2_virt_addr = ioremap_nocache((unsigned long)(a2_phys_base),
2209 a2_phys_size);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002210 if (!a2_virt_addr) {
2211 pr_err("%s: ioremap failed\n", __func__);
2212 ret = -ENOMEM;
2213 goto ioremap_failed;
2214 }
Jeff Hugo3910ee12012-08-21 14:08:20 -06002215 a2_props.phys_addr = (u32)(a2_phys_base);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002216 a2_props.virt_addr = a2_virt_addr;
Jeff Hugo3910ee12012-08-21 14:08:20 -06002217 a2_props.virt_size = a2_phys_size;
2218 a2_props.irq = a2_bam_irq;
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002219 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
2220 a2_props.num_pipes = A2_NUM_PIPES;
2221 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
2222 if (cpu_is_msm9615())
2223 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
2224 ret = sps_register_bam_device(&a2_props, &h);
2225 if (ret < 0) {
2226 pr_err("%s: register bam error %d\n", __func__, ret);
2227 goto register_bam_failed;
2228 }
2229 a2_device_handle = h;
Jeff Hugoc2696142012-05-03 11:42:13 -06002230
2231 mutex_lock(&delayed_ul_vote_lock);
2232 bam_mux_initialized = 1;
2233 if (need_delayed_ul_vote) {
2234 need_delayed_ul_vote = 0;
2235 msm_bam_dmux_kickoff_ul_wakeup();
2236 }
2237 mutex_unlock(&delayed_ul_vote_lock);
Jeff Hugo2bec9772012-04-05 12:25:16 -06002238 toggle_apps_ack();
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002239
Jeff Hugo18792a32012-06-20 15:25:55 -06002240 power_management_only_mode = 1;
2241 bam_connection_is_active = 1;
2242 complete_all(&bam_connection_completion);
2243
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002244 return 0;
2245
2246register_bam_failed:
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002247 iounmap(a2_virt_addr);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002248ioremap_failed:
2249 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002250}
Jeff Hugoade1f842011-08-03 15:53:59 -06002251
Jeff Hugoa670b762012-03-15 15:58:28 -06002252static void msm9615_bam_init(void)
Eric Holmberg604ab252012-01-15 00:01:18 -07002253{
2254 int ret = 0;
2255
2256 ret = bam_init();
2257 if (ret) {
2258 ret = bam_init_fallback();
2259 if (ret)
2260 pr_err("%s: bam init fallback failed: %d",
2261 __func__, ret);
2262 }
2263}
2264
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002265static void toggle_apps_ack(void)
2266{
2267 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
Eric Holmberg878923a2012-01-10 14:28:19 -07002268
Eric Holmberg7614a7f2013-07-29 15:47:12 -06002269 if (in_global_reset) {
2270 BAM_DMUX_LOG("%s: skipped due to SSR\n", __func__);
2271 return;
2272 }
2273
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302274 BAM_DMUX_LOG("%s: apps ack %d->%d\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -07002275 clear_bit & 0x1, ~clear_bit & 0x1);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002276 smsm_change_state(SMSM_APPS_STATE,
2277 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
2278 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
2279 clear_bit = ~clear_bit;
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002280 DBG_INC_ACK_OUT_CNT();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002281}
2282
Jeff Hugoade1f842011-08-03 15:53:59 -06002283static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
2284{
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002285 static int last_processed_state;
2286
2287 mutex_lock(&smsm_cb_lock);
Eric Holmberg878923a2012-01-10 14:28:19 -07002288 bam_dmux_power_state = new_state & SMSM_A2_POWER_CONTROL ? 1 : 0;
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002289 DBG_INC_A2_POWER_CONTROL_IN_CNT();
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302290 BAM_DMUX_LOG("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
Eric Holmberg878923a2012-01-10 14:28:19 -07002291 new_state);
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002292 if (last_processed_state == (new_state & SMSM_A2_POWER_CONTROL)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302293 BAM_DMUX_LOG("%s: already processed this state\n", __func__);
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002294 mutex_unlock(&smsm_cb_lock);
2295 return;
2296 }
2297
2298 last_processed_state = new_state & SMSM_A2_POWER_CONTROL;
Eric Holmberg878923a2012-01-10 14:28:19 -07002299
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002300 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302301 BAM_DMUX_LOG("%s: reconnect\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07002302 grab_wakelock();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002303 reconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002304 } else if (bam_mux_initialized &&
2305 !(new_state & SMSM_A2_POWER_CONTROL)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302306 BAM_DMUX_LOG("%s: disconnect\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002307 disconnect_to_bam();
Eric Holmberg006057d2012-01-11 10:10:42 -07002308 release_wakelock();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002309 } else if (new_state & SMSM_A2_POWER_CONTROL) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302310 BAM_DMUX_LOG("%s: init\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07002311 grab_wakelock();
Jeff Hugoa670b762012-03-15 15:58:28 -06002312 if (cpu_is_msm9615())
2313 msm9615_bam_init();
2314 else
Eric Holmberg604ab252012-01-15 00:01:18 -07002315 bam_init();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002316 } else {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302317 BAM_DMUX_LOG("%s: bad state change\n", __func__);
Jeff Hugoade1f842011-08-03 15:53:59 -06002318 pr_err("%s: unsupported state change\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002319 }
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002320 mutex_unlock(&smsm_cb_lock);
Jeff Hugoade1f842011-08-03 15:53:59 -06002321
2322}
2323
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002324static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
2325 uint32_t new_state)
2326{
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002327 DBG_INC_ACK_IN_CNT();
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302328 BAM_DMUX_LOG("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
Eric Holmberg878923a2012-01-10 14:28:19 -07002329 new_state);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002330 complete_all(&ul_wakeup_ack_completion);
2331}
2332
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002333static int bam_dmux_probe(struct platform_device *pdev)
2334{
2335 int rc;
Jeff Hugo3910ee12012-08-21 14:08:20 -06002336 struct resource *r;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002337
2338 DBG("%s probe called\n", __func__);
2339 if (bam_mux_initialized)
2340 return 0;
2341
Jeff Hugo3910ee12012-08-21 14:08:20 -06002342 if (pdev->dev.of_node) {
2343 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2344 if (!r) {
2345 pr_err("%s: reg field missing\n", __func__);
2346 return -ENODEV;
2347 }
2348 a2_phys_base = (void *)(r->start);
2349 a2_phys_size = (uint32_t)(resource_size(r));
2350 a2_bam_irq = platform_get_irq(pdev, 0);
2351 if (a2_bam_irq == -ENXIO) {
2352 pr_err("%s: irq field missing\n", __func__);
2353 return -ENODEV;
2354 }
2355 DBG("%s: base:%p size:%x irq:%d\n", __func__,
2356 a2_phys_base,
2357 a2_phys_size,
2358 a2_bam_irq);
2359 } else { /* fallback to default init data */
2360 a2_phys_base = (void *)(A2_PHYS_BASE);
2361 a2_phys_size = A2_PHYS_SIZE;
2362 a2_bam_irq = A2_BAM_IRQ;
2363 }
2364
Stephen Boyd69d35e32012-02-14 15:33:30 -08002365 xo_clk = clk_get(&pdev->dev, "xo");
2366 if (IS_ERR(xo_clk)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302367 BAM_DMUX_LOG("%s: did not get xo clock\n", __func__);
Jeff Hugod0befde2012-08-09 15:32:49 -06002368 xo_clk = NULL;
Stephen Boyd69d35e32012-02-14 15:33:30 -08002369 }
Stephen Boyd1c51a492011-10-26 12:11:47 -07002370 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002371 if (IS_ERR(dfab_clk)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302372 BAM_DMUX_LOG("%s: did not get dfab clock\n", __func__);
Jeff Hugod0befde2012-08-09 15:32:49 -06002373 dfab_clk = NULL;
2374 } else {
2375 rc = clk_set_rate(dfab_clk, 64000000);
2376 if (rc)
2377 pr_err("%s: unable to set dfab clock rate\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002378 }
2379
Jeff Hugofff43af92012-03-29 17:54:52 -06002380 /*
2381 * setup the workqueue so that it can be pinned to core 0 and not
2382 * block the watchdog pet function, so that netif_rx() in rmnet
2383 * only uses one queue.
2384 */
2385 bam_mux_rx_workqueue = alloc_workqueue("bam_dmux_rx",
2386 WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002387 if (!bam_mux_rx_workqueue)
2388 return -ENOMEM;
2389
2390 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
2391 if (!bam_mux_tx_workqueue) {
2392 destroy_workqueue(bam_mux_rx_workqueue);
2393 return -ENOMEM;
2394 }
2395
Jeff Hugo7960abd2011-08-02 15:39:38 -06002396 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002397 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06002398 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
2399 "bam_dmux_ch_%d", rc);
2400 /* bus 2, ie a2 stream 2 */
2401 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
2402 if (!bam_ch[rc].pdev) {
2403 pr_err("%s: platform device alloc failed\n", __func__);
2404 destroy_workqueue(bam_mux_rx_workqueue);
2405 destroy_workqueue(bam_mux_tx_workqueue);
2406 return -ENOMEM;
2407 }
2408 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002409
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002410 init_completion(&ul_wakeup_ack_completion);
2411 init_completion(&bam_connection_completion);
Eric Holmberg006057d2012-01-11 10:10:42 -07002412 init_completion(&dfab_unvote_completion);
Brent Hronik096f7d32013-06-28 15:43:08 -06002413 init_completion(&shutdown_completion);
2414 complete_all(&shutdown_completion);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002415 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
Jeff Hugo988e7ba2012-10-03 15:53:54 -06002416 INIT_DELAYED_WORK(&queue_rx_work, queue_rx_work_func);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002417 wake_lock_init(&bam_wakelock, WAKE_LOCK_SUSPEND, "bam_dmux_wakelock");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002418
Jeff Hugoade1f842011-08-03 15:53:59 -06002419 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
2420 bam_dmux_smsm_cb, NULL);
2421
2422 if (rc) {
2423 destroy_workqueue(bam_mux_rx_workqueue);
2424 destroy_workqueue(bam_mux_tx_workqueue);
2425 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
2426 return -ENOMEM;
2427 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002428
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002429 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
2430 bam_dmux_smsm_ack_cb, NULL);
2431
2432 if (rc) {
2433 destroy_workqueue(bam_mux_rx_workqueue);
2434 destroy_workqueue(bam_mux_tx_workqueue);
2435 smsm_state_cb_deregister(SMSM_MODEM_STATE,
2436 SMSM_A2_POWER_CONTROL,
2437 bam_dmux_smsm_cb, NULL);
2438 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
2439 rc);
2440 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
2441 platform_device_put(bam_ch[rc].pdev);
2442 return -ENOMEM;
2443 }
2444
Eric Holmbergfd1e2ae2011-11-15 18:28:17 -07002445 if (smsm_get_state(SMSM_MODEM_STATE) & SMSM_A2_POWER_CONTROL)
2446 bam_dmux_smsm_cb(NULL, 0, smsm_get_state(SMSM_MODEM_STATE));
2447
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002448 return 0;
2449}
2450
Jeff Hugo3910ee12012-08-21 14:08:20 -06002451static struct of_device_id msm_match_table[] = {
2452 {.compatible = "qcom,bam_dmux"},
2453 {},
2454};
2455
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002456static struct platform_driver bam_dmux_driver = {
2457 .probe = bam_dmux_probe,
2458 .driver = {
2459 .name = "BAM_RMNT",
2460 .owner = THIS_MODULE,
Jeff Hugo3910ee12012-08-21 14:08:20 -06002461 .of_match_table = msm_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002462 },
2463};
2464
2465static int __init bam_dmux_init(void)
2466{
2467#ifdef CONFIG_DEBUG_FS
2468 struct dentry *dent;
2469
2470 dent = debugfs_create_dir("bam_dmux", 0);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002471 if (!IS_ERR(dent)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002472 debug_create("tbl", 0444, dent, debug_tbl);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002473 debug_create("ul_pkt_cnt", 0444, dent, debug_ul_pkt_cnt);
2474 debug_create("stats", 0444, dent, debug_stats);
2475 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002476#endif
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302477
2478 bam_ipc_log_txt = ipc_log_context_create(BAM_IPC_LOG_PAGES, "bam_dmux");
2479 if (!bam_ipc_log_txt) {
2480 pr_err("%s : unable to create IPC Logging Context", __func__);
Eric Holmberg878923a2012-01-10 14:28:19 -07002481 }
2482
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07002483 rx_timer_interval = DEFAULT_POLLING_MIN_SLEEP;
2484
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002485 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002486 return platform_driver_register(&bam_dmux_driver);
2487}
2488
Jeff Hugoade1f842011-08-03 15:53:59 -06002489late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002490MODULE_DESCRIPTION("MSM BAM DMUX");
2491MODULE_LICENSE("GPL v2");