blob: 56922a87aa28a8deec8d9031b162e4fa4d9f008e [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;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700197
198static struct bam_ch_info bam_ch[BAM_DMUX_NUM_CHANNELS];
199static int bam_mux_initialized;
200
Jeff Hugo949080a2011-08-30 11:58:56 -0600201static int polling_mode;
Anurag Singhdcd8b4e2012-07-30 16:46:37 -0700202static unsigned long rx_timer_interval;
Jeff Hugo949080a2011-08-30 11:58:56 -0600203
204static LIST_HEAD(bam_rx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600205static DEFINE_MUTEX(bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700206static int bam_rx_pool_len;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600207static LIST_HEAD(bam_tx_pool);
Jeff Hugoc9749932011-11-02 17:50:40 -0600208static DEFINE_SPINLOCK(bam_tx_pool_spinlock);
Eric Holmberga623da82012-07-12 09:37:09 -0600209static DEFINE_MUTEX(bam_pdev_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600210
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700211struct bam_mux_hdr {
212 uint16_t magic_num;
213 uint8_t reserved;
214 uint8_t cmd;
215 uint8_t pad_len;
216 uint8_t ch_id;
217 uint16_t pkt_len;
218};
219
Jeff Hugod98b1082011-10-24 10:30:23 -0600220static void notify_all(int event, unsigned long data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700221static void bam_mux_write_done(struct work_struct *work);
222static void handle_bam_mux_cmd(struct work_struct *work);
Jeff Hugo949080a2011-08-30 11:58:56 -0600223static void rx_timer_work_func(struct work_struct *work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700224
Jeff Hugo949080a2011-08-30 11:58:56 -0600225static DECLARE_WORK(rx_timer_work, rx_timer_work_func);
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600226static struct delayed_work queue_rx_work;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700227
228static struct workqueue_struct *bam_mux_rx_workqueue;
229static struct workqueue_struct *bam_mux_tx_workqueue;
230
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600231/* A2 power collaspe */
232#define UL_TIMEOUT_DELAY 1000 /* in ms */
Jeff Hugo0b13a352012-03-17 23:18:30 -0600233#define ENABLE_DISCONNECT_ACK 0x1
Brent Hronik096f7d32013-06-28 15:43:08 -0600234#define SHUTDOWN_TIMEOUT_MS 500
Jeff Hugo1f317392013-07-24 16:28:52 -0600235#define UL_WAKEUP_TIMEOUT_MS 2000
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600236static void toggle_apps_ack(void);
237static void reconnect_to_bam(void);
238static void disconnect_to_bam(void);
239static void ul_wakeup(void);
240static void ul_timeout(struct work_struct *work);
241static void vote_dfab(void);
242static void unvote_dfab(void);
Jeff Hugod98b1082011-10-24 10:30:23 -0600243static void kickoff_ul_wakeup_func(struct work_struct *work);
Eric Holmberg006057d2012-01-11 10:10:42 -0700244static void grab_wakelock(void);
245static void release_wakelock(void);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600246
247static int bam_is_connected;
248static DEFINE_MUTEX(wakeup_lock);
249static struct completion ul_wakeup_ack_completion;
250static struct completion bam_connection_completion;
251static struct delayed_work ul_timeout_work;
252static int ul_packet_written;
Eric Holmbergbc9f21c2012-01-18 11:33:33 -0700253static atomic_t ul_ondemand_vote = ATOMIC_INIT(0);
Stephen Boyd69d35e32012-02-14 15:33:30 -0800254static struct clk *dfab_clk, *xo_clk;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600255static DEFINE_RWLOCK(ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600256static DECLARE_WORK(kickoff_ul_wakeup, kickoff_ul_wakeup_func);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600257static int bam_connection_is_active;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -0700258static int wait_for_ack;
Jeff Hugoae3a85e2011-12-02 17:10:18 -0700259static struct wake_lock bam_wakelock;
Eric Holmberg006057d2012-01-11 10:10:42 -0700260static int a2_pc_disabled;
261static DEFINE_MUTEX(dfab_status_lock);
262static int dfab_is_on;
263static int wait_for_dfab;
264static struct completion dfab_unvote_completion;
265static DEFINE_SPINLOCK(wakelock_reference_lock);
266static int wakelock_reference_count;
Jeff Hugo583a6da2012-02-03 11:37:30 -0700267static int a2_pc_disabled_wakelock_skipped;
Jeff Hugob1e7c582012-06-20 15:02:11 -0600268static int disconnect_ack = 1;
Jeff Hugocb798022012-04-09 14:55:40 -0600269static LIST_HEAD(bam_other_notify_funcs);
Jeff Hugo4b7c7b32012-04-18 16:25:14 -0600270static DEFINE_MUTEX(smsm_cb_lock);
Jeff Hugoc2696142012-05-03 11:42:13 -0600271static DEFINE_MUTEX(delayed_ul_vote_lock);
272static int need_delayed_ul_vote;
Jeff Hugo18792a32012-06-20 15:25:55 -0600273static int power_management_only_mode;
Jeff Hugoa82a95c2012-12-14 17:56:19 -0700274static int in_ssr;
275static int ssr_skipped_disconnect;
Brent Hronik096f7d32013-06-28 15:43:08 -0600276static struct completion shutdown_completion;
Jeff Hugocb798022012-04-09 14:55:40 -0600277
278struct outside_notify_func {
279 void (*notify)(void *, int, unsigned long);
280 void *priv;
281 struct list_head list_node;
282};
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600283/* End A2 power collaspe */
284
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600285/* subsystem restart */
286static int restart_notifier_cb(struct notifier_block *this,
287 unsigned long code,
288 void *data);
289
290static struct notifier_block restart_notifier = {
291 .notifier_call = restart_notifier_cb,
292};
293static int in_global_reset;
294/* end subsystem restart */
295
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700296#define bam_ch_is_open(x) \
297 (bam_ch[(x)].status == (BAM_CH_LOCAL_OPEN | BAM_CH_REMOTE_OPEN))
298
299#define bam_ch_is_local_open(x) \
300 (bam_ch[(x)].status & BAM_CH_LOCAL_OPEN)
301
302#define bam_ch_is_remote_open(x) \
303 (bam_ch[(x)].status & BAM_CH_REMOTE_OPEN)
304
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600305#define bam_ch_is_in_reset(x) \
306 (bam_ch[(x)].status & BAM_CH_IN_RESET)
307
Eric Holmberg878923a2012-01-10 14:28:19 -0700308struct kfifo bam_dmux_state_log;
Eric Holmberg878923a2012-01-10 14:28:19 -0700309static int bam_dmux_uplink_vote;
310static int bam_dmux_power_state;
311
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530312static void *bam_ipc_log_txt;
313
314#define BAM_IPC_LOG_PAGES 5
315
Eric Holmberg878923a2012-01-10 14:28:19 -0700316/**
317 * Log a state change along with a small message.
Eric Holmberg878923a2012-01-10 14:28:19 -0700318 * Complete size of messsage is limited to @todo.
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530319 * Logging is done using IPC Logging infrastructure.
320 *
321 * States
322 * D: 1 = Power collapse disabled
323 * R: 1 = in global reset
324 * P: 1 = BAM is powered up
325 * A: 1 = BAM initialized and ready for data
326 * V: 1 = Uplink vote for power
327 * U: 1 = Uplink active
328 * W: 1 = Uplink Wait-for-ack
329 * A: 1 = Uplink ACK received
330 * #: >=1 On-demand uplink vote
331 * D: 1 = Disconnect ACK active
Eric Holmberg878923a2012-01-10 14:28:19 -0700332 */
Eric Holmberg878923a2012-01-10 14:28:19 -0700333
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530334#define BAM_DMUX_LOG(fmt, args...) \
335do { \
336 if (bam_ipc_log_txt) { \
337 ipc_log_string(bam_ipc_log_txt, \
338 "<DMUX> %c%c%c%c %c%c%c%c%d%c " fmt, \
339 a2_pc_disabled ? 'D' : 'd', \
340 in_global_reset ? 'R' : 'r', \
341 bam_dmux_power_state ? 'P' : 'p', \
342 bam_connection_is_active ? 'A' : 'a', \
343 bam_dmux_uplink_vote ? 'V' : 'v', \
344 bam_is_connected ? 'U' : 'u', \
345 wait_for_ack ? 'W' : 'w', \
346 ul_wakeup_ack_completion.done ? 'A' : 'a', \
347 atomic_read(&ul_ondemand_vote), \
348 disconnect_ack ? 'D' : 'd', \
349 args); \
350 } \
351} while (0)
Eric Holmberg878923a2012-01-10 14:28:19 -0700352
Zaheerulla Meerf800bba2013-02-13 15:49:14 +0530353#define DMUX_LOG_KERR(fmt, args...) \
354do { \
355 BAM_DMUX_LOG(fmt, args); \
356 pr_err(fmt, args); \
357} while (0)
358
Eric Holmberg878923a2012-01-10 14:28:19 -0700359static inline void set_tx_timestamp(struct tx_pkt_info *pkt)
360{
361 unsigned long long t_now;
362
363 t_now = sched_clock();
364 pkt->ts_nsec = do_div(t_now, 1000000000U);
365 pkt->ts_sec = (unsigned)t_now;
366}
367
368static inline void verify_tx_queue_is_empty(const char *func)
369{
370 unsigned long flags;
371 struct tx_pkt_info *info;
372 int reported = 0;
373
374 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
375 list_for_each_entry(info, &bam_tx_pool, list_node) {
376 if (!reported) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530377 BAM_DMUX_LOG("%s: tx pool not empty\n", func);
Eric Holmberg454d9da2012-01-12 09:37:14 -0700378 if (!in_global_reset)
379 pr_err("%s: tx pool not empty\n", func);
Eric Holmberg878923a2012-01-10 14:28:19 -0700380 reported = 1;
381 }
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530382 BAM_DMUX_LOG("%s: node=%p ts=%u.%09lu\n", __func__,
Eric Holmberg454d9da2012-01-12 09:37:14 -0700383 &info->list_node, info->ts_sec, info->ts_nsec);
384 if (!in_global_reset)
385 pr_err("%s: node=%p ts=%u.%09lu\n", __func__,
386 &info->list_node, info->ts_sec, info->ts_nsec);
Eric Holmberg878923a2012-01-10 14:28:19 -0700387 }
388 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
389}
390
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700391static void queue_rx(void)
392{
393 void *ptr;
394 struct rx_pkt_info *info;
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700395 int ret;
396 int rx_len_cached;
Jeff Hugo949080a2011-08-30 11:58:56 -0600397
Jeff Hugoc9749932011-11-02 17:50:40 -0600398 mutex_lock(&bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700399 rx_len_cached = bam_rx_pool_len;
Jeff Hugoc9749932011-11-02 17:50:40 -0600400 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600401
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600402 while (bam_connection_is_active && rx_len_cached < NUM_BUFFERS) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700403 if (in_global_reset)
404 goto fail;
405
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600406 info = kmalloc(sizeof(struct rx_pkt_info),
407 GFP_NOWAIT | __GFP_NOWARN);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700408 if (!info) {
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600409 DMUX_LOG_KERR(
410 "%s: unable to alloc rx_pkt_info, will retry later\n",
411 __func__);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700412 goto fail;
413 }
414
415 INIT_WORK(&info->work, handle_bam_mux_cmd);
416
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600417 info->skb = __dev_alloc_skb(BUFFER_SIZE,
418 GFP_NOWAIT | __GFP_NOWARN);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700419 if (info->skb == NULL) {
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600420 DMUX_LOG_KERR(
421 "%s: unable to alloc skb, will retry later\n",
422 __func__);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700423 goto fail_info;
424 }
425 ptr = skb_put(info->skb, BUFFER_SIZE);
426
427 info->dma_address = dma_map_single(NULL, ptr, BUFFER_SIZE,
428 DMA_FROM_DEVICE);
429 if (info->dma_address == 0 || info->dma_address == ~0) {
430 DMUX_LOG_KERR("%s: dma_map_single failure %p for %p\n",
431 __func__, (void *)info->dma_address, ptr);
432 goto fail_skb;
433 }
434
435 mutex_lock(&bam_rx_pool_mutexlock);
436 list_add_tail(&info->list_node, &bam_rx_pool);
437 rx_len_cached = ++bam_rx_pool_len;
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700438 ret = sps_transfer_one(bam_rx_pipe, info->dma_address,
Jeff Hugoc85df962013-04-05 13:22:48 -0600439 BUFFER_SIZE, info, 0);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700440 if (ret) {
Eric Holmberg00cf8692012-07-16 14:21:19 -0600441 list_del(&info->list_node);
442 rx_len_cached = --bam_rx_pool_len;
443 mutex_unlock(&bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700444 DMUX_LOG_KERR("%s: sps_transfer_one failed %d\n",
445 __func__, ret);
Eric Holmberg00cf8692012-07-16 14:21:19 -0600446
447 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
448 DMA_FROM_DEVICE);
449
450 goto fail_skb;
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700451 }
Eric Holmberg00cf8692012-07-16 14:21:19 -0600452 mutex_unlock(&bam_rx_pool_mutexlock);
453
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700454 }
455 return;
456
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700457fail_skb:
458 dev_kfree_skb_any(info->skb);
459
460fail_info:
461 kfree(info);
462
463fail:
Arun Kumar Neelakantam799447f2012-12-13 18:06:49 +0530464 if (rx_len_cached == 0 && !in_global_reset) {
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600465 DMUX_LOG_KERR("%s: rescheduling\n", __func__);
466 schedule_delayed_work(&queue_rx_work, msecs_to_jiffies(100));
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700467 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700468}
469
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600470static void queue_rx_work_func(struct work_struct *work)
471{
472 queue_rx();
473}
474
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700475static void bam_mux_process_data(struct sk_buff *rx_skb)
476{
477 unsigned long flags;
478 struct bam_mux_hdr *rx_hdr;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600479 unsigned long event_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700480
481 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
482
483 rx_skb->data = (unsigned char *)(rx_hdr + 1);
484 rx_skb->tail = rx_skb->data + rx_hdr->pkt_len;
485 rx_skb->len = rx_hdr->pkt_len;
Jeff Hugoee88f672011-10-04 17:14:52 -0600486 rx_skb->truesize = rx_hdr->pkt_len + sizeof(struct sk_buff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600488 event_data = (unsigned long)(rx_skb);
489
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700490 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600491 if (bam_ch[rx_hdr->ch_id].notify)
492 bam_ch[rx_hdr->ch_id].notify(
493 bam_ch[rx_hdr->ch_id].priv, BAM_DMUX_RECEIVE,
494 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700495 else
496 dev_kfree_skb_any(rx_skb);
497 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
498
499 queue_rx();
500}
501
Eric Holmberg006057d2012-01-11 10:10:42 -0700502static inline void handle_bam_mux_cmd_open(struct bam_mux_hdr *rx_hdr)
503{
504 unsigned long flags;
505 int ret;
506
Eric Holmberga623da82012-07-12 09:37:09 -0600507 mutex_lock(&bam_pdev_mutexlock);
508 if (in_global_reset) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530509 BAM_DMUX_LOG("%s: open cid %d aborted due to ssr\n",
Eric Holmberga623da82012-07-12 09:37:09 -0600510 __func__, rx_hdr->ch_id);
511 mutex_unlock(&bam_pdev_mutexlock);
512 queue_rx();
513 return;
514 }
Eric Holmberg006057d2012-01-11 10:10:42 -0700515 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
516 bam_ch[rx_hdr->ch_id].status |= BAM_CH_REMOTE_OPEN;
517 bam_ch[rx_hdr->ch_id].num_tx_pkts = 0;
518 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Eric Holmberg006057d2012-01-11 10:10:42 -0700519 ret = platform_device_add(bam_ch[rx_hdr->ch_id].pdev);
520 if (ret)
521 pr_err("%s: platform_device_add() error: %d\n",
522 __func__, ret);
Eric Holmberga623da82012-07-12 09:37:09 -0600523 mutex_unlock(&bam_pdev_mutexlock);
524 queue_rx();
Eric Holmberg006057d2012-01-11 10:10:42 -0700525}
526
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700527static void handle_bam_mux_cmd(struct work_struct *work)
528{
529 unsigned long flags;
530 struct bam_mux_hdr *rx_hdr;
531 struct rx_pkt_info *info;
532 struct sk_buff *rx_skb;
533
534 info = container_of(work, struct rx_pkt_info, work);
535 rx_skb = info->skb;
Jeff Hugo949080a2011-08-30 11:58:56 -0600536 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE, DMA_FROM_DEVICE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700537 kfree(info);
538
539 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
540
541 DBG_INC_READ_CNT(sizeof(struct bam_mux_hdr));
542 DBG("%s: magic %x reserved %d cmd %d pad %d ch %d len %d\n", __func__,
543 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
544 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
545 if (rx_hdr->magic_num != BAM_MUX_HDR_MAGIC_NO) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700546 DMUX_LOG_KERR("%s: dropping invalid hdr. magic %x"
547 " reserved %d cmd %d"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700548 " pad %d ch %d len %d\n", __func__,
549 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
550 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
551 dev_kfree_skb_any(rx_skb);
552 queue_rx();
553 return;
554 }
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700555
556 if (rx_hdr->ch_id >= BAM_DMUX_NUM_CHANNELS) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700557 DMUX_LOG_KERR("%s: dropping invalid LCID %d"
558 " reserved %d cmd %d"
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700559 " pad %d ch %d len %d\n", __func__,
560 rx_hdr->ch_id, rx_hdr->reserved, rx_hdr->cmd,
561 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
562 dev_kfree_skb_any(rx_skb);
563 queue_rx();
564 return;
565 }
566
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700567 switch (rx_hdr->cmd) {
568 case BAM_MUX_HDR_CMD_DATA:
569 DBG_INC_READ_CNT(rx_hdr->pkt_len);
570 bam_mux_process_data(rx_skb);
571 break;
572 case BAM_MUX_HDR_CMD_OPEN:
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530573 BAM_DMUX_LOG("%s: opening cid %d PC enabled\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -0700574 rx_hdr->ch_id);
Eric Holmberg006057d2012-01-11 10:10:42 -0700575 handle_bam_mux_cmd_open(rx_hdr);
Jeff Hugob1e7c582012-06-20 15:02:11 -0600576 if (!(rx_hdr->reserved & ENABLE_DISCONNECT_ACK)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530577 BAM_DMUX_LOG("%s: deactivating disconnect ack\n",
Jeff Hugod7d2b062012-07-24 14:29:56 -0600578 __func__);
Jeff Hugob1e7c582012-06-20 15:02:11 -0600579 disconnect_ack = 0;
Jeff Hugo0b13a352012-03-17 23:18:30 -0600580 }
Eric Holmberg006057d2012-01-11 10:10:42 -0700581 dev_kfree_skb_any(rx_skb);
582 break;
583 case BAM_MUX_HDR_CMD_OPEN_NO_A2_PC:
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530584 BAM_DMUX_LOG("%s: opening cid %d PC disabled\n", __func__,
Eric Holmberg006057d2012-01-11 10:10:42 -0700585 rx_hdr->ch_id);
586
587 if (!a2_pc_disabled) {
588 a2_pc_disabled = 1;
Jeff Hugo322179f2012-02-29 10:52:34 -0700589 ul_wakeup();
Eric Holmberg006057d2012-01-11 10:10:42 -0700590 }
591
592 handle_bam_mux_cmd_open(rx_hdr);
Eric Holmberge779dba2011-11-04 18:22:01 -0600593 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700594 break;
595 case BAM_MUX_HDR_CMD_CLOSE:
596 /* probably should drop pending write */
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530597 BAM_DMUX_LOG("%s: closing cid %d\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -0700598 rx_hdr->ch_id);
Eric Holmberga623da82012-07-12 09:37:09 -0600599 mutex_lock(&bam_pdev_mutexlock);
600 if (in_global_reset) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530601 BAM_DMUX_LOG("%s: close cid %d aborted due to ssr\n",
Eric Holmberga623da82012-07-12 09:37:09 -0600602 __func__, rx_hdr->ch_id);
603 mutex_unlock(&bam_pdev_mutexlock);
604 break;
605 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700606 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
607 bam_ch[rx_hdr->ch_id].status &= ~BAM_CH_REMOTE_OPEN;
608 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo7960abd2011-08-02 15:39:38 -0600609 platform_device_unregister(bam_ch[rx_hdr->ch_id].pdev);
610 bam_ch[rx_hdr->ch_id].pdev =
611 platform_device_alloc(bam_ch[rx_hdr->ch_id].name, 2);
612 if (!bam_ch[rx_hdr->ch_id].pdev)
613 pr_err("%s: platform_device_alloc failed\n", __func__);
Eric Holmberga623da82012-07-12 09:37:09 -0600614 mutex_unlock(&bam_pdev_mutexlock);
Eric Holmberge779dba2011-11-04 18:22:01 -0600615 dev_kfree_skb_any(rx_skb);
Eric Holmberga623da82012-07-12 09:37:09 -0600616 queue_rx();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700617 break;
618 default:
Eric Holmberg878923a2012-01-10 14:28:19 -0700619 DMUX_LOG_KERR("%s: dropping invalid hdr. magic %x"
620 " reserved %d cmd %d pad %d ch %d len %d\n",
621 __func__, rx_hdr->magic_num, rx_hdr->reserved,
622 rx_hdr->cmd, rx_hdr->pad_len, rx_hdr->ch_id,
623 rx_hdr->pkt_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700624 dev_kfree_skb_any(rx_skb);
625 queue_rx();
626 return;
627 }
628}
629
630static int bam_mux_write_cmd(void *data, uint32_t len)
631{
632 int rc;
633 struct tx_pkt_info *pkt;
634 dma_addr_t dma_address;
Jeff Hugo626303bf2011-11-21 11:43:28 -0700635 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700636
Eric Holmbergd83cd2b2011-11-04 15:54:17 -0600637 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700638 if (pkt == NULL) {
639 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
640 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700641 return rc;
642 }
643
644 dma_address = dma_map_single(NULL, data, len,
645 DMA_TO_DEVICE);
646 if (!dma_address) {
647 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugo96cb7482011-12-07 13:28:31 -0700648 kfree(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700650 return rc;
651 }
652 pkt->skb = (struct sk_buff *)(data);
653 pkt->len = len;
654 pkt->dma_address = dma_address;
655 pkt->is_cmd = 1;
Eric Holmberg878923a2012-01-10 14:28:19 -0700656 set_tx_timestamp(pkt);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600657 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700658 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600659 list_add_tail(&pkt->list_node, &bam_tx_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660 rc = sps_transfer_one(bam_tx_pipe, dma_address, len,
Jeff Hugoc85df962013-04-05 13:22:48 -0600661 pkt, SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600662 if (rc) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700663 DMUX_LOG_KERR("%s sps_transfer_one failed rc=%d\n",
664 __func__, rc);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600665 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700666 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700667 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700668 dma_unmap_single(NULL, pkt->dma_address,
669 pkt->len,
670 DMA_TO_DEVICE);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600671 kfree(pkt);
Jeff Hugobb6da952012-01-16 15:02:42 -0700672 } else {
673 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600674 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700675
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600676 ul_packet_written = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677 return rc;
678}
679
680static void bam_mux_write_done(struct work_struct *work)
681{
682 struct sk_buff *skb;
683 struct bam_mux_hdr *hdr;
684 struct tx_pkt_info *info;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700685 struct tx_pkt_info *info_expected;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600686 unsigned long event_data;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700687 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700688
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600689 if (in_global_reset)
690 return;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700691
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700692 info = container_of(work, struct tx_pkt_info, work);
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700693
694 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
695 info_expected = list_first_entry(&bam_tx_pool,
696 struct tx_pkt_info, list_node);
697 if (unlikely(info != info_expected)) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700698 struct tx_pkt_info *errant_pkt;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700699
Eric Holmberg878923a2012-01-10 14:28:19 -0700700 DMUX_LOG_KERR("%s: bam_tx_pool mismatch .next=%p,"
701 " list_node=%p, ts=%u.%09lu\n",
702 __func__, bam_tx_pool.next, &info->list_node,
703 info->ts_sec, info->ts_nsec
704 );
705
706 list_for_each_entry(errant_pkt, &bam_tx_pool, list_node) {
707 DMUX_LOG_KERR("%s: node=%p ts=%u.%09lu\n", __func__,
708 &errant_pkt->list_node, errant_pkt->ts_sec,
709 errant_pkt->ts_nsec);
710
711 }
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700712 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
713 BUG();
714 }
715 list_del(&info->list_node);
716 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
717
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600718 if (info->is_cmd) {
719 kfree(info->skb);
720 kfree(info);
721 return;
722 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700723 skb = info->skb;
724 kfree(info);
725 hdr = (struct bam_mux_hdr *)skb->data;
Eric Holmberg9fdef262012-02-14 11:46:05 -0700726 DBG_INC_WRITE_CNT(skb->len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600727 event_data = (unsigned long)(skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700728 spin_lock_irqsave(&bam_ch[hdr->ch_id].lock, flags);
729 bam_ch[hdr->ch_id].num_tx_pkts--;
730 spin_unlock_irqrestore(&bam_ch[hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600731 if (bam_ch[hdr->ch_id].notify)
732 bam_ch[hdr->ch_id].notify(
733 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
734 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700735 else
736 dev_kfree_skb_any(skb);
737}
738
739int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
740{
741 int rc = 0;
742 struct bam_mux_hdr *hdr;
743 unsigned long flags;
744 struct sk_buff *new_skb = NULL;
745 dma_addr_t dma_address;
746 struct tx_pkt_info *pkt;
747
748 if (id >= BAM_DMUX_NUM_CHANNELS)
749 return -EINVAL;
750 if (!skb)
751 return -EINVAL;
752 if (!bam_mux_initialized)
753 return -ENODEV;
754
755 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
756 spin_lock_irqsave(&bam_ch[id].lock, flags);
757 if (!bam_ch_is_open(id)) {
758 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
759 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
760 return -ENODEV;
761 }
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700762
763 if (bam_ch[id].use_wm &&
764 (bam_ch[id].num_tx_pkts >= HIGH_WATERMARK)) {
765 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
766 pr_err("%s: watermark exceeded: %d\n", __func__, id);
767 return -EAGAIN;
768 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700769 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
770
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600771 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600772 if (!bam_is_connected) {
773 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600774 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700775 if (unlikely(in_global_reset == 1))
776 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600777 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600778 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600779 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600780
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700781 /* if skb do not have any tailroom for padding,
782 copy the skb into a new expanded skb */
783 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
784 /* revisit, probably dev_alloc_skb and memcpy is effecient */
785 new_skb = skb_copy_expand(skb, skb_headroom(skb),
786 4 - (skb->len & 0x3), GFP_ATOMIC);
787 if (new_skb == NULL) {
788 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600789 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700790 }
791 dev_kfree_skb_any(skb);
792 skb = new_skb;
793 DBG_INC_WRITE_CPY(skb->len);
794 }
795
796 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
797
798 /* caller should allocate for hdr and padding
799 hdr is fine, padding is tricky */
800 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
801 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
802 hdr->reserved = 0;
803 hdr->ch_id = id;
804 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
805 if (skb->len & 0x3)
806 skb_put(skb, 4 - (skb->len & 0x3));
807
808 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
809
810 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
811 __func__, skb->data, skb->tail, skb->len,
812 hdr->pkt_len, hdr->pad_len);
813
814 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
815 if (pkt == NULL) {
816 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600817 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700818 }
819
820 dma_address = dma_map_single(NULL, skb->data, skb->len,
821 DMA_TO_DEVICE);
822 if (!dma_address) {
823 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600824 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700825 }
826 pkt->skb = skb;
827 pkt->dma_address = dma_address;
828 pkt->is_cmd = 0;
Eric Holmberg878923a2012-01-10 14:28:19 -0700829 set_tx_timestamp(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700830 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700831 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600832 list_add_tail(&pkt->list_node, &bam_tx_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700833 rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len,
Jeff Hugoc85df962013-04-05 13:22:48 -0600834 pkt, SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600835 if (rc) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700836 DMUX_LOG_KERR("%s sps_transfer_one failed rc=%d\n",
837 __func__, rc);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600838 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700839 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700840 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700841 dma_unmap_single(NULL, pkt->dma_address,
842 pkt->skb->len, DMA_TO_DEVICE);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600843 kfree(pkt);
Jeff Hugo872bd062011-11-15 17:47:21 -0700844 if (new_skb)
845 dev_kfree_skb_any(new_skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700846 } else {
Jeff Hugobb6da952012-01-16 15:02:42 -0700847 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700848 spin_lock_irqsave(&bam_ch[id].lock, flags);
849 bam_ch[id].num_tx_pkts++;
850 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600851 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600852 ul_packet_written = 1;
853 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700854 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600855
856write_fail3:
857 kfree(pkt);
858write_fail2:
Arun Kumar Neelakantam406e5692013-01-17 18:58:04 +0530859 skb_pull(skb, sizeof(struct bam_mux_hdr));
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600860 if (new_skb)
861 dev_kfree_skb_any(new_skb);
862write_fail:
863 read_unlock(&ul_wakeup_lock);
864 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700865}
866
867int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600868 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700869{
870 struct bam_mux_hdr *hdr;
871 unsigned long flags;
872 int rc = 0;
873
874 DBG("%s: opening ch %d\n", __func__, id);
Eric Holmberg5d775432011-11-09 10:23:35 -0700875 if (!bam_mux_initialized) {
876 DBG("%s: not inititialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700877 return -ENODEV;
Eric Holmberg5d775432011-11-09 10:23:35 -0700878 }
879 if (id >= BAM_DMUX_NUM_CHANNELS) {
880 pr_err("%s: invalid channel id %d\n", __func__, id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700881 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700882 }
883 if (notify == NULL) {
884 pr_err("%s: notify function is NULL\n", __func__);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600885 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700886 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700887
888 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
889 if (hdr == NULL) {
890 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
891 return -ENOMEM;
892 }
893 spin_lock_irqsave(&bam_ch[id].lock, flags);
894 if (bam_ch_is_open(id)) {
895 DBG("%s: Already opened %d\n", __func__, id);
896 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
897 kfree(hdr);
898 goto open_done;
899 }
900 if (!bam_ch_is_remote_open(id)) {
901 DBG("%s: Remote not open; ch: %d\n", __func__, id);
902 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
903 kfree(hdr);
Eric Holmberg5d775432011-11-09 10:23:35 -0700904 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700905 }
906
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600907 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700908 bam_ch[id].priv = priv;
909 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700910 bam_ch[id].num_tx_pkts = 0;
911 bam_ch[id].use_wm = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700912 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
913
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600914 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600915 if (!bam_is_connected) {
916 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600917 ul_wakeup();
Brent Hronik96630422013-05-01 16:38:43 -0600918 if (unlikely(in_global_reset == 1)) {
919 kfree(hdr);
Jeff Hugo4838f412012-01-20 11:19:37 -0700920 return -EFAULT;
Brent Hronik96630422013-05-01 16:38:43 -0600921 }
Jeff Hugo061ce672011-10-21 17:15:32 -0600922 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600923 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600924 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600925
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700926 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
927 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
928 hdr->reserved = 0;
929 hdr->ch_id = id;
930 hdr->pkt_len = 0;
931 hdr->pad_len = 0;
932
933 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600934 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700935
936open_done:
937 DBG("%s: opened ch %d\n", __func__, id);
938 return rc;
939}
940
941int msm_bam_dmux_close(uint32_t id)
942{
943 struct bam_mux_hdr *hdr;
944 unsigned long flags;
945 int rc;
946
947 if (id >= BAM_DMUX_NUM_CHANNELS)
948 return -EINVAL;
949 DBG("%s: closing ch %d\n", __func__, id);
950 if (!bam_mux_initialized)
951 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700952
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600953 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600954 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600955 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600956 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700957 if (unlikely(in_global_reset == 1))
958 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600959 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600960 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600961 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600962
Jeff Hugo061ce672011-10-21 17:15:32 -0600963 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600964 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700965 bam_ch[id].priv = NULL;
966 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
967 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
968
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600969 if (bam_ch_is_in_reset(id)) {
970 read_unlock(&ul_wakeup_lock);
971 bam_ch[id].status &= ~BAM_CH_IN_RESET;
972 return 0;
973 }
974
Jeff Hugobb5802f2011-11-02 17:10:29 -0600975 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700976 if (hdr == NULL) {
977 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600978 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700979 return -ENOMEM;
980 }
981 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
982 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
983 hdr->reserved = 0;
984 hdr->ch_id = id;
985 hdr->pkt_len = 0;
986 hdr->pad_len = 0;
987
988 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600989 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700990
991 DBG("%s: closed ch %d\n", __func__, id);
992 return rc;
993}
994
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700995int msm_bam_dmux_is_ch_full(uint32_t id)
996{
997 unsigned long flags;
998 int ret;
999
1000 if (id >= BAM_DMUX_NUM_CHANNELS)
1001 return -EINVAL;
1002
1003 spin_lock_irqsave(&bam_ch[id].lock, flags);
1004 bam_ch[id].use_wm = 1;
1005 ret = bam_ch[id].num_tx_pkts >= HIGH_WATERMARK;
1006 DBG("%s: ch %d num tx pkts=%d, HWM=%d\n", __func__,
1007 id, bam_ch[id].num_tx_pkts, ret);
1008 if (!bam_ch_is_local_open(id)) {
1009 ret = -ENODEV;
1010 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
1011 }
1012 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
1013
1014 return ret;
1015}
1016
1017int msm_bam_dmux_is_ch_low(uint32_t id)
1018{
Eric Holmberged3ca0a2012-04-09 15:44:58 -06001019 unsigned long flags;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001020 int ret;
1021
1022 if (id >= BAM_DMUX_NUM_CHANNELS)
1023 return -EINVAL;
1024
Eric Holmberged3ca0a2012-04-09 15:44:58 -06001025 spin_lock_irqsave(&bam_ch[id].lock, flags);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001026 bam_ch[id].use_wm = 1;
1027 ret = bam_ch[id].num_tx_pkts <= LOW_WATERMARK;
1028 DBG("%s: ch %d num tx pkts=%d, LWM=%d\n", __func__,
1029 id, bam_ch[id].num_tx_pkts, ret);
1030 if (!bam_ch_is_local_open(id)) {
1031 ret = -ENODEV;
1032 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
1033 }
Eric Holmberged3ca0a2012-04-09 15:44:58 -06001034 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001035
1036 return ret;
1037}
1038
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001039static void rx_switch_to_interrupt_mode(void)
1040{
1041 struct sps_connect cur_rx_conn;
1042 struct sps_iovec iov;
1043 struct rx_pkt_info *info;
1044 int ret;
1045
1046 /*
1047 * Attempt to enable interrupts - if this fails,
1048 * continue polling and we will retry later.
1049 */
1050 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
1051 if (ret) {
1052 pr_err("%s: sps_get_config() failed %d\n", __func__, ret);
1053 goto fail;
1054 }
1055
1056 rx_register_event.options = SPS_O_EOT;
1057 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1058 if (ret) {
1059 pr_err("%s: sps_register_event() failed %d\n", __func__, ret);
1060 goto fail;
1061 }
1062
1063 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
1064 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
1065 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
1066 if (ret) {
1067 pr_err("%s: sps_set_config() failed %d\n", __func__, ret);
1068 goto fail;
1069 }
1070 polling_mode = 0;
Brent Hronik096f7d32013-06-28 15:43:08 -06001071 complete_all(&shutdown_completion);
Eric Holmberg006057d2012-01-11 10:10:42 -07001072 release_wakelock();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001073
1074 /* handle any rx packets before interrupt was enabled */
1075 while (bam_connection_is_active && !polling_mode) {
1076 ret = sps_get_iovec(bam_rx_pipe, &iov);
1077 if (ret) {
1078 pr_err("%s: sps_get_iovec failed %d\n",
1079 __func__, ret);
1080 break;
1081 }
1082 if (iov.addr == 0)
1083 break;
1084
1085 mutex_lock(&bam_rx_pool_mutexlock);
1086 if (unlikely(list_empty(&bam_rx_pool))) {
Eric Holmberg00cf8692012-07-16 14:21:19 -06001087 DMUX_LOG_KERR("%s: have iovec %p but rx pool empty\n",
1088 __func__, (void *)iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001089 mutex_unlock(&bam_rx_pool_mutexlock);
1090 continue;
1091 }
1092 info = list_first_entry(&bam_rx_pool, struct rx_pkt_info,
1093 list_node);
Eric Holmberg00cf8692012-07-16 14:21:19 -06001094 if (info->dma_address != iov.addr) {
1095 DMUX_LOG_KERR("%s: iovec %p != dma %p\n",
1096 __func__,
1097 (void *)iov.addr,
1098 (void *)info->dma_address);
1099 list_for_each_entry(info, &bam_rx_pool, list_node) {
1100 DMUX_LOG_KERR("%s: dma %p\n", __func__,
1101 (void *)info->dma_address);
1102 if (iov.addr == info->dma_address)
1103 break;
1104 }
1105 }
1106 BUG_ON(info->dma_address != iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001107 list_del(&info->list_node);
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001108 --bam_rx_pool_len;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001109 mutex_unlock(&bam_rx_pool_mutexlock);
1110 handle_bam_mux_cmd(&info->work);
1111 }
1112 return;
1113
1114fail:
1115 pr_err("%s: reverting to polling\n", __func__);
Jeff Hugofff43af92012-03-29 17:54:52 -06001116 queue_work_on(0, bam_mux_rx_workqueue, &rx_timer_work);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001117}
1118
Jeff Hugo949080a2011-08-30 11:58:56 -06001119static void rx_timer_work_func(struct work_struct *work)
1120{
1121 struct sps_iovec iov;
Jeff Hugo949080a2011-08-30 11:58:56 -06001122 struct rx_pkt_info *info;
1123 int inactive_cycles = 0;
1124 int ret;
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001125 u32 buffs_unused, buffs_used;
Jeff Hugo949080a2011-08-30 11:58:56 -06001126
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001127 while (bam_connection_is_active) { /* timer loop */
Jeff Hugo949080a2011-08-30 11:58:56 -06001128 ++inactive_cycles;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001129 while (bam_connection_is_active) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001130 if (in_global_reset)
1131 return;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001132
1133 ret = sps_get_iovec(bam_rx_pipe, &iov);
1134 if (ret) {
1135 pr_err("%s: sps_get_iovec failed %d\n",
1136 __func__, ret);
1137 break;
1138 }
Jeff Hugo949080a2011-08-30 11:58:56 -06001139 if (iov.addr == 0)
1140 break;
1141 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -06001142 mutex_lock(&bam_rx_pool_mutexlock);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001143 if (unlikely(list_empty(&bam_rx_pool))) {
Eric Holmberg00cf8692012-07-16 14:21:19 -06001144 DMUX_LOG_KERR(
1145 "%s: have iovec %p but rx pool empty\n",
1146 __func__, (void *)iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001147 mutex_unlock(&bam_rx_pool_mutexlock);
1148 continue;
1149 }
1150 info = list_first_entry(&bam_rx_pool,
1151 struct rx_pkt_info, list_node);
Eric Holmberg00cf8692012-07-16 14:21:19 -06001152 if (info->dma_address != iov.addr) {
1153 DMUX_LOG_KERR("%s: iovec %p != dma %p\n",
1154 __func__,
1155 (void *)iov.addr,
1156 (void *)info->dma_address);
1157 list_for_each_entry(info, &bam_rx_pool,
1158 list_node) {
1159 DMUX_LOG_KERR("%s: dma %p\n", __func__,
1160 (void *)info->dma_address);
1161 if (iov.addr == info->dma_address)
1162 break;
1163 }
1164 }
1165 BUG_ON(info->dma_address != iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001166 list_del(&info->list_node);
Eric Holmberg00cf8692012-07-16 14:21:19 -06001167 --bam_rx_pool_len;
Jeff Hugoc9749932011-11-02 17:50:40 -06001168 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -06001169 handle_bam_mux_cmd(&info->work);
1170 }
1171
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001172 if (inactive_cycles >= POLLING_INACTIVITY) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001173 rx_switch_to_interrupt_mode();
1174 break;
Jeff Hugo949080a2011-08-30 11:58:56 -06001175 }
1176
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001177 if (bam_adaptive_timer_enabled) {
1178 usleep_range(rx_timer_interval, rx_timer_interval + 50);
1179
1180 ret = sps_get_unused_desc_num(bam_rx_pipe,
1181 &buffs_unused);
1182
1183 if (ret) {
1184 pr_err("%s: error getting num buffers unused after sleep\n",
1185 __func__);
1186
1187 break;
1188 }
1189
1190 buffs_used = NUM_BUFFERS - buffs_unused;
1191
1192 if (buffs_unused == 0) {
1193 rx_timer_interval = MIN_POLLING_SLEEP;
1194 } else {
1195 if (buffs_used > 0) {
1196 rx_timer_interval =
1197 (2 * NUM_BUFFERS *
1198 rx_timer_interval)/
1199 (3 * buffs_used);
1200 } else {
1201 rx_timer_interval =
1202 MAX_POLLING_SLEEP;
1203 }
1204 }
1205
1206 if (rx_timer_interval > MAX_POLLING_SLEEP)
1207 rx_timer_interval = MAX_POLLING_SLEEP;
1208 else if (rx_timer_interval < MIN_POLLING_SLEEP)
1209 rx_timer_interval = MIN_POLLING_SLEEP;
1210 } else {
1211 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
1212 }
Jeff Hugo949080a2011-08-30 11:58:56 -06001213 }
1214}
1215
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001216static void bam_mux_tx_notify(struct sps_event_notify *notify)
1217{
1218 struct tx_pkt_info *pkt;
1219
1220 DBG("%s: event %d notified\n", __func__, notify->event_id);
1221
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001222 if (in_global_reset)
1223 return;
1224
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001225 switch (notify->event_id) {
1226 case SPS_EVENT_EOT:
1227 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001228 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001229 dma_unmap_single(NULL, pkt->dma_address,
1230 pkt->skb->len,
1231 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001232 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001233 dma_unmap_single(NULL, pkt->dma_address,
1234 pkt->len,
1235 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001236 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001237 break;
1238 default:
1239 pr_err("%s: recieved unexpected event id %d\n", __func__,
1240 notify->event_id);
1241 }
1242}
1243
Jeff Hugo33dbc002011-08-25 15:52:53 -06001244static void bam_mux_rx_notify(struct sps_event_notify *notify)
1245{
Jeff Hugo949080a2011-08-30 11:58:56 -06001246 int ret;
1247 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -06001248
1249 DBG("%s: event %d notified\n", __func__, notify->event_id);
1250
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001251 if (in_global_reset)
1252 return;
1253
Jeff Hugo33dbc002011-08-25 15:52:53 -06001254 switch (notify->event_id) {
1255 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -06001256 /* attempt to disable interrupts in this pipe */
1257 if (!polling_mode) {
1258 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
1259 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001260 pr_err("%s: sps_get_config() failed %d, interrupts"
1261 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001262 break;
1263 }
Jeff Hugoa9d32ba2011-11-21 14:59:48 -07001264 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
Jeff Hugo949080a2011-08-30 11:58:56 -06001265 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
1266 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
1267 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001268 pr_err("%s: sps_set_config() failed %d, interrupts"
1269 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001270 break;
1271 }
Brent Hronik096f7d32013-06-28 15:43:08 -06001272 INIT_COMPLETION(shutdown_completion);
Eric Holmberg006057d2012-01-11 10:10:42 -07001273 grab_wakelock();
Jeff Hugo949080a2011-08-30 11:58:56 -06001274 polling_mode = 1;
Jeff Hugofff43af92012-03-29 17:54:52 -06001275 /*
1276 * run on core 0 so that netif_rx() in rmnet uses only
1277 * one queue
1278 */
1279 queue_work_on(0, bam_mux_rx_workqueue, &rx_timer_work);
Jeff Hugo949080a2011-08-30 11:58:56 -06001280 }
Jeff Hugo33dbc002011-08-25 15:52:53 -06001281 break;
1282 default:
1283 pr_err("%s: recieved unexpected event id %d\n", __func__,
1284 notify->event_id);
1285 }
1286}
1287
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001288#ifdef CONFIG_DEBUG_FS
1289
1290static int debug_tbl(char *buf, int max)
1291{
1292 int i = 0;
1293 int j;
1294
1295 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
1296 i += scnprintf(buf + i, max - i,
1297 "ch%02d local open=%s remote open=%s\n",
1298 j, bam_ch_is_local_open(j) ? "Y" : "N",
1299 bam_ch_is_remote_open(j) ? "Y" : "N");
1300 }
1301
1302 return i;
1303}
1304
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001305static int debug_ul_pkt_cnt(char *buf, int max)
1306{
1307 struct list_head *p;
1308 unsigned long flags;
1309 int n = 0;
1310
1311 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
1312 __list_for_each(p, &bam_tx_pool) {
1313 ++n;
1314 }
1315 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
1316
1317 return scnprintf(buf, max, "Number of UL packets in flight: %d\n", n);
1318}
1319
1320static int debug_stats(char *buf, int max)
1321{
1322 int i = 0;
1323
1324 i += scnprintf(buf + i, max - i,
Eric Holmberg9fdef262012-02-14 11:46:05 -07001325 "skb read cnt: %u\n"
1326 "skb write cnt: %u\n"
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001327 "skb copy cnt: %u\n"
1328 "skb copy bytes: %u\n"
Eric Holmberg6074aba2012-01-18 17:59:44 -07001329 "sps tx failures: %u\n"
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001330 "sps tx stalls: %u\n"
Eric Holmberg1f1255d2012-02-22 13:37:21 -07001331 "rx queue len: %d\n"
1332 "a2 ack out cnt: %d\n"
1333 "a2 ack in cnt: %d\n"
1334 "a2 pwr cntl in: %d\n",
Eric Holmberg9fdef262012-02-14 11:46:05 -07001335 bam_dmux_read_cnt,
1336 bam_dmux_write_cnt,
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001337 bam_dmux_write_cpy_cnt,
1338 bam_dmux_write_cpy_bytes,
Eric Holmberg6074aba2012-01-18 17:59:44 -07001339 bam_dmux_tx_sps_failure_cnt,
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001340 bam_dmux_tx_stall_cnt,
Eric Holmberg1f1255d2012-02-22 13:37:21 -07001341 bam_rx_pool_len,
1342 atomic_read(&bam_dmux_ack_out_cnt),
1343 atomic_read(&bam_dmux_ack_in_cnt),
1344 atomic_read(&bam_dmux_a2_pwr_cntl_in_cnt)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001345 );
1346
1347 return i;
1348}
1349
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001350#define DEBUG_BUFMAX 4096
1351static char debug_buffer[DEBUG_BUFMAX];
1352
1353static ssize_t debug_read(struct file *file, char __user *buf,
1354 size_t count, loff_t *ppos)
1355{
1356 int (*fill)(char *buf, int max) = file->private_data;
1357 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
1358 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
1359}
1360
1361static int debug_open(struct inode *inode, struct file *file)
1362{
1363 file->private_data = inode->i_private;
1364 return 0;
1365}
1366
1367
1368static const struct file_operations debug_ops = {
1369 .read = debug_read,
1370 .open = debug_open,
1371};
1372
1373static void debug_create(const char *name, mode_t mode,
1374 struct dentry *dent,
1375 int (*fill)(char *buf, int max))
1376{
Eric Holmberge4ac80b2012-01-12 09:21:59 -07001377 struct dentry *file;
1378
1379 file = debugfs_create_file(name, mode, dent, fill, &debug_ops);
1380 if (IS_ERR(file))
1381 pr_err("%s: debugfs create failed %d\n", __func__,
1382 (int)PTR_ERR(file));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001383}
1384
1385#endif
1386
Jeff Hugod98b1082011-10-24 10:30:23 -06001387static void notify_all(int event, unsigned long data)
1388{
1389 int i;
Jeff Hugocb798022012-04-09 14:55:40 -06001390 struct list_head *temp;
1391 struct outside_notify_func *func;
Jeff Hugod98b1082011-10-24 10:30:23 -06001392
Jeff Hugoac8152a2013-04-19 11:05:19 -06001393 BAM_DMUX_LOG("%s: event=%d, data=%lu\n", __func__, event, data);
1394
Jeff Hugod98b1082011-10-24 10:30:23 -06001395 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
Jeff Hugoac8152a2013-04-19 11:05:19 -06001396 if (bam_ch_is_open(i))
Jeff Hugod98b1082011-10-24 10:30:23 -06001397 bam_ch[i].notify(bam_ch[i].priv, event, data);
1398 }
Jeff Hugocb798022012-04-09 14:55:40 -06001399
1400 __list_for_each(temp, &bam_other_notify_funcs) {
1401 func = container_of(temp, struct outside_notify_func,
1402 list_node);
1403 func->notify(func->priv, event, data);
1404 }
Jeff Hugod98b1082011-10-24 10:30:23 -06001405}
1406
1407static void kickoff_ul_wakeup_func(struct work_struct *work)
1408{
1409 read_lock(&ul_wakeup_lock);
1410 if (!bam_is_connected) {
1411 read_unlock(&ul_wakeup_lock);
1412 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -07001413 if (unlikely(in_global_reset == 1))
1414 return;
Jeff Hugod98b1082011-10-24 10:30:23 -06001415 read_lock(&ul_wakeup_lock);
1416 ul_packet_written = 1;
1417 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
1418 }
1419 read_unlock(&ul_wakeup_lock);
1420}
1421
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001422int msm_bam_dmux_kickoff_ul_wakeup(void)
Jeff Hugod98b1082011-10-24 10:30:23 -06001423{
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001424 int is_connected;
1425
1426 read_lock(&ul_wakeup_lock);
1427 ul_packet_written = 1;
1428 is_connected = bam_is_connected;
1429 if (!is_connected)
1430 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
1431 read_unlock(&ul_wakeup_lock);
1432
1433 return is_connected;
Jeff Hugod98b1082011-10-24 10:30:23 -06001434}
1435
Eric Holmberg878923a2012-01-10 14:28:19 -07001436static void power_vote(int vote)
1437{
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301438 BAM_DMUX_LOG("%s: curr=%d, vote=%d\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -07001439 bam_dmux_uplink_vote, vote);
1440
1441 if (bam_dmux_uplink_vote == vote)
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301442 BAM_DMUX_LOG("%s: warning - duplicate power vote\n", __func__);
Eric Holmberg878923a2012-01-10 14:28:19 -07001443
1444 bam_dmux_uplink_vote = vote;
1445 if (vote)
1446 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
1447 else
1448 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1449}
1450
Eric Holmberg454d9da2012-01-12 09:37:14 -07001451/*
1452 * @note: Must be called with ul_wakeup_lock locked.
1453 */
1454static inline void ul_powerdown(void)
1455{
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301456 BAM_DMUX_LOG("%s: powerdown\n", __func__);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001457 verify_tx_queue_is_empty(__func__);
1458
1459 if (a2_pc_disabled) {
1460 wait_for_dfab = 1;
1461 INIT_COMPLETION(dfab_unvote_completion);
1462 release_wakelock();
1463 } else {
1464 wait_for_ack = 1;
1465 INIT_COMPLETION(ul_wakeup_ack_completion);
1466 power_vote(0);
1467 }
1468 bam_is_connected = 0;
1469 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
1470}
1471
1472static inline void ul_powerdown_finish(void)
1473{
1474 if (a2_pc_disabled && wait_for_dfab) {
1475 unvote_dfab();
1476 complete_all(&dfab_unvote_completion);
1477 wait_for_dfab = 0;
1478 }
1479}
1480
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001481/*
1482 * Votes for UL power and returns current power state.
1483 *
1484 * @returns true if currently connected
1485 */
1486int msm_bam_dmux_ul_power_vote(void)
1487{
1488 int is_connected;
1489
1490 read_lock(&ul_wakeup_lock);
1491 atomic_inc(&ul_ondemand_vote);
1492 is_connected = bam_is_connected;
1493 if (!is_connected)
1494 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
1495 read_unlock(&ul_wakeup_lock);
1496
1497 return is_connected;
1498}
1499
1500/*
1501 * Unvotes for UL power.
1502 *
1503 * @returns true if vote count is 0 (UL shutdown possible)
1504 */
1505int msm_bam_dmux_ul_power_unvote(void)
1506{
1507 int vote;
1508
1509 read_lock(&ul_wakeup_lock);
1510 vote = atomic_dec_return(&ul_ondemand_vote);
1511 if (unlikely(vote) < 0)
1512 DMUX_LOG_KERR("%s: invalid power vote %d\n", __func__, vote);
1513 read_unlock(&ul_wakeup_lock);
1514
1515 return vote == 0;
1516}
1517
Jeff Hugocb798022012-04-09 14:55:40 -06001518int msm_bam_dmux_reg_notify(void *priv,
1519 void (*notify)(void *priv, int event_type,
1520 unsigned long data))
1521{
1522 struct outside_notify_func *func;
1523
1524 if (!notify)
1525 return -EINVAL;
1526
1527 func = kmalloc(sizeof(struct outside_notify_func), GFP_KERNEL);
1528 if (!func)
1529 return -ENOMEM;
1530
1531 func->notify = notify;
1532 func->priv = priv;
1533 list_add(&func->list_node, &bam_other_notify_funcs);
1534
1535 return 0;
1536}
1537
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001538static void ul_timeout(struct work_struct *work)
1539{
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001540 unsigned long flags;
1541 int ret;
1542
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001543 if (in_global_reset)
1544 return;
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001545 ret = write_trylock_irqsave(&ul_wakeup_lock, flags);
1546 if (!ret) { /* failed to grab lock, reschedule and bail */
1547 schedule_delayed_work(&ul_timeout_work,
1548 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1549 return;
1550 }
Eric Holmberg454d9da2012-01-12 09:37:14 -07001551 if (bam_is_connected) {
Eric Holmberg6074aba2012-01-18 17:59:44 -07001552 if (!ul_packet_written) {
1553 spin_lock(&bam_tx_pool_spinlock);
1554 if (!list_empty(&bam_tx_pool)) {
1555 struct tx_pkt_info *info;
1556
1557 info = list_first_entry(&bam_tx_pool,
1558 struct tx_pkt_info, list_node);
1559 DMUX_LOG_KERR("%s: UL delayed ts=%u.%09lu\n",
1560 __func__, info->ts_sec, info->ts_nsec);
1561 DBG_INC_TX_STALL_CNT();
1562 ul_packet_written = 1;
1563 }
1564 spin_unlock(&bam_tx_pool_spinlock);
1565 }
1566
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001567 if (ul_packet_written || atomic_read(&ul_ondemand_vote)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301568 BAM_DMUX_LOG("%s: pkt written %d\n",
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001569 __func__, ul_packet_written);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001570 ul_packet_written = 0;
1571 schedule_delayed_work(&ul_timeout_work,
1572 msecs_to_jiffies(UL_TIMEOUT_DELAY));
Eric Holmberg006057d2012-01-11 10:10:42 -07001573 } else {
Eric Holmberg454d9da2012-01-12 09:37:14 -07001574 ul_powerdown();
Eric Holmberg006057d2012-01-11 10:10:42 -07001575 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001576 }
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001577 write_unlock_irqrestore(&ul_wakeup_lock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001578 ul_powerdown_finish();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001579}
Jeff Hugo4838f412012-01-20 11:19:37 -07001580
1581static int ssrestart_check(void)
1582{
Jeff Hugob8156d72013-06-04 12:51:10 -06001583 int ret = 0;
1584
Eric Holmberg7614a7f2013-07-29 15:47:12 -06001585 if (in_global_reset) {
1586 DMUX_LOG_KERR("%s: modem timeout: already in SSR\n",
1587 __func__);
1588 return 1;
1589 }
1590
Jeff Hugob8156d72013-06-04 12:51:10 -06001591 DMUX_LOG_KERR("%s: modem timeout: BAM DMUX disabled for SSR\n",
1592 __func__);
Eric Holmberg90285e22012-02-22 12:33:05 -07001593 in_global_reset = 1;
Jeff Hugob8156d72013-06-04 12:51:10 -06001594 ret = subsystem_restart("modem");
1595 if (ret == -ENODEV)
1596 panic("modem subsystem restart failed\n");
Eric Holmberg90285e22012-02-22 12:33:05 -07001597 return 1;
Jeff Hugo4838f412012-01-20 11:19:37 -07001598}
1599
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001600static void ul_wakeup(void)
1601{
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001602 int ret;
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001603 int do_vote_dfab = 0;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001604
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001605 mutex_lock(&wakeup_lock);
1606 if (bam_is_connected) { /* bam got connected before lock grabbed */
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301607 BAM_DMUX_LOG("%s Already awake\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001608 mutex_unlock(&wakeup_lock);
1609 return;
1610 }
Eric Holmberg878923a2012-01-10 14:28:19 -07001611
Jeff Hugoc2696142012-05-03 11:42:13 -06001612 /*
Jeff Hugof5001732012-08-27 13:19:09 -06001613 * if this gets hit, that means restart_notifier_cb() has started
1614 * but probably not finished, thus we know SSR has happened, but
1615 * haven't been able to send that info to our clients yet.
1616 * in that case, abort the ul_wakeup() so that we don't undo any
1617 * work restart_notifier_cb() has done. The clients will be notified
1618 * shortly. No cleanup necessary (reschedule the wakeup) as our and
1619 * their SSR handling will cover it
1620 */
1621 if (unlikely(in_global_reset == 1)) {
1622 mutex_unlock(&wakeup_lock);
1623 return;
1624 }
1625
1626 /*
Jeff Hugoc2696142012-05-03 11:42:13 -06001627 * if someone is voting for UL before bam is inited (modem up first
1628 * time), set flag for init to kickoff ul wakeup once bam is inited
1629 */
1630 mutex_lock(&delayed_ul_vote_lock);
1631 if (unlikely(!bam_mux_initialized)) {
1632 need_delayed_ul_vote = 1;
1633 mutex_unlock(&delayed_ul_vote_lock);
1634 mutex_unlock(&wakeup_lock);
1635 return;
1636 }
1637 mutex_unlock(&delayed_ul_vote_lock);
1638
Eric Holmberg006057d2012-01-11 10:10:42 -07001639 if (a2_pc_disabled) {
1640 /*
1641 * don't grab the wakelock the first time because it is
1642 * already grabbed when a2 powers on
1643 */
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001644 if (likely(a2_pc_disabled_wakelock_skipped)) {
Eric Holmberg006057d2012-01-11 10:10:42 -07001645 grab_wakelock();
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001646 do_vote_dfab = 1; /* vote must occur after wait */
1647 } else {
Jeff Hugo583a6da2012-02-03 11:37:30 -07001648 a2_pc_disabled_wakelock_skipped = 1;
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001649 }
Eric Holmberg006057d2012-01-11 10:10:42 -07001650 if (wait_for_dfab) {
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001651 ret = wait_for_completion_timeout(
Eric Holmberg006057d2012-01-11 10:10:42 -07001652 &dfab_unvote_completion, HZ);
1653 BUG_ON(ret == 0);
1654 }
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001655 if (likely(do_vote_dfab))
1656 vote_dfab();
Eric Holmberg006057d2012-01-11 10:10:42 -07001657 schedule_delayed_work(&ul_timeout_work,
1658 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1659 bam_is_connected = 1;
1660 mutex_unlock(&wakeup_lock);
1661 return;
1662 }
1663
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001664 /*
1665 * must wait for the previous power down request to have been acked
1666 * chances are it already came in and this will just fall through
1667 * instead of waiting
1668 */
1669 if (wait_for_ack) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301670 BAM_DMUX_LOG("%s waiting for previous ack\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001671 ret = wait_for_completion_timeout(
Jeff Hugo1f317392013-07-24 16:28:52 -06001672 &ul_wakeup_ack_completion,
1673 msecs_to_jiffies(UL_WAKEUP_TIMEOUT_MS));
Eric Holmberg006057d2012-01-11 10:10:42 -07001674 wait_for_ack = 0;
Jeff Hugo4838f412012-01-20 11:19:37 -07001675 if (unlikely(ret == 0) && ssrestart_check()) {
1676 mutex_unlock(&wakeup_lock);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301677 BAM_DMUX_LOG("%s timeout previous ack\n", __func__);
Jeff Hugo4838f412012-01-20 11:19:37 -07001678 return;
1679 }
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001680 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001681 INIT_COMPLETION(ul_wakeup_ack_completion);
Eric Holmberg878923a2012-01-10 14:28:19 -07001682 power_vote(1);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301683 BAM_DMUX_LOG("%s waiting for wakeup ack\n", __func__);
Jeff Hugo1f317392013-07-24 16:28:52 -06001684 ret = wait_for_completion_timeout(&ul_wakeup_ack_completion,
1685 msecs_to_jiffies(UL_WAKEUP_TIMEOUT_MS));
Jeff Hugo4838f412012-01-20 11:19:37 -07001686 if (unlikely(ret == 0) && ssrestart_check()) {
1687 mutex_unlock(&wakeup_lock);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301688 BAM_DMUX_LOG("%s timeout wakeup ack\n", __func__);
Jeff Hugo4838f412012-01-20 11:19:37 -07001689 return;
1690 }
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301691 BAM_DMUX_LOG("%s waiting completion\n", __func__);
Jeff Hugo1f317392013-07-24 16:28:52 -06001692 ret = wait_for_completion_timeout(&bam_connection_completion,
1693 msecs_to_jiffies(UL_WAKEUP_TIMEOUT_MS));
Jeff Hugo4838f412012-01-20 11:19:37 -07001694 if (unlikely(ret == 0) && ssrestart_check()) {
1695 mutex_unlock(&wakeup_lock);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301696 BAM_DMUX_LOG("%s timeout power on\n", __func__);
Jeff Hugo4838f412012-01-20 11:19:37 -07001697 return;
1698 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001699
1700 bam_is_connected = 1;
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301701 BAM_DMUX_LOG("%s complete\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001702 schedule_delayed_work(&ul_timeout_work,
1703 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1704 mutex_unlock(&wakeup_lock);
1705}
1706
1707static void reconnect_to_bam(void)
1708{
1709 int i;
1710
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001711 in_global_reset = 0;
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001712 in_ssr = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001713 vote_dfab();
Jeff Hugo18792a32012-06-20 15:25:55 -06001714 if (!power_management_only_mode) {
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001715 if (ssr_skipped_disconnect) {
1716 /* delayed to here to prevent bus stall */
1717 sps_disconnect(bam_tx_pipe);
1718 sps_disconnect(bam_rx_pipe);
1719 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1720 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
1721 }
1722 ssr_skipped_disconnect = 0;
Jeff Hugo18792a32012-06-20 15:25:55 -06001723 i = sps_device_reset(a2_device_handle);
1724 if (i)
1725 pr_err("%s: device reset failed rc = %d\n", __func__,
1726 i);
1727 i = sps_connect(bam_tx_pipe, &tx_connection);
1728 if (i)
1729 pr_err("%s: tx connection failed rc = %d\n", __func__,
1730 i);
1731 i = sps_connect(bam_rx_pipe, &rx_connection);
1732 if (i)
1733 pr_err("%s: rx connection failed rc = %d\n", __func__,
1734 i);
1735 i = sps_register_event(bam_tx_pipe, &tx_register_event);
1736 if (i)
1737 pr_err("%s: tx event reg failed rc = %d\n", __func__,
1738 i);
1739 i = sps_register_event(bam_rx_pipe, &rx_register_event);
1740 if (i)
1741 pr_err("%s: rx event reg failed rc = %d\n", __func__,
1742 i);
1743 }
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001744
1745 bam_connection_is_active = 1;
1746
1747 if (polling_mode)
1748 rx_switch_to_interrupt_mode();
1749
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001750 toggle_apps_ack();
1751 complete_all(&bam_connection_completion);
Jeff Hugo18792a32012-06-20 15:25:55 -06001752 if (!power_management_only_mode)
1753 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001754}
1755
1756static void disconnect_to_bam(void)
1757{
1758 struct list_head *node;
1759 struct rx_pkt_info *info;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001760 unsigned long flags;
Brent Hronik096f7d32013-06-28 15:43:08 -06001761 unsigned long time_remaining;
1762
Eric Holmberg7614a7f2013-07-29 15:47:12 -06001763 if (!in_global_reset) {
1764 time_remaining = wait_for_completion_timeout(
1765 &shutdown_completion,
1766 msecs_to_jiffies(SHUTDOWN_TIMEOUT_MS));
1767 if (time_remaining == 0) {
1768 DMUX_LOG_KERR("%s: shutdown completion timed out\n",
1769 __func__);
1770 ssrestart_check();
1771 }
Brent Hronik096f7d32013-06-28 15:43:08 -06001772 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001773
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001774 bam_connection_is_active = 0;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001775
1776 /* handle disconnect during active UL */
1777 write_lock_irqsave(&ul_wakeup_lock, flags);
1778 if (bam_is_connected) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301779 BAM_DMUX_LOG("%s: UL active - forcing powerdown\n", __func__);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001780 ul_powerdown();
1781 }
1782 write_unlock_irqrestore(&ul_wakeup_lock, flags);
1783 ul_powerdown_finish();
1784
1785 /* tear down BAM connection */
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001786 INIT_COMPLETION(bam_connection_completion);
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001787
1788 /* in_ssr documentation/assumptions found in restart_notifier_cb */
Jeff Hugo18792a32012-06-20 15:25:55 -06001789 if (!power_management_only_mode) {
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001790 if (likely(!in_ssr)) {
Jeff Hugof7ae7a62013-04-19 11:18:32 -06001791 BAM_DMUX_LOG("%s: disconnect tx\n", __func__);
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001792 sps_disconnect(bam_tx_pipe);
Jeff Hugof7ae7a62013-04-19 11:18:32 -06001793 BAM_DMUX_LOG("%s: disconnect rx\n", __func__);
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001794 sps_disconnect(bam_rx_pipe);
1795 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1796 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
Jeff Hugof7ae7a62013-04-19 11:18:32 -06001797 BAM_DMUX_LOG("%s: device reset\n", __func__);
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001798 sps_device_reset(a2_device_handle);
1799 } else {
1800 ssr_skipped_disconnect = 1;
1801 }
Jeff Hugo18792a32012-06-20 15:25:55 -06001802 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001803 unvote_dfab();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001804
1805 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001806 while (!list_empty(&bam_rx_pool)) {
1807 node = bam_rx_pool.next;
1808 list_del(node);
1809 info = container_of(node, struct rx_pkt_info, list_node);
1810 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
1811 DMA_FROM_DEVICE);
1812 dev_kfree_skb_any(info->skb);
1813 kfree(info);
1814 }
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001815 bam_rx_pool_len = 0;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001816 mutex_unlock(&bam_rx_pool_mutexlock);
Eric Holmberg878923a2012-01-10 14:28:19 -07001817
Jeff Hugo0b13a352012-03-17 23:18:30 -06001818 if (disconnect_ack)
1819 toggle_apps_ack();
1820
Eric Holmberg878923a2012-01-10 14:28:19 -07001821 verify_tx_queue_is_empty(__func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001822}
1823
1824static void vote_dfab(void)
1825{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001826 int rc;
1827
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301828 BAM_DMUX_LOG("%s\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001829 mutex_lock(&dfab_status_lock);
1830 if (dfab_is_on) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301831 BAM_DMUX_LOG("%s: dfab is already on\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001832 mutex_unlock(&dfab_status_lock);
1833 return;
1834 }
Jeff Hugod0befde2012-08-09 15:32:49 -06001835 if (dfab_clk) {
1836 rc = clk_prepare_enable(dfab_clk);
1837 if (rc)
1838 DMUX_LOG_KERR("bam_dmux vote for dfab failed rc = %d\n",
1839 rc);
1840 }
1841 if (xo_clk) {
1842 rc = clk_prepare_enable(xo_clk);
1843 if (rc)
1844 DMUX_LOG_KERR("bam_dmux vote for xo failed rc = %d\n",
1845 rc);
1846 }
Eric Holmberg006057d2012-01-11 10:10:42 -07001847 dfab_is_on = 1;
1848 mutex_unlock(&dfab_status_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001849}
1850
1851static void unvote_dfab(void)
1852{
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301853 BAM_DMUX_LOG("%s\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001854 mutex_lock(&dfab_status_lock);
1855 if (!dfab_is_on) {
1856 DMUX_LOG_KERR("%s: dfab is already off\n", __func__);
1857 dump_stack();
1858 mutex_unlock(&dfab_status_lock);
1859 return;
1860 }
Jeff Hugod0befde2012-08-09 15:32:49 -06001861 if (dfab_clk)
1862 clk_disable_unprepare(dfab_clk);
1863 if (xo_clk)
1864 clk_disable_unprepare(xo_clk);
Eric Holmberg006057d2012-01-11 10:10:42 -07001865 dfab_is_on = 0;
1866 mutex_unlock(&dfab_status_lock);
1867}
1868
1869/* reference counting wrapper around wakelock */
1870static void grab_wakelock(void)
1871{
1872 unsigned long flags;
1873
1874 spin_lock_irqsave(&wakelock_reference_lock, flags);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301875 BAM_DMUX_LOG("%s: ref count = %d\n", __func__,
Eric Holmberg006057d2012-01-11 10:10:42 -07001876 wakelock_reference_count);
1877 if (wakelock_reference_count == 0)
1878 wake_lock(&bam_wakelock);
1879 ++wakelock_reference_count;
1880 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1881}
1882
1883static void release_wakelock(void)
1884{
1885 unsigned long flags;
1886
1887 spin_lock_irqsave(&wakelock_reference_lock, flags);
1888 if (wakelock_reference_count == 0) {
1889 DMUX_LOG_KERR("%s: bam_dmux wakelock not locked\n", __func__);
1890 dump_stack();
1891 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1892 return;
1893 }
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301894 BAM_DMUX_LOG("%s: ref count = %d\n", __func__,
Eric Holmberg006057d2012-01-11 10:10:42 -07001895 wakelock_reference_count);
1896 --wakelock_reference_count;
1897 if (wakelock_reference_count == 0)
1898 wake_unlock(&bam_wakelock);
1899 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001900}
1901
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001902static int restart_notifier_cb(struct notifier_block *this,
1903 unsigned long code,
1904 void *data)
1905{
1906 int i;
1907 struct list_head *node;
1908 struct tx_pkt_info *info;
1909 int temp_remote_status;
Jeff Hugo626303bf2011-11-21 11:43:28 -07001910 unsigned long flags;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001911
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001912 /*
1913 * Bam_dmux counts on the fact that the BEFORE_SHUTDOWN level of
1914 * notifications are guarenteed to execute before the AFTER_SHUTDOWN
1915 * level of notifications, and that BEFORE_SHUTDOWN always occurs in
1916 * all SSR events, no matter what triggered the SSR. Also, bam_dmux
1917 * assumes that SMD does its SSR processing in the AFTER_SHUTDOWN level
1918 * thus bam_dmux is guarenteed to detect SSR before SMD, since the
1919 * callbacks for all the drivers within the AFTER_SHUTDOWN level could
1920 * occur in any order. Bam_dmux uses this knowledge to skip accessing
1921 * the bam hardware when disconnect_to_bam() is triggered by SMD's SSR
1922 * processing. We do not wat to access the bam hardware during SSR
1923 * because a watchdog crash from a bus stall would likely occur.
1924 */
Jeff Hugo199294b2013-02-25 13:46:56 -07001925 if (code == SUBSYS_BEFORE_SHUTDOWN) {
1926 in_global_reset = 1;
Jeff Hugoa82a95c2012-12-14 17:56:19 -07001927 in_ssr = 1;
Zaheerulla Meerf800bba2013-02-13 15:49:14 +05301928 BAM_DMUX_LOG("%s: begin\n", __func__);
Jeff Hugo199294b2013-02-25 13:46:56 -07001929 flush_workqueue(bam_mux_rx_workqueue);
1930 }
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001931 if (code != SUBSYS_AFTER_SHUTDOWN)
1932 return NOTIFY_DONE;
1933
Eric Holmberg454d9da2012-01-12 09:37:14 -07001934 /* Handle uplink Powerdown */
1935 write_lock_irqsave(&ul_wakeup_lock, flags);
1936 if (bam_is_connected) {
1937 ul_powerdown();
1938 wait_for_ack = 0;
1939 }
Jeff Hugo4838f412012-01-20 11:19:37 -07001940 /*
1941 * if modem crash during ul_wakeup(), power_vote is 1, needs to be
1942 * reset to 0. harmless if bam_is_connected check above passes
1943 */
1944 power_vote(0);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001945 write_unlock_irqrestore(&ul_wakeup_lock, flags);
1946 ul_powerdown_finish();
Eric Holmberg006057d2012-01-11 10:10:42 -07001947 a2_pc_disabled = 0;
Jeff Hugo583a6da2012-02-03 11:37:30 -07001948 a2_pc_disabled_wakelock_skipped = 0;
Jeff Hugof62029d2012-07-17 13:39:53 -06001949 disconnect_ack = 1;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001950
1951 /* Cleanup Channel States */
Eric Holmberga623da82012-07-12 09:37:09 -06001952 mutex_lock(&bam_pdev_mutexlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001953 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
1954 temp_remote_status = bam_ch_is_remote_open(i);
1955 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001956 bam_ch[i].num_tx_pkts = 0;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001957 if (bam_ch_is_local_open(i))
1958 bam_ch[i].status |= BAM_CH_IN_RESET;
1959 if (temp_remote_status) {
1960 platform_device_unregister(bam_ch[i].pdev);
1961 bam_ch[i].pdev = platform_device_alloc(
1962 bam_ch[i].name, 2);
1963 }
1964 }
Eric Holmberga623da82012-07-12 09:37:09 -06001965 mutex_unlock(&bam_pdev_mutexlock);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001966
1967 /* Cleanup pending UL data */
Jeff Hugo626303bf2011-11-21 11:43:28 -07001968 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001969 while (!list_empty(&bam_tx_pool)) {
1970 node = bam_tx_pool.next;
1971 list_del(node);
1972 info = container_of(node, struct tx_pkt_info,
1973 list_node);
1974 if (!info->is_cmd) {
1975 dma_unmap_single(NULL, info->dma_address,
1976 info->skb->len,
1977 DMA_TO_DEVICE);
1978 dev_kfree_skb_any(info->skb);
1979 } else {
1980 dma_unmap_single(NULL, info->dma_address,
1981 info->len,
1982 DMA_TO_DEVICE);
1983 kfree(info->skb);
1984 }
1985 kfree(info);
1986 }
Jeff Hugo626303bf2011-11-21 11:43:28 -07001987 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001988
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301989 BAM_DMUX_LOG("%s: complete\n", __func__);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001990 return NOTIFY_DONE;
1991}
1992
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001993static int bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001994{
1995 u32 h;
1996 dma_addr_t dma_addr;
1997 int ret;
1998 void *a2_virt_addr;
Jeff Hugo4b2890d2012-01-16 16:14:21 -07001999 int skip_iounmap = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002000
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002001 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002002 /* init BAM */
Jeff Hugo3910ee12012-08-21 14:08:20 -06002003 a2_virt_addr = ioremap_nocache((unsigned long)(a2_phys_base),
2004 a2_phys_size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002005 if (!a2_virt_addr) {
2006 pr_err("%s: ioremap failed\n", __func__);
2007 ret = -ENOMEM;
Jeff Hugo994a92d2012-01-05 13:25:21 -07002008 goto ioremap_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002009 }
Jeff Hugo3910ee12012-08-21 14:08:20 -06002010 a2_props.phys_addr = (u32)(a2_phys_base);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002011 a2_props.virt_addr = a2_virt_addr;
Jeff Hugo3910ee12012-08-21 14:08:20 -06002012 a2_props.virt_size = a2_phys_size;
2013 a2_props.irq = a2_bam_irq;
Jeff Hugo927cba62011-11-11 11:49:52 -07002014 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002015 a2_props.num_pipes = A2_NUM_PIPES;
2016 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
Jeff Hugo75913c82011-12-05 15:59:01 -07002017 if (cpu_is_msm9615())
2018 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002019 /* need to free on tear down */
2020 ret = sps_register_bam_device(&a2_props, &h);
2021 if (ret < 0) {
2022 pr_err("%s: register bam error %d\n", __func__, ret);
2023 goto register_bam_failed;
2024 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002025 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002026
2027 bam_tx_pipe = sps_alloc_endpoint();
2028 if (bam_tx_pipe == NULL) {
2029 pr_err("%s: tx alloc endpoint failed\n", __func__);
2030 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002031 goto tx_alloc_endpoint_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002032 }
2033 ret = sps_get_config(bam_tx_pipe, &tx_connection);
2034 if (ret) {
2035 pr_err("%s: tx get config failed %d\n", __func__, ret);
2036 goto tx_get_config_failed;
2037 }
2038
2039 tx_connection.source = SPS_DEV_HANDLE_MEM;
2040 tx_connection.src_pipe_index = 0;
2041 tx_connection.destination = h;
2042 tx_connection.dest_pipe_index = 4;
2043 tx_connection.mode = SPS_MODE_DEST;
2044 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
2045 tx_desc_mem_buf.size = 0x800; /* 2k */
2046 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
2047 &dma_addr, 0);
2048 if (tx_desc_mem_buf.base == NULL) {
2049 pr_err("%s: tx memory alloc failed\n", __func__);
2050 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002051 goto tx_get_config_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002052 }
2053 tx_desc_mem_buf.phys_base = dma_addr;
2054 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
2055 tx_connection.desc = tx_desc_mem_buf;
2056 tx_connection.event_thresh = 0x10;
2057
2058 ret = sps_connect(bam_tx_pipe, &tx_connection);
2059 if (ret < 0) {
2060 pr_err("%s: tx connect error %d\n", __func__, ret);
2061 goto tx_connect_failed;
2062 }
2063
2064 bam_rx_pipe = sps_alloc_endpoint();
2065 if (bam_rx_pipe == NULL) {
2066 pr_err("%s: rx alloc endpoint failed\n", __func__);
2067 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002068 goto rx_alloc_endpoint_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002069 }
2070 ret = sps_get_config(bam_rx_pipe, &rx_connection);
2071 if (ret) {
2072 pr_err("%s: rx get config failed %d\n", __func__, ret);
2073 goto rx_get_config_failed;
2074 }
2075
2076 rx_connection.source = h;
2077 rx_connection.src_pipe_index = 5;
2078 rx_connection.destination = SPS_DEV_HANDLE_MEM;
2079 rx_connection.dest_pipe_index = 1;
2080 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06002081 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
2082 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002083 rx_desc_mem_buf.size = 0x800; /* 2k */
2084 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
2085 &dma_addr, 0);
2086 if (rx_desc_mem_buf.base == NULL) {
2087 pr_err("%s: rx memory alloc failed\n", __func__);
2088 ret = -ENOMEM;
2089 goto rx_mem_failed;
2090 }
2091 rx_desc_mem_buf.phys_base = dma_addr;
2092 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
2093 rx_connection.desc = rx_desc_mem_buf;
2094 rx_connection.event_thresh = 0x10;
2095
2096 ret = sps_connect(bam_rx_pipe, &rx_connection);
2097 if (ret < 0) {
2098 pr_err("%s: rx connect error %d\n", __func__, ret);
2099 goto rx_connect_failed;
2100 }
2101
2102 tx_register_event.options = SPS_O_EOT;
2103 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
2104 tx_register_event.xfer_done = NULL;
2105 tx_register_event.callback = bam_mux_tx_notify;
2106 tx_register_event.user = NULL;
2107 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
2108 if (ret < 0) {
2109 pr_err("%s: tx register event error %d\n", __func__, ret);
2110 goto rx_event_reg_failed;
2111 }
2112
Jeff Hugo33dbc002011-08-25 15:52:53 -06002113 rx_register_event.options = SPS_O_EOT;
2114 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
2115 rx_register_event.xfer_done = NULL;
2116 rx_register_event.callback = bam_mux_rx_notify;
2117 rx_register_event.user = NULL;
2118 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
2119 if (ret < 0) {
2120 pr_err("%s: tx register event error %d\n", __func__, ret);
2121 goto rx_event_reg_failed;
2122 }
2123
Jeff Hugoc2696142012-05-03 11:42:13 -06002124 mutex_lock(&delayed_ul_vote_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002125 bam_mux_initialized = 1;
Jeff Hugoc2696142012-05-03 11:42:13 -06002126 if (need_delayed_ul_vote) {
2127 need_delayed_ul_vote = 0;
2128 msm_bam_dmux_kickoff_ul_wakeup();
2129 }
2130 mutex_unlock(&delayed_ul_vote_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002131 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002132 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002133 complete_all(&bam_connection_completion);
Jeff Hugo2fb555e2012-03-14 16:33:47 -06002134 queue_rx();
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002135 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002136
2137rx_event_reg_failed:
2138 sps_disconnect(bam_rx_pipe);
2139rx_connect_failed:
2140 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
2141 rx_desc_mem_buf.phys_base);
2142rx_mem_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002143rx_get_config_failed:
2144 sps_free_endpoint(bam_rx_pipe);
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002145rx_alloc_endpoint_failed:
2146 sps_disconnect(bam_tx_pipe);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002147tx_connect_failed:
2148 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
2149 tx_desc_mem_buf.phys_base);
2150tx_get_config_failed:
2151 sps_free_endpoint(bam_tx_pipe);
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002152tx_alloc_endpoint_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002153 sps_deregister_bam_device(h);
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002154 /*
2155 * sps_deregister_bam_device() calls iounmap. calling iounmap on the
2156 * same handle below will cause a crash, so skip it if we've freed
2157 * the handle here.
2158 */
2159 skip_iounmap = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002160register_bam_failed:
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002161 if (!skip_iounmap)
2162 iounmap(a2_virt_addr);
Jeff Hugo994a92d2012-01-05 13:25:21 -07002163ioremap_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002164 /*destroy_workqueue(bam_mux_workqueue);*/
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002165 return ret;
2166}
2167
2168static int bam_init_fallback(void)
2169{
2170 u32 h;
2171 int ret;
2172 void *a2_virt_addr;
2173
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002174 /* init BAM */
Jeff Hugo3910ee12012-08-21 14:08:20 -06002175 a2_virt_addr = ioremap_nocache((unsigned long)(a2_phys_base),
2176 a2_phys_size);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002177 if (!a2_virt_addr) {
2178 pr_err("%s: ioremap failed\n", __func__);
2179 ret = -ENOMEM;
2180 goto ioremap_failed;
2181 }
Jeff Hugo3910ee12012-08-21 14:08:20 -06002182 a2_props.phys_addr = (u32)(a2_phys_base);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002183 a2_props.virt_addr = a2_virt_addr;
Jeff Hugo3910ee12012-08-21 14:08:20 -06002184 a2_props.virt_size = a2_phys_size;
2185 a2_props.irq = a2_bam_irq;
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002186 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
2187 a2_props.num_pipes = A2_NUM_PIPES;
2188 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
2189 if (cpu_is_msm9615())
2190 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
2191 ret = sps_register_bam_device(&a2_props, &h);
2192 if (ret < 0) {
2193 pr_err("%s: register bam error %d\n", __func__, ret);
2194 goto register_bam_failed;
2195 }
2196 a2_device_handle = h;
Jeff Hugoc2696142012-05-03 11:42:13 -06002197
2198 mutex_lock(&delayed_ul_vote_lock);
2199 bam_mux_initialized = 1;
2200 if (need_delayed_ul_vote) {
2201 need_delayed_ul_vote = 0;
2202 msm_bam_dmux_kickoff_ul_wakeup();
2203 }
2204 mutex_unlock(&delayed_ul_vote_lock);
Jeff Hugo2bec9772012-04-05 12:25:16 -06002205 toggle_apps_ack();
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002206
Jeff Hugo18792a32012-06-20 15:25:55 -06002207 power_management_only_mode = 1;
2208 bam_connection_is_active = 1;
2209 complete_all(&bam_connection_completion);
2210
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002211 return 0;
2212
2213register_bam_failed:
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002214 iounmap(a2_virt_addr);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002215ioremap_failed:
2216 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002217}
Jeff Hugoade1f842011-08-03 15:53:59 -06002218
Jeff Hugoa670b762012-03-15 15:58:28 -06002219static void msm9615_bam_init(void)
Eric Holmberg604ab252012-01-15 00:01:18 -07002220{
2221 int ret = 0;
2222
2223 ret = bam_init();
2224 if (ret) {
2225 ret = bam_init_fallback();
2226 if (ret)
2227 pr_err("%s: bam init fallback failed: %d",
2228 __func__, ret);
2229 }
2230}
2231
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002232static void toggle_apps_ack(void)
2233{
2234 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
Eric Holmberg878923a2012-01-10 14:28:19 -07002235
Eric Holmberg7614a7f2013-07-29 15:47:12 -06002236 if (in_global_reset) {
2237 BAM_DMUX_LOG("%s: skipped due to SSR\n", __func__);
2238 return;
2239 }
2240
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302241 BAM_DMUX_LOG("%s: apps ack %d->%d\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -07002242 clear_bit & 0x1, ~clear_bit & 0x1);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002243 smsm_change_state(SMSM_APPS_STATE,
2244 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
2245 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
2246 clear_bit = ~clear_bit;
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002247 DBG_INC_ACK_OUT_CNT();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002248}
2249
Jeff Hugoade1f842011-08-03 15:53:59 -06002250static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
2251{
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002252 static int last_processed_state;
2253
2254 mutex_lock(&smsm_cb_lock);
Eric Holmberg878923a2012-01-10 14:28:19 -07002255 bam_dmux_power_state = new_state & SMSM_A2_POWER_CONTROL ? 1 : 0;
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002256 DBG_INC_A2_POWER_CONTROL_IN_CNT();
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302257 BAM_DMUX_LOG("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
Eric Holmberg878923a2012-01-10 14:28:19 -07002258 new_state);
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002259 if (last_processed_state == (new_state & SMSM_A2_POWER_CONTROL)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302260 BAM_DMUX_LOG("%s: already processed this state\n", __func__);
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002261 mutex_unlock(&smsm_cb_lock);
2262 return;
2263 }
2264
2265 last_processed_state = new_state & SMSM_A2_POWER_CONTROL;
Eric Holmberg878923a2012-01-10 14:28:19 -07002266
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002267 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302268 BAM_DMUX_LOG("%s: reconnect\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07002269 grab_wakelock();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002270 reconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002271 } else if (bam_mux_initialized &&
2272 !(new_state & SMSM_A2_POWER_CONTROL)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302273 BAM_DMUX_LOG("%s: disconnect\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002274 disconnect_to_bam();
Eric Holmberg006057d2012-01-11 10:10:42 -07002275 release_wakelock();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002276 } else if (new_state & SMSM_A2_POWER_CONTROL) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302277 BAM_DMUX_LOG("%s: init\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07002278 grab_wakelock();
Jeff Hugoa670b762012-03-15 15:58:28 -06002279 if (cpu_is_msm9615())
2280 msm9615_bam_init();
2281 else
Eric Holmberg604ab252012-01-15 00:01:18 -07002282 bam_init();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002283 } else {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302284 BAM_DMUX_LOG("%s: bad state change\n", __func__);
Jeff Hugoade1f842011-08-03 15:53:59 -06002285 pr_err("%s: unsupported state change\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002286 }
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002287 mutex_unlock(&smsm_cb_lock);
Jeff Hugoade1f842011-08-03 15:53:59 -06002288
2289}
2290
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002291static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
2292 uint32_t new_state)
2293{
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002294 DBG_INC_ACK_IN_CNT();
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302295 BAM_DMUX_LOG("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
Eric Holmberg878923a2012-01-10 14:28:19 -07002296 new_state);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002297 complete_all(&ul_wakeup_ack_completion);
2298}
2299
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002300static int bam_dmux_probe(struct platform_device *pdev)
2301{
2302 int rc;
Jeff Hugo3910ee12012-08-21 14:08:20 -06002303 struct resource *r;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002304
2305 DBG("%s probe called\n", __func__);
2306 if (bam_mux_initialized)
2307 return 0;
2308
Jeff Hugo3910ee12012-08-21 14:08:20 -06002309 if (pdev->dev.of_node) {
2310 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2311 if (!r) {
2312 pr_err("%s: reg field missing\n", __func__);
2313 return -ENODEV;
2314 }
2315 a2_phys_base = (void *)(r->start);
2316 a2_phys_size = (uint32_t)(resource_size(r));
2317 a2_bam_irq = platform_get_irq(pdev, 0);
2318 if (a2_bam_irq == -ENXIO) {
2319 pr_err("%s: irq field missing\n", __func__);
2320 return -ENODEV;
2321 }
2322 DBG("%s: base:%p size:%x irq:%d\n", __func__,
2323 a2_phys_base,
2324 a2_phys_size,
2325 a2_bam_irq);
2326 } else { /* fallback to default init data */
2327 a2_phys_base = (void *)(A2_PHYS_BASE);
2328 a2_phys_size = A2_PHYS_SIZE;
2329 a2_bam_irq = A2_BAM_IRQ;
2330 }
2331
Stephen Boyd69d35e32012-02-14 15:33:30 -08002332 xo_clk = clk_get(&pdev->dev, "xo");
2333 if (IS_ERR(xo_clk)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302334 BAM_DMUX_LOG("%s: did not get xo clock\n", __func__);
Jeff Hugod0befde2012-08-09 15:32:49 -06002335 xo_clk = NULL;
Stephen Boyd69d35e32012-02-14 15:33:30 -08002336 }
Stephen Boyd1c51a492011-10-26 12:11:47 -07002337 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002338 if (IS_ERR(dfab_clk)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302339 BAM_DMUX_LOG("%s: did not get dfab clock\n", __func__);
Jeff Hugod0befde2012-08-09 15:32:49 -06002340 dfab_clk = NULL;
2341 } else {
2342 rc = clk_set_rate(dfab_clk, 64000000);
2343 if (rc)
2344 pr_err("%s: unable to set dfab clock rate\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002345 }
2346
Jeff Hugofff43af92012-03-29 17:54:52 -06002347 /*
2348 * setup the workqueue so that it can be pinned to core 0 and not
2349 * block the watchdog pet function, so that netif_rx() in rmnet
2350 * only uses one queue.
2351 */
2352 bam_mux_rx_workqueue = alloc_workqueue("bam_dmux_rx",
2353 WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002354 if (!bam_mux_rx_workqueue)
2355 return -ENOMEM;
2356
2357 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
2358 if (!bam_mux_tx_workqueue) {
2359 destroy_workqueue(bam_mux_rx_workqueue);
2360 return -ENOMEM;
2361 }
2362
Jeff Hugo7960abd2011-08-02 15:39:38 -06002363 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002364 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06002365 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
2366 "bam_dmux_ch_%d", rc);
2367 /* bus 2, ie a2 stream 2 */
2368 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
2369 if (!bam_ch[rc].pdev) {
2370 pr_err("%s: platform device alloc failed\n", __func__);
2371 destroy_workqueue(bam_mux_rx_workqueue);
2372 destroy_workqueue(bam_mux_tx_workqueue);
2373 return -ENOMEM;
2374 }
2375 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002376
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002377 init_completion(&ul_wakeup_ack_completion);
2378 init_completion(&bam_connection_completion);
Eric Holmberg006057d2012-01-11 10:10:42 -07002379 init_completion(&dfab_unvote_completion);
Brent Hronik096f7d32013-06-28 15:43:08 -06002380 init_completion(&shutdown_completion);
2381 complete_all(&shutdown_completion);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002382 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
Jeff Hugo988e7ba2012-10-03 15:53:54 -06002383 INIT_DELAYED_WORK(&queue_rx_work, queue_rx_work_func);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002384 wake_lock_init(&bam_wakelock, WAKE_LOCK_SUSPEND, "bam_dmux_wakelock");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002385
Jeff Hugoade1f842011-08-03 15:53:59 -06002386 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
2387 bam_dmux_smsm_cb, NULL);
2388
2389 if (rc) {
2390 destroy_workqueue(bam_mux_rx_workqueue);
2391 destroy_workqueue(bam_mux_tx_workqueue);
2392 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
2393 return -ENOMEM;
2394 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002395
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002396 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
2397 bam_dmux_smsm_ack_cb, NULL);
2398
2399 if (rc) {
2400 destroy_workqueue(bam_mux_rx_workqueue);
2401 destroy_workqueue(bam_mux_tx_workqueue);
2402 smsm_state_cb_deregister(SMSM_MODEM_STATE,
2403 SMSM_A2_POWER_CONTROL,
2404 bam_dmux_smsm_cb, NULL);
2405 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
2406 rc);
2407 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
2408 platform_device_put(bam_ch[rc].pdev);
2409 return -ENOMEM;
2410 }
2411
Eric Holmbergfd1e2ae2011-11-15 18:28:17 -07002412 if (smsm_get_state(SMSM_MODEM_STATE) & SMSM_A2_POWER_CONTROL)
2413 bam_dmux_smsm_cb(NULL, 0, smsm_get_state(SMSM_MODEM_STATE));
2414
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002415 return 0;
2416}
2417
Jeff Hugo3910ee12012-08-21 14:08:20 -06002418static struct of_device_id msm_match_table[] = {
2419 {.compatible = "qcom,bam_dmux"},
2420 {},
2421};
2422
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002423static struct platform_driver bam_dmux_driver = {
2424 .probe = bam_dmux_probe,
2425 .driver = {
2426 .name = "BAM_RMNT",
2427 .owner = THIS_MODULE,
Jeff Hugo3910ee12012-08-21 14:08:20 -06002428 .of_match_table = msm_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002429 },
2430};
2431
2432static int __init bam_dmux_init(void)
2433{
2434#ifdef CONFIG_DEBUG_FS
2435 struct dentry *dent;
2436
2437 dent = debugfs_create_dir("bam_dmux", 0);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002438 if (!IS_ERR(dent)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002439 debug_create("tbl", 0444, dent, debug_tbl);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002440 debug_create("ul_pkt_cnt", 0444, dent, debug_ul_pkt_cnt);
2441 debug_create("stats", 0444, dent, debug_stats);
2442 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002443#endif
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302444
2445 bam_ipc_log_txt = ipc_log_context_create(BAM_IPC_LOG_PAGES, "bam_dmux");
2446 if (!bam_ipc_log_txt) {
2447 pr_err("%s : unable to create IPC Logging Context", __func__);
Eric Holmberg878923a2012-01-10 14:28:19 -07002448 }
2449
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07002450 rx_timer_interval = DEFAULT_POLLING_MIN_SLEEP;
2451
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002452 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002453 return platform_driver_register(&bam_dmux_driver);
2454}
2455
Jeff Hugoade1f842011-08-03 15:53:59 -06002456late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002457MODULE_DESCRIPTION("MSM BAM DMUX");
2458MODULE_LICENSE("GPL v2");