blob: 67c3914f38cf08b061a678c00c93aa5db6792a71 [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
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600234static void toggle_apps_ack(void);
235static void reconnect_to_bam(void);
236static void disconnect_to_bam(void);
237static void ul_wakeup(void);
238static void ul_timeout(struct work_struct *work);
239static void vote_dfab(void);
240static void unvote_dfab(void);
Jeff Hugod98b1082011-10-24 10:30:23 -0600241static void kickoff_ul_wakeup_func(struct work_struct *work);
Eric Holmberg006057d2012-01-11 10:10:42 -0700242static void grab_wakelock(void);
243static void release_wakelock(void);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600244
245static int bam_is_connected;
246static DEFINE_MUTEX(wakeup_lock);
247static struct completion ul_wakeup_ack_completion;
248static struct completion bam_connection_completion;
249static struct delayed_work ul_timeout_work;
250static int ul_packet_written;
Eric Holmbergbc9f21c2012-01-18 11:33:33 -0700251static atomic_t ul_ondemand_vote = ATOMIC_INIT(0);
Stephen Boyd69d35e32012-02-14 15:33:30 -0800252static struct clk *dfab_clk, *xo_clk;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600253static DEFINE_RWLOCK(ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600254static DECLARE_WORK(kickoff_ul_wakeup, kickoff_ul_wakeup_func);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600255static int bam_connection_is_active;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -0700256static int wait_for_ack;
Jeff Hugoae3a85e2011-12-02 17:10:18 -0700257static struct wake_lock bam_wakelock;
Eric Holmberg006057d2012-01-11 10:10:42 -0700258static int a2_pc_disabled;
259static DEFINE_MUTEX(dfab_status_lock);
260static int dfab_is_on;
261static int wait_for_dfab;
262static struct completion dfab_unvote_completion;
263static DEFINE_SPINLOCK(wakelock_reference_lock);
264static int wakelock_reference_count;
Jeff Hugo583a6da2012-02-03 11:37:30 -0700265static int a2_pc_disabled_wakelock_skipped;
Jeff Hugob1e7c582012-06-20 15:02:11 -0600266static int disconnect_ack = 1;
Jeff Hugocb798022012-04-09 14:55:40 -0600267static LIST_HEAD(bam_other_notify_funcs);
Jeff Hugo4b7c7b32012-04-18 16:25:14 -0600268static DEFINE_MUTEX(smsm_cb_lock);
Jeff Hugoc2696142012-05-03 11:42:13 -0600269static DEFINE_MUTEX(delayed_ul_vote_lock);
270static int need_delayed_ul_vote;
Jeff Hugo18792a32012-06-20 15:25:55 -0600271static int power_management_only_mode;
Jeff Hugocb798022012-04-09 14:55:40 -0600272
273struct outside_notify_func {
274 void (*notify)(void *, int, unsigned long);
275 void *priv;
276 struct list_head list_node;
277};
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600278/* End A2 power collaspe */
279
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600280/* subsystem restart */
281static int restart_notifier_cb(struct notifier_block *this,
282 unsigned long code,
283 void *data);
284
285static struct notifier_block restart_notifier = {
286 .notifier_call = restart_notifier_cb,
287};
288static int in_global_reset;
289/* end subsystem restart */
290
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700291#define bam_ch_is_open(x) \
292 (bam_ch[(x)].status == (BAM_CH_LOCAL_OPEN | BAM_CH_REMOTE_OPEN))
293
294#define bam_ch_is_local_open(x) \
295 (bam_ch[(x)].status & BAM_CH_LOCAL_OPEN)
296
297#define bam_ch_is_remote_open(x) \
298 (bam_ch[(x)].status & BAM_CH_REMOTE_OPEN)
299
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600300#define bam_ch_is_in_reset(x) \
301 (bam_ch[(x)].status & BAM_CH_IN_RESET)
302
Eric Holmberg878923a2012-01-10 14:28:19 -0700303struct kfifo bam_dmux_state_log;
Eric Holmberg878923a2012-01-10 14:28:19 -0700304static int bam_dmux_uplink_vote;
305static int bam_dmux_power_state;
306
307
308#define DMUX_LOG_KERR(fmt...) \
309do { \
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530310 BAM_DMUX_LOG(fmt); \
Eric Holmberg878923a2012-01-10 14:28:19 -0700311 pr_err(fmt); \
312} while (0)
313
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530314static void *bam_ipc_log_txt;
315
316#define BAM_IPC_LOG_PAGES 5
317
Eric Holmberg878923a2012-01-10 14:28:19 -0700318/**
319 * Log a state change along with a small message.
Eric Holmberg878923a2012-01-10 14:28:19 -0700320 * Complete size of messsage is limited to @todo.
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530321 * Logging is done using IPC Logging infrastructure.
322 *
323 * States
324 * D: 1 = Power collapse disabled
325 * R: 1 = in global reset
326 * P: 1 = BAM is powered up
327 * A: 1 = BAM initialized and ready for data
328 * V: 1 = Uplink vote for power
329 * U: 1 = Uplink active
330 * W: 1 = Uplink Wait-for-ack
331 * A: 1 = Uplink ACK received
332 * #: >=1 On-demand uplink vote
333 * D: 1 = Disconnect ACK active
Eric Holmberg878923a2012-01-10 14:28:19 -0700334 */
Eric Holmberg878923a2012-01-10 14:28:19 -0700335
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530336#define BAM_DMUX_LOG(fmt, args...) \
337do { \
338 if (bam_ipc_log_txt) { \
339 ipc_log_string(bam_ipc_log_txt, \
340 "<DMUX> %c%c%c%c %c%c%c%c%d%c " fmt, \
341 a2_pc_disabled ? 'D' : 'd', \
342 in_global_reset ? 'R' : 'r', \
343 bam_dmux_power_state ? 'P' : 'p', \
344 bam_connection_is_active ? 'A' : 'a', \
345 bam_dmux_uplink_vote ? 'V' : 'v', \
346 bam_is_connected ? 'U' : 'u', \
347 wait_for_ack ? 'W' : 'w', \
348 ul_wakeup_ack_completion.done ? 'A' : 'a', \
349 atomic_read(&ul_ondemand_vote), \
350 disconnect_ack ? 'D' : 'd', \
351 args); \
352 } \
353} while (0)
Eric Holmberg878923a2012-01-10 14:28:19 -0700354
355static inline void set_tx_timestamp(struct tx_pkt_info *pkt)
356{
357 unsigned long long t_now;
358
359 t_now = sched_clock();
360 pkt->ts_nsec = do_div(t_now, 1000000000U);
361 pkt->ts_sec = (unsigned)t_now;
362}
363
364static inline void verify_tx_queue_is_empty(const char *func)
365{
366 unsigned long flags;
367 struct tx_pkt_info *info;
368 int reported = 0;
369
370 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
371 list_for_each_entry(info, &bam_tx_pool, list_node) {
372 if (!reported) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530373 BAM_DMUX_LOG("%s: tx pool not empty\n", func);
Eric Holmberg454d9da2012-01-12 09:37:14 -0700374 if (!in_global_reset)
375 pr_err("%s: tx pool not empty\n", func);
Eric Holmberg878923a2012-01-10 14:28:19 -0700376 reported = 1;
377 }
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530378 BAM_DMUX_LOG("%s: node=%p ts=%u.%09lu\n", __func__,
Eric Holmberg454d9da2012-01-12 09:37:14 -0700379 &info->list_node, info->ts_sec, info->ts_nsec);
380 if (!in_global_reset)
381 pr_err("%s: node=%p ts=%u.%09lu\n", __func__,
382 &info->list_node, info->ts_sec, info->ts_nsec);
Eric Holmberg878923a2012-01-10 14:28:19 -0700383 }
384 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
385}
386
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700387static void queue_rx(void)
388{
389 void *ptr;
390 struct rx_pkt_info *info;
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700391 int ret;
392 int rx_len_cached;
Jeff Hugo949080a2011-08-30 11:58:56 -0600393
Jeff Hugoc9749932011-11-02 17:50:40 -0600394 mutex_lock(&bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700395 rx_len_cached = bam_rx_pool_len;
Jeff Hugoc9749932011-11-02 17:50:40 -0600396 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -0600397
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600398 while (bam_connection_is_active && rx_len_cached < NUM_BUFFERS) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700399 if (in_global_reset)
400 goto fail;
401
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600402 info = kmalloc(sizeof(struct rx_pkt_info),
403 GFP_NOWAIT | __GFP_NOWARN);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700404 if (!info) {
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600405 DMUX_LOG_KERR(
406 "%s: unable to alloc rx_pkt_info, will retry later\n",
407 __func__);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700408 goto fail;
409 }
410
411 INIT_WORK(&info->work, handle_bam_mux_cmd);
412
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600413 info->skb = __dev_alloc_skb(BUFFER_SIZE,
414 GFP_NOWAIT | __GFP_NOWARN);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700415 if (info->skb == NULL) {
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600416 DMUX_LOG_KERR(
417 "%s: unable to alloc skb, will retry later\n",
418 __func__);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700419 goto fail_info;
420 }
421 ptr = skb_put(info->skb, BUFFER_SIZE);
422
423 info->dma_address = dma_map_single(NULL, ptr, BUFFER_SIZE,
424 DMA_FROM_DEVICE);
425 if (info->dma_address == 0 || info->dma_address == ~0) {
426 DMUX_LOG_KERR("%s: dma_map_single failure %p for %p\n",
427 __func__, (void *)info->dma_address, ptr);
428 goto fail_skb;
429 }
430
431 mutex_lock(&bam_rx_pool_mutexlock);
432 list_add_tail(&info->list_node, &bam_rx_pool);
433 rx_len_cached = ++bam_rx_pool_len;
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700434 ret = sps_transfer_one(bam_rx_pipe, info->dma_address,
435 BUFFER_SIZE, info,
436 SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700437 if (ret) {
Eric Holmberg00cf8692012-07-16 14:21:19 -0600438 list_del(&info->list_node);
439 rx_len_cached = --bam_rx_pool_len;
440 mutex_unlock(&bam_rx_pool_mutexlock);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700441 DMUX_LOG_KERR("%s: sps_transfer_one failed %d\n",
442 __func__, ret);
Eric Holmberg00cf8692012-07-16 14:21:19 -0600443
444 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
445 DMA_FROM_DEVICE);
446
447 goto fail_skb;
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700448 }
Eric Holmberg00cf8692012-07-16 14:21:19 -0600449 mutex_unlock(&bam_rx_pool_mutexlock);
450
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700451 }
452 return;
453
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700454fail_skb:
455 dev_kfree_skb_any(info->skb);
456
457fail_info:
458 kfree(info);
459
460fail:
Arun Kumar Neelakantam799447f2012-12-13 18:06:49 +0530461 if (rx_len_cached == 0 && !in_global_reset) {
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600462 DMUX_LOG_KERR("%s: rescheduling\n", __func__);
463 schedule_delayed_work(&queue_rx_work, msecs_to_jiffies(100));
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700464 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700465}
466
Jeff Hugo988e7ba2012-10-03 15:53:54 -0600467static void queue_rx_work_func(struct work_struct *work)
468{
469 queue_rx();
470}
471
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700472static void bam_mux_process_data(struct sk_buff *rx_skb)
473{
474 unsigned long flags;
475 struct bam_mux_hdr *rx_hdr;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600476 unsigned long event_data;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700477
478 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
479
480 rx_skb->data = (unsigned char *)(rx_hdr + 1);
481 rx_skb->tail = rx_skb->data + rx_hdr->pkt_len;
482 rx_skb->len = rx_hdr->pkt_len;
Jeff Hugoee88f672011-10-04 17:14:52 -0600483 rx_skb->truesize = rx_hdr->pkt_len + sizeof(struct sk_buff);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700484
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600485 event_data = (unsigned long)(rx_skb);
486
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700487 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600488 if (bam_ch[rx_hdr->ch_id].notify)
489 bam_ch[rx_hdr->ch_id].notify(
490 bam_ch[rx_hdr->ch_id].priv, BAM_DMUX_RECEIVE,
491 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700492 else
493 dev_kfree_skb_any(rx_skb);
494 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
495
496 queue_rx();
497}
498
Eric Holmberg006057d2012-01-11 10:10:42 -0700499static inline void handle_bam_mux_cmd_open(struct bam_mux_hdr *rx_hdr)
500{
501 unsigned long flags;
502 int ret;
503
Eric Holmberga623da82012-07-12 09:37:09 -0600504 mutex_lock(&bam_pdev_mutexlock);
505 if (in_global_reset) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530506 BAM_DMUX_LOG("%s: open cid %d aborted due to ssr\n",
Eric Holmberga623da82012-07-12 09:37:09 -0600507 __func__, rx_hdr->ch_id);
508 mutex_unlock(&bam_pdev_mutexlock);
509 queue_rx();
510 return;
511 }
Eric Holmberg006057d2012-01-11 10:10:42 -0700512 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
513 bam_ch[rx_hdr->ch_id].status |= BAM_CH_REMOTE_OPEN;
514 bam_ch[rx_hdr->ch_id].num_tx_pkts = 0;
515 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Eric Holmberg006057d2012-01-11 10:10:42 -0700516 ret = platform_device_add(bam_ch[rx_hdr->ch_id].pdev);
517 if (ret)
518 pr_err("%s: platform_device_add() error: %d\n",
519 __func__, ret);
Eric Holmberga623da82012-07-12 09:37:09 -0600520 mutex_unlock(&bam_pdev_mutexlock);
521 queue_rx();
Eric Holmberg006057d2012-01-11 10:10:42 -0700522}
523
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700524static void handle_bam_mux_cmd(struct work_struct *work)
525{
526 unsigned long flags;
527 struct bam_mux_hdr *rx_hdr;
528 struct rx_pkt_info *info;
529 struct sk_buff *rx_skb;
530
531 info = container_of(work, struct rx_pkt_info, work);
532 rx_skb = info->skb;
Jeff Hugo949080a2011-08-30 11:58:56 -0600533 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE, DMA_FROM_DEVICE);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700534 kfree(info);
535
536 rx_hdr = (struct bam_mux_hdr *)rx_skb->data;
537
538 DBG_INC_READ_CNT(sizeof(struct bam_mux_hdr));
539 DBG("%s: magic %x reserved %d cmd %d pad %d ch %d len %d\n", __func__,
540 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
541 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
542 if (rx_hdr->magic_num != BAM_MUX_HDR_MAGIC_NO) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700543 DMUX_LOG_KERR("%s: dropping invalid hdr. magic %x"
544 " reserved %d cmd %d"
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700545 " pad %d ch %d len %d\n", __func__,
546 rx_hdr->magic_num, rx_hdr->reserved, rx_hdr->cmd,
547 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
548 dev_kfree_skb_any(rx_skb);
549 queue_rx();
550 return;
551 }
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700552
553 if (rx_hdr->ch_id >= BAM_DMUX_NUM_CHANNELS) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700554 DMUX_LOG_KERR("%s: dropping invalid LCID %d"
555 " reserved %d cmd %d"
Eric Holmberg9ff40a52011-11-17 19:17:00 -0700556 " pad %d ch %d len %d\n", __func__,
557 rx_hdr->ch_id, rx_hdr->reserved, rx_hdr->cmd,
558 rx_hdr->pad_len, rx_hdr->ch_id, rx_hdr->pkt_len);
559 dev_kfree_skb_any(rx_skb);
560 queue_rx();
561 return;
562 }
563
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700564 switch (rx_hdr->cmd) {
565 case BAM_MUX_HDR_CMD_DATA:
566 DBG_INC_READ_CNT(rx_hdr->pkt_len);
567 bam_mux_process_data(rx_skb);
568 break;
569 case BAM_MUX_HDR_CMD_OPEN:
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530570 BAM_DMUX_LOG("%s: opening cid %d PC enabled\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -0700571 rx_hdr->ch_id);
Eric Holmberg006057d2012-01-11 10:10:42 -0700572 handle_bam_mux_cmd_open(rx_hdr);
Jeff Hugob1e7c582012-06-20 15:02:11 -0600573 if (!(rx_hdr->reserved & ENABLE_DISCONNECT_ACK)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530574 BAM_DMUX_LOG("%s: deactivating disconnect ack\n",
Jeff Hugod7d2b062012-07-24 14:29:56 -0600575 __func__);
Jeff Hugob1e7c582012-06-20 15:02:11 -0600576 disconnect_ack = 0;
Jeff Hugo0b13a352012-03-17 23:18:30 -0600577 }
Eric Holmberg006057d2012-01-11 10:10:42 -0700578 dev_kfree_skb_any(rx_skb);
579 break;
580 case BAM_MUX_HDR_CMD_OPEN_NO_A2_PC:
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530581 BAM_DMUX_LOG("%s: opening cid %d PC disabled\n", __func__,
Eric Holmberg006057d2012-01-11 10:10:42 -0700582 rx_hdr->ch_id);
583
584 if (!a2_pc_disabled) {
585 a2_pc_disabled = 1;
Jeff Hugo322179f2012-02-29 10:52:34 -0700586 ul_wakeup();
Eric Holmberg006057d2012-01-11 10:10:42 -0700587 }
588
589 handle_bam_mux_cmd_open(rx_hdr);
Eric Holmberge779dba2011-11-04 18:22:01 -0600590 dev_kfree_skb_any(rx_skb);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700591 break;
592 case BAM_MUX_HDR_CMD_CLOSE:
593 /* probably should drop pending write */
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530594 BAM_DMUX_LOG("%s: closing cid %d\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -0700595 rx_hdr->ch_id);
Eric Holmberga623da82012-07-12 09:37:09 -0600596 mutex_lock(&bam_pdev_mutexlock);
597 if (in_global_reset) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +0530598 BAM_DMUX_LOG("%s: close cid %d aborted due to ssr\n",
Eric Holmberga623da82012-07-12 09:37:09 -0600599 __func__, rx_hdr->ch_id);
600 mutex_unlock(&bam_pdev_mutexlock);
601 break;
602 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700603 spin_lock_irqsave(&bam_ch[rx_hdr->ch_id].lock, flags);
604 bam_ch[rx_hdr->ch_id].status &= ~BAM_CH_REMOTE_OPEN;
605 spin_unlock_irqrestore(&bam_ch[rx_hdr->ch_id].lock, flags);
Jeff Hugo7960abd2011-08-02 15:39:38 -0600606 platform_device_unregister(bam_ch[rx_hdr->ch_id].pdev);
607 bam_ch[rx_hdr->ch_id].pdev =
608 platform_device_alloc(bam_ch[rx_hdr->ch_id].name, 2);
609 if (!bam_ch[rx_hdr->ch_id].pdev)
610 pr_err("%s: platform_device_alloc failed\n", __func__);
Eric Holmberga623da82012-07-12 09:37:09 -0600611 mutex_unlock(&bam_pdev_mutexlock);
Eric Holmberge779dba2011-11-04 18:22:01 -0600612 dev_kfree_skb_any(rx_skb);
Eric Holmberga623da82012-07-12 09:37:09 -0600613 queue_rx();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700614 break;
615 default:
Eric Holmberg878923a2012-01-10 14:28:19 -0700616 DMUX_LOG_KERR("%s: dropping invalid hdr. magic %x"
617 " reserved %d cmd %d pad %d ch %d len %d\n",
618 __func__, rx_hdr->magic_num, rx_hdr->reserved,
619 rx_hdr->cmd, rx_hdr->pad_len, rx_hdr->ch_id,
620 rx_hdr->pkt_len);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700621 dev_kfree_skb_any(rx_skb);
622 queue_rx();
623 return;
624 }
625}
626
627static int bam_mux_write_cmd(void *data, uint32_t len)
628{
629 int rc;
630 struct tx_pkt_info *pkt;
631 dma_addr_t dma_address;
Jeff Hugo626303bf2011-11-21 11:43:28 -0700632 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633
Eric Holmbergd83cd2b2011-11-04 15:54:17 -0600634 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700635 if (pkt == NULL) {
636 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
637 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700638 return rc;
639 }
640
641 dma_address = dma_map_single(NULL, data, len,
642 DMA_TO_DEVICE);
643 if (!dma_address) {
644 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugo96cb7482011-12-07 13:28:31 -0700645 kfree(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700646 rc = -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700647 return rc;
648 }
649 pkt->skb = (struct sk_buff *)(data);
650 pkt->len = len;
651 pkt->dma_address = dma_address;
652 pkt->is_cmd = 1;
Eric Holmberg878923a2012-01-10 14:28:19 -0700653 set_tx_timestamp(pkt);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600654 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700655 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600656 list_add_tail(&pkt->list_node, &bam_tx_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700657 rc = sps_transfer_one(bam_tx_pipe, dma_address, len,
658 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600659 if (rc) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700660 DMUX_LOG_KERR("%s sps_transfer_one failed rc=%d\n",
661 __func__, rc);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600662 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700663 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700664 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700665 dma_unmap_single(NULL, pkt->dma_address,
666 pkt->len,
667 DMA_TO_DEVICE);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600668 kfree(pkt);
Jeff Hugobb6da952012-01-16 15:02:42 -0700669 } else {
670 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600671 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700672
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600673 ul_packet_written = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700674 return rc;
675}
676
677static void bam_mux_write_done(struct work_struct *work)
678{
679 struct sk_buff *skb;
680 struct bam_mux_hdr *hdr;
681 struct tx_pkt_info *info;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700682 struct tx_pkt_info *info_expected;
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600683 unsigned long event_data;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700684 unsigned long flags;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700685
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600686 if (in_global_reset)
687 return;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700688
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700689 info = container_of(work, struct tx_pkt_info, work);
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700690
691 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
692 info_expected = list_first_entry(&bam_tx_pool,
693 struct tx_pkt_info, list_node);
694 if (unlikely(info != info_expected)) {
Eric Holmberg878923a2012-01-10 14:28:19 -0700695 struct tx_pkt_info *errant_pkt;
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700696
Eric Holmberg878923a2012-01-10 14:28:19 -0700697 DMUX_LOG_KERR("%s: bam_tx_pool mismatch .next=%p,"
698 " list_node=%p, ts=%u.%09lu\n",
699 __func__, bam_tx_pool.next, &info->list_node,
700 info->ts_sec, info->ts_nsec
701 );
702
703 list_for_each_entry(errant_pkt, &bam_tx_pool, list_node) {
704 DMUX_LOG_KERR("%s: node=%p ts=%u.%09lu\n", __func__,
705 &errant_pkt->list_node, errant_pkt->ts_sec,
706 errant_pkt->ts_nsec);
707
708 }
Eric Holmberg1cde7a62011-12-19 18:34:01 -0700709 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
710 BUG();
711 }
712 list_del(&info->list_node);
713 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
714
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600715 if (info->is_cmd) {
716 kfree(info->skb);
717 kfree(info);
718 return;
719 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700720 skb = info->skb;
721 kfree(info);
722 hdr = (struct bam_mux_hdr *)skb->data;
Eric Holmberg9fdef262012-02-14 11:46:05 -0700723 DBG_INC_WRITE_CNT(skb->len);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600724 event_data = (unsigned long)(skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700725 spin_lock_irqsave(&bam_ch[hdr->ch_id].lock, flags);
726 bam_ch[hdr->ch_id].num_tx_pkts--;
727 spin_unlock_irqrestore(&bam_ch[hdr->ch_id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600728 if (bam_ch[hdr->ch_id].notify)
729 bam_ch[hdr->ch_id].notify(
730 bam_ch[hdr->ch_id].priv, BAM_DMUX_WRITE_DONE,
731 event_data);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700732 else
733 dev_kfree_skb_any(skb);
734}
735
736int msm_bam_dmux_write(uint32_t id, struct sk_buff *skb)
737{
738 int rc = 0;
739 struct bam_mux_hdr *hdr;
740 unsigned long flags;
741 struct sk_buff *new_skb = NULL;
742 dma_addr_t dma_address;
743 struct tx_pkt_info *pkt;
744
745 if (id >= BAM_DMUX_NUM_CHANNELS)
746 return -EINVAL;
747 if (!skb)
748 return -EINVAL;
749 if (!bam_mux_initialized)
750 return -ENODEV;
751
752 DBG("%s: writing to ch %d len %d\n", __func__, id, skb->len);
753 spin_lock_irqsave(&bam_ch[id].lock, flags);
754 if (!bam_ch_is_open(id)) {
755 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
756 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
757 return -ENODEV;
758 }
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700759
760 if (bam_ch[id].use_wm &&
761 (bam_ch[id].num_tx_pkts >= HIGH_WATERMARK)) {
762 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
763 pr_err("%s: watermark exceeded: %d\n", __func__, id);
764 return -EAGAIN;
765 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700766 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
767
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600768 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600769 if (!bam_is_connected) {
770 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600771 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700772 if (unlikely(in_global_reset == 1))
773 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600774 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600775 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600776 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600777
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700778 /* if skb do not have any tailroom for padding,
779 copy the skb into a new expanded skb */
780 if ((skb->len & 0x3) && (skb_tailroom(skb) < (4 - (skb->len & 0x3)))) {
781 /* revisit, probably dev_alloc_skb and memcpy is effecient */
782 new_skb = skb_copy_expand(skb, skb_headroom(skb),
783 4 - (skb->len & 0x3), GFP_ATOMIC);
784 if (new_skb == NULL) {
785 pr_err("%s: cannot allocate skb\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600786 goto write_fail;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700787 }
788 dev_kfree_skb_any(skb);
789 skb = new_skb;
790 DBG_INC_WRITE_CPY(skb->len);
791 }
792
793 hdr = (struct bam_mux_hdr *)skb_push(skb, sizeof(struct bam_mux_hdr));
794
795 /* caller should allocate for hdr and padding
796 hdr is fine, padding is tricky */
797 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
798 hdr->cmd = BAM_MUX_HDR_CMD_DATA;
799 hdr->reserved = 0;
800 hdr->ch_id = id;
801 hdr->pkt_len = skb->len - sizeof(struct bam_mux_hdr);
802 if (skb->len & 0x3)
803 skb_put(skb, 4 - (skb->len & 0x3));
804
805 hdr->pad_len = skb->len - (sizeof(struct bam_mux_hdr) + hdr->pkt_len);
806
807 DBG("%s: data %p, tail %p skb len %d pkt len %d pad len %d\n",
808 __func__, skb->data, skb->tail, skb->len,
809 hdr->pkt_len, hdr->pad_len);
810
811 pkt = kmalloc(sizeof(struct tx_pkt_info), GFP_ATOMIC);
812 if (pkt == NULL) {
813 pr_err("%s: mem alloc for tx_pkt_info failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600814 goto write_fail2;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700815 }
816
817 dma_address = dma_map_single(NULL, skb->data, skb->len,
818 DMA_TO_DEVICE);
819 if (!dma_address) {
820 pr_err("%s: dma_map_single() failed\n", __func__);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600821 goto write_fail3;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700822 }
823 pkt->skb = skb;
824 pkt->dma_address = dma_address;
825 pkt->is_cmd = 0;
Eric Holmberg878923a2012-01-10 14:28:19 -0700826 set_tx_timestamp(pkt);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700827 INIT_WORK(&pkt->work, bam_mux_write_done);
Jeff Hugo626303bf2011-11-21 11:43:28 -0700828 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600829 list_add_tail(&pkt->list_node, &bam_tx_pool);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700830 rc = sps_transfer_one(bam_tx_pipe, dma_address, skb->len,
831 pkt, SPS_IOVEC_FLAG_INT | SPS_IOVEC_FLAG_EOT);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600832 if (rc) {
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700833 DMUX_LOG_KERR("%s sps_transfer_one failed rc=%d\n",
834 __func__, rc);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600835 list_del(&pkt->list_node);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -0700836 DBG_INC_TX_SPS_FAILURE_CNT();
Jeff Hugo626303bf2011-11-21 11:43:28 -0700837 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmbergb5b08e52012-01-20 14:19:00 -0700838 dma_unmap_single(NULL, pkt->dma_address,
839 pkt->skb->len, DMA_TO_DEVICE);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600840 kfree(pkt);
Jeff Hugo872bd062011-11-15 17:47:21 -0700841 if (new_skb)
842 dev_kfree_skb_any(new_skb);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700843 } else {
Jeff Hugobb6da952012-01-16 15:02:42 -0700844 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700845 spin_lock_irqsave(&bam_ch[id].lock, flags);
846 bam_ch[id].num_tx_pkts++;
847 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Jeff Hugo7b80c802011-11-04 16:12:20 -0600848 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600849 ul_packet_written = 1;
850 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700851 return rc;
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600852
853write_fail3:
854 kfree(pkt);
855write_fail2:
Arun Kumar Neelakantam406e5692013-01-17 18:58:04 +0530856 skb_pull(skb, sizeof(struct bam_mux_hdr));
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600857 if (new_skb)
858 dev_kfree_skb_any(new_skb);
859write_fail:
860 read_unlock(&ul_wakeup_lock);
861 return -ENOMEM;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700862}
863
864int msm_bam_dmux_open(uint32_t id, void *priv,
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600865 void (*notify)(void *, int, unsigned long))
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700866{
867 struct bam_mux_hdr *hdr;
868 unsigned long flags;
869 int rc = 0;
870
871 DBG("%s: opening ch %d\n", __func__, id);
Eric Holmberg5d775432011-11-09 10:23:35 -0700872 if (!bam_mux_initialized) {
873 DBG("%s: not inititialized\n", __func__);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700874 return -ENODEV;
Eric Holmberg5d775432011-11-09 10:23:35 -0700875 }
876 if (id >= BAM_DMUX_NUM_CHANNELS) {
877 pr_err("%s: invalid channel id %d\n", __func__, id);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700878 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700879 }
880 if (notify == NULL) {
881 pr_err("%s: notify function is NULL\n", __func__);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600882 return -EINVAL;
Eric Holmberg5d775432011-11-09 10:23:35 -0700883 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700884
885 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_KERNEL);
886 if (hdr == NULL) {
887 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
888 return -ENOMEM;
889 }
890 spin_lock_irqsave(&bam_ch[id].lock, flags);
891 if (bam_ch_is_open(id)) {
892 DBG("%s: Already opened %d\n", __func__, id);
893 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
894 kfree(hdr);
895 goto open_done;
896 }
897 if (!bam_ch_is_remote_open(id)) {
898 DBG("%s: Remote not open; ch: %d\n", __func__, id);
899 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
900 kfree(hdr);
Eric Holmberg5d775432011-11-09 10:23:35 -0700901 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700902 }
903
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600904 bam_ch[id].notify = notify;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700905 bam_ch[id].priv = priv;
906 bam_ch[id].status |= BAM_CH_LOCAL_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700907 bam_ch[id].num_tx_pkts = 0;
908 bam_ch[id].use_wm = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700909 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
910
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600911 read_lock(&ul_wakeup_lock);
Jeff Hugo061ce672011-10-21 17:15:32 -0600912 if (!bam_is_connected) {
913 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600914 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700915 if (unlikely(in_global_reset == 1))
916 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600917 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600918 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600919 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600920
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700921 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
922 hdr->cmd = BAM_MUX_HDR_CMD_OPEN;
923 hdr->reserved = 0;
924 hdr->ch_id = id;
925 hdr->pkt_len = 0;
926 hdr->pad_len = 0;
927
928 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600929 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700930
931open_done:
932 DBG("%s: opened ch %d\n", __func__, id);
933 return rc;
934}
935
936int msm_bam_dmux_close(uint32_t id)
937{
938 struct bam_mux_hdr *hdr;
939 unsigned long flags;
940 int rc;
941
942 if (id >= BAM_DMUX_NUM_CHANNELS)
943 return -EINVAL;
944 DBG("%s: closing ch %d\n", __func__, id);
945 if (!bam_mux_initialized)
946 return -ENODEV;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700947
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600948 read_lock(&ul_wakeup_lock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600949 if (!bam_is_connected && !bam_ch_is_in_reset(id)) {
Jeff Hugo061ce672011-10-21 17:15:32 -0600950 read_unlock(&ul_wakeup_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600951 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -0700952 if (unlikely(in_global_reset == 1))
953 return -EFAULT;
Jeff Hugo061ce672011-10-21 17:15:32 -0600954 read_lock(&ul_wakeup_lock);
Jeff Hugod98b1082011-10-24 10:30:23 -0600955 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
Jeff Hugo061ce672011-10-21 17:15:32 -0600956 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600957
Jeff Hugo061ce672011-10-21 17:15:32 -0600958 spin_lock_irqsave(&bam_ch[id].lock, flags);
Jeff Hugo1c4531c2011-08-02 14:55:37 -0600959 bam_ch[id].notify = NULL;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700960 bam_ch[id].priv = NULL;
961 bam_ch[id].status &= ~BAM_CH_LOCAL_OPEN;
962 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
963
Jeff Hugo6e7a92a2011-10-24 05:25:13 -0600964 if (bam_ch_is_in_reset(id)) {
965 read_unlock(&ul_wakeup_lock);
966 bam_ch[id].status &= ~BAM_CH_IN_RESET;
967 return 0;
968 }
969
Jeff Hugobb5802f2011-11-02 17:10:29 -0600970 hdr = kmalloc(sizeof(struct bam_mux_hdr), GFP_ATOMIC);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700971 if (hdr == NULL) {
972 pr_err("%s: hdr kmalloc failed. ch: %d\n", __func__, id);
Jeff Hugoc6af54d2011-11-02 17:00:27 -0600973 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700974 return -ENOMEM;
975 }
976 hdr->magic_num = BAM_MUX_HDR_MAGIC_NO;
977 hdr->cmd = BAM_MUX_HDR_CMD_CLOSE;
978 hdr->reserved = 0;
979 hdr->ch_id = id;
980 hdr->pkt_len = 0;
981 hdr->pad_len = 0;
982
983 rc = bam_mux_write_cmd((void *)hdr, sizeof(struct bam_mux_hdr));
Jeff Hugoaab7ebc2011-09-07 16:46:04 -0600984 read_unlock(&ul_wakeup_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700985
986 DBG("%s: closed ch %d\n", __func__, id);
987 return rc;
988}
989
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -0700990int msm_bam_dmux_is_ch_full(uint32_t id)
991{
992 unsigned long flags;
993 int ret;
994
995 if (id >= BAM_DMUX_NUM_CHANNELS)
996 return -EINVAL;
997
998 spin_lock_irqsave(&bam_ch[id].lock, flags);
999 bam_ch[id].use_wm = 1;
1000 ret = bam_ch[id].num_tx_pkts >= HIGH_WATERMARK;
1001 DBG("%s: ch %d num tx pkts=%d, HWM=%d\n", __func__,
1002 id, bam_ch[id].num_tx_pkts, ret);
1003 if (!bam_ch_is_local_open(id)) {
1004 ret = -ENODEV;
1005 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
1006 }
1007 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
1008
1009 return ret;
1010}
1011
1012int msm_bam_dmux_is_ch_low(uint32_t id)
1013{
Eric Holmberged3ca0a2012-04-09 15:44:58 -06001014 unsigned long flags;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001015 int ret;
1016
1017 if (id >= BAM_DMUX_NUM_CHANNELS)
1018 return -EINVAL;
1019
Eric Holmberged3ca0a2012-04-09 15:44:58 -06001020 spin_lock_irqsave(&bam_ch[id].lock, flags);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001021 bam_ch[id].use_wm = 1;
1022 ret = bam_ch[id].num_tx_pkts <= LOW_WATERMARK;
1023 DBG("%s: ch %d num tx pkts=%d, LWM=%d\n", __func__,
1024 id, bam_ch[id].num_tx_pkts, ret);
1025 if (!bam_ch_is_local_open(id)) {
1026 ret = -ENODEV;
1027 pr_err("%s: port not open: %d\n", __func__, bam_ch[id].status);
1028 }
Eric Holmberged3ca0a2012-04-09 15:44:58 -06001029 spin_unlock_irqrestore(&bam_ch[id].lock, flags);
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001030
1031 return ret;
1032}
1033
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001034static void rx_switch_to_interrupt_mode(void)
1035{
1036 struct sps_connect cur_rx_conn;
1037 struct sps_iovec iov;
1038 struct rx_pkt_info *info;
1039 int ret;
1040
1041 /*
1042 * Attempt to enable interrupts - if this fails,
1043 * continue polling and we will retry later.
1044 */
1045 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
1046 if (ret) {
1047 pr_err("%s: sps_get_config() failed %d\n", __func__, ret);
1048 goto fail;
1049 }
1050
1051 rx_register_event.options = SPS_O_EOT;
1052 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
1053 if (ret) {
1054 pr_err("%s: sps_register_event() failed %d\n", __func__, ret);
1055 goto fail;
1056 }
1057
1058 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
1059 SPS_O_EOT | SPS_O_ACK_TRANSFERS;
1060 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
1061 if (ret) {
1062 pr_err("%s: sps_set_config() failed %d\n", __func__, ret);
1063 goto fail;
1064 }
1065 polling_mode = 0;
Eric Holmberg006057d2012-01-11 10:10:42 -07001066 release_wakelock();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001067
1068 /* handle any rx packets before interrupt was enabled */
1069 while (bam_connection_is_active && !polling_mode) {
1070 ret = sps_get_iovec(bam_rx_pipe, &iov);
1071 if (ret) {
1072 pr_err("%s: sps_get_iovec failed %d\n",
1073 __func__, ret);
1074 break;
1075 }
1076 if (iov.addr == 0)
1077 break;
1078
1079 mutex_lock(&bam_rx_pool_mutexlock);
1080 if (unlikely(list_empty(&bam_rx_pool))) {
Eric Holmberg00cf8692012-07-16 14:21:19 -06001081 DMUX_LOG_KERR("%s: have iovec %p but rx pool empty\n",
1082 __func__, (void *)iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001083 mutex_unlock(&bam_rx_pool_mutexlock);
1084 continue;
1085 }
1086 info = list_first_entry(&bam_rx_pool, struct rx_pkt_info,
1087 list_node);
Eric Holmberg00cf8692012-07-16 14:21:19 -06001088 if (info->dma_address != iov.addr) {
1089 DMUX_LOG_KERR("%s: iovec %p != dma %p\n",
1090 __func__,
1091 (void *)iov.addr,
1092 (void *)info->dma_address);
1093 list_for_each_entry(info, &bam_rx_pool, list_node) {
1094 DMUX_LOG_KERR("%s: dma %p\n", __func__,
1095 (void *)info->dma_address);
1096 if (iov.addr == info->dma_address)
1097 break;
1098 }
1099 }
1100 BUG_ON(info->dma_address != iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001101 list_del(&info->list_node);
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001102 --bam_rx_pool_len;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001103 mutex_unlock(&bam_rx_pool_mutexlock);
1104 handle_bam_mux_cmd(&info->work);
1105 }
1106 return;
1107
1108fail:
1109 pr_err("%s: reverting to polling\n", __func__);
Jeff Hugofff43af92012-03-29 17:54:52 -06001110 queue_work_on(0, bam_mux_rx_workqueue, &rx_timer_work);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001111}
1112
Jeff Hugo949080a2011-08-30 11:58:56 -06001113static void rx_timer_work_func(struct work_struct *work)
1114{
1115 struct sps_iovec iov;
Jeff Hugo949080a2011-08-30 11:58:56 -06001116 struct rx_pkt_info *info;
1117 int inactive_cycles = 0;
1118 int ret;
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001119 u32 buffs_unused, buffs_used;
Jeff Hugo949080a2011-08-30 11:58:56 -06001120
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001121 while (bam_connection_is_active) { /* timer loop */
Jeff Hugo949080a2011-08-30 11:58:56 -06001122 ++inactive_cycles;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001123 while (bam_connection_is_active) { /* deplete queue loop */
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001124 if (in_global_reset)
1125 return;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001126
1127 ret = sps_get_iovec(bam_rx_pipe, &iov);
1128 if (ret) {
1129 pr_err("%s: sps_get_iovec failed %d\n",
1130 __func__, ret);
1131 break;
1132 }
Jeff Hugo949080a2011-08-30 11:58:56 -06001133 if (iov.addr == 0)
1134 break;
1135 inactive_cycles = 0;
Jeff Hugoc9749932011-11-02 17:50:40 -06001136 mutex_lock(&bam_rx_pool_mutexlock);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001137 if (unlikely(list_empty(&bam_rx_pool))) {
Eric Holmberg00cf8692012-07-16 14:21:19 -06001138 DMUX_LOG_KERR(
1139 "%s: have iovec %p but rx pool empty\n",
1140 __func__, (void *)iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001141 mutex_unlock(&bam_rx_pool_mutexlock);
1142 continue;
1143 }
1144 info = list_first_entry(&bam_rx_pool,
1145 struct rx_pkt_info, list_node);
Eric Holmberg00cf8692012-07-16 14:21:19 -06001146 if (info->dma_address != iov.addr) {
1147 DMUX_LOG_KERR("%s: iovec %p != dma %p\n",
1148 __func__,
1149 (void *)iov.addr,
1150 (void *)info->dma_address);
1151 list_for_each_entry(info, &bam_rx_pool,
1152 list_node) {
1153 DMUX_LOG_KERR("%s: dma %p\n", __func__,
1154 (void *)info->dma_address);
1155 if (iov.addr == info->dma_address)
1156 break;
1157 }
1158 }
1159 BUG_ON(info->dma_address != iov.addr);
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001160 list_del(&info->list_node);
Eric Holmberg00cf8692012-07-16 14:21:19 -06001161 --bam_rx_pool_len;
Jeff Hugoc9749932011-11-02 17:50:40 -06001162 mutex_unlock(&bam_rx_pool_mutexlock);
Jeff Hugo949080a2011-08-30 11:58:56 -06001163 handle_bam_mux_cmd(&info->work);
1164 }
1165
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001166 if (inactive_cycles >= POLLING_INACTIVITY) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001167 rx_switch_to_interrupt_mode();
1168 break;
Jeff Hugo949080a2011-08-30 11:58:56 -06001169 }
1170
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07001171 if (bam_adaptive_timer_enabled) {
1172 usleep_range(rx_timer_interval, rx_timer_interval + 50);
1173
1174 ret = sps_get_unused_desc_num(bam_rx_pipe,
1175 &buffs_unused);
1176
1177 if (ret) {
1178 pr_err("%s: error getting num buffers unused after sleep\n",
1179 __func__);
1180
1181 break;
1182 }
1183
1184 buffs_used = NUM_BUFFERS - buffs_unused;
1185
1186 if (buffs_unused == 0) {
1187 rx_timer_interval = MIN_POLLING_SLEEP;
1188 } else {
1189 if (buffs_used > 0) {
1190 rx_timer_interval =
1191 (2 * NUM_BUFFERS *
1192 rx_timer_interval)/
1193 (3 * buffs_used);
1194 } else {
1195 rx_timer_interval =
1196 MAX_POLLING_SLEEP;
1197 }
1198 }
1199
1200 if (rx_timer_interval > MAX_POLLING_SLEEP)
1201 rx_timer_interval = MAX_POLLING_SLEEP;
1202 else if (rx_timer_interval < MIN_POLLING_SLEEP)
1203 rx_timer_interval = MIN_POLLING_SLEEP;
1204 } else {
1205 usleep_range(POLLING_MIN_SLEEP, POLLING_MAX_SLEEP);
1206 }
Jeff Hugo949080a2011-08-30 11:58:56 -06001207 }
1208}
1209
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001210static void bam_mux_tx_notify(struct sps_event_notify *notify)
1211{
1212 struct tx_pkt_info *pkt;
1213
1214 DBG("%s: event %d notified\n", __func__, notify->event_id);
1215
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001216 if (in_global_reset)
1217 return;
1218
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001219 switch (notify->event_id) {
1220 case SPS_EVENT_EOT:
1221 pkt = notify->data.transfer.user;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001222 if (!pkt->is_cmd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001223 dma_unmap_single(NULL, pkt->dma_address,
1224 pkt->skb->len,
1225 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001226 else
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001227 dma_unmap_single(NULL, pkt->dma_address,
1228 pkt->len,
1229 DMA_TO_DEVICE);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001230 queue_work(bam_mux_tx_workqueue, &pkt->work);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001231 break;
1232 default:
1233 pr_err("%s: recieved unexpected event id %d\n", __func__,
1234 notify->event_id);
1235 }
1236}
1237
Jeff Hugo33dbc002011-08-25 15:52:53 -06001238static void bam_mux_rx_notify(struct sps_event_notify *notify)
1239{
Jeff Hugo949080a2011-08-30 11:58:56 -06001240 int ret;
1241 struct sps_connect cur_rx_conn;
Jeff Hugo33dbc002011-08-25 15:52:53 -06001242
1243 DBG("%s: event %d notified\n", __func__, notify->event_id);
1244
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001245 if (in_global_reset)
1246 return;
1247
Jeff Hugo33dbc002011-08-25 15:52:53 -06001248 switch (notify->event_id) {
1249 case SPS_EVENT_EOT:
Jeff Hugo949080a2011-08-30 11:58:56 -06001250 /* attempt to disable interrupts in this pipe */
1251 if (!polling_mode) {
1252 ret = sps_get_config(bam_rx_pipe, &cur_rx_conn);
1253 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001254 pr_err("%s: sps_get_config() failed %d, interrupts"
1255 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001256 break;
1257 }
Jeff Hugoa9d32ba2011-11-21 14:59:48 -07001258 cur_rx_conn.options = SPS_O_AUTO_ENABLE |
Jeff Hugo949080a2011-08-30 11:58:56 -06001259 SPS_O_ACK_TRANSFERS | SPS_O_POLL;
1260 ret = sps_set_config(bam_rx_pipe, &cur_rx_conn);
1261 if (ret) {
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001262 pr_err("%s: sps_set_config() failed %d, interrupts"
1263 " not disabled\n", __func__, ret);
Jeff Hugo949080a2011-08-30 11:58:56 -06001264 break;
1265 }
Eric Holmberg006057d2012-01-11 10:10:42 -07001266 grab_wakelock();
Jeff Hugo949080a2011-08-30 11:58:56 -06001267 polling_mode = 1;
Jeff Hugofff43af92012-03-29 17:54:52 -06001268 /*
1269 * run on core 0 so that netif_rx() in rmnet uses only
1270 * one queue
1271 */
1272 queue_work_on(0, bam_mux_rx_workqueue, &rx_timer_work);
Jeff Hugo949080a2011-08-30 11:58:56 -06001273 }
Jeff Hugo33dbc002011-08-25 15:52:53 -06001274 break;
1275 default:
1276 pr_err("%s: recieved unexpected event id %d\n", __func__,
1277 notify->event_id);
1278 }
1279}
1280
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001281#ifdef CONFIG_DEBUG_FS
1282
1283static int debug_tbl(char *buf, int max)
1284{
1285 int i = 0;
1286 int j;
1287
1288 for (j = 0; j < BAM_DMUX_NUM_CHANNELS; ++j) {
1289 i += scnprintf(buf + i, max - i,
1290 "ch%02d local open=%s remote open=%s\n",
1291 j, bam_ch_is_local_open(j) ? "Y" : "N",
1292 bam_ch_is_remote_open(j) ? "Y" : "N");
1293 }
1294
1295 return i;
1296}
1297
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001298static int debug_ul_pkt_cnt(char *buf, int max)
1299{
1300 struct list_head *p;
1301 unsigned long flags;
1302 int n = 0;
1303
1304 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
1305 __list_for_each(p, &bam_tx_pool) {
1306 ++n;
1307 }
1308 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
1309
1310 return scnprintf(buf, max, "Number of UL packets in flight: %d\n", n);
1311}
1312
1313static int debug_stats(char *buf, int max)
1314{
1315 int i = 0;
1316
1317 i += scnprintf(buf + i, max - i,
Eric Holmberg9fdef262012-02-14 11:46:05 -07001318 "skb read cnt: %u\n"
1319 "skb write cnt: %u\n"
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001320 "skb copy cnt: %u\n"
1321 "skb copy bytes: %u\n"
Eric Holmberg6074aba2012-01-18 17:59:44 -07001322 "sps tx failures: %u\n"
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001323 "sps tx stalls: %u\n"
Eric Holmberg1f1255d2012-02-22 13:37:21 -07001324 "rx queue len: %d\n"
1325 "a2 ack out cnt: %d\n"
1326 "a2 ack in cnt: %d\n"
1327 "a2 pwr cntl in: %d\n",
Eric Holmberg9fdef262012-02-14 11:46:05 -07001328 bam_dmux_read_cnt,
1329 bam_dmux_write_cnt,
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001330 bam_dmux_write_cpy_cnt,
1331 bam_dmux_write_cpy_bytes,
Eric Holmberg6074aba2012-01-18 17:59:44 -07001332 bam_dmux_tx_sps_failure_cnt,
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001333 bam_dmux_tx_stall_cnt,
Eric Holmberg1f1255d2012-02-22 13:37:21 -07001334 bam_rx_pool_len,
1335 atomic_read(&bam_dmux_ack_out_cnt),
1336 atomic_read(&bam_dmux_ack_in_cnt),
1337 atomic_read(&bam_dmux_a2_pwr_cntl_in_cnt)
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07001338 );
1339
1340 return i;
1341}
1342
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001343#define DEBUG_BUFMAX 4096
1344static char debug_buffer[DEBUG_BUFMAX];
1345
1346static ssize_t debug_read(struct file *file, char __user *buf,
1347 size_t count, loff_t *ppos)
1348{
1349 int (*fill)(char *buf, int max) = file->private_data;
1350 int bsize = fill(debug_buffer, DEBUG_BUFMAX);
1351 return simple_read_from_buffer(buf, count, ppos, debug_buffer, bsize);
1352}
1353
1354static int debug_open(struct inode *inode, struct file *file)
1355{
1356 file->private_data = inode->i_private;
1357 return 0;
1358}
1359
1360
1361static const struct file_operations debug_ops = {
1362 .read = debug_read,
1363 .open = debug_open,
1364};
1365
1366static void debug_create(const char *name, mode_t mode,
1367 struct dentry *dent,
1368 int (*fill)(char *buf, int max))
1369{
Eric Holmberge4ac80b2012-01-12 09:21:59 -07001370 struct dentry *file;
1371
1372 file = debugfs_create_file(name, mode, dent, fill, &debug_ops);
1373 if (IS_ERR(file))
1374 pr_err("%s: debugfs create failed %d\n", __func__,
1375 (int)PTR_ERR(file));
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001376}
1377
1378#endif
1379
Jeff Hugod98b1082011-10-24 10:30:23 -06001380static void notify_all(int event, unsigned long data)
1381{
1382 int i;
Jeff Hugocb798022012-04-09 14:55:40 -06001383 struct list_head *temp;
1384 struct outside_notify_func *func;
Jeff Hugod98b1082011-10-24 10:30:23 -06001385
1386 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
Eric Holmberg454d9da2012-01-12 09:37:14 -07001387 if (bam_ch_is_open(i)) {
Jeff Hugod98b1082011-10-24 10:30:23 -06001388 bam_ch[i].notify(bam_ch[i].priv, event, data);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301389 BAM_DMUX_LOG("%s: cid=%d, event=%d, data=%lu\n",
Eric Holmberg454d9da2012-01-12 09:37:14 -07001390 __func__, i, event, data);
1391 }
Jeff Hugod98b1082011-10-24 10:30:23 -06001392 }
Jeff Hugocb798022012-04-09 14:55:40 -06001393
1394 __list_for_each(temp, &bam_other_notify_funcs) {
1395 func = container_of(temp, struct outside_notify_func,
1396 list_node);
1397 func->notify(func->priv, event, data);
1398 }
Jeff Hugod98b1082011-10-24 10:30:23 -06001399}
1400
1401static void kickoff_ul_wakeup_func(struct work_struct *work)
1402{
1403 read_lock(&ul_wakeup_lock);
1404 if (!bam_is_connected) {
1405 read_unlock(&ul_wakeup_lock);
1406 ul_wakeup();
Jeff Hugo4838f412012-01-20 11:19:37 -07001407 if (unlikely(in_global_reset == 1))
1408 return;
Jeff Hugod98b1082011-10-24 10:30:23 -06001409 read_lock(&ul_wakeup_lock);
1410 ul_packet_written = 1;
1411 notify_all(BAM_DMUX_UL_CONNECTED, (unsigned long)(NULL));
1412 }
1413 read_unlock(&ul_wakeup_lock);
1414}
1415
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001416int msm_bam_dmux_kickoff_ul_wakeup(void)
Jeff Hugod98b1082011-10-24 10:30:23 -06001417{
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001418 int is_connected;
1419
1420 read_lock(&ul_wakeup_lock);
1421 ul_packet_written = 1;
1422 is_connected = bam_is_connected;
1423 if (!is_connected)
1424 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
1425 read_unlock(&ul_wakeup_lock);
1426
1427 return is_connected;
Jeff Hugod98b1082011-10-24 10:30:23 -06001428}
1429
Eric Holmberg878923a2012-01-10 14:28:19 -07001430static void power_vote(int vote)
1431{
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301432 BAM_DMUX_LOG("%s: curr=%d, vote=%d\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -07001433 bam_dmux_uplink_vote, vote);
1434
1435 if (bam_dmux_uplink_vote == vote)
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301436 BAM_DMUX_LOG("%s: warning - duplicate power vote\n", __func__);
Eric Holmberg878923a2012-01-10 14:28:19 -07001437
1438 bam_dmux_uplink_vote = vote;
1439 if (vote)
1440 smsm_change_state(SMSM_APPS_STATE, 0, SMSM_A2_POWER_CONTROL);
1441 else
1442 smsm_change_state(SMSM_APPS_STATE, SMSM_A2_POWER_CONTROL, 0);
1443}
1444
Eric Holmberg454d9da2012-01-12 09:37:14 -07001445/*
1446 * @note: Must be called with ul_wakeup_lock locked.
1447 */
1448static inline void ul_powerdown(void)
1449{
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301450 BAM_DMUX_LOG("%s: powerdown\n", __func__);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001451 verify_tx_queue_is_empty(__func__);
1452
1453 if (a2_pc_disabled) {
1454 wait_for_dfab = 1;
1455 INIT_COMPLETION(dfab_unvote_completion);
1456 release_wakelock();
1457 } else {
1458 wait_for_ack = 1;
1459 INIT_COMPLETION(ul_wakeup_ack_completion);
1460 power_vote(0);
1461 }
1462 bam_is_connected = 0;
1463 notify_all(BAM_DMUX_UL_DISCONNECTED, (unsigned long)(NULL));
1464}
1465
1466static inline void ul_powerdown_finish(void)
1467{
1468 if (a2_pc_disabled && wait_for_dfab) {
1469 unvote_dfab();
1470 complete_all(&dfab_unvote_completion);
1471 wait_for_dfab = 0;
1472 }
1473}
1474
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001475/*
1476 * Votes for UL power and returns current power state.
1477 *
1478 * @returns true if currently connected
1479 */
1480int msm_bam_dmux_ul_power_vote(void)
1481{
1482 int is_connected;
1483
1484 read_lock(&ul_wakeup_lock);
1485 atomic_inc(&ul_ondemand_vote);
1486 is_connected = bam_is_connected;
1487 if (!is_connected)
1488 queue_work(bam_mux_tx_workqueue, &kickoff_ul_wakeup);
1489 read_unlock(&ul_wakeup_lock);
1490
1491 return is_connected;
1492}
1493
1494/*
1495 * Unvotes for UL power.
1496 *
1497 * @returns true if vote count is 0 (UL shutdown possible)
1498 */
1499int msm_bam_dmux_ul_power_unvote(void)
1500{
1501 int vote;
1502
1503 read_lock(&ul_wakeup_lock);
1504 vote = atomic_dec_return(&ul_ondemand_vote);
1505 if (unlikely(vote) < 0)
1506 DMUX_LOG_KERR("%s: invalid power vote %d\n", __func__, vote);
1507 read_unlock(&ul_wakeup_lock);
1508
1509 return vote == 0;
1510}
1511
Jeff Hugocb798022012-04-09 14:55:40 -06001512int msm_bam_dmux_reg_notify(void *priv,
1513 void (*notify)(void *priv, int event_type,
1514 unsigned long data))
1515{
1516 struct outside_notify_func *func;
1517
1518 if (!notify)
1519 return -EINVAL;
1520
1521 func = kmalloc(sizeof(struct outside_notify_func), GFP_KERNEL);
1522 if (!func)
1523 return -ENOMEM;
1524
1525 func->notify = notify;
1526 func->priv = priv;
1527 list_add(&func->list_node, &bam_other_notify_funcs);
1528
1529 return 0;
1530}
1531
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001532static void ul_timeout(struct work_struct *work)
1533{
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001534 unsigned long flags;
1535 int ret;
1536
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001537 if (in_global_reset)
1538 return;
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001539 ret = write_trylock_irqsave(&ul_wakeup_lock, flags);
1540 if (!ret) { /* failed to grab lock, reschedule and bail */
1541 schedule_delayed_work(&ul_timeout_work,
1542 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1543 return;
1544 }
Eric Holmberg454d9da2012-01-12 09:37:14 -07001545 if (bam_is_connected) {
Eric Holmberg6074aba2012-01-18 17:59:44 -07001546 if (!ul_packet_written) {
1547 spin_lock(&bam_tx_pool_spinlock);
1548 if (!list_empty(&bam_tx_pool)) {
1549 struct tx_pkt_info *info;
1550
1551 info = list_first_entry(&bam_tx_pool,
1552 struct tx_pkt_info, list_node);
1553 DMUX_LOG_KERR("%s: UL delayed ts=%u.%09lu\n",
1554 __func__, info->ts_sec, info->ts_nsec);
1555 DBG_INC_TX_STALL_CNT();
1556 ul_packet_written = 1;
1557 }
1558 spin_unlock(&bam_tx_pool_spinlock);
1559 }
1560
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001561 if (ul_packet_written || atomic_read(&ul_ondemand_vote)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301562 BAM_DMUX_LOG("%s: pkt written %d\n",
Eric Holmbergbc9f21c2012-01-18 11:33:33 -07001563 __func__, ul_packet_written);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001564 ul_packet_written = 0;
1565 schedule_delayed_work(&ul_timeout_work,
1566 msecs_to_jiffies(UL_TIMEOUT_DELAY));
Eric Holmberg006057d2012-01-11 10:10:42 -07001567 } else {
Eric Holmberg454d9da2012-01-12 09:37:14 -07001568 ul_powerdown();
Eric Holmberg006057d2012-01-11 10:10:42 -07001569 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001570 }
Jeff Hugoc040a5b2011-11-15 14:26:01 -07001571 write_unlock_irqrestore(&ul_wakeup_lock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001572 ul_powerdown_finish();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001573}
Jeff Hugo4838f412012-01-20 11:19:37 -07001574
1575static int ssrestart_check(void)
1576{
Eric Holmberg90285e22012-02-22 12:33:05 -07001577 DMUX_LOG_KERR("%s: modem timeout: BAM DMUX disabled\n", __func__);
1578 in_global_reset = 1;
1579 if (get_restart_level() <= RESET_SOC)
1580 DMUX_LOG_KERR("%s: ssrestart not enabled\n", __func__);
1581 return 1;
Jeff Hugo4838f412012-01-20 11:19:37 -07001582}
1583
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001584static void ul_wakeup(void)
1585{
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001586 int ret;
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001587 int do_vote_dfab = 0;
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001588
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001589 mutex_lock(&wakeup_lock);
1590 if (bam_is_connected) { /* bam got connected before lock grabbed */
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301591 BAM_DMUX_LOG("%s Already awake\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001592 mutex_unlock(&wakeup_lock);
1593 return;
1594 }
Eric Holmberg878923a2012-01-10 14:28:19 -07001595
Jeff Hugoc2696142012-05-03 11:42:13 -06001596 /*
Jeff Hugof5001732012-08-27 13:19:09 -06001597 * if this gets hit, that means restart_notifier_cb() has started
1598 * but probably not finished, thus we know SSR has happened, but
1599 * haven't been able to send that info to our clients yet.
1600 * in that case, abort the ul_wakeup() so that we don't undo any
1601 * work restart_notifier_cb() has done. The clients will be notified
1602 * shortly. No cleanup necessary (reschedule the wakeup) as our and
1603 * their SSR handling will cover it
1604 */
1605 if (unlikely(in_global_reset == 1)) {
1606 mutex_unlock(&wakeup_lock);
1607 return;
1608 }
1609
1610 /*
Jeff Hugoc2696142012-05-03 11:42:13 -06001611 * if someone is voting for UL before bam is inited (modem up first
1612 * time), set flag for init to kickoff ul wakeup once bam is inited
1613 */
1614 mutex_lock(&delayed_ul_vote_lock);
1615 if (unlikely(!bam_mux_initialized)) {
1616 need_delayed_ul_vote = 1;
1617 mutex_unlock(&delayed_ul_vote_lock);
1618 mutex_unlock(&wakeup_lock);
1619 return;
1620 }
1621 mutex_unlock(&delayed_ul_vote_lock);
1622
Eric Holmberg006057d2012-01-11 10:10:42 -07001623 if (a2_pc_disabled) {
1624 /*
1625 * don't grab the wakelock the first time because it is
1626 * already grabbed when a2 powers on
1627 */
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001628 if (likely(a2_pc_disabled_wakelock_skipped)) {
Eric Holmberg006057d2012-01-11 10:10:42 -07001629 grab_wakelock();
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001630 do_vote_dfab = 1; /* vote must occur after wait */
1631 } else {
Jeff Hugo583a6da2012-02-03 11:37:30 -07001632 a2_pc_disabled_wakelock_skipped = 1;
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001633 }
Eric Holmberg006057d2012-01-11 10:10:42 -07001634 if (wait_for_dfab) {
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001635 ret = wait_for_completion_timeout(
Eric Holmberg006057d2012-01-11 10:10:42 -07001636 &dfab_unvote_completion, HZ);
1637 BUG_ON(ret == 0);
1638 }
Jeff Hugo5f57ec92012-05-14 13:34:28 -06001639 if (likely(do_vote_dfab))
1640 vote_dfab();
Eric Holmberg006057d2012-01-11 10:10:42 -07001641 schedule_delayed_work(&ul_timeout_work,
1642 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1643 bam_is_connected = 1;
1644 mutex_unlock(&wakeup_lock);
1645 return;
1646 }
1647
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001648 /*
1649 * must wait for the previous power down request to have been acked
1650 * chances are it already came in and this will just fall through
1651 * instead of waiting
1652 */
1653 if (wait_for_ack) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301654 BAM_DMUX_LOG("%s waiting for previous ack\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001655 ret = wait_for_completion_timeout(
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001656 &ul_wakeup_ack_completion, HZ);
Eric Holmberg006057d2012-01-11 10:10:42 -07001657 wait_for_ack = 0;
Jeff Hugo4838f412012-01-20 11:19:37 -07001658 if (unlikely(ret == 0) && ssrestart_check()) {
1659 mutex_unlock(&wakeup_lock);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301660 BAM_DMUX_LOG("%s timeout previous ack\n", __func__);
Jeff Hugo4838f412012-01-20 11:19:37 -07001661 return;
1662 }
Jeff Hugof6c1c1e2011-12-01 17:43:49 -07001663 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001664 INIT_COMPLETION(ul_wakeup_ack_completion);
Eric Holmberg878923a2012-01-10 14:28:19 -07001665 power_vote(1);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301666 BAM_DMUX_LOG("%s waiting for wakeup ack\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001667 ret = wait_for_completion_timeout(&ul_wakeup_ack_completion, HZ);
Jeff Hugo4838f412012-01-20 11:19:37 -07001668 if (unlikely(ret == 0) && ssrestart_check()) {
1669 mutex_unlock(&wakeup_lock);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301670 BAM_DMUX_LOG("%s timeout wakeup ack\n", __func__);
Jeff Hugo4838f412012-01-20 11:19:37 -07001671 return;
1672 }
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301673 BAM_DMUX_LOG("%s waiting completion\n", __func__);
Jeff Hugo66f7f1e2012-01-16 14:30:42 -07001674 ret = wait_for_completion_timeout(&bam_connection_completion, HZ);
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 power on\n", __func__);
Jeff Hugo4838f412012-01-20 11:19:37 -07001678 return;
1679 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001680
1681 bam_is_connected = 1;
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301682 BAM_DMUX_LOG("%s complete\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001683 schedule_delayed_work(&ul_timeout_work,
1684 msecs_to_jiffies(UL_TIMEOUT_DELAY));
1685 mutex_unlock(&wakeup_lock);
1686}
1687
1688static void reconnect_to_bam(void)
1689{
1690 int i;
1691
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001692 in_global_reset = 0;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001693 vote_dfab();
Jeff Hugo18792a32012-06-20 15:25:55 -06001694 if (!power_management_only_mode) {
1695 i = sps_device_reset(a2_device_handle);
1696 if (i)
1697 pr_err("%s: device reset failed rc = %d\n", __func__,
1698 i);
1699 i = sps_connect(bam_tx_pipe, &tx_connection);
1700 if (i)
1701 pr_err("%s: tx connection failed rc = %d\n", __func__,
1702 i);
1703 i = sps_connect(bam_rx_pipe, &rx_connection);
1704 if (i)
1705 pr_err("%s: rx connection failed rc = %d\n", __func__,
1706 i);
1707 i = sps_register_event(bam_tx_pipe, &tx_register_event);
1708 if (i)
1709 pr_err("%s: tx event reg failed rc = %d\n", __func__,
1710 i);
1711 i = sps_register_event(bam_rx_pipe, &rx_register_event);
1712 if (i)
1713 pr_err("%s: rx event reg failed rc = %d\n", __func__,
1714 i);
1715 }
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001716
1717 bam_connection_is_active = 1;
1718
1719 if (polling_mode)
1720 rx_switch_to_interrupt_mode();
1721
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001722 toggle_apps_ack();
1723 complete_all(&bam_connection_completion);
Jeff Hugo18792a32012-06-20 15:25:55 -06001724 if (!power_management_only_mode)
1725 queue_rx();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001726}
1727
1728static void disconnect_to_bam(void)
1729{
1730 struct list_head *node;
1731 struct rx_pkt_info *info;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001732 unsigned long flags;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001733
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001734 bam_connection_is_active = 0;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001735
1736 /* handle disconnect during active UL */
1737 write_lock_irqsave(&ul_wakeup_lock, flags);
1738 if (bam_is_connected) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301739 BAM_DMUX_LOG("%s: UL active - forcing powerdown\n", __func__);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001740 ul_powerdown();
1741 }
1742 write_unlock_irqrestore(&ul_wakeup_lock, flags);
1743 ul_powerdown_finish();
1744
1745 /* tear down BAM connection */
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001746 INIT_COMPLETION(bam_connection_completion);
Jeff Hugo18792a32012-06-20 15:25:55 -06001747 if (!power_management_only_mode) {
1748 sps_disconnect(bam_tx_pipe);
1749 sps_disconnect(bam_rx_pipe);
1750 __memzero(rx_desc_mem_buf.base, rx_desc_mem_buf.size);
1751 __memzero(tx_desc_mem_buf.base, tx_desc_mem_buf.size);
1752 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001753 unvote_dfab();
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001754
1755 mutex_lock(&bam_rx_pool_mutexlock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001756 while (!list_empty(&bam_rx_pool)) {
1757 node = bam_rx_pool.next;
1758 list_del(node);
1759 info = container_of(node, struct rx_pkt_info, list_node);
1760 dma_unmap_single(NULL, info->dma_address, BUFFER_SIZE,
1761 DMA_FROM_DEVICE);
1762 dev_kfree_skb_any(info->skb);
1763 kfree(info);
1764 }
Eric Holmbergb5b08e52012-01-20 14:19:00 -07001765 bam_rx_pool_len = 0;
Eric Holmberg8df0cdb2012-01-04 17:40:46 -07001766 mutex_unlock(&bam_rx_pool_mutexlock);
Eric Holmberg878923a2012-01-10 14:28:19 -07001767
Jeff Hugo0b13a352012-03-17 23:18:30 -06001768 if (disconnect_ack)
1769 toggle_apps_ack();
1770
Eric Holmberg878923a2012-01-10 14:28:19 -07001771 verify_tx_queue_is_empty(__func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001772}
1773
1774static void vote_dfab(void)
1775{
Jeff Hugoca0caa82011-12-05 16:05:23 -07001776 int rc;
1777
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301778 BAM_DMUX_LOG("%s\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001779 mutex_lock(&dfab_status_lock);
1780 if (dfab_is_on) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301781 BAM_DMUX_LOG("%s: dfab is already on\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001782 mutex_unlock(&dfab_status_lock);
1783 return;
1784 }
Jeff Hugod0befde2012-08-09 15:32:49 -06001785 if (dfab_clk) {
1786 rc = clk_prepare_enable(dfab_clk);
1787 if (rc)
1788 DMUX_LOG_KERR("bam_dmux vote for dfab failed rc = %d\n",
1789 rc);
1790 }
1791 if (xo_clk) {
1792 rc = clk_prepare_enable(xo_clk);
1793 if (rc)
1794 DMUX_LOG_KERR("bam_dmux vote for xo failed rc = %d\n",
1795 rc);
1796 }
Eric Holmberg006057d2012-01-11 10:10:42 -07001797 dfab_is_on = 1;
1798 mutex_unlock(&dfab_status_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001799}
1800
1801static void unvote_dfab(void)
1802{
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301803 BAM_DMUX_LOG("%s\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07001804 mutex_lock(&dfab_status_lock);
1805 if (!dfab_is_on) {
1806 DMUX_LOG_KERR("%s: dfab is already off\n", __func__);
1807 dump_stack();
1808 mutex_unlock(&dfab_status_lock);
1809 return;
1810 }
Jeff Hugod0befde2012-08-09 15:32:49 -06001811 if (dfab_clk)
1812 clk_disable_unprepare(dfab_clk);
1813 if (xo_clk)
1814 clk_disable_unprepare(xo_clk);
Eric Holmberg006057d2012-01-11 10:10:42 -07001815 dfab_is_on = 0;
1816 mutex_unlock(&dfab_status_lock);
1817}
1818
1819/* reference counting wrapper around wakelock */
1820static void grab_wakelock(void)
1821{
1822 unsigned long flags;
1823
1824 spin_lock_irqsave(&wakelock_reference_lock, flags);
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301825 BAM_DMUX_LOG("%s: ref count = %d\n", __func__,
Eric Holmberg006057d2012-01-11 10:10:42 -07001826 wakelock_reference_count);
1827 if (wakelock_reference_count == 0)
1828 wake_lock(&bam_wakelock);
1829 ++wakelock_reference_count;
1830 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1831}
1832
1833static void release_wakelock(void)
1834{
1835 unsigned long flags;
1836
1837 spin_lock_irqsave(&wakelock_reference_lock, flags);
1838 if (wakelock_reference_count == 0) {
1839 DMUX_LOG_KERR("%s: bam_dmux wakelock not locked\n", __func__);
1840 dump_stack();
1841 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
1842 return;
1843 }
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301844 BAM_DMUX_LOG("%s: ref count = %d\n", __func__,
Eric Holmberg006057d2012-01-11 10:10:42 -07001845 wakelock_reference_count);
1846 --wakelock_reference_count;
1847 if (wakelock_reference_count == 0)
1848 wake_unlock(&bam_wakelock);
1849 spin_unlock_irqrestore(&wakelock_reference_lock, flags);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001850}
1851
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001852static int restart_notifier_cb(struct notifier_block *this,
1853 unsigned long code,
1854 void *data)
1855{
1856 int i;
1857 struct list_head *node;
1858 struct tx_pkt_info *info;
1859 int temp_remote_status;
Jeff Hugo626303bf2011-11-21 11:43:28 -07001860 unsigned long flags;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001861
1862 if (code != SUBSYS_AFTER_SHUTDOWN)
1863 return NOTIFY_DONE;
1864
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301865 BAM_DMUX_LOG("%s: begin\n", __func__);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001866 in_global_reset = 1;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001867
1868 /* Handle uplink Powerdown */
1869 write_lock_irqsave(&ul_wakeup_lock, flags);
1870 if (bam_is_connected) {
1871 ul_powerdown();
1872 wait_for_ack = 0;
1873 }
Jeff Hugo4838f412012-01-20 11:19:37 -07001874 /*
1875 * if modem crash during ul_wakeup(), power_vote is 1, needs to be
1876 * reset to 0. harmless if bam_is_connected check above passes
1877 */
1878 power_vote(0);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001879 write_unlock_irqrestore(&ul_wakeup_lock, flags);
1880 ul_powerdown_finish();
Eric Holmberg006057d2012-01-11 10:10:42 -07001881 a2_pc_disabled = 0;
Jeff Hugo583a6da2012-02-03 11:37:30 -07001882 a2_pc_disabled_wakelock_skipped = 0;
Jeff Hugof62029d2012-07-17 13:39:53 -06001883 disconnect_ack = 1;
Eric Holmberg454d9da2012-01-12 09:37:14 -07001884
1885 /* Cleanup Channel States */
Eric Holmberga623da82012-07-12 09:37:09 -06001886 mutex_lock(&bam_pdev_mutexlock);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001887 for (i = 0; i < BAM_DMUX_NUM_CHANNELS; ++i) {
1888 temp_remote_status = bam_ch_is_remote_open(i);
1889 bam_ch[i].status &= ~BAM_CH_REMOTE_OPEN;
Karthikeyan Ramasubramanian7bf5ca82011-11-21 13:33:19 -07001890 bam_ch[i].num_tx_pkts = 0;
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001891 if (bam_ch_is_local_open(i))
1892 bam_ch[i].status |= BAM_CH_IN_RESET;
1893 if (temp_remote_status) {
1894 platform_device_unregister(bam_ch[i].pdev);
1895 bam_ch[i].pdev = platform_device_alloc(
1896 bam_ch[i].name, 2);
1897 }
1898 }
Eric Holmberga623da82012-07-12 09:37:09 -06001899 mutex_unlock(&bam_pdev_mutexlock);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001900
1901 /* Cleanup pending UL data */
Jeff Hugo626303bf2011-11-21 11:43:28 -07001902 spin_lock_irqsave(&bam_tx_pool_spinlock, flags);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001903 while (!list_empty(&bam_tx_pool)) {
1904 node = bam_tx_pool.next;
1905 list_del(node);
1906 info = container_of(node, struct tx_pkt_info,
1907 list_node);
1908 if (!info->is_cmd) {
1909 dma_unmap_single(NULL, info->dma_address,
1910 info->skb->len,
1911 DMA_TO_DEVICE);
1912 dev_kfree_skb_any(info->skb);
1913 } else {
1914 dma_unmap_single(NULL, info->dma_address,
1915 info->len,
1916 DMA_TO_DEVICE);
1917 kfree(info->skb);
1918 }
1919 kfree(info);
1920 }
Jeff Hugo626303bf2011-11-21 11:43:28 -07001921 spin_unlock_irqrestore(&bam_tx_pool_spinlock, flags);
Eric Holmberg454d9da2012-01-12 09:37:14 -07001922
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05301923 BAM_DMUX_LOG("%s: complete\n", __func__);
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06001924 return NOTIFY_DONE;
1925}
1926
Jeff Hugo9dea05c2011-12-21 12:23:05 -07001927static int bam_init(void)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001928{
1929 u32 h;
1930 dma_addr_t dma_addr;
1931 int ret;
1932 void *a2_virt_addr;
Jeff Hugo4b2890d2012-01-16 16:14:21 -07001933 int skip_iounmap = 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001934
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001935 vote_dfab();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001936 /* init BAM */
Jeff Hugo3910ee12012-08-21 14:08:20 -06001937 a2_virt_addr = ioremap_nocache((unsigned long)(a2_phys_base),
1938 a2_phys_size);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001939 if (!a2_virt_addr) {
1940 pr_err("%s: ioremap failed\n", __func__);
1941 ret = -ENOMEM;
Jeff Hugo994a92d2012-01-05 13:25:21 -07001942 goto ioremap_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001943 }
Jeff Hugo3910ee12012-08-21 14:08:20 -06001944 a2_props.phys_addr = (u32)(a2_phys_base);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001945 a2_props.virt_addr = a2_virt_addr;
Jeff Hugo3910ee12012-08-21 14:08:20 -06001946 a2_props.virt_size = a2_phys_size;
1947 a2_props.irq = a2_bam_irq;
Jeff Hugo927cba62011-11-11 11:49:52 -07001948 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001949 a2_props.num_pipes = A2_NUM_PIPES;
1950 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
Jeff Hugo75913c82011-12-05 15:59:01 -07001951 if (cpu_is_msm9615())
1952 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001953 /* need to free on tear down */
1954 ret = sps_register_bam_device(&a2_props, &h);
1955 if (ret < 0) {
1956 pr_err("%s: register bam error %d\n", __func__, ret);
1957 goto register_bam_failed;
1958 }
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06001959 a2_device_handle = h;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001960
1961 bam_tx_pipe = sps_alloc_endpoint();
1962 if (bam_tx_pipe == NULL) {
1963 pr_err("%s: tx alloc endpoint failed\n", __func__);
1964 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07001965 goto tx_alloc_endpoint_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001966 }
1967 ret = sps_get_config(bam_tx_pipe, &tx_connection);
1968 if (ret) {
1969 pr_err("%s: tx get config failed %d\n", __func__, ret);
1970 goto tx_get_config_failed;
1971 }
1972
1973 tx_connection.source = SPS_DEV_HANDLE_MEM;
1974 tx_connection.src_pipe_index = 0;
1975 tx_connection.destination = h;
1976 tx_connection.dest_pipe_index = 4;
1977 tx_connection.mode = SPS_MODE_DEST;
1978 tx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT;
1979 tx_desc_mem_buf.size = 0x800; /* 2k */
1980 tx_desc_mem_buf.base = dma_alloc_coherent(NULL, tx_desc_mem_buf.size,
1981 &dma_addr, 0);
1982 if (tx_desc_mem_buf.base == NULL) {
1983 pr_err("%s: tx memory alloc failed\n", __func__);
1984 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07001985 goto tx_get_config_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001986 }
1987 tx_desc_mem_buf.phys_base = dma_addr;
1988 memset(tx_desc_mem_buf.base, 0x0, tx_desc_mem_buf.size);
1989 tx_connection.desc = tx_desc_mem_buf;
1990 tx_connection.event_thresh = 0x10;
1991
1992 ret = sps_connect(bam_tx_pipe, &tx_connection);
1993 if (ret < 0) {
1994 pr_err("%s: tx connect error %d\n", __func__, ret);
1995 goto tx_connect_failed;
1996 }
1997
1998 bam_rx_pipe = sps_alloc_endpoint();
1999 if (bam_rx_pipe == NULL) {
2000 pr_err("%s: rx alloc endpoint failed\n", __func__);
2001 ret = -ENOMEM;
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002002 goto rx_alloc_endpoint_failed;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002003 }
2004 ret = sps_get_config(bam_rx_pipe, &rx_connection);
2005 if (ret) {
2006 pr_err("%s: rx get config failed %d\n", __func__, ret);
2007 goto rx_get_config_failed;
2008 }
2009
2010 rx_connection.source = h;
2011 rx_connection.src_pipe_index = 5;
2012 rx_connection.destination = SPS_DEV_HANDLE_MEM;
2013 rx_connection.dest_pipe_index = 1;
2014 rx_connection.mode = SPS_MODE_SRC;
Jeff Hugo949080a2011-08-30 11:58:56 -06002015 rx_connection.options = SPS_O_AUTO_ENABLE | SPS_O_EOT |
2016 SPS_O_ACK_TRANSFERS;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002017 rx_desc_mem_buf.size = 0x800; /* 2k */
2018 rx_desc_mem_buf.base = dma_alloc_coherent(NULL, rx_desc_mem_buf.size,
2019 &dma_addr, 0);
2020 if (rx_desc_mem_buf.base == NULL) {
2021 pr_err("%s: rx memory alloc failed\n", __func__);
2022 ret = -ENOMEM;
2023 goto rx_mem_failed;
2024 }
2025 rx_desc_mem_buf.phys_base = dma_addr;
2026 memset(rx_desc_mem_buf.base, 0x0, rx_desc_mem_buf.size);
2027 rx_connection.desc = rx_desc_mem_buf;
2028 rx_connection.event_thresh = 0x10;
2029
2030 ret = sps_connect(bam_rx_pipe, &rx_connection);
2031 if (ret < 0) {
2032 pr_err("%s: rx connect error %d\n", __func__, ret);
2033 goto rx_connect_failed;
2034 }
2035
2036 tx_register_event.options = SPS_O_EOT;
2037 tx_register_event.mode = SPS_TRIGGER_CALLBACK;
2038 tx_register_event.xfer_done = NULL;
2039 tx_register_event.callback = bam_mux_tx_notify;
2040 tx_register_event.user = NULL;
2041 ret = sps_register_event(bam_tx_pipe, &tx_register_event);
2042 if (ret < 0) {
2043 pr_err("%s: tx register event error %d\n", __func__, ret);
2044 goto rx_event_reg_failed;
2045 }
2046
Jeff Hugo33dbc002011-08-25 15:52:53 -06002047 rx_register_event.options = SPS_O_EOT;
2048 rx_register_event.mode = SPS_TRIGGER_CALLBACK;
2049 rx_register_event.xfer_done = NULL;
2050 rx_register_event.callback = bam_mux_rx_notify;
2051 rx_register_event.user = NULL;
2052 ret = sps_register_event(bam_rx_pipe, &rx_register_event);
2053 if (ret < 0) {
2054 pr_err("%s: tx register event error %d\n", __func__, ret);
2055 goto rx_event_reg_failed;
2056 }
2057
Jeff Hugoc2696142012-05-03 11:42:13 -06002058 mutex_lock(&delayed_ul_vote_lock);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002059 bam_mux_initialized = 1;
Jeff Hugoc2696142012-05-03 11:42:13 -06002060 if (need_delayed_ul_vote) {
2061 need_delayed_ul_vote = 0;
2062 msm_bam_dmux_kickoff_ul_wakeup();
2063 }
2064 mutex_unlock(&delayed_ul_vote_lock);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002065 toggle_apps_ack();
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002066 bam_connection_is_active = 1;
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002067 complete_all(&bam_connection_completion);
Jeff Hugo2fb555e2012-03-14 16:33:47 -06002068 queue_rx();
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002069 return 0;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002070
2071rx_event_reg_failed:
2072 sps_disconnect(bam_rx_pipe);
2073rx_connect_failed:
2074 dma_free_coherent(NULL, rx_desc_mem_buf.size, rx_desc_mem_buf.base,
2075 rx_desc_mem_buf.phys_base);
2076rx_mem_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002077rx_get_config_failed:
2078 sps_free_endpoint(bam_rx_pipe);
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002079rx_alloc_endpoint_failed:
2080 sps_disconnect(bam_tx_pipe);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002081tx_connect_failed:
2082 dma_free_coherent(NULL, tx_desc_mem_buf.size, tx_desc_mem_buf.base,
2083 tx_desc_mem_buf.phys_base);
2084tx_get_config_failed:
2085 sps_free_endpoint(bam_tx_pipe);
Jeff Hugo8ff4a812012-01-17 11:03:13 -07002086tx_alloc_endpoint_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002087 sps_deregister_bam_device(h);
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002088 /*
2089 * sps_deregister_bam_device() calls iounmap. calling iounmap on the
2090 * same handle below will cause a crash, so skip it if we've freed
2091 * the handle here.
2092 */
2093 skip_iounmap = 1;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002094register_bam_failed:
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002095 if (!skip_iounmap)
2096 iounmap(a2_virt_addr);
Jeff Hugo994a92d2012-01-05 13:25:21 -07002097ioremap_failed:
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002098 /*destroy_workqueue(bam_mux_workqueue);*/
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002099 return ret;
2100}
2101
2102static int bam_init_fallback(void)
2103{
2104 u32 h;
2105 int ret;
2106 void *a2_virt_addr;
2107
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002108 /* init BAM */
Jeff Hugo3910ee12012-08-21 14:08:20 -06002109 a2_virt_addr = ioremap_nocache((unsigned long)(a2_phys_base),
2110 a2_phys_size);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002111 if (!a2_virt_addr) {
2112 pr_err("%s: ioremap failed\n", __func__);
2113 ret = -ENOMEM;
2114 goto ioremap_failed;
2115 }
Jeff Hugo3910ee12012-08-21 14:08:20 -06002116 a2_props.phys_addr = (u32)(a2_phys_base);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002117 a2_props.virt_addr = a2_virt_addr;
Jeff Hugo3910ee12012-08-21 14:08:20 -06002118 a2_props.virt_size = a2_phys_size;
2119 a2_props.irq = a2_bam_irq;
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002120 a2_props.options = SPS_BAM_OPT_IRQ_WAKEUP;
2121 a2_props.num_pipes = A2_NUM_PIPES;
2122 a2_props.summing_threshold = A2_SUMMING_THRESHOLD;
2123 if (cpu_is_msm9615())
2124 a2_props.manage = SPS_BAM_MGR_DEVICE_REMOTE;
2125 ret = sps_register_bam_device(&a2_props, &h);
2126 if (ret < 0) {
2127 pr_err("%s: register bam error %d\n", __func__, ret);
2128 goto register_bam_failed;
2129 }
2130 a2_device_handle = h;
Jeff Hugoc2696142012-05-03 11:42:13 -06002131
2132 mutex_lock(&delayed_ul_vote_lock);
2133 bam_mux_initialized = 1;
2134 if (need_delayed_ul_vote) {
2135 need_delayed_ul_vote = 0;
2136 msm_bam_dmux_kickoff_ul_wakeup();
2137 }
2138 mutex_unlock(&delayed_ul_vote_lock);
Jeff Hugo2bec9772012-04-05 12:25:16 -06002139 toggle_apps_ack();
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002140
Jeff Hugo18792a32012-06-20 15:25:55 -06002141 power_management_only_mode = 1;
2142 bam_connection_is_active = 1;
2143 complete_all(&bam_connection_completion);
2144
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002145 return 0;
2146
2147register_bam_failed:
Jeff Hugo4b2890d2012-01-16 16:14:21 -07002148 iounmap(a2_virt_addr);
Jeff Hugo9dea05c2011-12-21 12:23:05 -07002149ioremap_failed:
2150 return ret;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002151}
Jeff Hugoade1f842011-08-03 15:53:59 -06002152
Jeff Hugoa670b762012-03-15 15:58:28 -06002153static void msm9615_bam_init(void)
Eric Holmberg604ab252012-01-15 00:01:18 -07002154{
2155 int ret = 0;
2156
2157 ret = bam_init();
2158 if (ret) {
2159 ret = bam_init_fallback();
2160 if (ret)
2161 pr_err("%s: bam init fallback failed: %d",
2162 __func__, ret);
2163 }
2164}
2165
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002166static void toggle_apps_ack(void)
2167{
2168 static unsigned int clear_bit; /* 0 = set the bit, else clear bit */
Eric Holmberg878923a2012-01-10 14:28:19 -07002169
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302170 BAM_DMUX_LOG("%s: apps ack %d->%d\n", __func__,
Eric Holmberg878923a2012-01-10 14:28:19 -07002171 clear_bit & 0x1, ~clear_bit & 0x1);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002172 smsm_change_state(SMSM_APPS_STATE,
2173 clear_bit & SMSM_A2_POWER_CONTROL_ACK,
2174 ~clear_bit & SMSM_A2_POWER_CONTROL_ACK);
2175 clear_bit = ~clear_bit;
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002176 DBG_INC_ACK_OUT_CNT();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002177}
2178
Jeff Hugoade1f842011-08-03 15:53:59 -06002179static void bam_dmux_smsm_cb(void *priv, uint32_t old_state, uint32_t new_state)
2180{
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002181 static int last_processed_state;
2182
2183 mutex_lock(&smsm_cb_lock);
Eric Holmberg878923a2012-01-10 14:28:19 -07002184 bam_dmux_power_state = new_state & SMSM_A2_POWER_CONTROL ? 1 : 0;
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002185 DBG_INC_A2_POWER_CONTROL_IN_CNT();
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302186 BAM_DMUX_LOG("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
Eric Holmberg878923a2012-01-10 14:28:19 -07002187 new_state);
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002188 if (last_processed_state == (new_state & SMSM_A2_POWER_CONTROL)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302189 BAM_DMUX_LOG("%s: already processed this state\n", __func__);
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002190 mutex_unlock(&smsm_cb_lock);
2191 return;
2192 }
2193
2194 last_processed_state = new_state & SMSM_A2_POWER_CONTROL;
Eric Holmberg878923a2012-01-10 14:28:19 -07002195
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002196 if (bam_mux_initialized && new_state & SMSM_A2_POWER_CONTROL) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302197 BAM_DMUX_LOG("%s: reconnect\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07002198 grab_wakelock();
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002199 reconnect_to_bam();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002200 } else if (bam_mux_initialized &&
2201 !(new_state & SMSM_A2_POWER_CONTROL)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302202 BAM_DMUX_LOG("%s: disconnect\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002203 disconnect_to_bam();
Eric Holmberg006057d2012-01-11 10:10:42 -07002204 release_wakelock();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002205 } else if (new_state & SMSM_A2_POWER_CONTROL) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302206 BAM_DMUX_LOG("%s: init\n", __func__);
Eric Holmberg006057d2012-01-11 10:10:42 -07002207 grab_wakelock();
Jeff Hugoa670b762012-03-15 15:58:28 -06002208 if (cpu_is_msm9615())
2209 msm9615_bam_init();
2210 else
Eric Holmberg604ab252012-01-15 00:01:18 -07002211 bam_init();
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002212 } else {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302213 BAM_DMUX_LOG("%s: bad state change\n", __func__);
Jeff Hugoade1f842011-08-03 15:53:59 -06002214 pr_err("%s: unsupported state change\n", __func__);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002215 }
Jeff Hugo4b7c7b32012-04-18 16:25:14 -06002216 mutex_unlock(&smsm_cb_lock);
Jeff Hugoade1f842011-08-03 15:53:59 -06002217
2218}
2219
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002220static void bam_dmux_smsm_ack_cb(void *priv, uint32_t old_state,
2221 uint32_t new_state)
2222{
Eric Holmberg1f1255d2012-02-22 13:37:21 -07002223 DBG_INC_ACK_IN_CNT();
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302224 BAM_DMUX_LOG("%s: 0x%08x -> 0x%08x\n", __func__, old_state,
Eric Holmberg878923a2012-01-10 14:28:19 -07002225 new_state);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002226 complete_all(&ul_wakeup_ack_completion);
2227}
2228
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002229static int bam_dmux_probe(struct platform_device *pdev)
2230{
2231 int rc;
Jeff Hugo3910ee12012-08-21 14:08:20 -06002232 struct resource *r;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002233
2234 DBG("%s probe called\n", __func__);
2235 if (bam_mux_initialized)
2236 return 0;
2237
Jeff Hugo3910ee12012-08-21 14:08:20 -06002238 if (pdev->dev.of_node) {
2239 r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2240 if (!r) {
2241 pr_err("%s: reg field missing\n", __func__);
2242 return -ENODEV;
2243 }
2244 a2_phys_base = (void *)(r->start);
2245 a2_phys_size = (uint32_t)(resource_size(r));
2246 a2_bam_irq = platform_get_irq(pdev, 0);
2247 if (a2_bam_irq == -ENXIO) {
2248 pr_err("%s: irq field missing\n", __func__);
2249 return -ENODEV;
2250 }
2251 DBG("%s: base:%p size:%x irq:%d\n", __func__,
2252 a2_phys_base,
2253 a2_phys_size,
2254 a2_bam_irq);
2255 } else { /* fallback to default init data */
2256 a2_phys_base = (void *)(A2_PHYS_BASE);
2257 a2_phys_size = A2_PHYS_SIZE;
2258 a2_bam_irq = A2_BAM_IRQ;
2259 }
2260
Stephen Boyd69d35e32012-02-14 15:33:30 -08002261 xo_clk = clk_get(&pdev->dev, "xo");
2262 if (IS_ERR(xo_clk)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302263 BAM_DMUX_LOG("%s: did not get xo clock\n", __func__);
Jeff Hugod0befde2012-08-09 15:32:49 -06002264 xo_clk = NULL;
Stephen Boyd69d35e32012-02-14 15:33:30 -08002265 }
Stephen Boyd1c51a492011-10-26 12:11:47 -07002266 dfab_clk = clk_get(&pdev->dev, "bus_clk");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002267 if (IS_ERR(dfab_clk)) {
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302268 BAM_DMUX_LOG("%s: did not get dfab clock\n", __func__);
Jeff Hugod0befde2012-08-09 15:32:49 -06002269 dfab_clk = NULL;
2270 } else {
2271 rc = clk_set_rate(dfab_clk, 64000000);
2272 if (rc)
2273 pr_err("%s: unable to set dfab clock rate\n", __func__);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002274 }
2275
Jeff Hugofff43af92012-03-29 17:54:52 -06002276 /*
2277 * setup the workqueue so that it can be pinned to core 0 and not
2278 * block the watchdog pet function, so that netif_rx() in rmnet
2279 * only uses one queue.
2280 */
2281 bam_mux_rx_workqueue = alloc_workqueue("bam_dmux_rx",
2282 WQ_MEM_RECLAIM | WQ_CPU_INTENSIVE, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002283 if (!bam_mux_rx_workqueue)
2284 return -ENOMEM;
2285
2286 bam_mux_tx_workqueue = create_singlethread_workqueue("bam_dmux_tx");
2287 if (!bam_mux_tx_workqueue) {
2288 destroy_workqueue(bam_mux_rx_workqueue);
2289 return -ENOMEM;
2290 }
2291
Jeff Hugo7960abd2011-08-02 15:39:38 -06002292 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002293 spin_lock_init(&bam_ch[rc].lock);
Jeff Hugo7960abd2011-08-02 15:39:38 -06002294 scnprintf(bam_ch[rc].name, BAM_DMUX_CH_NAME_MAX_LEN,
2295 "bam_dmux_ch_%d", rc);
2296 /* bus 2, ie a2 stream 2 */
2297 bam_ch[rc].pdev = platform_device_alloc(bam_ch[rc].name, 2);
2298 if (!bam_ch[rc].pdev) {
2299 pr_err("%s: platform device alloc failed\n", __func__);
2300 destroy_workqueue(bam_mux_rx_workqueue);
2301 destroy_workqueue(bam_mux_tx_workqueue);
2302 return -ENOMEM;
2303 }
2304 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002305
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002306 init_completion(&ul_wakeup_ack_completion);
2307 init_completion(&bam_connection_completion);
Eric Holmberg006057d2012-01-11 10:10:42 -07002308 init_completion(&dfab_unvote_completion);
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002309 INIT_DELAYED_WORK(&ul_timeout_work, ul_timeout);
Jeff Hugo988e7ba2012-10-03 15:53:54 -06002310 INIT_DELAYED_WORK(&queue_rx_work, queue_rx_work_func);
Jeff Hugoae3a85e2011-12-02 17:10:18 -07002311 wake_lock_init(&bam_wakelock, WAKE_LOCK_SUSPEND, "bam_dmux_wakelock");
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002312
Jeff Hugoade1f842011-08-03 15:53:59 -06002313 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL,
2314 bam_dmux_smsm_cb, NULL);
2315
2316 if (rc) {
2317 destroy_workqueue(bam_mux_rx_workqueue);
2318 destroy_workqueue(bam_mux_tx_workqueue);
2319 pr_err("%s: smsm cb register failed, rc: %d\n", __func__, rc);
2320 return -ENOMEM;
2321 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002322
Jeff Hugoaab7ebc2011-09-07 16:46:04 -06002323 rc = smsm_state_cb_register(SMSM_MODEM_STATE, SMSM_A2_POWER_CONTROL_ACK,
2324 bam_dmux_smsm_ack_cb, NULL);
2325
2326 if (rc) {
2327 destroy_workqueue(bam_mux_rx_workqueue);
2328 destroy_workqueue(bam_mux_tx_workqueue);
2329 smsm_state_cb_deregister(SMSM_MODEM_STATE,
2330 SMSM_A2_POWER_CONTROL,
2331 bam_dmux_smsm_cb, NULL);
2332 pr_err("%s: smsm ack cb register failed, rc: %d\n", __func__,
2333 rc);
2334 for (rc = 0; rc < BAM_DMUX_NUM_CHANNELS; ++rc)
2335 platform_device_put(bam_ch[rc].pdev);
2336 return -ENOMEM;
2337 }
2338
Eric Holmbergfd1e2ae2011-11-15 18:28:17 -07002339 if (smsm_get_state(SMSM_MODEM_STATE) & SMSM_A2_POWER_CONTROL)
2340 bam_dmux_smsm_cb(NULL, 0, smsm_get_state(SMSM_MODEM_STATE));
2341
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002342 return 0;
2343}
2344
Jeff Hugo3910ee12012-08-21 14:08:20 -06002345static struct of_device_id msm_match_table[] = {
2346 {.compatible = "qcom,bam_dmux"},
2347 {},
2348};
2349
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002350static struct platform_driver bam_dmux_driver = {
2351 .probe = bam_dmux_probe,
2352 .driver = {
2353 .name = "BAM_RMNT",
2354 .owner = THIS_MODULE,
Jeff Hugo3910ee12012-08-21 14:08:20 -06002355 .of_match_table = msm_match_table,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002356 },
2357};
2358
2359static int __init bam_dmux_init(void)
2360{
2361#ifdef CONFIG_DEBUG_FS
2362 struct dentry *dent;
2363
2364 dent = debugfs_create_dir("bam_dmux", 0);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002365 if (!IS_ERR(dent)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002366 debug_create("tbl", 0444, dent, debug_tbl);
Eric Holmberg2fddbcd2011-11-28 18:25:57 -07002367 debug_create("ul_pkt_cnt", 0444, dent, debug_ul_pkt_cnt);
2368 debug_create("stats", 0444, dent, debug_stats);
2369 }
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002370#endif
Zaheerulla Meer6fbf32c2013-01-31 17:06:44 +05302371
2372 bam_ipc_log_txt = ipc_log_context_create(BAM_IPC_LOG_PAGES, "bam_dmux");
2373 if (!bam_ipc_log_txt) {
2374 pr_err("%s : unable to create IPC Logging Context", __func__);
Eric Holmberg878923a2012-01-10 14:28:19 -07002375 }
2376
Anurag Singhdcd8b4e2012-07-30 16:46:37 -07002377 rx_timer_interval = DEFAULT_POLLING_MIN_SLEEP;
2378
Jeff Hugo6e7a92a2011-10-24 05:25:13 -06002379 subsys_notif_register_notifier("modem", &restart_notifier);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002380 return platform_driver_register(&bam_dmux_driver);
2381}
2382
Jeff Hugoade1f842011-08-03 15:53:59 -06002383late_initcall(bam_dmux_init); /* needs to init after SMD */
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07002384MODULE_DESCRIPTION("MSM BAM DMUX");
2385MODULE_LICENSE("GPL v2");